pacman.utils package

Submodules

pacman.utils.graphs module

pacman.utils.simple_repr module

Simple Representation module.

This module provide utility methods and mixin to convert python objects to and from a so called ‘simple representation’.

A simple representation is composed only of simple python objects: * booleans * string * numbers * lists of simple python objects * dicts of simple python objects * namedtuple

When using namedtuple, they must obey the two following rules * be defined at module level (not in class /method) * the name of the class variable must match the name of the class. for

example:

Named = namedtuple(‘Named’, [‘foo’, ‘bar’])

class pacman.utils.simple_repr.SimpleRepr[source]

Bases: object

Mixin to transform python objects into a representation composed only of simple python types. The idea is that the simple repr can be directly converted into json or yaml. The simple representation can be obtained by calling simple_repr(o).

The class using this mixin must satisfy the following constraints: * All constructor’s parameters must be bool, string, number, objects

providing a _simple_repr() method (generally by using the the SimpleRep mixin) or list or dict of objects of these types.

  • the constructor parameter must map to an attribute with the same name preceded by ‘_’. If it not the case, the class may declare a _repr_mapping attribute which maps the argument name(s) with the attribute(s) names.

exception pacman.utils.simple_repr.SimpleReprException[source]

Bases: Exception

pacman.utils.simple_repr.from_repr(r)[source]

Build an instance from a simple representation.

Parameters

r

Returns

pacman.utils.simple_repr.simple_repr(o)[source]

Build a simple representation for object o.

o eithor already be a simple type (boolean, number, string of list/dict of these types) or implement _simple_repr().

Parameters

o – an object

Returns

a simple representation for this object

pacman.utils.expressionfunction module

class pacman.utils.expressionfunction.ExpressionFunction(expression, source_file=None, **fixed_vars)[source]

Bases: Callable, SimpleRepr

Callable object representing a function from a python string. expression.

Example: f = ExpressionFunction(‘a + b’) f.variable_names -> [‘a’, ‘b’] f(a=1, b=3) -> 4 f.expression -> ‘a + b’

Note: this callable only works with keyword arguments.

__init__(expression, source_file=None, **fixed_vars)[source]

Create a callable representing the expression.

Parameters

expression (str) – a valid python expression (any builtin python

function can be used, e.g. abs, round, etc.). for example “abs(a1 - b)”

Parameters

fixed_vars – extra keyword parameters will be interpreted as

fixed parameter for the expression and the produced callable will represent a partial evaluation if the expression with these parameter already fixed. If the name of these keyword parameter do not match any of the variables found in the expression, a ValueError is raised.

property expression
partial(**kwargs)[source]
property variable_names: List[str]

return: a set of variable names that must be set when calling f

Return type

List[str]

class pacman.utils.expressionfunction.VarCounterVisitor[source]

Bases: NodeVisitor

A simple visitor to count variables in an AST tree.

__init__()[source]
get_vars()[source]
visit(node)[source]

Visit a node.

Return type

Any

pacman.utils.various module

pacman.utils.various.func_args(f)[source]

Get the list of arguments for a function. This also works for function produced with functools.partial, as long as you only use keywords arguments.

Parameters

f – a function

Returns

the list of argument’s name for the function f

Module contents