bustawin / ordered-set-37 Goto Github PK
View Code? Open in Web Editor NEWDead simple & fast ordered set using python's 3.7+ dict.
License: The Unlicense
Dead simple & fast ordered set using python's 3.7+ dict.
License: The Unlicense
Hi,
according to the Python documentation of Set Types, the difference between remove(elem)
and discard(elem)
is that the former raises a KeyError
if elem
is not a member of the set whereas the latter is a no-op in this case.
Unfortunately, in ordered-set-37 version 1.0, both OrderedSet.remove(elem)
and OrderedSet.discard(elem)
raise a KeyError
if the argument is not in the set. Test case:
import pytest
from ordered_set_37 import OrderedSet
def test_discard_deletes_existing_element():
x = OrderedSet([1, 2, -1])
x.discard(2)
assert list(x) == [1, -1]
def test_discard_ignores_missing_element():
x = OrderedSet([1, 2, -1])
x.discard(3)
assert list(x) == [1, 2, -1]
def test_remove_deletes_existing_element():
x = OrderedSet([1, 2, -1])
x.remove(2)
assert list(x) == [1, -1]
def test_remove_raises_key_error_on_missing_element():
x = OrderedSet([1, 2, -1])
with pytest.raises(KeyError):
x.remove(3)
# x is not modified
assert list(x) == [1, 2, -1]
Abbreviated test output:
================ FAILURES =================
____ test_discard_ignores_missing_element____
def test_discard_ignores_missing_element():
x = OrderedSet([1, 2, -1])
> x.discard(3)
scratch_1.py:13:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <OrderedSet {1, 2, -1}>, x = 3
def discard(self, x: T) -> None:
> self._d.pop(x)
E KeyError: 3
../../.pyenv/versions/3.8.1/envs/ong-mono/lib/python3.8/site-packages/ordered_set_37/__init__.py:20: KeyError
=========================================
Proposed fix: Add an (ignored) default argument to the call of dict.pop(x)
in line 20 of ordered_set_37.__init__.py
, e.g.:
def discard(self, x: T) -> None:
self._d.pop(x, None)
Hi,
I wanted to point out that I made an implementation of OrderedSet
that uses the same foundations as your implementation but adds many more features.
(I called it StableSet
, as OrderedSet
was a name for a different implementation) .
I also used your tests to test full compatibility between your implementation and 2 other implementations.
Please have a look if you are interested:
https://pypi.org/project/stableset/
I made a PR to merge it upstream:
https://github.com/rspeer/ordered-set/pull/92/files
Hi,
I am wondering about the signature of OrderedSet.__init__(self, iterable: t.Optional[t.Iterator[T]] = None)
. Why did you declare iterable
to be an Iterator[T] (or None) instead of an Iterable[T]? dict.from_keys()
(which you call in your implementation) accepts any Iterable[T]
.
Not only does the parameter name suggest this was an oversight, but collections.abc.Iterator
also inherits from collections.abc.Iterable
; the former is therefore the more specialized type.
The collection classes are iterables but not iterator instances. In fact, initializing OrderedSet
from a list, set, or even another OrderedSet
instance works fine but makes mypy complain:
$ mypy ~/.pyenv/versions/ong-mono/lib/python3.8/site-packages/ordered_set_37/__init__.py ~/Library/Application\ Support/Jetbrains/PyCharm2020.2/scratches/scratch.py
/Users/ludwigc/Library/Application Support/Jetbrains/PyCharm2020.2/scratches/scratch.py:3: error: Argument 1 to "OrderedSet" has incompatible type "List[str]"; expected "Optional[Iterator[str]]"
/Users/ludwigc/Library/Application Support/Jetbrains/PyCharm2020.2/scratches/scratch.py:5: error: Argument 1 to "OrderedSet" has incompatible type "OrderedSet[str]"; expected "Optional[Iterator[<nothing>]]"
Found 2 errors in 1 file (checked 2 source files)
The built-in set
class has an update
method, but the OrderedSet
class doesn't.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.