monkeypatching

Monkey Patching class derived from ctypes.Union doesn't work

醉酒当歌 提交于 2020-03-16 06:34:57
问题 I am trying to "monkey patch" a class derived from Python ctypes "Union", but I am unable to do so - getting weird errors and sometimes seg-faults. The same thing works quite well when deriving from ctypes "Structure". I have narrowed this to down to the simplest possible test case which I am posting below. I am using Python 3.6.4. I am wondering if I am doing something wrong (or is there a problem with the ctypes "Union" implementation?). Please see the code below and the corresponding

Monkey Patching class derived from ctypes.Union doesn't work

社会主义新天地 提交于 2020-03-16 06:32:32
问题 I am trying to "monkey patch" a class derived from Python ctypes "Union", but I am unable to do so - getting weird errors and sometimes seg-faults. The same thing works quite well when deriving from ctypes "Structure". I have narrowed this to down to the simplest possible test case which I am posting below. I am using Python 3.6.4. I am wondering if I am doing something wrong (or is there a problem with the ctypes "Union" implementation?). Please see the code below and the corresponding

HTTPS request via urllib2 fails behind NTLM proxy

空扰寡人 提交于 2020-01-22 03:12:08
问题 Via Python's urllib2 I try to get data over HTTPS while I am behind a corporate NTLM proxy. I run proxy_url = ('http://user:pw@ntlmproxy:port/') proxy_handler = urllib2.ProxyHandler({'http': proxy_url}) opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler) urllib2.install_opener(opener) f = urllib2.urlopen('https://httpbin.org/ip') myfile = f.read() print myfile but I get as error urllib2.URLError: <urlopen error [Errno 8] _ssl.c:507: EOF occurred in violation of protocol> How can

Chrome extension on an extension?

China☆狼群 提交于 2020-01-14 13:44:08
问题 Is it possible, even in a hacky way, to make a chrome extension for an already existing extension? Or to monkey patch an existing extension? I wish to make some simple usability changes to a non-open source chrome extension. 回答1: Chrome extensions can't access the internal pages of other extensions which have chrome-extension:// address (toolbar popup, page action popup, options page, background page), nor access the content scripts of other extensions. The only case where it's possible to

pymongo + gevent: throw me a banana and just monkey_patch?

旧城冷巷雨未停 提交于 2020-01-11 18:52:41
问题 Quickie here that needs more domain expertise on pymongo than I have right now: Are the "right" parts of the pymongo driver written in python for me to call gevent monkey_patch() and successfully alter pymongo's blocking behavior on r/w within gevent "asynchronous" greenlets? If this will require a little more leg work on gevent and pymongo -- but it is feasible -- I would be more than willing to put in the time as long as i can get a little guidance over irc. Thanks! Note: At small scale

pymongo + gevent: throw me a banana and just monkey_patch?

泪湿孤枕 提交于 2020-01-11 18:52:12
问题 Quickie here that needs more domain expertise on pymongo than I have right now: Are the "right" parts of the pymongo driver written in python for me to call gevent monkey_patch() and successfully alter pymongo's blocking behavior on r/w within gevent "asynchronous" greenlets? If this will require a little more leg work on gevent and pymongo -- but it is feasible -- I would be more than willing to put in the time as long as i can get a little guidance over irc. Thanks! Note: At small scale

dynamically set an instance property / memoized attribute in python?

荒凉一梦 提交于 2020-01-06 07:14:02
问题 I have an existing example class in Python 2.7x class Example(object): a = None b = None c = None and an existing instance anInstance = Example() anInstance.a = 100 anInstance.b = 200 anInstance.c = 300 I've been refactoring/cleaning some code, and it's now known that anInstance.c is an expensive operation that is rarely used. in a perfect world I would just do this : class Example(object): _c = None @property def c(self): if self._c is not None: self._c = DO EXPENSIVE STUFF HERE return self.

How to get object itself in custom Object.prototype.xxx function?

穿精又带淫゛_ 提交于 2020-01-05 08:51:47
问题 Object.prototype.getB = function() { // how to get the current value a return a.b; }; const a = {b: 'c'}; a.getB(); As you can see, I want to make a function to all Object value. And I need to get the object value in this function then do something. 回答1: Monkey Patching What you want to do is called monkey patching — you mutate a built-in prototype. There are many wrong ways to do it, but I’ll demonstrate a way that is currently the most correct way. Methods In your case, the function body

How do I monkey-patch ruby's URI.parse method

两盒软妹~` 提交于 2020-01-03 08:39:08
问题 Some popular blog sites typically use square brackets in their URLs but ruby's built-in URI.parse() method chokes on them, raising a nasty exception, as per: http://redmine.ruby-lang.org/issues/show/1466 I'm trying to write a simple monkey-patch that gracefully handles URLs with the square bracket. The following is what I have so far: require 'uri' module URI def self.parse_with_safety(uri) safe_uri = uri.replace('[', '%5B') safe_uri = safe_uri.replace(']', '%5D') URI.parse_without_safety

Where is the best place to add methods to the Integer class in Rails?

断了今生、忘了曾经 提交于 2020-01-01 03:25:06
问题 Where is the best place to add a method to the integer class in Rails? I'd like to add a to_meters and to_miles methods. 回答1: If you have your heart set on mucking with the Numeric (or integer, etc) class to get unit conversion, then at least do it logically and with some real value. First, create a Unit class that stores the unit type (meters,feet, cubits, etc.) and the value on creation. Then add a bunch of methods to Numeric that correspond to the valid values unit can have: these methods