The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by iri]

Hello again,
This time I'm asking help with a message of error that appears
constantly and before didn't.
The fact is that I've got a program (GENASSOCIATION.PY) that makes the
following:
From a file (FILE_COD_POSTAL_PROD) containing lines with fields separated with
# it takes one (one field from each line; sometimes this field will have
nothing) and generates the
file ASSOCIATION.INDEX with those fields.

This is GENASSOCIATION.PY:
--------------------------------------------------------------------------------------------------------------------

import sys,os,time,string,shelve

flog=open('f:LOGS1ASSOCIATION.LOG','w')
sys.stdout=flog
ferror=open('f:LOGS1ASSOCIATION.ERROR','w')
sys.stderr=ferror

t0=time.ctime(time.time())
print 'Begins GenAssociation.py
'
print t0

print 'INDEXING ASSOCIATION.........'

fparam=open('f:INDOC1FILE_COD_POSTAL_PROD','r')
tabla=shelve.open('association.index')
buffer=fparam.readline() #we skip the first line because is a title line

while 1:
buffer=fparam.readline()
if not buffer:break
campos=string.split(buffer,'#')
cVANTIVE=string.strip(campos[2])

if len(string.strip(campos[5]))>0:
association=campos[5]
tabla[cVANTIVE]=association
print cVANTIVE+' : '+association
else:
tabla[cVANTIVE]=string.strip('nulo')

fparam.close()
tabla.close()

t1=time.ctime(time.time())
print 'Ends GenAssociation.py
'
print t1

print '
'

------------------------------------------------------------------------------------------------------------------------------


The problem is that if this program runs ONCE this error appears:

Traceback (innermost last):
File "C:progsactualGENASSOCIATION.PY", line 29, in ?
tabla[cVANTIVE]=string.strip('nulo')
File "C:Archivos de programaPythonLibshelve.py", line 71, in __setitem__
self.dict[key] = f.getvalue()
bsddb.error: (0, 'Error')

If the program runs TWICE or more (and we do not delete the association.index
created before)
everything is OK.

I have another 6 programs like this that take another fields of that file
FILE_COD_POSTAL_PROD and make another *.index and got no problem. So, what is
happening?
I was told that sometimes the fact of having the fields empty could give
problems. I filled
those empty fields of FILE_COD_POSTAL_PROD with a space and... resolved nothing.
Could you help me with any hint?
Thank you!

Irantzu
import-bot (20211) [Avatar] Offline
#2
Re: Problems with an error
[Originally posted by d. harms]


Hi Irantzu,

I don't see what would be causing your problem as I don't know what value
cVANTIVE could have that would cause this.

The suggestion I can make though, would be to put the lines:

print "campos:",campos
print "cVANTIVE:",cVANTIVE

after your else (i.e. just before the line: tabla[cVANTIVE]='nulo')

From this you should be able to find out what input line in your data file
(i.e. FILE_COD_POSTAL_PROD) is causing the problem and see if there is
something wrong with it.

If there is nothing wrong with the record you may be running into a problem
where there is a hash collision as mentioned at the end of the following
paragraph (which is taken from the documentation on shelve.py in
Python22Doclibmodule-shelve.html)

Restrictions:

The choice of which database package will be used (e.g. dbm or gdbm) depends
on which interface is available. Therefore it is not safe to open the database
directly using dbm. The database is also (unfortunately) subject to the
limitations of dbm, if it is used -- this means that (the pickled
representation of) the objects stored in the database should be fairly small,
and in rare cases key collisions may cause the database to refuse updates.


Daryl