apllom
apllom

Reputation: 121

What to do if collections.defaultdict is not available?

Solaris python 2.4.3:

from collections import defaultdict 

does not exist..

Please advise what could be an alternative to use multi-level dictionaries:

dictOut['1']['exec'] = 'shell1.sh'
dictOut['1']['onfailure'] = 'continue'
...
dictOut['2']['exec'] = 'shell2.sh'
dictOut['2']['onfailure'] = stop'

many thanks applom

Upvotes: 2

Views: 4473

Answers (4)

John Machin
John Machin

Reputation: 82924

Answered with looks-like-it-works code within the last 24 hours (found by searching for "defaultdict", choose "newest" or "active" order)

Upvotes: 2

Tony Veijalainen
Tony Veijalainen

Reputation: 5555

I just wonder why not to use single level dict with tuple as hash key?

Upvotes: 0

Michael Mior
Michael Mior

Reputation: 28753

As an alternative to setdefault, if you want extra level of dictionary goodness, try

class MultiDict(dict):
    def __getitem__(self, item):
        if item not in self.iterkeys():
            self[item] = MultiDict()

        return super(MultiDict, self).__getitem__(item)

Upvotes: 2

Amber
Amber

Reputation: 526543

setdefault?

dictOut.setdefault('1', {})['exec'] = 'shell1.sh'

Upvotes: 2

Related Questions