Skip to content

Commit

Permalink
Merge pull request #193 from CodeSystem2022/Semana-10
Browse files Browse the repository at this point in the history
Crear el método: agregar código al método obtener Conexión (Video 4).…
  • Loading branch information
lokywolf2295 authored Jun 27, 2023
2 parents 18acec8 + ed4af04 commit 7792690
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
24 changes: 10 additions & 14 deletions Python/Semana 10/conexion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from logger_base import log
import sys


class Conexion:
_DATABASE = 'test_bd'
_USERNAME = 'postgres'
Expand All @@ -11,16 +12,14 @@ class Conexion:
_HOST = '127.0.0.1'
_MIN_CON = 1
_MAX_CON = 5
_Pool = None

_pool = None

@classmethod
def obtenerConexion(cls):
conexion = cls.obtenerPool().getconn()
log.debug(f'Conexion obtenida del pool: {conexion}')
return conexion


@classmethod
def obtenerCursor(cls):
pass
Expand All @@ -29,13 +28,13 @@ def obtenerCursor(cls):
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._BD_PORT,
database=cls._DATABASE)
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
except Exception as e:
Expand All @@ -45,12 +44,9 @@ def obtenerPool(cls):
return cls._pool



if __name__ == ' __main__ ':
conexion1 = Conexion.obtenerConexion()
conexion2 = Conexion.obtenerConexion()
conexion3 = Conexion.obtenerConexion()
conexion4 = Conexion.obtenerConexion()
conexion5 = Conexion.obtenerConexion()


conexion5 = Conexion.obtenerConexion()
30 changes: 15 additions & 15 deletions Python/Semana 10/persona_dao.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from Persona import Persona
from conexion import Conexion
from Persona import *
from conexion import *
from logger_base import log


Expand All @@ -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'

Expand Down Expand Up @@ -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)
Binary file not shown.
16 changes: 8 additions & 8 deletions Python/Semana 9/conexion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from logger_base import log
import sys


class Conexion:
_DATABASE = 'test_bd'
_USERNAME = 'postgres'
Expand All @@ -16,11 +17,11 @@ class Conexion:
def obtenerConexion(cls):
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)
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:
Expand All @@ -29,8 +30,6 @@ def obtenerConexion(cls):
else:
return cls._conexion



@classmethod
def obtenerCursor(cls):
if cls._cursor is None:
Expand All @@ -42,7 +41,8 @@ def obtenerCursor(cls):
log.error(f'Ocurrio un error: {e}')
sys.exit()
else:
return cls._cursor
return cls._cursor


if __name__ == ' __main__ ':
Conexion.obtenerConexion()
Expand Down
2 changes: 1 addition & 1 deletion Python/Semana 9/persona_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from conexion import Conexion
from logger_base import log


class personaDAO:
""""
DAO significa: Data Access Object
Expand Down Expand Up @@ -75,3 +74,4 @@ def eliminar(cls, persona):
personas = personaDAO.seleccionar()
for persona in personas:
log.debug(persona)

0 comments on commit 7792690

Please sign in to comment.