monkeypatching

Add functionality to Django FlatPages without changing the original Django App

。_饼干妹妹 提交于 2019-12-03 02:50:13
问题 I would like to add a field to the Django FlatPage database model, but I do not really know how to extend this without editing the original application. What I want to do is to add the following field to the model: from django.db import models from django.contrib.flatpages.models import FlatPage as FlatPageOld class FlatPage(FlatPageOld): order = models.PositiveIntegerField(unique=True) How do I get to add this to the FlatPage model? Thanks in advance 回答1: Your approach is fine - you just don

pymongo + gevent: throw me a banana and just monkey_patch?

我的未来我决定 提交于 2019-12-03 00:46:24
Quickie here that needs more domain expertise on pymongo than I have right now: Are the "right" parts of the pymongo driver written in python for me to call gevent monkey_patch() and successfully alter pymongo's blocking behavior on r/w within gevent "asynchronous" greenlets? If this will require a little more leg work on gevent and pymongo -- but it is feasible -- I would be more than willing to put in the time as long as i can get a little guidance over irc. Thanks! Note: At small scale mongo writes are not a big problem because we are just queuing a write "request" before unblocking. BUT

Monkey patching a @property

感情迁移 提交于 2019-12-02 21:47:05
Is it at all possible to monkey patch the value of a @property of an instance of a class that I do not control? class Foo: @property def bar(self): return here().be['dragons'] f = Foo() print(f.bar) # baz f.bar = 42 # MAGIC! print(f.bar) # 42 Obviously the above would produce an error when trying to assign to f.bar . Is # MAGIC! possible in any way? The implementation details of the @property are a black box and not indirectly monkey-patchable. The entire method call needs to be replaced. It needs to affect a single instance only (class-level patching is okay if inevitable, but the changed

Patching Element.prototype.addEventListener breaks any Angular 2 app

我的未来我决定 提交于 2019-12-02 20:51:17
问题 I have an Angular 2 app https://github.com/DanWahlin/Angular-JumpStart. I tried to patch Element.prototype.addEventListener for some of my use case. Here is the patch: var origLis = Element.prototype.addEventListener; Element.prototype.addEventListener = function(type, handler, useCapture) { console.log("Added"); return origLis.apply(this, arguments); } If I patch Element.prototype.addEventListener , it breaks the Angular 2 app. No resources/XHRs are being sent in the app. I tried this on

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

人盡茶涼 提交于 2019-12-02 07:41:29
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? I'm not sure if you are allowed to do the following: put the class with your fix in the source path of your main application.

How to extend Class instance

喜夏-厌秋 提交于 2019-12-01 22:34:31
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 situation be referred as the monkey patching ? How to achieve what is needed? Yes, it's called monkey

Python - monkey patch fails, why?

微笑、不失礼 提交于 2019-12-01 21:59:08
问题 I want to monkey patch on f(*args, **kwargs) from an installed module. I use the idea of decorator on my own code, but other methods from the installed module fails to call f correctly. Here is an example: import numpy as np def log(func): def wrapper(*args, **kwargs): print('logging') return func(*args, **kwargs) return wrapper if __name__ == "__main__": a1 = np.asarray([0, 1, 2]) print(f'a1={a1}') a2 = np.array([0, 1, 2]) print(f'a2={a2}') np.array = log(np.array) a3 = np.asarray([0, 1, 2])

Python - monkey patch fails, why?

你说的曾经没有我的故事 提交于 2019-12-01 21:12:20
I want to monkey patch on f(*args, **kwargs) from an installed module. I use the idea of decorator on my own code, but other methods from the installed module fails to call f correctly. Here is an example: import numpy as np def log(func): def wrapper(*args, **kwargs): print('logging') return func(*args, **kwargs) return wrapper if __name__ == "__main__": a1 = np.asarray([0, 1, 2]) print(f'a1={a1}') a2 = np.array([0, 1, 2]) print(f'a2={a2}') np.array = log(np.array) a3 = np.asarray([0, 1, 2]) print(f'a3={a3}') a4 = np.array([0, 1, 2]) print(f'a4={a4}') The output is: a1=[0 1 2] a2=[0 1 2] a3=

Controller monkey patch in initializer gets lost when rails reloads classes

拥有回忆 提交于 2019-12-01 05:23:51
问题 I am trying to monkey patch controller classes in a third party gem. To be precise, I am trying to add parameter wrapping to devise controllers. In initializers/wrap_parameters.rb I added the following bit: Rails.application.config.after_initialize do DeviseController.class_eval do wrap_parameters :user, format: [:json] end end It works great when the application starts, but when I modify one of my controller classes, the parameter wrapping stops working immediately. As if the controller

Adding base class to existing object in python

只谈情不闲聊 提交于 2019-12-01 05:14:06
I have several objects of different kinds (different function names, different signatures) and I monkey patch them to have a common way to access them from different functions. Briefly, there is a dispatcher that takes the objects that I want to patch and depending on the object type it calls different patcher. A patcher will add methods to the object: def patcher_of_some_type(object): def target(self, value): # do something and call self methods object.target = types.MethodType(target, object) # many more of these As the program grows more complicated making a wrapper around the object (or