Skip to content

Instantly share code, notes, and snippets.

@stvhay
Last active April 21, 2020 19:35
Show Gist options
  • Save stvhay/29c63790f83016a6241b21aecadc1a32 to your computer and use it in GitHub Desktop.
Save stvhay/29c63790f83016a6241b21aecadc1a32 to your computer and use it in GitHub Desktop.
Use of cattr
import attr
import cattr
import json
@attr.s
class FooClass:
x = attr.ib()
y = attr.ib()
if __name__ == '__main__':
bar = FooClass(x=1, y=2)
serialized = cattr.unstructure(bar)
json_data = json.dumps(serialized)
print(json_data)
bar2 = cattr.structure(serialized, FooClass)
print(f'bar2={bar2}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment