Example #1
0
def blitz(expr,local_dict=None, global_dict=None,check_size=1,verbose=0,**kw):
    # this could call inline, but making a copy of the
    # code here is more efficient for several reasons.
    global function_catalog

    # this grabs the local variables from the *previous* call
    # frame -- that is the locals from the function that called
    # inline.
    call_frame = sys._getframe().f_back
    if local_dict is None:
        local_dict = call_frame.f_locals
    if global_dict is None:
        global_dict = call_frame.f_globals

    # 1. Check the sizes of the arrays and make sure they are compatible.
    #    This is expensive, so unsetting the check_size flag can save a lot
    #    of time.  It also can cause core-dumps if the sizes of the inputs
    #    aren't compatible.
    if check_size and not size_check.check_expr(expr,local_dict,global_dict):
        raise ValueError("inputs failed to pass size check.")

    # 2. try local cache
    try:
        results = function_cache[expr](*(local_dict,global_dict))
        return results
    except:
        pass
    try:
        results = attempt_function_call(expr,local_dict,global_dict)
    # 3. build the function
    except ValueError:
        # This section is pretty much the only difference
        # between blitz and inline
        ast = parser.suite(expr)
        ast_list = ast.tolist()
        expr_code = ast_to_blitz_expr(ast_list)
        arg_names = ast_tools.harvest_variables(ast_list)
        module_dir = global_dict.get('__file__',None)
        func = inline_tools.compile_function(expr_code,arg_names,local_dict,
                                             global_dict,module_dir,
                                             compiler='gcc',auto_downcast=1,
                                             verbose=verbose,
                                             type_converters=converters.blitz,
                                             **kw)
        function_catalog.add_function(expr,func,module_dir)
        try:
            results = attempt_function_call(expr,local_dict,global_dict)
        except ValueError:
            warnings.warn('compilation failed. Executing as python code',
                          BlitzWarning)
            exec(expr, global_dict, local_dict)
Example #2
0
 def test_calculated_index2(self):
     a = np.arange(10)
     nx = 0
     expr = 'a[1:5] + a[nx+1:5+nx]'
     size_check.check_expr(expr, locals())
Example #3
0
 def test_calculated_index2(self):
     a = np.arange(10)
     nx = 0
     expr = 'a[1:5] + a[nx+1:5+nx]'
     size_check.check_expr(expr,locals())
Example #4
0
 def check_calculated_index(self):
     a = arange(10)
     nx = 0
     expr = 'a[5] + a[nx+3]'
     size_check.check_expr(expr,locals())