Repository: planetarypy/pvl

Issue:
Provide some additional functions for pvl.collection.Quantity objects.
**Is your feature request related to a problem? Please describe.**
Sometimes a caller will expect a particular key in the returned object, and will expect a numeric value but won't know if the value is a bare numeric value (of type float or int) or a Quantity object, but in any case won't care about the unit, and is only after the numeric value.  In this case, they need to perform an `isinstance()` test to know whether they can use the value as-is, or if they need to call the `.value` attribute.

**Describe the solution you'd like**
Implement `__float__()` and `__int__()` functions in the `pvl.collection.Quantity` class, so that a caller can just call `float(value)` or `int(value)` to get the contents of `Quantity.value`, as needed, or a harmless conversion of a float to a float (if the value isn't a Quantity object), for example.

**Describe alternatives you've considered**
The alternate solution is to do nothing and continue to force type testing behavior on users, but this results in more complex downstream code.  This simple change would streamline certain use cases.


Discussion:

Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 0303c97..51e2522 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -30,6 +30,13 @@ and the release date, in year-month-day format (see examples below).
 Unreleased
 ----------
 
+Added
++++++
+* pvl.collections.Quantity objects now have __int__() and __float__() functions that
+  will return the int and float versions of their .value parameter to facilitate numeric
+  operations with Quantity objects (Issue 91).
+
+
 1.2.1 (2021-05-31)
 ------------------
 
diff --git a/pvl/collections.py b/pvl/collections.py
index 5679915..5eebdbd 100644
--- a/pvl/collections.py
+++ b/pvl/collections.py
@@ -692,7 +692,11 @@ class Quantity(namedtuple("Quantity", ["value", "units"])):
     for how to use 3rd party Quantity objects with pvl.
     """
 
-    pass
+    def __int__(self):
+        return int(self.value)
+
+    def __float__(self):
+        return float(self.value)
 
 
 class Units(Quantity):


Test patch:
diff --git a/tests/test_collections.py b/tests/test_collections.py
index 61b5126..b7cb3e7 100644
--- a/tests/test_collections.py
+++ b/tests/test_collections.py
@@ -880,4 +880,28 @@ class TestMultiDict(unittest.TestCase):
             )
 
         except ImportError:
-            pass
\ No newline at end of file
+            pass
+
+
+class TestQuantity(unittest.TestCase):
+
+    def setUp(self):
+        self.d = pvl.loads("a = 2 <m> b = 1.21 <gW> c = nine <planets>")
+
+    def test_int(self):
+        a = int(self.d["a"])
+        self.assertEqual(2, a)
+
+        b = int(self.d["b"])
+        self.assertEqual(1, b)
+
+        self.assertRaises(ValueError, int, self.d["c"])
+
+    def test_float(self):
+        a = float(self.d["a"])
+        self.assertEqual(2.0, a)
+
+        b = float(self.d["b"])
+        self.assertEqual(1.21, b)
+
+        self.assertRaises(ValueError, float, self.d["c"])