From a3936540d8c80e67b7f6cef7a43ac8eecce50f24 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Wed, 16 Dec 2015 15:24:26 +0100 Subject: [PATCH] Move escape characters to be a static class variable It is static and constant, so it won't need to initialise this dictionary every time it reads. Contributes to issue CURA-34. --- .../GCodeProfileReader/GCodeProfileReader.py | 17 +++++++++++------ plugins/GCodeWriter/GCodeWriter.py | 17 +++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/plugins/GCodeProfileReader/GCodeProfileReader.py b/plugins/GCodeProfileReader/GCodeProfileReader.py index ed37e93a18..f1111b453b 100644 --- a/plugins/GCodeProfileReader/GCodeProfileReader.py +++ b/plugins/GCodeProfileReader/GCodeProfileReader.py @@ -18,6 +18,17 @@ class GCodeProfileReader(ProfileReader): # compatibility, increment this version number! version = 1 + ## Dictionary that defines how characters are escaped when embedded in + # g-code. + # + # Note that the keys of this dictionary are regex strings. The values are + # not. + escape_characters = { + "\\\\": "\\", #The escape character. + "\\n": "\n", #Newlines. They break off the comment. + "\\r": "\r" #Carriage return. Windows users may need this for visualisation in their editors. + } + ## Initialises the g-code reader as a profile reader. def __init__(self): super().__init__() @@ -44,12 +55,6 @@ class GCodeProfileReader(ProfileReader): return None #Unescape the serialised profile. - escape_characters = { #Which special characters (keys) are replaced by what escape character (values). - #Note: The keys are regex strings. Values are not. - "\\\\": "\\", #The escape character. - "\\n": "\n", #Newlines. They break off the comment. - "\\r": "\r" #Carriage return. Windows users may need this for visualisation in their editors. - } escape_characters = dict((re.escape(key), value) for key, value in escape_characters.items()) pattern = re.compile("|".join(escape_characters.keys())) serialised = pattern.sub(lambda m: escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression. diff --git a/plugins/GCodeWriter/GCodeWriter.py b/plugins/GCodeWriter/GCodeWriter.py index a94735c2a2..98aa678ff8 100644 --- a/plugins/GCodeWriter/GCodeWriter.py +++ b/plugins/GCodeWriter/GCodeWriter.py @@ -16,6 +16,17 @@ class GCodeWriter(MeshWriter): # compatibility, increment this version number! version = 1 + ## Dictionary that defines how characters are escaped when embedded in + # g-code. + # + # Note that the keys of this dictionary are regex strings. The values are + # not. + escape_characters = { + "\\": "\\\\", #The escape character. + "\n": "\\n", #Newlines. They break off the comment. + "\r": "\\r" #Carriage return. Windows users may need this for visualisation in their editors. + } + def __init__(self): super().__init__() @@ -49,12 +60,6 @@ class GCodeWriter(MeshWriter): serialised = profile.serialise() #Escape characters that have a special meaning in g-code comments. - escape_characters = { #Which special characters (keys) are replaced by what escape character (values). - #Note: The keys are regex strings. Values are not. - "\\": "\\\\", #The escape character. - "\n": "\\n", #Newlines. They break off the comment. - "\r": "\\r" #Carriage return. Windows users may need this for visualisation in their editors. - } escape_characters = dict((re.escape(key), value) for key, value in escape_characters.items()) pattern = re.compile("|".join(escape_characters.keys())) serialised = pattern.sub(lambda m: escape_characters[re.escape(m.group(0))], serialised) #Perform the replacement with a regular expression.