Skip to main content

Project config files

Project description

What is this?

The most flexible file + cli config for python projects

  • stop hardcoding filepaths
  • stop wasing time using argparse
  • stop with messy config differences for different machines

How do I use this?

pip install quik-config

Create a config file like info.yaml with a structure like this:

# NOTE: names in parentheses are special, all other names are not!
(project):

    # a central place for filepaths
    (path_to):
        this_file:       "./info.yaml"
        blah_file:       "./data/results.txt"
        blahblah_folder: "./data/"

    # git-ignore this file path! (file will be generated automatically)
    (local_data): ./local_data.ignore.yaml
            # this^ is where you choose which profile(s)
            # and is where to store auth tokens and other secrets

    (profiles):
        (default):
            blah: "blah blah blah"
            mode: development # or production. Same thing really
            has_gpu: maybe
            constants:
                pi: 3 # its 'bout 3 

        PROFILE1:
            constants:
                pi: 3.1415926536
                e: 2.7182818285

        PROD:
            mode: production
            constants:
                pi: 3.1415926536
                problems: true

        PROFILE2:
            constants:
                pi: 3.14
                e: 2.72

        TEST:
            mode: testing

        GPU:
            has_gpu: true

Then load in your config in python!

from quik_config import find_and_load

info = find_and_load(
    "info.yaml",
    cd_to_filepath=True,
    parse_args=True,
    defaults_for_local_data=["PROFILE1", "PROD"],
)
# (generates the ./local_data.ignore.yaml if you dont have it)
# loads whatever profiles are mentioned in ./local_data.ignore.yaml 

# Use the data!
print(info.config)
# {
#     "mode": "production",     # from PROD
#     "has_gpu": False,         # from (default)
#     "constants": {
#         "pi": 3.1415926536,   # from PROFILE1
#         "e": 2.7182818285,    # from PROFILE1
#         "problems": True,     # from PROD
#     },
# }
print(info.config.mode)
print(info.config["mode"])
print(info.config.constants.problems)

Different Profiles For Different Machines

Lets say you've got an info.yaml like this:

(project):
    name: Example Project1
    poorly_maintained: true
    bug_report_url: https://stackoverflow.com/questions/


    (local_data): ./local_data.ignore.yaml
    (profiles):
        DEV:
            cores: 1
            database_ip: 192.168.10.10
            mode: dev
        LAPTOP:
            cores: 2
        DESKTOP:
            cores: 8
        UNIX:
            line_endings: "\n"
        WINDOWS:
            line_endings: "\r\n"
        PROD:
            database_ip: 183.177.10.83
            mode: prod
            cores: 32

On your Macbook you can edit the ./local_data.ignore.yaml (or your equivlent) to include something like the following:

(selected_profiles):
    - LAPTOP # the cores:2 will be used (instead of cores:1 from DEV)
    - UNIX   #     because LAPTOP is higher in the list than DEV
    - DEV

On your Windows laptop you can edit it and put:

(selected_profiles):
    - LAPTOP
    - WINDOWS
    - DEV

Command Line Arguments

If you have run.py like this:

from quik_config import find_and_load

info = find_and_load("info.yaml", parse_args=True)

print("config:",      info.config     )
print("unused_args:", info.unused_args)

# 
# call some other function you've got
# 
#from your_code import run
#run(*info.unused_args)

Example 0

Using the python file and config file above

python ./run.py

Running that will output:

config: {
    "mode": "development",
    "has_gpu": False,
    "constants": {
        "pi": 3
    }
}
unused_args: []

Example 1

Show help. This output can be overridden in the info.yaml by setting (help): under the (project): key.

python ./run.py -- --help

Note the -- is needed in front of the help.

You can also add show_help_for_no_args=True if you want that behavior.
Ex:

from quik_config import find_and_load
info = find_and_load(
    "info.yaml",
    show_help_for_no_args=True
    parse_args=True,
)

Example 2

Again but selecting some profiles

python ./run.py arg1 -- --profiles='[PROD]'
# or
python ./run.py arg1 -- @PROD'

Output:

config: {
    "mode": "production",
    "has_gpu": False,
    "constants": {
        "pi": 3.1415926536,
        "problems": True,
    },
}
unused_args: ["arg1"]

Example 3

Again but with custom arguments:

python ./run.py arg1 --  mode:my_custom_mode  constants:tau:6.2831853072
config: {
    "mode": "my_custom_mode",
    "has_gpu": False,
    "constants": {
        "pi": 3,
        "tau": 6.2831853072,
    },
}
unused_args: ["arg1"]

Example 4

Again but with really complicated arguments:
(each argument is parsed as yaml)

python ./run.py arg1 --  mode:my_custom_mode  'constants: { tau: 6.2831853072, pi: 3.1415926, reserved_letters: [ "C", "K", "i" ] }'

prints:

config: {
    "mode": "my_custom_mode", 
    "has_gpu": False, 
    "constants": {
        "pi": 3.1415926, 
        "tau": 6.2831853072, 
        "reserved_letters": ["C", "K", "i", ], 
    }, 
}
unused_args: ["arg1"]

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

quik_config-1.5.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

quik_config-1.5.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file quik_config-1.5.0.tar.gz.

File metadata

  • Download URL: quik_config-1.5.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for quik_config-1.5.0.tar.gz
Algorithm Hash digest
SHA256 01b06005fdd7ec807d8a448a5a5fe99dc9d3f2836a1f846ce2100b49f1c865b1
MD5 a3a46e704e2724f97d8a6b3eacdc21b6
BLAKE2b-256 06a534a3e176ee44d782cdc729c7b62cb5b216a2d7b8af42bdb93264cf58f4d3

See more details on using hashes here.

File details

Details for the file quik_config-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: quik_config-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for quik_config-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 49ccf54c44637def6e96c256c39129dac5cbf4453eaac291a5e0491bb48377a6
MD5 672e779d1ac2ba9ce72dc400abf43a18
BLAKE2b-256 651d0154a4e41208b968a0b075988425905fa05f41077d16efe049f2ecf0a305

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