Functions : Objects : Examples : Structure : Installation : Copyright : History | Version 0.8.1 |
As time passes there have often been situations where I thought "Hey, why not have this as builtin". In most cases the functionality was easily coded in Python. But I started to use them quite heavily and since performance is always an issue (at least for me: hits/second pay my bills), I decided to code them in C. Well, that's how it started and here we are now with an ever growing number of goodies...
indices(object)
tuple(range(len(object)))
-- a tad faster and a lot easier to type.
trange([start=0,]stop[,step=1])
range()
but returns a tuple instead of a list. Since
range()
is most often used in for-loops there
really is no need for a mutable data type and construction
of tuples is somewhat (20%) faster than that of lists. So
changing the usage of range()
in for-loops to
trange()
pays off in the long run.
range_len(object)
range(len(object))
.
tuples(a,b,c,...)
map(None,a,b,c,...)
does, except that the resulting list will always have the
length of the first sequence. The function returns a list
of tuples (a[0], b[0], c[0],...), (a[1], b[1],
c[1],...), ...
with missing elements being filled
in with None
. The latter case should be
avoided, though, since no optimizations are done
(map(None,...) performs better in these situations, well,
sometimes :).
lists(seq)
tuples()
, except that
None
entries are not interpreted as being
placeholders for missing entries. seq must be a sequence
of sequences, e.g. a list of tuples. These are then
converted into a tuple of lists. As with
tuples()
, the first item in the sequence
decides how many lists will be created. All other items
must have at least as many entries.
reverse(sequence)
sequence
in reverse order. A tuple is
returned, if the sequence itself is a tuple. In all other
cases a list is returned.
dict(items)
invdict(dictionary)
irange(object[,indices])
(index,object[index])
. If a sequence
indices
is given, the indices are read from
it. If not, then the index sequence defaults to
trange(len(object))
. Note that
object
can be any object that can handle
object[index]
, e.g. lists, tuples, string,
dictionaries, even your own objects, if they provide a
__getitem__-method. This makes very nifty constructions
possible and extracting items from another sequence
becomes a piece of cake. Give it a try ! You'll soon love
this little function.
ifilter(condition,object[,indices])
(index,object[index])
such that condition(object[index])
is true
and index is found in the sequence indices (defaulting to
trange(len(object))
). Order is
preserved. condition must be a callable object. Same
comment as above.
get(object,index[,default])
object[index]
, or, if that fails,
default
.
mget(object,indices[,defaults])
object[index]
for
each index in the sequence indices
. If a
lookup fails and the sequence defaults
is
given, then defaults[nth_index]
is used,
where nth_index
is the index of
index
in indices
(confused ? it
works as expected !). defaults
should have
the same length as indices
. If you need the
indices as well, try the irange
function. The
function raises an IndexError
in case it
can't find an entry in indices or defaults.
findattr(object_list,attrname)
attrname
found among the objects in the
list. Raises an AttributeError
if the
attribute is not found.
napply(number_of_calls,function[,args=(),kw={}])
number_of_calls
times with the same arguments and returns a tuple with the
return values. This is roughly equivalent to a for-loop
that repeatedly calls apply(function,args,kw)
and stores the return values in a tuple. Example: create
10 instances of a certain class... objects =
napply(10,Class,()).
mapply(callable_objects[,args=(),kw={}])
callable_objects
. This function has a
functionality dual to that of map()
. While
map applies many different arguments to one callable
object, this function applies one set of arguments to many
different callable objects.
method_mapply(objects,methodname[,args=(),kw={}])
objects
. A simple application is
e.g. method_mapply([a,b,c],'method', (x,y))
resulting in a tuple (a.method(x,y), b.method(x,y),
c.method(x,y))
. Thanks to Aaron Waters for
suggesting this function.
count(condition,sequence)
exists(condition,sequence)
forall(condition,sequence)
index(condition,sequence)
ValueError
is raised in case no item
is found. condition must be a callable object.
sizeof(object)
A note on the naming scheme used:
i
stands for indexed, meaning that you have
access to indices
m
stands for multi, meaning that processing involves multiple
objects
n
stands for n-times, e.g. a function is executed a
certain number of times
t
stands for tuple
x
stands for lazy evaluation
Since this is (and will always be) work-in-progress, more functions will eventually turn up in this module, so stopping by every now and then is not a bad idea :-).
As of version 0.4 the package also includes a contribution by
Christopher Tavares (see xmapmodule.c for his email address): an
extension type for lazy evaluation of map()
-constructs
called xmap()
:
xmap(func, seq, [seq, seq, ...])
map(func,
seq, [seq, seq, ...])
. The object behaves like a
list, but evaluation of the function is postponed until a
specific element from the list is requested. Unlike map,
xmap can handle sequences not having a __len__ method
defined (due to the evaluation-on-demand feature). These objects define one method:
tolist()
I am providing this extension AS-IS in this release, since I haven't had time yet to adapt it to my coding style.
Usage should be clear, so I'll skip the examples (see the test.py script which is included in the archive for some rudimentary examples).
[NewBuiltins] mxTools
Entries enclosed in brackets are packages (i.e. they are
directories that include a __init__.py file) or
submodules. Ones without brackets are just simple
subdirectories that are not accessible via
import
. These are used for compiling the C
extension modules which will get installed in the same place
where all your other site specific extensions live
(e.g. /usr/local/lib/python-x.xx/site-packages).
Note: Importing NewBuiltins
will
automatically install the functions and objects defined in
this module as builtins. They are then available in all other
modules without having to import then again every time. If you
don't want this feature, then import mxTools directly.
First, download the archive,
unzip it to a directory on your path and then follow these
steps (assuming you have already installed Python):
David Ascher has kindly provided pre-compiled Windows
versions of the extensions (for mxTools version 0.6 and xmap
version 0.2), so if you're running WinXX, you can skip the
following and start using the package right away.
Though the module has been tested, there may still be some
bugs left. Please post any bug reports, questions
etc. directly to me.
(c) 1997, 1998, Copyright by Marc-André Lemburg; All
Rights Reserved. mailto: mal@lemburg.com
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without fee
or royalty is hereby granted, provided that the above
copyright notice appear in all copies and that both the
copyright notice and this permission notice appear in
supporting documentation or portions thereof, including
modifications, that you make.
THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE
LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
OR PERFORMANCE OF THIS SOFTWARE !
Changes from 0.7 to 0.8:
Changes from 0.6 to 0.7:
Changes from 0.5 to 0.6:
Changes from 0.4 to 0.5:
Changes from 0.3 to 0.4:
Installation
What I'd like to hear from you...
Copyright & Disclaimer
History & Future