Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-marquez committed Jun 29, 2023
1 parent 3632318 commit da9b417
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 27 deletions.
128 changes: 128 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[tool.poetry]
name = "rut.py"
version = "1.0.0"
version = "1.0.1"
description = "Librería para el manejo de RUTs chilenos"
authors = ["Valentin Marquez <[email protected]>"]

Expand Down
2 changes: 1 addition & 1 deletion rutpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .core.rut import clean, validate, format, get_check_digit, generate
from .core.rut import clean, format_rut, generate, get_check_digit, validate
65 changes: 42 additions & 23 deletions rutpy/core/rut.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import re
import random
"""
rut.py
This module provides functions for handling Chilean RUTs (Rol Único Tributario).
It includes functions to clean, validate, get the check digit, format, and generate RUTs.
Authors:
- Valentin Marquez
from typing import Tuple
License:
MIT License
Functions:
- clean(rut: str) -> str
- validate(rut: str) -> bool
- get_check_digit(input: str) -> str
- format(rut: str, dots: bool = True, dash: bool = True) -> str
- generate(n: int = 1) -> str or List[str]
"""
import random
import re
from typing import List, Union


def clean(rut: str) -> str:
Expand All @@ -12,6 +30,10 @@ def clean(rut: str) -> str:
Returns:
str: The cleaned rut
Examples:
>>> clean("35.114.652-4")
'351146524'
"""
return re.sub(r'^0+|[^0-9kK]+', '', rut)

Expand All @@ -26,7 +48,7 @@ def validate(rut: str) -> bool:
bool: Whether the rut is valid or not
Examples:
>>> validate("60487586-4")
>>> validate("4612837-0")
True
"""
if not isinstance(rut, str):
Expand All @@ -52,11 +74,11 @@ def validate(rut: str) -> bool:
return v == rut[-1]


def get_check_digit(input: str) -> str:
def get_check_digit(digit: str) -> str:
"""Function to get the check digit of a rut
Args:
input (str): The rut to get the check digit from
digit (str): The rut to get the check digit from
Raises:
ValueError: If the rut is invalid
Expand All @@ -66,15 +88,14 @@ def get_check_digit(input: str) -> str:
Examples:
>>> get_check_digit("60487586")
'4'
'2'
"""
rut = list(map(int, clean(input)))
rut = list(map(int, clean(digit)))

if len(rut) == 0 or any(map(lambda x: x != x, rut)):
raise ValueError(f'"{input}" as RUT is invalid')
raise ValueError(f'"{digit}" as RUT is invalid')

modulus = 11
initial_value = 0
sum_result = sum(
current_value * ((index % 6) + 2)
for index, current_value in enumerate(reversed(rut))
Expand All @@ -90,7 +111,7 @@ def get_check_digit(input: str) -> str:
return str(check_digit)


def format(rut: str, dots: bool = True, dash: bool = True) -> str:
def format_rut(rut: str, dots: bool = True, dash: bool = True) -> str:
"""Function to format a rut
Args:
Expand Down Expand Up @@ -122,26 +143,24 @@ def format(rut: str, dots: bool = True, dash: bool = True) -> str:
return result


def generate(n: int = 1) -> str:
"""Genera RUTs chilenos aleatorios válidos.
def generate(num: int = 1) -> Union[str, List[str]]:
"""Generates random valid Chilean RUTs.
Args:
quantity (int, optional): Cantidad de RUTs a generar. Por defecto, es 1.
num (int, optional): Number of RUTs to generate. Defaults to 1.
Returns:
str or List[str]: El RUT generado. Si la cantidad es mayor a 1, se devuelve una lista de RUTs.
str or List[str]: The generated RUT. If the number is greater than 1, a list of RUTs is returned.
"""
ruts = []
for _ in range(n):
for _ in range(num):
rut_base = random.randint(1000000, 25000000)
rut = str(rut_base)
dv = get_check_digit(rut)
rut = format(rut + dv)
rut = format_rut(rut + dv)
ruts.append(rut)

if n == 1:
return ruts[0]
else:
return ruts

if num == 1:
return [ruts[0]]

return ruts
6 changes: 4 additions & 2 deletions rutpy/tests/test_rut.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest
from rutpy import clean, validate, get_check_digit, format, generate

from rutpy import clean, format_rut, generate, get_check_digit, validate


class RutpyTestCase(unittest.TestCase):
def test_clean(self):
Expand All @@ -23,7 +25,7 @@ def test_get_check_digit(self):
def test_format(self):
ruts = ['17.335.995-0', '20.709.073-5']
expected_results = ['17.335.995-0', '20.709.073-5']
formatted_ruts = [format(rut) for rut in ruts]
formatted_ruts = [format_rut(rut) for rut in ruts]
self.assertEqual(formatted_ruts, expected_results)

def test_generate(self):
Expand Down

0 comments on commit da9b417

Please sign in to comment.