srange Module

class srange.srange.srange(r='', auto_reset=True)[source]

String-range class.

This class provides functions to convert a string representing integers and ranges of integers to an object which can be iterated over all the values contained in the string and a list of individual values or subranges can be retrieved.

EXAMPLE::
>>> sr = srange("1,3,4-5,8-11,12-13,20")
>>> for val in sr:
                        print("%d," % val),
1, 3, 4, 5, 8, 9, 10, 11, 12, 13, 20,
NOTE:
String ranges can only contain integer numbers and must be strictly monotonically increasing. Multiple identical values are not allowed. addition of __resort_list() now allows for mis-ordered simple ranges, but they still must not overlap.
TODO:
The issues of the above note should be addressed to make the code more robust and forgiving. There is no reason one should not specify sub- ranges in arbitrary orders.

The range is checked to be monotonic, it returns None if no more values last is the last number obtained from this range, use -Inf to get start of range, it returns the next

variables and methods that you may be interested in

variables of interest description
self.r the input string, after formatting and compacting
self.previous_item the previous value produced, initially set very low
self.auto_reset if True (default), then previous_item is reset to min at each call to __iter__
methods of interest action
next() returns next value, updates previous_item too
reset_previous() reset the iterator so it starts with the first value
after(prev) returns value that follows prev, without changing the current point in iteration
first() returns the first number in the range, for self.r=”3,5,9-20”, self.first() returns 3
last() returns the last number in the range, for self.r=”3,5,9-20”, self.last() returns 20
len() returns number of points in the range, for self.r=”3,5,9-20”, self.len() returns 14
is_in_range(m) returns True if m is in self.r, otherwise False
index(ipnt) return the ipntth number from range, first number is ipnt==0, returns None if ipnt negative or too big
val2index(m) returns index into r that corresponds to m. e.g. for r=‘3,5,9-20’, m=5 returns 1.
sub_range(start,n,...) returns a new range that is a sub range of current one, setLast=False
list(self) returns a list where each element is a value in the range, CAUTION this can make a VERY big list
special methods command result using: sr = srange(‘1-4’)
__getitem__(n) print sr[2] 3
__len__() print len(sr) 4
__str__() print str(sr) 1-4
__repr__() print repr(sr) srange(‘1-4’, len=4, previous=0, auto_reset=True)
after(val)[source]

Return the value or the element that follows after the given value.

EXAMPLE::
>>> sr = srange("3,5,9-20")
>>> print sr.after(5)
9
first()[source]

Return the number of the first item in the range. This method uses but does not change any internal variables, e.g. no self.xxxx

EXAMPLE::
>>> sr = srange("3,5,9-20")
>>> print sr.first()
3
index(n)[source]

Return the n-th element from the string range. This method uses but does not change any internal variables, e.g. no self.xxxx

is_in_range(item)[source]

Return True if item is in string range self.r, False otherwise. This method uses but does not change any internal variables, e.g. no self.xxxx

last()[source]

Return the value of the last item in the range. This method uses but does not change any internal variables, e.g. no self.xxxx

EXAMPLE::
>>> sr = srange("3,5,9-20")
>>> print sr.last()
20
len()[source]

Return the number of items in the string range. This method uses but does not change any internal variables, e.g. no self.xxxx

EXAMPLE::
>>> sr = srange("3,5,9-20")
>>> print sr.len()
14
list()[source]

Expand a string range into a standard python list. This method uses but does not change any internal variables, e.g. no self.xxxx

EXAMPLE::
>>> print srange("3,5,9-13").list()
[3, 5, 9, 10, 11, 12, 13]
CAUTION:

The following statement:

>>> list("1-100000",";")

will produce a list with 100000 elements!

Max list length for a 32 bit system is (2^32 - 1)/2/4 = 536870912 on my computer I get a MemoryError for lengths > 1e8, so limit to 1e7

next()[source]

Return the next value in the string range. Also update self.previous_item.

reset_previous()[source]

Reset previous_item to the lowest possible integer value.

sub_range(start, n, set_last=False)[source]

Return a sub range from the original range as a new range string.

The new range starts at with the value start and has up to n elements. If start is not an element in the range, then it begin with first element after start. If set_last is True, then self.previous_item is set to the new end, otherwise no change is made. This method only changes an internal variable “self.previous_item” when set_last is True.

EXAMPLE::
>>> sr = srange('3,5,9-20')
>>> print sr.sub_range(start = 5, n = 3)
5,9-10
val2index(val)[source]

Return the index into the srange that corresponds to val. This method uses but does not change any internal variables, e.g. no self.xxxx

EXAMPLE::
>>> r = '3, 5, 9-20'
>>> print val2index(5)
1