| | |
- BaseIntervalSet
-
- FrozenIntervalSet
- IntervalSet
- Interval
- Largest
- Smallest
class BaseIntervalSet |
| |
BaseIntervalSet is the base class for IntervalSet and
FrozenIntervalSet. |
| |
Methods defined here:
- __add__(self, other)
- This function returns the union of two IntervalSets. It does the
same thing as the __or__ function. A set or a single value can
also be added.
>>> empty = IntervalSet()
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print evens + positives
-8,-6,-4,-2,[0,~)
>>> print negatives + zero
(-~,0]
>>> print empty + negatives
(-~,0)
>>> print empty + naturals
[0,~)
>>> print nonzero + evens
(-~,~)
>>> print evens + set((1,3))
-8,-6,-4,-2,0,1,2,3,4,6,8
>>> nonzero + 0 == IntervalSet.all()
True
- __and__(self, other)
- This function returns the intersection of self and other.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print naturals and naturals
[0,~)
>>> print evens & zero
0
>>> print negatives & zero
<Empty>
>>> print nonzero & positives
(0,~)
>>> print empty & zero
<Empty>
>>> print evens & set((4, -2, 0, 3))
-2,0,4
>>> print evens & 5
<Empty>
>>> print evens & 2
2
- __contains__(self, obj)
- Returns True if obj is a subset of self.
>>> some = IntervalSet(
... 2, 8, Interval(12, True, 17, False), Interval.greaterThan(17))
>>> all = IntervalSet.all()
>>> empty = IntervalSet.empty()
>>> 17 in empty
False
>>> 17 in all
True
>>> 17 in some
False
>>> r = Interval(100, True, 400, False)
>>> r in empty
False
>>> r in all
True
>>> r in some
True
>>> empty in all
False
>>> empty in some
False
>>> all in empty
False
>>> all in some
False
>>> some in empty
False
>>> some in all
True
- __eq__(self, other)
- Two IntervalSets are identical if they contain the exact same
sets. Note that an empty set is never equal to any other set,
even an empty one.
>>> IntervalSet(4) == IntervalSet(1)
False
>>> IntervalSet(5) == IntervalSet(5)
True
>>> s1 = IntervalSet.between(4, 7)
>>> s2 = IntervalSet(Interval(4, True, 7, False))
>>> s1 == s2
False
>>> s2.add(7)
>>> s1 == s2
True
- __gt__(self, other)
- To test if a set is a superset of another, you can use the >
operator. I don't like this, personally, but in my attempt
to implement a set-like object, I've duplicated this
functionality.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero > positives
False
>>> zero > naturals
False
>>> positives > zero
False
>>> r > zero
False
>>> r > positives
False
>>> positives > r
True
>>> negatives > IntervalSet.all()
False
>>> r2 > negatives
False
>>> negatives > positives
False
- __init__(self, *args)
- This function initializes an IntervalSet. There can be an
arbitrary number of arguments to the initializer.
If no parameters are provided, then an empty IntervalSet is
constructed.
>>> print IntervalSet() # An empty set
<Empty>
Interval objects arguments are added directly to the IntervalSet.
>>> print IntervalSet(Interval(4, False, 6, True))
(4,6]
>>> print IntervalSet(Interval.lessThan(2, True))
(-~,2]
Other IntervalSet objects are expanded and added to the
IntervalSet, as though they were or'ed together.
>>> s1 = IntervalSet.between(3, 29)
>>> print IntervalSet(6, 2, s1)
2,[3,29]
Each value of a set object is added as a discrete value.
>>> print IntervalSet(set([3, 7, 2, 1]))
1,2,3,7
All other arguments are added as discrete values.
>>> print IntervalSet("Bob", "Fred", "Mary")
'Bob','Fred','Mary'
- __invert__(self)
- This function returns the disjoint set of self. In other words,
all values self doesn't include are in the returned set.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> print ~(IntervalSet.empty())
(-~,~)
>>> ~negatives == naturals
True
>>> print ~positives
(-~,0]
>>> ~naturals == negatives
True
>>> print ~evens
(-~,-8),(-8,-6),(-6,-4),(-4,-2),(-2,0),(0,2),(2,4),(4,6),(6,8),(8,~)
>>> ~zero == nonzero
True
>>> ~nonzero == zero
True
- __iter__(self)
- Returns an iterator over the intervals in the set.
>>> s = IntervalSet(
... 2, 7, set([2, 87, 4, 3]), Interval.greaterThan(12),
... Interval.lessThan(-2))
>>> l = set()
>>> for i in s:
... l.add(str(i))
...
>>> print len(l)
6
>>> "2" in l
True
>>> "7" in l
True
>>> "87" in l
False
>>> "4" in l
True
>>> "3" in l
True
>>> "(12,~)" in l
True
>>> "(-~,-2)" in l
True
- __lt__(self, other)
- To test if a set is a subset of another, you can use the <
operator. I don't like this, personally, but in my attempt
to implement a set-like object, I've duplicated this
functionality.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero < positives
False
>>> zero < naturals
True
>>> positives < zero
False
>>> r < zero
False
>>> r < positives
True
>>> positives < r
False
>>> negatives < IntervalSet.all()
True
>>> r2 < negatives
True
>>> negatives < positives
False
- __nonzero__(self)
- An empty IntervalSet is the zero-like value.
>>> nonempty = IntervalSet(3)
>>> if IntervalSet.empty():
... print "Non-empty"
>>> if nonempty:
... print "Non-empty"
Non-empty
- __or__(self, other)
- This function returns the union of two IntervalSets. It does the same
thing as the __add__ function. It can also accept sets or single
values.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens | positives
-8,-6,-4,-2,[0,~)
>>> print negatives | zero
(-~,0]
>>> print empty | negatives
(-~,0)
>>> print empty | naturals
[0,~)
>>> print nonzero | evens
(-~,~)
>>> print evens | set((-3, -5))
-8,-6,-5,-4,-3,-2,0,2,4,6,8
>>> print nonzero | 0 == all
True
- __str__(self)
- This function shows a string representation of an IntervalSet.
The string is shown sorted, with all intervals normalized.
>>> print IntervalSet()
<Empty>
>>> print IntervalSet(62)
62
>>> print IntervalSet(62, 56)
56,62
>>> print IntervalSet(23, Interval(26, True, 32, False))
23,[26,32)
>>> print IntervalSet.lessThan(3) + IntervalSet.greaterThan(3)
(-~,3),(3,~)
>>> print IntervalSet(Interval.lessThan(6, True))
(-~,6]
- __sub__(self, other)
- Returns all values of self minus all matching values in other.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens - nonzero
0
>>> print empty - naturals
<Empty>
>>> print zero - naturals
<Empty>
>>> print positives - zero
(0,~)
>>> print naturals - negatives
[0,~)
>>> print all - zero
(-~,0),(0,~)
>>> all - zero == nonzero
True
>>> print evens - set((2, 4, 6, 8))
-8,-6,-4,-2,0
>>> print evens - 8
-8,-6,-4,-2,0,2,4,6
>>> print IntervalSet.lessThan(40) - 40
(-~,40)
- __xor__(self, other)
- This function returns the exclusive or of two IntervalSets.
Regular sets or single values can also be xor'ed.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print nonzero ^ naturals
(-~,0]
>>> print zero ^ negatives
(-~,0]
>>> print positives ^ empty
(0,~)
>>> print evens ^ zero
-8,-6,-4,-2,2,4,6,8
>>> print evens ^ set([2, 4, 6, 8, 10])
-8,-6,-4,-2,0,10
>>> print zero ^ 0
<Empty>
>>> print zero ^ 42
0,42
- copy(self)
- Returns a copy of the object.
>>> s = IntervalSet(7, 2, 3, set([2, 6, 2]), Interval.greaterThan(3))
>>> s2 = s.copy()
>>> s == s2
True
>>> s = FrozenIntervalSet(
... 7, 2, 3, set([2, 6, 2]), Interval.greaterThan(3))
>>> s2 = s.copy()
>>> s == s2
True
- difference(self, other)
- Returns all values of self minus all matching values in other. It
is identical to the - operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens.difference(nonzero)
0
>>> print empty.difference(naturals)
<Empty>
>>> print zero.difference(naturals)
<Empty>
>>> print positives.difference(zero)
(0,~)
>>> print naturals.difference(negatives)
[0,~)
>>> print all.difference(zero)
(-~,0),(0,~)
>>> all.difference(zero) == nonzero
True
>>> print evens.difference(set([2, 4, 6, 8]))
-8,-6,-4,-2,0
>>> print evens.difference(8)
-8,-6,-4,-2,0,2,4,6
>>> print IntervalSet.lessThan(40).difference(40)
(-~,40)
- intersection(self, other)
- This function returns the intersection of self and other. It is
identical to the & operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print naturals.intersection(naturals)
[0,~)
>>> print evens.intersection(zero)
0
>>> print negatives.intersection(zero)
<Empty>
>>> print nonzero.intersection(positives)
(0,~)
>>> print empty.intersection(zero)
<Empty>
>>> print evens.intersection(set([4, -2, 0, 3]))
-2,0,4
>>> print evens.intersection(5)
<Empty>
>>> print evens.intersection(2)
2
- issubset(self, obj)
- Returns true if self is a subset of obj.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero.issubset(positives)
False
>>> zero.issubset(naturals)
True
>>> positives.issubset(zero)
False
>>> r.issubset(zero)
False
>>> r.issubset(positives)
True
>>> positives.issubset(r)
False
>>> negatives.issubset(IntervalSet.all())
True
>>> r2.issubset(negatives)
True
>>> negatives.issubset(positives)
False
- issuperset(self, obj)
- Returns true if self is a superset of obj.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero.issuperset(positives)
False
>>> zero.issuperset(naturals)
False
>>> positives.issuperset(zero)
False
>>> r.issuperset(zero)
False
>>> r.issuperset(positives)
False
>>> positives.issuperset(r)
True
>>> negatives.issuperset(IntervalSet.all())
False
>>> r2.issuperset(negatives)
False
>>> negatives.issuperset(positives)
False
- symmetric_difference(self, other)
- This function returns the exclusive or of two IntervalSets.
Regular sets or single values can also be xor'ed. It is
identical to the ^ operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print nonzero.symmetric_difference(naturals)
(-~,0]
>>> print zero.symmetric_difference(negatives)
(-~,0]
>>> print positives.symmetric_difference(empty)
(0,~)
>>> print evens.symmetric_difference(zero)
-8,-6,-4,-2,2,4,6,8
>>> print evens.symmetric_difference(set([2, 4, 6, 8, 10]))
-8,-6,-4,-2,0,10
>>> print zero.symmetric_difference(0)
<Empty>
>>> print zero.symmetric_difference(42)
0,42
- union(self, other)
- This function returns the union of two BaseIntervalSets. It is
identical to the | operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens.union(positives)
-8,-6,-4,-2,[0,~)
>>> print negatives.union(zero)
(-~,0]
>>> print empty.union(negatives)
(-~,0)
>>> print empty.union(naturals)
[0,~)
>>> print nonzero.union(evens)
(-~,~)
>>> print evens.union(set([-3, -5]))
-8,-6,-5,-4,-3,-2,0,2,4,6,8
>>> print nonzero.union(0) == all
True
Class methods defined here:
- all(cls) from __builtin__.classobj
- Returns an interval set containing all values.
>>> print IntervalSet.all()
(-~,~)
- between(cls, a, b, closed=True) from __builtin__.classobj
- Returns an IntervalSet of all values between a and b. If closed is
True, then the endpoints are included; otherwise, they aren't.
>>> print IntervalSet.between(0, 100)
[0,100]
>>> print IntervalSet.between(-1, 1)
[-1,1]
- empty(cls) from __builtin__.classobj
- Returns an interval set containing no values.
>>> print IntervalSet.empty()
<Empty>
- greaterThan(cls, n, closed=False) from __builtin__.classobj
- Returns an IntervalSet of all values greater than or equal to n. If
closed is True, then an IntervalSet of all values greater than or
equal to n is returned.
>>> print IntervalSet.greaterThan(0)
(0,~)
>>> print IntervalSet.greaterThan(0, True)
[0,~)
>>> print IntervalSet.greaterThan(-23)
(-23,~)
>>> print IntervalSet.greaterThan(-23, True)
[-23,~)
- lessThan(cls, n, closed=False) from __builtin__.classobj
- Returns an IntervalSet of all values less than n. If closed is
True, then an IntervalSet of all values less than or equal to n is
returned.
>>> print IntervalSet.lessThan(0)
(-~,0)
>>> print IntervalSet.lessThan(0, True)
(-~,0]
>>> print IntervalSet.lessThan(-23)
(-~,-23)
>>> print IntervalSet.lessThan(-23, True)
(-~,-23]
- notEqualTo(cls, n) from __builtin__.classobj
- Returns an IntervalSet of all values not equal to n.
>>> print IntervalSet.notEqualTo(0)
(-~,0),(0,~)
>>> print IntervalSet.notEqualTo(-23)
(-~,-23),(-23,~)
|
class FrozenIntervalSet(BaseIntervalSet) |
| |
FrozenIntervalSet is like IntervalSet, only add and remove are not
implemented, and hashes can be generated.
>>> fs = FrozenIntervalSet(3, 6, 2, 4)
>>> fs.add(12)
Traceback (most recent call last):
...
AttributeError: FrozenIntervalSet instance has no attribute 'add'
>>> fs.remove(4)
Traceback (most recent call last):
...
AttributeError: FrozenIntervalSet instance has no attribute 'remove'
>>> fs.clear()
Traceback (most recent call last):
...
AttributeError: FrozenIntervalSet instance has no attribute 'clear'
Because FrozenIntervalSets are immutable, they can be used as a
dictionary key.
>>> d = {FrozenIntervalSet(3, 66) : 52, FrozenIntervalSet.lessThan(3) : 3} |
| |
Methods defined here:
- __hash__(self)
- Generates a 32-bit hash key
>>> fs = FrozenIntervalSet(4, 7, 3)
>>> key = hash(fs)
- __init__(self, *args)
Methods inherited from BaseIntervalSet:
- __add__(self, other)
- This function returns the union of two IntervalSets. It does the
same thing as the __or__ function. A set or a single value can
also be added.
>>> empty = IntervalSet()
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print evens + positives
-8,-6,-4,-2,[0,~)
>>> print negatives + zero
(-~,0]
>>> print empty + negatives
(-~,0)
>>> print empty + naturals
[0,~)
>>> print nonzero + evens
(-~,~)
>>> print evens + set((1,3))
-8,-6,-4,-2,0,1,2,3,4,6,8
>>> nonzero + 0 == IntervalSet.all()
True
- __and__(self, other)
- This function returns the intersection of self and other.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print naturals and naturals
[0,~)
>>> print evens & zero
0
>>> print negatives & zero
<Empty>
>>> print nonzero & positives
(0,~)
>>> print empty & zero
<Empty>
>>> print evens & set((4, -2, 0, 3))
-2,0,4
>>> print evens & 5
<Empty>
>>> print evens & 2
2
- __contains__(self, obj)
- Returns True if obj is a subset of self.
>>> some = IntervalSet(
... 2, 8, Interval(12, True, 17, False), Interval.greaterThan(17))
>>> all = IntervalSet.all()
>>> empty = IntervalSet.empty()
>>> 17 in empty
False
>>> 17 in all
True
>>> 17 in some
False
>>> r = Interval(100, True, 400, False)
>>> r in empty
False
>>> r in all
True
>>> r in some
True
>>> empty in all
False
>>> empty in some
False
>>> all in empty
False
>>> all in some
False
>>> some in empty
False
>>> some in all
True
- __eq__(self, other)
- Two IntervalSets are identical if they contain the exact same
sets. Note that an empty set is never equal to any other set,
even an empty one.
>>> IntervalSet(4) == IntervalSet(1)
False
>>> IntervalSet(5) == IntervalSet(5)
True
>>> s1 = IntervalSet.between(4, 7)
>>> s2 = IntervalSet(Interval(4, True, 7, False))
>>> s1 == s2
False
>>> s2.add(7)
>>> s1 == s2
True
- __gt__(self, other)
- To test if a set is a superset of another, you can use the >
operator. I don't like this, personally, but in my attempt
to implement a set-like object, I've duplicated this
functionality.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero > positives
False
>>> zero > naturals
False
>>> positives > zero
False
>>> r > zero
False
>>> r > positives
False
>>> positives > r
True
>>> negatives > IntervalSet.all()
False
>>> r2 > negatives
False
>>> negatives > positives
False
- __invert__(self)
- This function returns the disjoint set of self. In other words,
all values self doesn't include are in the returned set.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> print ~(IntervalSet.empty())
(-~,~)
>>> ~negatives == naturals
True
>>> print ~positives
(-~,0]
>>> ~naturals == negatives
True
>>> print ~evens
(-~,-8),(-8,-6),(-6,-4),(-4,-2),(-2,0),(0,2),(2,4),(4,6),(6,8),(8,~)
>>> ~zero == nonzero
True
>>> ~nonzero == zero
True
- __iter__(self)
- Returns an iterator over the intervals in the set.
>>> s = IntervalSet(
... 2, 7, set([2, 87, 4, 3]), Interval.greaterThan(12),
... Interval.lessThan(-2))
>>> l = set()
>>> for i in s:
... l.add(str(i))
...
>>> print len(l)
6
>>> "2" in l
True
>>> "7" in l
True
>>> "87" in l
False
>>> "4" in l
True
>>> "3" in l
True
>>> "(12,~)" in l
True
>>> "(-~,-2)" in l
True
- __lt__(self, other)
- To test if a set is a subset of another, you can use the <
operator. I don't like this, personally, but in my attempt
to implement a set-like object, I've duplicated this
functionality.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero < positives
False
>>> zero < naturals
True
>>> positives < zero
False
>>> r < zero
False
>>> r < positives
True
>>> positives < r
False
>>> negatives < IntervalSet.all()
True
>>> r2 < negatives
True
>>> negatives < positives
False
- __nonzero__(self)
- An empty IntervalSet is the zero-like value.
>>> nonempty = IntervalSet(3)
>>> if IntervalSet.empty():
... print "Non-empty"
>>> if nonempty:
... print "Non-empty"
Non-empty
- __or__(self, other)
- This function returns the union of two IntervalSets. It does the same
thing as the __add__ function. It can also accept sets or single
values.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens | positives
-8,-6,-4,-2,[0,~)
>>> print negatives | zero
(-~,0]
>>> print empty | negatives
(-~,0)
>>> print empty | naturals
[0,~)
>>> print nonzero | evens
(-~,~)
>>> print evens | set((-3, -5))
-8,-6,-5,-4,-3,-2,0,2,4,6,8
>>> print nonzero | 0 == all
True
- __str__(self)
- This function shows a string representation of an IntervalSet.
The string is shown sorted, with all intervals normalized.
>>> print IntervalSet()
<Empty>
>>> print IntervalSet(62)
62
>>> print IntervalSet(62, 56)
56,62
>>> print IntervalSet(23, Interval(26, True, 32, False))
23,[26,32)
>>> print IntervalSet.lessThan(3) + IntervalSet.greaterThan(3)
(-~,3),(3,~)
>>> print IntervalSet(Interval.lessThan(6, True))
(-~,6]
- __sub__(self, other)
- Returns all values of self minus all matching values in other.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens - nonzero
0
>>> print empty - naturals
<Empty>
>>> print zero - naturals
<Empty>
>>> print positives - zero
(0,~)
>>> print naturals - negatives
[0,~)
>>> print all - zero
(-~,0),(0,~)
>>> all - zero == nonzero
True
>>> print evens - set((2, 4, 6, 8))
-8,-6,-4,-2,0
>>> print evens - 8
-8,-6,-4,-2,0,2,4,6
>>> print IntervalSet.lessThan(40) - 40
(-~,40)
- __xor__(self, other)
- This function returns the exclusive or of two IntervalSets.
Regular sets or single values can also be xor'ed.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print nonzero ^ naturals
(-~,0]
>>> print zero ^ negatives
(-~,0]
>>> print positives ^ empty
(0,~)
>>> print evens ^ zero
-8,-6,-4,-2,2,4,6,8
>>> print evens ^ set([2, 4, 6, 8, 10])
-8,-6,-4,-2,0,10
>>> print zero ^ 0
<Empty>
>>> print zero ^ 42
0,42
- copy(self)
- Returns a copy of the object.
>>> s = IntervalSet(7, 2, 3, set([2, 6, 2]), Interval.greaterThan(3))
>>> s2 = s.copy()
>>> s == s2
True
>>> s = FrozenIntervalSet(
... 7, 2, 3, set([2, 6, 2]), Interval.greaterThan(3))
>>> s2 = s.copy()
>>> s == s2
True
- difference(self, other)
- Returns all values of self minus all matching values in other. It
is identical to the - operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens.difference(nonzero)
0
>>> print empty.difference(naturals)
<Empty>
>>> print zero.difference(naturals)
<Empty>
>>> print positives.difference(zero)
(0,~)
>>> print naturals.difference(negatives)
[0,~)
>>> print all.difference(zero)
(-~,0),(0,~)
>>> all.difference(zero) == nonzero
True
>>> print evens.difference(set([2, 4, 6, 8]))
-8,-6,-4,-2,0
>>> print evens.difference(8)
-8,-6,-4,-2,0,2,4,6
>>> print IntervalSet.lessThan(40).difference(40)
(-~,40)
- intersection(self, other)
- This function returns the intersection of self and other. It is
identical to the & operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print naturals.intersection(naturals)
[0,~)
>>> print evens.intersection(zero)
0
>>> print negatives.intersection(zero)
<Empty>
>>> print nonzero.intersection(positives)
(0,~)
>>> print empty.intersection(zero)
<Empty>
>>> print evens.intersection(set([4, -2, 0, 3]))
-2,0,4
>>> print evens.intersection(5)
<Empty>
>>> print evens.intersection(2)
2
- issubset(self, obj)
- Returns true if self is a subset of obj.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero.issubset(positives)
False
>>> zero.issubset(naturals)
True
>>> positives.issubset(zero)
False
>>> r.issubset(zero)
False
>>> r.issubset(positives)
True
>>> positives.issubset(r)
False
>>> negatives.issubset(IntervalSet.all())
True
>>> r2.issubset(negatives)
True
>>> negatives.issubset(positives)
False
- issuperset(self, obj)
- Returns true if self is a superset of obj.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero.issuperset(positives)
False
>>> zero.issuperset(naturals)
False
>>> positives.issuperset(zero)
False
>>> r.issuperset(zero)
False
>>> r.issuperset(positives)
False
>>> positives.issuperset(r)
True
>>> negatives.issuperset(IntervalSet.all())
False
>>> r2.issuperset(negatives)
False
>>> negatives.issuperset(positives)
False
- symmetric_difference(self, other)
- This function returns the exclusive or of two IntervalSets.
Regular sets or single values can also be xor'ed. It is
identical to the ^ operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print nonzero.symmetric_difference(naturals)
(-~,0]
>>> print zero.symmetric_difference(negatives)
(-~,0]
>>> print positives.symmetric_difference(empty)
(0,~)
>>> print evens.symmetric_difference(zero)
-8,-6,-4,-2,2,4,6,8
>>> print evens.symmetric_difference(set([2, 4, 6, 8, 10]))
-8,-6,-4,-2,0,10
>>> print zero.symmetric_difference(0)
<Empty>
>>> print zero.symmetric_difference(42)
0,42
- union(self, other)
- This function returns the union of two BaseIntervalSets. It is
identical to the | operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens.union(positives)
-8,-6,-4,-2,[0,~)
>>> print negatives.union(zero)
(-~,0]
>>> print empty.union(negatives)
(-~,0)
>>> print empty.union(naturals)
[0,~)
>>> print nonzero.union(evens)
(-~,~)
>>> print evens.union(set([-3, -5]))
-8,-6,-5,-4,-3,-2,0,2,4,6,8
>>> print nonzero.union(0) == all
True
Class methods inherited from BaseIntervalSet:
- all(cls) from __builtin__.classobj
- Returns an interval set containing all values.
>>> print IntervalSet.all()
(-~,~)
- between(cls, a, b, closed=True) from __builtin__.classobj
- Returns an IntervalSet of all values between a and b. If closed is
True, then the endpoints are included; otherwise, they aren't.
>>> print IntervalSet.between(0, 100)
[0,100]
>>> print IntervalSet.between(-1, 1)
[-1,1]
- empty(cls) from __builtin__.classobj
- Returns an interval set containing no values.
>>> print IntervalSet.empty()
<Empty>
- greaterThan(cls, n, closed=False) from __builtin__.classobj
- Returns an IntervalSet of all values greater than or equal to n. If
closed is True, then an IntervalSet of all values greater than or
equal to n is returned.
>>> print IntervalSet.greaterThan(0)
(0,~)
>>> print IntervalSet.greaterThan(0, True)
[0,~)
>>> print IntervalSet.greaterThan(-23)
(-23,~)
>>> print IntervalSet.greaterThan(-23, True)
[-23,~)
- lessThan(cls, n, closed=False) from __builtin__.classobj
- Returns an IntervalSet of all values less than n. If closed is
True, then an IntervalSet of all values less than or equal to n is
returned.
>>> print IntervalSet.lessThan(0)
(-~,0)
>>> print IntervalSet.lessThan(0, True)
(-~,0]
>>> print IntervalSet.lessThan(-23)
(-~,-23)
>>> print IntervalSet.lessThan(-23, True)
(-~,-23]
- notEqualTo(cls, n) from __builtin__.classobj
- Returns an IntervalSet of all values not equal to n.
>>> print IntervalSet.notEqualTo(0)
(-~,0),(0,~)
>>> print IntervalSet.notEqualTo(-23)
(-~,-23),(-23,~)
|
class Interval |
| |
An Interval is composed of the lower bound, a closed lower bound
flag, an upper bound, and a closed upper bound flag. The attributes
are called min, minClosed, max, and maxClosed, respectively. For an
infinite interval, the bound is set to inf or -inf. IntervalSets are
composed of zero to many Intervals. |
| |
Methods defined here:
- __add__(self, other)
- Yields an Interval that encompasses self and other. If the
Intervals don't overlap or aren't adjacent, then an exception is
raised.
>>> r1 = Interval.lessThan(-100)
>>> r2 = Interval.lessThan(-100, True)
>>> r3 = Interval.lessThan(100)
>>> r4 = Interval.lessThan(100, True)
>>> r5 = Interval.all()
>>> r6 = Interval.between(-100, 100, False)
>>> r7 = Interval(-100, False, 100, True)
>>> r8 = Interval.greaterThan(-100)
>>> r9 = Interval.equalTo(-100)
>>> r10 = Interval(-100, True, 100, False)
>>> r11 = Interval.between(-100, 100)
>>> r12 = Interval.greaterThan(-100, True)
>>> r13 = Interval.greaterThan(100)
>>> r14 = Interval.equalTo(100)
>>> r15 = Interval.greaterThan(100, True)
>>> print r4 + r9
(-~,100]
>>> print r1 + r10
(-~,100)
>>> print r8 + r15
(-100,~)
>>> print r13 + r15
[100,~)
>>> print r7 + r11
[-100,100]
>>> print r12 + r15
[-100,~)
>>> print r2 + r13
Traceback (most recent call last):
...
ArithmeticError: The Intervals are disjoint.
>>> print r5 + r6
(-~,~)
>>> print r5 + r14
(-~,~)
>>> print r3 + r4
(-~,100]
- __contains__(self, obj)
- Returns True if obj lies wholly within the Interval.
>>> all = Interval.all()
>>> lt = Interval.lessThan(10)
>>> le = Interval.lessThan(10, True)
>>> some = Interval(10, False, 20, True)
>>> single = Interval.equalTo(10)
>>> ge = Interval.greaterThan(10, True)
>>> gt = Interval.greaterThan(10)
>>> ne = Interval.equalTo(17)
>>> 10 in all
True
>>> 10 in lt
False
>>> 10 in le
True
>>> 10 in some
False
>>> 10 in single
True
>>> 10 in ge
True
>>> 10 in gt
False
>>> 10 in ne
False
>>> all in some
False
>>> lt in all
True
>>> lt in some
False
>>> single in ge
True
>>> ne in some
True
>>> set((4, 2, 6)) in some
False
>>> set((11, 13, 15)) in some
True
- __hash__(self)
- Intervals are to be considered immutable. Thus, a 32-bit hash can
be generated for them.
>>> h = hash(Interval.lessThan(5))
- __init__(self, lbound, linc, ubound, uinc)
- An Interval can represent an infinite set.
>>> r = Interval(-inf, False, inf, False) # All values
An unbound portion of an Interval cannot be included; negative and
positive infinity are not true values.
>>> r = Interval(-inf, True, inf, False) # Invalid Interval
Traceback (most recent call last):
...
ValueError: Unbound ends cannot be included in an interval.
>>> r = Interval(-inf, False, inf, True) # Invalid Interval
Traceback (most recent call last):
...
ValueError: Unbound ends cannot be included in an interval.
An Interval can represent sets unbounded on an end.
>>> r = Interval(-inf, False, 62, False)
>>> r = Interval(-inf, False, 37, True)
>>> r = Interval(246, True, inf, False)
>>> r = Interval(2468, False, inf, False)
An Interval can represent a set of values up to, but not including a
value.
>>> r = Interval(25, False, 28, False)
An Interval can represent a set of values that have an inclusive
boundary.
>>> r = Interval(29, True, 216, True)
An Interval can represent a single value
>>> r = Interval(82, True, 82, True)
Intervals that are not normalized, i.e. that have a lower bound
exceeding an upper bound, are silently normalized.
>>> print Interval(5, False, 2, True)
[2,5)
Intervals can represent an empty set.
>>> r = Interval(5, False, 5, False)
- __nonzero__(self)
- An interval is non-zero if the interval is not empty.
>>> if Interval(12, False, 12, False):
... print "Non-empty"
>>> if Interval(12, False, 12, True):
... print "Non-empty"
>>> if Interval(12, True, 12, True):
... print "Non-empty"
Non-empty
- __str__(self)
- This function yields a graphical representation of an Interval.
It is included in the __str__ of an IntervalSet. Non-inclusive
boundaries are bordered by a ( or ). Inclusive boundaries are
bordered by [ or ]. Unbound lower values are shown as -~,
representing negative infinity. Unbound upper values are shown as
~, representing infinity. Intervals consisting of only a single
value are shown as that value.
>>> print Interval.all()
(-~,~)
>>> print Interval.lessThan(100)
(-~,100)
>>> print Interval.lessThan(2593, True)
(-~,2593]
>>> print Interval.greaterThan(2378)
(2378,~)
>>> print Interval.between(26, 8234, False)
(26,8234)
>>> print Interval(237, False, 2348, True)
(237,2348]
>>> print Interval.greaterThan(347, True)
[347,~)
>>> print Interval(237, True, 278, False)
[237,278)
>>> print Interval.between(723, 2378)
[723,2378]
>>> print Interval.equalTo(5)
5
- adjacentTo(self, other)
- Returns True if self is adjacent to other, meaning that if they
were joined, there would be no discontinuity. They cannot
overlap.
>>> r1 = Interval.lessThan(-100)
>>> r2 = Interval.lessThan(-100, True)
>>> r3 = Interval.lessThan(100)
>>> r4 = Interval.lessThan(100, True)
>>> r5 = Interval.all()
>>> r6 = Interval.between(-100, 100, False)
>>> r7 = Interval(-100, False, 100, True)
>>> r8 = Interval.greaterThan(-100)
>>> r9 = Interval.equalTo(-100)
>>> r10 = Interval(-100, True, 100, False)
>>> r11 = Interval.between(-100, 100)
>>> r12 = Interval.greaterThan(-100, True)
>>> r13 = Interval.greaterThan(100)
>>> r14 = Interval.equalTo(100)
>>> r15 = Interval.greaterThan(100, True)
>>> r1.adjacentTo(r6)
False
>>> r6.adjacentTo(r11)
False
>>> r7.adjacentTo(r9)
True
>>> r3.adjacentTo(r10)
False
>>> r5.adjacentTo(r14)
False
>>> r6.adjacentTo(r15)
True
>>> r1.adjacentTo(r8)
False
>>> r12.adjacentTo(r14)
False
>>> r6.adjacentTo(r13)
False
>>> r2.adjacentTo(r15)
False
>>> r1.adjacentTo(r4)
False
- comesBefore(self, other)
- self comes before other when sorted if its lower bound is less
than other's smallest value. If the smallest value is the same,
then the Interval with the smallest upper bound comes first.
Otherwise, they are equal.
>>> Interval.equalTo(1).comesBefore(Interval.equalTo(4))
True
>>> Interval.lessThan(1, True).comesBefore(Interval.equalTo(4))
True
>>> Interval.lessThan(5, True).comesBefore(Interval.lessThan(5))
False
>>> Interval.lessThan(5).comesBefore(Interval.lessThan(5, True))
True
- overlaps(self, other)
- Returns True if the one Interval overlaps another. If they are
immediately adjacent, then this returns False. Use the adjacentTo
function for testing for adjacent Intervals.
>>> r1 = Interval.lessThan(-100)
>>> r2 = Interval.lessThan(-100, True)
>>> r3 = Interval.lessThan(100)
>>> r4 = Interval.lessThan(100, True)
>>> r5 = Interval.all()
>>> r6 = Interval.between(-100, 100, False)
>>> r7 = Interval(-100, False, 100, True)
>>> r8 = Interval.greaterThan(-100)
>>> r9 = Interval.equalTo(-100)
>>> r10 = Interval(-100, True, 100, False)
>>> r11 = Interval.between(-100, 100)
>>> r12 = Interval.greaterThan(-100, True)
>>> r13 = Interval.greaterThan(100)
>>> r14 = Interval.equalTo(100)
>>> r15 = Interval.greaterThan(100, True)
>>> r8.overlaps(r9)
False
>>> r12.overlaps(r6)
True
>>> r7.overlaps(r8)
True
>>> r8.overlaps(r4)
True
>>> r14.overlaps(r11)
True
>>> r10.overlaps(r13)
False
>>> r5.overlaps(r1)
True
>>> r5.overlaps(r2)
True
>>> r15.overlaps(r6)
False
>>> r3.overlaps(r1)
True
Class methods defined here:
- all(cls) from __builtin__.classobj
- Returns an interval encompassing all values
- between(cls, a, b, closed=True) from __builtin__.classobj
- Returns an interval between values a and b. If closed is True,
then the endpoints are included. Otherwise, the endpoints are
excluded.
>>> print Interval.between(2, 4)
[2,4]
>>> print Interval.between(2, 4, False)
(2,4)
- equalTo(cls, a) from __builtin__.classobj
- Returns an interval containing only a.
>>> print Interval.equalTo(32)
32
- greaterThan(cls, a, closed=False) from __builtin__.classobj
- Returns an interval containing all values greater than a. If
closed is True, then all values greater than or equal to a are
returned.
>>> print Interval.greaterThan(32)
(32,~)
>>> print Interval.greaterThan(32, True)
[32,~)
- lessThan(cls, a, closed=False) from __builtin__.classobj
- Returns an interval containing all values less than a. If closed
is True, then all values less than or equal to a are returned.
>>> print Interval.lessThan(32)
(-~,32)
>>> print Interval.lessThan(32, True)
(-~,32]
|
class IntervalSet(BaseIntervalSet) |
| |
IntervalSet is a class representing sets of continuous values, as
opposed to a discrete set, which is already implemented by the set
type in Python.
IntervalSets can be bounded, unbounded, and non-continuous. They were
designed to accomodate any sort of mathematical set dealing with
continuous values. This will usually mean numbers, but any Python
type that has valid comparison operations can be used in an
IntervalSet.
Because IntervalSets are mutable, it cannot be used as a dictionary
key.
>>> {IntervalSet(3, 66) : 52}
Traceback (most recent call last):
...
TypeError: unhashable instance |
| |
Methods defined here:
- __init__(self, *args)
- add(self, obj)
- This function adds an Interval, discrete value, or IntervalSet to
an IntervalSet.
>>> r = IntervalSet()
>>> r.add(4)
>>> print r
4
>>> r.add(Interval(23, False, 39, True))
>>> print r
4,(23,39]
>>> r.add(Interval.lessThan(25))
>>> print r
(-~,39]
>>> r2 = IntervalSet(Interval.all())
>>> r.add(r2)
>>> print r
(-~,~)
- clear(self)
- Removes all intervals from an IntervalSet.
>>> s = IntervalSet(2, 7, Interval.greaterThan(8), set([2, 6, 34]))
>>> print s
2,6,7,(8,~)
>>> s.clear()
>>> print s
<Empty>
- difference_update(self, obj)
- This function removes an Interval, discrete value, or IntervalSet
from an IntervalSet.
>>> r = IntervalSet.all()
>>> r.difference_update(4)
>>> print r
(-~,4),(4,~)
>>> r.difference_update(Interval(23, False, 39, True))
>>> print r
(-~,4),(4,23],(39,~)
>>> r.difference_update(Interval.lessThan(25))
>>> print r
(39,~)
>>> r2 = IntervalSet(Interval.all())
>>> r.difference_update(r2)
>>> print r
<Empty>
- discard(self, obj)
- This function removes an Interval, discrete value, or IntervalSet
from an IntervalSet.
>>> r = IntervalSet.all()
>>> r.discard(4)
>>> print r
(-~,4),(4,~)
>>> r.discard(Interval(23, False, 39, True))
>>> print r
(-~,4),(4,23],(39,~)
>>> r.discard(Interval.lessThan(25))
>>> print r
(39,~)
>>> r2 = IntervalSet(Interval.all())
>>> r.discard(r2)
>>> print r
<Empty>
- intersection_update(self, other)
- Removes elements not found in other.
>>> r = IntervalSet.all()
>>> r.intersection_update(4)
>>> print r
4
>>> r = IntervalSet.all()
>>> r.intersection_update(Interval(23, False, 39, True))
>>> print r
(23,39]
>>> r.intersection_update(Interval.lessThan(25))
>>> print r
(23,25)
>>> r2 = IntervalSet(Interval.all())
>>> r.intersection_update(r2)
>>> print r
(23,25)
- pop(self)
- Returns and discards an Interval from the set.
>>> s = IntervalSet(7, Interval.lessThan(2), set([2, 0]))
>>> l = []
>>> l.append(str(s.pop()))
>>> l.append(str(s.pop()))
>>> "(-~,2)" in l
False
>>> "(-~,2]" in l
True
>>> "7" in l
True
>>> print s
<Empty>
>>> i = s.pop()
Traceback (most recent call last):
...
KeyError: 'pop from an empty IntervalSet'
- remove(self, obj)
- This function removes an Interval, discrete value, or IntervalSet
from an IntervalSet. If the object is not in the set, a KeyError
is raised.
>>> r = IntervalSet.all()
>>> r.remove(4)
>>> print r
(-~,4),(4,~)
>>> r.remove(Interval(23, False, 39, True))
>>> print r
(-~,4),(4,23],(39,~)
>>> r.remove(Interval.lessThan(25))
Traceback (most recent call last):
...
KeyError: '(-~,25)'
>>> r2 = IntervalSet(Interval.all())
>>> r.remove(r2)
Traceback (most recent call last):
...
KeyError: '(-~,~)'
- symmetric_difference_update(self, other)
- Removes elements found in other and adds elements in other that
are not in self.
>>> r = IntervalSet.empty()
>>> r.symmetric_difference_update(4)
>>> print r
4
>>> r.symmetric_difference_update(Interval(23, False, 39, True))
>>> print r
4,(23,39]
>>> r.symmetric_difference_update(Interval.lessThan(25))
>>> print r
(-~,4),(4,23],[25,39]
>>> r2 = IntervalSet(Interval.all())
>>> r.symmetric_difference_update(r2)
>>> print r
4,(23,25),(39,~)
- update(self, other)
- Adds elements from other to self.
>>> r = IntervalSet()
>>> r.update(4)
>>> print r
4
>>> r.update(Interval(23, False, 39, True))
>>> print r
4,(23,39]
>>> r.update(Interval.lessThan(25))
>>> print r
(-~,39]
>>> r2 = IntervalSet(Interval.all())
>>> r.update(r2)
>>> print r
(-~,~)
Methods inherited from BaseIntervalSet:
- __add__(self, other)
- This function returns the union of two IntervalSets. It does the
same thing as the __or__ function. A set or a single value can
also be added.
>>> empty = IntervalSet()
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print evens + positives
-8,-6,-4,-2,[0,~)
>>> print negatives + zero
(-~,0]
>>> print empty + negatives
(-~,0)
>>> print empty + naturals
[0,~)
>>> print nonzero + evens
(-~,~)
>>> print evens + set((1,3))
-8,-6,-4,-2,0,1,2,3,4,6,8
>>> nonzero + 0 == IntervalSet.all()
True
- __and__(self, other)
- This function returns the intersection of self and other.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print naturals and naturals
[0,~)
>>> print evens & zero
0
>>> print negatives & zero
<Empty>
>>> print nonzero & positives
(0,~)
>>> print empty & zero
<Empty>
>>> print evens & set((4, -2, 0, 3))
-2,0,4
>>> print evens & 5
<Empty>
>>> print evens & 2
2
- __contains__(self, obj)
- Returns True if obj is a subset of self.
>>> some = IntervalSet(
... 2, 8, Interval(12, True, 17, False), Interval.greaterThan(17))
>>> all = IntervalSet.all()
>>> empty = IntervalSet.empty()
>>> 17 in empty
False
>>> 17 in all
True
>>> 17 in some
False
>>> r = Interval(100, True, 400, False)
>>> r in empty
False
>>> r in all
True
>>> r in some
True
>>> empty in all
False
>>> empty in some
False
>>> all in empty
False
>>> all in some
False
>>> some in empty
False
>>> some in all
True
- __eq__(self, other)
- Two IntervalSets are identical if they contain the exact same
sets. Note that an empty set is never equal to any other set,
even an empty one.
>>> IntervalSet(4) == IntervalSet(1)
False
>>> IntervalSet(5) == IntervalSet(5)
True
>>> s1 = IntervalSet.between(4, 7)
>>> s2 = IntervalSet(Interval(4, True, 7, False))
>>> s1 == s2
False
>>> s2.add(7)
>>> s1 == s2
True
- __gt__(self, other)
- To test if a set is a superset of another, you can use the >
operator. I don't like this, personally, but in my attempt
to implement a set-like object, I've duplicated this
functionality.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero > positives
False
>>> zero > naturals
False
>>> positives > zero
False
>>> r > zero
False
>>> r > positives
False
>>> positives > r
True
>>> negatives > IntervalSet.all()
False
>>> r2 > negatives
False
>>> negatives > positives
False
- __invert__(self)
- This function returns the disjoint set of self. In other words,
all values self doesn't include are in the returned set.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> print ~(IntervalSet.empty())
(-~,~)
>>> ~negatives == naturals
True
>>> print ~positives
(-~,0]
>>> ~naturals == negatives
True
>>> print ~evens
(-~,-8),(-8,-6),(-6,-4),(-4,-2),(-2,0),(0,2),(2,4),(4,6),(6,8),(8,~)
>>> ~zero == nonzero
True
>>> ~nonzero == zero
True
- __iter__(self)
- Returns an iterator over the intervals in the set.
>>> s = IntervalSet(
... 2, 7, set([2, 87, 4, 3]), Interval.greaterThan(12),
... Interval.lessThan(-2))
>>> l = set()
>>> for i in s:
... l.add(str(i))
...
>>> print len(l)
6
>>> "2" in l
True
>>> "7" in l
True
>>> "87" in l
False
>>> "4" in l
True
>>> "3" in l
True
>>> "(12,~)" in l
True
>>> "(-~,-2)" in l
True
- __lt__(self, other)
- To test if a set is a subset of another, you can use the <
operator. I don't like this, personally, but in my attempt
to implement a set-like object, I've duplicated this
functionality.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero < positives
False
>>> zero < naturals
True
>>> positives < zero
False
>>> r < zero
False
>>> r < positives
True
>>> positives < r
False
>>> negatives < IntervalSet.all()
True
>>> r2 < negatives
True
>>> negatives < positives
False
- __nonzero__(self)
- An empty IntervalSet is the zero-like value.
>>> nonempty = IntervalSet(3)
>>> if IntervalSet.empty():
... print "Non-empty"
>>> if nonempty:
... print "Non-empty"
Non-empty
- __or__(self, other)
- This function returns the union of two IntervalSets. It does the same
thing as the __add__ function. It can also accept sets or single
values.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens | positives
-8,-6,-4,-2,[0,~)
>>> print negatives | zero
(-~,0]
>>> print empty | negatives
(-~,0)
>>> print empty | naturals
[0,~)
>>> print nonzero | evens
(-~,~)
>>> print evens | set((-3, -5))
-8,-6,-5,-4,-3,-2,0,2,4,6,8
>>> print nonzero | 0 == all
True
- __str__(self)
- This function shows a string representation of an IntervalSet.
The string is shown sorted, with all intervals normalized.
>>> print IntervalSet()
<Empty>
>>> print IntervalSet(62)
62
>>> print IntervalSet(62, 56)
56,62
>>> print IntervalSet(23, Interval(26, True, 32, False))
23,[26,32)
>>> print IntervalSet.lessThan(3) + IntervalSet.greaterThan(3)
(-~,3),(3,~)
>>> print IntervalSet(Interval.lessThan(6, True))
(-~,6]
- __sub__(self, other)
- Returns all values of self minus all matching values in other.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens - nonzero
0
>>> print empty - naturals
<Empty>
>>> print zero - naturals
<Empty>
>>> print positives - zero
(0,~)
>>> print naturals - negatives
[0,~)
>>> print all - zero
(-~,0),(0,~)
>>> all - zero == nonzero
True
>>> print evens - set((2, 4, 6, 8))
-8,-6,-4,-2,0
>>> print evens - 8
-8,-6,-4,-2,0,2,4,6
>>> print IntervalSet.lessThan(40) - 40
(-~,40)
- __xor__(self, other)
- This function returns the exclusive or of two IntervalSets.
Regular sets or single values can also be xor'ed.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print nonzero ^ naturals
(-~,0]
>>> print zero ^ negatives
(-~,0]
>>> print positives ^ empty
(0,~)
>>> print evens ^ zero
-8,-6,-4,-2,2,4,6,8
>>> print evens ^ set([2, 4, 6, 8, 10])
-8,-6,-4,-2,0,10
>>> print zero ^ 0
<Empty>
>>> print zero ^ 42
0,42
- copy(self)
- Returns a copy of the object.
>>> s = IntervalSet(7, 2, 3, set([2, 6, 2]), Interval.greaterThan(3))
>>> s2 = s.copy()
>>> s == s2
True
>>> s = FrozenIntervalSet(
... 7, 2, 3, set([2, 6, 2]), Interval.greaterThan(3))
>>> s2 = s.copy()
>>> s == s2
True
- difference(self, other)
- Returns all values of self minus all matching values in other. It
is identical to the - operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens.difference(nonzero)
0
>>> print empty.difference(naturals)
<Empty>
>>> print zero.difference(naturals)
<Empty>
>>> print positives.difference(zero)
(0,~)
>>> print naturals.difference(negatives)
[0,~)
>>> print all.difference(zero)
(-~,0),(0,~)
>>> all.difference(zero) == nonzero
True
>>> print evens.difference(set([2, 4, 6, 8]))
-8,-6,-4,-2,0
>>> print evens.difference(8)
-8,-6,-4,-2,0,2,4,6
>>> print IntervalSet.lessThan(40).difference(40)
(-~,40)
- intersection(self, other)
- This function returns the intersection of self and other. It is
identical to the & operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print naturals.intersection(naturals)
[0,~)
>>> print evens.intersection(zero)
0
>>> print negatives.intersection(zero)
<Empty>
>>> print nonzero.intersection(positives)
(0,~)
>>> print empty.intersection(zero)
<Empty>
>>> print evens.intersection(set([4, -2, 0, 3]))
-2,0,4
>>> print evens.intersection(5)
<Empty>
>>> print evens.intersection(2)
2
- issubset(self, obj)
- Returns true if self is a subset of obj.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero.issubset(positives)
False
>>> zero.issubset(naturals)
True
>>> positives.issubset(zero)
False
>>> r.issubset(zero)
False
>>> r.issubset(positives)
True
>>> positives.issubset(r)
False
>>> negatives.issubset(IntervalSet.all())
True
>>> r2.issubset(negatives)
True
>>> negatives.issubset(positives)
False
- issuperset(self, obj)
- Returns true if self is a superset of obj.
>>> zero = IntervalSet(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> negatives = IntervalSet.lessThan(0)
>>> r = IntervalSet.between(3, 6)
>>> r2 = IntervalSet.between(-8, -2)
>>> zero.issuperset(positives)
False
>>> zero.issuperset(naturals)
False
>>> positives.issuperset(zero)
False
>>> r.issuperset(zero)
False
>>> r.issuperset(positives)
False
>>> positives.issuperset(r)
True
>>> negatives.issuperset(IntervalSet.all())
False
>>> r2.issuperset(negatives)
False
>>> negatives.issuperset(positives)
False
- symmetric_difference(self, other)
- This function returns the exclusive or of two IntervalSets.
Regular sets or single values can also be xor'ed. It is
identical to the ^ operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> print nonzero.symmetric_difference(naturals)
(-~,0]
>>> print zero.symmetric_difference(negatives)
(-~,0]
>>> print positives.symmetric_difference(empty)
(0,~)
>>> print evens.symmetric_difference(zero)
-8,-6,-4,-2,2,4,6,8
>>> print evens.symmetric_difference(set([2, 4, 6, 8, 10]))
-8,-6,-4,-2,0,10
>>> print zero.symmetric_difference(0)
<Empty>
>>> print zero.symmetric_difference(42)
0,42
- union(self, other)
- This function returns the union of two BaseIntervalSets. It is
identical to the | operator.
>>> negatives = IntervalSet.lessThan(0)
>>> positives = IntervalSet.greaterThan(0)
>>> naturals = IntervalSet.greaterThan(0, True)
>>> evens = IntervalSet(-8, -6, -4, -2, 0, 2, 4, 6, 8)
>>> zero = IntervalSet(0)
>>> nonzero = IntervalSet.notEqualTo(0)
>>> empty = IntervalSet.empty()
>>> all = IntervalSet.all()
>>> print evens.union(positives)
-8,-6,-4,-2,[0,~)
>>> print negatives.union(zero)
(-~,0]
>>> print empty.union(negatives)
(-~,0)
>>> print empty.union(naturals)
[0,~)
>>> print nonzero.union(evens)
(-~,~)
>>> print evens.union(set([-3, -5]))
-8,-6,-5,-4,-3,-2,0,2,4,6,8
>>> print nonzero.union(0) == all
True
Class methods inherited from BaseIntervalSet:
- all(cls) from __builtin__.classobj
- Returns an interval set containing all values.
>>> print IntervalSet.all()
(-~,~)
- between(cls, a, b, closed=True) from __builtin__.classobj
- Returns an IntervalSet of all values between a and b. If closed is
True, then the endpoints are included; otherwise, they aren't.
>>> print IntervalSet.between(0, 100)
[0,100]
>>> print IntervalSet.between(-1, 1)
[-1,1]
- empty(cls) from __builtin__.classobj
- Returns an interval set containing no values.
>>> print IntervalSet.empty()
<Empty>
- greaterThan(cls, n, closed=False) from __builtin__.classobj
- Returns an IntervalSet of all values greater than or equal to n. If
closed is True, then an IntervalSet of all values greater than or
equal to n is returned.
>>> print IntervalSet.greaterThan(0)
(0,~)
>>> print IntervalSet.greaterThan(0, True)
[0,~)
>>> print IntervalSet.greaterThan(-23)
(-23,~)
>>> print IntervalSet.greaterThan(-23, True)
[-23,~)
- lessThan(cls, n, closed=False) from __builtin__.classobj
- Returns an IntervalSet of all values less than n. If closed is
True, then an IntervalSet of all values less than or equal to n is
returned.
>>> print IntervalSet.lessThan(0)
(-~,0)
>>> print IntervalSet.lessThan(0, True)
(-~,0]
>>> print IntervalSet.lessThan(-23)
(-~,-23)
>>> print IntervalSet.lessThan(-23, True)
(-~,-23]
- notEqualTo(cls, n) from __builtin__.classobj
- Returns an IntervalSet of all values not equal to n.
>>> print IntervalSet.notEqualTo(0)
(-~,0),(0,~)
>>> print IntervalSet.notEqualTo(-23)
(-~,-23),(-23,~)
|
class Largest |
| |
This type doesn't do much; it implements a pseudo-value that's larger
than everything but itself.
>>> infinity = Largest()
>>> greatest = Largest()
>>> 6234 < infinity
True
>>> 6234 == infinity
False
>>> 6234 > infinity
False
>>> infinity > infinity
False
>>> infinity == greatest
True |
| |
Methods defined here:
- __cmp__(self, other)
- Always indicates that self is greater than other, unless both are of
type Largest, in which case they are equal.
- __hash__(self)
- Returns a value that can be used for generating hashes
- __neg__(self)
- The opposite of infinity is negative infinity, the smallest value.
- __repr__(self)
- The representation of the largest number is ~, which means infinity.
|
class Smallest |
| |
This type doesn't do much; it implements a pseudo-value that's smaller
than everything but itself.
>>> negInf = Smallest()
>>> smallest = Smallest()
>>> -264 < negInf
False
>>> -264 == negInf
False
>>> -264 > negInf
True
>>> negInf < negInf
False
>>> negInf == smallest
True |
| |
Methods defined here:
- __cmp__(self, other)
- Always indicates that self is less than other, unless both are of
type Smallest, in which case they are equal.
- __hash__(self)
- Returns a value that can be used for generating hashes
- __repr__(self)
- The representation of the smallest number is -~, which means negative
infinity.
| |