From 5d98e038980b3a65d7a2e6025df27d001f52401d Mon Sep 17 00:00:00 2001 From: Sam Wolk <36545842+szvsw@users.noreply.github.com> Date: Thu, 11 Aug 2022 17:58:17 -0400 Subject: [PATCH] fix unique name recursion overflow (#367) --- archetypal/template/umi_base.py | 20 +++++++------------- archetypal/umi_template.py | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/archetypal/template/umi_base.py b/archetypal/template/umi_base.py index 782d9d09..1ef934e0 100644 --- a/archetypal/template/umi_base.py +++ b/archetypal/template/umi_base.py @@ -501,7 +501,7 @@ def comments(self): class UniqueName(str): """Attribute unique user-defined names for :class:`UmiBase`.""" - existing = set() + existing = {} def __new__(cls, content): """Pick a name. Will increment the name if already used.""" @@ -519,17 +519,11 @@ def create_unique(cls, name): if not name: return None if name not in cls.existing: - cls.existing.add(name) + cls.existing[name] = 0 return name else: - match = re.match(r"^(.*?)(\D*)(\d+)$", name) - if match: - groups = list(match.groups()) - pad = len(groups[-1]) - groups[-1] = int(groups[-1]) - groups[-1] += 1 - groups[-1] = str(groups[-1]).zfill(pad) - name = "".join(map(str, groups)) - return cls.create_unique(name) - else: - return cls.create_unique(name + "_1") + current_count = cls.existing[name] + new_count = current_count + 1 + new_name = f"{name}_{str(new_count)}" + cls.existing[name] = new_count + return new_name diff --git a/archetypal/umi_template.py b/archetypal/umi_template.py index 0c6bc95b..21af240a 100644 --- a/archetypal/umi_template.py +++ b/archetypal/umi_template.py @@ -607,7 +607,7 @@ def to_dict(self): # create dict values for group_name, group in self: # reset unique names for group - UniqueName.existing = set() + UniqueName.existing = {} obj: UmiBase for obj in group: try: