Deeply nested dictionaries - should I look into a database or am I just doing it wrong?

Philip Semanchuk philip at semanchuk.com
Sun Jul 31 22:53:48 CEST 2011


On Jul 31, 2011, at 4:04 PM, Thorsten Kampe wrote:

> * Andrew Berg (Sun, 31 Jul 2011 13:36:43 -0500)
>> On 2011.07.31 02:41 AM, Thorsten Kampe wrote:
>>> Another approach would be named tuples instead of dictionaries or
>>> flat SQL tables.
>> What would the advantage of that be?
> 
> QueueItem.x264['avs']['filter']['fft3d']['ffte'] would be 
> QueueItem.x264.avs.filter.fft3d.ffte. I recently "migrated" from a 
> syntax of - example - datetuple[fieldpositions['tm_year'][0]] (where 
> fieldpositions was a dictionary containing a list) to 
> datetuple.tm_year_start which is much more readable.
> 
> The advantage of a SQL(ite) database would be simple flat tables but 
> accessing them would be more difficult.
> 
> Even a INI config file structure could match your problem.

INI files are OK for lightweight use, but I find them very fragile. Since there's no specification for them, libraries don't always agree on how to read them. For instance, some libraries treat # as the comment character, and others think it is ; and others accept both. There's no standard way to specify the encoding, and, as would be critical to the OP who is nesting dicts inside of dicts, not all INI file libraries accept nested sections.

To the OP -- if you're looking to write this to disk, I recommend XML or SQLite. 

JMHO,
Philip


More information about the Python-list mailing list