From d145aa5664b3ac94645c9e6cf32848b5263e3f31 Mon Sep 17 00:00:00 2001 From: Michael Scott Cuthbert Date: Mon, 12 Oct 2015 10:43:29 -0400 Subject: [PATCH] fix recurse remove for list arguments --- music21/stream/__init__.py | 45 +++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/music21/stream/__init__.py b/music21/stream/__init__.py index c9853858b2..8b7f17e6f7 100644 --- a/music21/stream/__init__.py +++ b/music21/stream/__init__.py @@ -47,6 +47,7 @@ from music21 import spanner from music21 import tie from music21 import repeat +from music21 import sites from music21 import tempo from music21 import timespans @@ -927,31 +928,54 @@ def remove(self, does not work with recurse=True yet. >>> p1 = stream.Part() - >>> m1 = stream.Measure() + >>> m1 = stream.Measure(number=1) >>> c = clef.BassClef() >>> m1.insert(0, c) - >>> m1.append(note.Note()) + >>> m1.append(note.Note(type='whole')) >>> p1.append(m1) + >>> m2 = stream.Measure(number=2) + >>> n2 = note.Note('D', type='half') + >>> m2.append(n2) + >>> n3 = note.Note(type='half') + >>> m2.append(n3) + >>> p1.append(m2) >>> p1.show('text') - {0.0} + {0.0} {0.0} {0.0} + {4.0} + {0.0} + {2.0} Without recurse=True: - >>> p1.remove(c) + >>> p1.remove(n2) >>> p1.show('text') - {0.0} + {0.0} {0.0} {0.0} + {4.0} + {0.0} + {2.0} With recurse=True: - >>> p1.remove(c, recurse=True) + >>> p1.remove(n2, recurse=True) >>> p1.show('text') - {0.0} + {0.0} + {0.0} {0.0} + {4.0} + {2.0} + With recurse=True and a list to remove: + + >>> p1.remove([c, n3], recurse=True) + >>> p1.show('text') + {0.0} + {0.0} + {4.0} + ''' # TODO: Next to clean up... a doozy -- filter out all the different options. @@ -963,7 +987,12 @@ def remove(self, if not common.isListLike(targetOrList): targetList = [targetOrList] elif len(targetOrList) > 1: + try: targetList = sorted(targetOrList, key=self.elementOffset) + except sites.SitesException: + # if recurse, it's not such a big deal... + targetList = targetOrList + else: targetList = targetOrList @@ -982,7 +1011,7 @@ def remove(self, indexInStream = s.index(target) s.remove(target) break - except StreamException: + except (StreamException, sites.SitesException): continue continue # recursion matched or didn't or wasn't run. either way no need for rest...