Skip to content

Commit

Permalink
Add crude working collapse() method to SelectorMethods.
Browse files Browse the repository at this point in the history
  • Loading branch information
lebedov committed Feb 8, 2015
1 parent d16f8e4 commit aa34ee0
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions neurokernel/plsel.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ def __init__(self, s):
self._str = copy.copy(s._str)
self._expanded = copy.copy(s._expanded)
self._max_levels = copy.copy(s._max_levels)
else:
assert isinstance(s, basestring) # python2 dependency
elif isinstance(s, basestring): # python2 dependency
self._str = copy.copy(s)

# Save expanded selector as tuple because it shouldn't need to be
# modified after expansion:
self._expanded = tuple(SelectorMethods.expand(s))
self._max_levels = max(map(len, self._expanded))
else:
self._expanded = tuple(SelectorMethods.expand(s))
self._max_levels = max(map(len, self._expanded))
#XXX

@property
def nonempty(self):
Expand Down Expand Up @@ -913,7 +916,45 @@ def are_consecutive(int_list):
return False

@classmethod
def collapse(cls, id_list):
def collapse(cls, selector):
"""
Collapse a selector into a single string.
Parameters
----------
selector : iterable
Expanded selector. If the selector is a string, it is returned
unchanged.
Returns
-------
s : str
String that comprises all identifiers in the specified expanded
selector.
"""

if isinstance(selector, basestring):
return selector

result_list = []
for t in selector:
s = ''
for x in t:
if type(x) in [str, unicode, int]:
s += '/'+str(x)
elif type(x) == slice:
start = str(x.start) if x.start is not None else ''
stop = str(x.stop) if x.stop is not None else ''
s += '[%s:%s]' % (start, stop)
elif type(x) in [tuple, list]:
s += '['+','.join(x)+']'
else:
raise ValueError('invalid selector')
result_list.append(s)
return ','.join(result_list)

@classmethod
def _collapse(cls, id_list):
"""
Collapse a list of identifiers into a selector string.
Expand All @@ -933,7 +974,9 @@ def collapse(cls, id_list):
number of levels.
"""

# XXX doesn't collapse expanded selectors such as /foo/xxx,/bar/yyy properly
# XXX doesn't collapse expanded selectors such as /foo/xxx,/bar/yyy
# properly
raise NotImplemented('unfinished method - should eventually replace collapse()')

# Can only collapse list identifiers that all have the same number of
# levels:
Expand Down

0 comments on commit aa34ee0

Please sign in to comment.