Skip to content

Commit

Permalink
Merge branch 'master' into configure_fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert committed May 29, 2019
2 parents ae86b53 + 0f83478 commit c4b5b1c
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions music21/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,21 @@ def getContextByClass(self,
>>> a = b.getContextByClass('Note', getElementMethod='getElementBefore')
>>> a
<music21.note.Note A>
# debugging a consistent error.
>>> for site, positionStart, searchType in b.contextSites(
... returnSortTuples=True,
... sortByCreationTime=False,
... followDerivation=True
... ):
... print(site, positionStart, searchType)
<music21.stream.Measure 3 offset=5.0> SortTuple(atEnd=0, offset=1.0, ...) elementsFirst
<music21.stream.Part 0x1118cadd8> SortTuple(atEnd=0, offset=6.0, ...) flatten
<music21.stream.Part 0x1118cadd8_flat> SortTuple(atEnd=0, offset=6.0, ...) flatten
<music21.stream.Part 0x1118cadd8_flat> SortTuple(atEnd=0, offset=6.0, ...) flatten
<music21.stream.Part 0x1118cadd8_flat_flat> SortTuple(atEnd=0, offset=6.0, ...) flatten
>>> c = b.getContextByClass('Note', getElementMethod='getElementAfter')
>>> c
<music21.note.Note C>
Expand All @@ -1355,7 +1370,7 @@ def getContextByClass(self,
>>> noteA.getContextByClass('TimeSignature')
<music21.meter.TimeSignature 4/4>
'''
def payloadExtractor(site, flatten, positionStart):
def payloadExtractor(checkSite, flatten, innerPositionStart):
'''
change the site (stream) to a Tree (using caches if possible),
then find the node before (or after) the positionStart and
Expand All @@ -1364,31 +1379,31 @@ def payloadExtractor(site, flatten, positionStart):
flatten can be True, 'semiFlat', or False.
'''
siteTree = site.asTree(flatten=flatten, classList=className)
siteTree = checkSite.asTree(flatten=flatten, classList=className)
if 'Offset' in getElementMethod:
# these methods match only by offset. Used in .getBeat among other places
if (('At' in getElementMethod and 'Before' in getElementMethod)
or ('At' not in getElementMethod and 'After' in getElementMethod)):
positionStart = ZeroSortTupleHigh.modify(offset=positionStart.offset)
innerPositionStart = ZeroSortTupleHigh.modify(offset=innerPositionStart.offset)
elif (('At' in getElementMethod and 'After' in getElementMethod)
or ('At' not in getElementMethod and 'Before' in getElementMethod)):
positionStart = ZeroSortTupleLow.modify(offset=positionStart.offset)
innerPositionStart = ZeroSortTupleLow.modify(offset=innerPositionStart.offset)
else:
raise Music21Exception(
'Incorrect getElementMethod: {}'.format(getElementMethod))

if 'Before' in getElementMethod:
contextNode = siteTree.getNodeBefore(positionStart)
contextNode = siteTree.getNodeBefore(innerPositionStart)
else:
contextNode = siteTree.getNodeAfter(positionStart)
contextNode = siteTree.getNodeAfter(innerPositionStart)

if contextNode is not None:
payload = contextNode.payload
return payload
else:
return None

def wellFormed(contextEl, site):
def wellFormed(checkContextEl, checkSite):
'''
Long explanation for a short method.
Expand Down Expand Up @@ -1468,19 +1483,22 @@ def wellFormed(contextEl, site):
this extacted section, one wants to see how that fits into a larger stream hierarchy.
'''
try:
selfSt = self.sortTuple(site, raiseExceptionOnMiss=True)
contextSt = contextEl.sortTuple(site, raiseExceptionOnMiss=True)
selfSortTuple = self.sortTuple(checkSite, raiseExceptionOnMiss=True)
contextSortTuple = checkContextEl.sortTuple(checkSite, raiseExceptionOnMiss=True)
except SitesException:
# might be raised by selfSt; should not be by contextSt. It just
# means that selfSt isn't in the same stream as contextSt, such as
# might be raised by selfSortTuple; should not be by contextSortTuple.
# It just means that selfSortTuple isn't in the same stream
# as contextSortTuple, such as
# when crossing measure borders. Thus it's well-formed.
return True

if 'Before' in getElementMethod and selfSt < contextSt:
# print(getElementMethod, selfSt.shortRepr(), contextSt.shortRepr(), self, contextEl)
if 'Before' in getElementMethod and selfSortTuple < contextSortTuple:
# print(getElementMethod, selfSortTuple.shortRepr(),
# contextSortTuple.shortRepr(), self, contextEl)
return False
elif 'After' in getElementMethod and selfSt > contextSt:
# print(getElementMethod, selfSt.shortRepr(), contextSt.shortRepr(), self, contextEl)
elif 'After' in getElementMethod and selfSortTuple > contextSortTuple:
# print(getElementMethod, selfSortTuple.shortRepr(),
# contextSortTuple.shortRepr(), self, contextEl)
return False
else:
return True
Expand All @@ -1498,7 +1516,9 @@ def wellFormed(contextEl, site):
followDerivation=followDerivation
):
if searchType in ('elementsOnly', 'elementsFirst'):
contextEl = payloadExtractor(site, flatten=False, positionStart=positionStart)
contextEl = payloadExtractor(site,
flatten=False,
innerPositionStart=positionStart)

if contextEl is not None and wellFormed(contextEl, site):
try:
Expand All @@ -1518,7 +1538,9 @@ def wellFormed(contextEl, site):
# containing site because that comes before.
return site # if the site itself is the context, return it...

contextEl = payloadExtractor(site, flatten='semiFlat', positionStart=positionStart)
contextEl = payloadExtractor(site,
flatten='semiFlat',
innerPositionStart=positionStart)
if contextEl is not None and wellFormed(contextEl, site):
try:
contextEl.activeSite = site
Expand Down

0 comments on commit c4b5b1c

Please sign in to comment.