您的位置:首页 > 编程语言 > Python开发

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()

###############################################################################
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: