Manage your application settings with Pydantic models, storing them in JSON file.
Project description
Pydantic File Settings
Manage your application settings with Pydantic models, storing them in a JSON file.
Features
- 🚀 Easy to use: Extend from
FileSettings
and you're good to go! - 🔒 Type-safe: Leverage Pydantic's powerful type checking and validation
- 💾 File-based: Store your settings in a JSON file for easy management
- 🔄 Auto-reload: Automatically load settings from file
- 💪 Flexible: Create, load, and save settings with ease
Installation
pip install pydantic-file-settings
Quick Start
Here's a simple example to get you started:
from pydantic_file_settings import FileSettings
from pydantic import Field
class MyAppSettings(FileSettings):
app_name: str = "My Awesome App"
debug_mode: bool = False
max_connections: int = Field(default=100, ge=1, le=1000)
# Create settings
settings = MyAppSettings.create("./config")
# Load existing settings
settings = MyAppSettings.load("./config")
# Modify and save settings
settings.debug_mode = True
settings.save()
Usage
Defining Your Settings
Inherit from FileSettings
and define your settings as class attributes:
from pydantic_file_settings import FileSettings
from pydantic import Field
class MyAppSettings(FileSettings):
app_name: str
debug_mode: bool = False
max_connections: int = Field(default=100, ge=1, le=1000)
api_key: str = Field(default="", env="MY_APP_API_KEY")
Creating Settings
To create a new settings file:
settings = MyAppSettings.create("./config")
Loading Settings
To load existing settings:
settings = MyAppSettings.load("./config")
Saving Settings
After modifying settings, save them back to the file:
settings.app_name = "New App Name"
settings.save()
Checking if Settings Exist
You can check if a settings file exists:
if MyAppSettings.exists("./config"):
print("Settings file found!")
Advanced Usage
Environment Variables
Pydantic File Settings supports loading values from environment variables. Use the env
parameter in Field
:
class MyAppSettings(FileSettings):
api_key: str = Field(default="", env="MY_APP_API_KEY")
Validation
Leverage Pydantic's validation features:
from pydantic import Field, validator
class MyAppSettings(FileSettings):
port: int = Field(default=8000, ge=1024, le=65535)
@validator("port")
def port_must_be_even(cls, v):
if v % 2 != 0:
raise ValueError("Port must be an even number")
return v
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgements
- Pydantic for the awesome data validation library
- Ruslan Iskov for creating and maintaining this project
Made with ❤️ by Ruslan Iskov
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
Hashes for pydantic_file_settings-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f41306cb2830026c0bf3e47d8f42c72309dcd76e6ba4e737524b2400313be86 |
|
MD5 | a50eb116b8aa99c8514797b4bc99b147 |
|
BLAKE2b-256 | 43fc3579f22b0fb9d54fbd1b34d03917824e18b54613748aec40d04fc808dca5 |
Hashes for pydantic_file_settings-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17ca892e9cc802b9e702aa3db1b208e2631068d94872220a129f87a6b0cfcbc8 |
|
MD5 | 0d82a27cb00ee764ffeae108330b493f |
|
BLAKE2b-256 | 4467d41d74da3d1d86934513e5adf899c78cb6db4b56a8427d3d4f3cb0e972b7 |