monkeypatching

Can I replace or modify a function on a jQuery UI widget? How? (Monkey Patching)

為{幸葍}努か 提交于 2019-12-18 13:09:10
问题 If I want to tweak some of the capability of a jQuery UI object, by replacing one of the functions, how would I go about doing that? Example: suppose I wanted to modify the way the jQuery autocomplete widget rendered the suggestions. There's a method on the autocomplete object that looks like this: _renderItem: function( ul, item) { return $( "<li></li>" ) .data( "item.autocomplete", item ) .append( "<a>" + item.label + "</a>" ) .appendTo( ul ); }, Could I replace this? I think this might be

Adding a datetime stamp to Python print

江枫思渺然 提交于 2019-12-18 12:18:47
问题 I am trying to debug the behaviour of a large library I depend on, which uses a scattering (no make that plethora) of debug print statements through its many source files. Trouble is, most if not all of these debug print statements do not contain a date/time stamp so it is hard to associate failures at the application level with failures within the library code itself. Rather than modifying the source code for all the debug prints suspected to be involved in the failure I am seeing, I thought

Monkey-patching Vs. S.O.L.I.D. principles?

不打扰是莪最后的温柔 提交于 2019-12-18 10:34:36
问题 I'm slowly moving from PHP5 to Python on some personal projects, and I'm currently loving the experience. Before choosing to go down the Python route I looked at Ruby. What I did notice from the ruby community was that monkey-patching was both common and highly-regarded. I also came across a lot of horror stories regarding the trials of debugging ruby s/w because someone included a relatively harmless library to do a little job but which patched some heavily used core object without telling

Is it possible to monkey patch in Java?

对着背影说爱祢 提交于 2019-12-17 19:48:20
问题 I don't want to discuss the merits of this approach, just if it is possible. I believe the answer to be "no". But maybe someone will surprise me! Imagine you have a core widget class. It has a method calculateHeight() , that returns a height. The height is too big - this result in buttons (say) that are too big. You can extend DefaultWidget to create your own NiceWidget, and implement your own calculateHeight() to return a nicer size. Now a library class WindowDisplayFactory, instantiates

Why is it frowned upon to modify JavaScript object's prototypes?

北慕城南 提交于 2019-12-17 06:52:22
问题 I've come across a few comments here and there about how it's frowned upon to modify a JavaScript object's prototype? I personally don't see how it could be a problem. For instance extending the Array object to have map and include methods or to create more robust Date methods? 回答1: The problem is that prototype can be modified in several places. For example one library will add map method to Array's prototype and your own code will add the same but with another purpose. So one implementation

What is monkey patching?

我是研究僧i 提交于 2019-12-16 19:02:26
问题 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? 回答1: No, it's not like any of those things. It's simply the dynamic replacement of attributes at runtime. 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

Mixing in a module within Object causes all Objects to inherit that module's instance methods as singleton methods

百般思念 提交于 2019-12-14 03:17:57
问题 When attempting to add my own behavior to the Object class, I get undesired effects that don't occur when mixing the module into a user-defined class. module Entity def some_instance_method puts 'foo' end def self.secret_class_method puts 'secret' end module ClassMethods def some_class_method puts 'bar' end end def self.included( other_mod ) other_mod.extend( ClassMethods ) end end Now, I create class Bob such that it includes Entity . class Bob; include Entity; end; This yields the desired

Date constructor monkey patch

僤鯓⒐⒋嵵緔 提交于 2019-12-13 18:26:16
问题 I'm trying to monkey patch a javascript Date constructor. I have the following code: var __Date = window.Date; window.Date = function(){ __Date.apply(this, Array.prototype.slice.call(arguments)); }; window.Date.prototype = __Date.prototype; As far as I know the javascript constructor (when called) does three things: It creates a new object that the this points to when the constructor function is executed It sets up this object to delegate to this constructor's prototype. It executes the

Python: how to monkey patch class method to other class method

◇◆丶佛笑我妖孽 提交于 2019-12-12 18:04:44
问题 I have got the following code: class A: def __init__(self): self.a = "This is mine, " def testfunc(self, arg1): print self.a + arg1 class B: def __init__(self): self.b = "I didn't think so" self.oldtestfunc = A.testfunc A.testfunc = self.testfuncPatch def testfuncPatch(self, arg): newarg = arg + self.b # B instance 'self' self.oldtestfunc(self, newarg) # A instance 'self' instA = A() instB = B() instA.testfunc("keep away! ") I want to do the following: Some class A consists of a function with

Should a plugin adding new instance-methods monkey-patch or subclass/mixin and replace the parent?

元气小坏坏 提交于 2019-12-12 14:21:15
问题 As a simple example take a class Polynomial class Polynomial(object): def __init__(self, coefficients): self.coefficients = coefficients for polynomials of the form p(x) = a_0 + a_1*x + a_2*x^2 + ... + a_n*x^n where the list coefficients = (a_0, a_1, ..., a_n) stores those coefficients. One plugin-module horner could then provide a function horner.evaluate_polynomial(p, x) to evaluate a Polynomial instance p at value x , i.e. return the value of p(x) . But instead of calling the function that