What Is The Preferred Method For Storing Application Persistent Data, A Flat File Or A Database?
What is the preferred way to store application-specific parameters (persistent data) for my Python program? I'm creating a Python program, which needs to store some parameters: 'p
Solution 1:
SQLite. Very easy to setup, and you gain a number of builtin db functions. You also won't have to handle file read/writes and parsing.
Solution 2:
pickle
it:
import pickle
options = {
'project_name': 'foo',
'start_year': 2000
}
with open('config.pickle', 'wb') as config:
pickle.dump(options, config)
The pickle
module lets you dump most Python objects into a file and read them back again.
Solution 3:
You can use shelve library. From shelve documentation:
A "shelf" is a persistent, dictionary-like object. The difference with dbm databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects -- anything that the "pickle" module can handle
import shelve
d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
d[key] = data # store data at key (overwrites old data if# using an existing key)
data = d[key] # retrieve a COPY of the data at key (raise# KeyError if no such key) -- NOTE that this# access returns a *copy* of the entry!
del d[key] # delete data stored at key (raises KeyError# if no such key)
flag = d.has_key(key) # true if the key exists; same as "key in d"
list = d.keys() # a list of all existing keys (slow!)
d.close()
Solution 4:
if scheme is fixed, sqldb is best choise, like sqlite3, plus memcached as cache. if relationship change often, i think flexible data may be stored in files(hash indexed).
Post a Comment for "What Is The Preferred Method For Storing Application Persistent Data, A Flat File Or A Database?"