Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating a defaultdict at init time

How can I get a callable factory for a defaultdict to allow populating it with a comprehension? I think it's probably not possible, but I can't think of a good reason why?

>>> def foo(*args):
...     # TODO
...
>>> from collections import defaultdict
>>> thing = foo(defaultdict, int)
>>> d = thing((i, i*i) for i in range(3))
>>> d[2]
# should return 4
>>> d[-1]
# should return 0
like image 517
wim Avatar asked Jan 14 '14 00:01

wim


2 Answers

Any arguments to defaultdict after the default_factory are treated just like arguments to dict:

>>> defaultdict(int, [(i, i*i) for i in range(5)])
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4, 3: 9, 4: 16})

Just pass the comprehension to defaultdict and let it do the work:

def defaultdict_factory_factory(default_factory):
    def defaultdict_factory(*args, **kwargs):
        return defaultdict(default_factory, *args, **kwargs)
    return defaultdict_factory

Or use functools.partial:

def defaultdict_factory_factory(default_factory):
    return partial(defaultdict, default_factory)
like image 119
user2357112 supports Monica Avatar answered Nov 02 '22 23:11

user2357112 supports Monica


Are you just looking for defaultdict.update?

>>> from collections import defaultdict
>>> thing = defaultdict(int)
>>> thing.update((i, i*i) for i in range(3))
>>> thing
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4})

You could put this into a function.

>>> def initdefaultdict(type_, *args, **kwargs):
...     d = defaultdict(type_)
...     d.update(*args, **kwargs)
...     return d
... 
>>> thing = initdefaultdict(int, ((i, i+10) for i in range(3)))
>>> thing
defaultdict(<type 'int'>, {0: 10, 1: 11, 2: 12})
>>> thing[3]
0

Or to satisfy your original requirements, return a function:

>>> def defaultdictinitfactory(type_): # this is your "foo"
...     def createupdate(*args, **kwargs):
...             d = defaultdict(type_)
...             d.update(*args, **kwargs)
...             return d
...     return createupdate
... 
>>> f = defaultdictinitfactory(int) # f is your "thing"
>>> d = f((i, i*i) for i in range(3))
>>> d
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4})
>>> 
like image 21
Peter Gibson Avatar answered Nov 03 '22 01:11

Peter Gibson