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

Add new msgpec.structs module #300

Merged
merged 2 commits into from Feb 5, 2023
Merged

Add new msgpec.structs module #300

merged 2 commits into from Feb 5, 2023

Conversation

jcrist
Copy link
Owner

@jcrist jcrist commented Feb 5, 2023

  • Adds new msgspec.structs module for struct-oriented utilities
  • Moves msgpsec.replace to msgspec.structs.replace. This isn't a breaking change since msgspec.replace hasn't acutally been released yet.
  • Adds new msgspec.structs.asdict/msgspec.structs.astuple for converting a struct to a dict or tuple. These functions don't operate recursively, any subfields are passed on as is.

Fixes #299.

This is a new module of struct-focused utilities. Since `replace` is a
less commonly used API we move it from the top-level namespace to
`msgspec.structs`. This isn't a breaking change since it hasn't been
released yet.
These utility functions are useful for converting a struct to a dict or
tuple. They're similar to `to_builtins` except they don't convert or
recurse. They're equivalent to the following user code, except faster:

```python
def asdict(x):
    return {n: getattr(x, n) for n in x.__struct_values__}

def astuple(x):
    return tuple(getattr(x, n) for n in x.__struct_values__)
```
@jcrist jcrist merged commit 1338505 into main Feb 5, 2023
@jcrist jcrist deleted the struct-utils branch February 5, 2023 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add asdict and astuple utilities for Structs
1 participant