Settings
modelskill.settings
The settings module holds package-wide configurables and provides a uniform API for working with them.
This module is inspired by pandas config module.
Overview
This module supports the following requirements:
- options are referenced using keys in dot.notation, e.g. "x.y.option - z".
- keys are case-insensitive.
- functions should accept partial/regex keys, when unambiguous.
- options can be registered by modules at import time.
- options have a default value, and (optionally) a description and validation function associated with them.
- options can be reset to their default value.
- all option can be reset to their default value at once.
- all options in a certain sub - namespace can be reset at once.
- the user can set / get / reset or ask for the description of an option.
- a developer can register an option.
Implementation
- Data is stored using nested dictionaries, and should be accessed through the provided API.
- "Registered options" have metadata associated with them, which are stored in auxiliary dictionaries keyed on the fully-qualified key, e.g. "x.y.z.option".
Examples:
>>> import modelskill as ms
>>> ms.options
metrics.list : [<function bias at 0x0000029D614A2DD0>, (...)]
plot.rcparams : {}
plot.scatter.legend.bbox : {'facecolor': 'white', (...)}
plot.scatter.legend.fontsize : 12
plot.scatter.legend.kwargs : {}
plot.scatter.oneone_line.color : blue
plot.scatter.oneone_line.label : 1:1
plot.scatter.points.alpha : 0.5
plot.scatter.points.label :
plot.scatter.points.size : 20
plot.scatter.quantiles.color : darkturquoise
plot.scatter.quantiles.kwargs : {}
plot.scatter.quantiles.label : Q-Q
plot.scatter.quantiles.marker : X
plot.scatter.quantiles.markeredgecolor : (0, 0, 0, 0.4)
plot.scatter.quantiles.markeredgewidth : 0.5
plot.scatter.quantiles.markersize : 3.5
plot.scatter.reg_line.kwargs : {'color': 'r'}
>>> ms.set_option("plot.scatter.points.size", 4)
>>> plot.scatter.points.size
4
>>> ms.get_option("plot.scatter.points.size")
4
>>> ms.options.plot.scatter.points.size = 10
>>> ms.options.plot.scatter.points.size
10
>>> ms.reset_option("plot.scatter.points.size")
>>> ms.options.plot.scatter.points.size
20
OptionsContainer
provide attribute-style access to a nested dict of options
Accessed by ms.options
Source code in modelskill/settings.py
get_option
Get value of a single option matching a pattern
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pat |
str
|
pattern of seeked option |
required |
Returns:
Type | Description |
---|---|
Any
|
value of matched option |
Source code in modelskill/settings.py
load_style
Load a number of options from a named style.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the predefined style to load. Available styles are: 'MOOD': Resembling the plots of the www.metocean-on-demand.com data portal. |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If a named style is not found. |
Examples:
Source code in modelskill/settings.py
register_option
Register an option in the package-wide modelskill settingss object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Fully-qualified key, e.g. "x.y.option - z". |
required |
defval |
object
|
Default value of the option. |
required |
doc |
str
|
Description of the option. |
''
|
validator |
Callable
|
Function of a single argument, should raise |
None
|
Raises:
Type | Description |
---|---|
ValueError if `validator` is specified and `defval` is not a valid value.
|
|
Source code in modelskill/settings.py
reset_option
Reset one or more options (matching a pattern) to the default value
Examples:
>>> ms.options.plot.scatter.points.size
20
>>> ms.options.plot.scatter.points.size = 10
>>> ms.options.plot.scatter.points.size
10
>>> ms.reset_option("plot.scatter.points.size")
>>> ms.options.plot.scatter.points.size
20
Source code in modelskill/settings.py
set_option
Set the value of one or more options
Examples:
>>> ms.set_option("plot.scatter.points.size", 4)
>>> ms.set_option({"plot.scatter.points.size": 4})
>>> ms.options.plot.scatter.points.size = 4