Skip to content

Commit e532560

Browse files
authored
Merge pull request #1693 from TimFelixBeyer/patch-15
Small simplifications and speed-ups
2 parents f82a14e + f8f0978 commit e532560

File tree

10 files changed

+100
-112
lines changed

10 files changed

+100
-112
lines changed

music21/abcFormat/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ def getMetronomeMarkObject(self) -> tempo.MetronomeMark|None:
719719
# there may be more than one dur divided by a space
720720
referent = 0.0 # in quarter lengths
721721
for dur in durs.split(' '):
722-
if dur.count('/') > 0:
722+
if '/' in dur:
723723
n, d = dur.split('/')
724724
else: # this is an error case
725725
environLocal.printDebug(['incorrectly encoded / unparsable duration:', dur])

music21/braille/segment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ def extractBrailleElements(
20602060
except BrailleSegmentException as notSupportedException: # pragma: no cover
20612061
isExempt = [isinstance(music21Object, music21Class)
20622062
for music21Class in excludeFromBrailleElements]
2063-
if isExempt.count(True) == 0:
2063+
if not any(isExempt):
20642064
environRules.warn(f'{notSupportedException}')
20652065

20662066
allElements.sort(key=lambda x: (x.offset, x.classSortOrder))

music21/chord/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -826,12 +826,12 @@ def __getitem__(self, key: int|str|note.Note|pitch.Pitch):
826826

827827
keyErrorStr = f'Cannot access component with: {key!r}'
828828
if isinstance(key, str):
829-
if key.count('.'):
829+
if '.' in key:
830830
key, attrStr = key.split('.', 1)
831-
if not attrStr.count('.'):
832-
attributes = (attrStr,)
833-
else:
831+
if '.' in attrStr:
834832
attributes = tuple(attrStr.split('.'))
833+
else:
834+
attributes = (attrStr,)
835835
else:
836836
attributes = ()
837837

@@ -923,7 +923,7 @@ def __setitem__(self, key, value):
923923
Traceback (most recent call last):
924924
ValueError: Chord index must be set to a valid note object
925925
'''
926-
if isinstance(key, str) and key.count('.'):
926+
if isinstance(key, str) and '.' in key:
927927
keySplit = key.split('.')
928928
keyFind = '.'.join(keySplit[0:-1])
929929
attr = keySplit[-1]

music21/duration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def dottedMatch(qLen: OffsetQLIn,
385385
durType, match = quarterLengthToClosestType(preDottedLength)
386386
except DurationException:
387387
continue
388-
if match is True:
388+
if match:
389389
return (dots, durType)
390390
return (False, False)
391391

@@ -441,7 +441,7 @@ def quarterLengthToNonPowerOf2Tuplet(
441441

442442
def quarterLengthToTuplet(
443443
qLen: OffsetQLIn,
444-
maxToReturn=4,
444+
maxToReturn: int = 4,
445445
tupletNumerators=defaultTupletNumerators
446446
) -> list[Tuplet]:
447447
'''
@@ -738,7 +738,7 @@ def quarterConversion(qLen: OffsetQLIn) -> QuarterLengthConversion:
738738

739739
def convertTypeToQuarterLength(
740740
dType: str,
741-
dots=0,
741+
dots: int = 0,
742742
tuplets: list[Tuplet]|None = None,
743743
dotGroups=None
744744
) -> OffsetQL:

music21/humdrum/spineParser.py

Lines changed: 66 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ def parse(self):
13271327
thisObject = SpineComment(eventC)
13281328
if thisObject.comment == '':
13291329
thisObject = None
1330-
elif eventC.count(' '):
1330+
elif ' ' in eventC:
13311331
thisObject = self.processChordEvent(eventC)
13321332
else: # Note or Rest
13331333
thisObject = self.processNoteEvent(eventC)
@@ -2208,7 +2208,7 @@ def hdStringToNote(contents):
22082208

22092209
# Detect rests first, because rests can contain manual positioning information,
22102210
# which is also detected by the `matchedNote` variable above.
2211-
if contents.count('r'):
2211+
if 'r' in contents:
22122212
thisObject = note.Rest()
22132213

22142214
elif matchedNote:
@@ -2231,91 +2231,88 @@ def hdStringToNote(contents):
22312231
thisObject.pitch.accidental = matchedSharp.group(0)
22322232
elif matchedFlat:
22332233
thisObject.pitch.accidental = matchedFlat.group(0)
2234-
elif contents.count('n'):
2234+
elif 'n' in contents:
22352235
thisObject.pitch.accidental = 'n'
22362236

22372237
# 3.2.2 -- Slurs, Ties, Phrases
22382238
# TODO: add music21 phrase information
2239-
if contents.count('{'):
2240-
for i in range(contents.count('{')):
2241-
pass # phraseMark start
2242-
if contents.count('}'):
2243-
for i in range(contents.count('}')):
2244-
pass # phraseMark end
2245-
if contents.count('('):
2246-
for i in range(contents.count('(')):
2247-
pass # slur start
2248-
if contents.count(')'):
2249-
for i in range(contents.count(')')):
2250-
pass # slur end
2251-
if contents.count('['):
2239+
for i in range(contents.count('{')):
2240+
pass # phraseMark start
2241+
for i in range(contents.count('}')):
2242+
pass # phraseMark end
2243+
for i in range(contents.count('(')):
2244+
pass # slur start
2245+
for i in range(contents.count(')')):
2246+
pass # slur end
2247+
if '[' in contents:
22522248
thisObject.tie = tie.Tie('start')
2253-
elif contents.count(']'):
2249+
elif ']' in contents:
22542250
thisObject.tie = tie.Tie('stop')
2255-
elif contents.count('_'):
2251+
elif '_' in contents:
22562252
thisObject.tie = tie.Tie('continue')
22572253

22582254
# 3.2.3 Ornaments
2259-
if contents.count('t'):
2255+
if 't' in contents:
22602256
thisObject.expressions.append(expressions.HalfStepTrill())
2261-
elif contents.count('T'):
2257+
elif 'T' in contents:
22622258
thisObject.expressions.append(expressions.WholeStepTrill())
22632259

2264-
if contents.count('w'):
2260+
if 'w' in contents:
22652261
thisObject.expressions.append(expressions.HalfStepInvertedMordent())
2266-
elif contents.count('W'):
2262+
elif 'W' in contents:
22672263
thisObject.expressions.append(expressions.WholeStepInvertedMordent())
2268-
elif contents.count('m'):
2264+
elif 'm' in contents:
22692265
thisObject.expressions.append(expressions.HalfStepMordent())
2270-
elif contents.count('M'):
2266+
elif 'M' in contents:
22712267
thisObject.expressions.append(expressions.WholeStepMordent())
22722268

2273-
if contents.count('S'):
2269+
if 'S' in contents:
22742270
thisObject.expressions.append(expressions.Turn())
2275-
elif contents.count('$'):
2271+
elif '$' in contents:
22762272
thisObject.expressions.append(expressions.InvertedTurn())
2277-
elif contents.count('R'):
2273+
elif 'R' in contents:
22782274
t1 = expressions.Turn()
22792275
t1.connectedToPrevious = True # true by default, but explicitly
22802276
thisObject.expressions.append(t1)
22812277

2282-
if contents.count(':'):
2278+
if ':' in contents:
22832279
# TODO: deal with arpeggiation -- should have been in a
22842280
# chord structure
22852281
pass
22862282

2287-
if contents.count('O'):
2283+
if 'O' in contents:
22882284
thisObject.expressions.append(expressions.Ornament())
22892285
# generic ornament
22902286

22912287
# 3.2.4 Articulation Marks
2292-
if contents.count("'"):
2288+
if "'" in contents:
22932289
thisObject.articulations.append(articulations.Staccato())
2294-
if contents.count('"'):
2290+
if '"' in contents:
22952291
thisObject.articulations.append(articulations.Pizzicato())
2296-
if contents.count('`'):
2292+
if '`' in contents:
22972293
# called 'attacca' mark but means staccatissimo:
22982294
# http://www.music-cog.ohio-state.edu/Humdrum/representations/kern.rep.html
22992295
thisObject.articulations.append(articulations.Staccatissimo())
2300-
if contents.count('~'):
2296+
if '~' in contents:
23012297
thisObject.articulations.append(articulations.Tenuto())
2302-
if contents.count('^'):
2298+
if '^' in contents:
23032299
thisObject.articulations.append(articulations.Accent())
2304-
if contents.count(';'):
2300+
if ';' in contents:
23052301
thisObject.expressions.append(expressions.Fermata())
23062302

23072303
# 3.2.5 Up & Down Bows
2308-
if contents.count('v'):
2304+
if 'v' in contents:
23092305
thisObject.articulations.append(articulations.UpBow())
2310-
elif contents.count('u'):
2306+
elif 'u' in contents:
23112307
thisObject.articulations.append(articulations.DownBow())
23122308

23132309
# 3.2.6 Stem Directions
2314-
if contents.count('/'):
2310+
if '/' in contents:
23152311
thisObject.stemDirection = 'up'
2316-
elif contents.count('\\'):
2312+
elif '\\' in contents:
23172313
thisObject.stemDirection = 'down'
23182314

2315+
23192316
# 3.2.7 Duration +
23202317
# 3.2.8 N-Tuplets
23212318

@@ -2326,7 +2323,7 @@ def hdStringToNote(contents):
23262323
durationFirst = int(foundRational.group(1))
23272324
durationSecond = float(foundRational.group(2))
23282325
thisObject.duration.quarterLength = 4 * durationSecond / durationFirst
2329-
if contents.count('.'):
2326+
if '.' in contents:
23302327
thisObject.duration.dots = contents.count('.')
23312328

23322329
elif foundNumber:
@@ -2336,20 +2333,16 @@ def hdStringToNote(contents):
23362333
if durationString == '000':
23372334
# for larger values, see https://extras.humdrum.org/man/rscale/
23382335
thisObject.duration.type = 'maxima'
2339-
if contents.count('.'):
2340-
thisObject.duration.dots = contents.count('.')
23412336
elif durationString == '00':
23422337
# for larger values, see https://extras.humdrum.org/man/rscale/
23432338
thisObject.duration.type = 'longa'
2344-
if contents.count('.'):
2345-
thisObject.duration.dots = contents.count('.')
23462339
else:
23472340
thisObject.duration.type = 'breve'
2348-
if contents.count('.'):
2349-
thisObject.duration.dots = contents.count('.')
2341+
if '.' in contents:
2342+
thisObject.duration.dots = contents.count('.')
23502343
elif durationType in duration.typeFromNumDict:
23512344
thisObject.duration.type = duration.typeFromNumDict[durationType]
2352-
if contents.count('.'):
2345+
if '.' in contents:
23532346
thisObject.duration.dots = contents.count('.')
23542347
else:
23552348
dT = int(durationType) + 0.0
@@ -2368,26 +2361,26 @@ def hdStringToNote(contents):
23682361
# humdrum tuplets that breaks normal usage. TODO: Refactor adding a Flavor = 'JRP'
23692362
# code that uses this other method...
23702363
JRP = flavors['JRP']
2371-
if JRP is False and contents.count('.'):
2364+
if JRP is False and '.' in contents:
23722365
newTup.durationNormal = duration.durationTupleFromTypeDots(
23732366
newTup.durationNormal.type, contents.count('.'))
23742367

23752368
thisObject.duration.appendTuplet(newTup)
2376-
if JRP is True and contents.count('.'):
2369+
if JRP is True and '.' in contents:
23772370
thisObject.duration.dots = contents.count('.')
23782371
# call Duration.TupletFixer after to correct this.
23792372

23802373
# 3.2.9 Grace Notes and Groupettos
2381-
if contents.count('q'):
2374+
if 'q' in contents:
23822375
thisObject = thisObject.getGrace()
23832376
thisObject.duration.type = 'eighth'
2384-
elif contents.count('Q'):
2377+
elif 'Q' in contents:
23852378
thisObject = thisObject.getGrace()
23862379
thisObject.duration.slash = False
23872380
thisObject.duration.type = 'eighth'
2388-
elif contents.count('P'):
2381+
elif 'P' in contents:
23892382
thisObject = thisObject.getGrace(appoggiatura=True)
2390-
elif contents.count('p'):
2383+
elif 'p' in contents:
23912384
pass # end appoggiatura duration -- not needed in music21...
23922385

23932386
# 3.2.10 Beaming
@@ -2427,53 +2420,53 @@ def hdStringToMeasure(contents, previousMeasure=None):
24272420

24282421
barline = bar.Barline()
24292422

2430-
if contents.count('-'):
2423+
if '-' in contents:
24312424
barline.type = 'none'
2432-
elif contents.count("'"):
2425+
elif "'" in contents:
24332426
barline.type = 'short'
2434-
elif contents.count('`'):
2427+
elif '`' in contents:
24352428
barline.type = 'tick'
2436-
elif contents.count('||'):
2429+
elif '||' in contents:
24372430
barline.type = 'double'
24382431
if contents.count(':') > 1:
24392432
barline.repeatDots = 'both'
2440-
elif contents.count(':|'):
2433+
elif ':|' in contents:
24412434
barline.repeatDots = 'left'
2442-
elif contents.count('|:'):
2435+
elif '|:' in contents:
24432436
barline.repeatDots = 'right'
2444-
elif contents.count('!!'):
2437+
elif '!!' in contents:
24452438
barline.type = 'heavy-heavy'
24462439
if contents.count(':') > 1:
24472440
barline.repeatDots = 'both'
2448-
elif contents.count(':!'):
2441+
elif ':!' in contents:
24492442
barline.repeatDots = 'left'
2450-
elif contents.count('!:'):
2443+
elif '!:' in contents:
24512444
barline.repeatDots = 'right'
2452-
elif contents.count('|!'):
2445+
elif '|!' in contents:
24532446
barline.type = 'final'
24542447
if contents.count(':') > 1:
24552448
barline.repeatDots = 'both'
2456-
elif contents.count(':|'):
2449+
elif ':|' in contents:
24572450
barline.repeatDots = 'left'
2458-
elif contents.count('!:'):
2451+
elif '!:' in contents:
24592452
barline.repeatDots = 'right'
2460-
elif contents.count('!|'):
2453+
elif '!|' in contents:
24612454
barline.type = 'heavy-light'
24622455
if contents.count(':') > 1:
24632456
barline.repeatDots = 'both'
2464-
elif contents.count(':!'):
2457+
elif ':!' in contents:
24652458
barline.repeatDots = 'left'
2466-
elif contents.count('|:'):
2459+
elif '|:' in contents:
24672460
barline.repeatDots = 'right'
2468-
elif contents.count('|'):
2461+
elif '|' in contents:
24692462
barline.type = 'regular'
24702463
if contents.count(':') > 1:
24712464
barline.repeatDots = 'both'
2472-
elif contents.count(':|'):
2465+
elif ':|' in contents:
24732466
barline.repeatDots = 'left'
2474-
elif contents.count('|:'):
2467+
elif '|:' in contents:
24752468
barline.repeatDots = 'right'
2476-
elif contents.count('=='):
2469+
elif '==' in contents:
24772470
barline.type = 'double'
24782471
if contents.count(':') > 1:
24792472
barline.repeatDots = 'both'
@@ -2483,7 +2476,7 @@ def hdStringToMeasure(contents, previousMeasure=None):
24832476
'Cannot import a double bar visually rendered as a single bar -- '
24842477
+ 'not sure exactly what that would mean anyhow.')
24852478

2486-
if contents.count(';'):
2479+
if ';' in contents:
24872480
barline.pause = expressions.Fermata()
24882481

24892482
if previousMeasure is not None:

0 commit comments

Comments
 (0)