monkeypatching

Monkey patching a Django form class?

旧时模样 提交于 2019-11-29 14:53:06
问题 Given a form class (somewhere deep in your giant Django app).. class ContactForm(forms.Form): name = ... surname = ... And considering you want to add another field to this form without extending or modifying the form class itself , why does not the following approach work? ContactForm.another_field = forms.CharField(...) (My first guess is that the metaclass hackery that Django uses applies only the first time the form class is constructed. If so, would there be a way to redeclare the class

Proper indentation in Django templates (without monkey-patching)?

旧街凉风 提交于 2019-11-29 11:07:16
I want to generate human-readable HTML and CSS code (properly indented) preprocessed by the Django template system for my standalone application. I've modified the render method from the NodeList class found in the django.template.base module. My code seems to work properly, but I'm using monkey-patching to replace the old render method. Is there a more elegant way that does not use monkey-patching in this case? Or maybe monkey-patching is the best way here? My code looks like this: ''' This module monkey-patches Django template system to preserve indentation when rendering templates. '''

Conjugate transpose operator “.H” in numpy

余生颓废 提交于 2019-11-28 23:21:26
It is very convenient in numpy to use the .T attribute to get a transposed version of an ndarray . However, there is no similar way to get the conjugate transpose. Numpy's matrix class has the .H operator, but not ndarray. Because I like readable code, and because I'm too lazy to always write .conj().T , I would like the .H property to always be available to me. How can I add this feature? Is it possible to add it so that it is brainlessly available every time numpy is imported? (A similar question could by asked about the .I inverse operator.) You can subclass the ndarray object like: from

How to monkey-patch code that gets auto-loaded in Rails?

余生颓废 提交于 2019-11-28 21:26:20
I'm monkey-patching a Rails engine with something like: SomeClass.class_eval do # ... end The first time I hit the web site, on development mode at least, it works, but the second time it's like my patch never existed. I presume it's Rails auto-reloading the engine (which is installed in vendor/) and not reloading my code. This is Rails 2.3. Any ideas how to do it so that my code also gets reloaded? EDIT: This solution only works for Rails 3+ since it's dependent on some functionality in Rails::Railtie. Put this code in an initializer. This question is quite old, but here's a solution I found:

Recommended approach to monkey patching a class in ruby

拜拜、爱过 提交于 2019-11-28 21:04:37
问题 I've noticed that there are two common ways to monkey patch a class in ruby: Define the new members on the class like so: class Array def new_method #do stuff end end And calling class_eval on the class object: Array.class_eval do def new_method #do stuff end end I'm wondering if there is any difference between the two and whether there are advantages to using one approach over the other? 回答1: Honestly, I used to use the 1st form (reopening the class), as it feels more natural, but your

Extending builtin classes in python

久未见 提交于 2019-11-28 19:12:37
How can I extend a builtin class in python? I would like to add a method to the str class. I've done some searching but all I'm finding is older posts, I'm hoping someone knows of something newer. S.Lott Just subclass the type >>> class X(str): ... def my_method(self): ... return int(self) ... >>> s = X("Hi Mom") >>> s.lower() 'hi mom' >>> s.my_method() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in my_method ValueError: invalid literal for int() with base 10: 'Hi Mom' >>> z = X("271828") >>> z.lower() '271828' >>> z.my_method() 271828 One way

Is it possible to monkey patch in Java?

早过忘川 提交于 2019-11-28 10:46:01
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 DefaultWidget in a fairly complex method. You would like it to use your NiceWidget. The factory class's

Proper indentation in Django templates (without monkey-patching)?

女生的网名这么多〃 提交于 2019-11-28 04:48:13
问题 I want to generate human-readable HTML and CSS code (properly indented) preprocessed by the Django template system for my standalone application. I've modified the render method from the NodeList class found in the django.template.base module. My code seems to work properly, but I'm using monkey-patching to replace the old render method. Is there a more elegant way that does not use monkey-patching in this case? Or maybe monkey-patching is the best way here? My code looks like this: ''' This

Is “monkey patching” really that bad? [closed]

青春壹個敷衍的年華 提交于 2019-11-28 04:44:57
Some languages like Ruby and JavaScript have open classes which allow you to modify interfaces of even core classes like numbers, strings, arrays, etc. Obviously doing so could confuse others who are familiar with the API but is there a good reason to avoid it otherwise, assuming that you are adding to the interface and not changing existing behavior? For example, it might be nice to add a an Array.map implementation to web browsers which don't implement ECMAScript 5th edition (and if you don't need all of jQuery). Or your Ruby arrays might benefit from a "sum" convenience method which uses

Monkey patching Devise (or any Rails gem)

天大地大妈咪最大 提交于 2019-11-28 04:33:43
I'm using the Devise authentication gem in my Rails project, and I want to change the keys it's using in flash alerts. (Devise uses :notice and :alert flash keys, but I want to change them to :success and :error so that I can display nice green/red boxes with Bootstrap .) So I want to be able to somehow override the set_flash_message method in DeviseController . Here's the new method: def set_flash_message(key, kind, options = {}) if key == 'alert' key = 'error' elsif key == 'notice' key = 'success' end message = find_message(kind, options) flash[key] = message if message.present? end But I