From 7082cbb1d05f684e674949edd87fef10e655f49a Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 19 Sep 2022 23:54:27 +0200 Subject: No longer create registry in public module --- szilagyi/__init__.py | 11 ++++------- szilagyi/tests.py | 28 +++++++++++++++++----------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/szilagyi/__init__.py b/szilagyi/__init__.py index 8d294d7..67da4a8 100644 --- a/szilagyi/__init__.py +++ b/szilagyi/__init__.py @@ -13,14 +13,11 @@ There is only one function provided, so the use should be straight-forward. For -4.88 """ -from pint import UnitRegistry from pint.quantity import Quantity from ._nomogram import calculate_swi as _calculate_swi from ._nomogram import MAX_TEMPERATURE_DIFFERENCE, MAX_CONVECTIVE_CLOUD_DEPTH -_registry = UnitRegistry() - def calculate_swi(temperature_difference: Quantity, convective_cloud_depth: Quantity) -> float: """ @@ -30,12 +27,12 @@ def calculate_swi(temperature_difference: Quantity, convective_cloud_depth: Quan of the original nomogram. The maximum values are available as MAX_TEMPERATURE_DIFFERENCE and MAX_CONVECTIVE_CLOUD_DEPTH constants. """ - if not temperature_difference.is_compatible_with(_registry.C): + if not temperature_difference.is_compatible_with("C"): raise ValueError("temperature_difference must be convertible to Celsius degrees") - if not convective_cloud_depth.is_compatible_with(_registry.ft): + if not convective_cloud_depth.is_compatible_with("ft"): raise ValueError("convective_cloud_depth must be convertible to Feet") - temperature_difference = temperature_difference.m_as(_registry.C) - convective_cloud_depth = convective_cloud_depth.m_as(_registry.ft) + temperature_difference = temperature_difference.m_as("C") + convective_cloud_depth = convective_cloud_depth.m_as("ft") if temperature_difference < 0 or temperature_difference > MAX_TEMPERATURE_DIFFERENCE: raise ValueError(f"temperature_difference must be within <0, {MAX_TEMPERATURE_DIFFERENCE}> range") if convective_cloud_depth < 0 or convective_cloud_depth > MAX_CONVECTIVE_CLOUD_DEPTH: diff --git a/szilagyi/tests.py b/szilagyi/tests.py index bf59873..40d82f7 100644 --- a/szilagyi/tests.py +++ b/szilagyi/tests.py @@ -1,16 +1,22 @@ import unittest +import pint from . import calculate_swi class CalculateSwi(unittest.TestCase): + def setUp(self): + ureg = pint.UnitRegistry() + self.C = ureg.C + self.ft = ureg.ft + def test_out_of_range(self): samples = [ - (20, -10), - (-10, 25000), - (20, 60000), - (41, 25000), - (41, -10), + (20 * self.C, -10 * self.ft), + (-10 * self.C, 25000 * self.ft), + (20 * self.C, 60000 * self.ft), + (41 * self.C, 25000 * self.ft), + (41 * self.C, -10 * self.ft), ] for dt, depth in samples: with self.assertRaises(ValueError): @@ -18,12 +24,12 @@ class CalculateSwi(unittest.TestCase): def test_between(self): samples = [ - (6.53, 30000, 1, 2), - (10.78, 20500, -1, 0), - (24.45, 2600, 0, 1), - (7.22, 1300, -10, -9), - (16.1, 6200, 0, 1), - (3, 20000, -4, -3), + (6.53 * self.C, 30000 * self.ft, 1, 2), + (10.78 * self.C, 20500 * self.ft, -1, 0), + (24.45 * self.C, 2600 * self.ft, 0, 1), + (7.22 * self.C, 1300 * self.ft, -10, -9), + (16.1 * self.C, 6200 * self.ft, 0, 1), + (3 * self.C, 20000 * self.ft, -4, -3), ] for dt, depth, low, high in samples: swi = calculate_swi(dt, depth) -- cgit v1.1