Skip to content

Commit

Permalink
Add pitches property to NotRest as well (#807)
Browse files Browse the repository at this point in the history
allows for iterating over NotRest objects and collecting pitches.
  • Loading branch information
mscuthbert authored Feb 3, 2021
1 parent dffe89e commit ea893da
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions music21/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import copy
import unittest

from typing import Optional, List, Union
from typing import Optional, List, Union, Tuple, Iterable

from music21 import base
from music21 import beam
Expand Down Expand Up @@ -1119,6 +1119,19 @@ def hasVolumeInformation(self) -> bool:
else:
return True

@property
def pitches(self) -> Tuple[pitch.Pitch]:
'''
Returns an empty tuple. (Useful for iterating over NotRests since they
include Notes and Chords
'''
return ()

@pitches.setter
def pitches(self, _value: Iterable[pitch.Pitch]):
pass


def _getVolume(self, forceClient: Optional[base.Music21Object] = None) -> volume.Volume:
# DO NOT CHANGE TO @property because of optional attributes
# lazy volume creation
Expand Down Expand Up @@ -1425,18 +1438,9 @@ def _setOctave(self, value: int):
See :attr:`~music21.pitch.Pitch.octave`.
''')

def _getPitches(self):
return (self.pitch,)

def _setPitches(self, value):
if common.isListLike(value):
self.pitch = value[0]
else:
raise NoteException(f'cannot set pitches with provided object: {value}')

pitches = property(_getPitches,
_setPitches,
doc='''
@property
def pitches(self) -> Tuple[pitch.Pitch]:
'''
Return the :class:`~music21.pitch.Pitch` object in a tuple.
This property is designed to provide an interface analogous to
that found on :class:`~music21.chord.Chord` so that `[c.pitches for c in s.notes]`
Expand Down Expand Up @@ -1473,7 +1477,15 @@ def _setPitches(self, value):
>>> n.pitch.diatonicNoteNum
Traceback (most recent call last):
AttributeError: 'str' object has no attribute 'diatonicNoteNum'
''')
'''
return (self.pitch,)

@pitches.setter
def pitches(self, value: Iterable[pitch.Pitch]):
if common.isListLike(value) and value:
self.pitch = value[0]
else:
raise NoteException(f'cannot set pitches with provided object: {value}')

def transpose(self, value, *, inPlace=False):
'''
Expand Down

0 comments on commit ea893da

Please sign in to comment.