Skip to content

Commit

Permalink
Unit tests + integration test. Minor refactor. TODO CR
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubsemerak committed Nov 7, 2023
1 parent e16e3d8 commit 3757f24
Show file tree
Hide file tree
Showing 11 changed files with 2,495 additions and 2,203 deletions.
10 changes: 6 additions & 4 deletions python/src/unece_excel_parser/lib/pint_registry.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
from pint import UnitRegistry
from typing import Optional

from pint import UnitRegistry


class PintRegistryManager:
_instance = None
_instance: Optional['PintRegistryManager'] = None

def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.registry = PintRegistryManager.create_registry()
cls._instance.registry = PintRegistryManager.__create_registry()
return cls._instance

def get_registry(self) -> UnitRegistry:
return self.registry

@staticmethod
def create_registry() -> UnitRegistry:
def __create_registry() -> UnitRegistry:
registry = UnitRegistry()
registry.define("E = 1 erlang = E")
registry.define("R = 1 roentgen = R")
Expand Down
13 changes: 11 additions & 2 deletions python/src/unece_excel_parser/lib/state.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from enum import Enum

from lib.normalizer import Normalizer


# src: https://unece.org/sites/default/files/2023-10/Rec20_Rev6e_2009.pdf
class State(Enum):
NOT_DEFINED = None
ACTIVE = None
# Added. New unit added in this release of the code list
ADDED = '+'
# Changed name. Changes to the unit name in this release of the code list
Expand All @@ -20,7 +22,14 @@ class State(Enum):
REINSTATED = '='

@staticmethod
def parse(state_string: str) -> 'State':
def parse(state_string: str | None) -> 'State':
state_string = Normalizer.normalize_value(state_string)

if state_string is None:
return State.ACTIVE
elif len(state_string) != 1:
raise ValueError(f"Invalid input: Only one character allowed in a state value but received: {state_string}")

if state_string in states_dictionary:
return states_dictionary[state_string]

Expand Down
26 changes: 8 additions & 18 deletions python/src/unece_excel_parser/lib/unit.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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:
Expand All @@ -31,6 +30,11 @@ def to_dict(self):
"categories": self.categories.name if self.categories is not None else None,
}

def __eq__(self, other):
if isinstance(other, Unit):
return self.state == other.state and self.common_code == other.common_code and self.name == other.name and self.description == other.description and self.categories == other.categories and self.symbol == other.symbol and self.parsed_symbol == other.parsed_symbol and self.conversion_factor == other.conversion_factor
return False

def __repr__(self):
return str(self.to_dict())

Expand All @@ -41,27 +45,13 @@ def parse(state, common_code, name, description, category, symbol, conversion_fa

symbol = Normalizer.normalize_value(symbol)

return Unit(Unit.__parse_state(state), Normalizer.normalize_value(common_code),
return Unit(State.parse(state), Normalizer.normalize_value(common_code),
Normalizer.normalize_unit_name(name),
Normalizer.normalize_value(description),
Category.parse_categories(Normalizer.normalize_value(category)),
symbol, Unit.__parse_unit_reference(symbol),
ConversionFactor.parse(conversion_factor))

@staticmethod
def __parse_state(state_string: str) -> State:
state_string = Normalizer.normalize_value(state_string)

if state_string is None:
return State.NOT_DEFINED

if len(state_string) == 0:
return State.NOT_DEFINED
elif len(state_string) != 1:
raise ValueError(f"Invalid input: Only one character allowed in a state value but received: {state_string}")

return State.parse(state_string)

@staticmethod
def __parse_unit_reference(symbol: str) -> str | None:
if symbol is None:
Expand Down
10 changes: 8 additions & 2 deletions python/src/unece_excel_parser/lib/units_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ class UnitsAnalyzer:
def __init__(self):
self._processed_symbols_info: Dict[str, list[SymbolInfo]] = {}
self._unit_common_codes_by_parsed_symbol: Dict[str, list[str]] = {}
self._duplicated_symbols: [str] = []
self._duplicated_symbols: set[str] = set()
self._processed_common_codes: set[str] = set()
self._unlinked_parsed_symbols_info: list[SymbolInfo] = []
self._referenced_unit_common_codes: set[str] = set()

def add_unit(self, unit: Unit):
if unit.common_code in self._processed_common_codes:
raise ValueError(f"Duplicated commonCode '{unit.common_code}'.")

self._processed_common_codes.add(unit.common_code)

if unit.symbol is not None:
if unit.symbol in self._processed_symbols_info:
self._duplicated_symbols.append(unit.symbol)
self._duplicated_symbols.add(unit.symbol)

self._processed_symbols_info.setdefault(unit.symbol, []).append(
SymbolInfo(unit.common_code, unit.parsed_symbol))
Expand Down
Loading

0 comments on commit 3757f24

Please sign in to comment.