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 :
%hist
After saving %
Delete the beginning and then execute .Input[1]:%hist -f filename.py%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')
%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