]> jfr.im git - z_archive/twitter.git/blame - twitter/util.py
- Check rate limit using the command line tool. Patch by @stalkr_
[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
13except ImportError:
14 from htmlentitydefs import name2codepoint
8ad2cf0b 15
16def htmlentitydecode(s):
17 return re.sub(
a5e40197 18 '&(%s);' % '|'.join(name2codepoint),
f7e63802 19 lambda m: chr(name2codepoint[m.group(1)]), s)
8ad2cf0b 20
a5e40197
MV
21def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "):
22 inputs = []
23 while True:
24 if inputs:
25 prompt = ps2
26 else:
27 prompt = ps1
7bfe7d97 28 inputs.append(input(prompt))
a5e40197
MV
29 try:
30 ret = eval('\n'.join(inputs), globals_, locals_)
31 if ret:
30e61103 32 print(str(ret))
a5e40197
MV
33 return
34 except SyntaxError:
35 pass
36
098660ce
MV
37def printNicely(string):
38 if hasattr(sys.stdout, 'buffer'):
39 sys.stdout.buffer.write(string.encode('utf8'))
40 print()
41 else:
42 print(string.encode('utf8'))
43
a5e40197 44__all__ = ["htmlentitydecode", "smrt_input"]