Skip to content

restructure #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ General Set API
===============


Inherited methods
=================


From the `Python documentation <https://docs.python.org/3.12/library/collections.abc.html>`__.


Expand All @@ -24,3 +28,18 @@ From the `Python documentation <https://docs.python.org/3.12/library/collections
+------------------------------------------------+----------------+-------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ``typing.FrozenSet`` (deprecated since 3.9) | | | |
+------------------------------------------------+----------------+-------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+


Subclassing
-----------

```python
from typing import (FrozenSet as tp_FrozenSet, Set as tp_Set, MutableSet as tp_MutableSet)
from collections.abc import Set as abc_FrozenSet, MutableSet as abc_Set

assert isinstance(set(), abc_Set)
assert isinstance(frozenset(), abc_FrozenSet)
assert isinstance(set(), tp_Set)
assert isinstance(frozenset(), tp_FrozenSet)
assert isinstance(set(), tp_MutableSet)
```
304 changes: 98 additions & 206 deletions orderedsets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@

__version__ = importlib_metadata.version(__package__ or __name__)

from collections.abc import Iterator, Set
from typing import AbstractSet, Any, Iterable, Optional, Type, TypeVar, Union
from collections.abc import Iterator
from typing import Any, Iterable, Optional, Type, TypeVar, Union

from collections.abc import MutableSet as abc_MutableSet
from collections.abc import Set as abc_Set

T = TypeVar("T")

Expand All @@ -45,201 +48,7 @@ class _NotProvided:
pass


class OrderedSet(AbstractSet[T]):
"""A set class that preserves insertion order.

It can be used as a drop-in replacement for :class:`set` where ordering is
desired.
"""

def __init__(self, items: Union[Iterable[T], Type[_NotProvided]] = _NotProvided)\
-> None:
"""Create a new :class:`OrderedSet`, optionally initialized with *items*."""
if items is _NotProvided:
self._dict = {}
else:
# type-ignore-reason:
# mypy thinks 'items' can still be Type[_NotProvided] here.
self._dict = dict.fromkeys(items) # type: ignore[arg-type]

def __eq__(self, other: object) -> bool:
"""Return whether this set is equal to *other*."""
return (isinstance(other, Set)
and len(self) == len(other)
and all(i in other for i in self))

def __repr__(self) -> str:
"""Return a string representation of this set."""
if len(self) == 0:
return "OrderedSet()"
return "OrderedSet({" + ", ".join([repr(k) for k in self._dict]) + "})"

def add(self, element: T) -> None:
"""Add *element* to this set."""
self._dict = {**self._dict, **{element: None}}

def clear(self) -> None:
"""Remove all elements from this set."""
self._dict.clear()

def copy(self) -> OrderedSet[T]:
"""Return a shallow copy of this set."""
return OrderedSet(self._dict.copy())

def difference(self, *others: Iterable[T]) -> OrderedSet[T]:
"""Return all elements that are in this set but not in *others*."""
if not others:
return OrderedSet(self._dict)
other_elems = set.union(*map(set, others))
items = {item: None for item in self if item not in other_elems}
return OrderedSet(items)

def difference_update(self, *others: Iterable[T]) -> None:
"""Update this set to remove all items that are in *others*."""
for other in others:
for e in other:
self.discard(e)

def discard(self, element: T) -> None:
"""Remove *element* from this set, if it is present."""
if element in self._dict:
del self._dict[element]

def intersection(self, *others: Iterable[T]) -> OrderedSet[T]:
"""Return the intersection of this set and *others*."""
if not others:
return OrderedSet(self._dict)

result_elements = []
for element in self._dict.keys():
if all(element in other for other in others):
result_elements.append(element)

return OrderedSet(result_elements)

def intersection_update(self, *others: Iterable[T]) -> None:
"""Update this set to be the intersection of itself and *others*."""
if not others:
return

common_keys = list(self._dict.keys())
for other in others:
common_keys = [key for key in common_keys if key in set(other)]

self._dict = dict.fromkeys(common_keys)

def isdisjoint(self, s: Iterable[T]) -> bool:
"""Return whether this set is disjoint with *s*."""
return self._dict.keys().isdisjoint(s)

def issubset(self, s: Iterable[T]) -> bool:
"""Return whether this set is a subset of *s*."""
return all(i in s for i in self)

def issuperset(self, s: Iterable[T]) -> bool:
"""Return whether this set is a superset of *s*."""
return set(self).issuperset(set(s))

def pop(self) -> T:
"""Remove and return the most recently added element from this set."""
items = list(self._dict)
result = items.pop()
self._dict = dict.fromkeys(items)
return result

def remove(self, element: T) -> None:
"""Remove *element* from this set, raising :exc:`KeyError` if not present."""
del self._dict[element]

def symmetric_difference(self, s: Iterable[T]) -> OrderedSet[T]:
"""Return the symmetric difference of this set and *s*."""
return OrderedSet(
dict.fromkeys([e for e in self._dict if e not in s]
+ [e for e in s if e not in self._dict]))

def symmetric_difference_update(self, s: Iterable[T]) -> None:
"""Update this set to be the symmetric difference of itself and *s*."""
self._dict = self.symmetric_difference(s)._dict

def union(self, *others: Iterable[T]) -> OrderedSet[T]:
"""Return the union of this set and *others*."""
return OrderedSet(list(self._dict)
+ [e for other in others for e in other])

def update(self, *others: Iterable[T]) -> None:
"""Update this set to be the union of itself and *others*."""
self._dict = self.union(*others)._dict

def __len__(self) -> int:
"""Return the number of elements in this set."""
return len(self._dict)

def __contains__(self, o: object) -> bool:
"""Return whether *o* is in this set."""
return o in self._dict

def __iter__(self) -> Iterator[T]:
"""Return an iterator over the elements of this set."""
return iter(self._dict)

def __and__(self, s: Set[T]) -> OrderedSet[T]:
"""Return the intersection of this set and *s*."""
return self.intersection(s)

def __iand__(self, s: Set[T]) -> OrderedSet[T]:
"""Update this set to be the intersection of itself and *s*."""
result = self.intersection(s)
self._dict = result._dict
return result

def __or__(self, s: Set[Any]) -> OrderedSet[T]:
"""Return the union of this set and *s*."""
return self.union(s)

def __ior__(self, s: Set[Any]) -> OrderedSet[T]:
"""Update this set to be the union of itself and *s*."""
result = self.union(s)
self._dict = result._dict
return result

def __sub__(self, s: Set[T]) -> OrderedSet[T]:
"""Return the difference of this set and *s*."""
return self.difference(s)

def __isub__(self, s: Set[T]) -> OrderedSet[T]:
"""Update this set to be the difference of itself and *s*."""
result = self.difference(s)
self._dict = result._dict
return result

def __xor__(self, s: Set[Any]) -> OrderedSet[T]:
"""Return the symmetric difference of this set and *s*."""
return self.symmetric_difference(s)

def __ixor__(self, s: Set[Any]) -> OrderedSet[T]:
"""Update this set to be the symmetric difference of itself and *s*."""
result = self.symmetric_difference(s)
self._dict = result._dict
return result

def __le__(self, s: Set[T]) -> bool:
"""Return whether this set is a subset of *s*."""
return self.issubset(s)

def __lt__(self, s: Set[T]) -> bool:
"""Return whether this set is a proper subset of *s*."""
return len(self) < len(s) and self.issubset(s)

def __ge__(self, s: Set[T]) -> bool:
"""Return whether this set is a superset of *s*."""
return set(self) >= set(s)

def __gt__(self, s: Set[T]) -> bool:
"""Return whether this set is a proper superset of *s*."""
return len(self) > len(s) and set(self) > set(s)


class FrozenOrderedSet(AbstractSet[T]):
class FrozenOrderedSet(abc_Set[T]):
"""A frozen set class that preserves insertion order.

It can be used as a drop-in replacement for :class:`frozenset` where
Expand Down Expand Up @@ -277,15 +86,15 @@ def __hash__(self) -> int:

def __eq__(self, other: object) -> bool:
"""Return whether this set is equal to *other*."""
return (isinstance(other, Set)
return (isinstance(other, abc_Set)
and len(self) == len(other)
and all(i in other for i in self))

def __repr__(self) -> str:
"""Return a string representation of this set."""
if len(self) == 0:
return "FrozenOrderedSet()"
return "FrozenOrderedSet({" + ", ".join([repr(k) for k in self._dict]) + "})"
return f"{self.__class__.__name__}()"
return self.__class__.__name__ + "({" + ", ".join([repr(k) for k in self._dict]) + "})"

def __len__(self) -> int:
"""Return the number of elements in this set."""
Expand All @@ -301,7 +110,7 @@ def __iter__(self) -> Iterator[T]:

def copy(self) -> FrozenOrderedSet[T]:
"""Return a shallow copy of this set."""
return FrozenOrderedSet(self._dict)
return self.__class__(self._dict)

def difference(self, *others: Iterable[T]) -> FrozenOrderedSet[T]:
"""Return the difference of this set and *others*."""
Expand Down Expand Up @@ -339,25 +148,108 @@ def issubset(self, s: Iterable[T]) -> bool:

def issuperset(self, s: Iterable[T]) -> bool:
"""Return whether this set is a superset of *s*."""
return set(self).issuperset(set(s))
return all(i in self for i in s)

def union(self, *others: Iterable[T]) -> FrozenOrderedSet[T]:
"""Return the union of this set and *others*."""
return FrozenOrderedSet(list(self._dict)
+ [e for other in others for e in other])

def __and__(self, s: Set[T]) -> FrozenOrderedSet[T]:
def __and__(self, s: abc_Set[T]) -> FrozenOrderedSet[T]:
"""Return the intersection of this set and *s*."""
return self.intersection(s)

def __or__(self, s: Set[Any]) -> FrozenOrderedSet[T]:
def __or__(self, s: abc_Set[Any]) -> FrozenOrderedSet[T]:
"""Return the union of this set and *s*."""
return self.union(s)

def __sub__(self, s: Set[T]) -> FrozenOrderedSet[T]:
def __sub__(self, s: abc_Set[T]) -> FrozenOrderedSet[T]:
"""Return the difference of this set and *s*."""
return self.difference(s)

def __xor__(self, s: Set[Any]) -> FrozenOrderedSet[T]:
def __xor__(self, s: abc_Set[Any]) -> FrozenOrderedSet[T]:
"""Return the symmetric difference of this set and *s*."""
return self.symmetric_difference(s)


class OrderedSet(abc_MutableSet[T], FrozenOrderedSet):
"""A set class that preserves insertion order.

It can be used as a drop-in replacement for :class:`set` where ordering is
desired.
"""

__hash__ = None

def add(self, element: T) -> None:
"""Add *element* to this set."""
self._dict = {**self._dict, **{element: None}}

def clear(self) -> None:
"""Remove all elements from this set."""
self._dict.clear()

def difference_update(self, *others: Iterable[T]) -> None:
"""Update this set to remove all items that are in *others*."""
for other in others:
for e in other:
self.discard(e)

def discard(self, element: T) -> None:
"""Remove *element* from this set, if it is present."""
if element in self._dict:
del self._dict[element]

def intersection_update(self, *others: Iterable[T]) -> None:
"""Update this set to be the intersection of itself and *others*."""
if not others:
return

common_keys = list(self._dict.keys())
for other in others:
common_keys = [key for key in common_keys if key in set(other)]

self._dict = dict.fromkeys(common_keys)

def pop(self) -> T:
"""Remove and return the most recently added element from this set."""
items = list(self._dict)
result = items.pop()
self._dict = dict.fromkeys(items)
return result

def remove(self, element: T) -> None:
"""Remove *element* from this set, raising :exc:`KeyError` if not present."""
del self._dict[element]

def symmetric_difference_update(self, s: Iterable[T]) -> None:
"""Update this set to be the symmetric difference of itself and *s*."""
self._dict = self.symmetric_difference(s)._dict

def update(self, *others: Iterable[T]) -> None:
"""Update this set to be the union of itself and *others*."""
self._dict = self.union(*others)._dict

def __iand__(self, s: abc_Set[T]) -> OrderedSet[T]:
"""Update this set to be the intersection of itself and *s*."""
result = self.intersection(s)
self._dict = result._dict
return result

def __ior__(self, s: abc_Set[Any]) -> OrderedSet[T]:
"""Update this set to be the union of itself and *s*."""
result = self.union(s)
self._dict = result._dict
return result

def __isub__(self, s: abc_Set[T]) -> OrderedSet[T]:
"""Update this set to be the difference of itself and *s*."""
result = self.difference(s)
self._dict = result._dict
return result

def __ixor__(self, s: abc_Set[Any]) -> OrderedSet[T]:
"""Update this set to be the symmetric difference of itself and *s*."""
result = self.symmetric_difference(s)
self._dict = result._dict
return result
Loading