當前位置: 首頁>>代碼示例>>Python>>正文


Python operator.length_hint方法代碼示例

本文整理匯總了Python中operator.length_hint方法的典型用法代碼示例。如果您正苦於以下問題:Python operator.length_hint方法的具體用法?Python operator.length_hint怎麽用?Python operator.length_hint使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在operator的用法示例。


在下文中一共展示了operator.length_hint方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_len

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:19,代碼來源:test_enumerate.py

示例2: test_len

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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) 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:22,代碼來源:test_enumerate.py

示例3: test_iter_unpack

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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") 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:19,代碼來源:test_struct.py

示例4: pbar

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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)) 
開發者ID:Georgetown-IR-Lab,項目名稱:OpenNIR,代碼行數:23,代碼來源:log.py

示例5: pbar_raw

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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)) 
開發者ID:Georgetown-IR-Lab,項目名稱:OpenNIR,代碼行數:27,代碼來源:log.py

示例6: test_length_hint

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:16,代碼來源:test_struct.py

示例7: test_repeat

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:6,代碼來源:test_itertools.py

示例8: test_repeat_with_negative_times

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:7,代碼來源:test_itertools.py

示例9: test_invariant

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:10,代碼來源:test_iterlen.py

示例10: test_mutation

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:15,代碼來源:test_iterlen.py

示例11: __getitem__

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
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) 
開發者ID:fabiommendes,項目名稱:sidekick,代碼行數:42,代碼來源:iter.py

示例12: __length_hint__

# 需要導入模塊: import operator [as 別名]
# 或者: from operator import length_hint [as 別名]
def __length_hint__(self, _hint=operator.length_hint):
        if self._size_hint is NotImplemented:
            return _hint(self._iterator, NotImplemented)
        return self._size_hint

    #
    # API
    # 
開發者ID:fabiommendes,項目名稱:sidekick,代碼行數:10,代碼來源:iter.py


注:本文中的operator.length_hint方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。