-
-
Save knxroot/d2ab0a247bf2f186fa3a to your computer and use it in GitHub Desktop.
Dígito verificador del RUT en Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding=utf-8 | |
# Idea of a Custom data types for validate the Chilean RUT in Flask-RESTful | |
# by @lacosox | |
from itertools import cycle | |
#rut='XXXXXXXX-X' | |
def chilean_rut(rut_text): | |
rut_text.upper().strip().replace(".", "").replace("K", "10") | |
(rut, dv)=rut_text.split('-') | |
if rut.isdigit(): | |
reversed_digits = map(int, reversed(str(rut))) | |
factors = cycle(range(2, 8)) | |
s = sum(d * f for d, f in zip(reversed_digits, factors)) | |
if dv <> str((-s) % 11): | |
raise ValueError("Invalid RUT, DV incorrect.") | |
else: | |
raise ValueError("Invalid RUT, only numbers is valid.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment