-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagenda.py
171 lines (139 loc) · 5.21 KB
/
agenda.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import csv
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Contacto:
def __init__(self, name, lastname, number):
self.name = name
self.lastname = lastname
self.number = number
def getInfo(self):
return [self.name, self.lastname, self.number]
def getName(self):
return self.name
def getLastname(self):
return self.lastname
def getFullName(self):
return self.name + ' ' + self.lastname
def getNumber(self):
return self.number
class Agenda:
def __init__(self, filePath):
self.contactList = []
self.filePath = filePath
self.file = open(filePath, 'r+')
self.header = []
self.importFromCSV()
def addNewContact(self, callback):
localFile = open(self.filePath, 'a')
writer = csv.writer(localFile)
name = input('\nenter name:\n')
lastname = input('\nenter lastname:\n')
number = input('\nenter phone number:\n')
newContact = Contacto(name, lastname, number)
# se añade el contacto a la clase agenda y al csv al mismo tiempo
self.contactList.append(newContact.getInfo())
writer.writerow(newContact.getInfo())
print(bcolors.OKGREEN + '\ncontacto guardado:',
str(newContact.getInfo()))
callback()
def importFromCSV(self):
print('importando contactos...')
localFile = open(self.filePath, newline='')
reader = csv.reader(localFile)
fileHeader = next(reader)
fileContent = [row for row in reader]
# gardar cabecera
self.header = fileHeader
# para cada linea de csv crear un contacto e introducirlo en la clase agenda
for row in fileContent:
newContact = Contacto(row[0], row[1], row[2])
self.contactList.append(newContact)
print(bcolors.OKGREEN + 'se han importando ' +
str(len(fileContent)) + ' contactos\n' + bcolors.ENDC)
def buscarContacto(self, callback):
inputFilter = input('introduce el campo de busqueda\n')
filterField = ''
columnIndex = False
for col in self.header:
if col == inputFilter:
filterField = col
columnIndex = self.header.index(col)
if (filterField != ''):
filterValue = input('introduce el ' + filterField + ' a buscar\n')
foundContacts = self.getContactsByColumn(columnIndex, filterValue)
numOfFoundContacts = len(foundContacts)
if foundContacts:
print('ha habido ' + str(numOfFoundContacts) + ' coincidencia/s: ')
print(foundContacts)
else:
print('no hay ninguna coincidencia')
else:
print(bcolors.FAIL + 'el campo que buscas no existe')
callback()
def getContactsByColumn(self, columnIndex, filter):
coincidences = []
for contact in self.contactList:
currentContactData = contact.getInfo()
fieldToSearch = currentContactData[columnIndex]
if(fieldToSearch == str(filter)):
coincidences.append(currentContactData)
return coincidences
def getTableHeader(self):
return self.header
def printContactList(self, printMode, callback):
tableHeader = str(self.getTableHeader())
printed = 0
for contact in self.contactList:
if (printMode == 'all'):
if printed == 0:
print('\n' + bcolors.OKBLUE + tableHeader + bcolors.ENDC)
printed = 1
print(contact.getInfo())
elif printMode == 'nombre':
print(contact.getName())
elif printMode == 'apellido':
print(contact.getLastname())
elif printMode == 'nombre completo':
print(contact.getFullName())
elif printMode == 'numero':
print(contact.getNumber())
callback()
class App:
def __init__(self, agenda):
self.agenda = agenda
print('Welcome to Python CSV Agenda!\n')
self.start()
def start(self):
mode = input(
'Which action do you want to perform?\n"search", "add" or "list"')
if mode == 'search':
self.searchMode()
elif mode == 'add':
self.addMode()
elif mode == 'list':
self.listMode()
def listMode(self):
printMode = input(
'Do you want to print a specific column or "all" columns?')
self.agenda.printContactList(printMode, self.restart)
def addMode(self):
self.agenda.addNewContact(self.restart)
def searchMode(self):
self.agenda.buscarContacto(self.restart)
def restart(self):
restart = input('Do you want a perform other operations? "y" or "n"\n')
if restart == 'y':
self.start()
elif restart == 'n':
print('Goodbye!')
exit()
# inicializar agenda al arrancar la aplicacion
# agenda = Agenda('./my-agenda.csv')
App(Agenda('./agenda.csv')).start()