def block_by_startswith(name, numchars, state=None, office=None, cycle=None):
    namestart = name[:numchars]
    if debug:
        log.debug("block_by_startswith = %s state=%s office=%s cycle=%s" % (name, state,office, cycle))
    print("block_by_startswith = %s state=%s office=%s cycle=%s" % (name, state,office, cycle))
        
    matches = Candidate.objects.filter(cand_name__istartswith=namestart)
    president_flag = False
    if office:
        if office.upper()[:1] == 'H':
            matches = matches.filter(cand_office='H')
        elif office.upper()[:1] == 'S':
            matches = matches.filter(cand_office='S')
        elif office.upper()[:1] == 'P':
            matches = matches.filter(cand_office='P')
            president_flag = True        
        
    if state and not president_flag:
        state = state.strip().upper()
        matches = matches.filter(cand_office_st=state)
    
    # default to the current year; this will break for the house between jan. 1 and whenever folks are sworn in, usually jan. 3, I think... 
    if cycle and len(str(cycle)) > 3:
        matches = matches.filter(cycle=cycle)
    
    matches = matches.order_by('cand_name', 'cand_office', 'cand_office_st', 'cand_office_district', 'cand_id', 'cand_pty_affiliation')
    match_values = matches.values('cand_name', 'cand_office', 'cand_office_st', 'cand_office_district', 'cand_id', 'cand_pty_affiliation').distinct()

    #print "result is %s" % match_values
    return match_values
dataset/ETHPy150Open sunlightlabs/read_FEC/fecreader/reconciliation/fec_reconciler.py/block_by_startswith/original