potpy.context – Context module

Module Contents

class potpy.context.Context[source]

A dict class that can call callables with arguments from itself.

Best explained with an example:

>>> def answer(question, foo):
...     return 'The answer to the %s question is: %d' % (question, foo)
...
>>> ctx = Context(foo=42, question='ultimate')
>>> ctx.inject(answer)
'The answer to the ultimate question is: 42'

Callable items are called before being passed to the callable:

>>> ctx = Context(foo=lambda bar: bar.upper(), bar='qux')
>>> ctx.inject(lambda foo: foo)
'QUX'

Note

Callable items are called during __getitem__():

>>> Context(foo=lambda: 42)['foo']
42

Contexts have 'context' as an implicit a member, so callables can refer to the context itself:

>>> ctx = Context(foo='foo')
>>> ctx.inject(lambda context: dict(context))
{'foo': 'foo'}

When injecting a call, you may override context items (or provide missing items) with keyword arguments:

>>> ctx.inject(lambda foo, bar: (foo, bar), bar='bar')
('foo', 'bar')

Note

*args- and **kwargs-style arguments cannot be injected at this time.

Note

Due to limitations of the inspect module, builtin and extension functions cannot be injected. You may work around this by wrapping the function in Python:

>>> ctx = Context(n='42')
>>> ctx.inject(lambda n: int(n))
42
inject(func, **kwargs)[source]

Inject arguments from context into a callable.

Parameters:
  • func – The callable to inject arguments into.
  • **kwargs – Specify values to override context items.

Project Versions

Table Of Contents

Previous topic

PotPy Documentation

Next topic

potpy.router – Router module

This Page