Python契约式设计 - AOP风格的实现
来源:互联网 发布:mac队麦基一号 编辑:程序博客网 时间:2024/06/05 16:51
###############################################################################
#
# This example shows how to implement DBC with an Python implementation of AOP
# - aspects.py.
#
# Note this implementation is not completely done, and will be developed
# further.
#
###############################################################################
import aspects
###############################################################################
class AbstractContractChecker:
"""
Base class of all contract-checker classes.
"""
pass
###############################################################################
class DefaultContractChecker(AbstractContractChecker):
"""
Default contract checker.
"""
# the contract
_contract = None
def __init__(self, contract):
""""""
# contract should be an instance of AbstractContract
assert isinstance(contract, AbstractContract)
# save contract
self._contract = contract
# attach contract-checking methods to wrapper
for c in self._contract.get_method_checkers():
aspects.with_wrap(self._check_contracts, c.get('method'))
def _check_contracts(self, *args, **kwargs):
"""Check contracts."""
# get current class instance
inst = self._get_instance(args)
# get wrapped method
wrapped = yield aspects.get_wrapped
# get contract-checker definition for the current method
d = self._get_checker_def(wrapped)
# check class invariants before the method is executed
self._check_invariant(d, inst, args, kwargs, None)
# check preconditions
pre_checker = d.get('pre_checker')
if pre_checker is not None:
pre_checker(inst, args, kwargs)
# continue the execution if preconditions satisfy
retval = yield aspects.proceed
# check postconditions
post_checker = d.get('post_checker')
if post_checker is not None:
post_checker(inst, args, kwargs, None, retval)
# check class invariants after the method is executed
self._check_invariant(d, inst, args, kwargs, None)
yield aspects.return_stop
def _check_invariant(self, d, inst, args, kwargs, old):
"""Check class invariant holds."""
inv_checker = self._contract.get_invariant_checker()
if d.get('check_inv') and inv_checker is not None:
inv_checker(inst, args, kwargs, old)
def _get_checker_def(self, wrapped):
"""Return contract-checker definition for wrapped method."""
retval = None
for c in self._contract.get_method_checkers():
if str(c.get('method')) == str(wrapped):
retval = c
break
return retval
def _get_instance(self, args):
"""Return current class instance."""
return args[0]
###############################################################################
class ConditionNotHoldError:
"""
Exception indicating condition does not hold.
"""
pass
###############################################################################
class AbstractContract:
"""
Base class of all contract classes.
"""
def check(self, condition, message = 'Condition does not hold.'):
"""Check specified condition holds."""
if not condition:
raise ConditionNotHoldError
def get_invariant_checker(self):
"""Return the method used to check class invariants."""
pass
def get_method_checkers(self):
"""Return a list of contract checkers."""
pass
###############################################################################
- Python契约式设计 - AOP风格的实现
- Python契约式设计范例 - AOP风格的实现
- Python契约式设计的范例 (2) - Tolerant风格
- Python契约式设计的范例
- Python契约式设计的范例 - Class invariants的使用
- Python契约式设计的范例 - assert指令的使用
- 契约式设计
- 契约式设计的优点、开销和限制
- 用metaclass实现AOP风格的Profiler
- 引入契约式设计
- Python实现自己的AOP
- java断言的使用和契约式设计编程模型简介
- Bertrand Meyer谈契约式设计
- Bertrand Meyer谈契约式设计
- Bertrand Meyer谈契约式设计
- Bertrand Meyer谈契约式设计
- Bertrand Meyer谈契约式设计
- Bertrand Meyer谈契约式设计
- linux 单看当前文件目录有多少个文件 以及当前目录占用空间的大小
- 化解字符串不能超过8000的方法
- 检查函数用的栈空间
- css属性border-collapse(细表格)empty-cells(显示空列,没看出来效果)text-transform(大小写)
- sql2005存储过程
- Python契约式设计 - AOP风格的实现
- 排课问题
- 给中国学生的第四封信:大学四年应是这样度过
- 探究CRC32算法实现原理-why table-driven implemention
- assert实作
- 同期及上期数据对比显示
- Linux文件查找命令find,xargs详述[转]
- sql2000存储过程
- 读取目录readdir