# Pydantic File Settings
[![PyPI version](https://badge.fury.io/py/pydantic-file-settings.svg)](https://badge.fury.io/py/pydantic-file-settings)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python Versions](https://img.shields.io/pypi/pyversions/pydantic-file-settings.svg)](https://pypi.org/project/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
```bash
pip install pydantic-file-settings
```
## Quick Start
Here's a simple example to get you started:
```python
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:
```python
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:
```python
settings = MyAppSettings.create("./config")
```
### Loading Settings
To load existing settings:
```python
settings = MyAppSettings.load("./config")
```
### Saving Settings
After modifying settings, save them back to the file:
```python
settings.app_name = "New App Name"
settings.save()
```
### Checking if Settings Exist
You can check if a settings file exists:
```python
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`:
```python
class MyAppSettings(FileSettings):
api_key: str = Field(default="", env="MY_APP_API_KEY")
```
### Validation
Leverage Pydantic's validation features:
```python
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](LICENSE) file for details.
## Acknowledgements
- [Pydantic](https://pydantic-docs.helpmanual.io/) for the awesome data validation library
- [Ruslan Iskov](https://github.com/ruslan-rv-ua) for creating and maintaining this project
---
Made with ❤️ by Ruslan Iskov
Raw data
{
"_id": null,
"home_page": null,
"name": "pydantic-file-settings",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "configuration, file, file-based, json, pydantic, settings",
"author": null,
"author_email": "Ruslan Iskov <ruslan.rv.ua@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/43/fc/3579f22b0fb9d54fbd1b34d03917824e18b54613748aec40d04fc808dca5/pydantic_file_settings-0.0.1.tar.gz",
"platform": null,
"description": "# Pydantic File Settings\n\n[![PyPI version](https://badge.fury.io/py/pydantic-file-settings.svg)](https://badge.fury.io/py/pydantic-file-settings)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Python Versions](https://img.shields.io/pypi/pyversions/pydantic-file-settings.svg)](https://pypi.org/project/pydantic-file-settings/)\n\nManage your application settings with Pydantic models, storing them in a JSON file.\n\n## Features\n\n- \ud83d\ude80 Easy to use: Extend from `FileSettings` and you're good to go!\n- \ud83d\udd12 Type-safe: Leverage Pydantic's powerful type checking and validation\n- \ud83d\udcbe File-based: Store your settings in a JSON file for easy management\n- \ud83d\udd04 Auto-reload: Automatically load settings from file\n- \ud83d\udcaa Flexible: Create, load, and save settings with ease\n\n## Installation\n\n```bash\npip install pydantic-file-settings\n```\n\n## Quick Start\n\nHere's a simple example to get you started:\n\n```python\nfrom pydantic_file_settings import FileSettings\nfrom pydantic import Field\n\nclass MyAppSettings(FileSettings):\n app_name: str = \"My Awesome App\"\n debug_mode: bool = False\n max_connections: int = Field(default=100, ge=1, le=1000)\n\n# Create settings\nsettings = MyAppSettings.create(\"./config\")\n\n# Load existing settings\nsettings = MyAppSettings.load(\"./config\")\n\n# Modify and save settings\nsettings.debug_mode = True\nsettings.save()\n```\n\n## Usage\n\n### Defining Your Settings\n\nInherit from `FileSettings` and define your settings as class attributes:\n\n```python\nfrom pydantic_file_settings import FileSettings\nfrom pydantic import Field\n\nclass MyAppSettings(FileSettings):\n app_name: str\n debug_mode: bool = False\n max_connections: int = Field(default=100, ge=1, le=1000)\n api_key: str = Field(default=\"\", env=\"MY_APP_API_KEY\")\n```\n\n### Creating Settings\n\nTo create a new settings file:\n\n```python\nsettings = MyAppSettings.create(\"./config\")\n```\n\n### Loading Settings\n\nTo load existing settings:\n\n```python\nsettings = MyAppSettings.load(\"./config\")\n```\n\n### Saving Settings\n\nAfter modifying settings, save them back to the file:\n\n```python\nsettings.app_name = \"New App Name\"\nsettings.save()\n```\n\n### Checking if Settings Exist\n\nYou can check if a settings file exists:\n\n```python\nif MyAppSettings.exists(\"./config\"):\n print(\"Settings file found!\")\n```\n\n## Advanced Usage\n\n### Environment Variables\n\nPydantic File Settings supports loading values from environment variables. Use the `env` parameter in `Field`:\n\n```python\nclass MyAppSettings(FileSettings):\n api_key: str = Field(default=\"\", env=\"MY_APP_API_KEY\")\n```\n\n### Validation\n\nLeverage Pydantic's validation features:\n\n```python\nfrom pydantic import Field, validator\n\nclass MyAppSettings(FileSettings):\n port: int = Field(default=8000, ge=1024, le=65535)\n \n @validator(\"port\")\n def port_must_be_even(cls, v):\n if v % 2 != 0:\n raise ValueError(\"Port must be an even number\")\n return v\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Acknowledgements\n\n- [Pydantic](https://pydantic-docs.helpmanual.io/) for the awesome data validation library\n- [Ruslan Iskov](https://github.com/ruslan-rv-ua) for creating and maintaining this project\n\n---\n\nMade with \u2764\ufe0f by Ruslan Iskov",
"bugtrack_url": null,
"license": "MIT",
"summary": "Manage your application settings with Pydantic models, storing them in JSON file.",
"version": "0.0.1",
"project_urls": {
"Bug Tracker": "https://github.com/ruslan-rv-ua/pydantic-file-settings/issues",
"Documentation": "https://github.com/ruslan-rv-ua/pydantic-file-settings#readme",
"Homepage": "https://github.com/ruslan-rv-ua/pydantic-file-settings",
"Repository": "https://github.com/ruslan-rv-ua/pydantic-file-settings.git"
},
"split_keywords": [
"configuration",
" file",
" file-based",
" json",
" pydantic",
" settings"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4467d41d74da3d1d86934513e5adf899c78cb6db4b56a8427d3d4f3cb0e972b7",
"md5": "0d82a27cb00ee764ffeae108330b493f",
"sha256": "17ca892e9cc802b9e702aa3db1b208e2631068d94872220a129f87a6b0cfcbc8"
},
"downloads": -1,
"filename": "pydantic_file_settings-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0d82a27cb00ee764ffeae108330b493f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 4788,
"upload_time": "2024-07-05T05:03:54",
"upload_time_iso_8601": "2024-07-05T05:03:54.047030Z",
"url": "https://files.pythonhosted.org/packages/44/67/d41d74da3d1d86934513e5adf899c78cb6db4b56a8427d3d4f3cb0e972b7/pydantic_file_settings-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "43fc3579f22b0fb9d54fbd1b34d03917824e18b54613748aec40d04fc808dca5",
"md5": "a50eb116b8aa99c8514797b4bc99b147",
"sha256": "9f41306cb2830026c0bf3e47d8f42c72309dcd76e6ba4e737524b2400313be86"
},
"downloads": -1,
"filename": "pydantic_file_settings-0.0.1.tar.gz",
"has_sig": false,
"md5_digest": "a50eb116b8aa99c8514797b4bc99b147",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 6124,
"upload_time": "2024-07-05T05:03:55",
"upload_time_iso_8601": "2024-07-05T05:03:55.911821Z",
"url": "https://files.pythonhosted.org/packages/43/fc/3579f22b0fb9d54fbd1b34d03917824e18b54613748aec40d04fc808dca5/pydantic_file_settings-0.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-05 05:03:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ruslan-rv-ua",
"github_project": "pydantic-file-settings",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pydantic-file-settings"
}