Tools to use with the attrs package

Data curation can profit a lot from a transparent data model and documented structure. This can be achieved using the attrs library,

  • defining core objects of the data as @attr.s decorated classes

  • using attrs validation and conversion functionality, to observe the principle of locality - i.e. have data cleanup defined close to the objects, while accessing clean data through the objects elsewhere in the code base.

clldutils.attrlib.asdict(obj, omit_defaults=True, omit_private=True)[source]

Enhanced version of attr.asdict.

Parameters:
  • omit_defaults – If True, only attribute values which differ from the default will be added.

  • omit_private – If True, values of private attributes (i.e. attributes with names starting with _) will not be added.

>>> @attr.s
... class Bag:
...     _private = attr.ib()
...     with_default = attr.ib(default=7)
...
>>> asdict(Bag('x'))
OrderedDict()
>>> asdict(Bag('x'), omit_defaults=False, omit_private=False)
OrderedDict([('_private', 'x'), ('with_default', 7)])
>>> attr.asdict(Bag('x'))
{'_private': 'x', 'with_default': 7}
clldutils.attrlib.valid_range(min_, max_, nullable=False)[source]

A validator that raises a ValueError if the provided value that is not in the range defined by min_ and max_.

clldutils.attrlib.valid_re(regex, nullable=False)[source]

Deprecated since version 3.9: Use attr.validators.matches_re instead.

clldutils.attrlib.valid_enum_member(choices, nullable=False)[source]

Deprecated since version 3.9: Use attr.validators.in_ instead.