Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35052
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!rt.uk.eu.org!feed.xsnews.nl!border-2.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; '#if': 0.05; '*args,': 0.07; 'function,': 0.07; 'lines.': 0.07; 'wrapper': 0.07; 'python': 0.09; '**kwargs):': 0.09; 'alternatives': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tends': 0.09; 'valueerror': 0.09; 'def': 0.10; '__lt__(self,': 0.16; 'conditional': 0.16; 'original:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:patch': 0.16; 'subject:possible': 0.16; 'tweak': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'all,': 0.21; '"",': 0.22; 'stopping': 0.22; 'patch': 0.24; 'raise': 0.24; 'idea': 0.24; 'pass': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'replace': 0.27; 'there.': 0.28; 'header:X-Complaints-To:1': 0.28; 'actual': 0.28; 'run': 0.28; 'arguments.': 0.29; 'factor': 0.29; 'marc': 0.29; 'subject:like': 0.29; 'class': 0.29; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'function.': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'changed': 0.34; 'conditions.': 0.35; 'false': 0.35; 'subject:?': 0.35; 'received:org': 0.36; 'method': 0.36; 'should': 0.36; 'possible': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'copying': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'apply': 0.39; 'header:Received:5': 0.40; 'kind': 0.61; 'dear': 0.66; 'subject:this': 0.84; 'x):': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Is it possible monkey patch like this? |
| Date | Tue, 18 Dec 2012 13:35:54 +0100 |
| Organization | None |
| References | <bbfb8154-e283-4961-9680-9d819bf00d01@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b4fd.dip.t-dialin.net |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1017.1355834069.29569.python-list@python.org> (permalink) |
| Lines | 71 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1355834069 news.xs4all.nl 6938 [2001:888:2000:d::a6]:43014 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:35052 |
Show key headers only | View raw
Marc Aymerich wrote:
> Dear all,
> I want to monkey patch a method that has lots of code so I want to avoid
> copying all the original method for changing just two lines. The thing is
> that I don't know how to do this kind of monkey patching.
>
> Consider the following code:
>
> class OringinalClass(object):
> def origina_method(self, *args, **kwargs):
> ...
> if some_condition(): # This condition should be changed
> raise SomeException
> ...
> if some_condition():
> ...
> #if some_condition(local_variable): # This condition should be
> #added
> # raise SomeException
> ...
>
>
> Is it possible to tell Python to run the original method without stopping
> when an exception is raised?
No.
> so I can catch them on a wrapper method and
> apply my conditional there.
>
> Any other idea on how to monkey patch those two conditionals ?
One of the cleanest alternatives is to factor out the condition in the
original class and then use a subclass:
class Original:
def method(self, *args, **kw):
self.check_condition(...)
...
def check_condition(self, ...):
if condition:
raise SomeException
class Sub(Original):
def check_condition(self, ...):
pass
If you insist on monkey-patching possible solutions depend on the actual
conditions. If some_condition() is a function, replace that function. If it
is actually an expression tweak the arguments. E. g:
>>> class Original:
... def method(self, x):
... if x < 0: raise ValueError
... print x * x
...
>>> Original().method(-2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in method
ValueError
>>> class Int(int):
... def __lt__(self, other): return False # a blunt lie
...
>>> Original().method(Int(-2))
4
This tends to get complex quickly, so in the long run you will not be happy
with that approach...
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is it possible monkey patch like this? Marc Aymerich <glicerinu@gmail.com> - 2012-12-18 02:26 -0800 Re: Is it possible monkey patch like this? Chris Angelico <rosuav@gmail.com> - 2012-12-18 21:34 +1100 Re: Is it possible monkey patch like this? Peter Otten <__peter__@web.de> - 2012-12-18 13:35 +0100 Re: Is it possible monkey patch like this? Terry Reedy <tjreedy@udel.edu> - 2012-12-18 11:49 -0500 Re: Is it possible monkey patch like this? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-19 00:54 +0000
csiph-web