我正试图在python(或ipython)中找到idl日志的等效函数。我知道ipython有%logstart函数,但它只记录ipython中的输入/输出,所以如果我运行脚本,它要求我输入值,这些值就不会进入日志。为了清楚地表明,当我运行EELTnoM6.py脚本时,这里是我的终端:
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中找不到相同的东西时,我感到非常惊讶。。。
我认为您只想使用日志记录功能。它非常棒,可以很好地处理多线程。
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
:中
[09/17/2014 04:11:49]信息:记录器初始化
[09/17/2014 04:11:51]调试:解调矩阵?:C