Skip to content

Commit 55d7612

Browse files
committed
remove **kwargs in favor of named parameters
1 parent 9e719a8 commit 55d7612

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

pyrit/common/utils.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import math
66
import random
77
import re
8-
from typing import List, Literal, Union
8+
from typing import List, Literal, Union, Optional
99

1010
logger = logging.getLogger(__name__)
1111

@@ -82,7 +82,13 @@ def get_random_indices(*, start: int, size: int, percentage: int) -> List[int]:
8282

8383

8484
def select_word_indices(
85-
words: List[str], mode: Literal["all", "custom", "keywords", "random", "regex"], **kwargs
85+
words: List[str],
86+
mode: Literal["all", "custom", "keywords", "random", "regex"],
87+
*,
88+
indices: Optional[List[int]] = None,
89+
keywords: Optional[List[str]] = None,
90+
percentage: Optional[int] = None,
91+
regex: Optional[Union[str, re.Pattern]] = None,
8692
) -> List[int]:
8793
"""
8894
Select indices from a list of words based on specified selection mode.
@@ -97,12 +103,10 @@ def select_word_indices(
97103
Args:
98104
words (List[str]): A list of words to select from.
99105
mode (str, optional): Selection mode. Defaults to "all".
100-
101-
Keyword Arguments:
102-
indices (List[int]): Custom indices to select (for "custom" mode).
103-
keywords (List[str]): List of keywords to match (for "keywords" mode).
104-
percentage (int): Percentage of indices to select (for "random" mode).
105-
regex (str or Pattern): Regular expression pattern to match (for "regex" mode).
106+
indices (List[int], optional): Custom indices to select (for "custom" mode).
107+
keywords (List[str], optional): List of keywords to match (for "keywords" mode).
108+
percentage (int, optional): Percentage of indices to select (for "random" mode). Defaults to None.
109+
regex (str or Pattern, optional): Regular expression pattern to match (for "regex" mode).
106110
107111
Returns:
108112
List[int]: Indices of selected words.
@@ -119,19 +123,19 @@ def select_word_indices(
119123
return list(range(len(words)))
120124

121125
case "keywords":
122-
word_list = kwargs.get("keywords", [])
126+
word_list = keywords or []
123127
return [i for i, word in enumerate(words) if word in word_list]
124128

125129
case "random":
126-
percentage = kwargs.get("percentage", 50)
127-
return get_random_indices(0, len(words), percentage)
130+
percentage = percentage or 50
131+
return get_random_indices(start=0, size=len(words), percentage=percentage)
128132

129133
case "regex":
130-
regex = kwargs.get("regex", r".")
131-
return [i for i, word in enumerate(words) if re.search(regex, word)]
134+
pattern = regex or r"."
135+
return [i for i, word in enumerate(words) if re.search(pattern, word)]
132136

133137
case "custom":
134-
custom_indices = kwargs.get("indices", [])
138+
custom_indices = indices or []
135139
valid_indices = [i for i in custom_indices if 0 <= i < len(words)]
136140
invalid_indices = [i for i in custom_indices if i < 0 or i >= len(words)]
137141
if invalid_indices:

0 commit comments

Comments
 (0)