14

In documentation of Python module attrs stated that there is a method to convert attributes’ class into dictionary representation:

Example:

>>> @attr.s
... class Coordinates(object):
...     x = attr.ib()
...     y = attr.ib()
...
>>> attr.asdict(Coordinates(x=1, y=2))
{'x': 1, 'y': 2}

How can I achieve the opposite, instantiating the Coordinates from its valid dictionary representation without boilerplate and with the joy of the attrs module?

kuza
  • 2,761
  • 3
  • 22
  • 56

3 Answers3

16

Apparently as easy as using dictionary unpacking (double star) operator in corresponding attrs class instantiation.

Example:

>>> Coordinates(**{'x': 1, 'y': 2})
Coordinates(x=1, y=2)
Philippe Ombredanne
  • 2,017
  • 21
  • 36
kuza
  • 2,761
  • 3
  • 22
  • 56
11

As a more universal solution, which works with attrs nested classes, enums or any other type annotated structures you can use https://github.com/Tinche/cattrs. It also supports structure/unstructure customization by defining the structure/unstructure hooks

Example:

import attr, cattr
   
@attr.s(slots=True, frozen=True)  # It works with normal classes too.
class C:
        a = attr.ib()
        b = attr.ib()
    
instance = C(1, 'a')
cattr.unstructure(instance)
# {'a': 1, 'b': 'a'}
cattr.structure({'a': 1, 'b': 'a'}, C)
# C(a=1, b='a')
zhukovgreen
  • 1,551
  • 16
  • 26
3

I do this in my web app to be able to serialize/de-serialize into JSON:

First I made a method on my classes that returns a more serializing friendly version:

def asdict(self, serializable=True):
    if serializable:
        as_dict['type'] = self.__class__.__name__
        return as_dict
    else:
        return attr.asdict(self)

Then when I need to convert one of these dictionaries (JSON objects actually) back into a class instance:

obj_type = values.pop('type')
if obj_type in obj_list:
    obj = getattr(sys.modules[__name__], obj_type)(**values)
  • As far as I understand, your solution requires an additional state that will carry the type name? Also, it is not clear to what classes do you refer to by saying: “_I made a method on my classes_.” Either way, I receive only python dictionaries I cannot modify but can expect concrete keys to model it into simple DTO for more comfort further usage. – kuza Oct 20 '17 at 09:29
  • Also, @patrick-shechetin you can make a generic method for dynamic creation of your **attrs** DTOs from your JSON dictionaries. Even more, if you have a JSON schema for them you don’t need to carry a type name in payload itself. As long as you can ensure your dictionary validates against the schema of a concrete type. You can find more in my other [Q/A](https://stackoverflow.com/q/45770755/7598113) regarding _JSL_ and _attrs_. – kuza Oct 20 '17 at 09:41