コード例 #1
0
ファイル: __init__.py プロジェクト: atlang/ace
def from_source(source, defaults=(), globals={}, constants={}, 
                size_calculator=None):
    """Creates a :class:`GenericFn` from the provided source, with the provided
    defaults (inline defaults cannot be resolved), globals, constants and
    size calculator.
    
    Inline defaults are not evaluated and should not be used.
    """
    ast = astx.infer_ast(source)
    ast = astx.extract_the(ast, _ast.FunctionDef)
    return GenericFn(ast, defaults, globals, constants, size_calculator)
コード例 #2
0
ファイル: __init__.py プロジェクト: nrfulton/ace
def fn(decl):
    """Create a :class:`generic cl.oquence function <GenericFn>` from a 
    Python function declaration.
    
    Typical usage is as a decorator::
    
        @clq.fn
        def sum(a, b, dest):
            gid = get_global_id(0)
            dest[gid] = a[gid] + b[gid]
            
    As with any decorator, the above is Python syntactic sugar for::
    
        def sum(a, b, dest):
            gid = get_global_id(0)
            dest[gid] = a[gid] + b[gid]
        sum = clq.fn(sum)
        
    .. WARNING:: Functions defined on the ``python`` or ``ipython`` command 
                 line do not retain their source, so this won't work. A bug 
                 has been filed, and has been resolved in version 0.11 of 
                 ipython.
                 
                   http://github.com/ipython/ipython/issues/issue/120
                   
                 A workaround for earlier versions of ipython is to use the 
                 fn.from_source form described below.

    To create a generic function from a string, use the ``fn.from_source``
    function::
    
        clq.fn.from_source('''
        def sum(a, b, dest):
            gid = get_global_id(0)
            dest[gid] = a[gid] + b[gid]
        ''')
        
    To create a generic function from an abstract syntax tree, use the 
    ``fn.from_ast`` function::
    
        clq.fn.from_ast(ast.parse('''
        def sum(a, b, dest):
            gid = get_global_id(0)
            dest[gid] = a[gid] + b[gid]
        '''))

    See the :mod:`ast` module in the Python standard library for more 
    information on manipulating Python syntax trees. The :mod:`cypy.astx`
    module provides several convenience functions for working with Python 
    ASTs as well.
    """
    ast = astx.infer_ast(decl)
    ast = astx.extract_the(ast, _ast.FunctionDef)
    return GenericFn(ast)
コード例 #3
0
ファイル: __init__.py プロジェクト: atlang/ace
def from_source(source,
                defaults=(),
                globals={},
                constants={},
                size_calculator=None):
    """Creates a :class:`GenericFn` from the provided source, with the provided
    defaults (inline defaults cannot be resolved), globals, constants and
    size calculator.
    
    Inline defaults are not evaluated and should not be used.
    """
    ast = astx.infer_ast(source)
    ast = astx.extract_the(ast, _ast.FunctionDef)
    return GenericFn(ast, defaults, globals, constants, size_calculator)
コード例 #4
0
ファイル: __init__.py プロジェクト: atlang/ace
def fn(__src=None, **constants):
    """Create a generic cl.oquence function based on the provided source and 
    constant bindings. 
    
    - If provided a Python function (e.g. if used as a decorator):
    
      - The abstract syntax tree will be read via :func:`cypy.astx.infer_ast`.
      
      - The function's globals and defaults will be used.
      
    - If ``__src`` is a :class:`GenericFn` already, a new generic function with 
      additional constants will be created. It will have the same abstract 
      syntax tree, defaults and globals.

    Constants can be passed in as keyword arguments.
    
    .. WARNING:: Functions defined on the Python/iPython command line do not 
                 retain their source, so this won't work. A bug has been filed: 
                 
                 http://github.com/ipython/ipython/issues/issue/120
                 
    """
    # __src in case a variable named 'src' is defined.
    
    if __src is None:
        # support using as a decorator with constants by deferring 
        # initialization until the source is also specified, presumably
        # directly below
        return lambda __src: fn(__src, **constants)
    elif isinstance(__src, GenericFn):
        return GenericFn(__src.ast, __src.defaults, __src.globals,
                         cypy.merge_dicts(__src.constants, constants),
                         __src.size_calculator)
    else:
        ast = astx.infer_ast(__src)
        ast = astx.extract_the(ast, _ast.FunctionDef)
        globals = __src.func_globals
        defaults = __src.func_defaults
        if defaults is None: 
            # In Python, func_defaults is None if there are no defaults but 
            # I don't want to have to special case that everywhere
            defaults = ()
        return GenericFn(ast, defaults, globals, constants)
コード例 #5
0
ファイル: __init__.py プロジェクト: atlang/ace
def fn(__src=None, **constants):
    """Create a generic cl.oquence function based on the provided source and 
    constant bindings. 
    
    - If provided a Python function (e.g. if used as a decorator):
    
      - The abstract syntax tree will be read via :func:`cypy.astx.infer_ast`.
      
      - The function's globals and defaults will be used.
      
    - If ``__src`` is a :class:`GenericFn` already, a new generic function with 
      additional constants will be created. It will have the same abstract 
      syntax tree, defaults and globals.

    Constants can be passed in as keyword arguments.
    
    .. WARNING:: Functions defined on the Python/iPython command line do not 
                 retain their source, so this won't work. A bug has been filed: 
                 
                 http://github.com/ipython/ipython/issues/issue/120
                 
    """
    # __src in case a variable named 'src' is defined.

    if __src is None:
        # support using as a decorator with constants by deferring
        # initialization until the source is also specified, presumably
        # directly below
        return lambda __src: fn(__src, **constants)
    elif isinstance(__src, GenericFn):
        return GenericFn(__src.ast, __src.defaults, __src.globals,
                         cypy.merge_dicts(__src.constants, constants),
                         __src.size_calculator)
    else:
        ast = astx.infer_ast(__src)
        ast = astx.extract_the(ast, _ast.FunctionDef)
        globals = __src.func_globals
        defaults = __src.func_defaults
        if defaults is None:
            # In Python, func_defaults is None if there are no defaults but
            # I don't want to have to special case that everywhere
            defaults = ()
        return GenericFn(ast, defaults, globals, constants)
コード例 #6
0
ファイル: __init__.py プロジェクト: nrfulton/ace
def from_source(src):
    ast = astx.infer_ast(src)
    ast = astx.extract_the(ast, _ast.FunctionDef)
    return GenericFn(ast)