# pydantic_dict
A `pydantic` model subclass that implements Python's dictionary interface.
**Example:**
```python
from pydantic_dict import BaseModelDict
class User(BaseModelDict):
id: int
name: str = "Jane Doe"
user = User(id=42)
user["session_id"] = "95607c42-250a-4913-9dfb-00eb6535e685"
assert user.session_id == "95607c42-250a-4913-9dfb-00eb6535e685"
assert user["session_id"] == "95607c42-250a-4913-9dfb-00eb6535e685"
user.pop("session_id")
assert "session_id" not in user
assert user.get("last_name", None) is None
user.update({"email": "jane.doe@email.com"})
print(user.json())
# >>> {"id": 42, "name": "Jane Doe", "email": "jane.doe@email.com"}
user.clear() # fields are NOT removed. only non-fields are removed
print(user.json())
# >>> {"id": 42, "name": "Jane Doe"}
user.setdefault("last_active", "2023-01-01T19:56:10Z")
del user["last_active"]
```
**`Unset` marker type**
The `Unset` marker type provides a way to "mark" that an optional model field
is by default not set and is not required to construct the model. This enables
more semantic usage of built-in dict methods like `get()` and `setdefault()`
that can return or set a default value. Likewise, fields that are `Unset` are
not considered to be members of a `BaseModelDict` dictionary (e.g.
`"unset_field" not in model_dict`) and are not included in `__iter__()`,
`keys()`, `values()`, or `len(model_dict)`. This feature is especially useful
when refactoring existing code to use pydantic.
**Example:**
```python
from pydantic_dict import BaseModelDict, Unset
from typing import Optional
class User(BaseModelDict):
id: int
name: str = "Jane Doe"
email: Optional[str] = Unset
user = User(id=42)
assert "email" not in user
user["email"] # raises KeyError
assert len(user) == 2
assert set(user.keys()) == {"id", "name"}
user.setdefault("email", f"{user.id}@service.com") # returns `42@service.com`
assert "email" in user
```
## Install
```shell
pip install pydantic_dict
```
Raw data
{
"_id": null,
"home_page": "",
"name": "pydantic-dict",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "pydantic,serialization,deserialization,json",
"author": "",
"author_email": "Austin Raney <aaraney@protonmail.com>",
"download_url": "https://files.pythonhosted.org/packages/88/d6/54df197ed4a9d33bb02645004c1aadd3b50760bb03947fb18740625349ab/pydantic_dict-0.0.3.tar.gz",
"platform": null,
"description": "# pydantic_dict\n\nA `pydantic` model subclass that implements Python's dictionary interface.\n\n**Example:**\n\n```python\nfrom pydantic_dict import BaseModelDict\n\nclass User(BaseModelDict):\n id: int\n name: str = \"Jane Doe\"\n\nuser = User(id=42)\n\nuser[\"session_id\"] = \"95607c42-250a-4913-9dfb-00eb6535e685\"\nassert user.session_id == \"95607c42-250a-4913-9dfb-00eb6535e685\"\nassert user[\"session_id\"] == \"95607c42-250a-4913-9dfb-00eb6535e685\"\nuser.pop(\"session_id\")\nassert \"session_id\" not in user\n\nassert user.get(\"last_name\", None) is None\n\nuser.update({\"email\": \"jane.doe@email.com\"})\nprint(user.json())\n# >>> {\"id\": 42, \"name\": \"Jane Doe\", \"email\": \"jane.doe@email.com\"}\n\nuser.clear() # fields are NOT removed. only non-fields are removed\nprint(user.json())\n# >>> {\"id\": 42, \"name\": \"Jane Doe\"}\n\nuser.setdefault(\"last_active\", \"2023-01-01T19:56:10Z\")\ndel user[\"last_active\"]\n```\n\n**`Unset` marker type**\n\nThe `Unset` marker type provides a way to \"mark\" that an optional model field\nis by default not set and is not required to construct the model. This enables\nmore semantic usage of built-in dict methods like `get()` and `setdefault()`\nthat can return or set a default value. Likewise, fields that are `Unset` are\nnot considered to be members of a `BaseModelDict` dictionary (e.g.\n`\"unset_field\" not in model_dict`) and are not included in `__iter__()`,\n`keys()`, `values()`, or `len(model_dict)`. This feature is especially useful\nwhen refactoring existing code to use pydantic.\n\n**Example:**\n\n\n```python\nfrom pydantic_dict import BaseModelDict, Unset\nfrom typing import Optional\n\nclass User(BaseModelDict):\n id: int\n name: str = \"Jane Doe\"\n email: Optional[str] = Unset\n\nuser = User(id=42)\n\nassert \"email\" not in user\nuser[\"email\"] # raises KeyError\n\nassert len(user) == 2\nassert set(user.keys()) == {\"id\", \"name\"}\n\nuser.setdefault(\"email\", f\"{user.id}@service.com\") # returns `42@service.com`\nassert \"email\" in user\n```\n\n## Install\n\n```shell\npip install pydantic_dict\n```\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "A pydantic model subclass that implements Python's dictionary interface.",
"version": "0.0.3",
"project_urls": null,
"split_keywords": [
"pydantic",
"serialization",
"deserialization",
"json"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "52f3b2ec888024b29b923a3bf0d86c5a182687bb20712ce3b66149fbdfb94933",
"md5": "6d6ba24db633c2091b1f8579c02d8f6d",
"sha256": "1bf99ee97cd314e871f306b79e29de711a4897b34d9c8810c09081b9f9259c74"
},
"downloads": -1,
"filename": "pydantic_dict-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6d6ba24db633c2091b1f8579c02d8f6d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 7691,
"upload_time": "2023-06-30T17:19:21",
"upload_time_iso_8601": "2023-06-30T17:19:21.892492Z",
"url": "https://files.pythonhosted.org/packages/52/f3/b2ec888024b29b923a3bf0d86c5a182687bb20712ce3b66149fbdfb94933/pydantic_dict-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "88d654df197ed4a9d33bb02645004c1aadd3b50760bb03947fb18740625349ab",
"md5": "62fa2917f77fac87cd830e1e3cd6be22",
"sha256": "af492397a4fa255aa9e03105a7f57dc69effcd5e7be573b2e31374c3f9d1e429"
},
"downloads": -1,
"filename": "pydantic_dict-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "62fa2917f77fac87cd830e1e3cd6be22",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 13736,
"upload_time": "2023-06-30T17:19:23",
"upload_time_iso_8601": "2023-06-30T17:19:23.001957Z",
"url": "https://files.pythonhosted.org/packages/88/d6/54df197ed4a9d33bb02645004c1aadd3b50760bb03947fb18740625349ab/pydantic_dict-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-30 17:19:23",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pydantic-dict"
}