txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

If - not sure if working.

Wed Jul 03, 2013 2:35 pm

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.
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

DrMag
Posts: 63
Joined: Sat Jun 01, 2013 7:59 pm

Re: If - not sure if working.

Wed Jul 03, 2013 6:20 pm

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.

User avatar
rpdom
Posts: 15605
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: If - not sure if working.

Wed Jul 03, 2013 6:49 pm

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?

DrMag
Posts: 63
Joined: Sat Jun 01, 2013 7:59 pm

Re: If - not sure if working.

Wed Jul 03, 2013 10:34 pm

That would be helpful too. Let us know the results!

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: If - not sure if working.

Thu Jul 04, 2013 9:28 am

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
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: If - not sure if working.

Thu Jul 04, 2013 9:32 am

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
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: If - not sure if working.

Thu Jul 04, 2013 10:32 am

IT WORKS IT'S ALIVE!

Now to change print for some LCD messages.
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

Return to “Python”