Skip to content

Commit

Permalink
Enhancement to 'For' loops
Browse files Browse the repository at this point in the history
* For integer loops, ensure that the loop variable is an integer and
  make the directory name be value of the loop variable to make it easier
  to find the iteration of interest.
  • Loading branch information
paulsaxe committed Aug 23, 2024
1 parent ed9b5b0 commit a5eedc0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
=======
History
=======
2024.8.23 -- Enhancement to 'For' loops
* For integer loops, ensure that the loop variable is an integer and
make the directory name be value of the loop variable to make it easier
to find the iteration of interest.

2024.8.21 -- Bugfix: Error selecting systems by name
* There was a bug selecting system by name in the For Systems in Database branch.

Expand Down
53 changes: 35 additions & 18 deletions loop_step/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,30 +162,42 @@ def run(self):

# Set up some unchanging variables
if P["type"] == "For":
# Some local variables need each iteration

# See if loop variables are all integers
integers = True
start = P["start"]
if isinstance(start, str):
start = float(start)
if start.is_integer():
start = int(start)
else:
integers = False
step = P["step"]
if isinstance(step, str):
step = float(step)
if step.is_integer():
step = int(step)
else:
integers = False
end = P["end"]
if isinstance(end, str):
end = float(end)
if end.is_integer():
end = int(end)
else:
integers = False

if integers:
fmt = f"0{len(str(end))}d"

if self._loop_value is None:
self.logger.info(
"For {} from {} to {} by {}".format(
P["variable"], P["start"], P["end"], P["step"]
)
)

# See if loop variables are all integers
start = P["start"]
if isinstance(start, str):
start = float(start)
if isinstance(start, float) and start.is_integer():
start = int(start)
step = P["step"]
if isinstance(step, str):
step = float(step)
if isinstance(step, float) and step.is_integer():
step = int(step)
end = P["end"]
if isinstance(end, str):
end = float(end)
if isinstance(end, float) and end.is_integer():
end = int(end)

self.logger.info("Initializing loop")
self._loop_count = 0
self._loop_value = start
Expand Down Expand Up @@ -404,6 +416,10 @@ def run(self):

self.set_variable(P["variable"], self._loop_value)

# For integer loops, we can use the value for the directory names
if integers:
self._custom_directory_name = f"iter_{self._loop_value:{fmt}}"

# Set up the index variables
tmp = self.get_variable("_loop_indices")
self.set_variable(
Expand All @@ -416,8 +432,9 @@ def run(self):
self.set_variable("_loop_index", self._loop_value)

# See if we are at the end of loop
if self._loop_value > end:
if self._loop_count > self._loop_length:
self._loop_value = None
self._custom_directory_name = None

# Revert the loop index variables to the next outer loop
# if there is one, or remove them.
Expand Down

0 comments on commit a5eedc0

Please sign in to comment.