Skip to content

Commit

Permalink
sync up common math additions
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Bender committed Mar 24, 2021
1 parent 979a7e3 commit ddc6a1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
37 changes: 33 additions & 4 deletions py27/bacpypes/primitivedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -596,7 +625,7 @@ def __str__(self):
# Unsigned
#

class Unsigned(Atomic):
class Unsigned(Atomic, CommonMath):

_app_tag = Tag.unsignedAppTag
_low_limit = 0
Expand Down Expand Up @@ -688,7 +717,7 @@ class Unsigned16(Unsigned):
# Integer
#

class Integer(Atomic):
class Integer(Atomic, CommonMath):

_app_tag = Tag.integerAppTag

Expand Down Expand Up @@ -763,7 +792,7 @@ def __str__(self):
# Real
#

class Real(Atomic):
class Real(Atomic, CommonMath):

_app_tag = Tag.realAppTag

Expand Down Expand Up @@ -808,7 +837,7 @@ def __str__(self):
# Double
#

class Double(Atomic):
class Double(Atomic, CommonMath):

_app_tag = Tag.doubleAppTag

Expand Down
7 changes: 4 additions & 3 deletions py34/bacpypes/primitivedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
#
Expand Down

0 comments on commit ddc6a1b

Please sign in to comment.