|
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 | +================== |
3 | 4 |
|
4 | | -Easily use and manipulate unit-aware measurement objects in Python. |
| 5 | +**High precision unit-aware measurement objects in Python.** |
5 | 6 |
|
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') |
10 | 10 |
|
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. |
13 | 14 |
|
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>`_. |
20 | 19 |
|
21 | | -Example: |
| 20 | +Installation |
| 21 | +============ |
22 | 22 |
|
23 | | -.. code-block:: python |
| 23 | +You can install the latest version of the package with Pip:: |
24 | 24 |
|
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 |
33 | 26 |
|
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 | +===== |
39 | 29 |
|
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 | +------------------------- |
42 | 32 |
|
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") |
49 | 67 |
|
| 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: |
50 | 73 |
|
| 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") |
51 | 78 |
|
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. |
55 | 80 |
|
| 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. |
0 commit comments