Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify code and speed up for loops #1631

Merged
merged 6 commits into from
Jan 3, 2024

Conversation

TimFelixBeyer
Copy link
Contributor

@TimFelixBeyer TimFelixBeyer commented Jul 10, 2023

Opening this PR now so I don't forget - enjoy the sabbatical!

This PR is improves readability and speed throughout music21.
It looks fairly messy but all the changes are quite straightforward and do not affect any functionality.

Many changes are of this type:

for i in range(len(list)): 
    item = list[i]

to the more pythonic

for item in list:

In the process, a bunch of other small functions were simplified drastically as well.
Happy to break it up into smaller PR's if required as well.

Simplifies various code without changing functionality
@coveralls
Copy link

coveralls commented Jul 10, 2023

Coverage Status

coverage: 93.038% (-0.008%) from 93.046%
when pulling 022f013 on TimFelixBeyer:pythonic-for-loops
into 432b116 on cuthbertLab:master.

@TimFelixBeyer TimFelixBeyer changed the title Simplify and speed up for loops Simplify code and speed up for loops Jul 13, 2023
Copy link
Member

@mscuthbert mscuthbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had an hour on hold and decided I'd peek at what was done in music21 land. Intriguing and very welcome PR! A few changes requested but LOTS and LOTS of enthusiasm. Anything not commented on is approved (as are most of the comments)

music21/beam.py Outdated
Comment on lines 331 to 332
beamLast = None
for i in range(len(beamsList)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one I'd prefer to leave as is -- the descriptive labels for the names help me understand the logic and I don't feel that two-levels of nested ifs simplify the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you feel about:

for i in range(0, len(beamsList)):
    previousBeamIsNone = (i == 0 or beamsList[i - 1] is None)
    nextBeamIsNone = (i + 1 == len(beamsList) or beamsList[i + 1] is None)
    if previousBeamIsNone and nextBeamIsNone:
        beamsList[i] = None
return beamsList

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's still much slower for me to read and not faster for the computer.

music21/beam.py Outdated Show resolved Hide resolved
music21/beam.py Show resolved Hide resolved
music21/beam.py Show resolved Hide resolved
music21/analysis/patel.py Outdated Show resolved Hide resolved
music21/scale/intervalNetwork.py Show resolved Hide resolved
music21/scale/intervalNetwork.py Show resolved Hide resolved
music21/search/serial.py Show resolved Hide resolved
music21/stream/base.py Show resolved Hide resolved
music21/stream/base.py Show resolved Hide resolved
Copy link
Member

@mscuthbert mscuthbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi -- thanks for the changes. I'm going to make the changes back in the three places I still want them reverted and then merge -- I'm only back a few days a month and this PR is taking too much of my mind space to reread when I do. Feel free after I merge to keep arguing for why I'm doing something inefficient or old-way and then if you convince me, make a followup PR on them, but even approahing those lines with a fresh set of eyes months later I still couldn't quickly understand them until I read the code they were replacing.

music21/beam.py Outdated
Comment on lines 331 to 332
beamLast = None
for i in range(len(beamsList)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's still much slower for me to read and not faster for the computer.

if uniqueName in self.additionalRichMetadataAttributes:
return True
return False
return (super()._isStandardUniqueName(uniqueName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's much harder for me to keep track of why we're returning what we're returning and it's really not a speed improvement in the system. In the old system we test one thing and if it's good, we return True, and then we test another thing, and if it's good, we return True. Then if we get to the end, we return False. Very easy to look at the logic. In the new way, I have to look ahead and see "is there going to be an and clause? Are we going to add the values and do something with it? Are the parentheses there for a reason or only because the line is too long?"

It just makes me read code slower, and there's no discernible speedup.

for step, alter, accidental in zip(steps, alters, accidentals):
p = pitch.Pitch(step)
if accidental is not None:
accidentalName = self.mxAccidentalNameToM21.get(accidental, accidental)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem obvious to me that container.get(thing, thing) is a clear way of saying that if the thing doesn't have a translation in the container to leave it alone.

It's a code style in music21 and I'm the one who will be living with it for the next 20 years likely.

music21/pitch.py Show resolved Hide resolved
for i in range(len(mLists) - 1):
if len(mLists[i]) != len(mLists[i + 1]):
for mThis, mNext in zip(mLists, mLists[1:]):
if len(mThis) != len(mNext):
raise UnequalPartsLengthException(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just a ValueError

for i in range(len(mLists) - 1):
if len(mLists[i]) != len(mLists[i + 1]):
for mThis, mNext in zip(mLists, mLists[1:]):
if len(mThis) != len(mNext):
raise UnequalPartsLengthException(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or StreamException

@mscuthbert mscuthbert merged commit eab94f1 into cuthbertLab:master Jan 3, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants