Module interval :: Class IntervalSet
[show private | hide private]
[frames | no frames]

Type IntervalSet

     object --+    
              |    
BaseIntervalSet --+
                  |
                 IntervalSet


The mutable version of 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

Method Summary
  __init__(self, items)
Initializes the IntervalSet
  __delitem__(self, index)
Removes the interval at the given index from the IntervalSet
  __hash__(self)
Raises an error since IntervalSets are mutable
  __repr__(self)
Returns an evaluable representation of the object
  add(self, obj)
Adds an Interval or discrete value to the object
  clear(self)
Removes all Intervals from the object
  difference_update(self, other)
Removes any elements in the given value from the object
  discard(self, obj)
Removes a value from the object
  intersection_update(self, other)
Removes values not found in the parameter
  pop(self)
Returns and discards an Interval or value from the IntervalSet
  remove(self, obj)
Removes a value from the object
  symmetric_difference_update(self, other)
Updates the object as though doing an xor with the parameter
  update(self, other)
Adds elements from the given value to the object
    Inherited from BaseIntervalSet
  __add__(self, other)
Returns the union of two IntervalSets
  __and__(self, other)
Returns the intersection of self and other.
  __cmp__(self, other)
Compares two BaseIntervalSets
  __contains__(self, obj)
Tells whether the BaseIntervalSet contains the given value
  __eq__(self, other)
Tests if two BaseIntervalSets are equivalent
  __ge__(self, other)
Tests if the given operand is a superset or is equal to the object
  __getitem__(self, index)
Gets the interval at the given index
  __gt__(self, other)
Tests if the given operand is a superset of the object
  __invert__(self)
Returns the disjoint set of self
  __iter__(self)
Returns an iterator over the intervals in the set
  __le__(self, other)
Tests if the given operand is a subset or is equal to the object
  __len__(self)
Returns the number of intervals contained in the object
  __lt__(self, other)
Tests if the given operand is a subset of the object
  __ne__(self, other)
Tests if two BaseIntervalSets are not equivalent
  __or__(self, other)
Returns the union of two IntervalSets.
  __str__(self)
Returns a string representation of the object
  __sub__(self, other)
Subtracts intervals in the given object from the object and returns the result
  __xor__(self, other)
Returns the exclusive or of two IntervalSets.
  all(cls)
Returns an interval set containing all values (Class method)
  between(cls, a, b, closed)
Returns an IntervalSet of all values between a and b. (Class method)
  bounds(self)
Returns an interval that encompasses the entire BaseIntervalSet
  copy(self)
Returns a copy of the object
  difference(self, other)
Returns the difference between the object and the given object
  empty(cls)
Returns an interval set containing no values. (Class method)
  greater_than(cls, n)
Returns an IntervalSet containing values greater than the given value (Class method)
  greater_than_or_equal_to(cls, n)
Returns an IntervalSet containing values greater than or equal to the given value (Class method)
  intersection(self, other)
Returns the intersection between the object and the given value
  issubset(self, other)
Tells if the given object is a subset of the object
  issuperset(self, other)
Tells whether the given object is a superset of the object
  less_than(cls, n)
Returns an IntervalSet containing values less than the given value (Class method)
  less_than_or_equal_to(cls, n, closed)
Returns an IntervalSet containing values less than or equal to the given value (Class method)
  lower_bound(self)
Returns the lower boundary of the BaseIntervalSet
  lower_closed(self)
Returns a boolean telling whether the lower bound is closed or not
  not_equal_to(cls, n)
Returns an IntervalSet of all values not equal to n (Class method)
  symmetric_difference(self, other)
Returns the exclusive or of the given value with the object
  union(self, other)
Returns the union of the given value with the object
  upper_bound(self)
Returns the upper boundary of the BaseIntervalSet
  upper_closed(self)
Returns a boolean telling whether the upper bound is closed or not
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value

Instance Method Details

__init__(self, items=[])
(Constructor)

Initializes the IntervalSet
Overrides:
interval.BaseIntervalSet.__init__

__delitem__(self, index)
(Index deletion operator)

Removes the interval at the given index from the IntervalSet
>>> interval = IntervalSet()
>>> del interval[0]
Traceback (most recent call last):

    ...

IndexError: Index is out of range

>>> interval = IntervalSet.between(-12, 2)
>>> del interval[1]
Traceback (most recent call last):

    ...

IndexError: Index is out of range

>>> len(interval)
1

>>> del interval[0]
>>> len(interval)
0

>>> interval = IntervalSet([2, 7, -2])
>>> len(interval)
3

>>> del interval[1]
>>> len(interval)
2

>>> print interval
-2,7

__hash__(self)
(Hashing function)

Raises an error since IntervalSets are mutable
Overrides:
__builtin__.object.__hash__

__repr__(self)
(Representation operator)

Returns an evaluable representation of the object
>>> IntervalSet([Interval()])
IntervalSet([Interval(-Inf, Inf, lower_closed=False, upper_closed=False)])

>>> IntervalSet()
IntervalSet([])

>>> IntervalSet([2, 4])
IntervalSet([Interval(2, 2, lower_closed=True, upper_closed=True), Interval(4, 4, lower_closed=True, upper_closed=True)])
Overrides:
__builtin__.object.__repr__

add(self, obj)

Adds an Interval or discrete value to the object
>>> r = IntervalSet()
>>> r.add(4)
>>> print r
4

>>> r.add(Interval(23, 39, lower_closed=False))
>>> print r
4,(23..39]

>>> r.add(Interval.less_than(25))
>>> print r
(...39]

clear(self)

Removes all Intervals from the object
>>> s = IntervalSet([2, 7, Interval.greater_than(8), 2, 6, 34])
>>> print s
2,6,7,(8...)

>>> s.clear()
>>> print s
<Empty>

difference_update(self, other)

Removes any elements in the given value from the object

This function removes the elements in other from self. other can be any iterable object.
>>> r = IntervalSet.all()
>>> r.difference_update([4])
>>> print r
(...4),(4...)

>>> r.difference_update(
...   IntervalSet([Interval(23, 39, lower_closed=False)]))
>>> print r
(...4),(4..23],(39...)

>>> r.difference_update(IntervalSet.less_than(25))
>>> print r
(39...)

>>> r2 = IntervalSet.all()
>>> r.difference_update(r2)
>>> print r
<Empty>

discard(self, obj)

Removes a value from the object

This function removes an Interval or discrete value from an IntervalSet.
>>> r = IntervalSet.all()
>>> r.discard(4)
>>> print r
(...4),(4...)

>>> r.discard(Interval(23, 39, lower_closed=False))
>>> print r
(...4),(4..23],(39...)

>>> r.discard(Interval.less_than(25))
>>> print r
(39...)

intersection_update(self, other)

Removes values not found in the parameter

Removes elements not found in other. other can be any iterable object
>>> r = IntervalSet.all()
>>> r.intersection_update([4])
>>> print r
4

>>> r = IntervalSet.all()
>>> r.intersection_update(
...   IntervalSet([Interval(23, 39, lower_closed=False)]))
>>> print r
(23..39]

>>> r.intersection_update(IntervalSet.less_than(25))
>>> print r
(23..25)

>>> r2 = IntervalSet.all()
>>> r.intersection_update(r2)
>>> print r
(23..25)

pop(self)

Returns and discards an Interval or value from the IntervalSet
>>> s = IntervalSet([7, Interval.less_than(2), 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)

Removes a value from the object

This function removes an Interval, discrete value, or set 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, 39, lower_closed=False))
>>> print r
(...4),(4..23],(39...)

>>> r.remove(Interval.less_than(25))
Traceback (most recent call last):

    ...

KeyError: '(...25)'

symmetric_difference_update(self, other)

Updates the object as though doing an xor with the parameter

Removes elements found in other and adds elements in other that are not in self. other can be any iterable object.
>>> r = IntervalSet.empty()
>>> r.symmetric_difference_update([4])
>>> print r
4

>>> r.symmetric_difference_update(
...   IntervalSet([Interval(23, 39, lower_closed=False)]))
>>> print r
4,(23..39]

>>> r.symmetric_difference_update(IntervalSet.less_than(25))
>>> print r
(...4),(4..23],[25..39]

>>> r2 = IntervalSet.all()
>>> r.symmetric_difference_update(r2)
>>> print r
4,(23..25),(39...)

update(self, other)

Adds elements from the given value to the object

Adds elements from other to self. other can be any iterable object.
>>> r = IntervalSet()
>>> r.update([4])
>>> print r
4

>>> r.update(IntervalSet([Interval(23, 39, lower_closed=False)]))
>>> print r
4,(23..39]

>>> r.update(IntervalSet.less_than(25))
>>> print r
(...39]

>>> r2 = IntervalSet.all()
>>> r.update(r2)
>>> print r
(...)

Generated by Epydoc 2.1 on Wed Nov 9 22:22:38 2005 http://epydoc.sf.net