Skip to content

Commit

Permalink
Merge pull request #1666 from cuthbertLab/etree-deprecation
Browse files Browse the repository at this point in the history
Avoid testing truth value of ElementTree.Element
  • Loading branch information
mscuthbert authored Jan 2, 2024
2 parents ca10a4f + 27bf716 commit 432b116
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
12 changes: 6 additions & 6 deletions music21/musicxml/m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ def pageLayoutToXmlPrint(self, pageLayout, mxPrintIn=None):
setb(pageLayout, mxPrint, 'page-number')

mxPageLayout = self.pageLayoutToXmlPageLayout(pageLayout)
if mxPageLayout:
if len(mxPageLayout):
mxPrint.append(mxPageLayout)

if mxPrintIn is None:
Expand All @@ -1104,7 +1104,7 @@ def pageLayoutToXmlPageLayout(self, pageLayout, mxPageLayoutIn=None):
mxPageMargins = Element('page-margins')
for direction in ('left', 'right', 'top', 'bottom'):
seta(pageLayout, mxPageMargins, direction + '-margin')
if mxPageMargins:
if len(mxPageMargins):
mxPageLayout.append(mxPageMargins)

if mxPageLayoutIn is None:
Expand Down Expand Up @@ -1158,7 +1158,7 @@ def systemLayoutToXmlPrint(self, systemLayout, mxPrintIn=None):

mxSystemLayout = Element('system-layout')
self.systemLayoutToXmlSystemLayout(systemLayout, mxSystemLayout)
if mxSystemLayout:
if len(mxSystemLayout):
mxPrint.append(mxSystemLayout)

if mxPrintIn is None:
Expand Down Expand Up @@ -1204,7 +1204,7 @@ def systemLayoutToXmlSystemLayout(self, systemLayout, mxSystemLayoutIn=None):
for direction in ('top', 'bottom', 'left', 'right'):
seta(systemLayout, mxSystemMargins, direction + '-margin')

if mxSystemMargins:
if len(mxSystemMargins):
mxSystemLayout.append(mxSystemMargins)

seta(systemLayout, mxSystemLayout, 'system-distance', 'distance')
Expand Down Expand Up @@ -2505,7 +2505,7 @@ def setTitles(self) -> None:
firstTitleFound = titles[0]
mxWorkTitle = SubElement(mxWork, 'work-title')
mxWorkTitle.text = str(titles[0])
if mxWork:
if len(mxWork):
mxScoreHeader.append(mxWork)

movementNumbers: tuple[metadata.Text, ...] = mdObj['movementNumber']
Expand Down Expand Up @@ -4942,7 +4942,7 @@ def noteToNotations(
for x in (mxArticulations,
mxTechnicalMark,
mxOrnaments):
if x:
if x is not None:
notations.append(x)

# TODO: dynamics in notations
Expand Down
19 changes: 7 additions & 12 deletions music21/musicxml/partStaffExporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,9 @@ def isMultiAttribute(m21Class: type[M21ObjType],
# Initial PartStaff in group: find earliest mxAttributes, set clef #1 and <staves>
if initialPartStaffRoot is None:
initialPartStaffRoot = self.getRootForPartStaff(ps)
mxAttributes = initialPartStaffRoot.find('measure/attributes')
clef1: Element | None = mxAttributes.find('clef') if mxAttributes else None
if clef1 is not None:
clef1.set('number', '1')
if (mxAttributes := initialPartStaffRoot.find('measure/attributes')) is not None:
if (clef1 := mxAttributes.find('clef')) is not None:
clef1.set('number', '1')

mxStaves = Element('staves')
mxStaves.text = str(len(group))
Expand All @@ -591,11 +590,11 @@ def isMultiAttribute(m21Class: type[M21ObjType],

if multiKey and mxAttributes is not None:
key1 = mxAttributes.find('key')
if key1:
if key1 is not None:
key1.set('number', '1')
if multiMeter and mxAttributes is not None:
meter1 = mxAttributes.find('time')
if meter1:
if meter1 is not None:
meter1.set('number', '1')

# Subsequent PartStaffs in group: set additional clefs on mxAttributes
Expand Down Expand Up @@ -631,19 +630,15 @@ def isMultiAttribute(m21Class: type[M21ObjType],
)

if multiMeter:
oldMeter: Element | None = thisPartStaffRoot.find(
'measure/attributes/time'
)
if oldMeter:
if (oldMeter := thisPartStaffRoot.find('measure/attributes/time')) is not None:
oldMeter.set('number', str(staffNumber))
helpers.insertBeforeElements(
mxAttributes,
oldMeter,
tagList=['staves']
)
if multiKey:
oldKey: Element | None = thisPartStaffRoot.find('measure/attributes/key')
if oldKey:
if (oldKey := thisPartStaffRoot.find('measure/attributes/key')) is not None:
oldKey.set('number', str(staffNumber))
helpers.insertBeforeElements(
mxAttributes,
Expand Down
2 changes: 1 addition & 1 deletion music21/musicxml/xmlToM21.py
Original file line number Diff line number Diff line change
Expand Up @@ -2844,7 +2844,7 @@ def xmlToChord(self, mxNoteList: list[ET.Element]) -> chord.ChordBase:
notes.append(self.xmlToSimpleNote(mxNote, freeSpanners=False))

c: chord.ChordBase
if any(mxNote.find('unpitched') for mxNote in mxNoteList):
if any(mxNote.find('unpitched') is not None for mxNote in mxNoteList):
c = percussion.PercussionChord(notes)
else:
c = chord.Chord(notes) # type: ignore # they are all Notes.
Expand Down

0 comments on commit 432b116

Please sign in to comment.