[Tutor] Conditional attribute access / key access

Knacktus knacktus at googlemail.com
Mon Aug 30 16:44:08 CEST 2010


Hey everyone,

I have a huge number of data items coming from a database. So far 
there're no restrictions about how to model the items. They can be 
dicts, objects of a custom class (preferable with __slots__) or namedTuple.

Those items have references to each other using ids. Fresh from the 
database the items look like this (using dicts as examples):

item_1 = {"id": 1, "name": "root", "children_ids": [2, 3]}
item_2 = {"id": 2, "name": "child_1", "children_ids": [4]}
item_3 = {"id": 3, "name": "child_2", "children_ids": [6, 7, 8]}

Now I'd like to resolve the references on demand. For that purpose I 
think about adding another entry in my dicts ("children_obj_refs"):

item_1 = {"id" = 1, "nam"e = "root", "children_ids" = [2, 3], 
"children_obj_refs" = [item_2, item_3]}

To achieve that substitution dynamically on demand I could use a function:

def get_children(item):
     try:
         return item["children_obj_refs"]
     except AttributeError:
         # pseudocode for retrieving items from db based on ids
         fresh_items_from_db = get_items_from_db(item["children_ids"])
         # Now create new dict entry for future usage
         item["children_obj_refs"] = fresh_items_from_db

However, I dislike to have to call a function all the time. I'd rather 
get this done under the hood when accessing the dict with 
item["children_obj_refs"]. So I've looked into subclassing a dict and 
overwriting the __getitem__() method like this:

class TestSubDict(dict):

     def __getitem__(self, key):
         try:
             return self[key]
         except KeyError:
             # load item from db and so on ...

But I run into recursion limit problems.

Any recommendations about that? (I'm not restricted to dicts, custom 
classes are OK to, but with __slots__ to limit memory consumption.)

Many thanks in advance and cheers,

Jan



More information about the Tutor mailing list