import addList l = [9, 2, 3, 4, 5] print l print str(addList.add(l))
class MockStdOut: def __init__(self): self.src_stdout = None self.stdout = None def __enter__(self) -> StringIO: print("Change stdout") self.src_stdout, self.stdout = sys.stdout, StringIO() sys.stdout = self.stdout print("Start mocking") return self.stdout def __exit__(self, *exc_info): self.stdout.close() sys.stdout = self.src_stdout print("Returning stdout") input_array = [1, 2, 3] add(input_array) with MockStdOut() as stdout: add(input_array) stdout.flush() stdout.seek(0) mock_strings = stdout.read() print("StringIO:", mock_strings, sep="\n")
# -*- coding: utf-8 -*- #Though it looks like an ordinary python import, the addList module is implemented in C import addList l = [1,2,3,4,5] print("Sum of List - " + str(l) + " = " + str(addList.add(l)))
try: newInfo.updatefeed(rsstree) except: # print(("[{0}] {1} update failed.").format(timeStampExec(), i)) logging.warning(i + " update failed.") rsstree = ET.parse('feed.xml') rss = rsstree.getroot() rsstree = newInfo.updatefeed(rss) file.close() try: ET.ElementTree(rsstree).write('feed.xml', encoding='utf-8') with open("list.json", 'w') as file: json.dump(context, file, indent=4) file.close() # print('[' + timeStampExec() + '] XML file update success!') logging.debug("XML file update success!") except: # print('[' + timeStampExec() + '] XML file update failed or nothing need to be updated!') logging.info("XML file update failed or nothing need to be updated!") return if __name__ == '__main__': if sys.argv != None: try: tup = sys.argv[1:] addList.add(tup) except Exception as e: print(sys.argv) main()
#Though it looks like an ordinary python import, the addList module is implemented in C import addList l = [1,2,3,4,5] print "Sum of List - " + str(l) + " = " + str(addList.add(l))
#!/usr/bin/python import addList print(addList.add([1, 2, 3, 7]))
# ctypes模块提供了和C语言兼容的数据类型和函数来加载dll/so文件, # 因此在调用时不需对源文件做任何的修改。 from ctypes import * # import c lib adder = CDLL('./adder.so') # call c function add_int res_int = adder.add_int(4, 5) print('4+5= ', res_int) # call c function add_float # Python中的十进制值转化为c_float类型,然后才能传送给C函数。有许多限制,例如并不能在C中对对象进行操作 a = c_float(5.5) b = c_float(4.1) adder.add_float.restype = c_float res_float = adder.add_float(a, b) print('5.5+4.1= ', res_float) # SWIG # 适合多语言, 但是编写一个额外的接口文件来作为SWIG(终端工具)的入口 # Python/C API 简单,而且可以在C代码中操作你的Python对象 #module that talks to the C code import addList l = [1, 23, 4, 5, 6] print("sum of List :", str(l), '=', str(addList.add(l)))
import addList l = [1,2,3,4,5] print 'Sum of list - ', str(l), '=', str(addList.add(l))