- def _prep_items_attrs(self,
- entries: list[GalleryItem]
- ) -> tuple[BasicItemsAttrs, PromptsDiff]:
-
- def diff_prompts(basic_items_attrs: BasicItemsAttrs) -> PromptsDiff:
- if 'prompt' not in basic_items_attrs:
- return {}
- prompts_diff: PromptsDiff = {}
- prompts = list(basic_items_attrs['prompt'])
-
- def find_longest_equal(prompts, j, matcher):
- longest_total, temp_longest = '', ''
- while j < len(prompts[0]):
- if 'end' == matcher:
- temp_longest = prompts[0][-j] + temp_longest
- else:
- temp_longest += prompts[0][j]
- if len(temp_longest) > len(longest_total):
- found_in_all = True
- for prompt in prompts[1:]:
- if ('start' == matcher
- and not prompt.startswith(temp_longest)) or\
- ('end' == matcher
- and not prompt.endswith(temp_longest)) or\
- ('in' == matcher
- and temp_longest not in prompt):
- found_in_all = False
- break
- if not found_in_all:
- break
- longest_total = temp_longest
- j += 1
- return longest_total
-
- prefix = find_longest_equal(prompts, 0, 'start')
- suffix = find_longest_equal(prompts, 1, matcher='end')
- cores = [p[len(prefix):] for p in prompts]
- if suffix:
- for i, p in enumerate(cores):
- cores[i] = p[:-len(suffix)]
- longest_total = ''
- for i in range(len(cores[0])):
- temp_longest = find_longest_equal(cores, j=i, matcher='in')
+ @staticmethod
+ def _diff_prompts(prompts: list[str]) -> dict[str, tuple[str, str]]:
+ if not prompts:
+ return {}
+
+ def find_longest_equal(prompts, j, matcher):
+ longest_total, temp_longest = '', ''
+ while j < len(prompts[0]):
+ if 'end' == matcher:
+ temp_longest = prompts[0][-j] + temp_longest
+ else:
+ temp_longest += prompts[0][j]