From ddc6a1ba71d0527ef60588ff905cc1d0f8631891 Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Wed, 24 Mar 2021 00:14:03 -0400 Subject: [PATCH] sync up common math additions --- py27/bacpypes/primitivedata.py | 37 ++++++++++++++++++++++++++++++---- py34/bacpypes/primitivedata.py | 7 ++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/py27/bacpypes/primitivedata.py b/py27/bacpypes/primitivedata.py index 2fad74ba..8921ac6f 100755 --- a/py27/bacpypes/primitivedata.py +++ b/py27/bacpypes/primitivedata.py @@ -504,6 +504,35 @@ def is_valid(cls, arg): """Return True if arg is valid value for the class.""" raise NotImplementedError("call on a derived class of Atomic") +class CommonMath: + def __add__(self, other): + return self.value + other.value if isinstance(other, Atomic) else (self.value + other) + + def __sub__(self, other): + return self.value - other.value if isinstance(other, Atomic) else (self.value - other) + + def __mul__(self, other): + return self.value * other.value if isinstance(other, Atomic) else (self.value * other) + + def __lt__(self, other): + return self.value < other.value if isinstance(other, Atomic) else (self.value < other) + + def __gt__(self, other): + return self.value > other.value if isinstance(other, Atomic) else (self.value > other) + + def __le__(self, other): + return self.value <= other.value if isinstance(other, Atomic) else (self.value <= other) + + def __ge__(self, other): + return self.value >= other.value if isinstance(other, Atomic) else (self.value >= other) + + def __ne__(self, other): + return self.value != other.value if isinstance(other, Atomic) else (self.value != other) + + def __eq__(self, other): + return self.value == other.value if isinstance(other, Atomic) else (self.value == other) + + # # Null # @@ -596,7 +625,7 @@ def __str__(self): # Unsigned # -class Unsigned(Atomic): +class Unsigned(Atomic, CommonMath): _app_tag = Tag.unsignedAppTag _low_limit = 0 @@ -688,7 +717,7 @@ class Unsigned16(Unsigned): # Integer # -class Integer(Atomic): +class Integer(Atomic, CommonMath): _app_tag = Tag.integerAppTag @@ -763,7 +792,7 @@ def __str__(self): # Real # -class Real(Atomic): +class Real(Atomic, CommonMath): _app_tag = Tag.realAppTag @@ -808,7 +837,7 @@ def __str__(self): # Double # -class Double(Atomic): +class Double(Atomic, CommonMath): _app_tag = Tag.doubleAppTag diff --git a/py34/bacpypes/primitivedata.py b/py34/bacpypes/primitivedata.py index 4cbbd5c0..32f6a146 100755 --- a/py34/bacpypes/primitivedata.py +++ b/py34/bacpypes/primitivedata.py @@ -515,13 +515,13 @@ def __mul__(self, other): def __lt__(self, other): return self.value < other.value if isinstance(other, Atomic) else (self.value < other) - + def __gt__(self, other): return self.value > other.value if isinstance(other, Atomic) else (self.value > other) - + def __le__(self, other): return self.value <= other.value if isinstance(other, Atomic) else (self.value <= other) - + def __ge__(self, other): return self.value >= other.value if isinstance(other, Atomic) else (self.value >= other) @@ -531,6 +531,7 @@ def __ne__(self, other): def __eq__(self, other): return self.value == other.value if isinstance(other, Atomic) else (self.value == other) + # # Null #