Is there a way to get a local timestamp in my IPython prompt?

谁说胖子不能爱 提交于 2019-12-04 04:22:37

问题


Is there a way to get a local timestamp in my IPython prompt? I'm using IPython 0.10 and Python 2.6 on 64-bit Windows Vista.

My current default prompt is

[C:Python26/Scripts]
|9>

OK, I tried to follow your directions exactly. However, my experience has been that all config editing is best kept to my ipy_user_conf.py. To quote from it:

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.

So I now have these lines in 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'[\#] '

And I get this, for an example:

16:49:50 In[9]:1/7
1           [9] 0.14285714285714285

16:50:09 In[10]:

Questions:

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

    [C:Python26/Scripts]
    |8>
    

Once more with feeling. I'm so sorry for the mess I've made. I need to report the lines I either added or modified actually are:

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:


The easiest way is to edit your ipythonrc (in your home\_ipython directory), and add these lines:

import_mod datetime
prompt_in1 '${datetime.datetime.now()} In [\#]: '
# or
prompt_in1 '${datetime.datetime.now().strftime("%H:%M:%S")} In [\#]: '

Alternatively, you can also just add the import_mod datetime to the rc file, and add this to the main() function of ipy_user_conf.py (in the same directory):

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


来源:https://stackoverflow.com/questions/3879752/is-there-a-way-to-get-a-local-timestamp-in-my-ipython-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!