Skip to content

Commit

Permalink
Merge pull request #25 from molssi-seamm/dev
Browse files Browse the repository at this point in the history
Further improving naming of loop subdirectories
  • Loading branch information
seamm authored Jul 30, 2024
2 parents 93ae014 + 779ae12 commit dcb88ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
=======
History
=======
2024.7.30 -- Further improving naming of loop subdirectories
* For loops now use the value of loop index as the subdirectory
* Improved handling of lists for ForEach loops so blank-deliminated lists work
properly. Quotes can be used for values with embedded blanks, much like the Linux
commandline.

2024.7.28 -- Improved naming of loop subdirectories
* The subdirectories now start at 1, not 0, to make counting more normal
* When looping over systems, now have the option to name the directories
Expand Down
54 changes: 34 additions & 20 deletions loop_step/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
from pathlib import Path
import re
import shlex
import sys
import traceback

Expand Down Expand Up @@ -70,7 +71,7 @@ def working_path(self):
if self._custom_directory_name is not None:
tmp = Path(self.directory) / self._custom_directory_name
else:
tmp = Path(self.directory) / f"iter_{self._loop_value+1:{self.iter_format}}"
tmp = Path(self.directory) / f"iter_{self._loop_value:{self.iter_format}}"
return tmp

def describe(self):
Expand Down Expand Up @@ -106,7 +107,10 @@ def description_text(self, P=None):
if self.is_expr(P["values"]):
subtext = f"Foreach {P['variable']} in {P['values']}\n"
else:
values = [str(v) for v in P["values"]]
if isinstance(P["values"], str):
values = [str(v) for v in shlex.split(P["values"])]
else:
values = [str(v) for v in P["values"]]
if len(values) > 5:
last = values[-1]
values = values[0:6]
Expand Down Expand Up @@ -208,8 +212,11 @@ def run(self):
self.set_variable("_loop_index", self._loop_value)
elif P["type"] == "Foreach":
if self._loop_value is None:
self._loop_value = -1
self._loop_length = len(P["values"])
self._loop_value = 0
if isinstance(P["values"], str):
self._loop_length = len(shlex.split(P["values"]))
else:
self._loop_length = len(P["values"])
printer.important(
__(
f"The loop will have {self._loop_length} iterations.\n\n",
Expand Down Expand Up @@ -238,7 +245,7 @@ def run(self):
self.table.shape[0], P["table"]
)
)
self._loop_value = -1
self._loop_value = 0
self._loop_length = self.table.shape[0]
printer.important(
__(
Expand Down Expand Up @@ -316,8 +323,9 @@ def run(self):
systems = [s for s in systems if s.n_configurations > 0]
configurations = [s.configurations[0] for s in systems]
if self._loop_value is None:
self._loop_value = -1
self._loop_value = 0
self._loop_length = len(configurations)
print(f"{self._loop_length} configurations found.")
printer.important(
__(
f"The loop will have {self._loop_length} iterations.\n\n",
Expand Down Expand Up @@ -406,12 +414,12 @@ def run(self):
self.logger.info(" Loop value = {}".format(self._loop_value))
elif P["type"] == "Foreach":
self.logger.debug(f"Foreach {P['variable']} in {P['values']}")
if self._loop_value >= 0:
if self._loop_value > 0:
self.write_final_structure()

self._loop_value += 1

if self._loop_value >= self._loop_length:
if self._loop_value > self._loop_length:
self._loop_value = None
self._loop_length = None

Expand All @@ -429,7 +437,10 @@ def run(self):
# return the next node after the loop
break

value = P["values"][self._loop_value]
if isinstance(P["values"], str):
value = shlex.split(P["values"])[self._loop_value - 1]
else:
value = P["values"][self._loop_value - 1]
self.set_variable(P["variable"], value)

# Set up the index variables
Expand All @@ -444,20 +455,20 @@ def run(self):
self.set_variable("_loop_index", self._loop_value)
self.logger.info(" Loop value = {}".format(value))
elif P["type"] == "For rows in table":
if self._loop_value >= 0:
if self._loop_value > 0:
self.write_final_structure()

# Loop until query is satisfied
while True:
self._loop_value += 1

if self._loop_value >= self.table.shape[0]:
if self._loop_value > self.table.shape[0]:
break

if where == "Use all rows":
break

row = self.table.iloc[self._loop_value]
row = self.table.iloc[self._loop_value - 1]

self.logger.debug(f"Query {row[column]} {op} {value}")
if op == "==":
Expand Down Expand Up @@ -502,7 +513,7 @@ def run(self):
f"Loop query '{op}' not implemented"
)

if self._loop_value >= self.table.shape[0]:
if self._loop_value > self.table.shape[0]:
self._loop_value = None

self.delete_variable("_row")
Expand Down Expand Up @@ -537,26 +548,28 @@ def run(self):
self.logger.debug(" _loop_indices = {}".format(tmp))
self.set_variable(
"_loop_indices",
(*tmp[0:-1], self.table.index[self._loop_value]),
(*tmp[0:-1], self.table.index[self._loop_value - 1]),
)
self.logger.debug(
" --> {}".format(self.get_variable("_loop_indices"))
)
self.set_variable("_loop_index", self.table.index[self._loop_value])
self.set_variable(
"_loop_index", self.table.index[self._loop_value - 1]
)
self.table_handle["current index"] = self.table.index[
self._loop_value
self._loop_value - 1
]

row = self.table.iloc[self._loop_value]
row = self.table.iloc[self._loop_value - 1]
self.set_variable("_row", row)
self.logger.debug(" _row = {}".format(row))
elif P["type"] == "For systems in the database":
if self._loop_value >= 0:
if self._loop_value > 0:
self.write_final_structure()

self._loop_value += 1

if self._loop_value >= self._loop_length:
if self._loop_value > self._loop_length:
self._loop_value = None
self._loop_length = None
self._custom_directory_name = None
Expand All @@ -576,7 +589,8 @@ def run(self):
break

# Set the default system and configuration
configuration = configurations[self._loop_value]
print(f"{self._loop_value} / {self._loop_length}")
configuration = configurations[self._loop_value - 1]
system_db = configuration.system_db
system = configuration.system
system_db.system = configuration.system
Expand Down

0 comments on commit dcb88ac

Please sign in to comment.