Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
IntSet Class Reference

Public Member Functions

def __init__
 
def reset
 
def __cmp__
 
def __hash__
 
def __repr__
 
def normalize
 
def tostring
 
def tolist
 
def fromlist
 
def clone
 
def min
 
def max
 
def contains
 
def append
 
def addpair
 
def fromstring
 

Data Fields

 pairs
 
 sep
 
 rng
 

Detailed Description

Class implementing sets of integers.

This is an efficient representation for sets consisting of several
continuous ranges, e.g. 1-100,200-400,402-1000 is represented
internally as a list of three pairs: [(1,100), (200,400),
(402,1000)].  The internal representation is always kept normalized.

The constructor has up to three arguments:
- the string used to initialize the set (default ''),
- the separator between ranges (default ',')
- the separator between begin and end of a range (default '-')
The separators must be strings (not regexprs) and should be different.

The tostring() function yields a string that can be passed to another
IntSet constructor; __repr__() is a valid IntSet constructor itself.

Definition at line 767 of file mhlib.py.

Constructor & Destructor Documentation

def __init__ (   self,
  data = None,
  sep = ',
  rng = '-' 
)

Definition at line 790 of file mhlib.py.

791  def __init__(self, data = None, sep = ',', rng = '-'):
792  self.pairs = []
793  self.sep = sep
794  self.rng = rng
795  if data: self.fromstring(data)

Member Function Documentation

def __cmp__ (   self,
  other 
)

Definition at line 799 of file mhlib.py.

References filecmp.cmp(), and IntSet.pairs.

800  def __cmp__(self, other):
801  return cmp(self.pairs, other.pairs)
def __hash__ (   self)

Definition at line 802 of file mhlib.py.

References IntSet.pairs.

803  def __hash__(self):
804  return hash(self.pairs)
def __repr__ (   self)

Definition at line 805 of file mhlib.py.

References IntSet.rng, IntSet.sep, and IntSet.tostring().

806  def __repr__(self):
807  return 'IntSet(%s, %s, %s)' % (`self.tostring()`,
808  `self.sep`, `self.rng`)
def addpair (   self,
  xlo,
  xhi 
)

Definition at line 881 of file mhlib.py.

References IntSet.normalize().

882  def addpair(self, xlo, xhi):
883  if xlo > xhi: return
884  self.pairs.append((xlo, xhi))
885  self.normalize()
def append (   self,
  x 
)

Definition at line 856 of file mhlib.py.

References IntSet.pairs.

857  def append(self, x):
858  for i in range(len(self.pairs)):
859  lo, hi = self.pairs[i]
860  if x < lo: # Need to insert before
861  if x+1 == lo:
862  self.pairs[i] = (x, hi)
863  else:
864  self.pairs.insert(i, (x, x))
865  if i > 0 and x-1 == self.pairs[i-1][1]:
866  # Merge with previous
867  self.pairs[i-1:i+1] = [
868  (self.pairs[i-1][0],
869  self.pairs[i][1])
870  ]
871  return
872  if x <= hi: # Already in set
873  return
874  i = len(self.pairs) - 1
875  if i >= 0:
876  lo, hi = self.pairs[i]
877  if x-1 == hi:
878  self.pairs[i] = lo, x
879  return
880  self.pairs.append((x, x))
def clone (   self)

Definition at line 840 of file mhlib.py.

References IntSet.pairs.

841  def clone(self):
842  new = IntSet()
843  new.pairs = self.pairs[:]
844  return new
def contains (   self,
  x 
)

Definition at line 851 of file mhlib.py.

References IntSet.pairs.

852  def contains(self, x):
853  for lo, hi in self.pairs:
854  if lo <= x <= hi: return 1
855  return 0
def fromlist (   self,
  list 
)

Definition at line 836 of file mhlib.py.

References ParsingError.append(), Unmarshaller.append, Unpickler.append, and IntSet.append().

837  def fromlist(self, list):
838  for i in list:
839  self.append(i)
def fromstring (   self,
  data 
)

Definition at line 886 of file mhlib.py.

References IntSet.normalize(), IntSet.pairs, IntSet.rng, and IntSet.sep.

887  def fromstring(self, data):
888  new = []
889  for part in data.split(self.sep):
890  list = []
891  for subp in part.split(self.rng):
892  s = subp.strip()
893  list.append(int(s))
894  if len(list) == 1:
895  new.append((list[0], list[0]))
896  elif len(list) == 2 and list[0] <= list[1]:
897  new.append((list[0], list[1]))
898  else:
899  raise ValueError, 'bad data passed to IntSet'
900  self.pairs = self.pairs + new
901  self.normalize()
902 
903 
904 # Subroutines to read/write entries in .mh_profile and .mh_sequences
def max (   self)

Definition at line 848 of file mhlib.py.

References IntSet.pairs.

849  def max(self):
850  return self.pairs[-1][-1]
def min (   self)

Definition at line 845 of file mhlib.py.

References IntSet.pairs.

846  def min(self):
847  return self.pairs[0][0]
def normalize (   self)

Definition at line 809 of file mhlib.py.

References IntSet.max(), and IntSet.pairs.

810  def normalize(self):
811  self.pairs.sort()
812  i = 1
813  while i < len(self.pairs):
814  alo, ahi = self.pairs[i-1]
815  blo, bhi = self.pairs[i]
816  if ahi >= blo-1:
817  self.pairs[i-1:i+1] = [(alo, max(ahi, bhi))]
818  else:
819  i = i+1
def reset (   self)

Definition at line 796 of file mhlib.py.

References IntSet.pairs.

797  def reset(self):
798  self.pairs = []
def tolist (   self)

Definition at line 829 of file mhlib.py.

References IntSet.pairs.

830  def tolist(self):
831  l = []
832  for lo, hi in self.pairs:
833  m = range(lo, hi+1)
834  l = l + m
835  return l
def tostring (   self)

Definition at line 820 of file mhlib.py.

References IntSet.pairs, IntSet.rng, and IntSet.sep.

821  def tostring(self):
822  s = ''
823  for lo, hi in self.pairs:
824  if lo == hi: t = `lo`
825  else: t = `lo` + self.rng + `hi`
826  if s: s = s + (self.sep + t)
827  else: s = t
828  return s

Field Documentation

pairs

Definition at line 791 of file mhlib.py.

rng

Definition at line 793 of file mhlib.py.

sep

Definition at line 792 of file mhlib.py.


The documentation for this class was generated from the following file: