python-3.x - 如何通过单击将变量作为参数传递给方法?
问题描述
您好,我对 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
解决方案
让我们从这个开始......
你需要写:
def __init__(self, tablename):
self.tablename = tablename
将装饰器放在方法而不是构造器上......
老实说,我很难理解这段代码的目的是什么?你想让它做什么?如果您解释更多,我可以提供帮助:)
推荐阅读
- java - 将 Datagram Packet 数据放入 BufferedReader 和 PrintWriter
- java - Jackson ObjectMapper.findAndRegisterModules() 无法序列化 LocalDateTime
- azure-devops - 记录您的工作 - Jira vs' Azure DevOps Board
- r - 如何使用按钮更改 R 闪亮应用程序中的页面
- python - 如何为多标签分类创建类权重?
- sql - ORA-40593: 在执行 DBMS_JSON.CREATE_VIEW 期间发现名称冲突:名称
- node.js - 错误:ORA-01008:并非所有变量都绑定
- python - 遍历数据框以根据这些值提取最小值和最大值以及其他列
- javascript - 当请求的源上存在标头时,请求的资源上不存在“Access-Control-Allow-Origin”标头
- python-3.x - 如何在 python 中发送 PURGE 方法并检查响应