-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
executable file
·166 lines (141 loc) · 8.03 KB
/
data.py
File metadata and controls
executable file
·166 lines (141 loc) · 8.03 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from datetime import date, timedelta
import parameters
import shifts as shiftsModule
import employee as employeeModule
import sys
import utilities
class Data:
start_date = None # date object (Must be a monday)
end_date = None # date object (Must be a sunday)
days = 0
weeks = 0
is_holiday = list() # [i] = whether day start_date + i is holiday
employees = list()
shifts = shiftsModule.Shifts()
days_map = dict() # (only for printing) i: date object, date object: i
# -- utilities
def is_on_weekend(self,day):
return day % 7 in {5,6}
def __init__(self):
self.start_date = parameters.start_date
self.end_date = parameters.end_date
self.days = (self.end_date - self.start_date).days + 1
for i in range(self.days):
self.days_map[i] = self.start_date + timedelta(i)
self.days_map[self.start_date + timedelta(i)] = i
self.weeks = int(self.days / 7)
self.is_holiday = [False for i in range(self.days)] # i: whether day start_date + i is holiday
self.read_holidays()
self.shifts.read_file(len(self.is_holiday), self.is_holiday)
self.shifts.create_shift_adjacency_matrix()
self.read_employees()
self.compute_number_of_shifts_per_week()
def read_holidays(self):
with open(parameters.file_festivita, 'r') as source:
lines = source.readlines()
for line in lines:
try:
day, month, year = [int(item) for item in line.split()[0].split('/')]
except IndexError:
continue
# -- make sure format is correct
delta = date(utilities.format_year(year), month, day) - self.start_date
if 0 <= delta.days < self.days:
self.is_holiday[delta.days] = True
def read_employees(self):
# -- read file
with open(parameters.file_operatori, 'r') as source:
lines = source.readlines()
# try:
i = 0
while i < len(lines):
if lines[i].strip():
name = lines[i].rstrip().lstrip()
i += 1
# -- ccnl contract
ccnl_contract = False
if "ccnl" in lines[i]:
ccnl_contract = True
i += 1
# -- identify shifts per week
if "turni per settimana" not in lines[i]:
if utilities.similar_string("turni per settimana", lines[i].split('=')[0]) < .6:
sys.exit(
"Errore in file operatori. Per operatore {0} manca riga per definire turni per settimana".format(
employee.name))
else:
print("Warning!! Possibili errori di battiture in file operatori, riga {0}: {1}".format(i, lines[i]))
shifts_per_week = int(lines[i].rstrip().split('=')[-1])
i += 1
# -- create Employee
employee = employeeModule.Employee(name, ccnl_contract, {w: shifts_per_week for w in range(self.weeks)},
{shift: False for shift in self.shifts},
[False for d in range(self.days)],
len(self.employees))
self.employees.append(employee)
# -- identify optional lines
while lines[i].strip():
try:
keyword, value = lines[i].rstrip().split('=')
except ValueError:
sys.exit("Errore. In", lines[i], "ci sono troppi =. Ce ne dovrebbe esere solo 1.")
if "giorni non disponibile" == keyword.rstrip().lstrip():
for v in value.split(','):
# -- safety check
utilities.safety_check(v.count('/') == 2,
"Data nel formato sbagliato in riga \"giorni non disponibile\" per operatore %s" % employee.name)
# -- get date and transform it in day number
day, month, year = v.split('/')
delta = date(utilities.format_year(year), int(month), int(day)) - self.start_date
# -- if day is in range considered, update employee.unuavailable
if 0 <= delta.days < self.days:
for shift in self.shifts.select(day=delta.days):
employee.unavailable[shift] = True
elif "turni non disponibile" == keyword.rstrip().lstrip():
for v in value.split(','):
# -- safety check
utilities.safety_check(v.count('-') == 1, "Formato sbagliato in riga \"turni non disponibile\" per operatore %s" % employee.name)
shift_name, shift_day = v.split('-')
# -- safety check
utilities.safety_check(shift_day.count('/') == 2, "Data nel formato sbagliato in riga \"turni non disponibile\" per operatore %s" % employee.name)
# -- get date and transform it in day number
day, month, year = shift_day.split('/')
delta = date(utilities.format_year(year), int(month), int(day)) - self.start_date
# -- if day is in range considered, update employee.unuavailable
if 0 <= delta.days < self.days:
self.employees[-1].unavailable[
self.shifts.get_shift[shift_name.lstrip().rstrip(), delta.days]] = True
elif 'turni specifici' == keyword.rstrip().lstrip():
# -- re-set unavailable
for k in self.employees[-1].unavailable.keys():
self.employees[-1].unavailable[k] = True
for v in value.split(','):
# -- safety check
utilities.safety_check(v.count('-') == 1,
"Formato sbagliato in riga \"turni specifici\" per operatore %s" % employee.name)
shift_name, weekday = v.split('-')
shift_name = shift_name.strip()
weekday_short = weekday.strip()[:3]
# -- update all associate shifts
for day in range(utilities.days_map[weekday_short], self.days, 7):
if not self.is_holiday[day]:
self.employees[-1].unavailable[
self.shifts.get_shift[shift_name, day]] = False
else:
keywords = ["turni specifici", "turni non disponibile", "giorni non disponibile"]
best_option = keywords[
max(enumerate(keywords), key=lambda x: utilities.similar_string(x[1], keyword))[0]]
sys.exit(
"Opzione non riconosciuta in opzione: \"{0}\". Forse intendevi \"{1}\"?".format(keyword,
best_option))
i += 1
i += 1
def compute_number_of_shifts_per_week(self):
return
# TODO pre-process the number of shifts per week per employee
# for employee in self.employees:
# for w in range(0, self.weeks):
# employee.shifts_week =
# except Exception as e:
# print("Errore in file", parameters.file_operatori)
# sys.exit(e)