A memo for making useful tools with just a bit of Python. Python is easy and nice. Assuming editing of NC program.
--Read all the contents of the file and throw it into the list
def readAll(fileName):
with open(fileName, "r") as f
ls = f.read().split("\n")
return ls
# f = open(fileName, "r")
# ls = f.read().split("\n")
# f.close()
return ls
--Delete comments (characters in parentheses) with regular expressions
# 'O1000(SAMPLE)'
# → 'O1000'
import re
cmtptn = re.compile(r"\(.*?\)")
def removeComment(str):
return cmtptn.sub("", str)
――Perspective with regular expressions
# 'G01X50.Y-50.Z-10.F500'
# → ['G01', 'X50.', 'Y-50.', 'Z-10.', 'F500']
ptr = re.compile(r"([A-Z#][^A-Z]+)")
def parse(str):
p = ptr.findall(str)
# print p
return p
--Read the contents of the file received as an argument
import sys
if __name__ == "__main__":
prm = sys.argv
f1 = readAll(prm[1])
for line in f1:
print line
# line = removeComment(line)
# p = parse(line)
--Write to file
#It seems easier to redirect the printed one
# open/Use with to write (see readAll)
f = open(fname, "w")
f.write("foo")
f.close
--Dictionary
# coding: UTF-8
gcode = {
"G00": u"Positioning"
, "G01": u"Linear interpolation"
}
#When printing
print str(gcode).decode("unicode-escape")
--Only those that are included in the dictionary are parsed.
filtered = filter( lambda cd:gcode.has_key(cd) , parsed)
--Get display name from dictionary
dsp = map( lambda cd:gcode[cd], filtered)
print str(dsp).decode("unicode-escape")
--Meeting judgment
#It is delicate whether the judgment conditions are met
wptn = re.compile(r"M\d{3,}P\d{2,}")
def isWaitCode(str):
return wptn.match(str)
--Standard output without line breaks
def sysout(v):
sys.stdout.write(str(v))
#new line
def newline():
sysout("\n")
Recommended Posts