Config

Provides Config and some related helpers.

class 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(self, 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
parse_cmd_args(self, args)[source]
Parameters:args (list[str]|tuple[str]) –
add_line(self, key, value)[source]

Adds one specific configuration (key,value) pair to the inner set of parameters :type key: str :type value: str

has(self, 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(self, key)[source]
Return type:boolean

:returns True if and only if the value of the given key has a specified data type

is_true(self, key, default=False)[source]
Parameters:
  • key (str) –
  • default (bool) –
Returns:

bool(value) if it is set or default

Return type:

bool

is_of_type(self, 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(self, 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

set(self, key, value)[source]
update(self, dikt)[source]
value(self, key, default, index=None, list_join_str=', ')[source]
Parameters:list_join_str (str) –
Return type:str | T
typed_value(self, key, default=None, index=None)[source]
Return type:T | object
opt_typed_value(self, key, default=None)[source]
Parameters:
  • key (str) –
  • default (T|None) –
Return type:

T|object|str|None

int(self, 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(self, 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(self, 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

float(self, key, default, index=0)[source]

Parses the value of the given key as float, returning default if not existent :type key: str :type default: T :type index: int :rtype: float | T

list(self, key, default=None)[source]
Return type:list[str] | T
int_list(self, key, default=None)[source]
Return type:list[int] | T
float_list(self, key, default=None)[source]
Return type:list[float] | T
int_pair(self, key, default=None)[source]
Parameters:
  • key (str) –
  • default ((int,int)|None) –
Return type:

(int,int)

Config.set_global_config(config)[source]

Will define the global config, returned by get_global_config()

Parameters:config (Config) –
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

Config.network_json_from_config(config, mask=None)[source]
Parameters:mask (str) – “unity”, “none” or “dropout”
Return type:dict[str]
Config.get_devices_init_args(config)[source]
Return type:list[dict[str]]