def normalize_district(header, office, row):
    """
    Example of what we had before:

    'district': '{0} {1}'.format(
        self.district_offices[normalize_races(sh_val)],
        "".join(map(str, [int(s) for s in sh_val.strip() if s.isdigit()][:2])
        ))})

    normalize_district now provides a more standardized and clean API than
    was the case with the mess before.
    """

    norm_office = normalize_races(office)
    dist_str = "".join(
        map(str, [int(s) for s in office.strip() if s.isdigit()][:2]))

    bth_regex = re.compile(r'((leg|con).*dis.*)', re.IGNORECASE)
    leg_regex = re.compile(r'leg.*dis.*', re.IGNORECASE)
    con_regex = re.compile(r'con.*dis.*', re.IGNORECASE)

    if not row:
        row = {}

    try:
        row[filter(lambda x: bth_regex.search(x), header)[0]]
        if norm_office is 'U.S. Representative':
            dist = row[filter(lambda x: leg_regex.search(x), header)[0]]
            return dist
        if norm_office in ('State Representative', 'State Senate'):
            dist = row[filter(lambda x: con_regex.search(x), header)[0]]
            return dist
    except __HOLE__:
        if dist_str is "":
            return None
        if int(dist_str) > 49:
            dist_str = dist_str[:1]
        return dist_str
dataset/ETHPy150Open openelections/openelections-core/openelex/us/wa/load.py/normalize_district