Skip to content

Commit

Permalink
New test, revert pydot-use in CI, clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Jan 16, 2024
1 parent 1e4e80d commit b297462
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 24 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ucumvert is available as Python package from [PyPi](https://pypi.org/project/ucu
pip install ucumvert
```

To install the most recent code from git in developer mode including creation of a virtual environment (pip should be newer than 23.1) use:
To install the most recent code from git in developer mode including creation of a virtual environment use:

Linux

Expand All @@ -43,6 +43,7 @@ git clone https://github.com/dalito/ucumvert.git
cd ucumvert
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install -e .[dev]
```

Expand All @@ -53,6 +54,7 @@ git clone https://github.com/dalito/ucumvert.git
cd ucumvert
py -m venv .venv
.venv\Scripts\activate.bat
py -m pip install --upgrade pip
pip install -e .[dev]
```

Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ Changelog = "https://github.com/nfdi4cat/ucumvert/blob/main/CHANGELOG.md"
tests = [
"pytest",
"coverage",
"pydot",
]
lint = [
"black",
]
dev = [
"ucumvert[tests,lint]",
"ruff",
"openpyxl",
"ucumvert[tests,lint]",
"ruff",
"openpyxl",
"pydot",
]

[project.scripts]
Expand Down
4 changes: 1 addition & 3 deletions src/ucumvert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from ucumvert.parser import (
get_ucum_parser,
make_parse_tree_png,
update_lark_ucum_grammar_file,
)
from ucumvert.ucum_pint import (
Expand All @@ -22,7 +21,7 @@
__version__ = "0.0.0"
__version_tuple__ = (0, 0, 0)

try:
try: # pragma: no cover
import pydot # noqa: F401

HAS_PYDOT = True
Expand All @@ -31,7 +30,6 @@

__all__ = [
"get_ucum_parser",
"make_parse_tree_png",
"ucum_preprocessor",
"update_lark_ucum_grammar_file",
"UcumToPintTransformer",
Expand Down
10 changes: 3 additions & 7 deletions src/ucumvert/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import textwrap
from pathlib import Path

from lark import tree
from lark.exceptions import LarkError, UnexpectedInput, VisitError

import ucumvert
from ucumvert.parser import (
get_ucum_parser,
make_parse_tree_png,
update_lark_ucum_grammar_file,
)
from ucumvert.ucum_pint import UcumToPintTransformer, find_matching_pint_definitions
Expand All @@ -31,13 +31,9 @@ def interactive():
if ucum_code and (ucum_code in "qQ"):
break
try:
parsed_data = ucum_parser.parse(ucum_code)
if ucumvert.HAS_PYDOT:
parsed_data = make_parse_tree_png(
ucum_parser, ucum_code, filename="parse_tree.png"
)
print("Created visualization of parse tree (parse_tree.png).")
else:
parsed_data = ucum_parser.parse(ucum_code)
tree.pydot__tree_to_png(parsed_data, "parse_tree_unit.png")
print(parsed_data.pretty())
except UnexpectedInput as e:
print(e)
Expand Down
8 changes: 1 addition & 7 deletions src/ucumvert/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import textwrap
from pathlib import Path

from lark import Lark, Transformer, tree
from lark import Lark, Transformer

import ucumvert
from ucumvert.xml_util import (
Expand Down Expand Up @@ -175,9 +175,3 @@ def get_ucum_parser(grammar_file=None):
with grammar_file.open("r", encoding="utf8") as f:
ucum_grammar = f.read()
return Lark(ucum_grammar, start="main_term", strict=True)


def make_parse_tree_png(parser, data, filename="parse_tree_unit.png"):
parsed_data = parser.parse(data)
tree.pydot__tree_to_png(parsed_data, filename)
return parsed_data
2 changes: 0 additions & 2 deletions src/ucumvert/ucum_pint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from ucumvert.parser import (
get_ucum_parser,
make_parse_tree_png,
)
from ucumvert.xml_util import (
get_metric_units,
Expand Down Expand Up @@ -380,7 +379,6 @@ def run_examples(): # pragma: no cover
parser = get_ucum_parser()
for unit in test_ucum_units:
print("parsing ucum code:", unit)
make_parse_tree_png(unit, filename="parse_tree.png")
parsed_data = parser.parse(unit)
q = UcumToPintTransformer().transform(parsed_data)
print(f"Pint {q!r}")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_xm_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from ucumvert.xml_util import get_metric_units, get_non_metric_units, get_units


def test_get_units():
assert len(get_units()) == 303 # noqa: PLR2004
assert set(get_units()) == set(get_metric_units() + get_non_metric_units())

0 comments on commit b297462

Please sign in to comment.