-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
157 lines (121 loc) · 4.59 KB
/
utils.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
import datetime
import unidecode
def datetime_to_brstring(dt):
try:
return dt.strftime('%d/%m/%Y')
except AttributeError as e:
return ''
def convert_datetime_format(dt_str, dt_str_format, to_format):
try:
return datetime.datetime.strptime(dt_str, dt_str_format).strftime(to_format)
except AttributeError as e:
return ''
def yyyymmdd_to_date_time(dt_str):
try:
return datetime.datetime.strptime(dt_str, '%Y%m%d')
except ValueError as e:
return ''
def sort_array_by_datetime(unsorted_array, reverse=False):
sorted_array = sorted(
unsorted_array,
key=lambda x: x[4], reverse=reverse
)
return sorted_array
def sort_array_date_br(data_array, opt=1):
"""
recebe um array, transforma a coluna 4 e 5 para datetime
ordena
passa as colunas pra formato string br
:param data_array: array
:param index: int
:return: array
"""
# passa para datetime
for i in range(len(data_array)):
if opt != 1:
data_array[i][9] = yyyymmdd_to_date_time(data_array[i][9])
data_array[i][4] = yyyymmdd_to_date_time(data_array[i][4])
data_array[i][5] = yyyymmdd_to_date_time(data_array[i][5])
# ordena por datetime
data_array = sort_array_by_datetime(data_array, reverse=True)
# passa para data br str
for i in range(len(data_array)):
if opt != 1:
data_array[i][9] = datetime_to_brstring(data_array[i][9])
data_array[i][4] = datetime_to_brstring(data_array[i][4])
data_array[i][5] = datetime_to_brstring(data_array[i][5])
return data_array
def sort_array_by_date_and_index(data_array, index=4):
"""
recebe um array, transforma uma coluna especifica pra datetime
ordena
passa o a coluna pra formato string br
:param data_array: array
:param index: int
:return: aray
"""
# passa para datetime
for i in range(len(data_array)):
data_array[i][index] = yyyymmdd_to_date_time(data_array[i][index])
# ordena por datetime
data_array = sort_array_by_datetime(data_array, reverse=True)
# passa para data br str
for i in range(len(data_array)):
data_array[i][index] = datetime_to_brstring(data_array[i][index])
return data_array
def format_datetime_array(array):
array_aux = []
for i in array:
date_initial = i.split(' - ')[0]
date_end = i.split(' - ')[1]
date_initial = date_initial[6:8] + '/' + date_initial[4:6] + '/' + date_initial[0:4]
date_end = date_end[6:8] + '/' + date_end[4:6] + '/' + date_end[0:4]
array_aux.append(date_initial + ' - ' + date_end)
return array_aux
def remove_duplicates_array(array):
"""Este método está seendo usado no lugar de set porque o set tira a ordem"""
retval = []
for i in array:
if i not in retval:
retval.append(i)
return retval
def fix_date_mapa_final(array):
retval = []
for i in array:
if i not in retval:
data_ini_br = yyyymmdd_to_date_time(i['data_inicial']).strftime('%d/%m/%Y')
data_fim_br = yyyymmdd_to_date_time(i['data_final']).strftime('%d/%m/%Y')
i['data_inicial'] = data_ini_br
i['data_final'] = data_fim_br
retval.append(i)
return retval
def generate_csv_str(cardapio_aux):
header = [['ATENDIMENTO', 'UNIDADE', 'AGRUPAMENTO', 'IDADE', 'DATA', 'STATUS', 'REFEICÃO', 'CARDÁPIO']]
cardapio_aux = header + cardapio_aux
csvlist = '\n'.join(['"' + str('";"'.join(row)) + '"' for row in cardapio_aux])
return unidecode.unidecode(csvlist)
def date_to_str(date):
return datetime.datetime.strftime(date, '%d/%m/%Y')
def monday_to_sunday(date):
return date + datetime.timedelta(days=6)
def last_monday(date):
while date.weekday() != 0:
date -= datetime.timedelta(days=1)
return date
def generate_ranges(days):
dates = []
last_date = datetime.datetime.utcnow()
current_day = datetime.datetime.utcnow()
if days == 'all':
dates = ['18/12/2017 - 22/12/2017']
last_date = datetime.datetime.utcnow()
current_day = last_monday(datetime.datetime(2017, 12, 23))
elif int(days) > 0:
current_day = last_monday(current_day)
last_date = datetime.datetime.utcnow() + datetime.timedelta(days=int(days))
else:
current_day = last_monday(current_day + datetime.timedelta(days=int(days)))
while current_day < last_date:
dates.append(date_to_str(current_day) + ' - ' + date_to_str(monday_to_sunday(current_day)))
current_day += datetime.timedelta(days=7)
return dates