def obtenerCursor(cls): if cls.__cursor is None: try: cls.__cursor = cls.__conexion.cursor() logger.info(f'Cursor exitoso: {cls.__cursor}') return cls.__cursor except Exception as e: logger.error(f'Cursor fallido: {e}') sys.exit() else: return cls.__cursor
def obtenerConexion(cls): if cls.__conexion is None: try: cls.__conexion = db.connect(database=cls.__DATABASE, user=cls.__USERNAME, password=cls.__PASSWORD, port=cls.__DB_PORT, host=cls.__HOST) logger.info(f'Conexión exitosa: {cls.__conexion}') return cls.__conexion except Exception as e: logger.error(f'Conexión fallida: {e}') sys.exit() cls.__conexion.close() else: return cls.__conexion
from usuario import Usuario opcion = None while opcion != '5': print('Opciones:') print('1. Listar usuarios:') print('2. Agregar usuario') print('3. Modificar usuario') print('4. Eliminar usuario') print('5. Salir') opcion = input('Escribe tu opción (1-5): ') if opcion == '1': # Listado de usuarios usuarios = UsuarioDao.seleccionar() for usuario in usuarios: logger.info(usuario) elif opcion == '2': # Insertamos un nuevo registro username_var = input('Escribe el username: '******'Escribe el password: '******'Usuarios insertados: {usuarios_insertadas}') elif opcion == '3': # Actualizar un registro existente id_usuario_var = int(input('Escribe el id_usuario a modificar: ')) username_var = input('Escribe el nuevo username: '******'Escribe el nuevo password: ') usuario = Usuario(id_usuario_var, username_var, password_var) usuarios_actualizadas = UsuarioDao.actualizar(usuario)
def get_id_persona(self): return self.__id_persona def get_nombre(self): return self.__nombre def get_apellido(self): return self.__apellido def get_email(self): return self.__email def set_id_persona(self, id_persona): self.__id_persona = id_persona def set_nombre(self, nombre): self.__nombre = nombre def set_apellido(self, apellido): self.__apellido = apellido def set_email(self, email): self.__email = email if __name__ == '__main__': # por convencion si no se pasan todos los parametros es mejor poner el nombre de la variable persona1 = Persona(nombre="Gustavo", apellido="Marquez", email="*****@*****.**") logger.info(persona1)
@classmethod def obtenerCursor(cls): if cls.__cursor is None: try: cls.__cursor = cls.obtenerConexion().cursor() logger.debug(f'Se abrio el cursor con éxito: {cls.__cursor}') return cls.__cursor except Exception as e: logger.error(f'Error al obtener cursor:{e}') sys.exit() else: return cls.__cursor @classmethod def cerrar(cls): if cls.__cursor is not None: try: cls.__cursor.close() except Exception as e: logger.error(f'Error al cerrar cursor: {e}') if cls.__conexion is not None: try: cls.__conexion.close() except Exception as e: logger.error(f'Error al cerrar conexión: {e}') logger.debug('Se han cerrado los objetos de conexión y cursor') if __name__ == '__main__': logger.info(Conexion.obtenerCursor()) Conexion.cerrar()
def packing(self, init_unpacked_items, box_type_to_code): """ Following for loop will iterate through all the input trucks. Iteration will stop as soon as the list of unpacked items become empty. Also creates two dictionaries. The first dictionary contains the information about all the trucks with packed_items, residual volume, box_types in it Second dictionary contains the information about unpacked items after the corresponding truck has been filled to its max. Parameters ---------- init_unpacked_items : The very first state of the unpacked items dictionary before loading the first truck box_type_to_code : Dictionary that converts box type (integer code) to the actual box code. Returns ------- per_truck_packed : Dictionary with key : truck name (TRUCK-1 for example) value : [packed_items, list of unique boxes packed, residual_volume of the truck] """ a = perf_counter() # Start the clock per_truck_packed = {} per_truck_unpacked = {} # Loop for every truck input for i in range(1, len(self.truck_size_dict) + 1): truck_name = f"TRUCK-{i}" # First truck needs explicit input of unpacked items dictionary. # Remaining trucks will eventually get from the packing of previous truck. if i == 1: try: ret_values = self.place_the_box( self.truck_size_dict[f"TRUCK-{i}"], init_unpacked_items) if ret_values == "Invalid box quantity": raise CustomException( "Packing aborted : There are no boxes to pack") except CustomException as error: logger.info(error.message) else: pkd, un_pkd, res_vol = ret_values else: if len(per_truck_unpacked[f"TRUCK-{i-1}"]) == 0: logger.info("No more boxes left to pack") elif self.truck_size_dict[truck_name] is not None: pkd, un_pkd, res_vol = self.place_the_box( self.truck_size_dict[f"TRUCK-{i}"], per_truck_unpacked[f"TRUCK-{i-1}"], ) else: logger.info( f"""There are {len(per_truck_unpacked[f"TRUCK-{i-1}"])} but no more trucks remaining""") per_truck_packed[truck_name] = [ pkd, self.make_dict_from_list( [pkd[f"box-{x}"][1] for x in range(1, len(pkd) + 1)], box_type_to_code, ), res_vol, ] logger.debug( f"\n>>>> packed {len(per_truck_packed[truck_name][0])} items " ) per_truck_unpacked[truck_name] = un_pkd logger.debug( f"\n>>>> There are {len(per_truck_unpacked[truck_name])} items \ not packed into {truck_name}") b = perf_counter() # Stop the clock logger.debug(f"\n>>>> Took {b-a:0.3f} seconds to complete packing") return per_truck_packed
try: cls.__cursor = cls.get_connected().cursor() logger.debug( f'the cursor was successfully opened: {cls.__cursor}') return cls.__cursor except Exception as e: logger.error(f'error when get to the Cursor: {e}') sys.exit() else: return cls.__cursor @classmethod def close(cls): if cls.__cursor is not None: try: cls.__cursor.close() except Exception as e: logger.error(f'Error to the close Cursor: {e}') if cls.__connection is not None: try: cls.__connection.close() except Exception as e: logger.error(f'Error to the close Connection: {e}') logger.debug('the connection and cursor objects have been closed') if __name__ == '__main__': logger.info(Connection.get_cursor()) Connection.close()
from logger_base import logger logger.info("Logger conexion test")
if cls.__cursor is None: try: cls.__cursor = cls.__conexion.cursor() logger.debug(f'Se ha establecido el cursor: {cls.__cursor}') except Exception as e: logger.error(f"Error al establecer el cursor {e}") sys.exit() else: return cls.__cursor @classmethod def cerrar(cls): if cls.__cursor is not None: try: cls.__cursor.close() logger.debug(f'Se ha cerrado el cursor: {cls.__cursor}') except Exception as e: logger.error(f'Error al cerrar el cursor {e}') sys.exit if cls.__conexion is not None: try: cls.__conexion.close() logger.debug(f'Se ha cerrado la conexion: {cls.__conexion}') except Exception as e: logger.error(f'Error al cerrar la conexión: {e}') logger.debug("Se han cerrado todos los objetos de conexion y cursor") if __name__ == '__main__': logger.info('desde conexion')