your feedback to my first project please

Peter Otten __peter__ at web.de
Sat Jan 7 07:59:46 EST 2012


patrick at bierans.de wrote:
> It's my first script in python and I'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.
>   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...


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /