Add your description here
Project description
pydantic-config-parser
This library provides special utility when using pydantic to parse config file.
Features ✨
- Override ONLY specified fields in config file.
- Parse YAML string to JSON object utility.
Override only specified fields in config file
Example 1
from pydantic import BaseModel
from pydantic_config_parser import recursive_override_each_fields
class ConfigRoot(BaseModel):
a: int = 0
b: str = "default"
override_json_s = """
{
"a": 1
}
"""
config = recursive_override_each_fields(ConfigRoot(), json_loads_as_dict(override_json_s))
config.model_dump_json()
# {
# "a": 1, # <- override
# "b": "default"
# }
Example 2: Override sub model
from pydantic import BaseModel
from pydantic_config_parser import recursive_override_each_fields
class ConfigSub(BaseModel):
a: int = 0
b: str = "default"
class ConfigRoot(BaseModel):
a: int = 0
b: str = "default"
c: list[int] = [1, 2, 3]
d: dict[str, int] = {"a": 1, "b": 2}
e: ConfigSub = ConfigSub()
override_json_s = """
{
"e": {
"a": 2
}
}
"""
config = recursive_override_each_fields(ConfigRoot(), json_loads_as_dict(override_json_s))
config.model_dump_json()
# {
# "a": 0,
# "b": "default",
# "c": [1, 2, 3],
# "d": {"a": 1, "b": 2},
# "e": {
# "a": 2, # <- override
# "b": "default"
# }
# }
Parse YAML utility
If you want to parse YAML string to JSON object, you can use yaml_loads_as_dict.
pip install pydantic-config-parser[yaml]
This will install PyYAML as a dependency.
from pydantic_config_parser import yaml_loads_as_json
class ConfigRoot(BaseModel):
a: int = 0
b: str = "default"
yaml_s = """
b: test
"""
dict_s = yaml_loads_as_json(yaml_s) # YAML feature is supported
config = recursive_override_each_fields(ConfigRoot(), dict_s)
config.model_dump_json()
# {
# "a": 0,
# "b": "test" # <- override
# }
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_config_parser-0.1.0.tar.gz.
File metadata
- Download URL: pydantic_config_parser-0.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf297c97eb861173a370da0f458ad79513e6248c6b0df3fa0cb06b597a028ea3
|
|
| MD5 |
cae2fb622f1f3aabef31677d4ae1f03a
|
|
| BLAKE2b-256 |
3ad4ced70502bb3f070b99ad873029a4a3d761cab3126cf546fe2d1256b3fd07
|
Provenance
The following attestation bundles were made for pydantic_config_parser-0.1.0.tar.gz:
Publisher:
release.yml on pollenjp/pydantic-config-parser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_config_parser-0.1.0.tar.gz -
Subject digest:
bf297c97eb861173a370da0f458ad79513e6248c6b0df3fa0cb06b597a028ea3 - Sigstore transparency entry: 151031372
- Sigstore integration time:
-
Permalink:
pollenjp/pydantic-config-parser@3927203ece084d212b0373a0507e2377af3381ef -
Branch / Tag:
refs/heads/main - Owner: https://github.com/pollenjp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3927203ece084d212b0373a0507e2377af3381ef -
Trigger Event:
push
-
Statement type:
File details
Details for the file pydantic_config_parser-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_config_parser-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
511fa42517fe1d6592cb7ba30b6c64a481831f415ad85595a671e82606c3915b
|
|
| MD5 |
630343d61535c9cb1984a816b5eb86e4
|
|
| BLAKE2b-256 |
7f00185bcf45b796b0ff79e1e5d5db69820bda1aac6821ebe88b1457b7bf0aba
|
Provenance
The following attestation bundles were made for pydantic_config_parser-0.1.0-py3-none-any.whl:
Publisher:
release.yml on pollenjp/pydantic-config-parser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_config_parser-0.1.0-py3-none-any.whl -
Subject digest:
511fa42517fe1d6592cb7ba30b6c64a481831f415ad85595a671e82606c3915b - Sigstore transparency entry: 151031373
- Sigstore integration time:
-
Permalink:
pollenjp/pydantic-config-parser@3927203ece084d212b0373a0507e2377af3381ef -
Branch / Tag:
refs/heads/main - Owner: https://github.com/pollenjp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3927203ece084d212b0373a0507e2377af3381ef -
Trigger Event:
push
-
Statement type: