上载后使用Python装饰器删除文件

上载后使用Python装饰器删除文件,python,pandas,slack,Python,Pandas,Slack,试图用Python来理解装饰器 我正在尝试编写一个包含2个函数的类: 一种函数,它将数据帧作为参数,并使用tablate将其写入文本文件 将文件上载到Slack并在5秒后将其删除的功能 我在理解如何使用装饰器以及如何使用args和kwargs时被绊倒了 我的结构的最终结果是: from slacker import Slacker from tabulate import tabulate import functools import time, os class Slack: de

试图用Python来理解装饰器

我正在尝试编写一个包含2个函数的类:

  • 一种函数,它将数据帧作为参数,并使用tablate将其写入文本文件
  • 将文件上载到Slack并在5秒后将其删除的功能
  • 我在理解如何使用装饰器以及如何使用args和kwargs时被绊倒了

    我的结构的最终结果是:

    from slacker import Slacker
    from tabulate import tabulate
    import functools
    import time, os
    
    class Slack:
        def __init__(self, api_token, channel):
            self.Slacker = Slacker(api_token)
            self.channel = channel
    
        @functools.wraps(func)
        def upload(func):
            def upload_wrapper(self, *args, **kwargs):
                self.slack.files.upload(file, 
                                        channels=self.channel,
                                        title=head,
                                        initial_comment=comment)
                time.sleep(5)
                os.remove(file)
            return upload_wrapper(self, *args, **kwargs)
    
        def generate_text_file(self, filename, df):
            table = tabulate(filename, tablefmt="grid", headers=df.columns)
            with open(filename, 'w') as f:
                f.write(table)
    
    我期望的结果是调用一个函数,它为我准备一个文件,然后将它上传到Slack并删除它,这样系统上就不会保留任何文件(我会通过cronjob来运行)


    任何帮助都将不胜感激……

    我从未见过在python中对表达式应用装饰程序。你确定这是合法的开始吗?看起来你应该用表达式作为参数调用函数为什么不合法??
    slack = Slacker(api_token)
    
    def upload(func):
        @functools.wraps(func)
        def upload_wrapper(*args, **kwargs):
            slack.files.upload(file,
                              channels=channel, 
                              title=head,
                              initial_comment=comment)
    
            time.sleep(5)
            os.remove(file)
        return upload_wrapper(*args, **kwargs)
    
    from slacker import Slacker
    from tabulate import tabulate
    import functools
    import time, os
    
    class Slack:
        def __init__(self, api_token, channel):
            self.Slacker = Slacker(api_token)
            self.channel = channel
    
        @functools.wraps(func)
        def upload(func):
            def upload_wrapper(self, *args, **kwargs):
                self.slack.files.upload(file, 
                                        channels=self.channel,
                                        title=head,
                                        initial_comment=comment)
                time.sleep(5)
                os.remove(file)
            return upload_wrapper(self, *args, **kwargs)
    
        def generate_text_file(self, filename, df):
            table = tabulate(filename, tablefmt="grid", headers=df.columns)
            with open(filename, 'w') as f:
                f.write(table)
    
    workspace_name = Slack(api_token, channel=channel_name)
    
    df = some_pandas_function()
    
    @upload
    workspace_name.generate_text_file("filename.txt", df)