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

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 2013-11-24 15:48 +0100
Subject Re: Implement logic on object.attribute and object.attribute()
Newsgroups comp.lang.python
Message-ID <mailman.3139.1385304915.18130.python-list@python.org> (permalink)

Show all headers | 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