Created
September 27, 2010 19:15
-
-
Save gregturn/599630 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python recipe21_report.py recipe21 | |
=============================== | |
== Processing module recipe21 | |
=============================== | |
Documentation for module recipe21 | |
------------------------------- | |
This is documentation for the this entire recipe. | |
With it, we can demonstrate usage of the code. | |
>>> cart = ShoppingCart().add("tuna sandwich", 15.0) | |
>>> len(cart) | |
1 | |
>>> cart.item(1) | |
'tuna sandwich' | |
>>> cart.price(1) | |
15.0 | |
>>> print round(cart.total(9.25), 2) | |
16.39 | |
------------------------------- | |
Documentation for Item.module Item - None | |
Documentation for Item.__init__() - None | |
Documentation for ShoppingCart.module ShoppingCart | |
------------------------------- | |
This object is used to store the goods. | |
It conveniently calculates total cost including | |
tax. | |
------------------------------- | |
Documentation for ShoppingCart.__init__() - None | |
Documentation for ShoppingCart.__len__() | |
------------------------------- | |
Support len(cart) operation. | |
------------------------------- | |
Documentation for ShoppingCart.add() | |
------------------------------- | |
Add an item to the internal list. | |
------------------------------- | |
Documentation for ShoppingCart.item() | |
------------------------------- | |
Look up the item. The cart is a 1-based index. | |
------------------------------- | |
Documentation for ShoppingCart.price() | |
------------------------------- | |
Look up the price. The cart is a 1-based index. | |
------------------------------- | |
Documentation for ShoppingCart.total() | |
------------------------------- | |
Add up all costs, and then apply a sales tax. | |
------------------------------- | |
Running doctests for recipe21 | |
------------------------------- | |
(ptc)gturnquist-mbp:03 gturnquist$ python recipe21_report.py recipe21 -v | |
=============================== | |
== Processing module recipe21 | |
=============================== | |
Documentation for module recipe21 | |
------------------------------- | |
This is documentation for the this entire recipe. | |
With it, we can demonstrate usage of the code. | |
>>> cart = ShoppingCart().add("tuna sandwich", 15.0) | |
>>> len(cart) | |
1 | |
>>> cart.item(1) | |
'tuna sandwich' | |
>>> cart.price(1) | |
15.0 | |
>>> print round(cart.total(9.25), 2) | |
16.39 | |
------------------------------- | |
Documentation for Item.module Item - None | |
Documentation for Item.__init__() - None | |
Documentation for ShoppingCart.module ShoppingCart | |
------------------------------- | |
This object is used to store the goods. | |
It conveniently calculates total cost including | |
tax. | |
------------------------------- | |
Documentation for ShoppingCart.__init__() - None | |
Documentation for ShoppingCart.__len__() | |
------------------------------- | |
Support len(cart) operation. | |
------------------------------- | |
Documentation for ShoppingCart.add() | |
------------------------------- | |
Add an item to the internal list. | |
------------------------------- | |
Documentation for ShoppingCart.item() | |
------------------------------- | |
Look up the item. The cart is a 1-based index. | |
------------------------------- | |
Documentation for ShoppingCart.price() | |
------------------------------- | |
Look up the price. The cart is a 1-based index. | |
------------------------------- | |
Documentation for ShoppingCart.total() | |
------------------------------- | |
Add up all costs, and then apply a sales tax. | |
------------------------------- | |
Running doctests for recipe21 | |
------------------------------- | |
Trying: | |
cart = ShoppingCart().add("tuna sandwich", 15.0) | |
Expecting nothing | |
ok | |
Trying: | |
len(cart) | |
Expecting: | |
1 | |
ok | |
Trying: | |
cart.item(1) | |
Expecting: | |
'tuna sandwich' | |
ok | |
Trying: | |
cart.price(1) | |
Expecting: | |
15.0 | |
ok | |
Trying: | |
print round(cart.total(9.25), 2) | |
Expecting: | |
16.39 | |
ok | |
9 items had no tests: | |
recipe21.Item | |
recipe21.Item.__init__ | |
recipe21.ShoppingCart | |
recipe21.ShoppingCart.__init__ | |
recipe21.ShoppingCart.__len__ | |
recipe21.ShoppingCart.add | |
recipe21.ShoppingCart.item | |
recipe21.ShoppingCart.price | |
recipe21.ShoppingCart.total | |
1 items passed all tests: | |
5 tests in recipe21 | |
5 tests in 10 items. | |
5 passed and 0 failed. | |
Test passed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import * | |
def print_doc(name, item): | |
if item.__doc__: | |
print "Documentation for %s" % name | |
print "-------------------------------" | |
print item.__doc__ | |
print "-------------------------------" | |
else: | |
print "Documentation for %s - None" % name | |
def print_docstrings(m, prefix=""): | |
print_doc(prefix + "module %s" % m.__name__, m) | |
for (name, value) in getmembers(m, isclass): | |
if name == '__class__': continue | |
print_docstrings(value, prefix=name + ".") | |
if __name__ == "__main__": | |
import sys | |
import doctest | |
for arg in sys.argv[1:]: | |
if arg.startswith("-"): continue | |
print "===============================" | |
print "== Processing module %s" % arg | |
print "===============================" | |
m = __import__(arg) | |
print_docstrings(m) | |
print "Running doctests for %s" % arg | |
print "-------------------------------" | |
doctest.testmod(m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This is documentation for the this entire recipe. | |
With it, we can demonstrate usage of the code. | |
>>> cart = ShoppingCart().add("tuna sandwich", 15.0) | |
>>> len(cart) | |
1 | |
>>> cart.item(1) | |
'tuna sandwich' | |
>>> cart.price(1) | |
15.0 | |
>>> print round(cart.total(9.25), 2) | |
16.39 | |
""" | |
class ShoppingCart(object): | |
""" | |
This object is used to store the goods. | |
It conveniently calculates total cost including | |
tax. | |
""" | |
def __init__(self): | |
self.items = [] | |
def add(self, item, price): | |
"Add an item to the internal list." | |
self.items.append(Item(item, price)) | |
return self | |
def item(self, index): | |
"Look up the item. The cart is a 1-based index." | |
return self.items[index-1].item | |
def price(self, index): | |
"Look up the price. The cart is a 1-based index." | |
return self.items[index-1].price | |
def total(self, sales_tax): | |
"Add up all costs, and then apply a sales tax." | |
sum_price = sum([item.price for item in self.items]) | |
return sum_price*(1.0 + sales_tax/100.0) | |
def __len__(self): | |
"Support len(cart) operation." | |
return len(self.items) | |
class Item(object): | |
def __init__(self, item, price): | |
self.item = item | |
self.price = price |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment