【发布时间】:2021-12-03 20:15:45
【问题描述】:
我正在用 rp2040、RTC 和 NeoPixel 条构建一个基本的日光闹钟。我正在为警报对象构建类,其中包含:触发时间(小时、分钟、秒)、触发函数和被触发函数的参数,这是相关的代码片段:
class Alarm(Clock):
def __init__(self, time, trigger, *args):
self.absolute_alarm = super().calculate_absolute_time(time)
self.trigger = trigger
self.args = args
def trigger_alarm(self):
self.trigger(*self.args)
在主 code.py 这是第 61 行抛出错误:
wake_up_alarm = Alarm((7,0,0), light_bar_soft_fade, 1, pixels_60)
绝对时间只是从午夜开始的秒数,这比试图在 12 小时时间中调整小时/分钟更容易。 Clock 是一个 NeoPixel 继承的类,它只包含一些发布功能,这一切都按预期工作。当我运行此代码时,我收到此错误:
code.py output:
Traceback (most recent call last):
File "code.py", line 61, in <module>
TypeError: can't convert tuple to int
我已将其范围缩小到抛出元组对象,但我看不出 Python 认为它需要在哪里成为 int?我该如何纠正这个问题?
完整的课程代码不会太长,以防万一:
import time
import neopixel
class Clock(neopixel.NeoPixel):
def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None, debugging=False):
super().__init__(
pin, n, bpp=bpp, brightness=brightness, auto_write=auto_write, pixel_order=pixel_order
)
if len(self.byteorder) == 3: self.fill((0,0,0))
if len(self.byteorder) == 4: self.fill((0,0,0,0))
self.last_minute = self.last_hour = self.last_second = 0
self.clock_face = self.alarms = []
self.show()
def post(self, t):
self.clock_face = []
for each in range(0, 12): self.clock_face.append([0, 0, 0, 0])
self.clock_face[t.tm_hour % 12][0] = 1
self.clock_face[int(t.tm_min/5)][2] = 1
for each in range(0, 12): self[each] = self.clock_face[each]
self.show()
for alarm in self.alarms:
if calculate_absolute_time(t) == alarm.absolute_alarm: alarm.trigger()
def calculate_absolute_time(self, time):
try:
absolute_time = (time.tm_hour * 60 + time.tm_min) * 60 + time.tm_sec
except AttributeError:
absolute_time = (time[0] * 60 + time[1]) * 60 + time[2]
return absolute_time
最少的可重现代码
import neopixel
import board
class Clock(neopixel.NeoPixel):
def __init__(
self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None, debugging=False,):
super().__init__(pin, n, bpp=bpp, brightness=brightness, auto_write=auto_write, pixel_order=pixel_order,)
def calculate_absolute_time(self, time):
return True
class Alarm(Clock):
def __init__(self, time, trigger, *args):
pass
def light_bar_soft_fade(*args):
pass
clock_pixels = Clock(board.D10, 12, brightness=1, auto_write=False,
pixel_order=(1, 0, 2, 3))
wake_up_alarm = Alarm((7,0,0), light_bar_soft_fade, 1, clock_pixels)
新的错误信息:
Traceback (most recent call last):
File "code.py", line 18, in <module>
TypeError: can't convert tuple to int
【问题讨论】:
-
元组没有名为
tm_hour等的属性。Clock.calculate_absolute_time似乎期望诸如命名元组之类的东西或(很可能)time.struct_time类型的值。 -
这就是为什么我包含了 except AttributeError: 这样我就可以传递 RTC 结构化时间或 [hr,min,sec] 列表。更不用说错误是从 code.py 在 init 上抛出的,而不是从 clock.py 的 calculate_absolute_time
-
如果您将实际的
struct_time值传递给calculate_absolute_time,会发生什么?我会使该功能不那么灵活,并需要调用者。请尝试生成minimal reproducible example;您的代码中没有(明显的)第 61 行,因此不清楚错误是否在您认为的位置。 -
这是第 61 行:
wake_up_alarm = Alarm((7,0,0), light_bar_soft_fade, 1, pixels_60) -
从
Clock.post调用了什么函数calculate_absolute_time?这不是Clock.calculate_absolute_time。
标签: python python-3.x class oop adafruit-circuitpython