From 9dda3f22ae6722fd03cfb74d15f114a8adee00f6 Mon Sep 17 00:00:00 2001 From: Michael Scott Cuthbert Date: Sat, 3 Oct 2015 22:35:00 -0400 Subject: [PATCH] add filter repr and restore reset iterator when filter added. --- music21/stream/filter.py | 29 +++++++++++++++++++++++++++-- music21/stream/iterator.py | 15 +++++++++------ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/music21/stream/filter.py b/music21/stream/filter.py index e14c695bff..c531b45ca6 100644 --- a/music21/stream/filter.py +++ b/music21/stream/filter.py @@ -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() + '' + ''' + return '<{0}.{1} '.format(self.__module__, self.__class__.__name__) + '{0}>' + class IsFilter(StreamFilter): derivationStr = 'is' @@ -138,7 +148,8 @@ class ClassFilter(StreamFilter): >>> sI.filters.append(stream.filter.ClassFilter('Note')) >>> sI.filters - [] + [] + >>> for x in sI: ... print(x) @@ -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): ''' @@ -171,7 +195,8 @@ class ClassNotFilter(ClassFilter): >>> sI.filters.append(stream.filter.ClassNotFilter('Note')) >>> sI.filters - [] + [] + >>> for x in sI: ... print(x) diff --git a/music21/stream/iterator.py b/music21/stream/iterator.py index fbc37e0453..4ed4adff5f 100644 --- a/music21/stream/iterator.py +++ b/music21/stream/iterator.py @@ -302,7 +302,9 @@ def matchingElements(self): >>> sI.notes is sI True - + >>> sI.filters + [] + >>> sI.matchingElements() [, , , , , @@ -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):