Store simple user settings in Python

I am programming a website in which users will have a number of settings, such as their choice of colour scheme, etc. I'm happy to store these as plain text files, and security is not an issue.

The way I currently see it is: there is a dictionary, where all the keys are users and the values are dictionaries with the users' settings in them.

For example, userdb["bob"]["colour_scheme"] would have the value "blue".

What is the best way to store it on file? Pickling the dictionary?

Are there better ways of doing what I am trying to do?


Asked by: Audrey427 | Posted: 05-10-2021






Answer 1

I would use the ConfigParser module, which produces some pretty readable and user-editable output for your example:

[bob]
colour_scheme: blue
british: yes
[joe]
color_scheme: that's 'color', silly!
british: no

The following code would produce the config file above, and then print it out:

import sys
from ConfigParser import *

c = ConfigParser()

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

c.add_section("joe")
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

Note that ConfigParser only supports strings, so you'll have to convert as I have above for the Booleans. See effbot for a good run-down of the basics.

Answered by: Vanessa814 | Posted: 06-11-2021



Answer 2

Using cPickle on the dictionary would be my choice. Dictionaries are a natural fit for these kind of data, so given your requirements I see no reason not to use them. That, unless you are thinking about reading them from non-python applications, in which case you'd have to use a language neutral text format. And even here you could get away with the pickle plus an export tool.

Answered by: Owen566 | Posted: 06-11-2021



Answer 3

I don't tackle the question which one is best. If you want to handle text-files, I'd consider ConfigParser -module. Another you could give a try would be simplejson or yaml. You could also consider a real db table.

For instance, you could have a table called userattrs, with three columns:

  • Int user_id
  • String attribute_name
  • String attribute_value

If there's only few, you could store them into cookies for quick retrieval.

Answered by: Patrick507 | Posted: 06-11-2021



Answer 4

Here's the simplest way. Use simple variables and import the settings file.

Call the file userprefs.py

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

In your application, you need to be sure that you can import this file. You have many choices.

  1. Using PYTHONPATH. Require PYTHONPATH be set to include the directory with the preferences files.

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

    b. An environment variable to name the file.

  2. Extending sys.path to include the user's home directory

Example

import sys
import os
sys.path.insert(0,os.path.expanduser("~"))
import userprefs 
print userprefs.color

Answered by: Melissa721 | Posted: 06-11-2021



Answer 5

For a database-driven website, of course, your best option is a db table. I'm assuming that you are not doing the database thing.

If you don't care about human-readable formats, then pickle is a simple and straightforward way to go. I've also heard good reports about simplejson.

If human readability is important, two simple options present themselves:

Module: Just use a module. If all you need are a few globals and nothing fancy, then this is the way to go. If you really got desperate, you could define classes and class variables to emulate sections. The downside here: if the file will be hand-edited by a user, errors could be hard to catch and debug.

INI format: I've been using ConfigObj for this, with quite a bit of success. ConfigObj is essentially a replacement for ConfigParser, with support for nested sections and much more. Optionally, you can define expected types or values for a file and validate it, providing a safety net (and important error feedback) for users/administrators.

Answered by: Emily662 | Posted: 06-11-2021



Answer 6

I would use shelve or an sqlite database if I would have to store these setting on the file system. Although, since you are building a website you probably use some kind of database so why not just use that?

Answered by: Dominik671 | Posted: 06-11-2021



Answer 7

The built-in sqlite3 module would probably be far simpler than most alternatives, and gets you ready to update to a full RDBMS should you ever want or need to.

Answered by: Catherine140 | Posted: 06-11-2021



Answer 8

If human readablity of configfiles matters an alternative might be the ConfigParser module which allows you to read and write .ini like files. But then you are restricted to one nesting level.

Answered by: Tara421 | Posted: 06-11-2021



Answer 9

If you have a database, I might suggest storing the settings in the database. However, it sounds like ordinary files might suit your environment better.

You probably don't want to store all the users settings in the same file, because you might run into trouble with concurrent access to that one file. If you stored each user's settings as a dictionary in their own pickled file, then they would be able to act independently.

Pickling is a reasonable way to store such data, but unfortunately the pickle data format is notoriously not-human-readable. You might be better off storing it as repr(dictionary) which will be a more readable format. To reload the user settings, use eval(open("file").read()) or something like that.

Answered by: Alford279 | Posted: 06-11-2021



Answer 10

Is there are particular reason you're not using the database for this? it seems the normal and natural thing to do - or store a pickle of the settings in the db keyed on user id or something.

You haven't described the usage patterns of the website, but just thinking of a general website - but I would think that keeping the settings in a database would cause much less disk I/O than using files.

OTOH, for settings that might be used by client-side code, storing them as javascript in a static file that can be cached would be handy - at the expense of having multiple places you might have settings. (I'd probably store those settings in the db, and rebuild the static files as necessary)

Answered by: Ned541 | Posted: 06-11-2021



Answer 11

I agree with the reply about using Pickled Dictionary. Very simple and effective for storing simple data in a Dictionary structure.

Answered by: Patrick850 | Posted: 06-11-2021



Answer 12

If you don't care about being able to edit the file yourself, and want a quick way to persist python objects, go with pickle. If you do want the file to be readable by a human, or readable by some other app, use ConfigParser. If you need anything more complex, go with some sort of database, be it relational (sqlite), or object-oriented (axiom, zodb).

Answered by: Nicole825 | Posted: 06-11-2021



Similar questions

python - What is the best way to store database settings with Django?

I'm attempting to write a browser game using Django but I'm getting a bit stuck on how to store the settings for the game. For example, the game is tick based and I want to store the current tick. I have decided that I want only one game per database to avoid problems with the built-in user authorisation system (e.g. I don't want to say username X is unavailable because it is already used in a different game). As far as I ...


python - Django project using wrong (old) database settings

recently I started a small Django project that I developed on a local machine using a SQLite3 database and the integrated development server. I now copied the whole project to a server running Debian. Everything worked well as long as I kept using the SQLite3 database. Now I wanted to switch to a local MySQL database, so I changed the settings.py file in my project's root folder, created the database and added a us...


python - About Database Settings in Django Project

I want to set database setting in django project. Which settings.py I should use. I found many settings.py file. I have setup devstack where many folders are there like horizon, cinder, nova etc. I found settings.py in horizon folder. and from when I setup Django I found in /usr/local/lib/python2.7/dist-packages/django/conf/project_template/project_name folder. Please make ...


python - django - MySQL strict mode with database url in settings

I'm using a database URL string in my settings like: DATABASES = { 'default': "mysql://root:@localhost:3306/mydb" } When I migrate I get this warning: MySQL Strict Mode is not set for database connection 'default' Now my question: How can I combine the two things? I cannot use the "regular" way to set the database settings with a dictio...


python - Single model dynamic database settings in Django

For example assume that I have 100 clients who uses WordPress and I have to write a service in Django which should return list of posts from WordPress's MySQL DB. The problem is 100 clients are having different database connection settings. I know that I can use DatabaseRouter to switch databases which are already loaded in settings. But I don't know how to ma...


python - Django ignores test database settings

I have an app deployed on pythonanywhere which runs fine. Problem is that when I want to run test django, my test database settings is completely ignored. Each time I run test I get the following message.though. Creating test database for alias 'default'... Got an error creating the test database: (1044, "Access denied fo...


database - Problem opening berkeley db in python

I have problems opening a berkeley db in python using bdtables. As bdtables is used by the library I am using to access the database, I need it to work. The problem seems to be that the db environment I am trying to open (I got a copy of the database to open), is version 4.4 while libdb is version 4.6. I get the following error using bsddb.dbtables.bsdTableDB([dbname],[folder]): (-30972, "DB_VERSION...


python - Sometimes can't delete an Oracle database row using Django

I have a unit test which contains the following line of code Site.objects.get(name="UnitTest").delete() and this has worked just fine until now. However, that statement is currently hanging. It'll sit there forever trying to execute the delete. If I just say print Site.objects.get(name="UnitTest") then it works, so I know that it can retrieve the site. ...


python - How do I test a django database schema?

I want to write tests that can show whether or not the database is in sync with my models.py file. Actually I have already written them, only to find out that django creates a new database each time the tests are run based on the models.py file. Is there any way I can make the models.py test use the existing database schema? The one that's in mysql/postgresql, and not the one that's in /myapp/models.py ?


python - Using user input to find information in a Mysql database

I need to design a program using python that will ask the user for a barcode. Then, using this barcode, it will search a mysql to find its corresponding product. I am a bit stuck on how to get started. Does anyone have any tips for me?


python - Given an rpm package name, query the yum database for updates

I was imagining a 3-line Python script to do this but the yum Python API is impenetrable. Is this even possible? Is writing a wrapper for 'yum list package-name' the only way to do this?


python - Import XML into SQL database

I'm working with a 20 gig XML file that I would like to import into a SQL database (preferably MySQL, since that is what I am familiar with). This seems like it would be a common task, but after Googling around a bit I haven't been able to figure out how to do it. What is the best way to do this? I know this ability is built into MySQL 6.0, but that is not an option right now because it is an alpha development rel...


python - Using 'old' database with django

I'm using a hand built (Postgres) database with Django. With "inspectdb" I was able to automatically create a model for it. The problem is that some tables have multiple primary keys (for many-to-many relations) and they are not accessible via Django. What's the best way to access these tables?


python - How do I notify a process of an SQLite database change done in a different process?

Let's say I have two or more processes dealing with an SQLite database - a "player" process and many "editor" processes. The "player" process reads the database and updates a view - in my case it would be a waveform being mixed to the soundcard depending on events stored in the database. An "editor" process is any editor for that database: it changes the database constantly. Now I want the player t...


database - python orm

This is a newbie theory question - I'm just starting to use Python and looking into Django and orm. Question: If I develop my objects and through additional development modify the base object structures, inheritance, etc. - would Django's ORM solution modify the database automatically OR do I need to perform a conversion (if the app is live)? So, I start with a basic Phone app Object person: name, address, city, s...


python - Large Sqlite database search

How is it possible to implement an efficient large Sqlite db search (more than 90000 entries)? I'm using Python and SQLObject ORM: import re ... def search1(): cr = re.compile(ur'foo') for item in Item.select(): if cr.search(item.name) or cr.search(item.skim): print item.name This function runs in more than 30 seconds. How sh...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top