16
|
1 #!/usr/bin/python
|
|
2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv>
|
|
3
|
|
4 try:
|
|
5 # Python 3
|
|
6 exec('say = print')
|
|
7 except SyntaxError:
|
|
8 try:
|
|
9 # Python 2.6/2.7
|
|
10 exec('say = __builtins__["print"]')
|
|
11 except Exception:
|
|
12 # Python 2.5
|
|
13 import sys
|
|
14 # This should fully emulate the print function of Python 2.6 in Python 2.3+
|
|
15 # The error messages are taken from Python 2.6/2.7
|
|
16 def saytypeerror(value, name):
|
|
17 return TypeError(name + ' must be None, str or unicode, not ' + type(value).__name__)
|
|
18 def say(*values, **kwargs):
|
|
19 sep = kwargs.pop('sep' , None)
|
|
20 end = kwargs.pop('end' , None)
|
|
21 file = kwargs.pop('file', None)
|
|
22 if kwargs: raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.popitem()[0])
|
|
23 if sep is None: sep = ' '
|
|
24 if end is None: end = '\n'
|
|
25 if file is None: file = sys.stdout
|
|
26 if not isinstance(sep, basestring): raise saytypeerror(sep, 'sep')
|
|
27 if not isinstance(end, basestring): raise saytypeerror(end, 'end')
|
|
28 file.write(sep.join((str(i) for i in values)) + end)
|
|
29
|
|
30 def import_urllib():
|
|
31 try:
|
|
32 # Python 3
|
|
33 import urllib.request
|
|
34 return urllib.request, lambda url: urllib.request.urlopen(url).read().decode()
|
|
35 except ImportError:
|
|
36 # Python 2
|
|
37 import urllib
|
|
38 return urllib, lambda url: urllib.urlopen(url).read() |