-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendario.py
410 lines (318 loc) · 10.3 KB
/
calendario.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import json
from os import scandir, getcwd
def comprobar_mes(num):
if(num == 2):
return 'febrero'
elif(num == 1 or num == 3 or num == 5 or num == 7 or
num == 8 or num == 10 or num == 12):
return '31'
else:
return '30'
def get_mes(i):
switcher = {
1: "Enero",
2: "Febrero",
3: "Marzo",
4: "Abril",
5: "Mayo",
6: "Junio",
7: "Julio",
8: "Agosto",
9: "Septiembre",
10: "Octubre",
11: "Noviembre",
12: "Diciembre"
}
return switcher.get(i)
def get_num_mes(i):
switcher = {
"Enero": 1,
"Febrero": 2,
"Marzo": 3,
"Abril": 4,
"Mayo": 5,
"Junio": 6,
"Julio": 7,
"Agosto": 8,
"Septiembre": 9,
"Octubre": 10,
"Noviembre": 11,
"Diciembre": 12
}
return switcher.get(i)
def get_weekday(i):
switcher = {
1: "lunes",
2: "martes",
3: "miercoles",
4: "jueves",
5: "viernes",
6: "sabado",
7: "domingo",
}
return switcher.get(i)
def get_dayofweek(i):
switcher = {
"lunes": 0,
"martes": 1,
"miercoles":2,
"jueves": 3,
"viernes": 4,
"sabado": 5,
"domingo": 6,
}
return switcher.get(i)
def get_dias_restantes(i):
switcher = {
'lunes': 6,
'martes': 5,
'miercoles':4,
'jueves': 3,
'viernes': 2,
'sabado': 1,
'domingo': 0,
}
return switcher.get(i)
def es_bisiesto(anyo):
if(anyo%4 == 0 and anyo % 100 != 0):
return True
elif(anyo%4 == 0 and anyo%400 == 0):
return True
else:
return False
def get_next_weekday(weekday, passed_days):
if passed_days == 7 or passed_days == 0:
return weekday
else:
return get_weekday((get_dayofweek(weekday)+passed_days)%7+1)
class calendar:
meses = []
dia_semana = 'lunes'
anyo = 2000
convenio = 0
def __init__(self, crear, anyo, convenio = 1, dia_semana = 'lunes'):
if crear:
self.create(dia_semana, anyo, convenio)
else:
self.load(anyo)
def create(self, dia_semana, anyo, convenio):
self.meses = []
self.dia_semana = dia_semana
self.anyo = int(anyo)
self.convenio = convenio
next_week = 1
next_weekday = dia_semana
for i in range(12):
mes = ""
if(comprobar_mes(i+1) == 'febrero'):
mes = febrero(next_weekday, es_bisiesto(self.anyo), next_week)
elif(comprobar_mes(i+1) == '30'):
mes = dias30(get_mes(i+1), next_weekday, next_week)
else:
mes = dias31(get_mes(i+1), next_weekday, next_week)
next_week = mes.next_weeks()
next_weekday = mes.get_next_month_weekday()
self.meses.append(mes)
self.save()
def load(self, anyo):
f = open(getcwd()+"\\data\\"+anyo+'.sv', 'r')
data = json.load(f)
f.close()
self.create(data[anyo]['dia_semana'], int(anyo), data[anyo]['convenio'])
for mes in self.meses:
mes.load(data[str(self.anyo)])
self.save()
def to_string(self):
for mes in self.meses:
print(mes.to_string())
def get_horas(self):
aux = 0
for mes in self.meses:
aux += mes.get_horas()
return float(aux)
def get_mes(self, num):
return self.meses[num]
def get_convenio(self):
return self.convenio
def set_horas(self, mes, dia, horas):
dias = 0
dias_semana = 0
i = 0
encontrado = False
while not encontrado:
semana = self.meses[get_num_mes(mes)-1].semanas[i]
dias_semana = semana.get_days()
if dias + dias_semana < dia:
i += 1
dias += dias_semana
else:
num_dia = dia - dias -1
semana.dias[num_dia].set_horas(horas)
encontrado = True
self.save()
def set_convenio(self, convenio):
self.convenio = convenio
self.save()
def save(self):
data = {self.anyo: {'dia_semana': self.dia_semana, 'convenio': self.convenio}}
for mes in self.meses:
data[self.anyo][mes.get_mes()] = mes.save()
f = open(getcwd()+"\\data\\"+str(self.anyo)+'.sv', 'w+')
json.dump(data, f, indent = 4)
f.close()
class mes:
mes = 'enero'
semanas = []
next_month_weekday = 'lunes'
def __init__(self, mes, dia_semana, total, num_semana):
self.semanas = []
self.mes = mes
n_weekday = dia_semana
n_semana = num_semana
i = 1
while i <= total:
week = semana(n_weekday, i, total, n_semana)
self.semanas.append(week)
n_semana += 1
i += week.get_days()
n_weekday = get_next_weekday(n_weekday, week.get_days())
self.next_month_weekday = n_weekday
def to_string(self):
aux = "\n"+self.mes
for semana in self.semanas:
aux += "\n"+semana.to_string()
return aux
def next_weeks(self):
return self.semanas[-1].get_next_week()
def get_next_month_weekday(self):
return self.next_month_weekday
def get_horas(self):
aux = 0
for semana in self.semanas:
aux += semana.get_horas()
return float(aux)
def get_semanas(self):
return self.semanas
def get_mes(self):
return self.mes
def save(self):
data = {}
for semana in self.semanas:
data[semana.get_num_semana()] = semana.save()
return data
def load(self, data):
for semana in self.semanas:
semana.load(data[self.mes])
class dias31(mes):
def __init__(self, mes, dia_semana, num_semana):
super().__init__(mes, dia_semana, 31, num_semana)
class dias30(mes):
def __init__(self, mes, dia_semana, num_semana):
super().__init__(mes, dia_semana, 30, num_semana)
class febrero:
mes = 'Febrero'
semanas = []
next_month_weekday = 'lunes'
def __init__(self, dia_semana, bisiesto, num_semana):
self.semanas = []
total = 28
if bisiesto:
total = 29
n_weekday = dia_semana
n_semana = num_semana
i = 1
while i <= total:
week = semana(n_weekday, i, total, n_semana)
self.semanas.append(week)
n_semana += 1
i += week.get_days()
n_weekday = get_next_weekday(n_weekday, week.get_days())
self.next_month_weekday = n_weekday
def to_string(self):
aux = "\n"+self.mes
for semana in self.semanas:
aux += "\n"+semana.to_string()
return aux
def next_weeks(self):
return self.semanas[-1].get_next_week()
def get_next_month_weekday(self):
return self.next_month_weekday
def get_horas(self):
aux = 0
for semana in self.semanas:
aux += semana.get_horas()
return float(aux)
def get_semanas(self):
return self.semanas
def get_mes(self):
return self.mes
def save(self):
data = {}
for semana in self.semanas:
data[semana.get_num_semana()] = semana.save()
return data
def load(self, data):
for semana in self.semanas:
semana.load(data[self.mes])
class semana:
dias = []
num_semana = 0
dia_semana = 1
def __init__(self, dia_semana, dia_inicio, dia_final, num_semana):
self.dias = []
self.num_semana = num_semana
self.dia_semana = dia_semana
num_dias = get_dias_restantes(dia_semana)
if(dia_inicio + num_dias < dia_final):
dia_final = dia_inicio+num_dias
for i in range(dia_inicio, dia_final+1):
self.dias.append(dia(i))
def to_string(self):
aux = "Num semana: " + str(self.num_semana)
aux += "\nDia semana: " + self.dia_semana
for dia in self.dias:
aux += "\n" + dia.to_string()
return aux
def get_next_week(self):
if len(self.dias) <= get_dias_restantes(self.dia_semana):
return self.num_semana
else:
return self.num_semana+1
def get_days(self):
return len(self.dias)
def get_dias(self):
return self.dias
def get_horas(self):
aux = 0
for dia in self.dias:
aux += dia.get_horas()
return float(aux)
def get_num_semana(self):
return self.num_semana
def get_dia_semana(self):
return self.dia_semana
def save(self):
data = {}
for dia in self.dias:
data[dia.get_dia()] = dia.save()
return data
def load(self, data):
for dia in self.dias:
dia.load(data[str(self.num_semana)])
class dia:
dia = 1
horas = 0
def __init__(self, dia):
self.dia = dia
def to_string(self):
return "Dia: "+str(self.dia)+" Horas: "+str(self.horas)
def get_horas(self):
return float(self.horas)
def get_dia(self):
return self.dia
def set_horas(self, horas):
self.horas = horas
def save(self):
return self.horas
def load(self, data):
self.horas = data[str(self.dia)]