Formatron
v0.5.0
v0.5.0
v0.4.11
v0.4.10
v0.4.9
v0.4.8
v0.4.7
v0.4.6
v0.4.5
v0.4.4
v0.4.3
v0.4.2
v0.4.1
v0.4.0
v0.3.4
v0.3.3
v0.3.2
v0.3.1
v0.3.0
v0.2.0
v0.1.3
v0.1.2
v0.1.1
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.
1
VALID_IDENTIFIER_CHARACTERS =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
2
3
def
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)
formatron.formats.utils.escape_identifier
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
src
formatron
formats
utils.py
Generated by
1.11.0