From ad0b0fbf20e61f70144c866bc9f28ae07bee06e4 Mon Sep 17 00:00:00 2001 From: tim hsu Date: Sat, 5 Oct 2013 13:50:48 +0800 Subject: [PATCH 1/2] uniout now detects ipython and support ipython now --- _uniout.py | 31 +++++++++++++++++++++++++++++++ uniout.py | 38 ++++++++++---------------------------- 2 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 _uniout.py diff --git a/_uniout.py b/_uniout.py new file mode 100644 index 0000000..f2b03f7 --- /dev/null +++ b/_uniout.py @@ -0,0 +1,31 @@ +import re +import sys +# the helpers + +escape_x_re = re.compile(r'(?:\\x[0-9a-f]{2})+') +escape_u_re = re.compile(r'(?:\\u[0-9a-f]{4}|\\U[0-9a-f]{8})+') +encoding = sys.getfilesystemencoding() + +def dexuescape(s): + r'''decode the \x, \u and \U in a escaped string -> encoded string''' + s = escape_x_re.sub(lambda m: m.group().decode('string-escape'), s) + s = escape_u_re.sub(lambda m: m.group().decode('unicode-escape').encode(encoding), s) + + # for Python < 2.7 + if isinstance(s, unicode): + s = s.encode(encoding) + + return s + +# make uniout +uniout = lambda: 'middleware of stdout' # any instance + +# make uniout look like stdout +for attrname in dir(sys.stdout): + if not attrname.startswith('__'): + setattr(uniout, attrname, getattr(sys.stdout, attrname)) + +# modify the write method to de-escape +uniout.write = lambda s: sys.__stdout__.write(dexuescape(s)) + +__slots__ = [uniout, dexuescape ] diff --git a/uniout.py b/uniout.py index 3edc7ff..4a60bcf 100644 --- a/uniout.py +++ b/uniout.py @@ -3,36 +3,18 @@ __version__ = '0.2.2' -import re import sys +from _uniout import uniout -# the helpers -escape_x_re = re.compile(r'(?:\\x[0-9a-f]{2})+') -escape_u_re = re.compile(r'(?:\\u[0-9a-f]{4}|\\U[0-9a-f]{8})+') -encoding = sys.getfilesystemencoding() +def runs_in_ipython(): -def dexuescape(s): - r'''decode the \x, \u and \U in a escaped string -> encoded string''' - s = escape_x_re.sub(lambda m: m.group().decode('string-escape'), s) - s = escape_u_re.sub(lambda m: m.group().decode('unicode-escape').encode(encoding), s) + import __builtin__ + return '__IPYTHON__' in __builtin__.__dict__ and \ + __builtin__.__dict__['__IPYTHON__'] - # for Python < 2.7 - if isinstance(s, unicode): - s = s.encode(encoding) - - return s - -# make uniout -uniout = lambda: 'middleware of stdout' # any instance - -# make uniout look like stdout -for attrname in dir(sys.stdout): - if not attrname.startswith('__'): - setattr(uniout, attrname, getattr(sys.stdout, attrname)) - -# modify the write method to de-escape -uniout.write = lambda s: sys.__stdout__.write(dexuescape(s)) - -# install the uniout -sys.stdout = uniout +if runs_in_ipython(): + from IPython.utils import io + io.stdout = io.IOStream(uniout) +else: + sys.stdout = uniout From 4aaa3029b3e4d9b0e38c2a71869fcfa0f8d4f094 Mon Sep 17 00:00:00 2001 From: tim hsu Date: Sat, 5 Oct 2013 14:15:03 +0800 Subject: [PATCH 2/2] use __all__ in _uniout moudel, to restict the number exported symbol --- _uniout.py | 2 +- uniout.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/_uniout.py b/_uniout.py index f2b03f7..c949a3a 100644 --- a/_uniout.py +++ b/_uniout.py @@ -28,4 +28,4 @@ def dexuescape(s): # modify the write method to de-escape uniout.write = lambda s: sys.__stdout__.write(dexuescape(s)) -__slots__ = [uniout, dexuescape ] +__all__ = ['uniout', 'dexuescape' ] diff --git a/uniout.py b/uniout.py index 4a60bcf..f5bc2e5 100644 --- a/uniout.py +++ b/uniout.py @@ -8,7 +8,6 @@ def runs_in_ipython(): - import __builtin__ return '__IPYTHON__' in __builtin__.__dict__ and \ __builtin__.__dict__['__IPYTHON__']