Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_config_parser-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_config_parser-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

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

Hashes for pydantic_config_parser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bf297c97eb861173a370da0f458ad79513e6248c6b0df3fa0cb06b597a028ea3
MD5 cae2fb622f1f3aabef31677d4ae1f03a
BLAKE2b-256 3ad4ced70502bb3f070b99ad873029a4a3d761cab3126cf546fe2d1256b3fd07

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_config_parser-0.1.0.tar.gz:

Publisher: release.yml on pollenjp/pydantic-config-parser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydantic_config_parser-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_config_parser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 511fa42517fe1d6592cb7ba30b6c64a481831f415ad85595a671e82606c3915b
MD5 630343d61535c9cb1984a816b5eb86e4
BLAKE2b-256 7f00185bcf45b796b0ff79e1e5d5db69820bda1aac6821ebe88b1457b7bf0aba

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page