Django使用真正的Python文件进行设置,Trac使用.ini文件,其他一些软件使用XML文件来保存这些信息.
这些方法中有一种是由Guido和/或Python社区提供的吗?
Lak*_*sad 33
取决于主要的目标受众.
如果是程序员无论如何都要更改文件,只需使用settings.py之类的python文件
如果它是最终用户,那么考虑一下ini文件.
drd*_*man 15
不知道这是否可以被视为"官方",但它在标准库中:14.2.ConfigParser - 配置文件解析器.
显然,这不是一个通用的解决方案.只需使用最适合任务的任何东西,没有任何必要的复杂性(特别是 - 图灵完整性!考虑自动或GUI配置器).
Mos*_*vah 15
我用架子(http://docs.python.org/library/shelve.html):
shelf = shelve.open(filename)
shelf["users"] = ["David", "Abraham"]
shelf.sync() # Save
Run Code Online (Sandbox Code Playgroud)
还有一个选项,PyQt.Qt具有独立于平台的方式来存储QSettings类的设置.在引擎盖下,在Windows上它使用注册表,在linux中它将设置存储在隐藏的conf文件中.QSettings工作得很好,而且很无聊.
我不确定是否存在"官方"方式(在Python的Zen中没有提及:)) - 我倾向于自己使用Config Parser模块,我认为你会发现这很常见.我更喜欢python文件方法,因为你可以回写它并在需要时动态重新加载.
据我所知,没有幸福的解决方案.存储应用程序设置没有正确或错误的方式,只要你很舒服,xml,json或所有类型的文件都可以.对于python我个人使用pypref它非常简单,跨平台和直接.
pypref非常有用,因为可以存储静态和动态设置和首选项...
from pypref import Preferences
# create singleton preferences instance
pref = Preferences(filename="preferences_test.py")
# create preferences dict
pdict = {'preference 1': 1, 12345: 'I am a number'}
# set preferences. This would automatically create preferences_test.py
# in your home directory. Go and check it.
pref.set_preferences(pdict)
# lets update the preferences. This would automatically update
# preferences_test.py file, you can verify that.
pref.update_preferences({'preference 1': 2})
# lets get some preferences. This would return the value of the preference if
# it is defined or default value if it is not.
print pref.get('preference 1')
# In some cases we must use raw strings. This is most likely needed when
# working with paths in a windows systems or when a preference includes
# especial characters. That's how to do it ...
pref.update_preferences({'my path': " r'C:\Users\Me\Desktop' "})
# Sometimes preferences to change dynamically or to be evaluated real time.
# This also can be done by using dynamic property. In this example password
# generator preference is set using uuid module. dynamic dictionary
# must include all modules name that must be imported upon evaluating
# a dynamic preference
pre = {'password generator': "str(uuid.uuid1())"}
dyn = {'password generator': ['uuid',]}
pref.update_preferences(preferences=pre, dynamic=dyn)
# lets pull 'password generator' preferences twice and notice how
# passwords are different at every pull
print pref.get('password generator')
print pref.get('password generator')
# those preferences can be accessed later. Let's simulate that by creating
# another preferences instances which will automatically detect the
# existance of a preferences file and connect to it
newPref = Preferences(filename="preferences_test.py")
# let's print 'my path' preference
print newPref.get('my path')
Run Code Online (Sandbox Code Playgroud)
最简单的使用方法之一是使用json模块。保存文件,config.json详细信息如下所示。
将数据保存在json文件中:
{
"john" : {
"number" : "948075049" ,
"password":"thisisit"
}
}
Run Code Online (Sandbox Code Playgroud)
从 json 文件中读取:
import json
#open the config.json file
with open('config.json') as f:
mydata = json.load(f) ;
#Now mydata is a python dictionary
print("username is " , mydata.get('john').get('number') , " password is " , mydata.get('john').get('password')) ;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31401 次 |
| 最近记录: |