Define some magic methods for usertypes
This commit is contained in:
parent
2190d1bb49
commit
fd9f801cab
@ -40,6 +40,9 @@ class FakeDictTests(TestCase):
|
||||
with self.assertRaises(TypeError):
|
||||
self.fd["eggs"] = "bar"
|
||||
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(self.fd), "FakeDict('foo')")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
@ -37,6 +37,19 @@ class InitTests(TestCase):
|
||||
nl = NeighborList([1, 2, 3])
|
||||
self.assertEqual(nl.items, [1, 2, 3])
|
||||
|
||||
def test_len(self):
|
||||
nl = NeighborList([1, 2, 3])
|
||||
self.assertEqual(len(nl), 3)
|
||||
|
||||
def test_repr(self):
|
||||
nl = NeighborList([1, 2, 3])
|
||||
self.assertEqual(repr(nl), 'NeighborList([1, 2, 3])')
|
||||
|
||||
def test_contains(self):
|
||||
nl = NeighborList([1, 2, 3])
|
||||
self.assertIn(2, nl)
|
||||
self.assertNotIn(4, nl)
|
||||
|
||||
|
||||
class DefaultTests(TestCase):
|
||||
|
||||
|
@ -23,6 +23,7 @@ Module attributes:
|
||||
|
||||
import operator
|
||||
import logging
|
||||
import collections.abc
|
||||
|
||||
_UNSET = object()
|
||||
|
||||
@ -55,7 +56,7 @@ class EnumBase(type):
|
||||
return cls._mapping[key]
|
||||
|
||||
|
||||
class NeighborList:
|
||||
class NeighborList(collections.abc.Sequence):
|
||||
|
||||
"""A list of items which saves it current position.
|
||||
|
||||
@ -94,6 +95,15 @@ class NeighborList:
|
||||
self._mode = mode
|
||||
self.fuzzyval = None
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self._items[key]
|
||||
|
||||
def __len__(self):
|
||||
return len(self._items)
|
||||
|
||||
def __repr__(self):
|
||||
return '{}({})'.format(self.__class__.__name__, self._items)
|
||||
|
||||
def _snap_in(self, offset):
|
||||
"""Set the current item to the closest item to self.fuzzyval.
|
||||
|
||||
@ -208,3 +218,6 @@ class FakeDict:
|
||||
|
||||
def __getitem__(self, _key):
|
||||
return self._val
|
||||
|
||||
def __repr__(self):
|
||||
return "FakeDict('{}')".format(self._val)
|
||||
|
Loading…
Reference in New Issue
Block a user