MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / load_dict

Method load_dict

21-async/mojifinder/bottle.py:2111–2133  ·  view source on GitHub ↗

Import values from a dictionary structure. Nesting can be used to represent namespaces. >>> ConfigDict().load_dict({'name': {'space': {'key': 'value'}}}) {'name.space.key': 'value'}

(self, source, namespace='', make_namespaces=False)

Source from the content-addressed store, hash-verified

2109 return self
2110
2111 def load_dict(self, source, namespace='', make_namespaces=False):
2112 ''' Import values from a dictionary structure. Nesting can be used to
2113 represent namespaces.
2114
2115 >>> ConfigDict().load_dict({'name': {'space': {'key': 'value'}}})
2116 {'name.space.key': 'value'}
2117 '''
2118 stack = [(namespace, source)]
2119 while stack:
2120 prefix, source = stack.pop()
2121 if not isinstance(source, dict):
2122 raise TypeError('Source is not a dict (r)' % type(key))
2123 for key, value in source.items():
2124 if not isinstance(key, basestring):
2125 raise TypeError('Key is not a string (%r)' % type(key))
2126 full_key = prefix + '.' + key if prefix else key
2127 if isinstance(value, dict):
2128 stack.append((full_key, value))
2129 if make_namespaces:
2130 self[full_key] = self.Namespace(self, full_key)
2131 else:
2132 self[full_key] = value
2133 return self
2134
2135 def update(self, *a, **ka):
2136 ''' If the first parameter is a string, all keys are prefixed with this

Callers 1

__init__Method · 0.80

Calls 3

popMethod · 0.80
itemsMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected