Message from discussion
ten small Python programs
Path: g2news1.google.com!news3.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!news.tiscali.de!newsfeed.freenet.de!feeder.news-service.com!feeder.news-service.com!newsgate.cistron.nl!xs4all!transit2.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path: <showel...@yahoo.com>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@bag.python.org
X-Spam-Status: OK 0.011
X-YMail-OSG: G.q417kVM1kpKDiK0PLjwJn6YgJqcv1OZnjqRHtz0si6G68WJwAAWloaa.L1TgYRH3W0n_a_Tu5rvie1yzWbxmUoqOnBPqRBUutKCgVq7uxhSksx_0RKUbd7mvDw8Q--
Date: Sun, 27 May 2007 11:08:38 -0700 (PDT)
From: Steve Howell <showel...@yahoo.com>
Subject: Re: ten small Python programs
To: Steven Bethard <steven.beth...@gmail.com>, python-l...@python.org
In-Reply-To: <I4OdnWQN3OqCJsTbnZ2dnUVZ_qiqnZ2d@comcast.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.8268.1180289324.32031.python-list@python.org>
Lines: 57
NNTP-Posting-Host: 194.109.207.14
X-Trace: 1180289324 news.xs4all.nl 334 [::ffff:194.109.207.14]:42687
X-Complaints-To: abuse@xs4all.nl
--- Steven Bethard <steven.beth...@gmail.com> wrote:
> 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.
>
I approve this change, although in a sense, it's
harder for a Python newbie, because it introduces
inheritance a little earlier than I would have liked.
FWIW I'm in the minority (I think) of people that
prefer roll-your-own testing, but I don't want to
argue that, because I think it mostly comes down to
personal preference.
I'll only defend my position by posting this link,
which suggests that roll-your-own even has validity in
an educational setting:
http://www.elkner.net/jeff/testFirst/index.html
> P.S. The "right" way to add money is using the
> decimal module, but I
> couldn't think of a better example.
Agreed. Maybe somebody else will come up with
something more creative, but I'm happy enough with our
current version.
____________________________________________________________________________________Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, photos & more.
http://mobile.yahoo.com/go?refer=1GNXIC