getting values from cache

N

nodrogbrown

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
 
N

nodrogbrown

also
if i were to write unit test for this method ,how shd i do it? shd i
be checking all values in the matrix created inside and so on?

gordon
 
G

Gabriel Genellina

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top