]> jfr.im git - z_archive/twitter.git/blob - twitter/util.py
Fix a test.
[z_archive/twitter.git] / twitter / util.py
1 """
2 Internal utility functions.
3
4 `htmlentitydecode` came from here:
5 http://wiki.python.org/moin/EscapingHtml
6 """
7
8
9 import re
10 import sys
11 try:
12 from html.entities import name2codepoint
13 unichr = chr
14 except ImportError:
15 from htmlentitydefs import name2codepoint
16
17 def htmlentitydecode(s):
18 return re.sub(
19 '&(%s);' % '|'.join(name2codepoint),
20 lambda m: unichr(name2codepoint[m.group(1)]), s)
21
22 def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "):
23 inputs = []
24 while True:
25 if inputs:
26 prompt = ps2
27 else:
28 prompt = ps1
29 inputs.append(input(prompt))
30 try:
31 ret = eval('\n'.join(inputs), globals_, locals_)
32 if ret:
33 print(str(ret))
34 return
35 except SyntaxError:
36 pass
37
38 def printNicely(string):
39 if hasattr(sys.stdout, 'buffer'):
40 sys.stdout.buffer.write(string.encode('utf8'))
41 print()
42 else:
43 print(string.encode('utf8'))
44
45 __all__ = ["htmlentitydecode", "smrt_input"]