[英]Saving all the variables(Work Space) of a session in Python_Shelve_Pickle
I am trying to write a program in python, Which can work almost like a R's save.我正在尝试用 python 编写一个程序,它几乎可以像 R 的保存一样工作。 image which saves your workspace.
保存您的工作区的图像。 Follwoing is my code, but it is not running smoothly, Please help me to make it work
以下是我的代码,但运行不顺利,请帮助我使其工作
I have used following sample data and a plot so that it can be tested upon it我使用了以下示例数据和绘图,以便可以对其进行测试
import shelve
import numpy as np
import matplotlib.pyplot as plt
import pickle
import os
import pandas as pd
x = np.arange(-3, 3, 0.01)
y = np.sin(np.pi*x)
fig = plt.figure()
ax = fig.add_subplot(111)
line=ax.plot(x, y)
#shelving or pickling my session
my_shelf = shelve.open('shelve.out','c') # 'n' for new
for name in dir():
if not name.startswith (('__','_','In','Out','exit','quit','get_ipython')):
try:
my_shelf[name] = globals()[name] # I didn't undersatnd why to use globals()
except Exception:
pass
print('ERROR shelving: {0}'.format(name))
my_shelf.close()
my_shelf = shelve.open('shelve.out','r')
for key in my_shelf:
globals()[key]=my_shelf[key]
my_shelf.close()
with open('save.p', 'wb') as f:
for name in dir():
if not name.startswith (('__','_','In','Out','exit','quit','get_ipython')):
try:
pickle.dump(name, f)
except Exception:
print('ERROR shelving: {0}'.format(name))
pass
with open('save.p',"rb") as f: # Python 3: open(..., 'rb')
pickle.load(f)
I Will highly appreciate any help, Apologies for any mis-indentation, while pasting on the overflow they got change我将非常感谢任何帮助,为任何错误的缩进道歉,同时粘贴他们得到改变的溢出
The dill module provides this functionality. dill 模块提供了这个功能。
dump_session(filename='/tmp/session.pkl', main=None, byref=False, **kwds) dump_session(filename='/tmp/session.pkl', main=None, byref=False, **kwds)
load_session(filename='/tmp/session.pkl', main=None, **kwds)负载会话(文件名 =“/tmp/session.pkl”,主要 = 无,**kwds)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.