Skip to content

Commit 2b3ff90

Browse files
Claudio Rojas RodriguezClaudio Rojas Rodriguez
authored andcommitted
add init project
1 parent 3280cff commit 2b3ff90

6 files changed

Lines changed: 220 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ __pycache__/
77
*.so
88

99
# Distribution / packaging
10+
.pypirc
1011
.Python
1112
env/
1213
build/

LICENTE.txt

Whitespace-only changes.

README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,93 @@
1-
# RUTFunctionsPython
1+
# RUTfunctions
2+
3+
[![Build Status](https://travis-ci.org/claudioDcv/RUTfunctions.svg?branch=master)](https://travis-ci.org/claudioDcv/RUTfunctions)
4+
5+
[![codecov](https://codecov.io/gh/claudioDcv/RUTfunctions/branch/master/graph/badge.svg)](https://codecov.io/gh/claudioDcv/RUTfunctions)
6+
7+
Functional helpers for handling RUT Chile written in `ES6` without dependencies
8+
9+
> Helpers funcionales para manipulación de RUT Chileno escritos en `ES6` sin dependencias
10+
11+
--------------------------------------------------------------------------------
12+
13+
## Install NPM
14+
15+
`npm install rutfunctions`
16+
17+
## Install YARN
18+
19+
`yarn add rutfunctions`
20+
21+
## How to Test
22+
23+
Run one, or a combination of the following commands to lint and test your code:
24+
25+
- npm run lint -- lint the source code with ESLint
26+
- npm test -- run unit tests with Mocha
27+
- npm run test:watch -- run unit tests with Mocha, and watch files for changes
28+
- npm run test:cover -- run unit tests with code coverage by Istanbul
29+
30+
## Base in <https://github.com/kriasoft/babel-starter-kit>
31+
32+
## Using:
33+
34+
- rutClean
35+
36+
```
37+
@param paramrut {string}= 16.761.256-9
38+
@return {string} = 167512569
39+
```
40+
41+
- rutValidate
42+
43+
```
44+
@param paramrut {string} = 16.761.256-9
45+
@return {boolean} = true
46+
```
47+
48+
```javascript
49+
// Example
50+
rutValidate('167512569')
51+
true
52+
rutValidate('167512568')
53+
false
54+
rutValidate('16.751.256-8')
55+
false
56+
rutValidate('16.751.256-9')
57+
true
58+
```
59+
60+
- rutFormat
61+
62+
```
63+
@param paramrut {number/string} = 167512569
64+
@return {string} = 16.761.256-9
65+
```
66+
67+
- rutGetDv
68+
69+
```
70+
@param paramrut {number/string} = 16751256 / 16.751.256
71+
@return {string} = 9
72+
```
73+
74+
- rutGetNumber
75+
76+
```
77+
@param paramrut {string} = 16.751.256-9 / 16751256-9
78+
@return {string} = 16751256
79+
```
80+
81+
- rutCalcDv
82+
83+
```
84+
@param paramrut {string} = 16.751.256-9 / 16751256-9
85+
@return {string} = 9
86+
```
87+
88+
- rutAddDv
89+
90+
```
91+
@param paramrut {string} = 16751256
92+
@return {string} = 167512569
93+
```

rutfunctions/rutfunctions.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# create by claudio.dcv@gmail.com
2+
import re
3+
import math
4+
5+
6+
'''
7+
@param paramrut {string}= 16.761.256-9
8+
@return {string} = 167512569
9+
'''
10+
rut_clean = lambda paramrut : re.sub(r'[^0-9kK]+', '', str(paramrut)).upper()
11+
12+
13+
'''
14+
@param paramrut {number/string} = 16751256 / 16.751.256
15+
@return {string} = 9
16+
'''
17+
def rut_calc_dv(paramrut):
18+
rut = rut_clean(str(paramrut))
19+
reverse_rut = ''.join(c for c in rut if c.isdigit())[::-1]
20+
result = 0
21+
n = 1
22+
23+
for digit_rut in reverse_rut:
24+
n += 1
25+
result += int(digit_rut) * n
26+
n = 1 if n == 7 else n
27+
28+
result = 11 - (result % 11)
29+
result = 0 if result == 11 else result
30+
return str('K' if result == 10 else result)
31+
32+
33+
'''
34+
@example =
35+
RUT_validate('167512569')
36+
True
37+
RUT_validate('167512568')
38+
False
39+
RUT_validate('16.751.256-8')
40+
False
41+
RUT_validate('16.751.256-9')
42+
True
43+
@param paramrut {string} = 16.761.256-9
44+
@return {boolean} = True
45+
'''
46+
def rut_validate(paramrut):
47+
rut = str(paramrut)
48+
if (not re.match(r'^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$', rut)):
49+
return False
50+
51+
rut = rut_clean(rut)
52+
53+
dv = rut[-1]
54+
rut_number = rut[0:-1]
55+
56+
return rut_calc_dv(rut_number) == dv
57+
58+
59+
'''
60+
@param paramrut {number/string} = 167512569
61+
@return {string} = 16.761.256-9
62+
'''
63+
def rut_format(paramrut):
64+
rut = str(rut_clean(paramrut))
65+
result = '{}-{}'.format(rut[-4:-1], rut[-1])
66+
reverse_rut = rut[0:-1][::-1]
67+
68+
def for_in_rut(i, rr, out):
69+
out = rr[i:i+3][::-1] + '.' + out
70+
i += 3
71+
return out if i > len(rr) else for_in_rut(i, rr, out)
72+
73+
return for_in_rut(3, reverse_rut, result)
74+
75+
76+
'''
77+
@param paramrut {string} = 16.751.256-9 / 16751256-9
78+
@return {string} = 16751256
79+
'''
80+
rut_get_number = lambda paramrut : rut_clean(paramrut.split('-', 1)[0])
81+
82+
83+
'''
84+
@param paramrut {string} = 16.751.256-9 / 16751256-9
85+
@return {string} = 9
86+
'''
87+
rut_get_dv = lambda paramrut : rut_clean(paramrut.split('-', 1)[1])
88+
89+
90+
'''
91+
@param paramrut {string} = 16751256
92+
@return {string} = 167512569
93+
'''
94+
rut_add_dv = lambda paramrut : '{}-{}'.format(paramrut, rut_calc_dv(paramrut))
95+
96+
'''
97+
test
98+
99+
test1 = rut_clean('16.752.156-9');
100+
test2 = rut_calc_dv('16751256');
101+
test3 = rut_validate('16.751.256-9');
102+
test4 = rut_validate('16.751.256-8');
103+
test5 = rut_format('167512569');
104+
test6 = rut_get_number('16751256');
105+
test7 = rut_get_dv('16.751.256-9');
106+
test8 = rut_add_dv('16751256');
107+
108+
print(test1, test2, test3, test4, test5, test6, test7, test8);
109+
result:
110+
('167521569', '9', True, False, '16.751.256-9', '16751256', '9', '16751256-9')
111+
'''

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from distutils.core import setup
2+
setup(
3+
name = 'rutfunctions',
4+
packages = ['rutfunctions'], # this must be the same as the name above
5+
version = '0.1',
6+
description = 'helpers to chilean rut',
7+
author = 'Claudio Rojas',
8+
author_email = 'claudio.dcv@gmail.com',
9+
url = 'https://github.com/claudioDcv/RUTFunctionsPython', # use the URL to the github repo
10+
download_url = 'https://github.com/{user_name}/{repo}/tarball/0.1',
11+
keywords = ['python-functions', 'es6', 'chilean-rut-utils', 'functional-programming'],
12+
classifiers = [],
13+
)

0 commit comments

Comments
 (0)