You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filters.py 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """
  2. Commonly useful filters for `attr.asdict`.
  3. """
  4. from __future__ import absolute_import, division, print_function
  5. from ._compat import isclass
  6. from ._make import Attribute
  7. def _split_what(what):
  8. """
  9. Returns a tuple of `frozenset`s of classes and attributes.
  10. """
  11. return (
  12. frozenset(cls for cls in what if isclass(cls)),
  13. frozenset(cls for cls in what if isinstance(cls, Attribute)),
  14. )
  15. def include(*what):
  16. """
  17. Whitelist *what*.
  18. :param what: What to whitelist.
  19. :type what: `list` of `type` or `attr.Attribute`\\ s
  20. :rtype: `callable`
  21. """
  22. cls, attrs = _split_what(what)
  23. def include_(attribute, value):
  24. return value.__class__ in cls or attribute in attrs
  25. return include_
  26. def exclude(*what):
  27. """
  28. Blacklist *what*.
  29. :param what: What to blacklist.
  30. :type what: `list` of classes or `attr.Attribute`\\ s.
  31. :rtype: `callable`
  32. """
  33. cls, attrs = _split_what(what)
  34. def exclude_(attribute, value):
  35. return value.__class__ not in cls and attribute not in attrs
  36. return exclude_