Repository: yt-project/unyt
Pull request #151 (2020-05-03 13:52:30)

Add special case when printing degree Celsius and fix spelling mistake


Patch:
diff --git a/docs/usage.rst b/docs/usage.rst
index 8e851e8..f962e4e 100644
--- a/docs/usage.rst
+++ b/docs/usage.rst
@@ -332,6 +332,30 @@ using the :meth:`@accepts <unyt.dimensions.accepts>` and :meth:`@returns <unyt.d
 .. note::
    Using these decorators may incur some performance overhead, especially for small arrays.
 
+Temperature Units
+-----------------
+
+The temperature unit degree Celsius has the symbol °C, but since the degree character
+is an invalid Python identifier, Uynt uses the symbol `degC`. Printing a degree Celsius
+quantity will show the correct symbol.
+
+  >>> from unyt import degC
+  >>> Ta = 23*degC
+  >>> print(Ta)
+  23 °C
+
+The `degC` symbol has alternative names `degree_Celsius`, `Celsius` and `°C`.
+
+  >>> from unyt import degree_Celsius, unyt_array
+  >>> Ta = 23*degree_Celsius
+  >>> print(Ta)
+  23 °C
+  >>> Ta = unyt_array([-40, 23, 70], '°C')
+  >>> print(Ta)
+  [-40  23  70] °C
+
+These comments also apply to degree Fahrenheit.
+
 Unit Conversions and Unit Systems
 +++++++++++++++++++++++++++++++++
 
diff --git a/unyt/_parsing.py b/unyt/_parsing.py
index 2ce4393..8dc8be1 100644
--- a/unyt/_parsing.py
+++ b/unyt/_parsing.py
@@ -87,6 +87,7 @@ def parse_unyt_expr(unit_expr):
     # Avoid a parse error if someone uses the percent unit and the
     # parser tries to interpret it as the modulo operator
     unit_expr = unit_expr.replace("%", "percent")
+    unit_expr = unit_expr.replace("°", "deg")
     try:
         unit_expr = parse_expr(
             unit_expr, global_dict=global_dict, transformations=unit_text_transform
diff --git a/unyt/_unit_lookup_table.py b/unyt/_unit_lookup_table.py
index 6d165bc..8970d6a 100644
--- a/unyt/_unit_lookup_table.py
+++ b/unyt/_unit_lookup_table.py
@@ -414,13 +414,13 @@ default_unit_name_alternatives = OrderedDict(
         ("Wb", ("weber",)),
         ("lm", ("lumen",)),
         ("lx", ("lux",)),
-        ("degC", ("degree_celsius", "celcius")),
+        ("degC", ("degree_celsius", "degree_Celsius", "celcius", "celsius", "°C")),
         # Imperial and other non-metric units
         ("inch", ("in",)),
         ("ft", ("foot",)),
         ("yd", ("yard",)),
         ("furlong", ("fur",)),
-        ("degF", ("degree_fahrenheit", "fahrenheit")),
+        ("degF", ("degree_fahrenheit", "degree_Fahrenheit", "fahrenheit", "°F")),
         ("R", ("degree_rankine", "rankine")),
         ("lbf", ("pound_force",)),
         ("lb", ("pound", "pound_mass", "lbm")),
diff --git a/unyt/unit_object.py b/unyt/unit_object.py
index fad605f..44d48cb 100644
--- a/unyt/unit_object.py
+++ b/unyt/unit_object.py
@@ -331,8 +331,13 @@ class Unit(object):
     def __str__(self):
         if self.expr == sympy_one:
             return "dimensionless"
+        unit_str = self.expr.__str__()
+        if unit_str == "degC":
+            return "°C"
+        if unit_str == "degF":
+            return "°F"
         # @todo: don't use dunder method?
-        return self.expr.__str__()
+        return unit_str
 
     #
     # Start unit operations


Test patch:
diff --git a/unyt/tests/test_mpl_interface.py b/unyt/tests/test_mpl_interface.py
index 8181a6f..ea57f63 100644
--- a/unyt/tests/test_mpl_interface.py
+++ b/unyt/tests/test_mpl_interface.py
@@ -44,8 +44,8 @@ def test_label(ax):
 def test_convert_unit(ax):
     x = [0, 1, 2] * s
     y = [1000, 2000, 3000] * K
-    ax.plot(x, y, yunits="Celcius")
-    expected = y.to("Celcius")
+    ax.plot(x, y, yunits="Celsius")
+    expected = y.to("Celsius")
     line = ax.lines[0]
     original_y_array = line.get_data()[1]
     converted_y_array = line.convert_yunits(original_y_array)
diff --git a/unyt/tests/test_units.py b/unyt/tests/test_units.py
index 4f50cc8..cc37ee7 100644
--- a/unyt/tests/test_units.py
+++ b/unyt/tests/test_units.py
@@ -829,3 +829,21 @@ def test_bel_neper():
 
 def test_henry():
     assert (Unit("H") / Unit("Ω")).dimensions == time
+
+
+def test_degC():
+    assert Unit("degree_celsius") == Unit("degC")
+    assert Unit("degree_Celsius") == Unit("degC")
+    assert Unit("Celsius") == Unit("degC")
+    assert Unit("°C") == Unit("degC")
+    a = 1 * Unit("degC")
+    assert str(a) == "1 °C"
+
+
+def test_degF():
+    assert Unit("degree_fahrenheit") == Unit("degF")
+    assert Unit("degree_Fahrenheit") == Unit("degF")
+    assert Unit("Fahrenheit") == Unit("degF")
+    assert Unit("°F") == Unit("degF")
+    a = 1 * Unit("degF")
+    assert str(a) == "1 °F"