Skip to main content

Module to validate, format, clean and get verification digit for Chileans RUT numbers

Project description

Chilean RUT | GitHub license

Python module to validate, format, clean and get verification digit for Chileans RUT/RUN numbers

Introduction

What is a RUN/RUT?

It's a unique identification number given to every natural person (RUN) or juridical person (RUT).

  • RUN (Rol Único Nacional)
  • RUT (Rol Único Tributario)

For individuals, the RUN number is the same as the RUT and also in Chile the word "RUT" is commonly used to refer to the RUN number, they are not synonymous but they are used as if they were.

RUT / RUN numbers have seven or eight digits, plus a verification digit, and are generally written in this format: xx.xxx.xxx-z. Where Z can be a digit or the letter K, and it’s determined by "Module 11 algorithm" ensuring that typing mistakes when entering a RUT or RUN number will result in an invalid number.

Installation

$ pip install chilean_rut

Usage

is_valid

Checks if a Chilean RUT number is valid or not.

Argument Type Default Description
rut string None A Chilean RUT Number
import chilean_rut

# Valid RUT numbers (correct verification digit)
chilean_rut.is_valid('17.317.684-8') # True
chilean_rut.is_valid('17317684-8)    # True
chilean_rut.is_valid('173176848)     # True
# Wrong RUT numbers (bad verification digit)
chilean_rut.is_valid('17.317.684-2)  # False
chilean_rut.is_valid('17317684-2)    # False
chilean_rut.is_valid('173176842)     # False

get_verification_digit

Calculates the verification number or letter.

Argument Type Default Description
rut string None A Chilean RUT number without verification digit
import chilean_rut

chilean_rut.get_verification_digit('22174688')     # 0
chilean_rut.get_verification_digit('22191269)      # 1
chilean_rut.get_verification_digit('16615805)      # 2
chilean_rut.get_verification_digit('14505346)      # 3
chilean_rut.get_verification_digit('6088258)       # 4
chilean_rut.get_verification_digit('5391862)       # k
chilean_rut.get_verification_digit('12312-K')      # ValueError
chilean_rut.get_verification_digit('12.312-K')     # ValueError
chilean_rut.get_verification_digit('17317684-8')   # ValueError
chilean_rut.get_verification_digit('12.450.547-k') # ValueError

format_rut

Formats Chilean RUT number adding dots as thousands separator and a dash before verification digit.

Argument Type Default Description
rut string None A Chilean RUT Number
validate_rut bool True Validate RUT number before format
import chilean_rut

# Valid RUT numbers (correct verification digit)
chilean_rut.format_rut('17317684-8', True)   # 17.317.684-8
chilean_rut.format_rut('12.450.547-k, True)  # 12.450.547-k
chilean_rut.format_rut('61410767', True)      # 6.141.076-7
# Wrong RUT numbers (bad verification digit)
chilean_rut.format_rut('17317684-1', True)   # ValueError
chilean_rut.format_rut('12.450.547-2', True)  # ValueError
chilean_rut.format_rut('61410763', True)      # ValueError

# Valid RUT numbers (correct verification digit)
chilean_rut.format_rut('17317684-8', False)   # 17.317.684-8
chilean_rut.format_rut('12.450.547-k, False)  # 12.450.547-k
chilean_rut.format_rut('61410767', False)      # 6.141.076-7
# Wrong RUT numbers (bad verification digit)
chilean_rut.format_rut('17317684-1', False)   # 17.317.684-1
chilean_rut.format_rut('12.450.547-2', False)  # 12.450.547-2
chilean_rut.format_rut('61410763', False)      # 6.141.076-3

# Invalid RUT format
chilean_rut.format_rut('123.111.111-2', True)   # ValueError
chilean_rut.format_rut('123.111.111-2', False) # ValueError

clean_rut

Cleans Chilean RUT number removing dots (thousands separador) and dash before verification digit.

Argument Type Default Description
rut string None A Chilean RUT Number
validate_rut bool True Validates RUT number before clean
import chilean_rut

# Valid RUT numbers (correct verification digit)
chilean_rut.clean_rut('17317684-8', True)   # 173176848
chilean_rut.clean_rut('12.450.547-k, True)  # 12450547k
chilean_rut.clean_rut('61410767, True)      # 61410767
# Wrong RUT numbers (bad verification digit)
chilean_rut.clean_rut('17317684-1', True)   # ValueError
chilean_rut.clean_rut('12.450.547-2', True)  # ValueError
chilean_rut.clean_rut('61410763', True)      # ValueError

# Valid RUT numbers (correct verification digit)
chilean_rut.clean_rut('17317684-8', False)   # 173176848
chilean_rut.clean_rut('12.450.547-k, False)  # 12450547k
chilean_rut.clean_rut('61410767, False)      # 61410767
# Wrong RUT numbers (bad verification digit)
chilean_rut.clean_rut('17317684-1', False)   # 173176841
chilean_rut.clean_rut('12.450.547-2', False)  # 124505472
chilean_rut.clean_rut('61410763', False)      # 61410763

# Invalid RUT format
chilean_rut.format_rut('123.111.111-2,True)   # ValueError
chilean_rut.format_rut('123.111.111-2, False) # ValueError

Test

Running tests:

$ pytest

Checking the package installs correctly with different Python versions and interpreters.

Tested with python3.6, python3.7, python3.8, python3.9 and python3.10 versions:

$ tox

Contributing

Contributions are welcome - submit an issue/pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chilean-rut-0.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chilean_rut-0.1.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file chilean-rut-0.1.1.tar.gz.

File metadata

  • Download URL: chilean-rut-0.1.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.0 Darwin/20.6.0

File hashes

Hashes for chilean-rut-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9b8beb9c148b9d21c4b726587a04e5566a37330c8d2179eec30d6ca52d08af7b
MD5 2164531d54d6f9f5deb89cbff5ccb670
BLAKE2b-256 e31e62b8eb798ac9cd1ad15f42321f7a6fe48c5ad6a5d008136e3643123ffc62

See more details on using hashes here.

File details

Details for the file chilean_rut-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: chilean_rut-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.0 Darwin/20.6.0

File hashes

Hashes for chilean_rut-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2ff072d0d80640e250410d6b0711ab72d093634d38634ad17464f7ecdf8b006f
MD5 7818d93ca6fabd776f3ffea83a968589
BLAKE2b-256 629d3b815310310b7618d670f6e00610ac49c525989e4dfb490220e615643131

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page