Has anyone already got a collection_class mod that uses defaultdict?

90 views
Skip to first unread message

Eric Ongerth

unread,
Mar 10, 2011, 4:02:09 PM3/10/11
to sqlalchemy
I make occasional use of alternate collection classes for my
sqlalchemy relationships. For example, column_mapped_collection. The
existing collection classes in sqlalchemy.orm.collections work really
well for me; I have a lot of relational data that very naturally
belongs in dicts rather than lists because it makes sense as one thing
indexed or mapped by another.

I find that I often need to add an item to one of these dict-mapped
collections and would rather not have to check whether the key already
exists in the dict. So I'm doing, essentially:
collection.setdefault(key, []).append(value).

As a matter of syntactic sugar (which alternative collection classes
already are, anyway) I would like to have collection classes backed by
dicts with the capability of python's defaultdict, so that I could
write more simply, and with equivalent results:
collection[key].append(value)

I understand how to create my own collection class which will behave
as desired. My question is, has anyone already got code for this that
I could borrow rather than reinvent the wheel? Thanks in advance. --
Eric

Eric Ongerth

unread,
Mar 10, 2011, 4:55:30 PM3/10/11
to sqlalchemy
Ach, I did it again... proceeded as if column_mapped_collection and
attribute_mapped_collection provide collection classes that are dicts
of lists. No, they're just dicts of scalar values!

Again and again I need dicts of lists. They seem to really fit the
data that I tend to work with; A's related to C's indexed by B's (the
B typically indicating the *type* of relationship between the A and
the C).

Eventually I need to work my own implementation of dict-of-lists and
dict-of-dicts collection classes up to full strength so I can share
them as a recipe or extension or something.

Eric Ongerth

unread,
Mar 10, 2011, 6:20:50 PM3/10/11
to sqlalchemy
So, jek, if you're listening, or anyone else -- is there an already
existing, working implementation of a Dict of Lists or Dict of Sets
collection class?

Michael Bayer

unread,
Mar 13, 2011, 1:26:53 AM3/13/11
to sqlal...@googlegroups.com

On Mar 10, 2011, at 6:20 PM, Eric Ongerth wrote:

> So, jek, if you're listening, or anyone else -- is there an already
> existing, working implementation of a Dict of Lists or Dict of Sets
> collection class?

the association_proxy is always there to flatten the "object" in the middle of two relationships typically along an association - so just A->relationship->dict of B->relationship->set of C, association proxy from A to B.cs.

> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>

Eric Ongerth

unread,
Mar 13, 2011, 4:26:27 AM3/13/11
to sqlalchemy
Thank you, Mike. Hmm... I tried exactly that technique, the last time
I found myself with a similar need, a year or two ago. I seem to
remember it only caused the problem to regress one step farther. But
I must have missed something and will probably give it another shot.

Any suggestions on how to replace the dict in one of these situations
with a defaultdict? Just override __setitem__ and __getitem__ ?

- Eric

Michael Bayer

unread,
Mar 13, 2011, 1:24:37 PM3/13/11
to sqlal...@googlegroups.com
its probably easier to use __missing__ - see attached.

dict_of_sets_with_default.py

Eric Ongerth

unread,
Mar 13, 2011, 7:38:01 PM3/13/11
to sqlalchemy
Cool! I was unaware of __missing__. Thanks again.


On Mar 13, 10:24 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
> its probably easier to use __missing__ - see attached.
>
>  dict_of_sets_with_default.py
> 1KViewDownload

Eric Ongerth

unread,
Mar 13, 2011, 7:50:33 PM3/13/11
to sqlalchemy
Ah, brilliant! So now I'm analyzing what prevented me from coming up
with that solution before.... it looks like my problem, or mental
block, was that I was trying to find a magic incantation to allow me
to get the whole dict-of-set behavior defined from the A class, rather
than allowing it to be staged across A and B where it naturally fits.
That is exactly where I should have seen that associationproxy would
handle the rest, but I must have been attempting to use
associationproxy in a different way. At some point I remember
succumbing to a mental storm of infinite recursion and had to just put
down the keyboard and back away slowly.

I'm excited to give this a test-drive.

On Mar 13, 10:24 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
> its probably easier to use __missing__ - see attached.
>
>  dict_of_sets_with_default.py
> 1KViewDownload
>
>
>
> On Mar 13, 2011, at 4:26 AM, Eric Ongerth wrote:
>
Reply all
Reply to author
Forward
0 new messages