hbutils.model.clazz

Overview:

Useful functions for build class models.

asitems

hbutils.model.clazz.asitems(items: Iterable[str])[source]
Overview:

Define fields in the class level.

Arguments:
  • items (Iterable[str]): Field name items.

Returns:
  • decorator: Decorator to decorate the given class.

Examples::
>>> @visual()
>>> @constructor(doc='''
>>>     Overview:
>>>         This is the constructor of class :class:`T`.
>>> ''')
>>> @asitems(['x', 'y'])
>>> class T:
>>>     @property
>>>     def x(self):
>>>         return self.__x
>>>
>>>     @property
>>>     def y(self):
>>>         return self.__y

visual

hbutils.model.clazz.visual(items: Optional[Iterable] = None, show_id: bool = False)[source]
Overview:

Decorate class to be visible by repr.

Arguments:
  • items (Optional[Iterable]): Items to be displayed. Default is None, which means automatically find the private fields and display them.

  • show_id (bool): Show hex id in representation string or not.

Returns:
  • decorator: Decorator to decorate the given class.

Examples::
>>> @visual()
>>> class T:
...     def __init__(self, x, y):
...         self.__x = x
...         self.__y = y
>>> repr(T(1, 2))
<T x: 1, y: 2>

constructor

hbutils.model.clazz.constructor(params: Optional[Iterable] = None, doc: Optional[str] = None)[source]
Overview:

Decorate class to create a init function.

Arguments:
  • params (Optional[Iterable]): Parameters of constructor, should be a iterator of items,

    each item should be a single string or a tuple of string and default value. Default is None, which means no arguments.

  • doc (Optional[str]): Documentation of constructor function.

Returns:
  • decorator: Decorator to decorate the given class.

Examples::
>>> @constructor(['x', ('y', 2)], '''
>>>     Overview:
>>>         This is the constructor of class :class:`T`.
>>> ''')
>>> class T:
>>>     # the same as:
>>>     # def __init__(self, x, y=2):
>>>     #     self.__x = x
>>>     #     self.__y = y
>>>
>>>     @property
>>>     def x(self):
>>>         return self.__x
>>>
>>>     @property
>>>     def y(self):
>>>         return self.__y

hasheq

hbutils.model.clazz.hasheq(items: Optional[Iterable] = None)[source]
Overview:

Decorate class to be visible by repr.

Arguments:
  • items (Optional[Iterable]): Items to be hashed and compared. Default is None, which means automatically find the private fields and display them.

Returns:
  • decorator: Decorator to decorate the given class.

Examples::
>>> @hasheq(['x', 'y'])
>>> class T:
>>>     def __init__(self, x, y):
>>>         self.__x = x
>>>         self.__y = y

Using with asitems()

>>> @hasheq()
>>> @constructor()
>>> @asitems(['x', 'y'])
>>> class T1:
>>>     pass

accessor

hbutils.model.clazz.accessor(items: Optional[Iterable] = None, readonly: bool = False)[source]
Overview:

Decorate class to be accessible by the accessors.

Arguments:
  • items (Optional[Iterable]): Items to be hashed and compared. Default is None, which means automatically find the private fields and display them.

  • readonly (bool): Default readonly or not. Default is False, which means make the accessor be writable when rw option is not given.

Returns:
  • decorator: Decorator to decorate the given class.

Examples::
>>> @accessor(readonly=True)
>>> @asitems(['x', 'y'])
>>> class T:
>>>     def __init__(self, x, y):
>>>         self.__x = x
>>>         self.__y = y
>>>
>>> t = T(2, 100)
>>> t.x  # 2
>>> t.y  # 100