Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should python dictionaries be stored in pytables?

pytables doesn't natively support python dictionaries. The way I've approached it is to make a data structure of the form:

tables_dict = {
'key'         : tables.StringCol(itemsize=40),
'value'       : tables.Int32Col(),
}

(note that I ensure that the keys are <40 characters long) and then create a table using this structure:

file_handle.createTable('/', 'dictionary', tables_dict)

and then populate it with:

file_handle.dictionary.append(dictionary.items())

and retrieve data with:

dict(file_handle.dictionary.read())

This works ok, but reading the dictionary back in is extremely slow. I think the problem is that the read() function is causing the entire dictionary to be loaded into memory, which shouldn't really be necessary. Is there a better way to do this?

like image 824
tdc Avatar asked Jan 25 '12 12:01

tdc


1 Answers

You can ask PyTables to search inside the table, and also create an index on the key column to speed that up.

To create an index:

table.cols.key.createIndex()

To query the values where key equals the variable search_key:

[row['value'] for row in table.where('key == search_key')]

http://pytables.github.com/usersguide/optimization.html#searchoptim

like image 93
Janne Karila Avatar answered Oct 29 '22 09:10

Janne Karila