Skip to main content

Smarter config for Python

Project description

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.

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

pyfiguration-0.2.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

pyfiguration-0.2.1-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file pyfiguration-0.2.1.tar.gz.

File metadata

  • Download URL: pyfiguration-0.2.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pyfiguration-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1b4cb8e43ae084e52e6de9bc420e4f259d03d5fc692db97135f783a632f74e63
MD5 983b911e2ff2b8fa7243e01eeefb79e3
BLAKE2b-256 bb7b81af411e29bd55c8f6194999dfaccd5816945888f5f2205dd8fb35e1e8ed

See more details on using hashes here.

File details

Details for the file pyfiguration-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyfiguration-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pyfiguration-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d3a73f7c1dbd43ba386c00bd4a32aac843b18b34a9a35e1ec06d193349889312
MD5 a8d105c8e78cdbb2f2623ac5983b4b14
BLAKE2b-256 ad17db8e96f10d3db30e8f3ebb6ac564bdb1c24191d81f7bc24d7a23d1518d1e

See more details on using hashes here.

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