Miscellaneous utility functions.
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.
Automatically assigns local variables to self. Generally used in __init__ methods, as in:
def __init__(self, foo, bar, baz=1):
autoassign(self, locals())
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)]
Marked for deletion.. Python2.5 provides this.
Returns the edit distance between two networks.
This is a good (but not the only one) metric for determining similarity between two networks.
Given a list of values, generate histogram and calculate the entropy.
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
Given a nested datastructure, flatten it.
Calculates the Levenshtein distance between strings a and b.
Adds two log values.
Ensures accuracy even when the difference between values is large.
Randomly select an item from a [log] weighted list of items.
Fucntion just rescale logweights and exponentiates before calling probwheel.
Sums a list of log values, ensuring accuracy.
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.
A syntactic shortform for doing nested loops.
Normalizes a list of numbers (sets sum to 1.0).
Randomly select an item from a weighted list of items.
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.
Opposite of zip().
jj is a tuple of list indexes (or keys) to extract or unzip. If not specified, all items are unzipped.