python猴子补丁_猴子打补丁是什么?

猴子补丁是什么?

简单地说,猴子补丁是在程序运行时对模块或类进行更改。

用法示例

在Pandas文档中有一个猴子补丁的例子:import pandas as pddef 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 classdf =

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

姓名损坏警告

如果您使用的是name-mangling(在属性前面加上双下划线,这会更改名称,我不建议这样做),如果这样做,您将不得不手动命名magle。因为我不推荐你的名字,所以我不会在这里演示。

测试实例

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

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

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

raise datasource.DataRetrievalErrordatasource.Structure.get_data = get_data

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

只需执行上述操作,就会更改Structure对象,因此您需要在单元测试中使用设置和解压,以避免这样做,例如: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_datadef tearDown(self):

# give the real method back to the Structure object:

datasource.Structure.get_data = self.real_get_data

(虽然上述情况很好,但使用mock库来修补代码。mock氏patch与以上操作相比,修饰器更容易出错,这将需要更多代码行,从而增加引入错误的机会。中的代码。mock但我想它也是以类似的方式使用猴子补丁的。)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值