tml wrote:
> I have this code, but the returning "syms" doesn't work for 
> TurboGear's jsonify. Returning "newSyms" works
> 
> >
> > chem_data = Table('chem_data', metadata,
> >     Column('id', Integer, primary_key=True),
> >     Column('symbol', String(8), nullable=False, index=True),
> >     Column('name', Unicode(64), nullable=False, index=True),
> >     Column('location', Unicode(32)),
> >     Column('description', Unicode),
> >     mysql_engine='InnoDB'
> > )
> >
> > def searchData(filter, limit=10):
> >         cols = [chem_data.c.name, chem_data.c.symbol]
> >         symList = select(cols, or_(chem_data.c.symbol.like(filter),
> >                      chem_data.c.name.like(filter)), 
> limit=limit).execute()
> >         if symList.rowcount:
> >                 syms = symList.fetchall()
> >                 print "XXXXX", syms, type(syms)
> >                 newSyms = [(t.name, t.symbol) for t in syms]
> >                 print "YYYYY", newSyms, type(newSyms)
> >                 return newSyms # This works
> >                 return syms # This wont work.
> >         else:
> >                 return []
> >
> > The print returns:
> > XXXXX [(u'Oxygen', 'O')] <type 'list'> YYYYY  [(u'Oxygen', 
> 'O')] <type 
> > 'list'>
> >
> > Does fetchall() return some kind of weird list? or might be 
> a bug in 
> > SA...
> >
> > thanks.
> >
> > -tml
> 

They are normal lists, but each element is an instance of
sqlalchemy.engine.base.RowProxy. Try adding 'print type(syms[0])' to see
that.

RowProxy is handy because you can access the values in a list-like way
(row[0]), a dictionary-like way (row['symbol']), or even in an
'object-like' way (row.name). You can even use your column objects to
access the values (row[chem_data.c.location])

jsonify doesn't know how to convert RowProxy objects, but it is easy to
add a rule to convert them. Add something like this to your json.py
(untested):

#-----------------------------------
import sqlalchemy

@jsonify.when("isinstance(obj, sqlalchemy.engine.base.RowProxy)")
def jsonify_row(obj):
    return tuple(obj)
    # or you could use dict(obj) if you want to use dictionary-style
access

#-----------------------------------

You may even be able to add a rule to convert the ResultProxy object as
well (the object returned by 'execute()' (also untested):

@jsonify.when("isinstance(obj, sqlalchemy.engine.base.ResultProxy)")
def jsonify_results(obj):
    return obj.fetchall()

Then your searchData function can just return symList directly.

Hope that helps,

Simon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" 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/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to