Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow asdict to exclude fields based on values #63

Open
Gallaecio opened this issue Jun 23, 2022 · 0 comments
Open

Allow asdict to exclude fields based on values #63

Gallaecio opened this issue Jun 23, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@Gallaecio
Copy link
Member

Gallaecio commented Jun 23, 2022

e.g.

def asdict(*, unset_values=None) -> dict:
    if unset_values is None:
        unset_values = set()
    return _asdict(self, unset_values)
    ...

def _asdict(obj: Any, unset_values) -> Any:
    if isinstance(obj, dict):
        return {key: _asdict(value, unset_values=unset_values) for key, value in obj.items() if value not in unset_values}
    if isinstance(obj, (list, set, tuple)):
        return obj.__class__(_asdict(x, unset_values=unset_values) for x in obj if x not in unset_values)
    if isinstance(obj, ItemAdapter):
        return obj.asdict(unset_values=unset_values)
    if ItemAdapter.is_item(obj):
        return ItemAdapter(obj).asdict(unset_values=unset_values)
    return

Context: zytedata/zyte-common-items#2 (comment)

@Gallaecio Gallaecio added the enhancement New feature or request label Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant