From cd7ebe198b3f33adf1dfa1f7759613163b9c2335 Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Tue, 10 Oct 2017 09:29:50 +0100 Subject: [PATCH 1/2] Remove BitwiseInt class and tests thereof. --- lib/iris/fileformats/pp.py | 124 +------------------------------ lib/iris/tests/test_pp_module.py | 109 --------------------------- 2 files changed, 2 insertions(+), 231 deletions(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 296f9e6133..9bce85f950 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -538,127 +538,6 @@ def __ge__(self, other): return self._compare(other, operator.ge) -class BitwiseInt(SplittableInt): - """ - A class to hold an integer, of fixed bit-length, which can easily get/set - each bit individually. - - .. deprecated:: 1.8 - - Please use `int` instead. - - .. note:: - - Uses a fixed number of bits. - Will raise an Error when attempting to access an out-of-range flag. - - >>> a = BitwiseInt(511) - >>> a.flag1 - 1 - >>> a.flag8 - 1 - >>> a.flag128 - 1 - >>> a.flag256 - 1 - >>> a.flag512 - AttributeError: 'BitwiseInt' object has no attribute 'flag512' - >>> a.flag512 = 1 - AttributeError: Cannot set a flag that does not exist: flag512 - - """ - - def __init__(self, value, num_bits=None): - # intentionally empty docstring as all covered in the class docstring. - """ """ - warn_deprecated('BitwiseInt is deprecated - please use `int` instead.') - - SplittableInt.__init__(self, value) - self.flags = () - - # do we need to calculate the number of bits based on the given value? - self._num_bits = num_bits - if self._num_bits is None: - self._num_bits = 0 - while((value >> self._num_bits) > 0): - self._num_bits += 1 - else: - # make sure the number of bits is enough to store the given value. - if (value >> self._num_bits) > 0: - raise ValueError("Not enough bits to store value") - - self._set_flags_from_value() - - def _set_flags_from_value(self): - all_flags = [] - - # Set attributes "flag[n]" to 0 or 1 - for i in range(self._num_bits): - flag_name = 1 << i - flag_value = ((self._value >> i) & 1) - object.__setattr__(self, 'flag%d' % flag_name, flag_value) - - # Add to list off all flags - if flag_value: - all_flags.append(flag_name) - - self.flags = tuple(all_flags) - - def _set_value_from_flags(self): - self._value = 0 - for i in range(self._num_bits): - bit_value = pow(2, i) - flag_name = "flag%i" % bit_value - flag_value = object.__getattribute__(self, flag_name) - self._value += flag_value * bit_value - - def __iand__(self, value): - """Perform an &= operation.""" - self._value &= value - self._set_flags_from_value() - return self - - def __ior__(self, value): - """Perform an |= operation.""" - self._value |= value - self._set_flags_from_value() - return self - - def __iadd__(self, value): - """Perform an inplace add operation""" - self._value += value - self._set_flags_from_value() - return self - - def __setattr__(self, name, value): - # Allow setting of the attribute flags - # Are we setting a flag? - if name.startswith("flag") and name != "flags": - # true and false become 1 and 0 - if not isinstance(value, bool): - raise TypeError("Can only set bits to True or False") - - # Setting an existing flag? - if hasattr(self, name): - # which flag? - flag_value = int(name[4:]) - # on or off? - if value: - self |= flag_value - else: - self &= ~flag_value - - # Fail if an attempt has been made to set a flag that does not - # exist - else: - raise AttributeError("Cannot set a flag that does not" - " exist: %s" % name) - - # If we're not setting a flag, then continue as normal - else: - SplittableInt.__setattr__(self, name, value) - - def _make_flag_getter(value): def getter(self): warn_deprecated('The `flag` attributes are deprecated - please use ' @@ -693,7 +572,8 @@ def __new__(cls, classname, bases, class_dict): return type.__new__(cls, classname, bases, class_dict) -class _LBProc(six.with_metaclass(_FlagMetaclass, BitwiseInt)): +@six.add_metaclass(_FlagMetaclass) +class _LBProc(object): # Use a metaclass to define the `flag1`, `flag2`, `flag4, etc. # properties. def __init__(self, value): diff --git a/lib/iris/tests/test_pp_module.py b/lib/iris/tests/test_pp_module.py index 542143639e..e3106f95e4 100644 --- a/lib/iris/tests/test_pp_module.py +++ b/lib/iris/tests/test_pp_module.py @@ -330,116 +330,7 @@ def test_save_single(self): self.assertEqual(self.file_checksum(temp_filename), self.file_checksum(filepath)) os.remove(temp_filename) - - -class TestBitwiseInt(tests.IrisTest): - - def test_3(self): - with mock.patch('warnings.warn') as warn: - t = pp.BitwiseInt(3) - self.assertEqual(warn.call_count, 1) - self.assertEqual(t[0], 3) - self.assertTrue(t.flag1) - self.assertTrue(t.flag2) - self.assertRaises(AttributeError, getattr, t, "flag1024") - - def test_setting_flags(self): - with mock.patch('warnings.warn') as warn: - t = pp.BitwiseInt(3) - self.assertEqual(warn.call_count, 1) - self.assertEqual(t._value, 3) - - t.flag1 = False - self.assertEqual(t._value, 2) - t.flag2 = False - self.assertEqual(t._value, 0) - - t.flag1 = True - self.assertEqual(t._value, 1) - t.flag2 = True - self.assertEqual(t._value, 3) - - self.assertRaises(AttributeError, setattr, t, "flag1024", True) - self.assertRaises(TypeError, setattr, t, "flag2", 1) - with mock.patch('warnings.warn') as warn: - t = pp.BitwiseInt(3, num_bits=11) - self.assertEqual(warn.call_count, 1) - t.flag1024 = True - self.assertEqual(t._value, 1027) - - def test_standard_operators(self): - with mock.patch('warnings.warn') as warn: - t = pp.BitwiseInt(323) - self.assertEqual(warn.call_count, 1) - - self.assertTrue(t == 323) - self.assertFalse(t == 324) - - self.assertFalse(t != 323) - self.assertTrue(t != 324) - - self.assertTrue(t >= 323) - self.assertFalse(t >= 324) - - self.assertFalse(t > 323) - self.assertTrue(t > 322) - - self.assertTrue(t <= 323) - self.assertFalse(t <= 322) - - self.assertFalse(t < 323) - self.assertTrue(t < 324) - - self.assertTrue(t in [323]) - self.assertFalse(t in [324]) - - def test_323(self): - with mock.patch('warnings.warn') as warn: - t = pp.BitwiseInt(323) - self.assertEqual(warn.call_count, 1) - self.assertRaises(AttributeError, getattr, t, 'flag0') - - self.assertEqual(t.flag1, 1) - self.assertEqual(t.flag2, 1) - self.assertEqual(t.flag4, 0) - self.assertEqual(t.flag8, 0) - self.assertEqual(t.flag16, 0) - self.assertEqual(t.flag32, 0) - self.assertEqual(t.flag64, 1) - self.assertEqual(t.flag128, 0) - self.assertEqual(t.flag256, 1) - - - def test_33214(self): - with mock.patch('warnings.warn') as warn: - t = pp.BitwiseInt(33214) - self.assertEqual(warn.call_count, 1) - self.assertEqual(t[0], 4) - self.assertEqual(t.flag1, 0) - self.assertEqual(t.flag2, 1) - - def test_negative_number(self): - with mock.patch('warnings.warn') as warn: - try: - _ = pp.BitwiseInt(-5) - except ValueError as err: - self.assertEqual(str(err), 'Negative numbers not supported with splittable integers object') - self.assertEqual(warn.call_count, 1) - - def test_128(self): - with mock.patch('warnings.warn') as warn: - t = pp.BitwiseInt(128) - self.assertEqual(warn.call_count, 1) - self.assertEqual(t.flag1, 0) - self.assertEqual(t.flag2, 0) - self.assertEqual(t.flag4, 0) - self.assertEqual(t.flag8, 0) - self.assertEqual(t.flag16, 0) - self.assertEqual(t.flag32, 0) - self.assertEqual(t.flag64, 0) - self.assertEqual(t.flag128, 1) - class TestSplittableInt(tests.IrisTest): From 1552a20d3ca83cc1ea660810a7648ed3e25137c7 Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Wed, 11 Oct 2017 15:47:30 +0100 Subject: [PATCH 2/2] Change _LBProc inheritance --- lib/iris/fileformats/pp.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 9bce85f950..f0decb3c62 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -572,8 +572,7 @@ def __new__(cls, classname, bases, class_dict): return type.__new__(cls, classname, bases, class_dict) -@six.add_metaclass(_FlagMetaclass) -class _LBProc(object): +class _LBProc(six.with_metaclass(_FlagMetaclass, SplittableInt)): # Use a metaclass to define the `flag1`, `flag2`, `flag4, etc. # properties. def __init__(self, value):