Last active
March 7, 2017 22:22
-
-
Save pablocastelo/48e214f3a9cb93edf5fed8dd67695190 to your computer and use it in GitHub Desktop.
Checador de números usando el PNN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import pandas as pd | |
import re | |
pnn = pd.read_csv('pnn_Publico_01_03_2017.csv', encoding='Latin-1', index_col=False) | |
n = [] | |
for i in list(pnn.columns): | |
n.append(i.strip().replace(' ','_')) | |
pnn.columns = n | |
pnn = pnn.rename(columns = {'TIPO_RED':'TIPO_DE_RED'}) | |
def separar(num): | |
num = str(num).strip() | |
num = re.sub(r'\D', '', num) | |
r = ({}) | |
r['FULL'] = num | |
if len(num) == 10: | |
r['valid'] = '1' | |
if num[0:2] in ['33', '55', '81']: | |
r['NIR'] = num[:2] | |
r['SERIE'] = num[2:6] | |
r['ELSE'] = num[-4:] | |
else: | |
r['NIR'] = num[:3] | |
r['SERIE'] = num[3:6] | |
r['ELSE'] = num[-4:] | |
else: | |
r['valid'] = '0' | |
return r | |
def separar_7(num): | |
num = str(num).strip() | |
num = re.sub(r'\D', '', num) | |
r = ({}) | |
r['FULL'] = num | |
if len(num) == 7: | |
r['valid'] = '1' | |
r['SERIE'] = num[:3] | |
r['ELSE'] = num[-4:] | |
else: | |
r['valid'] = '0' | |
return r | |
def dial_info(sep, lada = '33'): | |
r = ({}) | |
lada = str(lada) | |
if sep['NIR'] == lada: | |
if sep['TIPO_DE_RED'] == 'FIJO': | |
r['local'] = sep['SERIE'] + sep['ELSE'] | |
else: | |
r['local'] = '044' + sep['gsm'] | |
else: | |
if sep['TIPO_DE_RED'] == 'FIJO': | |
r['local'] = '01' + sep['gsm'] | |
else: | |
r['local'] = '045' + sep['gsm'] | |
return r | |
def get_info(num, lada = '33'): | |
s = separar(num) | |
r = ({}) | |
if s['valid'] == '1': | |
try: | |
p = pnn[(pnn.NIR == int(s['NIR'])) & | |
(pnn['SERIE'] == int(s['SERIE'])) & | |
(pnn['NUMERACION_INICIAL'] <= int(s['ELSE'])) & | |
(pnn['NUMERACION_FINAL'] >= int(s['ELSE']))] | |
r = ({'gsm': s['FULL']}) | |
r.update(p.to_dict('records')[0]) | |
r['NIR'] = str(r['NIR']) | |
r['SERIE'] = str(r['SERIE']) | |
r['ELSE'] = s['ELSE'] | |
r['valid'] = '1' | |
r.update(dial_info(r, lada)) | |
if r['TIPO_DE_RED'] == 'MOVIL': | |
r['esMovil'] = '1' | |
else: | |
r['esMovil'] = '0' | |
except: | |
r['valid'] = '0' | |
else: | |
r['valid'] = '0' | |
return r | |
def get_info_7(num, lada = '33'): | |
s = separar_7(num) | |
r = ({}) | |
if s['valid'] == '1': | |
try: | |
r = ({}) | |
p = pnn[(pnn['ESTADO'] == 'SIN') & | |
(pnn['SERIE'] == int(s['SERIE'])) & | |
(pnn['NUMERACION_INICIAL'] <= int(s['ELSE'])) & | |
(pnn['NUMERACION_FINAL'] >= int(s['ELSE']))] | |
r.update(p[:1].to_dict('records')[0]) | |
r['NIR'] = str(r['NIR']) | |
r['SERIE'] = str(s['SERIE']) | |
r['ELSE'] = s['ELSE'] | |
r['FULL'] = r['NIR'] + r['SERIE'] + r['ELSE'] | |
r['gsm'] = r['NIR'] + r['SERIE'] + r['ELSE'] | |
r.update(dial_info(r, lada)) | |
if r['TIPO_DE_RED'] == 'MOVIL': | |
r['esMovil'] = '1' | |
else: | |
r['esMovil'] = '0' | |
r['valid'] = '1' | |
except: | |
r['valid'] = '0' | |
else: | |
r['valid'] = '0' | |
return r | |
def dial_plan(num): | |
if len(num) in [10,11]: | |
r = get_info(num[-10:]) | |
if r['valid'] == '1': | |
if r['TIPO_DE_RED'] == 'MOVIL': | |
return '1' + r['gsm'] | |
else: | |
return r['gsm'] | |
else: | |
return 'not_valid' | |
else: | |
return 'not_valid' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment