mate#

This module implements:

class attrs_mate.mate.AttrsClass[source]#

A mixins class provideing more utility methods.

classmethod keys()[source]#

Fieldname list.

values()[source]#

Value list.

items()[source]#

Fieldname and value pair list.

to_dict() Dict[str, Any][source]#

Convert to dictionary.

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-in property decorator, but the returned value are automatically cached, and will be called only once.

Reference:

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/

class lazyproperty(func)[source]#

decorator for cached property method.

Usage Example:

class User(object):
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    @LazyClass.lazyproperty
    def fullname(self):
        return "{} {}".format(self.lastname, self.firstname)
classmethod lazymake(*args, **kwargs)[source]#

A factory method to make an object, prefer to use cache.

Parameters:
  • args – same argument as Class.__init__(*args, **kwargs)

  • kwargs – same argument as Class.__init__(*args, **kwargs)