-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.py
102 lines (70 loc) · 2.56 KB
/
src.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Import libraries
import pandas as pd
pd.set_option('display.max_columns', None)
import numpy as np
import mysql.connector
from sqlalchemy import create_engine
import warnings
warnings.filterwarnings('ignore')
import sys
sys.path.insert(1, 'notebooks/')
from passwords import CURSOR
from passwords import ENGINE
# SQL functions
def add_table(name):
'''
This function adds a dataframe to the database the variable `CURSOR` is pointing as a table, setting the {name}_id as the primary key.
Args:
str: the name of a csv to look for. The same name should be as one of the columns like this: name_id.
Returns:
int: process code.
'''
try:
try:
df = pd.read_csv(f'../data/{name}.csv')
except:
print(f"Error: couldn't find {name}.csv in ../data/")
return (1)
if f'{name}_id' not in df.columns:
print(f"Error: {name}_id not in dataframe")
return (2)
# Downcasting so that we have smallint and floats instead of bigint and doubles
for c in df.select_dtypes(include='float'):
df[c] = pd.to_numeric(df[c], downcast='float')
for c in df.select_dtypes('integer'):
df[c] = pd.to_numeric(df[c], downcast='integer')
df.to_sql(name=f'{name}',
con=CURSOR,
if_exists='append',
index=False
)
with CURSOR.connect() as con:
con.execute(f'ALTER TABLE `{name}` ADD PRIMARY KEY (`{name}_id`);')
print("Table created succesfully")
print('-'*15)
return (0)
except:
print ('Unexpected error. Use help(add_table) for usage.')
return (3)
# Formatting functions
def format_phone_number(phone):
'''
Returns the phone number introduced to the standar Spanish format "+34 XXX XXX XXX".
Args:
phone: a string of a phone number with or without '+' and with or without 34 at the beggining (it will be added if it doesn't have it).
Returns:
string: a string formatted "+34 XXX XXX XXX".
int: error code.
'''
try:
phone = ''.join(filter(str.isdigit, phone))
if not phone.startswith('34'):
phone = '34' + phone
if len(phone) < 11:
print ('Phone number too short')
return (1)
formatted_phone = f'+34 {phone[3:6]} {phone[6:9]} {phone[9:13]}'
return formatted_phone
except:
print ('Unexpected error. Use help(format_phone_number) for usage.')
return (2)