Ejemplo n.º 1
0
def require(*objects, **mapping):
    """Simple decorator for requiring local objects and modules to be available
    when the decorated function is called on the engine.

    Modules specified by name or passed directly will be imported
    prior to calling the decorated function.

    Objects other than modules will be pushed as a part of the task.
    Functions can be passed positionally,
    and will be pushed to the engine with their __name__.
    Other objects can be passed by keyword arg.

    Examples
    --------

    ::

        In [1]: @ipp.require('numpy')
           ...: def norm(a):
           ...:     return numpy.linalg.norm(a,2)

    ::

        In [2]: foo = lambda x: x*x
        In [3]: @ipp.require(foo)
           ...: def bar(a):
           ...:     return foo(1-a)
    """
    names = []
    for obj in objects:
        if isinstance(obj, ModuleType):
            obj = obj.__name__

        if isinstance(obj, string_types):
            names.append(obj)
        elif hasattr(obj, '__name__'):
            mapping[obj.__name__] = obj
        else:
            raise TypeError(
                "Objects other than modules and functions "
                "must be passed by kwarg, but got: %s" % type(obj)
            )

    for name, obj in mapping.items():
        mapping[name] = can(obj)
    return depend(_require, *names, **mapping)
Ejemplo n.º 2
0
def require(*objects, **mapping):
    """Simple decorator for requiring local objects and modules to be available
    when the decorated function is called on the engine.
    
    Modules specified by name or passed directly will be imported
    prior to calling the decorated function.
    
    Objects other than modules will be pushed as a part of the task.
    Functions can be passed positionally,
    and will be pushed to the engine with their __name__.
    Other objects can be passed by keyword arg.
    
    Examples
    --------
    
    ::
    
        In [1]: @ipp.require('numpy')
           ...: def norm(a):
           ...:     return numpy.linalg.norm(a,2)
    
    ::
    
        In [2]: foo = lambda x: x*x
        In [3]: @ipp.require(foo)
           ...: def bar(a):
           ...:     return foo(1-a)
    """
    names = []
    for obj in objects:
        if isinstance(obj, ModuleType):
            obj = obj.__name__
        
        if isinstance(obj, string_types):
            names.append(obj)
        elif hasattr(obj, '__name__'):
            mapping[obj.__name__] = obj
        else:
            raise TypeError("Objects other than modules and functions "
                "must be passed by kwarg, but got: %s" % type(obj)
            )
    
    for name, obj in mapping.items():
        mapping[name] = can(obj)
    return depend(_require, *names, **mapping)
Ejemplo n.º 3
0
 def cancan(self, f):
     """decorator to pass through canning into self.user_ns"""
     return uncan(can(f), self.user_ns)