Skip to content

Commit

Permalink
Fix wrong symbol defined in input excel for Q27 and the newton metre …
Browse files Browse the repository at this point in the history
…per metre definition. (#7)
  • Loading branch information
jakubsemerak authored Nov 9, 2023
1 parent f378301 commit 529f475
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .run/parser.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="C:\repos\quadient\unece-units\python\src\unece_excel_parser" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/python/src/unece_excel_parser" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/python/src/unece_excel_parser/unece_excel_parser.py" />
<option name="SCRIPT_NAME" value="unece_excel_parser.py" />
<option name="PARAMETERS" value="-rec20 files/rec20_Rev17e-2021.xlsx -rec21 files/rec21_Rev12e_Annex-V-VI_2021.xls" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ Units library supporting UNECE rec. 20 and rec. 21 standards
# Development

- developed on python `3.12.0`
- mainly using `pint` library for linking units
- in the project root folder:
- `cd python`
- create virtual environment `python -m venv venv`
- activate virtual environment, for example on windows run `.\venv\Scripts\Activate.ps1`
- run `pip install -e src/unece_excel_parser` to install dependencies & fix imports
- run `pip install -e src/unece_excel_parser[dev]` to install dev dependencies (for running tests)
- (optional) set python interpreter in dev IDE to point to python.exe in venv folder
- `cd python`
- create virtual environment `python -m venv venv`
- activate virtual environment, for example on windows run `.\venv\Scripts\Activate.ps1`
- run `pip install -e src/unece_excel_parser` to install dependencies & fix imports
- run `pip install -e src/unece_excel_parser[dev]` to install dev dependencies (for running tests)
- (optional) set python interpreter in dev IDE to point to python.exe in venv folder

## Run

Expand All @@ -24,4 +25,13 @@ From `python/src/unece_excel_parser` folder run:
UNECE standards are located on https://unece.org/trade/uncefact/cl-recommendations. Basic description of the UNECE
standard is located on https://unece.org/sites/default/files/2023-10/Rec20_Rev6e_2009.pdf.

Also the Excel files and the PDF explaining the data in the standard are located in the `python/src/files` folder.
Also the Excel files and the PDF explaining the data in the standard are located in the `python/src/files` folder.

# Known issues

The `pint` library does reduce the units and this could cause some unexpected
behaviour where units get reduced, see https://github.com/hgrecco/pint/issues/551. For example `N * m / m` is reduced
to `N`.

The input excel for rec. 20 standard contains a lot of bugs, inconsistencies and typos and we are trying to correct
them, but it could be not perfect.
1 change: 1 addition & 0 deletions python/src/unece_excel_parser/lib/pint_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ def __create_registry() -> UnitRegistry:
registry.define('parts_per_billion_US = 1 ppb = ppb')
registry.define("beats_per_minute = 1 BPM = BPM")
registry.define("month = 1 mo = mo")
registry.define("Nm = newton * meter = N * m")
registry.case_sensitive = True
return registry
16 changes: 11 additions & 5 deletions python/src/unece_excel_parser/lib/unit.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from pint import errors

from lib.category import Category
from lib.category import Category
from lib.conversion_factor import ConversionFactor
from lib.normalizer import Normalizer
from lib.pint_registry import PintRegistryManager
from lib.state import State
from pint import errors


class Unit:
_symbol_corrections_by_common_code: dict[(str, str | None)] = {
"Q27": "(N * m) / m",
}

def __init__(self, state: State, common_code: str, name: str, description: str, categories: Category, symbol: str,
parsed_symbol: str, conversion_factor: ConversionFactor):
self.state = state
Expand Down Expand Up @@ -50,11 +53,14 @@ def parse(state, common_code, name, description, category, symbol, conversion_fa
Normalizer.normalize_unit_name(name),
Normalizer.normalize_value(description),
Category.parse_categories(Normalizer.normalize_value(category)),
symbol, Unit.__parse_unit_reference(symbol),
symbol, Unit.__parse_unit_reference(symbol, common_code),
ConversionFactor.parse(conversion_factor))

@staticmethod
def __parse_unit_reference(symbol: str) -> str | None:
def __parse_unit_reference(symbol: str, common_code: str) -> str | None:
if common_code in Unit._symbol_corrections_by_common_code:
symbol = Unit._symbol_corrections_by_common_code[common_code]

if symbol is None:
return None

Expand Down
67 changes: 36 additions & 31 deletions python/src/unece_excel_parser/parsedUneceUnits.json
Original file line number Diff line number Diff line change
Expand Up @@ -1556,8 +1556,7 @@
"factor": 1.0,
"parsedSymbol": "newton / meter",
"commonCodeReferences": [
"M34",
"Q27"
"M34"
]
},
"parsedSymbol": "newton / meter",
Expand Down Expand Up @@ -4010,7 +4009,8 @@
"factor": 9.80665,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "force_kilogram",
Expand Down Expand Up @@ -4180,7 +4180,8 @@
"factor": 1000,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "kilonewton",
Expand Down Expand Up @@ -4254,7 +4255,8 @@
"factor": 9.80665,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": null,
Expand Down Expand Up @@ -4608,7 +4610,8 @@
"factor": 1000000,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "meganewton",
Expand Down Expand Up @@ -4924,7 +4927,8 @@
"factor": 1e-06,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "micronewton",
Expand Down Expand Up @@ -5560,7 +5564,8 @@
"factor": 0.001,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "millinewton",
Expand All @@ -5587,8 +5592,7 @@
"parsedSymbol": "newton / meter",
"commonCodeReferences": [
"4P",
"M34",
"Q27"
"M34"
]
},
"parsedSymbol": "millinewton / meter",
Expand Down Expand Up @@ -6491,7 +6495,8 @@
"factor": 4.448222,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "force_pound",
Expand Down Expand Up @@ -9035,7 +9040,8 @@
"factor": 1e-05,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "dyne",
Expand Down Expand Up @@ -9068,8 +9074,7 @@
"parsedSymbol": "newton / meter",
"commonCodeReferences": [
"4P",
"M34",
"Q27"
"M34"
]
},
"parsedSymbol": "dyne / centimeter",
Expand Down Expand Up @@ -11588,7 +11593,7 @@
"parsedSymbol": "kilogram * meter ** 2 / radian / second ** 2",
"commonCodeReferences": null
},
"parsedSymbol": "number_meter / degree",
"parsedSymbol": "Nm / degree",
"categories": "LEVEL_1_NORMATIVE|LEVEL_1_NORMATIVE_COMMON"
},
{
Expand Down Expand Up @@ -19971,7 +19976,8 @@
"factor": 0.2780139,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "force_ounce",
Expand Down Expand Up @@ -20829,7 +20835,8 @@
"factor": 8896.443,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": null,
Expand Down Expand Up @@ -21453,8 +21460,7 @@
"parsedSymbol": "newton / meter",
"commonCodeReferences": [
"4P",
"M34",
"Q27"
"M34"
]
},
"parsedSymbol": "newton / centimeter",
Expand Down Expand Up @@ -21604,8 +21610,7 @@
"factor": 1.0,
"parsedSymbol": "newton / meter",
"commonCodeReferences": [
"4P",
"Q27"
"4P"
]
},
"parsedSymbol": "newton / meter",
Expand Down Expand Up @@ -22286,7 +22291,8 @@
"factor": 4448.222000000001,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "kip",
Expand All @@ -22302,7 +22308,8 @@
"factor": 0.138255,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": "poundal",
Expand Down Expand Up @@ -22332,7 +22339,8 @@
"factor": 0.00980665,
"parsedSymbol": "newton",
"commonCodeReferences": [
"NEW"
"NEW",
"Q27"
]
},
"parsedSymbol": null,
Expand Down Expand Up @@ -23623,8 +23631,7 @@
"parsedSymbol": "newton / meter",
"commonCodeReferences": [
"4P",
"M34",
"Q27"
"M34"
]
},
"parsedSymbol": "kilonewton / meter",
Expand All @@ -23641,8 +23648,7 @@
"parsedSymbol": "newton / meter",
"commonCodeReferences": [
"4P",
"M34",
"Q27"
"M34"
]
},
"parsedSymbol": "poundal / inch",
Expand All @@ -23659,8 +23665,7 @@
"parsedSymbol": "newton / meter",
"commonCodeReferences": [
"4P",
"M34",
"Q27"
"M34"
]
},
"parsedSymbol": "force_pound / yard",
Expand Down Expand Up @@ -27341,7 +27346,7 @@
"M77"
]
},
"parsedSymbol": "newton / meter",
"parsedSymbol": "newton",
"categories": "LEVEL_3_INFORMATIVE"
},
{
Expand Down
9 changes: 7 additions & 2 deletions python/tests/unece_excel_parser/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
from inflection import camelize

from constants import *
from lib.category import Category
from lib.conversion_factor import ConversionFactor
from lib.state import State

from constants import *


@pytest.mark.parametrize(
"text_input",
Expand Down Expand Up @@ -75,3 +75,8 @@ def test_to_dict_contains_all_public_properties():
def test_parse_unit_reference_does_parse_symbol_properly(text_input: str | None, expected_parsed_symbol: str | None):
unit = Unit.parse(STATE, COMMON_CODE, NAME, DESCRIPTION, CATEGORY, text_input, CONVERSION_FACTOR)
assert unit.parsed_symbol == expected_parsed_symbol


def test_parse_unit_reference_does_replace_symbol_for_defined_common_codes():
unit = Unit.parse(STATE, "Q27", NAME, DESCRIPTION, CATEGORY, "N·m/m²", CONVERSION_FACTOR)
assert unit.parsed_symbol == "newton"

0 comments on commit 529f475

Please sign in to comment.