Skip to content

Commit

Permalink
reformatted slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Mar 3, 2020
1 parent 3cb208f commit 8cebf54
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions python_utils/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ def to_int(input_, default=0, exception=(ValueError, TypeError), regexp=None):
123
>>> to_int('abc123abc456', regexp=True)
123
>>> to_int('abc123', regexp=re.compile('(\d+)'))
>>> to_int('abc123', regexp=re.compile(r'(\d+)'))
123
>>> to_int('123abc', regexp=re.compile('(\d+)'))
>>> to_int('123abc', regexp=re.compile(r'(\d+)'))
123
>>> to_int('abc123abc', regexp=re.compile('(\d+)'))
>>> to_int('abc123abc', regexp=re.compile(r'(\d+)'))
123
>>> to_int('abc123abc456', regexp=re.compile('(\d+)'))
>>> to_int('abc123abc456', regexp=re.compile(r'(\d+)'))
123
>>> to_int('abc123', regexp='(\d+)')
>>> to_int('abc123', regexp=r'(\d+)')
123
>>> to_int('123abc', regexp='(\d+)')
>>> to_int('123abc', regexp=r'(\d+)')
123
>>> to_int('abc', regexp='(\d+)')
>>> to_int('abc', regexp=r'(\d+)')
0
>>> to_int('abc123abc', regexp='(\d+)')
>>> to_int('abc123abc', regexp=r'(\d+)')
123
>>> to_int('abc123abc456', regexp='(\d+)')
>>> to_int('abc123abc456', regexp=r'(\d+)')
123
>>> to_int('1234', default=1)
1234
Expand Down Expand Up @@ -110,23 +110,23 @@ def to_float(input_, default=0, exception=(ValueError, TypeError),
'123.00'
>>> '%.2f' % to_float('abc0.456', regexp=True)
'0.46'
>>> '%.2f' % to_float('abc123.456', regexp=re.compile('(\d+\.\d+)'))
>>> '%.2f' % to_float('abc123.456', regexp=re.compile(r'(\d+\.\d+)'))
'123.46'
>>> '%.2f' % to_float('123.456abc', regexp=re.compile('(\d+\.\d+)'))
>>> '%.2f' % to_float('123.456abc', regexp=re.compile(r'(\d+\.\d+)'))
'123.46'
>>> '%.2f' % to_float('abc123.46abc', regexp=re.compile('(\d+\.\d+)'))
>>> '%.2f' % to_float('abc123.46abc', regexp=re.compile(r'(\d+\.\d+)'))
'123.46'
>>> '%.2f' % to_float('abc123abc456', regexp=re.compile('(\d+(\.\d+|))'))
>>> '%.2f' % to_float('abc123abc456', regexp=re.compile(r'(\d+(\.\d+|))'))
'123.00'
>>> '%.2f' % to_float('abc', regexp='(\d+)')
>>> '%.2f' % to_float('abc', regexp=r'(\d+)')
'0.00'
>>> '%.2f' % to_float('abc123', regexp='(\d+)')
>>> '%.2f' % to_float('abc123', regexp=r'(\d+)')
'123.00'
>>> '%.2f' % to_float('123abc', regexp='(\d+)')
>>> '%.2f' % to_float('123abc', regexp=r'(\d+)')
'123.00'
>>> '%.2f' % to_float('abc123abc', regexp='(\d+)')
>>> '%.2f' % to_float('abc123abc', regexp=r'(\d+)')
'123.00'
>>> '%.2f' % to_float('abc123abc456', regexp='(\d+)')
>>> '%.2f' % to_float('abc123abc456', regexp=r'(\d+)')
'123.00'
>>> '%.2f' % to_float('1234', default=1)
'1234.00'
Expand Down Expand Up @@ -248,8 +248,9 @@ def remap(value, old_min, old_max, new_min, new_max):
>>> remap(33, 0, 100, -500, 500)
-170
This is a great use case example. Take an AVR that has dB values the minimum being -80dB and the maximum
being 10dB and you want to convert volume percent to the equilivint in that dB range
This is a great use case example. Take an AVR that has dB values the
minimum being -80dB and the maximum being 10dB and you want to convert
volume percent to the equilivint in that dB range
>>> remap(46.0, 0.0, 100.0, -80.0, 10.0)
-38.6
Expand All @@ -269,8 +270,9 @@ def remap(value, old_min, old_max, new_min, new_max):
:param new_max: the maximum of the new range
:type new_max: int, float
:return: value that has been re ranged, if the value is an int floor division is used so the
returned value will always be rounded down to the closest whole number.
:return: value that has been re ranged, if the value is an int floor
division is used so the returned value will always be rounded down
to the closest whole number.
:rtype: int, float
"""
old_range = old_max - old_min
Expand All @@ -281,9 +283,12 @@ def remap(value, old_min, old_max, new_min, new_max):
if old_range == 0:
new_value = new_min
else:
new_value = (value - old_min) * new_range
if isinstance(value, int):
new_value = (((value - old_min) * new_range) // old_range) + new_min
new_value = new_value // old_range
else:
new_value = (((value - old_min) * new_range) / old_range) + new_min
new_value = new_value / old_range

new_value += new_min

return new_value

0 comments on commit 8cebf54

Please sign in to comment.