Page 1 of 1

If - not sure if working.

Posted: Wed Jul 03, 2013 2:35 pm
by txt3rob
i have a CSV file with times in there which i would like to run a command that if the file was last downloaded over 1hr ago it runs the codes i require and if it's younger than 1 hour old i want it to just do its normal thing.

Code: Select all

path = ("/tmp/trains.csv")
ftime = os.path.getmtime(path)
curtime = time.time()
difftime = curtime - ftime
if difftime > 3600:
	downloaddue()
	empty()
	importdue()
	sleep (10)
	display()

display()
sleep (2)
when running this code it just simply seem's to run the main check and then continue to do the download etc when i would like it to ignore it cause the file is younger than 1 hour old.

Re: If - not sure if working.

Posted: Wed Jul 03, 2013 6:20 pm
by DrMag
Looks alright to me; you can try debugging by scattering a handful of print statements into it:

Code: Select all

path = ("/tmp/trains.csv")
ftime = os.path.getmtime(path)
curtime = time.time()
difftime = curtime - ftime
if difftime > 3600:
   print("If new, I shouldn't print!")
   downloaddue()
   print("downloaddue() was run.")
   empty()
   print("empty() was run.")
   importdue()
   print("importdue() was run.")
   sleep (10)
   display()

display()
sleep (2)
If any of the print statements are displayed and you're certain the file being checked is less than an hour old, you know there's something wrong.

Re: If - not sure if working.

Posted: Wed Jul 03, 2013 6:49 pm
by rpdom
Firstly I don't know Python. I do know a bit about debugging code though.

How about a print/display/whatever statement before the "if" to show the values of curtime and ftime?

Re: If - not sure if working.

Posted: Wed Jul 03, 2013 10:34 pm
by DrMag
That would be helpful too. Let us know the results!

Re: If - not sure if working.

Posted: Thu Jul 04, 2013 9:28 am
by txt3rob
it seems that the file has no time stamp which i find weird so the above code although being correct inst working how i need it to.

the file downloads a csv from google docs and this has train times imported to it so now i need to work out a way to add a timestamp for the birth of the file on the pi so this function works.

i am not a python coder myself but giving this a good go.

hopefully once the project is complete ill upload it to github for people to use for themselves but they will need the adafruits 16x2 LCD plate

Re: If - not sure if working.

Posted: Thu Jul 04, 2013 9:32 am
by txt3rob

Code: Select all

root@raspberrypi:~/piticker# ./test.py
If new, I shouldn't print!
downloaddue() was run.
empty() was run.
importdue() was run.
This ran ok as the data was yesterdays so ill try again now to see where i get to.

time to try in about an 1hr as that's the setting i've set for it to grab new data.

Thanks for the help guys :) much appreciated

Re: If - not sure if working.

Posted: Thu Jul 04, 2013 10:32 am
by txt3rob
IT WORKS IT'S ALIVE!

Now to change print for some LCD messages.