{"id":17281040,"url":"https://github.com/wchresta/from-dict","last_synced_at":"2026-03-27T07:11:48.958Z","repository":{"id":37008883,"uuid":"241003593","full_name":"wchresta/from-dict","owner":"wchresta","description":"Python library to construct data structures from dictionaries.","archived":false,"fork":false,"pushed_at":"2025-01-03T04:33:03.000Z","size":53,"stargazers_count":2,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-09-05T03:24:12.769Z","etag":null,"topics":["dict","dictionaries","json-schema","python","python3","python310","python37","python38","python39","typing"],"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/wchresta.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-17T02:28:36.000Z","updated_at":"2025-01-03T04:33:07.000Z","dependencies_parsed_at":"2024-03-22T16:29:24.752Z","dependency_job_id":"89b45797-25eb-4650-976c-f686262fe3e4","html_url":"https://github.com/wchresta/from-dict","commit_stats":{"total_commits":26,"total_committers":2,"mean_commits":13.0,"dds":"0.46153846153846156","last_synced_commit":"5e9f715997547f13a4a851ec286e6e412206f324"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/wchresta/from-dict","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wchresta%2Ffrom-dict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wchresta%2Ffrom-dict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wchresta%2Ffrom-dict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wchresta%2Ffrom-dict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wchresta","download_url":"https://codeload.github.com/wchresta/from-dict/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wchresta%2Ffrom-dict/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31032130,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-27T06:08:13.374Z","status":"ssl_error","status_checked_at":"2026-03-27T06:08:07.217Z","response_time":164,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["dict","dictionaries","json-schema","python","python3","python310","python37","python38","python39","typing"],"created_at":"2024-10-15T09:22:57.108Z","updated_at":"2026-03-27T07:11:48.921Z","avatar_url":"https://github.com/wchresta.png","language":"Python","readme":"# from-dict\nCreate data structures from partially known dictionaries.\n\n## Features\n* Transform dicts to `attr.s`, `dataclass`, `NamedTuple`, and normal classes that have type-hints for all their __init__ parameters.\n* Supports nested structures when using `typing.List` and `typing.Dict` type hints.\n* Insert additional fields existing in dict into structure with `fd_copy_unknown=True`\n* Optional run-time type-checking with `fd_check_types=True`\n* Supports forward references\n* Raise an exception if there are more arguments supplied than are required with `fd_error_on_unknown=True`\n* Supports Literal type hints\n\n\n## Example\n```python\nfrom dataclasses import dataclass\nfrom typing import List, Optional\nfrom from_dict import from_dict\n\n\n@dataclass(frozen=True)\nclass Preference:\n    name: str\n    score: int\n\n\n@dataclass(frozen=True)\nclass Customer:\n    name: str\n    nick_name: Optional[str]\n    preferences: List[Preference]\n\n\ninput_customer_data = {\n    \"name\": \"Christopher Lee\",\n    \"nick_name\": None,\n    \"preferences\": [\n        { \"name\": \"The Hobbit\", \"score\": 37 },\n        { \"name\": \"Count Dooku\", \"score\": 2 },\n        { \"name\": \"Saruman\", \"score\": 99 }\n    ],\n    \"friend\": \"Mellon\"\n}\n\ncustomer = from_dict(Customer, input_customer_data)\n# Structured data is available as attributes since attr.s exposes them like that\nassert customer.name == \"Christopher Lee\"\n# Nested structures are also constructed. List[sub_strucutre] and Dict[key, sub_structure] are supported\nassert customer.preferences[0].name == \"The Hobbit\"\n# Data not defined in the strucutre is inserted into the __dict__ if possible\nassert customer.__dict__[\"friend\"] == \"Mellon\"\n```\n\n## Use cases\n\n`from-dict` is especially useful when used on big and partially known data structures like JSON. Since undefined \nstructure is ignored, we can use `from-dict` to avoid `try-catch` and `KeyError` hell:\n\nAssume we want to interact with the Google GeoCoding API\n(cf. https://developers.google.com/maps/documentation/geocoding/intro):\n\nThe JSON that is returned on requests contains some keys that we are not interested in. So we create \ndata-structures that contain the keys that we actually want to use:\n\n```python\nfrom dataclasses import dataclass\nfrom typing import List\n\n@dataclass(frozen=True)\nclass AddressComponent:\n    long_name: str\n    short_name: str\n    types: List[str]\n\n@dataclass(frozen=True)\nclass Result:\n    address_components: List[AddressComponent]\n    formatted_address: str\n\n@dataclass(frozen=True)\nclass Response:\n    results: List[Result]\n```\n\nWith that, given the `response` of the API, we can extract the fields and ignore everything else.\n\n```python\nfrom from_dict import from_dict\n\n# This will throw a TypeError if something goes wrong.\nstructured_response: Response = from_dict(Response, \n                                          response, \n                                          fd_check_types=True,   # Do check types at run-time\n                                          fd_copy_unknown=False  # Do not copy undefined data to __dict__\n                                          )\n\n# Now, we can access the data in a statically known manner\nfor res in structured_response.results:\n    print(f\"The formatted address is {res.formatted_address}\")\n    for addr_comp in res.address_components:\n        print(f\"Component {addr_comp.long_name}\")\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwchresta%2Ffrom-dict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwchresta%2Ffrom-dict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwchresta%2Ffrom-dict/lists"}