简体   繁体   English

ConfigParser之类的东西是否适合在运行之间保存状态(键,值)?

[英]Is something like ConfigParser appropriate for saving state (key, value) between runs?

I want to save a set of key, value pairs (string, int) between runs of a Python program, reload them on subsequent runs and write the changes to be available on the next run. 我想在Python程序的运行之间保存一组键值对(字符串,整数),在后续运行时重新加载它们,并将更改写入下次运行时可用。

I don't think of this data as a configuration file, but it would fit the ConfigParser capabilities quite well. 我不认为这些数据是配置文件,但它很适合ConfigParser功能。 I would only need two [sections]. 我只需要两个[部分]。 It's only a few hundred pairs and very simple so I don't think it's necessary to do an actual database. 它只有几百对而且非常简单,所以我认为不需要做一个真正的数据库。

Is it appropriate to use ConfigParser in this way? 以这种方式使用ConfigParser是否合适? I've also considered using Perl and XML::Simple. 我也考虑过使用Perl和XML :: Simple。 What about that? 那个怎么样? Is there a way to do this in bash without Python or Perl? 有没有办法在没有Python或Perl的bash中执行此操作?

Well, you have better options. 好吧,你有更好的选择。 You can for example use pickle or json format. 例如,您可以使用picklejson格式。 Pickle serializing module is very easy to use. Pickle序列化模块非常易于使用。

import cPickle
cPickle.dump(obj, open('save.p', 'wb')) 
obj = cPickle.load(open('save.p', 'rb'))

The format is not human readable and unpickling is not secure against erroneous or maliciously constructed data. 格式不是人类可读的,并且unpickling对于错误或恶意构造的数据是不安全的。 You should not unpickle untrusted data. 您不应该取消不受信任的数据。

If you are using python 2.6 there is a builtin module called json . 如果您使用的是python 2.6,则会有一个名为json的内置模块。 It is as easy as pickle to use: 它就像泡菜一样简单:

import json
encoded = json.dumps(obj)
obj = json.loads(encoded)

Json format is human readable and is very similar to the dictionary string representation in python. Json格式是人类可读的,与python中的字典字符串表示非常相似。 And doesn't have any security issues like pickle. 而且没有像泡菜这样的安全问题。

If you are using an earlier version of python you can simplejson instead. 如果您使用的是早期版本的python,则可以使用simplejson

For me, PyYAML works well for these kind of things. 对我来说, PyYAML适合这类事情。 I used to use pickle or ConfigParser before. 我之前习惯使用pickle或ConfigParser。

ConfigParser is a fine way of doing it. ConfigParser是一种很好的方法。 There are other ways (the json and cPickle modules already mentioned may be useful) that are also good, depending on whether you want to have text files or binary files and if you want code to work simply in older versions of Python or not. 还有其他方法(已经提到的json和cPickle模块可能有用)也很好,这取决于你是想要文本文件还是二进制文件,以及你是否希望代码只在旧版本的Python中工作。

You may want to have a thin abstraction layer on top of your chosen way to make it easier to change your mind. 您可能希望在您选择的方式之上拥有一个精简的抽象层,以便更容易改变主意。

Sounds like a job for a dbm . 听起来像是dbm的工作。 Basically it is a hash that lives external to your program. 基本上它是一个存在于程序外部的哈希。 There are many implementations. 有很多实现。 In Perl it is trivial to tie a dbm to a hash (ie make it look like a dbm is really a normal hash variable). 在Perl中, 将dbm绑定到散列 (即使它看起来像dbm实际上是普通的散列变量)是微不足道的。 I don't know if there is any equivalent in mechanism in Python, but I would be surprised if there weren't. 我不知道Python中是否有任何等效的机制,但如果没有,我会感到惊讶。

重新做在bash:如果你的字符串是有效标识符,你可以使用环境变量和env

If you can update the state key by key then any of the DBM databases will work. 如果可以按键更新状态键,则任何DBM数据库都可以正常工作。 If you need really high performance and compact storage then Tokyo Cabinet - http://tokyocabinet.sourceforge.net/ is the cool toy. 如果您需要真正的高性能和紧凑型存储,那么Tokyo Cabinet - http://tokyocabinet.sourceforge.net/就是很酷的玩具。

If you want to save and load the whole thing at once (to maybe keep old versions or some such) and don't have too much data then just use JSON. 如果你想一次保存和加载整个东西(可能保留旧版本或其他一些)并且没有太多数据,那么只需使用JSON。 It's much nicer to work with than XML. 使用它比使用XML要好得多。 I don't know how the JSON implementation is in Python, but in Perl the JSON::XS module is insanely fast. 我不知道JSON实现是如何在Python中实现的,但在Perl中,JSON :: XS模块的速度非常快。

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

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