From 338dca455fb8d9f0b4c5755a21367ef2c89a2008 Mon Sep 17 00:00:00 2001 From: tim hsu Date: Sat, 5 Oct 2013 16:15:56 +0800 Subject: [PATCH 1/3] Add a new class UniOut. and make stdout and stderr wraps with UniOut --- _uniout.py | 23 ++++++++++++++--------- uniout.py | 8 +++++--- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/_uniout.py b/_uniout.py index b096af4..189b381 100644 --- a/_uniout.py +++ b/_uniout.py @@ -21,15 +21,20 @@ def dexuescape(s): 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)) +class UniOut: -# modify the write method to de-escape -uniout.write = lambda s: sys.__stdout__.write(dexuescape(s)) + def __init__(self,stream): -__all__ = ['uniout', 'dexuescape'] + # make uniout look like stdout + for attrname in dir(stream): + if not attrname.startswith('_'): + setattr(self, attrname, getattr(stream, attrname)) + + self.stream = stream + + # modify the write method to de-escape + self.write = lambda data: self.stream.write(dexuescape(data)) + + +__all__ = ['UniOut', 'dexuescape'] diff --git a/uniout.py b/uniout.py index e168030..ff00e74 100644 --- a/uniout.py +++ b/uniout.py @@ -4,7 +4,7 @@ __version__ = '0.2.2' import sys -from _uniout import uniout +from _uniout import UniOut def runs_in_ipython(): import __builtin__ @@ -13,6 +13,8 @@ def runs_in_ipython(): if runs_in_ipython(): from IPython.utils import io - io.stdout = io.IOStream(uniout) + io.stdout = UniOut(sys.stdout) + io.stderr = UniOut(sys.stderr) else: - sys.stdout = uniout + sys.stdout = UniOut(sys.stdout) + sys.stderr = UniOut(sys.stderr) From fa1b84b07a605114a03cd7bfa2f58ddcf33d7f76 Mon Sep 17 00:00:00 2001 From: tim hsu Date: Sat, 5 Oct 2013 16:55:58 +0800 Subject: [PATCH 2/3] rename from UniOut to Uniout --- _uniout.py | 4 ++-- uniout.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/_uniout.py b/_uniout.py index 189b381..22e35f6 100644 --- a/_uniout.py +++ b/_uniout.py @@ -22,7 +22,7 @@ def dexuescape(s): return s -class UniOut: +class Uniout: def __init__(self,stream): @@ -37,4 +37,4 @@ def __init__(self,stream): self.write = lambda data: self.stream.write(dexuescape(data)) -__all__ = ['UniOut', 'dexuescape'] +__all__ = ['Uniout', 'dexuescape'] diff --git a/uniout.py b/uniout.py index ff00e74..bc57fa9 100644 --- a/uniout.py +++ b/uniout.py @@ -4,7 +4,7 @@ __version__ = '0.2.2' import sys -from _uniout import UniOut +from _uniout import Uniout def runs_in_ipython(): import __builtin__ @@ -13,8 +13,8 @@ def runs_in_ipython(): if runs_in_ipython(): from IPython.utils import io - io.stdout = UniOut(sys.stdout) - io.stderr = UniOut(sys.stderr) + io.stdout = Uniout(sys.stdout) + io.stderr = Uniout(sys.stderr) else: - sys.stdout = UniOut(sys.stdout) - sys.stderr = UniOut(sys.stderr) + sys.stdout = Uniout(sys.stdout) + sys.stderr = Uniout(sys.stderr) From 393f3859577c3805d08a270c1ae6a92315b014be Mon Sep 17 00:00:00 2001 From: tim hsu Date: Sat, 5 Oct 2013 17:07:02 +0800 Subject: [PATCH 3/3] change to new style object and code formating --- _uniout.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_uniout.py b/_uniout.py index 22e35f6..5bc03eb 100644 --- a/_uniout.py +++ b/_uniout.py @@ -22,17 +22,17 @@ def dexuescape(s): return s -class Uniout: +class Uniout(object): - def __init__(self,stream): + def __init__(self, stream): + + self.stream = stream # make uniout look like stdout for attrname in dir(stream): if not attrname.startswith('_'): setattr(self, attrname, getattr(stream, attrname)) - self.stream = stream - # modify the write method to de-escape self.write = lambda data: self.stream.write(dexuescape(data))