Skip to content

Commit ce7b76f

Browse files
committed
* refactoring
* additional tests
1 parent e059a6d commit ce7b76f

File tree

3 files changed

+78
-58
lines changed

3 files changed

+78
-58
lines changed

ppp.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,18 +441,18 @@ def __processprompts(self, prompt, negative_prompt):
441441
negative_prompt = self.__add_to_insertion_points(
442442
negative_prompt, p_processor.add_at["insertion_point"], n_processor.insertion_at
443443
)
444-
if len(p_processor.add_at["start"]) > 0:
444+
if p_processor.add_at["start"]:
445445
negative_prompt = self.__add_to_start(negative_prompt, p_processor.add_at["start"])
446-
if len(p_processor.add_at["end"]) > 0:
446+
if p_processor.add_at["end"]:
447447
negative_prompt = self.__add_to_end(negative_prompt, p_processor.add_at["end"])
448448

449449
# Clean up
450450
prompt = self.__cleanup(prompt)
451451
negative_prompt = self.__cleanup(negative_prompt)
452452

453453
# Check for wildcards not processed
454-
foundP = len(p_processor.detectedWildcards) > 0
455-
foundNP = len(n_processor.detectedWildcards) > 0
454+
foundP = bool(p_processor.detectedWildcards)
455+
foundNP = bool(n_processor.detectedWildcards)
456456
if foundP or foundNP:
457457
if self.wil_ifwildcards == self.IFWILDCARDS_CHOICES.stop:
458458
self.logger.error("Found unprocessed wildcards!")
@@ -473,10 +473,11 @@ def __processprompts(self, prompt, negative_prompt):
473473
if foundNP:
474474
negative_prompt = self.WILDCARD_STOP.format(npwl) + negative_prompt
475475
self.interrupt()
476+
476477
# Check for special character sequences that should not be in the result
477478
compound_prompt = prompt + "\n" + negative_prompt
478479
found_sequences = re.findall(r"::|\$\$|\$\{|[{}]", compound_prompt)
479-
if len(found_sequences) > 0:
480+
if found_sequences:
480481
self.logger.warning(
481482
f"""Found probably invalid character sequences on the result ({', '.join(map(lambda x: '"' + x + '"', set(found_sequences)))}). Something might be wrong!"""
482483
)
@@ -1255,15 +1256,13 @@ def __get_choices_internal(
12551256
break
12561257
if passes:
12571258
filtered_choice_values.append(c)
1258-
if len(filtered_choice_values) == 0:
1259+
if not filtered_choice_values:
12591260
self.__ppp.logger.warning(
12601261
f"Wildcard filter specifier '{','.join(['+'.join(y for y in x) for x in filter_specifier])}' found no matches in choices for wildcard '{wildcard_key}'!"
12611262
)
12621263
else:
12631264
filtered_choice_values = choice_values.copy()
1264-
if len(filtered_choice_values) == 0:
1265-
num_choices = 0
1266-
else:
1265+
if filtered_choice_values:
12671266
if from_value < 0:
12681267
from_value = 1
12691268
elif from_value > len(filtered_choice_values):
@@ -1277,6 +1276,8 @@ def __get_choices_internal(
12771276
if from_value < to_value
12781277
else from_value
12791278
)
1279+
else:
1280+
num_choices = 0
12801281
if num_choices < 2:
12811282
repeating = False
12821283
if self.__ppp.debug_level == DEBUG_LEVEL.full:
@@ -1541,7 +1542,7 @@ def wildcard(self, tree: lark.Tree):
15411542
if self.__ppp.debug_level == DEBUG_LEVEL.full:
15421543
self.__ppp.logger.debug(f"Processing wildcard: {wildcard_key}")
15431544
selected_wildcards = self.__ppp.wildcard_obj.get_wildcards(wildcard_key)
1544-
if len(selected_wildcards) == 0:
1545+
if not selected_wildcards:
15451546
self.detectedWildcards.append(wc)
15461547
self.result += wc
15471548
t2 = time.time()

ppp_wildcards.py

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ def __hash__(self) -> int:
8282
return hash(deep_freeze(self.wildcards))
8383

8484
def __sizeof__(self):
85-
return (
86-
self.wildcards.__sizeof__()
87-
+ self.__wildcards_folders.__sizeof__()
88-
+ self.__wildcard_files.__sizeof__()
89-
)
85+
return self.wildcards.__sizeof__() + self.__wildcards_folders.__sizeof__() + self.__wildcard_files.__sizeof__()
9086

9187
def refresh_wildcards(self, debug_level: DEBUG_LEVEL, wildcards_folders: Optional[list[str]]):
9288
"""
@@ -245,51 +241,60 @@ def __get_choices(self, obj: object, full_path: str, key_parts: list[str]) -> li
245241
Returns:
246242
list: list of choices
247243
"""
248-
choices = None
249-
if obj is not None:
250-
if isinstance(obj, (str, dict)):
251-
choices = [obj]
252-
elif isinstance(obj, (int, float, bool)):
253-
choices = [str(obj)]
254-
elif isinstance(obj, list) and len(obj) > 0:
255-
choices = []
256-
for i, c in enumerate(obj):
257-
invalid_choice = False
258-
if isinstance(c, str):
259-
choice = c
260-
elif isinstance(c, (int, float, bool)):
261-
choice = str(c)
262-
elif isinstance(c, list):
263-
# we create an anonymous wildcard
264-
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c)
265-
elif isinstance(c, dict):
266-
if self.is_dict_choices_options(c) or self.is_dict_choice_options(c):
267-
# we assume it is a choice or wildcard parameters in object format
268-
choice = c
269-
choice_content = choice.get("content", choice.get("text", None))
270-
if choice_content is not None and isinstance(choice_content, list):
271-
# we create an anonymous wildcard
272-
choice["content"] = self.__create_anonymous_wildcard(
273-
full_path, key_parts, i, choice_content
274-
)
275-
if "text" in choice:
276-
del choice["text"]
277-
elif len(c) == 1:
278-
# we assume it is an anonymous wildcard with options
279-
firstkey = list(c.keys())[0]
280-
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c[firstkey], firstkey)
281-
else:
282-
invalid_choice = True
283-
else:
284-
invalid_choice = True
285-
if invalid_choice:
286-
self.__logger.warning(
287-
f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!"
288-
)
289-
else:
290-
choices.append(choice)
244+
if obj is None:
245+
return None
246+
if isinstance(obj, (str, dict)):
247+
return [obj]
248+
if isinstance(obj, (int, float, bool)):
249+
return [str(obj)]
250+
if not isinstance(obj, list) or len(obj) == 0:
251+
self.__logger.warning(f"Invalid format in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!")
252+
return None
253+
choices = []
254+
for i, c in enumerate(obj):
255+
if isinstance(c, (str, int, float, bool)):
256+
choices.append(str(c))
257+
elif isinstance(c, list):
258+
# we create an anonymous wildcard
259+
choices.append(self.__create_anonymous_wildcard(full_path, key_parts, i, c))
260+
elif isinstance(c, dict):
261+
choices.append(self.__process_dict_choice(c, full_path, key_parts, i))
262+
else:
263+
self.__logger.warning(
264+
f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!"
265+
)
291266
return choices
292267

268+
def __process_dict_choice(self, c: dict, full_path: str, key_parts: list[str], i: int) -> dict:
269+
"""
270+
Process a dictionary choice.
271+
272+
Args:
273+
c (dict): The dictionary choice.
274+
full_path (str): The path to the file.
275+
key_parts (list[str]): The parts of the key.
276+
i (int): The index of the choice.
277+
278+
Returns:
279+
dict: The processed choice.
280+
"""
281+
if self.is_dict_choices_options(c) or self.is_dict_choice_options(c):
282+
# we assume it is a choice or wildcard parameters in object format
283+
choice = c
284+
choice_content = choice.get("content", choice.get("text", None))
285+
if choice_content is not None and isinstance(choice_content, list):
286+
# we create an anonymous wildcard
287+
choice["content"] = self.__create_anonymous_wildcard(full_path, key_parts, i, choice_content)
288+
if "text" in choice:
289+
del choice["text"]
290+
return choice
291+
if len(c) == 1:
292+
# we assume it is an anonymous wildcard with options
293+
firstkey = list(c.keys())[0]
294+
return self.__create_anonymous_wildcard(full_path, key_parts, i, c[firstkey], firstkey)
295+
self.__logger.warning(f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!")
296+
return None
297+
293298
def __create_anonymous_wildcard(self, full_path, key_parts, i, content, options=None):
294299
"""
295300
Create an anonymous wildcard.

tests/tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,20 @@ def test_wc_wildcard2_yaml(self): # simple yaml wildcard with default options
722722
ppp=self.__nocupppp,
723723
)
724724

725+
def test_wc_test2_yaml(self): # simple yaml wildcard
726+
self.__process(
727+
PromptPair("the choice is: __testwc/test2__", ""),
728+
PromptPair("the choice is: 2", ""),
729+
ppp=self.__nocupppp,
730+
)
731+
732+
def test_wc_test3_yaml(self): # simple yaml wildcard
733+
self.__process(
734+
PromptPair("the choice is: __testwc/test3__", ""),
735+
PromptPair("the choice is: one choice", ""),
736+
ppp=self.__nocupppp,
737+
)
738+
725739
def test_wc_wildcard_filter_index(self): # wildcard with positional index filter
726740
self.__process(
727741
PromptPair("the choice is: __yaml/wildcard2'2'__", ""),

0 commit comments

Comments
 (0)