def just_in_time_method(func):
if not inspect.ismethod:<EOL><INDENT>raise MetaError("<STR_LIT>")<EOL><DEDENT>def wrapper(self, *args, **kwargs):<EOL><INDENT>if self.item is None:<EOL><INDENT>self.item = self.factory[self.key]<EOL><DEDENT>return getattr(self.item, func.__name__)(*args, **kwargs)<EOL><DEDENT>return wrapper<EOL>
This is a dcorator for methods. It redirect calls to the decorated method
to the equivalent method in a class member called 'item'. 'item' is expected
to be None when the class is instantiated. The point is to easily allow
on-demand construction or loading of large, expensive objects just-in-time.

To apply this decorator to a method in a class, the class must have the
following instance variables:

+--------------------------------------------------------------------------+
| Name    |  Description                                                   |
+=========+================================================================+
| item    | The wrapped object. Calls to this method will be redirected    |
|         | to the method with the same name in item. This should be set   |
|         | to None when the object is created; it will be loaded          |
|         | on-demand the first time this method is called.                |
+---------+----------------------------------------------------------------+
| factory | An object from which 'item' can be loaded. Can be a factory,   |
|         | or similar, but must provide the subscript operator, as this   |
|         | is used to pass a key that uniquely identifies 'item'          |
+---------+----------------------------------------------------------------+
| key     | any object that uniquely identifies 'item'; must be what is    |
|         | expected by 'index' as argument for the subscript operator.    |
+--------------------------------------------------------------------------+

A common pattern is to create a new class B which inherits from A, implement
the above requirements in B and then apply this decorator to all the methods
inherited from A. If 'item' is an object of type A, then this pattern makes
B behave exactly like A, but with just-in-time construction.