{"id":19779887,"url":"https://github.com/jondot/attrs-serde","last_synced_at":"2025-04-30T21:32:23.290Z","repository":{"id":33251548,"uuid":"156894379","full_name":"jondot/attrs-serde","owner":"jondot","description":"A serialization addon for attrs.","archived":false,"fork":false,"pushed_at":"2022-12-09T10:48:45.000Z","size":909,"stargazers_count":25,"open_issues_count":15,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-09T00:49:42.146Z","etag":null,"topics":["attrs","python","serialisation","serialization"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jondot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-09T17:17:50.000Z","updated_at":"2024-09-24T03:40:07.000Z","dependencies_parsed_at":"2023-01-15T00:15:19.268Z","dependency_job_id":null,"html_url":"https://github.com/jondot/attrs-serde","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jondot%2Fattrs-serde","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jondot%2Fattrs-serde/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jondot%2Fattrs-serde/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jondot%2Fattrs-serde/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jondot","download_url":"https://codeload.github.com/jondot/attrs-serde/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224225355,"owners_count":17276435,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attrs","python","serialisation","serialization"],"created_at":"2024-11-12T05:37:59.810Z","updated_at":"2024-11-12T05:38:00.338Z","avatar_url":"https://github.com/jondot.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](media/cover.png)\n\n# attrs-serde\n\nA serialization addon for [attrs](https://attrs.org).\n\n\n```py\nperson_dict = {\"contact\": {\"personal\": {\"name\": \"John\"}, \"phone\": \"555-112233\"}}\n\nname_path = [\"contact\", \"personal\", \"name\"]\nphone_path = [\"contact\", \"phone\"]\n\n@serde\n@attrs\nclass Person(object):\n    name = attrib(metadata={\"to\": name_path, \"from\": name_path})\n    phone = attrib(metadata={\"to\": phone_path, \"from\": phone_path})\n\n\u003e\u003e\u003e p = Person.from_dict(person_dict)\nPerson(name=John phone=555-112233)\n\n\u003e\u003e\u003e p.to_dict\n{\"contact\": {\"personal\": {\"name\": \"John\"}, \"phone\": \"555-112233\"}}\n```\n\n\n## Quick Start\n\nInstall using pip/pipenv/etc. (we recommend [poetry](https://github.com/sdispater/poetry) for sane dependency management):\n\n```\n$ poetry add attrs-serde\n```\n\nDecorate with `serde` for automatic `to_dict` and `from_dict`. Provide paths in `metadata`:\n\n1. `from` - path to fetch field value from\n2. `to` - path to serialize value into (creates nested dictionaries as needed)\n\nExample:\n\n```py\nfrom attrs_serde import serde\nfrom attr import attrs, attrib\n@serde\n@attrs\nclass Person(object):\n    name = attrib(metadata={\"to\": name_path, \"from\": name_path})\n    phone = attrib(metadata={\"to\": phone_path, \"from\": phone_path})\n```\n\nCustom `from` and `to` keys (in case you or a different extension use those):\n\n```py\nfrom attrs_serde import serde\nfrom attr import attrs, attrib\n@serde(from_key=\"get\", to_key=\"set\")\n@attrs\nclass Person(object):\n    name = attrib(metadata={\"get\": name_path, \"set\": name_path})\n    phone = attrib(metadata={\"get\": phone_path, \"set\": phone_path})\n```\n\n## Performance\n\n`attrs-serde` works with `cytoolz` (mostly C implementation) and so presents very low overhead over what `attrs` already does.\n\n\nAgainst manual object construction:\n\n```\n------------------------------------------------------------------------------------- benchmark 'deserialization': 2 tests ------------------------------------------------------------------------------------\nName (time in ns)              Min                    Max                  Mean                StdDev                Median                 IQR            Outliers  OPS (Kops/s)            Rounds  Iterations\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\ntest_deser_baseline       583.2500 (1.0)       5,037.3500 (1.0)        641.4743 (1.0)        161.4237 (1.0)        603.8500 (1.0)       33.7500 (1.0)     2315;3276    1,558.9089 (1.0)       77828          20\ntest_deser_serde        1,976.0000 (3.39)     88,504.0000 (17.57)    2,226.3774 (3.47)     1,195.7336 (7.41)     2,127.0000 (3.52)     110.0000 (3.26)     484;1576      449.1601 (0.29)      86806           1\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n```\n\nSerialization against attr's `asdict`:\n\n```\n-------------------------------------------------------------------------- benchmark 'serialization': 2 tests --------------------------------------------------------------------------\nName (time in us)        Min                 Max              Mean            StdDev            Median               IQR            Outliers  OPS (Kops/s)            Rounds  Iterations\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\ntest_ser_baseline     2.6600 (1.0)      130.4550 (1.33)     2.9098 (1.0)      1.3230 (1.0)      2.7940 (1.0)      0.1320 (1.0)       302;882      343.6625 (1.0)       46642           1\ntest_ser_serde        5.0390 (1.89)      98.4540 (1.0)      5.6411 (1.94)     2.2398 (1.69)     5.4465 (1.95)     0.2890 (2.19)      491;912      177.2706 (0.52)      32936           1\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n```\n\n### Thanks:\n\nTo all [Contributors](https://github.com/jondot/attrs-serde/graphs/contributors) - you make this happen, thanks!\n\n# Copyright\n\nCopyright (c) 2018 [@jondot](http://twitter.com/jondot). See [LICENSE](LICENSE.txt) for further details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjondot%2Fattrs-serde","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjondot%2Fattrs-serde","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjondot%2Fattrs-serde/lists"}