Problema con Shelves

Francesc Alted falted en openlc.org
Vie Jun 6 20:20:16 CEST 2003


A Divendres 06 Juny 2003 19:12, luis miguel morillas va escriure:
> Asunto: [Python-es]
> 	Fecha: vie, jun 06, 2003 at 08:56:04 -0400
>
> Citando a  Juan Pablo Villalobos Fernandez,DireccionSuperior,Reduc 
(jpvilla en thor.uc.edu.ve):
> > e['entra'].append(valor introducido por algun usuario).No se si es que
> > estoy tratando de agregar valores como si fuera una lista. El hecho es
> > que "e['entra'] ya tiene un valor predeterminado, pero quiero que sea
> > posible asignarle otros valores sin eliminar el que ya existe.

Veo que quieres agregar valores a una lista en disco. Si tus datos van a
crecer mucho, shelve se vuelve muy ineficiente. Tal vez te interesaria dar
un vistazo al paquete pytables (http://pytables.sf.net), que te permite
trabajar con muchos más datos sin que decaiga la velocidad. Sin embargo, el
paquete tiene todavia alguna limitación, siendo la más importante es que no
se pueden borrar registros una vez introducidos (aunque estoy trabajando en
ello para solucionarlo).

Aqui tienes un ejemplo de uso, por si te apetece ver como funciona:

--
from tables import *

class Particle(IsDescription):
    name        = Col("CharType", shape=16)  # 16-character String
    lati        = Col("Int32", shape=1)      # integer
    longi       = Col("Int32", shape=1)      # integer
    pressure    = Col("Float32", shape=1)    # float  (single-precision)
    temperature = Col("Float64", shape=1)    # double (double-precision)

# Open a file in "w"rite mode
fileh = openFile("table-simple.h5", mode = "w")

# Create a new table in root
table = fileh.createTable(fileh.root, 'table', Particle, "Title example")
particle = table.row

# Fill the table with 10 particles
for i in xrange(10):
    # First, assign the values to the Particle record
    particle['name']  = 'Particle: %6d' % (i)
    particle['lati'] = i
    particle['longi'] = 10 - i
    particle['pressure'] = float(i*i)
    particle['temperature'] = float(i**2)
    # This injects the row values.
    particle.append()

# We need to flush the buffers in table in order to get an
# accurate number of records on it.
table.flush()

print "Table contents:"
for row in table.iterrows():
    print row

# Finally, close the file
fileh.close()
--


Saludos,

-- 
Francesc Alted




Más información sobre la lista de distribución Python-es