Miscellaneous utility functions.

pebl.util.as_list(c)

Ensures that the result is a list.

If input is a list/tuple/set, return it. If it’s None, return empty list. Else, return a list with input as the only element.

pebl.util.autoassign(self, locals)

Automatically assigns local variables to self. Generally used in __init__ methods, as in:

def __init__(self, foo, bar, baz=1): 
    autoassign(self, locals())
pebl.util.cartesian_product(list_of_lists)

Given n lists (or sets), generate all n-tuple combinations.

>>> list(cartesian_product([[0,1], [0,1,"foo"]]))
[(0, 0), (0, 1), (0, 'foo'), (1, 0), (1, 1), (1, 'foo')]
>>> list(cartesian_product([[0,1], [0,1], [0,1]]))
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
pebl.util.cond(condition, expr1, expr2)

Marked for deletion.. Python2.5 provides this.

pebl.util.edit_distance(network1, network2)

Returns the edit distance between two networks.

This is a good (but not the only one) metric for determining similarity between two networks.

pebl.util.entropy_of_list(lst)

Given a list of values, generate histogram and calculate the entropy.

pebl.util.extended_property(func)

Function decorator for defining property attributes

The decorated function is expected to return a dictionary containing one or more of the following pairs:

  • fget - function for getting attribute value
  • fset - function for setting attribute value
  • fdel - function for deleting attribute
pebl.util.flatten(seq)

Given a nested datastructure, flatten it.

pebl.util.levenshtein(a, b)

Calculates the Levenshtein distance between strings a and b.

from http://hetland.org/coding/python/levenshtein.py

pebl.util.logadd(x, y)

Adds two log values.

Ensures accuracy even when the difference between values is large.

pebl.util.logscale_probwheel(items, logweights)

Randomly select an item from a [log] weighted list of items.

Fucntion just rescale logweights and exponentiates before calling probwheel.

pebl.util.logsum(lst)

Sums a list of log values, ensuring accuracy.

pebl.util.lru_cache(maxsize)

Decorator applying a least-recently-used cache with the given maximum size.

Arguments to the cached function must be hashable. Cache performance statistics stored in f.hits and f.misses.

from http://code.activestate.com/recipes/498245/

pebl.util.nestediter(lst1, lst2)

A syntactic shortform for doing nested loops.

pebl.util.normalize(lst)

Normalizes a list of numbers (sets sum to 1.0).

pebl.util.probwheel(items, weights)

Randomly select an item from a weighted list of items.

pebl.util.rescale_logvalues(lst)

Rescales a list of log values by setting max value to 0.0

This function is necessary when working with list of log values. Without it, we could have overflows. This is a lot faster than using arbitrary precision math libraries.

pebl.util.unzip(l, *jj)

Opposite of zip().

jj is a tuple of list indexes (or keys) to extract or unzip. If not specified, all items are unzipped.

Previous topic

taskcontroller.ec2 – Amazon EC2 task controller

This Page