Skip to content

Instantly share code, notes, and snippets.

@knxroot
Forked from rbonvall/rut.py
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knxroot/d2ab0a247bf2f186fa3a to your computer and use it in GitHub Desktop.
Save knxroot/d2ab0a247bf2f186fa3a to your computer and use it in GitHub Desktop.
Dígito verificador del RUT en Python
# 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