Skip to content

Commit

Permalink
Changed equals(): Can compare to lists and sets
Browse files Browse the repository at this point in the history
  • Loading branch information
rindPHI committed Dec 22, 2021
1 parent e052bab commit 2ee4cbf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/test_implementation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Test toolchain for all examples

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
test:

runs-on: ubuntu-latest
env:
DISPLAY: ':99'
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: "3.10"
- name: Test with pytest
run: |
python3 -m pytest --html=report.html --self-contained-html
- name: Upload report
if: always()
uses: actions/upload-artifact@v2
with:
name: Test Report
path: report.html
8 changes: 6 additions & 2 deletions orderedset/orderedset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ def __init__(self, base: Optional[Union[Dict[T, None], Iterable[T]]] = None):
else:
self.the_dict = dict.fromkeys(base)

def __eq__(self, o: object) -> bool:
return isinstance(o, OrderedSet) and list(self.the_dict) == list(o.the_dict)
def __eq__(self, other: object) -> bool:
if isinstance(other, (OrderedSet, list)):
return len(self) == len(other) and list(self) == list(other)
elif isinstance(other, Set):
return set(self) == set(other)
return NotImplemented

def __ne__(self, o: object) -> bool:
return not self.__eq__(o)
Expand Down
6 changes: 6 additions & 0 deletions orderedset/tests/test_orderedset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def test_tolist(self):
def test_eq(self):
self.assertTrue(OrderedSet([1, 2, 3]) == OrderedSet([1, 2, 3]))

def test_eq_set(self):
self.assertTrue({1, 2, 3} == OrderedSet([3, 1, 2]))

def test_eq_list(self):
self.assertTrue([3, 1, 2] == OrderedSet([3, 1, 2]))

def test_ne(self):
self.assertTrue(OrderedSet([1, 2, 3]) != OrderedSet([3, 2, 1]))

Expand Down
10 changes: 10 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
attrs>=21.2.0
iniconfig>=1.1.1
packaging>=21.3
pluggy>=1.0.0
py>=1.11.0
pyparsing>=3.0.6
pytest>=6.2.5
pytest-html>=3.1.1
pytest-metadata>=1.11.0
toml>=0.10.2

0 comments on commit 2ee4cbf

Please sign in to comment.