Skip to content

Commit 0f96357

Browse files
author
Kevin J Walters
committed
Saving a bit of memory by punting frequently used ValueError("Out of range") to MIDIMessage. #3
1 parent 0fba8ff commit 0f96357

File tree

8 files changed

+10
-7
lines changed

8 files changed

+10
-7
lines changed

adafruit_midi/channel_pressure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class ChannelPressure(MIDIMessage):
6262
def __init__(self, pressure):
6363
self.pressure = pressure
6464
if not 0 <= self.pressure <= 127:
65-
raise ValueError("Out of range")
65+
raise self._EX_VALUEERROR_OOR
6666

6767
# channel value is mandatory
6868
def as_bytes(self, channel=None):

adafruit_midi/control_change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(self, control, value):
6565
self.control = control
6666
self.value = value
6767
if not 0 <= self.control <= 127 or not 0 <= self.value <= 127:
68-
raise ValueError("Out of range")
68+
raise self._EX_VALUEERROR_OOR
6969

7070
# channel value is mandatory
7171
def as_bytes(self, channel=None):

adafruit_midi/midi_message.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class MIDIMessage:
113113
CHANNELMASK = None
114114
ENDSTATUS = None
115115

116+
# Commonly used exceptions to save memory
117+
_EX_VALUEERROR_OOR = ValueError("Out of range")
118+
116119
# Each element is ((status, mask), class)
117120
# order is more specific masks first
118121
_statusandmask_to_class = []

adafruit_midi/note_off.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, note, velocity):
6666
self.note = note_parser(note)
6767
self.velocity = velocity
6868
if not 0 <= self.note <= 127 or not 0 <= self.velocity <= 127:
69-
raise ValueError("Out of range")
69+
raise self._EX_VALUEERROR_OOR
7070

7171
# channel value is mandatory
7272
def as_bytes(self, channel=None):

adafruit_midi/note_on.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, note, velocity):
6666
self.note = note_parser(note)
6767
self.velocity = velocity
6868
if not 0 <= self.note <= 127 or not 0 <= self.velocity <= 127:
69-
raise ValueError("Out of range")
69+
raise self._EX_VALUEERROR_OOR
7070

7171
# channel value is mandatory
7272
def as_bytes(self, channel=None):

adafruit_midi/pitch_bend_change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class PitchBendChange(MIDIMessage):
6363
def __init__(self, pitch_bend):
6464
self.pitch_bend = pitch_bend
6565
if not 0 <= self.pitch_bend <= 16383:
66-
raise ValueError("Out of range")
66+
raise self._EX_VALUEERROR_OOR
6767

6868
# channel value is mandatory
6969
def as_bytes(self, channel=None):

adafruit_midi/polyphonic_key_pressure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(self, note, pressure):
6565
self.note = note_parser(note)
6666
self.pressure = pressure
6767
if not 0 <= self.note <= 127 or not 0 <= self.pressure <= 127:
68-
raise ValueError("Out of range")
68+
raise self._EX_VALUEERROR_OOR
6969

7070
# channel value is mandatory
7171
def as_bytes(self, channel=None):

adafruit_midi/program_change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class ProgramChange(MIDIMessage):
6262
def __init__(self, patch):
6363
self.patch = patch
6464
if not 0 <= self.patch <= 127:
65-
raise ValueError("Out of range")
65+
raise self._EX_VALUEERROR_OOR
6666

6767
# channel value is mandatory
6868
def as_bytes(self, channel=None):

0 commit comments

Comments
 (0)