Skip to main content

PropArgs

A module for systematically organizing user preferences acquired from a data store, env vars, a parameter file, the command line and/or user choices.

How it Works

Overview

PropArgs is initialized by sequentially loading the following five stages of input:

  1. Data Store
  2. Property Dictionary
  3. Environment
  4. Command Line
  5. User

The above is the default ordering. The user may change that order.

Properties may be added in any stage except User. A property defined in one stage may be overwritten in later stages.

Each property has a key and a value and is accessed like a dictionary:

>>> pa = PropArgs.create_props()
>>> pa["prop_nm"] = 1  # assigns the value 1 to the property "prop_nm"
>>> pa["prop_name"]
1

In addition to this, each property may have associated metadata. These are at present: a question for the user-input prompt, a datatype, an upper bound, and a lower bound. If at any point in the loading process the metadata rules are broken (e.g. the val exceeds the upper bound) an error will be raised.

The system has some similarities to the traitlets configuration module developed for iPython and Jupyter, but is more flexible, seeks configuration info from more sources, and is not tied to Python classes. (The property structure itself keeps the config info, but those values may be loaded into clases or not as per the needs of the application.)

Details

Data Store

The Data Store will be either a JSON file or a database.

The Data Store file will be specified on initialization

PropArgs.create_props(ds_file=file_name)

The JSON formatting is as follows:

{
    "prop_name_1": {
        "val": 1,
        "question": "What value should this property have?",
        "atype": "INT",
        "hival": 10,
        "lowval": 0
    },
    "prop_name_2": {
        "val": "Hello World."
    },
    "prop_name_3": {
    }
}

Note that a property need not have all (or any) fields defined. If no "val" is specified, a default "val" is assigned according to its atype:

  • "BOOL" defaults to False
  • "INT" defaults to 0
  • "FLT" defaults to 0.0
  • "CMPLX" defaults to 0j
  • "STR" defaults to ""
  • If no type is given, the value defaults to None.

Details on database data stores to come ...

Property Dictionary

Properties may be loaded by passing a dictionary at initialization:

>>> pa = PropArgs.create_props(prop_dict=prop_dict)

the dictionary formatted as follows:

{
    "prop_name_1": {
        "val": 1,
        "question": "What value should this property have?",
        "atype": "INT",
        "hival": 10,
        "lowval": 0
    },
    "prop_name_2": {
        "val": "Hello World."
    }
}

Environment

PropArgs will read and add all the environment variables of the program in which PropArgs is initialized. (i.e. everything in python's os.environ)

Command Line

Properties will be read from the command line as follows

$ python program_reading_props.py --props prop_1=val_1,prop_2=val_2,prop_3=val_3  #etc...

User

The final stage is to ask the user for input. The user will only be prompted about properties that have a question.

The default behavior is to prompt questions on the client's command line:

>>> pa = PropArgs.create_props()
What is the value of prop_1? ("I'm the default prop value") <enter_value>
What is the value of prop_2? [0.0-100.0] (20) <enter_value>

However, the client program may take over and ask questions in its own way. In this case we will provide a flag that skips the user stage, and instead the client will call pa.get_questions() for a JSON of the properties with questions.

>>> pa = PropArgs(skip_user_questions=True)
>>> pa.get_questions()
{
    "prop_name_1": {
        "val": 2.0,
        "question": "",
        "atype": "DBL",
        "hival": 10.0,
        "lowval": -0.5
    },
    "prop_name_2": {
        "val": "default",
        "question": "What should prop_name_2 be?"
    }
}

Other Ways Props May Be Added Or Modified

Outside of accessing a PropArgs object like a dictionary, values may be gotten through the get(key, default=None) method. Or if the key has not already been associated with a property, a new property will be bound to default (or None if a default isn't specified).

>>> pa = PropArgs.create_props()
>>> pa.get('new_prop', default=2)
2
>>> pa.get('newer_prop')
None

Datatypes and Type Casting

Each property may be given a datatype by using the "atype" property. PropArgs currently supports five datatypes: "BOOL" "INT" "FLT" "CMPLX" and "STR". When PropArgs is initialized, the property value is cast to its "atype". For most "atype"s, this type casting is done in the usual python way. So a prop

An "atype" of "BOOL" casts "yes" (any casing), "true" (any casing), and 1 to True. Everything else is cast to False.

Credits

Idea - Robert Dodson

Development - Gene Callahan and Nathan Conroy

Metadata

Release files for propargs 0.0.20

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

Source distribution (sdist)

Source distribution for propargs 0.0.20
File Size Uploaded Provenance Actions
propargs-0.0.20.tar.gz 9.7 kB Without provenance Details for propargs-0.0.20.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for propargs 0.0.20
File Interpreter ABI Platform Actions
propargs-0.0.20-py3-none-any.whl Python 3 none any Details for propargs-0.0.20-py3-none-any.whl

Total release size: 31.5 kB

Release files / propargs-0.0.20.tar.gz

Download URL propargs-0.0.20.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f86dea174aa44630bdb49d9093aeb696a70f2ad3369f999696beddd658527f91
BLAKE2b-256 checksum
How to use checksums
beb645489f88e0360fa49300fdb7f3a1b3e57fcf46cbd1ee2c88fe2d04c49c84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.0

Release files / propargs-0.0.20-py3-none-any.whl

Download URL propargs-0.0.20-py3-none-any.whl
Size 21.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a915923eab902e24d811f41132660141baee7dfc6273cc61e2768a89dcdbfe9
BLAKE2b-256 checksum
How to use checksums
cf07c5b13bead2c9a749e3bdec3af73d06e575595a768b0e2db325ad7fa90290
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.0

Security for propargs 0.0.20

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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