mpacts.core.units. context

Functions and classes related to context definitions and application.

copyright:2016 by Pint Authors, see AUTHORS for more details..
license:BSD, see LICENSE for more details.

In order to be able to use this module import it like this:

import mpacts.core.units.context
#or assign it to a shorter name
import mpacts.core.units.context as con


class mpacts.core.units.context.Context(name, aliases=(), defaults=None)

Bases: object

A specialized container that defines transformation functions from one dimension to another. Each Dimension are specified using a UnitsContainer. Simple transformation are given with a function taking a single parameter.

>>> timedim = UnitsContainer({'[time]': 1})
>>> spacedim = UnitsContainer({'[length]': 1})
>>> def f(time):
...     'Time to length converter'
...     return 3. * time
>>> c = Context()
>>> c.add_transformation(timedim, spacedim, f)
>>> c.transform(timedim, spacedim, 2)

Conversion functions may take optional keyword arguments and the context can have default values for these arguments.

>>> def f(time, n):
...     'Time to length converter, n is the index of refraction of the material'
...     return 3. * time / n
>>> c = Context(n=3)
>>> c.add_transformation(timedim, spacedim, f)
>>> c.transform(timedim, spacedim, 2)
Context(name, parent, **kwargs)
add_transformation(src, dst, func)

Add a transformation function to the context.

defaults = None

Maps defaults variable names to values

classmethod from_context(context, **defaults)

Creates a new context that shares the funcs dictionary with the original context. The default values are copied from the original context and updated with the new defaults.

If defaults is empty, return the same context.

classmethod from_lines(lines, to_base_func=None)
funcs = None

Maps (src, dst) -> transformation function

relation_to_context = None

Maps (src, dst) -> self Used as a convenience dictionary to be composed by ContextChain

remove_transformation(src, dst)

Add a transformation function to the context.

transform(src, dst, registry, value)

Transform a value.