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

Marc Aymerich glicerinu at gmail.com
Sun Nov 24 15:48:49 CET 2013


On Sun, Nov 24, 2013 at 3:37 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Mon, Nov 25, 2013 at 1:16 AM, Marc Aymerich <glicerinu at 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



More information about the Python-list mailing list