[Tutor] str.strip strange result...?
#python2.7 >>> s="V01_1" >>> s.strip("_1") 'V0' Wouldn't you expect the result to be "V01" ? Cheers Jignesh ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] str.strip strange result...?
Gotcha and thank you for the reminder to read the documentation. Very clear, indeed. Many thanks! Cheers Jignesh On Fri, 15 Jan 2016 at 17:32, Mark Lawrence wrote: > On 15/01/2016 16:25, Jignesh Sutar wrote: > > #python2.7 > > > >>>> s="V01_1" > >>>> s.strip("_1") > > 'V0' > > > > Wouldn't you expect the result to be "V01" ? > > > > Cheers > > Jignesh > > No, never expect anything from a given programming language. What did > you not understand about this > https://docs.python.org/3/library/stdtypes.html#str.strip when you read > it? > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Loop in pre-defined blocks
Is there a better way to code the below than to specify blocks as I have. Ideally I'd like to specify blocks simply as *blocks=(12,20,35)* blocks=[(1,12), (13,20), (25,35)] for i,j in enumerate(blocks): for x in xrange(blocks[i][0],blocks[i][1]+1): print i+1, x Thanks in advance. Jignesh ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Loop blocks
Is there a better way to code the below than to specify blocks as I have. Ideally I'd like to specify blocks simply as *blocks=(12,20,35)* blocks=[(1,12), (13,20), (25,35)] for i,j in enumerate(blocks): for x in xrange(blocks[i][0],blocks[i][1]+1): print i+1, x Thanks in advance. Jignesh ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop in pre-defined blocks
Sorry, to be a little bit more descriptive. I'd like to loop from 1 to 35 but within this loop there are divisions which I need to prefix that particular division number. My output would look like this: 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 3 25 3 26 3 27 3 28 3 29 3 30 3 31 3 32 3 33 3 34 3 35 On Sat, 11 Jun 2016 at 00:02 Alan Gauld via Tutor wrote: > On 10/06/16 23:43, Jignesh Sutar wrote: > > Is there a better way to code the below than to specify blocks as I have. > > Ideally I'd like to specify blocks simply as *blocks=(12,20,35)* > > > > blocks=[(1,12), (13,20), (25,35)] > > for i,j in enumerate(blocks): > > for x in xrange(blocks[i][0],blocks[i][1]+1): > > print i+1, x > > > Can you explain in English what you are trying to do. > Working through your algorithm in my head is too much > like hard work. At the very least show us the output. > > Better still explain what it means - what the data > represents and how the outputs relate to the inputs. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to CSV string containing quote and comma
Thanks Steve, Alan. Sound advice. Very much a novice so trying to pick up good habits. Will definitely take on board your comments! Thanks again. Jignesh On 10 December 2013 00:46, Alan Gauld wrote: > On 09/12/13 23:46, Steven D'Aprano wrote: > > Python has two different quote characters ' and " so you can use one for >> delimiters and the other inside the string: >> > > And if you need both you can also use triple quotes. > > > If you need both, you can escape the one that matches the delimiter: >> >> s = 'this string contains both \' single and " double quotes' >> > > Or using triple quotes: > > > s = '''this string contains both ' single and " double quotes''' > > Python will then do the escaping for you. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] formatting datetime.timedelta to "HH:MM:SS"
Hi, I've googled around extensively to try figure this out assuming it should be straight forward (and it probably is) but I'm clearly missing something. I'm trying to get the total run time of the program but have the final time being displayed in a particular format. I.e. without the seconds in milliseconds decimal points and just to customize it a bit more. import time from datetime import datetime startTime = datetime.now() time.sleep(5.1564651443644) endTime = datetime.now() exe_time = endTime-startTime print type(startTime) print type(endTime) print type(exe_time) print "startTime: ", startTime print "endTime:", endTime print "exe_time: ", exe_time #how to format this to "D Days, HH: MM: SS" ? #exe_time: 0:00:05.156000 #desired 0 Days, 0h: 00:m: 05s Thanks in advance, Jignesh ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] formatting datetime.timedelta to "HH:MM:SS"
> > print str(exe_time).split('.')[0] Sorry, I guess my question was why I can't use something similar to below on exe_time (of type datetime.timedelta)? Rather than doing string manipulation on decimals or colons to extract the same. now = datetime.now() print now.hour print now.minute print now.year On 11 December 2013 12:43, David Robinow wrote: > On Wed, Dec 11, 2013 at 5:55 AM, Jignesh Sutar wrote: > > Hi, > > > > I've googled around extensively to try figure this out assuming it > should be > > straight forward (and it probably is) but I'm clearly missing something. > > > > I'm trying to get the total run time of the program but have the final > time > > being displayed in a particular format. I.e. without the seconds in > > milliseconds decimal points and just to customize it a bit more. > > > > import time > > from datetime import datetime > > startTime = datetime.now() > > time.sleep(5.1564651443644) > > endTime = datetime.now() > > exe_time = endTime-startTime > > > > print type(startTime) > > print type(endTime) > > print type(exe_time) > > > > print "startTime: ", startTime > > print "endTime:", endTime > > print "exe_time: ", exe_time #how to format this to "D Days, HH: MM: SS" > ? > > #exe_time: 0:00:05.156000 > > #desired 0 Days, 0h: 00:m: 05s > > > print str(exe_time).split('.')[0] > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] formatting datetime.timedelta to "HH:MM:SS"
Thanks Mark, print('%02d:%02d:%04d' % (now.hour, now.minute, now.year)) That works for; now = datetime.now() but not for; exe_time = endTime-startTime Thanks, Jignesh On 11 December 2013 13:37, Mark Lawrence wrote: > On 11/12/2013 13:12, Jignesh Sutar wrote: > >> print str(exe_time).split('.')[0] >> Sorry, I guess my question was why I can't use something similar to >> below on exe_time (of type datetime.timedelta)? Rather than doing string >> manipulation on decimals or colons to extract the same. >> >> now = datetime.now() >> print now.hour >> print now.minute >> print now.year >> >> > Old style > > print('%02d:%02d:%04d' % (now.hour, now.minute, now.year)) > > New style > > print('{}:{}:{}'.format(now.hour, now.minute, now.year)) > > Sorry I can never remember the formatting types to go between {} so look > for them around here http://docs.python.org/3/library/string.html# > formatstrings > > -- > My fellow Pythonistas, ask not what our language can do for you, ask what > you can do for our language. > > Mark Lawrence > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] formatting datetime.timedelta to "HH:MM:SS"
Thanks folks, I think I have this as a working solution: import datetime, time a= datetime.datetime.now() time.sleep(7.1564651443644) b= datetime.datetime.now() #for testing longer time periods #a= datetime.datetime(2003, 8, 4, 8, 31, 4,0) #b= datetime.datetime(2004, 8, 5, 19, 32, 6,0) c = b-a print "%s days, %.2dh: %.2dm: %.2ds" % (c.days,c.seconds//3600,(c.seconds//60)%60, c.seconds%60) The attributes for timedelta seem only to be days, seconds and microseconds only. Nonetheless these can be used to deduce HH:MM:SS. Thanks, Jignesh On 11 December 2013 16:21, Ricardo Aráoz wrote: > El 11/12/13 10:37, Mark Lawrence escribió: > > On 11/12/2013 13:12, Jignesh Sutar wrote: >> >>> print str(exe_time).split('.')[0] >>> Sorry, I guess my question was why I can't use something similar to >>> below on exe_time (of type datetime.timedelta)? Rather than doing string >>> manipulation on decimals or colons to extract the same. >>> >>> now = datetime.now() >>> print now.hour >>> print now.minute >>> print now.year >>> >>> >> Old style >> >> print('%02d:%02d:%04d' % (now.hour, now.minute, now.year)) >> >> New style >> >> print('{}:{}:{}'.format(now.hour, now.minute, now.year)) >> >> Sorry I can never remember the formatting types to go between {} so look >> for them around here http://docs.python.org/3/library/string.html# >> formatstrings >> >> > > Or just use strftime() : > > >>> import datetime > >>> n = datetime.datetime.now() > >>> n.strftime('%H:%M:%S') > '13:19:04' > > >>> > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] formatting datetime.timedelta to "HH:MM:SS"
> > Yes, because exe-time is not a ate, a point in time, but a time delta (a > difference), thus does not hold the same attributes. Write out dir() on > 'now' and on 'exe_time' to get more info. [dir() tells you about what info > an object knows, and what methods it understands).] Thanks Denis, that's very useful. I guess I was trying to deduce the same information from the documentation but dir is a good way of double checking. > This is a correct and general solution. Maybe worth being built-in, in > fact, in my view. Thanks for confirming. Yes, exactly, I was hoping to achieve this without all the modulus calculations. Cheers, Jignesh On 11 December 2013 21:18, spir wrote: > On 12/11/2013 06:40 PM, Jignesh Sutar wrote: > >> c = b-a >> print "%s days, %.2dh: %.2dm: %.2ds" % >> (c.days,c.seconds//3600,(c.seconds//60)%60, c.seconds%60) >> > > This is a correct and general solution. Maybe worth being built-in, in > fact, in my view. > > Denis > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to determine which function code is being called from
Hi I'm trying to exclude a certain line of code if the function is called by another function, see illustration below: def funcA(): print "running from funcA" # print only if running from funcA print "running from funcA or funcB" #print when running from either function print "running from funcB" # print only when running from funcB def funcB(): funcA() funcB() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to determine which function code is being called from
Hi All, thanks for the comments and yes I am a tad confused and a little out of my comfort zone with what I am trying to achieve here. Is this below any better way (or even worse?) of trying to achieve this. Though this doesn't actually achieve what I want it to, it may nonetheless highlight the concept in principle? def funcA(runfromB=False): if runfromB is False:"running from funcA" # print only if running from funcA print "running from funcA or funcB" #print when running from either function if runfromB is True: "running from funcB" # print only when running from funcB def funcB(runfromB): funcA(runfromB=runfromB) funcB(runfromB=True) On 6 March 2014 20:37, Jerry Hill wrote: > On Thu, Mar 6, 2014 at 12:00 PM, Jignesh Sutar wrote: > > Hi I'm trying to exclude a certain line of code if the function is > called by > > another function, see illustration below: > > As other have said, this is not often a good idea. That said, it is > possible to inspect the call stack to see what called a particular > function, like this (python 3.3): > > iimport inspect > > def funcA(): > caller = inspect.stack()[1][3] > print('funcA was called by ' + caller) > if caller == '': > print("running from funcA")# print only if running from funcA > if caller in ('', 'funcB'): > print("running from funcA or funcB") # print when running from > either function > if caller == 'funcB': > print("running from funcB") # print only when running from funcB > > def funcB(): > funcA() > > print('- Calling funcA() directly -') > funcA() > print('- Calling funcB() -') > funcB() > > Output: > > >>> > - Calling funcA() directly - > funcA was called by > running from funcA > running from funcA or funcB > - Calling funcB() - > funcA was called by funcB > running from funcA or funcB > running from funcB > >>> > > > -- > Jerry > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Multiple for and if/else statements into a single list comprehension
Is it possible to get two nested for statements followed by a nested if/else statement all into a single list comprehension ie. the equivalent of the below: for i in xrange(1,20): for j in xrange(1,10): if j<6: j=int("8"+str(j)) else: j=int("9"+str(j)) print "%(i)02d_%(j)02d" % locals() # double for statement without if/else works print "\n".join(["%(i)02d_%(j)02d" % locals() for i in xrange(1,20) for j in xrange(1,10)]) #now try to incorporate if/else part #failed attempt 1 print "\n".join(["%(i)02d_%(j)02d" % locals() for i in xrange(1,20) for j in xrange(1,10) j=int("8"+str(j)) if j<6 else int("9"+str(j))]) #failed attempt 2 print "\n".join(["%(i)02d_%(j)02d" % locals() for i in xrange(1,20) j=int("8"+str(j)) if j<6 else int("9"+str(j)) for j in xrange(1,10)]) #failed attempt 3 print "\n".join(["%(i)02d_%(j)02d" % locals() j=int("8"+str(j)) if j<6 else int("9"+str(j)) for i in xrange(1,20) for j in xrange(1,10)]) Many thanks in advance. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Multiple for and if/else statements into a single list comprehension
Is it possible to get two nested for statements followed by a nested if/else statement all into a single list comprehension ie. the equivalent of the below: for i in xrange(1,20): for j in xrange(1,10): if j<6: j=int("8"+str(j)) else: j=int("9"+str(j)) print "%(i)02d_%(j)02d" % locals() # double for statement without if/else works print "\n".join(["%(i)02d_%(j)02d" % locals() for i in xrange(1,20) for j in xrange(1,10)]) #now try to incorporate if/else part #failed attempt 1 print "\n".join(["%(i)02d_%(j)02d" % locals() for i in xrange(1,20) for j in xrange(1,10) j=int("8"+str(j)) if j<6 else int("9"+str(j))]) #failed attempt 2 print "\n".join(["%(i)02d_%(j)02d" % locals() for i in xrange(1,20) j=int("8"+str(j)) if j<6 else int("9"+str(j)) for j in xrange(1,10)]) #failed attempt 3 print "\n".join(["%(i)02d_%(j)02d" % locals() j=int("8"+str(j)) if j<6 else int("9"+str(j)) for i in xrange(1,20) for j in xrange(1,10)]) Many thanks in advance. Jignesh ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiple for and if/else statements into a single list comprehension
Thanks Peter/Denis. I wasn't aware of genexp. I see how you have adapted the code to make it work, I'll adapt the same in my program. Good point about duplicating j , Denis, I guess I was happy to override the outer j as it was intermediate. On 17 March 2014 12:36, spir wrote: > On 03/17/2014 11:22 AM, Jignesh Sutar wrote: > >> Is it possible to get two nested for statements followed by a nested >> if/else statement all into a single list comprehension ie. the equivalent >> of the below: >> >> >> for i in xrange(1,20): >> for j in xrange(1,10): >> if j<6: >> j=int("8"+str(j)) >> else: >> j=int("9"+str(j)) >> print "%(i)02d_%(j)02d" % locals() >> > > You can do it by reformulating your inner block into an expression (here, > using a ternary if expression), which will then become the expression part > of the comprehension. However, a few remarks: > > * don't do that: the only advantage is to make your code unreadable > * you may reformulate using 2 comprehensions; if you don't want > intermediate lists, use a generator expression for the inner one > * above, the inner j is a new variable with a distinct meaning: why do you > call it j? > * do you really need string concat to perform arithmetic? > > > d > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Test to check if values of dictionary are all equal (which happen to be dictionaries)
I needed to test if the values of all entries in a dictionary were equal but since the values themselves were dictionaries I couldn't simply take a set of the values and test if this equated to one. So I ended up taking all combination of the keys and testing pairs of sub dictionaries. I just want to check that there isn't a more direct way of doing this that testing all combinations? import itertools dictmain={"K1": {1:"SD_V1",2:"SD_V2"}, "K2": {1:"SD_V1",2:"SD_V2"}, "K3": {1:"SD_V1",2:"SD_V2"}} for compare in list(itertools.combinations(dictmain,2)): print "Comparing dictionaries:", compare if dictmain[compare[0]]==dictmain[compare[1]]: print "comb dict are equal" else: print "comb dict are NOT equal" break Many thanks in advance, Jignesh ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor