Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45679 > unrolled thread
| Started by | stackoverflowuser95@gmail.com |
|---|---|
| First post | 2013-05-21 10:27 -0700 |
| Last post | 2013-05-22 02:31 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
More general way of generating PyODBC queries as a dict? stackoverflowuser95@gmail.com - 2013-05-21 10:27 -0700
Re: More general way of generating PyODBC queries as a dict? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-21 19:33 -0400
Re: More general way of generating PyODBC queries as a dict? stackoverflowuser95@gmail.com - 2013-05-22 02:31 -0700
| From | stackoverflowuser95@gmail.com |
|---|---|
| Date | 2013-05-21 10:27 -0700 |
| Subject | More general way of generating PyODBC queries as a dict? |
| Message-ID | <0f78102c-2e30-4858-ae62-b8d5cecfc345@googlegroups.com> |
Here are my averagely general class methods for creating a dictionary from the result of database queries:
def make_schema_dict(self):
schema = [i[2] for i in self.cursor.tables()
if i[2].startswith('tbl_') or i[2].startswith('vw_')]
self.schema = {table: {'scheme': [row.column_name for row
in self.cursor.columns(table)]}
for table in schema}
def last_table_query_as_dict(self, table):
return {'data': [{col: row.__getattribute__(col) for col in self.schema[table]['scheme']
if col != 'RowNum'} for row in self.cursor.fetchall()]}
Unfortunately as you can see, there are many complications.
For example, when multiple tables are queried; some hackish lambdas are required to generate the resulting dictionary.
Can you think of some more general methods?
(and yes I know this is against the PEP; and that this is also on SO)
[toc] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-05-21 19:33 -0400 |
| Message-ID | <mailman.1942.1369179204.3114.python-list@python.org> |
| In reply to | #45679 |
On Tue, 21 May 2013 10:27:07 -0700 (PDT), stackoverflowuser95@gmail.com
declaimed the following in gmane.comp.python.general:
>
> For example, when multiple tables are queried; some hackish lambdas are required to generate the resulting dictionary.
>
> Can you think of some more general methods?
>
What about using the information from
cursor.description
You did state PyODBC, did you not?
"""
description
This read-only attribute is a list of 7-item tuples, each containing
(name, type_code, display_size, internal_size, precision, scale,
null_ok). pyodbc only provides values for name, type_code,
internal_size, and null_ok. The other values are set to None.
"""
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | stackoverflowuser95@gmail.com |
|---|---|
| Date | 2013-05-22 02:31 -0700 |
| Message-ID | <350aaf43-2259-44cc-9ca3-e86a6823164d@googlegroups.com> |
| In reply to | #45679 |
On Wednesday, May 22, 2013 9:33:18 AM UTC+10, Dennis Lee Bieber wrote:
> On Tue, 21 May 2013 10:27:07 -0700 (PDT), stackoverflowuser95@gmail.com
>
> declaimed the following in gmane.comp.python.general:
>
>
>
> >
>
> > For example, when multiple tables are queried; some hackish lambdas are required to generate the resulting dictionary.
>
> >
>
> > Can you think of some more general methods?
>
> >
>
> What about using the information from
>
>
>
> cursor.description
>
>
>
> You did state PyODBC, did you not?
>
>
>
> """
>
> description
>
>
>
> This read-only attribute is a list of 7-item tuples, each containing
>
> (name, type_code, display_size, internal_size, precision, scale,
>
> null_ok). pyodbc only provides values for name, type_code,
>
> internal_size, and null_ok. The other values are set to None.
>
> """
>
> --
>
> Wulfraed Dennis Lee Bieber AF6VN
>
> wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
YAY: `[{c[0]: v for (c, v) in zip(row.cursor_description, row)} for row in self.cursor.fetchall()]`
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web