本文整理汇总了Python中collections.abc.KeysView方法的典型用法代码示例。如果您正苦于以下问题:Python abc.KeysView方法的具体用法?Python abc.KeysView怎么用?Python abc.KeysView使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections.abc
的用法示例。
在下文中一共展示了abc.KeysView方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extra_serializer
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def extra_serializer(obj: Any) -> Union[int, str, List[Any], Dict[str, Any]]:
"""JSON serializer for objects not serializable by default json code"""
if isinstance(obj, datetime.datetime):
return dtutil.dt2ts(obj)
if isinstance(obj, bytes):
return obj.decode('utf-8')
if isinstance(obj, decimal.Decimal):
return obj.to_eng_string()
if isinstance(obj, (set, KeysView)):
return list(obj)
if isinstance(obj, Exception):
stack = traceback.extract_tb(obj.__traceback__)
return traceback.format_list(stack)
if hasattr(obj, 'to_dict'):
return obj.to_dict()
if hasattr(obj, '__attrs_attrs__'):
val: Dict[str, Any] = {}
for a in obj.__attrs_attrs__:
val[a.name] = getattr(obj, a.name)
return val
raise TypeError('Type {t} not serializable - {obj}'.format(t=type(obj), obj=obj))
示例2: test_MutableMapping_subclass
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def test_MutableMapping_subclass(self):
# Test issue 9214
mymap = UserDict()
mymap['red'] = 5
self.assertIsInstance(mymap.keys(), Set)
self.assertIsInstance(mymap.keys(), KeysView)
self.assertIsInstance(mymap.items(), Set)
self.assertIsInstance(mymap.items(), ItemsView)
mymap = UserDict()
mymap['red'] = 5
z = mymap.keys() | {'orange'}
self.assertIsInstance(z, set)
list(z)
mymap['blue'] = 7 # Shouldn't affect 'z'
self.assertEqual(sorted(z), ['orange', 'red'])
mymap = UserDict()
mymap['red'] = 5
z = mymap.items() | {('orange', 3)}
self.assertIsInstance(z, set)
list(z)
mymap['blue'] = 7 # Shouldn't affect 'z'
self.assertEqual(sorted(z), [('orange', 3), ('red', 5)])
示例3: variables
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def variables(self):
"""The variables of the binary quadratic model."""
return KeysView(self.linear)
示例4: keys
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def keys(self):
return KeysView(self)
示例5: make_unique_list
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def make_unique_list(key, allow_array=False):
if isinstance(key, (Index, abc.KeysView)):
key = list(key)
is_list = (
isinstance(key, (list, tuple, np.record))
if allow_array
else isinstance(key, (list, tuple, np.ndarray, np.record))
)
is_list_of_str = is_list and all(isinstance(item, str) for item in key)
return key if is_list_of_str else key if is_list and len(key) < 20 else [key]
示例6: has_intrinsic_functions
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def has_intrinsic_functions(parameter):
intrinsic_functions = ["Fn::Sub", "!Sub", "!GetAtt"]
result = False
if isinstance(parameter, (list, tuple, dict, KeysView)):
for item in parameter:
if item in intrinsic_functions:
result = True
break
return result
示例7: keys
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def keys(self) -> KeysView:
r"""Return all keys of the resources.
"""
return self.resources.keys()
示例8: keys
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def keys(self):
"D.keys() -> a set-like object providing a view on D's keys"
return KeysView(self)
示例9: keys
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def keys(self):
"""D.keys() => a set-like object providing a view on D's keys"""
return stdlib_collections.KeysView(self)
示例10: short_repr
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import KeysView [as 别名]
def short_repr(obj, noneAsNA=False):
'''Return a short representation of obj for clarity.'''
if obj is None:
return 'unspecified' if noneAsNA else 'None'
elif isinstance(obj, str) and len(obj) > 80:
return '{}...{}'.format(obj[:60].replace('\n', '\\n'),
obj[-20:].replace('\n', '\\n'))
elif isinstance(obj, (str, int, float, bool)):
return repr(obj)
elif hasattr(obj, '__short_repr__'):
return obj.__short_repr__()
elif isinstance(obj, Sequence): # should be a list or tuple
if len(obj) == 0:
return '[]'
elif len(obj) == 1:
return f'{short_repr(obj[0])}'
elif len(obj) == 2:
return f'{short_repr(obj[0])}, {short_repr(obj[1])}'
else:
return f'{short_repr(obj[0])}, {short_repr(obj[1])}, ... ({len(obj)} items)'
elif isinstance(obj, dict):
if not obj:
return ''
elif len(obj) == 1:
first_key = list(obj.keys())[0]
return f'{short_repr(first_key)!r}:{short_repr(obj[first_key])!r}'
else:
first_key = list(obj.keys())[0]
return f'{short_repr(first_key)}:{short_repr(obj[first_key])}, ... ({len(obj)} items)'
elif isinstance(obj, KeysView):
if not obj:
return ''
elif len(obj) == 1:
return short_repr(next(iter(obj)))
else:
return f'{short_repr(next(iter(obj)))}, ... ({len(obj)} items)'
# elif hasattr(obj, 'target_name'):
# return obj.target_name()
else:
ret = str(obj)
if len(ret) > 40:
return f'{repr(obj)[:35]}...'
else:
return ret
#
# SoS Workflow dictionary
#