Show HN: zodiac, a new approach to monkey patching in python
github.com
github.com
with patch('path.to.object') as my_mock: my_mock.method.return_value = ...
also you can patch a module/class with a function.
Monkeypatching gevent's socket globally allows you to use libraries and drivers like pymongo off the shelf, and they will be non-blocking by default.
Not to mention that if you are using a c-extension monkey patching won't help much.
(I've worked on a lot of code that took the second trade-off. The result is almost always a full rewrite. Maintain your codebase or it goes out of control really fast.)
In the example I gave, you'd have to rewrite whatever database drivers you're using, or at least hack them to work in a non-blocking fashion. That could have just as much development cost as monkeypatching.
zodiac is an attempt to address the problems that make monkeypatching such a risky, unpredictable option, by making patches smaller and therefore using more system code. Hopefully, for situations where monkeypatching is necessary, I can make it cleaner and more maintainable.
Nice one. That's a deep concept. Programmers and projects generally get themselves into trouble because of a misunderstanding of the trade-offs.
It's rookie programmers who are enamored with a new bit of tech, but don't understand the downsides. It's managers who have an inaccurate estimation of risks and so lack the confidence to make decisive moves.
This is why experience can be so valuable in the programming field. This is the only way to learn about the cost/benefit trade offs.
Maybe "facile" is a better term than "easy" for this, though.