From 878450b112e6b1ba8f0d473aaacb05b56fb4f03b Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Wed, 6 Dec 2017 15:30:52 -0600 Subject: [PATCH] Fix generate_numbered_unique_names() to respect the counter argument. Closes #1 --- pytools/__init__.py | 7 +++---- test/test_pytools.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pytools/__init__.py b/pytools/__init__.py index 7bc99bb..a9c58ce 100644 --- a/pytools/__init__.py +++ b/pytools/__init__.py @@ -1855,10 +1855,9 @@ def generate_unique_names(prefix): def generate_numbered_unique_names(prefix, num=None): - orig_num = num - num = 0 - if orig_num is None: - yield (num, prefix) + if num is None: + yield (0, prefix) + num = 0 while True: name = "%s_%d" % (prefix, num) diff --git a/test/test_pytools.py b/test/test_pytools.py index d73553d..5346c25 100644 --- a/test/test_pytools.py +++ b/test/test_pytools.py @@ -195,6 +195,17 @@ def test_diskdict(): del d +def test_generate_numbered_unique_names(): + from pytools import generate_numbered_unique_names + + gen = generate_numbered_unique_names("a") + assert next(gen) == (0, "a") + assert next(gen) == (1, "a_0") + + gen = generate_numbered_unique_names("b", 6) + assert next(gen) == (7, "b_6") + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) -- GitLab