Resolve argument values from arg, env or default values
Project description
Overview
When running code there is often a need to initialise arguments either directly from a passed in value, indirectly via an environment variable or config file or a via default value. Argparse provides this functionality (or can be easily augmented to, with the exception of loading from a config file) already but has one major drawback; It does not work when the code is invoked as a library.
arg_init provides functionality to resolve argument values for a given function/method from either a config file, an environment variable, an argument value or a default value. Introspection is used to determine the arguments of the calling function, and a dictionary is created of resolved values for each argument. Resolved values are determined using a predefined priority system that can be customised by the user.
When resolving from an environment variable, the environment variable name is assumed to be the same as the argument name, in uppercase e.g. An argument, arg1 would resolve from an environment variable, "ARG1". This behaviour can be modified by providing an alternate name via argument defaults or by setting an env_prefix.
If the calling function is a class method, arguments may also be made available as class attributes. See reference for more details.
Because argument initialisation is implemented in the application, it will work if called via a CLI script or as a library by another python program.
arg_init provides two classes; ClassArgInit and FunctionArgInit for initialising arguments of bound class functions and unbound functions respectively. These classes iterate over all arguments of the calling function, exposing a dictionary containing key/value pairs of argument name, with values assigned according to the priority system selected.
Notes
arg_init uses introspection (via the inspect module) to determine function arguments and values. Its use is minimal and is only executed once at startup so performance should not be an issue.
It is not practical to dynamically determine if the function to be processed is a bound function (a class method, with a class reference (self) as the first parameter) or an unbound function (a simple function), so selection is determined by the use of the called class: ClassArgInit of FunctionArgInit.
Fucntionality is identical for both implementations, with the following exception:
ClassArgInit:
- Class attributes are set (may be optionally disabled) that represent the resolved argument values
Priority
The argument value is set when a non None value is found, or all options are exhausted. At this point the argument is set to None.
What priority should be used to set an argument?
Argument Priority Order
If passed in arguments have priorty:
And if the function has a non None default argument e.g. f(a=1), then the argument value will always be used to set the value, never allowing a config or env value to take effect.
There are two obvious solutions to this:
- Lower the priority of arguments.
- Provide an alternate means to specify a default value. If a default value is required in the function signature, to allow ommission of an argument when calling, ensure it is set to None.
Default Values
The problem: How to avoid violating the DRY principle when an application can be invoked via a CLI or as a library.
If an application is to be called as a library then the defaults MUST be implemented in the application, not the CLI script. But ArgumentParser will pass in None values if no value is specified for an argument. This None value will be used in preference to function default! So defaults must be also be specified in ArgumentParser and the applicication. This is not a good design pattern.
Providing an alternate means to specify a default value resolves this.
Priority Order
arg-init supports customisable priority models. It is left to the user to select an appropriate priority sequence (or use the default option) for each specfic use case.
Default Priority Order
The default priority implemented is:
CONFIG_PRIORITY
- Config
- Env
- Arg
- Default
Predefined Priority Orders
Two further predifined priority models are provided
- ENV_PRIORITY
- ARG_PRIOIRTY
The user may also define a custom priority order if the predefined options are not suitable.
Usage
Simple Useage
The following examples show how to use arg_init to initialise a class or function
from arg_init import ClassArgInit
class MyApp:
def __init__(self, arg1=None):
ClassArgInit()
...
from arg_init import FunctionArgInit
def func(arg1=None):
FunctionArgInit()
...
In the examples above, arg1 will be initialised with the value from the config file, the environment variable "ARG1", else it will take the passed in value. Finally it will have a default value of None assigned.
As these examples use the default priority sytem, they will not work if used with ArgumentParser without ArgumentParser replicating the default values.
Other use cases
The example below shows how to change the environment variable name used to initialise the argument using ArgDefaults.
from arg_init import FunctionArgInit, ArgDefaults
def func(arg1=None):
arg1_defaults = ArgDefaults(env_name="TEST")
args = FunctionArgInit(defaults={"arg1": arg1_defaults}).args
...
The example below shows how to use argument priority when resolving the values of arguments.
from arg_init import FunctionArgInit, ARG_PRIOIRITY, ArgDefaults
def func(arg1=None):
arg1_defaults = ArgDefaults("arg1", default_value=1)
args = FunctionArgInit(priority=ARG_PRIORITY, defaults=[arg1_defaults]).args
...
Recommendation
To avoid namespace clashes with environment variables, it is recommneded to always supply an env_prefix argument when initialising ClassArgInit/FunctionArgInit. All environment variables are expected to have this prefix e.g. with an env_prefix of "myapp", arg1 would map to the environment variable "MYAPP_ARG1".
from arg_init import ClassArgInit
class MyApp:
def __init__(self, arg1=None):
args = ClassArgInit(env_prefix="myapp").args
...
Note: In this example, arg1 would resolve against the environment variable "MYAPP_ARG1".
Please see the documentation for further details on usage.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file arg_init-0.0.6.tar.gz
.
File metadata
- Download URL: arg_init-0.0.6.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6ab15de4b8aedf41818c47cd9bf6e281247014950fbffcc1e67122018f1d95a |
|
MD5 | 44d50d4f2eb1f4abce3d39ddf3687c57 |
|
BLAKE2b-256 | f1dbe352bd25581ae4ff9772dbfcd7107958b0fbfb3f806a0a0c3aba15d32f2c |
File details
Details for the file arg_init-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: arg_init-0.0.6-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 680f0d8628eaf684d99ccd6cb5d3c02da0fb1d4eca4ca9461b27ffece5de306b |
|
MD5 | e7da251ac9dc409ed9441bf691bc1555 |
|
BLAKE2b-256 | abc5e683b7e1ff333b4d3ac1b08a3da79dac994e8e560d85457b3c157b00ac14 |