def compile_stuff(i): c_code = """ double sum(int a, int b) { return a + b; } // Code number %d """ % (i % 2) func = inline(c_code, cache_dir="test_locking_cache") s = func(i, 3) assert s == (i+3) print("In run %d, result is %d as expected." % (i, s))
def gmsh_cpp_inline(code, extra_include_dirs=None, extra_library_dirs=None, extra_libraries=None, extra_system_headers=None): p = { 'system_headers': ['Gmsh.h'] + (extra_system_headers or []), 'include_dirs': GMSH_INCLUDE_DIRS + (extra_include_dirs or []), 'library_dirs': GMSH_LIBRARY_DIRS + (extra_library_dirs or []), 'libraries': ['Gmsh'] + (extra_libraries or []) } return inline(code, **p)
#!/usr/local/bin/python import logging import pocketsphinx as ps import sphinxbase as sb from os import path ## redirect SWIG library from instant import inline from os import fdopen, dup import sys stdout = fdopen(dup(sys.stdout.fileno()), 'w') stderr = fdopen(dup(sys.stderr.fileno()), 'w') redirect = inline(""" void redirect(void) { freopen("my_stdout.txt", "w", stdout); freopen("my_stderr.txt", "w", stderr); } """) redirect() POCKETSPHINX_SHARE_DIR = '/usr/local/share/pocketsphinx/' MODELDIR = POCKETSPHINX_SHARE_DIR+'model' DATADIR = POCKETSPHINX_SHARE_DIR+'test/data' # function_map = {0:sb.Config.set_string, # 1:sb.Config.set_float # } # config.set_string('-hmm', path.join(MODELDIR, 'hmm/en_US/hub4wsj_sc_8k')) # config.set_string('-dict', path.join(MODELDIR, 'lm/en_US/cmu07a.dic')) default_config = [ {'name': '-hmm', 'func': sb.Config.set_string, 'value': path.join(MODELDIR, 'hmm/en_US/hub4wsj_sc_8k')},
def test_system_call_subprocess(): os.environ["INSTANT_SYSTEM_CALL_METHOD"] = "SUBPROCESS" add_func = inline("double add(double a, double b){ return a+b; }", cache_dir="test_cache") print("The sum of 3 and 4.5 is ", add_func(3, 4.5))
def test_system_call_os_system(): os.environ["INSTANT_SYSTEM_CALL_METHOD"] = "OS_SYSTEM" add_func = inline("double add(double c, double d){ return c+d; }", cache_dir="test_cache") print("The sum of 3 and 4.5 is ", add_func(3, 4.5))
def test_inline(): add_func = inline("double add(double a, double b){ return a+b; }", cache_dir="test_cache") assert add_func(3, 4.5) == 7.5
from instant import inline source = """ double find_sum(double r1, double r2) { return sum(r1 + r2); } """ find_sum = inline(source) x = 1.0 y = 2.5 print("sin({0}+{1}) = {2}".format(x, y, find_sum(x, y)))
from instant import inline c_code = r""" double add( double a, double b ) { return a + b; } """ add_func = inline( c_code ) print add_func( 15, -3.4 )
from instant import inline source = """ double hw1(double r1, double r2) { return sin(r1 + r2); } """ hw1 = inline(source) x = 1.0 y = 2.5 print("sin({0}+{1}) = {2}".format(x, y, hw1(x, y)))
cpp_code = """ void dabla(dolfin::Vector& a, dolfin::Vector& b, double c, double d) { for (unsigned int i=0; i < a.size(); i++) { b.setitem(i, d*a[i] + c); } } """ include_dirs, flags, libs, libdirs = instant.header_and_libs_from_pkgconfig( "dolfin") headers = ["dolfin.h"] func = instant.inline(cpp_code, system_headers=headers, include_dirs=include_dirs, libraries=libs, library_dirs=libdirs) #func = instant.inline(cpp_code, system_headers=headers) if __name__ == '__main__': nx = ny = 1 mesh = df.UnitSquareMesh(nx, ny) V = df.FunctionSpace(mesh, 'CG', 1) Vv = df.VectorFunctionSpace(mesh, 'CG', 1, dim=3) f = df.interpolate(df.Expression("0"), V) f1 = df.interpolate(df.Expression(("1", "0", "0")), Vv) f2 = df.interpolate(df.Expression(("0", "1", "0")), Vv) print 'a=', f1.vector().array() print 'b=', f2.vector().array()
from instant import inline source = """ double hw1(double r1, double r2) { return sin(r1 + r2); } """ hw1 = inline(source) x= 1.0 y =2.5 print "sin({0}+{1}) = {2}".format(x,y,hw1(x,y))