Skip to content

Commit f823e66

Browse files
authored
Merge pull request #10 from IGITUGraz/ArbitraryOutputDirs
Make it necessary to specify the output directory to SimManager. the earlier directory name can be created using the 'make_param_string'. Additional changes: imported tools within the __init__.py of the tools package make optional arguments compulsory keyword arguments
2 parents cb12f94 + 3b4fd77 commit f823e66

6 files changed

Lines changed: 36 additions & 34 deletions

File tree

simmanager/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "Arjun Rao, Anand Subramoney"
2-
__version__ = "0.8.3"
2+
__version__ = "0.9.0"
33

44
from .paths import Paths
55
from .simmanager import SimManager

simmanager/_utils.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@ def _get_output(args, input_str=None, as_bytes=False):
2828
return stdout_, stderr_
2929

3030

31-
def make_param_string(delimiter='-', **kwargs):
32-
"""
33-
Takes a dictionary and constructs a string of the form key1-val1-key2-val2-... (denoted here as {key-val*})
34-
The keys are alphabetically sorted
35-
:param str delimiter: Delimiter to use (default is '-')
36-
:param dict kwargs: A python dictionary
37-
:return:
38-
"""
39-
string_list = []
40-
for key, value in sorted(kwargs.items()):
41-
string_list.append(key)
42-
if isinstance(value, float):
43-
value_string = "{:.2f}".format(value)
44-
else:
45-
value_string = "{}".format(value)
46-
string_list.append(value_string)
47-
param_string = delimiter.join(string_list)
48-
return param_string
49-
50-
5131
def order_dict_alphabetically(d):
5232
"""
5333
Sort a given dictionary alphabetically

simmanager/paths.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
from ._utils import make_param_string
3+
from .tools import make_param_string
44

55
__author__ = 'anand'
66

simmanager/simmanager.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import os
22

33
from .tools.stdouthandling import stdout_teed
4+
from .tools.params import make_param_string
45
from .simmetadatamanager import SimMetadataManager, SimMetadataManagerError
56
from .paths import Paths
67
import subprocess
78
import shlex
89
from ._utils import _rm_anything_recursive
910
from ._utils import _get_output
1011

11-
from ._utils import make_param_string
1212
from ._utils import order_dict_alphabetically
1313

1414
__author__ = 'anand'
@@ -53,10 +53,12 @@ class SimManager:
5353
:param sim_name: Simulation Name
5454
:param root_dir: Root directory containing results. See point 1. above for more
5555
details
56+
:param output_dir_name: string containing the name of the output
57+
directory. If specified, param_dict and suffix are ignored.
5658
:param param_dict: Dictionary in the form of ``dict(paramname1=param1val, paramname2=param2val)``.
5759
See point 1. above to see it's usage in creating the output directory.
5860
Default is an empty dictionary.
59-
:param suffix: Suffix used for various output files. This is passed on as an
61+
:param file_suffix: Suffix used for various output files. This is passed on as an
6062
argument in the creation of the contained Paths object
6163
:param write_protect_dirs: Enable/Disable write protecting the directories
6264
:param tee_stdx_to: Give file name here to tee the output to the provided file name.
@@ -68,25 +70,26 @@ class SimManager:
6870
from the EDITOR environment variable. It defaults to vim.
6971
"""
7072

71-
def __init__(self, sim_name, root_dir, param_dict={}, suffix="", write_protect_dirs=True, tee_stdx_to=None, open_desc_for_edit=False):
73+
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):
7274

7375
self._sim_name = sim_name
7476
self._root_dir = root_dir
7577
if not os.path.exists(root_dir):
7678
raise SimManagerError("The root directory {} does not exist. Please create it.".format(root_dir))
77-
self._suffix = suffix
78-
self._param_combo = order_dict_alphabetically(param_dict)
79+
self._output_dir_name = output_dir_name
80+
self._file_suffix = file_suffix
81+
7982
self._write_protect_dirs = write_protect_dirs
8083
self.tee_stdx_to = tee_stdx_to
8184
self.open_desc_for_edit = open_desc_for_edit
8285
self.stdout_redirected_obj = None
8386

8487
def __enter__(self):
8588
output_dir_path = self._aquire_output_dir()
86-
self._paths = Paths(output_dir_path, suffix=self._suffix)
89+
self._paths = Paths(output_dir_path, suffix=self._file_suffix)
8790
try:
8891
self._store_sim_reproduction_data()
89-
except SimMetadataManagerError as E:
92+
except SimMetadataManagerError:
9093
_rm_anything_recursive(output_dir_path)
9194
raise
9295
try:
@@ -118,11 +121,7 @@ def _aquire_output_dir(self):
118121
in it. If directory already exists, raise a :class:`PathsError`
119122
complaining about this.
120123
"""
121-
param_string = make_param_string(**self._param_combo)
122-
if param_string == "":
123-
output_dir_path = os.path.join(self._root_dir, self._sim_name)
124-
else:
125-
output_dir_path = os.path.join(self._root_dir, self._sim_name, param_string)
124+
output_dir_path = os.path.join(self._root_dir, self._sim_name, self._output_dir_name)
126125
try:
127126
os.makedirs(output_dir_path)
128127
except OSError:

simmanager/tools/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .timer import Timer
2+
from .stdouthandling import stdout_teed
3+
from .stdouthandling import stdout_redirected
4+
from .stdouthandling import stdout_discarded
5+
from .params import make_param_string

simmanager/tools/params.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def make_param_string(delimiter='-', **kwargs):
2+
"""
3+
Takes a dictionary and constructs a string of the form key1-val1-key2-val2-... (denoted here as {key-val*})
4+
The keys are alphabetically sorted
5+
:param str delimiter: Delimiter to use (default is '-')
6+
:param dict kwargs: A python dictionary
7+
:return:
8+
"""
9+
string_list = []
10+
for key, value in sorted(kwargs.items()):
11+
string_list.append(key)
12+
if isinstance(value, float):
13+
value_string = "{:.2f}".format(value)
14+
else:
15+
value_string = "{}".format(value)
16+
string_list.append(value_string)
17+
param_string = delimiter.join(string_list)
18+
return param_string

0 commit comments

Comments
 (0)