vgrabovets
2/27/2017 - 8:29 AM

postcode_match

postcode_match

def postcode_match(postal_code, text):
    codes = re.compile(r'(\b\d{4}\b)')
    codes_set = set([re.sub(r'\s', '', code) for code in set(codes.findall(text))]) # noqa
    postcode_found = int(postal_code in codes_set)
    other_postcode = 0
    if postcode_found == 0:
        for code in codes_set:
            if code[:2] == postal_code [:2]:
                other_postcode = 1
                break
    return (postcode_found, other_postcode)