程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to export IPython history to Py file?

編輯:Python

Hello , I'm yuechuang .

Blog launch :https://www.aiyc.top/1907.html

When I often teach one-on-one students , use IPython To demonstrate the code , After all IPython You don't have to. , I can't stop when I use it . I don't even want to use it Pycharm To debug the code .

however , It took so long , Have been thinking inertia to answer programming one-on-one students : IPython The advantage of is what I just said , But the code can't be saved .

A lot of people ask me , I have always been thinking about this and have done a very thorough answer . however , Today, a student , Computer majors in Berkeley , Ask me after class : teacher ,IPython Really can't keep 「 preservation 」 Code ?

I said yes. ! A very positive answer , There is no mistake in this answer all the time ! however , When I hear this sentence , I was stunned : Is it always right ?

That moment , It seems that something is broken , All the ignorance of self is reflected . I think of Chai Jing 《 See 》 In one sentence : If you want to “ See ”, Open your eyes from ignorance . That's the hardest part , Because ignorance is myself , Like a stone, it has become a dam in the heart .

Then I looked for it , Then there are the following notes :

Back to the question above , There are two ways to solve :

  1. use %hist After saving % Delete the beginning and then execute .Input[1]:%hist -f filename.py
  2. use %logstart and %logstop . It will take what you use % The command corresponds to Python Code ( Like the one below magic…).
In [7]: %logstart /tmp/test_log.py
Activating auto-logging. Current session state plus future input saved.
Filename : /tmp/test_log.py
Mode : backup
Output logging : False
Raw input log : False
Timestamping : False
State : active
In [8]: a = 10
In [9]: b = a*a
In [10]: %who
a b
In [11]: %logstop
In [12]: !cat /tmp/test_log.py
# IPython log file
357x46
357*46
54*32
53*42
52*43
532*4
get_ipython().magic(u'logstart /tmp/test_log.py')
a = 10
b = a*a
get_ipython().magic(u'who')
get_ipython().magic(u'logstop')
  1. for example , For your use case , Yes %save magic command You just type in %save my_useful_session 10-20 23 Save input line 10 to 20 and 23 To my_useful_session.py

If your IPython The conversation is as follows

In [1] : import numpy as np
....
In [135]: counter=collections.Counter(mapusercluster[3])
In [136]: counter
Out[136]: Counter({2: 700, 0: 351, 1: 233})

You want to go from 1 Save to 135, And then in the same IPython Use the following command on the session

In [137]: %save aiyc.py 1-135

This will all Python Statement is saved in the current directory ( start-up IPython The location of ) Medium aiyc.py In file .

Besides , The document states :

This function uses the same as %history For input range , Then save the line to the specified file name .

for example , This allows references to earlier sessions , for example

%save current_session ~0/
%save previous_session ~1/

AI Yuechuang · Launch the tutorial class , Include 「Python Language tutoring class 、C++ The tutorial class 、 Algorithm / Data structure tutorial class 、 Children programming 、pygame Game development 」, It's all one-on-one teaching : One on one coaching + One on one + Homework assignment + Project practice, etc .QQ、 Wechat Online , Be ready to respond !V:Jiabcdefh


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved