2014年8月9日土曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 6(A Modular Approach to Program Organization)、6.6(Exercises) 3-a, b.を解いてみる。

6.6(Exercises) 3-a, b.

コード(BBEdit)

exercise.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

def average(num1, num2):
    """ (number, number) -> number
    Return the average of num1 and num2.
    >>> average(10, 20)
    15.0
    >>> average(2.5, 3.0)
    2.75
    """
    return (num1 + num2) / 2

入出力結果(Terminal, IPython)

$ ipython
Python 3.4.1 (default, May 21 2014, 01:39:38) 
Type "copyright", "credits" or "license" for more information.

IPython 2.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import exercise

In [2]: import doctest

In [3]: doctest.testmod(exercise)
**********************************************************************
File "/Users/kamimura/Documents/py/prac_prog/ch6/exercise.py", line 7, in exercise.average
Failed example:
    average(10, 20)
Expected:
    15.0
Got:
    20.0
**********************************************************************
File "/Users/kamimura/Documents/py/prac_prog/ch6/exercise.py", line 9, in exercise.average
Failed example:
    average(2.5, 3.0)
Expected:
    2.75
Got:
    4.0
**********************************************************************
1 items had failures:
   2 of   2 in exercise.average
***Test Failed*** 2 failures.
Out[3]: TestResults(failed=2, attempted=2)

In [4]: doctest.testmod(exercise)
**********************************************************************
File "/Users/kamimura/Documents/py/prac_prog/ch6/exercise.py", line 7, in exercise.average
Failed example:
    average(10, 20)
Expected:
    15.0
Got:
    20.0
**********************************************************************
File "/Users/kamimura/Documents/py/prac_prog/ch6/exercise.py", line 9, in exercise.average
Failed example:
    average(2.5, 3.0)
Expected:
    2.75
Got:
    4.0
**********************************************************************
1 items had failures:
   2 of   2 in exercise.average
***Test Failed*** 2 failures.
Out[4]: TestResults(failed=2, attempted=2)

In [5]: import imp

In [6]: imp.reload(exercise)
Out[6]: <module 'exercise' from '/Users/kamimura/Documents/py/prac_prog/ch6/exercise.py'>

In [7]: doctest.testmod(exercise)
**********************************************************************
File "/Users/kamimura/Documents/py/prac_prog/ch6/exercise.py", line 7, in exercise.average
Failed example:
    average(10, 20)
Expected:
    15
Got:
    15.0
**********************************************************************
1 items had failures:
   1 of   2 in exercise.average
***Test Failed*** 1 failures.
Out[7]: TestResults(failed=1, attempted=2)

In [8]: imp.reload(exercise)
Out[8]: <module 'exercise' from '/Users/kamimura/Documents/py/prac_prog/ch6/exercise.py'>

In [9]: doctest.testmod(exercise)
Out[9]: TestResults(failed=0, attempted=2)

In [10]: quit()
$

0 コメント:

コメントを投稿