[sqlalchemy] Re: export and import JSON from database (JSON type)

2009-03-02 Thread eLuke

On Feb 28, 10:50 am, Michael Bayer mike...@zzzcomputing.com wrote:

 A method like this would work:

 def from_json(self,json):
  json_obj = simplejson.loads(json)
  for k, v in json_obj.values():
  setattr(self, k, v)

 if you're concerned about dates you can use a date-based TypeDecorator
 which can receive a fully qualified datestring as an argument.   Or
 use descriptors on your mapped classes (i.e. date = property(def
 get_date()/def set_date()))

Thanks for the code example, setattr() was exactly what I was looking
for!

I did have to make a minor change; I initially got this error:

for k, v in json_obj.values():
ValueError: too many values to unpack

but was OK once I made the change to:

for k, v in json_obj.iteritems():
setattr(self, k, v)

Thanks also for the hint to look at TypeDecorator and descriptors.

I was surprised to find that just passing the default attribute types/
values of comment after the setattr() calls via session.save
(comment) actually worked, since the postdate attribute on the
comment class instance had a type of unicode. I was expecting to
have to handle changing this to a datetime type in order to get it to
work.

Am I right in guessing that this works because the underlying database
(PostgreSQL) recognized the unicode date string and was able to
convert it, or is SA doing something for me with its internal type
engine/system in conjunction with the db (i.e. in other words, I'm
trying to figure out what component is making this work so I know
where to look for what's considered a legal/well-formed/importable
javascript date string)?

Thanks again for all of the help, -e
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: export and import JSON from database (JSON type)

2009-02-28 Thread eLuke

On Feb 25, 12:17 pm, Roger Demetrescu roger.demetre...@gmail.com
wrote:

 Note that this implementation is very simple. Depending of your use
 case, you probably should take a look at MutableType [1] and
 types.TypeEngine.is_mutable().

 [1] -http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/types.html#sql...

  On Wed, Feb 25, 2009 at 10:27 AM, Roger Demetrescu
  roger.demetre...@gmail.com wrote:

  I did something like that recently:

  -

  from sqlalchemy import types
  import simplejson

  class JsonString(types.TypeDecorator):
 impl = types.String
 def process_result_value(self, value, dialect):
 if value is None:
 return None
 else:
 return simplejson.loads(value)

 def process_bind_param(self, value, dialect):
 if value is None:
 return None
 else:
 return simplejson.dumps(value)

  -


This looks like something I should be using, but I'm not sure. I've
been playing around with simplejson and loads but not getting very
far when it comes to getting my json object's properties into my
python SA object's (model's) fields.

I have a simple model/class named Comment with columns id (int),
comment (text), and postdate (datetime).

Below are the main bits of the python code I'm using to test how I
should handle this:

# json from the client's browser
json = r'{id:1,postdate:Sat Jan 31 2009 22:18:15 GMT-0500
(Eastern Standard Time),comment:Comment text.}'
# create comment instance
comment = model.Comment()
# decode json to python object (dict)
json_obj = simplejson.loads(json)
# ???

So now I have json_obj as a python dict object with key/value
pairs... and I need to somehow get this into:

comment.id
comment.comment
comment.postdate

I've searched and searched and have only found one good example piece
of code that I may be able to use to at least handle the parsing/
generation of the datetime type from a javascript Date string; but,
I'm still stuck on how to iterate over the dict keys and do an
assignment to the properties in the comment object instance in a way
that's reusable for other model classes.

Thanks for your time, -e

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---