collection[-1] raises an error : *** ValueError: Collection slices must have a nonnegative start point.
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lazr.restfulclient |
Triaged
|
Low
|
Unassigned |
Bug Description
It would be nice, for a collection, to be able to do [-1] to get the last element, like [0] gets the first. The following is a distillation of a chat Leonard and I had about this.
Leonard says: this could be done on server or client and while tricky there is no desire to avoid it per se.
a collection is a series of pages (in the vm sense) with a forward pointer called the next_link.
In small collections its one page and next is NULL.
One approach: client side only: ask for the root of the collection, we get a page + size of collection in items. Then we can make a request for -1 by taking size-1 and asking for collection?
If we change the server, requestion ws.start=-1 could do something other than blow up (as it does? probably does? today). One possibility is to translate the -1 to 'start at the end of the list'. Another is to start at the end of the list *and* invert the direction of additional data included. That is: 0 gives item 0 and page_count -1 additional items with monotonically increasing indices. -1 would give item -1 and page_count -1 additional items with monotonically decreasing indices.
I'm +1 on this being done eventually. It should be considered for inclusion in the upcoming effort to make working with lazr.restful operations more "natural".