
__all__ = ['juvenile_filter', 'filter', 'sentence_match_arrest_filter']

def juvenile_filter(arrest_records, sentence_records, **kw):
    _tmp_arrest = arrest_records.copy()
    _tmp_sentence = sentence_records.copy()
    arrest_del_rows, sentence_del_rows = [], []
    
    del_cycle = dict((row['cycnum'], row['juv_flag']) 
                     for row in arrest_records.itervalues())
    
    for row in _tmp_arrest.itervalues():
        #handle MN exception
        if row['rapstate'] == 'mn':
            if row['juv_flag']:
                del arrest_records[row['id']]
                arrest_del_rows.append(row)
        else:
            if row['juv_flag']==True:
                del arrest_records[row['id']]
                arrest_del_rows.append(row)
    
    for row in _tmp_sentence.itervalues():
        #handle MN exception
        if row['rapstate'] == 'mn':
            if row['juv_flag']:
                del sentence_records[row['id']]
                sentence_del_rows.append(row)
    
    return (arrest_del_rows, sentence_del_rows)

def filter(records, flags):
    _tmp = records.copy()
    del_rows = []
     
    for row in _tmp.itervalues():
        for flag in flags:
            if row.get(flag):
                del records[row['id']]
                del_rows.append(row)
    
    return del_rows
        
def sentence_match_arrest_filter(arrest_records, sentence_records, **kw):
    _arrest_set = set((row['casenum'], row['cycnum']) for row in arrest_records.itervalues())
    del_rows = []
    
    _tmp = sentence_records.copy()
    for row in _tmp.itervalues():
        if (row['casenum'], row['cycnum']) not in _arrest_set:
            row['arrest_match_flag'] = True
            del sentence_records[row['id']]
            del_rows.append(row)
    
    return del_rows
        
        
    
    
    