class MusicService(Service): __nirum_service_methods__ = { 'get_music_by_artist_name': { 'artist_name': str, '_return': typing.Sequence[str], '_names': NameDict([('artist_name', 'artist_name')]) }, 'incorrect_return': { '_return': str, '_names': NameDict([]) }, 'get_artist_by_music': { 'music': str, '_return': str, '_names': NameDict([('music', 'norae')]) } } __nirum_method_names__ = NameDict([ ('get_music_by_artist_name', 'get_music_by_artist_name'), ('incorrect_return', 'incorrect_return'), ('get_artist_by_music', 'find_artist'), ]) def get_music_by_artist_name(self, artist_name: str) -> typing.Sequence[str]: raise NotImplementedError('get_music_by_artist_name') def incorrect_return(self) -> str: raise NotImplementedError('incorrect_return') def get_artist_by_music(self, music: str) -> str: raise NotImplementedError('get_artist_by_music')
class Point(object): __slots__ = ('left', 'top') __nirum_record_behind_name__ = 'point' __nirum_field_types__ = {'left': Offset, 'top': Offset} __nirum_field_names__ = NameDict([('left', 'x')]) def __init__(self, left, top): self.left = left self.top = top validate_record_type(self) def __repr__(self): return '{0.__module__}.{0.__qualname__}({1})'.format( type(self), ', '.join('{}={}'.format(attr, getattr(self, attr)) for attr in self.__slots__)) def __eq__(self, other): return isinstance(other, Point) and all( getattr(self, attr) == getattr(other, attr) for attr in self.__slots__) def __nirum_serialize__(self): return serialize_record_type(self) @classmethod def __nirum_deserialize__(cls, values): return deserialize_record_type(cls, values) def __hash__(self): return hash((self.__class__, self.left, self.top))
class Circle(Shape): __slots__ = ( 'origin', 'radius' ) __nirum_tag__ = Shape.Tag.circle __nirum_tag_types__ = { 'origin': Point, 'radius': Offset } __nirum_tag_names__ = NameDict([]) def __init__(self, origin: Point, radius: Offset) -> None: self.origin = origin self.radius = radius validate_union_type(self) def __repr__(self) -> str: return '{0.__module__}.{0.__qualname__}({1})'.format( type(self), ', '.join('{}={}'.format(attr, getattr(self, attr)) for attr in self.__slots__) ) def __eq__(self, other) -> bool: return isinstance(other, Circle) and all( getattr(self, attr) == getattr(other, attr) for attr in self.__slots__ )
class Rectangle(Shape): __slots__ = ( 'upper_left', 'lower_right' ) __nirum_tag__ = Shape.Tag.rectangle __nirum_tag_types__ = { 'upper_left': Point, 'lower_right': Point } __nirum_tag_names__ = NameDict([]) def __init__(self, upper_left: Point, lower_right: Point) -> None: self.upper_left = upper_left self.lower_right = lower_right validate_union_type(self) def __repr__(self) -> str: return '{0.__module__}.{0.__qualname__}({1})'.format( type(self), ', '.join('{}={}'.format(attr, getattr(self, attr)) for attr in self.__slots__) ) def __eq__(self, other) -> bool: return isinstance(other, Rectangle) and all( getattr(self, attr) == getattr(other, attr) for attr in self.__slots__ )
class Shape: __nirum_union_behind_name__ = 'shape' __nirum_field_names__ = NameDict([ ]) class Tag(enum.Enum): rectangle = 'rectangle' circle = 'circle' def __init__(self, *args, **kwargs): raise NotImplementedError( "{0.__module__}.{0.__qualname__} cannot be instantiated " "since it is an abstract class. Instantiate a concrete subtype " "of it instead.".format( type(self) ) ) def __nirum_serialize__(self) -> typing.Mapping[str, typing.Any]: pass @classmethod def __nirum_deserialize__(cls: type, value) -> 'Shape': pass
def test_name_dict(): nd = NameDict([('left', 'x'), ('right', 'right')]) assert nd['left'] == 'x' assert nd['right'] == 'right' assert nd.behind_names['right'] == 'right' assert len(nd) == 2 assert set(list(nd)) == set(['left', 'right']) with raises(KeyError): nd['top'] with raises(KeyError): nd.behind_names['left']
class MusicService(Service): __nirum_service_methods__ = { 'get_music_by_artist_name': { 'artist_name': text_type, '_return': typing.Sequence[text_type], '_names': NameDict([('artist_name', 'artist_name')]) }, 'incorrect_return': { '_return': text_type, '_names': NameDict([]) }, 'get_artist_by_music': { 'music': text_type, '_return': text_type, '_names': NameDict([('music', 'norae')]) }, 'raise_application_error_request': { '_return': text_type, '_names': NameDict([]) }, } __nirum_method_names__ = NameDict([ ('get_music_by_artist_name', 'get_music_by_artist_name'), ('incorrect_return', 'incorrect_return'), ('get_artist_by_music', 'find_artist'), ('raise_application_error_request', 'raise_application_error_request'), ]) def get_music_by_artist_name(self, artist_name): raise NotImplementedError('get_music_by_artist_name') def incorrect_return(self): raise NotImplementedError('incorrect_return') def get_artist_by_music(self, music): raise NotImplementedError('get_artist_by_music') def raise_application_error_request(self): raise NotImplementedError('raise_application_error_request')
def test_name_dict_assert(): with raises(AssertionError): NameDict([('left', 'x'), ('right', 'x')]) with raises(AssertionError): NameDict([('left', 'x'), ('left', 'y')])