Name | objectify JSON |
Version |
1.0.0
JSON |
| download |
home_page | None |
Summary | A package to convert a dictionary to an object to achieve similar syntax and auto-completion as in TypeScript when annotating objects with interfaces. |
upload_time | 2025-02-08 23:19:57 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.10 |
license | None |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Objectify
Objectify is a lightweight library designed to convert a dictionary into a typed object based on a provided type argument.
It allows developers to work seamlessly with dictionaries using dot notation while benefiting from type safety, type hinting, and IDE autocompletion.
The idea behind objectify was inspired by TypeScript, where you can take any object, define an interface or type for it,
and "cast" the object to that type. This approach gives you immediate access to types and autocompletion, making writing code more efficient and less error-prone.
In Python, the closest existing standard solution is `TypedDict`, which provides type hints and key autocompletion for dictionaries.
However, its main drawback is that you must still access values using square brackets and string keys (e.g., `data['key']`),
which is less convenient and takes more time compared to dot notation (`data.key`).
This functionality is especially useful when working with large nested payloads (e.g., JSON responses from APIs).
By leveraging objectify, you can easily map such data to Python objects while maintaining clarity and reducing errors.
## Features
- **Type Safety**: Objectify ensures that the object's attributes match the specified types.
- **Custom Classes**: Objectify supports custom classes as type arguments, allowing for more complex object structures.
- **Dataclasses**: Objectify works seamlessly with dataclasses, providing a simple way to convert dictionaries to dataclass objects.
- **Nested Objects**: Objectify can handle nested dictionaries and convert them to nested objects.
- **Literal Types**: Objectify supports `Literal` types for specifying exact values that an attribute can take.
- **Error Handling**: Objectify raises `TypeError` for mismatched types, unsupported types, and invalid literal values.
## Installation
```bash
pip install objectify
```
## Usage
### Simple Example
```python
from objectify import dict_to_object
class User:
name: str
age: int
is_active: bool
data = {
'name': 'Alice',
'age': 30,
'is_active': True
}
user = dict_to_object(data, User)
print(user.name) # 'Alice'
print(user.age) # 30
print(user.is_active) # True
```
### Complex Example
```python
from objectify import dict_to_object
class Address:
street: str
city: str
zip_code: int
class User:
name: str
age: int
address: list[Address]
data = {
'name': 'Alice',
'age': 30,
'address': [
{'street': '123 Main St', 'city': 'Springfield', 'zip_code': 12345},
{'street': '456 Elm St', 'city': 'Rivertown', 'zip_code': 54321}
]
}
user = dict_to_object(data, User)
print(user.name) # 'Alice'
print(user.age) # 30
print(user.address[0].street) # '123 Main St'
print(user.address[1].city) # 'Rivertown'
```
Raw data
{
"_id": null,
"home_page": null,
"name": "objectify",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Andreas Marten Viks <andreasviks1@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/7a/6f/b8c6930ce6a50e276a982094c489f2c1a4e68fa3cd593c30f0dfe0ff1753/objectify-1.0.0.tar.gz",
"platform": null,
"description": "# Objectify\n\nObjectify is a lightweight library designed to convert a dictionary into a typed object based on a provided type argument.\nIt allows developers to work seamlessly with dictionaries using dot notation while benefiting from type safety, type hinting, and IDE autocompletion.\n\nThe idea behind objectify was inspired by TypeScript, where you can take any object, define an interface or type for it,\nand \"cast\" the object to that type. This approach gives you immediate access to types and autocompletion, making writing code more efficient and less error-prone.\n\nIn Python, the closest existing standard solution is `TypedDict`, which provides type hints and key autocompletion for dictionaries.\nHowever, its main drawback is that you must still access values using square brackets and string keys (e.g., `data['key']`),\nwhich is less convenient and takes more time compared to dot notation (`data.key`).\n\nThis functionality is especially useful when working with large nested payloads (e.g., JSON responses from APIs).\nBy leveraging objectify, you can easily map such data to Python objects while maintaining clarity and reducing errors.\n\n\n## Features\n\n- **Type Safety**: Objectify ensures that the object's attributes match the specified types.\n- **Custom Classes**: Objectify supports custom classes as type arguments, allowing for more complex object structures.\n- **Dataclasses**: Objectify works seamlessly with dataclasses, providing a simple way to convert dictionaries to dataclass objects.\n- **Nested Objects**: Objectify can handle nested dictionaries and convert them to nested objects.\n- **Literal Types**: Objectify supports `Literal` types for specifying exact values that an attribute can take.\n- **Error Handling**: Objectify raises `TypeError` for mismatched types, unsupported types, and invalid literal values.\n\n## Installation\n\n```bash\npip install objectify\n```\n\n## Usage\n\n### Simple Example\n```python\nfrom objectify import dict_to_object\n\n\nclass User:\n name: str\n age: int\n is_active: bool\n\n\ndata = {\n 'name': 'Alice',\n 'age': 30,\n 'is_active': True\n}\n\nuser = dict_to_object(data, User)\nprint(user.name) # 'Alice'\nprint(user.age) # 30\nprint(user.is_active) # True\n```\n\n### Complex Example\n```python\nfrom objectify import dict_to_object\n\n\nclass Address:\n street: str\n city: str\n zip_code: int\n\n\nclass User:\n name: str\n age: int\n address: list[Address]\n\n\ndata = {\n 'name': 'Alice',\n 'age': 30,\n 'address': [\n {'street': '123 Main St', 'city': 'Springfield', 'zip_code': 12345},\n {'street': '456 Elm St', 'city': 'Rivertown', 'zip_code': 54321}\n ]\n}\n\nuser = dict_to_object(data, User)\nprint(user.name) # 'Alice'\nprint(user.age) # 30\nprint(user.address[0].street) # '123 Main St'\nprint(user.address[1].city) # 'Rivertown'\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "A package to convert a dictionary to an object to achieve similar syntax and auto-completion as in TypeScript when annotating objects with interfaces.",
"version": "1.0.0",
"project_urls": {
"Changelog": "https://github.com/anviks/dict-to-obj/blob/master/CHANGELOG.md",
"Homepage": "https://github.com/anviks/dict-to-obj",
"Issues": "https://github.com/anviks/dict-to-obj/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "4f17d8588d47fc3151437f931f94e0a980257418a6ee3a7ee9c035976a4fad23",
"md5": "de07adcffb5942d167559de29cf88c18",
"sha256": "cf52206b5e6fdcb8c55671740828f2551fcb4fd0f7205f03ac511dbe4b7e54a9"
},
"downloads": -1,
"filename": "objectify-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "de07adcffb5942d167559de29cf88c18",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 5003,
"upload_time": "2025-02-08T23:19:55",
"upload_time_iso_8601": "2025-02-08T23:19:55.955305Z",
"url": "https://files.pythonhosted.org/packages/4f/17/d8588d47fc3151437f931f94e0a980257418a6ee3a7ee9c035976a4fad23/objectify-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "7a6fb8c6930ce6a50e276a982094c489f2c1a4e68fa3cd593c30f0dfe0ff1753",
"md5": "a7ed0c1152fcfa283f0f3764fa8f62c0",
"sha256": "3b3b5f53f0d5f6eb5b1abbff285126249948b2143f6ebf192a09cfadcfe14504"
},
"downloads": -1,
"filename": "objectify-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "a7ed0c1152fcfa283f0f3764fa8f62c0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 7221,
"upload_time": "2025-02-08T23:19:57",
"upload_time_iso_8601": "2025-02-08T23:19:57.047123Z",
"url": "https://files.pythonhosted.org/packages/7a/6f/b8c6930ce6a50e276a982094c489f2c1a4e68fa3cd593c30f0dfe0ff1753/objectify-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-08 23:19:57",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "anviks",
"github_project": "dict-to-obj",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "objectify"
}