Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds unit conversion #20

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7ba9cf2
Adds unit conversion
kdschlosser Jun 15, 2021
06a598b
changes floating point computations
kdschlosser Jun 19, 2021
33d51d7
Merge branch 'develop' into unit_conversion
wolph Oct 31, 2021
5feeea2
added first batch of tests for unit conversion branch
wolph Nov 1, 2021
a1c292f
Trimmed some code that was no longer needed.
kdschlosser Nov 1, 2021
dc1dcca
Trimmed some code that was no longer needed.
kdschlosser Nov 1, 2021
f4dffbe
Merge remote-tracking branch 'origin/unit_conversion' into unit_conve…
kdschlosser Nov 1, 2021
da27386
Trimmed some code that was no longer needed.
kdschlosser Nov 1, 2021
a4fa888
Adds support for complex SI symbol notation
kdschlosser Nov 3, 2021
c8959e2
Testing code and correcting issues.
kdschlosser Nov 3, 2021
8be3f42
Tracebacks fixed and units added
kdschlosser Nov 3, 2021
3549a28
Adds type hinting and moves docstring
kdschlosser Nov 3, 2021
dae32e1
Removes test code
kdschlosser Nov 3, 2021
ed8a007
Changes how precision of returned value is handled
kdschlosser Nov 3, 2021
8f6527f
Merge branch 'develop' into unit_conversion
wolph Nov 7, 2021
b396a37
Rewrite to add support for more units
kdschlosser Nov 7, 2021
e0694ec
I am pretty sure this is what you wanted.
kdschlosser Nov 8, 2021
65653fd
documentation
kdschlosser Nov 10, 2021
6253614
adds unit_converts module to docs
kdschlosser Nov 10, 2021
43c79e7
Several changes
kdschlosser Nov 21, 2021
3d05447
re-added tests... how/why were they gone?
wolph Nov 22, 2021
44af537
Splits unit converter into several files
kdschlosser Nov 22, 2021
472dbe6
Merge remote-tracking branch 'origin/unit_conversion' into unit_conve…
kdschlosser Nov 22, 2021
b89ca20
Changes to ensure decimal.Decimal is used
kdschlosser Nov 22, 2021
effc48c
Adds code style inspection and pep8 ignores
kdschlosser Nov 22, 2021
ca807b5
Fixes RecursionError problem if an unsupported unit is given.
kdschlosser Nov 22, 2021
503c37d
Changes returned value type
kdschlosser Nov 22, 2021
d9359ac
Returned value is always a float
kdschlosser Nov 22, 2021
a808de5
Gets unit compatibility checking working
kdschlosser Nov 22, 2021
f81e684
Code organization
kdschlosser Nov 22, 2021
0126bbb
Adds some additional units and increases the precision of others
kdschlosser Nov 23, 2021
ac9e62e
make all conversion factors strings to ensure precision when converti…
wolph Nov 24, 2021
e8e9ba8
fixed tests
wolph Nov 24, 2021
8237b00
added different method of calculations
wolph Nov 24, 2021
f4f6ca3
Adds quantities and constants
kdschlosser Nov 25, 2021
bd230ad
Merge remote-tracking branch 'origin/unit_conversion' into unit_conve…
kdschlosser Nov 25, 2021
0c62cfd
improves accuracy and other changes
kdschlosser Nov 27, 2021
4eb03c8
code cleanup and documentation changes
kdschlosser Nov 28, 2021
6ed2785
Working on the quantities
kdschlosser Nov 28, 2021
12963f8
More work on the quantities
kdschlosser Nov 28, 2021
4b11ee3
Fix missing closing quote
kdschlosser Nov 28, 2021
7adec5e
New quantity system is done.
kdschlosser Nov 29, 2021
ee54851
Gets the type hinting and autocomplete working for the `units` module…
kdschlosser Nov 29, 2021
e0ca265
Adds Optics quantities
kdschlosser Nov 30, 2021
21df582
Adds data quantities and more data units
kdschlosser Nov 30, 2021
d97dd26
Adds a couple more units.
kdschlosser Dec 1, 2021
555a77e
Reworked the operator methods.
kdschlosser Dec 2, 2021
87faf92
Adds exponent operator support
kdschlosser Dec 2, 2021
a29a6df
Adds 2 way conversions
kdschlosser Dec 3, 2021
8ce1f5d
added media query for wide screens
wolph Dec 6, 2021
7eebf47
Merge branch 'develop' into unit_conversion
wolph May 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions _python_utils_tests/test_unit_converter.py
@@ -0,0 +1,78 @@
import pytest

from python_utils import unit_converter


def test_main():
import unit_converter_example
assert unit_converter_example


temperature_units = (
'K',
'°C',
'°F',
'°R',
pytest.param('X', marks=pytest.mark.xfail),
)
units = ('value', 'from_unit', 'to_unit', 'expected'), [
(71, 'in³', 'mm³', 1163481.544),
(129.5674, 'in²', 'mm²', 83591.704),
(3.657, 'gal', 'l', 13.843),
(500.679, 'g', 'lb', 1.104),
(75.1, '°F', 'K', 297.094),
(132.7, 'mi/h', 'km/h', 213.560),
(1.0, 'P', 'Pa s', 0.1),
(56.0, 'in', 'cm', 142.24),
(50.34, 'ftHg', 'mmHg', 15343.632),
(50.34, 'inH2O', 'cmH2O', 127.864),
(50.34, 'inHg', 'psi', 24.725)
]


@pytest.mark.parametrize('input_', temperature_units)
@pytest.mark.parametrize('output', temperature_units)
def test_temperature(input_, output):
unit_converter.convert(1, input_, output)


@pytest.mark.parametrize('prefix', [
'Y',
'Z',
'E',
'P',
'T',
'G',
'M',
'k',
'h',
'd',
'c',
'm',
'µ',
'n',
'p',
'f',
'a',
'z',
'y',
pytest.param('X', marks=pytest.mark.xfail),
])
def test_unit_prefixes(prefix):
unit_converter.convert(1, '%sm' % prefix, 'm')


@pytest.mark.parametrize(*units)
def test_units(value, from_unit, to_unit, expected):
result = unit_converter.convert(value, from_unit, to_unit)
print(f'{value} {from_unit} = {result} {to_unit}')
assert round(result, 3) == expected


@pytest.mark.parametrize(*units)
def test_calculated_units(value, from_unit, to_unit, expected):
from_unit = unit_converter.Unit(from_unit)
to_unit = unit_converter.Unit(to_unit)
result = value * (from_unit / to_unit)
print(f'{value} {from_unit} = {result} {to_unit}')
assert round(result, 3) == expected
31 changes: 31 additions & 0 deletions docs/_static/custom.css
@@ -0,0 +1,31 @@
@media only screen and (min-width: 1200px) {
/* Newlines (\a) and spaces (\20) before each parameter */
.sig-param::before {
content: "\a\20\20\20\20\20\20";
white-space: pre;
}

/* Newline after the last parameter (so the closing bracket is on a new line) */
dt em.sig-param:last-of-type::after {
content: "\a";
white-space: pre;
}

/* To have blue background of width of the block (instead of width of content) */
dl.class > dt:first-of-type {
display: block !important;
}

div.body {
max-width: 1250px;
}

div.bodywrapper {
width: auto;
}

div.document {
width: auto;
margin: 30px 10% 0 10%;
}
}
14 changes: 14 additions & 0 deletions docs/python_utils.rst
Expand Up @@ -60,6 +60,20 @@ python\_utils\.time module
:undoc-members:
:show-inheritance:

python\_utils\.unit_converter module
------------------------------------

.. automodule:: python_utils.unit_converter
:members: convert
:exclude-members: main

.. automodule:: python_utils.unit_converter.constants

.. autoclass:: python_utils.unit_converter::Unit
:members: __call__

.. automodule:: python_utils.unit_converter.units


Module contents
---------------
Expand Down