On Mon, Sep 7, 2009 at 3:25 PM, (jpgerek)<[email protected]> wrote:
>
> Hi, I'm new with python and I having problems to implement the typical
> memcache pattern.
>
> I wan't to make a decorator of the most common memcache pattern so I
> don't have to implement the logic everytime. My intention is having a
> function that receives the key to get and a callback with the
> datastore gets, in case the memcache key expires, it's evicted or
> whatever.
>
> This is what I have now:
>
> class MC():
>   �...@staticmethod
>    def get( key, get_data_callback ):
>        data_set = memcache.get( key )
>        if data_set == None:
>            data_set = get_data_callback()
>            memcache.set( key, data_set )
>        return data_set
>
> I'm having problems with the callback when some params are required.
> For instance:
>
> class DB():
>   �...@staticmethod
>    def get_user( id ):
>          return User().all().filter('id =', id').fetch(1)
>
> def get_user( id ):
>    return MC.get( key, lambda id: DB.get_user( id ) )
>
> The error I get when I call 'get_user(1)' is something like:
>
> TypeError: <lambda>() takes exactly 1 argument (0 given)
>
> Though it works in this case:
>
> def get_user_one():
>     return User().all().filter('id = 1').fetch(1)
>
> It seems to be something with the params, they are lost, could it be
> solved with closures? other approach?

Look at this.  You pass a function/lambda to MC.get, this
function/lambda takes one parameter.  This function/lambda is passed
as the get_data_callback argument, and you then call get_data_callback
in that MC.get function with no parameters.  So why are you calling a
function that you have setup to take one parameter, with no
parameters?

Your
  lambda id: DB.get_user( id )
as you can see, takes one parameter, and it is required.  If you want
the passed in id to only be use, then change it to:
  lambda: DB.get_user( id )
or if you want the passed in id to be default, but overridable, then
change it to:
  lambda id=id: DB.get_user( id )
But the first one *requires* a single argument.

You might want to learn a bit more python first, this has nothing to
do with appengine.  :)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to