qdyn.config module¶
Module containing utilities for managing QDYN config files
Summary¶
Functions:
Inverse of |
|
Generate a routine, e.g. |
|
Get a dumped .json-file with all the allowed section names and correspondig items of the new config structure genereated by reading the ‘para_t’ type in the def.f90 file |
|
Return the value of a user-defined key in the config file (in the |
|
Extract value from config_data by the given key_tuple |
|
Equivalent to |
|
Equivalent to |
|
Parse the multi-line string containing the contents of a config file, and return a nested data structure of the config file data. |
|
Set a user-defined value in the config file. |
|
Set a value in config_data, cf. |
|
Write out a config file |
Reference¶
-
qdyn.config.
read_config_file
(filename)[source]¶ Equivalent to
read_config_str(open(filename).read())
-
qdyn.config.
read_config_str
(config_str)[source]¶ Parse the multi-line string containing the contents of a config file, and return a nested data structure of the config file data.
Return an ordered dictionary containing the following mapping:
section name => dict(key => value)
or:
section name => list of dicts(key => value)
-
qdyn.config.
write_config
(config_data, filename)[source]¶ Write out a config file
- Parameters
config_data (dict) – data structure as returned by
read_config_str()
.filename (str) – name of file to which to write config
-
qdyn.config.
config_data_to_str
(config_data)[source]¶ Inverse of
read_config_str()
-
qdyn.config.
get_config_value
(config_data, key_tuple)[source]¶ Extract value from config_data by the given key_tuple
- Parameters
config_data (dict) – data structure as returned by
read_config_str()
.key_tuple (tuple) – tuple of keys. For example if key_tuple is
('pulse', 0, 'id')
, then the returned value would beconfig_data['pulse'][0]['id']
- Raises
ValueError – if any of the keys in key_tuple are invalid or cannot be found
-
qdyn.config.
get_config_user_value
(config_data, key)[source]¶ Return the value of a user-defined key in the config file (in the
user_strings
,user_reals
,user_logicals
, or user_ints` section). Return the first value found in any of the above sections, as the type corresponding to the section where the key was found. Raise a KeyError if key does not exist in any of the user-defined sections
-
qdyn.config.
set_config_value
(config_data, key_tuple, value)[source]¶ Set a value in config_data, cf. get_config_value. The key that is set must already exist in config_data
-
qdyn.config.
set_config_user_value
(config_data, key, value)[source]¶ Set a user-defined value in the config file. The value must be an instance of str, float, bool, or int, and it will be set for the given key in the corresponding section
user_strings
,user_reals
,user_loigcals
, oruser_ints
. This routine may be used to add new user-defined data to config_data; a missing user-defined section will be created as necessary.
-
qdyn.config.
generate_make_config
(config_template, variables, dependencies=None, checks=None)[source]¶ Generate a routine, e.g. make_config, that may be used to generate config file data based on the given template.
- Parameters
config_template (dict) – data structure as returned by
read_config_str()
that will serve as a templatevariables (dict) – mapping of a keyword variable name to a key-tuple in the config (cf.
set_config_value()
)dependencies (dict) – mapping of a key-tuple to a callable that calculates a value for that entry in the config file.
checks (dict) – mapping of a keyword variable name to a callable that checks whether a given value is acceptable.
Example
>>> config_template = read_config_str(r""" ... pulse: type = gauss, t_FWHM = 1.8, E_0 = 1.0, w_L = 0.2, & ... & oct_shape = flattop, ftrt = 0.2, oct_lambda_a = 100, & ... & oct_increase_factor = 10 ... * id = 1, t_0 = 0, oct_outfile = pulse1.dat ! pulse 1 ... * id = 2, t_0 = 2.5, oct_outfile = pulse2.dat ! pulse 2 ... * id = 3, t_0 = 5, oct_outfile = pulse3.dat ! pulse 3""")
>>> make_config = generate_make_config(config_template, ... variables={'E_0': ('pulse', 0, 'E_0'), }, ... dependencies={ ... ('pulse', 1, 'E_0'): lambda kwargs: kwargs['E_0'], ... ('pulse', 2, 'E_0'): lambda kwargs: kwargs['E_0']}, ... checks={'E_0': lambda val: val >= 0.0})
>>> print(make_config.__doc__) Generate config file data (``config_data``) based on the given keyword parameters. Keyword Arguments: E_0: Set ``config_data['pulse'][0]['E_0']`` (default: 1.0) Also, the following will be set to values that depend on the given keyword arguments: * ``config_data['pulse'][0]['E_0']`` * ``config_data['pulse'][0]['E_0']`` If called without arguments, data equivalent to the following config file is returned:: pulse: type = gauss, t_FWHM = 1.8, E_0 = 1.0, w_L = 0.2, oct_shape = flattop, & ftrt = 0.2, oct_lambda_a = 100, oct_increase_factor = 10 * id = 1, t_0 = 0, oct_outfile = pulse1.dat * id = 2, t_0 = 2.5, oct_outfile = pulse2.dat * id = 3, t_0 = 5, oct_outfile = pulse3.dat Raises: TypeError: if an invalid keyword is passed. ValueError: if any value fails to pass checks.
>>> config = make_config(E_0=0.1) >>> print(config_data_to_str(config)) pulse: type = gauss, t_FWHM = 1.8, E_0 = 0.1, w_L = 0.2, oct_shape = flattop, & ftrt = 0.2, oct_lambda_a = 100, oct_increase_factor = 10 * id = 1, t_0 = 0, oct_outfile = pulse1.dat * id = 2, t_0 = 2.5, oct_outfile = pulse2.dat * id = 3, t_0 = 5, oct_outfile = pulse3.dat