A pydantic model subclass that implements Python's dictionary interface.
Project description
pydantic_dict
A pydantic model subclass that implements Python's dictionary interface.
Example:
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:
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
pip install pydantic_dict
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_dict-0.0.3.tar.gz.
File metadata
- Download URL: pydantic_dict-0.0.3.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af492397a4fa255aa9e03105a7f57dc69effcd5e7be573b2e31374c3f9d1e429
|
|
| MD5 |
62fa2917f77fac87cd830e1e3cd6be22
|
|
| BLAKE2b-256 |
88d654df197ed4a9d33bb02645004c1aadd3b50760bb03947fb18740625349ab
|
File details
Details for the file pydantic_dict-0.0.3-py3-none-any.whl.
File metadata
- Download URL: pydantic_dict-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bf99ee97cd314e871f306b79e29de711a4897b34d9c8810c09081b9f9259c74
|
|
| MD5 |
6d6ba24db633c2091b1f8579c02d8f6d
|
|
| BLAKE2b-256 |
52f3b2ec888024b29b923a3bf0d86c5a182687bb20712ce3b66149fbdfb94933
|