Cookbook package

Cookbook

Some python recipes from the python cookbook

Introduction

This python package implements some recipes from the python cookbook. Some of the recipes are copied verbatim from the python cookbook and some are modified versions of those at the cookbook. Others I (John Reid) have written from scratch.

License

All novel parts of this software are released under the BSD. See the accompanying file python/cookbook/LICENSE. Please also refer to original code for their licensing terms.

Prerequisites

I have tested this software on python 2.6 and 2.7.

Installation

The standard python package install method should work:

python setup.py build
python setup.py install
cookbook.version_string()

Return the release and svn revision as a string.

Bidirectional map

class cookbook.bidirectional_map.BidirectionalMap

A bidirectional map that maintains 2 dicts, one to map each way between elements of type 1 and type 2.

The map is built by adding pairs of elements that are related.

add(v1, v2)

Add a relationship between v1 of type 1 and v2 of type 2.

discard(v1, v2)

Removed a relationship between v1 of type 1 and v2 of type 2.

Will do nothing if relationship is not in map.

map1 = None

Maps from keys of type 1 to values of type 2.

map2 = None

Maps from keys of type 2 to values of type 1.

remove(v1, v2)

Removed a relationship between v1 of type 1 and v2 of type 2.

Will throw key error if relationship is not in map.

Bunch

From: http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ (released under PSF license).

class cookbook.bunch.Bunch(**kwds)

Often we want to just collect a bunch of stuff together, naming each item of the bunch; a dictionary’s OK for that, but a small do-nothing class is even handier, and prettier to use.

Cache decorator

Adapted from: http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/ (released under PSF license).

The latest version of Python introduced a new language feature, function and method decorators (PEP 318, http://www.python.org/peps/pep-0318.html). This recipe show a common callable transformation that can benefit from the new syntax, often referred to as Memoization pattern.

Memoization is a useful pattern for improving the amortized performance of a computationally expensive function or method. The new syntax allows making explicit that a callable is memoized.

A restriction of the recipe above is that the arguments of the function (and also the method receiver object for methods) have to be hashable.

The decorator can be generalized by allowing different caching policies (e.g. a FIFO cache or a cache implementing an LRU policy) apart from the implied “cache-forever” policy of a dict.

class cookbook.cache_decorator.ImmutableDict(*args, **kwds)

A hashable dict.

class cookbook.cache_decorator.PickledMemoize(function, pickle_file, dump_on_update=True)

Like Memoize but pickles its cache/memo for use in next python session

dump_cache()

Dump the memo to the cache.

file

@return: The file we pickle to and from.

load_cache()

Load the memo from the cache.

class cookbook.cache_decorator.pickled_method(file, name)

Pickles the result of the method (ignoring arguments) and uses this if possible on the next call.

Console

Get console terminal size if possible.

From: http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python

cookbook.console.getTerminalSize()

Get console terminal size if possible.

Const

In Python, any variable can be re-bound at will – and modules don’t let you define special methods such as an instance’s __setattr__ to stop attribute re-binding. Easy solution (in Python 2.1 and up): use an instance as ‘module’...

Adapted from http://code.activestate.com/recipes/65207-constants-in-python/ (released under PSF license).

Decorate

Decorator helpers.

cookbook.decorate.DecorateInstanceMethod

Decorator that allows other decorators to decorate instance methods.

cookbook.decorate.simple_decorator(decorator)

This decorator can be used to turn simple functions into well-behaved decorators, so long as the decorators are fairly simple. If a decorator expects a function and returns a function (no descriptors), and if it doesn’t modify function attributes or docstring, then it is eligible to use this. Simply apply @simple_decorator to your decorator and it will automatically preserve the docstring and function attributes of functions to which it is applied.

From http://wiki.python.org/moin/PythonDecoratorLibrary

Dicts

class cookbook.dicts.ChoiceDict(values, cycle=False)

Dictionary that assigns one value in a sequence of values to each key. Can be useful for assigning colours/line styles in plots for example.

it = None

Iterator over values.

class cookbook.dicts.DictOf(missing_value_creator, take_key_as_arg=False)

A dictionary where missing values are created by an __init__ time supplied function.

class cookbook.dicts.DictOfDicts

A dictionary where the values are dictionarys. If an accessed key is missing, the value is initialised to an empty dict

class cookbook.dicts.DictOfInts

A dictionary where the values are ints. If an accessed key is missing, the value is initialised to 0. Useful for counting

class cookbook.dicts.DictOfLists

A dictionary where the values are lists. If an accessed key is missing, the value is initialised to an empty list

class cookbook.dicts.DictOfSets

A dictionary where the values are sets. If an accessed key is missing, the value is initialised to an empty set

class cookbook.dicts.UniqueIds

Holds a unique integer id for each key that is accessed.

cookbook.dicts.dict_of(missing_value_creator, take_key_as_arg=False)

@return: A function that creates DictOf using these args.

Equivalence

From http://code.activestate.com/recipes/499354-equivalence-partition/

cookbook.equivalence.check_equivalence_partition(classes, partitions, relation)

Checks that a partition is consistent under the relationship

cookbook.equivalence.equivalence_enumeration(iterable, relation)

Partitions a set of objects into equivalence classes

Same as equivalence_partition() but also numbers the classes.

Parameters:
  • iterable – collection of objects to be partitioned
  • relation – equivalence relation. I.e. relation(o1,o2) evaluates to True if and only if o1 and o2 are equivalent
Returns:

(classes, partitions, ids) classes is a sequence of sets. Each one is an equivalence class partitions is a dictionary mapping objects to equivalence classes ids is a dictionary mapping objects to the indices of their equivalence classes

cookbook.equivalence.equivalence_partition(iterable, relation)

Partitions a set of objects into equivalence classes

Parameters:
  • iterable – collection of objects to be partitioned
  • relation – equivalence relation. I.e. relation(o1,o2) evaluates to True if and only if o1 and o2 are equivalent
Returns:

(classes, partitions) classes is a sequence of sets. Each one is an equivalence class. partitions is a dictionary mapping objects to equivalence classes.

Function as task

Code to run a function as a task. Useful to make sequential code parallel.

Uses pickling to pass arguments and get result back.

exception cookbook.function_as_task.CalledProcessError(returncode, cmd, output=None)

This exception is raised when a process run by check_call() or check_output() returns a non-zero exit status. The exit status will be stored in the returncode attribute; check_output() will also store the output in the output attribute.

cookbook.function_as_task.check_output(*popenargs, **kwargs)

Run command with arguments and return its output as a byte string.

If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute.

The arguments are the same as for the Popen constructor. Example:

>>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'

The stdout argument is not allowed as it is used internally. To capture standard error in the result, use stderr=STDOUT.

>>> check_output(["/bin/sh", "-c",
...               "ls -l non_existent_file ; exit 0"],
...              stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
cookbook.function_as_task.create_queue(num_worker_threads, do_work)

Create a queue and a number of worker threads on it.

cookbook.function_as_task.create_worker_on_queue(q, do_work)

Create a worker function that takes tasks off a queue.

cookbook.function_as_task.do_task(fn, args_filename, result_filename)

Actually do the task.

cookbook.function_as_task.log_exception(exc_type, exc_obj, exc_tb)

Log an exception. Can be used as replacement for sys.excepthook.

cookbook.function_as_task.run_as_subprocess(module_name, function_name, *args, **kwargs)

Run the named function as a subprocess.

cookbook.function_as_task.sleep(time_to_sleep)

Function to test run function as task functionality.

Group by

http://code.activestate.com/recipes/259173-groupby/ Licensed under the PSF License

Interval

Code to handle intervals.

class cookbook.interval.Interval(start, end)

Represents an interval. Defined as half-open interval [start,end), which includes the start position but not the end. Start and end do not have to be numeric types.

as_slice()

@return: A slice object that the interval indexes.

empty()

@return: True iff self is empty.

end

The interval’s end

hull(other)

@return: Interval containing both self and other.

intersection(other)

Intersection. @return: An empty intersection if there is none.

overlap(other)

@return: True iff self intersects other.

proper_subset(other)

@return: True iff self is proper subset of other.

separation(other)

@return: The distance between self and other.

singleton()

@return: True iff self.end - self.start == 1.

start

The interval’s start

subset(other)

@return: True iff self is subset of other.

zero_in()

@return: True iff 0 in self.

LRU cache

One-line decorator call adds caching to functions with hashable arguments and no keyword arguments. When the maximum size is reached, the least recently used entry is discarded – appropriate for long-running processes which cannot allow caches to grow without bound. Includes built-in performance instrumentation.

Adapted from http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/ (released under PSF license).

cookbook.lru_cache.lru_cache(maxsize=0, cache_storage_file=None)

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.

Named tuple

From http://code.activestate.com/recipes/500261-named-tuples/ Released under PSF license.

Output context

Code to implement output contexts.

class cookbook.output_context.OutputContext

An output context. Uses the Borg design pattern: http://code.activestate.com/recipes/66531-singleton-we-dont-need-no-stinkin-singleton-the-bo/

cache_filename(object_name)

@return: A callable that returns a filename suitable for caching an object.

caching_decorator(name, dump_on_update=False)

@return: A decorator that caches the results of methods.

setUp(root_dir, tag=None)

Set up the output context, i.e. tell it where any output should go.

cookbook.output_context.ensure_dir_exists(dir)

Make sure the given directory exists.

cookbook.output_context.output_context = <cookbook.output_context.OutputContext object at 0x3e14e90>

An object we can use to access the output context.

Permutation

Code to handle permutations and their inverses.

cookbook.permutation.chain_permutations(permutation1, permutation2)

@return: A permutation that consists of the 2 permutations chained together.

cookbook.permutation.identity_permutation(size)

@return: The identity permutation.

cookbook.permutation.invert_permutation(permutation)

@return: The inversion of the permutation.

cookbook.permutation.permute(x, permutation)

@return: A list that is the permuted version of x.

cookbook.permutation.permute_yield(x, permutation)

A generator that yields a permuted version of x.

Persisted cache

Code for lazy cached initialisation.

class cookbook.persisted_cache.Cache(initialiser)

Caches the result of calling a function. The function is evaluated once on the first invocation of self() and the cached result is returned for every invocation thereafter.

class cookbook.persisted_cache.PersistedCache(initialiser, pickle_file)

Caches the result of calling a function. The function is evaluated once on the first invocation of self() and the cached result is returned for every invocation thereafter. The persist method stores the cached result in the given file. This pickled object is reused in preference to function invocation the next time the object is loaded.

persist()

Pickle object to disk

cookbook.persisted_cache.persist_all_in(variables)

Persist all the PersistedCaches in the given set of variables. E.g. persist_all_in(vars().values())

Pre/post conditions

From http://wiki.python.org/moin/PythonDecoratorLibrary

Provide pre-/postconditions as function decorators.

Example usage:

>>> def in_ge20(inval):
...    assert inval >= 20, 'Input value < 20'
...
>>> def out_lt30(retval, inval):
...    assert retval < 30, 'Return value >= 30'
...
>>> @precondition(in_ge20)
... @postcondition(out_lt30)
... def inc(value):
...   return value + 1
...
>>> inc(5)
Traceback (most recent call last):
  ...
AssertionError: Input value < 20
>>> inc(29)
Traceback (most recent call last):
  ...
AssertionError: Return value >= 30
>>> inc(20)
21

You can define as many pre-/postconditions for a function as you like. It is also possible to specify both types of conditions at once:

>>> @conditions(in_ge20, out_lt30)
... def add1(value):
...   return value + 1
...
>>> add1(5)
Traceback (most recent call last):
  ...
AssertionError: Input value < 20

An interesting feature is the ability to prevent the creation of pre-/postconditions at function definition time. This makes it possible to use conditions for debugging and then switch them off for distribution.

>>> debug = False
>>> @precondition(in_ge20, debug)
... def dec(value):
...   return value - 1
...
>>> dec(5)
4

Progress

From: http://code.activestate.com/recipes/473899-progress-meter/ Licensed under the PSF License

Here is a silly example of its usage:

import progress import time import random

total = 1000 p = progress.ProgressMeter(total=total)

while total > 0:
cnt = random.randint(1, 25) p.update(cnt) total -= cnt time.sleep(random.random())

Here is an example of its output:

[————————-> ] 41% 821.2/sec

2006-02-20 Denis Barmenkov: ANSI codes replaced by Backspace (0x08) characters

Pylab utilities

Some utilities to use with matplotlib.

cookbook.pylab_utils.create_format_cycler(**formats)

Cycle through various format strings so that many different data can be displayed together.

If specifying many formats it is best to make them relatively prime to ensure the largest total cycle length.

cookbook.pylab_utils.get_fig_size_for_latex(fig_width_pt=345.0)

Get fig_width_pt from LaTeX using showthecolumnwidth

@return: figure size tuple if fig_width_pt was specified

cookbook.pylab_utils.layout_sub_plot(num_sub_plots, num_rows=0, num_cols=0)

Specify either num_rows or num_cols.

cookbook.pylab_utils.pylab_context_ioff(*args, **kwds)

A way of turning off pylab’s interactive mode with a ‘with’ statement. For example you could do:

with pylab_context_ioff(): ... pylab.figure()

to ensure the figure was created in non-interactive mode. After the context has ended, pylab’s interactive status is returned to what it was before the context was entered.

cookbook.pylab_utils.pylab_ioff(fn)

Decorator that turns pylab interactive mode off for duration of call to decorated function.

cookbook.pylab_utils.set_rcParams_for_latex(fig_width_pt=None)

Set the rcParams in matplotlib to use postscript output.

If fig_width_pt is given then set default figure size to match.

cookbook.pylab_utils.simple_colours = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

Basic pylab colours.

cookbook.pylab_utils.violin_plot(ax, data, pos, bp=False, facecolor='y', alpha=0.3)

create violin plots on an axis

Pytables utilities

Code to ease using pytables.

cookbook.pytables_utils.add_to_table(table)

@return: A decorator that adds arguments and the result of the function to the table.

cookbook.pytables_utils.cache_in_table(table, *arg_names)

@return: A decorator that caches the arguments and the result of the function in the table.

Red-black interval tree

An augmented red-black tree that handles intervals.

class cookbook.rb_interval_tree.rbintervalnode(key)

A node of a red black tree of half-open intervals.

max

The maximum value of any endpoint in the subtree rooted at this node.

subtree_interval()

@return: An interval that the subtree rooted here spans.

class cookbook.rb_interval_tree.rbintervaltree

A red black tree of half-open intervals. See Cormen, Leiserson, Rivest, Stein 2nd edition pg 311.

check_invariants()

Check the max invariant of the nodes and the red-black properties.

find_closest_intervals(interval, max_distance=None)

@return: The closest intervals in the interval tree to the given interval.

Search for a node whose interval overlaps i.

Red-black tree

A red-black tree implementation.

class cookbook.rbtree.rbnode(key)

A node in a red black tree. See Cormen, Leiserson, Rivest, Stein 2nd edition pg 273.

key

The node’s key

left

The node’s left child

p

The node’s parent

red

Is the node red?

right

The node’s right child

class cookbook.rbtree.rbtree(create_node=<class 'cookbook.rbtree.rbnode'>)

A red black tree. See Cormen, Leiserson, Rivest, Stein 2nd edition pg 273.

check_invariants()

@return: True iff satisfies all criteria to be red-black tree.

insert_key(key)

Insert the key into the tree.

insert_node(z)

Insert node z into the tree.

maximum(x=None)

@return: The maximum value in the subtree rooted at x.

minimum(x=None)

@return: The minimum value in the subtree rooted at x.

nil

The tree’s nil node

root

The tree’s root node

search(key, x=None)

Search the subtree rooted at x (or the root if not given) iteratively for the key.

@return: self.nil if it cannot find it.

cookbook.rbtree.test_tree(t, keys)

Insert keys one by one checking invariants and membership as we go.

cookbook.rbtree.write_tree_as_dot(t, f, show_nil=False)

Write the tree in the dot language format to f.

Reverse dict

From http://code.activestate.com/recipes/415903-two-dict-classes-which-can-lookup-keys-by-value-an/ Released under PSF license

class cookbook.reverse_dict.LookupDict(*args, **kw)

A dictionary which can lookup values by key, and keys by value. The lookup method returns a list of keys with matching values to the value argument.

class cookbook.reverse_dict.ReverseDict(*args, **kw)

A dictionary which can lookup values by key, and keys by value. All values and keys must be hashable, and unique.

Script utilities

Code to simplify logging and options in scripts.

cookbook.script_basics.boost_python_dlopen_flags(*args, **kwds)

A context manager that temporarily sets the dlopen flags for loading multiple boost.python modules and then returns them to previous values.

cookbook.script_basics.log_exception(exc_type, exc_obj, exc_tb)

Log an exception. Used as replacement for sys.excepthook.

cookbook.script_basics.log_options(option_parser, options)

Log the values of the options.

cookbook.script_basics.prepend_to_environment_variable(var_name, value)

Prepend a value to colon separated list in the named environment variable.

cookbook.script_basics.preserve_dlopenflags(*args, **kwds)

A context manager that temporarily sets the dlopen flags and then returns them to previous values.

cookbook.script_basics.setup_logging(filename=None, level=20, log_command_line=True)

Set up logging.

cookbook.script_basics.show_environment()

Print some details about the environment python is running in.

Logging utilities

Utility functions for simple logging operations.

cookbook.simple_logging.add_file_handler(filename, logger=None, handlerType=<class 'logging.FileHandler'>)

Adds a file handler to the logger if it does not already exist. If no logger is given, defaults to root logger. Returns the handler if one is created.

cookbook.simple_logging.add_logging_to_file(filename, mode='w')

Add logging to named file.

cookbook.simple_logging.basic_config(level=20)

Set up simple logging to stderr.

cookbook.simple_logging.create_log_exceptions_decorator(level=40, log_traceback=True, logger=None)

@return: Decorator to log exceptions raised inside a function.

cookbook.simple_logging.log_exception(level=40, logger=None)

Log the current exception.

cookbook.simple_logging.log_exception_traceback(level=40, logger=None)

Log the traceback for the current exception.

cookbook.simple_logging.log_to_file_and_stderr(filename, level=20, mode='w')

Set up simple logging to named file.

cookbook.simple_logging.use_handler(*args, **kwds)

A context manager that can be used to temporarily add a handler to a logger.

Typical usage:

from cookbook.simple_logging import use_handler from logging import getLogger, FileHandler, Formatter

with use_handler(getLogger(), FileHandler(‘output.log’)) as handler:
handler.setFormatter(Formatter(“%(asctime)s:%(levelname)s: %(message)s”)) <do some stuff>

Timer

Code to time things.

class cookbook.timer.SimpleTimer

Times things.

duration()

@return: The duration since start.

restart()

Restarts the timer.

class cookbook.timer.Timer(msg=None, level=20, logger=None)

Times things and implements the context management protocol (see PEP 343). A typical use case would be:

from cookbook.timer import Timer

with Timer(msg=’description of task’, level=logging.INFO, logger=getLogger(__name__)) as timer:
<do some stuff>

Venn

Creates Venn diagrams from up to 4 sets.

cookbook.venn.calc_areas(bounds)

Calculate the areas given by these bounds.

cookbook.venn.calc_error(areas, sizes)

Calculate the error associated with these areas.

cookbook.venn.calc_grid_areas(xs, ys)

Calculate the areas given by the grid bounds.

cookbook.venn.calc_separation(bounds)

@return: A separation value. The closer to 0 it is, the better the bounds will be separated.

cookbook.venn.calc_squareness(bounds)

@return: A square-ness value. The closer to 0 it is, the more square the bounds will be.

cookbook.venn.create_svg(bounds, sizes=None, labels=None, scale=120, font_size=50)

Create a SVG figure from the bounds.

cookbook.venn.find_bounds(sizes)

Use an optimizer to find the bounds.

cookbook.venn.fit_venn4(sets)

Fit a Venn diagram to the 4 sets.

cookbook.venn.initial_bounds()

Return a suitable set of initial bounds.

cookbook.venn.mid_points(xs)

Calculate mid_points

Workflow

Code to set up logging and options in a workflow

cookbook.workflow.caching_decorator(cache_name)

@return: A decorator that caches the results of the function.

cookbook.workflow.ensure_dir_exists(directory)

Makes a directory if it does not already exist.

cookbook.workflow.initialise_workflow(options)

Initialise the workflow:

  • initialise basic logging
  • over-ride options with any defined in options.output_dir/options.py
  • set up logging to file in options.output_dir
  • log option values
  • set up a cache directory to store cached output
cookbook.workflow.log_attributes(module)

Log the attributes in a module.

cookbook.workflow.logger_has_file_handler(logger)

@return: Does the logger have a file handler already?

cookbook.workflow.output_cached_method(name)

@return: Decorator that stores output of methods in cache directory.

cookbook.workflow.output_file(options, filename)

A path to the given filename in the output directory.