Skip to content

Commit 0f597ca

Browse files
committed
Refactor to use namedtuple instead of custom classes.
1 parent 10f17e1 commit 0f597ca

File tree

1 file changed

+9
-44
lines changed

1 file changed

+9
-44
lines changed

adafruit_irremote.py

+9-44
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
https://github.com/adafruit/circuitpython/releases
5151
5252
"""
53-
5453
import array
54+
from collections import namedtuple
5555
import time
5656

5757
__version__ = "0.0.0-auto.0"
@@ -96,7 +96,7 @@ def decode_bits(pulses):
9696
# TODO The name pulses is redefined several times below, so we'll stash the
9797
# original in a separate variable for now. It might be worth refactoring to
9898
# avoid redefining pulses, for the sake of readability.
99-
input_pulses = pulses
99+
input_pulses = tuple(pulses)
100100
pulses = list(pulses) # Copy to avoid mutating input.
101101

102102
# special exception for NEC repeat code!
@@ -170,52 +170,17 @@ def decode_bits(pulses):
170170
output[i // 8] = output[i // 8] << 1
171171
if pulse_length:
172172
output[i // 8] |= 1
173-
return IRMessage(input_pulses, code=output)
174-
175-
176-
class BaseIRMessage:
177-
"Contains the pulses that were parsed as one message."
178-
179-
def __init__(self, pulses):
180-
# Stash an immutable copy of pulses.
181-
self.pulses = tuple(pulses)
173+
return IRMessage(tuple(input_pulses), code=tuple(output))
182174

183-
def __repr__(self):
184-
return f"{self.__class__.__name__}({self.pulses})"
185175

176+
IRMessage = namedtuple("IRMessage", ("pulses", "code"))
177+
"Pulses and the code they were parsed into"
186178

187-
class IRMessage(BaseIRMessage):
188-
"""
189-
Message interpreted as bytes.
190-
191-
>>> m.code # the output of interest (the parsed bytes)
192-
>>> m.pulses # the original pulses
193-
"""
194-
195-
def __init__(self, pulses, *, code):
196-
super().__init__(pulses)
197-
self.code = code
198-
199-
def __repr__(self):
200-
return f"{self.__class__.__name__}" f"(pulses={self.pulses}, code={self.code})"
201-
202-
203-
class UnparseableIRMessage(BaseIRMessage):
204-
"Message that could not be interpreted."
205-
206-
def __init__(self, pulses, *, reason):
207-
super().__init__(pulses)
208-
self.reason = reason
209-
210-
def __repr__(self):
211-
return (
212-
f"{self.__class__.__name__}" f"(pulses={self.pulses}, reason={self.reason})"
213-
)
214-
179+
UnparseableIRMessage = namedtuple("IRMessage", ("pulses", "reason"))
180+
"Pulses and the reason that they could not be parsed into a code"
215181

216-
class NECRepeatIRMessage(BaseIRMessage):
217-
"Message interpreted as an NEC repeat code."
218-
pass
182+
NECRepeatIRMessage = namedtuple("NECRepeatIRMessage", ("pulses",))
183+
"Pulses interpreted as an NEC repeat code"
219184

220185

221186
class NonblockingGenericDecode:

0 commit comments

Comments
 (0)