From da9b41738a4c7c6fefb64d8a73c4434b26b1baa6 Mon Sep 17 00:00:00 2001 From: Valentin Marquez Date: Thu, 29 Jun 2023 08:56:48 +0000 Subject: [PATCH] Fix errors --- poetry.lock | 128 ++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- rutpy/__init__.py | 2 +- rutpy/core/rut.py | 65 ++++++++++++-------- rutpy/tests/test_rut.py | 6 +- 5 files changed, 176 insertions(+), 27 deletions(-) create mode 100644 poetry.lock diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 0000000..dd56962 --- /dev/null +++ b/poetry.lock @@ -0,0 +1,128 @@ +# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand. + +[[package]] +name = "atomicwrites" +version = "1.4.1" +description = "Atomic file writes." +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +files = [ + {file = "atomicwrites-1.4.1.tar.gz", hash = "sha256:81b2c9071a49367a7f770170e5eec8cb66567cfbbc8c73d20ce5ca4a8d71cf11"}, +] + +[[package]] +name = "attrs" +version = "23.1.0" +description = "Classes Without Boilerplate" +optional = false +python-versions = ">=3.7" +files = [ + {file = "attrs-23.1.0-py3-none-any.whl", hash = "sha256:1f28b4522cdc2fb4256ac1a020c78acf9cba2c6b461ccd2c126f3aa8e8335d04"}, + {file = "attrs-23.1.0.tar.gz", hash = "sha256:6279836d581513a26f1bf235f9acd333bc9115683f14f7e8fae46c98fc50e015"}, +] + +[package.extras] +cov = ["attrs[tests]", "coverage[toml] (>=5.3)"] +dev = ["attrs[docs,tests]", "pre-commit"] +docs = ["furo", "myst-parser", "sphinx", "sphinx-notfound-page", "sphinxcontrib-towncrier", "towncrier", "zope-interface"] +tests = ["attrs[tests-no-zope]", "zope-interface"] +tests-no-zope = ["cloudpickle", "hypothesis", "mypy (>=1.1.1)", "pympler", "pytest (>=4.3.0)", "pytest-mypy-plugins", "pytest-xdist[psutil]"] + +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "iniconfig" +version = "2.0.0" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.7" +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + +[[package]] +name = "packaging" +version = "23.1" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "packaging-23.1-py3-none-any.whl", hash = "sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61"}, + {file = "packaging-23.1.tar.gz", hash = "sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f"}, +] + +[[package]] +name = "pluggy" +version = "1.2.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.7" +files = [ + {file = "pluggy-1.2.0-py3-none-any.whl", hash = "sha256:c2fd55a7d7a3863cba1a013e4e2414658b1d07b6bc57b3919e0c63c9abb99849"}, + {file = "pluggy-1.2.0.tar.gz", hash = "sha256:d12f0c4b579b15f5e054301bb226ee85eeeba08ffec228092f8defbaa3a4c4b3"}, +] + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["pytest", "pytest-benchmark"] + +[[package]] +name = "py" +version = "1.11.0" +description = "library with cross-python path, ini-parsing, io, code, log facilities" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +files = [ + {file = "py-1.11.0-py2.py3-none-any.whl", hash = "sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378"}, + {file = "py-1.11.0.tar.gz", hash = "sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719"}, +] + +[[package]] +name = "pytest" +version = "6.2.5" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.6" +files = [ + {file = "pytest-6.2.5-py3-none-any.whl", hash = "sha256:7310f8d27bc79ced999e760ca304d69f6ba6c6649c0b60fb0e04a4a77cacc134"}, + {file = "pytest-6.2.5.tar.gz", hash = "sha256:131b36680866a76e6781d13f101efb86cf674ebb9762eb70d3082b6f29889e89"}, +] + +[package.dependencies] +atomicwrites = {version = ">=1.0", markers = "sys_platform == \"win32\""} +attrs = ">=19.2.0" +colorama = {version = "*", markers = "sys_platform == \"win32\""} +iniconfig = "*" +packaging = "*" +pluggy = ">=0.12,<2.0" +py = ">=1.8.2" +toml = "*" + +[package.extras] +testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] + +[[package]] +name = "toml" +version = "0.10.2" +description = "Python Library for Tom's Obvious, Minimal Language" +optional = false +python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" +files = [ + {file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"}, + {file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"}, +] + +[metadata] +lock-version = "2.0" +python-versions = "^3.8" +content-hash = "64524bddb6b8ae6185a66ebfdb9e55485dd9fe3b66a78a4edf793c0e7c1055b8" diff --git a/pyproject.toml b/pyproject.toml index 25ad14b..c07c286 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] diff --git a/rutpy/__init__.py b/rutpy/__init__.py index 5c8c7a0..139c439 100644 --- a/rutpy/__init__.py +++ b/rutpy/__init__.py @@ -1 +1 @@ -from .core.rut import clean, validate, format, get_check_digit, generate \ No newline at end of file +from .core.rut import clean, format_rut, generate, get_check_digit, validate diff --git a/rutpy/core/rut.py b/rutpy/core/rut.py index 35b103f..11a5ffc 100644 --- a/rutpy/core/rut.py +++ b/rutpy/core/rut.py @@ -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: @@ -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) @@ -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): @@ -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 @@ -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)) @@ -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: @@ -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 diff --git a/rutpy/tests/test_rut.py b/rutpy/tests/test_rut.py index 7f0e118..ace038c 100644 --- a/rutpy/tests/test_rut.py +++ b/rutpy/tests/test_rut.py @@ -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): @@ -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):