当前位置: 首页>>代码示例>>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;未经允许,请勿转载。