diff --git a/crank/util.py b/crank/util.py index 6126797..1f06f15 100644 --- a/crank/util.py +++ b/crank/util.py @@ -187,9 +187,10 @@ def method_matches_args(method, params, remainder, lax_params=False): class Path(collections.deque): def __init__(self, value='/', separator='/'): self.separator = separator - self._assign(value) - + super(Path, self).__init__() + + self._assign(value) def _assign(self, value): separator = self.separator @@ -212,3 +213,10 @@ class Path(collections.deque): def __repr__(self): return "" % super(Path, self).__repr__() + + def __getitem__(self, i): + try: + return super(Path, self).__getitem__(i) + + except TypeError: + return Path([self[i] for i in xrange(*i.indices(len(self)))]) diff --git a/tests/test_util.py b/tests/test_util.py index ab95c4e..9dbb89e 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -237,3 +237,16 @@ def test_path_unicode(): instance.path = case yield assert_path, instance, expected, unicode + +def test_path_slicing(): + class MockOb(object): + path = Path() + + instance = MockOb() + + instance.path = '/foo/bar/baz' + + assert str(instance.path[1:]) == 'foo/bar/baz' + assert str(instance.path[2:]) == 'bar/baz' + assert str(instance.path[0:2]) == '/foo' + assert str(instance.path[::2]) == '/bar'