forked from platzi/curso_Python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
140 lines (100 loc) · 3.31 KB
/
main.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
import csv
import os
CLIENT_SCHEMA = ['name', 'company', 'email', 'position']
CLIENT_TABLE = '.clients.csv'
clients = []
def create_client(client):
global clients
if client not in clients:
clients.append(client)
else:
print('Client already in client\'s list')
def list_clients():
print('uid | name | company | email | position ')
print('*' * 50)
for idx, client in enumerate(clients):
print('{uid} | {name} | {company} | {email} | {position}'.format(
uid=idx,
name=client['name'],
company=client['company'],
email=client['email'],
position=client['position']))
def update_client(client_id, updated_client):
global clients
if len(clients) - 1 >= client_id:
clients[client_id] = updated_client
else:
print('Client not in client\'s list')
def delete_client(client_id):
global clients
for idx, client in enumerate(clients):
if idx == client_id:
del clients[idx]
break
def search_client(client_name):
for client in clients:
if client['name'] != client_name:
continue
else:
return True
def _get_client_field(field_name, message='What is the client {}?'):
field = None
while not field:
field = input(message.format(field_name))
return field
def _get_client_from_user():
client = {
'name': _get_client_field('name'),
'company': _get_client_field('company'),
'email': _get_client_field('email'),
'position': _get_client_field('position'),
}
return client
def _initialize_clients_from_storage():
with open(CLIENT_TABLE, mode='r') as f:
reader = csv.DictReader(f, fieldnames=CLIENT_SCHEMA)
for row in reader:
clients.append(row)
def _save_clients_to_storage():
tmp_table_name = '{}.tmp'.format(CLIENT_TABLE)
with open(tmp_table_name, mode='w') as f:
writer = csv.DictWriter(f, fieldnames=CLIENT_SCHEMA)
writer.writerows(clients)
os.remove(CLIENT_TABLE)
os.rename(tmp_table_name, CLIENT_TABLE)
def _print_welcome():
print('WELCOME TO PLATZI VENTAS')
print('*' * 50)
print('What would you like to do today?:')
print('[C]reate client')
print('[L]ist clients')
print('[U]pdate client')
print('[D]elete client')
print('[S]earch client')
if __name__ == '__main__':
_initialize_clients_from_storage()
_print_welcome()
command = input()
command = command.upper()
if command == 'C':
client = _get_client_from_user()
create_client(client)
elif command == 'L':
list_clients()
elif command == 'U':
client_id = int(_get_client_field('id'))
updated_client = _get_client_from_user()
update_client(client_id, updated_client)
elif command == 'D':
client_id = int(_get_client_field('id'))
delete_client(client_id)
elif command == 'S':
client_name = _get_client_field('name')
found = search_client(client_name)
if found:
print('The client is in the client\'s list')
else:
print('The client: {} is not in our client\'s list'.format(client_name))
else:
print('Invalid command')
_save_clients_to_storage()