From a5eedc03280ad35005cd39d2b02d28099964d27c Mon Sep 17 00:00:00 2001 From: Paul Saxe Date: Fri, 23 Aug 2024 13:13:54 -0400 Subject: [PATCH] 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. --- HISTORY.rst | 5 +++++ loop_step/loop.py | 53 +++++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 55433bd..19678a8 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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. diff --git a/loop_step/loop.py b/loop_step/loop.py index e963b13..5f736e8 100644 --- a/loop_step/loop.py +++ b/loop_step/loop.py @@ -162,6 +162,35 @@ 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( @@ -169,23 +198,6 @@ def run(self): ) ) - # 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 @@ -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( @@ -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.