Skip to content

Commit

Permalink
add filter repr and restore reset iterator when filter added.
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert committed Oct 4, 2015
1 parent 98171ee commit 9dda3f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
29 changes: 27 additions & 2 deletions music21/stream/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def __init__(self):
#def reset(self):
# pass

def _reprHead(self):
'''
returns a head that can be used with .format() to add additional
elements.
>>> stream.filter.StreamFilter()._reprHead()
'<music21.stream.filter.StreamFilter {0}>'
'''
return '<{0}.{1} '.format(self.__module__, self.__class__.__name__) + '{0}>'


class IsFilter(StreamFilter):
derivationStr = 'is'
Expand Down Expand Up @@ -138,7 +148,8 @@ class ClassFilter(StreamFilter):
>>> sI.filters.append(stream.filter.ClassFilter('Note'))
>>> sI.filters
[<music21.stream.filter.ClassFilter object at 0x...>]
[<music21.stream.filter.ClassFilter Note>]
>>> for x in sI:
... print(x)
<music21.note.Note C>
Expand All @@ -154,10 +165,23 @@ def __init__(self, classList=()):
classList = (classList,)

self.classList = classList

def __eq__(self, other):
if other.__class__ is not self.__class__:
return False
if self.classList != other.classList:
return False
return True

def __call__(self, item, iterator):
return item.isClassOrSubclass(self.classList)

def __repr__(self):
if len(self.classList) == 1:
return self._reprHead().format(str(self.classList[0]))
else:
return self._reprHead().format(str(self.classList))


class ClassNotFilter(ClassFilter):
'''
Expand All @@ -171,7 +195,8 @@ class ClassNotFilter(ClassFilter):
>>> sI.filters.append(stream.filter.ClassNotFilter('Note'))
>>> sI.filters
[<music21.stream.filter.ClassNotFilter object at 0x...>]
[<music21.stream.filter.ClassNotFilter Note>]
>>> for x in sI:
... print(x)
<music21.note.Rest rest>
Expand Down
15 changes: 9 additions & 6 deletions music21/stream/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ def matchingElements(self):
<music21.stream.iterator.StreamIterator for Part:tn3/4 @:0>
>>> sI.notes is sI
True
>>> sI.filters
[<music21.stream.filter.ClassFilter NotRest>]
>>> sI.matchingElements()
[<music21.note.Note C>, <music21.note.Note D>,
<music21.note.Note E>, <music21.note.Note F>, <music21.note.Note G>,
Expand Down Expand Up @@ -529,13 +531,14 @@ def addFilter(self, newFilter):
'''
adds a filter to the list.
resets caches -- do not add filters any other way
# TODO: support remove filter.
resets caches -- do not add filters any other way
'''
if newFilter not in self.filters:
self.filters.append(newFilter)
for f in self.filters:
if newFilter == f:
return self
self.filters.append(newFilter)

self.resetCaches()
return self

def removeFilter(self, oldFilter):
Expand Down

0 comments on commit 9dda3f2

Please sign in to comment.