开发者

Python integer to read-only buffer

开发者 https://www.devze.com 2022-12-12 18:24 出处:网络
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, a

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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号