Skip to main content

Python YAML based Configuration

Project description

Python Application Configuration

This is a configuration tool for application configurations, similar to Spring Framework Core.

Application Settings

Nontrivial applications have external settings, such as properties, ini, xml, yaml, or others(e.g., config server). we are going to use YAML files since it is the current popular way to configure applications. One of the advantages with YAML is that it supports tree structure to avoid duplicated key paths. XML is a good option too for complex settings, but it's out of favor because of open/close tags.

All different formats of settings eventually resolves to key-value pairs. The keys are tree paths for grouping. The values are simple data types, such as string, boolean, int, float, date time, etc.

Besides these setting files, we want to overwrite some settings via command line parameters or environment variables. To coordinate settings from different sources, we adapt the following:

  • yaml settings is the baseline
  • may refer environment variables through ${env_var_name:default_value}
  • may pass in from command line like -Ekey=value. This could overwrite yaml or environment variable values.

However, currently environment variables and command line overwrites treat all values as string. One way to get around is to use separate YAML files for different cases.

Sample YAML file, test.yaml:

db:
    url: 'go://disney'
    user: 'mickey mouse'
    server_port: 8080
    price: 3.1415925
    begin_date: !!timestamp '2010-11-17 13:12:11'
    ext_var: ${my_env}

Sample Python code:

# load settings
app_ctx = IocContext('test')  # It looks for test.yaml
app_ctx.load_conf_file(current_path) ## need to tell where test.yaml is, current_path

# run with command parameter: -Edb.url=magic_kingdom, os env: set/export my_var=my_value
# we can get settings:
app_ctx.get_conf('db.user')        # mickey mouse
app_ctx.get_conf('db.url')         # magic_kingdom, overwritten by command line
app_ctx.get_conf('db.server_port') # 8080, int
app_ctx.get_conf('db.price')       # 3.1415925, float
app_ctx.get_conf('db.begin_date')  # datetime object for 2010-11-17 13:12:11
app_ctx.get_conf('db.ext_var')     # my_value, set by environment variable

Application Configuration

Applications consist of components, which could consist of other components. So we end up with a component tree where the application is at the top. To assembly these components, we could hardwire them together. A better approach is IoC or DIP.

Our IoC container has 3 layers:

  • a key-value pair store to work with tree paths, illustrated above.
  • an object container to manage object life cycles and set dependencies.
  • a context to load YAML configurations and inject them to objects.

There are 2 ways to utilize the lib:

  • use context as a configuration tool. Once the context is loaded with YAML files, it can be referred elsewhere.
  • use context as an IoC container. Register objects in the container, then we can retrieve objects with dependencies and configurations fully populated.

The configuration has 3 precedences, from high to low:

  • after loading the configuration, there is a chance to override it because initializing objects. So we can get overrides from command line and overwrite settings.
  • set environment variables and retrieve them in YAML files. This is a more restrictive way than where we blindly overwrite all possible settings in the environment.
  • YAML files.

There is an encryption feature for passwords.

Python is a functional language, but there are cases where we need classes. Passing all parameters to functions can pollute function interfaces and create information leaks. For example, passing a database connection around to retrieve data results changes to all functions if we switch from database storage to others, e.g., elastic search server. So hiding the connection in a class (through constructor) and keep the data retrieval interface clean would be a better solution.

Due to the nature of Python, singletons are not as bad as in Java. Especially during application start up, if we can guaranty single threaded, there are more playroom than the Java case.

TODOs

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

pypigeonhole-config-0.0.2.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypigeonhole_config-0.0.2-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file pypigeonhole-config-0.0.2.tar.gz.

File metadata

  • Download URL: pypigeonhole-config-0.0.2.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0.post20201103 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for pypigeonhole-config-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2a785480f1351ded1038357ded77cfdb8566310ed69cd9281f35d76991052191
MD5 2e9a1dfb18622343a41c10daf5b8bf1a
BLAKE2b-256 5042b71e25452142d29280b1a9077dafbb3678894c5e99d816bbf77def41fade

See more details on using hashes here.

File details

Details for the file pypigeonhole_config-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pypigeonhole_config-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0.post20201103 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for pypigeonhole_config-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2fc91fa2de45d3c36db603ad897e27e49258b92cf2142ec6113d2c2a5e94a64a
MD5 3e084d021edd78bb1d015234fc5804a5
BLAKE2b-256 cb1b03af8d06d86d6aff7497ff9e2d4344b9461213ae983fab6bfaf758fec1c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page