User-friendly parameter dictionaries in Python
Project description
ParamsDict
ParamsDict is a Python package for defining user-friendly parameter dictionaries.
It has three advantages over built-in dictionaries:
Keys are fixed. This prevents users from accidentally mistyping keys - in particular, a mistyped key in a built-in dictionary will not raise an error, and the user may not have a way to know there is a mistake in their code.
Values can be constrained. This guarantees users enter valid parameters.
All keys have linked descriptions. This means users can check parameter definitions without having to refer to the documentation.
The package also generates clear errors messages so if an input is an invalid, the user knows why it didn’t work.
ParamsDict is used in BipartitePandas and PyTwoWay. The original code was written for those projects, under the supervision of Professor Thibaut Lamadon at the University of Chicago. The package was inspired by this post on Stack Overflow.
Installation
The package provides a Python interface. Installation is handled by pip. The source of the package is available on GitHub at ParamsDict.
To install via pip, from the command line run:
pip install paramsdict
To make sure you are running the most up-to-date version of ParamsDict, from the command line run:
pip install --upgrade paramsdict
Quick Start
Say you are writing a function that simulates values from a normal distribution and you want the user to be able to specify the number of draws, as well as the mean and standard deviation of the normal distribution. You also want to constrain the user so that the number of draws is a positive integer, while the mean and standard deviation must be floats or integers and the standard deviation is non-negative. Finally, assume you have two algorithms for simulating from the normal distribution, 'a' and 'b', which you want the user to be able to specify.
from paramsdict import ParamsDict
def _gt0(x):
return x > 0
def _gteq0(x):
return x >= 0
sim_params = ParamsDict({
'n': (10, 'type_constrained', (int, _gt0),
'''
(default=10) Number of draws to simulate.
''', '> 0'),
'mean': (0, 'type', (int, float),
'''
(default=0) Mean of draws.
''', None),
'sd': (1, 'type_constrained', ((int, float), _gteq0),
'''
(default=1) Standard deviation of draws.
''', '>= 0'),
'algorithm': ('a', 'set', ['a', 'b'],
'''
(default='a') Algorithm for simulating from normal distribution.
''', None)
})
Now the user can instantiate a new instance of sim_params by running my_sim_params = sim_params().
They can set their own values by inputting a dictionary while instantiating the ParamsDict, e.g. my_sim_params = sim_params({'sd': 2}). If they enter an invalid value, it will raise an error.
If the user wants to know what a particular key does, they can run sim_params().describe(key), e.g. if they run sim_params().describe('sd') it will print a description of 'sd'. Alternatively, to print descriptions for every key in the parameter dictionary, they can run sim_params().describe_all().
Advanced
ParamsDict includes a variety of options for parameters. These include:
'type' - the key must be associated with a particular type
'list_of_type' - the value must be a particular type or a list of values of a particular type
'type_none' - the value can either be None or must be a particular type
'list_of_type_none': the value can either be None or must be a particular type or a list of values of a particular type
'type_constrained' - the value must be a particular type and fulfill given constraints
'type_constrained_none' - the value can either be None or must be a particular type and fulfill given constraints
'dict_of_type' - the value must be a dictionary where the values are of a particular type
'dict_of_type_none' - the value can either be None or must be a dictionary where the values are of a particular type
'array_of_type' - the value must be an array of values of a particular datatype
'array_of_type_none' - the value can either be None or must be an array of values of a particular datatype
'array_of_type_constrained' - the value must be an array of values of a particular datatype and fulfill given constraints
'array_of_type_constrained_none' - the value can either be None or must be an array of values of a particular datatype and fulfill given constraints
'set' - the value must be a member of a given set of values
'list_of_set' - the value must be a member of a given set of values or a list of members of a given set of values
'any' - the value can be anything
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 paramsdict-0.0.3.tar.gz
.
File metadata
- Download URL: paramsdict-0.0.3.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eda8c7d255fdaa3c2892ca4b351e3bfbf36b7c45c7e99dd6077f4cbda4092cf3 |
|
MD5 | 55017df5eb7797a521ea085a0aca40c2 |
|
BLAKE2b-256 | c67aea4c486edc4ab44bafec3ebc83a48f2728d0540846240dc179a702028f59 |
File details
Details for the file paramsdict-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: paramsdict-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7971bded5f4b1b48d1ff9ef249d554e7cfc92701b7329ce2ab790185b6fd00ff |
|
MD5 | bc523242bbd25a4ebabd3604c91fd176 |
|
BLAKE2b-256 | 5bc8bfa0da16645d6da932f7710508b36302f1255a3314cf74ee8cc86eccee75 |