猴子补丁

A MonkeyPatch is a piece of Python code which extends or modifies other code at runtime (typically at startup).

A simple example looks like this:

from SomeOtherProduct.SomeModule import SomeClass

def speak(self):
    return "ook ook eee eee eee!"

SomeClass.speak = speak

For instance, consider a class that has a method get_data. This method does an external lookup (on a database or web API, for example), and various other methods in the class call it.

However, in a unit test, you don‘t want to depend on the external data source - so you dynamically replace the get_data method with a stub that returns some fixed data.

Because Python classes are mutable, and methods are just attributes of the class, you can do this as much as you like - and, in fact, you can even replace classes and functions in a module in exactly the same way.

时间: 2024-08-14 21:19:11

猴子补丁的相关文章

基础入门_Python-模块和包.Gevent异步/状态获取/超时设置/猴子补丁?

简单介绍: 说明: Gevent是一个基于libev的并发库,为各种并发和网络相关的任务提供了整洁的API 快速安装: pip install --upgrade gevent 主要模式: 说明: Greenlet以C扩展模块形式接入PY轻量级协程,它们运行于主进程内部,被协作式的调度,且不同于multiprocessing和threading等真正的并行执行,它在同一时刻只能有一个协程在运行 公有方法 gevent.spawn(cls, *args, **kwargs) 创建一个Greenle

Python Monkey patch猴子补丁

monkey patch (猴子补丁)   用来在运行时动态修改已有的代码,而不需要修改原始代码. 简单的monkey patch 实现:[python] #coding=utf-8 def originalFunc():     print 'this is original function!'      def modifiedFunc():     modifiedFunc=1     print 'this is modified function!'      def main():

Python之猴子补丁

1.在运行时,对属性,方法,函数等进行动态替换 2.其目的往往是为了通过替换,修改来增强,扩展原有代码的能力 #test2.py class Person: def get_score(self): ret = {'english':80,'history':100,'chinese':150} return ret #test3.py def get_score(self): return dict(name=self.__class__.__name__,english=100,chines

python 模块会导入几次?猴子补丁为什么可以实现?

一共三个文件 a.py内容是 print('被导入') x = 1 b.py内容是 import a a.x = 2 c.py内容是 import a import b print(a.x) 现在运行c文件,这个结果出乎很多人的意料大部分python人员都猜不对,结果是 1.可以发现a模块被两个地方导入了,但是只打印一次 “被导入”. 2.在c文件里面不管是先导入a还是导入b,打印x的结果都是2. 不光是自己的文件如此,导入库文件也是一样.所以这就是猴子补丁能实现的原因. 2.再多想一下,为什么

第二种方式,修改python unittest的执行顺序,使用猴子补丁

1.按照测试用例的上下顺序,而不是按方法的名称的字母顺序来执行测试用例. 之前的文章链接 之前写的,不是猴子补丁,而是要把Test用例的类名传到run里面去执行,与原生的使用有一点区别.现在修改成,用的时候代码与原生unittest完全一模一样,但运行时候使用与原生不同的逻辑. import time import unittest from unittest.main import TestProgram from app.utils.utils_ydf import LogManager c

python中的猴子补丁Monkey Patch

python中的猴子补丁Monkey Patch 什么是猴子补丁 the term monkey patch only refers to dynamic modifications of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as desired

猴子补丁的应用,猴子补丁来改变日志。

打这个猴子补丁,就可以不需要修改任何一处代码,就能使项目中所有py文件的所有控制台日志变彩色和可点击跳转. import logging class ColorHandler(logging.Handler): """ A handler class which writes logging records, appropriately formatted, to a stream. Note that this class does not close the stream

Python面试题之“猴子补丁”(monkey patching)指的是什么?这种做法好吗?

“猴子补丁”就是指,在函数或对象已经定义之后,再去改变它们的行为. 举个例子: import datetime datetime.datetime.now = lambda: datetime.datetime(2012, 12, 12) 大部分情况下,这是种很不好的做法 - 因为函数在代码库中的行为最好是都保持一致.打“猴子补丁”的原因可能是为了测试.mock包对实现这个目的很有帮助. 为什么提这个问题? 答对这个问题说明你对单元测试的方法有一定了解.你如果提到要避免“猴子补丁”,可以说明你不

python协程初步--gevent库使用以及解释什么是猴子补丁monkey_patch

协程工作的特点是遇到阻塞或耗时的任务时就切换,协程的生存依赖于线程,线程依赖于协程 一个似乎有点问题的例子 import gevent,time def kisscpc(num): for i in range(num): print ("吻了第%s下陈培昌"%(i+1),gevent.getcurrent()) time.sleep(1) def kisscj(num): for i in range(num): print ("吻了第%s下程劲"%(i+1),g

第三次小组分享 猴子补丁

猴子补丁 什么是: 1,这个词原来为Guerrilla Patch,杂牌军.游击队,说明这部分不是原装的,在英文里guerilla发音和gorllia(猩猩)相似,再后来就写了monkey(猴子). 2,还有一种解释是说由于这种方式将原来的代码弄乱了(messing with it),在英文里叫monkeying about(顽皮的),所以叫做Monkey Patch. 功能: 功能: 动态的属性的替换 用处 在运行时替换方法.属性等 在不修改第三方代码的情况下增加原来不支持的功能 在运行时为内