-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (94 loc) · 3.98 KB
/
main.py
File metadata and controls
116 lines (94 loc) · 3.98 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
"""
Assignment 1
Rigre Garciandia
Assumptions:
- I decided to rename the class Person detailed in the instructions to Employee for naming consistency
- To avoid a conflict with built-in name 'id', I renamed the field in the instructions to 'emp_id'
- If input file contains duplicate IDs, I will not ask the user to correct it, just print an error message
"""
import sys
import pathlib
import re
import pickle
class Employee:
def __init__(self, last, first, mi, emp_id, phone):
self.last = last
self.first = first
self.mi = mi
self.emp_id = emp_id
self.phone = phone
def display(self):
print("Employee id:", self.emp_id)
print("\t", self.first, self.mi, self.last)
print("\t", self.phone, "\n")
def gen_employees(text_in_list):
"""
Generates Employee objects based on the input strings. Asks user for manual corrections if needed
:param text_in_list: list of input entries
:return: a dictionary of Employee objects generated from the input strings
"""
if not text_in_list:
raise Exception("input must not be empty")
if not type(text_in_list) == list:
raise TypeError("text_in must be a list")
result = {}
for line_in in text_in_list:
# split on comma to get fields as text variables
last, first, mi, emp_id, phone = line_in.split(",")
# modify last name and first name fo be in Capital Case
last = last.title()
first = first.title()
# modify the MI to be a single upper case letter, use 'X' as MI if one is missing
if mi:
mi = mi.upper()
else:
mi = 'X'
# modify emp_id if necessary
m = re.match(r'^[a-zA-Z]{2}\d{4}$', emp_id)
while not m:
print("ID invalid:", emp_id, "\nID is two letters followed by 4 digits")
emp_id = input("Please enter a valid ID: ")
m = re.match(r'^[a-zA-Z]{2}\d{4}$', emp_id)
# modify phone number if necessary
is_phone_in_dash_format = False
while not is_phone_in_dash_format:
dot_format_match = re.match(r'^[0-9]{3}\.[0-9]{3}\.[0-9]{4}$', phone) # xxx.xxx.xxxx
space_format_match = re.match(r'^[0-9]{3}\s[0-9]{3}\s[0-9]{4}$', phone) # xxx xxx xxxxx
all_together_format_match = re.match(r'^[0-9]{10}$', phone) # xxxxxxxxxx
dash_format_match = re.match(r'^[0-9]{3}-[0-9]{3}-[0-9]{4}$', phone) # xxx-xxx-xxxx
if dash_format_match:
is_phone_in_dash_format = True
elif dot_format_match:
phone = re.sub(r'\.', '-', phone)
elif space_format_match:
phone = re.sub(r'\s', '-', phone)
elif all_together_format_match:
phone = phone[:3] + '-' + phone[3:6] + '-' + phone[6:]
else:
print("Phone", phone, "is invalid")
print("Enter phone number in form 123-456-7890")
phone = input("Enter phone number:")
if emp_id not in result:
result[emp_id] = Employee(last, first, mi, emp_id, phone)
else:
print("Error: ID", emp_id, "is repeated in the input file")
return result
if __name__ == '__main__':
# Check if the relative path was entered
if len(sys.argv) < 2:
print("Please enter the relative path of the data file as a system arg")
quit()
# Retrieve the input data in text format
rel_path = sys.argv[1]
with open(pathlib.Path.cwd().joinpath(rel_path), 'r') as f:
text_in = f.read().splitlines()
# Generate Employee objects from input data
employees = gen_employees(text_in[1:]) # ignoring header lines
# Pickle the employees
pickle.dump(employees, open('employees.pickle', 'wb'))
# read the pickle back in
employees_in = pickle.load(open('employees.pickle', 'rb'))
# output employees
print("\n\nEmployee list:\n")
for obj_id in employees_in.keys():
employees_in[obj_id].display()