-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
145 lines (109 loc) · 3.51 KB
/
main.py
File metadata and controls
145 lines (109 loc) · 3.51 KB
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
import sys
import csv
import os
CLIENT_TABLE = '.clients.csv'
CLIENT_SCHEMA = ['name', 'company', 'email', 'position']
clients = []
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 = f'{CLIENT_TABLE}.tmp'
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)
# Reusable functions
def _not_founded():
return print('Client is not in the client\'s list')
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_clients_data():
return {
'name': _get_client_field('name'),
'company': _get_client_field('company'),
'email': _get_client_field('email'),
'position': _get_client_field('position')
}
# Action functions
def create_client(client):
global clients
if client not in clients:
clients.append(client)
else:
print('The client you enter is already on the client\'s list. Please try again')
def list_clients():
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:
_not_founded()
def delete_client(client_id):
global clients
for idx, dummy_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 _print_welcome():
print('WELCOME TO 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_clients_data()
create_client(client)
list_clients()
elif command == 'L':
list_clients()
elif command == 'U':
list_clients()
client_id = int(_get_client_field('id'))
updated_client = _get_clients_data()
update_client(client_id, updated_client)
list_clients()
elif command == 'D':
list_clients()
client_id = int(_get_client_field('id'))
delete_client(client_id)
list_clients()
elif command == 'S':
client_name = _get_client_field('name')
found = search_client(client_name.lower())
if found:
print(f'The client: {client_name.lower()} is in the client\'s list')
list_clients()
else:
print(f'The client: {client_name} is not in our client\'s list')
else:
print('Invalid command! Please try again')
_save_clients_to_storage()