# file: runme.py import example a = 37 b = 42 # Now call our C function with a bunch of callbacks print "Trying some C callback functions" print " a =", a print " b =", b print " ADD(a,b) =", example.do_op(a,b,example.ADD) print " SUB(a,b) =", example.do_op(a,b,example.SUB) print " MUL(a,b) =", example.do_op(a,b,example.MUL) print "Here is what the C callback function objects look like in Python" print " ADD =", example.ADD print " SUB =", example.SUB print " MUL =", example.MUL print "Call the functions directly..." print " add(a,b) =", example.add(a,b) print " sub(a,b) =", example.sub(a,b)
b = example.new_intp() c = example.new_intp() example.intp_assign(a, 37) example.intp_assign(b, 42) print(" a =", a) print(" b =", b) print(" c =", c) # 在指针对象上调用add函数 example.add(a, b, c) # 得到结果 r = example.intp_value(c) print(" 37 + 42 =", r) # 清除指针 example.delete_intp(a) example.delete_intp(b) example.delete_intp(c) # 现在尝试一下类型映射库(typemaps) # 这样的方式会简单许多,因为不再需要操纵指针。 print("Trying the typemap library") r = example.sub(37, 42) print(" 37 - 42 =", r) # 现在尝试一下具有多个返回值的函数 print("Testing multiple return values") q, r = example.divide(42, 37) print(" 42/37 = %d remainder %d" % (q, r))
print " a =", a print " b =", b print " c =", c # Call the add() function with some pointers example.add(a, b, c) # Now get the result r = example.intp_value(c) print " 37 + 42 =", r # Clean up the pointers example.delete_intp(a) example.delete_intp(b) example.delete_intp(c) # Now try the typemap library # This should be much easier. Now how it is no longer # necessary to manufacture pointers. print "Trying the typemap library" r = example.sub(37, 42) print " 37 - 42 =", r # Now try the version with multiple return values print "Testing multiple return values" q, r = example.divide(42, 37) print " 42/37 = %d remainder %d" % (q, r)
# file: runme.py import example a = 37 b = 42 # Now call our C function with a bunch of callbacks print("Trying some C callback functions") print(" a = %s" % a) print(" b = %s" % b) print(" ADD(a,b) = %s" % example.do_op(a, b, example.ADD)) print(" SUB(a,b) = %s" % example.do_op(a, b, example.SUB)) print(" MUL(a,b) = %s" % example.do_op(a, b, example.MUL)) print("Here is what the C callback function objects look like in Python") print(" ADD = %s" % example.ADD) print(" SUB = %s" % example.SUB) print(" MUL = %s" % example.MUL) print("Call the functions directly...") print(" add(a,b) = %s" % example.add(a, b)) print(" sub(a,b) = %s" % example.sub(a, b))
import example from pprint import pprint import inspect pprint(inspect.getmembers(example)) print(help(example)) pprint(example.sub(5, 2))
import example from example import add as a from example import sub as b print(example.add(4, 5)) print(example.sub(4, 5)) print(example.mul(4, 5)) print(a(5, 4)) print(b(5, 4))
import sys sys.path.insert(1, './lib') import example print(example.add()) print(example.sub())
import example result1 = example.add(1, 3) #测试 typemaps 中的INPUT, OUTPUT result2 = example.sub(3, 2) print(result1) print(result2)
import sys sys.path.insert(1, './lib') import example nums = [1, 2, 3, 4] print( "--------------------------------------------------------------------------------" ) print("add tst") print( "--------------------------------------------------------------------------------" ) for x in nums: for y in nums: print(example.add(x, y)) print( "--------------------------------------------------------------------------------" ) print("sub tst") print( "--------------------------------------------------------------------------------" ) for x in nums: for y in nums: print(example.sub(x, y))
import sys sys.path.insert(1, './lib') import example nums = [1, 2, 3, 4] print( "--------------------------------------------------------------------------------" ) print("add tst") print( "--------------------------------------------------------------------------------" ) for x in nums: for y in nums: print(example.add(j=x, i=y)) print( "--------------------------------------------------------------------------------" ) print("sub tst") print( "--------------------------------------------------------------------------------" ) for x in nums: for y in nums: print(example.sub(j=x, i=y))
# file: runme.py import example a = 37 b = 42 # Now call our C function with a bunch of callbacks print("Trying some C callback functions") print(" a =", a) print(" b =", b) print(" ADD(a,b) =", example.do_op(a, b, example.ADD)) print(" SUB(a,b) =", example.do_op(a, b, example.SUB)) print(" MUL(a,b) =", example.do_op(a, b, example.MUL)) print("Here is what the C callback function objects look like in Python") print(" ADD =", example.ADD) print(" SUB =", example.SUB) print(" MUL =", example.MUL) print("Call the functions directly...") print(" add(a,b) =", example.add(a, b)) print(" sub(a,b) =", example.sub(a, b))
def test_sub(): a = np.random.rand() b = np.random.rand() np.testing.assert_allclose(example.sub(a, b), a - b)
print("Numbers::Three = " + str(test_enum)) print("compare with example::None : " + str(example.Num_None == test_enum)) x = demo1.num() y = demo2.num() print(str(x) + " + " + str(y) + " = " + str(example.add(x, y))) #print(str(x) + " + " + str(y) + " = " + str(example.add())) 导出的函数参数需要全部满足 #print(str(x) + " + " + str(y) + " = " + str(example.add1())) add1里面定义了arg1,arg2但是没有默认参数 print( str(x) + " + " + str(y) + " = " + str(example.add1(x=1, y=2))) #print(str(x) + " + " + str(y) + " = " + str(example.add(x = 1, y = 2))) add1定义了arg1,arg2,这里没有,所以不能这么用 print(str(x) + " + " + str(y) + " = " + str(example.add2())) #print(str(x) + " + " + str(y) + " = " + str(example.add3())) print(str(x) + " - " + str(y) + " = " + str(example.sub(x, y))) print( str(x) + " * " + str(y) + " = " + str(example.mul(example.B(), x, y))) print(str(x) + " / " + str(y) + " = " + str(example.div(x, y))) #print(str(x) + " / " + str(y) + " = " + str(example.div2(x, y))) pydemo = example.PyDemo() pydemo.name = "pydemo" pydemo.set_num(4) print(str(pydemo.name)) pydemo.print() except Exception as identifier: print("except : " + str(identifier))
import example print(example.add(1, 1)) print(example.sub(2, 1))
# file: runme.py import example a = 37 b = 42 # Now call our C function with a bunch of callbacks print "Trying some C callback functions" print " a =", a print " b =", b print " ADD(a,b) =", example.do_op(a, b, example.ADD) print " SUB(a,b) =", example.do_op(a, b, example.SUB) print " MUL(a,b) =", example.do_op(a, b, example.MUL) print "Here is what the C callback function objects look like in Python" print " ADD =", example.ADD print " SUB =", example.SUB print " MUL =", example.MUL print "Call the functions directly..." print " add(a,b) =", example.add(a, b) print " sub(a,b) =", example.sub(a, b)