From: W. Trevor King Date: Thu, 15 Mar 2012 01:33:06 +0000 (-0400) Subject: Add rich comparisons to BitwiseOperator (compare by .value). X-Git-Tag: 0.4~6 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1a682bcddfaf6ac3e2fa8d650732c824bbc30bb0;p=pycomedi.git Add rich comparisons to BitwiseOperator (compare by .value). --- 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."