MCPcopy
hub / github.com/ScrapeGraphAI/Scrapegraph-ai / _parse_input_keys

Method _parse_input_keys

scrapegraphai/nodes/base_node.py:136–235  ·  view source on GitHub ↗

Parses the input keys expression to extract relevant keys from the state based on logical conditions. The expression can contain AND (&), OR (|), and parentheses to group conditions. Args: state (dict): The current state of the graph. express

(self, state: dict, expression: str)

Source from the content-addressed store, hash-verified

134 )
135
136 def _parse_input_keys(self, state: dict, expression: str) -> List[str]:
137 """
138 Parses the input keys expression to extract
139 relevant keys from the state based on logical conditions.
140 The expression can contain AND (&), OR (|), and parentheses to group conditions.
141
142 Args:
143 state (dict): The current state of the graph.
144 expression (str): The input keys expression to parse.
145
146 Returns:
147 List[str]: A list of key names that match the input keys expression logic.
148
149 Raises:
150 ValueError: If the expression is invalid or if no state keys match the expression.
151 """
152
153 if not expression:
154 raise ValueError("Empty expression.")
155
156 pattern = (
157 r"\b("
158 + "|".join(re.escape(key) for key in state.keys())
159 + r")(\b\s*\b)("
160 + "|".join(re.escape(key) for key in state.keys())
161 + r")\b"
162 )
163 if re.search(pattern, expression):
164 raise ValueError(
165 "Adjacent state keys found without an operator between them."
166 )
167
168 expression = expression.replace(" ", "")
169
170 if (
171 expression[0] in "&|"
172 or expression[-1] in "&|"
173 or "&&" in expression
174 or "||" in expression
175 or "&|" in expression
176 or "|&" in expression
177 ):
178 raise ValueError("Invalid operator usage.")
179
180 open_parentheses = close_parentheses = 0
181 for i, char in enumerate(expression):
182 if char == "(":
183 open_parentheses += 1
184 elif char == ")":
185 close_parentheses += 1
186 # Check for invalid operator sequences
187 if char in "&|" and i + 1 < len(expression) and expression[i + 1] in "&|":
188 raise ValueError(
189 "Invalid operator placement: operators cannot be adjacent."
190 )
191
192 if open_parentheses != close_parentheses:
193 raise ValueError("Missing or unbalanced parentheses in expression.")

Callers 1

get_input_keysMethod · 0.95

Calls 1

evaluate_expressionFunction · 0.85

Tested by

no test coverage detected