to conserve memory & avoid redundant DB storage (yes possibly pre-optimizing), I'm using namedtuple instead of dictionary.
But I need to search the collection of recs and my dictionary approach is:
import operator
def query(D,key,val, keynotfound=None):
'''
D: a list of dictionaries (but I want it to be namedtuples)
key: the key to query
val: the value to search for
keynotfound: value if key is not found
Returns elements in D such that operator(D.get(key,None), val) is true
'''
op = operator.eq
def try_op(f,x,y):
try:
return f(x,y)
except Exception, exc:
return False
return (x for x in D if try_op(op, x.get(key,keynotfound),val))
not working on namedtuple Any tips on how to subclass namedtuple to make it searchable like a dict? And not every instance will contain the same key/fields as the query-key so I need to skip that row rather than throw a key/attr error..