From 8671676140a38b851b92f6acf2a5326d030e32d4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Feb 2024 11:07:02 +0000 Subject: [PATCH 1/7] use application reg in docs --- docs/advanced/performance.rst | 2 +- docs/advanced/serialization.rst | 4 ++-- docs/getting/overview.rst | 2 +- docs/user/contexts.rst | 8 ++++---- docs/user/formatting.rst | 4 ++-- docs/user/numpy.ipynb | 2 +- docs/user/plotting.rst | 10 +++++----- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/advanced/performance.rst b/docs/advanced/performance.rst index d7b8a0cd5..7e373b21f 100644 --- a/docs/advanced/performance.rst +++ b/docs/advanced/performance.rst @@ -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/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/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/user/contexts.rst b/docs/user/contexts.rst index 4077503fd..a008087c3 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): @@ -197,7 +197,7 @@ functions. For example: .. doctest:: - >>> ureg = pint.UnitRegistry() + >>> ureg = pint.get_application_registry() >>> c = pint.Context('ab') >>> c.add_transformation('[length]', '[time]', ... lambda ureg, x: x / ureg.speed_of_light) @@ -258,7 +258,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 +278,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/formatting.rst b/docs/user/formatting.rst index 7b0f15b68..176a2a325 100644 --- a/docs/user/formatting.rst +++ b/docs/user/formatting.rst @@ -26,7 +26,7 @@ In case the format is omitted, the corresponding value in the object's .. ipython:: - In [1]: ureg = pint.UnitRegistry() + In [1]: ureg = pint.get_application_registry() ...: ureg.default_format = "~P" In [2]: u = ureg.Unit("m ** 2 / s ** 2") @@ -71,7 +71,7 @@ Let's look at some examples: .. ipython:: python - ureg = pint.UnitRegistry() + ureg = pint.get_application_registry() u = ureg.kg * ureg.m / ureg.s ** 2 f"{u:P}" # using the pretty format 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}" From 2dee19966197e1a4348bf281446792a79b5f97fa Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Feb 2024 11:13:14 +0000 Subject: [PATCH 2/7] context docs --- docs/user/contexts.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/user/contexts.rst b/docs/user/contexts.rst index a008087c3..c3a62a317 100644 --- a/docs/user/contexts.rst +++ b/docs/user/contexts.rst @@ -197,7 +197,8 @@ functions. For example: .. doctest:: - >>> ureg = pint.get_application_registry() + >>> 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) From c2ca8f8fe881bc033c5fc41c29a424db6dd0889f Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Feb 2024 12:38:54 +0000 Subject: [PATCH 3/7] docs --- docs/advanced/defining.rst | 14 ++++++++------ docs/advanced/measurement.rst | 5 +++-- docs/advanced/performance.rst | 4 ++-- docs/advanced/pitheorem.rst | 8 ++++---- docs/advanced/wrapping.rst | 4 ++-- docs/getting/tutorial.rst | 15 +++++++-------- docs/user/angular_frequency.rst | 4 ++-- docs/user/defining-quantities.rst | 4 ++-- docs/user/nonmult.rst | 10 ++++++---- 9 files changed, 36 insertions(+), 32 deletions(-) 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 7e373b21f..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') 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/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/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/defining-quantities.rst b/docs/user/defining-quantities.rst index e40b08cf9..3bdee208d 100644 --- a/docs/user/defining-quantities.rst +++ b/docs/user/defining-quantities.rst @@ -16,8 +16,8 @@ quantity by multiplying a ``Unit()`` and a scalar: .. doctest:: - >>> from pint import UnitRegistry - >>> ureg = UnitRegistry() + >>> import pint + >>> ureg = pint.UnitRegistry() >>> ureg.meter >>> 30.0 * ureg.meter diff --git a/docs/user/nonmult.rst b/docs/user/nonmult.rst index a649d2ad1..dc6b64f8a 100644 --- a/docs/user/nonmult.rst +++ b/docs/user/nonmult.rst @@ -16,8 +16,8 @@ For example, to convert from celsius to fahrenheit: .. doctest:: - >>> from pint import UnitRegistry - >>> ureg = UnitRegistry() + >>> import pint + >>> ureg = pint.get_application_registry() >>> ureg.default_format = '.3f' >>> Q_ = ureg.Quantity >>> home = Q_(25.4, ureg.degC) @@ -113,7 +113,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 +131,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 From 245d1e87f4876da1029a63469825ca5108909b14 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Feb 2024 12:45:44 +0000 Subject: [PATCH 4/7] docs --- docs/user/nonmult.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/user/nonmult.rst b/docs/user/nonmult.rst index dc6b64f8a..1035e04bd 100644 --- a/docs/user/nonmult.rst +++ b/docs/user/nonmult.rst @@ -17,7 +17,8 @@ For example, to convert from celsius to fahrenheit: .. doctest:: >>> import pint - >>> ureg = pint.get_application_registry() + >>> ureg = pint.UnitRegistry() + >>> pint.set_application_registry(ureg) >>> ureg.default_format = '.3f' >>> Q_ = ureg.Quantity >>> home = Q_(25.4, ureg.degC) From b2a349f78410953fc07b31706c0918d95a2e4a71 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Feb 2024 12:54:32 +0000 Subject: [PATCH 5/7] docs --- docs/user/formatting.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/user/formatting.rst b/docs/user/formatting.rst index 72850480f..954cabb2d 100644 --- a/docs/user/formatting.rst +++ b/docs/user/formatting.rst @@ -69,6 +69,7 @@ Modifier Meaning Example Unit modifiers +-------------- ======== =================================================== ================================ Modifier Meaning Example From 3fdbff1cbf75079d0706c7b86b23aa096877884c Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Feb 2024 12:59:29 +0000 Subject: [PATCH 6/7] docs --- docs/user/defining-quantities.rst | 1 + docs/user/systems.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/user/defining-quantities.rst b/docs/user/defining-quantities.rst index 3bdee208d..090b0f15d 100644 --- a/docs/user/defining-quantities.rst +++ b/docs/user/defining-quantities.rst @@ -18,6 +18,7 @@ quantity by multiplying a ``Unit()`` and a scalar: >>> import pint >>> ureg = pint.UnitRegistry() + >>> pint.set_application_registry(ureg) >>> ureg.meter >>> 30.0 * ureg.meter diff --git a/docs/user/systems.rst b/docs/user/systems.rst index 5a1c27b32..85ff096da 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' From 32f0bac2268d5603fe4e339bb367ea0ff701dffe Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Feb 2024 13:08:39 +0000 Subject: [PATCH 7/7] docs --- docs/user/systems.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user/systems.rst b/docs/user/systems.rst index 85ff096da..fb14dcae6 100644 --- a/docs/user/systems.rst +++ b/docs/user/systems.rst @@ -9,7 +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) + >>> pint.set_application_registry(ureg) >>> ureg.default_system 'mks'