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

Format with black and isort #446

Merged
merged 12 commits into from
Dec 11, 2022
22 changes: 21 additions & 1 deletion .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: Run
name: Lint
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -33,3 +33,23 @@ jobs:

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

run-formatter:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Upgrade pip
run: python3 -m pip install --upgrade pip

- name: Install the `galois` package with [dev]
run: python3 -m pip install .[dev]

- name: Format with black
run: python3 -m black . --check
12 changes: 11 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
{
"python.testing.pytestArgs": ["-c=pyproject.toml", "tests/"],
"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,
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
}
6 changes: 3 additions & 3 deletions benchmarks/test_fec.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
A pytest module to benchmark forward-error correction encoding/decoding.
"""
import pytest
import numpy as np
import pytest

import galois

Expand All @@ -20,8 +20,8 @@ def setup_method(self):
self.C_sys = self.code_sys.encode(self.M)
self.C_non_sys = self.code_non_sys.encode(self.M)

self.C_sys[:,0:self.code_sys.t] += self.GF.Random(low=1) # Add t bit errors
self.C_non_sys[:,0:self.code_non_sys.t] += self.GF.Random(low=1) # Add t bit errors
self.C_sys[:, 0 : self.code_sys.t] += self.GF.Random(low=1) # Add t bit errors
self.C_non_sys[:, 0 : self.code_non_sys.t] += self.GF.Random(low=1) # Add t bit errors

def test_encode_systematic(self, benchmark):
benchmark(self.code_sys.encode, self.M)
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/test_field_arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
A pytest module to benchmark FieldArray arithmetic.
"""
import pytest
import numpy as np
import pytest

import galois

Expand All @@ -19,7 +19,7 @@ def setup_method(self):
np.random.seed(123456789)
self.x = self.GF.Random(self.N)
self.y = self.GF.Random(self.N, low=1)
self.z = np.random.randint(0, 2*self.GF.order, self.N)
self.z = np.random.randint(0, 2 * self.GF.order, self.N)

def test_add(self, benchmark):
benchmark(np.add, self.x, self.y)
Expand Down
26 changes: 18 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import inspect
import os
import sys

sys.path.insert(0, os.path.abspath("."))
sys.path.insert(0, os.path.abspath(".."))

Expand All @@ -24,11 +25,12 @@
# confuses Sphinx when parsing overloaded functions. When not building the documentation, the @set_module("galois")
# decorator works as intended.
import builtins

setattr(builtins, "__sphinx_build__", True)

import galois
import numpy

import galois

# -- Project information -----------------------------------------------------

Expand Down Expand Up @@ -124,15 +126,15 @@
"social": [
{
"icon": "fontawesome/brands/github",
"link": "https://github.com/mhostetter/galois"
"link": "https://github.com/mhostetter/galois",
},
{
"icon": "fontawesome/brands/python",
"link": "https://pypi.org/project/galois/"
"link": "https://pypi.org/project/galois/",
},
{
"icon": "fontawesome/brands/twitter",
"link": "https://twitter.com/galois_py"
"link": "https://twitter.com/galois_py",
},
],
"edit_uri": "",
Expand Down Expand Up @@ -172,7 +174,7 @@
],
"analytics": {
"provider": "google",
"property": "G-4FW9NCNFZH"
"property": "G-4FW9NCNFZH",
},
"version_dropdown": True,
"version_json": "../versions.json",
Expand Down Expand Up @@ -283,10 +285,15 @@
# -- Monkey-patching ---------------------------------------------------------

SPECIAL_MEMBERS = [
"__repr__", "__str__", "__int__",
"__call__", "__len__", "__eq__",
"__repr__",
"__str__",
"__int__",
"__call__",
"__len__",
"__eq__",
]


def autodoc_skip_member(app, what, name, obj, skip, options):
"""
Instruct autodoc to skip members that are inherited from np.ndarray.
Expand Down Expand Up @@ -342,6 +349,7 @@ def autodoc_process_bases(app, name, obj, options, bases):
# class properties may be created using `@classmethod @property def foo(cls): return "bar"`. In earlier versions, they must be created
# in the metaclass, however Sphinx cannot find or document them. Adding this workaround allows Sphinx to document them.


def classproperty(obj):
ret = classmethod(obj)
ret.__doc__ = obj.__doc__
Expand All @@ -363,7 +371,9 @@ def classproperty(obj):
setattr(galois._domains._meta.ArrayMeta, p, ArrayMeta_property)


FieldArrayMeta_properties = [member for member in dir(galois.FieldArray) if inspect.isdatadescriptor(getattr(type(galois.FieldArray), member, None))]
FieldArrayMeta_properties = [
member for member in dir(galois.FieldArray) if inspect.isdatadescriptor(getattr(type(galois.FieldArray), member, None))
]
for p in FieldArrayMeta_properties:
# Fetch the class properties from the private metaclasses
if p in ArrayMeta_properties:
Expand Down
8 changes: 4 additions & 4 deletions docs/ipython_with_reprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def run(self) -> List[docutils.nodes.Node]:
ws = " "
new_lines = [
".. md-tab-set::",
""
"",
]

for element_repr, title in zip(element_reprs, titles):
Expand All @@ -58,15 +58,15 @@ def run(self) -> List[docutils.nodes.Node]:
else:
items = first_code_line.rsplit(")", 1)
assert len(items) == 2
items.insert(1, f", repr=\"{element_repr}\")")
items.insert(1, f', repr="{element_repr}")')
new_first_code_line = "".join(items)
new_lines += [
f"{ws}{ws}{ws}{new_first_code_line}",
]
else:
new_lines += [
f"{ws}{ws}{ws}@suppress",
f"{ws}{ws}{ws}{field}.repr(\"{element_repr}\")",
f'{ws}{ws}{ws}{field}.repr("{element_repr}")',
f"{ws}{ws}{ws}{first_code_line}",
]

Expand All @@ -80,7 +80,7 @@ def run(self) -> List[docutils.nodes.Node]:
new_lines += [
f"{ws}{ws}{ws}@suppress",
f"{ws}{ws}{ws}{field}.repr()",
""
"",
]

self.state_machine.insert_input(new_lines, self.state_machine.input_lines.source(0))
Expand Down
35 changes: 23 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[build-system]
requires = [
"setuptools>=62",
"setuptools >= 62",
"wheel",
"setuptools_scm[toml]>=6.2"
"setuptools_scm[toml] >= 6.2"
]

[project]
Expand Down Expand Up @@ -49,7 +49,8 @@ dynamic = ["version"]

[project.optional-dependencies]
dev = [
"pylint>=2.14",
"pylint >= 2.14",
"black >= 22.8.0",
"pytest",
"pytest-cov[toml]",
"pytest-xdist",
Expand Down Expand Up @@ -78,31 +79,41 @@ where = ["src"]
universal = false

[tool.pylint]
ignore-paths = [
"src/galois/_version.py",
]
disable = [
"comparison-with-callable", # pylint doesn't understand metaclass properties
"consider-using-enumerate",
"eval-used",
"fixme",
"global-statement",
"invalid-name",
"invalid-unary-operand-type",
"line-too-long",
"missing-function-docstring",
"missing-module-docstring",
"no-else-return",
"not-callable", # pylint doesn't understand metaclass properties
"protected-access",
"too-many-ancestors",
"too-many-arguments",
"too-many-branches",
"too-many-instance-attributes",
"too-many-lines",
"too-many-locals",
"unnecessary-lambda-assignment",
"unneeded-not",
"unsubscriptable-object", # pylint doesn't understand metaclass properties
]
min-similarity-lines = 100
max-line-length = 140

[tool.black]
line-length = 140
exclude = '''
/(
build
| tests
)/
| 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.pytest.ini_options]
minversion = "6.2"
Expand Down
13 changes: 10 additions & 3 deletions scripts/create_conway_polys_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
cursor = conn.cursor()

cursor.execute(
"""
"""
CREATE TABLE polys (
characteristic INTEGER NOT NULL,
degree INTEGER NOT NULL,
coefficients TEXT NOT NULL,
PRIMARY KEY (characteristic, degree)
)
""")
"""
)

text = requests.get(POLY_TEXT_FILE_URL).text
for line in text.splitlines():
Expand All @@ -35,7 +36,13 @@
characteristic, degree, coefficients = line.split(",", maxsplit=2)
print(f"Conway polynomial for GF({characteristic}^{degree})")

cursor.execute("INSERT INTO polys (characteristic, degree, coefficients) VALUES (?,?,?)", (int(characteristic), int(degree), coefficients))
cursor.execute(
"""
INSERT INTO polys (characteristic, degree, coefficients)
VALUES (?,?,?)
""",
(int(characteristic), int(degree), coefficients),
)

conn.commit()
conn.close()
Loading