I've read that it is possible to add a method to an existing object (e.g. not in the class definition) in python, I think this is called Monkey Patching (or in some cases Duck Punching). I understand that it's not always a good decision to do so. But, how might one do this?
And if you don't know python, can your language of choice do t...
I've had to do some introspection in python and it wasn't pretty:
name = sys._getframe(1).f_code
name = "%s:%d %s()" %(os.path.split(name.co_filename)[1],name.co_firstlineno,name.co_name)
To get something like
foo.py:22 bar() blah blah
In our debugging output.
I'd ideally like to prepend anything to stderr with this sort o...
a colleague pointed me the other day to BCEL which , as best I can tell from his explanation and a quick read, a way to modify at run time the byte code. My first thought was that it sounded dangerous, and my second thought was that it sounded cool. Then I gave it some more thought and I recalled the codinghorror post on monkey-patchin...
In the Flex framework a custom preloader can be used while the site is loading.
In the Adobe docs it specifies that 'the progress bar [preloader] is displayed if less than half of the application is downloaded after 700 milliseconds of downloading.'
However I ALWAYS want the preloader to appear instantly since I know that 95% of our us...
I want to load the flex framework as an RSL (SWZ, using player caching) but I need to monkey patch a couple of bug fixes in the framework.
A number of forums suggest this is not possible. Has anyone gotten this to work?
...
I am still very new to Ruby (reading through the Pickaxe and spending most of my time in irb), and now that I know it's possible to patch classes in Ruby, I'm wondering when it's acceptable to do so, specifically whether it's acceptable to patch Ruby's base classes. For example: I answered another Ruby question here where the poster want...
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 ...
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
...
When I reading source code of Beast, I found a lot of code like this:
<%= 'Password'[:password_title] %>
It seems like a call to [] method with Symbol as input parameter to a String to me, but I didn't find such type of parameter of String [] method in the ruby API. What is this means?
thanks in advance.
...
This is more general question then language-specific, altho I bumped into this problem while playing with python ncurses module. I needed to display locale characters and have them recognized as characters, so I just quickly monkey-patched few functions / methods from curses module.
This was what I call a fast and ugly solution, even i...
I am writing a Django app, and I would like an account to be created on our Google Apps hosted email using the Provisioning API whenever an account is created locally.
I would solely use signals, but since I would like the passwords to be synchronized across sites, I have monkeypatched User.objects.create_user and User.set_password usin...
I'm trying to monkey-patch (duck-punch :-) a LWP::UserAgent instance, like so:
sub _user_agent_get_basic_credentials_patch {
return ($username, $password);
}
my $agent = LWP::UserAgent->new();
$agent->get_basic_credentials = _user_agent_get_basic_credentials_patch;
This isn't the right syntax -- it yields:
Can't modify non-lval...
I'm monkey patching a package using a technique given at the beginning of "How can I monkey-patch an instance method in Perl?". The problem that I'm running into is that the original subroutine used a package-level my variable which the patched subroutine appears not to have access to, either by full path specification or implicit use.
...
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
...
Apologies for the recursive nature of this question but the chosen answer to a question on SO got me questioning my understanding of reflection.
I thought reflection was mainly about querying the internal happenings of a program while it's running. The example given in this response patches Ruby's built-in Integer class.
Isn't this m...
I'm trying to monkey patch a Perl class: I want to change the behavior of an existing method.
This node on perlmonks shows how to add a function to an existing class. I found that this pattern can also be used to provide a new implementation for an existing function.
However, I'd like to know how to call the original function.
I'm lo...
I am still new to Ruby and basically just writing my first micro-program after finishing Cooper's book. I was pointed to the direction of avoiding monkey patching but the problem is I don't know what are the alternatives to achieve the same behavior.
Basically, I want to add a new method that is accessible by every string object. The obv...
I've heard a lot of oohs and aahs about how monkey patching is so cool or monkey patching is the worst thing on earth. I have the idea that it's not good if you write some code and then someone else changes its functionality to do something else. But I'm looking for a concrete example of a situation where this could really hurt you. I me...
In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary concerns.
However, I rarely hear the same arguments made in the context of Python although it is also permitted in the Python language.
Why this distinction?
...
Before I set about to writing this myself, has anyone seen a ruby implementation of the following behavior?
puts 7.nextprime(); #=> 11
puts 7.previousprime(); #=> 5
puts 7.isprime(); #=> true
Obviously this kind of thing would be ugly for large numbers but for integers never exceeding a few thousand (the common instance fo...