Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gmatteo committed Nov 9, 2023
1 parent 5c5687a commit f0709d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
26 changes: 13 additions & 13 deletions abipy/flowtk/qutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ def __init__(self, header, command, arr_options):
self.command = str(command)
self.header = str(header)
self.arr_options = arr_options
self.arr_options_str = "\n".join(arr_options)
from abipy.tools.text import rm_multiple_spaces
self.arr_options_str = rm_multiple_spaces("\n".join(arr_options))

def __str__(self):
# Add slurm array section.
Expand All @@ -227,34 +228,33 @@ def __str__(self):
#index=${SLURM_ARRAY_TASK_ID}
# Check if the index is within the range of the array
if (( index >= 0 && index < ${#OPTS_LIST[@]} )); then
OPTS="${OPTS_LIST[index]}"
echo "Selected entry at index $index: $OPTS"
else
echo "Index $index is out of range"
exit 1
fi
OPTS="${OPTS_LIST[index]}"
echo "Selected entry at index $index: $OPTS"
""" % (self.arr_options_str)

end = f"{self.command} ${{OPTS}}"
end = f"{self.command} ${{OPTS}} > job_${{index}}.log 2> job_${{index}}.err"

return header + select_opts + end

def sbatch(self, slurm_filepath: PathLike, dry_run=False) -> int:
def sbatch(self, slurm_filepath: PathLike) -> int:
with open(slurm_filepath, "wt") as fh:
fh.write(str(self))

queue_id = slurm_submit(slurm_filepath)
queue_id = slurm_sbatch(slurm_filepath)

# Save slurm job id in .qid file
with open(slurm_filepath + ".qid", "wt") as fh:
fh.write("# Slurm job id")
fh.write(str(queue_id))

return queue_id



def slurm_submit(script_file) -> int:
def slurm_sbatch(script_file) -> int:
"""
Submit a job script to the queue with sbatch
Submit a job script to the queue with sbatch. Return JOB ID.
"""
from subprocess import Popen, PIPE
# need string not bytes so must use universal_newlines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ def test_nums_and_text(self):
numbers = [1.0, 1.0, 1.0]
text = ' acell Bohr'
self.assertEqual(nums_and_text(line), (numbers, text.replace(", u'", ", '")))

original_string = "hello world with multiple spaces"
cleaned_string = rm_multiple_spaces(original_string)
assert cleaned_string == "hello world with multiple spaces"
6 changes: 6 additions & 0 deletions abipy/tools/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ def rreplace(s, old, new, occurrence):
# https://stackoverflow.com/questions/2556108/rreplace-how-to-replace-the-last-occurrence-of-an-expression-in-a-string
li = s.rsplit(old, occurrence)
return new.join(li)


def rm_multiple_spaces(string: str) -> str:
"""remove multiple spaces in a string."""
import re
return re.sub(' +', ' ', string)

0 comments on commit f0709d8

Please sign in to comment.