Skip to content

Commit

Permalink
Add Selector.union() classmethod, abbreviate long selector string in …
Browse files Browse the repository at this point in the history
…Selector.__repr__(), use SelectorMethods.collapse() in Selector when computing string corresponding to expanded selectors.
  • Loading branch information
lebedov committed Feb 12, 2015
1 parent b3ff89d commit baf2fc5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
37 changes: 34 additions & 3 deletions neurokernel/plsel.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self, s):
else:
self._expanded = tuple(SelectorMethods.expand(s))
self._max_levels = max(map(len, self._expanded))
#XXX
self._str = SelectorMethods.collapse(self._expanded)

@property
def nonempty(self):
Expand Down Expand Up @@ -117,7 +117,7 @@ def add(cls, *sels):
Returns
-------
result : Selector
Selector containing all of the port identifiers comprised by the
Selector containing all of the port identifiers comprised by all of the
arguments.
"""

Expand All @@ -136,6 +136,18 @@ def add(cls, *sels):
def concat(cls, *sels):
"""
Concatenate the identifiers in multiple selectors elementwise.
Parameters
----------
sels : Selector
Selector instances.
Returns
-------
result : Selector
Each port identifier in the returned Selector is equivalent to
the elementwise concatenation of the identifiers in the listed
Selector instances.
"""

out = cls('')
Expand Down Expand Up @@ -169,6 +181,22 @@ def prod(cls, *sels):
for i in itertools.product(*[s.expanded for s in sels]))
return out

@classmethod
def union(cls, *sels):
"""
Compute the union of the identifiers in multiple selectors.
"""

out = cls('')
out._expanded = \
tuple(sorted(reduce(lambda a, b: set(a.expanded).union(b.expanded), sels)))
try:
out._max_levels = max([s.max_levels for s in sels if s.nonempty])
except ValueError:
out._max_levels = 0
out._str = SelectorMethods.collapse(out._expanded)
return out

def __add__(self, y):
return self.add(self, y)

Expand All @@ -186,7 +214,10 @@ def __iter__(self):
yield ((),)

def __repr__(self):
return 'Selector(\'%s\')' % self._str
if len(self._str) <= 100:
return 'Selector(\'%s\')' % self._str
else:
return 'Selector(\'%s\')' % (self._str[0:25]+' ... '+self._str[-25:])

class SelectorParser(object):
"""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_plsel.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ def test_selector_iter(self):
self.assertSequenceEqual([s for s in sel],
[((),)])

def test_selector_union_empty(self):
a = Selector('')
b = Selector('')
c = Selector.union(a, b)
assert len(c) == 0
assert c.expanded == ((),)
assert c.max_levels == 0
assert c.str == ''

def test_selector_union_nonempty(self):
a = Selector('/x[0:3]')
b = Selector('/x[2:5]')
c = Selector.union(a, b)
assert len(c) == 5
assert c.expanded == (('x', 0), ('x', 1), ('x', 2), ('x', 3), ('x', 4))
assert c.max_levels == 2
assert c.str == '/x/0,/x/1,/x/2,/x/3,/x/4'

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

0 comments on commit baf2fc5

Please sign in to comment.