python: lenient version of iterator.groupby that skips the records on exception -


i using iterator.groupby group similar entries in iterator based on attribute value this:

 employment = dict(itertools.groupby(xtable_iterator), operator.attrgetter('key_to_ytable')))   

key_to_ytable attribute can throw exception. so, whole dict constructor fails. skip entries 'key_to_ytable' attribute access throws exception , process remaining entries using groupby.

what alternatives?
1. inherit groupby , override functions or write custom groupby
2. use custom attrgetter returns none on exception , filter out nones
3. other solution?

some background: have library encapsulates database table iterator of record object. attributes of record object columns. in case of foreign key, library looks corresponding table , fetches value attribute value. unfortunately, tables not guaranteed in sync. so, foreign key may refer non-existent record, in case, library throws exception.

i vote custom attrgetter in situation, like:

employment = dict(itertools.groupby(xtable_iterator),                   lambda x: getattr(x, 'key_to_ytable', none)) employment.pop(none) 

this simpler , faster doing filter or anything, know there 1 none there.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -