Skip to main content

Tests TestCoverage Release PythonVersion ReadTheDocs

PyFiguration

PyFiguration is a configuration tool for Python. It allows you to define which configuration are used from right inside your code. The PyFiguration command line tool helps you inspect which configurations are available, what the allowed values are, and helps to inspect the validity of a configuration file for a specific script.

Basic usage

In your code you can define which configurations should be available. This example creates a simple Flask server. The port on which the server should start depends on the configuration.

""" script.py
"""
from pyfiguration import conf
from flask import Flask


@conf.addIntField(
    field="server.port",
    description="The port on which the server will start",
    default=8000,
    minValue=80,
    maxValue=9999,
)
def startServer():
    app = Flask(__name__)
    port = conf["server"]["port"]
    print(f"Starting on port {port}")
    app.run(port=port)


if __name__ == "__main__":
    startServer()

You can use the PyFiguration command line tool to inspect this module/script:

$ pyfiguration inspect script -s script.py
The following options can be used in a configuration file for the module 'script.py':
server:
  port:
    allowedDataType: int
    default: 8000
    description: The port on which the server will start
    maxValue: 9999
    minValue: 80
    required: true

This tells you that the default value for server.port is 8000, and that it should be an integer between 80 and 9999. Running the script (python script.py) will start the server on the default port. Lets create a configuration file to overwrite the default:

# config.yaml
server:
  port: 5000

Now we can start the script again, pointing to the config file to use it:

$ python script.py --config ./config.yaml
Starting on port 5000
 * Serving Flask app "script" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Success! The script has used the configuration we've defined in config.yaml file. It's possible to use multiple configuration files, and both in YAML and JSON formats. Note that the keys will be overwritten if there are duplicates. This is a useful feature that you can use, for example, to set defaults in defaults.yaml and then overwrite with deployment specific settings in deployment.yaml. It's also possible to reference a full folder. PyFiguration will read all the files in the folder. For a full example checkout the ./examples folder of this repository.

If you have a configuration file and a script, you can also use the PyFiguration command line to check the config file for errors. Imaging this configuration file:

# config_with_warnings.yaml
server:
  port: 500.0
  not_needed_key: some random value

We've obviously made 2 mistakes here: 1: the port is a float, 2: there is a key that is not being used by our script. Lets use the command line tool to investigate.

$ pyfiguration inspect config -s script.py -c config_with_warnings.yaml
--------
 Errors
--------
   ✗ Value '500.0' is not of the correct type. The allowed data type is: int
----------
 Warnings
----------
   ! Key 'server.not_needed_key' doesn't exist in the definition and is not used in the module.

Metadata

Release files for pyfiguration 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfiguration 0.2.1
File Size Uploaded
pyfiguration-0.2.1.tar.gz 14.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyfiguration 0.2.1
File Interpreter ABI Platform
pyfiguration-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 31.6 kB

Release files / pyfiguration-0.2.1.tar.gz

Download URL pyfiguration-0.2.1.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1b4cb8e43ae084e52e6de9bc420e4f259d03d5fc692db97135f783a632f74e63
BLAKE2b-256 checksum
How to use checksums
bb7b81af411e29bd55c8f6194999dfaccd5816945888f5f2205dd8fb35e1e8ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release files / pyfiguration-0.2.1-py3-none-any.whl

Download URL pyfiguration-0.2.1-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d3a73f7c1dbd43ba386c00bd4a32aac843b18b34a9a35e1ec06d193349889312
BLAKE2b-256 checksum
How to use checksums
ad17db8e96f10d3db30e8f3ebb6ac564bdb1c24191d81f7bc24d7a23d1518d1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.6

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page