-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
int parameters can now be formatted by %d #2431
Merged
hjoliver
merged 4 commits into
cylc:master
from
matthewrmshin:allow-percent-d-tmpl-for-int-params
Sep 28, 2017
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,6 @@ | |
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import re | ||
import unittest | ||
from copy import copy | ||
from task_id import TaskID | ||
from parsec.OrderedDict import OrderedDictWithDefaults | ||
|
||
"""Parameter expansion for runtime namespace names and graph strings. | ||
|
||
Uses recursion to achieve nested looping over any number of parameters. In its | ||
|
@@ -65,6 +58,12 @@ def expand(template, params, results, values=None): | |
#------------------------------------------------------------------------------ | ||
""" | ||
|
||
import re | ||
import unittest | ||
|
||
from cylc.task_id import TaskID | ||
from parsec.OrderedDict import OrderedDictWithDefaults | ||
|
||
# To split runtime heading name lists. | ||
REC_NAMES = re.compile(r'(?:[^,<]|\<[^>]*\>)+') | ||
# To extract 'name', '<parameters>', and 'other' from | ||
|
@@ -154,14 +153,11 @@ def expand(self, runtime_heading): | |
elif sval.startswith('='): | ||
# Check that specific parameter values exist. | ||
val = sval[1:].strip() | ||
# Pad integer values here. | ||
try: | ||
int(val) | ||
nval = int(val) | ||
except ValueError: | ||
nval = val | ||
else: | ||
nval = val.zfill(len(self.param_cfg[pname][0])) | ||
if not item_in_iterable(nval, self.param_cfg[pname]): | ||
if not item_in_iterable(val, self.param_cfg[pname]): | ||
raise ParamExpandError( | ||
"ERROR, parameter %s out of range: %s" % ( | ||
pname, p_tmpl)) | ||
|
@@ -196,7 +192,7 @@ def _expand_name(self, str_tmpl, param_list, results, spec_vals=None): | |
spec_vals = {} | ||
if not param_list: | ||
# Inner loop. | ||
current_values = copy(spec_vals) | ||
current_values = dict(spec_vals) | ||
try: | ||
results.append((str_tmpl % current_values, current_values)) | ||
except KeyError as exc: | ||
|
@@ -236,6 +232,9 @@ def replace_params(self, name_in, param_values, origin): | |
class GraphExpander(object): | ||
"""Handle parameter expansion of graph string lines.""" | ||
|
||
_REMOVE = -32768 | ||
_REMOVE_REC = re.compile(r'^.*' + str(_REMOVE) + r'.*?=>\s*?') | ||
|
||
def __init__(self, parameters): | ||
"""Initialize the parameterized task name expander. | ||
|
||
|
@@ -296,7 +295,8 @@ def expand(self, line): | |
except ValueError: | ||
nval = val | ||
else: | ||
nval = val.zfill(len(self.param_cfg[pname][0])) | ||
nval = val.zfill( | ||
len(str(self.param_cfg[pname][0]))) | ||
if nval != val: | ||
line = re.sub(item, | ||
'%s=%s' % (pname, nval), line) | ||
|
@@ -332,14 +332,18 @@ def _expand_graph(self, line, all_params, | |
param_values[pname] = values[pname] | ||
elif offs.startswith('='): | ||
# Specific value. | ||
param_values[pname] = offs[1:] | ||
try: | ||
# Template may require an integer | ||
param_values[pname] = int(offs[1:]) | ||
except ValueError: | ||
param_values[pname] = offs[1:] | ||
else: | ||
# Index offset. | ||
plist = all_params[pname] | ||
cur_idx = plist.index(values[pname]) | ||
off_idx = cur_idx + int(offs) | ||
if off_idx < 0: | ||
offval = "--<REMOVE>--" | ||
offval = self._REMOVE | ||
else: | ||
offval = plist[off_idx] | ||
param_values[pname] = offval | ||
|
@@ -352,7 +356,7 @@ def _expand_graph(self, line, all_params, | |
'defined.' % str(exc.args[0])) | ||
line = re.sub('<' + p_group + '>', repl, line) | ||
# Remove out-of-range nodes to first arrow. | ||
line = re.sub('^.*--<REMOVE>--.*?=>\s*?', '', line) | ||
line = self._REMOVE_REC.sub('', line) | ||
line_set.add(line) | ||
else: | ||
# Recurse through index ranges. | ||
|
@@ -367,9 +371,9 @@ class TestParamExpand(unittest.TestCase): | |
|
||
def setUp(self): | ||
"""Create some parameters and templates for use in tests.""" | ||
ivals = [str(i) for i in range(2)] | ||
jvals = [str(j) for j in range(3)] | ||
kvals = [str(k) for k in range(2)] | ||
ivals = [i for i in range(2)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
jvals = [j for j in range(3)] | ||
kvals = [k for k in range(2)] | ||
params_map = {'i': ivals, 'j': jvals, 'k': kvals} | ||
templates = {'i': '_i%(i)s', | ||
'j': '_j%(j)s', | ||
|
@@ -381,56 +385,56 @@ def test_name_one_param(self): | |
"""Test name expansion and returned value for a single parameter.""" | ||
self.assertEqual( | ||
self.name_expander.expand('foo<j>'), | ||
[('foo_j0', {'j': '0'}), | ||
('foo_j1', {'j': '1'}), | ||
('foo_j2', {'j': '2'})] | ||
[('foo_j0', {'j': 0}), | ||
('foo_j1', {'j': 1}), | ||
('foo_j2', {'j': 2})] | ||
) | ||
|
||
def test_name_two_params(self): | ||
"""Test name expansion and returned values for two parameters.""" | ||
self.assertEqual( | ||
self.name_expander.expand('foo<i,j>'), | ||
[('foo_i0_j0', {'i': '0', 'j': '0'}), | ||
('foo_i0_j1', {'i': '0', 'j': '1'}), | ||
('foo_i0_j2', {'i': '0', 'j': '2'}), | ||
('foo_i1_j0', {'i': '1', 'j': '0'}), | ||
('foo_i1_j1', {'i': '1', 'j': '1'}), | ||
('foo_i1_j2', {'i': '1', 'j': '2'})] | ||
[('foo_i0_j0', {'i': 0, 'j': 0}), | ||
('foo_i0_j1', {'i': 0, 'j': 1}), | ||
('foo_i0_j2', {'i': 0, 'j': 2}), | ||
('foo_i1_j0', {'i': 1, 'j': 0}), | ||
('foo_i1_j1', {'i': 1, 'j': 1}), | ||
('foo_i1_j2', {'i': 1, 'j': 2})] | ||
) | ||
|
||
def test_name_two_names(self): | ||
"""Test name expansion for two names.""" | ||
self.assertEqual( | ||
self.name_expander.expand('foo<i>, bar<j>'), | ||
[('foo_i0', {'i': '0'}), | ||
('foo_i1', {'i': '1'}), | ||
('bar_j0', {'j': '0'}), | ||
('bar_j1', {'j': '1'}), | ||
('bar_j2', {'j': '2'})] | ||
[('foo_i0', {'i': 0}), | ||
('foo_i1', {'i': 1}), | ||
('bar_j0', {'j': 0}), | ||
('bar_j1', {'j': 1}), | ||
('bar_j2', {'j': 2})] | ||
) | ||
|
||
def test_name_specific_val_1(self): | ||
"""Test singling out a specific value, in name expansion.""" | ||
self.assertEqual( | ||
self.name_expander.expand('foo<i=0>'), | ||
[('foo_i0', {'i': '0'})] | ||
[('foo_i0', {'i': 0})] | ||
) | ||
|
||
def test_name_specific_val_2(self): | ||
"""Test specific value in the first parameter of a pair.""" | ||
self.assertEqual( | ||
self.name_expander.expand('foo<i=0,j>'), | ||
[('foo_i0_j0', {'i': '0', 'j': '0'}), | ||
('foo_i0_j1', {'i': '0', 'j': '1'}), | ||
('foo_i0_j2', {'i': '0', 'j': '2'})] | ||
[('foo_i0_j0', {'i': 0, 'j': 0}), | ||
('foo_i0_j1', {'i': 0, 'j': 1}), | ||
('foo_i0_j2', {'i': 0, 'j': 2})] | ||
) | ||
|
||
def test_name_specific_val_3(self): | ||
"""Test specific value in the second parameter of a pair.""" | ||
self.assertEqual( | ||
self.name_expander.expand('foo<i,j=1>'), | ||
[('foo_i0_j1', {'i': '0', 'j': '1'}), | ||
('foo_i1_j1', {'i': '1', 'j': '1'})] | ||
[('foo_i0_j1', {'i': 0, 'j': 1}), | ||
('foo_i1_j1', {'i': 1, 'j': 1})] | ||
) | ||
|
||
def test_name_fail_bare_value(self): | ||
|
@@ -458,14 +462,14 @@ def test_name_multiple(self): | |
"""Test expansion of two names, with one and two parameters.""" | ||
self.assertEqual( | ||
self.name_expander.expand('foo<i>, bar<i,j>'), | ||
[('foo_i0', {'i': '0'}), | ||
('foo_i1', {'i': '1'}), | ||
('bar_i0_j0', {'i': '0', 'j': '0'}), | ||
('bar_i0_j1', {'i': '0', 'j': '1'}), | ||
('bar_i0_j2', {'i': '0', 'j': '2'}), | ||
('bar_i1_j0', {'i': '1', 'j': '0'}), | ||
('bar_i1_j1', {'i': '1', 'j': '1'}), | ||
('bar_i1_j2', {'i': '1', 'j': '2'})] | ||
[('foo_i0', {'i': 0}), | ||
('foo_i1', {'i': 1}), | ||
('bar_i0_j0', {'i': 0, 'j': 0}), | ||
('bar_i0_j1', {'i': 0, 'j': 1}), | ||
('bar_i0_j2', {'i': 0, 'j': 2}), | ||
('bar_i1_j0', {'i': 1, 'j': 0}), | ||
('bar_i1_j1', {'i': 1, 'j': 1}), | ||
('bar_i1_j2', {'i': 1, 'j': 2})] | ||
) | ||
|
||
def test_graph_expand_1(self): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
edge "foo@001.1" "bar@001.1" solid | ||
edge "foo@002.1" "bar@002.1" solid | ||
edge "foo@003.1" "bar@003.1" solid | ||
edge "foo@004.1" "bar@004.1" solid | ||
edge "foo@005.1" "bar@005.1" solid | ||
graph | ||
node "bar@001.1" "bar@001\n1" unfilled ellipse black | ||
node "bar@002.1" "bar@002\n1" unfilled ellipse black | ||
node "bar@003.1" "bar@003\n1" unfilled ellipse black | ||
node "bar@004.1" "bar@004\n1" unfilled ellipse black | ||
node "bar@005.1" "bar@005\n1" unfilled ellipse black | ||
node "foo@001.1" "foo@001\n1" unfilled ellipse black | ||
node "foo@002.1" "foo@002\n1" unfilled ellipse black | ||
node "foo@003.1" "foo@003\n1" unfilled ellipse black | ||
node "foo@004.1" "foo@004\n1" unfilled ellipse black | ||
node "foo@005.1" "foo@005\n1" unfilled ellipse black | ||
stop |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why has this case been handled differently to the previous example https://github.com/cylc/cylc/pull/2431/files#diff-4b401b4bbdbea5e41b7f93c7b9ef92d8R157?