Skip to main content

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

Project description

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

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_file_settings-0.0.1.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

pydantic_file_settings-0.0.1-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

Supported by

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