From 5be2a1fd3ddf33bc377f6beb5d3d301d565e2ee9 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Tue, 1 Nov 2022 21:33:19 +0000 Subject: [PATCH 01/10] ci: Add `B023` to `.flake8` ignores --- .flake8 | 1 + genetic_algorithm/basic_string.py | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.flake8 b/.flake8 index 0d9ef18d142b..582234724b78 100644 --- a/.flake8 +++ b/.flake8 @@ -6,3 +6,4 @@ extend-ignore = # Formatting style for `black` E203 # Whitespace before ':' W503 # Line break occurred before a binary operator + B023 # Function definition does not bind loop variable \ No newline at end of file diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index 45b8be651f6e..7c9b31f18b8b 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -129,9 +129,7 @@ def select(parent_1: tuple[str, float]) -> list[str]: child_n = int(parent_1[1] * 100) + 1 child_n = 10 if child_n >= 10 else child_n for _ in range(child_n): - parent_2 = population_score[ # noqa: B023 - random.randint(0, N_SELECTED) - ][0] + parent_2 = population_score[random.randint(0, N_SELECTED)][0] child_1, child_2 = crossover(parent_1[0], parent_2) # Append new string to the population list. From 7f0a525b2992f9ead98873fac35f61c4ff3fe591 Mon Sep 17 00:00:00 2001 From: Caeden Perelli-Harris Date: Tue, 1 Nov 2022 21:47:29 +0000 Subject: [PATCH 02/10] Update genetic_algorithm/basic_string.py Co-authored-by: Christian Clauss --- genetic_algorithm/basic_string.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index 7c9b31f18b8b..da6bf5db351a 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -129,7 +129,8 @@ def select(parent_1: tuple[str, float]) -> list[str]: child_n = int(parent_1[1] * 100) + 1 child_n = 10 if child_n >= 10 else child_n for _ in range(child_n): - parent_2 = population_score[random.randint(0, N_SELECTED)][0] + random_x = random.randint(0, N_SELECTED) + parent_2 = population_score[random_x][0] child_1, child_2 = crossover(parent_1[0], parent_2) # Append new string to the population list. From 60f8566e67aab4fc5ae69ea5f90a7b11ac139af7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 2 Nov 2022 01:01:44 +0100 Subject: [PATCH 03/10] Update .flake8 --- .flake8 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.flake8 b/.flake8 index 582234724b78..aba6c25bde96 100644 --- a/.flake8 +++ b/.flake8 @@ -1,9 +1,7 @@ [flake8] max-line-length = 88 -max-complexity = 25 +max-complexity = 24 extend-ignore = - A003 # Class attribute is shadowing a python builtin # Formatting style for `black` E203 # Whitespace before ':' W503 # Line break occurred before a binary operator - B023 # Function definition does not bind loop variable \ No newline at end of file From 0c8fe6df97fee5a74d7583364207398f60c083c7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 2 Nov 2022 08:45:54 +0100 Subject: [PATCH 04/10] Fix shadowing --- data_structures/heap/heap.py | 12 +++---- .../linked_list/merge_two_lists.py | 4 +-- data_structures/queue/double_ended_queue.py | 31 +++++++++---------- linear_algebra/src/lib.py | 12 ------- other/lfu_cache.py | 14 ++++----- other/lru_cache.py | 14 ++++----- 6 files changed, 37 insertions(+), 50 deletions(-) diff --git a/data_structures/heap/heap.py b/data_structures/heap/heap.py index 071790d18448..92e90bec33cc 100644 --- a/data_structures/heap/heap.py +++ b/data_structures/heap/heap.py @@ -88,12 +88,12 @@ def build_max_heap(self, collection: Iterable[float]) -> None: for i in range(self.heap_size // 2 - 1, -1, -1): self.max_heapify(i) - def max(self) -> float: - """return the max in the heap""" - if self.heap_size >= 1: - return self.h[0] - else: - raise Exception("Empty heap") + # def max(self) -> float: + # """return the max in the heap""" + # if self.heap_size >= 1: + # return self.h[0] + # else: + # raise Exception("Empty heap") def extract_max(self) -> float: """get and remove max from heap""" diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py index 93cf7a7e1602..61e2412aa7fd 100644 --- a/data_structures/linked_list/merge_two_lists.py +++ b/data_structures/linked_list/merge_two_lists.py @@ -13,7 +13,7 @@ @dataclass class Node: data: int - next: Node | None + next_node: Node | None class SortedLinkedList: @@ -32,7 +32,7 @@ def __iter__(self) -> Iterator[int]: node = self.head while node: yield node.data - node = node.next + node = node.next_node def __len__(self) -> int: """ diff --git a/data_structures/queue/double_ended_queue.py b/data_structures/queue/double_ended_queue.py index 7053879d4512..11942db8305c 100644 --- a/data_structures/queue/double_ended_queue.py +++ b/data_structures/queue/double_ended_queue.py @@ -42,8 +42,8 @@ class _Node: """ val: Any = None - next: Deque._Node | None = None - prev: Deque._Node | None = None + next_node: Deque._Node | None = None + prev_node: Deque._Node | None = None class _Iterator: """ @@ -81,7 +81,7 @@ def __next__(self) -> Any: # finished iterating raise StopIteration val = self._cur.val - self._cur = self._cur.next + self._cur = self._cur.next_node return val @@ -128,8 +128,8 @@ def append(self, val: Any) -> None: self._len = 1 else: # connect nodes - self._back.next = node - node.prev = self._back + self._back.next_node = node + node.prev_node = self._back self._back = node # assign new back to the new node self._len += 1 @@ -170,8 +170,8 @@ def appendleft(self, val: Any) -> None: self._len = 1 else: # connect nodes - node.next = self._front - self._front.prev = node + node.next_node = self._front + self._front.prev_node = node self._front = node # assign new front to the new node self._len += 1 @@ -264,10 +264,9 @@ def pop(self) -> Any: assert not self.is_empty(), "Deque is empty." topop = self._back - self._back = self._back.prev # set new back - self._back.next = ( - None # drop the last node - python will deallocate memory automatically - ) + self._back = self._back.prev_node # set new back + # drop the last node - python will deallocate memory automatically + self._back.next_node = None self._len -= 1 @@ -300,8 +299,8 @@ def popleft(self) -> Any: assert not self.is_empty(), "Deque is empty." topop = self._front - self._front = self._front.next # set new front and drop the first node - self._front.prev = None + self._front = self._front.next_node # set new front and drop the first node + self._front.prev_node = None self._len -= 1 @@ -385,8 +384,8 @@ def __eq__(self, other: object) -> bool: # compare every value if me.val != oth.val: return False - me = me.next - oth = oth.next + me = me.next_node + oth = oth.next_node return True @@ -424,7 +423,7 @@ def __repr__(self) -> str: while aux is not None: # append the values in a list to display values_list.append(aux.val) - aux = aux.next + aux = aux.next_node return "[" + ", ".join(repr(val) for val in values_list) + "]" diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 079731487b3a..775e0244abb2 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -40,7 +40,6 @@ class Vector: __sub__(other: Vector): vector subtraction __mul__(other: float): scalar multiplication __mul__(other: Vector): dot product - set(components: Collection[float]): changes the vector components copy(): copies this vector and returns it component(i): gets the i-th component (0-indexed) change_component(pos: int, value: float): changes specified component @@ -119,17 +118,6 @@ def __mul__(self, other: float | Vector) -> float | Vector: else: # error case raise Exception("invalid operand!") - def set(self, components: Collection[float]) -> None: - """ - input: new components - changes the components of the vector. - replaces the components with newer one. - """ - if len(components) > 0: - self.__components = list(components) - else: - raise Exception("please give any vector") - def copy(self) -> Vector: """ copies this vector and returns it. diff --git a/other/lfu_cache.py b/other/lfu_cache.py index 2f26bb6cc74a..b68ba3a4605c 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -166,14 +166,14 @@ class LFUCache(Generic[T, U]): or as a function decorator. >>> cache = LFUCache(2) - >>> cache.set(1, 1) - >>> cache.set(2, 2) + >>> cache.put(1, 1) + >>> cache.put(2, 2) >>> cache.get(1) 1 - >>> cache.set(3, 3) + >>> cache.put(3, 3) >>> cache.get(2) is None True - >>> cache.set(4, 4) + >>> cache.put(4, 4) >>> cache.get(1) is None True >>> cache.get(3) @@ -224,7 +224,7 @@ def __contains__(self, key: T) -> bool: >>> 1 in cache False - >>> cache.set(1, 1) + >>> cache.put(1, 1) >>> 1 in cache True """ @@ -250,7 +250,7 @@ def get(self, key: T) -> U | None: self.miss += 1 return None - def set(self, key: T, value: U) -> None: + def put(self, key: T, value: U) -> None: """ Sets the value for the input key and updates the Double Linked List """ @@ -297,7 +297,7 @@ def cache_decorator_wrapper(*args: T) -> U: result = cls.decorator_function_to_instance_map[func].get(args[0]) if result is None: result = func(*args) - cls.decorator_function_to_instance_map[func].set(args[0], result) + cls.decorator_function_to_instance_map[func].put(args[0], result) return result def cache_info() -> LFUCache[T, U]: diff --git a/other/lru_cache.py b/other/lru_cache.py index aa910e487406..1e5eeac45b4e 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -150,8 +150,8 @@ class LRUCache(Generic[T, U]): >>> cache = LRUCache(2) - >>> cache.set(1, 1) - >>> cache.set(2, 2) + >>> cache.put(1, 1) + >>> cache.put(2, 2) >>> cache.get(1) 1 @@ -166,7 +166,7 @@ class LRUCache(Generic[T, U]): {1: Node: key: 1, val: 1, has next: True, has prev: True, \ 2: Node: key: 2, val: 2, has next: True, has prev: True} - >>> cache.set(3, 3) + >>> cache.put(3, 3) >>> cache.list DoubleLinkedList, @@ -182,7 +182,7 @@ class LRUCache(Generic[T, U]): >>> cache.get(2) is None True - >>> cache.set(4, 4) + >>> cache.put(4, 4) >>> cache.get(1) is None True @@ -238,7 +238,7 @@ def __contains__(self, key: T) -> bool: >>> 1 in cache False - >>> cache.set(1, 1) + >>> cache.put(1, 1) >>> 1 in cache True @@ -266,7 +266,7 @@ def get(self, key: T) -> U | None: self.miss += 1 return None - def set(self, key: T, value: U) -> None: + def put(self, key: T, value: U) -> None: """ Sets the value for the input key and updates the Double Linked List """ @@ -315,7 +315,7 @@ def cache_decorator_wrapper(*args: T) -> U: result = cls.decorator_function_to_instance_map[func].get(args[0]) if result is None: result = func(*args) - cls.decorator_function_to_instance_map[func].set(args[0], result) + cls.decorator_function_to_instance_map[func].put(args[0], result) return result def cache_info() -> LRUCache[T, U]: From b82dfc3ccd97718e5dd86889ce0bf56e47bdf143 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 2 Nov 2022 09:00:52 +0100 Subject: [PATCH 05/10] Fix shadowing --- data_structures/binary_tree/fenwick_tree.py | 8 ++++---- genetic_algorithm/basic_string.py | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/data_structures/binary_tree/fenwick_tree.py b/data_structures/binary_tree/fenwick_tree.py index 96020d1427af..babd75ac4b31 100644 --- a/data_structures/binary_tree/fenwick_tree.py +++ b/data_structures/binary_tree/fenwick_tree.py @@ -46,7 +46,7 @@ def init(self, arr: list[int]) -> None: self.size = len(arr) self.tree = deepcopy(arr) for i in range(1, self.size): - j = self.next(i) + j = self.next_(i) if j < self.size: self.tree[j] += self.tree[i] @@ -64,13 +64,13 @@ def get_array(self) -> list[int]: """ arr = self.tree[:] for i in range(self.size - 1, 0, -1): - j = self.next(i) + j = self.next_(i) if j < self.size: arr[j] -= arr[i] return arr @staticmethod - def next(index: int) -> int: + def next_(index: int) -> int: return index + (index & (-index)) @staticmethod @@ -102,7 +102,7 @@ def add(self, index: int, value: int) -> None: return while index < self.size: self.tree[index] += value - index = self.next(index) + index = self.next_(index) def update(self, index: int, value: int) -> None: """ diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index da6bf5db351a..c4fc885d977d 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -130,7 +130,10 @@ def select(parent_1: tuple[str, float]) -> list[str]: child_n = 10 if child_n >= 10 else child_n for _ in range(child_n): random_x = random.randint(0, N_SELECTED) - parent_2 = population_score[random_x][0] + # TODO: The BugBear B023 below should be fixed... + # https://github.com/PyCQA/flake8-bugbear#list-of-warnings + # https://github.com/PyCQA/flake8-bugbear/issues/269 + parent_2 = population_score[random_x][0] # noqa: B023 child_1, child_2 = crossover(parent_1[0], parent_2) # Append new string to the population list. From 6b4435945a0cbfb18aa45a389ec963a7832b4310 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 2 Nov 2022 09:05:13 +0100 Subject: [PATCH 06/10] pre-commit: Run yesqa before flake8 --- .pre-commit-config.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a0ea03b9b8cd..e382f57cef6c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,10 +33,10 @@ repos: args: - --py310-plus - - repo: https://github.com/PyCQA/flake8 - rev: 5.0.4 + - repo: https://github.com/asottile/yesqa + rev: v1.4.0 hooks: - - id: flake8 # See .flake8 for args + - id: yesqa additional_dependencies: &flake8-plugins - flake8-bugbear - flake8-builtins @@ -44,10 +44,10 @@ repos: - flake8-comprehensions - pep8-naming - - repo: https://github.com/asottile/yesqa - rev: v1.4.0 + - repo: https://github.com/PyCQA/flake8 + rev: 5.0.4 hooks: - - id: yesqa + - id: flake8 # See .flake8 for args additional_dependencies: *flake8-plugins From ee210fc7bd78a8292991d76eb6da108cb045d114 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 2 Nov 2022 09:19:39 +0100 Subject: [PATCH 07/10] Flake8: Reduce max-complexity --- .flake8 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.flake8 b/.flake8 index aba6c25bde96..0c36ffe81da3 100644 --- a/.flake8 +++ b/.flake8 @@ -1,6 +1,6 @@ [flake8] max-line-length = 88 -max-complexity = 24 +max-complexity = 23 # Should be 10 extend-ignore = # Formatting style for `black` E203 # Whitespace before ':' From 854e84e8541116752200cb37e0e0aa7e46181021 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Nov 2022 08:20:58 +0000 Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- arithmetic_analysis/newton_raphson.py | 2 +- arithmetic_analysis/newton_raphson_new.py | 2 +- audio_filters/butterworth_filter.py | 14 +++++++------- ciphers/rsa_key_generator.py | 4 ++-- .../binary_tree/binary_search_tree_recursive.py | 8 ++++---- .../binary_tree/non_recursive_segment_tree.py | 4 ++-- data_structures/binary_tree/segment_tree.py | 12 ++++++------ data_structures/heap/min_heap.py | 2 +- data_structures/linked_list/__init__.py | 2 +- data_structures/stacks/postfix_evaluation.py | 2 +- dynamic_programming/longest_common_subsequence.py | 2 +- .../longest_increasing_subsequence_o(nlogn).py | 4 ++-- fractals/mandelbrot.py | 2 +- genetic_algorithm/basic_string.py | 2 +- graphs/articulation_points.py | 2 +- graphs/dinic.py | 2 +- graphs/minimum_spanning_tree_prims.py | 4 ++-- graphs/tests/test_min_spanning_tree_prim.py | 2 +- hashes/sha1.py | 2 +- .../sequential_minimum_optimization.py | 2 +- maths/newton_raphson.py | 4 ++-- other/lfu_cache.py | 2 +- other/linear_congruential_generator.py | 2 +- other/lru_cache.py | 2 +- other/sdes.py | 4 ++-- physics/lorentz_transformation_four_vector.py | 2 +- physics/n_body_simulation.py | 2 +- project_euler/problem_011/sol2.py | 2 +- quantum/ripple_adder_classic.py | 2 +- strings/manacher.py | 2 +- 30 files changed, 50 insertions(+), 50 deletions(-) diff --git a/arithmetic_analysis/newton_raphson.py b/arithmetic_analysis/newton_raphson.py index 86ff9d350dde..8cec83a4fb67 100644 --- a/arithmetic_analysis/newton_raphson.py +++ b/arithmetic_analysis/newton_raphson.py @@ -5,7 +5,7 @@ from __future__ import annotations from decimal import Decimal -from math import * # noqa: F401, F403 +from math import * from sympy import diff diff --git a/arithmetic_analysis/newton_raphson_new.py b/arithmetic_analysis/newton_raphson_new.py index dd1d7e0929cf..dc28a67f4e59 100644 --- a/arithmetic_analysis/newton_raphson_new.py +++ b/arithmetic_analysis/newton_raphson_new.py @@ -8,7 +8,7 @@ # Newton's Method - https://en.wikipedia.org/wiki/Newton's_method from sympy import diff, lambdify, symbols -from sympy.functions import * # noqa: F401, F403 +from sympy.functions import * def newton_raphson( diff --git a/audio_filters/butterworth_filter.py b/audio_filters/butterworth_filter.py index cffedb7a68fd..8dd42dbc629e 100644 --- a/audio_filters/butterworth_filter.py +++ b/audio_filters/butterworth_filter.py @@ -11,7 +11,7 @@ def make_lowpass( - frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) # noqa: B008 + frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) ) -> IIRFilter: """ Creates a low-pass filter @@ -39,7 +39,7 @@ def make_lowpass( def make_highpass( - frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) # noqa: B008 + frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) ) -> IIRFilter: """ Creates a high-pass filter @@ -67,7 +67,7 @@ def make_highpass( def make_bandpass( - frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) # noqa: B008 + frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) ) -> IIRFilter: """ Creates a band-pass filter @@ -96,7 +96,7 @@ def make_bandpass( def make_allpass( - frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) # noqa: B008 + frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) ) -> IIRFilter: """ Creates an all-pass filter @@ -124,7 +124,7 @@ def make_peak( frequency: int, samplerate: int, gain_db: float, - q_factor: float = 1 / sqrt(2), # noqa: B008 + q_factor: float = 1 / sqrt(2), ) -> IIRFilter: """ Creates a peak filter @@ -156,7 +156,7 @@ def make_lowshelf( frequency: int, samplerate: int, gain_db: float, - q_factor: float = 1 / sqrt(2), # noqa: B008 + q_factor: float = 1 / sqrt(2), ) -> IIRFilter: """ Creates a low-shelf filter @@ -193,7 +193,7 @@ def make_highshelf( frequency: int, samplerate: int, gain_db: float, - q_factor: float = 1 / sqrt(2), # noqa: B008 + q_factor: float = 1 / sqrt(2), ) -> IIRFilter: """ Creates a high-shelf filter diff --git a/ciphers/rsa_key_generator.py b/ciphers/rsa_key_generator.py index 2573ed01387b..d24b4f2bf2c8 100644 --- a/ciphers/rsa_key_generator.py +++ b/ciphers/rsa_key_generator.py @@ -2,8 +2,8 @@ import random import sys -from . import cryptomath_module as cryptoMath # noqa: N812 -from . import rabin_miller as rabinMiller # noqa: N812 +from . import cryptomath_module as cryptoMath +from . import rabin_miller as rabinMiller def main() -> None: diff --git a/data_structures/binary_tree/binary_search_tree_recursive.py b/data_structures/binary_tree/binary_search_tree_recursive.py index 97eb8e25bedd..0d0ac8fd1e22 100644 --- a/data_structures/binary_tree/binary_search_tree_recursive.py +++ b/data_structures/binary_tree/binary_search_tree_recursive.py @@ -357,7 +357,7 @@ def test_put(self) -> None: assert t.root.left.left.parent == t.root.left assert t.root.left.left.label == 1 - with self.assertRaises(Exception): # noqa: B017 + with self.assertRaises(Exception): t.put(1) def test_search(self) -> None: @@ -369,7 +369,7 @@ def test_search(self) -> None: node = t.search(13) assert node.label == 13 - with self.assertRaises(Exception): # noqa: B017 + with self.assertRaises(Exception): t.search(2) def test_remove(self) -> None: @@ -515,7 +515,7 @@ def test_get_max_label(self) -> None: assert t.get_max_label() == 14 t.empty() - with self.assertRaises(Exception): # noqa: B017 + with self.assertRaises(Exception): t.get_max_label() def test_get_min_label(self) -> None: @@ -524,7 +524,7 @@ def test_get_min_label(self) -> None: assert t.get_min_label() == 1 t.empty() - with self.assertRaises(Exception): # noqa: B017 + with self.assertRaises(Exception): t.get_min_label() def test_inorder_traversal(self) -> None: diff --git a/data_structures/binary_tree/non_recursive_segment_tree.py b/data_structures/binary_tree/non_recursive_segment_tree.py index 075ff6c912ff..255df8505c04 100644 --- a/data_structures/binary_tree/non_recursive_segment_tree.py +++ b/data_structures/binary_tree/non_recursive_segment_tree.py @@ -86,7 +86,7 @@ def update(self, p: int, v: T) -> None: p = p // 2 self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1]) - def query(self, l: int, r: int) -> T | None: # noqa: E741 + def query(self, l: int, r: int) -> T | None: """ Get range query value in log(N) time :param l: left element index @@ -106,7 +106,7 @@ def query(self, l: int, r: int) -> T | None: # noqa: E741 l, r = l + self.N, r + self.N res: T | None = None - while l <= r: # noqa: E741 + while l <= r: if l % 2 == 1: res = self.st[l] if res is None else self.fn(res, self.st[l]) if r % 2 == 0: diff --git a/data_structures/binary_tree/segment_tree.py b/data_structures/binary_tree/segment_tree.py index 949a3ecdd32c..011db07e6e5c 100644 --- a/data_structures/binary_tree/segment_tree.py +++ b/data_structures/binary_tree/segment_tree.py @@ -15,8 +15,8 @@ def left(self, idx): def right(self, idx): return idx * 2 + 1 - def build(self, idx, l, r): # noqa: E741 - if l == r: # noqa: E741 + def build(self, idx, l, r): + if l == r: self.st[idx] = A[l] else: mid = (l + r) // 2 @@ -27,13 +27,13 @@ def build(self, idx, l, r): # noqa: E741 def update(self, a, b, val): return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val) - def update_recursive(self, idx, l, r, a, b, val): # noqa: E741 + def update_recursive(self, idx, l, r, a, b, val): """ update(1, 1, N, a, b, v) for update val v to [a,b] """ if r < a or l > b: return True - if l == r: # noqa: E741 + if l == r: self.st[idx] = val return True mid = (l + r) // 2 @@ -45,13 +45,13 @@ def update_recursive(self, idx, l, r, a, b, val): # noqa: E741 def query(self, a, b): return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1) - def query_recursive(self, idx, l, r, a, b): # noqa: E741 + def query_recursive(self, idx, l, r, a, b): """ query(1, 1, N, a, b) for query max of [a,b] """ if r < a or l > b: return -math.inf - if l >= a and r <= b: # noqa: E741 + if l >= a and r <= b: return self.st[idx] mid = (l + r) // 2 q1 = self.query_recursive(self.left(idx), l, mid, a, b) diff --git a/data_structures/heap/min_heap.py b/data_structures/heap/min_heap.py index 0403624f285a..456977fef11b 100644 --- a/data_structures/heap/min_heap.py +++ b/data_structures/heap/min_heap.py @@ -66,7 +66,7 @@ def build_heap(self, array): # this is min-heapify method def sift_down(self, idx, array): while True: - l = self.get_left_child_idx(idx) # noqa: E741 + l = self.get_left_child_idx(idx) r = self.get_right_child_idx(idx) smallest = idx diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 85660a6d2c27..6ba660231ae1 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -11,7 +11,7 @@ class Node: - def __init__(self, item: Any, next: Any) -> None: # noqa: A002 + def __init__(self, item: Any, next: Any) -> None: self.item = item self.next = next diff --git a/data_structures/stacks/postfix_evaluation.py b/data_structures/stacks/postfix_evaluation.py index 28128f82ec19..c03672303e57 100644 --- a/data_structures/stacks/postfix_evaluation.py +++ b/data_structures/stacks/postfix_evaluation.py @@ -22,7 +22,7 @@ def solve(post_fix): stack = [] - div = lambda x, y: int(x / y) # noqa: E731 integer division operation + div = lambda x, y: int(x / y) # integer division operation opr = { "^": op.pow, "*": op.mul, diff --git a/dynamic_programming/longest_common_subsequence.py b/dynamic_programming/longest_common_subsequence.py index 3468fd87da8d..0da181eb113f 100644 --- a/dynamic_programming/longest_common_subsequence.py +++ b/dynamic_programming/longest_common_subsequence.py @@ -38,7 +38,7 @@ def longest_common_subsequence(x: str, y: str): n = len(y) # declaring the array for storing the dp values - l = [[0] * (n + 1) for _ in range(m + 1)] # noqa: E741 + l = [[0] * (n + 1) for _ in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): diff --git a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py index 5e11d729f395..44e333e97779 100644 --- a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py +++ b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py @@ -7,13 +7,13 @@ from __future__ import annotations -def ceil_index(v, l, r, key): # noqa: E741 +def ceil_index(v, l, r, key): while r - l > 1: m = (l + r) // 2 if v[m] >= key: r = m else: - l = m # noqa: E741 + l = m return r diff --git a/fractals/mandelbrot.py b/fractals/mandelbrot.py index f97bcd17031c..5d61b72e172f 100644 --- a/fractals/mandelbrot.py +++ b/fractals/mandelbrot.py @@ -36,7 +36,7 @@ def get_distance(x: float, y: float, max_step: int) -> float: """ a = x b = y - for step in range(max_step): # noqa: B007 + for step in range(max_step): a_new = a * a - b * b + x b = 2 * a * b + y a = a_new diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index c4fc885d977d..ea8b3a3dc977 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -133,7 +133,7 @@ def select(parent_1: tuple[str, float]) -> list[str]: # TODO: The BugBear B023 below should be fixed... # https://github.com/PyCQA/flake8-bugbear#list-of-warnings # https://github.com/PyCQA/flake8-bugbear/issues/269 - parent_2 = population_score[random_x][0] # noqa: B023 + parent_2 = population_score[random_x][0] child_1, child_2 = crossover(parent_1[0], parent_2) # Append new string to the population list. diff --git a/graphs/articulation_points.py b/graphs/articulation_points.py index d28045282425..3fcaffd73725 100644 --- a/graphs/articulation_points.py +++ b/graphs/articulation_points.py @@ -1,5 +1,5 @@ # Finding Articulation Points in Undirected Graph -def compute_ap(l): # noqa: E741 +def compute_ap(l): n = len(l) out_edge_count = 0 low = [0] * n diff --git a/graphs/dinic.py b/graphs/dinic.py index aaf3a119525c..4f5e81236984 100644 --- a/graphs/dinic.py +++ b/graphs/dinic.py @@ -37,7 +37,7 @@ def depth_first_search(self, vertex, sink, flow): # Here we calculate the flow that reaches the sink def max_flow(self, source, sink): flow, self.q[0] = 0, source - for l in range(31): # noqa: E741 l = 30 maybe faster for random data + for l in range(31): # l = 30 maybe faster for random data while True: self.lvl, self.ptr = [0] * len(self.q), [0] * len(self.q) qi, qe, self.lvl[source] = 0, 1, 1 diff --git a/graphs/minimum_spanning_tree_prims.py b/graphs/minimum_spanning_tree_prims.py index 5b2eaa4bff40..9e68abb102a8 100644 --- a/graphs/minimum_spanning_tree_prims.py +++ b/graphs/minimum_spanning_tree_prims.py @@ -2,7 +2,7 @@ from collections import defaultdict -def prisms_algorithm(l): # noqa: E741 +def prisms_algorithm(l): node_position = [] @@ -110,7 +110,7 @@ def delete_minimum(heap, positions): e = int(input("Enter number of edges: ").strip()) adjlist = defaultdict(list) for x in range(e): - l = [int(x) for x in input().strip().split()] # noqa: E741 + l = [int(x) for x in input().strip().split()] adjlist[l[0]].append([l[1], l[2]]) adjlist[l[1]].append([l[0], l[2]]) print(prisms_algorithm(adjlist)) diff --git a/graphs/tests/test_min_spanning_tree_prim.py b/graphs/tests/test_min_spanning_tree_prim.py index 91feab28fc81..0ff70a31bea5 100644 --- a/graphs/tests/test_min_spanning_tree_prim.py +++ b/graphs/tests/test_min_spanning_tree_prim.py @@ -4,7 +4,7 @@ def test_prim_successful_result(): - num_nodes, num_edges = 9, 14 # noqa: F841 + num_nodes, num_edges = 9, 14 edges = [ [0, 1, 4], [0, 7, 8], diff --git a/hashes/sha1.py b/hashes/sha1.py index b19e0cfafea3..dde1efc557bb 100644 --- a/hashes/sha1.py +++ b/hashes/sha1.py @@ -133,7 +133,7 @@ class SHA1HashTest(unittest.TestCase): Test class for the SHA1Hash class. Inherits the TestCase class from unittest """ - def testMatchHashes(self): # noqa: N802 + def testMatchHashes(self): msg = bytes("Test String", "utf-8") self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest()) diff --git a/machine_learning/sequential_minimum_optimization.py b/machine_learning/sequential_minimum_optimization.py index 66535e806c43..12245bc5d957 100644 --- a/machine_learning/sequential_minimum_optimization.py +++ b/machine_learning/sequential_minimum_optimization.py @@ -314,7 +314,7 @@ def _get_new_alpha(self, i1, i2, a1, a2, e1, e2, y1, y2): l, h = max(0.0, a2 - a1), min(self._c, self._c + a2 - a1) else: l, h = max(0.0, a2 + a1 - self._c), min(self._c, a2 + a1) - if l == h: # noqa: E741 + if l == h: return None, None # calculate eta diff --git a/maths/newton_raphson.py b/maths/newton_raphson.py index f2b7cb9766d2..99d265994eae 100644 --- a/maths/newton_raphson.py +++ b/maths/newton_raphson.py @@ -23,7 +23,7 @@ def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=Fa a = x0 # set the initial guess steps = [a] error = abs(f(a)) - f1 = lambda x: calc_derivative(f, x, h=step) # noqa: E731 Derivative of f(x) + f1 = lambda x: calc_derivative(f, x, h=step) # Derivative of f(x) for _ in range(maxiter): if f1(a) == 0: raise ValueError("No converging solution found") @@ -43,7 +43,7 @@ def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=Fa if __name__ == "__main__": from matplotlib import pyplot as plt - f = lambda x: m.tanh(x) ** 2 - m.exp(3 * x) # noqa: E731 + f = lambda x: m.tanh(x) ** 2 - m.exp(3 * x) solution, error, steps = newton_raphson( f, x0=10, maxiter=1000, step=1e-6, logsteps=True ) diff --git a/other/lfu_cache.py b/other/lfu_cache.py index b68ba3a4605c..46ac234f35bc 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -303,7 +303,7 @@ def cache_decorator_wrapper(*args: T) -> U: def cache_info() -> LFUCache[T, U]: return cls.decorator_function_to_instance_map[func] - setattr(cache_decorator_wrapper, "cache_info", cache_info) # noqa: B010 + setattr(cache_decorator_wrapper, "cache_info", cache_info) return cache_decorator_wrapper diff --git a/other/linear_congruential_generator.py b/other/linear_congruential_generator.py index 777ee6355b9b..c6bf2280dd7e 100644 --- a/other/linear_congruential_generator.py +++ b/other/linear_congruential_generator.py @@ -14,7 +14,7 @@ class LinearCongruentialGenerator: # will only be called once per instance and it ensures that each instance will # generate a unique sequence of numbers. - def __init__(self, multiplier, increment, modulo, seed=int(time())): # noqa: B008 + def __init__(self, multiplier, increment, modulo, seed=int(time())): """ These parameters are saved and used when nextNumber() is called. diff --git a/other/lru_cache.py b/other/lru_cache.py index 1e5eeac45b4e..cfdb0aced83e 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -321,7 +321,7 @@ def cache_decorator_wrapper(*args: T) -> U: def cache_info() -> LRUCache[T, U]: return cls.decorator_function_to_instance_map[func] - setattr(cache_decorator_wrapper, "cache_info", cache_info) # noqa: B010 + setattr(cache_decorator_wrapper, "cache_info", cache_info) return cache_decorator_wrapper diff --git a/other/sdes.py b/other/sdes.py index 695675000632..7d1c01d00f46 100644 --- a/other/sdes.py +++ b/other/sdes.py @@ -44,9 +44,9 @@ def function(expansion, s0, s1, key, message): right = message[4:] temp = apply_table(right, expansion) temp = xor(temp, key) - l = apply_sbox(s0, temp[:4]) # noqa: E741 + l = apply_sbox(s0, temp[:4]) r = apply_sbox(s1, temp[4:]) - l = "0" * (2 - len(l)) + l # noqa: E741 + l = "0" * (2 - len(l)) + l r = "0" * (2 - len(r)) + r temp = apply_table(l + r, p4_table) temp = xor(left, temp) diff --git a/physics/lorentz_transformation_four_vector.py b/physics/lorentz_transformation_four_vector.py index f58b40e5906b..bda852c25520 100644 --- a/physics/lorentz_transformation_four_vector.py +++ b/physics/lorentz_transformation_four_vector.py @@ -145,7 +145,7 @@ def transformation_matrix(velocity: float) -> np.array: def transform( - velocity: float, event: np.array = np.zeros(4), symbolic: bool = True # noqa: B008 + velocity: float, event: np.array = np.zeros(4), symbolic: bool = True ) -> np.array: """ >>> transform(29979245,np.array([1,2,3,4]), False) diff --git a/physics/n_body_simulation.py b/physics/n_body_simulation.py index 2b701283f166..cdd05b52737e 100644 --- a/physics/n_body_simulation.py +++ b/physics/n_body_simulation.py @@ -246,7 +246,7 @@ def update(frame: int) -> list[plt.Circle]: update_step(body_system, DELTA_TIME, patches) return patches - anim = animation.FuncAnimation( # noqa: F841 + anim = animation.FuncAnimation( fig, update, interval=INTERVAL, blit=True ) diff --git a/project_euler/problem_011/sol2.py b/project_euler/problem_011/sol2.py index 9ea0db991aaf..2958305331a9 100644 --- a/project_euler/problem_011/sol2.py +++ b/project_euler/problem_011/sol2.py @@ -35,7 +35,7 @@ def solution(): 70600674 """ with open(os.path.dirname(__file__) + "/grid.txt") as f: - l = [] # noqa: E741 + l = [] for _ in range(20): l.append([int(x) for x in f.readline().split()]) diff --git a/quantum/ripple_adder_classic.py b/quantum/ripple_adder_classic.py index c07757af7fff..c58f458923df 100644 --- a/quantum/ripple_adder_classic.py +++ b/quantum/ripple_adder_classic.py @@ -62,7 +62,7 @@ def full_adder( def ripple_adder( val1: int, val2: int, - backend: Backend = qiskit.Aer.get_backend("aer_simulator"), # noqa: B008 + backend: Backend = qiskit.Aer.get_backend("aer_simulator"), ) -> int: """ Quantum Equivalent of a Ripple Adder Circuit diff --git a/strings/manacher.py b/strings/manacher.py index c58c7c19ec44..ca546e533acd 100644 --- a/strings/manacher.py +++ b/strings/manacher.py @@ -50,7 +50,7 @@ def palindromic_string(input_string: str) -> str: # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: - l = j - k + 1 # noqa: E741 + l = j - k + 1 r = j + k - 1 # update max_length and start position From 5e066d7c88b7e1df6ccb3c2a8fc3fe02b3f1da69 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Nov 2022 17:17:47 +0000 Subject: [PATCH 09/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/n_body_simulation.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/physics/n_body_simulation.py b/physics/n_body_simulation.py index cdd05b52737e..0fe6adf223df 100644 --- a/physics/n_body_simulation.py +++ b/physics/n_body_simulation.py @@ -246,9 +246,7 @@ def update(frame: int) -> list[plt.Circle]: update_step(body_system, DELTA_TIME, patches) return patches - anim = animation.FuncAnimation( - fig, update, interval=INTERVAL, blit=True - ) + anim = animation.FuncAnimation(fig, update, interval=INTERVAL, blit=True) plt.show() From 201c1a37512d37058e265a06b944081d76136dfb Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Wed, 2 Nov 2022 17:24:23 +0000 Subject: [PATCH 10/10] fix: Move comment to newline --- .flake8 | 4 +++- physics/n_body_simulation.py | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.flake8 b/.flake8 index 0c36ffe81da3..960c6ef6ad61 100644 --- a/.flake8 +++ b/.flake8 @@ -1,6 +1,8 @@ [flake8] max-line-length = 88 -max-complexity = 23 # Should be 10 + +# Should be 10 +max-complexity = 23 extend-ignore = # Formatting style for `black` E203 # Whitespace before ':' diff --git a/physics/n_body_simulation.py b/physics/n_body_simulation.py index cdd05b52737e..0fe6adf223df 100644 --- a/physics/n_body_simulation.py +++ b/physics/n_body_simulation.py @@ -246,9 +246,7 @@ def update(frame: int) -> list[plt.Circle]: update_step(body_system, DELTA_TIME, patches) return patches - anim = animation.FuncAnimation( - fig, update, interval=INTERVAL, blit=True - ) + anim = animation.FuncAnimation(fig, update, interval=INTERVAL, blit=True) plt.show()