Your Linux Data Center Experts

On Friday Evelyn was asking for a library that did moving averages. I took a look around for things like “moving average”, “circular buffer” and “ring buffer”, but couldn't find anything that seemed to do the job. Possibly because it's “too trivial” to implement on your own. So, I put together some code and released movingaverage.py on pypi and hosted the code on github.

However, the unit tests fail under Python 3, where division returns floats always. I'm pretty sure that what I want to do is change the test so that it understands when it's running on 3 and 2 and expects different values there, I don't think I want it to act the same on both platforms. Or, I guess maybe I'll need to add a “float divide” option to 2.6, or a “// divide” option to 3? Any suggestions?

It's literally a line long, at it's core. It's implemented as a list comprehension. It's taken me nearly 2 hours to do, of course. I did a simple test and found that there was a fence-post error. So I added unit tests, and released it. Then the bug-reports started rolling in; Evelyn has Python 2.6 on Mandrake, and the unit tests were using a 2.7 (Fedora 14) feature of the unittest module. Then I had to fix a license inconsistency (it's in the public domain)…

I'm reminded of when I did string.rsplit()… Initially it was refused because it was “too trivial to reimplement on your own”. The demonstration implementation had a bug in it though. :-) Obviously, it eventually went into the standard library, but not without a year worth of discussion…

Enjoy.

comments powered by Disqus

Join our other satisfied clients. Contact us today.