hbutils.reflection.clazz

Overview:

Useful functions for processing python classes and types.

class_wraps

hbutils.reflection.clazz.class_wraps(wrapped: type, assigned: Tuple[str] = '__module__', '__name__', '__qualname__', '__doc__', '__annotations__', updated: Tuple[str] = ())[source]
Overview:

Wrapper decorator for class.

Arguments:
  • wrapped (type): Wrapped class.

  • assigned (Tuple[str]): Wrapper assignments, equal to functools.wraps()’s

    WRAPPER_ASSIGNMENTS.

  • updated (Tuple[str]): Wrapper updates, default is (), no update will be done.

Examples:
>>> def cls_dec(clazz):
>>>     @class_wraps(clazz)
>>>     class _NewClazz(clazz):
>>>         pass
>>>
>>>     return _NewClazz

common_base

hbutils.reflection.clazz.common_base(cls: type, *clss: type) → type[source]
Overview:

Get common base class of the given classes. Only __base__ is considered.

Arguments:
  • cls (type): First class.

  • clss (type): Other classes.

Returns:
  • base (type): Common base class.

Examples::
>>> from hbutils.reflection import common_base
>>> common_base(object)
<class 'object'>
>>> common_base(object, int, str)
<class 'object'>
>>> common_base(RuntimeError, ValueError, KeyError)
<class 'Exception'>

asitems

hbutils.reflection.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.reflection.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.reflection.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.reflection.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.reflection.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