A simple and efficient OrderedSet in pure python. Implements both MutableSet and Sequence.
fromtechcable.orderedsetimportOrderedSet# prints {1, 2, 7, 3}print(OrderedSet([1, 2, 7, 2, 3]))
# Implements all standard set methods, still preserves orderprint(OrderedSet.of[1,2]) |OrderedSet([3,2,4])) # {1,2,3,4}# OrderedSet.of(1, 2) is shorthand for OrderedSet([1, 2]),# avoiding an extra pair of bracketsprint(OrderedSet.of(1, 2)) # {1, 2}# Implements `append` method, returning True on success# and False if the item was a duplicateoset=OrderedSet()
oset.append(1) # Trueoset.append(2) # Trueoset.append(1) # False - already in set, did nothingoset.extend([2,3]) # True - at least one successoset.append([2,3]) # False - all duplicatesSupports pydantic validation & serialization:
importpydanticfromtechcable.orderedsetimportOrderedSetmodel=pydantic.TypeAdapter(OrderedSet[int])
# prints OrderedSet.of(1,2,7,8)print(repr(model.validate_python([1,2,7,8])))
assertmodel.dump_python(OrderedSet.of(1,2,7,8)) == [1,2,7,8]- Implement
OrderedFrozenSet - Consider acceleration module using C/Rust/Cython
- Probably unnecessary since this has library has very little overhead compared to the builtin
set/list
- Probably unnecessary since this has library has very little overhead compared to the builtin
Licensed under either the Apache 2.0 License or MIT License at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.