Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to ruff #516

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:

jobs:
run-linter:
name: Lint
name: Ruff Lint
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -31,14 +31,11 @@ jobs:
- name: Install the `galois` package with [dev]
run: python3 -m pip install .[dev]

- name: Lint code with pylint
run: python3 -m pylint src/galois/

- name: Lint tests with pylint
run: python3 -m pylint tests/
- name: Lint with ruff
run: python3 -m ruff check --output-format=github .

run-formatter:
name: Format
name: Ruff Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -54,5 +51,5 @@ jobs:
- name: Install the `galois` package with [dev]
run: python3 -m pip install .[dev]

- name: Format with black
run: python3 -m black . --check
- name: Format with ruff
run: python3 -m ruff format --check .
14 changes: 7 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": [
"-c=pyproject.toml",
"tests/"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"notebook.formatOnSave.enabled": true,
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
},
},
"editor.formatOnSave": true,
"editor.rulers": [
120
]
],
}
52 changes: 18 additions & 34 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ dynamic = ["version"]

[project.optional-dependencies]
dev = [
"pylint >= 2.14",
"black >= 22.8.0",
"ruff >= 0.1.0",
"pytest",
"pytest-cov[toml]",
"pytest-xdist",
Expand Down Expand Up @@ -89,40 +88,25 @@ where = ["src"]
[tool.distutils.bdist_wheel]
universal = false

[tool.pylint]
ignore-paths = ["src/galois/_version.py"]
disable = [
"comparison-with-callable", # pylint doesn't understand metaclass properties
"duplicate-bases", # pylint is false alarming on this error
"fixme",
"global-statement",
"invalid-name",
"missing-function-docstring",
"protected-access",
"too-many-ancestors",
"too-many-arguments",
"too-many-branches",
"too-many-instance-attributes",
"too-many-lines",
"too-many-locals",
"unneeded-not",
]
min-similarity-lines = 100
max-line-length = 120

[tool.black]
[tool.ruff]
src = ["src"]
extend-include = ["*.ipynb"]
extend-exclude = ["build", "dist", "docs", "src/galois/_version.py"]
line-length = 120
exclude = '''
/(
build
)/
| src/galois/_version.py
'''
# NOTE: You must use single-quoted strings in TOML for regular expressions. It's the equivalent of r-strings in Python.
# For some reason, this exclude line doesn't work when on a single line.

[tool.isort]
profile = "black"
[tool.ruff.lint]
select = [
"E", # pycodestyle
"F", # Pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
# "SIM",# flake8-simplify
"I", # isort
]
ignore = ["E501", "E713", "E714", "E741"]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401", "F403"]

[tool.pytest.ini_options]
minversion = "6.2"
Expand Down
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
Loading