예제 #1
0
    def test_LengthHint(self):
        import operator

        class WithLen:
            def __len__(self):
                return 1

            def __length_hint__(self):
                return 42

        class NoLen:
            def __length_hint__(self):
                return 2

        module = self.import_extension('test_LengthHint',
                                       [('length_hint', 'METH_VARARGS', """
                 PyObject *obj = PyTuple_GET_ITEM(args, 0);
                 Py_ssize_t i = PyLong_AsSsize_t(PyTuple_GET_ITEM(args, 1));
                 return PyLong_FromSsize_t(PyObject_LengthHint(obj, i));
             """)])
        assert module.length_hint(WithLen(), 5) == operator.length_hint(
            WithLen(), 5) == 1
        assert module.length_hint(NoLen(), 5) == operator.length_hint(
            NoLen(), 5) == 2
        assert module.length_hint(object(), 5) == operator.length_hint(
            object(), 5) == 5
예제 #2
0
def check_lengthhint_iteration(iterator, expected_start_lengthhint):
    for length in range(expected_start_lengthhint, 0, -1):
        assert operator.length_hint(iterator) == length
        next(iterator)
    assert operator.length_hint(iterator) == 0
    with pytest.raises(StopIteration):
        next(iterator)
예제 #3
0
파일: test_base.py 프로젝트: ybibaev/chiter
def test_add_right_with_length_hint():
    one = range(5, 10)
    two = ChIter(range(5))
    i = one + two
    length = length_hint(one) + length_hint(two)

    assert length_hint(i) == length
예제 #4
0
    def test_length_hint_empty(self):

        looter = instaLooter.InstaLooter(profile="jkshksjdhfjkhdkfhk")
        self.assertEqual(operator.length_hint(looter), 0)

        looter = instaLooter.InstaLooter(hashtag="jkshksjdhfjkhdkfhk")
        self.assertEqual(operator.length_hint(looter), 0)
예제 #5
0
 def test_immutable_during_iteration(self):
     it = self.it
     self.assertEqual(length_hint(it), n)
     next(it)
     self.assertEqual(length_hint(it), n - 1)
     self.mutate()
     self.assertRaises(RuntimeError, next, it)
     self.assertEqual(length_hint(it), 0)
def test_accumulate_lengthhint_failure1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    acc = accumulate(f_it)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP, match=_hf.FailLengthHint.EXC_MSG):
        operator.length_hint(acc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP, match=_hf.FailLengthHint.EXC_MSG):
        list(acc)
예제 #7
0
def test_grouper_lengthhint_failure2():
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    it = grouper(of_it, 2)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #8
0
def _cdr_Iterator(z):
    if length_hint(z, 1) == 0:
        raise ConsError("Not a cons pair")

    if isinstance(z, Iterator) and length_hint(z, 2) <= 1:
        return iter([])

    return islice(z, 1, None)
예제 #9
0
 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)
예제 #10
0
 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)
예제 #11
0
def test_accumulate_lengthhint_failure2():
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    acc = accumulate(of_it)
    with pytest.raises(OverflowError):
        operator.length_hint(acc)

    with pytest.raises(OverflowError):
        list(acc)
예제 #12
0
def test_sideeffects_failure_lengthhint2():
    # This only checks for overflow if the length_hint is above PY_SSIZE_T_MAX
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    it = sideeffects(of_it, return_None)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
def test_roundrobin_failure_lengthhint2():
    # This only checks for overflow if the length_hint is above PY_SSIZE_T_MAX
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    it = roundrobin(of_it)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #14
0
def test_successive_lengthhint1():
    it = successive([0] * 6, 4)
    assert operator.length_hint(it) == 3
    next(it)
    assert operator.length_hint(it) == 2
    next(it)
    assert operator.length_hint(it) == 1
    next(it)
    assert operator.length_hint(it) == 0
예제 #15
0
def test_sideeffects_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = sideeffects(f_it, return_None)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP,
                       match=_hf.FailLengthHint.EXC_MSG):
        operator.length_hint(it)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP,
                       match=_hf.FailLengthHint.EXC_MSG):
        list(it)
예제 #16
0
def test_replicate_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = replicate(f_it, 3)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        operator.length_hint(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        list(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)
예제 #17
0
def test_grouper_lengthhint_failure1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = grouper(f_it, 2)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        operator.length_hint(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        list(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)
예제 #18
0
def test_merge_lengthhint_failure5():
    # Like the test above but this time with a "started" merge
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize)
    it = merge(toT([1, 2, 3]), of_it)
    next(it)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #19
0
def test_sideeffects_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = sideeffects(f_it, return_None)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        operator.length_hint(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        list(it)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)
def test_roundrobin_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = roundrobin(f_it)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP,
                       match=_hf.FailLengthHint.EXC_MSG):
        operator.length_hint(it)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP,
                       match=_hf.FailLengthHint.EXC_MSG):
        list(it)
예제 #21
0
def test_intersperse_lengthhint_failure1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = intersperse(f_it, 2)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP,
                       match=_hf.FailLengthHint.EXC_MSG):
        operator.length_hint(it)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP,
                       match=_hf.FailLengthHint.EXC_MSG):
        list(it)
예제 #22
0
def test_intersperse_lengthhint_failure2():
    # This is the easy way to overflow the length_hint: If the iterable itself
    # has a length_hint > sys.maxsize
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    it = intersperse(of_it, 2)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #23
0
def test_accumulate_lengthhint_failure1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    acc = accumulate(f_it)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        operator.length_hint(acc)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP) as exc:
        list(acc)
    assert _hf.FailLengthHint.EXC_MSG in str(exc)
def test_successive_failure_lengthhint1():
    f_it = _hf.FailLengthHint(toT([1, 2, 3]))
    it = successive(f_it)
    with pytest.raises(_hf.FailLengthHint.EXC_TYP,
                       match=_hf.FailLengthHint.EXC_MSG):
        operator.length_hint(it)

    with pytest.raises(_hf.FailLengthHint.EXC_TYP,
                       match=_hf.FailLengthHint.EXC_MSG):
        list(it)
예제 #25
0
def test_successive_failure_lengthhint2():
    # This only checks for overflow if the length_hint is above PY_SSIZE_T_MAX.
    # In theory that would be possible because with times the length would be
    # shorter but "length_hint" throws the exception so we propagate it.
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1)
    it = successive(of_it)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #26
0
def test_intersperse_lengthhint_failure3():
    # The length_hint method multiplies the length_hint of the iterable with
    # 2 (and adds/subtracts 1) so it's actually possible to have overflow even
    # if the length of the iterable doesn't trigger the overflow!
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize)
    it = intersperse(of_it, 2)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
def test_roundrobin_failure_lengthhint3():
    # Check if by adding the different lengths it could lead to overflow.
    # We use two iterables both with sys.maxsize length.
    it1 = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize)
    it2 = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize)
    it = roundrobin(it1, it2)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #28
0
def test_replicate_failure_lengthhint3():
    # It is also possible that the length_hint overflows when the length is
    # below maxsize but "times * length" is above maxsize.
    # In this case length = maxsize / 2 but times = 3
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize // 2)
    it = replicate(of_it, 3)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #29
0
def test_merge_lengthhint_failure4():
    # Overflow could also happen when adding length_hints that individually are
    # below the sys.maxsize
    # In this case we have 3 + sys.maxsize
    of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize)
    it = merge(toT([1, 2, 3]), of_it)
    with pytest.raises(OverflowError):
        operator.length_hint(it)

    with pytest.raises(OverflowError):
        list(it)
예제 #30
0
def test_accumulate_lengthhint1():
    it = accumulate([1, 2, 3, 4])
    assert operator.length_hint(it) == 4
    next(it)
    assert operator.length_hint(it) == 3
    next(it)
    assert operator.length_hint(it) == 2
    next(it)
    assert operator.length_hint(it) == 1
    next(it)
    assert operator.length_hint(it) == 0
예제 #31
0
    def test_immutable_during_iteration(self):
        # objects such as deques, sets, and dictionaries enforce
        # length immutability  during iteration

        it = self.it
        self.assertEqual(length_hint(it), n)
        next(it)
        self.assertEqual(length_hint(it), n-1)
        self.mutate()
        self.assertRaises(RuntimeError, next, it)
        self.assertEqual(length_hint(it), 0)
예제 #32
0
    def test_immutable_during_iteration(self):
        # objects such as deques, sets, and dictionaries enforce
        # length immutability  during iteration

        it = self.it
        self.assertEqual(length_hint(it), n)
        next(it)
        self.assertEqual(length_hint(it), n-1)
        self.mutate()
        self.assertRaises(RuntimeError, next, it)
        self.assertEqual(length_hint(it), 0)
예제 #33
0
 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)
예제 #34
0
 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)
예제 #35
0
파일: cons.py 프로젝트: logpy/logpy
def is_null(a):
    """Check if an object is a "Lisp-like" null.

    A "Lisp-like" null object is one that can be used as a `cdr` to produce a
    non-`ConsPair` collection (e.g. `None`, `[]`, `()`, `OrderedDict`, etc.)

    It's important that this function be used when considering an arbitrary
    object as the terminating `cdr` for a given collection (e.g. when unifying
    `cons` objects); otherwise, fixed choices for the terminating `cdr`, such
    as `None` or `[]`, will severely limit the applicability of the
    decomposition.

    Also, for relevant collections with no concrete length information, `None`
    is returned, and it signifies the uncertainty of the negative assertion.
    """
    if a is None:
        return True
    elif any(isinstance(a, d)
             for d, in cdr.funcs.keys()
             if not issubclass(d, (ConsPair, type(None)))):
        lhint = length_hint(a, -1)
        if lhint == 0:
            return True
        elif lhint > 0:
            return False
        else:
            return None
    else:
        return False
예제 #36
0
 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)
예제 #37
0
def find_sub_in_series_greedy(series = [1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 6, 8], search_series = [6, 7]):
  search_series_iter = iter(search_series) # Calling __iter__() on an iterator returns self, but calling the same on a collections.abc.Sequence tends to return a new collections.abc.Iterator.  We take advantage of that.
  cur_ind = int()
  matches = list()
  for match_max in range(len(series) - (len(series) % operator.length_hint(search_series))):
    try:
      if(cur_ind > (len(series) - operator.length_hint(search_series_iter))):
        break
      if(operator.length_hint(search_series_iter) > 0):
        if(operator.length_hint(search_series_iter) == len(search_series)):
          cur_ind = series.index(next(search_series_iter), cur_ind)
        elif(next(search_series_iter) == series[cur_ind + 1]): cur_ind += 1
        else:
          search_series_iter = iter(search_series)
      else:
        cur_ind += 1
        matches.append(cur_ind - len(search_series))
        search_series_iter = iter(search_series)
    except ValueError:
      cur_ind = len(search_series) - 1 # If collections.Sequence.index() fails to find an identity, it will always fail to find an identity.  Get out of this mess before we end up trying to find the same value in our (sub)set of remaining search space.
  return matches
예제 #38
0
파일: cons.py 프로젝트: logpy/logpy
def is_cons(a):
    """Determine if an object is the result of a `cons`.

    This is automatically determined by the accepted `cdr` types for each
    `cons_merge` implementation, since any such implementation implies that
    `cons` can construct that type.
    """
    return (issubclass(type(a), ConsPair) or
            (any(isinstance(a, d)
                 for _, d in cons_merge.funcs.keys()
                 if d not in (object, ConsPair)) and
             length_hint(a, 0) > 0))
예제 #39
0
def has_repeating_members(coll, myident = 3, num = 2):
  try:
    startindex = coll.index(myident) + 1
    if(startindex):
      subsearch = iter(coll[startindex:])
      if(operator.length_hint(subsearch) >= num - 1):
        for ign_count in range(num - 1):
          if(next(subsearch) != myident):
            break
        else:
          return True
    else: return False
  except ValueError:
    startindex = None
  return False
예제 #40
0
    def test_length_hint(self):
        class X(object):
            def __init__(self, value):
                self.value = value

            def __length_hint__(self):
                if type(self.value) is type:
                    raise self.value
                else:
                    return self.value

        self.assertEqual(operator.length_hint([], 2), 0)
        self.assertEqual(operator.length_hint(iter([1, 2, 3])), 3)

        self.assertEqual(operator.length_hint(X(2)), 2)
        self.assertEqual(operator.length_hint(X(NotImplemented), 4), 4)
        self.assertEqual(operator.length_hint(X(TypeError), 12), 12)
        with self.assertRaises(TypeError):
            operator.length_hint(X("abc"))
        with self.assertRaises(ValueError):
            operator.length_hint(X(-2))
        with self.assertRaises(LookupError):
            operator.length_hint(X(LookupError))
예제 #41
0
파일: monoid.py 프로젝트: bashwork/common
    'lift': str,
    'plus': operator.add,
})

ListMonoid = Monoid(**{
    'zero': [],
    'lift': lambda x: [x],
    'plus': operator.add,
})

IterMonoid = Monoid(**{
    'zero': iter([]),
    'lift': lambda x: iter([x]),
    'plus': itertools.chain,
})
IterMonoid.is_zero = lambda x: length_hint(x) == 0

SetMonoid = Monoid(**{
    'zero': set(),
    'lift': lambda x: {x},
    'plus': lambda l,r: l.union(r),
})

TupleMonoid = Monoid(**{
    'zero': tuple(),
    'lift': lambda x: (x,),
    'plus': operator.add,
})

FunctionMonoid = Monoid(**{
    'zero': combinator.identity,