tags:

views:

137

answers:

3

Hi,

I want to use a tuple (1,2,3) as a key using the shelve module in Python. I can do this with dictionaries:

d = {}
d[(1,2,3)] = 4

But if i try it with shelve:

s = shelve.open('myshelf')
s[(1,2,3)] = 4

I get: "TypeError: String or Integer object expected for key, tuple found"

Any suggestions?

+4  A: 

How about using the repr() of the tuple:

s[repr((1,2,3))] = 4
Ned Batchelder
+4  A: 

As per the docs,

the values (not the keys!) in a shelf can be essentially arbitrary Python objects

My emphasis: shelf keys must be strings, period. So, you need to turn your tuple into a str; depending on what you'll have in the tuple, repr, some separator.join, pickling, marshaling, etc, may be fruitfully employed for that purpose.

Alex Martelli
+1  A: 

Why not stick with dictionaries if you want to have arbitray keys ? The other option is to build a wrapper class around your tuple with a repr or str method to change it to a string. I am thinking of a library(natural response to shelves) - your tuple can be the elements in the Dewey decimal and the str creates a concatenated complete representation.

whatnick