我有一个类 MasterData,它的实例包含一些关于所有可能的小工具的一般信息。
我有另一个类CustomerData,它的实例包含:
- 有关这些小工具的子集的一些特定信息,由客户提供给我们
- 从每个小工具到
MasterData中相同小工具的链接>
CustomerData 构造函数接受参数:
- 客户提供的文件
- 我为每个客户编写的一个 Python 模块,其中包含有关客户文件的各种详细信息,以模块级变量的形式;例如,它说明哪个客户字段包含产品标识符;等
链接就像从客户文件中获取适当的 ID 并在主数据的适当字段中查找一样简单。在客户模块中提供了包含相关 ID(客户和主)的字段名称等。实际链接是在 CustomerData 构造函数中执行的。
在我们想出新的、复杂得多的匹配规则之前,一切正常。每个匹配规则只适用于一个特定的客户,所以我想把所有可能的匹配规则都放在per-customer模块中,然后让应用程序选择它想使用的规则。但遗憾的是,新的匹配规则需要对主数据进行操作,模块自然是一个“被动”对象,连主数据都没有链接。
是否应该在以主数据实例和客户记录为参数的模块中创建全局函数,并返回主数据记录?
或者我应该将匹配规则放在别处吗?
请您参考如下方法:
Should I create global functions in the modules that take the master data instance and the customer record as a parameter, and return the master data record?
一般。
a Python module that I wrote for each customer that contains various details about the customer file, in the form of module-level variables; e.g., it says which customer field contains the product identifier; etc.
这实际上不应该是一个模块。这不是错误。但这可能会造成混淆。
考虑改变一些东西,这样你就有了这样的东西。
class AbstractCustomerData( object ):
"""Generic features common to all customer data."""
然后。每个客户都有一个模块,其中有
from the_master_copy import AbstractCustomerData
class ThisCustomerData( AbstractCustomerData ):
"""All the various kinds of overrides and whatnot."""
def the_matching_rule( self, master_data_object_or_collection_or_whatever ):
"""This customer's override."""
这样,您就可以在客户模块中使用您的ThisCustomerData 实例。该模块不是任何东西的参数;该模块包含客户的代码。
客户安装您的应用程序后看起来像这样
import generic_stuff
from customer_module import ThisCustomerData, other_feature, yet_more
现在您可以从 generic_stuff 加上从客户的扩展模块导入的客户特定覆盖构建客户的特定应用程序实例。
这样做的好处是允许更多、更多的自由,因为您不局限于将模块插入类中。您正在使用模块来包含一个类定义,它现在可以执行任何。
