2012-09-14 128 views
1

我想在python(或ipython)中找到idl日誌的等效函數。 我知道ipython有%logstart函數,但只記錄在ipython中的輸入/輸出,所以如果我運行我的腳本,並要求我輸入值,這些不會進入日誌。爲了在這裏清楚的是我的終端,當我運行EELTnoM6.py腳本:python相當於idl的日誌功能?

In [11]: run EELTnoM6 

################################################################ 
##    STOKES vector after the system     ## 
################################################################ 

== Demodulation matrix == 
Automatic:    A 
ZIMPOL/EELT:    Z 
EELT IF     Eif 
Custom     C 
Demodulation matrix?: C 
Efficiency of the detector?: 1. 
Demodulation matrix? (e.g. [ [1.,0.],[0.,1.] ]): [[1/6.,1/6.,1/6.,1/6.,1/6.,1/6.],  [0.5,-0.5,0.,0.,0.,0.],[0.,0.,-0.5,0.5,0.,0.],[0.,0.,0.,0.,-0.5,0.5]] 
ModelStokesMeasurement time = 65.6509261131 
Simulation time = 151.731481075 

和這裏是我在日誌中得到:

# IPython log file 
%logstart -o -r EELTnoM6_log rotate 
ls 
%logstop 
run EELTnoM6 
%logoff 

我想在日誌存儲輸入我給當腳本詢問,即

Demodulation matrix?: C 
Efficiency of the detector?: 1. 
Demodulation matrix? (e.g. [ [1.,0.],[0.,1.] ]): [[1/6.,1/6.,1/6.,1/6.,1/6.,1/6.],[0.5,-0.5,0.,0.,0.,0.],[0.,0.,-0.5,0.5,0.,0.],[0.,0.,0.,0.,-0.5,0.5]] 

所以C,1和矩陣能夠擁有相同的值再次運行。這在IDl中非常容易,所以當我找不到ipython時,我感到非常驚訝...

回答

0

我想你只是想用logging函數。它非常棒,並且能夠很好地處理多線程。

import logging 

log_file = 'journal.log' # Specify path to your log file 

log_format = '[%(asctime)s] %(levelname)s: %(message)s' # How the output is displayed 
log_date_fmt = '%m/%d/%Y %I:%M:%S %p' # How the asctime is displayed 

logging.basicConfig(filename=log_file, level='DEBUG', filemode='w', format=log_format, datefmt=log_date_fmt) # Fire up the logger 
logging.info('Logger initialized') # There are DEBUG, INFO, WARNING, ERROR levels 

def journal(msg): 
    ans = raw_input(msg) # Get your user input 
    logging.debug(msg + ans) # Choosing to put it as DEBUG category 
    return ans 

ans = journal('Demodulation matrix?: ') # From your example 

給人的輸出(包含在./journal.log

[2014年9月17日下午4點11分49秒] INFO:記錄器初始化

[2014年9月17日04: DEBUG:解調矩陣?:C