Skip to content

Commit

Permalink
implement and test del attr/item
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed May 4, 2013
1 parent dd4ad12 commit 0ce4ec9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Record/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,13 @@ def __setattr__(self, key, value):
except KeyError:
raise AttributeError(key)

def __delattr__(self, key):
self[key] = None

def __delitem__(self, key):
if isinstance(key, int):
raise TypeError('cannot delete record items')
self[key] = None

def __contains__(self, key):
return key in self._schema
8 changes: 8 additions & 0 deletions src/Record/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def test_attribute(self):
self.assertEqual(getattr(r, 'd', 2), 2)
self.assertRaises(AttributeError, getattr, r, 'd')
self.assertRaises(AttributeError, setattr, r, 'd', 2)
del r.a
self.assertTrue(r.a is None)
r.b = 2
delattr(r, 'b')
self.assertTrue(r.b is None)

def test_mapping(self):
r = R()
Expand All @@ -98,6 +103,9 @@ def test_mapping(self):
self.assertEqual(r.b, 42)
self.assertRaises(KeyError, r.__getitem__, 'd')
self.assertRaises(KeyError, r.__setitem__, 'd', 2)
del r['a']
self.assertTrue(r.a is None)
self.assertRaises(TypeError, r.__delitem__, 1)

def test_sequence(self):
r = R()
Expand Down

0 comments on commit 0ce4ec9

Please sign in to comment.