Python で使用する C ライブラリをラップしたいと考えています。また、テスト駆動開発を使用してクラスを実装したいと考えています。これまでに作成した 2 つのクラスとそのインターフェイスを次に示します。私がラップする必要があるのと同じように、たくさんのクラスがあります。
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
最初の質問は、インターフェイス クラスを含めることで読みやすくすることですか? 必要ないことに気づきます。オブジェクト名とクラス名はどうですか。これらをより明確にする方法について何か提案はありますか?
次に、これらのファンクターを含むクラスをどのように作成すればよいでしょうか? ユニットテスト可能なクラス?
私が思いつくことができる最高のものは、これらの行に沿ったものです:
class Our:
def __init__(self, our_open, our_data_create, ...):
self.opener = our_open
self.data_create = our_data_create
our_open = OurOpener(our_library)
our_data_create = OurDataCreator(our_library)
our = Our(our_open, our_data_create)
しかし、クラス コンストラクターに渡すファンクターは多数あります。
これを行う方が良いですか?
アップデート:
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)