本文整理汇总了Python中lib.utils.string_utils.StringMethods.exchange_dicts_items方法的典型用法代码示例。如果您正苦于以下问题:Python StringMethods.exchange_dicts_items方法的具体用法?Python StringMethods.exchange_dicts_items怎么用?Python StringMethods.exchange_dicts_items使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib.utils.string_utils.StringMethods
的用法示例。
在下文中一共展示了StringMethods.exchange_dicts_items方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_obj_attrs_values
# 需要导入模块: from lib.utils.string_utils import StringMethods [as 别名]
# 或者: from lib.utils.string_utils.StringMethods import exchange_dicts_items [as 别名]
def update_obj_attrs_values(obj, is_replace_attrs_values,
is_allow_none_values, **attrs):
"""Update object's attributes values."""
for obj_attr_name in attrs:
obj_attr_value = None
if obj_attr_name in Representation.all_attrs_names():
_obj_attr_value = attrs.get(obj_attr_name)
if not is_replace_values_of_dicts:
# convert repr from objects to dicts exclude datetime objects
obj_attr_value = (
cls.repr_obj_to_dict(_obj_attr_value) if
not isinstance(_obj_attr_value, datetime) else _obj_attr_value)
if not is_replace_attrs_values:
origin_obj_attr_value = getattr(obj, obj_attr_name)
obj_attr_value = (
dict(origin_obj_attr_value.items() + obj_attr_value.items())
if obj_attr_name == "custom_attributes" else
help_utils.convert_to_list(origin_obj_attr_value) +
help_utils.convert_to_list(obj_attr_value))
if is_replace_values_of_dicts and isinstance(_obj_attr_value, dict):
obj_attr_value = StringMethods.exchange_dicts_items(
transform_dict=_obj_attr_value,
dicts=help_utils.convert_to_list(
getattr(obj, obj_attr_name)),
is_keys_not_values=False)
obj_attr_value = (
obj_attr_value if isinstance(getattr(obj, obj_attr_name), list)
else obj_attr_value[0])
if (is_allow_none_values is True or
(is_allow_none_values is False and
obj_attr_value is not None)):
setattr(obj, obj_attr_name, obj_attr_value)
return obj
示例2: _create_list_objs
# 需要导入模块: from lib.utils.string_utils import StringMethods [as 别名]
# 或者: from lib.utils.string_utils.StringMethods import exchange_dicts_items [as 别名]
def _create_list_objs(self, entity_factory, list_scopes):
"""Create and return list of objects used entity factory and UI data
(list of scopes UI text elements {"header": "item", ...} remapped to
list of dicts {"attr": "value", ...}).
Return list of created objects.
"""
list_factory_objs = [
entity_factory().obj_inst() for _ in xrange(len(list_scopes))]
list_scopes_with_upper_keys = [
StringMethods.dict_keys_to_upper_case(scope) for scope in list_scopes]
list_scopes_to_convert = StringMethods.exchange_dicts_items(
transform_dict=Representation.remap_collection(),
dicts=list_scopes_with_upper_keys, is_keys_not_values=True)
# convert and represent values in scopes
for scope in list_scopes_to_convert:
# convert u'None', u'No person' to None type
StringMethods.update_dicts_values(scope, ["None", "No person"], None)
for key, val in scope.iteritems():
if val:
if key in ["mandatory", "verified"]:
# convert u'false', u'true' like to Boolean
scope[key] = StringMethods.get_bool_value_from_arg(val)
if key in ["updated_at", "created_at"]:
# UI like u'08/20/2017' to date=2017-08-20, timetz=00:00:00
datetime_val = parser.parse(val)
if str(datetime_val.time()) != "00:00:00":
# UI like u'08/20/2017 07:30:45 AM +03:00' to date=2017-08-20,
# timetz=04:30:45+00:00 if 'tzinfo', else:
# CSV like u'08-20-2017 04:30:45' to date=2017-08-20,
# timetz=04:30:45+00:00
datetime_val = (
datetime_val.astimezone(tz=tz.tzutc()) if datetime_val.tzinfo
else datetime_val.replace(tzinfo=tz.tzutc()))
scope[key] = datetime_val
if (key == "comments" and isinstance(val, list) and
all(isinstance(comment, dict) for comment in val)):
# extract datetime from u'(Creator) 08/20/2017 07:30:45 AM +03:00'
scope[key] = [
{k: (parser.parse(re.sub(regex.TEXT_W_PARENTHESES,
Symbols.BLANK, v)
).astimezone(tz=tz.tzutc())
if k == "created_at" else v)
for k, v in comment.iteritems()} for comment in val]
# convert multiple values to list of strings and split if need it
if (key in Representation.people_attrs_names and
not isinstance(val, list)):
# split Tree View values if need 'Ex1, Ex2 F' to ['Ex1', 'Ex2 F']
# Info Widget values will be represent by internal methods
scope[key] = val.split(", ")
# convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d'
if (key == "slug" and
(self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS) and
Symbols.STAR in val):
scope[key] = val.replace(Symbols.STAR, Symbols.BLANK)
return [
factory_obj.update_attrs(is_allow_none=True, **scope) for
scope, factory_obj in zip(list_scopes_to_convert, list_factory_objs)]