- Timestamp:
- Aug 11, 2016, 11:44:38 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r999 r1003 4 4 import re 5 5 import numpy.ma as ma 6 from cStringIO import StringIO 7 import sys 6 8 7 9 main = 'nc_var_tools.py' … … 24 26 25 27 ####### Content 28 # Capturing: Class to capture the standard output from a function 26 29 # CFmonthU_daysU: Function to transform from a CF date series with units as 'months since [DATE]' to 'days since [DATE]' 27 30 # CFvar_DIAGvar: Function to provide which model diagnostic values can provide a CF-variable from ASCII file … … 8262 8265 return dictv 8263 8266 8267 class Capturing(list): 8268 """ Class to capture the standard output from a function 8269 from: http://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call 8270 Usage: 8271 8272 'output' is now a list containing the lines printed by the function call. 8273 >>> romdict = {'i': 1, 'ii': 2, 'iii': 3, 'iv': 4, 'v':5, 'vi':6, 'vii':7, 'viii':8, 'ix': 9, 'x':10} 8274 >>> with Capturing() as output: 8275 >>> printing_dictionary(romdict) 8276 >>> print output 8277 [' # i: 1', ' # ii: 2', ' # iii: 3', ' # iv: 4'] 8278 """ 8279 def __enter__(self): 8280 self._stdout = sys.stdout 8281 sys.stdout = self._stringio = StringIO() 8282 return self 8283 def __exit__(self, *args): 8284 self.extend(self._stringio.getvalue().splitlines()) 8285 sys.stdout = self._stdout 8286 8264 8287 #quit() 8265 8288
Note: See TracChangeset
for help on using the changeset viewer.