fix parsing integer lines

master
Phuck 2 years ago
parent 0fbb5ee9e2
commit 1c8a05d068

@ -20,6 +20,16 @@ MINY = 200
MAXY = 1000
OOZA_HEADER_PATH = "ooza/config.g"
NUMBER_RE = r"(\d*)?(\.)?(\d*)?"
def get_number_re(prefix):
pattern = f"{prefix}{NUMBER_RE}"
return re.compile(pattern)
X_RE = get_number_re("X")
Y_RE = get_number_re("Y")
@dataclass
@ -31,14 +41,15 @@ class GCodeInfo:
def get_xy(line):
xmatch = re.search(r"X(\d*\.\d*)", line)
ymatch = re.search(r"Y(\d*\.\d*)", line)
if xmatch is None or ymatch is None:
return None, None
xval = yval = None
xmatch = X_RE.search(line)
ymatch = Y_RE.search(line)
if xmatch is not None:
xval = float(xmatch.group().strip("X"))
if ymatch is not None:
yval = float(ymatch.group().strip("Y"))
return xval, yval

@ -0,0 +1,23 @@
import unittest
from rewritegcode import get_xy
class TestRewriteGcode(unittest.TestCase):
def test_get_xy(self):
cases = [
("G1 X4.8 Y3.4", (4.8, 3.4)),
("G1 X400.8", (400.8, None)),
("G1 Y4.8", (None, 4.8)),
("G1 X4 Y3", (4, 3)),
("G1 X40.32 Y3", (40.32, 3)),
("G1 X.32 Y3", (.32, 3)),
]
for (line, expected) in cases:
with self.subTest(line=line, expected=expected):
actual = get_xy(line)
self.assertEqual(expected, actual)
if __name__ == "__main__":
unittest.main()
Loading…
Cancel
Save