2023-01-13 02:28:26 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
|
|
|
Commonly useful filters for `attr.asdict`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from ._make import Attribute
|
|
|
|
|
|
|
|
|
|
|
|
def _split_what(what):
|
|
|
|
"""
|
|
|
|
Returns a tuple of `frozenset`s of classes and attributes.
|
|
|
|
"""
|
|
|
|
return (
|
2023-01-13 02:28:26 +00:00
|
|
|
frozenset(cls for cls in what if isinstance(cls, type)),
|
2023-01-12 01:04:47 +00:00
|
|
|
frozenset(cls for cls in what if isinstance(cls, Attribute)),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def include(*what):
|
|
|
|
"""
|
2023-01-13 02:28:26 +00:00
|
|
|
Include *what*.
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-13 02:28:26 +00:00
|
|
|
:param what: What to include.
|
|
|
|
:type what: `list` of `type` or `attrs.Attribute`\\ s
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
:rtype: `callable`
|
|
|
|
"""
|
|
|
|
cls, attrs = _split_what(what)
|
|
|
|
|
|
|
|
def include_(attribute, value):
|
|
|
|
return value.__class__ in cls or attribute in attrs
|
|
|
|
|
|
|
|
return include_
|
|
|
|
|
|
|
|
|
|
|
|
def exclude(*what):
|
|
|
|
"""
|
2023-01-13 02:28:26 +00:00
|
|
|
Exclude *what*.
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-13 02:28:26 +00:00
|
|
|
:param what: What to exclude.
|
|
|
|
:type what: `list` of classes or `attrs.Attribute`\\ s.
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
:rtype: `callable`
|
|
|
|
"""
|
|
|
|
cls, attrs = _split_what(what)
|
|
|
|
|
|
|
|
def exclude_(attribute, value):
|
|
|
|
return value.__class__ not in cls and attribute not in attrs
|
|
|
|
|
|
|
|
return exclude_
|