75 lines
2.0 KiB
ReStructuredText
75 lines
2.0 KiB
ReStructuredText
>>> import builtins
|
|
|
|
>>> builtin_vars = vars(builtins)
|
|
|
|
>>> key = 'something to search for'
|
|
|
|
>>> if key in locals():
|
|
... value = locals()[key]
|
|
... elif key in globals():
|
|
... value = globals()[key]
|
|
... elif key in builtin_vars:
|
|
... value = builtin_vars[key]
|
|
... else:
|
|
... raise NameError(f'name {key!r} is not defined')
|
|
Traceback (most recent call last):
|
|
...
|
|
NameError: name 'something to search for' is not defined
|
|
|
|
##############################################################################
|
|
|
|
>>> mappings = locals(), globals(), vars(builtins)
|
|
>>> for mapping in mappings:
|
|
... if key in mapping:
|
|
... value = mapping[key]
|
|
... break
|
|
... else:
|
|
... raise NameError(f'name {key!r} is not defined')
|
|
Traceback (most recent call last):
|
|
...
|
|
NameError: name 'something to search for' is not defined
|
|
|
|
##############################################################################
|
|
|
|
>>> import collections
|
|
|
|
>>> mappings = collections.ChainMap(
|
|
... locals(), globals(), vars(builtins))
|
|
>>> mappings[key]
|
|
Traceback (most recent call last):
|
|
...
|
|
KeyError: 'something to search for'
|
|
|
|
##############################################################################
|
|
|
|
>>> import json
|
|
>>> import pathlib
|
|
>>> import argparse
|
|
>>> import collections
|
|
|
|
>>> DEFAULT = dict(verbosity=1)
|
|
|
|
>>> config_file = pathlib.Path('config.json')
|
|
>>> if config_file.exists():
|
|
... config = json.load(config_file.open())
|
|
... else:
|
|
... config = dict()
|
|
|
|
>>> parser = argparse.ArgumentParser()
|
|
>>> parser.add_argument('-v', '--verbose', action='count',
|
|
... dest='verbosity')
|
|
_CountAction(...)
|
|
|
|
>>> args, _ = parser.parse_known_args(args=['-v'])
|
|
>>> defined_args = {k: v for k, v in vars(args).items() if v}
|
|
>>> combined = collections.ChainMap(defined_args, config, DEFAULT)
|
|
>>> combined['verbosity']
|
|
1
|
|
|
|
>>> args, _ = parser.parse_known_args(['-vv'])
|
|
>>> defined_args = {k: v for k, v in vars(args).items() if v}
|
|
>>> combined = collections.ChainMap(defined_args, config, DEFAULT)
|
|
>>> combined['verbosity']
|
|
2
|
|
|