简体   繁体   English

如何将文档存储在pickle文件中?

[英]How can I store documentation in a pickle file?

In Python, I generate quite often a pickle file to conserve the work I have done during programming. 在Python中,我经常会生成一个pickle文件来保存我在编程过程中所做的工作。

Is there any possibility to store something like a docstring in the pickle that explains how this pickle was generated and what it's meaning is. 是否有可能在泡菜中存储诸如文档字符串之类的内容,以解释该泡菜的产生方式及其含义。

Because you can combine all kinds of items into dictionaries, tuples, and lists before pickling them, I would say the most straightforward solution would be to use a dictionary that has a docstring key. 因为您可以在腌制它们之前将各种项目组合到字典,元组和列表中,所以我想说最直接的解决方案是使用带有文档字符串键的字典。

pickle_dict = {'objs': [some, stuff, inhere],
               'docstring': 'explanation of those objects'}

Of course, depending on what you are pickling, you may want key-value pairs for each object instead of a list of objects. 当然,根据要腌制的内容,可能需要每个对象的键值对而不是对象列表。

When you open the pickle back up, you can just read the docstring to remember how this pickle came to be. 当您打开泡菜备份时,您只需阅读文档字符串即可记住该泡菜的样子。


As an alternative solution, I often just need to save one or two integer values about the pickle. 作为一种替代解决方案,我通常只需要保存一个或两个关于泡菜的整数值。 In this case, I choose to save in the title of the pickle file. 在这种情况下,我选择保存在pickle文件的标题中。 Depending on what you are doing, this could be preferred so you can read the "docstring" without having to unpickle it. 根据您的操作,可能会更喜欢此方法,这样您就可以读取“ docstring”而不必解开。

DataFrames and lists don't typically have docstrings because they are data. DataFrame和列表通常没有文档字符串,因为它们是数据。 The docstring specification says: docstring规范说:

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. docstring是一个字符串文字,它作为模块,函数,类或方法定义中的第一条语句出现 Such a docstring becomes the __doc__ special attribute of that object. 这样的文档字符串成为该对象的__doc__特殊属性。

You can create any of these to make a docstring associated with the process that uses your data. 您可以创建任何这些文件,以使文档字符串与使用数据的流程关联。 The main class of your module for example. 例如,模块的主class

class MyClass:
    """My docstring"""

    def __init__(self, df):
        self.df = df # Your dataframe
    ...

Something like this seems like it is closest to what you were asking within the conventions of the language. 像这样的事情似乎最接近您在语言惯例中的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM