【问题标题】:how do I pass variables as arguments to a method via click?如何通过单击将变量作为参数传递给方法?
【发布时间】:2021-04-18 17:41:41
【问题描述】:

您好,我对 click 和 python 比较陌生,我正在尝试通过 click 将变量传递给类中的方法。我不确定点击装饰器是否应该高于 init 或高于方法本身。

import click
import os
from datetime import datetime


class table_update:
    scriptdirectory = os.path.dirname(os.path.realpath(__file__))
    @click.command()
    @click.option('-tn','--tablename',prompt=True)
    def __init__(self,*args,**kwargs):
        self.tablename = self.tablename
       
    # @click.group()
    # @click.command()
    # @click.option('--tn','--tablename',prompt=True)
    def gettime(self,tablename):
        """
        Set up timer and time functions for table update
        """
        print('get_time function running')
        print(f"Class variable ScriptDirectoryname inherited:{self.scriptdirectory}")
        print(f"here's the table: {tablename}")
        global_start = datetime.now()
        # get project path name & set to current path
        changedir= os.chdir(self.scriptdirectory)
        print(changedir)

time = table_update().gettime()
time

【问题讨论】:

    标签: python-3.x class inheritance methods click


    【解决方案1】:

    让我们从这个开始......

    你需要写:

    def __init__(self, tablename):
        self.tablename = tablename
    

    将装饰器放在方法而不是构造器上......

    老实说,我很难理解这段代码的目的是什么?你想让它做什么? 如果您解释更多,我可以提供帮助:)

    【讨论】:

    • 嗨@TheOriginalOdis,我正在尝试做一个简单的工作流程,将表发送到数据库。每个步骤都是一个方法,其中一些方法需要通过单击传递的参数。 - 设置计时器 - 设置日志记录 - 设置并发送电子邮件警报 - 创建与 db 的连接 - 在 pc 上查找表的索引位置作为文件 - 截断表 - 将表发送到 db - 按顺序执行所有方法
    猜你喜欢
    • 1970-01-01
    • 2018-09-03
    • 2015-04-19
    • 2020-12-20
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多