|
@@ -7,14 +7,14 @@ from ._make import NOTHING, _obj_setattr, fields
|
|
|
from .exceptions import AttrsAttributeNotFoundError
|
|
|
|
|
|
|
|
|
-def asdict(
|
|
|
- inst,
|
|
|
- recurse=True,
|
|
|
- filter=None,
|
|
|
- dict_factory=dict,
|
|
|
- retain_collection_types=False,
|
|
|
+def asdict(
|
|
|
+ inst,
|
|
|
+ recurse=True,
|
|
|
+ filter=None,
|
|
|
+ dict_factory=dict,
|
|
|
+ retain_collection_types=False,
|
|
|
value_serializer=None,
|
|
|
-):
|
|
|
+):
|
|
|
"""
|
|
|
Return the ``attrs`` attribute values of *inst* as a dict.
|
|
|
|
|
@@ -59,49 +59,49 @@ def asdict(
|
|
|
|
|
|
if recurse is True:
|
|
|
if has(v.__class__):
|
|
|
- rv[a.name] = asdict(
|
|
|
+ rv[a.name] = asdict(
|
|
|
v,
|
|
|
True,
|
|
|
filter,
|
|
|
dict_factory,
|
|
|
retain_collection_types,
|
|
|
value_serializer,
|
|
|
- )
|
|
|
+ )
|
|
|
elif isinstance(v, (tuple, list, set, frozenset)):
|
|
|
cf = v.__class__ if retain_collection_types is True else list
|
|
|
- rv[a.name] = cf(
|
|
|
- [
|
|
|
- _asdict_anything(
|
|
|
+ rv[a.name] = cf(
|
|
|
+ [
|
|
|
+ _asdict_anything(
|
|
|
i,
|
|
|
filter,
|
|
|
dict_factory,
|
|
|
retain_collection_types,
|
|
|
value_serializer,
|
|
|
- )
|
|
|
- for i in v
|
|
|
- ]
|
|
|
- )
|
|
|
+ )
|
|
|
+ for i in v
|
|
|
+ ]
|
|
|
+ )
|
|
|
elif isinstance(v, dict):
|
|
|
df = dict_factory
|
|
|
- rv[a.name] = df(
|
|
|
- (
|
|
|
- _asdict_anything(
|
|
|
+ rv[a.name] = df(
|
|
|
+ (
|
|
|
+ _asdict_anything(
|
|
|
kk,
|
|
|
filter,
|
|
|
df,
|
|
|
retain_collection_types,
|
|
|
value_serializer,
|
|
|
- ),
|
|
|
- _asdict_anything(
|
|
|
+ ),
|
|
|
+ _asdict_anything(
|
|
|
vv,
|
|
|
filter,
|
|
|
df,
|
|
|
retain_collection_types,
|
|
|
value_serializer,
|
|
|
- ),
|
|
|
- )
|
|
|
- for kk, vv in iteritems(v)
|
|
|
- )
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ for kk, vv in iteritems(v)
|
|
|
+ )
|
|
|
else:
|
|
|
rv[a.name] = v
|
|
|
else:
|
|
@@ -117,10 +117,10 @@ def _asdict_anything(
|
|
|
value_serializer,
|
|
|
):
|
|
|
"""
|
|
|
- ``asdict`` only works on attrs instances, this works on anything.
|
|
|
- """
|
|
|
- if getattr(val.__class__, "__attrs_attrs__", None) is not None:
|
|
|
- # Attrs class.
|
|
|
+ ``asdict`` only works on attrs instances, this works on anything.
|
|
|
+ """
|
|
|
+ if getattr(val.__class__, "__attrs_attrs__", None) is not None:
|
|
|
+ # Attrs class.
|
|
|
rv = asdict(
|
|
|
val,
|
|
|
True,
|
|
@@ -130,48 +130,48 @@ def _asdict_anything(
|
|
|
value_serializer,
|
|
|
)
|
|
|
elif isinstance(val, (tuple, list, set, frozenset)):
|
|
|
- cf = val.__class__ if retain_collection_types is True else list
|
|
|
- rv = cf(
|
|
|
- [
|
|
|
- _asdict_anything(
|
|
|
+ cf = val.__class__ if retain_collection_types is True else list
|
|
|
+ rv = cf(
|
|
|
+ [
|
|
|
+ _asdict_anything(
|
|
|
i,
|
|
|
filter,
|
|
|
dict_factory,
|
|
|
retain_collection_types,
|
|
|
value_serializer,
|
|
|
- )
|
|
|
- for i in val
|
|
|
- ]
|
|
|
- )
|
|
|
- elif isinstance(val, dict):
|
|
|
- df = dict_factory
|
|
|
- rv = df(
|
|
|
- (
|
|
|
+ )
|
|
|
+ for i in val
|
|
|
+ ]
|
|
|
+ )
|
|
|
+ elif isinstance(val, dict):
|
|
|
+ df = dict_factory
|
|
|
+ rv = df(
|
|
|
+ (
|
|
|
_asdict_anything(
|
|
|
kk, filter, df, retain_collection_types, value_serializer
|
|
|
),
|
|
|
_asdict_anything(
|
|
|
vv, filter, df, retain_collection_types, value_serializer
|
|
|
),
|
|
|
- )
|
|
|
- for kk, vv in iteritems(val)
|
|
|
- )
|
|
|
- else:
|
|
|
- rv = val
|
|
|
+ )
|
|
|
+ for kk, vv in iteritems(val)
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ rv = val
|
|
|
if value_serializer is not None:
|
|
|
rv = value_serializer(None, None, rv)
|
|
|
|
|
|
- return rv
|
|
|
-
|
|
|
-
|
|
|
-def astuple(
|
|
|
- inst,
|
|
|
- recurse=True,
|
|
|
- filter=None,
|
|
|
- tuple_factory=tuple,
|
|
|
- retain_collection_types=False,
|
|
|
-):
|
|
|
- """
|
|
|
+ return rv
|
|
|
+
|
|
|
+
|
|
|
+def astuple(
|
|
|
+ inst,
|
|
|
+ recurse=True,
|
|
|
+ filter=None,
|
|
|
+ tuple_factory=tuple,
|
|
|
+ retain_collection_types=False,
|
|
|
+):
|
|
|
+ """
|
|
|
Return the ``attrs`` attribute values of *inst* as a tuple.
|
|
|
|
|
|
Optionally recurse into other ``attrs``-decorated classes.
|
|
@@ -206,56 +206,56 @@ def astuple(
|
|
|
continue
|
|
|
if recurse is True:
|
|
|
if has(v.__class__):
|
|
|
- rv.append(
|
|
|
- astuple(
|
|
|
- v,
|
|
|
- recurse=True,
|
|
|
- filter=filter,
|
|
|
- tuple_factory=tuple_factory,
|
|
|
- retain_collection_types=retain,
|
|
|
- )
|
|
|
- )
|
|
|
+ rv.append(
|
|
|
+ astuple(
|
|
|
+ v,
|
|
|
+ recurse=True,
|
|
|
+ filter=filter,
|
|
|
+ tuple_factory=tuple_factory,
|
|
|
+ retain_collection_types=retain,
|
|
|
+ )
|
|
|
+ )
|
|
|
elif isinstance(v, (tuple, list, set, frozenset)):
|
|
|
cf = v.__class__ if retain is True else list
|
|
|
- rv.append(
|
|
|
- cf(
|
|
|
- [
|
|
|
- astuple(
|
|
|
- j,
|
|
|
- recurse=True,
|
|
|
- filter=filter,
|
|
|
- tuple_factory=tuple_factory,
|
|
|
- retain_collection_types=retain,
|
|
|
- )
|
|
|
- if has(j.__class__)
|
|
|
- else j
|
|
|
- for j in v
|
|
|
- ]
|
|
|
- )
|
|
|
- )
|
|
|
+ rv.append(
|
|
|
+ cf(
|
|
|
+ [
|
|
|
+ astuple(
|
|
|
+ j,
|
|
|
+ recurse=True,
|
|
|
+ filter=filter,
|
|
|
+ tuple_factory=tuple_factory,
|
|
|
+ retain_collection_types=retain,
|
|
|
+ )
|
|
|
+ if has(j.__class__)
|
|
|
+ else j
|
|
|
+ for j in v
|
|
|
+ ]
|
|
|
+ )
|
|
|
+ )
|
|
|
elif isinstance(v, dict):
|
|
|
df = v.__class__ if retain is True else dict
|
|
|
- rv.append(
|
|
|
- df(
|
|
|
+ rv.append(
|
|
|
+ df(
|
|
|
(
|
|
|
astuple(
|
|
|
kk,
|
|
|
tuple_factory=tuple_factory,
|
|
|
- retain_collection_types=retain,
|
|
|
- )
|
|
|
- if has(kk.__class__)
|
|
|
- else kk,
|
|
|
+ retain_collection_types=retain,
|
|
|
+ )
|
|
|
+ if has(kk.__class__)
|
|
|
+ else kk,
|
|
|
astuple(
|
|
|
vv,
|
|
|
tuple_factory=tuple_factory,
|
|
|
- retain_collection_types=retain,
|
|
|
- )
|
|
|
- if has(vv.__class__)
|
|
|
- else vv,
|
|
|
+ retain_collection_types=retain,
|
|
|
+ )
|
|
|
+ if has(vv.__class__)
|
|
|
+ else vv,
|
|
|
)
|
|
|
- for kk, vv in iteritems(v)
|
|
|
- )
|
|
|
- )
|
|
|
+ for kk, vv in iteritems(v)
|
|
|
+ )
|
|
|
+ )
|
|
|
else:
|
|
|
rv.append(v)
|
|
|
else:
|
|
@@ -294,21 +294,21 @@ def assoc(inst, **changes):
|
|
|
Use `evolve` instead.
|
|
|
"""
|
|
|
import warnings
|
|
|
-
|
|
|
- warnings.warn(
|
|
|
- "assoc is deprecated and will be removed after 2018/01.",
|
|
|
- DeprecationWarning,
|
|
|
- stacklevel=2,
|
|
|
- )
|
|
|
+
|
|
|
+ warnings.warn(
|
|
|
+ "assoc is deprecated and will be removed after 2018/01.",
|
|
|
+ DeprecationWarning,
|
|
|
+ stacklevel=2,
|
|
|
+ )
|
|
|
new = copy.copy(inst)
|
|
|
attrs = fields(inst.__class__)
|
|
|
for k, v in iteritems(changes):
|
|
|
a = getattr(attrs, k, NOTHING)
|
|
|
if a is NOTHING:
|
|
|
raise AttrsAttributeNotFoundError(
|
|
|
- "{k} is not an attrs attribute on {cl}.".format(
|
|
|
- k=k, cl=new.__class__
|
|
|
- )
|
|
|
+ "{k} is not an attrs attribute on {cl}.".format(
|
|
|
+ k=k, cl=new.__class__
|
|
|
+ )
|
|
|
)
|
|
|
_obj_setattr(new, k, v)
|
|
|
return new
|