From 1a682bcddfaf6ac3e2fa8d650732c824bbc30bb0 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 14 Mar 2012 21:33:06 -0400 Subject: [PATCH] Add rich comparisons to BitwiseOperator (compare by .value). --- pycomedi/constant.pyx | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/pycomedi/constant.pyx b/pycomedi/constant.pyx index 91e9bf3..0edf64b 100644 --- a/pycomedi/constant.pyx +++ b/pycomedi/constant.pyx @@ -73,6 +73,26 @@ also start a bitwise chain with an integer. >>> 64 | TRIG_SRC.now +Rich comparisons with other flags and integers are also supported. + +>>> 64 > TRIG_SRC.now +True +>>> TRIG_SRC.now > 64 +False +>>> TRIG_SRC.now >= 2 +True +>>> TRIG_SRC.now >= 3 +False +>>> import copy +>>> TRIG_SRC.now == copy.deepcopy(TRIG_SRC.now) +True +>>> TRIG_SRC.now != TRIG_SRC.now +False +<<< TRIG_SRC.now <= 2 +True +<<< TRIG_SRC.now < 3 +False + .. [#ops] See `emulating numeric types`_ and `NotImplementedError` in `the standard type hierarchy`_. @@ -150,6 +170,30 @@ cdef class BitwiseOperator (object): s,o = BitwiseOperator._prepare_self_other(self, other) return BitwiseOperator(int(long.__or__(s, o))) + def __lt__(self, other): + s,o = BitwiseOperator._prepare_self_other(self, other) + return s < o + + def __le__(self, other): + s,o = BitwiseOperator._prepare_self_other(self, other) + return s <= o + + def __eq__(self, other): + s,o = BitwiseOperator._prepare_self_other(self, other) + return s == o + + def __ne__(self, other): + s,o = BitwiseOperator._prepare_self_other(self, other) + return s != o + + def __gt__(self, other): + s,o = BitwiseOperator._prepare_self_other(self, other) + return s > o + + def __ge__(self, other): + s,o = BitwiseOperator._prepare_self_other(self, other) + return s >= o + class _NamedInt (BitwiseOperator): "A flag or enum item." -- 2.26.2