From fd8888dc465eea96f3a6fbfda2e427d949ccf2ad Mon Sep 17 00:00:00 2001 From: SasoriOlkof Date: Tue, 21 Apr 2020 22:04:02 +0200 Subject: [PATCH 1/4] updated the organic name when creating the object updated the organic name when creating the object Now when you create a measurement object the unit is converted to the most optimal for example, if you declare Capacitance('0.0001 F'), it returns the object Capacitance('100 uF') I recommend calling the function self.update_org_name() after calculations or before printing the value on the screen --- measurement/base.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/measurement/base.py b/measurement/base.py index 236d30e..708719c 100644 --- a/measurement/base.py +++ b/measurement/base.py @@ -5,6 +5,7 @@ import inspect import warnings from functools import total_ordering +from math import floor, log10 from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union @@ -257,6 +258,20 @@ def __init__( self.unit = self._units[unit] self.unit.org_name = unit self.si_value = self.unit.to_si(value) + self.units_conversor = { float(v.factor):k for k,v in self._units.items() if len(k) <= 3 } + self.update_org_name() + + def update_org_name(self): + if self.si_value == 0: + self.unit.org_name = self.units_conversor[1.0] + else: + factor = float(10**int((floor(log10(self.si_value)/3)*3))) + if factor in self.units_conversor: + self.unit.org_name = self.units_conversor[factor] + elif factor > max(self.units_conversor): + self.unit.org_name = self.units_conversor[max(self.units_conversor)] + elif factor < min(self.units_conversor): + self.unit.org_name = self.units_conversor[min(self.units_conversor)] def __getattr__(self, name): try: From aff641c4c155107c9c762cb6d53f1dd74fbdd73a Mon Sep 17 00:00:00 2001 From: SasoriOlkof Date: Wed, 22 Apr 2020 10:52:27 +0200 Subject: [PATCH 2/4] added test for TestAbstractMeasure.update_org_name --- tests/test_base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_base.py b/tests/test_base.py index dce1951..48cdd8f 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -78,7 +78,10 @@ def test_get_symbols__unique_names(self): class TestAbstractMeasure: measure = Distance unit = "m" - + + def test_update_org_name(self): + assert str(Distance('0.001 m')) == "1 mm" + def test_repr(self): assert repr(Distance("1 km")) == 'Distance(metre="1E+3")' From 5eb8f9536c38ec0a7fe0d1d4e2c943c80d61647f Mon Sep 17 00:00:00 2001 From: SasoriOlkof Date: Wed, 22 Apr 2020 11:01:02 +0200 Subject: [PATCH 3/4] fixed back error --- measurement/base.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/measurement/base.py b/measurement/base.py index 708719c..dc5ef80 100644 --- a/measurement/base.py +++ b/measurement/base.py @@ -258,20 +258,22 @@ def __init__( self.unit = self._units[unit] self.unit.org_name = unit self.si_value = self.unit.to_si(value) - self.units_conversor = { float(v.factor):k for k,v in self._units.items() if len(k) <= 3 } + self.units_conversor = self.units_conversor = { + float(v.factor): k for k, v in self._units.items() if len(k) <= 3 + } self.update_org_name() - + def update_org_name(self): if self.si_value == 0: self.unit.org_name = self.units_conversor[1.0] else: - factor = float(10**int((floor(log10(self.si_value)/3)*3))) + factor = float(10 ** int((floor(log10(self.si_value) / 3) * 3))) if factor in self.units_conversor: self.unit.org_name = self.units_conversor[factor] elif factor > max(self.units_conversor): - self.unit.org_name = self.units_conversor[max(self.units_conversor)] + self.unit.org_name = self.units_conversor[max(self.units_conversor)] elif factor < min(self.units_conversor): - self.unit.org_name = self.units_conversor[min(self.units_conversor)] + self.unit.org_name = self.units_conversor[min(self.units_conversor)] def __getattr__(self, name): try: From 7b84c0d83169383468bae11ba869e1ade735214d Mon Sep 17 00:00:00 2001 From: SasoriOlkof Date: Wed, 22 Apr 2020 11:03:39 +0200 Subject: [PATCH 4/4] fixed black error --- tests/test_base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_base.py b/tests/test_base.py index 48cdd8f..7427c85 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -78,10 +78,10 @@ def test_get_symbols__unique_names(self): class TestAbstractMeasure: measure = Distance unit = "m" - + def test_update_org_name(self): - assert str(Distance('0.001 m')) == "1 mm" - + assert str(Distance("0.001 m")) == "1 mm" + def test_repr(self): assert repr(Distance("1 km")) == 'Distance(metre="1E+3")'