monkeypatching

In Rails, how to add a new method to String class?

ぐ巨炮叔叔 提交于 2019-11-27 03:23:14
I want to build an index for different objects in my Rails project and would like to add a 'count_occurences' method that I can call on String objects. I saw I could do something like class String def self.count_occurences do_something_here end end What's the exact way to define this method, and where to put the code in my Rails project? Thanks You can define a new class in your application at lib/ext/string.rb and put this content in it: class String def to_magic "magic" end end To load this class, you will need to require it in your config/application.rb file or in an initializer. If you had

Can you patch *just* a nested function with closure, or must the whole outer function be repeated?

人盡茶涼 提交于 2019-11-27 00:53:57
A 3rd party library we use contains a rather long function that uses a nested function inside it. Our use of that library triggers a bug in that function, and we very much would like to solve that bug. Unfortunately, the library maintainers are somewhat slow with fixes, but we don't want to have to fork the library. We also cannot hold our release until they have fixed the issue. We would prefer to use monkey-patching to fix this issue here as that is easier to track than patching the source. However, to repeat a very large function where just replacing the inner function would be enough feels

Monkey patching Devise (or any Rails gem)

空扰寡人 提交于 2019-11-27 00:30:58
问题 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 =

How to Mock an HTTP request in a unit testing scenario in Python

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 23:54:34
问题 I would like to include a Web server for all my test related to HTTP. It doesn't need to be very sophisticated. I would prefer not to be dependent of being online. So I could test some options of my program. Start the server Create a few resources (URI) with appropriate mime types, response code, etc. Run the tests (would be good to not have to start the server for each tests too) Shut down the server. Any hints on this code would be helpful. I tried a few things with BaseHTTPServer but not

Monkey patching a class in another module in Python

余生颓废 提交于 2019-11-26 21:38:46
I'm working with a module written by someone else. I'd like to monkey patch the __init__ method of a class defined in the module. The examples I have found showing how to do this have all assumed I'd be calling the class myself (e.g. Monkey-patch Python class ). However, this is not the case. In my case the class is initalised within a function in another module. See the (greatly simplified) example below: thirdpartymodule_a.py class SomeClass(object): def __init__(self): self.a = 42 def show(self): print self.a thirdpartymodule_b.py import thirdpartymodule_a def dosomething(): sc =

How to multiply functions in python?

我们两清 提交于 2019-11-26 20:20:32
问题 def sub3(n): return n - 3 def square(n): return n * n It's dead easy to compose functions in python: >>> my_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [square(sub3(n)) for n in my_list] [9, 4, 1, 0, 1, 4, 9, 16, 25, 36] Unfortunately, when wanting to use the composition as a key , it's kind of lame: >>> sorted(my_list, key=lambda n: square(sub3(n))) [3, 2, 4, 1, 5, 0, 6, 7, 8, 9] This should really just be sorted(my_list, key=square*sub3) , because heck, function __mul__ isn't used for anything

Monkey-patch Python class

谁说我不能喝 提交于 2019-11-26 19:52:52
I've got a class, located in a separate module, which I can't change. from module import MyClass class ReplaceClass(object) ... MyClass = ReplaceClass This doesn't change MyClass anywhere else but this file. However if I'll add a method like this def bar(): print 123 MyClass.foo = bar this will work and foo method will be available everywhere else. How do I replace the class completely? import module class ReplaceClass(object): .... module.MyClass = ReplaceClass Avoid the from ... import (horrid;-) way to get barenames when what you need most often are qualified names. Once you do things the

Can I patch a Python decorator before it wraps a function?

谁说我不能喝 提交于 2019-11-26 19:52:00
I have a function with a decorator that I'm trying test with the help of the Python Mock library. I'd like to use mock.patch to replace the real decorator with a mock 'bypass' decorator which just calls the function. What I can't figure out is how to apply the patch before the real decorator wraps the function. I've tried a few different variations on the patch target and reordering the patch and import statements, but without success. Any ideas? Decorators are applied at function definition time. For most functions, this is when the module is loaded. (Functions that are defined in other

What does 'Monkey Patching' exactly Mean in Ruby?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 19:51:56
According to Wikipedia, a monkey patch is: a way to extend or modify the runtime code of dynamic languages [...] without altering the original source code. The following statement from the same entry confused me: In Ruby, the term monkey patch was misunderstood to mean any dynamic modification to a class and is often used as a synonym for dynamically modifying any class at runtime. I would like to know the exact meaning of monkey patching in Ruby. Is it doing something like the following, or is it something else? class String def foo "foo" end end The short answer is that there is no "exact"

Is it possible to replace (monkeypatch) PHP functions?

你离开我真会死。 提交于 2019-11-26 19:03:32
You can do this in Python, but is it possible in PHP? >>> def a(): print 1 ... >>> def a(): print 2 ... >>> a() 2 e.g.: <? function var_dump() {} ?> Fatal error: Cannot redeclare var_dump() in /tmp/- on line 1 This is a bit late, but I just want to point out that since PHP 5.3, it is actually possible to override internal functions without using a PHP extension. The trick is that you can redefine an internal PHP function inside a namespace. It's based on the way PHP does name resolution for functions: Inside namespace (say A\B), calls to unqualified functions are resolved at run-time. Here is