Name | noconf JSON |
Version |
1.0
JSON |
| download |
home_page | https://github.com/dnouri/noconf |
Summary | Component-based configuration for everyone! |
upload_time | 2023-08-25 11:54:38 |
maintainer | |
docs_url | None |
author | Daniel Nouri |
requires_python | |
license | |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
======
noconf
======
Key features
============
1. **Flexible configuration**: noconf allows you to configure your
Python applications using configuration files that use Python
syntax.
2. **Multiple configuration files**: noconf allows you to split your
configuration across multiple files, making it easier to manage and
update your configuration as your application grows.
3. **Configuration referencing**: noconf allows you to reference other
parts of your configuration from within your configuration,
avoiding duplication and keeping your configuration DRY (Don't
Repeat Yourself).
4. **Component-based programming**: noconf enables you to initialize
classes using your configuration, making it easy to compose complex
systems out of smaller, reusable components. This promotes code
reuse and maintainability.
Usage
=====
At a minimum, noconf allows us to read configuration that's stored in
files with Python syntax, where the file contains exactly one top
level dictionary.
Let's first write a very simple config file:
>>> config1_fname = folder / "config1.py"
>>> config1_fname.write_text("""
... {'key1': 'value1', 'key2': ['value2']}
... """)
40
>>> from noconf import load
>>> load(config1_fname)
{'key1': 'value1', 'key2': ['value2']}
We can also chain configuration files, that is, load multiple
configuration files and merge them, where contents of the files later
in the list of files to load will take precedence:
>>> config2_fname = folder / "config2.py"
>>> config2_fname.write_text("""
... {'key1': 'new', 'key3': {'__copy__': 'key2'}}
... """)
47
>>> load((config1_fname, config2_fname)) # a tuple of config files
{'key1': 'new', 'key2': ['value2'], 'key3': ['value2']}
Notice that in the above example, we also used a `'__copy__'` feature,
which allows us to refer to other parts in the configuration, and to
avoid duplication.
We can also instantiate classes directly from the configuration.
Let's create a configuration file that instantiates a Python logging
FileHandler class. We're also going to configure the FileHandler with
a filename that's passed as an environment variable. We use the
special `environ` variable in noconf to access environment variables:
>>> setenv("LOGFILE", str(folder / "mylogfile.txt"))
>>> config3_fname = folder / "config3.py"
>>> config3_fname.write_text("""
... {
... 'handlers': [
... {
... '!': 'logging.FileHandler',
... 'filename': environ['LOGFILE'],
... },
... ],
... }
... """)
135
>>> config = load(config3_fname)
>>> filehandler = config['handlers'][0]
>>> from pathlib import Path
>>> Path(filehandler.baseFilename).parts[-1]
'mylogfile.txt'
Raw data
{
"_id": null,
"home_page": "https://github.com/dnouri/noconf",
"name": "noconf",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Daniel Nouri",
"author_email": "daniel.nouri@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/29/70/eff04f7a719a2ff74bb5a1d66aa0a469878d6156fcdf78dc7cdab1ed1f89/noconf-1.0.tar.gz",
"platform": null,
"description": "======\nnoconf\n======\n\nKey features\n============\n\n1. **Flexible configuration**: noconf allows you to configure your\n Python applications using configuration files that use Python\n syntax.\n\n2. **Multiple configuration files**: noconf allows you to split your\n configuration across multiple files, making it easier to manage and\n update your configuration as your application grows.\n\n3. **Configuration referencing**: noconf allows you to reference other\n parts of your configuration from within your configuration,\n avoiding duplication and keeping your configuration DRY (Don't\n Repeat Yourself).\n\n4. **Component-based programming**: noconf enables you to initialize\n classes using your configuration, making it easy to compose complex\n systems out of smaller, reusable components. This promotes code\n reuse and maintainability.\n\nUsage\n=====\n\nAt a minimum, noconf allows us to read configuration that's stored in\nfiles with Python syntax, where the file contains exactly one top\nlevel dictionary.\n\nLet's first write a very simple config file:\n\n>>> config1_fname = folder / \"config1.py\"\n>>> config1_fname.write_text(\"\"\"\n... {'key1': 'value1', 'key2': ['value2']}\n... \"\"\")\n40\n\n>>> from noconf import load\n>>> load(config1_fname)\n{'key1': 'value1', 'key2': ['value2']}\n\nWe can also chain configuration files, that is, load multiple\nconfiguration files and merge them, where contents of the files later\nin the list of files to load will take precedence:\n\n>>> config2_fname = folder / \"config2.py\"\n>>> config2_fname.write_text(\"\"\"\n... {'key1': 'new', 'key3': {'__copy__': 'key2'}}\n... \"\"\")\n47\n>>> load((config1_fname, config2_fname)) # a tuple of config files\n{'key1': 'new', 'key2': ['value2'], 'key3': ['value2']}\n\nNotice that in the above example, we also used a `'__copy__'` feature,\nwhich allows us to refer to other parts in the configuration, and to\navoid duplication.\n\nWe can also instantiate classes directly from the configuration.\nLet's create a configuration file that instantiates a Python logging\nFileHandler class. We're also going to configure the FileHandler with\na filename that's passed as an environment variable. We use the\nspecial `environ` variable in noconf to access environment variables:\n\n>>> setenv(\"LOGFILE\", str(folder / \"mylogfile.txt\"))\n\n>>> config3_fname = folder / \"config3.py\"\n>>> config3_fname.write_text(\"\"\"\n... {\n... 'handlers': [\n... {\n... '!': 'logging.FileHandler',\n... 'filename': environ['LOGFILE'],\n... },\n... ],\n... }\n... \"\"\")\n135\n>>> config = load(config3_fname)\n>>> filehandler = config['handlers'][0]\n\n>>> from pathlib import Path\n>>> Path(filehandler.baseFilename).parts[-1]\n'mylogfile.txt'",
"bugtrack_url": null,
"license": "",
"summary": "Component-based configuration for everyone!",
"version": "1.0",
"project_urls": {
"Homepage": "https://github.com/dnouri/noconf"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2970eff04f7a719a2ff74bb5a1d66aa0a469878d6156fcdf78dc7cdab1ed1f89",
"md5": "0c9e5ae3bc6160f53904aa3f8efb769f",
"sha256": "138960fec7641fb854f229bc20b4bd96a017f4c15d90d9a1cf8bdf5b1954a00b"
},
"downloads": -1,
"filename": "noconf-1.0.tar.gz",
"has_sig": false,
"md5_digest": "0c9e5ae3bc6160f53904aa3f8efb769f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8095,
"upload_time": "2023-08-25T11:54:38",
"upload_time_iso_8601": "2023-08-25T11:54:38.815730Z",
"url": "https://files.pythonhosted.org/packages/29/70/eff04f7a719a2ff74bb5a1d66aa0a469878d6156fcdf78dc7cdab1ed1f89/noconf-1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-25 11:54:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dnouri",
"github_project": "noconf",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "noconf"
}