The following functions are installed as Python builtin functions at package import time. They are then available as normal builtin functions in every module without explicit import in each module using them (though it is good practice to still put a 'import mx.Tools.NewBuiltins
' at the top of each module relying on these add-ons).
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(sequence)
apply(map,(None,)+tuple(sequence))
does, except that the resulting list will always have the length of the first sub-sequence in 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
.
Note that the function is of the single argument type meaning that calling tuples(a,b,c)
is the same as calling tuples((a,b,c))
. tuples() can be used as inverse to lists().
lists(sequence)
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)
setdict(sequence,value=None)
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.
get(object,index[,default])
object[index]
, or, if that fails, default
. If default
is not given or the singleton NotGiven
an error is raised (the error produced by the object).
extract(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.
iremove(object,indices)
This changes the object in place and thus is only possible for mutable types.
For sequences the index list must be sorted ascending; an IndexError
will be raised otherwise (and the object left in an undefined state).
findattr(object_list,attrname)
attrname
found among the objects in the list. Raises an AttributeError
if the attribute is not found.
attrlist(object_list,attrname)
attrname
found among the objects in the list.
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 a tuple of 10 random integers... l = napply(10,whrandom.randint,(0,10)).
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)
acquire(object,name)
This function can be used as __getattr__ hook in Python classes to enable implicit acquisition along a predefined lookup chain (object.baseobj provides a way to set up this chain). See Examples/Acquistion.py for some sample code.
defined(name)
The function has intimate knowledge about how symbol resolution works in Python: it first looks in locals(), then in globals() and if that fails in __builtins__.
reval(codestring[,locals={}])
No builtins are available for the evaluation. locals can be given as local namespace to use when evaluating the codestring.
After a suggestion by Tim Peters on comp.lang.python.
truth(object)
This function is different from the one in the operator
module: the function does not return truth singletons but integers.
sign(object)
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 :-).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4