Simplify asdict and has (#48)
This commit is contained in:
parent
e833e0c1cd
commit
588fe48019
|
@ -13,6 +13,8 @@ Changes:
|
||||||
|
|
||||||
- ``attr.asdict``\ 's ``dict_factory`` arguments is now propagated on recursion.
|
- ``attr.asdict``\ 's ``dict_factory`` arguments is now propagated on recursion.
|
||||||
`#45 <https://github.com/hynek/attrs/issues/45>`_
|
`#45 <https://github.com/hynek/attrs/issues/45>`_
|
||||||
|
- ``attr.asdict`` and ``attr.has`` are significantly faster.
|
||||||
|
`#48 <https://github.com/hynek/attrs/issues/48>`_
|
||||||
|
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
|
@ -3,7 +3,7 @@ from __future__ import absolute_import, division, print_function
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
from ._compat import iteritems
|
from ._compat import iteritems
|
||||||
from ._make import Attribute, NOTHING, fields
|
from ._make import Attribute, NOTHING, _fast_attrs_iterate
|
||||||
|
|
||||||
|
|
||||||
def asdict(inst, recurse=True, filter=None, dict_factory=dict):
|
def asdict(inst, recurse=True, filter=None, dict_factory=dict):
|
||||||
|
@ -30,7 +30,7 @@ def asdict(inst, recurse=True, filter=None, dict_factory=dict):
|
||||||
.. versionadded:: 16.0.0
|
.. versionadded:: 16.0.0
|
||||||
*dict_factory*
|
*dict_factory*
|
||||||
"""
|
"""
|
||||||
attrs = fields(inst.__class__)
|
attrs = _fast_attrs_iterate(inst)
|
||||||
rv = dict_factory()
|
rv = dict_factory()
|
||||||
for a in attrs:
|
for a in attrs:
|
||||||
v = getattr(inst, a.name)
|
v = getattr(inst, a.name)
|
||||||
|
@ -71,12 +71,7 @@ def has(cl):
|
||||||
|
|
||||||
:rtype: :class:`bool`
|
:rtype: :class:`bool`
|
||||||
"""
|
"""
|
||||||
try:
|
return getattr(cl, "__attrs_attrs__", None) is not None
|
||||||
fields(cl)
|
|
||||||
except ValueError:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def assoc(inst, **changes):
|
def assoc(inst, **changes):
|
||||||
|
|
|
@ -15,12 +15,12 @@ from . import simple_classes, nested_classes
|
||||||
from attr._funcs import (
|
from attr._funcs import (
|
||||||
asdict,
|
asdict,
|
||||||
assoc,
|
assoc,
|
||||||
fields,
|
|
||||||
has,
|
has,
|
||||||
)
|
)
|
||||||
from attr._make import (
|
from attr._make import (
|
||||||
attr,
|
attr,
|
||||||
attributes,
|
attributes,
|
||||||
|
fields,
|
||||||
)
|
)
|
||||||
|
|
||||||
MAPPING_TYPES = (dict, OrderedDict)
|
MAPPING_TYPES = (dict, OrderedDict)
|
||||||
|
|
Loading…
Reference in New Issue