Skip to content

Using ruff for linting #01 #77

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

Merged
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
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ tasks:
lint:
desc: Lints the code and reports on issues.
cmds:
- uv run black --check .
- uv run ruff check .
# - uv run ruff format . --check

build:
desc: Builds the python package
Expand Down
56 changes: 47 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies = [
"scipy==1.*",
"numpy==1.*",
"plotly==5.*",
# "attrs==23.*"
"psutil==5.*",
"rich==13.*",
"pandas==2.*",
Expand All @@ -43,8 +42,7 @@ dimred = "lasso.dimred.run:main"
dev = [
"pytest==8.*",
"pytest-cov==5.*",
"black==24.*",
"ruff==0.3.*",
"ruff==0.11.*",
"mkdocs==1.*",
"mkdocs-material==9.*",
"mkdocstrings[python]==0.*",
Expand All @@ -58,10 +56,50 @@ dev = [
requires = ["setuptools>=78", "setuptools-scm>=8"]
build-backend = "setuptools.build_meta"

[tool.black]
# We allow longer lines since 80 is quite short
line-length=100
[tool.ruff]
required-version = "==0.11.*"
line-length = 100
indent-width = 4
preview = true

[tool.flake8]
exclude = [".git", "*migrations*"]
max-line-length = 100
# Output serialization format for violations. The default serialization
# format is "full" [env: RUFF_OUTPUT_FORMAT=] [possible values:
# concise, full, json, json-lines, junit, grouped, github, gitlab,
# pylint, rdjson, azure, sarif]
output-format = "grouped"

[tool.ruff.lint]
isort.lines-after-imports = 2
select = [
"C", # Complexity checks (e.g., McCabe complexity, comprehensions)
# "ANN001", "ANN201", "ANN401", # flake8-annotations (required strict type annotations for public functions)
# "S", # flake8-bandit (checks basic security issues in code)
# "BLE", # flake8-blind-except (checks the except blocks that do not specify exception)
# "FBT", # flake8-boolean-trap (ensure that boolean args can be used with kw only)
# "E", # pycodestyle errors (PEP 8 style guide violations)
# "W", # pycodestyle warnings (e.g., extra spaces, indentation issues)
# "DOC", # pydoclint issues (e.g., extra or missing return, yield, warnings)
# "A", # flake8-buitins (check variable and function names to not shadow builtins)
# "N", # Naming convention checks (e.g., PEP 8 variable and function names)
# "F", # Pyflakes errors (e.g., unused imports, undefined variables)
# "I", # isort (Ensures imports are sorted properly)
# "B", # flake8-bugbear (Detects likely bugs and bad practices)
# "TID", # flake8-tidy-imports (Checks for banned or misplaced imports)
# "UP", # pyupgrade (Automatically updates old Python syntax)
# "YTT", # flake8-2020 (Detects outdated Python 2/3 compatibility issues)
# "FLY", # flynt (Converts old-style string formatting to f-strings)
# "PIE", # flake8-pie
# "PL", # pylint
# "RUF", # Ruff-specific rules (Additional optimizations and best practices)
]

ignore = [
"PLR2004", # [magic-value-comparision](https://docs.astral.sh/ruff/rules/magic-value-comparison)
"C90", # [mccabe](https://docs.astral.sh/ruff/rules/#mccabe-c90)
]

[tool.ruff.lint.per-file-ignores]

[tool.ruff.format]
docstring-code-format = true
skip-magic-trailing-comma = true
34 changes: 4 additions & 30 deletions src/lasso/diffcrash/diffcrash_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def __init__(
self.n_processes = self._parse_n_processes(n_processes)

def _setup_logger(self) -> logging.Logger:

# better safe than sorry
os.makedirs(self.logfile_dir, exist_ok=True)

Expand All @@ -310,7 +309,6 @@ def _setup_logger(self) -> logging.Logger:
return logger

def _parse_diffcrash_home(self, diffcrash_home) -> str:

diffcrash_home_ok = len(diffcrash_home) != 0

msg = self._msg_option.format("diffcrash-home", diffcrash_home)
Expand All @@ -328,7 +326,6 @@ def _parse_diffcrash_home(self, diffcrash_home) -> str:
return diffcrash_home

def _parse_crash_code(self, crash_code) -> str:

# these guys are allowed
valid_crash_codes = ["dyna", "radioss", "pam"]

Expand All @@ -340,16 +337,14 @@ def _parse_crash_code(self, crash_code) -> str:

if not crash_code_ok:
err_msg = (
f"Invalid crash code '{crash_code}'. "
f"Please use one of: {str(valid_crash_codes)}"
f"Invalid crash code '{crash_code}'. Please use one of: {str(valid_crash_codes)}"
)
self.logger.error(err_msg)
raise RuntimeError(str_error(err_msg))

return crash_code

def _parse_reference_run(self, reference_run) -> str:

reference_run_ok = Path(reference_run).is_file()

msg = self._msg_option.format("reference-run", reference_run)
Expand All @@ -364,7 +359,6 @@ def _parse_reference_run(self, reference_run) -> str:
return reference_run

def _parse_use_id_mapping(self, use_id_mapping) -> bool:

msg = self._msg_option.format("use-id-mapping", use_id_mapping)
print(str_info(msg))
self.logger.info(msg)
Expand All @@ -386,7 +380,6 @@ def _parse_simulation_runs(
reference_run: str,
exclude_runs: typing.Sequence[str],
):

# search all denoted runs
simulation_runs = []
for pattern in simulation_run_patterns:
Expand Down Expand Up @@ -442,19 +435,16 @@ def natural_keys(text):
return simulation_runs

def _parse_config_file(self, config_file) -> Union[str, None]:

_msg_config_file = ""
if len(config_file) > 0 and not Path(config_file).is_file():
config_file = None
_msg_config_file = f"Can not find config file '{config_file}'"

# missing config file
else:

config_file = None
_msg_config_file = (
"Config file missing. "
"Consider specifying the path with the option '--config-file'."
"Config file missing. Consider specifying the path with the option '--config-file'."
)

msg = self._msg_option.format("config-file", config_file)
Expand All @@ -468,7 +458,6 @@ def _parse_config_file(self, config_file) -> Union[str, None]:
return config_file

def _parse_parameter_file(self, parameter_file) -> Union[None, str]:

_msg_parameter_file = ""
if len(parameter_file) > 0 and not Path(parameter_file).is_file():
parameter_file = None
Expand All @@ -492,7 +481,6 @@ def _parse_parameter_file(self, parameter_file) -> Union[None, str]:
return parameter_file

def _parse_n_processes(self, n_processes) -> int:

print(str_info(self._msg_option.format("n-processes", n_processes)))

if n_processes <= 0:
Expand Down Expand Up @@ -640,7 +628,6 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
# entry 0 is the reference run, thus we start at 1
# pylint: disable = consider-using-enumerate
for i_filepath in range(len(self.simulation_runs)):

# parameter file missing
if self.parameter_file is None:
if self.use_id_mapping:
Expand Down Expand Up @@ -691,7 +678,6 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
return_code_future.done() for return_code_future in return_code_futures
)
while n_imports_finished != len(return_code_futures):

# check again
n_new_imports_finished = sum(
return_code_future.done() for return_code_future in return_code_futures
Expand All @@ -717,7 +703,6 @@ def run_import(self, pool: futures.ThreadPoolExecutor):

# print failure
if any(return_code != 0 for return_code in return_codes):

n_failed_runs = 0
for i_run, return_code in enumerate(return_codes):
if return_code != 0:
Expand All @@ -739,7 +724,6 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
# check log files
messages = self.check_if_logfiles_show_success("DFC_Import_*.log")
if messages:

# print failure
msg = f"Running Imports ... done in {time.time() - start_time:.2f}s "
print(str_error(msg))
Expand Down Expand Up @@ -774,14 +758,12 @@ def run_math(self, pool: futures.ThreadPoolExecutor):

start_time = time.time()
return_code_future = pool.submit(
run_subprocess,
[self.diffcrash_home / f"DFC_Math_{self.crash_code}", self.project_dir],
run_subprocess, [self.diffcrash_home / f"DFC_Math_{self.crash_code}", self.project_dir]
)
return_code = return_code_future.result()

# check return code
if return_code != 0:

msg = f"Running Math ... done in {time.time() - start_time:.2f}s "
print(str_error(msg))
self.logger.error(msg)
Expand All @@ -793,7 +775,6 @@ def run_math(self, pool: futures.ThreadPoolExecutor):
# check logs
messages = self.check_if_logfiles_show_success("DFC_MATH*.log")
if messages:

# print failure
msg = f"Running Math ... done in {time.time() - start_time:.2f}s "
print(str_error(msg))
Expand Down Expand Up @@ -889,7 +870,6 @@ def run_export(self, pool: futures.ThreadPoolExecutor):
# check logs
messages = self.check_if_logfiles_show_success("DFC_Export_*")
if messages:

# print failure
msg = f"Running Export ... done in {time.time() - start_time:.2f}s "
print(str_error(msg))
Expand All @@ -900,10 +880,7 @@ def run_export(self, pool: futures.ThreadPoolExecutor):
print(str_error(msg))
self.logger.error(msg)

msg = (
"At least one export failed. "
f"Please check the log files in '{self.logfile_dir}'."
)
msg = f"At least one export failed. Please check the log files in '{self.logfile_dir}'."
self.logger.error(msg)
raise RuntimeError(str_error(msg))

Expand Down Expand Up @@ -945,7 +922,6 @@ def run_matrix(self, pool: futures.ThreadPoolExecutor):

# check return code
if return_code != 0:

# print failure
msg = f"Running Matrix ... done in {time.time() - start_time:.2f}s "
print(str_error(msg))
Expand All @@ -958,7 +934,6 @@ def run_matrix(self, pool: futures.ThreadPoolExecutor):
# check log file
messages = self.check_if_logfiles_show_success("DFC_Matrix_*")
if messages:

# print failure
msg = f"Running Matrix ... done in {time.time() - start_time:.2f}s "
print(str_error(msg))
Expand Down Expand Up @@ -1021,7 +996,6 @@ def run_eigen(self, pool: futures.ThreadPoolExecutor):
# check log file
messages = self.check_if_logfiles_show_success("DFC_Matrix_*")
if messages:

# print failure
msg = f"Running Eigen ... done in {time.time() - start_time:.2f}s "
print(str_error(msg))
Expand Down
2 changes: 0 additions & 2 deletions src/lasso/diffcrash/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


def _parse_stages(start_stage: str, end_stage: str):

# check validity
if start_stage not in DC_STAGES or end_stage not in DC_STAGES:
raise ValueError(
Expand Down Expand Up @@ -76,7 +75,6 @@ def main():

# initiate threading pool for handling jobs
with futures.ThreadPoolExecutor(max_workers=diffcrash_run.n_processes) as pool:

# setup
if start_stage_index <= DC_STAGES.index(DC_STAGE_SETUP) <= end_stage_index:
diffcrash_run.run_setup(pool)
Expand Down
Loading