1# SPDX-License-Identifier: MIT

2

3"""

4 Commonly useful filters for `attr.asdict`.

5"""

6

7 from ._make import Attribute

8

9

10 def _split_what(what):

11 """

12 Returns a tuple of `frozenset`s of classes and attributes.

13 """

14 return (

15 frozenset(cls for cls in what if isinstance(cls, type)),

16 frozenset(cls for cls in what if isinstance(cls, Attribute)),

17 )

18

19

20 def include(*what):

21 """

22 Include *what*.

23

24 :param what: What to include.

25 :type what: `list` of `type` or `attrs.Attribute`\\ s

26

27 :rtype: `callable`

28 """

29 cls, attrs = _split_what(what)

30

31 def include_(attribute, value):

32 return value.__class__ in cls or attribute in attrs

33

34 return include_

35

36

37 def exclude(*what):

38 """

39 Exclude *what*.

40

41 :param what: What to exclude.

42 :type what: `list` of classes or `attrs.Attribute`\\ s.

43

44 :rtype: `callable`

45 """

46 cls, attrs = _split_what(what)

47

48 def exclude_(attribute, value):

49 return value.__class__ not in cls and attribute not in attrs

50

51 return exclude_