monkeypatching

How can I monkey-patch an instance method in Perl?

血红的双手。 提交于 2019-11-28 04:33:20
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-lvalue subroutine call at [module] line [lineno]. As I recall (from Programming Perl ), dispatch lookup is performed dynamically based on the blessed package ( ref($agent) , I believe), so I'm not sure how instance monkey patching would even work without affecting the

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

↘锁芯ラ 提交于 2019-11-28 02:59:23
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 successful yet. nosetests command seems to wait indefinitely. import unittest from foo import core class

How to multiply functions in python?

我怕爱的太早我们不能终老 提交于 2019-11-27 20:26:25
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 else anyway: >>> square * sub3 TypeError: unsupported operand type(s) for *: 'function' and 'function'

Convert Google Analytics cookies to Local/Session Storage

佐手、 提交于 2019-11-27 18:12:24
UPDATE http://jsfiddle.net/musicisair/rsKtp/embedded/result/ Google Analytics sets 4 cookies that will be sent with all requests to that domain (and ofset its subdomains). From what I can tell no server actually uses them directly ; they're only sent with __utm.gif as a query param. Now, obviously Google Analytics reads, writes and acts on their values and they will need to be available to the GA tracking script. So, what I am wondering is if it is possible to: rewrite the __utm* cookies to local storage after ga.js has written them delete them after ga.js has run rewrite the cookies FROM

Monkey patch XMLHTTPRequest.onreadystatechange

拈花ヽ惹草 提交于 2019-11-27 16:35:58
问题 How would go about monkey patching the XMLHTTPRequest 's onreadystatechange function. I'm trying to add a function that would be called when every ajax request made from a page come back. I know this sounds like a terrible idea, but the use case is quite peculiar. I want to use a certain SDK with a console (jqconsole) but show status and results from ajax calls within the console without modifying the external SDK. I've looked at this post which had great info, but nothing on monkey patching

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

流过昼夜 提交于 2019-11-27 14:00:50
问题 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? 回答1: EDIT: This solution only works for Rails 3+ since it's dependent on some functionality in

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

可紊 提交于 2019-11-27 11:58:15
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 the original function receives it. Here's the full original function: function Mj(a, b, c, d, e) { function k() { 4 == (m && 'readyState' in m ? m.readyState : 0) && b && ff(b) (m) } var m = new XMLHttpRequest; 'onloadend' in m ? m.addEventListener('loadend', k, !1) : m.onreadystatechange = k; c = ('GET').toUpperCase(); d = d || ''; m.open(c, a, !0);

How to monkey patch Django?

白昼怎懂夜的黑 提交于 2019-11-27 09:05:13
I came upon this post on monkey patching Django: from django.contrib.auth.models import User User.add_to_class('openid', models.CharField(max_length=250,blank=True)) def get_user_name(self): if self.first_name or self.last_name: return self.first_name + " " + self.last_name return self.username User.add_to_class("get_user_name",get_user_name) I understand that this isn't ideal and it's better to add fields and functions to User through a separate model Profile . With that said, I just want to understand how this would work: Where would I put the monkey patching code? When is the code run --

How does one monkey patch a function in python?

隐身守侯 提交于 2019-11-27 06:24:19
I'm having trouble replacing a function from a different module with another function and it's driving me crazy. Let's say I have a module bar.py that looks like this: from a_package.baz import do_something_expensive def a_function(): print do_something_expensive() And I have another module that looks like this: from bar import a_function a_function() from a_package.baz import do_something_expensive do_something_expensive = lambda: 'Something really cheap.' a_function() import a_package.baz a_package.baz.do_something_expensive = lambda: 'Something really cheap.' a_function() I would expect to

Convert Google Analytics cookies to Local/Session Storage

早过忘川 提交于 2019-11-27 04:15:53
问题 UPDATE http://jsfiddle.net/musicisair/rsKtp/embedded/result/ Google Analytics sets 4 cookies that will be sent with all requests to that domain (and ofset its subdomains). From what I can tell no server actually uses them directly ; they're only sent with __utm.gif as a query param. Now, obviously Google Analytics reads, writes and acts on their values and they will need to be available to the GA tracking script. So, what I am wondering is if it is possible to: rewrite the __utm* cookies to