author | Gregory Szorc <gregory.szorc@gmail.com> |
Sat, 18 Jan 2020 10:12:41 -0800 | |
changeset 44120 | 35cd52c4a5cc |
parent 41578 | a5493a251ad3 |
child 49761 | e1c586b9a43c |
permissions | -rw-r--r-- |
34398 | 1 |
""" |
2 |
Commonly useful filters for :func:`attr.asdict`. |
|
3 |
""" |
|
4 |
||
5 |
from __future__ import absolute_import, division, print_function |
|
6 |
||
7 |
from ._compat import isclass |
|
8 |
from ._make import Attribute |
|
9 |
||
10 |
||
11 |
def _split_what(what): |
|
12 |
""" |
|
13 |
Returns a tuple of `frozenset`s of classes and attributes. |
|
14 |
""" |
|
15 |
return ( |
|
16 |
frozenset(cls for cls in what if isclass(cls)), |
|
17 |
frozenset(cls for cls in what if isinstance(cls, Attribute)), |
|
18 |
) |
|
19 |
||
20 |
||
21 |
def include(*what): |
|
41578
a5493a251ad3
attr: make some docstrings raw strings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
34398
diff
changeset
|
22 |
r""" |
34398 | 23 |
Whitelist *what*. |
24 |
||
25 |
:param what: What to whitelist. |
|
26 |
:type what: :class:`list` of :class:`type` or :class:`attr.Attribute`\ s |
|
27 |
||
28 |
:rtype: :class:`callable` |
|
29 |
""" |
|
30 |
cls, attrs = _split_what(what) |
|
31 |
||
32 |
def include_(attribute, value): |
|
33 |
return value.__class__ in cls or attribute in attrs |
|
34 |
||
35 |
return include_ |
|
36 |
||
37 |
||
38 |
def exclude(*what): |
|
41578
a5493a251ad3
attr: make some docstrings raw strings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
34398
diff
changeset
|
39 |
r""" |
34398 | 40 |
Blacklist *what*. |
41 |
||
42 |
:param what: What to blacklist. |
|
43 |
:type what: :class:`list` of classes or :class:`attr.Attribute`\ s. |
|
44 |
||
45 |
:rtype: :class:`callable` |
|
46 |
""" |
|
47 |
cls, attrs = _split_what(what) |
|
48 |
||
49 |
def exclude_(attribute, value): |
|
50 |
return value.__class__ not in cls and attribute not in attrs |
|
51 |
||
52 |
return exclude_ |