[Tutor] Troubles with Python modules
Alberto Troiano
albertito_g at hotmail.com
Tue May 17 15:36:14 CEST 2005
Hey
I'm working on Python 2.2 over Linux REd Hat 9.0 and here is the code I have
import os
import sys
import MySQLdb
import datetime
import time
class conexion(object):
def __init__(self):
self.db = MySQLdb.connect(host="localhost", user="administrador",
passwd="123456",db="seguridad")
def consulta(self,query,args=None):
try:
self.cursor=self.db.cursor()
self.sql=self.cursor.execute(query,args)
except:
self.cerrar()
else:
self.cerrar()
def cerrar(self):
self.db.close()
def getpath():
global ruta
global user
global cam
try:
i=0
for arg in sys.argv:
if i==1:
ruta+=arg
elif i==2:
user=arg
elif i==3:
cam=arg
else:
pass
i+=1
except:
f=open("cotascamonerrors.log","a+")
f.write("ERROR ------ No se pudo encontrar el path "+ruta+".
Contacte al administrador.")
f.close()
def getHoursMinutes(ts=None):
if ts:
t = time.strptime(ts, '%H:%M')
else:
t = time.localtime()
return (t.tm_hour, t.tm_min)
global ruta
global user
global cam
ruta="/var/www/html/home/"
getpath()
os.chdir(ruta)
os.system("mkdir grabacion")
days={1:"Lunes",2:"Martes",3:"Miercoles",4:"Jueves",5:"Viernes",6:"Sabado",7:"Domingo"}
while 1:
hoy=datetime.datetime.now()
dia=days[hoy.isoweekday()]
query="SELECT %s, Limite from camara where CodigoCamara=%s"
args=(dia,cam)
cur=conexion()
cur.consulta(query,args)
res=cur.cursor.fetchall()
for i in res:
horag=i[0]
limite=i[1]
horai1=horag[0:5]
horaf1=horag[6:11]
horai2=horag[12:17]
horaf2=horag[18:23]
if getHoursMinutes(horai1) <= getHoursMinutes() <=
getHoursMinutes(horaf1):
cur=conexion()
query="SELECT count(*) from imagen where CodigoCamara=%s and
Usuario=%s"
args(cam,user)
cur.consulta(query,args)
j=cur.cursor.fetchall()
for k in j:
actual=k[0]
if actual < limite:
cur=conexion()
grupo=datetime.datetime.today().strftime("%Y%m%d%H%M%S")
try:
os.system("cp webcam.jpg grabacion/cam"+grupo+".jpg")
query="INSERT INTO imagen values (%s,%s,%s,NOW(),NOW(),%s)
where CodigoCamara=%s and Usuario=%s"
grupo1=datetime.datetime.today().strftime("%Y%m%d%H%M")
ruta1=ruta+"/grabacion/cam"+grupo+".jpg"
args(user,cam,ruta1,grupo1)
cur.consulta(query,args)
except:
pass
else:
f=open("cotascamonerrors.log","a+")
f.write("ERROR ------ El usuario "+user+" agoto su espacio de
almacenamiento.")
f.close()
elif getHoursMinutes(horai2) <= getHoursMinutes() <=
getHoursMinutes(horaf2):
cur=conexion()
query="SELECT count(*) from imagen where CodigoCamara=%s and
Usuario=%s"
args(cam,user)
cur.consulta(query,args)
j=cur.cursor.fetchall()
for k in j:
actual=k[0]
if actual < limite:
cur=conexion()
grupo=datetime.datetime.today().strftime("%Y%m%d%H%M%S")
try:
os.system("cp webcam.jpg grabacion/cam"+grupo+".jpg")
query="INSERT INTO imagen values
(%s,%s,%s,NOW(),NOW(),%s)where CodigoCamara=%s and Usuario=%s"
grupo1=datetime.datetime.today().strftime("%Y%m%d%H%M")
ruta1=ruta+"/grabacion/cam"+grupo+".jpg"
args(user,cam,ruta1,grupo1)
cur.consulta(query,args)
except:
pass
else:
f=open("cotascamonerrors.log","a+")
f.write("ERROR ------ El usuario "+user+" agoto su espacio de
almacenamiento.")
f.close()
else:
pass
First I'd like to know if this code can be shorter or more efficient (if you
have the time)
Second the error is the following:
Traceback (most recent call last):
File "/root/cotascamon.py", line 4, in ?
import datetime
ImportError: No module named datetime
I think this module is in Python 2.3. What can I do??????
Then I have installed Python 2.3.4 in the same Linux but I can't use it
because it doesn't recognze the module MySQLdb
Here is the error
Traceback (most recent call last):
File "/root/cotascamon.py", line 3, in ?
import MySQLdb
File "/usr/local/lib/python2.3/site-packages/MySQLdb/__init__.py", line
27, in ?
import _mysql
ImportError: No module named _mysql
I have cross modules
Modules Python 2.2 ----------------- Python 2.3.4
MySQLdb YES NO
datetime NO YES
Heeeeeeeeeelp
Thanks in advanced
Alberto
More information about the Tutor
mailing list