简体   繁体   English

如何将这些修饰的函数封装到一个类中?

[英]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.我试图将 Slack API 的 V2 包装到一个类中,以便我可以将有关我的机器人的信息封装起来。 Here is one of their example snippets:这是他们的示例片段之一:

import slack

slack_token = os.environ["SLACK_API_TOKEN"]
rtmclient = slack.RTMClient(token=slack_token)

@slack.RTMClient.run_on(event='message')
def say_hello(**payload):
    data = payload['data']
    if 'Hello' in data['text']:
        channel_id = data['channel']
        thread_ts = data['ts']
        user = data['user']

        webclient = payload['web_client']
        webclient.chat_postMessage(
            channel=channel_id,
            text="Hi <@{}>!".format(user),
            thread_ts=thread_ts
        )

rtmclient.start()

My understanding here is that this say_hello function is being passed into the slack object because of the decorator, so if I were to wrap this into a class, that function isn't really sitting inside my class.我的理解是,由于装饰器,这个say_hello函数被传递到 slack 对象中,所以如果我将它包装到一个类中,该函数并没有真正位于我的类中。 How do I wrap the say_hello function for it to be able to call methods and reference properties that belonged to an instance of my class?我如何包装say_hello函数以使其能够调用属于我的类的实例的方法和引用属性?

Have a look at how decorators work!看看装饰器是如何工作的!

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

@decorator_factory                                                                                                                                                                            
def inc(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:可能有更好的规范方法来实现您想要的,但这可以完成工作:

class Client():                                                                                                                                                                               

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

    def decorated(self, x, y, z):                                                                                                                                                                      
        pass            

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:在 Markus 的解决方案中,直接调用“run_on”函数,然后再调用“start”函数,类似如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM