Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion ten small Python programs

View parsed - Show only message text

Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
NNTP-Posting-Date: Sun, 27 May 2007 12:08:46 -0500
Date: Sun, 27 May 2007 11:08:43 -0600
From: Steven Bethard <steven.beth...@gmail.com>
User-Agent: Thunderbird 2.0.0.0 (Windows/20070326)
MIME-Version: 1.0
Newsgroups: comp.lang.python
Subject: Re: ten small Python programs
References: <mailman.8220.1180209928.32031.python-list@python.org>
In-Reply-To: <mailman.8220.1180209928.32031.python-list@python.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <I4OdnWQN3OqCJsTbnZ2dnUVZ_qiqnZ2d@comcast.com>
Lines: 34
NNTP-Posting-Host: 67.166.43.236
X-Trace: sv3-9Qkibgh+1SUVyFw8Qw7b1DPuAgFpoiXMAZ+y1jULdFVIMrgjjAk+QZn+OiCwcpoi9Vlp/+zvdRt6H/q!TMLYC3EMdpO1E504lrh9bcBLNhh6YnC/TdIeSdPgK3UukUCehrykobHZA90sAXQ4S45u3AD+nzI3!SUnrp/YXLomn2DqOXOYUm2gEzfp5tQ==
X-Complaints-To: abuse@comcast.net
X-DMCA-Complaints-To: d...@comcast.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.34

Steve Howell wrote:
> --- Steven Bethard <steven.beth...@gmail.com> wrote:
>> Very cool! Do you mind putting this up on the Wiki
>> somewhere so that we 
>> can link to it more easily? Maybe something like:
>>
>>      http://wiki.python.org/moin/SimplePrograms
>>
> 
> Done.

I think I would rewrite the current unit-testing example to use the 
standard library unittest module::

     # Let's write reusable code, and unit test it.
     def add_money(amounts):
         # do arithmetic in pennies so as not to accumulate float errors
         pennies = sum([round(int(amount * 100)) for amount in amounts])
         return float(pennies / 100.0)
     import unittest
     class TestAddMoney(unittest.TestCase):
         def test_float_errors(self):
             self.failUnlessEqual(add_money([0.13, 0.02]), 0.15)
             self.failUnlessEqual(add_money([100.01, 99.99]), 200)
             self.failUnlessEqual(add_money([0, -13.00, 13.00]), 0)
     if __name__ == '__main__':
         unittest.main()

I believe I've still kept it to 13 lines.

STeVe

P.S. The "right" way to add money is using the decimal module, but I 
couldn't think of a better example.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google