Skip to content

Commit

Permalink
Fix linter test
Browse files Browse the repository at this point in the history
* and more type annotations
  • Loading branch information
nvuaille committed Mar 8, 2022
1 parent 53595e9 commit 291161e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
19 changes: 11 additions & 8 deletions music21/noteworthy/binaryTranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@

from music21.noteworthy import constants

from typing import List

environLocal = environment.Environment('noteworthy.translate')


Expand Down Expand Up @@ -273,7 +275,7 @@ def readToNUL(self, updateParsePosition=True):
self.parsePosition = nulPosition + 1
return ret

def isValidNWCFile(self, updateParsePosition=True):
def isValidNWCFile(self, updateParsePosition=True) -> bool:
storedPP = self.parsePosition
self.parsePosition = 0
header1 = self.readToNUL()
Expand Down Expand Up @@ -439,7 +441,7 @@ def parseHeader(self):
# print('StaffCount', self.numberOfStaves)
self.skipBytes(1)

def dumpToNWCText(self):
def dumpToNWCText(self) -> List[str]:
infos = '|SongInfo|Title:' + self.title.decode('latin_1')
infos += '|Author:' + self.author.decode('latin_1')
dumpObjects = [infos]
Expand Down Expand Up @@ -467,7 +469,7 @@ class NWCStaff:
def __init__(self, parent=None):
self.parent: NWCConverter = parent
self.lyrics = []
self.objects: ist[NWCObjects] = []
self.objects: list[NWCObject] = []
self.instrumentName = None
self.group = None
self.layerWithNextStaff = None
Expand All @@ -493,7 +495,7 @@ def parseStaff(self):
self.parseObjects()
# environLocal.warn([self.parent.parsePosition, self.objects])

def dump(self) -> list[str]:
def dump(self) -> List[str]:
dumpObjects = []

instruName = self.instrumentName if self.instrumentName else 'NoName'
Expand All @@ -504,7 +506,8 @@ def dump(self) -> list[str]:

staffInstruString = '|StaffInstrument|Name:' + instruName
if instruName in constants.MidiInstruments:
staffInstruString += '|Patch:' + str(constants.MidiInstruments.index(self.instrumentName))
staffInstruString += '|Patch:'
staffInstruString += str(constants.MidiInstruments.index(self.instrumentName))

staffInstruString += '|Trans:' + str(self.transposition)

Expand Down Expand Up @@ -650,9 +653,9 @@ class NWCObject:
Each parsing method should set up the 'dumpMethod' method that return the nwctxt version
of the object.
'''
def __init__(self, staffParent=None, parserParent=None):
self.staffParent = staffParent
self.parserParent = parserParent
def __init__(self, staffParent: NWCStaff = None, parserParent: NWCConverter = None):
self.staffParent: NWCStaff = staffParent
self.parserParent: NWCConverter = parserParent
self.type = None
self.placement = 0
self.pos = 0
Expand Down
28 changes: 12 additions & 16 deletions music21/noteworthy/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def parseList(self, dataList):
for attribute in sections[2:]:
try:
(name, value) = attribute.split(':', 1)
if command == 'Chord' and (name == 'Dur' or name == 'Pos'):
if command == 'Chord' and (name in ('Dur', 'Pos')):
if attributes.get(name) is None:
attributes[name] = [value]
else:
Expand Down Expand Up @@ -219,7 +219,7 @@ def parseList(self, dataList):
elif command == 'TimeSig':
self.createTimeSignature(attributes)
except Exception as e:
print('Cannot create object from "%s"' % (attribute))
print('Cannot create object from "%s"' % (attributes))
raise e

# Add the last Stuff
Expand Down Expand Up @@ -297,20 +297,20 @@ def setTieFromPitchInfo(self, noteOrChord, pitchInfo):
thisNoteIsTied = True
self.withinTie = True

chord = noteOrChord if 'Chord' in noteOrChord.classes else None
achord = noteOrChord if 'Chord' in noteOrChord.classes else None

# if Tied
if thisNoteBeginsATie:
if chord is not None:
for p in chord.pitches:
chord.setTie(tie.Tie('start'), p)
if achord is not None:
for p in achord.pitches:
achord.setTie(tie.Tie('start'), p)
else:
noteOrChord.tie = tie.Tie('start')

if self.withinTie is True and thisNoteIsTied is False:
if chord is not None:
for p in chord.pitches:
chord.setTie(tie.Tie('stop'), p)
if achord is not None:
for p in achord.pitches:
achord.setTie(tie.Tie('stop'), p)
else:
noteOrChord.tie = tie.Tie('stop')
self.withinTie = False
Expand Down Expand Up @@ -428,7 +428,7 @@ def getStepAndOctaveFromPosition(self, positionNote):
octave = 6
minPosition = 1
dictionary = 'dictionaryTreble'
elif currentClef == 'BASS' or currentClef == 'PERCUSSION':
elif currentClef in ('BASS', 'PERCUSSION'):
octave = 3
minPosition = -1
dictionary = 'dictionaryBass'
Expand Down Expand Up @@ -548,7 +548,7 @@ def translateChord(self, attributes):
isRestChord = 'Dur2' in attributes.keys()
i = 0

def getVoiceAtDuration(inner_self, voiceId, duration):
def getVoiceAtDuration(inner_self, voiceId, dur):
# first check if voice already exists in measure
voice = None
hasVoice = False
Expand Down Expand Up @@ -576,7 +576,7 @@ def getVoiceAtDuration(inner_self, voiceId, duration):
inner_self.currentMeasure.append(voice)


cd = duration.quarterLength
cd = dur.quarterLength
vd = voice._getDuration().quarterLength
# if current voice is late, add rest
if cd - vd > 0:
Expand Down Expand Up @@ -612,7 +612,6 @@ def getVoiceAtDuration(inner_self, voiceId, duration):

if isRestChord:
restDurInfo = attributes['Dur2']
restPitchInfo = attributes['Pos2']
r = note.Rest()
r.stepShift = 3
self.setDurationForObject(r, restDurInfo)
Expand Down Expand Up @@ -988,9 +987,6 @@ def createStaffInstrument(self, attributes):
try:
staffInstru = instrument.instrumentFromMidiProgram(patch)
readTranspo = int(attributes.get('Trans', 1))
initialTranspo = 0
if staffInstru.transposition is not None:
initialTranspo = staffInstru.transposition.semitones
self.currentTransposition = 12 * readTranspo // 12
staffInstru.transposition = interval.Interval(readTranspo)
self.currentPart.append(staffInstru)
Expand Down

0 comments on commit 291161e

Please sign in to comment.