push2-python icon indicating copy to clipboard operation
push2-python copied to clipboard

Receive decorator events inside of class

Open frederikbrammer opened this issue 2 years ago • 1 comments

Hey there,

I would like to organize everything related to the Ableton Push interaction inside of a class named PushService. However, when the decorated function is called, I need to have access to the object's properties. How can I access self property inside of that function?

Here's my class definition:

class PushService():
    def __init__(self, eventQueueService: EventQueueService):
        self.eventQueueService = eventQueueService

        print("Initializing push service...")
        self.push = push2_python.Push2(run_simulator=True)

        # Start by setting all pad colors to white
        self.push.pads.set_all_pads_to_color('white')

    @push2_python.on_pad_pressed()
    def on_pad_pressed(push, pad_n, pad_ij, velocity):
        if pad_ij == (7,7):
            self.eventQueueService.registerBeat()     # <= Here I need access to the self property to call the registerBeat function

        # Set pressed pad color to green
        print('Pad', pad_ij, 'pressed with velocity', velocity)
        push.pads.set_pad_color(pad_ij, 'green')

Thanks a lot in advance!

frederikbrammer avatar Apr 16 '23 20:04 frederikbrammer

Hi @frederikbrammer, thanks for your message.

The library is not designed to work in this way, although it would make total sense to do what you're proposing. A great solution would be to actually update the library to support this kind of behaviour. You'd probably have to inherit from a base class and then push actions would result in object method calls (and decorators would not be needed). Unfortunately I don't have time to spend on that so it will not be part of the library (although I'd accept contributions).

The way I've solved similar issues in the past is to have global functions decorated accordingly (just like in the code examples) and then have these global functions call methods from a singleton object instance of what would be your PushService class. It is a bit ugly but it works.

If you'd like to contribute a solution like this and make it generic enough to include it in the library I'd be happy to include it as well.

Thanks!

ffont avatar Apr 17 '23 09:04 ffont