On 01/11/2015 06:47 AM, Ganesh Pal wrote:
Hello Team,

Iam trying to generate a file which should have the  contents in the
below format

# cat  /root/schedule.conf

Sunday 10:20 10:30
Sunday 10:30 10:40
Sunday 10:50  10:60


I have to build the above format on the local linux machine using
python 2.7 version and then copy the file to the remote machine.

Here is the progress and few questions on the same ,

Program :

node-1# cat time_range.py
def yield_times():
     from datetime import date, time, datetime, timedelta
     start = datetime.combine(date.today(), time(23, 50))
     yield start.strftime("%A %H:%M")
     while True:
         start += timedelta(minutes=10)
         yield start.strftime("%A %H:%M")
gen = yield_times()
for ii in range(05):
      print gen.next()

node-1# python time_range.py
Sunday 23:50
Monday 00:00
Monday 00:10
Monday 00:20
Monday 00:30

(a)  How do I modify the output to have an another column with a
difference of 5 mins

Example :

node-1# python time_range.py

Sunday 23:50 23:05
Monday 00:00 00:05
Monday 00:10 00:05
Monday 00:20 00:15
Monday 00:30 00: 20

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.

and in the other loop, you want
    res = "{} {} {}\n".format(gen.next)
    print res,


(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.


(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.


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.

     from datetime import date, time, datetime, timedelta
     start = datetime.combine(date.today(), time(int(h),int(m))) /*
converting string to int)

Got some C code in there?  Use # for comments.

You don't seem to be honoring the 'd' value from the other machine.

     yield start.strftime("%A %H:%M")
     while True:
         start += timedelta(minutes=10)
         yield start.strftime("%A %H:%M")
gen = yield_times()
for ii in range(15):
      print gen.next()


Regards,
Ganesh



--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to