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

Add reversed keys(), items(), values() to sqlitedict #173

Open
jiangwen365 opened this issue Mar 9, 2023 · 5 comments
Open

Add reversed keys(), items(), values() to sqlitedict #173

jiangwen365 opened this issue Mar 9, 2023 · 5 comments

Comments

@jiangwen365
Copy link

I would like to suggest adding reversed methods to sqlitedict, such as reversed_keys(), reversed_items(), and reversed_values(). Currently, there are no built-in ways to get content in reverse order from sqlitedict, and I think it would be graceful and more efficient to have such methods available.

Having reversed methods would be useful for various use cases, such as iterating over a sqlitedict in reverse order, finding the last item in a dict, or checking if a dict contains a certain item from the end.

I believe that adding reversed methods would be relatively easy to implement by adding the DESC keyword to the underlying SQL statements used by sqlitedict. This would allow for efficient and performant reversed queries without introducing significant overhead.

Overall, I think that adding reversed methods to sqlitedict would be a valuable addition to the library and would improve its usability and flexibility.

Thank you for considering this suggestion.

@piskvorky
Copy link
Owner

piskvorky commented Mar 9, 2023

I don't think these methods are a part of the Python dict API, are they?

Sqlitedict's goal is to be a "dict with commit". So its gracefulness should mimic that of dict, for better or worse.

@jiangwen365
Copy link
Author

With Python dict, you can do this:

d = {}
iter(d.items())
<dict_itemiterator object at 0x000001FB98C53B30>
reversed(d.items())
<dict_reverseitemiterator object at 0x000001FB98CF4BD0>

aslo you can refere to this: https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

Right now, to access the latest entries, I have to dump all the data in sqlitedict into a list, and then reverse the list.

@piskvorky
Copy link
Owner

piskvorky commented Mar 9, 2023

OK, that dict_reverseitemiterator is interesting. Maybe there is some "under the hood" API for reverse iteration in dict, after all.

If that's the case we could support it too. But we'd want to use the same API, rather than invent new methods (that users will never know about).

@jiangwen365
Copy link
Author

It would be great if that could be supported and consistent with dict!

@piskvorky
Copy link
Owner

piskvorky commented Mar 10, 2023

@mpenkov do you know how that works? Any way to supply our own dict_reverseitemiterator?

@jiangwen365 can you look into it & open a PR? We probably won't have time for this in the near future.

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

No branches or pull requests

2 participants