[egenix-users] Converting ZQL method return value to dictionary

Wade Leftwich wade at lightlink.com
Sun Aug 10 08:27:52 CEST 2003


Jerry Westrick wrote:
> Hello:
> 
> I'm not sure if this is the right list, if not sorry.
> 
> I'm trying to convert the result of a Zql method into a dictionary.
> the code I'm using is: 
> 
> # Get User Info
> for row in context.Get_UserInfo(user=request.AUTHENTICATED_USER):
>     values['user'] = row
> 
> The Get_UserInfo is a Z SQL Method, returning a single row
> from a mxODBC conneciton.
> 
> when I executed the following lines: 
> 
> print values['user'] 
> return printed
> 
> 
> I get 
> 
> <r instance at 8d8f130>
> 
> I would to convert this r instance to a dictionary, so I can add/modify
>  values in it like the following:
> 
> values['user']['justforfun'] = 'Additional info not from select!'
> 
> which at the moment gives me:
> 
> Error Type: TypeError
> Error Value: object does not support item or slice assignment
> 
> 
> 
> Now to my questions:
> 1) the class "r", is it a Zope thingy or a mxODBC thingy?
> 2) is there a mothod to do what I need?
> 
> 
> Than you for your time...
> Jerry
> 
> P.S.  There are other dictionary functions not support by this "R"
> instance that I would like to use also...
> 
> 

In the past I have frequently written a function to convert each tuple
in a zsql result to a dictionary. But looking at the current docs

http://zope.org/Documentation/Books/ZopeBook/current/RelationalDatabases.stx

toward the bottom of the page, I see that zsql results have a 
dictionaries() method that returns a list of dicts. I wonder if that's a 
recent addition, or if I just never noticed it before?

If that does not work for you, try this:

def row2dict(row, names):
     D = {}
     for i in range(len(names)):
         D[names[i]] = row[i]
     return D

result = context.somezsqlmethod()
for row in result:
     print row2dict(row, result.names())


-- Wade Leftwich
Ithaca, NY










More information about the egenix-users mailing list