指示要使用特定参数调用的函数而不执行该函数 [英] Instructing a function to be called with a specific argument without executing it

查看:53
本文介绍了指示要使用特定参数调用的函数而不执行该函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对逻辑进行编码以过滤Pandas数据帧.我想将逻辑编码为字典,以子组名称作为键,并使用过滤子组的函数作为值:

I am trying to encode logic to filter a Pandas dataframe. I'd like to encode the logic as a dictionary, with the subgroup name as the key, and the function to filter for the subgroup as the value:

analytics_table_mappings = {
"Jets Fans": BaseFilter.for_jets_fans,
"Patriots Fans": BaseFilter.for_patriots_fans,
...
}

我的BaseFilter.for_jets_fansBaseFilter.for_patriots_fans是静态方法,包含用于为每组风扇过滤数据框的逻辑.

My BaseFilter.for_jets_fans and BaseFilter.for_patriots_fans are static methods that contain the logic to filter my dataframe for each group of fans.

但是,我想创建一个函数BaseFilter.for_team_fans,该函数接受team字符串参数来指定要过滤哪些球队的粉丝.

However, I'd like to create a function BaseFilter.for_team_fans that accepts a team string parameter to designate which team's fans to filter for.

我当前的尝试是对类似这样的内容进行编码

My current attempt is to encoding something like this

analytics_table_mappings = {
"Jets Fans": {"func": BaseFilter.for_team_fans, "args": "Jets"},
"Patriots Fans":  {"func": BaseFilter.for_team_fans, "args": "Patriots"},
...
}

我的问题:是否有一种更优雅,更轻松,更可维护的方法来实现这一目标?就上下文而言,我是一名数据科学家,这是我最终建立的一个大型模型的一部分需要交给我的工程团队进行维护和保养.他们要求我限制特定领域语言(DSL)的数量,以帮助软化学习曲线并提高代码库的可维护性.我觉得要使用

My question: Is there a more elegant, less convoluted, more maintainable way of doing this? For context, I'm a data scientist, and this is part of a large model that I eventually need to hand off to my engineering team to maintain and upkeep. They've asked me to limit the amount of domain specific language (DSL) to help soften the learning curve and increase the code base maintainability. I feel like using

"Jets Fans": {"func": BaseFilter.for_team_fans, "args": "Jets"},
"Patriots Fans":  {"func": BaseFilter.for_team_fans, "args": "Patriots"}, 

具有迅速发展成非常复杂且难以管理的DSL的潜力.之所以对我的过滤逻辑进行编码是因为我们要过滤的指标类型以及我们如何对其进行过滤-可能会不断发展,因此,我没有将其硬编码到我的代码库中,而是将其分离为由字典组成的单独的configurations.py文件(即analytics_table_mappings).因此,我想保持过滤器逻辑的灵活性,同时仍使工程师可以维护它.

has the potential to quickly evolve into a very complex and unmanageable DSL. The reason why I am encoding my filtering logic is because the types of metrics that we filter for, and how we filter for them- are likely to evolve frequently, so instead of hardcoding them into my code base, I separated the filter logic out into separate configurations.py files that are comprised of dictionaries (ie. analytics_table_mappings). Thus, I'd like to keep flexibility in my filter logic while still making it maintainable for my engineers.

添加:

我还需要能够处理必须传递多个参数的实例.例如:

I also need to be capable of handling instances where multiple parameters must be passed. For instance:

    "Jets Fans": {"func": BaseFilter.for_team_fans, "args": "Jets"},
    "Patriots Fans":  {"func": BaseFilter.for_team_fans, "args": "Patriots"},
"NFC Fans": {"func": BaseFilter.for_team_fans, "args": ["Bears", "Packers", ...]}

推荐答案

您可以考虑 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆