You are not logged in.

#1 2012-02-21 15:43:50

vansmith
Member
Registered: 2012-02-21
Posts: 2

Python - dbm & iteritems

Hey everyone,

So I've been writing a Python app that depends on dbm to store information. I intend to make it work across platforms so I'm using anydbm to let Python decide which dbm backend to use. This works really well in Windows, OS X and other Linux distributions but for some reason, I kept getting errors when trying to run the app in Arch.

In my app, I iterate through the items in the database using iteritems (excerpt follows):

for x, y in self.db.iteritems():
     titles.append(x)

Running that in Python (I'm using Python 2 by the way since the app uses wxPython) gives me an AttributeError for iteritems. Lo and behold, iteritems indeed doesn't exist as a method for the anydbm or gdbm modules (nor does items). Am I missing something really obvious here?

Thanks in advance for any help!

Offline

#2 2012-02-21 19:16:51

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: Python - dbm & iteritems

@vansmith:  "anydbm" uses "gdbm" on Arch, which by documentation doesn't support ".items()" or ".values()":

The gdbm module provides an interface to the GNU DBM library. gdbm objects behave like mappings (dictionaries), except that keys and values are always strings. Printing a gdbm object doesn’t print the keys and values, and the items() and values() methods are not supported.

Using "shelve" might probably help, but I'd use ordinary dictionaries with JSON as storage backends instead to avoid all the incompatibility issues of the various dbm interfaces and libraries.

Offline

#3 2012-02-22 20:37:58

vansmith
Member
Registered: 2012-02-21
Posts: 2

Re: Python - dbm & iteritems

Thanks for the response. Unfortunately, shelve causes problems in OS X and Windows (for whatever reason). I suppose I could go the JSON route - I might have to look into that. Thanks!

Offline

#4 2012-02-22 20:47:45

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,094

Re: Python - dbm & iteritems

Shelve is not portable. I would strongly suggest json/bison instead. (or some form of (no)sql, depending on the data.)

Last edited by Mr.Elendig (2012-02-22 20:48:58)


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#5 2012-02-23 06:54:52

keenerd
Package Maintainer (PM)
Registered: 2007-02-22
Posts: 647
Website

Re: Python - dbm & iteritems

Shebang problem.

Iteritems() was renamed to items() in python 3.  Everything is iterative everywhere by default.

Change your shebang to

#! /usr/bin/env python2

and it will run on Arch.

However it will not run on Debian, because they are just as stubborn as we are about python symlinks.

Offline

Board footer

Powered by FluxBB