monkeypatching

How to create new closure cell objects?

谁说胖子不能爱 提交于 2019-12-22 16:40:10
问题 I need to monkey-patch my library to replace an instance of a symbol, and it's getting referenced by some function closures. I need to copy those functions (since I also need access to original unpatched version of the function as well), but __closure__ is immutable, and I can't copy.copy it, so how can I create new closure cells objects in Python 2.7? I for example given this function def f(): def incorrectfunction(): return 0 def g(): return incorrectfunction() return g def correctfunction(

Python: Monkeypatching a method of an object

巧了我就是萌 提交于 2019-12-22 09:49:55
问题 I'm hitting an API in python through requests' Session class. I'm making GET & POST method call using requests.Session(). On every call(GET/POST) failure, I want to notify another process. I can do this by creating a utility method as follows: s = request.Session() def post(): try: s.post(URL,data,headers) except: notify_another_process() And call this method instead of requests.Session().post directly. But, I want to monkeypatch this code to requests.Session().post and want the additional

Python monkey patch private function

删除回忆录丶 提交于 2019-12-22 05:43:20
问题 I have a module with a function (call it a() ) that calls another function defined in the same module (call it __b() ). __b() is a function which speaks to a website via urllib2 and gets some data back. Now I'm trying to test a() , but of course would rather not have my unit tests speak to the public internet. Thus, I'm thinking if I can monkey patch __b() with a function which returns canned data, then I can write the tests for a() . To be more concrete, my module looks kinda like: def a():

Tracking down implicit unicode conversions in Python 2

孤街浪徒 提交于 2019-12-21 12:06:37
问题 I have a large project where at various places problematic implicit Unicode conversions (coersions) were used in the form of e.g.: someDynamicStr = "bar" # could come from various sources # works u"foo" + someDynamicStr u"foo{}".format(someDynamicStr) someDynamicStr = "\xff" # uh-oh # raises UnicodeDecodeError u"foo" + someDynamicStr u"foo{}".format(someDynamicStr) (Possibly other forms as well.) Now I would like to track down those usages, especially those in actively used code. It would be

Is it possible to do monkey patching in Java, if not is there an alternative?

与世无争的帅哥 提交于 2019-12-21 07:56:22
问题 This was asked 8 years ago here and since then 8 years have passed. I wanted to ask this question again to see if anyone has developed a framework, tool or library that does monkey patching. Basically what I need it for is a java application that I applied my own patch to. Since this project is maintained by another team I want to be able to keep/apply any patch I make, to the patches they make. 回答1: There are a number of techniques that might be applicable here, but your question is too

Is it possible to override __getitem__ at instance level in Python?

我的梦境 提交于 2019-12-21 03:44:26
问题 With the following code : import types class Foo(): def __getitem__(self, x): return x def new_get(self, x): return x + 1 x = Foo() x.__getitem__ = types.MethodType(new_get, x) x.__getitem__(42) will return 43, but x[42] will return 42. Is there a way to override __getitem__ at instance level in Python? 回答1: This is unfortunately, and quite surprisingly, not allowed: For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s

Is it possible to give javascript partial class behavior like C# or monkey patching like Ruby does?

和自甴很熟 提交于 2019-12-21 03:02:46
问题 The idea behind partial classes is that you can group certain functions together. The best example of this in C# is putting control definitions in one file and the event handlers in another. In Ruby, you can use Monkey patching to replace entire functions etc to get code to do something you want it to. I haven't found a reason to do this yet, but I figure as web improves, more of an application is going to be on the client side so I'm wondering if some of the great features I find in server

How to monkey patch or override a swc class in Flex?

烈酒焚心 提交于 2019-12-20 07:06:10
问题 I'm using a swc from the Axiis project to display visualizations in a project I'm working on. I've run into a bug where re-compiling the library swc would be an easy solution, but I can only use the buggy version of the swc. I have the Axiis source and compiled a version with the bug fixed, though I'm not allowed to use it because of client version restrictions. Does anyone know how I can use the updated Actionscript class/file in my code so it overrides the swc class? 回答1: I'm not sure if

How to extend Class instance

不羁岁月 提交于 2019-12-20 02:01:43
问题 MyClass is defined in module.py . There is no way we can modify it. But we do know the Class definition looks like this: class MyClass: def method(self, msg): print 'from method:', msg I start my script with importing the module and then declaring an object's instance: import module foo = module.MyClass() Then I write my own function: def function(msg): print 'from function:', msg Now, every time foo.method('') is used I want to call function() so it prints the same message too. Would this

How To Run Arbitrary Code After Django is “Fully Loaded”

六月ゝ 毕业季﹏ 提交于 2019-12-19 05:07:17
问题 I need to perform some fairly simple tasks after my Django environment has been "fully loaded". More specifically I need to do things like Signal.disconnect() some Django Signals that are setup by my third party library by default and connect my own Signals and I need to do some "monkey patching" to add convenience functions to some Django models from another library. I've been doing this stuff in my Django app's __init__.py file, which seems to work fine for the monkey patching, but doesn't