]> jfr.im git - z_archive/twitter.git/blame - twitter/util.py
Make the code python 2 and 3 compatible.
[z_archive/twitter.git] / twitter / util.py
CommitLineData
8ad2cf0b 1"""
2Internal utility functions.
3
4`htmlentitydecode` came from here:
5 http://wiki.python.org/moin/EscapingHtml
6"""
7
8
9import re
3930cc7b
MV
10try:
11 from html.entities import name2codepoint
12except ImportError:
13 from htmlentitydefs import name2codepoint
8ad2cf0b 14
15def htmlentitydecode(s):
16 return re.sub(
a5e40197 17 '&(%s);' % '|'.join(name2codepoint),
f7e63802 18 lambda m: chr(name2codepoint[m.group(1)]), s)
8ad2cf0b 19
a5e40197
MV
20def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "):
21 inputs = []
22 while True:
23 if inputs:
24 prompt = ps2
25 else:
26 prompt = ps1
7bfe7d97 27 inputs.append(input(prompt))
a5e40197
MV
28 try:
29 ret = eval('\n'.join(inputs), globals_, locals_)
30 if ret:
7bfe7d97 31 print(ret)
a5e40197
MV
32 return
33 except SyntaxError:
34 pass
35
36__all__ = ["htmlentitydecode", "smrt_input"]