Skip to content

Commit cc543e1

Browse files
Fixes using ruff (#383)
* Ruff fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent de4c9e2 commit cc543e1

18 files changed

+103
-85
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repos:
44
hooks:
55
- id: ruff
66
name: ruff lint
7-
args: ["--select", "I", "--fix"]
7+
args: ["--fix"]
88
files: ^pysqa/
99
- id: ruff-format
1010
name: ruff format

pyproject.toml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,43 @@ include = ["pysqa*"]
5353
[tool.setuptools.dynamic]
5454
version = {attr = "pysqa.__version__"}
5555

56+
[tool.ruff]
57+
exclude = [".ci_support", "docs", "notebooks", "tests", "setup.py", "_version.py"]
58+
59+
[tool.ruff.lint]
60+
select = [
61+
# pycodestyle
62+
"E",
63+
# Pyflakes
64+
"F",
65+
# pyupgrade
66+
"UP",
67+
# flake8-bugbear
68+
"B",
69+
# flake8-simplify
70+
"SIM",
71+
# isort
72+
"I",
73+
# flake8-comprehensions
74+
"C4",
75+
# eradicate
76+
"ERA",
77+
# pylint
78+
"PL",
79+
]
80+
ignore = [
81+
# ignore exception naming
82+
"B904",
83+
# ignore line-length violations
84+
"E501",
85+
# Too many arguments in function definition
86+
"PLR0913",
87+
# Too many branches
88+
"PLR0912",
89+
# Too many statements
90+
"PLR0915",
91+
]
92+
5693
[tool.versioneer]
5794
VCS = "git"
5895
style = "pep440-pre"

pysqa/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
from ._version import get_versions
44

5-
__all__ = [QueueAdapter]
5+
__all__ = ["QueueAdapter"]
66
__version__ = get_versions()["version"]

pysqa/base/abstract.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from typing import List, Optional, Union
2+
from typing import Optional, Union
33

44
import pandas
55
from jinja2 import Template
@@ -15,7 +15,7 @@ def submit_job(
1515
cores: Optional[int] = None,
1616
memory_max: Optional[int] = None,
1717
run_time_max: Optional[int] = None,
18-
dependency_list: Optional[List[str]] = None,
18+
dependency_list: Optional[list[str]] = None,
1919
command: Optional[str] = None,
2020
submission_template: Optional[Union[str, Template]] = None,
2121
**kwargs,
@@ -67,7 +67,7 @@ def get_status_of_job(self, process_id: int) -> Union[str, None]:
6767
pass
6868

6969
@abstractmethod
70-
def get_status_of_jobs(self, process_id_lst: List[int]) -> List[str]:
70+
def get_status_of_jobs(self, process_id_lst: list[int]) -> list[str]:
7171
"""
7272
Get the status of multiple jobs.
7373

pysqa/base/cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def command_line(
123123
elif mode_list:
124124
working_directory = os.path.abspath(os.path.expanduser(working_directory))
125125
remote_dirs, remote_files = [], []
126-
for p, folder, files in os.walk(working_directory):
126+
for p, _folder, files in os.walk(working_directory):
127127
remote_dirs.append(p)
128128
remote_files += [os.path.join(p, f) for f in files]
129129
print(

pysqa/base/config.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import List, Optional, Tuple, Union
2+
from typing import Optional, Union
33

44
import pandas
55
import yaml
@@ -10,13 +10,13 @@
1010
from pysqa.base.validate import check_queue_parameters, value_error_if_none
1111

1212

13-
class Queues(object):
13+
class Queues:
1414
"""
1515
Queues is an abstract class simply to make the list of queues available for auto completion. This is mainly used in
1616
interactive environments like jupyter.
1717
"""
1818

19-
def __init__(self, list_of_queues: List[str]):
19+
def __init__(self, list_of_queues: list[str]):
2020
"""
2121
Initialize the Queues object.
2222
@@ -45,7 +45,7 @@ def __getattr__(self, item: str) -> str:
4545
else:
4646
raise AttributeError
4747

48-
def __dir__(self) -> List[str]:
48+
def __dir__(self) -> list[str]:
4949
"""
5050
Get the list of queues.
5151
@@ -189,7 +189,7 @@ def check_queue_parameters(
189189
run_time_max: Optional[int] = None,
190190
memory_max: Optional[int] = None,
191191
active_queue: Optional[dict] = None,
192-
) -> Tuple[
192+
) -> tuple[
193193
Union[float, int, None], Union[float, int, None], Union[float, int, None]
194194
]:
195195
"""
@@ -223,7 +223,7 @@ def _job_submission_template(
223223
cores: Optional[int] = None,
224224
memory_max: Optional[int] = None,
225225
run_time_max: Optional[int] = None,
226-
dependency_list: Optional[List[int]] = None,
226+
dependency_list: Optional[list[int]] = None,
227227
command: Optional[str] = None,
228228
**kwargs,
229229
) -> str:
@@ -297,8 +297,8 @@ def _load_templates(queue_lst_dict: dict, directory: str = ".") -> None:
297297
directory (str, optional): The directory where the queue template files are located. Defaults to ".".
298298
"""
299299
for queue_dict in queue_lst_dict.values():
300-
if "script" in queue_dict.keys():
301-
with open(os.path.join(directory, queue_dict["script"]), "r") as f:
300+
if "script" in queue_dict:
301+
with open(os.path.join(directory, queue_dict["script"])) as f:
302302
try:
303303
queue_dict["template"] = Template(f.read())
304304
except TemplateSyntaxError as error:
@@ -321,5 +321,5 @@ def read_config(file_name: str = "queue.yaml") -> dict:
321321
Returns:
322322
dict: The parsed configuration as a dictionary.
323323
"""
324-
with open(file_name, "r") as f:
324+
with open(file_name) as f:
325325
return yaml.load(f, Loader=yaml.FullLoader)

pysqa/base/core.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import importlib
33
import os
44
import subprocess
5-
from typing import List, Optional, Tuple, Union
5+
from typing import Optional, Union
66

77
import pandas
88
from jinja2 import Template
@@ -52,7 +52,7 @@ def execute_command(
5252
split_output: bool = True,
5353
shell: bool = False,
5454
error_filename: str = "pysqa.err",
55-
) -> Union[str, List[str]]:
55+
) -> Union[str, list[str]]:
5656
"""
5757
A wrapper around the subprocess.check_output function.
5858
@@ -96,7 +96,7 @@ def get_queue_commands(queue_type: str) -> Union[SchedulerCommands, None]:
9696
Returns:
9797
SchedulerCommands: queuing system commands class instance
9898
"""
99-
if queue_type in queue_type_dict.keys():
99+
if queue_type in queue_type_dict:
100100
class_name = queue_type_dict[queue_type]["class_name"]
101101
module_name = queue_type_dict[queue_type]["module_name"]
102102
if module_name is not None and class_name is not None:
@@ -129,10 +129,9 @@ def __init__(
129129
):
130130
self._commands = get_queue_commands(queue_type=queue_type)
131131
if queue_type_dict[queue_type]["module_name"] is not None:
132-
self._submission_template = getattr(
133-
importlib.import_module(queue_type_dict[queue_type]["module_name"]),
134-
"template",
135-
)
132+
self._submission_template = importlib.import_module(
133+
queue_type_dict[queue_type]["module_name"]
134+
).template
136135
self._execute_command_function = execute_command
137136

138137
def submit_job(
@@ -143,7 +142,7 @@ def submit_job(
143142
cores: Optional[int] = None,
144143
memory_max: Optional[int] = None,
145144
run_time_max: Optional[int] = None,
146-
dependency_list: Optional[List[str]] = None,
145+
dependency_list: Optional[list[str]] = None,
147146
command: Optional[str] = None,
148147
submission_template: Optional[Union[str, Template]] = None,
149148
**kwargs,
@@ -277,7 +276,7 @@ def get_status_of_job(self, process_id: int) -> Union[str, None]:
277276
else:
278277
return None
279278

280-
def get_status_of_jobs(self, process_id_lst: List[int]) -> List[str]:
279+
def get_status_of_jobs(self, process_id_lst: list[int]) -> list[str]:
281280
"""
282281
Get the status of multiple jobs.
283282
@@ -311,7 +310,7 @@ def _list_command_to_be_executed(self, queue_script_path: str) -> list:
311310

312311
def _execute_command(
313312
self,
314-
commands: Union[str, List[str]],
313+
commands: Union[str, list[str]],
315314
working_directory: Optional[str] = None,
316315
split_output: bool = True,
317316
shell: bool = False,
@@ -347,10 +346,10 @@ def _write_queue_script(
347346
cores: Optional[int] = None,
348347
memory_max: Optional[int] = None,
349348
run_time_max: Optional[int] = None,
350-
dependency_list: Optional[List[int]] = None,
349+
dependency_list: Optional[list[int]] = None,
351350
command: Optional[str] = None,
352351
**kwargs,
353-
) -> Tuple[str, str]:
352+
) -> tuple[str, str]:
354353
"""
355354
Write the queue script to a file.
356355
@@ -399,7 +398,7 @@ def _job_submission_template(
399398
cores: Optional[int] = None,
400399
memory_max: Optional[int] = None,
401400
run_time_max: Optional[int] = None,
402-
dependency_list: Optional[List[int]] = None,
401+
dependency_list: Optional[list[int]] = None,
403402
command: Optional[str] = None,
404403
**kwargs,
405404
) -> str:

pysqa/base/modular.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(
2929
directory: str = "~/.queues",
3030
execute_command: callable = execute_command,
3131
):
32-
super(ModularQueueAdapter, self).__init__(
32+
super().__init__(
3333
config=config, directory=directory, execute_command=execute_command
3434
)
3535
self._queue_to_cluster_dict = {
@@ -212,4 +212,4 @@ def _switch_cluster_command(cluster_module: str):
212212
list: The switch cluster command.
213213
214214
"""
215-
return ["module", "--quiet", "swap", "cluster/{};".format(cluster_module)]
215+
return ["module", "--quiet", "swap", f"cluster/{cluster_module};"]

pysqa/base/remote.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import os
44
import warnings
5-
from typing import List, Optional, Union
5+
from typing import Optional, Union
66

77
import pandas
88
import paramiko
@@ -85,64 +85,46 @@ def __init__(
8585
directory: str = "~/.queues",
8686
execute_command: callable = execute_command,
8787
):
88-
super(RemoteQueueAdapter, self).__init__(
88+
super().__init__(
8989
config=config, directory=directory, execute_command=execute_command
9090
)
9191
self._ssh_host = config["ssh_host"]
9292
self._ssh_username = config["ssh_username"]
9393
self._ssh_known_hosts = os.path.abspath(
9494
os.path.expanduser(config["known_hosts"])
9595
)
96-
if "ssh_key" in config.keys():
96+
if "ssh_key" in config:
9797
self._ssh_key = os.path.abspath(os.path.expanduser(config["ssh_key"]))
9898
self._ssh_ask_for_password = False
9999
else:
100100
self._ssh_key = None
101-
if "ssh_password" in config.keys():
101+
if "ssh_password" in config:
102102
self._ssh_password = config["ssh_password"]
103103
self._ssh_ask_for_password = False
104104
else:
105105
self._ssh_password = None
106-
if "ssh_ask_for_password" in config.keys():
107-
self._ssh_ask_for_password = config["ssh_ask_for_password"]
108-
else:
109-
self._ssh_ask_for_password = False
110-
if "ssh_key_passphrase" in config.keys():
111-
self._ssh_key_passphrase = config["ssh_key_passphrase"]
112-
else:
113-
self._ssh_key_passphrase = None
114-
if "ssh_two_factor_authentication" in config.keys():
106+
self._ssh_ask_for_password = config.get("ssh_ask_for_password", False)
107+
self._ssh_key_passphrase = config.get("ssh_key_passphrase")
108+
if "ssh_two_factor_authentication" in config:
115109
self._ssh_two_factor_authentication = config[
116110
"ssh_two_factor_authentication"
117111
]
118112
else:
119113
self._ssh_two_factor_authentication = False
120-
if "ssh_authenticator_service" in config.keys():
114+
if "ssh_authenticator_service" in config:
121115
self._ssh_authenticator_service = config["ssh_authenticator_service"]
122116
self._ssh_two_factor_authentication = True
123117
else:
124118
self._ssh_authenticator_service = None
125-
if "ssh_proxy_host" in config.keys():
126-
self._ssh_proxy_host = config["ssh_proxy_host"]
127-
else:
128-
self._ssh_proxy_host = None
119+
self._ssh_proxy_host = config.get("ssh_proxy_host")
129120
self._ssh_remote_config_dir = config["ssh_remote_config_dir"]
130121
self._ssh_remote_path = config["ssh_remote_path"]
131122
self._ssh_local_path = os.path.abspath(
132123
os.path.expanduser(config["ssh_local_path"])
133124
)
134-
if "ssh_delete_file_on_remote" in config.keys():
135-
self._ssh_delete_file_on_remote = config["ssh_delete_file_on_remote"]
136-
else:
137-
self._ssh_delete_file_on_remote = True
138-
if "ssh_port" in config.keys():
139-
self._ssh_port = config["ssh_port"]
140-
else:
141-
self._ssh_port = 22
142-
if "ssh_continous_connection" in config.keys():
143-
self._ssh_continous_connection = config["ssh_continous_connection"]
144-
else:
145-
self._ssh_continous_connection = False
125+
self._ssh_delete_file_on_remote = config.get("ssh_delete_file_on_remote", True)
126+
self._ssh_port = config.get("ssh_port", 22)
127+
self._ssh_continous_connection = config.get("ssh_continous_connection", False)
146128
self._ssh_connection = None
147129
self._ssh_proxy_connection = None
148130
self._remote_flag = True
@@ -571,7 +553,7 @@ def _execute_remote_command(self, command: str) -> str:
571553
else:
572554
ssh = self._open_ssh_connection()
573555
stdin, stdout, stderr = ssh.exec_command(command)
574-
warnings.warn(stderr.read().decode())
556+
warnings.warn(message=stderr.read().decode(), stacklevel=2)
575557
output = stdout.read().decode()
576558
if not self._ssh_continous_connection:
577559
ssh.close()
@@ -592,7 +574,7 @@ def _get_remote_working_dir(self, working_directory: str) -> str:
592574
os.path.relpath(working_directory, self._ssh_local_path),
593575
)
594576

595-
def _create_remote_dir(self, directory: Union[str, List[str]]) -> None:
577+
def _create_remote_dir(self, directory: Union[str, list[str]]) -> None:
596578
"""
597579
Creates a remote directory on the SSH server.
598580
@@ -628,7 +610,7 @@ def _transfer_data_to_remote(self, working_directory: str) -> None:
628610
)
629611
file_dict = {}
630612
new_dir_list = []
631-
for p, folder, files in os.walk(working_directory):
613+
for p, _folder, files in os.walk(working_directory):
632614
new_dir_list.append(
633615
self._get_file_transfer(
634616
file=p,

pysqa/base/validate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import re
2-
from typing import Optional, Tuple, Union
2+
from typing import Optional, Union
33

44

55
def check_queue_parameters(
66
active_queue: Optional[dict] = None,
77
cores: int = 1,
88
run_time_max: Optional[int] = None,
99
memory_max: Optional[int] = None,
10-
) -> Tuple[Union[float, int, None], Union[float, int, None], Union[float, int, None]]:
10+
) -> tuple[Union[float, int, None], Union[float, int, None], Union[float, int, None]]:
1111
"""
1212
Check the parameters of a queue.
1313

0 commit comments

Comments
 (0)