getting values from cache

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Jan 27 12:45:14 EST 2008


En Sat, 26 Jan 2008 05:21:52 -0200, nodrogbrown <nodrogbrown at gmail.com>  
escribi�:

> 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

I'd try to avoid duplicating the calculation code.

   get newfilenameslist
   update_cache = True
   try:
     f=open(cachefile)
   except IOError:
     pass
   else:
     read oldfilenameslist
     f.close()
     if oldfilenameslist==newfilenameslist:
       update_cache = False
   if update_cache:
     do calculations
     write file

Also, if you split the data in two, you can just read the filename list  
alone:

pickle.dump(filenameslist, f2)
pickle.dump((imgwdth,imght,mynumpymatrix1),f2)

-- 
Gabriel Genellina




More information about the Python-list mailing list