Created
February 14, 2011 01:36
-
-
Save alice1017/825368 to your computer and use it in GitHub Desktop.
redirect a output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| #coding:utf-8 | |
| import sys,types | |
| from StringIO import StringIO | |
| class Redirect: | |
| def __init__(self): | |
| sys.stdout = StringIO() | |
| self.stdout = sys.stdout | |
| def getitem(self): | |
| if self.check(): | |
| item = sys.stdout.getvalue() | |
| self.stdout.close() | |
| sys.stdout = sys.__stdout__ | |
| return item | |
| def getitems(self): | |
| if self.check(): | |
| item = sys.stdout.getvalue() | |
| items = item.split("\n") | |
| self.stdout.close() | |
| sys.stdout = sys.__stdout__ | |
| return items | |
| def quit(self): | |
| if self.check(): self.stdout.close();sys.stdout = sys.__stdout__ | |
| def check(self): | |
| if isinstance(self.stdout,types.InstanceType): return True | |
| else:return False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment