ruff config added for replacing flake8, code style und naming changes

This commit is contained in:
Nicolas Kruse 2025-10-26 22:30:38 +01:00
parent 6445ac9724
commit a971b98f2d
12 changed files with 60 additions and 56 deletions

24
.flake8
View File

@ -1,24 +0,0 @@
[flake8]
# Specify the maximum allowed line length
max-line-length = 88
# Ignore specific rules
# For example, E501: Line too long, W503: Line break before binary operator
ignore = E501, W503, W504, E226, E265
# Exclude specific files or directories
exclude =
.git,
__pycache__,
build,
dist,
.conda,
.venv
# Enable specific plugins or options
# Example: Enabling flake8-docstrings
select = C,E,F,W,D
# Specify custom error codes to ignore or enable
per-file-ignores =
tests/*: D, E712

View File

@ -32,7 +32,7 @@ copapy = ["obj/*.o", "py.typed"]
[project.optional-dependencies]
dev = [
"flake8",
"ruff",
"mypy",
"pytest"
]
@ -50,4 +50,23 @@ show_error_codes = true
minversion = "6.0"
addopts = "-ra -q"
testpaths = ["tests"]
pythonpath = ["src"]
pythonpath = ["src"]
[tool.ruff]
lint.ignore = ["E501", "E226", "E265"]
# Equivalent to Flake8's "exclude"
exclude = [
".git",
"__pycache__",
"build",
"dist",
".conda",
".venv",
]
# "D" for dockstrings
lint.select = ["C", "E", "F", "W"]
[tool.ruff.lint.per-file-ignores]
"tests/*" = ["D", "E712"]

View File

@ -161,11 +161,11 @@ class variable(Generic[TNum], Net):
def __lt__(self, other: NumLike) -> 'variable[bool]':
ret = add_op('gt', [other, self])
return variable(ret.source, dtype='bool')
def __ge__(self, other: NumLike) -> 'variable[bool]':
ret = add_op('ge', [self, other])
return variable(ret.source, dtype='bool')
def __le__(self, other: NumLike) -> 'variable[bool]':
ret = add_op('ge', [other, self])
return variable(ret.source, dtype='bool')

View File

@ -14,7 +14,7 @@ COMMAND_SIZE = 4
class data_writer():
def __init__(self, byteorder: ByteOrder):
self._data: list[tuple[str, bytes, int]] = list()
self._data: list[tuple[str, bytes, int]] = []
self.byteorder: ByteOrder = byteorder
def write_int(self, value: int, num_bytes: int = 4, signed: bool = False) -> None:

View File

@ -37,4 +37,3 @@ def abs(x: T) -> T:
ret = (x < 0) * -x + (x >= 0) * x
return ret # pyright: ignore[reportReturnType]

View File

@ -4,7 +4,7 @@ from coparun_module import coparun, read_data_mem
import struct
from ._basic_types import stencil_db_from_package
from ._basic_types import variable, Net, Node, Write, NumLike
from ._compiler import compile_to_instruction_list
from ._compiler import compile_to_dag
def add_read_command(dw: binw.data_writer, variables: dict[Net, tuple[int, int, str]], net: Net) -> None:
@ -18,7 +18,7 @@ def add_read_command(dw: binw.data_writer, variables: dict[Net, tuple[int, int,
class Target():
def __init__(self, arch: str = 'native', optimization: str = 'O3') -> None:
self.sdb = stencil_db_from_package(arch, optimization)
self._variables: dict[Net, tuple[int, int, str]] = dict()
self._variables: dict[Net, tuple[int, int, str]] = {}
def compile(self, *variables: int | float | variable[int] | variable[float] | variable[bool] | Iterable[int | float | variable[int] | variable[float] | variable[bool]]) -> None:
nodes: list[Node] = []
@ -30,7 +30,7 @@ class Target():
else:
nodes.append(Write(s))
dw, self._variables = compile_to_instruction_list(nodes, self.sdb)
dw, self._variables = compile_to_dag(nodes, self.sdb)
dw.write_com(binw.Command.END_COM)
assert coparun(dw.get_data()) > 0

View File

@ -1,6 +1,6 @@
from ._target import add_read_command
from ._basic_types import Net, Op, Node, CPConstant, Write
from ._compiler import compile_to_instruction_list, \
from ._compiler import compile_to_dag, \
stable_toposort, get_const_nets, get_all_dag_edges, add_read_ops, \
add_write_ops
@ -11,7 +11,7 @@ __all__ = [
"Node",
"CPConstant",
"Write",
"compile_to_instruction_list",
"compile_to_dag",
"stable_toposort",
"get_const_nets",
"get_all_dag_edges",

View File

@ -1,5 +1,5 @@
from copapy import variable, NumLike
from copapy.backend import Write, compile_to_instruction_list, add_read_command
from copapy.backend import Write, compile_to_dag, add_read_command
import copapy
import subprocess
import struct
@ -49,7 +49,7 @@ def test_compile():
out = [Write(r) for r in ret]
il, variables = compile_to_instruction_list(out, copapy.generic_sdb)
il, variables = compile_to_dag(out, copapy.generic_sdb)
# run program command
il.write_com(_binwrite.Command.RUN_PROG)

View File

@ -1,5 +1,5 @@
from copapy import variable, NumLike
from copapy.backend import Write, compile_to_instruction_list
from copapy.backend import Write, compile_to_dag
import copapy
import subprocess
from copapy import _binwrite
@ -26,7 +26,7 @@ def test_compile():
out = [Write(r) for r in ret]
il, _ = compile_to_instruction_list(out, copapy.generic_sdb)
il, _ = compile_to_dag(out, copapy.generic_sdb)
# run program command
il.write_com(_binwrite.Command.RUN_PROG)

View File

@ -1,6 +1,6 @@
from coparun_module import coparun
from copapy import variable
from copapy.backend import Write, compile_to_instruction_list, add_read_command
from copapy.backend import Write, compile_to_dag, add_read_command
import copapy
from copapy import _binwrite
@ -15,7 +15,7 @@ def test_compile():
r2 = i1 + 9
out = [Write(r1), Write(r2), Write(c2)]
il, variables = compile_to_instruction_list(out, copapy.generic_sdb)
il, variables = compile_to_dag(out, copapy.generic_sdb)
# run program command
il.write_com(_binwrite.Command.RUN_PROG)

View File

@ -1,5 +1,5 @@
from copapy import NumLike, variable
from copapy.backend import Write, Net, compile_to_instruction_list, add_read_command
from copapy.backend import Write, Net, compile_to_dag, add_read_command
import copapy
import subprocess
from copapy import _binwrite
@ -29,7 +29,7 @@ def test_compile():
ret = function(c1, c2)
dw, variable_list = compile_to_instruction_list([Write(net) for net in ret], copapy.generic_sdb)
dw, variable_list = compile_to_dag([Write(net) for net in ret], copapy.generic_sdb)
# run program command
dw.write_com(_binwrite.Command.RUN_PROG)

View File

@ -1,33 +1,43 @@
from copapy import _binwrite, variable
from copapy.backend import Write, compile_to_instruction_list
from copapy import variable
from copapy.backend import Write, compile_to_dag
import copapy as cp
from copapy._binwrite import Command
def test_compile() -> None:
"""Test compilation of a simple program."""
c1 = variable(9.0)
#ret = [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4]
#ret = [c1 // 3.3 + 5]
ret = [cp.sqrt(c1)]
ret = [c1 // 3.3 + 5]
#ret = [cp.sqrt(c1)]
#c2 = cp._math.get_42()
#ret = [c2]
out = [Write(r) for r in ret]
il, _ = compile_to_instruction_list(out, cp.generic_sdb)
dw, vars = compile_to_dag(out, cp.generic_sdb)
# run program command
il.write_com(_binwrite.Command.RUN_PROG)
dw.write_com(Command.RUN_PROG)
il.write_com(_binwrite.Command.READ_DATA)
il.write_int(0)
il.write_int(36)
# read first 32 byte
dw.write_com(Command.READ_DATA)
dw.write_int(0)
dw.write_int(32)
il.write_com(_binwrite.Command.END_COM)
# read variables
for addr, lengths, _ in vars.values():
dw.write_com(Command.READ_DATA)
dw.write_int(addr)
dw.write_int(lengths)
dw.write_com(Command.END_COM)
print('* Data to runner:')
il.print()
dw.print()
il.to_file('bin/test.copapy')
dw.to_file('bin/test.copapy')
if __name__ == "__main__":