mate#
This module implements:
- class attrs_mate.mate.AttrsClass[source]#
A mixins class provideing more utility methods.
- to_OrderedDict() OrderedDict[str, Any] [source]#
Convert to ordered dictionary.
- classmethod from_dict(dct_or_obj: Optional[Union[Dict[str, Any], AttrsClass]]) Optional[AttrsClass] [source]#
Construct an instance from dictionary data.
- classmethod from_list(list_of_dct_or_obj: List[Optional[Union[Dict[str, Any], AttrsClass]]]) List[Optional[AttrsClass]] [source]#
Construct list of instance from list of dictionary data.
- classmethod from_mapper(map_of_dct_or_obj: Optional[Dict[str, Optional[Union[Dict[str, Any], AttrsClass]]]]) Optional[Dict[str, Optional[Union[Dict[str, Any], AttrsClass]]]] [source]#
Construct dict of instance from dict of
AttrsClass
liked data. It could be a dictionary, an instance of this class, or None.
- classmethod ib_nested(**kwargs)[source]#
Declare a field that is another
AttrsClass
.Note
nested object has default value
None
, so it has to put it after those attribute doesn’t has default value.
- classmethod ib_list_of_nested(**kwargs)[source]#
Declare a field that is a list of other
AttrsClass
.
- classmethod ib_map_of_nested(key_type: K, nullable=True, value_nullable=True, **kwargs)[source]#
- Declare a field that is a mapper that key is any hashable value and
value is instance of
AttrsClass
.
- attrs_mate.mate.DictClass#
This might work too:
import weakref DictClass = weakref.WeakValueDictionary
- class attrs_mate.mate.LazyClass[source]#
Inheriting from this class gives you a factory method
LazyClass.lazymake()
. It allows you to use the same API as the default__init__(*args, **kwargs)
, but using cache.You can implement your own
uuid(self)
property method like this. The uuid value will be used as the key for new instance:class MyClass(LazyClass): ... @LazyClass.lazyproperty def uuid(self): return ...
LazyClass.lazyproperty
is a decorator which is similar to the built-inproperty
decorator, but the returned value are automatically cached, and will be called only once.Reference:
8.10 使用延迟计算属性: https://python-cookbook-3rd-edition.readthedocs.io/zh_CN/latest/c08/p10_using_lazily_computed_properties.html
8.25 创建缓存实例: https://python-cookbook-3rd-edition.readthedocs.io/zh_CN/latest/c08/p25_creating_cached_instances.html
Deprecated since version 1.2.X: introduced cached property >=3.8, which is better than this if <3.7, you can use https://pypi.org/project/cached-property/