Skip to content Skip to sidebar Skip to footer

How To Wrap These Decorated Functions Into A Class?

I am attempting to wrap V2 of the Slack API into a class so that I can keep information about my bot encapsulated. Here is one of their example snippets: import slack slack_token

Solution 1:

Have a look at how decorators work!

defdecorator_factory(f):                                                                                                                                                                     
    defdecoration(*args, **kwargs):                                                                                                                                                          
        print('before')                                                                                                                                                                       
        r = f(*args, **kwargs)                                                                                                                                                                
        print('after')                                                                                                                                                                        
        return r                                                                                                                                                                              
    return decoration                                                                                                                                                                         

@decorator_factory                                                                                                                                                                            definc(i):                                                                                                                                                                                   
    '''                                                                                                                                                                                       
    >>> inc(23)                                                                                                                                                                               
    before                                                                                                                                                                                    
    after                                                                                                                                                                                     
    42                                                                                                                                                                                        
    '''return i + 1

There may be a better, canonical way to achieve what you want, but this would do the job:

classClient():                                                                                                                                                                               

    def__init__(self):                                                                                                                                                                       
        slack.RTMClient.run_on(event='message')(self.decorated)                                                                                                                               

    defdecorated(self, x, y, z):                                                                                                                                                                      
        pass

Solution 2:

They key is not to use the decorator at all.

From Markus' solution, just call the "run_on" function directly, before you call "start" function, similar to the below:

rtmclient = slack.RTMClient(token=self.Token)
rtmclient.run_on(event='message')(self.handle_command)
rtmclient.start()

Post a Comment for "How To Wrap These Decorated Functions Into A Class?"