]> jfr.im git - z_archive/twitter.git/blame - twitter/util.py
make inversion optional
[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
098660ce 10import sys
3930cc7b
MV
11try:
12 from html.entities import name2codepoint
d9e92207 13 unichr = chr
3930cc7b
MV
14except ImportError:
15 from htmlentitydefs import name2codepoint
8ad2cf0b 16
17def htmlentitydecode(s):
18 return re.sub(
a5e40197 19 '&(%s);' % '|'.join(name2codepoint),
1bb6d474 20 lambda m: unichr(name2codepoint[m.group(1)]), s)
8ad2cf0b 21
a5e40197
MV
22def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "):
23 inputs = []
24 while True:
25 if inputs:
26 prompt = ps2
27 else:
28 prompt = ps1
7bfe7d97 29 inputs.append(input(prompt))
a5e40197
MV
30 try:
31 ret = eval('\n'.join(inputs), globals_, locals_)
32 if ret:
30e61103 33 print(str(ret))
a5e40197
MV
34 return
35 except SyntaxError:
36 pass
37
098660ce
MV
38def 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
a5e40197 45__all__ = ["htmlentitydecode", "smrt_input"]