我希望包装一个在python中使用的C库。我还希望使用测试驱动开发来实现我的类。下面是我目前为止编写的两个类及其接口。有一大堆像这样的课程我需要包装。在
class FunctionWrapperForOurLibrary:
def __init__():
pass
def __call__():
pass
class OurOpener(FunctionWrapperForOurLibrary):
def __init__(self, our_library):
self.our_library = our_library
our_library.OurOpen.argtypes = [c_char_p, c_char_p]
our_library.OurOpen.restype = OUR_ERROR
def __call__(self, admin_path, company_path):
status = self.our_library.OurOpen(admin_path, company_path)
if status.lRc:
raise Exception("Unable to connect to database due to this error: "+str(status.lRc))
class OurDataCreator(FunctionWrapperForOurLibrary):
def __init__(self, our):
self.our_library = our_library
our_library.OurCreateData.argtypes = [c_int]
our_library.OurCreateData.restype = POINTER(OUR_DATA)
def __call__(self, our_structure_type):
data = self.our_library.OurCreateData(our_structure_type)
return data
第一个问题,我是否通过包含接口类来帮助可读性?我知道不需要。我的对象名和类名呢。有什么建议可以让这些更清楚?在
第二,如何编写一个包含这些函子的类?一个可以单元测试的类?在
我能想到的最好的办法是:
^{pr2}$但是我将有大量的函子传递给类构造函数。在
做这个更好吗?在
更新:
class Our:
def __init__(self, our_library):
self.our_library = our_library
our_library.OurOpen.argtypes = [c_char_p, c_char_p]
our_library.OurOpen.restype = OUR_ERROR
our_library.OurCreateData.argtypes = [c_int]
our_library.OurCreateData.restype = POINTER(OUR_DATA)
def open(self, admin_path, company_path):
status = self.our_library.OurOpen(admin_path, company_path)
if status.lRc:
raise Exception("Unable to connect to database due to this error: "+str(status.lRc))
def create_data(self, our_structure_type):
return self.our_library.OurCreateData(our_structure_type)
目前没有回答
相关问题 更多 >
编程相关推荐