def add(self, item, group_by=None):
        """General purpose class to group items by certain criteria."""
        key = None

        if not group_by:
            group_by = self.group_by

        if group_by:
            # if group_by is a function, use it with item as argument
            if hasattr(group_by, '__call__'):
                key = group_by(item)

            # if the item has attribute of group_by as string, use that as key
            elif isinstance(group_by, str) and hasattr(item, group_by):
                key = getattr(item, group_by)

            else:
                key = None
                # try to match str(item) with regular expression
                if isinstance(group_by, str):
                    match = re.search(group_by, str(item))
                    if match:
                        if len(match.groups()) > 0:
                            key = match.group(1)
                        else:
                            key = match.group()

        self.groups.setdefault(key, list()).append(item)
def add ( self , item , group_by = None ) : key = None if not group_by : group_by = self . group_by if group_by : if hasattr ( group_by , '__call__' ) : key = group_by ( item ) elif isinstance ( group_by , str ) and hasattr ( item , group_by ) : key = getattr ( item , group_by ) else : key = None if isinstance ( group_by , str ) : match = re . search ( group_by , str ( item ) ) if match : if len ( match . groups ( ) ) > 0 : key = match . group ( 1 ) else : key = match . group ( ) self . groups . setdefault ( key , list ( ) ) . append ( item )
General purpose class to group items by certain criteria.