try/except 和决策
try/except and descision making
我正在开发一个函数,它将不同类型的 date_formats 作为参数并将其分派给一个负责解析此格式的函数
换句话说:
def parse_format(date_format):
# descision making happens here
try:
from_timestamp(date_format)
except:
pass
try:
from_dateformat(date_format)
except:
pass
def from_timestamp(format):
# raise if not in charge
def from_dateformat(format):
# raise if not in charge
def from_custom_format(format):
# raise if not in charge
目前,parse_format 有多个 try/except 块。这是要走的路,还是有更明显的方法?此外,我该如何处理每次函数调用都失败的情况?
我会这样做:
class UnrecognizedFormatError(Exception):
pass
def parse_format(date_format):
methods = (from_timestamp, from_dateformat)
for method in methods:
try:
return method(date_format)
except:
pass
raise UnrecognizedFormatError
还有一些重点:
except
没有特定的异常是不好的,因为异常可能会从意想不到的地方抛出,例如 运行 内存不足,或者脚本中的键盘中断。所以请使用 except SomeException as e
形式,并使用特定的异常类型。
- 如果每个函数都失败,此代码将抛出一个
UnrecognizedFormatError
,允许函数的用户做出适当的响应。
好吧,我认为这是 try/except/else/finally 的好地方 - else 捕捉你的最终情况,其中每个函数调用都失败,并且 'finally' 是 运行 无论发生什么在您的 try/except 语句中。如果您的异常选择得当,那么它会为您选择正确的功能。
此外,我猜这是一个学习练习,因为您描述的 activity 最好在 date.strftime()
中完成
def from_timestamp(format):
# raise if not in charge
def from_dateformat(format):
# raise if not in charge
def from_custom_format(format):
# raise if not in charge
def parse_format(date_format):
# decision making happens here
try:
from_timestamp(date_format)
except(FirstException):
from_dateformat(date_format)
except(SecondException):
from_custom_format(date_format)
else:
whatever_you_do_if_it_all_goes_wrong()
finally:
thing_that_happens_regardless_of_what's_called()
我正在开发一个函数,它将不同类型的 date_formats 作为参数并将其分派给一个负责解析此格式的函数
换句话说:
def parse_format(date_format):
# descision making happens here
try:
from_timestamp(date_format)
except:
pass
try:
from_dateformat(date_format)
except:
pass
def from_timestamp(format):
# raise if not in charge
def from_dateformat(format):
# raise if not in charge
def from_custom_format(format):
# raise if not in charge
目前,parse_format 有多个 try/except 块。这是要走的路,还是有更明显的方法?此外,我该如何处理每次函数调用都失败的情况?
我会这样做:
class UnrecognizedFormatError(Exception):
pass
def parse_format(date_format):
methods = (from_timestamp, from_dateformat)
for method in methods:
try:
return method(date_format)
except:
pass
raise UnrecognizedFormatError
还有一些重点:
except
没有特定的异常是不好的,因为异常可能会从意想不到的地方抛出,例如 运行 内存不足,或者脚本中的键盘中断。所以请使用except SomeException as e
形式,并使用特定的异常类型。- 如果每个函数都失败,此代码将抛出一个
UnrecognizedFormatError
,允许函数的用户做出适当的响应。
好吧,我认为这是 try/except/else/finally 的好地方 - else 捕捉你的最终情况,其中每个函数调用都失败,并且 'finally' 是 运行 无论发生什么在您的 try/except 语句中。如果您的异常选择得当,那么它会为您选择正确的功能。
此外,我猜这是一个学习练习,因为您描述的 activity 最好在 date.strftime()
def from_timestamp(format):
# raise if not in charge
def from_dateformat(format):
# raise if not in charge
def from_custom_format(format):
# raise if not in charge
def parse_format(date_format):
# decision making happens here
try:
from_timestamp(date_format)
except(FirstException):
from_dateformat(date_format)
except(SecondException):
from_custom_format(date_format)
else:
whatever_you_do_if_it_all_goes_wrong()
finally:
thing_that_happens_regardless_of_what's_called()