diff --git a/bin/iteratortools.py b/bin/iteratortools.py new file mode 100644 index 0000000000000000000000000000000000000000..234eca29cc4ba388971b552b07dddaa9d2a15134 --- /dev/null +++ b/bin/iteratortools.py @@ -0,0 +1,162 @@ + + +class Iterate: + @classmethod + def toEnd(cls, it): + try: + while True: + it.next() + except StopIteration: + pass + + @classmethod + def until(cls, it, stop, withLast=True): + uit = UntilIterator(it, stop, withLast) + Iterate.toEnd(uit) + + + + +class FileIterator: + def __init__(self, file): + self.file = file + + def __iter__(self): + return self + + def next(self): + l = self.file.readline() + if l=='': + raise StopIteration + return l + + + + +class BufferedIterator: + + def __init__(self, it, size=1): + self.it = it + self.size = size + self.buffer = [] + self.pos = -1 + self.stoped = False + + def __iter__(self): + return self + + def next(self): + if self.stoped: + raise StopIteration + if self.pos == len(self.buffer)-1: + try: + next = self.it.next() + self.buffer.append(next) + except StopIteration: + self.stoped = True + raise StopIteration + if len(self.buffer)>self.size: + self.buffer.pop(0) + self.pos -= 1 + self.pos += 1 + return self.buffer[self.pos] + + def back(self): + if self.pos<0: + raise NameError('You can only go back '+str(size)+' steps!') + self.pos -= 1 + self.stoped = False + return self.pos+1 + + def end(self): + try: + self.next() + except StopIteration: + self.stoped = True + return True + self.back() + return False + + + + +class UntilIterator: + + def __init__(self, it, stop, withLast=True): + self.it = it + self.stop = stop + self.withLast = withLast + self.stoped = False + + def __iter__(self): + return self + + def next(self): + if self.stoped: + raise StopIteration + + try: + item = self.it.next() + except StopIteration: + self.stoped = True + raise StopIteration + + if self.stop(item): + self.stoped = True + if self.withLast: + return item + else: + self.it.back() + raise StopIteration + return item + + + + +class FilterIterator: + + def __init__(self, it, include): + self.include=include + self.it = it + self.stoped = False + + def __iter__(self): + return self + + def next(self): + if self.stoped: + raise StopIteration + + try: + item = self.it.next() + while not(self.include(item)): + item = self.it.next() + return item + except StopIteration: + self.stoped = True + raise StopIteration + + + + +class HierarchyIterator: + + def __init__(self, it, up, down): + self.it = it + self.up = up + self.down = down + self.level = 0 + + def __iter__(self): + return self + + def next(self): + item = self.it.next() + if self.up(item): + self.level += 1 + r = (item, self.level) + if self.down(item): + self.level -= 1 + if self.level < 0: + self.level = 0 + return r diff --git a/bin/regexptools.py b/bin/regexptools.py new file mode 100644 index 0000000000000000000000000000000000000000..c7ed6cf2c98a0df31b034f6adc9cd4b58f6c0388 --- /dev/null +++ b/bin/regexptools.py @@ -0,0 +1,26 @@ +import re + + +def regexpMatchFunction(regexp): + return lambda s : bool(re.compile(regexp).search(s)) + +def regexpInverseMatchFunction(regexp): + return lambda s : not(bool(re.compile(regexp).search(s))) + + + +class NumericRegexp: + unsigned_int = '[0-9]+' + int = '[+-]?'+unsigned_int + + unsigned_fraction = '\.'+unsigned_int + fraction = '[+-]?'+unsigned_fraction + float = '('+int+'('+unsigned_fraction+')?|'+int+'\.|'+fraction+')' + + nan = '[nN][aA][nN]' + inf = '[+-]?[iI][nN][fF]' + + exp = float+'([eE]'+float+')?' + num = '('+nan+'|'+inf+'|'+exp+')' + + diff --git a/bin/treetools.py b/bin/treetools.py new file mode 100644 index 0000000000000000000000000000000000000000..9b8f1313cadb7f088e556e0f8499348500fa1636 --- /dev/null +++ b/bin/treetools.py @@ -0,0 +1,39 @@ +import sys + + +class Tree(list): + def printHierarchic(self,prefix=' ',level=0,maxlevel=100): + for child in self: + if isinstance(child, Tree): + if (level<maxlevel): + child.printHierarchic(prefix,level+1,maxlevel) + else: + for i in range(0,level): + sys.stdout.write(prefix) + sys.stdout.write(child.strip()+'\n') + + + + +class TreeGenerator: + + def __init__(self): + self.tree = Tree() + self.hierarchy = [self.tree] + + def feed(self,levelitem): + item = levelitem[0] + level = levelitem[1] + + # grow hierarchy to current level if neccessary + while len(self.hierarchy)-1 < level: + newlevel = Tree() + self.hierarchy[-1].append(newlevel) + self.hierarchy.append(newlevel) + + # shrink hierarchy to current level if neccessary + while len(self.hierarchy)-1 > level: + self.hierarchy.pop() + + self.hierarchy[-1].append(item) +