Formatron v0.5.0
Formatron empowers everyone to control the output format of language models with minimal overhead.
Loading...
Searching...
No Matches
utils.py
Go to the documentation of this file.
1VALID_IDENTIFIER_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
2
3def escape_identifier(s: str) -> str:
4 """
5 For each character in the string, if it is a valid kbnf identifier character,
6 add it to the result. Otherwise, add its Unicode code point to the result.
7
8 Args:
9 s: The string to escape.
10
11 Returns:
12 The escaped string.
13
14 Examples:
15 >>> escape_identifier("hello")
16 "hello"
17 >>> escape_identifier("hello_world")
18 "hello_world"
19 >>> escape_identifier("hello world")
20 "hellou20world"
21 """
22 result = []
23 for c in s:
24 if c in VALID_IDENTIFIER_CHARACTERS:
25 result.append(c)
26 else:
27 result.append(f"u{ord(c):x}")
28 return "".join(result)
str escape_identifier(str s)
For each character in the string, if it is a valid kbnf identifier character, add it to the result.
Definition utils.py:23