Skip to content

Commit

Permalink
some potential speedups
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert committed Oct 14, 2022
1 parent 57e374a commit 0da76b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
10 changes: 3 additions & 7 deletions music21/analysis/transposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
environLocal = environment.Environment('analysis.transposition')


class TranspositionException(exceptions21.Music21Exception):
pass


class TranspositionChecker:
'''
Given a list of pitches, checks for the number of distinct transpositions.
Expand Down Expand Up @@ -55,14 +51,14 @@ class TranspositionChecker:
'''
def __init__(self, pitches: Iterable[pitch.Pitch] = ()):
if not pitches:
raise TranspositionException(
raise TypeError(
'Must have at least one element in list'
)
if not common.isIterable(pitches):
raise TranspositionException('Must be a list or tuple')
raise TypeError('Must be a list or tuple')
# p0 = pitches[0]
# if not isinstance(p0, pitch.Pitch):
# raise TranspositionException('List must have pitch objects')
# raise TypeError('List must have pitch objects')
self.pitches: Iterable[pitch.Pitch] = pitches
self.allTranspositions: list = []
self.allNormalOrders: list = []
Expand Down
29 changes: 24 additions & 5 deletions music21/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@ def __init__(self,
)

if durationTuple is not None:
self.addDurationTuple(durationTuple)
self.addDurationTuple(durationTuple, _skipInform=True)

if dots is not None:
storeDots = dots
Expand All @@ -1727,7 +1727,7 @@ def __init__(self,

if type is not None:
nt = durationTupleFromTypeDots(type, storeDots)
self.addDurationTuple(nt)
self.addDurationTuple(nt, _skipInform=True)
# permit as keyword so can be passed from notes
elif quarterLength is not None:
self.quarterLength = quarterLength
Expand Down Expand Up @@ -1812,7 +1812,22 @@ def __deepcopy__(self, memo):
'''
Don't copy client when creating
'''
return common.defaultDeepcopy(self, memo, ignoreAttributes={'client'})
if (self._componentsNeedUpdating is False
and len(self._components) == 1
and self._dotGroups == (0,)
and self._linked is True
and not self._tuplets): # 99% of notes...
# ignore all but components
return self.__class__(durationTuple=self._components[0])
elif (self._componentsNeedUpdating is False
and not self._components
and self._dotGroups == (0,)
and not self._tuplets
and self._linked is True):
# ignore all
return self.__class__()
else:
return common.defaultDeepcopy(self, memo, ignoreAttributes={'client'})

# PRIVATE METHODS #

Expand Down Expand Up @@ -1870,7 +1885,10 @@ def _setLinked(self, value: bool):

linked = property(_getLinked, _setLinked)

def addDurationTuple(self, dur: DurationTuple | Duration | str | OffsetQLIn):
def addDurationTuple(self,
dur: DurationTuple | Duration | str | OffsetQLIn,
*,
_skipInform=False):
'''
Add a DurationTuple or a Duration's components to this Duration.
Does not simplify the Duration. For instance, adding two
Expand Down Expand Up @@ -1902,7 +1920,8 @@ def addDurationTuple(self, dur: DurationTuple | Duration | str | OffsetQLIn):
if self.linked:
self._quarterLengthNeedsUpdating = True

self.informClient()
if not _skipInform:
self.informClient()

def appendTuplet(self, newTuplet: Tuplet) -> None:
'''
Expand Down

0 comments on commit 0da76b7

Please sign in to comment.