Esempio n. 1
0
def make_sorted_distinct_sequence(iterable, sense=SortSense.ascending):
    """Create a sorted immutable sequence from an iterable series.

    The resulting collected will be sorted ascending.

    Args:
        iterable: An iterable series of comparable values.

        sense: If None, the any original sense of the data is preserved,
            so ascending data remains ascending, and descending data
            remains descending. If the original data was unsorted,
            the result will be ascending. Force a particular sense
            by specifying SortSense.ascending or SortSense.descending.

    Returns:
        An immutable collection which supports the Sized, Iterable,
        Container and Sequence protocols.
    """
    if isinstance(iterable, range):
        if sense is None:
            return iterable
        elif sense == SortSense.ascending:
            if iterable.step > 0:
                return iterable
            else:
                return reversed_range(iterable)
        elif sense == SortSense.descending:
            if iterable.step < 0:
                return iterable
            else:
                return reversed_range(iterable)
        else:
            raise TypeError(
                "sense {} is neither a SortSense nor None".format(sense))

    if sense == SortSense.ascending:
        sorted_seq = SortedFrozenSet(iterable)
    elif sense == SortSense.descending:
        sorted_seq = ReversedSequenceView(SortedFrozenSet(iterable))
    elif sense is None:
        items = list(iterable)
        if is_sorted(items, reverse=True, distinct=True):
            sorted_seq = ReversedSequenceView(SortedFrozenSet(iterable))
        else:
            sorted_seq = SortedFrozenSet(iterable)
    else:
        raise TypeError(
            "sense {} is neither a SortSense nor None".format(sense))

    return compress_sorted_sequence_to_range(sorted_seq)
Esempio n. 2
0
 def test_reversed(self):
     s = SortedFrozenSet([1, 3, 5, 7])
     r = reversed(s)
     assert next(r) == 7
     assert next(r) == 5
     assert next(r) == 3
     assert next(r) == 1
     with pytest.raises(StopIteration):
         next(r)
Esempio n. 3
0
    def test_from_iterable(self):
        def gen6842():
            yield 6
            yield 8
            yield 4
            yield 2

        g = gen6842()
        s = SortedFrozenSet(g)
Esempio n. 4
0
    def __init__(self, keys, value):
        """Initialize a ConstantCatalog.

        The catalog is initialized by a description with an iterable series of
        keys and a constant value to be associated with all the keys.

        Args:
            keys: An iterable series of keys.
            value: A value associated with all keys.
        """
        self._keys = SortedFrozenSet(keys)
        self._value = value
Esempio n. 5
0
 def s(self):
     return SortedFrozenSet([7, 2, 1, 1, 9])
Esempio n. 6
0
 def test_one(self):
     s = SortedFrozenSet([42])
     assert len(s) == 1
Esempio n. 7
0
 def test_with_duplicates(self):
     s = SortedFrozenSet([5, 5, 5])
     assert len(s) == 1
Esempio n. 8
0
 def test_issuperset_negative(self):
     s = SortedFrozenSet({1, 2})
     t = [1, 2, 3]
     assert not s.issuperset(t)
Esempio n. 9
0
 def test_difference(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [2, 3, 4]
     assert s.difference(t) == SortedFrozenSet({1})
Esempio n. 10
0
 def s(self):
     return SortedFrozenSet([6, 7, 3, 9])
Esempio n. 11
0
 def test_isdisjoint_negative(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [3, 4, 5]
     assert not s.isdisjoint(t)
Esempio n. 12
0
 def test_difference(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [2, 3, 4]
     assert s.difference(t) == SortedFrozenSet({1})
Esempio n. 13
0
 def test_count_one(self):
     s = SortedFrozenSet([1, 5, 7, 9])
     assert s.count(7) == 1
Esempio n. 14
0
 def test_union(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [2, 3, 4]
     assert s.union(t) == SortedFrozenSet({1, 2, 3, 4})
Esempio n. 15
0
 def test_symmetric_difference(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [2, 3, 4]
     assert s.symmetric_difference(t) == SortedFrozenSet({1, 4})
Esempio n. 16
0
 def test_intersection(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [2, 3, 4]
     assert s.intersection(t) == SortedFrozenSet({2, 3})
Esempio n. 17
0
 def test_isdisjoint_negative(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [3, 4, 5]
     assert not s.isdisjoint(t)
Esempio n. 18
0
 def test_isdisjoint_positive(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [4, 5, 6]
     assert s.isdisjoint(t)
Esempio n. 19
0
 def test_count_zero(self):
     s = SortedFrozenSet([1, 5, 7, 9])
     assert s.count(11) == 0
Esempio n. 20
0
 def test_union(self):
     s = SortedFrozenSet({1, 2, 3})
     t = SortedFrozenSet({2, 3, 4})
     assert s | t == SortedFrozenSet({1, 2, 3, 4})
Esempio n. 21
0
 def test_index_negative(self):
     s = SortedFrozenSet([1, 5, 8, 9])
     with pytest.raises(ValueError):
         s.index(15)
Esempio n. 22
0
 def test_difference(self):
     s = SortedFrozenSet({1, 2, 3})
     t = SortedFrozenSet({2, 3, 4})
     assert s - t == SortedFrozenSet({1})
Esempio n. 23
0
 def test_isdisjoint_positive(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [4, 5, 6]
     assert s.isdisjoint(t)
Esempio n. 24
0
 def test_union(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [2, 3, 4]
     assert s.union(t) == SortedFrozenSet({1, 2, 3, 4})
Esempio n. 25
0
 def test_intersection(self):
     s = SortedFrozenSet({1, 2, 3})
     t = SortedFrozenSet({2, 3, 4})
     assert s & t == SortedFrozenSet({2, 3})
Esempio n. 26
0
 def test_issuperset_positive(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [1, 2, 3]
     assert s.issuperset(t)
Esempio n. 27
0
 def test_symmetric_difference(self):
     s = SortedFrozenSet({1, 2, 3})
     t = SortedFrozenSet({2, 3, 4})
     assert s ^ t == SortedFrozenSet({1, 4})
Esempio n. 28
0
 def test_type_mismatch(self):
     assert SortedFrozenSet([1, 2, 3]) != [1, 2, 3]
Esempio n. 29
0
 def test_intersection(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [2, 3, 4]
     assert s.intersection(t) == SortedFrozenSet({2, 3})
Esempio n. 30
0
 def test_identical(self):
     s = SortedFrozenSet([10, 11, 12])
     assert not s != s
Esempio n. 31
0
 def test_symmetric_difference(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [2, 3, 4]
     assert s.symmetric_difference(t) == SortedFrozenSet({1, 4})
Esempio n. 32
0
 def test_lt_negative(self):
     s = SortedFrozenSet({1, 2, 3})
     t = SortedFrozenSet({1, 2, 3})
     assert not s < t
Esempio n. 33
0
 def test_empty(self):
     s = SortedFrozenSet()
     assert len(s) == 0
Esempio n. 34
0
 def test_le_lt_positive(self):
     s = SortedFrozenSet({1, 2})
     t = SortedFrozenSet({1, 2, 3})
     assert s <= t
Esempio n. 35
0
 def test_ten(self):
     s = SortedFrozenSet(range(10))
     assert len(s) == 10
Esempio n. 36
0
 def test_ge_eq_positive(self):
     s = SortedFrozenSet({1, 2, 3})
     t = SortedFrozenSet({1, 2, 3})
     assert s >= t
Esempio n. 37
0
 def test_empty(self):
     s = SortedFrozenSet()
Esempio n. 38
0
 def test_ge_negative(self):
     s = SortedFrozenSet({1, 2})
     t = SortedFrozenSet({1, 2, 3})
     assert not s >= t
Esempio n. 39
0
 def s(self):
     return SortedFrozenSet([1, 4, 9, 13, 15])
Esempio n. 40
0
 def test_issuperset_positive(self):
     s = SortedFrozenSet({1, 2, 3})
     t = [1, 2, 3]
     assert s.issuperset(t)
Esempio n. 41
0
 def test_issuperset_negative(self):
     s = SortedFrozenSet({1, 2})
     t = [1, 2, 3]
     assert not s.issuperset(t)
Esempio n. 42
0
 def test_index_positive(self):
     s = SortedFrozenSet([1, 5, 8, 9])
     assert s.index(8) == 2