Skip to content

Commit

Permalink
Fix #22
Browse files Browse the repository at this point in the history
  • Loading branch information
slightlynybbled committed Jan 13, 2023
1 parent f1a2a82 commit e7f29bc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions engineering_notation/engineering_notation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion engineering_notation/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.7.0'
__version__ = '0.8.0'
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions tests/test_engnum.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def test_enum_eq():
assert not (EngNumber('220k') == object)



def test_enum_gt():
# positive_numbers
assert EngNumber('220k') > 219000
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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'

Expand Down

0 comments on commit e7f29bc

Please sign in to comment.