From 69eb790eba403e83c0f500e225557be4dff2c246 Mon Sep 17 00:00:00 2001 From: Angel Escobar <111505818+angesc2022@users.noreply.github.com> Date: Sat, 24 Jun 2023 12:50:43 -0300 Subject: [PATCH 1/3] =?UTF-8?q?Crear=20el=20m=C3=A9todo:=20agregar=20c?= =?UTF-8?q?=C3=B3digo=20al=20m=C3=A9todo=20obtener=20Conexi=C3=B3n=20(Vide?= =?UTF-8?q?o=204).=20=20Crear=20los=20objetos=20de=20conexi=C3=B3n=20en=20?= =?UTF-8?q?el=20m=C3=A9todo=20principal=20(Video=205).=20=20Commitear=20to?= =?UTF-8?q?do,=20crear=20tag:=20python02=20y=20realizar=20push.=20=20Hacer?= =?UTF-8?q?=20Pull=20Recuest=20a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__pycache__/logger_base.cpython-310.pyc | Bin 0 -> 663 bytes Python/Semana 9/conexion.py | 59 ++++++++++-------- 2 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 Python/Semana 9/__pycache__/logger_base.cpython-310.pyc diff --git a/Python/Semana 9/__pycache__/logger_base.cpython-310.pyc b/Python/Semana 9/__pycache__/logger_base.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9dcdba16275eb70f83d325bb8b945d67aad29840 GIT binary patch literal 663 zcmZWl%Wl*#6pb@^xXDacAXuSnB25#>CY~~rb-cK9;%`}W31NnEkNShQJCERY7qk*?tC`YL#(>o-4LvhHRW`R_W0fwdEDC~pZf#i zWI0AO{fQHex82il&UDs_3L?D-=OtW0DcQh+kKsHQCET*%Oh}M*q(24WyaKIRfwww8 ze6t8o7vVApn{Gd24NEy|YkgSOMP&8%*s*u=T2-u#T|9%lYI}OlBriefo~CKVgiO=! z?kPykE+Alm6sRNMu(~X|JG*5e^SZmYTm8b66teig3n*2q?*8umOo>)xtc<3yi&_hO z+fNY0{)%al9oI4!MQ@+HI68Y3`!CUn*SC`6*|Jr@sx2KpFnE-vy*&! Date: Mon, 26 Jun 2023 22:37:31 -0300 Subject: [PATCH 2/3] Arreglo carpeta semana 9 --- Python/Semana 9/conexion.py | 59 +++++++++++++++------------------- Python/Semana 9/persona_dao.py | 2 +- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/Python/Semana 9/conexion.py b/Python/Semana 9/conexion.py index e7016e9..940f476 100644 --- a/Python/Semana 9/conexion.py +++ b/Python/Semana 9/conexion.py @@ -1,56 +1,49 @@ -from psycopg2 import pool +import psycopg2 as bd # psycopg2 as db otra manera de importar el psycopg2 from logger_base import log import sys + class Conexion: _DATABASE = 'test_bd' _USERNAME = 'postgres' _PASSWORD = 'admin' _BD_PORT = '5432' _HOST = '127.0.0.1' - _MIN_CON = 1 - _MAX_CON = 5 - _Pool = None - + _conexion = None + _cursor = None @classmethod def obtenerConexion(cls): - conexion = cls.obtenerPool().getconn() - log.debug(f'Conexion obtenida del pool: {conexion}') - return conexion - + if cls._conexion is None: + try: + cls._conexion = bd.connect(host=cls._HOST, + user=cls._USERNAME, + password=cls._PASSWORD, + port=cls._BD_PORT, + database=cls._DATABASE) + log.debug(f'Conexion Exitosa: {cls._conexion}') + return cls._conexion + except Exception as e: + log.error(f'Ocurrio un error: {e}') + sys.exit() + else: + return cls._conexion @classmethod def obtenerCursor(cls): - pass - - @classmethod - def obtenerPool(cls): - if cls._pool is None: + if cls._cursor is None: try: - cls._pool = pool._simpleConnectionPool(cls._MIN_CON, - cls._MAX_CON, - host=cls._HOST, - User=cls._USERNAME, - password=cls._PASSWORD, - port=cls._BD_PORT, - database=cls._DATABASE) - log.debug(f'creacion del pool exitosa: {cls._pool}') - return cls._pool + cls._cursor = cls.obtenerConexion().cursor() + log.debug(f'Se abrio correctamente el cursor: {cls._cursor}') + return cls._cursor except Exception as e: - log.error(f'Ocurrio un error al obtener el pool: {e}') + log.error(f'Ocurrio un error: {e}') sys.exit() else: - return cls._pool - + return cls._cursor if __name__ == ' __main__ ': - conexion1 = Conexion.obtenerConexion() - conexion2 = Conexion.obtenerConexion() - conexion3 = Conexion.obtenerConexion() - conexion4 = Conexion.obtenerConexion() - conexion5 = Conexion.obtenerConexion() - - + Conexion.obtenerConexion() + Conexion.obtenerCursor() diff --git a/Python/Semana 9/persona_dao.py b/Python/Semana 9/persona_dao.py index 245e9bc..df2c5fd 100644 --- a/Python/Semana 9/persona_dao.py +++ b/Python/Semana 9/persona_dao.py @@ -2,7 +2,6 @@ from conexion import Conexion from logger_base import log - class personaDAO: """" DAO significa: Data Access Object @@ -75,3 +74,4 @@ def eliminar(cls, persona): personas = personaDAO.seleccionar() for persona in personas: log.debug(persona) + \ No newline at end of file From 8f22b0dd4350a5a647fc800b85785e42b59b2c80 Mon Sep 17 00:00:00 2001 From: lokywolf2295 Date: Mon, 26 Jun 2023 22:38:34 -0300 Subject: [PATCH 3/3] Update(Python): fix errors and format files --- Python/Semana 10/conexion.py | 31 +++++++++++++++++++------------ Python/Semana 10/persona_dao.py | 30 +++++++++++++++--------------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Python/Semana 10/conexion.py b/Python/Semana 10/conexion.py index 527bced..1caf225 100644 --- a/Python/Semana 10/conexion.py +++ b/Python/Semana 10/conexion.py @@ -3,6 +3,7 @@ from logger_base import log import sys + class Conexion: _DATABASE = 'test_bd' _USERNAME = 'postgres' @@ -15,9 +16,9 @@ class Conexion: @classmethod def obtenerConexion(cls): - pass - - + conexion = cls.obtenerPool().getconn() + log.debug(f'Conexion obtenida del pool: {conexion}') + return conexion @classmethod def obtenerCursor(cls): @@ -28,18 +29,24 @@ def obtenerPool(cls): if cls._pool is None: try: cls._pool = pool.SimpleConnectionPool(cls._MIN_CON, - cls._MAX_CON, - host=cls._HOST, - user=cls._USERNAME, - password=cls._PASSWORD, - port=cls._DB_PORT, - database=cls._DATABASE) - log.debug(f'creación del pool exitosa: {cls._pool}') + cls._MAX_CON, + host=cls._HOST, + user=cls._USERNAME, + password=cls._PASSWORD, + port=cls._BD_PORT, + database=cls._DATABASE) + log.debug(f'creacion del pool exitosa: {cls._pool}') + return cls._pool except Exception as e: - log.error(f'Ocurrió un error al obtener el pool: {e}') + log.error(f'Ocurrio un error al obtener el pool: {e}') sys.exit() else: return cls._pool + if __name__ == ' __main__ ': - pass + conexion1 = Conexion.obtenerConexion() + conexion2 = Conexion.obtenerConexion() + conexion3 = Conexion.obtenerConexion() + conexion4 = Conexion.obtenerConexion() + conexion5 = Conexion.obtenerConexion() diff --git a/Python/Semana 10/persona_dao.py b/Python/Semana 10/persona_dao.py index 245e9bc..3206283 100644 --- a/Python/Semana 10/persona_dao.py +++ b/Python/Semana 10/persona_dao.py @@ -1,5 +1,5 @@ -from Persona import Persona -from conexion import Conexion +from Persona import * +from conexion import * from logger_base import log @@ -12,8 +12,8 @@ class personaDAO: Update ->Actualizar Delete -> Eliminar """ - _SELECCIONAR = 'SELECT * FROM persona ORDER BY id_Personar' - _INSERTAR = 'INSERT INTO persona(nombre, apellido, email) VALUES (%s, %s, %S)' + _SELECCIONAR = 'SELECT * FROM persona ORDER BY id_persona' + _INSERTAR = 'INSERT INTO persona(nombre, apellido, email) VALUES (%s, %s, %s)' _ACTUALIZAR = 'UPDATE persona SET nombre=%s, apellido=%s, email=%s WHERE id_persona=%s' _ELIMINAR = 'DELETE FROM persona WHERE id_persona=%s' @@ -60,18 +60,18 @@ def eliminar(cls, persona): if __name__ == '__main__': # Eliminar un registro - persona1 = Persona(id_persona=8) + # persona1 = Persona(id_persona=8) # Actualizar un registro -# persona1 = Persona(1, 'Juan jose', 'pena', 'jjpena@mail.com') -# personas_actualizadas = personaDAO.actualizar(persona1) -# log.debug(f'Personas actualizadas: {personas_actualizadas}') + # persona1 = Persona(1, 'Juan jose', 'pena', 'jjpena@mail.com') + # personas_actualizadas = personaDAO.actualizar(persona1) + # log.debug(f'Personas actualizadas: {personas_actualizadas}') -# Insertar un registro -# persona1 = Persona(nombre = 'Omero', apellido= 'Ramos',email= 'omeror@mail.com') -# personas_insertadas = personaDAO.insertar(persona1) -# log.debug(f'Personas insertadas: {personas_insertadas}') + # Insertar un registro + # persona1 = Persona(nombre = 'Omero', apellido= 'Ramos',email= 'omeror@mail.com') + # personas_insertadas = personaDAO.insertar(persona1) + # log.debug(f'Personas insertadas: {personas_insertadas}') # Seleccionamos objetos -personas = personaDAO.seleccionar() -for persona in personas: - log.debug(persona) + personas = personaDAO.seleccionar() + for persona in personas: + log.debug(persona)