Search code examples
pythongoogle-app-enginewebapp2

Storing Dictionary in GAE with PickleProperty


I have a python dictionary i'm trying to store in GAE Gql Datastore through pickle property.

I tried to follow this question and answer, Store a list of dictionaries in GAE

but it's not working for my code below.. I get an error that there's a problem with my list

i setup the pickle property and data model class as follows,

class PickleProperty(db.Property):
    def get_value_for_datastore(self, model_instance):
        value = getattr(model_instance, self.name, None)
        return pickle.dumps(value)

    def make_value_from_datastore(self, value):
        return pickle.mloads(value)

class MDB(db.Model):
    Name = db.StringProperty(required=True)
    Times = PickleProperty()
    created = db.DateTimeProperty(auto_now_add = True)

I have a function in my code that inserts the data and reads it out which is

def m_time_manage(m=""):

    if not m:
        r = db.GqlQuery("select * from MDB")
    else:
        #find specific masjid requested
        r = db.GqlQuery("select * from MDB where Name = %s" % (s))

    #if masjid time data doesn't exist in db throw in placeholders...
    if r is None:
        times = {}
        times['a'] = "8:49"
        times['b'] = "10:19"

        times_entry = MDB(Name="abc",Times=times)
        times_entry.put()

    r = db.GqlQuery("select * from MasjidDB")
    return (r[0].Name, r[0].Times)

in my get function I call the m_time_manage() ftn as follows,

(mName, times) = m_time_manage()
        self.write(times)

I get this error that there's something with the pickle.loads and end of file error... the full error is but i'm not sure how else to store a dictionary in python,

 line 31, in make_value_from_datastore
    return pickle.loads(value)
  File "/usr/lib/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 880, in load_eof
    raise EOFError

thanks for any help in advance


Solution

  • I'd recommend switching to NDB and using its built-in PickleProperty. See https://developers.google.com/appengine/docs/python/ndb/properties