def calc_check_digit(number): """Calculate the check digit. The number passed should not have the check digit included.""" number = to_unicode(number) number = (' ' + number)[-12:] check = sum(_alphabet.index(n) * (13 - i) for i, n in enumerate(number)) return _alphabet[(11 - check) % 11]
def validate(number): """Check if the number is a valid Cadastral Reference. This checks the length, formatting and check digits.""" number = compact(number) n = to_unicode(number) if not all(c in alphabet for c in n): raise InvalidFormat() if len(n) != 20: raise InvalidLength() if calc_check_digits(n) != n[18:]: raise InvalidChecksum() return number
def compact(number): """Convert the number to the minimal representation. This strips the number of any valid separators and removes surrounding whitespace.""" number = clean(number, ' ').upper().strip() for prefix in ('УНП', u'УНП', 'UNP', u'UNP'): if type(number) == type(prefix) and number.startswith(prefix): number = number[len(prefix):] # Replace Cyrillic letters with Latin letters cleaned = ''.join(_cyrillic_to_latin.get(x, x) for x in to_unicode(number)) if type(cleaned) != type(number): # pragma: no cover (Python2 only) cleaned = cleaned.encode('utf-8') return cleaned
def validate(number, validate_check_digits=False): """Check if the number is a valid RFC.""" number = compact(number) n = to_unicode(number) if len(n) in (10, 13): # number assigned to person if not re.match(u'^[A-Z&Ñ]{4}[0-9]{6}[0-9A-Z]{0,3}$', n): raise InvalidFormat() if n[:4] in _name_blacklist: raise InvalidComponent() _get_date(n[4:10]) elif len(n) == 12: # number assigned to company if not re.match(u'^[A-Z&Ñ]{3}[0-9]{6}[0-9A-Z]{3}$', n): raise InvalidFormat() _get_date(n[3:9]) else: raise InvalidLength() if validate_check_digits and len(n) >= 12: if not re.match(u'^[1-9A-V][1-9A-Z][0-9A]$', n[-3:]): raise InvalidComponent() if n[-1] != calc_check_digit(n[:-1]): raise InvalidChecksum() return number
def calc_check_digits(number): """Calculate the check digits for the number.""" number = to_unicode(compact(number)) return (_check_digit(number[0:7] + number[14:18]) + _check_digit(number[7:14] + number[14:18]))
def _to_min(court): """Convert the court name for quick comparison without encoding issues.""" return ''.join(x for x in unicodedata.normalize('NFD', to_unicode(court).lower()) if x in 'abcdefghijklmnopqrstuvwxyz')