From a2cc663244e22abe753017c82f577b0dc66969d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jules=20Ch=C3=A9ron?= Date: Sat, 3 Sep 2022 18:48:29 +0200 Subject: [PATCH] Fix doctests --- docs/dev/pint-convert.rst | 44 ++++++++++++++++++++++++--------- docs/user/getting.rst | 16 +++++++++--- docs/user/performance.rst | 26 ++++++++++++++++++- pint/facets/context/registry.py | 2 +- pint/facets/plain/__init__.py | 2 +- pint/facets/plain/quantity.py | 4 +-- 6 files changed, 74 insertions(+), 20 deletions(-) diff --git a/docs/dev/pint-convert.rst b/docs/dev/pint-convert.rst index 8d548f8cf..dbb0804f4 100644 --- a/docs/dev/pint-convert.rst +++ b/docs/dev/pint-convert.rst @@ -6,38 +6,52 @@ Command-line script The script `pint-convert` allows a quick conversion to a target system or between arbitrary compatible units. -By default, `pint-convert` converts to SI units:: +By default, `pint-convert` converts to SI units: + +.. code-block:: console $ pint-convert 225lb 225 pound = 102.05828325 kg -use the `--sys` argument to change it:: +use the `--sys` argument to change it: + +.. code-block:: console $ pint-convert --sys US 102kg 102 kilogram = 224.871507429 lb -or specify directly the target units:: +or specify directly the target units: + +.. code-block:: console $ pint-convert 102kg lb 102 kilogram = 224.871507429 lb -The input quantity can contain expressions:: +The input quantity can contain expressions: + +.. code-block:: console $ pint-convert 7ft+2in 7.166666666666667 foot = 2.1844 m -in some cases parentheses and quotes may be needed:: +in some cases parentheses and quotes may be needed: + +.. code-block:: console $ pint-convert "225lb/(7ft+2in)" 31.3953488372093 pound / foot = 46.7214261353 kg/m -If a number is omitted, 1 is assumed:: +If a number is omitted, 1 is assumed: + +.. code-block:: console $ pint-convert km mi 1 kilometer = 0.621371192237 mi The default precision is 12 significant figures, it can be changed with `-p`, -but note that the accuracy may be affected by floating-point errors:: +but note that the accuracy may be affected by floating-point errors: + +.. code-block:: console $ pint-convert -p 3 mi 1 mile = 1.61e+03 m @@ -46,7 +60,9 @@ but note that the accuracy may be affected by floating-point errors:: 1 light_year = 9460730472580.80078125 km Some contexts are automatically enabled, allowing conversion between not fully -compatible units:: +compatible units: + +.. code-block:: console $ pint-convert 540nm 540 nanometer = 5.4e-07 m @@ -59,7 +75,9 @@ compatible units:: With the `uncertainties` package, the experimental uncertainty in the physical constants is considered, and the result is given in compact notation, with the -uncertainty in the last figures in parentheses:: +uncertainty in the last figures in parentheses: + +.. code-block:: console $ pint-convert Eh eV 1 hartree = 27.21138624599(5) eV @@ -73,13 +91,17 @@ and the maximum number of uncertainty digits (`-u`, 2 by default):: $ pint-convert -p 20 -u 4 Eh eV 1 hartree = 27.21138624598847(5207) eV -The uncertainty can be disabled with `-U`):: +The uncertainty can be disabled with `-U`): + +.. code-block:: console $ pint-convert -p 20 -U Eh eV 1 hartree = 27.211386245988471444 eV Correlations between experimental constants are also known, and taken into -account. Use `-C` to disable it:: +account. Use `-C` to disable it: + +.. code-block:: console $ pint-convert --sys atomic m_p 1 proton_mass = 1836.15267344(11) m_e diff --git a/docs/user/getting.rst b/docs/user/getting.rst index d4b250c91..c696faee9 100644 --- a/docs/user/getting.rst +++ b/docs/user/getting.rst @@ -31,19 +31,27 @@ That's all! You can check that Pint is correctly installed by starting up python Getting the code ---------------- -You can also get the code from PyPI_ or GitHub_. You can either clone the public repository:: +You can also get the code from PyPI_ or GitHub_. You can either clone the public repository: + +.. code-block:: console $ git clone git://github.com/hgrecco/pint.git -Download the tarball:: +Download the tarball: + +.. code-block:: console $ curl -OL https://github.com/hgrecco/pint/tarball/master -Or, download the zipball:: +Or, download the zipball: + +.. code-block:: console $ curl -OL https://github.com/hgrecco/pint/zipball/master -Once you have a copy of the source, you can embed it in your Python package, or install it into your site-packages easily:: +Once you have a copy of the source, you can embed it in your Python package, or install it into your site-packages easily: + +.. code-block:: console $ python setup.py install diff --git a/docs/user/performance.rst b/docs/user/performance.rst index 3daa81d8c..d7b8a0cd5 100644 --- a/docs/user/performance.rst +++ b/docs/user/performance.rst @@ -11,7 +11,7 @@ Pint can impose a significant performance overhead on computationally-intensive Use magnitudes when possible ---------------------------- -It's significantly faster to perform mathematical operations on magnitudes (even though your'e still using pint to retrieve them from a quantity object). +It's significantly faster to perform mathematical operations on magnitudes (even though you're still using pint to retrieve them from a quantity object). .. ipython:: :verbatim: @@ -65,6 +65,30 @@ A safer method: wrapping ------------------------ A better way to use magnitudes is to use pint's wraps decorator (See :ref:`wrapping`). By decorating a function with wraps, you pass only the magnitude of an argument to the function body according to units you specify. As such this method is safer in that you are sure the magnitude is supplied in the correct units. +.. ipython:: + + In [1]: import pint + + In [2]: ureg = pint.UnitRegistry() + + In [3]: import numpy as np + + In [4]: def f(x, y): + ...: return (x - y) / (x + y) * np.log(x/y) + + In [5]: @ureg.wraps(None, ('meter', 'meter')) + ...: def g(x, y): + ...: return (x - y) / (x + y) * np.log(x/y) + + In [6]: a = 1 * ureg.meter + + In [7]: b = 1 * ureg.centimeter + + In [8]: %timeit f(a, b) + 1000 loops, best of 3: 312 µs per loop + + In [9]: %timeit g(a, b) + 10000 loops, best of 3: 65.4 µs per loop Speed up registry instantiation diff --git a/pint/facets/context/registry.py b/pint/facets/context/registry.py index eb9b3b40b..95ecf1466 100644 --- a/pint/facets/context/registry.py +++ b/pint/facets/context/registry.py @@ -270,7 +270,7 @@ def context(self, *names, **kwargs) -> ContextManager[Context]: -------- Context can be called by their name: - import pint.facets.context.objects + >>> import pint.facets.context.objects >>> import pint >>> ureg = pint.UnitRegistry() >>> ureg.add_context(pint.facets.context.objects.Context('one')) diff --git a/pint/facets/plain/__init__.py b/pint/facets/plain/__init__.py index 4970f5b86..604cb42d4 100644 --- a/pint/facets/plain/__init__.py +++ b/pint/facets/plain/__init__.py @@ -1,6 +1,6 @@ """ pint.facets.plain - ~~~~~~~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~ Base implementation for registry, units and quantities. diff --git a/pint/facets/plain/quantity.py b/pint/facets/plain/quantity.py index 5e4f33ddb..d4c1a55ed 100644 --- a/pint/facets/plain/quantity.py +++ b/pint/facets/plain/quantity.py @@ -618,9 +618,9 @@ def to_compact(self, unit=None) -> PlainQuantity[_MagnitudeType]: >>> import pint >>> ureg = pint.UnitRegistry() >>> (200e-9*ureg.s).to_compact() - + >>> (1e-2*ureg('kg m/s^2')).to_compact('N') - + """ if not isinstance(self.magnitude, numbers.Number):