Skip to content

Commit

Permalink
convert from = to 〜 in UnitConversion descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Oct 20, 2021
1 parent a81f3dc commit 325a076
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ a sanity check:
.. code-block:: python
>>> print(str(m2pc))
m = 3.0857e+16*pc
m 3.0857e+16*pc
In addition, you can use it to directly perform conversions:

Expand Down
28 changes: 20 additions & 8 deletions quantiphy.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _named_regex(name, regex):
def _scale(scale, number, units):
if isinstance(scale, str):
# if scale is string, it contains the units to convert from
number = convert_units(scale, units, number)
number = _convert_units(scale, units, number)
units = scale
else:
try:
Expand Down Expand Up @@ -234,8 +234,11 @@ class IncompatiblePreferences(QuantiPhyError, ValueError):
_unit_conversions = {}


# convert_units() {{{2
def convert_units(to_units, from_units, value):
# _convert_units() {{{2
def _convert_units(to_units, from_units, value):
# not intended to be used by the user; if you want this functionality,
# simply use: Quantity(value, from_units).scale(to_units).

if to_units == from_units:
return value
try:
Expand Down Expand Up @@ -314,7 +317,7 @@ class UnitConversion(object):
you can convert it to a string to get a summary of the conversion::
>>> print(str(m2pc))
m = 3.0857e+16*pc
m 3.0857e+16*pc
The act of creating this unit conversion establishes a conversion between
meters (m) and parsecs (parsec, pc) that is accessible when creating or
Expand All @@ -338,7 +341,7 @@ class UnitConversion(object):
>>> conversion = UnitConversion('F', 'C', 1.8, 32)
>>> print(str(conversion))
F = 1.8*C + 32
F 1.8*C + 32
You can also use functions to perform the conversions, which is appropriate
when the conversion is nonlinear (cannot be described with a slope and
Expand All @@ -354,7 +357,12 @@ class UnitConversion(object):
... return 20*log10(value)
>>> converter = UnitConversion('V', 'dBV', from_dB, to_dB)
>>> print(str(converter))
V 〜 from_dB(dBV), dBV 〜 to_dB(V)
>>> converter = UnitConversion('A', 'dBA', from_dB, to_dB)
>>> print(str(converter))
A 〜 from_dB(dBA), dBA 〜 to_dB(A)
>>> print('{:pdBV}, {:pdBV}'.format(Quantity('100mV'), Quantity('10V')))
-20 dBV, 20 dBV
Expand All @@ -376,6 +384,7 @@ def __init__(self, to_units, from_units, slope=1, intercept=0):
self.from_units = from_units.split() if isinstance(from_units, str) else from_units
self.slope = slope
self.intercept = intercept

if callable(slope) or callable(intercept):
# the slope and intercept arguments are actually the forward and
# reverse conversion functions.
Expand Down Expand Up @@ -450,6 +459,9 @@ def convert(self, value=1, from_units=None, to_units=None):
Example::
>>> print(str(m2pc))
m 〜 3.0857e+16*pc
>>> m = m2pc.convert()
>>> print(str(m))
30.857e15 m
Expand Down Expand Up @@ -509,16 +521,16 @@ def convert(self, value=1, from_units=None, to_units=None):
def __str__(self):
if callable(self.slope) or callable(self.intercept):
# using functions to do the conversion, have no good description
return '{} = {}({}), {} = {}({})'.format(
return '{} {}({}), {} {}({})'.format(
self.to_units[0], self.slope.__name__, self.from_units[0],
self.from_units[0], self.intercept.__name__, self.to_units[0]
)
if self.intercept:
return '{} = {}*{} + {}'.format(
return '{} {}*{} + {}'.format(
self.to_units[0], self.slope, self.from_units[0],
Quantity(self.intercept, self.to_units[0]).render(show_units=False)
)
return '{} = {}*{}'.format(
return '{} {}*{}'.format(
self.to_units[0], self.slope, self.from_units[0]
)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_doctests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def test_quantiphy():
Quantity.reset_prefs()
rv = doctest.testfile('../quantiphy.py', optionflags=doctest.ELLIPSIS)
assert rv.failed == 0
assert rv.attempted == 97
assert rv.attempted == 100
# this target should be undated when the number of doctests change

def test_manual():
if sys.version_info < (3, 6):
Expand Down
14 changes: 8 additions & 6 deletions tests/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ def test_add():
except TypeError:
assert True

def test_coversion():
def test_linear_conversion():
Quantity.reset_prefs()
conversion = UnitConversion('USD', 'BTC', 100000)
assert str(conversion) == 'USD = 100000*BTC'
assert str(conversion) == 'USD 100000*BTC'

result = conversion.convert(1, 'BTC', 'USD')
assert str(result) == '100 kUSD'
Expand Down Expand Up @@ -316,8 +316,10 @@ def test_coversion():
dollar = conversion.convert(bitcoin)
assert str(dollar) == '200 kUSD'


def test_affine_conversion():
conversion = UnitConversion('F', 'C', 1.8, 32)
assert str(conversion) == 'F = 1.8*C + 32'
assert str(conversion) == 'F 1.8*C + 32'

result = conversion.convert(0, 'C', 'F')
assert str(result) == '32 F'
Expand Down Expand Up @@ -347,7 +349,7 @@ def test_coversion():
assert isinstance(exception.value, KeyError)
assert exception.value.args == ('X',)

def test_func():
def test_func_converters():
Quantity.reset_prefs()

def from_dB(value):
Expand All @@ -357,12 +359,12 @@ def to_dB(value):
return 20*math.log10(value)

vconverter = UnitConversion('V', 'dBV', from_dB, to_dB)
assert str(vconverter) == 'V = from_dB(dBV), dBV = to_dB(V)'
assert str(vconverter) == 'V from_dB(dBV), dBV to_dB(V)'
assert str(vconverter.convert(Quantity('100mV'))) == '-20 dBV'
assert str(vconverter.convert(Quantity('-20dBV'))) == '100 mV'

aconverter = UnitConversion('A', 'dBA', from_dB, to_dB)
assert str(aconverter) == 'A = from_dB(dBA), dBA = to_dB(A)'
assert str(aconverter) == 'A from_dB(dBA), dBA to_dB(A)'
assert str(aconverter.convert(Quantity('100mA'))) == '-20 dBA'
assert str(aconverter.convert(Quantity('-20dBA'))) == '100 mA'

Expand Down

0 comments on commit 325a076

Please sign in to comment.