本文整理汇总了Python中odict.OrderedDict.update方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict.update方法的具体用法?Python OrderedDict.update怎么用?Python OrderedDict.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类odict.OrderedDict
的用法示例。
在下文中一共展示了OrderedDict.update方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _flatten_dictionary
# 需要导入模块: from odict import OrderedDict [as 别名]
# 或者: from odict.OrderedDict import update [as 别名]
def _flatten_dictionary(self, params, parent=None):
"""
Flatten a hierarchical dictionary into a simple dictionary.
>>> self._flatten_dictionary({
"test": {
"foo": 12,
"bar": "hello",
},
"baz": False
})
{
"test[foo]": 12,
"test[bar]": hello,
"baz": False
}
"""
data = OrderedDict()
for key, val in params.items():
full_key = parent + "[" + key + "]" if parent else key
if isinstance(val, dict):
data.update(self._flatten_dictionary(val, full_key))
else:
data[full_key] = val
return data
示例2: _flatten_errors
# 需要导入模块: from odict import OrderedDict [as 别名]
# 或者: from odict.OrderedDict import update [as 别名]
def _flatten_errors(self, params, parent=None):
"""
A modified version of the flatten_dictionary method above used
to coerce the structure holding errors returned by Braintree into
a flattened dictionary where the keys are the names of the fields
and the values the error messages, which can be directly used to
set the field errors on the Django form object for display in
templates.
"""
data = OrderedDict()
for key, val in params.items():
full_key = parent + "[" + key + "]" if parent else key
if full_key.endswith("[errors]"):
full_key = full_key[:-len("[errors]")]
if isinstance(val, dict):
data.update(self._flatten_errors(val, full_key))
elif key == "errors":
for error in val:
data[full_key + "[" + error["attribute"] + "]"] = [error["message"]]
else:
data[full_key] = [val]
return data