python猴子补丁好处_什么是猴子补丁?

What is a monkey patch?

简单地说,monkey patching就是在程序运行时对模块或类进行更改。

使用示例

熊猫文档中有一个猴子修补的例子:import pandas as pd

def just_foo_cols(self):

"""Get a list of column names containing the string 'foo'

"""

return [x for x in self.columns if 'foo' in x]

pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class

df = pd.DataFrame([list(range(4))], columns=["A","foo","foozball","bar"])

df.just_foo_cols()

del pd.DataFrame.just_foo_cols # you can also remove the new method

要分解此问题,首先导入模块:import pandas as pd

接下来,我们创建一个方法定义,它在任何类定义的范围之外都是未绑定和自由的(因为函数和未绑定方法之间的区别是毫无意义的,所以Python 3取消了未绑定方法):def just_foo_cols(self):

"""Get a list of column names containing the string 'foo'

"""

return [x for x in self.columns if 'foo' in x]

接下来,我们只需将该方法附加到要在其上使用的类:pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class

然后我们可以在类的实例上使用该方法,完成后删除该方法:df = pd.DataFrame([list(range(4))], columns=["A","foo","foozball","bar"])

df.just_foo_cols()

del pd.DataFrame.just_foo_cols # you can also remove the new method

名称损坏警告

如果您使用名称管理(在属性前面加上双下划线,这会更改名称,我不建议这样做),那么如果您这样做,就必须手动命名管理。既然我不推荐使用mangling这个名字,我就不在这里演示了。

测试示例

例如,我们如何在测试中使用这些知识?

假设我们需要模拟对外部数据源的数据检索调用,该调用会导致错误,因为我们希望在这种情况下确保正确的行为。我们可以对数据结构进行猴子修补以确保这种行为。(因此使用丹尼尔·罗斯曼建议的类似方法名:)import datasource

def get_data(self):

'''monkey patch datasource.Structure with this to simulate error'''

raise datasource.DataRetrievalError

datasource.Structure.get_data = get_data

当我们测试它的行为依赖于这个方法引发错误时,如果正确实现,我们将在测试结果中得到这个行为。

执行上述操作将在流程的生命周期内更改Structure对象,因此您将希望在unittests中使用设置和拆卸来避免执行此操作,例如:def setUp(self):

# retain a pointer to the actual real method:

self.real_get_data = datasource.Structure.get_data

# monkey patch it:

datasource.Structure.get_data = get_data

def tearDown(self):

# give the real method back to the Structure object:

datasource.Structure.get_data = self.real_get_data

(虽然上面的方法很好,但是使用mock库修补代码可能是一个更好的主意。mock的{}decorator比执行上述操作更不容易出错,这将需要更多的代码行,从而有更多的机会引入错误。我还没有审查mock中的代码,但我想它也会以类似的方式使用monkey补丁。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值