1 | #!/usr/bin/env python
|
2 | """
|
3 | util.py
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | import sys
|
8 |
|
9 | PY2 = sys.version[0] == '2'
|
10 |
|
11 | # This uses Python3 keyword-only syntax!
|
12 | #def log(msg, *args, file=sys.stdout):
|
13 | #
|
14 | # WHY oh WHY didn't they rename print to something else, like say(). You have
|
15 | # to do something like this to hide "if PY2" because it doesn't even parse.
|
16 |
|
17 | #s = getattr(__builtins__, 'print')
|
18 |
|
19 | #def say(msg, *args, **kwargs):
|
20 | # if msg:
|
21 | # msg = msg % args
|
22 | # if 'file' in kwargs:
|
23 | # f = kwargs.pop('file')
|
24 | # else:
|
25 | # f = sys.stdout
|
26 | # if kwargs:
|
27 | # raise ValueError('Invalid keyword arguments %s' % kwargs)
|
28 | # if PY2:
|
29 | # print >>f, msg
|
30 | # else:
|
31 | # p(msg, file=f)
|
32 |
|
33 |
|
34 | def log(msg, *args):
|
35 | if args:
|
36 | msg = msg % args
|
37 | print(msg, file=sys.stderr)
|
38 |
|
39 |
|
40 | # compiler.py always tested isinstance(s, str)
|
41 | def is_unicode(s):
|
42 | if PY2:
|
43 | return isinstance(s, unicode)
|
44 | #return isinstance(s, unicode) or isinstance(s, str)
|
45 | else:
|
46 | return isinstance(s, str)
|
47 |
|
48 |
|
49 | if __name__ == '__main__':
|
50 | #say('say: %d', 1)
|
51 | log('log: %d', 1)
|
52 | #log('Test: %d', 1, foo=3)
|