Storing a lambda function in the Google App Engine Datastore (python 2.7) -


i creating bunch of foo objects in gae.

i want each foo have function called fooify(). calling fooify(object) should return object, transformed in way.

for example, fooify([2,3]) might return:

  • [1,2] (fooify = lambda x: [n-1 n in x])
  • [2,3] (fooify = lambda x: x)
  • [2,4] (fooify = lambda x: [x[0],x[1]+1])

i want able define fooify when create objects, preferably like:

foo1=foo() foo1.fooify=lambda x: [n^2 n in x] foo1.put() 

the foo class looks like:

from google.appengine.ext import ndb class foo(ndb.model):     name=ndb.stringproperty(default="generic foo", indexed=true)     fooify=ndb.somethingproperty(indexed=false)    # contains function 

i know isn't datastore made for, still it. option can see following:

class foo(ndb.model):     name=ndb.stringproperty(default="generic foo", indexed=true)     fooify_str=ndb.stringproperty(indexed=false)    # contains "lambda x: [n+3 n in x]"      def fooify(self,obj):         func = eval(self.fooify_str)         return func(obj) 

which uses eval() create main part of fooify function. don't want use eval() because seems sort of hack-ish, , means have write fooify_str functions strings, (annoying - no syntax highlighting, etc.)

is there way around i'm missing?

using polymodel may suit needs if different fooify functions organized in class hierarchy.

you can define model:

class foo(polymodel.polymodel):   name = ndb.stringproperty()  class myfirstfoo(foo):   def fooify(self, obj):     ...  class mysecondfoo(foo):   def fooify(self, obj):     ... 

ndb store these under foo kind, query on foo return results (i.e. foo.query().filter(...). however, keeps track of class hierarchy, when load entity knows myfirstfoo versus mysecondfoo.


Comments