Skip to content

Commit 0978a66

Browse files
committed
Switch to Decimal based values
1 parent 43b2438 commit 0978a66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1581
-1640
lines changed

.bandit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bandit]
2+
exclude: tests

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 4
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
charset = utf-8
11+
end_of_line = lf
12+
max_line_length = 88
13+
14+
[*.{json,yml,yaml}]
15+
indent_size = 2
16+
17+
[LICENSE]
18+
insert_final_newline = false

.github/workflows/ci.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,31 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/setup-python@v1
14-
- uses: actions/checkout@v1
14+
- uses: actions/checkout@v2
1515
- run: python -m pip install isort
1616
- run: isort --check-only --diff --recursive .
1717

18+
flake8:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/setup-python@v1
22+
- uses: actions/checkout@v2
23+
- run: python -m pip install flake8
24+
- run: flake8 .
25+
1826
pydocstyle:
1927
runs-on: ubuntu-latest
2028
steps:
2129
- uses: actions/setup-python@v1
22-
- uses: actions/checkout@v1
30+
- uses: actions/checkout@v2
2331
- run: python -m pip install pydocstyle
2432
- run: pydocstyle .
2533

2634
black:
2735
runs-on: ubuntu-latest
2836
steps:
2937
- uses: actions/setup-python@v1
30-
- uses: actions/checkout@v1
38+
- uses: actions/checkout@v2
3139
- run: python -m pip install black
3240
- run: black --check --diff .
3341

@@ -36,22 +44,23 @@ jobs:
3644
- isort
3745
- pydocstyle
3846
- black
47+
- flake8
3948
strategy:
4049
matrix:
4150
os:
4251
- ubuntu-latest
52+
- windows-latest
53+
- macos-latest
4354
python-version:
44-
- 3.5
45-
- 3.6
4655
- 3.7
4756
- 3.8
4857
runs-on: ${{ matrix.os }}
4958
steps:
5059
- name: Set up Python ${{ matrix.python-version }}
51-
uses: actions/setup-python@v1.1.1
60+
uses: actions/setup-python@v1
5261
with:
5362
python-version: ${{ matrix.python-version }}
54-
- uses: actions/checkout@v1
63+
- uses: actions/checkout@v2
5564
- run: python -m pip install --upgrade setuptools wheel codecov
5665
- run: python setup.py test
5766
- run: codecov

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ include/
99
lib/
1010
docs/_build/
1111
.coverage
12+
htmlcov/

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Adam Coddington
3+
Copyright (c) 2020 Johannes Hoppe
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.rst

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,86 @@
1-
.. image:: https://travis-ci.org/coddingtonbear/python-measurement.svg?branch=master
2-
:target: https://travis-ci.org/coddingtonbear/python-measurement
1+
==================
2+
Python measurement
3+
==================
34

4-
Easily use and manipulate unit-aware measurement objects in Python.
5+
**High precision unit-aware measurement objects in Python.**
56

6-
`django.contrib.gis.measure <https://github.com/django/django/blob/master/django/contrib/gis/measure.py>`_
7-
has these wonderful 'Distance' objects that can be used not only for storing a
8-
unit-aware distance measurement, but also for converting between different
9-
units and adding/subtracting these objects from one another.
7+
>>> from measurement import measures
8+
>>> measures.Distance("12 megaparsec")["British yard"]
9+
Decimal('404948208659679393828910.8771')
1010

11-
This module not only provides those Distance and Area measurement
12-
objects, but also other measurements including:
11+
This package provides a large reference collection of various measure and
12+
their corresponding SI (Metric), US or Imperial units. Its high precision
13+
supports use cases all the way from quantum mechanics to astrophysics.
1314

14-
- Energy
15-
- Speed
16-
- Temperature
17-
- Time
18-
- Volume
19-
- Weight
15+
- Documentation for python-measurement is available an
16+
`ReadTheDocs <https://python-measurement.readthedocs.org/>`_.
17+
- Please post issues on
18+
`Github <https://github.com/coddingtonbear/python-measurement/issues>`_.
2019

21-
Example:
20+
Installation
21+
============
2222

23-
.. code-block:: python
23+
You can install the latest version of the package with Pip::
2424

25-
>>> from measurement.measures import Weight
26-
>>> weight_1 = Weight(lb=125)
27-
>>> weight_2 = Weight(kg=40)
28-
>>> added_together = weight_1 + weight_2
29-
>>> added_together
30-
Weight(lb=213.184976807)
31-
>>> added_together.kg # Maybe I actually need this value in kg?
32-
96.699
25+
python3 -m pip install measurement
3326

34-
.. warning::
35-
Measurements are stored internally by converting them to a
36-
floating-point number of a (generally) reasonable SI unit. Given that
37-
floating-point numbers are very slightly lossy, you should be aware of
38-
any inaccuracies that this might cause.
27+
Usage
28+
=====
3929

40-
TLDR: Do not use this in
41-
`navigation algorithms guiding probes into the atmosphere of extraterrestrial worlds <http://en.wikipedia.org/wiki/Mars_Climate_Orbiter>`_.
30+
Using Measurement Objects
31+
-------------------------
4232

43-
- Documentation for python-measurement is available an
44-
`ReadTheDocs <https://python-measurement.readthedocs.org/>`_.
45-
- Please post issues on
46-
`Github <https://github.com/coddingtonbear/python-measurement/issues>`_.
47-
- Test status available on
48-
`Travis-CI <https://travis-ci.org/coddingtonbear/python-measurement>`_.
33+
You can import any of the above measures from `measurement.measures`
34+
and use it for easily handling measurements like so:
35+
36+
>>> from measurement.measures import Mass
37+
>>> m = Mass(lb=135) # Represents 135 lbs
38+
>>> print(m)
39+
135 lb
40+
>>> print(m["long ton"])
41+
0.06027063971456692913385826772
42+
43+
You can create a measurement unit using any compatible unit and can transform
44+
it into any compatible unit. See :doc:`measures` for information about which
45+
units are supported by which measures.
46+
47+
.. seealso::
48+
Should you be planing to go to Mars, you might need to increase your
49+
`decimal precision`_, like so:
50+
51+
>>> import decimal
52+
>>> decimal.getcontext().prec = 28
53+
54+
.. _decimal precision: https://docs.python.org/3.8/library/decimal.html
55+
56+
Guessing Measurements
57+
---------------------
58+
59+
If you happen to be in a situation where you are processing a list of
60+
value/unit pairs (like you might find at the beginning of a recipe), you can
61+
use the `guess` function to give you a measurement object.:
62+
63+
>>> from measurement.utils import guess
64+
>>> m = guess(10, "mg")
65+
>>> print(repr(m))
66+
Mass(gram="0.010")
4967

68+
By default, this will check all built-in measures, and will return the first
69+
measure having an appropriate unit. You may want to constrain the list of
70+
measures checked (or your own measurement classes, too) to make sure
71+
that your measurement is not mis-guessed, and you can do that by specifying
72+
the ``measures`` keyword argument:
5073

74+
>>> from measurement.measures import Distance, Temperature, Volume
75+
>>> m = guess(24, "°F", measures=[Distance, Volume, Temperature])
76+
>>> print(repr(m))
77+
Temperature(fahrenheit="24.00000000000000000000000008")
5178

52-
.. image:: https://d2weczhvl823v0.cloudfront.net/coddingtonbear/python-measurement/trend.png
53-
:alt: Bitdeli badge
54-
:target: https://bitdeli.com/free
79+
If no match is found, a :class:`ValueError` exception will be raised.
5580

81+
.. note::
82+
It is absolutely possible for this to misguess due to common measurement
83+
abbreviations overlapping -- for example, both Temperature and Energy
84+
accept the argument ``c`` for representing degrees celsius and calories
85+
respectively. It is advisible that you constrain the list of measurements
86+
to check to ones that you would consider appropriate for your input data.

docs/caliper.svg

Lines changed: 101 additions & 0 deletions
Loading

docs/conf.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
11
"""Sphinx configuration file."""
2+
import inspect
3+
24
from pkg_resources import get_distribution
35

6+
from measurement.base import AbstractMeasure
7+
48
project = "python-measurement"
5-
copyright = "2013, Adam Coddington"
9+
copyright = "2020, Johannes Hoppe"
610
release = get_distribution("measurement").version
711
version = ".".join(release.split(".")[:2])
12+
html_theme = "python_docs_theme"
813

914
master_doc = "index"
1015

16+
html_logo = "caliper.svg"
17+
1118
extensions = [
1219
"sphinx.ext.autodoc",
1320
"sphinx.ext.viewcode",
1421
"sphinx.ext.napoleon",
1522
"sphinx.ext.intersphinx",
1623
"sphinx.ext.doctest",
24+
"sphinx.ext.inheritance_diagram",
1725
]
1826

1927
intersphinx_mapping = {
2028
"python": ("https://docs.python.org/3", None),
2129
}
30+
31+
inheritance_graph_attrs = dict(rankdir="TB")
32+
33+
graphviz_output_format = "svg"
34+
35+
autodoc_member_order = "bysource"
36+
37+
38+
def process_measures(app, what, name, obj, options, lines):
39+
if inspect.isclass(obj) and issubclass(obj, AbstractMeasure):
40+
lines.append("**Supported Units:**")
41+
lines.extend(
42+
f" :{obj._attr_to_unit(name)}: {', '.join(value.symbols)}"
43+
for name, value in obj._org_units.items()
44+
)
45+
return lines
46+
47+
48+
def setup(app):
49+
app.connect("autodoc-process-docstring", process_measures)

docs/custom_measures.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Custom Measures
2+
===============
3+
4+
API reference
5+
-------------
6+
7+
.. inheritance-diagram:: measurement.base
8+
9+
.. automodule:: measurement.base
10+
:show-inheritance:
11+
:members:
12+
:inherited-members:

0 commit comments

Comments
 (0)