Jun 25, 2010

Re: What is the best way to convert a dictionary of data into a datastore entity?

If you are going to use your dictionaries as dictionaries back in your
program then why not save those into a Blob or Text datastore property
(by pickling or repr - eval or other means).

There was a long thread going here about the more efficient way to do this here :
http://groups.google.com/group/google-appengine-python/browse_thread/thread/8b07e7c24cb434f2/035f1b4e247deef9
http://groups.google.com/group/google-appengine-python/browse_thread/thread/b929234f093f355c/806aa0c3de0d2528
an example where i use repr eval:
from google.appengine.ext import db

class DicPropertyEval(db.Property):
    data_type = dict
    def get_value_for_datastore(self, model_instance):
        return db.Text(repr(super(DicPropertyEval,self).get_value_for_datastore(model_instance) ) )
    def make_value_from_datastore(self, value):
        if value is None:
            return dict()
        return eval(value)
    def default_value(self):
        if self.default is None:return dict()
        else:return super(DicPropertyEval,self).default_value().copy()

class DataStoreDic(db.Model):
    dicVal = DicPropertyEval(indexed=False) 
The above method is not the fastest one but I prefer it sometimes coz
the dictionary is readable - editable by a human while in datastore.

happy coding ;-)

On 25 Ιούν, 20:57, Barry Hunter <barrybhun...@gmail.com> wrote:
> I'm no python expert, but I think Expando Class is designed for this
> sort of situation
>
> http://code.google.com/appengine/docs/python/datastore/expandoclass.html
>
> On 25 June 2010 17:52, richardhenry <richardhe...@me.com> wrote:
>
> > I have millions of Python dictionaries that have some common fields
> > (clearer for me to call them "fields" than "keys" here), but the
> > fields will vary a great deal from one dict to another.
>
> > Since I don't know in advance the fields that a dictionary will
> > contain, I was thinking of iterating through the values in the dict,
> > picking a data store Property (IntProperty(), StringProperty(), etc.)
> > class to use, and then creating a class using type('Content',
> > (db.Model,), {"name": StringProperty() ...}). Then I can store the
> > data from the dictionary in this class and save() it.
>
> > Am I headed down the right route? Does this make sense, or am I
> > missing some built-in feature that makes this much easier?
>
> > To summarize: I have millions of dictionaries that I want to convert
> > to datastore entities. All of the dict are related, and they do have
> > *some* common fields, but the majority of the data varies from dict to
> > dict. What's the best way to convert these dicts into entities in the
> > datastore?
>
> > Richard

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete