From 53425d9772618b28540f1c7192504525299597b8 Mon Sep 17 00:00:00 2001 From: Arjun Rao Date: Fri, 20 Aug 2021 18:59:20 +0200 Subject: [PATCH 1/4] Cleanly exposing the tools withing the tools submodule. --- simmanager/_utils.py | 20 -------------------- simmanager/simmanager.py | 2 +- simmanager/tools/__init__.py | 5 +++++ simmanager/tools/params.py | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 simmanager/tools/params.py diff --git a/simmanager/_utils.py b/simmanager/_utils.py index f72b7af..94d932c 100644 --- a/simmanager/_utils.py +++ b/simmanager/_utils.py @@ -28,26 +28,6 @@ def _get_output(args, input_str=None, as_bytes=False): return stdout_, stderr_ -def make_param_string(delimiter='-', **kwargs): - """ - Takes a dictionary and constructs a string of the form key1-val1-key2-val2-... (denoted here as {key-val*}) - The keys are alphabetically sorted - :param str delimiter: Delimiter to use (default is '-') - :param dict kwargs: A python dictionary - :return: - """ - string_list = [] - for key, value in sorted(kwargs.items()): - string_list.append(key) - if isinstance(value, float): - value_string = "{:.2f}".format(value) - else: - value_string = "{}".format(value) - string_list.append(value_string) - param_string = delimiter.join(string_list) - return param_string - - def order_dict_alphabetically(d): """ Sort a given dictionary alphabetically diff --git a/simmanager/simmanager.py b/simmanager/simmanager.py index b6721e6..1edde3c 100644 --- a/simmanager/simmanager.py +++ b/simmanager/simmanager.py @@ -1,6 +1,7 @@ import os from .tools.stdouthandling import stdout_teed +from .tools.params import make_param_string from .simmetadatamanager import SimMetadataManager, SimMetadataManagerError from .paths import Paths import subprocess @@ -8,7 +9,6 @@ from ._utils import _rm_anything_recursive from ._utils import _get_output -from ._utils import make_param_string from ._utils import order_dict_alphabetically __author__ = 'anand' diff --git a/simmanager/tools/__init__.py b/simmanager/tools/__init__.py index e69de29..e6e5f7a 100644 --- a/simmanager/tools/__init__.py +++ b/simmanager/tools/__init__.py @@ -0,0 +1,5 @@ +from .timer import Timer +from .stdouthandling import stdout_teed +from .stdouthandling import stdout_redirected +from .stdouthandling import stdout_discarded +from .params import make_param_string \ No newline at end of file diff --git a/simmanager/tools/params.py b/simmanager/tools/params.py new file mode 100644 index 0000000..f3f498b --- /dev/null +++ b/simmanager/tools/params.py @@ -0,0 +1,18 @@ +def make_param_string(delimiter='-', **kwargs): + """ + Takes a dictionary and constructs a string of the form key1-val1-key2-val2-... (denoted here as {key-val*}) + The keys are alphabetically sorted + :param str delimiter: Delimiter to use (default is '-') + :param dict kwargs: A python dictionary + :return: + """ + string_list = [] + for key, value in sorted(kwargs.items()): + string_list.append(key) + if isinstance(value, float): + value_string = "{:.2f}".format(value) + else: + value_string = "{}".format(value) + string_list.append(value_string) + param_string = delimiter.join(string_list) + return param_string From cf15da6124459d0bafb592a32213f02d84a30fa3 Mon Sep 17 00:00:00 2001 From: Arjun Rao Date: Fri, 20 Aug 2021 19:03:11 +0200 Subject: [PATCH 2/4] BREAKING CHANGE: Taking only output file name as (mandatory) input argument. params and stuff handled via the tools function make_param_string. --- simmanager/simmanager.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/simmanager/simmanager.py b/simmanager/simmanager.py index 1edde3c..3af86f1 100644 --- a/simmanager/simmanager.py +++ b/simmanager/simmanager.py @@ -53,10 +53,12 @@ class SimManager: :param sim_name: Simulation Name :param root_dir: Root directory containing results. See point 1. above for more details + :param output_dir_name: string containing the name of the output + directory. If specified, param_dict and suffix are ignored. :param param_dict: Dictionary in the form of ``dict(paramname1=param1val, paramname2=param2val)``. See point 1. above to see it's usage in creating the output directory. Default is an empty dictionary. - :param suffix: Suffix used for various output files. This is passed on as an + :param file_suffix: Suffix used for various output files. This is passed on as an argument in the creation of the contained Paths object :param write_protect_dirs: Enable/Disable write protecting the directories :param tee_stdx_to: Give file name here to tee the output to the provided file name. @@ -68,14 +70,15 @@ class SimManager: from the EDITOR environment variable. It defaults to vim. """ - def __init__(self, sim_name, root_dir, param_dict={}, suffix="", write_protect_dirs=True, tee_stdx_to=None, open_desc_for_edit=False): + def __init__(self, sim_name, root_dir, output_dir_name, *, file_suffix="", write_protect_dirs=True, tee_stdx_to=None, open_desc_for_edit=False): self._sim_name = sim_name self._root_dir = root_dir if not os.path.exists(root_dir): raise SimManagerError("The root directory {} does not exist. Please create it.".format(root_dir)) - self._suffix = suffix - self._param_combo = order_dict_alphabetically(param_dict) + self._output_dir_name = output_dir_name + self._file_suffix = file_suffix + self._write_protect_dirs = write_protect_dirs self.tee_stdx_to = tee_stdx_to self.open_desc_for_edit = open_desc_for_edit @@ -83,10 +86,10 @@ def __init__(self, sim_name, root_dir, param_dict={}, suffix="", write_protect_d def __enter__(self): output_dir_path = self._aquire_output_dir() - self._paths = Paths(output_dir_path, suffix=self._suffix) + self._paths = Paths(output_dir_path, suffix=self._file_suffix) try: self._store_sim_reproduction_data() - except SimMetadataManagerError as E: + except SimMetadataManagerError: _rm_anything_recursive(output_dir_path) raise try: @@ -118,11 +121,7 @@ def _aquire_output_dir(self): in it. If directory already exists, raise a :class:`PathsError` complaining about this. """ - param_string = make_param_string(**self._param_combo) - if param_string == "": - output_dir_path = os.path.join(self._root_dir, self._sim_name) - else: - output_dir_path = os.path.join(self._root_dir, self._sim_name, param_string) + output_dir_path = os.path.join(self._root_dir, self._sim_name, self._output_dir_name) try: os.makedirs(output_dir_path) except OSError: From 73167bb5e1a5df4b7252c8fc0c7dcd0e6d3fe1b1 Mon Sep 17 00:00:00 2001 From: Arjun Rao Date: Mon, 23 Aug 2021 18:23:09 +0200 Subject: [PATCH 3/4] Bugfix make_param_string import --- simmanager/paths.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simmanager/paths.py b/simmanager/paths.py index 04a4b06..feca240 100644 --- a/simmanager/paths.py +++ b/simmanager/paths.py @@ -1,6 +1,6 @@ import os -from ._utils import make_param_string +from .tools import make_param_string __author__ = 'anand' From 3b4fd77a6351b56d37d28e0e957f63e0ba72d849 Mon Sep 17 00:00:00 2001 From: Arjun Rao Date: Mon, 23 Aug 2021 18:26:54 +0200 Subject: [PATCH 4/4] Updated version to v0.9.0 --- simmanager/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simmanager/__init__.py b/simmanager/__init__.py index 3404dd7..d4aa291 100644 --- a/simmanager/__init__.py +++ b/simmanager/__init__.py @@ -1,5 +1,5 @@ __author__ = "Arjun Rao, Anand Subramoney" -__version__ = "0.8.3" +__version__ = "0.9.0" from .paths import Paths from .simmanager import SimManager