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

use application reg in docs #1931

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/advanced/defining.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ way is to create a new file (e.g. `mydef.txt`) with your definitions::

and then in Python, you can load it as:

>>> from pint import UnitRegistry
>>> import pint
>>> # First we create the registry.
>>> ureg = UnitRegistry()
>>> ureg = pint.UnitRegistry()
>>> # Set pint to use the new registry
>>> pint.set_application_registry(ureg)
>>> # Then we append the new definitions
>>> ureg.load_definitions('/your/path/to/my_def.txt') # doctest: +SKIP

If you make a translation of the default units or define a completely new set,
you don't want to append the translated definitions so you just give the
filename to the constructor::

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry('/your/path/to/default_es.txt') # doctest: +SKIP
>>> ureg = pint.UnitRegistry('/your/path/to/default_es.txt') # doctest: +SKIP

In the definition file, prefixes are identified by a trailing dash::

Expand Down Expand Up @@ -100,10 +101,11 @@ Let's add a dog_year (sometimes written as dy) equivalent to 52 (human) days:

.. doctest::

>>> from pint import UnitRegistry
>>> import pint
>>> # We first instantiate the registry.
>>> # If we do not provide any parameter, the default unit definitions are used.
>>> ureg = UnitRegistry()
>>> ureg = pint.UnitRegistry()
>>> pint.set_application_registry(ureg)
>>> Q_ = ureg.Quantity

# Here we add the unit
Expand Down
5 changes: 3 additions & 2 deletions docs/advanced/measurement.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ quantity using the ``plus_minus()`` method.
:skipif: not_installed['uncertainties']

>>> import numpy as np
>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> book_length = (20. * ureg.centimeter).plus_minus(2.)
>>> print(book_length)
(20.0 +/- 2.0) centimeter
Expand Down
6 changes: 3 additions & 3 deletions docs/advanced/performance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ It's significantly faster to perform mathematical operations on magnitudes (even
.. ipython::
:verbatim:

In [1]: from pint import UnitRegistry
In [1]: import pint

In [2]: ureg = UnitRegistry()
In [2]: ureg = pint.get_application_registry()

In [3]: q1 = ureg('1m')

Expand Down Expand Up @@ -69,7 +69,7 @@ A better way to use magnitudes is to use pint's wraps decorator (See :ref:`wrapp

In [1]: import pint

In [2]: ureg = pint.UnitRegistry()
In [2]: ureg = pint.get_application_registry()

In [3]: import numpy as np

Expand Down
8 changes: 4 additions & 4 deletions docs/advanced/pitheorem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ parameters.

.. testsetup:: *

from pint import UnitRegistry
ureg = UnitRegistry()
import pint
ureg = pint.UnitRegistry()
Q_ = ureg.Quantity

To start with a very simple case, consider that you want to find a dimensionless
Expand Down Expand Up @@ -41,8 +41,8 @@ you can use derived dimensions such as speed:

.. doctest::

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg.pi_theorem({'V': '[speed]', 'T': '[time]', 'L': '[length]'})
[{'V': 1.0, 'T': 1.0, 'L': -1.0}]

Expand Down
4 changes: 2 additions & 2 deletions docs/advanced/serialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The easiest way to do this is by converting the quantity to a string:
.. doctest::

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> duration = 24.2 * ureg.years
>>> duration
<Quantity(24.2, 'year')>
Expand All @@ -28,7 +28,7 @@ to recover it in another process/machine, you just:
.. doctest::

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> duration = ureg('24.2 year')
>>> print(duration)
24.2 year
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced/wrapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ You could wrap this function to use Quantities instead:

.. doctest::

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> import pint
>>> ureg = pint.get_application_registry()
>>> Q_ = ureg.Quantity
>>> def mypp_caveman(length):
... return pendulum_period(length.to(ureg.meter).magnitude) * ureg.second
Expand Down
2 changes: 1 addition & 1 deletion docs/getting/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ It is extremely easy and natural to use:
.. code-block:: python

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> 3 * ureg.meter + 4 * ureg.cm
<Quantity(3.04, 'meter')>

Expand Down
15 changes: 7 additions & 8 deletions docs/getting/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ and perform unit conversions in Python.
Initializing a Registry
-----------------------

Before using Pint, initialize a :class:`UnitRegistry() <pint.registry.UnitRegistry>`
object. The ``UnitRegistry`` stores the unit definitions, their relationships,
and handles conversions between units.
Pint's ``UnitRegistry`` stores the unit definitions, their relationships,
and handles conversions between units. A ``UnitRegistry`` populated with the
`default list of units`_ and prefixes is initialized when you import Pint:

.. doctest::

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> import pint
>>> ureg = pint.get_application_registry()

If no parameters are given to the constructor, the ``UnitRegistry`` is populated
with the `default list of units`_ and prefixes.

Defining a Quantity
-------------------
Expand Down Expand Up @@ -434,7 +432,8 @@ You can also specify the format locale at the registry level either at creation:

.. doctest::

>>> ureg = UnitRegistry(fmt_locale='fr_FR')
>>> ureg = pint.UnitRegistry(fmt_locale='fr_FR')
>>> pint.set_application_registry(ureg)

or later:

Expand Down
4 changes: 2 additions & 2 deletions docs/user/angular_frequency.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ By default, pint treats angle quantities as `dimensionless`, so allows conversio

.. code-block:: python

>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> import pint
>>> ureg = pint.get_application_registry()
>>> angular_frequency = ureg('60rpm')
>>> angular_frequency.to('Hz')
<Quantity(6.28318531, 'hertz')>
Expand Down
7 changes: 4 additions & 3 deletions docs/user/contexts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ raise an error if you do this directly:
.. doctest::

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> q = 500 * ureg.nm
>>> q.to('Hz')
Traceback (most recent call last):
Expand Down Expand Up @@ -198,6 +198,7 @@ functions. For example:
.. doctest::

>>> ureg = pint.UnitRegistry()
>>> pint.set_application_registry(ureg)
>>> c = pint.Context('ab')
>>> c.add_transformation('[length]', '[time]',
... lambda ureg, x: x / ureg.speed_of_light)
Expand Down Expand Up @@ -258,7 +259,7 @@ Programmatically:

.. code-block:: python

>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> q = ureg.Quantity("1 BTU")
>>> q.to("J")
1055.056 joule
Expand All @@ -278,7 +279,7 @@ Or with a definitions file::

.. code-block:: python

>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> ureg.load_definitions("somefile.txt")
>>> q = ureg.Quantity("1 BTU")
>>> q.to("J")
Expand Down
5 changes: 3 additions & 2 deletions docs/user/defining-quantities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ quantity by multiplying a ``Unit()`` and a scalar:

.. doctest::

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> pint.set_application_registry(ureg)
>>> ureg.meter
<Unit('meter')>
>>> 30.0 * ureg.meter
Expand Down
3 changes: 2 additions & 1 deletion docs/user/formatting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ where each part is optional and the order of these is arbitrary.
.. doctest::

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> q = 2.3e-6 * ureg.m ** 3 / (ureg.s ** 2 * ureg.kg)
>>> f"{q:~P}" # short pretty
'2.3×10⁻⁶ m³/kg/s²'
Expand Down Expand Up @@ -67,6 +67,7 @@ Modifier Meaning Example
``#`` Call :py:meth:`Quantity.to_compact` first ``1.0 m·mg/s²`` (``f"{q:#~P}"``)
======== =================================================== ================================


Unit modifiers
--------------

Expand Down
11 changes: 7 additions & 4 deletions docs/user/nonmult.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ For example, to convert from celsius to fahrenheit:

.. doctest::

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> pint.set_application_registry(ureg)
>>> ureg.default_format = '.3f'
>>> Q_ = ureg.Quantity
>>> home = Q_(25.4, ureg.degC)
Expand Down Expand Up @@ -113,7 +114,8 @@ to be explicitly created:

.. doctest::

>>> ureg = UnitRegistry()
>>> ureg = pint.UnitRegistry()
>>> pint.set_application_registry(ureg)
>>> home = 25.4 * ureg.degC
Traceback (most recent call last):
...
Expand All @@ -130,7 +132,8 @@ to true. In this mode, pint behaves differently:

.. doctest::

>>> ureg = UnitRegistry(autoconvert_offset_to_baseunit = True)
>>> ureg = pint.UnitRegistry(autoconvert_offset_to_baseunit = True)
>>> pint.set_application_registry(ureg)
>>> T = 25.4 * ureg.degC
>>> T
<Quantity(25.4, 'degree_Celsius')>
Expand Down
2 changes: 1 addition & 1 deletion docs/user/numpy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"# Import Pint\n",
"import pint\n",
"\n",
"ureg = pint.UnitRegistry()\n",
"ureg = pint.get_application_registry()\n",
"Q_ = ureg.Quantity\n",
"\n",
"# Silence NEP 18 warning\n",
Expand Down
10 changes: 5 additions & 5 deletions docs/user/plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ a **UnitRegistry**:
.. testsetup:: *

import pint
ureg = pint.UnitRegistry()
ureg = pint.get_application_registry()

.. doctest::

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> ureg.setup_matplotlib()

This support can also be disabled with:
Expand All @@ -36,7 +36,7 @@ This allows plotting quantities with different units:
import numpy as np
import pint

ureg = pint.UnitRegistry()
ureg = pint.get_application_registry()
ureg.setup_matplotlib(True)

y = np.linspace(0, 30) * ureg.miles
Expand All @@ -56,7 +56,7 @@ This also allows controlling the actual plotting units for the x and y axes:
import numpy as np
import pint

ureg = pint.UnitRegistry()
ureg = pint.get_application_registry()
ureg.setup_matplotlib(True)

y = np.linspace(0, 30) * ureg.miles
Expand All @@ -79,7 +79,7 @@ Users have the possibility to change the format of the units on the plot:
import numpy as np
import pint

ureg = pint.UnitRegistry()
ureg = pint.get_application_registry()
ureg.setup_matplotlib(True)

ureg.mpl_formatter = "{:~P}"
Expand Down
1 change: 1 addition & 0 deletions docs/user/systems.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Pint Unit Registry has the concept of system, which is a group of units

>>> import pint
>>> ureg = pint.UnitRegistry(system='mks')
>>> pint.set_application_registry(ureg)
>>> ureg.default_system
'mks'

Expand Down
Loading