Skip to content

Commit

Permalink
Merge pull request #4 from timtan/dev
Browse files Browse the repository at this point in the history
Add a new class UniOut. and make stdout and stderr wraps with UniOut
  • Loading branch information
moskytw committed Oct 5, 2013
2 parents 62756e4 + 393f385 commit 31e41ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
23 changes: 14 additions & 9 deletions _uniout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(object):

# modify the write method to de-escape
uniout.write = lambda s: sys.__stdout__.write(dexuescape(s))
def __init__(self, stream):

__all__ = ['uniout', 'dexuescape']
self.stream = stream

# make uniout look like stdout
for attrname in dir(stream):
if not attrname.startswith('_'):
setattr(self, attrname, getattr(stream, attrname))

# modify the write method to de-escape
self.write = lambda data: self.stream.write(dexuescape(data))


__all__ = ['Uniout', 'dexuescape']
8 changes: 5 additions & 3 deletions uniout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__version__ = '0.2.2'

import sys
from _uniout import uniout
from _uniout import Uniout

def runs_in_ipython():
import __builtin__
Expand All @@ -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)

0 comments on commit 31e41ec

Please sign in to comment.