我有一个脚本要求用户输入很多日期,主要是在类似下面的功能中。这些需要考虑无效的日期格式:
import numpy as np
import pandas as pd
import xlwings as xw
import datetime as dtt
def week_init():
"""Creates an initial week datetime and determines what the checking cutoff
'beforeday' to be used in Stage 2 is."""
week = input('Week to check: MM/DD/YYYY\n')
switch = 1
while switch == 1:
try:
week = dtt.datetime.strptime(week,'%m/%d/%Y') #turns input to a datetime
switch = 0
except ValueError:
week = input('Unrecognized date format, please try again: MM/DD/YYYY\n')
beforeday = (input('Check days before date (Press enter to use today): MM/DD/YYYY\n')
or dtt.date.today())
if (beforeday != dtt.date.today()):
switch = 1
while switch == 1:
try:
beforeday = dtt.datetime.strptime(beforeday,'%m/%d/%Y')
switch = 0
except ValueError:
beforeday = input('Unrecognized date format, please try again: MM/DD/YYYY\n')
return week, beforeday
还有一些功能可以检查给定日期的索引,并且必须处理与任何索引不匹配的给定日期:
def spotmap(week, bam, pt):
"""Maps the pivoted data for the chosen week to the BAM dataframe's SD column."""
print('Mapping data to BAM... ',end=''),
switch = 1
while switch == 1:
try:
bam['SD'] = bam.index.to_series().map(pt.fillna(0)['len',week])
switch = 0
except KeyError:
print('Invalid date, please try again.')
week = input('Week start date (Sunday): MM/DD/YYYY\n')
print('Done')
return bam
由于脚本在获得这些日期后有很多事情要做,我不希望它在出现问题时崩溃,但是如果没有正确的日期输入它就无法进行,所以我目前让它循环与那些"开关"控制何时愿意继续使用有效日期的变量。但是,正如您所看到的,这些try / except块正在快速膨胀其他简单的代码。有没有办法压缩这些东西?还有,比开关更好的方法吗?
Repetitive Try and Except Clauses
这里的答案建议装饰者,但据我在文档和this这样的页面中可以看出,这些是用于包装函数,而不是用于替换内部代码块。此外,我不确定它们有多么有用,因为try / except块在他们尝试完成的内容(即,他们正在改变哪个变量)中大多是唯一的。我想我真的只是希望有更好的语法。
答案 0 :(得分:0)
尝试在函数中包含重复调用以减少膨胀。这是对它的抨击:
import numpy as np
import pandas as pd
import xlwings as xw
import datetime as dtt
def loop_until_good(func, arg):
looping = True
while looping:
(arg, looping) = func(arg)
return arg
def make_datetime(date):
try:
date = dtt.datetime.strptime(date,'%m/%d/%Y') #turns input to a datetime
return date, False
except ValueError:
date = input('Unrecognized date format, please try again: MM/DD/YYYY\n')
return date, True
def bammit(arg):
(bam, week, pt) = arg
try:
bam['SD'] = bam.index.to_series().map(pt.fillna(0)['len',week])
return (bam, week, pt), False
except KeyError:
print('Invalid date, please try again.')
week = input('Week start date (Sunday): MM/DD/YYYY\n')
return (bam, week, pt), True
def week_init():
"""Creates an initial week datetime and determines what the checking cutoff
'beforeday' to be used in Stage 2 is."""
week = str(input('Week to check: MM/DD/YYYY\n'))
print type(week)
print week
loop_until_good(make_datetime, week)
beforeday = (input('Check days before date (Press enter to use today): MM/DD/YYYY\n')
or dtt.date.today())
if (beforeday != dtt.date.today()):
loop_until_good(make_datetime, beforeday)
return week, beforeday
def spotmap(week, bam, pt):
"""Maps the pivoted data for the chosen week to the BAM dataframe's SD column."""
print('Mapping data to BAM... ')
loop_until_good(bammit, (bam, week, pt))
print('Done')
return bam