本文整理匯總了Python中collections.abc方法的典型用法代碼示例。如果您正苦於以下問題:Python collections.abc方法的具體用法?Python collections.abc怎麽用?Python collections.abc使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類collections
的用法示例。
在下文中一共展示了collections.abc方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: validate_stream
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def validate_stream(meta):
stream = meta.stream
if stream is None:
return
if not isinstance(stream, collections.abc.MutableMapping):
raise InvalidStream("Stream must be None or a dict.")
if "include" not in stream:
raise InvalidStream("Specify what the stream will return with the 'include' key.")
include = stream["include"] = set(stream["include"])
# []
if not include:
raise InvalidStream("Must include at least one of 'keys', 'old', or 'new'.")
# ["what is this", "keys"]
for value in include:
if value not in {"new", "keys", "old"}:
raise InvalidStream("Streams can only contain 'keys', 'old', and/or 'new'.")
# ["keys", "old"]
if include == {"new", "keys"} or include == {"old", "keys"}:
raise InvalidStream("The option 'keys' cannot be used with either 'old' or 'new'.")
stream.setdefault("arn", None)
示例2: _reshape_2D
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def _reshape_2D(X, name):
"""
Use Fortran ordering to convert ndarrays and lists of iterables to lists of
1D arrays.
Lists of iterables are converted by applying `np.asarray` to each of their
elements. 1D ndarrays are returned in a singleton list containing them.
2D ndarrays are converted to the list of their *columns*.
*name* is used to generate the error message for invalid inputs.
"""
# Iterate over columns for ndarrays, over rows otherwise.
X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))
if len(X) == 0:
return [[]]
if X.ndim == 1 and not isinstance(X[0], collections.abc.Iterable):
# 1D array of scalars: directly return it.
return [X]
elif X.ndim in [1, 2]:
# 2D array, or 1D array of iterables: flatten them first.
return [np.reshape(x, -1) for x in X]
else:
raise ValueError("{} must have 2 or fewer dimensions".format(name))
示例3: from_dict
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def from_dict(cls, entries, **kwargs):
"""
Create Catalog from the given set of entries
Parameters
----------
entries : dict-like
A mapping of name:entry which supports dict-like functionality,
e.g., is derived from ``collections.abc.Mapping``.
kwargs : passed on the constructor
Things like metadata, name; see ``__init__``.
Returns
-------
Catalog instance
"""
cat = cls(**kwargs)
cat._entries = entries
return cat
示例4: check_pickle
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def check_pickle(self, itorg, seq):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
d = pickle.dumps(itorg, proto)
it = pickle.loads(d)
# Cannot assert type equality because dict iterators unpickle as list
# iterators.
# self.assertEqual(type(itorg), type(it))
self.assertTrue(isinstance(it, collections.abc.Iterator))
self.assertEqual(list(it), seq)
it = pickle.loads(d)
try:
next(it)
except StopIteration:
continue
d = pickle.dumps(it, proto)
it = pickle.loads(d)
self.assertEqual(list(it), seq[1:])
# Test basic use of iter() function
示例5: test_strings
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def test_strings(self):
if len('') != 0: self.fail('len(\'\')')
if len('a') != 1: self.fail('len(\'a\')')
if len('abcdef') != 6: self.fail('len(\'abcdef\')')
if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
if 0*'abcde' != '': self.fail('string repetition 0*')
if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string')
if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
else: self.fail('in/not in string')
x = 'x'*103
if '%s!'%x != x+'!': self.fail('nasty string formatting bug')
#extended slices for strings
a = '0123456789'
self.assertEqual(a[::], a)
self.assertEqual(a[::2], '02468')
self.assertEqual(a[1::2], '13579')
self.assertEqual(a[::-1],'9876543210')
self.assertEqual(a[::-2], '97531')
self.assertEqual(a[3::-2], '31')
self.assertEqual(a[-100:100:], a)
self.assertEqual(a[100:-100:-1], a[::-1])
self.assertEqual(a[-100:100:2], '02468')
示例6: test_itemiterator_pickling
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def test_itemiterator_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
data = {1:"a", 2:"b", 3:"c"}
# dictviews aren't picklable, only their iterators
itorg = iter(data.items())
d = pickle.dumps(itorg, proto)
it = pickle.loads(d)
# note that the type of type of the unpickled iterator
# is not necessarily the same as the original. It is
# merely an object supporting the iterator protocol, yielding
# the same objects as the original one.
# self.assertEqual(type(itorg), type(it))
self.assertIsInstance(it, collections.abc.Iterator)
self.assertEqual(dict(it), data)
it = pickle.loads(d)
drop = next(it)
d = pickle.dumps(it, proto)
it = pickle.loads(d)
del data[drop[0]]
self.assertEqual(dict(it), data)
示例7: test_union
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def test_union(self):
u = self.s.union(self.otherword)
for c in self.letters:
self.assertEqual(c in u, c in self.d or c in self.otherword)
self.assertEqual(self.s, self.thetype(self.word))
self.assertEqual(type(u), self.basetype)
self.assertRaises(PassThru, self.s.union, check_pass_thru())
self.assertRaises(TypeError, self.s.union, [[]])
for C in set, frozenset, dict.fromkeys, str, list, tuple:
self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd'))
self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg'))
self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc'))
self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef'))
self.assertEqual(self.thetype('abcba').union(C('ef'), C('fg')), set('abcefg'))
# Issue #6573
x = self.thetype()
self.assertEqual(x.union(set([1]), x, set([2])), self.thetype([1, 2]))
示例8: test_iterator_pickling
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def test_iterator_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
itorg = iter(self.s)
data = self.thetype(self.s)
d = pickle.dumps(itorg, proto)
it = pickle.loads(d)
# Set iterators unpickle as list iterators due to the
# undefined order of set items.
# self.assertEqual(type(itorg), type(it))
self.assertIsInstance(it, collections.abc.Iterator)
self.assertEqual(self.thetype(it), data)
it = pickle.loads(d)
try:
drop = next(it)
except StopIteration:
continue
d = pickle.dumps(it, proto)
it = pickle.loads(d)
self.assertEqual(self.thetype(it), data - self.thetype((drop,)))
示例9: test_update
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def test_update(self):
retval = self.s.update(self.otherword)
self.assertEqual(retval, None)
for c in (self.word + self.otherword):
self.assertIn(c, self.s)
self.assertRaises(PassThru, self.s.update, check_pass_thru())
self.assertRaises(TypeError, self.s.update, [[]])
for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')):
for C in set, frozenset, dict.fromkeys, str, list, tuple:
s = self.thetype('abcba')
self.assertEqual(s.update(C(p)), None)
self.assertEqual(s, set(q))
for p in ('cdc', 'efgfe', 'ccb', 'ef', 'abcda'):
q = 'ahi'
for C in set, frozenset, dict.fromkeys, str, list, tuple:
s = self.thetype('abcba')
self.assertEqual(s.update(C(p), C(q)), None)
self.assertEqual(s, set(s) | set(p) | set(q))
示例10: getChild
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def getChild(self, suffix):
"""
Get a logger which is a descendant to this one.
This is a convenience method, such that
logging.getLogger('abc').getChild('def.ghi')
is the same as
logging.getLogger('abc.def.ghi')
It's useful, for example, when the parent logger is named using
__name__ rather than a literal string.
"""
if self.root is not self:
suffix = '.'.join((self.name, suffix))
return self.manager.getLogger(suffix)
示例11: __getitem_inner__
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def __getitem_inner__(self, params):
if self.__origin__ is tuple and self._special:
if params == ():
return self.copy_with((_TypingEmpty,))
if not isinstance(params, tuple):
params = (params,)
if len(params) == 2 and params[1] is ...:
msg = "Tuple[t, ...]: t must be a type."
p = _type_check(params[0], msg)
return self.copy_with((p, _TypingEllipsis))
msg = "Tuple[t0, t1, ...]: each t must be a type."
params = tuple(_type_check(p, msg) for p in params)
return self.copy_with(params)
if self.__origin__ is collections.abc.Callable and self._special:
args, result = params
msg = "Callable[args, result]: result must be a type."
result = _type_check(result, msg)
if args is Ellipsis:
return self.copy_with((_TypingEllipsis, result))
msg = "Callable[[arg, ...], result]: each arg must be a type."
args = tuple(_type_check(arg, msg) for arg in args)
params = args + (result,)
return self.copy_with(params)
return super().__getitem__(params)
示例12: test_seek_and_read
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def test_seek_and_read(self):
def mkfile(basename, content):
part = os.path.join(self.tmpdir.name, basename)
with fopen(part, 'wb') as f:
f.write(content)
return part
content = b'abc\nd\nefgh\nij'
part1 = mkfile('1', content[:4])
part2 = mkfile('2', content[4:5])
part3 = mkfile('3', content[5:])
with MultiFileReader(part1, part2, part3) as m:
self.assertEqual(m.size, len(content))
m.seek(2)
self.assertEqual(m.read(2), content[2:4])
m.seek(1)
self.assertEqual(m.read(len(content) - 2), content[1:-1])
m.seek(-1, whence=io.SEEK_END)
self.assertEqual(m.read(10), content[-1:])
m.seek(4)
m.seek(-2, whence=io.SEEK_CUR)
self.assertEqual(m.read(3), content[2:5])
示例13: move_to
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def move_to(self, position):
"""Set the Coordinator to a specific endpoint or time, or load state from a token.
:param position: "trim_horizon", "latest", :class:`~datetime.datetime`, or a
:attr:`Coordinator.token <bloop.stream.coordinator.Coordinator.token>`
"""
if isinstance(position, collections.abc.Mapping):
move = _move_stream_token
elif hasattr(position, "timestamp") and callable(position.timestamp):
move = _move_stream_time
elif isinstance(position, str) and position.lower() in ["latest", "trim_horizon"]:
move = _move_stream_endpoint
else:
raise InvalidPosition("Don't know how to move to position {!r}".format(position))
move(self, position)
示例14: validate_projection
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def validate_projection(projection):
validated_projection = {
"mode": None,
"included": None,
"available": None,
"strict": True
}
# String check first since it is also an Iterable.
# Without this, the following will make "unknown" a list
if isinstance(projection, str):
if projection not in ("keys", "all"):
raise InvalidModel(f"{projection!r} is not a valid Index projection.")
validated_projection["mode"] = projection
elif isinstance(projection, collections.abc.Iterable):
projection = list(projection)
# These checks aren't done together; that would allow a mix
# of column instances and column names. There aren't any cases
# where a mix is required, over picking a style. Much more likely,
# the user is trying to do something odd and doesn't understand what
# the index projection means.
if (
all(isinstance(p, str) for p in projection) or
all(isinstance(p, Column) for p in projection)):
validated_projection["mode"] = "include"
validated_projection["included"] = projection
else:
raise InvalidModel("Index projection must be a list of strings or Columns to select specific Columns.")
else:
raise InvalidModel("Index projection must be 'all', 'keys', or a list of Columns or Column names.")
return validated_projection
示例15: validate_encryption
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import abc [as 別名]
def validate_encryption(meta):
encryption = meta.encryption
if encryption is None:
return
if not isinstance(encryption, collections.abc.MutableMapping):
raise InvalidModel("Encryption must be None or a dict.")
if "enabled" not in encryption:
raise InvalidModel("Encryption must specify whether it is enabled with the 'enabled' key.")