【问题标题】:Ableton Live 11 Remote Scripts Python3, If statement not catching empty listAbleton Live 11 远程脚本 Python3,If 语句未捕获空列表
【发布时间】:2021-12-19 16:40:27
【问题描述】:

我目前正在为 Python3 中的 Ableton live 11 开发步进音序器。这是我手动从 Python2 转换为 3 的代码。我正在定义一个可能为空也可能不为空的列表,因此我插入了一个检查以使用 if 语句查看列表是否为空,但它不是发现列表为空。这是我当前问题的一些代码:

      notes = self._time_step(time).filter_notes(self._clip_notes)
      if notes:
          most_significant_velocity =  max(notes, key=lambda n: n[3])
          do something
      else:
          do something different

我收到以下错误:

2021-11-05T12:18:12.100094: info: RemoteScriptError: most_significant_velocity = max(notes, key=lambda n: n[3]) 
2021-11-05T12:18:12.100138: info: RemoteScriptError: 

2021-11-05T12:18:12.100184: info: RemoteScriptError: ValueError
2021-11-05T12:18:12.100227: info: RemoteScriptError: : 
2021-11-05T12:18:12.100271: info: RemoteScriptError: max() arg is an empty sequence

在这种情况下,Notes 是一个空列表,因此“做其他事情”应该是被调用的内容。相反,if 语句没有捕捉到注释是空的。有关如何调试或解决此问题的任何想法?

【问题讨论】:

  • 您是否尝试过打印notes 并查看其内容?

标签: python python-3.x midi ableton-live


【解决方案1】:

als文件中的notes xml树可以为空,如果没有notes则为空。

所以你可以包装一个元组(列表)来强制一个空列表。

note = (self._time_step(time).filter_notes(self._clip_notes),)
for n in notes:
    doSomething

def parseNotes(self, notes=()):
    if type(notes) in (list, tuple):
        doSomething

【讨论】:

    【解决方案2】:

    我同意你的看法——这对我来说似乎很奇怪。我会假设即使 notes 变量中没有真正的注释,它也持有某种类型的对象(其中没有任何注释),因此不会默认为 else。

    我确定你知道这一点,但是当你写 if variable_name: 时,它总是会触发,除非 variable_name = None 的值

    作为一个 Ableton 大佬,我很想知道你在做什么,介意分享一个 repo 吗? :)

    【讨论】:

    • 给你:github.com/CJmusic/APCJ40_MKII 虽然我刚刚开始重写代码,但情况会好很多。这是一个 APC40 Mk2 的控制脚本,试图恢复一些旧的步进音序器代码。
    猜你喜欢
    • 2021-01-06
    • 2014-05-14
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多