本文整理汇总了Python中lib.utils.string_utils.StringMethods.merge_dicts_by_same_key方法的典型用法代码示例。如果您正苦于以下问题:Python StringMethods.merge_dicts_by_same_key方法的具体用法?Python StringMethods.merge_dicts_by_same_key怎么用?Python StringMethods.merge_dicts_by_same_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib.utils.string_utils.StringMethods
的用法示例。
在下文中一共展示了StringMethods.merge_dicts_by_same_key方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_repr_rest_to_ui
# 需要导入模块: from lib.utils.string_utils import StringMethods [as 别名]
# 或者: from lib.utils.string_utils.StringMethods import merge_dicts_by_same_key [as 别名]
def convert_repr_rest_to_ui(obj):
"""Convert object's attributes from REST to UI like representation."""
def convert_attr_val_repr_dict_to_unicode(attr_name, attr_value):
"""Convert attribute value from dictionary to unicode representation
(get value by key from dictionary 'attr_value' where key determine
according to 'attr_name').
"""
if isinstance(attr_value, dict):
converted_attr_value = attr_value
if attr_name in Representation.people_attrs_names + [
"created_by", "modified_by"
]:
converted_attr_value = unicode(attr_value.get("email"))
if attr_name in ["custom_attribute_definitions", "program", "audit",
"mapped_objects"]:
converted_attr_value = (
unicode(attr_value.get("title")) if
attr_name != "custom_attribute_definitions" else
{attr_value.get("id"): attr_value.get("title").upper()}
)
if attr_name in ["custom_attribute_values"]:
converted_attr_value = {attr_value.get("custom_attribute_id"):
attr_value.get("attribute_value")}
if obj_attr_name == "comments":
converted_attr_value = {
k: (parser.parse(v).replace(tzinfo=tz.tzutc()) if
k == "created_at" and isinstance(v, unicode) else v)
for k, v in attr_value.iteritems()
if k in ["modified_by", "created_at", "description"]}
if attr_name == "assertions":
for name, assertion_id in ControlEntity.ASSERTIONS.iteritems():
if assertion_id == attr_value["id"]:
converted_attr_value = name
return converted_attr_value
origin_obj = copy.deepcopy(obj)
for obj_attr_name in obj.__dict__.keys():
# 'Ex', u'Ex', 1, None to 'Ex', u'Ex', 1, None
obj_attr_value = getattr(obj, obj_attr_name)
# REST like u'08-20-2017T04:30:45' to date=2017-08-20,
# timetz=04:30:45+00:00
if (obj_attr_name in ["updated_at", "created_at"] and
isinstance(obj_attr_value, unicode)):
obj_attr_value = (parser.parse(obj_attr_value).
replace(tzinfo=tz.tzutc()))
if isinstance(obj_attr_value, dict) and obj_attr_value:
# "modified_by" {"type": "Person", "id": x} to u'[email protected]'
# todo: deprecated?
if obj_attr_name == "modified_by":
from lib.service import rest_service
obj_attr_value = getattr(rest_service.ObjectsInfoService().get_obj(
obj=Representation.repr_dict_to_obj(obj_attr_value)), "email")
# {'name': u'Ex1', 'type': u'Ex2', ...} to u'Ex1'
else:
obj_attr_value = convert_attr_val_repr_dict_to_unicode(
obj_attr_name, obj_attr_value)
# [el1, el2, ...] or [{item1}, {item2}, ...] to [u'Ex1, u'Ex2', ...]
if (isinstance(obj_attr_value, list) and
all(isinstance(item, dict) for item in obj_attr_value)):
obj_attr_value = [
convert_attr_val_repr_dict_to_unicode(obj_attr_name, item) for
item in obj_attr_value]
setattr(obj, obj_attr_name, obj_attr_value)
# merge "custom_attribute_definitions" and "custom_attribute_values"
obj_cas_attrs_names = [
"custom_attributes", "custom_attribute_definitions",
"custom_attribute_values"]
if set(obj_cas_attrs_names).issubset(obj.__dict__.keys()):
cas_def = obj.custom_attribute_definitions
cas_val = obj.custom_attribute_values
# form CAs values of CAs definitions exist but CAs values not, or CAs
# definitions have different then CAs values lengths
if (cas_def and
(not cas_val or (isinstance(cas_def and cas_val, list)) and
len(cas_def) != len(cas_val))):
from lib.entities.entities_factory import (
CustomAttributeDefinitionsFactory)
cas_val_dicts_keys = ([_.keys()[0] for _ in cas_val] if
isinstance(cas_val, list) else [None])
_cas_val = [
{k: v} for k, v in
CustomAttributeDefinitionsFactory.generate_ca_title_id(
[Representation.repr_dict_to_obj(cad)
for cad in origin_obj.custom_attribute_definitions]
).iteritems() if k not in cas_val_dicts_keys]
cas_val = _cas_val if not cas_val else cas_val + _cas_val
cas_def_dict = (
dict([_def.iteritems().next() for _def in cas_def]) if
(isinstance(cas_def, list) and
all(isinstance(_def, dict)
for _def in cas_def)) else None)
cas_val_dict = (
dict([_val.iteritems().next() for _val in cas_val]) if
(isinstance(cas_def, list) and
all(isinstance(_def, dict)
for _def in cas_def)) else None)
cas = StringMethods.merge_dicts_by_same_key(cas_def_dict, cas_val_dict)
if obj.custom_attributes:
cas.update(obj.custom_attributes)
if cas in [{None: None}, {}]:
cas = None
#.........这里部分代码省略.........