Module bd
[hide private]
[frames] | no frames]

Source Code for Module bd

  1  #-*- coding: iso-8859-15 -*- 
  2   
  3  """ 
  4  [bd.py] 
  5  Contiene la clase BD que tiene metodos que crean, editan y consultan la base de 
  6  datos tiempos.dat,  y tambien contiene la clase statistic que aprovechando 
  7  los métodos de la clase anterior, hace estadisticas con los datos y consultas. 
  8  """ 
  9   
 10  __author__ = "Victor Ramirez de la Corte" 
 11  __date__ = "02/04/2009" 
 12  __version__ = "PyRubik v0.5" 
 13   
 14  import sqlite3 as dbapi 
 15  import time 
 16  from language import * 
 17  import os, sys 
 18  import pygame 
 19   
20 -class BD:
21 """Creamos y definimos funciones para guardar y utilizar la base de datos"""
22 - def __init__(self):
23 self.BD = dbapi.connect("Tiempos.dat") 24 #creamos el cursor 25 self.c = self.BD.cursor() 26 #Creamos una tabla tiempos 27 self.c.execute("""create table if not exists tiempos(id int PRIMARY KEY,\ 28 tipo FLOAT, tiempo FLOAT, fecha Timestamp)""") #time(2) cent. de seg. --> hh:mm:ss.nn
29 30
31 - def guardar(self, tiempo, tipo):
32 """guarda un tiempo en la base de datos, pasándole 33 como parámetros un tiempo y el tipo de cubo""" 34 #añadir un nuevo tiempo detrás de los demás #TODO try id sea autonumérico 35 self.c.execute('select * from tiempos') 36 cont = len(self.c.fetchall()) + 1 37 #fecha actual 38 date = time.time() 39 40 #insertamos datos dentro de la tabla 41 self.c.execute('insert into tiempos values (%d,"%s","%f","%s")' % (cont,tipo,tiempo,date)) 42 43 #hacemos el commit para que todo lo anterior se ejecute de verdad :P 44 self.BD.commit()
45 46 47 ######### CONSULTAS #############
48 - def consultarTodo(self):
49 """Consulta la base de datos completa""" 50 self.c.execute('select * from tiempos') 51 todo = "" 52 for fila in self.c: 53 Min = fila[2]/60 54 Seg = fila[2]%60 55 #tiempo 56 tiempo = lll.time + "%02d:%05.2f" %(Min, Seg) 57 #fecha 58 fecha = lll.date + time.strftime('%H:%M:%S, %d-%b-%Y', time.localtime(fila[3])) 59 todo += "%s %40s" % (tiempo, fecha) + "\n" 60 return todo
61 62
63 - def consultarMediaDe(self,x = 5):
64 """Consultar la media de los x ultimos números""" 65 self.c.execute('select * from tiempos') 66 if x > len(self.c.fetchall()): 67 print lll.error5 68 cont = x 69 total = 0 70 self.c.execute('select * from tiempos order by id DESC') #ordenar inversamente 71 for tupla in self.c.fetchall(): 72 total += tupla[2] 73 cont -=1 74 if cont < 0: break 75 76 #hacemos la media y creamos la cadena "00:00.00" 77 total = total/x 78 Min = total/60 79 Seg = total%60 80 return lll.avg + "%d: %02d:%05.2f" %(x, Min, Seg)
81
82 - def consultarRecord(self):
83 """Consulta el mejor tiempo""" 84 #ordenar ASC, DESC 85 self.c.execute('select * from tiempos order by tiempo ASC') 86 for tupla in self.c.fetchall(): 87 tiempo = tupla[2] 88 break 89 Min = tiempo/60 90 Seg = tiempo%60 91 return lll.record + "%02d:%05.2f" %(Min, Seg)
92
93 - def consultarTiemposDe(self, tipo = "3x3x3"):
94 """Consulta los tiempos del tipo de cubo que le des en la entrada""" 95 self.c.execute("""select * from tiempos where tipo='%s'""" % tipo) 96 todo = "" 97 for fila in self.c: 98 Min = fila[2]/60 99 Seg = fila[2]%60 100 #tiempo 101 tiempo = lll.time + "%02d:%05.2f" %(Min, Seg) 102 #fecha 103 fecha = lll.date + time.strftime('%H:%M:%S, %d-%b-%Y', time.localtime(fila[3])) 104 todo += "%s %40s" % (tiempo, fecha) + "\n" 105 return todo
106 107
108 - def close(self):
109 """cerramos el cursor y la base de datos""" 110 self.c.close() 111 self.BD.close()
112