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

Named tuple output for dataset classes #71

Closed
jonasteuwen opened this issue May 31, 2022 · 1 comment
Closed

Named tuple output for dataset classes #71

jonasteuwen opened this issue May 31, 2022 · 1 comment
Labels
enhancement New feature or request Stale

Comments

@jonasteuwen
Copy link
Contributor

jonasteuwen commented May 31, 2022

Is your feature request related to a problem? Please describe.
Output of datasets should be named tuples

Describe the solution you'd like
E.g.

def create_namedtuple_from_dict(obj):
    if isinstance(obj, dict):
        fields = sorted(obj.keys())
        namedtuple_type = namedtuple(
            typename="GenericObject",
            field_names=fields,
            rename=True,
        )
        field_value_pairs = OrderedDict((str(field), create_namedtuple_from_dict(obj[field])) for field in fields)
        try:
            return namedtuple_type(**field_value_pairs)
        except TypeError:
            # Cannot create namedtuple instance so fallback to dict (invalid attribute names)
            return dict(**field_value_pairs)
    elif isinstance(obj, (list, set, tuple, frozenset)):
        return [create_namedtuple_from_dict(item) for item in obj]
    else:
        return obj
@jonasteuwen jonasteuwen added the enhancement New feature or request label May 31, 2022
@github-actions
Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Stale
Projects
None yet
Development

No branches or pull requests

1 participant