Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't remove decimal from floats #20

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@

# build
build/*
src/pygcode.egg-info/*
dist/pygcode-*.egg
46 changes: 0 additions & 46 deletions src/pygcode.egg-info/PKG-INFO

This file was deleted.

21 changes: 0 additions & 21 deletions src/pygcode.egg-info/SOURCES.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/pygcode.egg-info/dependency_links.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/pygcode.egg-info/not-zip-safe

This file was deleted.

3 changes: 0 additions & 3 deletions src/pygcode.egg-info/requires.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/pygcode.egg-info/top_level.txt

This file was deleted.

154 changes: 81 additions & 73 deletions src/pygcode/dialects/linuxcnc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@

# ======================== WORDS ========================

REGEX_FLOAT = re.compile(r'^\s*-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float
REGEX_INT = re.compile(r'^\s*-?\d+')
REGEX_NUMBER = re.compile(r'^\s*-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float
REGEX_POSITIVEINT = re.compile(r'^\s*\d+')
REGEX_CODE = re.compile(r'^\s*\d+(\.\d)?') # float, but can't be negative

# Value cleaning functions
def _clean_codestr(value):
if value < 10:
return "0%g" % value
return "%g" % value
def CLASS_NUMBER(value):
if isinstance(value, (int, float)):
return value
if '.' in value:
return float(value)
return int(value)

# Value cleaning functions
CLEAN_NONE = lambda v: v
CLEAN_FLOAT = lambda v: "{0:g}".format(round(v, 3))
CLEAN_CODE = _clean_codestr

def CLEAN_NUMBER(v):
if isinstance(v, int):
return str(v)
fstr = "{0:g}".format(round(v, 3)) # FIXME bad practice for inches
if '.' not in fstr:
return fstr + '.'
return fstr
CLEAN_CODE = lambda v: '{:02}'.format(v)
CLEAN_INT = lambda v: "%g" % v

WORD_MAP = {
Expand All @@ -40,74 +48,74 @@ def _clean_codestr(value):

# Rotational Axes
'A': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Absolute or incremental position of A axis (rotational axis around X axis)",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'B': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Absolute or incremental position of B axis (rotational axis around Y axis)",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'C': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Absolute or incremental position of C axis (rotational axis around Z axis)",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'D': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Defines diameter or radial offset used for cutter compensation. D is used for depth of cut on lathes. It is used for aperture selection and commands on photoplotters.",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# Feed Rates
'E': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Precision feedrate for threading on lathes",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'F': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Feedrate",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# G-Codes
'G': WordType(
cls=float,
cls=CLASS_NUMBER,
value_regex=REGEX_CODE,
description="Address for preparatory commands",
clean_value=CLEAN_CODE,
clean_value=CLEAN_NONE,
),
# Tool Offsets
'H': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Defines tool length offset; Incremental axis corresponding to C axis (e.g., on a turn-mill)",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# Arc radius center coords
'I': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Defines arc center in X axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'J': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Defines arc center in Y axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'K': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Defines arc center in Z axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles, equal to L address.",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# Loop Count
'L': WordType(
Expand All @@ -118,10 +126,10 @@ def _clean_codestr(value):
),
# Miscellaneous Function
'M': WordType(
cls=float,
cls=CLASS_NUMBER,
value_regex=REGEX_CODE,
description="Miscellaneous function",
clean_value=CLEAN_CODE,
clean_value=CLEAN_NONE,
),
# Line Number
'N': WordType(
Expand All @@ -139,31 +147,31 @@ def _clean_codestr(value):
),
# Parameter (arbitrary parameter)
'P': WordType(
cls=float, # parameter is often an integer, but can be a float
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER, # parameter is often an integer, but can be a float
value_regex=REGEX_NUMBER,
description="Serves as parameter address for various G and M codes",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# Peck increment
'Q': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Depth to increase on each peck; Peck increment in canned cycles",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# Arc Radius
'R': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Defines size of arc radius, or defines retract height in milling canned cycles",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# Spindle speed
'S': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Defines speed, either spindle speed or surface speed depending on mode",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# Tool Selecton
'T': WordType(
Expand All @@ -174,41 +182,41 @@ def _clean_codestr(value):
),
# Incremental axes
'U': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Incremental axis corresponding to X axis (typically only lathe group A controls) Also defines dwell time on some machines (instead of 'P' or 'X').",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'V': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Incremental axis corresponding to Y axis",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'W': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Incremental axis corresponding to Z axis (typically only lathe group A controls)",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
# Linear Axes
'X': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Absolute or incremental position of X axis.",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'Y': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Absolute or incremental position of Y axis.",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
'Z': WordType(
cls=float,
value_regex=REGEX_FLOAT,
cls=CLASS_NUMBER,
value_regex=REGEX_NUMBER,
description="Absolute or incremental position of Z axis.",
clean_value=CLEAN_FLOAT,
clean_value=CLEAN_NUMBER,
),
}

Expand Down
Loading