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


Groups > comp.lang.python > #60381

Re: Implement logic on object.attribute and object.attribute()

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <glicerinu@gmail.com>
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; '"this': 0.03; '"""': 0.07; 'attribute': 0.07; '*args,': 0.09; 'http': 0.09; 'subject:()': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'wrote': 0.14; '(put': 0.16; '**kwargs):': 0.16; '24,': 0.16; '@property': 0.16; 'callable.': 0.16; 'chris,': 0.16; 'instance"': 0.16; 'lambda:': 0.16; 'magic': 0.16; 'name)': 0.16; 'name):': 0.16; 'overridden': 0.16; 'resource,': 0.16; 'should.': 0.16; 'tying': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'cc:addr:python.org': 0.22; 'string,': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; '25,': 0.31; '>>>>': 0.31; 'implicit': 0.31; 'marc': 0.31; 'steven': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'interface': 0.32; 'skip:_ 10': 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'skip:j 20': 0.36; 'doing': 0.36; "didn't": 0.36; 'thanks': 0.36; "i'll": 0.36; 'nov': 0.38; 'pm,': 0.38; 'resource': 0.38; 'functional': 0.39; 'realize': 0.39; 'skip:* 10': 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'request.': 0.70; 'subject:skip:o 10': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=FtBynmGWO9cyuUg0M9B+JilCxMDAELTOHtB8gSItgY0=; b=X7IToHzDGrbUciuW3bmwfs7rbrLzafEOq4Gtz2p0DaWF+GhGkSsmWiVeIooRKzTQJD ch2MS24BzexP6ePeojy6Aqebz64mYNkbk65PeTGVnqMHfCHlcpfLmX1nDPtOvUIabWyU T/D2Dfxx8wzgx7uZ+rTeOX74eATL2xpYXoEv2BGJnKL+wbq4/1zublppzaT3aFNOwPAz 0J0xT/Vl7C6jFwspa9yyz6ERZ77geay+ScZG1PXElcMzIILQk47btJsGrHX/nhOfMBk4 KoDhvFbC9R3WEsKZb7WwO6k4cp5IArbvRACvb7Sj0NKWSq5+v9x8uMs5MzbPdQ6HQdhv 5Qgw==
X-Received by 10.220.192.198 with SMTP id dr6mr1232495vcb.19.1385304549902; Sun, 24 Nov 2013 06:49:09 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CAPTjJmoS44uVhBpb73ev49_rYQmyLLrX7n77YmJBcOCJ1xzp0w@mail.gmail.com>
References <CA+DCN_uqn5OukuZ5QWWjJCUTksaFw50L03ZU7uqdm4a6ZOn7CA@mail.gmail.com> <mailman.3128.1385298299.18130.python-list@python.org> <529202e3$0$29993$c3e8da3$5496439d@news.astraweb.com> <CA+DCN_uGQt1dj+Ab+CkA_8oPD8DG2c1YVGUfWbaRJAJ6+hiRBQ@mail.gmail.com> <CAPTjJmoS44uVhBpb73ev49_rYQmyLLrX7n77YmJBcOCJ1xzp0w@mail.gmail.com>
From Marc Aymerich <glicerinu@gmail.com>
Date Sun, 24 Nov 2013 15:48:49 +0100
Subject Re: Implement logic on object.attribute and object.attribute()
To Chris Angelico <rosuav@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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.3139.1385304915.18130.python-list@python.org> (permalink)
Lines 94
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385304915 news.xs4all.nl 15992 [2001:888:2000:d::a6]:45658
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60381

Show key headers only | View raw


On Sun, Nov 24, 2013 at 3:37 PM, Chris Angelico <rosuav@gmail.com> wrote:
> On Mon, Nov 25, 2013 at 1:16 AM, Marc Aymerich <glicerinu@gmail.com> wrote:
>> ...     def do_get(self):
>> ...         # Do a HTTP GET request.
>> ...         return "Get stuff"
>> ...     def do_put(self):
>> ...         # Do a HTTP PUT request.
>> ...         return "Put stuff"
>
> To make this a bit more realistic, try this instead - tying in with
> what I said in response to Roy:
>
> class CallableString(str):
>     # Like a string, but callable.
>     def function(self):
>         raise NotImplementedError(
>                 "this must be overridden on the instance"
>                 )
>     def __call__(self):
>         return self.function()
>
>
> class Magic_HTTP_Thing:
>     @property
>     def attribute(self):
>         result = CallableString(self.do_get())
>         result.function = lambda: self.do_put()
>         return result
>     def do_get(self):
>         # Do a HTTP GET request.
>         print("Doing the GET call, please wait...")
>         time.sleep(1)
>         return "Get stuff"
>     def do_put(self):
>         # Do a HTTP PUT request.
>         print("Doing the PUT call, please wait...")
>         time.sleep(1)
>         return "Put stuff"
>
> (PUT or POST, makes no difference; I think you were looking for POST,
> but Steven wrote PUT here so I'll use that for simplicity)
>
>>>> Magic_HTTP_Thing().attribute()
> Doing the GET call, please wait...
> Doing the PUT call, please wait...
> 'Put stuff'
>
> And that's what you don't want happening. Your PUT / POST calls are
> going to take twice as long as they should.

Thanks Chris,
didn't realize about the implicit GET when calling the attribute :(

I think I'll put the get call on __repr__, __str__ and __getattr__,
something like

class HTTPAttribute(object):
    """ functional endpoint representation """
    def __repr__(self):
        self._retrieve()
        return json.dumps(self.__dict__)

    def __str__(self):
        self._retrieve()
        return json.dumps(self.__dict__)

    def __init__(self, resource, uri):
        self._resource = resource
        self.uri = uri

    def __call__(self, *args, **kwargs):
        return self._resource._api.post(self.uri, *args, **kwargs).content

    def __getattr__(self, name):
        self._retrieve()
        if hasattr(self, name):
            return getattr(self, name)
        raise AttributeError("'%s' object has no attribute '%s'" %
(str(type(self)), name))

    def _retrieve(self):
        resource = self._resource._api.retrieve(self.uri)
        for key, value in resource._data.iteritems():
            setattr(self, key, value)


and that's it,


But still I'll reconsider an interface with less magic  :P


-- 
Marc

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


Thread

Re: Implement logic on object.attribute and object.attribute() Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-11-24 05:04 -0800
  Re: Implement logic on object.attribute and object.attribute() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-24 13:45 +0000
    Re: Implement logic on object.attribute and object.attribute() Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-11-24 06:04 -0800
    Re: Implement logic on object.attribute and object.attribute() Chris Angelico <rosuav@gmail.com> - 2013-11-25 01:13 +1100
      Re: Implement logic on object.attribute and object.attribute() Roy Smith <roy@panix.com> - 2013-11-24 09:21 -0500
        Re: Implement logic on object.attribute and object.attribute() Chris Angelico <rosuav@gmail.com> - 2013-11-25 01:31 +1100
    Re: Implement logic on object.attribute and object.attribute() Chris Angelico <rosuav@gmail.com> - 2013-11-25 01:13 +1100
    Re: Implement logic on object.attribute and object.attribute() Marc Aymerich <glicerinu@gmail.com> - 2013-11-24 15:16 +0100
    Re: Implement logic on object.attribute and object.attribute() Chris Angelico <rosuav@gmail.com> - 2013-11-25 01:37 +1100
      Re: Implement logic on object.attribute and object.attribute() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-24 15:11 +0000
    Re: Implement logic on object.attribute and object.attribute() Marc Aymerich <glicerinu@gmail.com> - 2013-11-24 15:34 +0100
    Re: Implement logic on object.attribute and object.attribute() Marc Aymerich <glicerinu@gmail.com> - 2013-11-24 15:48 +0100
    Re: Implement logic on object.attribute and object.attribute() Chris Angelico <rosuav@gmail.com> - 2013-11-25 01:57 +1100

csiph-web