Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18642

Re: your feedback to my first project please

From Peter Otten <__peter__@web.de>
Subject Re: your feedback to my first project please
Date 2012-01-07 13:59 +0100
Organization None
References <499.26273427463$1325938350@news.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.4509.1325941199.27778.python-list@python.org> (permalink)

Show all headers | View raw


patrick@bierans.de wrote:

>    It&#39;s my first script in python and I&#39;d like to know if my way
>    of coding <br/>

A warm welcome and a grumpy "please post in plain-text" ;)

>    I am settled - so you can go full on. ;)

You seem to be sure you won't regret that ;)

>    - What about my coding style in general?

Looks good.

>    &#160; Is my naming of variables, classes and methods/functions stupid?

> def __init__(self, dim_, default_=0):

No. Except for the trailing underscore. I think "dim" is a keyword in 
Basic...

>    - Do my test cases cover enough? Are they unelegant?

Don't use assert, use the appropriate method, like self.assertEquals(...).
Generally your test cases test the wrong stuff. An attribute's leading 
underscore is a good hint that you shouldn't inspect its value in your 
tests. For example, to get some confidence that the internal variables are 
initialised correctly you could do

class TestAverageStack(unittest.TestCase):
    def test(self):
        a = AverageStack(10)
        self.assertEquals(a.inout(100), 10)

That way you remain free to completely rewrite the AverageTest 
implementation without changing the test suite.

> Can I write some variables in the assertion messages? I found
> some code in the internet using % and ` but it is not working here.

You must be doing it wrong.

>>> assert False, "who's afraid of %s, %s, and %s?" % ("red", "yellow", 
"blue")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: who's afraid of red, yellow, and blue?

>    - Do you have some performance improvements for my class? It will have
>    to run at highest performance.

Rewrite it in C ;) Seriously, have a look at collections.deque; if you need 
to support only integers you can also safely store the sum and add new 
values and remove outdated ones without risking that errors accumulate.

>                 for i in range(self._dim):
>                         _sum += self._data[i]
>                         _count += 1
>                 return _sum / _count

You need "from __future__  import division":

>>> 1/2
0
>>> from __future__ import division
>>> 1/2
0.5

Also:

>>> d = {1:10, 2:20, 3:30}
>>> sum(d.itervalues())
60

That's all folks...

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: your feedback to my first project please Peter Otten <__peter__@web.de> - 2012-01-07 13:59 +0100

csiph-web