Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert committed Aug 9, 2022
1 parent e73a964 commit 2c2b866
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
3 changes: 1 addition & 2 deletions music21/chord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4571,13 +4571,12 @@ def sortDiatonicAscending(self, *, inPlace=False):
Changed in v.6 -- if inPlace is True do not return anything.
'''
if inPlace:
# not until pitches inform notes which inform chords when they change.
if self._cache.get('isSortedAscendingDiatonic', False):
return None
returnObj = self
self.clearCache()
else:
# cache is not copied ever.
# cache is not copied to the new item.
returnObj = copy.deepcopy(self)
returnObj._notes.sort(key=lambda x: (x.pitch.diatonicNoteNum, x.pitch.ps))
returnObj._cache['isSortedAscendingDiatonic'] = True
Expand Down
33 changes: 15 additions & 18 deletions music21/stream/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,35 +1800,32 @@ def _deepcopySubclassable(self: StreamType,
removeFromIgnore=None
) -> StreamType:
# NOTE: this is a performance critical operation
defaultIgnoreSet = {'_offsetDict', 'streamStatus', '_elements', '_endElements', '_cache',
}
defaultIgnoreSet = {
'_offsetDict', 'streamStatus', '_elements', '_endElements', '_cache',
}
if ignoreAttributes is None:
ignoreAttributes = defaultIgnoreSet
else: # pragma: no cover
ignoreAttributes = ignoreAttributes | defaultIgnoreSet
new = super()._deepcopySubclassable(memo, ignoreAttributes, removeFromIgnore)

# PyCharm seems to think that this is a StreamCore
# noinspection PyTypeChecker
new: StreamType = super()._deepcopySubclassable(memo, ignoreAttributes, removeFromIgnore)

if removeFromIgnore is not None: # pragma: no cover
ignoreAttributes = ignoreAttributes - removeFromIgnore

if '_offsetDict' in ignoreAttributes:
newOffsetDict: t.Dict[int, t.Tuple[OffsetQLSpecial, base.Music21Object]] = {}
setattr(new, '_offsetDict', newOffsetDict)
# all subclasses of Music21Object that define their own
# __deepcopy__ methods must be sure to not try to copy activeSite
elif '_offsetDict' in self.__dict__:
newOffsetDict2: t.Dict[int, t.Tuple[OffsetQLSpecial, base.Music21Object]] = {}
setattr(new, '_offsetDict', newOffsetDict2)
# new._offsetDict will get filled when ._elements is copied.
newOffsetDict: t.Dict[int, t.Tuple[OffsetQLSpecial, base.Music21Object]] = {}
new._offsetDict = newOffsetDict

if 'streamStatus' in ignoreAttributes:
# update the client
if self.streamStatus is not None:
# storedClient = self.streamStatus.client # Should be self.
# self.streamStatus.client = None
newStreamStatus = copy.deepcopy(self.streamStatus)
newStreamStatus.client = new
setattr(new, 'streamStatus', newStreamStatus)
# self.streamStatus.client = storedClient
# self.streamStatus.client = None
newStreamStatus = copy.deepcopy(self.streamStatus)
newStreamStatus.client = new
setattr(new, 'streamStatus', newStreamStatus)
# self.streamStatus.client = storedClient
if '_elements' in ignoreAttributes:
# must manually add elements to new Stream
for e in self._elements:
Expand Down

0 comments on commit 2c2b866

Please sign in to comment.