pydantic-file-settings

Manage your application settings with Pydantic models, storing them in JSON file.


Keywords
configuration, file, file-based, json, pydantic, settings
License
MIT
Install
pip install pydantic-file-settings==0.0.1

Documentation

Pydantic File Settings

PyPI version License: MIT Python Versions

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