I am programming (in Python) an internet site that may have customers. These customers will have many configurations, for example their selection of colour plan, etc. I am pleased to store these as plain text files, and security isn't an problem.

Generate an income presently view it is: there is a dictionary, where all of the secrets are customers and also the values are dictionaries using the users' configurations inside them.

For instance, userdb["bob"]["colour_plan"] might have the worthiness "blue".

What's the easiest method to store it on file? Pickling the dictionary?

Exist possible ways to do what I am attempting to do, anyway?

I do not tackle the question which is better. If you wish to handle text-files, I'd consider ConfigParser -module. Another you can provide a try could be simplejson or yaml. You might think about a real db table.

For example, you might have a table known as userattrs, with three posts:

  • Int user_id
  • String attribute_title
  • String attribute_value

If there's only couple of, you can store them into snacks for convenient retrieval.

I'd make use of the ConfigParser module, which produces some pretty readable and user-editable output for the example:


colour_plan: blue

british: yes


color_plan: that's 'color', silly!

british: no

The next code would make the config file above, after which print it:

import sys
from ConfigParser import *

c = ConfigParser()

c.set("bob", "colour_scheme", "blue")
c.set("bob", "british", str(True))

c.set("joe", "color_scheme", "that's 'color', silly!")
c.set("joe", "british", str(False))

c.write(sys.stdout)  # this outputs the configuration to stdout
                     # you could put a file-handle here instead

for section in c.sections(): # this is how you read the options back in
    print section
    for option in c.options(section):
            print "\t", option, "=", c.get(section, option)

print c.get("bob", "british") # To access the "british" attribute for bob directly

Observe that ConfigParser only supports strings, so you will need to convert when i have above for that Booleans. See effbot for any good run-lower from the fundamentals.

Using cPickle around the dictionary could be my choice. Dictionaries really are a natural fit of these type of data, so given your needs I see pointless not for their services. That, unless of course you are looking at reading through them from non-python programs, by which situation you'd need to use a language neutral text format. As well as here you can get away using the pickle along with an export tool.

Here's the easiest way. Use simple variables and import the configurations file.

Call the file userprefs.py

# a user prefs file
color = 0x010203
font = "times new roman"
position = ( 12, 13 )
size = ( 640, 480 )

Inside your application, you'll need to be certain that you could import this file. You've many options.

  1. Using PYTHONPATH. Require PYTHONPATH be set to incorporate your directory using the preferences files.

    a. An explicit command-line parameter to title the file (not the very best, but simple)

    b. An atmosphere variable to title the file.

  2. Stretching sys.path to incorporate anyone's home directory


import sys
import os
import userprefs 
print userprefs.color

I'd use shelve or perhaps an sqlite database basically would need to store these setting around the file system. Although, as you are creating a website you most likely apply certain type of database so why wouldn't you only use that?