From 7a6f9b87d7666b9c0332db9a224caa46b62b0e56 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 28 Oct 2021 19:24:11 -0700 Subject: [PATCH 01/18] makes LRUCache constructor concrete --- other/least_recently_used.py | 1 + 1 file changed, 1 insertion(+) diff --git a/other/least_recently_used.py b/other/least_recently_used.py index 9d6b6d7cb6a6..4a52fb6be3f5 100644 --- a/other/least_recently_used.py +++ b/other/least_recently_used.py @@ -52,6 +52,7 @@ def display(self): if __name__ == "__main__": + lru_cache = LRUCache(4) lru_cache.refer(1) lru_cache.refer(2) From f95854f9ae64fa16c1591f0ef47634676e0f4166 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 28 Oct 2021 19:50:30 -0700 Subject: [PATCH 02/18] fixes bug in dq_removal in other/least_recently_used + deque.remove() operates by value not index --- other/least_recently_used.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/other/least_recently_used.py b/other/least_recently_used.py index 4a52fb6be3f5..e90f5e5dbade 100644 --- a/other/least_recently_used.py +++ b/other/least_recently_used.py @@ -33,12 +33,7 @@ def refer(self, x): last_element = self.dq_store.pop() self.key_reference_map.remove(last_element) else: - index_remove = 0 - for idx, key in enumerate(self.dq_store): - if key == x: - index_remove = idx - break - self.dq_store.remove(index_remove) + self.dq_store.remove(x) self.dq_store.appendleft(x) self.key_reference_map.add(x) From 0b1334c1196f30b46bb9ef4d27ed065dc924bb9f Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 28 Oct 2021 19:55:09 -0700 Subject: [PATCH 03/18] [mypy] Annotates other/least_recently_used over generic type + clean-up: rename key_reference to match type. --- other/least_recently_used.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/other/least_recently_used.py b/other/least_recently_used.py index e90f5e5dbade..43647da72d89 100644 --- a/other/least_recently_used.py +++ b/other/least_recently_used.py @@ -1,20 +1,23 @@ import sys from collections import deque +from typing import Generic, TypeVar +T = TypeVar("T") -class LRUCache: + +class LRUCache(Generic[T]): """Page Replacement Algorithm, Least Recently Used (LRU) Caching.""" - dq_store = object() # Cache store of keys - key_reference_map = object() # References of the keys in cache + dq_store: deque[T] # Cache store of keys + key_reference: set[T] # References of the keys in cache _MAX_CAPACITY: int = 10 # Maximum capacity of cache - def __init__(self, n: int): + def __init__(self, n: int) -> None: """Creates an empty store and map for the keys. The LRUCache is set the size n. """ self.dq_store = deque() - self.key_reference_map = set() + self.key_reference = set() if not n: LRUCache._MAX_CAPACITY = sys.maxsize elif n < 0: @@ -22,23 +25,23 @@ def __init__(self, n: int): else: LRUCache._MAX_CAPACITY = n - def refer(self, x): + def refer(self, x: T) -> None: """ Looks for a page in the cache store and adds reference to the set. Remove the least recently used key if the store is full. Update store to reflect recent access. """ - if x not in self.key_reference_map: + if x not in self.key_reference: if len(self.dq_store) == LRUCache._MAX_CAPACITY: last_element = self.dq_store.pop() - self.key_reference_map.remove(last_element) + self.key_reference.remove(last_element) else: self.dq_store.remove(x) self.dq_store.appendleft(x) - self.key_reference_map.add(x) + self.key_reference.add(x) - def display(self): + def display(self) -> None: """ Prints all the elements in the store. """ From 77e4209a7e9e8328279b705e280d77bee404b775 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 28 Oct 2021 19:58:48 -0700 Subject: [PATCH 04/18] [mypy] updates example to demonstrate LRUCache with complex type --- other/least_recently_used.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/other/least_recently_used.py b/other/least_recently_used.py index 43647da72d89..2b04cc6e5350 100644 --- a/other/least_recently_used.py +++ b/other/least_recently_used.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import sys from collections import deque from typing import Generic, TypeVar @@ -51,11 +53,11 @@ def display(self) -> None: if __name__ == "__main__": - lru_cache = LRUCache(4) - lru_cache.refer(1) + lru_cache: LRUCache[str | int] = LRUCache(4) + lru_cache.refer("A") lru_cache.refer(2) lru_cache.refer(3) - lru_cache.refer(1) + lru_cache.refer("A") lru_cache.refer(4) lru_cache.refer(5) lru_cache.display() From ec878d9f8b908a4c1fe04c186a7bb9d52066cd87 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 28 Oct 2021 21:59:25 -0700 Subject: [PATCH 05/18] Adds doctest to other/least_recently_used --- other/least_recently_used.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/other/least_recently_used.py b/other/least_recently_used.py index 2b04cc6e5350..cb692bb1b1c0 100644 --- a/other/least_recently_used.py +++ b/other/least_recently_used.py @@ -8,7 +8,27 @@ class LRUCache(Generic[T]): - """Page Replacement Algorithm, Least Recently Used (LRU) Caching.""" + """ + Page Replacement Algorithm, Least Recently Used (LRU) Caching. + + >>> lru_cache: LRUCache[str | int] = LRUCache(4) + >>> lru_cache.refer("A") + >>> lru_cache.refer(2) + >>> lru_cache.refer(3) + + >>> lru_cache + LRUCache(4) => [3, 2, 'A'] + + >>> lru_cache.refer("A") + >>> lru_cache + LRUCache(4) => ['A', 3, 2] + + >>> lru_cache.refer(4) + >>> lru_cache.refer(5) + >>> lru_cache + LRUCache(4) => [5, 4, 'A', 3] + + """ dq_store: deque[T] # Cache store of keys key_reference: set[T] # References of the keys in cache @@ -50,8 +70,14 @@ def display(self) -> None: for k in self.dq_store: print(k) + def __repr__(self) -> str: + return f"LRUCache({self._MAX_CAPACITY}) => {list(self.dq_store)}" + if __name__ == "__main__": + import doctest + + doctest.testmod() lru_cache: LRUCache[str | int] = LRUCache(4) lru_cache.refer("A") @@ -61,3 +87,6 @@ def display(self) -> None: lru_cache.refer(4) lru_cache.refer(5) lru_cache.display() + + print(lru_cache) + assert str(lru_cache) == "LRUCache(4) => [5, 4, 'A', 3]" From 2fd99ca9212cb76116f69d814a40fcf054890e31 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 12:34:36 +0100 Subject: [PATCH 06/18] mypy.ini: Remove exclude = (other/least_recently_used.py) --- mypy.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 7dbc7c4ffc80..de473bbb2bc6 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,3 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (other/least_recently_used.py) From d7cdb96d2433bfbe7ef2f1e433a49896a21b347f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 12:42:09 +0100 Subject: [PATCH 07/18] Various mypy configs --- .github/workflows/build.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e5f8d6b39a7b..2c9520ba95f2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,7 +21,10 @@ jobs: run: | python -m pip install --upgrade pip setuptools six wheel python -m pip install mypy pytest-cov -r requirements.txt - - run: mypy . # See `mypy.ini` for configuration settings. + - run: mypy --ignore-missing-imports . || true + - run: mypy --install-types . || true + - run: mypy --install-types --non-interactive . || true + - run: mypy --ignore-missing-imports --install-types --non-interactive . - name: Run tests run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/validate_solutions.py --cov-report=term-missing:skip-covered --cov=. . - if: ${{ success() }} From eb9a038b697341b2f0458b937bd98038d1e97e1b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 12:42:34 +0100 Subject: [PATCH 08/18] Delete mypy.ini --- mypy.ini | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 mypy.ini diff --git a/mypy.ini b/mypy.ini deleted file mode 100644 index de473bbb2bc6..000000000000 --- a/mypy.ini +++ /dev/null @@ -1,4 +0,0 @@ -[mypy] -ignore_missing_imports = True -install_types = True -non_interactive = True From e9d83aca09410e1420e5df41636739567a9cdec0 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 12:51:29 +0100 Subject: [PATCH 09/18] Add mypy to .pre-commit-config.yaml --- .pre-commit-config.yaml | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e60003051365..62f0d491c529 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,22 +12,26 @@ repos: data_structures/heap/binomial_heap.py )$ - id: requirements-txt-fixer + - repo: https://github.com/psf/black rev: 21.4b0 hooks: - id: black + - repo: https://github.com/PyCQA/isort rev: 5.8.0 hooks: - id: isort args: - --profile=black + - repo: https://github.com/asottile/pyupgrade rev: v2.29.0 hooks: - id: pyupgrade args: - --py39-plus + - repo: https://gitlab.com/pycqa/flake8 rev: 3.9.1 hooks: @@ -36,13 +40,16 @@ repos: - --ignore=E203,W503 - --max-complexity=25 - --max-line-length=88 -# FIXME: fix mypy errors and then uncomment this -# - repo: https://github.com/pre-commit/mirrors-mypy -# rev: v0.782 -# hooks: -# - id: mypy -# args: -# - --ignore-missing-imports + + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.782 + hooks: + - id: mypy + args: + - --ignore-missing-imports + additional_dependencies: + - types-all + - repo: https://github.com/codespell-project/codespell rev: v2.0.0 hooks: @@ -50,13 +57,13 @@ repos: args: - --ignore-words-list=ans,crate,fo,followings,hist,iff,mater,secant,som,tim - --skip="./.*,./strings/dictionary.txt,./strings/words.txt,./project_euler/problem_022/p022_names.txt" - - --quiet-level=2 exclude: | (?x)^( strings/dictionary.txt | strings/words.txt | project_euler/problem_022/p022_names.txt )$ + - repo: local hooks: - id: validate-filenames From ab28a5ee9578e8693f6d7f3d20fb42fd8b8b8a02 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 12:52:16 +0100 Subject: [PATCH 10/18] mypy --ignore-missing-imports --install-types --non-interactive . --- .github/workflows/build.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2c9520ba95f2..df000cda5997 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,9 +21,6 @@ jobs: run: | python -m pip install --upgrade pip setuptools six wheel python -m pip install mypy pytest-cov -r requirements.txt - - run: mypy --ignore-missing-imports . || true - - run: mypy --install-types . || true - - run: mypy --install-types --non-interactive . || true - run: mypy --ignore-missing-imports --install-types --non-interactive . - name: Run tests run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/validate_solutions.py --cov-report=term-missing:skip-covered --cov=. . From 14e8583394f4e6ee345e1fa45f86421a71b03ad7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 13:02:44 +0100 Subject: [PATCH 11/18] mypy v0.910 --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 62f0d491c529..57908d0d2285 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -42,7 +42,7 @@ repos: - --max-line-length=88 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.782 + rev: v0.910 hooks: - id: mypy args: From c13681402dc26706b6bc02d1bc46e25f55d9e12c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 13:11:22 +0100 Subject: [PATCH 12/18] Pillow=8.3.7 --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 57908d0d2285..62e87afea29a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -48,6 +48,7 @@ repos: args: - --ignore-missing-imports additional_dependencies: + - Pillow=8.3.7 - types-all - repo: https://github.com/codespell-project/codespell From f7aab7d9764d220db1fc0916446074301fc9a4b4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 13:13:23 +0100 Subject: [PATCH 13/18] Pillow==8.3.7 --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 62e87afea29a..06608f7ac84f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -48,7 +48,7 @@ repos: args: - --ignore-missing-imports additional_dependencies: - - Pillow=8.3.7 + - Pillow==8.3.7 - types-all - repo: https://github.com/codespell-project/codespell From 17f3054f0302822d22c554479654d4df764e4eed Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 13:16:21 +0100 Subject: [PATCH 14/18] Pillow==8.3.2 --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 06608f7ac84f..633817bec308 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -48,7 +48,7 @@ repos: args: - --ignore-missing-imports additional_dependencies: - - Pillow==8.3.7 + - Pillow==8.3.2 - types-all - repo: https://github.com/codespell-project/codespell From 1a188cc9f30b0e9a0d03bf0c2911f70a7b7a8ef8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 13:26:26 +0100 Subject: [PATCH 15/18] Update .pre-commit-config.yaml --- .pre-commit-config.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 633817bec308..9e43bb4a9cdb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -45,11 +45,6 @@ repos: rev: v0.910 hooks: - id: mypy - args: - - --ignore-missing-imports - additional_dependencies: - - Pillow==8.3.2 - - types-all - repo: https://github.com/codespell-project/codespell rev: v2.0.0 From 4b1cd7b783a9fa82bec07fa06bda67d5fdee3a6c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 13:31:12 +0100 Subject: [PATCH 16/18] Update requirements.txt --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index e01d87cffabe..9a26dcc21f36 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,5 +16,4 @@ sympy tensorflow texttable tweepy -types-requests xgboost From ab109efa5e8f692ee722c4d20766f79c93ec6c12 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 13:36:48 +0100 Subject: [PATCH 17/18] Update pre-commit.yml --- .github/workflows/pre-commit.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 27a5a97c0b6c..19196098b1c1 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -14,6 +14,8 @@ jobs: ~/.cache/pip key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }} - uses: actions/setup-python@v2 + with: + python-version: 3.9 - uses: psf/black@21.4b0 - name: Install pre-commit run: | From 60659568314644c37a7f63188c0d76712f45dba4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Nov 2021 13:44:06 +0100 Subject: [PATCH 18/18] --install-types # See mirrors-mypy README.md --- .pre-commit-config.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9e43bb4a9cdb..0ebd6dfa0d7e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -45,6 +45,10 @@ repos: rev: v0.910 hooks: - id: mypy + args: + - --ignore-missing-imports + - --install-types # See mirrors-mypy README.md + - --non-interactive - repo: https://github.com/codespell-project/codespell rev: v2.0.0