returnn.config
#
Provides Config
and some related helpers.
- class returnn.config.Config(items=None)[source]#
Reads in some config file, and provides access to the key/value items. We support some simple text-line-based config, JSON, and Python format.
- Parameters:
items (dict[str]|None) – optional initial typed_dict
- load_file(f)[source]#
Reads the configuration parameters from a file and adds them to the inner set of parameters.
- Parameters:
f (string|io.TextIOBase|io.StringIO) –
- classmethod get_config_file_type(f)[source]#
- Parameters:
f (str) – file path
- Returns:
“py”, “js” or “txt”
- Return type:
str
- add_line(key, value)[source]#
Adds one specific configuration (key,value) pair to the inner set of parameters :type key: str :type value: str
- has(key)[source]#
Returns whether the given key is present in the inner set of parameters :type key: string :rtype: boolean :returns True if and only if the given key is in the inner set of parameters
- is_typed(key)[source]#
- Return type:
boolean
:returns True if and only if the value of the given key has a specified data type
- is_true(key, default=False)[source]#
- Parameters:
key (str) –
default (bool) –
- Returns:
bool(value) if it is set or default
- Return type:
bool
- is_of_type(key, types)[source]#
- Parameters:
key (str) –
types (type|tuple[type]) – for isinstance() check
- Returns:
whether is_typed(key) is True and isinstance(value, types) is True
- Return type:
bool
- get_of_type(key, types, default=None)[source]#
- Parameters:
key (str) –
types (type|list[type]|T) – for isinstance() check
default (T|None) –
- Returns:
if is_of_type(key, types) is True, returns the value, otherwise default
- Return type:
T
- value(key, default, index=None, list_join_str=',')[source]#
- Parameters:
list_join_str (str) –
- Return type:
str | T
- opt_typed_value(key, default=None)[source]#
- Parameters:
key (str) –
default (T|None) –
- Return type:
T|object|str|None
- int(key, default, index=0)[source]#
Parses the value of the given key as integer, returning default if not existent :type key: str :type default: T :type index: int :rtype: int | T
- bool(key, default, index=0)[source]#
Parses the value of the given key as boolean, returning default if not existent :type key: str :type default: T :type index: bool :rtype: bool | T
- bool_or_other(key, default, index=0)[source]#
- Parameters:
key (str) –
default (T) –
index (int) –
- Returns:
if we have typed value, just as-is. otherwise try to convert to bool. or default if not there.
- Return type:
bool|T|object
- returnn.config.global_config_ctx(config: Config)[source]#
sets the config as global config in this context, and recovers the original global config afterwards
- returnn.config.set_global_config(config)[source]#
Will define the global config, returned by
get_global_config()
- Parameters:
config (Config) –
- returnn.config.get_global_config(raise_exception=True, auto_create=False)[source]#
- Parameters:
raise_exception (bool) – if no global config is found, raise an exception, otherwise return None
auto_create (bool) – if no global config is found, it creates one and returns it
- Return type:
Config|None