Python operator.length_hint() Examples
The following are 27
code examples of operator.length_hint().
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
operator
, or try the search function
.
Example #1
Source File: test_enumerate.py From android_universal with MIT License | 6 votes |
def test_len(self): for s in ('hello', tuple('hello'), list('hello'), range(5)): self.assertEqual(operator.length_hint(reversed(s)), len(s)) r = reversed(s) list(r) self.assertEqual(operator.length_hint(r), 0) class SeqWithWeirdLen: called = False def __len__(self): if not self.called: self.called = True return 10 raise ZeroDivisionError def __getitem__(self, index): return index r = reversed(SeqWithWeirdLen()) self.assertRaises(ZeroDivisionError, operator.length_hint, r)
Example #2
Source File: log.py From OpenNIR with MIT License | 6 votes |
def pbar_raw(self, *args, **kwargs): level = kwargs.pop('level', 'DEBUG') quiet = kwargs.pop('quiet', False) if 'total' not in kwargs and 'total_from' in kwargs: total_from = kwargs.pop('total_from') if hasattr(total_from, '__len__'): kwargs['total'] = len(total_from) elif hasattr(total_from, '__length_hint__'): kwargs['total'] = operator.length_hint(total_from) else: raise ValueError('total_from does not have __len__ or __length_hint__') if 'ncols' not in kwargs: kwargs['ncols'] = 80 if 'desc' in kwargs: if not quiet: self.log(level, '[starting] {desc}'.format(**kwargs)) if 'leave' not in kwargs: kwargs['leave'] = False if 'smoothing' not in kwargs: kwargs['smoothing'] = 0. # disable smoothing by default; mean over entire life of pbar with tqdm(*args, **kwargs) as pbar: yield pbar if not quiet: pbar.bar_format = '{desc}: [{elapsed}] [{n_fmt}it] [{rate_fmt}]' self.log(level, '[finished] ' + str(pbar))
Example #3
Source File: log.py From OpenNIR with MIT License | 6 votes |
def pbar(self, it, *args, **kwargs): level = kwargs.pop('level', 'DEBUG') quiet = kwargs.pop('quiet', False) if 'ncols' not in kwargs: kwargs['ncols'] = 80 if 'desc' in kwargs: if not quiet: self.log(level, '[starting] {desc}'.format(**kwargs)) if 'leave' not in kwargs: kwargs['leave'] = False if 'total' not in kwargs and hasattr(it, '__len__'): kwargs['total'] = len(it) elif 'total' not in kwargs and hasattr(it, '__length_hint__'): kwargs['total'] = operator.length_hint(it) if 'smoothing' not in kwargs: kwargs['smoothing'] = 0. # disable smoothing by default; mean over entire life of pbar pbar = tqdm(it, *args, **kwargs) yield from pbar if not quiet: pbar.bar_format = '{desc}: [{elapsed}] [{n_fmt}it] [{rate_fmt}]' self.log(level, '[finished] ' + str(pbar))
Example #4
Source File: test_enumerate.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_len(self): for s in ('hello', tuple('hello'), list('hello'), range(5)): self.assertEqual(operator.length_hint(reversed(s)), len(s)) r = reversed(s) list(r) self.assertEqual(operator.length_hint(r), 0) class SeqWithWeirdLen: called = False def __len__(self): if not self.called: self.called = True return 10 raise ZeroDivisionError def __getitem__(self, index): return index r = reversed(SeqWithWeirdLen()) if sys.implementation.name == "ironpython": # this seems like an implementation detail? self.assertEqual(operator.length_hint(r), 10) else: self.assertRaises(ZeroDivisionError, operator.length_hint, r)
Example #5
Source File: test_enumerate.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_len(self): for s in ('hello', tuple('hello'), list('hello'), range(5)): self.assertEqual(operator.length_hint(reversed(s)), len(s)) r = reversed(s) list(r) self.assertEqual(operator.length_hint(r), 0) class SeqWithWeirdLen: called = False def __len__(self): if not self.called: self.called = True return 10 raise ZeroDivisionError def __getitem__(self, index): return index r = reversed(SeqWithWeirdLen()) self.assertRaises(ZeroDivisionError, operator.length_hint, r)
Example #6
Source File: test_enumerate.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_len(self): for s in ('hello', tuple('hello'), list('hello'), range(5)): self.assertEqual(operator.length_hint(reversed(s)), len(s)) r = reversed(s) list(r) self.assertEqual(operator.length_hint(r), 0) class SeqWithWeirdLen: called = False def __len__(self): if not self.called: self.called = True return 10 raise ZeroDivisionError def __getitem__(self, index): return index r = reversed(SeqWithWeirdLen()) self.assertRaises(ZeroDivisionError, operator.length_hint, r)
Example #7
Source File: test_struct.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_iter_unpack(self): import operator packed = struct.pack('hlhlhl', 1, 2, 3, 4, 5, 6) it = struct.iter_unpack('hl', packed) self.assertEqual(operator.length_hint(it), 3) self.assertEqual(it.__next__(), (1, 2)) self.assertEqual(operator.length_hint(it), 2) self.assertEqual(it.__next__(), (3, 4)) self.assertEqual(operator.length_hint(it), 1) self.assertEqual(it.__next__(), (5, 6)) self.assertEqual(operator.length_hint(it), 0) self.assertRaises(StopIteration, next, it) # struct.error: iterative unpacking requires a buffer of a multiple of {N} bytes self.assertRaises(struct.error, struct.iter_unpack, "h", b"\0")
Example #8
Source File: test_iterlen.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_mutation(self): d = list(range(n)) it = reversed(d) next(it) next(it) self.assertEqual(length_hint(it), n - 2) d.append(n) self.assertEqual(length_hint(it), n - 2) # ignore append d[1:] = [] self.assertEqual(length_hint(it), 0) self.assertEqual(list(it), []) # confirm invariant d.extend(range(20)) self.assertEqual(length_hint(it), 0) ## -- Check to make sure exceptions are not suppressed by __length_hint__()
Example #9
Source File: test_iterlen.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_mutation(self): d = list(range(n)) it = reversed(d) next(it) next(it) self.assertEqual(length_hint(it), n - 2) d.append(n) self.assertEqual(length_hint(it), n - 2) # ignore append d[1:] = [] self.assertEqual(length_hint(it), 0) self.assertEqual(list(it), []) # confirm invariant d.extend(range(20)) self.assertEqual(length_hint(it), 0) ## -- Check to make sure exceptions are not suppressed by __length_hint__()
Example #10
Source File: test_iterlen.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_mutation(self): d = list(range(n)) it = iter(d) next(it) next(it) self.assertEqual(length_hint(it), n - 2) d.append(n) self.assertEqual(length_hint(it), n - 1) # grow with append d[1:] = [] self.assertEqual(length_hint(it), 0) self.assertEqual(list(it), []) d.extend(range(20)) self.assertEqual(length_hint(it), 0)
Example #11
Source File: test_iterlen.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_invariant(self): it = self.it for i in reversed(range(1, n+1)): self.assertEqual(length_hint(it), i) next(it) self.assertEqual(length_hint(it), 0) self.assertRaises(StopIteration, next, it) self.assertEqual(length_hint(it), 0)
Example #12
Source File: test_itertools.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_repeat_with_negative_times(self): self.assertEqual(operator.length_hint(repeat(None, -1)), 0) self.assertEqual(operator.length_hint(repeat(None, -2)), 0) self.assertEqual(operator.length_hint(repeat(None, times=-1)), 0) self.assertEqual(operator.length_hint(repeat(None, times=-2)), 0)
Example #13
Source File: test_itertools.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_repeat(self): self.assertEqual(operator.length_hint(repeat(None, 50)), 50) self.assertEqual(operator.length_hint(repeat(None, 0)), 0) self.assertEqual(operator.length_hint(repeat(None), 12), 12)
Example #14
Source File: test_struct.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_length_hint(self): lh = operator.length_hint s = struct.Struct('>IB') b = bytes(range(1, 16)) it = s.iter_unpack(b) self.assertEqual(lh(it), 3) next(it) self.assertEqual(lh(it), 2) next(it) self.assertEqual(lh(it), 1) next(it) self.assertEqual(lh(it), 0) self.assertRaises(StopIteration, next, it) self.assertEqual(lh(it), 0)
Example #15
Source File: iter.py From sidekick with MIT License | 5 votes |
def __length_hint__(self, _hint=operator.length_hint): if self._size_hint is NotImplemented: return _hint(self._iterator, NotImplemented) return self._size_hint # # API #
Example #16
Source File: iter.py From sidekick with MIT License | 5 votes |
def __getitem__(self, item, _chain=itertools.chain): if isinstance(item, int): if item >= 0: head = [] for i, x in enumerate(self._iterator): head.append(x) if i == item: self._iterator = _chain(head, self._iterator) return x else: self._iterator = _iter(head) self._size_hint = len(head) raise IndexError(item) else: raise IndexError("negative indexes are not supported") elif isinstance(item, slice): a, b, c = item.start, item.step, item.stop return iter(itertools.islice(self._iterator, a, b, c)) elif callable(item): return iter(filter(item, self._iterator), self._size_hint) elif isinstance(item, list): if not item: return [] if isinstance(item[0], bool): self._iterator, data = itertools.tee(self._iterator, 2) return [x for key, x in zip(item, data) if key] elif isinstance(item[0], int): self._iterator, data = itertools.tee(self._iterator, 2) data = list(itertools.islice(data, max(item) + 1)) return [data[i] for i in item] else: raise TypeError("index must contain only integers or booleans") else: size = operator.length_hint(item, -1) size = None if size == -1 else size return iter(compress_or_select(item, self._iterator), size)
Example #17
Source File: test_iterlen.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_mutation(self): d = list(range(n)) it = iter(d) next(it) next(it) self.assertEqual(length_hint(it), n - 2) d.append(n) self.assertEqual(length_hint(it), n - 1) # grow with append d[1:] = [] self.assertEqual(length_hint(it), 0) self.assertEqual(list(it), []) d.extend(range(20)) self.assertEqual(length_hint(it), 0)
Example #18
Source File: test_iterlen.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_invariant(self): it = self.it for i in reversed(range(1, n+1)): self.assertEqual(length_hint(it), i) next(it) self.assertEqual(length_hint(it), 0) self.assertRaises(StopIteration, next, it) self.assertEqual(length_hint(it), 0)
Example #19
Source File: test_itertools.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_repeat_with_negative_times(self): self.assertEqual(operator.length_hint(repeat(None, -1)), 0) self.assertEqual(operator.length_hint(repeat(None, -2)), 0) self.assertEqual(operator.length_hint(repeat(None, times=-1)), 0) self.assertEqual(operator.length_hint(repeat(None, times=-2)), 0)
Example #20
Source File: test_itertools.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_repeat(self): self.assertEqual(operator.length_hint(repeat(None, 50)), 50) self.assertEqual(operator.length_hint(repeat(None, 0)), 0) self.assertEqual(operator.length_hint(repeat(None), 12), 12)
Example #21
Source File: test_struct.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_length_hint(self): lh = operator.length_hint s = struct.Struct('>IB') b = bytes(range(1, 16)) it = s.iter_unpack(b) self.assertEqual(lh(it), 3) next(it) self.assertEqual(lh(it), 2) next(it) self.assertEqual(lh(it), 1) next(it) self.assertEqual(lh(it), 0) self.assertRaises(StopIteration, next, it) self.assertEqual(lh(it), 0)
Example #22
Source File: test_iterlen.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_mutation(self): d = list(range(n)) it = reversed(d) next(it) next(it) self.assertEqual(length_hint(it), n - 2) d.append(n) self.assertEqual(length_hint(it), n - 2) # ignore append d[1:] = [] self.assertEqual(length_hint(it), 0) self.assertEqual(list(it), []) # confirm invariant d.extend(range(20)) self.assertEqual(length_hint(it), 0) ## -- Check to make sure exceptions are not suppressed by __length_hint__()
Example #23
Source File: test_iterlen.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_mutation(self): d = list(range(n)) it = iter(d) next(it) next(it) self.assertEqual(length_hint(it), n - 2) d.append(n) self.assertEqual(length_hint(it), n - 1) # grow with append d[1:] = [] self.assertEqual(length_hint(it), 0) self.assertEqual(list(it), []) d.extend(range(20)) self.assertEqual(length_hint(it), 0)
Example #24
Source File: test_iterlen.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_invariant(self): it = self.it for i in reversed(range(1, n+1)): self.assertEqual(length_hint(it), i) next(it) self.assertEqual(length_hint(it), 0) self.assertRaises(StopIteration, next, it) self.assertEqual(length_hint(it), 0)
Example #25
Source File: test_itertools.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_repeat_with_negative_times(self): self.assertEqual(operator.length_hint(repeat(None, -1)), 0) self.assertEqual(operator.length_hint(repeat(None, -2)), 0) self.assertEqual(operator.length_hint(repeat(None, times=-1)), 0) self.assertEqual(operator.length_hint(repeat(None, times=-2)), 0)
Example #26
Source File: test_itertools.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_repeat(self): self.assertEqual(operator.length_hint(repeat(None, 50)), 50) self.assertEqual(operator.length_hint(repeat(None, 0)), 0) self.assertEqual(operator.length_hint(repeat(None), 12), 12)
Example #27
Source File: test_struct.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_length_hint(self): lh = operator.length_hint s = struct.Struct('>IB') b = bytes(range(1, 16)) it = s.iter_unpack(b) self.assertEqual(lh(it), 3) next(it) self.assertEqual(lh(it), 2) next(it) self.assertEqual(lh(it), 1) next(it) self.assertEqual(lh(it), 0) self.assertRaises(StopIteration, next, it) self.assertEqual(lh(it), 0)