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

How can I structure to an OrderedDict? #27

Closed
pombredanne opened this issue Feb 8, 2018 · 2 comments
Closed

How can I structure to an OrderedDict? #27

pombredanne opened this issue Feb 8, 2018 · 2 comments

Comments

@pombredanne
Copy link
Contributor

cattr.structure(OrderedDict([(1, 2), (3, 4)]), Dict) returns an unordered mapping. Is there a way to structure to an ordered mapping instead? (I am on Python 2.7 btw)

@Tinche
Copy link
Member

Tinche commented Feb 8, 2018

Yeah, so the problem there is no OrderedDict[k, v] in typing.

Try this:

from collections import OrderedDict

import cattr

cattr.register_structure_hook(OrderedDict, lambda v, _: OrderedDict(v))

print(cattr.structure(OrderedDict([(1, 2), (3, 4)]), OrderedDict))

@pombredanne
Copy link
Contributor Author

Simple and clean. Thanks! I am digging your excellent attr extension to replace schematics code in https://github.com/nexB/scancode-toolkit/blob/1f6538874688de65b7b70bd09f39a6402c074110/src/packagedcode/models.py#L275 FWIW.

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

No branches or pull requests

2 participants