-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
229 lines (181 loc) · 6.9 KB
/
Copy pathutils.py
File metadata and controls
229 lines (181 loc) · 6.9 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
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
import os
import pickle
import itertools
from datetime import datetime
import mysql.connector
# colocar os dados para conectar ao BD aqui :)
db_connection = mysql.connector.connect(
host="localhost",
user="root",
password="kashira",
database="laboratorio"
)
db_cursor = db_connection.cursor()
class ExameLaboratorial:
id_iter = itertools.count()
def __init__(self, tipo, descricao):
self.__id = next(self.id_iter)
self._tipo = tipo
self._descricao = descricao
def get_id(self):
return self.__id
def get_tipo(self):
return self._tipo
def __str__(self):
return f"Exame {self.__id} - Tipo: {self._tipo}, Descrição: {self._descricao}"
class Paciente:
def __init__(self, cpf, nome, data_nascimento):
self.__cpf = cpf
self._nome = nome
self._data_nascimento = data_nascimento
def get_cpf(self):
return self.__cpf
def get_nome(self):
return self._nome
def __str__(self):
return f"Paciente {self.__cpf} - Nome: {self._nome}, Data de Nascimento: {self._data_nascimento}"
class RegistroExameLaboratorial:
if not os.path.exists('ultimo_id_registro.txt'):
with open('ultimo_id_registro.txt', 'w') as f:
f.write('0')
with open('ultimo_id_registro.txt', 'r') as f:
id_iter = itertools.count(start=int(f.read().strip()))
def __init__(self, exame, paciente, data_criacao):
self.__id = next(self.id_iter)
with open('ultimo_id_registro.txt', 'w') as f:
f.write(str(self.__id + 1))
self.__exame = exame
self.__paciente = paciente
self.__data_criacao = data_criacao
def get_paciente(self):
return self.__paciente
def __str__(self):
return (f"Registro de Exame Laboratorial {self.__id}:\n"
f" - {self.__exame}\n"
f" - {self.__paciente}\n"
f" - Data de Criação: {self.__data_criacao}")
class FilaDeEspera:
def __init__(self):
self.__fila = []
if not os.path.exists('backup_fila.bak'): self.__salvar()
with open('backup_fila.bak', 'rb') as arquivo_backup:
self.__fila = pickle.load(arquivo_backup)
def inserir(self, registro):
if not isinstance(registro, RegistroExameLaboratorial): return False
self.__fila.append(registro)
self.__salvar()
return True
def remover(self):
if not self.__fila: return None
registro_removido = self.__fila.pop(0)
self.__salvar()
return registro_removido
def limpar(self):
while self.__fila: self.remover()
def __salvar(self):
with open('backup_fila.bak', 'wb') as arquivo_backup:
pickle.dump(self.__fila, arquivo_backup)
def __str__(self):
if not self.__fila:
return "Fila de espera vazia."
return "\n".join(str(registro) for registro in self.__fila)
def __len__(self):
return len(self.__fila)
class ExamesEmColeta:
def __init__(self):
self.__lista = []
if not os.path.exists('backup_lista.bak'): self.__salvar()
with open('backup_lista.bak', 'rb') as arquivo_backup:
self.__lista = pickle.load(arquivo_backup)
def __salvar(self):
with open('backup_lista.bak', 'wb') as arquivo_backup:
pickle.dump(self.__lista, arquivo_backup)
def inserir(self, registro):
if not isinstance(registro, RegistroExameLaboratorial): return False
self.__lista.append(registro)
self.__salvar()
return True
def remover(self, cpf):
if not self.__lista: return None
indice = -1
for i, registro in enumerate(self.__lista):
paciente = registro.get_paciente()
if paciente.get_cpf() == cpf:
indice = i
break
if indice == -1: return False
registro_removido = self.__lista.pop(indice)
self.__salvar()
return registro_removido
def limpar(self):
while self.__lista: self.__lista.pop(0)
self.__salvar()
def __str__(self):
if not self.__lista:
return "Nenhum exame em coleta."
return "\n".join(str(registro) for registro in self.__lista)
def __len__(self):
return len(self.__lista)
class ExamesColetados:
def __init__(self):
self.__arquivo_historico = 'arquivo_historico.hist'
def inserir(self, registro):
if not isinstance(registro, RegistroExameLaboratorial): return False
with open(self.__arquivo_historico, 'a') as arquivo:
arquivo.write(str(registro))
arquivo.close()
return True
def limpar(self):
with open(self.__arquivo_historico, 'w'):
pass
def __str__(self):
if not os.path.exists(self.__arquivo_historico):
return "Nenhum exame registrado."
with open(self.__arquivo_historico, 'r+') as arquivo:
conteudo = arquivo.read()
if len(conteudo) == 0:
arquivo.close()
return "Nenhum exame registrado."
else:
arquivo.close()
return conteudo
class Registrador:
def __init__(self):
self.__fila = FilaDeEspera()
self.__em_coleta = ExamesEmColeta()
self.__coletados = ExamesColetados()
def limpar(self):
self.__fila.limpar()
self.__em_coleta.limpar()
self.__coletados.limpar()
def registrar(self, id_exame, cpf_paciente):
db_cursor.execute("SELECT * FROM pacientes WHERE cpf = %s", (cpf_paciente,))
paciente_data = db_cursor.fetchone()
if not paciente_data:
return False
paciente = Paciente(*paciente_data)
db_cursor.execute("SELECT * FROM exames WHERE id = %s", (id_exame,))
exame_data = db_cursor.fetchone()
if not exame_data:
return False
exame = ExameLaboratorial(*exame_data[1:])
exame._ExameLaboratorial__id = exame_data[0]
registro = RegistroExameLaboratorial(exame, paciente, datetime.now().strftime('%d/%m/%Y %H:%M:%S'))
self.__fila.inserir(registro)
return True
def chamar_paciente(self):
registro_chamado = self.__fila.remover()
if self.__em_coleta.inserir(registro_chamado): return registro_chamado
else: return False
def dar_alta(self, cpf):
registro_alta = self.__em_coleta.remover(cpf)
if self.__coletados.inserir(registro_alta): return registro_alta
else: return False
def exibir_fila_de_espera(self):
return str(self.__fila)
def exibir_exames_coletados(self):
return str(self.__coletados)
def exibir_exames_em_coleta(self):
return str(self.__em_coleta)
def exibir_quadro_geral(self):
return 'Fila de Espera\n' + str(self.__fila) + '\n\nExames em Coleta\n' + str(self.__em_coleta) + '\n\nExames Coletados\n' + str(self.__coletados)