chiark / gitweb /
Speedup gcodeInterpreter a bit by compiling the regexp
authordaid <daid303@gmail.com>
Thu, 22 Mar 2012 14:30:28 +0000 (15:30 +0100)
committerdaid <daid303@gmail.com>
Thu, 22 Mar 2012 14:30:28 +0000 (15:30 +0100)
SkeinPyPy/newui/gcodeInterpreter.py

index 79c278dcc9d94ea365429fec277675ddb40dfe4f..d4fc68f3fe796ba0920336dfd84d4925a4cac777 100644 (file)
@@ -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)
+