Skip to content

Commit

Permalink
Added int() and float() methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
slightlynybbled authored and jjonesAtMoog committed May 31, 2017
1 parent 8cc9b7c commit 6a5bf5c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
28 changes: 28 additions & 0 deletions engineering_notation/engineering_notation.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ def __str__(self):
"""
return self.__repr__()

def __int__(self):
"""
Implements the 'int()' method
:return:
"""
return int(self.eng_num)

def __float__(self):
"""
Implements the 'float()' method
:return:
"""
return float(self.eng_num)

def __add__(self, other):
"""
Add two engineering numbers, with units
Expand Down Expand Up @@ -294,6 +308,20 @@ def __str__(self, eng=True, context=None):
"""
return self.__repr__()

def __int__(self):
"""
Implements the 'int()' method
:return:
"""
return int(self.number)

def __float__(self):
"""
Implements the 'float()' method
:return:
"""
return float(self.number)

def __add__(self, other):
"""
Add two engineering numbers
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.2.3'
__version__ = '0.3.0'
20 changes: 20 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ def test_enum_le():
assert EngNumber('220k') <= 220000


def test_enum_to_int():
assert int(EngNumber('220k')) == 220000
assert int(EngNumber('220m')) == 0


def test_enum_to_float():
assert float(EngNumber('220k')) == 220000.0
assert float(EngNumber('220m')) == 0.220


''' tests for EngUnit()'''


Expand Down Expand Up @@ -212,3 +222,13 @@ def test_le():
EngUnit('220kohm') >= 219000
with pytest.raises(AttributeError):
219000 >= EngUnit('220kohm')


def test_to_int():
assert int(EngUnit('220k')) == 220000
assert int(EngUnit('220m')) == 0


def test_to_float():
assert float(EngUnit('220k')) == 220000.0
assert float(EngUnit('220m')) == 0.220

0 comments on commit 6a5bf5c

Please sign in to comment.