Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72991

Re: Decorating one method of a class C with another method of class C?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'class,': 0.07; 'socket': 0.07; 'dan': 0.09; 'decorator': 0.09; 'subject:method': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"list': 0.16; '*a,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'operation,': 0.16; 'simple.': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'putting': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'skip:" 30': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; 'assert': 0.31; 'class': 0.32; 'probably': 0.32; 'regular': 0.32; "i'd": 0.34; 'subject:with': 0.35; 'connection': 0.35; 'operations': 0.35; 'received:google.com': 0.35; 'subject:one': 0.36; 'method': 0.36; "i'll": 0.36; 'subject:?': 0.36; 'operating': 0.37; 'configured': 0.38; 'work?': 0.38; 'simple': 0.61; 'making': 0.63; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=mWmLMuYxLRm+j8BW8njKZli+1QfGdQMlSzfIz3Czfzo=; b=IxbIZC23lfLdZadJG6GtJ7fTbgHe3CDjrqSBZWNKHpgWprF3hCYFB26YTtP51e3m8A 1sTMk7JtkZnRm7Mf5J5w/xd+CZ0zhwuOXmCWO67A8Y4CpnRS1xMrwKarWdYvKkLuGZ/m FFj2KyrBIKcmFgBR68K4acFb3FDFVIXmaUbORPyNytaQmGl8R5f2ekHfj7pILqgBTXxs QSTjnTiDobmjTgl+KJWQwLkn7RtEmlUh3wccR2XPGOJ4YfQDtSGWsfBbrueQ+cQB4rDr fLMfJa0jR6fIDWRXggC4eqncTMH8J4L2tKJ3uOHU2xNsRCUpxZb7hmUTZ5W467a1O7gc MMrw==
MIME-Version 1.0
X-Received by 10.220.44.20 with SMTP id y20mr3029956vce.60.1402251674799; Sun, 08 Jun 2014 11:21:14 -0700 (PDT)
In-Reply-To <CAGGBd_qiaw5jahp8QTkc0R+pz3RRvo6c_QTgXZ=ZdrOnvEv++w@mail.gmail.com>
References <CAGGBd_pM5DGp85LVfeQgYwqS8DU8nuQL8Q8G5EzfQ5BX26fa-w@mail.gmail.com> <85lht91s30.fsf@benfinney.id.au> <CAGGBd_qiaw5jahp8QTkc0R+pz3RRvo6c_QTgXZ=ZdrOnvEv++w@mail.gmail.com>
Date Mon, 9 Jun 2014 04:21:14 +1000
Subject Re: Decorating one method of a class C with another method of class C?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10899.1402251682.18130.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402251682 news.xs4all.nl 2920 [2001:888:2000:d::a6]:34034
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72991

Show key headers only | View raw


On Mon, Jun 9, 2014 at 4:04 AM, Dan Stromberg <drsalists@gmail.com> wrote:
> I have a class that's operating on a socket.
>
> I'd like to have simple operations on that socket like "list
> configured hosts", "allow connection to host", etc.  And I'd like them
> to be decorated with "reconnected_to_server_if_needed".
>
> I'll probably end up putting a try/except in each simple operation,
> making them a bit less simple.

Can you have the decorator outside the class, calling a regular method
to do its main work?

def recon_if_needed(f):
    def inner(self, *a, **kw):
        if self.disconnected: self.connect()
        return f(self, *a, **kw)
    return inner
class SocketWorker:
    @recon_if_needed
    def send_packet(self, packet):
        assert not self.disconnected

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Decorating one method of a class C with another method of class C? Chris Angelico <rosuav@gmail.com> - 2014-06-09 04:21 +1000

csiph-web