From 6ccb132a0a86c49887681c5ab1c798a2a84cb5fd Mon Sep 17 00:00:00 2001 From: Candoran2 <45334438+Candoran2@users.noreply.github.com> Date: Sun, 19 Sep 2021 23:25:21 +0200 Subject: [PATCH] Added rich comparison functions for bitfield. --- source/bitfield.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/source/bitfield.py b/source/bitfield.py index fef752f59..3cab0e48f 100644 --- a/source/bitfield.py +++ b/source/bitfield.py @@ -34,13 +34,6 @@ def __hash__(self): def __int__(self): return self._value - def __eq__(self, other): - if isinstance(other, BasicBitfield): - return self._value == other._value - elif isinstance(other, int): - return self._value == other - return False - def __init__(self, value=None): super().__init__() if value is not None: @@ -61,6 +54,24 @@ def __str__(self): info += f"\n\t{field} = {str(val)}" return info + # rich comparison methods + def __lt__(self, other): + return self._value < other + + def __le__(self, other): + return self._value <= other + + def __eq__(self, other): + return self._value == other + + def __ne__(self, other): + return self._value != other + + def __gt__(self, other): + return self._value > other + + def __ge__(self, other): + return self._value >= other # basic arithmetic functions def __add__(self, other):