Open jiangwen365 opened 1 year ago
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.
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.
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).
It would be great if that could be supported and consistent with dict!
@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.
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.