towhee.hparam.hyperparameter.param_scope

class towhee.hparam.hyperparameter.param_scope(*args, **kws)[source]

Bases: HyperParameter

thread-safe scoped hyperparameter

Examples: create a scoped HyperParameter >>> with param_scope(**{‘a’: 1, ‘b’: 2}) as cfg: … print(cfg.a) 1

read parameter in a function >>> def foo(): … with param_scope() as cfg: … return cfg.a >>> with param_scope(**{‘a’: 1, ‘b’: 2}) as cfg: … foo() # foo should get cfg using a with statement 1

update some config only in new scope >>> with param_scope(**{‘a’: 1, ‘b’: 2}) as cfg: … cfg.b … with param_scope(**{‘b’: 3}) as cfg2: … cfg2.b 2 3

Methods

callholder

clear

copy

dispatch

Return a call holder.

fromkeys

Create a new dictionary with keys from iterable and values set to value.

get

get a parameter by a string name

init

init param_scope for a new thread.

items

keys

load

Load parameters from json file, similar as json.load.

loads

Load parameters from JSON string, similar as json.loads.

pop

If key is not found, default is returned if given, otherwise KeyError is raised

popitem

Remove and return a (key, value) pair as a 2-tuple.

put

put/update a parameter with a string name

setdefault

Insert key with a value of default if key is not in the dictionary.

update

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values

Attributes

tls

__call__() Any

Return a parameter accessor.

Returns:

holder of the current parameter

Return type:

Any

Examples: >>> cfg = HyperParameter(a=1, b = {‘c’:2, ‘d’: 3}) >>> cfg().a.get_or_else(‘default’) # default value for simple parameter 1 >>> cfg().b.c.get_or_else(‘default’) # default value for nested parameter 2 >>> cfg().b.undefined.get_or_else(‘default’) ‘default’

__getattr__(name)

read parameter with object-style api

Examples:

for simple parameters: >>> hp = HyperParameter(a=1, b = {‘c’:2, ‘d’: 3}) >>> hp.a 1

for nested parameters: >>> hp.b.c 2

>>> getattr(hp, 'b.c')
2
__getitem__()

x.__getitem__(y) <==> x[y]

__init__(*args, **kws)[source]
__or__(value, /)

Return self|value.

__repr__()

Return repr(self).

__setitem__(key, value)

set value and convert the value into HyperParameter if necessary

clear() None.  Remove all items from D.
copy() a shallow copy of D
dispatch(callback: Optional[Callable] = None)

Return a call holder.

Examples: >>> def debug_print(path, index, *arg, **kws): … return (path, index, arg, kws) >>> ch = param_scope().dispatch(debug_print) >>> ch.my.foo(a=1,b=2) (‘my.foo’, None, (), {‘a’: 1, ‘b’: 2})

>>> ch.myspace2.gee(c=1,d=2)
('myspace2.gee', None, (), {'c': 1, 'd': 2})
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(name: str) Any

get a parameter by a string name

Parameters:

name (str) – parameter name

Returns:

parameter value

Return type:

Any

Examples: >>> cfg = HyperParameter(a=1, b = {‘c’:2, ‘d’: 3}) >>> cfg.get(‘a’) 1 >>> cfg.get(‘b.c’) 2

static init(params)[source]

init param_scope for a new thread.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
static load(f)

Load parameters from json file, similar as json.load.

static loads(s)

Load parameters from JSON string, similar as json.loads.

pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, default is returned if given, otherwise KeyError is raised

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

put(name: str, value: Any)

put/update a parameter with a string name

Parameters:
  • name (str) – parameter name, ‘obj.prop’ is supported

  • value (Any) – parameter value

Examples: >>> cfg = HyperParameter() >>> cfg.put(‘param1’, 1) >>> cfg.put(‘obj1.propA’, ‘A’)

>>> cfg.param1
1
>>> cfg.obj1.propA
'A'
setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values