Skip to main content

Travis CI w/ Logo codecov PyPI

status

GitHub last commit (branch) GitHub issues GitHub pull requests GitHub contributors

configya

alt text

pip install configya

What is this?

I often make YAML configuration files for codes to input default parameters. I kept repeating myself and wanting more robustness to the structure required in the file... so I decided to make a generic one.

What it does?

  • Inherit the YAMLConfig class and pass to the super class a dict with the structure of your config and default values as well as the location and name of the config file.
  • Upon creating a config object, it will check if there is an existing config, and if not create a default one
  • If there is an existing config, it makes sure the structure and value types conform to default file structure and types
  • If it finds errors, it yells at you, backs yours up and tries to correct the corrupted one.
  • Only simple values like strings and numbers are checked... but maybe more in the future

What next?

fork it and add more... I'm sure this could be better

Check it out

from configya import YAMLConfig

Create a dictionary with the (nested) structure you desire and give it some default values

my_structure = {}
my_structure['n_workers'] = 4
my_structure['server'] = dict(name='supercomputer', address='10.10.1.1')
my_structure['my_iq'] = 80
my_structure['hours_spent_this'] = 'too many'

Create your class somewhere in your package

class MyConfig(YAMLConfig):  
    def __init__(self):

        super(MyConfig, self).__init__(my_structure,
                                       '~/.my_cool_program',
                                       'config.yml')
    

The first time you make it, the default config will be written. It will yell at you.

config = MyConfig()
/Users/jburgess/.environs/yaml_config/lib/python3.7/site-packages/configya-0.3.0-py3.7.egg/configya/yaml_config.py:84: NoConfigurationWarning: No configuration file found! Making one in /Users/jburgess/.my_cool_program/config.yml
!ls /Users/jburgess/.my_cool_program/
config.yml

Yes, it is there.

config
/Users/jburgess/.my_cool_program/config.yml





hours_spent_this: too many
my_iq: 80
n_workers: 4
server:
  address: 10.10.1.1
  name: supercomputer
config['server']['name']
'supercomputer'
config['my_iq']
80

Well...

We can modify with class access

config.my_iq = 200
config['my_iq']
200

Safety first

It will not allow you to clobber nested dicts

config['server'] = 5
---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)

<ipython-input-10-3d0b32c9a17b> in <module>
----> 1 config['server'] = 5


~/.environs/yaml_config/lib/python3.7/site-packages/configya-0.3.0-py3.7.egg/configya/yaml_config.py in __setitem__(self, key, item)
    294         if key in self._configuration:
    295 
--> 296             assert not isinstance(self._configuration[key], dict), f"Woah, you are going to overwrite the structure"
    297 
    298             self._configuration[key] = item


AssertionError: Woah, you are going to overwrite the structure

Cannot add things that are not there

config['ooops'] = 10

Or find them

config['what?'] 

What if I manually change the config in my github repo, and the one on my local computer is not up to date?

my_structure = {}
my_structure['n_workers'] = 4
my_structure['server'] = dict(name='supercomputer', address='10.10.1.1')
my_structure['my_iq'] = 80
my_structure['hours_spent_this'] = 'too many'
# Added a new number!
my_structure['total_number_of_postdocs'] = 1e5
class MyConfig(YAMLConfig):  
    def __init__(self):

        super(MyConfig, self).__init__(my_structure,
                                       '~/.my_cool_program',
                                       'config.yml')
    
config = MyConfig()
/Users/jburgess/.environs/yaml_config/lib/python3.7/site-packages/configya-0.3.0-py3.7.egg/configya/yaml_config.py:109: BadStructureWarning: The user config file /Users/jburgess/.my_cool_program/config.yml was corrupt
  BadStructureWarning,
/Users/jburgess/.environs/yaml_config/lib/python3.7/site-packages/configya-0.3.0-py3.7.egg/configya/yaml_config.py:113: BadStructureWarning: and has been backed up to /Users/jburgess/.my_cool_program/config.yml.bak and replaced
  BadStructureWarning,
/Users/jburgess/.environs/yaml_config/lib/python3.7/site-packages/configya-0.3.0-py3.7.egg/configya/yaml_config.py:117: BadStructureWarning: with the default config. The CURRENT config is now default
  BadStructureWarning,
config
/Users/jburgess/.my_cool_program/config.yml





hours_spent_this: too many
my_iq: 80
n_workers: 4
server:
  address: 10.10.1.1
  name: supercomputer
total_number_of_postdocs: 100000.0
!ls /Users/jburgess/.my_cool_program/
config.yml     config.yml.bak

Also, if you manually edit the config in an editor, it will check if the types are correct. If not, it will replace that value with the default value and backup your config


Metadata

Release files for configya 1.1.0

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

Source distribution (sdist)

Source distribution for configya 1.1.0
File Size Uploaded
configya-1.1.0.tar.gz 26.1 kB Details

Release files / configya-1.1.0.tar.gz

Download URL configya-1.1.0.tar.gz
Size 26.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ae00efa8a8b66487ead63d7b6a339e1c567aabb1a005263d1cb8fb37373e96c9
BLAKE2b-256 checksum
How to use checksums
bc82f86df9b1417e3c93db6a0aac2eda0da497593fe68babf596c7ba1d976bc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 release file

1.0.2

1 release file

1.0.0

2 release files

0.3.0

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