在两个数据集之间创建映射的规则放在哪里?

2024-09-28 05:24:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类MasterData,它的实例包含一些关于所有可能的小工具的一般信息。你知道吗

我还有另一个类CustomerData,其实例包含:

  • 客户向我们提供的关于这些小工具子集的一些特定信息
  • 从每个小工具到MasterData中相同小工具的链接

CustomerData构造函数采用以下参数:

  • 客户提供的文件
  • 我为每个客户编写的Python模块,以模块级变量的形式包含客户文件的各种详细信息;例如,它表示哪个客户字段包含产品标识符;等等

链接非常简单,只需从客户文件中获取适当的ID并在主数据的适当字段中查找即可。在customer模块中提供了包含相关id(customer和master)的字段的名称。实际的链接是在CustomerData构造函数中执行的。你知道吗

在我们提出新的、更复杂的匹配规则之前,一切都很顺利。每个匹配规则只适用于一个特定的客户,所以我想把所有可能的匹配规则放在每个客户模块中,然后让应用程序选择它想要使用的规则。但不幸的是,新的匹配规则要求对主数据进行操作,模块自然是一个“被动”对象,甚至没有到主数据的链接。你知道吗

我是否应该在以主数据实例和客户记录为参数的模块中创建全局函数,并返回主数据记录?你知道吗

或者我应该把匹配规则放在别处?你知道吗


Tags: 模块文件工具数据实例信息参数客户
1条回答
网友
1楼 · 发布于 2024-09-28 05:24:22

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."""

这样,就可以在customer模块中使用ThisCustomerData实例。模块不是任何东西的参数;模块包含客户的代码。你知道吗

客户安装的应用程序如下所示

import generic_stuff
from customer_module import ThisCustomerData, other_feature, yet_more

现在,您可以从通用的应用程序构建客户特定的应用程序实例,再加上从客户的扩展模块导入的客户特定的覆盖。你知道吗

这样做的好处是允许更多的自由,因为您不局限于将模块插入类中。您正在使用module来包含一个类定义,它现在可以做任何事情。你知道吗

相关问题 更多 >

    热门问题