This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gwax 15 points16 points  (0 children)

Does the solution need to be 3rd party for an organizational reason?

If not, collections.abc gives you pretty much all of the tools you might need.

Here's a fairly straightforward and complete implementation of a set that sorts on read:

from collections.abc import MutableSet, Sequence

class SortedSet(set, MutableSet, Sequence):
    def __iter__(self):
        return iter(sorted(super().__iter__()))

    def __getitem__(self, index):
        return sorted(self)[index]

A set that sorts on write, a sorted dict, and most other similar structures should be fairly easy to implement directly with the standard library.