Complex Nested Dictionaries

Russell E. Owen no at spam.invalid
Mon Feb 23 18:28:26 CET 2004


In article <4036a303 at news.bmi.net>, "T. Earle" <tnospamwade at bmi.net> 
wrote:

>Russell,
>
>> If you really only want to look up data by headline, then a dictionary
>> of dictionaries or nested lists or some other kind of collection is easy
>> and should suffice. For instance:
>> warndict["High Wind Warning"] = (
>>     (time1, {
>>         state1: (zone1, zone2, zone3),
>>         state2: (zone1, zone3),
>>     }),
>>     (time2, {...}),
>> )
>
>This definitely seems to be the structure I've been looking for or at least
>have in mind.  Since I'm no expert, could offer some code examples on how to
>create this structure on the fly?
>
>> However, I suspect you will also want to be able to locate data by
>> state, time or zone. If that is true, I really think you should consider
>> storing the data in a relational database. It sounds like a perfect
>> match to your problem. Python has some nice interfaces to various
>> databases (including PostgreSQL and MySQL).
>
>My first inclination was to go with a database; however, I thought about it
>and concluded there may be too much variability each time the program is
>executed.  For example, there will be times when there are no headlines;
>other times, there will be numerous headlines.  Because of this variability,
>the database would have to be created from scratch each time the program is
>ran.  As a result, would a database still be the right choice?
>
>I really appreciate your help and suggestions

Regarding a database: if you are mainly interested in fairly current 
events (rather than being able to go back and search for old events) and 
you don't have a huge # of events, then a database does seem "overkill". 

However, if you have a lot of events or want to do a lot of searching, 
it may be worth keeping a database around. If you use a database, I 
recommend creating only one of them. Just add new events, and 
occasionally purge old data if you don't care about it anymore.

Here is some sample code (untested) to create the structure shown above. 
I assume a simple (for me) structure for the input data; modify 
addHealine accordingly if your data needs more massaging first.

This code exposes the internal data, because the class is itself the 
dictionary of data. Whether or not this is a good idea depends on how 
you want to search for data. If the built in dict methods are of 
interest, then you are all set. If not, I would make HeadDict *contain* 
a dict instead of *being* a dict, then write your own methods to 
retrieve data.

- Download the RO package from http://astro.washington.edu/owen and install it in site-packages
or anywhere on your PythonPath. RO includes RO.Alg.ListDict, which 
supports a dictionary whose values are a list and for which the 
expression md[key] = value appends "value" to the list associated with 
"key", creating a new list if "key" doesn't already exist.

import RO.Alg

class HeadDict(RO.Alg.ListDict):
    def addHeadline(self, headline, time, stateZoneList)
        """Add a headline for a given time. stateZoneList is of the form:
        ((state1, zones_for_state1), (state2, zones_for_state2), ...)
        """
        stateZoneDict = dict(stateZoneList)
        self[headline] = (time, stateZoneDict)

warndict = HeadDict()
warnDict.addHeadline("High Wind Warning", time1, stateZoneList1)
warnDict.addHeadline("High Wind Warning", time2, stateZoneList2)

-- Russell



More information about the Python-list mailing list