From: daid Date: Thu, 22 Mar 2012 14:30:28 +0000 (+0100) Subject: Speedup gcodeInterpreter a bit by compiling the regexp X-Git-Tag: RC1~27 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=ef26c9029e4209f91703c86c66e5544851d56729;p=cura.git Speedup gcodeInterpreter a bit by compiling the regexp --- diff --git a/SkeinPyPy/newui/gcodeInterpreter.py b/SkeinPyPy/newui/gcodeInterpreter.py index 79c278dc..d4fc68f3 100644 --- a/SkeinPyPy/newui/gcodeInterpreter.py +++ b/SkeinPyPy/newui/gcodeInterpreter.py @@ -1,3 +1,6 @@ +from __future__ import absolute_import +import __init__ + import sys import math import threading @@ -8,6 +11,8 @@ from newui import util3d class gcode(): def __init__(self, filename): + self.regMatch = {} + fileSize = os.stat(filename).st_size filePos = 0 gcodeFile = open(filename, 'r') @@ -156,8 +161,10 @@ class gcode(): print "Extruded a total of: %d mm of filament" % (self.extrusionAmount) print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute) - def getCodeInt(self, str, id): - m = re.search(id + '([^\s]+)', str) + def getCodeInt(self, line, code): + if code not in self.regMatch: + self.regMatch[code] = re.compile(code + '([^\s]+)') + m = self.regMatch[code].search(line) if m == None: return None try: @@ -165,8 +172,10 @@ class gcode(): except: return None - def getCodeFloat(self, str, id): - m = re.search(id + '([^\s]+)', str) + def getCodeFloat(self, line, code): + if code not in self.regMatch: + self.regMatch[code] = re.compile(code + '([^\s]+)') + m = self.regMatch[code].search(line) if m == None: return None try: @@ -174,3 +183,7 @@ class gcode(): except: return None +if __name__ == '__main__': + for filename in sys.argv[1:]: + gcode(filename) +