Skip to content

Commit

Permalink
Update source to conform with ruff linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Oct 25, 2023
1 parent 7ad35dd commit 4f69f40
Show file tree
Hide file tree
Showing 71 changed files with 134 additions and 262 deletions.
2 changes: 1 addition & 1 deletion scripts/create_conway_polys_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main():
degree = int(items[1])

# Degree-descending coefficients
coeffs = np.array(eval(items[2])[::-1]) # pylint: disable=eval-used
coeffs = np.array(eval(items[2])[::-1])

idxs = np.nonzero(coeffs)[0]
nonzero_degrees = (degree - idxs).tolist()
Expand Down
4 changes: 2 additions & 2 deletions scripts/create_irreducible_polys_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"""
from __future__ import annotations

import hashlib
import io
import os
import sqlite3
from pathlib import Path

import requests
import hashlib
import io
from pdfminer.high_level import extract_text


Expand Down
4 changes: 1 addition & 3 deletions scripts/generate_fec_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
Install SageMath:
* `sudo apt install sagemath`
"""
import json
import os
import pickle
import random
import shutil

import numpy as np
Expand Down Expand Up @@ -42,7 +40,7 @@ def to_field(field, integer):
return field(" + ".join(l))
try:
return field.fetch_int(int(integer))
except:
except: # noqa: E722
return field(integer)


Expand Down
19 changes: 10 additions & 9 deletions scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def set_seed(seed):
random.seed(seed)


def I(element):
def I(element): # noqa: E743
"""Convert from various finite field elements to an integer"""
if isinstance(element, sage.rings.finite_rings.element_pari_ffelt.FiniteFieldElement_pari_ffelt):
coeffs = element._vector_()
Expand All @@ -63,7 +63,7 @@ def F(integer):
return FIELD(" + ".join(l))
try:
return FIELD.fetch_int(int(integer))
except:
except: # noqa: E722
return FIELD(integer)


Expand All @@ -74,7 +74,7 @@ def arange(low, high, sparse=False):
else:
X = np.empty(SPARSE_SIZE, dtype=object)
iterator = np.nditer(X, flags=["multi_index", "refs_ok"])
for i in iterator:
for _ in iterator:
X[iterator.multi_index] = random.randint(low, high - 1)
else:
X = np.arange(low, high, dtype=np.int64)
Expand Down Expand Up @@ -270,7 +270,7 @@ def make_luts(field, sub_folder, seed, sparse=False):
for i in range(Z.shape[0]):
try:
Z[i] = I(field.fetch_int(X[i]).log(alpha))
except:
except: # noqa: E722
Z[i] = I(log(F(X[i]), alpha))
d = {"X": X, "Z": Z}
save_pickle(d, folder, "log.pkl")
Expand Down Expand Up @@ -364,7 +364,7 @@ def make_luts(field, sub_folder, seed, sparse=False):
X = []
Y = []
Z = []
for i in range(3):
for _ in range(3):
x = randint_matrix(0, order, (10,))
y = randint_matrix(0, order, (10,))
X.append(x)
Expand Down Expand Up @@ -801,8 +801,9 @@ def make_luts(field, sub_folder, seed, sparse=False):
Y = [random_coeffs(0, order, MIN_COEFFS, MAX_COEFFS) for i in range(20)]
# Add some specific polynomial types
X.append([0]), Y.append(random_coeffs(0, order, MIN_COEFFS, MAX_COEFFS)) # 0 / y
X.append(random_coeffs(0, order, MIN_COEFFS, MAX_COEFFS // 2)), Y.append(
random_coeffs(0, order, MAX_COEFFS // 2, MAX_COEFFS)
(
X.append(random_coeffs(0, order, MIN_COEFFS, MAX_COEFFS // 2)),
Y.append(random_coeffs(0, order, MAX_COEFFS // 2, MAX_COEFFS)),
) # x / y with x.degree < y.degree
X.append(random_coeffs(0, order, 2, MAX_COEFFS)), Y.append(random_coeffs(0, order, 1, 2)) # x / y with y.degree = 0
Q = []
Expand Down Expand Up @@ -994,7 +995,7 @@ def make_luts(field, sub_folder, seed, sparse=False):
for i in range(20):
n = random.randint(2, 4) # The number of polynomials
x, y = [], []
for j in range(n):
for _ in range(n):
d = random.randint(3, 5)
x.append(random_coeffs(0, order, d, d + 1))
y.append(random_coeffs(0, order, d + 1, d + 2)) # Ensure modulus degree is greater than remainder degree
Expand All @@ -1005,7 +1006,7 @@ def make_luts(field, sub_folder, seed, sparse=False):
y = [list_to_poly(yy) for yy in y]
z = crt(x, y)
Z[i] = poly_to_list(z)
except:
except: # noqa: E722
Z[i] = None
d = {"X": X, "Y": Y, "Z": Z}
save_pickle(d, folder, "crt.pkl")
Expand Down
3 changes: 1 addition & 2 deletions scripts/generate_int_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* `sudo apt install sagemath`
"""
import json
import os
import pickle
import random
Expand Down Expand Up @@ -156,7 +155,7 @@ def save_pickle(d, folder, name):
try:
z = crt(X[i], Y[i])
Z[i] = int(z)
except:
except: # noqa: E722
Z[i] = None
d = {"X": X, "Y": Y, "Z": Z}
save_pickle(d, FOLDER, "crt.pkl")
Expand Down
8 changes: 4 additions & 4 deletions scripts/sparse_poly_performance_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import numpy as np
from IPython import get_ipython

import galois

ipython = get_ipython()
assert ipython is not None, "Must run this script with ipython3"

import galois


def get_coeffs(degree, N, field):
while True:
Expand Down Expand Up @@ -55,13 +55,13 @@ def get_coeffs(degree, N, field):
print(" SparsePoly:\t", end="")
p1._type = "sparse"
p2._type = "sparse"
p1.nonzero_degrees, p1.nonzero_coeffs, p2.nonzero_degrees, p2.nonzero_coeffs
print(p1.nonzero_degrees, p1.nonzero_coeffs, p2.nonzero_degrees, p2.nonzero_coeffs)
ipython.run_line_magic("timeit", "p1 * p2")

print(" DensePoly:\t", end="")
p1._type = "dense"
p2._type = "dense"
p1.coeffs, p2.coeffs # Ensure _coeffs is created for arithmetic
print(p1.coeffs, p2.coeffs) # Ensure _coeffs is created for arithmetic
ipython.run_line_magic("timeit", "p1 * p2")

degree += 100
6 changes: 4 additions & 2 deletions src/galois/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
A performant NumPy extension for Galois fields and their applications.
"""
# pylint: disable=wrong-import-position
# ruff: noqa: F405, E402
# isort: skip_file

try:
Expand All @@ -13,9 +13,11 @@
__version_tuple__ = (0, 0, 0)
warnings.warn(
"An error occurred during package install where setuptools_scm failed to create a _version.py file."
"Defaulting version to 0.0.0."
"Defaulting version to 0.0.0.",
stacklevel=3,
)


# Import class/functions from nested private modules
from ._domains import *
from ._polys import * # Needs to be imported before _fields
Expand Down
21 changes: 9 additions & 12 deletions src/galois/_codes/_bch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from __future__ import annotations

from typing import Type, overload
from typing import overload

import numba
import numpy as np
Expand Down Expand Up @@ -77,15 +77,14 @@ class BCH(_CyclicCode):
Group:
fec
"""
# pylint: disable=no-member

def __init__(
self,
n: int,
k: int | None = None,
d: int | None = None,
field: Type[FieldArray] | None = None,
extension_field: Type[FieldArray] | None = None,
field: type[FieldArray] | None = None,
extension_field: type[FieldArray] | None = None,
alpha: ElementLike | None = None,
c: int = 1,
systematic: bool = True,
Expand Down Expand Up @@ -474,7 +473,6 @@ def encode(self, message: ArrayLike, output: Literal["codeword", "parity"] = "co
""",
)
def detect(self, codeword: ArrayLike) -> bool | np.ndarray:
# pylint: disable=useless-super-delegation
return super().detect(codeword)

@overload
Expand Down Expand Up @@ -685,11 +683,11 @@ def _decode_codeword(self, codeword: FieldArray) -> tuple[FieldArray, np.ndarray
print(bch.field.properties)
""",
)
def field(self) -> Type[FieldArray]:
def field(self) -> type[FieldArray]:
return super().field

@property
def extension_field(self) -> Type[FieldArray]:
def extension_field(self) -> type[FieldArray]:
r"""
The Galois field $\mathrm{GF}(q^m)$ that defines the BCH syndrome arithmetic.
Expand Down Expand Up @@ -1113,7 +1111,7 @@ def is_systematic(self) -> bool:

def _generator_poly_from_d(
d: int,
field: Type[FieldArray],
field: type[FieldArray],
alpha: FieldArray,
c: int,
) -> tuple[Poly, FieldArray]:
Expand All @@ -1136,8 +1134,8 @@ def _generator_poly_from_d(
def _generator_poly_from_k(
n: int,
k: int,
field: Type[FieldArray],
extension_field: Type[FieldArray],
field: type[FieldArray],
extension_field: type[FieldArray],
alpha: FieldArray,
c: int,
) -> tuple[Poly, FieldArray]:
Expand Down Expand Up @@ -1196,7 +1194,7 @@ class bch_decode_jit(Function):
- Lin, S. and Costello, D. Error Control Coding. Section 7.4.
"""

def __init__(self, field: Type[FieldArray], extension_field: Type[FieldArray]):
def __init__(self, field: type[FieldArray], extension_field: type[FieldArray]):
super().__init__(field)
self.extension_field = extension_field

Expand Down Expand Up @@ -1225,7 +1223,6 @@ def __call__(self, codeword, design_n, alpha, c, roots):
return dec_codeword, N_errors

def set_globals(self):
# pylint: disable=global-variable-undefined
global CHARACTERISTIC, SUBTRACT, MULTIPLY, RECIPROCAL, POWER
global CONVOLVE, POLY_ROOTS, POLY_EVALUATE, BERLEKAMP_MASSEY

Expand Down
2 changes: 0 additions & 2 deletions src/galois/_codes/_cyclic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class _CyclicCode(_LinearCode):
A FEC base class for cyclic codes.
"""

# pylint: disable=abstract-method

def __init__(
self,
n: int,
Expand Down
4 changes: 2 additions & 2 deletions src/galois/_codes/_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from __future__ import annotations

from typing import Type, overload
from typing import overload

import numpy as np
from typing_extensions import Literal
Expand Down Expand Up @@ -302,7 +302,7 @@ def _decode_codeword(self, codeword: FieldArray) -> tuple[FieldArray, np.ndarray
###############################################################################

@property
def field(self) -> Type[FieldArray]:
def field(self) -> type[FieldArray]:
r"""
The Galois field $\mathrm{GF}(q)$ that defines the codeword alphabet.
"""
Expand Down
8 changes: 3 additions & 5 deletions src/galois/_codes/_reed_solomon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from __future__ import annotations

from typing import Type, overload
from typing import overload

import numpy as np
from typing_extensions import Literal
Expand Down Expand Up @@ -73,14 +73,13 @@ class ReedSolomon(_CyclicCode):
Group:
fec
"""
# pylint: disable=no-member

def __init__(
self,
n: int,
k: int | None = None,
d: int | None = None,
field: Type[FieldArray] | None = None,
field: type[FieldArray] | None = None,
alpha: ElementLike | None = None,
c: int = 1,
systematic: bool = True,
Expand Down Expand Up @@ -430,7 +429,6 @@ def encode(self, message: ArrayLike, output: Literal["codeword", "parity"] = "co
""",
)
def detect(self, codeword: ArrayLike) -> bool | np.ndarray:
# pylint: disable=useless-super-delegation
return super().detect(codeword)

@overload
Expand Down Expand Up @@ -637,7 +635,7 @@ def _decode_codeword(self, codeword: FieldArray) -> tuple[FieldArray, np.ndarray
print(rs.field.properties)
""",
)
def field(self) -> Type[FieldArray]:
def field(self) -> type[FieldArray]:
return super().field

@extend_docstring(
Expand Down
4 changes: 1 addition & 3 deletions src/galois/_databases/_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
"""
from __future__ import annotations

import sys
import sqlite3
import sys
from pathlib import Path

# pylint: disable=too-few-public-methods


class DatabaseInterface:
"""
Expand Down
8 changes: 3 additions & 5 deletions src/galois/_domains/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,6 @@ def astype(self, dtype, order="K", casting="unsafe", subok=True, copy=True):
# Override arithmetic operators so type checking is appeased
###############################################################################

# pylint: disable=useless-super-delegation,no-member

def __add__(self, other: npt.NDArray) -> Self:
return super().__add__(other)

Expand Down Expand Up @@ -491,7 +489,7 @@ def __rtruediv__(self, other: npt.NDArray) -> Self:
return super().__rtruediv__(other)

def __floordiv__(self, other: npt.NDArray) -> Self:
return super().__floordiv__(other) # pylint: disable=too-many-function-args
return super().__floordiv__(other)

def __ifloordiv__(self, other: npt.NDArray) -> Self:
return super().__ifloordiv__(other)
Expand All @@ -512,7 +510,7 @@ def __rmod__(self, other: npt.NDArray) -> Self:
return super().__rmod__(other)

def __pow__(self, other: int | npt.NDArray) -> Self:
return super().__pow__(other) # pylint: disable=too-many-function-args
return super().__pow__(other)

def __ipow__(self, other: int | npt.NDArray) -> Self:
return super().__ipow__(other)
Expand All @@ -536,7 +534,7 @@ def __ilshift__(self, other: int | npt.NDArray) -> Self:
# return super().__rlshift__(other)

def __rshift__(self, other: int | npt.NDArray) -> Self:
return super().__rshift__(other) # pylint: disable=too-many-function-args
return super().__rshift__(other)

def __irshift__(self, other: int | npt.NDArray) -> Self:
return super().__irshift__(other)
Expand Down
Loading

0 comments on commit 4f69f40

Please sign in to comment.