A simple library that facilitates the use of Pydantic models for configuration
Project description
pydrantic
What is This?
pydrantic is a simple library that makes it easier to use Pydantic models as configs for machine learning experiments.
The library provides utilities that Pydantic models with features inspired by other configuration libraries like (e.g. Hydra). These include:
- Command-line overrides: Users can override config fields from the command line.
- Serialization: Users can serialize configs to YAML, pickle, or dill files.
- Variables: Users can define variables that can be used in the config.
- Launching sweeps: Users can launch sweeps from the command line.
- Object instantiation: Users can instantiate Pydantic models from the command line or from a file.
Why use Pydantic models as configs?
Installation
To install the latest release from PyPI:
git clone https://github.com/seyuboglu/pydrantic.git
cd pydrantic
pip install -e .
Usage
The Basics
pydrantic is a fork of Jordan's awesome pydra library.
pydrantic inherits the same CLI syntax from pydra, but uses Pydantic models for config classes.
So, in some sense, pydrantic is just a more opinionated version of pydra.
The choice of Pydantic models for config classes encourages a strict separation between config and functionality. It also enables a few nice features like safer serialization, built-in type validation, and dependencies between fields in the config.
import pydrantic
class MyConfig(pydrantic.RunConfig):
foo: int = 5
bar: int = 10
def run(self):
print(f"foo: {self.foo}")
print(f"bar: {self.bar}")
if __name__ == "__main__":
config = MyConfig()
pydrantic.main(config)
You can run this script with:
python script.py
python script.py -u foo=10
Nested Configs
Configs can contain nested Pydantic models or dictionaries.
from pydrantic import RunConfig
from pydantic import BaseModel
class InnerConfig(BaseModel):
x: int = 1
y: int = 2
class MyConfig(RunConfig):
inner: InnerConfig = InnerConfig()
def run(self):
print(f"Inner x: {self.inner.x}")
print(f"Inner y: {self.inner.y}")
if __name__ == "__main__":
config = MyConfig()
main(config)
You can access nested fields from the command line using dots:
python script.py -u inner.x=5
--in
You can also temporarily scope your assignments to a nested config using the --in flag. Use in-- to end the scoping region. Using the above example:
python script.py --in inner x=5 y=10 in-- --in d a=100 b=101 in--
Both will set the same variable.
Serializing Configs
To produce a human-readable serialization of your config, you can use the to_dict() method. We also provide a few helper functions to save configs to YAML, pickle, or dill files.
import pydrantic
class MyConfig(pydrantic.Config):
def __init__(self):
super().__init__()
self.x = 5
self.y = 10
@pydrantic.main(MyConfig)
def main(config: MyConfig):
as_dict = config.to_dict()
print(as_dict)
pydrantic.save_yaml(as_dict, "conf.yaml")
pydrantic.save_pickle(as_dict, "conf.pkl")
pydrantic.save_dill(as_dict, "conf.dill")
if __name__ == "__main__":
main()
Running Tests
To run the repo's test suite, use:
python -m unittest discover tests
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 pydrantic-0.0.2.tar.gz.
File metadata
- Download URL: pydrantic-0.0.2.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39063bf8999511d9246bfc29c6d45f56eb7acb73a1bcd04b5353b9f5501cfb53
|
|
| MD5 |
cfd43b8e7d74b685a1b3454ca6926d17
|
|
| BLAKE2b-256 |
3fe601b04a244749e48ae296c329d8cae5748f765734efe3db36d39444c8a435
|
File details
Details for the file pydrantic-0.0.2-py3-none-any.whl.
File metadata
- Download URL: pydrantic-0.0.2-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bc18b5ec623baa9f55a5471577372953b8e7bf2c37ac120c48817de5897d1b8
|
|
| MD5 |
bf3841e86a685e8d722b67271b14d02a
|
|
| BLAKE2b-256 |
75e67688b92bb70cdfb08d5cb401b7a548491c9721f256304e3dc093670dfc92
|