-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor parse_script function for readability and maintainability #90
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,25 +5,39 @@ | |
def parse_script(response: str) -> tuple[str, str]: | ||
"""Split the response into a message and a script. | ||
|
||
Expected use is: run the script if there is one, otherwise print the message. | ||
The function splits the response by '```' delimiters, extracting the message and script parts. | ||
It checks for common mistakes, such as 'python' prefix and attempts to parse the script as JSON. | ||
Finally, it validates the script as valid Python code using ast module. | ||
|
||
Args: | ||
response (str): The input response containing a message and optionally a script. | ||
|
||
Returns: | ||
tuple[str, str]: A tuple containing the message (or error message) and the script. | ||
""" | ||
# Parse delimiter | ||
n_delimiters = response.count("```") | ||
if n_delimiters < 2: | ||
delimiter_count = response.count("```") | ||
if delimiter_count < 2: | ||
return response, "" | ||
|
||
segments = response.split("```") | ||
message = f"{segments[0]}\n{segments[-1]}" | ||
script = "```".join(segments[1:-1]).strip() # Leave 'inner' delimiters alone | ||
|
||
# Check for common mistakes | ||
if script.split("\n")[0].startswith("python"): | ||
script = "\n".join(script.split("\n")[1:]) | ||
try: # Make sure it isn't json | ||
if script.startswith("python"): | ||
script = script[len("python"):] # Remove 'python' prefix | ||
|
||
# Attempt to parse script as JSON | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The transformation from the prefix 'python' might not handle additional whitespace or characters that could follow the prefix, such as 'python3'. Consider using a more robust method to strip out not just 'python' but also variations to cater for possible user inputs. We could use regex for this purpose to ensure a standard and safe removal of the prefix. |
||
try: | ||
script = json.loads(script) | ||
except Exception: | ||
except json.JSONDecodeError: | ||
pass | ||
try: # Make sure it's valid python | ||
|
||
# Validate the script as valid Python code | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the |
||
ast.parse(script) | ||
except SyntaxError: | ||
return f"Script contains invalid Python:\n{response}", "" | ||
except SyntaxError as e: | ||
return f"Script contains invalid Python:\n{e}", "" | ||
|
||
return message, script |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line attempts to concatenate a message with the last segment after splitting, which might not necessarily be always the part of the original message, especially if there are an uneven number of '
'. Ensure that relevant test cases are checking for varying counts of '
' to avoid potential errors in message reconstruction.