From 3fb6e663087782953075d5862c1d701ae009422c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Fr=C3=ADvaldsk=C3=BD?= Date: Mon, 30 Oct 2023 15:53:55 +0100 Subject: [PATCH 1/2] 'split_params' now handles parameter values prefixed with a '+' sign. --- gcodeparser/gcode_parser.py | 2 +- test/test_split_params.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/gcodeparser/gcode_parser.py b/gcodeparser/gcode_parser.py index 09aaab4..2bd60df 100644 --- a/gcodeparser/gcode_parser.py +++ b/gcodeparser/gcode_parser.py @@ -114,7 +114,7 @@ def element_type(element: str): def split_params(line): - regex = r'((?!\d)\w+?)(".*"|(\d+\.?)+|-?\d*\.?\d*)' + regex = r'((?!\d)\w+?)(".*"|(\d+\.?)+|[-+]?\d*\.?\d*)' elements = re.findall(regex, line) params = {} for element in elements: diff --git a/test/test_split_params.py b/test/test_split_params.py index 9368982..5b79fa7 100644 --- a/test/test_split_params.py +++ b/test/test_split_params.py @@ -31,10 +31,16 @@ def test_split_string_with_semicolon_params(): def test_split_neg_int_params(): assert split_params(' P-0 S-1 X-10') == {'P': 0, 'S': -1, 'X': -10} +def test_split_positive_int_params(): + assert split_params(' P+0 S+1 X+10') == {'P': 0, 'S': 1, 'X': 10} + def test_split_neg_float_params(): assert split_params(' P-0.1 S-1.1345 X-10.0') == {'P': -0.1, 'S': -1.1345, 'X': -10.0} +def test_split_positive_float_params(): + assert split_params(' P+0.1 S+1.1345 X+10.0') == {'P': 0.1, 'S': 1.1345, 'X': 10.0} + def test_split_ip_params(): assert split_params('P192.168.0.1 S1') == {'P': '192.168.0.1', 'S': 1} From 0de949bc15df931f9e58a0aa71c4dfd940b96f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Fr=C3=ADvaldsk=C3=BD?= Date: Mon, 30 Oct 2023 15:31:01 +0000 Subject: [PATCH 2/2] Modified 'get_lines' to properly work with parameter values with explicit '+' sign --- gcodeparser/gcode_parser.py | 2 +- test/test_get_lines.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/gcodeparser/gcode_parser.py b/gcodeparser/gcode_parser.py index 2bd60df..b65cec4 100644 --- a/gcodeparser/gcode_parser.py +++ b/gcodeparser/gcode_parser.py @@ -76,7 +76,7 @@ def __init__(self, gcode: str, include_comments=False): def get_lines(gcode, include_comments=False): - regex = r'(?!; *.+)(G|M|T|g|m|t)(\d+)(([ \t]*(?!G|M|g|m)\w(".*"|([-\d\.]*)))*)[ \t]*(;[ \t]*(.*))?|;[ \t]*(.+)' + regex = r'(?!; *.+)(G|M|T|g|m|t)(\d+)(([ \t]*(?!G|M|g|m)\w(".*"|([-+\d\.]*)))*)[ \t]*(;[ \t]*(.*))?|;[ \t]*(.+)' regex_lines = re.findall(regex, gcode) lines = [] for line in regex_lines: diff --git a/test/test_get_lines.py b/test/test_get_lines.py index 2a50454..9df8826 100644 --- a/test/test_get_lines.py +++ b/test/test_get_lines.py @@ -26,6 +26,15 @@ def test_params(): assert get_lines('G1 X10 Y20')[0] == line +def test_params_with_explicit_positive_values(): + line = GcodeLine( + command=('G', 1), + params={'X': 10, 'Y': 20}, + comment='', + ) + assert get_lines('G1 X+10 Y+20')[0] == line + + def test_2_commands_line(): line1 = GcodeLine( command=('G', 91),