Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'output': 0.05; 'represents': 0.05; 'retrieved': 0.05; '(b)': 0.07; 'column': 0.07; 'modify': 0.07; 'string': 0.09; 'arguments': 0.09; 'assuming': 0.09; 'formatting': 0.09; 'machines.': 0.09; 'things,': 0.09; 'trailing': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'comma': 0.16; 'decrements': 0.16; 'descriptor': 0.16; 'expecting': 0.16; 'i.e': 0.16; 'increment': 0.16; 'inputs': 0.16; 'newline,': 0.16; 'res': 0.16; 'statement.': 0.16; 'true:': 0.16; 'typeerror:': 0.16; 'unnecessary.': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'first.': 0.19; 'machine': 0.22; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; 'entries': 0.24; 'skip:{ 20': 0.24; 'mon,': 0.24; 'file.': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'correct': 0.29; 'leave': 0.29; '(c)': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'getting': 0.31; "skip:' 10": 0.31; 'about.': 0.31; 'coded': 0.31; 'easy,': 0.31; 'object.': 0.31; 'yields': 0.31; 'file': 0.32; 'another': 0.32; '(most': 0.33; 'call.': 0.33; 'guess': 0.33; 'skip:# 10': 0.33; 'totally': 0.33; 'third': 0.33; 'skip:d 20': 0.34; 'except': 0.35; 'monday': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'building': 0.35; 'format.': 0.36; 'object,': 0.36; 'yield': 0.36; 'doing': 0.36; 'method': 0.36; 'thanks': 0.36; "i'll": 0.36; 'should': 0.36; 'two': 0.37; 'remote': 0.38; 'problems': 0.38; 'skip:[ 10': 0.38; 'issue': 0.38; 'pm,': 0.38; 'recent': 0.39; '12,': 0.39; 'sure': 0.39; 'how': 0.40; '2nd': 0.60; 'dave': 0.60; 'new': 0.61; 'course': 0.61; 'first': 0.61; 'happen': 0.63; 'minutes': 0.67; 'between': 0.67; 'date,': 0.68; 'sunday': 0.68; 'results': 0.69; 'printing,': 0.74; 'day': 0.76; '2015': 0.84; 'pain': 0.84; 'angel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=JcwpFGev+CDSKpg+7ksHCg1O8VcSKY5yd5tHIVBKalg=; b=MsIQAdmA+dfGljR2rWInsm1g3dALHK81QyHHKLI3FUu2gVf56NiqDEAyXuVTO/6bub CCtbAcoTetHlZT10ZPF7lVMzf8gUF6gmoo0FRQEV5Re8w090tpyTv4uct2SOea7ViKqn 4INCQP/CWwNyO3XRMsl5CwLsBRzrOQgkC7VjNjq7RkoH8mjhx669YOJODR72LeFvktBH YaZar5Vxpy7axzom3iCpmbX1nt8UXdCViw1tzrwzGl3gVuamtYUUCj9vDnIUy6oRIN5A Z4m2CUBurAx95SI9O+KF/AVBe4NVcvH189HAykos9HTvoxADSaDUnwVWP5mqT2OoMGo1 XmCA== MIME-Version: 1.0 X-Received: by 10.112.166.73 with SMTP id ze9mr34896943lbb.38.1421056428374; Mon, 12 Jan 2015 01:53:48 -0800 (PST) In-Reply-To: References: <54B2884B.90301@davea.name> Date: Mon, 12 Jan 2015 15:23:48 +0530 Subject: Re: Few Coding suggestions - resending From: Ganesh Pal To: Dave Angel Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 144 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421056430 news.xs4all.nl 2881 [2001:888:2000:d::a6]:60845 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83600 On Mon, Jan 12, 2015 at 8:42 AM, Ganesh Pal wrote: > On Sun, Jan 11, 2015 at 7:57 PM, Dave Angel 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 =3D start + timedelta(minutes=3D5) >> >> 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 func= tion. 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 =3D datetime.strftime("%H") > #m =3D datetime.strftime("%M") > h=3D23 > m=3D50 > d =3D date.today() > print d > > def yield_times(): > global h,m,d > start =3D datetime.combine(d, time(int(h),int(m))) > end =3D datetime.combine(d, time(int(h),int(m))) > while True: > start +=3D timedelta(minutes=3D10) > end =3D start + timedelta(minutes=3D5) > yield(start.strftime("%A"),start.strftime("%H:%M"),end.strftime("= %H:%M")) > gen =3D 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 =3D "{} {} {}\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 m= ake this easy, i avoided doing any formatting in the print statement. I use= d a trailing comma on it to avoid print adding a newline, and instead put i= t 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 =3D time.strftime("%H") >>> m =3D time.strftime("%M") >>> d =3D date.today() >>> >> >> Those first two are strings, but the third is a datetime object. That l= ast 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 =3D 23 and y =3D50 , i try using the > string directly I end up getting the below error : > > h =3D datetime.strftime("%H") > m =3D 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 > h =3D 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_t= imes(), put them inside the parens. >> > sure . > > Regards, > Gpal