diff --git a/docs/advanced/defining.rst b/docs/advanced/defining.rst index 43344bcaf..d10b7244f 100644 --- a/docs/advanced/defining.rst +++ b/docs/advanced/defining.rst @@ -53,9 +53,11 @@ 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 @@ -63,8 +65,7 @@ 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:: @@ -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 diff --git a/docs/advanced/measurement.rst b/docs/advanced/measurement.rst index a49c8212b..e18be3a39 100644 --- a/docs/advanced/measurement.rst +++ b/docs/advanced/measurement.rst @@ -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 diff --git a/docs/advanced/performance.rst b/docs/advanced/performance.rst index d7b8a0cd5..79104b185 100644 --- a/docs/advanced/performance.rst +++ b/docs/advanced/performance.rst @@ -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') @@ -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 diff --git a/docs/advanced/pitheorem.rst b/docs/advanced/pitheorem.rst index cd3716528..4212c1b7e 100644 --- a/docs/advanced/pitheorem.rst +++ b/docs/advanced/pitheorem.rst @@ -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 @@ -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}] diff --git a/docs/advanced/serialization.rst b/docs/advanced/serialization.rst index 581bc9d4b..c4c3990c1 100644 --- a/docs/advanced/serialization.rst +++ b/docs/advanced/serialization.rst @@ -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 @@ -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 diff --git a/docs/advanced/wrapping.rst b/docs/advanced/wrapping.rst index 21d8177c3..b80018f50 100644 --- a/docs/advanced/wrapping.rst +++ b/docs/advanced/wrapping.rst @@ -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 diff --git a/docs/getting/overview.rst b/docs/getting/overview.rst index 61dfc14f4..e57e3da6a 100644 --- a/docs/getting/overview.rst +++ b/docs/getting/overview.rst @@ -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 diff --git a/docs/getting/tutorial.rst b/docs/getting/tutorial.rst index bb3505b51..4dd7ddc3a 100644 --- a/docs/getting/tutorial.rst +++ b/docs/getting/tutorial.rst @@ -8,17 +8,15 @@ and perform unit conversions in Python. Initializing a Registry ----------------------- -Before using Pint, initialize a :class:`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 ------------------- @@ -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: diff --git a/docs/user/angular_frequency.rst b/docs/user/angular_frequency.rst index 4fbb7bdce..56a60eb73 100644 --- a/docs/user/angular_frequency.rst +++ b/docs/user/angular_frequency.rst @@ -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') diff --git a/docs/user/contexts.rst b/docs/user/contexts.rst index 4077503fd..c3a62a317 100644 --- a/docs/user/contexts.rst +++ b/docs/user/contexts.rst @@ -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): @@ -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) @@ -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 @@ -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") diff --git a/docs/user/defining-quantities.rst b/docs/user/defining-quantities.rst index e40b08cf9..090b0f15d 100644 --- a/docs/user/defining-quantities.rst +++ b/docs/user/defining-quantities.rst @@ -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 >>> 30.0 * ureg.meter diff --git a/docs/user/formatting.rst b/docs/user/formatting.rst index f17939a86..954cabb2d 100644 --- a/docs/user/formatting.rst +++ b/docs/user/formatting.rst @@ -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²' @@ -67,6 +67,7 @@ Modifier Meaning Example ``#`` Call :py:meth:`Quantity.to_compact` first ``1.0 m·mg/s²`` (``f"{q:#~P}"``) ======== =================================================== ================================ + Unit modifiers -------------- diff --git a/docs/user/nonmult.rst b/docs/user/nonmult.rst index a649d2ad1..1035e04bd 100644 --- a/docs/user/nonmult.rst +++ b/docs/user/nonmult.rst @@ -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) @@ -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): ... @@ -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 diff --git a/docs/user/numpy.ipynb b/docs/user/numpy.ipynb index 54910018e..6d9fce23d 100644 --- a/docs/user/numpy.ipynb +++ b/docs/user/numpy.ipynb @@ -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", diff --git a/docs/user/plotting.rst b/docs/user/plotting.rst index 3c3fc39c1..50bfcfaa1 100644 --- a/docs/user/plotting.rst +++ b/docs/user/plotting.rst @@ -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: @@ -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 @@ -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 @@ -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}" diff --git a/docs/user/systems.rst b/docs/user/systems.rst index 5a1c27b32..fb14dcae6 100644 --- a/docs/user/systems.rst +++ b/docs/user/systems.rst @@ -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'