Ignition v0.1.0 documentation

Ignition Utils

Code Tools

Tools for manipulating strings of code

ignition.utils.code_tools.comment_code(code_str, line_comment=None, block_comment=None)[source]

Adds comments to a snippet of code.

If line_comment is a given string then each line is prepended with the line_comment string.

If block_comment is a tuple of two strings (begin, end) the code str is surrounded by the strings.

Example:

>>> code = "This is \na mulitiline\ncomment"
>>> print comment_code(code, line_comment="//")
// This is
// a mulitiline
// comment
>>> print comment_code(code, block_comment=("/*","*/")
/*
This is
a mulitiline
comment
*/
ignition.utils.code_tools.indent_code(code_str, indent)[source]

Indent a snippet of code with indent number of spaces

Enum

Robust enumerated type support in Python.

This package provides a module for robust enumerations in Python.

An enumeration object is created with a sequence of string arguments to the Enum() constructor:

>>> from enum import Enum
>>> Colours = Enum('red', 'blue', 'green')
>>> Weekdays = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')

The return value is an immutable sequence object with a value for each of the string arguments. Each value is also available as an attribute named from the corresponding string argument:

>>> pizza_night = Weekdays[4]
>>> game_night = Weekdays['mon']
>>> shirt_colour = Colours.green

The values are constants that can be compared only with values from the same enumeration; comparison with other values will invoke Python’s fallback comparisons:

>>> pizza_night == Weekdays.fri
True
>>> shirt_colour > Colours.red
True
>>> shirt_colour == "green"
False

Each value from an enumeration exports its sequence index as an integer, and can be coerced to a simple string matching the original arguments used to create the enumeration:

>>> str(pizza_night)
'fri'
>>> shirt_colour.index
2

The object can also take a list or tuple argument for keys that map to the same value. The key method will only return the first value in the argument, so to test inclusion use the keys method, which always returns a list:

>>> multi = Enum(('foo', 'bar'), 'baz')
>>> multi.foo == multi.bar
True
>>> str(multi.foo)
'foo'
>>> 'foo' in multi.foo.keys
True
>>> 'baz' in multi.foo.keys
False
class ignition.utils.enum.Enum(*keys, **kwargs)[source]

Enumerated type.

exception ignition.utils.enum.EnumBadKeyError(key)[source]

Raised when creating an Enum with non-string keys.

exception ignition.utils.enum.EnumEmptyError[source]

Raised when attempting to create an empty enumeration.

exception ignition.utils.enum.EnumException(*args, **kwargs)[source]

Base class for all exceptions in this module.

exception ignition.utils.enum.EnumImmutableError(*args)[source]

Raised when attempting to modify an Enum.

class ignition.utils.enum.EnumValue(enumtype, index, key)[source]

A specific value of an enumerated type.

Iterators

Defines some custom iterators

class ignition.utils.iterators.UpdatingPermutationIterator(items, n=-1)[source]

A permutation iterator that can update to stop cycling on a particular position.

>>> iter = UpdatingPermuataionIterator(range(2))
>>> list(iter)
[[0, 1], [1, 0]]
>>> iter = UpdatingPermutationIterator(range(3))
>>> iter.next()
[0, 1, 2]
>>> iter.bad_pos(0)
>>> iter.next()
[1, 0, 2]
>>> iter.bad_pos(0)
>>> list(iter)
>>> [[2, 0, 1], [2, 1, 0]]
ignition.utils.iterators.flatten(alst)[source]

A recursive flattening algorithm for handling arbitrarily nested iterators

>>> flatten([0, [1,(2, 3), [4, [5, [6, 7]]]], 8])
[1, 2, 3, 4, 5, 6, 7, 8]
ignition.utils.iterators.flatten_list(alst)[source]

Similar to flatten except only flattens lists

>>> flatten_list([0, (2, 3), [4])
[0, (2, 3), 4]
ignition.utils.iterators.nested_list_idxs(alst)[source]

Returns tuple generator corresponding to all indexes in the nested list

>>> list(nested_list_iter([[1,2],[3]])
[(0,0), (0,1), (1,0)]

Iterative Method Tester

Implements testing routines for given iterative method solvers