Exemple #1
0
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)
Exemple #2
0
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)
Exemple #3
0
comparison_operators = {
    _ast.Eq: '==',
    _ast.NotEq: '!=',
    _ast.Lt: '<',
    _ast.LtE: '<=',
    _ast.Gt: '>',
    _ast.GtE: '>=',
    _ast.Is: 'is',
    _ast.IsNot: 'is not',
    _ast.In: 'in',
    _ast.NotIn: 'not in',
}

all_operators = cypy.merge_dicts(unary_operators, 
                                binary_operators,
                                boolean_operators, 
                                comparison_operators)

C_unary_operators = dict(unary_operators)
C_unary_operators[_ast.Not] = "!"

C_binary_operators = dict(binary_operators)
del C_binary_operators[_ast.Pow]
del C_binary_operators[_ast.FloorDiv]

C_boolean_operators = {
    _ast.And: "&&",
    _ast.Or: "||"
}

C_comparison_operators = {
Exemple #4
0
}

comparison_operators = {
    _ast.Eq: '==',
    _ast.NotEq: '!=',
    _ast.Lt: '<',
    _ast.LtE: '<=',
    _ast.Gt: '>',
    _ast.GtE: '>=',
    _ast.Is: 'is',
    _ast.IsNot: 'is not',
    _ast.In: 'in',
    _ast.NotIn: 'not in',
}

all_operators = cypy.merge_dicts(unary_operators, binary_operators,
                                 boolean_operators, comparison_operators)

C_unary_operators = dict(unary_operators)
C_unary_operators[_ast.Not] = "!"

C_binary_operators = dict(binary_operators)
del C_binary_operators[_ast.Pow]
del C_binary_operators[_ast.FloorDiv]

C_boolean_operators = {_ast.And: "&&", _ast.Or: "||"}

C_comparison_operators = {
    _ast.Eq: "==",
    _ast.NotEq: "!=",
    _ast.Lt: "<",
    _ast.LtE: "<=",