Name | safeconfig JSON |
Version |
1.0.7
JSON |
| download |
home_page | https://github.com/vahidk/safeconfig |
Summary | Structured, flexible, and secure configuration management for Python with CLI support. |
upload_time | 2024-07-11 05:46:43 |
maintainer | None |
docs_url | None |
author | Vahid Kazemi |
requires_python | None |
license | MIT |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<h1 align="center">
<img src="https://raw.githubusercontent.com/vahidk/safeconfig/main/media/safeconfig.png" alt="SafeConfig Library" width="256">
</h1><br>
[](https://pypi.org/project/safeconfig/)
## Overview
SafeConfig provides a structured and flexible way to define, validate, and manage configurations for your Python applications. It supports hierarchical configuration structures with fields that can be variables, arrays, or nested structures. It also includes a command-line interface (CLI) parser to easily override configurations via CLI arguments.
## Features
- Define hierarchical configurations with nested structures.
- Support for variable, array, and struct field types.
- Validation of field values.
- Load and save configurations from/to JSON and YAML files.
- Override configurations using command-line arguments.
## Installation
To install the library, simply run:
```bash
pip install safeconfig
```
## Usage
### Defining a Configuration Schema
To define a schema, create a class that inherits from `Struct` and define the fields using `Variable`, `Array`, and other `Struct` subclasses.
```python
from safeconfig import Variable, Array, Struct
class TrainerConfig(Struct):
learning_rate = Variable(float, description="Learning rate for training", default=0.001)
epochs = Variable(int, description="Number of training epochs", optional=True)
data = Array(str, description="Dataset paths.")
```
### Create a Configuration File
Here is an example configuration file in YAML format:
```yaml
learning_rate: 0.01
epochs: 10
data:
- "/data/dataset1"
- "/data/dataset2"
```
### Loading and Saving Configuration Files
You can load the configuration from a JSON or YAML file using the `read` method.
```python
config = TrainerConfig()
config.read("path/to/config.yaml")
```
Note that the Struct will be used as a schema to validate all the attributes.
Similarly, you can save the configuration to a JSON or YAML file using the `write` method.
```python
config.write("path/to/config.yaml")
```
### Accessing and Modifying the Configuration
You can access and modify the configuration fields directly or using the `set` and `get` methods.
```python
# Accessing fields
print(config.learning_rate)
print(config.data)
# Modifying fields
config.learning_rate = 0.01
config.data[0] = '/path/to/data'
# Using set and get methods
config.set({'learning_rate': 0.01, 'data': ['/path/to/data']})
print(config.get())
```
### Using the CLI Parser
The CLI parser allows you to override configuration values using command-line arguments. It also supports loading configurations from a file specified via CLI.
```python
from safeconfig import CLIParser
if __name__ == "__main__":
parser = CLIParser(TrainerConfig())
config = parser.parse_args()
print(config)
```
Now you can load configuration files by passing a config file path or override fields with corresponding command line arguments:
```bash
python your_script.py --config path/to/config.yaml \
--learning_rate 0.01 \
--data /data/dataset1 /data/dataset2
--print_config
```
Help command will is automatically generated based on the schema:
```bash
python your_script.py --help
```
## Contributing
Contributions are welcome. Please fork the repository and submit a pull request with your changes.
## License
This project is licensed under the MIT License. See the `LICENSE` file for more details.
Raw data
{
"_id": null,
"home_page": "https://github.com/vahidk/safeconfig",
"name": "safeconfig",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Vahid Kazemi",
"author_email": "vkazemi@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/74/4f/289a89031e3c91378cee247c98d673b926d90e4cda9cb8cc4042f79c5ad5/safeconfig-1.0.7.tar.gz",
"platform": null,
"description": "<h1 align=\"center\">\n<img src=\"https://raw.githubusercontent.com/vahidk/safeconfig/main/media/safeconfig.png\" alt=\"SafeConfig Library\" width=\"256\">\n</h1><br>\n\n\n[](https://pypi.org/project/safeconfig/)\n\n\n## Overview\n\nSafeConfig provides a structured and flexible way to define, validate, and manage configurations for your Python applications. It supports hierarchical configuration structures with fields that can be variables, arrays, or nested structures. It also includes a command-line interface (CLI) parser to easily override configurations via CLI arguments.\n\n## Features\n\n- Define hierarchical configurations with nested structures.\n- Support for variable, array, and struct field types.\n- Validation of field values.\n- Load and save configurations from/to JSON and YAML files.\n- Override configurations using command-line arguments.\n\n## Installation\n\nTo install the library, simply run:\n\n```bash\npip install safeconfig\n```\n\n## Usage\n\n### Defining a Configuration Schema\n\nTo define a schema, create a class that inherits from `Struct` and define the fields using `Variable`, `Array`, and other `Struct` subclasses.\n\n```python\nfrom safeconfig import Variable, Array, Struct\n\nclass TrainerConfig(Struct):\n learning_rate = Variable(float, description=\"Learning rate for training\", default=0.001)\n epochs = Variable(int, description=\"Number of training epochs\", optional=True)\n data = Array(str, description=\"Dataset paths.\")\n```\n\n### Create a Configuration File\n\nHere is an example configuration file in YAML format:\n\n```yaml\nlearning_rate: 0.01\nepochs: 10\ndata:\n - \"/data/dataset1\"\n - \"/data/dataset2\"\n```\n\n### Loading and Saving Configuration Files\n\nYou can load the configuration from a JSON or YAML file using the `read` method.\n\n```python\nconfig = TrainerConfig()\nconfig.read(\"path/to/config.yaml\")\n```\n\nNote that the Struct will be used as a schema to validate all the attributes.\n\nSimilarly, you can save the configuration to a JSON or YAML file using the `write` method.\n\n```python\nconfig.write(\"path/to/config.yaml\")\n```\n\n### Accessing and Modifying the Configuration\n\nYou can access and modify the configuration fields directly or using the `set` and `get` methods.\n\n```python\n# Accessing fields\nprint(config.learning_rate)\nprint(config.data)\n\n# Modifying fields\nconfig.learning_rate = 0.01\nconfig.data[0] = '/path/to/data'\n\n# Using set and get methods\nconfig.set({'learning_rate': 0.01, 'data': ['/path/to/data']})\nprint(config.get())\n```\n\n### Using the CLI Parser\n\nThe CLI parser allows you to override configuration values using command-line arguments. It also supports loading configurations from a file specified via CLI.\n\n```python\nfrom safeconfig import CLIParser\n\nif __name__ == \"__main__\":\n parser = CLIParser(TrainerConfig())\n config = parser.parse_args()\n print(config)\n```\n\nNow you can load configuration files by passing a config file path or override fields with corresponding command line arguments:\n\n```bash\npython your_script.py --config path/to/config.yaml \\\n--learning_rate 0.01 \\\n--data /data/dataset1 /data/dataset2\n--print_config\n```\n\nHelp command will is automatically generated based on the schema:\n\n```bash\npython your_script.py --help\n```\n\n## Contributing\n\nContributions are welcome. Please fork the repository and submit a pull request with your changes.\n\n## License\n\nThis project is licensed under the MIT License. See the `LICENSE` file for more details.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Structured, flexible, and secure configuration management for Python with CLI support.",
"version": "1.0.7",
"project_urls": {
"Homepage": "https://github.com/vahidk/safeconfig"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "744f289a89031e3c91378cee247c98d673b926d90e4cda9cb8cc4042f79c5ad5",
"md5": "26d72f2766d496059efa7dc25ccf932e",
"sha256": "e3cdc846522e8966aa9e4b5dfd618f5ae662a2d9213633f9b34c4e0adb8fa3da"
},
"downloads": -1,
"filename": "safeconfig-1.0.7.tar.gz",
"has_sig": false,
"md5_digest": "26d72f2766d496059efa7dc25ccf932e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11137,
"upload_time": "2024-07-11T05:46:43",
"upload_time_iso_8601": "2024-07-11T05:46:43.942797Z",
"url": "https://files.pythonhosted.org/packages/74/4f/289a89031e3c91378cee247c98d673b926d90e4cda9cb8cc4042f79c5ad5/safeconfig-1.0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-11 05:46:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "vahidk",
"github_project": "safeconfig",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "safeconfig"
}