A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://www.egenix.com/www2002/python/eGenix-mx-Extensions-v2.x.html/mxTools.html below:

eGenix.com: Website 2002: Python: mxTools

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)
Returns the same as tuple(range(len(object))) -- a tad faster and a lot easier to type.
trange([start=0,]stop[,step=1])
This works like the builtin function 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)
Returns the same as range(len(object)).
tuples(sequence)
Returns much the same as 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)
Same as tuples(sequence), except that a tuple of lists is returned. Can be used as inverse to tuples().
reverse(sequence)
Returns a tuple or list with the elements from 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)
Constructs a dictionary from the given items sequence. The sequence items must contain sequence entries with at least two values. The first one is interpreted as key, the second one as associated object. Remaining values are ignored.
setdict(sequence,value=None)
Constructs a dictionary from the given sequence. The sequence must contain hashable objects which are used as keys. The values are all set to value. Multiple keys are silently ignored. The function comes in handy whenever you need to work with a sequence in a set based context (e.g. to determine the set of used values).
invdict(dictionary)
Constructs a new dictionary from the given one with inverted mappings. Keys become values and vice versa. Note that no exception is raised if the values are not unique. The result is undefined in this case (there is a value:key entry, but it is not defined which key gets used).
irange(object[,indices])
Builds a tuple of tuples (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])
Builds a list of tuples (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])
Returns 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])
Builds a list with entries 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)
Removes the items indexed by indices from object.

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)
Returns the first attribute with name attrname found among the objects in the list. Raises an AttributeError if the attribute is not found.
attrlist(object_list,attrname)
Returns a list of all attributes with name attrname found among the objects in the list.
napply(number_of_calls,function[,args=(),kw={}])
Calls the given function 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={}])
Creates a tuple of values by applying the given arguments to each object in the sequence 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={}])
Creates a tuple of values by applying the given arguments to each object's <methodname> method. The objects are processed as given in the sequence 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)
Counts the number of objects in sequence for which condition returns true and returns the result as integer. condition must be a callable object.
exists(condition,sequence)
Return 1 if and only if condition is true for at least one of the items in sequence and 0 otherwise. condition must be a callable object.
forall(condition,sequence)
Return 1 if and only if condition is true for all of the items in sequence and 0 otherwise. condition must be a callable object.
index(condition,sequence)
Return the index of the first item for which condition is true. A ValueError is raised in case no item is found. condition must be a callable object.
sizeof(object)
Returns the number of bytes allocated for the given Python object. Additional space allocated by the object and stored in pointers is not taken into account (though the pointer itself is). If the object defines tp_itemsize in its type object then it is assumed to be a variable size object and the size is adjusted accordingly.
acquire(object,name)
Looks up the attribute name in object.baseobj and returns the result. If object does not have an attribute 'baseobj' or that attribute is None or the attribute name starts with an underscore, an AttributeError is raised.

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)
Returns true iff a symbol name is defined in the current namespace.

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={}])
Evaluates the given codestring in a restricted environment that only allows access to operators and basic type constructors like (), [] and {}.

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)
Returns the truth value of object as truth singleton (True or False). Note that the singletons are ordinary Python integers 1 and 0, so you can also use them in calculations.

This function is different from the one in the operator module: the function does not return truth singletons but integers.

sign(object)
Returns the signum of object interpreted as number, i.e. -1 for negative numbers, +1 for positive ones and 0 in case it is equal to 0. The method used is equivalent to cmp(object,-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