Python integer to read-only buffer

1.5k views Asked by At

I am using cdb for a constant database in python. I would like to associate integer id's with some strings, and I would like to avoid storing each of these integer id's as strings, and instead store them as an integer. cdb though is looking for either a string or a read only buffer. Is there a way that I can store these keys as integers instead of strings?

For example:

cdb = cdb.cdbmake("test.cdb","test.cdb.tmp")
key = 5
value = "some test string"

#this throws an error
maker.add(key,value)
#TypeError: add() argument 1 must be string or read-only buffer, not int

#this would work, but seems inefficient
maker.add(str(key),value)
1

There are 1 answers

0
Nick Craig-Wood On BEST ANSWER

According to the cdb website the database only takes strings as keys

A cdb is an associative array: it maps strings (keys) to strings (data).

So you will have to convert the integers to strings first. I suggest you wrap the str in a utility function and forget about the overhead.