Skip to content

Commit

Permalink
Make Selector class iterable (where the iterator returns valid select…
Browse files Browse the repository at this point in the history
…ors for each identifier comprised by the class instance).
  • Loading branch information
lebedov committed Feb 8, 2015
1 parent 4bc46ea commit d16f8e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions neurokernel/plsel.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ def __len__(self):
else:
return len(self._expanded)

def __iter__(self):
if self.nonempty:
for t in self._expanded:
yield (t,)
else:
yield ((),)

def __repr__(self):
return 'Selector(\'%s\')' % self._str

Expand Down
10 changes: 10 additions & 0 deletions tests/test_plsel.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ def test_selector_prod_nonempty(self):
('x', 1, 'a'), ('x', 1, 'b'), ('x', 1, 'c'))
assert s.str == '/x[0:2]+[a,b,c]'

def test_selector_iter(self):
sel = Selector('/x[0:3]')
self.assertSequenceEqual([s for s in sel],
[(('x', 0),),
(('x', 1),),
(('x', 2),)])
sel = Selector('')
self.assertSequenceEqual([s for s in sel],
[((),)])

class test_path_like_selector(TestCase):
def setUp(self):
self.df = df.copy()
Expand Down

0 comments on commit d16f8e4

Please sign in to comment.