Ver Mensaje Individual
  #3 (permalink)  
Antiguo 31/05/2010, 07:50
refreegrata
 
Fecha de Ingreso: agosto-2008
Mensajes: 198
Antigüedad: 15 años, 9 meses
Puntos: 27
Respuesta: duda con herencia y con decoradores(novato)

al final hice una clase intermedia, llamada sentencia, ubicada entre conexion, y las clases de las tablas. Pensé que no funcionaría, pero en realidad si sirve
Código PHP:
#!/usr/bin/env python
# -*- coding: utf-8 -*
import psycopg2
class conexion(object):
    
''' clase para conectar '''

    
def __init__(self):
        
self.__host 'localhost'
        
self.__dbname 'codificacion'
        
self.__user 'postgres'
        
self.__password '12345'
        
# self.__conexion
        # self.__cursor
#------------------------------------------------------------------------------------
#--------------------------------------CONEXION--------------------------------------
#------------------------------------------------------------------------------------
    
def conexionAbrir(self):
        
''' abrir conexion '''
        
try:
            
self.__conexion psycopg2.connect('host='+self.__host+' dbname='+self.__dbname+' user='+self.__user+' password='+self.__password)
        
except:
            return 
False
        
return True

    def conexionCerrar
(self):
        
''' cerrar conexion '''
        
try:
            
self.__conexion.close()
        
except:
            return 
False
        
return True

    def conexionCommit
(self):
        try:
            
self.__conexion.commit()
        
except:
            return 
False
        
return True

    def conexionRollback
(selfcommit True):
        try:
            
self.__conexion.rollback()
        
except:
            return 
False
        
return True
#------------------------------------------------------------------------------------
#---------------------------------------CURSOR----------------------------------------
#------------------------------------------------------------------------------------

    
def cursorAbrir(self):
        
''' crear cursor '''
        
try:
            
self.__cursor self.__conexion.cursor()
        
except:
            return 
False
        
return True

    def cursorCerrar
(self):
        
''' cerrar cursor '''
        
try:
            
self.__cursor.close()
        
except:
            return 
False
        
return True

    def cursorFetchall
(self):
        try:
            return 
self.__cursor.fetchall()
        
except:
            return 
False

    def cursorFetchmany
(selfdimension):
        try:
            return 
self.__cursor.fetchmany(dimension)
        
except:
            return 
False

    def cursorFetchone
(self):
        try:
            return 
self.__cursor.fetchone()
        
except:
            return 
False

    def getCursor
(self):
        return 
self.__cursor

    cursor 
property(getCursor)
    
    
def sentenciaSQL(selfinstruccion, *parametro):
        
''' ejecutar sentencia '''
        
try:
            
self.__cursor.execute(instruccionparametro)
        
except:
            return 
False
        
return True 
Código PHP:
#!/usr/bin/env python
# -*- coding: utf-8 -*
from dato.conexion import conexion
class sentencia(conexion):
    
''' clase del cursor '''

    
def __init__(self):
        
''' Constructor '''
        
conexion.__init__(self)
    
    
def sentencia(selff, *args, **kw_args):
        if 
conexion.conexionAbrir(self) and self.cursorAbrir():
            
error f(*args, **kw_args)
        else:
            
error True
        self
.cursorCerrar()
        
self.conexionCerrar()
        return 
error 
Código PHP:
#!/usr/bin/env python
# -*- coding: utf-8 -*
from dato.sentencia import sentencia
class tabla1(sentencia):
    
''' clase para conectar a tabla '''

    
def __init__(self):
        
sentencia.__init__(self)

    
def insert(self, *informacion):
        return 
self.sentencia(self.__insert,*informacion)
    
    
def __insert(self, *informacion):
        for 
i in informacion:
            
sentencia.sentenciaSQL(self,'insert into tabla1 (informacion) VALUES (%s)',i)
        return 
sentencia.conexionCommit(self
no creí ,que pasando la funcion de tabla1 a la clase intermedia pudiese encontrar el camino a tabla1, pero, increiblemente si lo hace.