Skip to content

Commit

Permalink
Fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
jules-ch committed Oct 4, 2022
1 parent 7f1cfcf commit a2cc663
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 20 deletions.
44 changes: 33 additions & 11 deletions docs/dev/pint-convert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions docs/user/getting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion docs/user/performance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pint/facets/context/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
2 changes: 1 addition & 1 deletion pint/facets/plain/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
pint.facets.plain
~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~
Base implementation for registry, units and quantities.
Expand Down
4 changes: 2 additions & 2 deletions pint/facets/plain/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,9 @@ def to_compact(self, unit=None) -> PlainQuantity[_MagnitudeType]:
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> (200e-9*ureg.s).to_compact()
<PlainQuantity(200.0, 'nanosecond')>
<Quantity(200.0, 'nanosecond')>
>>> (1e-2*ureg('kg m/s^2')).to_compact('N')
<PlainQuantity(10.0, 'millinewton')>
<Quantity(10.0, 'millinewton')>
"""

if not isinstance(self.magnitude, numbers.Number):
Expand Down

0 comments on commit a2cc663

Please sign in to comment.