python: lenient version of iterator.groupby that skips the records on exception
I am using a iterator.groupby to group similar entries in an iterator together based on an attribute value like this:
employment = dict(itertools.groupby(xtable_iterator), operator.attrgetter('key_to_ytable')))
key_to_ytable is an attribute that can throw an exception. So, my whole dict constructor fails. I would like to just skip the entries for which the 'key_to_ytable' attribute access throws exception and process the remaining entries using groupby.
What are my alternatives?
1. Inherit from groupby and override functions OR write my custom groupby 2. Use a custom attrgetter that returns None on exception and then filter out the Nones 3. Any other solution?Some background: I have a library that开发者_StackOverflow社区 encapsulates the database table as an iterator of record object. The attributes of this record object are the columns. In case of foreign key, the library looks up the corresponding table and fetches the value as an attribute value. Unfortunately, the tables are not guaranteed to be in sync. So, a foreign key may refer to an non-existent record, in which case, the library throws an exception.
I vote for the custom attrgetter in this situation, like:
employment = dict(itertools.groupby(xtable_iterator),
lambda x: getattr(x, 'key_to_ytable', None))
employment.pop(None)
This is simpler and probably faster than doing a filter or anything, you know there will only be one None there.
精彩评论