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


Groups > comp.lang.python > #60371

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

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'attribute': 0.07; 'method.': 0.07; 'badly': 0.09; 'http': 0.09; 'solution,': 0.09; 'subject:()': 0.09; 'yeah,': 0.09; 'api': 0.11; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'mostly': 0.14; '24,': 0.16; '@property': 0.16; 'api,': 0.16; 'behave': 0.16; 'callable.': 0.16; 'executed.': 0.16; 'get,': 0.16; 'instance"': 0.16; 'lambda:': 0.16; 'overridden': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.18; 'trying': 0.19; 'not,': 0.20; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'string,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'post': 0.26; 'skip:" 20': 0.27; 'header:In-Reply- To:1': 0.27; 'rest': 0.29; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; "d'aprano": 0.31; 'marc': 0.31; 'steven': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'interface': 0.32; 'quite': 0.32; 'skip:_ 10': 0.34; 'except': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'accessing': 0.36; 'possible': 0.36; 'hi,': 0.36; 'nov': 0.38; 'pm,': 0.38; 'first': 0.61; 'information': 0.63; 'our': 0.64; 'map': 0.64; 'request.': 0.70; 'behavior': 0.77; '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=2OhiWo52XYrTsVj0K36ewP3Dqn2eR0yy3xzOJAFx1R0=; b=E/shW9OjxUg1oKWmQiQ3T+nex4PFwVrpzebS4JJtIDCvzcbhB/2HF/5CBaKSmDblyh 4gFCJ0sX5p4vn8YG7x4VkeZw5iSYsFTNJgoxxItCFRbs++wcqCN4HJ8MtnjyWltXiyuS 6MPDEqk6rXXkTwSPinsTIrYLStYSlNXz2Kh8sTwN+Rv5daToDxNZOQZTJspvmClDN5cB zKdMb0c3g2S6D9+rqBaQVyWvnIdXSM3Vs29lukBTX00I4xUYYX8EFWCOMEUCMaHfkv9K l9Y2+Sr95EvP98qehSJBw8LNPE71xiDKF5R+yYL9BxYd1a71vf3fSltEo6PWgTs3hmp6 6jww==
X-Received by 10.52.171.79 with SMTP id as15mr18132831vdc.1.1385302618386; Sun, 24 Nov 2013 06:16:58 -0800 (PST)
MIME-Version 1.0
In-Reply-To <529202e3$0$29993$c3e8da3$5496439d@news.astraweb.com>
References <CA+DCN_uqn5OukuZ5QWWjJCUTksaFw50L03ZU7uqdm4a6ZOn7CA@mail.gmail.com> <mailman.3128.1385298299.18130.python-list@python.org> <529202e3$0$29993$c3e8da3$5496439d@news.astraweb.com>
From Marc Aymerich <glicerinu@gmail.com>
Date Sun, 24 Nov 2013 15:16:38 +0100
Subject Re: Implement logic on object.attribute and object.attribute()
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
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.3133.1385302626.18130.python-list@python.org> (permalink)
Lines 87
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385302626 news.xs4all.nl 15950 [2001:888:2000:d::a6]:42521
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60371

Show key headers only | View raw


On Sun, Nov 24, 2013 at 2:45 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Sun, 24 Nov 2013 05:04:16 -0800, Devin Jeanpierre wrote:
>
>> On Sun, Nov 24, 2013 at 4:52 AM, Marc Aymerich <glicerinu@gmail.com>
>> wrote:
>>> Hi,
>>> I'm playing with python internals to make objects behave like this:
>>>
>>> if I access to "object.attribute" I want to return the result of an
>>> HTTP GET request. However if i call "object.attribute()" I want an HTTP
>>> POST request to be executed.
>>
>> Uh oh. What you want is impossible. You cannot call an attribute without
>> first accessing it. :(
>
> Not quite impossible. All you need is an object that behaves like a
> string, except it has a __call__ method. Here's a sketch of a solution,
> completely untested.
>
>
> 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 = CallableStr(self.do_get())
>         result.function = lambda: self.do_put()
>     def do_get(self):
>         # Do a HTTP GET request.
>         return "Get stuff"
>     def do_put(self):
>         # Do a HTTP PUT request.
>         return "Put stuff"


OMG steven, it actually works :)

>>> 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.
...         return "Get stuff"
...     def do_put(self):
...         # Do a HTTP PUT request.
...         return "Put stuff"
...
>>>
>>> Magic_HTTP_Thing().attribute
'Get stuff'
>>> Magic_HTTP_Thing().attribute()
'Put stuff'

>
> Possible or not, it doesn't seem like a reasonable API to me.

yeah, this is a "corner case" of our REST API, I have some badly
design endpoints that mostly behave like functions, but some of them
also contain state information that you can GET, I was trying to map
this behavior to python objects and this interface is the best that
occurred to me :)


-- 
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