diff --git a/engineering_notation/engineering_notation.py b/engineering_notation/engineering_notation.py index f48f2f0..71ba4c0 100644 --- a/engineering_notation/engineering_notation.py +++ b/engineering_notation/engineering_notation.py @@ -39,7 +39,7 @@ class EngUnit: Represents an engineering number, complete with units """ def __init__(self, value, - precision=2, significant=0): + precision=2, significant=0, unit: str = None): """ Initialize engineering with units :param value: the desired value in the form of a string, int, or float @@ -48,7 +48,7 @@ def __init__(self, value, if given, significant takes precendence over precision """ suffix_keys = [key for key in _suffix_lookup.keys() if key != ''] - self.unit = None + self.unit = unit if isinstance(value, str): # parse the string into unit and engineering number @@ -61,7 +61,7 @@ def __init__(self, value, else: break - if len(value) >= v_index: + if self.unit is None and len(value) >= v_index: self.unit = value[v_index:] self.eng_num = EngNumber(new_value, precision, significant) diff --git a/engineering_notation/version.py b/engineering_notation/version.py index a71c5c7..32a90a3 100644 --- a/engineering_notation/version.py +++ b/engineering_notation/version.py @@ -1 +1 @@ -__version__ = '0.7.0' +__version__ = '0.8.0' diff --git a/readme.md b/readme.md index 8b5010f..856bb3f 100644 --- a/readme.md +++ b/readme.md @@ -94,6 +94,7 @@ Additionally, since there are 'reserved' letters for sizing the number, you must 2mm # <<< this value equivalent to "0.002m" >>> EngUnit('2meter') 2meter # <<< this value is equivalent to "0.002eter", the "m" was used to scale the unit! +>>> EngUnit('2', unit='meter') # <<< this will work better ``` # Contributions diff --git a/tests/test_engnum.py b/tests/test_engnum.py index c0891c2..a37aa35 100644 --- a/tests/test_engnum.py +++ b/tests/test_engnum.py @@ -191,7 +191,6 @@ def test_enum_eq(): assert not (EngNumber('220k') == object) - def test_enum_gt(): # positive_numbers assert EngNumber('220k') > 219000 @@ -307,9 +306,15 @@ def test_to_str(): assert str(EngUnit('220ohm') == '220ohm') # negative_numbers - assert str(EngUnit('-220') == '-220') + assert str(EngUnit('-220') == '-220') assert str(EngUnit('-220ohm') == '-220ohm') + assert EngUnit('220ohm').unit == 'ohm' + assert EngUnit('220', unit='ohm').unit == 'ohm' + + assert EngUnit('2m', unit='meter').unit == 'meter' + assert EngUnit('2m', unit='meter').eng_num == EngNumber('2m') + def test_to_str_large(): # positive_numbers @@ -320,8 +325,8 @@ def test_to_str_large(): assert str(EngUnit(220001.25)) == '220k' # negative_numbers - assert str(EngUnit('-220kHz')) == '-220kHz' - assert str(EngUnit('-220000')) == '-220k' + assert str(EngUnit('-220kHz')) == '-220kHz' + assert str(EngUnit('-220000')) == '-220k' assert str(EngUnit(-220000)) == '-220k' assert str(EngUnit(-220000.00)) == '-220k' assert str(EngUnit(-220001.25)) == '-220k' @@ -335,8 +340,8 @@ def test_to_str_small(): assert str(EngUnit(0.220000125)) == '220m' # negative_numbers - assert str(EngUnit('-220mohm')) == '-220mohm' - assert str(EngUnit('-0.220')) == '-220m' + assert str(EngUnit('-220mohm')) == '-220mohm' + assert str(EngUnit('-0.220')) == '-220m' assert str(EngUnit(-0.220)) == '-220m' assert str(EngUnit(-0.220000125)) == '-220m'