Package gavo :: Package utils :: Module algotricks
[frames] | no frames]

Module algotricks

source code

Some fundamental algorithms not found in the standard library.

Classes
  IndexedGraph
is a graph that is indexed by both incoming and outgoing edges.
  DeferringDict
is a dictionary that stores tuples of a callable and its arguments and will, on the first access, do the calls.
Functions
 
topoSort(edges)
returns a list with the nodes in the graph edges sorted topologically.
source code
 
commonPrefixLength(t1, t2)
returns the length of the common prefix of the sequences t1, t2.
source code
 
chunk(items, getChunkKey, getKeyKey=<__builtin__.function object>)
returns items in a chunked form.
source code
 
identity(val) source code
 
uniqueItems(seq)
returns a list of the uniqe items in seq as they appear in seq.
source code
Variables
  __package__ = 'gavo.utils'
Function Details

topoSort(edges)

source code 

returns a list with the nodes in the graph edges sorted topologically.

edges is a sequence of (from, to)-tuples, where both from and to must be hashable.

A ValueError is raised if the graph is not acyclic.

chunk(items, getChunkKey, getKeyKey=<__builtin__.function object>)

source code 

returns items in a chunked form.

getChunkKey(item)->chunk key is a function returning the chunk title, getKeyKey(chunk)->sort key a function returning a sort key for the chunks (default is just the chunk key, i.e., chunk[0]

The chunked form is a sequence of pairs of a chunk key and a sequence of items belonging to that chunk.

The internal order within each chunk is not disturbed, so you should sort items as desired before passing things in.

This is supposed to work like this:

>>> chunk(["abakus", "Analysis", "knopf", "Kasper", "kohl"],
...   lambda item: item[0].lower())
[('a', ['abakus', 'Analysis']), ('k', ['knopf', 'Kasper', 'kohl'])]

uniqueItems(seq)

source code 

returns a list of the uniqe items in seq as they appear in seq.

Execept for order and return type, this is essentially like set(seq).