Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DictRow should behave like an OrderedDict? #177

Closed
psycoteam opened this issue Aug 18, 2013 · 6 comments
Closed

DictRow should behave like an OrderedDict? #177

psycoteam opened this issue Aug 18, 2013 · 6 comments
Milestone

Comments

@psycoteam
Copy link

psycoteam commented Aug 18, 2013

Originally submitted by: Laurent Fasnacht

I think RowDict should behave like an OrderedDict. In particular, I would expect the following to be True:

[row_dict[k] for k in row_dict.keys()] == list(row_dict)
@dvarrazzo
Copy link
Member

It's a sort of reasonable expectation, yes. If you provide the implementation for it we would merge it to devel. The best way to contribute is to open a feature branch on a github clone.

Current devel supports Python from 2.5 to 3.3. The branch should include tests and documentation.

I expect the new feature to not slow down the current implementation.

@psycoteam psycoteam added this to the psycopg3 milestone Aug 28, 2014
@auvipy
Copy link

auvipy commented Mar 6, 2017

Dict's are Ordered from 3.6

@kumatty
Copy link

kumatty commented Mar 22, 2017

I encountered the same issue when I tried to export csv from a postgres database. I used the first dict row's keys as the csv header and I realized the csv header and the data did not match. At least it worked with python's native sqlite3 adapter when I used sqlite3, i.e., keys of a row and actual data of a row are in the same order.

I would like to suggest to change a little bit DictRow#keys implementation as per the following:

def keys(self):
  - return self._index.keys()
    return sorted(self._index, key=self._index.get)

Or at least it would be nice to have a method like ordered_keys. What do you think?

(For now I made a super class of DictRow that overrides keys() as workaround.)

@dvarrazzo
Copy link
Member

You can use [d[0] for d in cur.description] as keys: that will work and is available even when your dataset is empty.

@kumatty
Copy link

kumatty commented Mar 22, 2017

Thanks for your quick reply, I actually didn't know that but the thing is I do not have a cursor object when I actually process the data. I know this is just our implementation problem but if DictRow has the information of keys I think it would be nice to know the order of the keys too.
I also think our situation is not quite limited because in many code "how we get the data" and "how we process the data" are separated.

@dvarrazzo
Copy link
Member

Sure. For the moment you'll have to use your own subclass of DictRow. If you have an improvement to propose patches are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants