monkeypatching

Monkey patching a class in another module in Python

Monkey patching a class in another module in Python Question: I’m working with a module written by someone else. I’d like to monkey patch the __init__ method of a class defined in the module. The examples I have found showing how to do this have all assumed I’d be calling the class myself (e.g. Monkey-patch …

Total answers: 6

Making object JSON serializable with regular encoder

Making object JSON serializable with regular encoder Question: The regular way of JSON-serializing custom non-serializable objects is to subclass json.JSONEncoder and then pass a custom encoder to json.dumps(). It usually looks like this: class CustomEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, Foo): return obj.to_json() return json.JSONEncoder.default(self, obj) print(json.dumps(obj, cls=CustomEncoder)) What I’m trying to do, is to …

Total answers: 6

Using Celery on processes and gevent in tasks at the same time

Using Celery on processes and gevent in tasks at the same time Question: I’d like to use Celery as a queue for my tasks so my web app could enqueue a task, return a response and the task will be processed meanwhile / someday / … I build a kind of API, so I don’t …

Total answers: 4

Reassigning a module's function for testing purposes

Reassigning a module's function for testing purposes Question: I was considering reassigning some functions from a standard library module in my testing suite, however I have found that doing so has a global effect (when I expected them to only have an effect locally). For example: import time def test(): time.sleep = "hello" #woah there! …

Total answers: 2

How to Mock an HTTP request in a unit testing scenario in Python

How to Mock an HTTP request in a unit testing scenario in Python Question: I would like to include a Web server for all my test related to HTTP. It doesn’t need to be very sophisticated. I would prefer not to be dependent of being online. So I could test some options of my program. …

Total answers: 1

Can I patch a Python decorator before it wraps a function?

Can I patch a Python decorator before it wraps a function? Question: I have a function with a decorator that I’m trying test with the help of the Python Mock library. I’d like to use mock.patch to replace the real decorator with a mock ‘bypass’ decorator which just calls the function. What I can’t figure …

Total answers: 12

What is monkey patching?

What is monkey patching? Question: I am trying to understand, what is monkey patching or a monkey patch? Is that something like methods/operators overloading or delegating? Does it have anything common with these things? Asked By: Sergei Basharov || Source Answers: According to Wikipedia: In Python, the term monkey patch only refers to dynamic modifications …

Total answers: 8

Can I add custom methods/attributes to built-in Python types?

Can I add custom methods/attributes to built-in Python types? Question: For example—say I want to add a helloWorld() method to Python’s dict type. Can I do this? JavaScript has a prototype object that behaves this way. Maybe it’s bad design and I should subclass the dict object, but then it only works on the subclasses …

Total answers: 8

Monkey-patch Python class

Monkey-patch Python class Question: I’ve got a class, located in a separate module, which I can’t change. from module import MyClass class ReplaceClass(object) … MyClass = ReplaceClass This doesn’t change MyClass anywhere else but this file. However if I’ll add a method like this def bar(): print 123 MyClass.foo = bar this will work and …

Total answers: 4