认识莳萝

(31) 2024-03-20 23:01:01

Has it ever happened to you that you’ve rebooted your computer and forgot about that important Jupyter Notebook you’ve intended to use later? “It took so long to finish and all is lost now”.

H作为它曾经发生在你身上,你已经重新启动您的计算机,忘了你打算以后使用重要Jupyter笔记本? “花了很长时间才能完成,现在一切都丢失了”。

I feel you! It happened to me many times.

我感觉到你了! 这件事在我身上发生了很多次。

Well, the only advice I can give you is to “Restart the Kernel and rerun all the cells”. But it doesn’t have to be like that next time.

好吧,我唯一能给您的建议是“重新启动内核并重新运行所有单元”。 但这不必下次再这样。

There is a tool that enables you to save the state of a Jupyter Notebook Session and restore it later in a single command.

有一个工具可让您保存Jupyter Notebook会话的状态并稍后在一个命令中将其还原。

In case you’ve missed my other posts about Jupyter Notebooks:

如果您错过了我有关Jupyter Notebooks的其他帖子:

认识莳萝 (Meet Dill)

认识莳萝 (https://mushiming.com/)  第1张

Save and restore a session with Dill
保存和恢复与Dill的会话

Dill enables you to save the state of a Jupyter Notebook session and restore it with a single command.

Dill使您可以保存Jupyter Notebook会话的状态并使用单个命令将其还原。

To install Dill is as simple as installing any other Python package:

安装Dill就像安装其他Python包一样简单:

pip install dill

To a save a Jupyter Notebook Session simply execute the command below:

要保存Jupyter Notebook会话,只需执行以下命令:

import dill
dill.dump_session('notebook_env.db')

To restore a Jupyter Notebook session:

要恢复Jupyter Notebook会话:

import dill
dill.load_session('notebook_env.db')

Dill also works with python interpreter sessions.

Dill还可以使用python解释器会话。

认识莳萝 (https://mushiming.com/)  第2张

giphy giphy

缺点 (Shortcomings)

Dill has a few shortcomings that you should be aware of:

莳萝有一些缺点,您应该意识到:

  • This approach doesn’t work with generators.

    这种方法不适用于生成器。

  • The saved session doesn’t include graphs that were generated. So you need to rerun the code.

    保存的会话不包括生成的图形。 因此,您需要重新运行代码。

  • Some users report that the restoring session doesn’t work on another machine.

    一些用户报告说恢复会话在另一台计算机上不起作用。

关于莳萝 (About Dill)

Dill extends python's pickle module for serializing and de-serializing Python objects to the majority of the built-in python types. Serialization is the process of converting an object to a byte stream, and the inverse of which is converting a byte stream back to a python object hierarchy.

Dill扩展了python的pickle模块,用于对大多数内置Python类型进行序列化和反序列化。 序列化是将对象转换为字节流的过程,而相反的过程是将字节流转换回python对象层次结构。

Dill provides the user with the same interface as the pickle module, and also includes some additional features. In addition to pickling python objects, Dill provides the ability to save the state of an interpreter session in a single command. Hence, it would be feasible to save an interpreter session, close the interpreter, ship the pickled file to another computer, open a new interpreter, unpickle the session and thus continue from the 'saved' state of the original interpreter session.

Dill为用户提供了与pickle模块相同的界面,并且还包含一些其他功能。 除了腌制python对象外,Dill还提供了在单个命令中保存解释器会话状态的功能。 因此,保存一个解释器会话,关闭该解释器,将已腌制的文件发送到另一台计算机,打开一个新的解释器,解开会话的会话,从而从原始解释器会话的“保存”状态继续是可行的。

Dill can be used to store python objects to a file, but the primary usage is to send python objects across the network as a byte stream. Dill is quite flexible and allows arbitrary user-defined classes and functions to be serialized. Thus Dill is not intended to be secure against erroneously or maliciously constructed data. It is left to the user to decide whether the data they unpickle is from a trustworthy source.

Dill可用于将python对象存储到文件中,但主要用途是作为字节流在网络上发送python对象。 Dill非常灵活,可以序列化任意用户定义的类和函数。 因此,Dill并非旨在防止错误或恶意构建的数据。 留给用户决定他们释放的数据是否来自可信赖的来源。

主要特点 (Major Features)

Dill can pickle the following standard types[1]:

莳萝可以腌制以下标准类型[1]:

  • none, type, bool, int, long, float, complex, str, unicode,

    无,类型,布尔,整数,长整型,浮点型,复杂,str,unicode,

  • tuple, list, dict, file, buffer, builtin,

    元组,列表,字典,文件,缓冲区,内置,

  • both old and new style classes,

    新旧样式的课程,

  • instances of old and new style classes,

    新旧样式类的实例,

  • set, frozenset, array, functions, exceptions

    设置,frozenset,数组,函数,异常

Dill can also pickle more 'exotic' standard types[1]:

莳萝还可以腌制更多的“异国”标准类型[1]:

  • functions with yields, nested functions, lambdas

    带有收益的函数,嵌套函数,lambdas

  • cell, method, unboundmethod, module, code, methodwrapper,

    单元格,方法,未绑定方法,模块,代码,methodwrapper,

  • dictproxy, methoddescriptor, getsetdescriptor, memberdescriptor,

    dictproxy,methoddescriptor,getsetdescriptor,memberdescriptor,

  • wrapperdescriptor, xrange, slice,

    包装器描述符,xrange,切片,

  • notimplemented, ellipsis, quit

    未实现,省略号,退出

Dill cannot yet pickle these standard types[1]:

莳萝还不能腌制这些标准类型[1]:

  • frame, generator, traceback

    框架,发生器,回溯

Dill also provides the capability to[1]:

Dill还提供以下功能:[1]:

  • save and load python interpreter sessions

    保存和加载python解释器会话

  • save and extract the source code from functions and classes

    保存并从函数和类中提取源代码

  • interactively diagnose pickling errors

    交互式诊断酸洗错误

  • [1] https://github.com/uqfoundation/dill

    [1] https://github.com/uqfoundation/dill

我的选择 (My option)

Dill is most certainly a tool that is helpful to have in your toolbox. I use it when I need to reboot my computer in another OS or when I expect that the Jupyter Kernel might crash with the next command.

莳萝无疑是一种非常有用的工具,可以放入您的工具箱。 当我需要在另一个操作系统上重新启动计算机时,或者当我期望Jupyter内核可能由于下一个命令而崩溃时,我会使用它。

IMO Dill is not a tool that you should automatically use in every Jupyter Notebook. Use it wisely, only when you need it. Also, be careful if any of the underlying code uses generators as cannot pickle it yet.

IMO Dill不是您应该在每个Jupyter Notebook中自动使用的工具。 仅在需要时才明智地使用它。 另外,请注意,如果任何基础代码都使用生成器,因为还无法对其进行酸洗。

你走之前 (Before you go)

认识莳萝 (https://mushiming.com/)  第3张

giphy giphy

Follow me on Twitter to join me on my creative journey.

在Twitter上关注我,加入我的创作之旅。

These are a few links that might interest you:

这些链接可能会让您感兴趣:

- Your First Machine Learning Model in the Cloud- AI for Healthcare- Parallels Desktop 50% off- School of Autonomous Systems- Data Science Nanodegree Program- 5 lesser-known pandas tricks- How NOT to write pandas code

翻译自: https://towardsdatascience.com/how-to-restore-your-jupyter-notebook-session-dfeadbd86d65

THE END

发表回复