Package topo :: Package misc :: Module inlinec
[hide private]
[frames] | no frames]

Module inlinec

source code

Interface class for inline C/C++ functions. Based on the SciPy Weave package.

Weave (from SciPy) allows programmers to implement Python methods or functions using C code written as a string in the Python file. This is generally done to speed up code that would be slow if written directly in Python. Because not all users can be assumed to have a working C/C++ compiler, it is crucial for such optimizations to be optional. This file provides an interface for processing the inline C/C++ code in a way that can gracefully revert back to the unoptimized version when Weave is not available.

The fallback is implemented by making use of the way Python allows function names to be overwritten, as in these simple examples:

def x(y = 5): return y def x2(y = 6): return y*y print 'x:', x(), 'x2:', x2() # Result -- x: 5 x2: 36 x = x2 print 'x:', x(), 'x2:', x2() # Result -- x: 36 x2: 36

In this file, inline() is overwritten to call inline_weave() if Weave is available. If Weave is not available, inline() will raise a NotImplementedError exception. For a program to be usable without Weave, just test inlinec.optimized after defining each optimized component, replacing it with a non-optimized equivalent if inlinec.optimized is False.

For more information on weave, see: http://old.scipy.org/documentation/weave/weaveusersguide.html

$Id: inlinec.py 8248 2008-03-26 04:09:56Z ceball $

Functions [hide private]
 
inline_weave(*params, **nparams) source code
 
inline(*params, **nparams) source code
 
provide_unoptimized_equivalent(optimized_name, unoptimized_name, local_dict)
If not using optimization, replace the optimized component with its unoptimized equivalent.
source code
Variables [hide private]
  import_weave = True
  weave_imported = True
  inline_named_params = {'compiler': 'gcc', 'extra_compile_args'...
  optimized = True
  warn_for_each_unoptimized_component = False
Function Details [hide private]

provide_unoptimized_equivalent(optimized_name, unoptimized_name, local_dict)

source code 

If not using optimization, replace the optimized component with its unoptimized equivalent.

The objects named by optimized_name and unoptimized_name should be plug-compatible. The local_dict argument should be given the contents of locals(), so that this function can replace the optimized version with the unoptimized one in the namespace from which it has been called.

As an example, calling this function as:

provide_unoptimized_equivalent("sort_opt","sort",locals())

is equivalent to putting the following code directly into the calling location:

if not optimized:
  sort_opt = sort
  print 'module: Inline-optimized components not available; using sort instead of sort_opt.'

Variables Details [hide private]

inline_named_params

Value:
{'compiler': 'gcc',
 'extra_compile_args': ['-O2',
                        '-Wno-unused-variable -fomit-frame-pointer',
                        '-funroll-loops'],
 'extra_link_args': ['-lstdc++'],
 'verbose': 0}