def get_pybind11_flags(build_ext, includes, abi_compile_flags): pybind11_dir = os.environ.get('PYBIND11_HOME') if pybind11_dir: pybind11_dir += '/include' else: try: import pybind11 pybind11_dir = [pybind11.get_include()] except Exception: last_err = 'Unable found PYBIND11_HOME,please pip3 install pybind11 or set PYBIND11_HOME' raise DistutilsPlatformError(last_err) last_err = None cpp_flags = ['-I' + i for i in includes] + abi_compile_flags try: lib_file = test_compile(build_ext, 'example', extra_preargs=cpp_flags, include_dirs=pybind11_dir, code=textwrap.dedent('''\ #include <pybind11/pybind11.h> namespace py = pybind11; int add(int i, int j) { return i + j; } PYBIND11_MODULE(example, m) { m.doc() = "pybind11 example plugin"; // optional module docstring m.def("add", &add, "A function which adds two numbers", py::arg("i"), py::arg("j")); } ''')) lib_dir = os.path.dirname(lib_file) if lib_dir not in sys.path: sys.path.append(lib_dir) import example example.add(1, 2) sys.path.pop() return pybind11_dir except (CompileError, LinkError): last_err = 'Unable to determine -I include flags to use with pybind11 (see error above).' except Exception: last_err = 'Unable to determine -I include flags to use with pybind11. ' \ 'Last error:\n\n%s' % traceback.format_exc() raise DistutilsPlatformError(last_err)
def test(self): sys.path.append(".") import example self.output.info("Add %s" % example.add(2, 3)) assert example.add(2, 40) == 42
def test_addition(): """Test addition.""" assert example.add(2, 2) == 4
import example # First create some objects using the pointer library. print "Testing the pointer library" a = example.new_intp() 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 # 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"
def test_cpp_module(): import example assert example.add(1, 2) == 3 assert example.add(8, 2) == 10
#导入模块和命名 import example as e; import imp; import sys; #调用模块中的函数名 print(e.add(5,5)); #输出路径 print(dir(e)); #重新加载路径 imp.reload(e); #获取系统的路径 print(sys.path); #打印出当前模块中的所有模块函数 print(dir())
import example # 首先,使用指针库创建一些对象 print("Testing the pointer library") a = example.new_intp() 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)
def test(self): sys.path.append("./bin") import example print("Add %s" % example.add(2, 3)) assert example.add(2, 40) == 42
def test_freestanding_add(): assert add(5, 6) == 11
import example sum = example.add(4, 5.5) print('Direct import sum is : %d', sum) from example import add sum = add(4, 5.5) print('From example import sum is : %d', sum) import math print("The value of pi is", math.pi) # import module by renaming it import math as m print("The value of pi is", m.pi) # import only pi from math module from math import pi print("The value of pi is", pi) # Output: 3.141592653589793 print(math.pi) # Output: -1.0 print(math.cos(math.pi)) # Output: 22026.465794806718 print(math.exp(10)) # Output: 3.0 print(math.log10(1000))
# 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))
import example print example.add(i=3)
def test_add_2_and_2(self): self.assertEqual( 4, example.add(2, 2) )
def test_add_2_and_minus_2(self): self.assertEqual( 0, example.add(2, -2) )
import os import sys sys.path.append(os.getenv('EXAMPLE_MODULE_PATH')) import example v = example.add() assert v == 3 print(v) v = example.add(1, 2) assert v == 3 print(v) v = example.add(i=2, j=3) assert v == 5 print(v)
import example print(example.add(2, 3))
import example print example.add(1, 2) p = example.Pet('Molly') print p print p.getName() p.setName('Charly') print p.getName()
from example import add assert add(1, 2) == 3 assert add(1, 3.0) == 4 assert add("a", "b") == "Bad inputs"
else: return n * factorial(n - 1) print(factorial(4)) #anonymous/lambda fnx double = lambda x: x * 2 print(double(5)) my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(filter(lambda x: (x % 2 == 0), my_list)) print(new_list) #module mod = example.add(4, 5.5) print(mod) print(dir(example)) print(dir()) def outer(): first_num = 1 def inner(): first_num = 0 second_num = 1 print('inner - second_num is: ', second_num) inner() print('outer - first_num is: ', first_num)
def test_add_100_and_1(self): self.assertEqual( 101, example.add(100, 1) )
# Python模块 # 在一个 example.py 里面写下如下代码: def add(a, b): """这个程序增加了两个 然后返回结果""" result = a + b return result # 在main.py 里面导入这个模块 import example print(example.add(4, 2))
import sys sys.path.insert(1, './lib') import example print(example.add(1, 2)) print(example.add(1.1, 2.2))
import example print(example.add(4, 5.5)) with open("text.txt", 'w', encoding = 'utf-8') as f: f.write("my first file\n") f.write("This file \n\n") f.write("contains three lines\n")
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 example result1 = example.add(1, 3) #测试 typemaps 中的INPUT, OUTPUT result2 = example.sub(3, 2) print(result1) print(result2)
def subsetsums(slices, pizza_types, pizza_slices): for target in range(slices, 0, -1): res = add(pizza_slices, pizza_types, target) if res: return len(res), res return 0, []
import example hello = example.add(5, 6) print hello
def test_add(): assert example.add(1, 1) == 2 assert not example.add(0, 1) == 2
#!/usr/bin/env python3 """Minimal example of a C++ to Python Binding using pybind11 Original code available at https://pybind11.readthedocs.io/en/latest/basics.html#compiling-the-test-cases """ import example a, b = 1, 2 print(a, "+", b, "=", example.add(i=a, j=b)) # >>>help(example) print("THE answer is", example.the_answer)
# 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 # Test print example.add(1, 2) # Test with arguments print example.add(i=1, j=2) # Test literal print example.add2(1, 2) # will print doc info # help(example) # Test literal print example.add() print example.the_answer print example.what
import example a = 1 b = 2 print("Add: {} + {} = {}".format(a, b, example.add(a, b)))
#!/usr/bin/env python import example example.add(2,3)
def test_add(self): self.assertEquals(4, example.add(2, 2)) self.assertEquals(4, example.add(2, 2))
import example result = example.add(1, 2) print(f'Result: {result}')
# 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_add(): assert add(5, 4) == 9 assert add(1, 1) == 2
def test_1_add(): """Test 1.""" assert example.add(3, 4) == 7
import example from math import * import sys result = example.add(4, 5) print(result) print(pi) print(e) print(sys.path)
import example print(example.add(1, 2))
def test_add_12_and_24(self): self.assertEqual( 36, example.add(12, 24) )