10

I have something like:

from attr import attrs, attrib

@attrs
class Foo():
    max_count = attrib()
    @property
    def get_max_plus_one(self):
         return self.max_count + 1

Now when I do:

f = Foo(max_count=2)
f.get_max_plus_one =>3

I want to convert this to dict:

{'max_count':2, 'get_max_plus_one': 3}

When I used attr.asdict(f) I do not get the @property. I get only {'max_count':2}.

What is the cleanest way to achieve the above?

martineau
  • 119,623
  • 25
  • 170
  • 301
suprita shankar
  • 1,554
  • 2
  • 16
  • 47

4 Answers4

3

In general, you would have to iterate over the classes attributes and check for instances of property and then call the properties __get__ method using the instance. So, something like:

In [16]: class A:
    ...:     @property
    ...:     def x(self):
    ...:         return 42
    ...:     @property
    ...:     def y(self):
    ...:         return 'foo'
    ...:

In [17]: a = A()

In [18]: vars(a)
Out[18]: {}

In [19]: a.x
Out[19]: 42

In [20]: a.y
Out[20]: 'foo'

In [21]: {n:p.__get__(a) for n, p in vars(A).items() if isinstance(p, property)}
Out[21]: {'x': 42, 'y': 'foo'}
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
3

I’m afraid that isn’t supported by attrs at the moment. You may want to follow/comment on https://github.com/python-attrs/attrs/issues/353 which may give you what you want eventually.

hynek
  • 3,647
  • 1
  • 18
  • 26
1

For this case, you can use dir on the object, and get only the properties that do not start with __ i.e. ignore the magic methods:

In [496]: class Foo():
     ...:     def __init__(self):
     ...:         self.max_count = 2
     ...:     @property
     ...:     def get_max_plus_one(self):
     ...:          return self.max_count + 1
     ...:     

In [497]: f = Foo()

In [498]: {prop: getattr(f, prop) for prop in dir(f) if not prop.startswith('__')}
Out[498]: {'get_max_plus_one': 3, 'max_count': 2}

To handle the regular methods that do not start with __, you can add a callable test:

In [521]: class Foo():
     ...:     def __init__(self):
     ...:         self.max_count = 2
     ...:     @property
     ...:     def get_max_plus_one(self):
     ...:          return self.max_count + 1
     ...:     def spam(self):
     ...:         return 10
     ...:     

In [522]: f = Foo()

In [523]: {prop: getattr(f, prop) for prop in dir(f) if not (prop.startswith('__') or callable(getattr(Foo, prop, None)))}
Out[523]: {'get_max_plus_one': 3, 'max_count': 2}
heemayl
  • 39,294
  • 7
  • 70
  • 76
0

If you define:

import attr

def attr2dict(inst):
    dic = attr.asdict(inst)
    dic.update({n: p.__get__(inst) for n, p in vars(type(inst)).items() if isinstance(p, property)})
    return dic

Then you get what you were looking for:

>>> attr2dict(f)
{'max_count': 2, 'get_max_plus_one': 3}
kadee
  • 8,067
  • 1
  • 39
  • 31