Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I avoid storing a command in ipython history?

In bash I can prevent a command from being saved to bash history by putting a space in front of it. I cannot use this method in ipython. How would I do the equivalent in ipython?

like image 488
Andrew Avatar asked Jan 29 '16 20:01

Andrew


People also ask

How do I clear my IPython history?

History is store on $(ipython locate)/profile_default/history. sqlite by default. You can remove the file, and or do any operation you want on it (secure erase, etc..). It's an sqlite file so you can load it with any sqlite program and do query on it.

How do you stop an execution in IPython?

For the current version of IPython(Jupyter), you can simply click on Interrupt option under kernel tab.

Where does IPython Store history?

IPython stores its files—config, command history and extensions—in the directory ~/. ipython/ by default. If set, this environment variable should be the path to a directory, which IPython will use for user data.

How do I clean my IPython console?

For both Python shell and IPython shell, they have command history through the arrow key. Clicking the up arrow key will show previously typed commands. To clear the output on Console, use Ctrl-L.


1 Answers

I think I finally figure this out. This method not really 'prevent' the ipython to record history but actually delete the history record. But I think still it is a good way to achieve this goal.

ipython history is stored in a sqlite database file in $(ipython locate)/profile_default/history.sqlite.

The database table History has four columns: session, line, source and source_raw. The session column presents for the session_id of current session, it can be get with the method in ipython: get_ipython().history_manager.hisdb.get_last_session_id() in ipython. The line column is just the line num.

So what I'm going to do is I'm going to delete this record in the database within ipython enviroment:

1) The history db object can be get using get_ipython().history_manager.db in ipython

hismgr = get_ipython().history_manager   

2) get the session id:

session_id = hismgr.get_last_session_id()

3) delete the history line with the line_id (assume it is 111 here) and session_id

hismgr.db.execute('DELETE FROM history WHERE line={0} and session={1}'.format(111, session_id))

4) the In and Out Array List is also kind like history, but it stored in memory, so it could be sweep or rewrite like variable.

In[111]=Out[111]=''

To 'prevent a command from being saved to ipython history', means delete the line's history record in database and and rewrite In and Out Array in ipython enviroment. And we could combine these four lines into to one to do the job one time for all. Here is an example if we want to delete line 128:

In [127]: history -l 3 -n
 124: history -l 3 -n
 125: hismgr = get_ipython().history_manager; session_id = hismgr.get_last_session_id();hismgr.db.execute('DELETE FROM history WHERE line={0} and session={1}'.format(123, session_id))
 126: history -l 3 -n

In [128]: passwd='123456'

In [129]: hismgr = get_ipython().history_manager; session_id = hismgr.get_last_session_id();hismgr.db.execute('DELETE FROM history WHERE line={0} and session={1}'.format(128, session_id));In[128]=Out[128]='';
Out[129]: <sqlite3.Cursor at 0x7f8dce3dae30>

In [130]: history -l 3 -n
 126: history -l 3 -n
 127: history -l 3 -n
 129: hismgr = get_ipython().history_manager; session_id = hismgr.get_last_session_id();hismgr.db.execute('DELETE FROM history WHERE line={0} and session={1}'.format(128, session_id));In[128]=Out[128]='';

The history line 128 is gone.

Here is some documents I went through

IPython.core.history.HistoryManager

python-sqlite3

like image 172
Evan Avatar answered Oct 10 '22 02:10

Evan