Skip to content

Commit

Permalink
Merge pull request #297 from pyiron/ruff
Browse files Browse the repository at this point in the history
add ruff formatter
  • Loading branch information
jan-janssen committed May 14, 2024
2 parents 5f3db0e + 3b94954 commit bba08c9
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 279 deletions.
37 changes: 23 additions & 14 deletions .ci_support/release.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
def get_setup_version_and_pattern(setup_content):
depend_lst, version_lst = [], []
for l in setup_content:
if '==' in l:
lst = l.split('[')[-1].split(']')[0].replace(' ', '').replace('"', '').replace("'", '').split(',')
if "==" in l:
lst = (
l.split("[")[-1]
.split("]")[0]
.replace(" ", "")
.replace('"', "")
.replace("'", "")
.split(",")
)
for dep in lst:
if dep != '\n':
version_lst.append(dep.split('==')[1])
depend_lst.append(dep.split('==')[0])
if dep != "\n":
version_lst.append(dep.split("==")[1])
depend_lst.append(dep.split("==")[0])

version_high_dict = {d: v for d, v in zip(depend_lst, version_lst)}
return version_high_dict
Expand All @@ -16,14 +23,14 @@ def get_env_version(env_content):
read_flag = False
depend_lst, version_lst = [], []
for l in env_content:
if 'dependencies:' in l:
if "dependencies:" in l:
read_flag = True
elif read_flag:
lst = l.replace('-', '').replace(' ', '').replace('\n', '').split("=")
lst = l.replace("-", "").replace(" ", "").replace("\n", "").split("=")
if len(lst) == 2:
depend_lst.append(lst[0])
version_lst.append(lst[1])
return {d:v for d, v in zip(depend_lst, version_lst)}
return {d: v for d, v in zip(depend_lst, version_lst)}


def update_dependencies(setup_content, version_low_dict, version_high_dict):
Expand All @@ -35,27 +42,29 @@ def update_dependencies(setup_content, version_low_dict, version_high_dict):
version_combo_dict[dep] = dep + "==" + ver

setup_content_new = ""
pattern_dict = {d:d + "==" + v for d, v in version_high_dict.items()}
pattern_dict = {d: d + "==" + v for d, v in version_high_dict.items()}
for l in setup_content:
for k, v in pattern_dict.items():
if v in l:
l = l.replace(v, version_combo_dict[k])
setup_content_new +=l
setup_content_new += l
return setup_content_new


if __name__ == "__main__":
with open('pyproject.toml', "r") as f:
with open("pyproject.toml", "r") as f:
setup_content = f.readlines()

with open('environment.yml', "r") as f:
with open("environment.yml", "r") as f:
env_content = f.readlines()

setup_content_new = update_dependencies(
setup_content=setup_content[2:],
version_low_dict=get_env_version(env_content=env_content),
version_high_dict=get_setup_version_and_pattern(setup_content=setup_content[2:]),
version_high_dict=get_setup_version_and_pattern(
setup_content=setup_content[2:]
),
)

with open('pyproject.toml', "w") as f:
with open("pyproject.toml", "w") as f:
f.writelines("".join(setup_content[:2]) + setup_content_new)
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.4
hooks:
- id: ruff
name: ruff lint
args: ["--fix"]
files: ^pysqa/
- id: ruff-format
name: ruff format
1 change: 1 addition & 0 deletions pysqa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

from ._version import get_versions

__all__ = [QueueAdapter]
__version__ = get_versions()["version"]
2 changes: 2 additions & 0 deletions pysqa/executor/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from pysqa.executor.executor import Executor

__all__ = [Executor]
4 changes: 2 additions & 2 deletions pysqa/wrapper/lsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def convert_queue_status(queue_status_output: str) -> pandas.DataFrame:
job_id_lst, user_lst, status_lst, job_name_lst = [], [], [], []
line_split_lst = queue_status_output.split("\n")
if len(line_split_lst) > 1:
for l in line_split_lst[1:]:
line_segments = l.split()
for line in line_split_lst[1:]:
line_segments = line.split()
if len(line_segments) > 1:
job_id_lst.append(int(line_segments[0]))
user_lst.append(line_segments[1])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
setup(
version=versioneer.get_version(),
cmdclass=versioneer.get_cmdclass(),
)
)
2 changes: 1 addition & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_memory_string_comparison(self):
)
self.assertEqual(
BasisQueueAdapter._value_in_range(
90000 * 1024 ** 2, value_min="1K", value_max="70G"
90000 * 1024**2, value_min="1K", value_max="70G"
),
"70G",
)
Expand Down
144 changes: 89 additions & 55 deletions tests/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ class TestCMD(unittest.TestCase):
def setUpClass(cls):
cls.test_dir = os.path.abspath(os.path.dirname(__file__))

@unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
def assert_stdout_command_line(self, cmd_args, execute_command, expected_output, mock_stdout):
command_line(
arguments_lst=cmd_args,
execute_command=execute_command
)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def assert_stdout_command_line(
self, cmd_args, execute_command, expected_output, mock_stdout
):
command_line(arguments_lst=cmd_args, execute_command=execute_command)
self.assertEqual(mock_stdout.getvalue(), expected_output)

def test_help(self):
Expand Down Expand Up @@ -45,33 +44,41 @@ def execute_command(

self.assert_stdout_command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--config_directory",
os.path.join(self.test_dir, "config", "slurm"),
"--submit",
"--queue", "slurm",
"--job_name", "test",
"--working_directory", ".",
"--cores", "2",
"--memory", "1GB",
"--run_time", "10",
"--command", "echo hello"
"--queue",
"slurm",
"--job_name",
"test",
"--working_directory",
".",
"--cores",
"2",
"--memory",
"1GB",
"--run_time",
"10",
"--command",
"echo hello",
],
execute_command,
"1\n",
)
with open("run_queue.sh") as f:
output = f.readlines()
content = [
'#!/bin/bash\n',
'#SBATCH --output=time.out\n',
'#SBATCH --job-name=test\n',
'#SBATCH --chdir=.\n',
'#SBATCH --get-user-env=L\n',
'#SBATCH --partition=slurm\n',
'#SBATCH --time=4320\n',
'#SBATCH --mem=1GBG\n',
'#SBATCH --cpus-per-task=10\n',
'\n',
'echo hello'
"#!/bin/bash\n",
"#SBATCH --output=time.out\n",
"#SBATCH --job-name=test\n",
"#SBATCH --chdir=.\n",
"#SBATCH --get-user-env=L\n",
"#SBATCH --partition=slurm\n",
"#SBATCH --time=4320\n",
"#SBATCH --mem=1GBG\n",
"#SBATCH --cpus-per-task=10\n",
"\n",
"echo hello",
]
self.assertEqual(output, content)
os.remove("run_queue.sh")
Expand All @@ -88,12 +95,14 @@ def execute_command(

self.assert_stdout_command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--config_directory",
os.path.join(self.test_dir, "config", "slurm"),
"--delete",
"--id", "1"
"--id",
"1",
],
execute_command,
"S\n"
"S\n",
)

def test_status(self):
Expand All @@ -104,27 +113,40 @@ def execute_command(
shell=False,
error_filename="pysqa.err",
):
with open(os.path.join(self.test_dir, "config", "slurm", "squeue_output")) as f:
with open(
os.path.join(self.test_dir, "config", "slurm", "squeue_output")
) as f:
return f.read()

self.assert_stdout_command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--status"
"--config_directory",
os.path.join(self.test_dir, "config", "slurm"),
"--status",
],
execute_command,
json.dumps({
"jobid": [5322019, 5322016, 5322017, 5322018, 5322013], "user": ["janj", "janj", "janj", "janj", "maxi"],
"jobname": ["pi_19576488", "pi_19576485", "pi_19576486", "pi_19576487", "pi_19576482"],
"status": ["running", "running", "running", "running", "running"],
"working_directory": [
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_1",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_2",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_3",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_4",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_5"
]
}) +"\n"
json.dumps(
{
"jobid": [5322019, 5322016, 5322017, 5322018, 5322013],
"user": ["janj", "janj", "janj", "janj", "maxi"],
"jobname": [
"pi_19576488",
"pi_19576485",
"pi_19576486",
"pi_19576487",
"pi_19576482",
],
"status": ["running", "running", "running", "running", "running"],
"working_directory": [
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_1",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_2",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_3",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_4",
"/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_5",
],
}
)
+ "\n",
)

def test_list(self):
Expand All @@ -139,19 +161,31 @@ def execute_command(

self.assert_stdout_command_line(
[
"--config_directory", os.path.join(self.test_dir, "config", "slurm"),
"--config_directory",
os.path.join(self.test_dir, "config", "slurm"),
"--list",
"--working_directory", os.path.join(self.test_dir, "config", "slurm"),

"--working_directory",
os.path.join(self.test_dir, "config", "slurm"),
],
execute_command,
json.dumps({
"dirs": [os.path.join(self.test_dir, "config", "slurm")],
"files": sorted([
os.path.join(self.test_dir, "config", "slurm", "squeue_output"),
os.path.join(self.test_dir, "config", "slurm", "slurm_extra.sh"),
os.path.join(self.test_dir, "config", "slurm", "slurm.sh"),
os.path.join(self.test_dir, "config", "slurm", "queue.yaml"),
])
}) + "\n"
json.dumps(
{
"dirs": [os.path.join(self.test_dir, "config", "slurm")],
"files": sorted(
[
os.path.join(
self.test_dir, "config", "slurm", "squeue_output"
),
os.path.join(
self.test_dir, "config", "slurm", "slurm_extra.sh"
),
os.path.join(self.test_dir, "config", "slurm", "slurm.sh"),
os.path.join(
self.test_dir, "config", "slurm", "queue.yaml"
),
]
),
}
)
+ "\n",
)
18 changes: 9 additions & 9 deletions tests/test_execute_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,47 @@ def test_commands_as_lst(self):
working_directory=".",
split_output=True,
shell=False,
error_filename="pysqa.err"
error_filename="pysqa.err",
)
self.assertEqual(output, ['hello', ''])
self.assertEqual(output, ["hello", ""])

def test_commands_as_lst_no_split(self):
output = execute_command(
commands=["echo", "hello"],
working_directory=".",
split_output=False,
shell=False,
error_filename="pysqa.err"
error_filename="pysqa.err",
)
self.assertEqual(output, 'hello\n')
self.assertEqual(output, "hello\n")

def test_commands_as_lst_shell_true(self):
output = execute_command(
commands=["echo", "hello"],
working_directory=".",
split_output=True,
shell=True,
error_filename="pysqa.err"
error_filename="pysqa.err",
)
self.assertEqual(output, ['hello', ''])
self.assertEqual(output, ["hello", ""])

def test_commands_as_str(self):
output = execute_command(
commands="echo hello",
working_directory=".",
split_output=True,
shell=False,
error_filename="pysqa.err"
error_filename="pysqa.err",
)
self.assertEqual(output, ['hello', ''])
self.assertEqual(output, ["hello", ""])

def test_commands_fails(self):
output = execute_command(
commands="exit 1",
working_directory=".",
split_output=True,
shell=False,
error_filename="pysqa_fails.err"
error_filename="pysqa_fails.err",
)
self.assertIsNone(output)
with open("pysqa_fails.err") as f:
Expand Down
Loading

0 comments on commit bba08c9

Please sign in to comment.