-
Notifications
You must be signed in to change notification settings - Fork 601
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
Fixed sonnet json formatting issue #293
Conversation
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.
LGTM, added some comments
return match.group(0).replace("\n", "\\n") | ||
|
||
pattern = r'"(?:[^"\\]|\\.)*"' | ||
text = re.sub(pattern, replace_newlines, text) |
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.
TIL repl
arg can be a function
def replace_newlines(match): | ||
return match.group(0).replace("\n", "\\n") | ||
|
||
pattern = r'"(?:[^"\\]|\\.)*"' |
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.
Bonus points for regex 101 link
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.
Added!
|
||
# escape new lines within strings | ||
def replace_newlines(match): | ||
return match.group(0).replace("\n", "\\n") |
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.
Is there a latent NoneType
error here? Can we type hint match
with re.Match
, and the return as str
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.
Added a type - I don't think sub will call on None matches
@@ -184,4 +184,12 @@ def llm_read_json(text: str) -> dict: | |||
text = "{" + text.split("{", 1)[-1] | |||
# split anything after the last } | |||
text = text.rsplit("}", 1)[0] + "}" | |||
|
|||
# escape new lines within strings |
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.
What do you think of re.escape
?
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.
Don't see the connection
Fixes #292