Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83600 > unrolled thread

Re: Few Coding suggestions - resending

Started byGanesh Pal <ganesh1pal@gmail.com>
First post2015-01-12 15:23 +0530
Last post2015-01-12 15:23 +0530
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Few Coding suggestions - resending Ganesh Pal <ganesh1pal@gmail.com> - 2015-01-12 15:23 +0530

#83600 — Re: Few Coding suggestions - resending

FromGanesh Pal <ganesh1pal@gmail.com>
Date2015-01-12 15:23 +0530
SubjectRe: Few Coding suggestions - resending
Message-ID<mailman.17610.1421056430.18130.python-list@python.org>
On Mon, Jan 12, 2015 at 8:42 AM, Ganesh Pal <ganesh1pal@gmail.com> wrote:
> On Sun, Jan 11, 2015 at 7:57 PM, Dave Angel <davea@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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web