2

I'm currently working on a step sequencer for Ableton live 11 in Python3. This is code I've converted manually from Python2 to 3. I'm defining a list that may or may not be empty, so I've inserted a check to see if the list is empty with an if statement but it isn't catching that the list is empty. Here is some code of my current issue:

      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

I get the following error:

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 in this instance is an empty list so the "do something else phrase" should be what's being called. Instead the if statement isn't catching that notes is empty. Any ideas on how to debug or fix this issue?

ducksinc
  • 41
  • 3

2 Answers2

0

I agree with you - that seems pretty odd to me. I would assume that even if there are no true notes inside of the notes variable, it is holding some type of object (without any notes in it) and therefore isn't defaulting to the else.

I'm sure you know this but when you write if variable_name: it will always trigger unless the value of variable_name = None

As a big Ableton guy I'm very curious to see what you're doing more, mind sharing a repo? :)

  • Here you go: https://github.com/CJmusic/APCJ40_MKII Although I've just started a code rewrite that's going much better. It's a control script for an APC40 Mk2, trying to revive some old step sequencer code. – ducksinc Nov 25 '21 at 04:23
0

The notes xml tree in als files can be empty and is empty if there are no notes.

so you could wrap in a tuple(list) to force an empty list.

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

or

def parseNotes(self, notes=()):
    if type(notes) in (list, tuple):
        doSomething
Ol Sen
  • 3,163
  • 2
  • 21
  • 30