Python collections.abc() Examples
The following are 30
code examples of collections.abc().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
collections
, or try the search function
.
Example #1
Source File: test_set.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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]))
Example #2
Source File: test_set.py From ironpython3 with Apache License 2.0 | 6 votes |
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]))
Example #3
Source File: test_dict.py From ironpython3 with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_set.py From ironpython3 with Apache License 2.0 | 6 votes |
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,)))
Example #5
Source File: test_set.py From ironpython3 with Apache License 2.0 | 6 votes |
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))
Example #6
Source File: test_iter.py From ironpython3 with Apache License 2.0 | 6 votes |
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
Example #7
Source File: test_iter.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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
Example #8
Source File: __init__.py From Imogen with MIT License | 6 votes |
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)
Example #9
Source File: test_set.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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))
Example #10
Source File: typing.py From Imogen with MIT License | 6 votes |
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)
Example #11
Source File: test_types.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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')
Example #12
Source File: test_set.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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,)))
Example #13
Source File: test_dict.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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)
Example #14
Source File: slob.py From slob with GNU General Public License v3.0 | 6 votes |
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])
Example #15
Source File: test_iter.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_builtin_tuple(self): self.assertEqual(tuple(SequenceClass(5)), (0, 1, 2, 3, 4)) self.assertEqual(tuple(SequenceClass(0)), ()) self.assertEqual(tuple([]), ()) self.assertEqual(tuple(()), ()) self.assertEqual(tuple("abc"), ("a", "b", "c")) d = {"one": 1, "two": 2, "three": 3} self.assertEqual(tuple(d), tuple(d.keys())) self.assertRaises(TypeError, tuple, list) self.assertRaises(TypeError, tuple, 42) f = open(TESTFN, "w") try: for i in range(5): f.write("%d\n" % i) finally: f.close() f = open(TESTFN, "r") try: self.assertEqual(tuple(f), ("0\n", "1\n", "2\n", "3\n", "4\n")) f.seek(0, 0) self.assertEqual(tuple(f), ("0\n", "1\n", "2\n", "3\n", "4\n")) finally: f.close() try: unlink(TESTFN) except OSError: pass # Test filter()'s use of iterators.
Example #16
Source File: test_collections.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_name_fixer(self): for spec, renamed in [ [('efg', 'g%hi'), ('efg', '_1')], # field with non-alpha char [('abc', 'class'), ('abc', '_1')], # field has keyword [('8efg', '9ghi'), ('_0', '_1')], # field starts with digit [('abc', '_efg'), ('abc', '_1')], # field with leading underscore [('abc', 'efg', 'efg', 'ghi'), ('abc', 'efg', '_2', 'ghi')], # duplicate field [('abc', '', 'x'), ('abc', '_1', 'x')], # fieldname is a space ]: self.assertEqual(namedtuple('NT', spec, rename=True)._fields, renamed)
Example #17
Source File: test_collections.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_factory(self): Point = namedtuple('Point', 'x y') self.assertEqual(Point.__name__, 'Point') self.assertEqual(Point.__slots__, ()) self.assertEqual(Point.__module__, __name__) self.assertEqual(Point.__getitem__, tuple.__getitem__) self.assertEqual(Point._fields, ('x', 'y')) self.assertIn('class Point(tuple)', Point._source) self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char self.assertRaises(ValueError, namedtuple, 'class', 'efg ghi') # type has keyword self.assertRaises(ValueError, namedtuple, '9abc', 'efg ghi') # type starts with digit self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit self.assertRaises(ValueError, namedtuple, 'abc', '_efg ghi') # field with leading underscore self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names namedtuple('_', 'a b c') # Test leading underscores in a typename nt = namedtuple('nt', 'the quick brown fox') # check unicode input self.assertNotIn("u'", repr(nt._fields)) nt = namedtuple('nt', ('the', 'quick')) # check unicode input self.assertNotIn("u'", repr(nt._fields)) self.assertRaises(TypeError, Point._make, [11]) # catch too few args self.assertRaises(TypeError, Point._make, [11, 22, 33]) # catch too many args
Example #18
Source File: test_iter.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_in_and_not_in(self): for sc5 in IteratingSequenceClass(5), SequenceClass(5): for i in range(5): self.assertIn(i, sc5) for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5: self.assertNotIn(i, sc5) self.assertRaises(TypeError, lambda: 3 in 12) self.assertRaises(TypeError, lambda: 3 not in map) d = {"one": 1, "two": 2, "three": 3, 1j: 2j} for k in d: self.assertIn(k, d) self.assertNotIn(k, d.values()) for v in d.values(): self.assertIn(v, d.values()) self.assertNotIn(v, d) for k, v in d.items(): self.assertIn((k, v), d.items()) self.assertNotIn((v, k), d.items()) f = open(TESTFN, "w") try: f.write("a\n" "b\n" "c\n") finally: f.close() f = open(TESTFN, "r") try: for chunk in "abc": f.seek(0, 0) self.assertNotIn(chunk, f) f.seek(0, 0) self.assertIn((chunk + "\n"), f) finally: f.close() try: unlink(TESTFN) except OSError: pass # Test iterators with operator.countOf (PySequence_Count).
Example #19
Source File: fix_operator.py From Imogen with MIT License | 5 votes |
def _isMappingType(self, node, results): return self._handle_type2abc(node, results, "collections.abc", "Mapping")
Example #20
Source File: fix_operator.py From Imogen with MIT License | 5 votes |
def _isSequenceType(self, node, results): return self._handle_type2abc(node, results, "collections.abc", "Sequence")
Example #21
Source File: typing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def __instancecheck__(self, obj): # For unparametrized Callable we allow this, because # typing.Callable should be equivalent to # collections.abc.Callable. if self.__args__ is None and self.__result__ is None: return isinstance(obj, collections_abc.Callable) else: raise TypeError("Callable[] cannot be used with isinstance().")
Example #22
Source File: test_set.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.set = set((1, 2, 3)) self.other = 'abc' self.otherIsIterable = True #------------------------------------------------------------------------------
Example #23
Source File: test_set.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_discard_absent(self): self.set.discard("d") self.assertEqual(self.set, set("abc"))
Example #24
Source File: test_set.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_instancesWithoutException(self): # All of these iterables should load without exception. set([1,2,3]) set((1,2,3)) set({'one':1, 'two':2, 'three':3}) set(range(3)) set('abc') set(gooditer())
Example #25
Source File: test_set.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_difference_update(self): retval = self.s.difference_update(self.otherword) self.assertEqual(retval, None) for c in (self.word + self.otherword): if c in self.word and c not in self.otherword: self.assertIn(c, self.s) else: self.assertNotIn(c, self.s) self.assertRaises(PassThru, self.s.difference_update, check_pass_thru()) self.assertRaises(TypeError, self.s.difference_update, [[]]) self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')): for C in set, frozenset, dict.fromkeys, str, list, tuple: s = self.thetype('abcba') self.assertEqual(s.difference_update(C(p)), None) self.assertEqual(s, set(q)) s = self.thetype('abcdefghih') s.difference_update() self.assertEqual(s, self.thetype('abcdefghih')) s = self.thetype('abcdefghih') s.difference_update(C('aba')) self.assertEqual(s, self.thetype('cdefghih')) s = self.thetype('abcdefghih') s.difference_update(C('cdc'), C('aba')) self.assertEqual(s, self.thetype('efghih'))
Example #26
Source File: test_set.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_difference_update(self): retval = self.s.difference_update(self.otherword) self.assertEqual(retval, None) for c in (self.word + self.otherword): if c in self.word and c not in self.otherword: self.assertIn(c, self.s) else: self.assertNotIn(c, self.s) self.assertRaises(PassThru, self.s.difference_update, check_pass_thru()) self.assertRaises(TypeError, self.s.difference_update, [[]]) self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')): for C in set, frozenset, dict.fromkeys, str, list, tuple: s = self.thetype('abcba') self.assertEqual(s.difference_update(C(p)), None) self.assertEqual(s, set(q)) s = self.thetype('abcdefghih') s.difference_update() self.assertEqual(s, self.thetype('abcdefghih')) s = self.thetype('abcdefghih') s.difference_update(C('aba')) self.assertEqual(s, self.thetype('cdefghih')) s = self.thetype('abcdefghih') s.difference_update(C('cdc'), C('aba')) self.assertEqual(s, self.thetype('efghih'))
Example #27
Source File: test_set.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_sub_and_super(self): p, q, r = map(self.thetype, ['ab', 'abcde', 'def']) self.assertTrue(p < q) self.assertTrue(p <= q) self.assertTrue(q <= q) self.assertTrue(q > p) self.assertTrue(q >= p) self.assertFalse(q < r) self.assertFalse(q <= r) self.assertFalse(q > r) self.assertFalse(q >= r) self.assertTrue(set('a').issubset('abc')) self.assertTrue(set('abc').issuperset('a')) self.assertFalse(set('a').issubset('cbs')) self.assertFalse(set('cbs').issuperset('a'))
Example #28
Source File: test_set.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_instancesWithoutException(self): # All of these iterables should load without exception. set([1,2,3]) set((1,2,3)) set({'one':1, 'two':2, 'three':3}) set(range(3)) set('abc') set(gooditer())
Example #29
Source File: test_set.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_add_present(self): self.set.add("c") self.assertEqual(self.set, set("abc"))
Example #30
Source File: fix_operator.py From Imogen with MIT License | 5 votes |
def _check_method(self, node, results): method = getattr(self, "_" + results["method"][0].value) if isinstance(method, collections.abc.Callable): if "module" in results: return method else: sub = (str(results["obj"]),) invocation_str = method.invocation % sub self.warning(node, "You should use '%s' here." % invocation_str) return None