import functools
## Check return type and value.
# @details The return value could be one following situation:
# 1. value type is specified in the \a cls;
# 2. None;
# 3. value type is not specified and not None.
#
# In the 1st situation, return original value.
# In the 2nd situation, if allow_none is true, return None; otherwise, return \a default.
# In the 3rd situation, raise SyntaxError.
# @param default is the default return value.
# @param cls is the specified return value type.
# @param allow_none - if true, the return value could be None; otherwise, the return value must be assigned.
# @exception SyntaxError
def checkReturn(default, allow_type = None, allow_none = False):
def check_decorator(function):
def decorator_wrapper(*args, **kwargs):
result = function(*args, **kwargs)
cls = allow_type
try:
if allow_type is None:
cls = (type(default),)
elif not issubclass(allow_type, tuple):
cls = (allow_type,)
except Exception as e:
raise
if type(result) in cls:
pass
elif result is None:
if allow_none:
pass
else:
result = default
else:
raise SyntaxError("The type of return value {0} is not in {1}.".format(result, cls))
return result
return decorator_wrapper
return check_decorator
2014年7月23日 星期三
Python - decorator運用(2)
在撰寫函式時,因為python的特性,不會去檢查回傳值的資料型態是否正確,也不會去檢查是否遺漏回傳值,這對於撰寫複雜一點的函式容易有疏忽的地方,這時候就能靠decorator做一些協助,例如遺漏回傳值時補上預設回傳值,或者回傳值型態錯誤時丟出例外錯誤。
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言