Python契约式设计的范例 - assert指令的使用
2009-07-23 16:49
351 查看
###############################################################################
#
# This example shows the use of 'assert' instructions in a program.
#
###############################################################################
import sys
import array
###############################################################################
class IntegerStack5:
"""
Stacks: Data tructures with a Last-In, First-Out access policy.
This stack holds integer elements.
Examples::
# test non-empty stack
>>> x = IntegerStack5(1)
>>> x.is_empty()
True
>>> x.is_full()
False
>>> x.put(1)
>>> x.is_empty()
False
>>> x.is_full()
True
>>> x.item()
1
>>> x.remove()
>>> x.is_empty()
True
>>> x.is_full()
False
# test empty stack
>>> y = IntegerStack5()
>>> y.is_empty()
True
>>> y.is_full()
True
inv[]::
# the number of stack elements fall into reasonable range
self._valid_element_count()
# stack is empty if and only if its element count is 0
self._is_empty() == (self._count == 0)
# stack is full if and only if its element count is equal to its
# capacity
self._is_full() == (self._count == self._capacity)
# item is at the top of the stack, or stack is empty
self._item_is_at_top()
"""
# maximum number of stack elements
_capacity = 0
# number of stack elements
_count = 0
# the array used to hold the stack elements
_representation = array.array('i', [])
def __init__(self, n = 0):
"""Allocate stack for a maximum of n elements.
pre::
# n should be a non-negative integer
isinstance(n, int) and n >= 0
post[]::
# stack compacity is equal to n
self.capacity() == n
# the array holding stack elements should not be void
self._representation is not None
# stack is empty
self.is_empty()
"""
self._capacity = n
self._count = 0
def capacity(self):
"""Maximum number of stack elements."""
return self._capacity
def count(self):
"""Number of stack elements."""
return self._count
def item(self):
"""Top element.
pre::
# stack is not empty
not self.is_empty()
"""
return self._item()
def is_empty(self):
"""Is stack empty?
post[]::
# stack is empty if and only if its element count is 0
__return__ == self._is_empty()
"""
return self._is_empty()
def is_full(self):
"""Is stack representation full?
post[]::
# stack is full if and only if its element count is equal to
# its capacity
__return__ == self._is_full()
"""
return self._is_full()
def put(self, x):
"""Put x on top.
pre::
# x is an integer
isinstance(x, int)
# stack is not full
not self.is_full()
post[self]::
# stack is not empty
not self.is_empty()
# stack element count increases by 1
self._count is __old__.self._count + 1
"""
# self._representation is not None because of class invariant
assert self._representation is not None
self._representation.append(x)
self._count += 1
def remove(self):
"""Remove top element.
pre::
# stack is not empty
not self.is_empty()
post[self]::
# stack is not full
not self.is_full()
# stack element count decrease by 1
self._count is __old__.self._count - 1
"""
# self._representation is not None because of class invariant
assert self._representation is not None
self._representation.pop()
self._count -= 1
def _is_empty(self):
"""Is stack empty?
Help method.
"""
return self._count == 0
def _is_full(self):
"""Is stack representation full?
Help method.
"""
return self._count == self._capacity
def _item(self):
"""Return top element if stack is not empty.
Help method.
pre::
# valid boundary
0 < self._count <= len(self._representation)
"""
return self._representation[self._count - 1]
def _item_is_at_top(self):
"""Is item at the top of the stack?
This method helps express assertions.
"""
return self._count > 0 and /
self._item() == self._representation[self._count - 1] or /
self._is_empty()
def _valid_element_count(self):
"""Is the number of stack elements fall into reasonable range?
This method helps express assertions.
"""
return self._capacity >= self._count >= 0 and /
self._count is len(self._representation)
###############################################################################
import contract
contract.checkmod(__name__)
###############################################################################
def _test():
import doctest, use_assert_instruction
return doctest.testmod(use_assert_instruction)
###############################################################################
if __name__ == '__main__':
_test()
###############################################################################
#
# This example shows the use of 'assert' instructions in a program.
#
###############################################################################
import sys
import array
###############################################################################
class IntegerStack5:
"""
Stacks: Data tructures with a Last-In, First-Out access policy.
This stack holds integer elements.
Examples::
# test non-empty stack
>>> x = IntegerStack5(1)
>>> x.is_empty()
True
>>> x.is_full()
False
>>> x.put(1)
>>> x.is_empty()
False
>>> x.is_full()
True
>>> x.item()
1
>>> x.remove()
>>> x.is_empty()
True
>>> x.is_full()
False
# test empty stack
>>> y = IntegerStack5()
>>> y.is_empty()
True
>>> y.is_full()
True
inv[]::
# the number of stack elements fall into reasonable range
self._valid_element_count()
# stack is empty if and only if its element count is 0
self._is_empty() == (self._count == 0)
# stack is full if and only if its element count is equal to its
# capacity
self._is_full() == (self._count == self._capacity)
# item is at the top of the stack, or stack is empty
self._item_is_at_top()
"""
# maximum number of stack elements
_capacity = 0
# number of stack elements
_count = 0
# the array used to hold the stack elements
_representation = array.array('i', [])
def __init__(self, n = 0):
"""Allocate stack for a maximum of n elements.
pre::
# n should be a non-negative integer
isinstance(n, int) and n >= 0
post[]::
# stack compacity is equal to n
self.capacity() == n
# the array holding stack elements should not be void
self._representation is not None
# stack is empty
self.is_empty()
"""
self._capacity = n
self._count = 0
def capacity(self):
"""Maximum number of stack elements."""
return self._capacity
def count(self):
"""Number of stack elements."""
return self._count
def item(self):
"""Top element.
pre::
# stack is not empty
not self.is_empty()
"""
return self._item()
def is_empty(self):
"""Is stack empty?
post[]::
# stack is empty if and only if its element count is 0
__return__ == self._is_empty()
"""
return self._is_empty()
def is_full(self):
"""Is stack representation full?
post[]::
# stack is full if and only if its element count is equal to
# its capacity
__return__ == self._is_full()
"""
return self._is_full()
def put(self, x):
"""Put x on top.
pre::
# x is an integer
isinstance(x, int)
# stack is not full
not self.is_full()
post[self]::
# stack is not empty
not self.is_empty()
# stack element count increases by 1
self._count is __old__.self._count + 1
"""
# self._representation is not None because of class invariant
assert self._representation is not None
self._representation.append(x)
self._count += 1
def remove(self):
"""Remove top element.
pre::
# stack is not empty
not self.is_empty()
post[self]::
# stack is not full
not self.is_full()
# stack element count decrease by 1
self._count is __old__.self._count - 1
"""
# self._representation is not None because of class invariant
assert self._representation is not None
self._representation.pop()
self._count -= 1
def _is_empty(self):
"""Is stack empty?
Help method.
"""
return self._count == 0
def _is_full(self):
"""Is stack representation full?
Help method.
"""
return self._count == self._capacity
def _item(self):
"""Return top element if stack is not empty.
Help method.
pre::
# valid boundary
0 < self._count <= len(self._representation)
"""
return self._representation[self._count - 1]
def _item_is_at_top(self):
"""Is item at the top of the stack?
This method helps express assertions.
"""
return self._count > 0 and /
self._item() == self._representation[self._count - 1] or /
self._is_empty()
def _valid_element_count(self):
"""Is the number of stack elements fall into reasonable range?
This method helps express assertions.
"""
return self._capacity >= self._count >= 0 and /
self._count is len(self._representation)
###############################################################################
import contract
contract.checkmod(__name__)
###############################################################################
def _test():
import doctest, use_assert_instruction
return doctest.testmod(use_assert_instruction)
###############################################################################
if __name__ == '__main__':
_test()
###############################################################################
相关文章推荐
- Python契约式设计的范例 - Class invariants的使用
- Python契约式设计范例 - AOP风格的实现
- Python契约式设计的范例
- Python使用设计模式中的责任链模式与迭代器模式的示例
- Python中断言assert的使用
- Python正则表达式的七个使用范例
- 使用Tkinter设计基于python的GUI交互
- python程序设计基础4:python函数设计和使用
- pyqt4使用qt designer设计的ui文件的方法(将ui文件转换为python源码和类的方法)
- 实例讲解Python设计模式编程之工厂方法模式的使用
- 流程的Python 第六章:使用一等函数来设计模式
- 使用Mixin设计模式进行Python编程的方法讲解
- 使用Python语言设计基于HTML的C语言语法加亮显示程序
- java断言的使用和契约式设计编程模型简介
- python assert 语句使用
- Python正则表达式使用范例分享
- Python中何时使用断言 assert
- Python使用Matplotlib实现Logos设计代码
- python中assert的使用
- Python正则表达式的七个使用范例