点击类型转换
Click type conversion
我正在使用 click
定义一个 CLI,它采用 datetime
s 和逗号分隔的参数列表。
import click
def valid_date(s):
try:
return dt.strptime(s, "%Y-%m-%d")
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise Exception(msg)
except TypeError:
return None
split_parameter = lambda _, __, s: s.split(",")
check_date = lambda _, __, s: valid_date(s)
@click.command()
@click.argument('symbols', callback=split_parameter)
@click.option('--start_date', callback=check_date)
@click.option('--end_date', callback=check_date)
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
return None
它有效,但围绕 callback
的逻辑有点尴尬。拆分列表和转换为 datetime
都会中断点击的简单结构。有没有一种 pythonic 的方式来做到这一点?
您可以将丑陋的 lambda _, __, x
部分封装在装饰器中:
import click
from datetime import datetime as dt
def click_callback(f):
return lambda _, __, x: f(x)
def check_date(s):
try:
return dt.strptime(s, "%Y-%m-%d")
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise Exception(msg)
except TypeError:
return None
@click.command()
@click.argument('symbols', callback=click_callback(lambda s: s.split(',')))
@click.option('--start_date', callback=click_callback(check_date))
@click.option('--end_date', callback=click_callback(check_date))
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
print(symbols, start_date, end_date, file_name)
if __name__ == '__main__':
f()
或者像这样(只显示相关部分):
def click_callback(f):
return {'callback': lambda _, __, x: f(x)}
@click.command() # vvvvvvvvvvvvvvvv
@click.argument('symbols', **click_callback(lambda s: s.split(',')))
@click.option('--start_date', **click_callback(check_date))
@click.option('--end_date', **click_callback(check_date))
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
print(symbols, start_date, end_date, file_name)
我想要 '05-Jul-2018' 格式的日期字符串,以下对我有用
def check_date(ctx, param, value):
try:
_ = dt.strptime(value, "%Y-%m-%d")
return value
except ValueError:
msg = "Not a valid date: '{0}'.".format(value)
raise Exception(msg)
def getDate():
return '09-Jul-2018' #you can have any code here
@click.command()
@click.option('--start_date', callback=check_date, default=lambda :getDate())
def f(symbols, start_date):
print(symbols, start_date)
if __name__ == '__main__':
f()
您可以找到更多帮助Here
我正在使用 click
定义一个 CLI,它采用 datetime
s 和逗号分隔的参数列表。
import click
def valid_date(s):
try:
return dt.strptime(s, "%Y-%m-%d")
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise Exception(msg)
except TypeError:
return None
split_parameter = lambda _, __, s: s.split(",")
check_date = lambda _, __, s: valid_date(s)
@click.command()
@click.argument('symbols', callback=split_parameter)
@click.option('--start_date', callback=check_date)
@click.option('--end_date', callback=check_date)
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
return None
它有效,但围绕 callback
的逻辑有点尴尬。拆分列表和转换为 datetime
都会中断点击的简单结构。有没有一种 pythonic 的方式来做到这一点?
您可以将丑陋的 lambda _, __, x
部分封装在装饰器中:
import click
from datetime import datetime as dt
def click_callback(f):
return lambda _, __, x: f(x)
def check_date(s):
try:
return dt.strptime(s, "%Y-%m-%d")
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise Exception(msg)
except TypeError:
return None
@click.command()
@click.argument('symbols', callback=click_callback(lambda s: s.split(',')))
@click.option('--start_date', callback=click_callback(check_date))
@click.option('--end_date', callback=click_callback(check_date))
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
print(symbols, start_date, end_date, file_name)
if __name__ == '__main__':
f()
或者像这样(只显示相关部分):
def click_callback(f):
return {'callback': lambda _, __, x: f(x)}
@click.command() # vvvvvvvvvvvvvvvv
@click.argument('symbols', **click_callback(lambda s: s.split(',')))
@click.option('--start_date', **click_callback(check_date))
@click.option('--end_date', **click_callback(check_date))
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
print(symbols, start_date, end_date, file_name)
我想要 '05-Jul-2018' 格式的日期字符串,以下对我有用
def check_date(ctx, param, value):
try:
_ = dt.strptime(value, "%Y-%m-%d")
return value
except ValueError:
msg = "Not a valid date: '{0}'.".format(value)
raise Exception(msg)
def getDate():
return '09-Jul-2018' #you can have any code here
@click.command()
@click.option('--start_date', callback=check_date, default=lambda :getDate())
def f(symbols, start_date):
print(symbols, start_date)
if __name__ == '__main__':
f()
您可以找到更多帮助Here