Questions tagged [monkeypatching]

Dynamically modifying run-time behavior by replacing program elements with new program elements

Monkeypatching, in late-bound languages, refers to modifying run-time behavior by replacing program elements with new program elements.

Examples include replacing a method on a class with a new function, or patching a member of a class.

The term may have a pejorative connotation, as the technique is usually used on third-party code.

For more discussion, see "Monkeypatching for Humans" on Coding Horror.

949 questions
52
votes
3 answers

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

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 …
alex
  • 1,900
  • 3
  • 24
  • 34
42
votes
2 answers

Conjugate transpose operator ".H" in numpy

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…
benpro
  • 4,325
  • 4
  • 19
  • 17
39
votes
1 answer

Dynamically add a property or method to an object in groovy

Is it possible to add a property or a method to an object dynamically in Groovy? This is what I have tried so far: class Greet { def name Greet(who) { name = who[0].toUpperCase() + [1..-1] } def salute() { println "Hello $name!" } } g = new…
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76
38
votes
4 answers

Extending builtin classes in python

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.
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
35
votes
4 answers

Using Celery on processes and gevent in tasks at the same time

I'd like to use Celery as a queue for my tasks so my web app could enqueue a task, return a response and the task will be processed meanwhile / someday / ... I build a kind of API, so I don't know what sort of tasks will be there in advance - in…
Honza Javorek
  • 8,566
  • 8
  • 47
  • 66
34
votes
2 answers

Advantages of Using MethodType in Python

What are the advantages of using MethodType from the types module? You can use it to add methods to an object. But we can do that easily without it: def func(): print 1 class A: pass obj = A() obj.func = func It works even if we delete func…
Existent
  • 697
  • 1
  • 7
  • 14
34
votes
1 answer

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

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…
karlcow
  • 6,977
  • 4
  • 38
  • 72
33
votes
2 answers

Recommended approach to monkey patching a class in ruby

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…
stantona
  • 3,260
  • 2
  • 24
  • 28
32
votes
4 answers

How to multiply functions in python?

def sub3(n): return n - 3 def square(n): return n * n It's 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, to use the…
wim
  • 338,267
  • 99
  • 616
  • 750
31
votes
8 answers

How can I modify the XMLHttpRequest responsetext received by another function?

I am trying to modify the responseText received by a function that I cannot modify. This function creates a XMLHttpRequest that I can attach to, but I have been unable to "wrap" the responseText in a way that allows me to modify the content before…
Shadow
  • 4,168
  • 5
  • 41
  • 72
29
votes
6 answers

Is "monkey patching" really that bad?

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…
maerics
  • 151,642
  • 46
  • 269
  • 291
29
votes
2 answers

Why python's monkeypatch doesn't work when importing a class instead of a module?

I was having issues while using the code of the accepted answer here. The code works depending on how I do the import of datetime. Why is that? Is it possible to mock it so it works both ways? I am using Python 3.4. The following code illustrates…
rgargente
  • 1,821
  • 2
  • 19
  • 30
27
votes
7 answers

Monkey patching Devise (or any Rails gem)

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…
Yarin
  • 173,523
  • 149
  • 402
  • 512
26
votes
2 answers

How do I import the pytest monkeypatch plugin?

I want to use the pytest monkeypatch plugin, but I can't figure out how to import it. I've tried: import monkeypath import pytest.monkeypatch from pytest import monkeypatch
John Bachir
  • 22,495
  • 29
  • 154
  • 227
25
votes
8 answers

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

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…
Phillip B Oldham
  • 18,807
  • 20
  • 94
  • 134
1
2
3
63 64