[Tutor] accessing Postgres db results by column name

2010-04-09 Thread Serdar Tumgoren
Hi folks,

Does anyone know if there's native support in psycopg2 for accessing rows in
a result-set by column name (rather than by index)?

I'd like to be able to do something like below:

cur.execute('select id, name from mytable')
data = cur.fetchall()
for row in data:
print row['id'], row['name']

The functionality I have in mind is built into sqlite3:


http://docs.python.org/py3k/library/sqlite3.html#accessing-columns-by-name-instead-of-by-index

And there are a few Python recipes that let you mimic this behavior:


http://code.activestate.com/recipes/81252-using-dtuple-for-flexible-query-result-access/

http://code.activestate.com/recipes/52293-generate-field-name-to-column-number-dictionary/

But I'm wondering if any Postgres db adapters offer native support, similar
to sqlite3? I didn't notice it in a quick scan of the psycopg2 docs (
http://initd.org/psycopg/docs/), but perhaps the functionality is not
documented or someone knows of a different Postgres adapter that has this
capability?

As always, any pointers are greatly appreciated!

Regards,

Serdar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] accessing Postgres db results by column name

2010-04-09 Thread Serdar Tumgoren
  class reg(object):
 ... def __init__(self, cursor, registro):
 ... for (attr, val) in zip((d[0] for d in cursor.description),
 registro) :
 ... setattr(self, attr, val)

  for row in cursor.fetchall() :
 ... r = reg(cursor, row)
 ... print r.CosCPrd, r.CosCAno, r.CosCMes, r.CosCImpSis, r.CosCUsr


WOW!!! That works beautifully! Many many thanks!

Regards,

Serdar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] accessing Postgres db results by column name

2010-04-09 Thread Ricardo Aráoz
Serdar Tumgoren wrote:
 Hi folks,

 Does anyone know if there's native support in psycopg2 for accessing
 rows in a result-set by column name (rather than by index)?

 I'd like to be able to do something like below:

 cur.execute('select id, name from mytable')
 data = cur.fetchall()
 for row in data:
 print row['id'], row['name']

 The functionality I have in mind is built into sqlite3:

   
 http://docs.python.org/py3k/library/sqlite3.html#accessing-columns-by-name-instead-of-by-index

 And there are a few Python recipes that let you mimic this behavior:


 http://code.activestate.com/recipes/81252-using-dtuple-for-flexible-query-result-access/

 http://code.activestate.com/recipes/52293-generate-field-name-to-column-number-dictionary/

 But I'm wondering if any Postgres db adapters offer native support,
 similar to sqlite3? I didn't notice it in a quick scan of the psycopg2
 docs (http://initd.org/psycopg/docs/), but perhaps the functionality
 is not documented or someone knows of a different Postgres adapter
 that has this capability?

 As always, any pointers are greatly appreciated!

I do it in mssql, but I think it should be the same with psycopg (sorry
I didn't polish it but I'm finishing my day and no time) :

 class reg(object):
... def __init__(self, cursor, registro):
... for (attr, val) in zip((d[0] for d in cursor.description),
registro) :
... setattr(self, attr, val)

 for row in cursor.fetchall() :
... r = reg(cursor, row)
... print r.CosCPrd, r.CosCAno, r.CosCMes, r.CosCImpSis, r.CosCUsr

HTH


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] accessing Postgres db results by column name

2010-04-09 Thread Serdar Tumgoren
Hey everyone,

Ricardo was nice enough to post his solution as a recipe on ActiveState. For
anyone interested in bookmarking it, here's the link:

http://code.activestate.com/recipes/577186-accessing-cursors-by-field-name/

Serdar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Linux lib path

2010-04-09 Thread Joson
Hi all,

How to append a path (/var/myprog/src) to sys.path, but not in the dynamic
way like sys.path.apend(packpath), please?
I use debian os. and I'd tried to set the classpath in /etc/profile (export
CLASSPATH=...), and the pythonpath too (export PYTHONPATH=...). I found
it didn't work.

Best regards,

Joson
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] accessing Postgres db results by column name

2010-04-09 Thread Martin Walsh
Serdar Tumgoren wrote:
 Hey everyone,
 
 Ricardo was nice enough to post his solution as a recipe on ActiveState.
 For anyone interested in bookmarking it, here's the link:
 
 http://code.activestate.com/recipes/577186-accessing-cursors-by-field-name/
 
 Serdar
 

I really like Ricardo's solution ... attribute access is a nice touch,
bookmarking it now.

FWIW, it would seem that psycopg2 also has a DictCursor (and
DictConnection).

http://initd.org/psycopg/docs/extras.html

HTH,
Marty


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor