Is there any backport for the following methods to work with python 2.4:
any, all, collections.defaultdict, collections.deque
Is there any backport for the following methods to work with python 2.4:
any, all, collections.defaultdict, collections.deque
As Tim points out,
all
andany
are trivial.defaultdict
isn't much more difficult. Here's a passable implementation I believe. It's essentially a translation of the docs into code.update: removed ternary expression because I remembered that that's not in 2.4
If you are just using it to build a dict, then you might want to change the EAFP to LBYL for
__getitem__
. right now it's optimized to build the dict and then use it for a while with a lot of non-miss lookups.never mind. Just read Tims post all the way through. You got your wish.deque
is going to be tougher. I wish I had the time to do that just because It's probably my favorite out of collections but it's non trivial.Well, at least for
any
andall
it's easy:deque
is already in 2.4.As for
defaultdict
, I guess you can emulate that easily withsetdefault()
.Quoting from Alex Martelli`s (and others') highly recommended Python Cookbook: