如何在我的IPython提示符中获取本地时间戳?

3
有没有一种方法可以在我的IPython提示符中获取本地时间戳?我正在使用IPython 0.10和Python 2.6在64位Windows Vista上。
我的当前默认提示符是:
[C:Python26/Scripts]
|9>

好的,我尝试严格按照您的指示操作。但是我的经验告诉我,所有配置编辑最好保留在ipy_user_conf.py文件中。引用其中的一句话:

User configuration file for IPython (ipy_user_conf.py)

This is a more flexible and safe way to configure ipython than *rc files
(ipythonrc, ipythonrc-pysh etc.)

This file is always imported on ipython startup. You can import the
ipython extensions you need here (see IPython/Extensions directory).

Feel free to edit this file to customize your ipython experience.

Note that as such this file does nothing, for backwards compatibility.
Consult e.g. file 'ipy_profile_sh.py' for an example of the things 
you can do here.

See http://ipython.scipy.org/moin/IpythonExtensionApi for detailed
description on what you could do here.

现在我在main()函数中有以下这些行:

# -- prompt
# A different, more compact set of prompts from the default ones, that
# always show your current location in the filesystem:

o.prompt_in1 = r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Normal\n\C_Green|\#>'
o.prompt_in2 = r'.\D: '
o.prompt_out = r'[\#] '

并且我拿到了这个作为例子:
16:49:50 In[9]:1/7
1           [9] 0.14285714285714285

16:50:09 In[10]:

问题:

  1. What is that 1?
  2. How can I keep the current directory in the prompt? Before, I had

    [C:Python26/Scripts]
    |8>
    

再试一次。 非常抱歉我造成的混乱。我需要汇报我添加或修改的行如下:

import_all("os sys random datetime")
o.prompt_in1 = r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Normal\n\C_Green|\#>'
o.prompt_in1 = r'${datetime.now().strftime("%H:%M:%S")}[\#]:'
o.prompt_out = r'[\#] '
1个回答

2
最简单的方法是编辑您的ipythonrc文件(位于您的home\_ipython目录中),并添加以下行:
import_mod datetime
prompt_in1 '${datetime.datetime.now()} In [\#]: '
# or
prompt_in1 '${datetime.datetime.now().strftime("%H:%M:%S")} In [\#]: '

或者,您也可以将 import_mod datetime 添加到 rc 文件中,并将其添加到 ipy_user_conf.py 的 main() 函数中(在同一目录中):

o = ip.options
o.prompt_in1 = r'${datetime.datetime.now()} In [\#]: '
# or
o.prompt_in1 = r'${datetime.datetime.now().strftime("%H:%M:%S")} In [\#]: '

import_all("os sys random datetime") #execf('~/_ipython/ns.py')# -- 提示符 # 一组不同的、更紧凑的提示符,总是显示您当前在文件系统中的位置: o.prompt_in1 = r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Normal\n\C_Green|\#>' o.prompt_in2 = r'.\D: ' o.prompt_out = r'[\#] ' o.prompt_in1 = r'${datetime.now().strftime("%H:%M:%S")} In[\#]: ' - NotSuper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接