Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.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; 'f.close()': 0.09; 'formatting': 0.09; 'machines.': 0.09; 'skip:\\ 10': 0.09; 'things,': 0.09; 'trailing': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; "'''": 0.16; '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; 'pdb': 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; '>>>>': 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; 'skip:o 30': 0.61; 'new': 0.61; 'course': 0.61; 'first': 0.61; 'happen': 0.63; 'valuable': 0.63; 'skip:n 10': 0.64; 'dear': 0.65; 'here': 0.66; '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=C3NPxC/WQV/1NySZtekzzib4zxUu+Ffx4FPyDvaWRaM=; b=nd4ULW5/WftzQ9kpvu/2RHcrt//NO6ZKF37+w0GhxOjcMeDeeSQFzUmCl+nysnLwtN RkTuIaGjs8n1akZ9JT1GihOMkHx7ty7xmZBvRRUNbbQOKOX+TWuVAexJVlEMU9Jvo21a 1CXrfiuIRNUFdtyTPCVlCiOLNJNZSETVT92ld1jKHFaj3J2HMY3L72U+WSh8Ebf8zFpH qNrygliXUIzlNo4IHN9pTj78IuueJp6APHwVzGhUjHAF+cl5C20fNm1XF7EzPo4EPpzW PDKOAWo6voNLqGxq0dGLKZT/A6yeWTTyBWMx17Nr4xQrX8LzL26gsVSnq075h1Wx/Dlj riSg== MIME-Version: 1.0 X-Received: by 10.152.3.195 with SMTP id e3mr36731716lae.8.1421069985648; Mon, 12 Jan 2015 05:39:45 -0800 (PST) In-Reply-To: References: <54B2884B.90301@davea.name> Date: Mon, 12 Jan 2015 19:09:45 +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: 185 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421069993 news.xs4all.nl 2854 [2001:888:2000:d::a6]:49665 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83609 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 =3D datetime.now() h =3D n.strftime("%H") m =3D n.strftime("%M") d =3D date.today() #''' get the sleep_schedule_structure ''' def yield_times(): 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=3D4) end =3D start + timedelta(minutes=3D2) yield(start.strftime("%A"),start.strftime("%H:%M"),end.strftime("%H= :%M")) gen =3D yield_times() f =3D open('/tmp/sleep_sched.conf','wb') for ii in range(10): a,b,c =3D gen.next() res =3D "{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 wrote: > 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 fun= ction. 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 = make this easy, i avoided doing any formatting in the print statement. I us= ed 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 =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 = 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 =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_= times(), put them inside the parens. >>> >> sure . >> >> Regards, >> Gpal