From 751cadf562c2126e1873b672ef7eed8820509ae5 Mon Sep 17 00:00:00 2001 From: Mirochill <200482516+Mirochill@users.noreply.github.com> Date: Mon, 25 May 2026 20:53:53 +0200 Subject: [PATCH] fix: support unicode micro prefixes --- src/unitpy/definitions/ledger.py | 7 ++++--- src/unitpy/definitions/prefix.py | 2 +- src/unitpy/utils/parsing.py | 2 +- tests/test_define_unit.py | 9 ++++++++- tests/test_parsing_unit.py | 4 ++++ 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/unitpy/definitions/ledger.py b/src/unitpy/definitions/ledger.py index 97bdb29..72a691a 100644 --- a/src/unitpy/definitions/ledger.py +++ b/src/unitpy/definitions/ledger.py @@ -151,14 +151,15 @@ def add_derived_quantities(): def add_with_prefix(entry: Entry): for pre in ledger.prefixes.values(): + prefixed_abbrs = [abbr + entry.abbr for abbr in pre.abbr] ledger.add_unit( Entry( label=pre.label + entry.label, - abbr=pre.abbr[0] + entry.abbr, + abbr=prefixed_abbrs[0], base_unit=entry.base_unit, multiplier=entry.multiplier, prefix=pre, - additional_labels=get_additional_labels(pre.abbr, entry.additional_labels) + additional_labels=prefixed_abbrs[1:] + get_additional_labels(pre.abbr, entry.additional_labels) ) ) @@ -208,4 +209,4 @@ def add_extra_quantities(): add_extra_quantities() -ledger.add_in_main_duplicates() \ No newline at end of file +ledger.add_in_main_duplicates() diff --git a/src/unitpy/definitions/prefix.py b/src/unitpy/definitions/prefix.py index fd55a08..aa62f07 100644 --- a/src/unitpy/definitions/prefix.py +++ b/src/unitpy/definitions/prefix.py @@ -27,7 +27,7 @@ def __repr__(self): "femto": Prefix("femto", 1e-15, ("f",)), "pico": Prefix("pico", 1e-12, ("p",)), "nano": Prefix("nano", 1e-9, ("n",)), - "micro": Prefix("micro", 1e-6, ("u", "µ")), + "micro": Prefix("micro", 1e-6, ("u", "µ", "μ", "𝜇", "𝝁")), "milli": Prefix("milli", 1e-3, ("m",)), "centi": Prefix("centi", 1e-2, ("c",)), "deci": Prefix("deci", 1e-1, ("d",)), diff --git a/src/unitpy/utils/parsing.py b/src/unitpy/utils/parsing.py index 92c43a7..4a311ff 100644 --- a/src/unitpy/utils/parsing.py +++ b/src/unitpy/utils/parsing.py @@ -151,7 +151,7 @@ def get_number(self) -> int | float | None: return None def get_unit(self) -> Unit | None: - match = re.match(r'[a-zA-Z_]+', self.expression[self.pos:]) + match = re.match(r'[^+\-*/^()]+', self.expression[self.pos:]) if match: value = self.expression[self.pos:self.pos + match.end()] if value in ledger: diff --git a/tests/test_define_unit.py b/tests/test_define_unit.py index 40edc3d..ce30624 100644 --- a/tests/test_define_unit.py +++ b/tests/test_define_unit.py @@ -71,4 +71,11 @@ def test_unit_prefix(): def test_unit_prefix2(): u = unitpy.Unit("kPa") assert u.abbr == "kPa" - assert u.multiplier == 1000 \ No newline at end of file + assert u.multiplier == 1000 + + +@pytest.mark.parametrize("micro_symbol", ["µ", "μ", "𝜇", "𝝁"]) +def test_unit_micro_prefix_symbols(micro_symbol): + u = unitpy.Unit(f"{micro_symbol}m") + assert u == unitpy.Unit("um") + assert u.multiplier == 1e-6 diff --git a/tests/test_parsing_unit.py b/tests/test_parsing_unit.py index b72fe17..fb8f9ca 100644 --- a/tests/test_parsing_unit.py +++ b/tests/test_parsing_unit.py @@ -16,6 +16,10 @@ ["ns", {ledger.get_entry("ns"): 1}], ["mK", {ledger.get_entry("mK"): 1}], ["Gg", {ledger.get_entry("Gg"): 1}], + ["µm", {ledger.get_entry("um"): 1}], + ["μm", {ledger.get_entry("um"): 1}], + ["𝜇m", {ledger.get_entry("um"): 1}], + ["𝝁m", {ledger.get_entry("um"): 1}], # division ["g/ml", {ledger.get_entry("g"): 1, ledger.get_entry("ml"): -1}],