getting values from cache

nodrogbrown nodrogbrown at gmail.com
Sat Jan 26 02:21:52 EST 2008


hi
i am writing code to check a folder containing images and then process
thir vals using PIL and do some calc to create a matrix of values .if
the folder has any new imgs added the program will do all calc again
and dump the values into a cachefile.If the folder contents remain
unaltered the program should not do calc but load the vals from
cachefile..it is assumed that malicious alterations are not made on
the folder and so i wont be doing any thorogh check but just checking
if contents of folder have changed..i do something like this


def checkCache(self):
   filenameslist=getfilenameslist() # made by parsing folder before
this
   try:
      f=open(cachefile)

   except IOError:
	    #no cache found ,do all calc
        mynumpymatrix1,imgwdth,imght=docalculations()
        f2=open(cachefile,"w")
        #dump values as tuple
pickle.dump((filenameslist,imgwdth,imght,mynumpymatrix1),f2)
	    f2.close()
   else:
        #cache exists, need to check if folder contents changed
        oldfilenameslist,wd,ht, mynumpymatrix1=pickle.load(f)
        f.close()

        if(filenamelist==oldfilelist):
            #if oldfilenamelst same,it means folder hasn't changed
            #use the vals from cache.....
        else:
            #folder changed
            mynumpymatrix1,imgwdth,imght=docalculations()
            f3=open(cachefile,"w")
 
pickle.dump((filenameslist,imgwdth,imght,mynumpymatrix1),f3)
            f3.close()

 this works and does what i need in my code..but i want to know if a
more elegant solution is possible
 i am not worried about someone deliberately renaming files like
aaaa.jpeg to aaa.jped and a.jpeg to deceive the checking
 since it is assumed that noone has permission to modify the folder
except a trusted admin/code

 will be grateful for your suggestions
 tia



More information about the Python-list mailing list