sqlitedict icon indicating copy to clipboard operation
sqlitedict copied to clipboard

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

Open jiangwen365 opened this issue 1 year ago • 5 comments

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.

jiangwen365 avatar Mar 09 '23 09:03 jiangwen365

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.

piskvorky avatar Mar 09 '23 09:03 piskvorky

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.

jiangwen365 avatar Mar 09 '23 10:03 jiangwen365

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).

piskvorky avatar Mar 09 '23 10:03 piskvorky

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

jiangwen365 avatar Mar 10 '23 05:03 jiangwen365

@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.

piskvorky avatar Mar 10 '23 07:03 piskvorky