-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
input prompts are now cleaned up automatically.
- Loading branch information
1 parent
df7d88c
commit 180c393
Showing
4 changed files
with
62 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
''' | ||
This class forks a stream, allowing you to capture output while still displaying | ||
output in the terminal. | ||
See: http://www.tentech.ca/2011/05/stream-tee-in-python-saving-stdout-to-file-while-keeping-the-console-alive/ | ||
''' | ||
class StreamTee(object): | ||
# Based on https://gist.github.com/327585 by Anand Kunal | ||
def __init__(self, stream1, stream2, symbol='.'): | ||
self._stream1 = stream1 | ||
self._stream2 = stream2 | ||
self.__missing_method_name = None # Hack! | ||
self._loading_symbol = symbol | ||
self._num_inputs = 0 | ||
|
||
def __getattribute__(self, name): | ||
return object.__getattribute__(self, name) | ||
|
||
def __getattr__(self, name): | ||
self.__missing_method_name = name # Could also be a property | ||
return getattr(self, '__methodmissing__') | ||
|
||
def __methodmissing__(self, *args, **kwargs): | ||
if len(args) > 0 and args[0] != '\n' and args[0] != self._loading_symbol: | ||
self._num_inputs += 1 | ||
|
||
# Emit method call to the forked stream | ||
callable2 = getattr(self._stream2, self.__missing_method_name) | ||
callable2(*args, **kwargs) | ||
|
||
# Emit method call to stream 1 | ||
callable1 = getattr(self._stream1, self.__missing_method_name) | ||
return callable1(*args, **kwargs) | ||
|
||
@property | ||
def num_inputs(self): | ||
return self._num_inputs |
This file contains 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