Thanks to all and dear Dave Angel, I got this working :) , using your valuable inputs .
Here is the final program: Throttling-1# cat sleep_schedule_file.py #!/usr/bin/python import time import pdb from datetime import date, time, datetime, timedelta #'''run these on the remote commmand ''' n = datetime.now() h = n.strftime("%H") m = n.strftime("%M") d = date.today() #''' get the sleep_schedule_structure ''' def yield_times(): start = datetime.combine(d, time(int(h),int(m))) end = datetime.combine(d, time(int(h),int(m))) while True: start += timedelta(minutes=4) end = start + timedelta(minutes=2) yield(start.strftime("%A"),start.strftime("%H:%M"),end.strftime("%H:%M")) gen = yield_times() f = open('/tmp/sleep_sched.conf','wb') for ii in range(10): a,b,c = gen.next() res = "{0} {1} {2} \n".format(a,b,c) print res, f.write(res) f.close() Gpal On Mon, Jan 12, 2015 at 3:23 PM, Ganesh Pal <ganesh1...@gmail.com> wrote: > On Mon, Jan 12, 2015 at 8:42 AM, Ganesh Pal <ganesh1...@gmail.com> wrote: >> On Sun, Jan 11, 2015 at 7:57 PM, Dave Angel <da...@davea.name> wrote: >>>> >>>> >>> No idea how that represents "a difference of 5 minutes". So I'll take a >>> totally wild guess that you meant: >>> >>> Sunday 23:50 23:55 >>> Monday 00:00 00:05 >>> Monday 00:10 00:15 >>> Monday 00:20 00:25 >>> Monday 00:30 00:35 >>> >>> which would have the 2nd column 5 minutes after the first. >>> >>> You need another datetime object, let's call it 'end' >>> >>> end = start + timedelta(minutes=5) >>> >>> and now you want to yield three things, in a tuple: >>> >>> yield (start.strftime("%A"), start.strftime("%H:%M"), >>> end.strftime("%H:%M")) >>> >>> Of course that's a pain to do twice, as you have two yields in that >>> function. I'll leave you to reorder the loop to make that unnecessary. >>> Hint, do the yield first, THEN increment the start variable. >> >> Thanks for the inputs , I tried doing the increments and decrements >> first and then Yield >> >> Program: >> >> Login-1@SNAP-BOX-1 new]$ cat time_range_01.py >> #!/usr/bin/python >> import time >> from datetime import date, time, datetime, timedelta >> #h = datetime.strftime("%H") >> #m = datetime.strftime("%M") >> h=23 >> m=50 >> d = date.today() >> print d >> >> def yield_times(): >> global h,m,d >> start = datetime.combine(d, time(int(h),int(m))) >> end = datetime.combine(d, time(int(h),int(m))) >> while True: >> start += timedelta(minutes=10) >> end = start + timedelta(minutes=5) >> >> yield(start.strftime("%A"),start.strftime("%H:%M"),end.strftime("%H:%M")) >> gen = yield_times() >> for i in range(10): >> print gen.next() >> >> output : >> [Login-1@SNAP-BOX-1 new]$ python time_range_01.py >> 2015-01-12 >> ('Tuesday', '00:00', '00:05') >> ('Tuesday', '00:10', '00:15') >> ('Tuesday', '00:20', '00:25') >> ('Tuesday', '00:30', '00:35') >> ('Tuesday', '00:40', '00:45') >> ('Tuesday', '00:50', '00:55') >> ('Tuesday', '01:00', '01:05') >> ('Tuesday', '01:10', '01:15') >> ('Tuesday', '01:20', '01:25') >> ('Tuesday', '01:30', '01:35') >> >> PS : Except formatting the results looks pretty much what i was >> expecting .I was pretty much happy to see this last night :) . >> >> >>> and in the other loop, you want >>> res = "{} {} {}\n".format(gen.next) >>> print res, >>> >>>> >> >> Thanks , I will have to modify for the new output >> >>>> (b) how to copy the above output (i.e with the new column to a file.) >>>> >>> >>> Instead of printing, just do f.write(res). Or do both. Notice that to make >>> this easy, i avoided doing any formatting in the print statement. I used a >>> trailing comma on it to avoid print adding a newline, and instead put it >>> explicitly in the format method call. >>> >>>> >> >> Sure >> >>>> (c) The final output should be a file , having the entries day , >>>> start , end time of the remote file. how do i pass the this to >>>> yeild-times() >>>> >>>> import time >>>> >>>> /* Assuming I retrive the below values h,m,d from the remote machine*/ >>>> >>>> h = time.strftime("%H") >>>> m = time.strftime("%M") >>>> d = date.today() >>>> >>> >>> Those first two are strings, but the third is a datetime object. That last >>> can be painful to pass between machines. >> >> Correct , also looks like I have another issue , if I happen to pass >> the first two strings >> >> # instead hard coded value i.e h = 23 and y =50 , i try using the >> string directly I end up getting the below error : >> >> h = datetime.strftime("%H") >> m = datetime.strftime("%M") >> >> [Login-1@SNAP-BOX-1 new]$ python time_range_01.py >> Traceback (most recent call last): >> File "time_range_01.py", line 4, in <module> >> h = datetime.strftime("%H") >> TypeError: descriptor 'strftime' requires a 'datetime.date' object but >> received a 'str' >> >> The date time object is retrieved once once from the remote machine >> and just for building the file in the required format. >> Do you foresee any problems using this ? >> >>> >>>> >>>> def yield_times(): >>>> global h,m,d >>> >>> >>> No idea what that's all about. If you want to pass arguments to >>> yield_times(), put them inside the parens. >>> >> sure . >> >> Regards, >> Gpal -- https://mail.python.org/mailman/listinfo/python-list