{ } Raw JSON

bundles / astropy latest / astropy / extern / configobj / configobj / Section / walk

function

astropy.extern.configobj.configobj:Section.walk

source: /astropy/extern/configobj/configobj.py :840

Signature

def   walk ( self function raise_errors = True call_on_sections = False ** keywargs )

Summary

Walk every member and call a function on the keyword and value.

Extended Summary

Return a dictionary of the return values

If the function raises an exception, raise the errror unless raise_errors=False, in which case set the return value to False.

Any unrecognized keyword arguments you pass to walk, will be pased on to the function you pass in.

Note: if call_on_sections is True then - on encountering a subsection, first the function is called for the whole subsection, and then recurses into it's members. This means your function must be able to handle strings, dictionaries and lists. This allows you to change the key of subsections as well as for ordinary members. The return value when called on the whole subsection has to be discarded.

See the encode and decode methods for examples, including functions.

>>> config = '''[XXXXsection]
... XXXXkey = XXXXvalue'''.splitlines()
>>> cfg = ConfigObj(config)
>>> cfg
ConfigObj({'XXXXsection': {'XXXXkey': 'XXXXvalue'}})
>>> def transform(section, key):
...     val = section[key]
...     newkey = key.replace('XXXX', 'CLIENT1')
...     section.rename(key, newkey)
...     if isinstance(val, (tuple, list, dict)):
...         pass
...     else:
...         val = val.replace('XXXX', 'CLIENT1')
...         section[newkey] = val
>>> cfg.walk(transform, call_on_sections=True)
{'CLIENT1section': {'CLIENT1key': None}}
>>> cfg
ConfigObj({'CLIENT1section': {'CLIENT1key': 'CLIENT1value'}})

Aliases

  • astropy.config.configuration.configobj.Section.walk