From mysecretrobotfactory at gmail.com Thu Jun 1 11:30:22 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 1 Jun 2017 08:30:22 -0700 Subject: [Tutor] threading tutorial In-Reply-To: References: <20170525220359.GA44584@cskk.homeip.net> Message-ID: Oh i get it alright, however in my code I have to push the W button like this: import pyautogui import time pyautogui.keyDown('w') time.sleep(2) pyautogui.keyUp('w') while the example you gave: def fn(): global run_me while run_me: ... do some work ... and then elsewhere you go: global run_me run_me = True ... create and start the Thread ... ... later ... run_me = False T.join() theoretically deals with my problem, in practice though, my function spend almost all its time holding down the 'w' button, given how many miliseconds i need it. and so it's not responsive enough for this reason. Is there a way to pause/kill the thread? thanks! On Thu, May 25, 2017 at 7:47 PM, Michael C wrote: > message received, i ll take a look tomorrow asap. > > thanks for replying!!! > > On Thu, May 25, 2017 at 3:03 PM, Cameron Simpson wrote: > >> On 25May2017 11:52, Michael C wrote: >> >>> Right now all i need is to grab 3 values from 3 variables before killing >>> a >>> thread, like this: >>> >>> def stuff(): >>> do stuff, >>> get values, (x,y,d) >>> >>> # main code >>> startthread(stuff(), blah) >>> # if else need to sleep or kill the thread, and because I'll restart the >>> thread later, I'd like to get the values from the thread, say x,y,d in >>> order to restart the thread. >>> loop. >>> >>> Therefore, how do I get a few values from a few variables from the thread >>> and then close it? >>> >>> Threading is very new to me, so I have to be very diligent. >>> >> >> You always need to be diligent with threads :-) >> >> Can you explain why you need to use a thread for this? The first cut of >> your program looks like you could do it with a ordinary function: >> >> def stuff(): >> ... compute x, y, z ... >> return x, y, z >> >> def main(): >> x, y, z = stuff() >> >> OTOH, your later description suggests that you want to kick off a thread >> to work on something, and have your main program let it run, or pause it. >> The implication is that x, y, z represent the thread state allowing you to >> restart it from scratch at the same point where you paused/stopped things. >> >> There are a few different ways to manage that scenario. But first, write >> yourself a small Thread based program to familiarise yourself with threads. >> For example (untested): >> >> from __future__ import print_function >> from time import sleep >> from threading import Thread >> >> def thread_main_body(): >> print("thread started") >> for n in range(20): >> print("thread", n) >> sleep(0.3) >> print("thread done") >> >> def main(): >> print("main") >> T = Thread(target=thread_main_body) >> print("main: Thread created but _not_ started") >> sleep(1) >> T.start() >> print("thread started") >> for n in range(10): >> print("main", n) >> sleep(0.4) >> print("main program waiting for thread") >> T.join() >> print("main program done") >> >> You should see the main thread and your subthread outputs interleaved. >> The sleeps are just to ensure some interleaving and to give good >> interactive feel. It should run for about 8 seconds overall. Make sure >> you're happy you understand what is happening, why, and when. >> >> There are a few things you need to keep in mind with threads (in Python, >> and to a degree in other languages): >> >> 1: You can't kill/stop a Thread. Instead, the usual approach to to share >> some state with some kind of "running" flag, a boolean saying that the >> Thread's function should continue. Then the thread polls that regularly. >> Eg, if the thread function runs a main loop it might look like this: >> >> def fn(): >> global run_me >> while run_me: >> ... do some work ... >> >> and then elsewhere you go: >> >> global run_me >> run_me = True >> ... create and start the Thread ... >> ... later ... >> run_me = False >> T.join() >> >> so effectively you ask the Thread to stop, and it obeys when it notices >> the change to "run_me". Using a global for this is pretty crube, and not >> the general approach, BTW. >> >> 2: Like any other function, the local varaibles to the thread function >> are not available outside. Thus the "global" hack above. So to share state >> you would usually make some kind of object with the state, and pass it in >> to the Thread when you create and start it: >> >> def fn(state): >> while state.run_me: >> ... do stuff, keep important things like results in "state" ... >> state.x = 1 >> state.y = whatever >> >> class State(object): >> pass >> >> def main(): >> state = State() >> state.run_me = True >> T = Thread(target=fn, args=(state,)) >> T.start() >> for n in range(10): >> print("main", n, "x =", state.x, "y =", state.y) >> sleep(0.3) >> state.run_me = False >> T.join() >> >> As I remarked, there are a few ways to approach your scenario. The above >> should get you started on one approach. Pausing can be done in a few ways, >> either by starting and stopping individual threads, one after another, or >> by starting one thread and using a mutex of some kind to cause it to >> suspend activity when needed. Yet another approach is corroutines, but I'd >> recommend getting threading understood first to avoid confusion. >> >> Come back woith some functioning code and more questions. >> >> Cheers, >> Cameron Simpson >> > > From malaclypse2 at gmail.com Thu Jun 1 13:52:50 2017 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 1 Jun 2017 13:52:50 -0400 Subject: [Tutor] threading tutorial In-Reply-To: References: <20170525220359.GA44584@cskk.homeip.net> Message-ID: On Thu, Jun 1, 2017 at 11:30 AM, Michael C wrote: > Oh i get it alright, however in my code I have to push the W button like > this: > > import pyautogui > import time > > pyautogui.keyDown('w') > time.sleep(2) > pyautogui.keyUp('w') ... > theoretically deals with my problem, in practice though, my function spend > almost > all its time holding down the 'w' button, given how many miliseconds i need > it. and so it's not responsive enough > for this reason. >From that example, it looks like you spend almost all the time sleeping, right? Maybe sleep for a shorter amount of time, in a loop where you can check the flag? Something like: global run_me time_to_sleep = 2 time_asleep = 0 pyautogui.keyDown('w') while run_me and (time_asleep < time_to_sleep): delta = time_to_sleep/100 time.sleep(delta) time_asleep += delta pyautogui.keyUp('w') That would let you check the flag more often, so you can clean up properly. -- Jerry From mysecretrobotfactory at gmail.com Thu Jun 1 13:53:34 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 1 Jun 2017 10:53:34 -0700 Subject: [Tutor] threading tutorial In-Reply-To: References: <20170525220359.GA44584@cskk.homeip.net> Message-ID: let me try that! thanks! On Thu, Jun 1, 2017 at 10:52 AM, Jerry Hill wrote: > On Thu, Jun 1, 2017 at 11:30 AM, Michael C > wrote: > > Oh i get it alright, however in my code I have to push the W button like > > this: > > > > import pyautogui > > import time > > > > pyautogui.keyDown('w') > > time.sleep(2) > > pyautogui.keyUp('w') > > ... > > > theoretically deals with my problem, in practice though, my function > spend > > almost > > all its time holding down the 'w' button, given how many miliseconds i > need > > it. and so it's not responsive enough > > for this reason. > > From that example, it looks like you spend almost all the time > sleeping, right? Maybe sleep for a shorter amount of time, in a loop > where you can check the flag? Something like: > > global run_me > time_to_sleep = 2 > time_asleep = 0 > > pyautogui.keyDown('w') > while run_me and (time_asleep < time_to_sleep): > delta = time_to_sleep/100 > time.sleep(delta) > time_asleep += delta > pyautogui.keyUp('w') > > That would let you check the flag more often, so you can clean up properly. > > -- > Jerry > From tvbare at gmail.com Thu Jun 1 15:34:37 2017 From: tvbare at gmail.com (Terry) Date: Thu, 1 Jun 2017 14:34:37 -0500 Subject: [Tutor] Pasting an image with transparency Message-ID: Slackware 14.2 64-bit Python 2.7.13 I am trying to automate some photo processing by pasting a sig or watermark. The sig image is a .png with transparency but when it pastes it does so with a black background. Is there a way to paste with transparency? from PIL import Image from PIL import ImageEnhance fname = "sample.jpg" im = Image.open(fname) print(im.format, im.size, im.mode) out = im.resize((1068, 712)) enh = ImageEnhance.Sharpness(out) enh = enh.enhance(2.5) sig = Image.open("/home/tvbare/pics/recent_pics/sigs/opi_sig_landscape.png") print(sig.format, sig.size, sig.mode) box = (768, 616, 1018, 662) enh.paste(sig, box) enh.show() -- Terry "Hoots" To stay young, never lose your sense of wonder. ***** My main photo gallery can be seen at: I also keep a secondary gallery at: ***** From alan.gauld at yahoo.co.uk Thu Jun 1 21:17:05 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 2 Jun 2017 02:17:05 +0100 Subject: [Tutor] threading tutorial In-Reply-To: References: <20170525220359.GA44584@cskk.homeip.net> Message-ID: On 01/06/17 16:30, Michael C wrote: > Oh i get it alright, however in my code I have to push the W button like > this: > > import pyautogui > import time > > pyautogui.keyDown('w') > time.sleep(2) > pyautogui.keyUp('w') So this emulates a user pressing the w key for 2 seconds. What's not clear is where this appears in your design, is it part of the code running in the thread or is it part of the code that stops the thread? If you can explain a little bit more of the high level requirement hee rather than the implementation perhaps we can come up with a better solution - ideally one that doesn't involve any keypress emulation at all... > while the example you gave: > > def fn(): > global run_me > while run_me: > ... do some work ... > > and then elsewhere you go: > > global run_me > run_me = True > ... create and start the Thread ... > ... later ... > run_me = False > T.join() > > theoretically deals with my problem, in practice though, my function spend > almost all its time holding down the 'w' button, The fact you say the function suggests you mean the thread. So the question is why does it need to hold the key down for so long? Indeed why does a background process need to emulate a button press? What is the button press doing? Is it in turn detected by some other process/thread? We need to understand how the various bits interact to give a better solution. > Is there a way to pause/kill the thread? Not really other than setting a flag, but if its the thread that's doing the sleeping then killing it won't help - in fact if you could kill it in the middle of the sleep() you'd have the problem of a key being "held down" forever - probably a bad thing... Thats why its better to have the thread kill itself on detection of the semaphore. -- 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 From mysecretrobotfactory at gmail.com Thu Jun 1 21:20:04 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 02 Jun 2017 01:20:04 +0000 Subject: [Tutor] threading tutorial In-Reply-To: References: <20170525220359.GA44584@cskk.homeip.net> Message-ID: ihave to look at this tomorrow, thanks for the reply! On Thu, Jun 1, 2017 at 6:18 PM Alan Gauld via Tutor wrote: > On 01/06/17 16:30, Michael C wrote: > > Oh i get it alright, however in my code I have to push the W button like > > this: > > > > import pyautogui > > import time > > > > pyautogui.keyDown('w') > > time.sleep(2) > > pyautogui.keyUp('w') > > So this emulates a user pressing the w key for 2 seconds. > What's not clear is where this appears in your design, > is it part of the code running in the thread or is it > part of the code that stops the thread? > > If you can explain a little bit more of the high level > requirement hee rather than the implementation perhaps > we can come up with a better solution - ideally one > that doesn't involve any keypress emulation at all... > > > while the example you gave: > > > > def fn(): > > global run_me > > while run_me: > > ... do some work ... > > > > and then elsewhere you go: > > > > global run_me > > run_me = True > > ... create and start the Thread ... > > ... later ... > > run_me = False > > T.join() > > > > theoretically deals with my problem, in practice though, my function > spend > > almost all its time holding down the 'w' button, > > The fact you say the function suggests you mean the thread. > So the question is why does it need to hold the key down > for so long? Indeed why does a background process need > to emulate a button press? What is the button press doing? > Is it in turn detected by some other process/thread? We > need to understand how the various bits interact to give > a better solution. > > > Is there a way to pause/kill the thread? > > Not really other than setting a flag, but if its the > thread that's doing the sleeping then killing it > won't help - in fact if you could kill it in the > middle of the sleep() you'd have the problem of > a key being "held down" forever - probably a > bad thing... Thats why its better to have the > thread kill itself on detection of the semaphore. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From szattila88 at gmail.com Fri Jun 2 02:20:03 2017 From: szattila88 at gmail.com (=?UTF-8?Q?Attila_Szab=C3=B3?=) Date: Fri, 2 Jun 2017 08:20:03 +0200 Subject: [Tutor] Issue with wsgi_ref.simple_server - sometimes very slow! Message-ID: Hi All, I'm facing a really strange behavior with python's wsgi_ref.simple_server module. I have the following setup: - Raspberry Pi 2 - Ubuntu Mate 16.04 - Python3.5 - I have the following simple source: #!/usr/bin/env python3 import wsgiref.simple_server def my_func(env, start_response): start_response('200 OK', []) return [''.encode()] server = wsgiref.simple_server.make_server( '0.0.0.0', 19891, my_func, ) server.serve_forever() After several requests (around every ~5 requests) the response time is getting really-really slow (1-60sec) compared to the average 0.1s response, the server serves the request and then quick again for couple of new requests and then again....getting to be slow... I'm trying to reach the node via my router, and also from outside internet and the latency is there. I have tried the same code also on Windows10 with same python version, from behind the same router and the issue was not present. Also I have removed the router and connected internet directly to Raspberry PI and I faced the same issue. So I think I can say that that is not because of the router for sure. Also, always when I interrupt the running server I'm getting the following exception: Exception happened during processing of request from ('192.168.1.100', 3540) Traceback (most recent call last): File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock self.process_request(request, client_address) File "/usr/lib/python3.5/socketserver.py", line 341, in process_request self.finish_request(request, client_address) File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python3.5/socketserver.py", line 681, in __init__ self.handle() File "/usr/lib/python3.5/wsgiref/simple_server.py", line 119, in handle self.raw_requestline = self.rfile.readline(65537) File "/usr/lib/python3.5/socket.py", line 575, in readinto return self._sock.recv_into(b) KeyboardInterrupt That's why I think there is some issue with that recv_into function, but I cannot figure it out how to resolve this... :/ Does anybody faced this same issue? Or anybody has an idea what should I try or what should I modify in python source to solve this issue? Thanks, Attila From alan.gauld at yahoo.co.uk Fri Jun 2 05:16:58 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 2 Jun 2017 10:16:58 +0100 Subject: [Tutor] Issue with wsgi_ref.simple_server - sometimes very slow! In-Reply-To: References: Message-ID: This appears to be a duplicate of the message you sent on 31st May. Please don not send multiple copies of the same message, it fragments the threads and messes up the archives for searching. One message is sufficient, if you don't get a response it probably means nobody knows the answer (or maybe your question was insufficiently specific, although that's not an issue here). If you want to clarify the issue send a follow-on to your own message, please don't start a new thread. Thanks Alan G. Moderator. On 02/06/17 07:20, Attila Szab? wrote: > Hi All, > > I'm facing a really strange behavior with python's wsgi_ref.simple_server > module. > > I have the following setup: > - Raspberry Pi 2 > - Ubuntu Mate 16.04 > - Python3.5 > - > > I have the following simple source: > #!/usr/bin/env python3 > import wsgiref.simple_server > > def my_func(env, start_response): > start_response('200 OK', []) > return [''.encode()] > > server = wsgiref.simple_server.make_server( > '0.0.0.0', > 19891, > my_func, > ) > > server.serve_forever() > .... From alan.gauld at yahoo.co.uk Fri Jun 2 05:19:43 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 2 Jun 2017 10:19:43 +0100 Subject: [Tutor] Pasting an image with transparency In-Reply-To: References: Message-ID: On 01/06/17 20:34, Terry wrote: > Slackware 14.2 64-bit > Python 2.7.13 > > I am trying to automate some photo processing by pasting a > sig or watermark. The sig image is a .png with transparency > but when it pastes it does so with a black background. Is there > a way to paste with transparency? > I believe there is a Pillow support forum, this is probably better addressed to the PIL experts there. https://python-pillow.org/ Or possibly even to a general imaging/graphics discussion forum. -- 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 From __peter__ at web.de Fri Jun 2 09:31:11 2017 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Jun 2017 15:31:11 +0200 Subject: [Tutor] Pasting an image with transparency References: Message-ID: Terry wrote: > Slackware 14.2 64-bit > Python 2.7.13 > > I am trying to automate some photo processing by pasting a > sig or watermark. The sig image is a .png with transparency > but when it pastes it does so with a black background. Is there > a way to paste with transparency? > > > > from PIL import Image > from PIL import ImageEnhance > > fname = "sample.jpg" > > im = Image.open(fname) > print(im.format, im.size, im.mode) > > out = im.resize((1068, 712)) > > enh = ImageEnhance.Sharpness(out) > enh = enh.enhance(2.5) > > sig = > Image.open("/home/tvbare/pics/recent_pics/sigs/opi_sig_landscape.png") > print(sig.format, sig.size, sig.mode) > box = (768, 616, 1018, 662) > enh.paste(sig, box) > > enh.show() > We read the docstring so you don't have to ;) >>> from PIL import Image >>> image = Image.open("sample.jpg") # some random pic I have lying around >>> help(image.paste) Help on method paste in module PIL.Image: paste(im, box=None, mask=None) method of PIL.JpegImagePlugin.JpegImageFile instance [...] Note that if you paste an "RGBA" image, the alpha band is ignored. You can work around this by using the same image as both source image and mask. [...] So let's try that. (Since I don't have a transparent picture handy I'm using a copy of .) >>> stamp = Image.open("transparent.png") >>> image.paste(stamp, (0, 0), mask=stamp) >>> image.show() Seems to work... From meenuravi89 at gmail.com Fri Jun 2 13:27:01 2017 From: meenuravi89 at gmail.com (meenu ravi) Date: Fri, 2 Jun 2017 12:27:01 -0500 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: References: Message-ID: Hello, I'm planning to create a blog that provides solution with explanation for python programming challenges available in websites like Hackerearth, codecademy, etc., so that if others also share their solution along with explanation in the same blog, it will be helpful for beginners. I wanted to make sure that it's not illegal to share solution like that. I have seen solutions available in different websites. So I hope its legal. But still wanted to confirm with views of people in large forum. I felt it may be spoon-feeding the programmers, but I was able to find solutions for almost all the programs when I searched. Thanks, Meena From japhy at pearachute.com Fri Jun 2 17:47:00 2017 From: japhy at pearachute.com (Japhy Bartlett) Date: Fri, 02 Jun 2017 21:47:00 +0000 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: References: Message-ID: The only legal concern is if you're copying challenges directly from the sites; someone has some sort of ownership and copyright on the code and description. Don't copy / paste anything and you'll be fine. If you do, check the license first (it may be open source). On Fri, Jun 2, 2017 at 12:29 PM meenu ravi wrote: > Hello, > > > > I'm planning to create a blog that provides solution with explanation for > > python programming challenges available in websites like Hackerearth, > > codecademy, etc., so that if others also share their solution along with > > explanation in the same blog, it will be helpful for beginners. I wanted to > > make sure that it's not illegal to share solution like that. I have seen > > solutions available in different websites. So I hope its legal. But still > > wanted to confirm with views of people in large forum. I felt it may be > > spoon-feeding the programmers, but I was able to find solutions for almost > > all the programs when I searched. > > > > Thanks, > > Meena > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > From mats at wichmann.us Fri Jun 2 13:35:49 2017 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 2 Jun 2017 11:35:49 -0600 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: References: Message-ID: On 06/02/2017 11:27 AM, meenu ravi wrote: > Hello, > > I'm planning to create a blog that provides solution with explanation for > python programming challenges available in websites like Hackerearth, > codecademy, etc., so that if others also share their solution along with > explanation in the same blog, it will be helpful for beginners. I wanted to > make sure that it's not illegal to share solution like that. I have seen > solutions available in different websites. So I hope its legal. But still > wanted to confirm with views of people in large forum. I felt it may be > spoon-feeding the programmers, but I was able to find solutions for almost > all the programs when I searched. Seems like it helps defeat the purpose of such sites if the answers are all located in one place... From tmrsg11 at gmail.com Fri Jun 2 13:46:43 2017 From: tmrsg11 at gmail.com (C W) Date: Fri, 2 Jun 2017 13:46:43 -0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder Message-ID: Dear Python list, I am an R user learning Python. What is a good editor? 1) Pycharm PyCharm evaluates the entire script, I just want to change a few lines in the script. For example, import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 1,0.1) y = np.sin(2 * np.pi * x) plt.figure(1) plt.clf() plt.plot(x, y) plt.show() Now, I want to do a scatter plot, but don't want to generate the data again. I just want the "line by line" evaluation like in R and Matlab. Basically, you can type in console and add to the existing variables. 2) Spyder Spyder looks a lot like RStudio, I like it! But, it does not have an app icon in applications. I am baffled. I do ~/anaconda/bin/spyder every time. Am I missing something or is this the way it is? Thank you very much! From tvbare at gmail.com Fri Jun 2 14:54:43 2017 From: tvbare at gmail.com (Terry) Date: Fri, 2 Jun 2017 13:54:43 -0500 Subject: [Tutor] Pasting an image with transparency In-Reply-To: References: Message-ID: <7d3e9c9b-695a-8f0b-1c7a-1ce6c2ee47a6@gmail.com> On 06/02/2017 08:31 AM, Peter Otten wrote: > Terry wrote: > > We read the docstring so you don't have to ;) > I have to remember to utilize available help functions... my bad. >>>> from PIL import Image >>>> image = Image.open("sample.jpg") # some random pic I have lying around >>>> help(image.paste) > Help on method paste in module PIL.Image: > > paste(im, box=None, mask=None) method of PIL.JpegImagePlugin.JpegImageFile > instance > > [...] > Note that if you paste an "RGBA" image, the alpha band is > ignored. You can work around this by using the same image as > both source image and mask. > [...] > > So let's try that. (Since I don't have a transparent picture handy I'm using > a copy of .) > >>>> stamp = Image.open("transparent.png") >>>> image.paste(stamp, (0, 0), mask=stamp) >>>> image.show() > Seems to work... > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor I first posted my question to the image-sig list but never got a reply. Awesome! Thank you for responding. -- Terry "Hoots" To stay young, never lose your sense of wonder. ***** My main photo gallery can be seen at: I also keep a secondary gallery at: ***** From danny.yoo at gmail.com Fri Jun 2 19:51:46 2017 From: danny.yoo at gmail.com (Danny Yoo) Date: Fri, 2 Jun 2017 16:51:46 -0700 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: References: Message-ID: I'm not a fan of the idea of publishing solutions of coding challenge problems because it often violates the honor codes of institutions. Even if some sites are okay with this, the majority probably are not. Rather than muddy the water, might be best to skirt the issue. What is the problem you're trying to solve, though? It's laudable that you want to help, so perhaps there's something else you can do that's both effective and not as controversial. From danny.yoo at gmail.com Fri Jun 2 20:00:37 2017 From: danny.yoo at gmail.com (Danny Yoo) Date: Fri, 2 Jun 2017 17:00:37 -0700 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: References: Message-ID: Legality is the lowest of bars. We should aim higher. I'm pretty sure that the listed sites should strongly prefer *not* to have solutions available like this. The more I think about this, the more I'm tending to say: don't do this. It may feel like charity, but the authors of the problem sets will not look at this kindly. From steve at pearwood.info Fri Jun 2 20:27:23 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 3 Jun 2017 10:27:23 +1000 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: References: Message-ID: <20170603002723.GC17170@ando.pearwood.info> On Fri, Jun 02, 2017 at 05:00:37PM -0700, Danny Yoo wrote: > Legality is the lowest of bars. We should aim higher. > > I'm pretty sure that the listed sites should strongly prefer *not* to have > solutions available like this. > > The more I think about this, the more I'm tending to say: don't do this. > It may feel like charity, but the authors of the problem sets will not look > at this kindly. I don't think "what the authors might want" is the only factor here. Personally, I think these programming challenge sites probably do more harm than good, discouraging people that they're not good enough to be a programmer because they can't solve the (often exceedingly tricky) problems on their own. I think they're often dick-measuring contests, for elite programmers to show off and sneer at "lesser mortals" who can't solve the problems. In the real world, nobody has to solve these sorts of problems under the constraints given. In real life programming, you get to look for existing solutions, you get to consult with your colleagues, pass ideas back and forth, etc. If you need a solution to X, and your colleague already solved it for another project, you say "Hey Fred, I'm stealing your code" and if Fred gets upset you talk to his project manager who tells Fred to cooperate. (Well, that's the way it is in companies that are not dysfunctional.) These problems are the very definition of Solved Problems. They have been solved thousands of times! They're fine for people who *enjoy* this sort of challenge, but I believe that for every one of them, there are probably a hundred or a thousand programmers who do not enjoy these challenges, who are discouraged by them, but who would learn a lot from being able to read and re-use the solutions. That's just my opinion. People may disagree. -- Steve From meenuravi89 at gmail.com Fri Jun 2 21:11:38 2017 From: meenuravi89 at gmail.com (meenu ravi) Date: Fri, 2 Jun 2017 20:11:38 -0500 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: <20170603002723.GC17170@ando.pearwood.info> References: <20170603002723.GC17170@ando.pearwood.info> Message-ID: Thank you all for your views. I was hesitating for the same reason. Now I'm clear that I shouldn't go for a blog that gives straightforward solution for the challenges. Thanks, Meena On Jun 2, 2017 7:30 PM, "Steven D'Aprano" wrote: > On Fri, Jun 02, 2017 at 05:00:37PM -0700, Danny Yoo wrote: > > Legality is the lowest of bars. We should aim higher. > > > > I'm pretty sure that the listed sites should strongly prefer *not* to > have > > solutions available like this. > > > > The more I think about this, the more I'm tending to say: don't do this. > > It may feel like charity, but the authors of the problem sets will not > look > > at this kindly. > > I don't think "what the authors might want" is the only factor here. > Personally, I think these programming challenge sites probably do more > harm than good, discouraging people that they're not good enough to be a > programmer because they can't solve the (often exceedingly tricky) > problems on their own. I think they're often dick-measuring contests, > for elite programmers to show off and sneer at "lesser mortals" who > can't solve the problems. > > In the real world, nobody has to solve these sorts of problems under the > constraints given. In real life programming, you get to look for > existing solutions, you get to consult with your colleagues, pass ideas > back and forth, etc. If you need a solution to X, and your colleague > already solved it for another project, you say "Hey Fred, I'm stealing > your code" and if Fred gets upset you talk to his project manager who > tells Fred to cooperate. > > (Well, that's the way it is in companies that are not dysfunctional.) > > These problems are the very definition of Solved Problems. They have > been solved thousands of times! > > They're fine for people who *enjoy* this sort of challenge, but I > believe that for every one of them, there are probably a hundred or a > thousand programmers who do not enjoy these challenges, who are > discouraged by them, but who would learn a lot from being able to read > and re-use the solutions. > > That's just my opinion. People may disagree. > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From dyoo at hashcollision.org Fri Jun 2 23:26:09 2017 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 2 Jun 2017 20:26:09 -0700 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: References: <20170603002723.GC17170@ando.pearwood.info> Message-ID: Steven says: >> I don't think "what the authors might want" is the only factor here. >> Personally, I think these programming challenge sites probably do more >> harm than good, discouraging people that they're not good enough to be a >> programmer because they can't solve the (often exceedingly tricky) >> problems on their own. [cut] >> They're fine for people who *enjoy* this sort of challenge, but I >> believe that for every one of them, there are probably a hundred or a >> thousand programmers who do not enjoy these challenges, who are >> discouraged by them, but who would learn a lot from being able to read >> and re-use the solutions. >> >> That's just my opinion. People may disagree. Hi Steven, Yes, that's a good point. I do need to be careful of myself: I have a strong tendency to think in terms of black and white. Thanks for keeping me honest. I think the kind and the quality of the problem is a big factor. If it's of the vanilla, intro-to-programming variety, I think caution is warranted. If it's a more difficult programming challenge problem, then because it has enough density and complexity, the problem won't just fall apart like cotton candy from discussing it, so that's ok too. The source of the problem also contributes another dimension. If it's coming from a book like Steven Skiena's "Programming Challenges", then I think it's perfectly ok to share and reuse solutions. If it's coming from some university homework problem set, I have some reservations. This topic is very much in the zeitgeist, by the way: https://thenextweb.com/dd/2017/05/30/lets-teach-computer-science-students-to-cheat/#.tnw_YelJZVuo https://www.nytimes.com/2017/05/29/us/computer-science-cheating.html?_r=2 > Thank you all for your views. I was hesitating for the same reason. Now I'm > clear that I shouldn't go for a blog that gives straightforward solution > for the challenges. Meena, I do want to add: if you do something like this, focus on the process rather than the end-solution, because that's the part that's worthwhile. Good luck! From meenuravi89 at gmail.com Sat Jun 3 00:18:45 2017 From: meenuravi89 at gmail.com (meenu ravi) Date: Fri, 2 Jun 2017 23:18:45 -0500 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: References: <20170603002723.GC17170@ando.pearwood.info> Message-ID: Great, thanks! On Jun 2, 2017 10:26 PM, "Danny Yoo" wrote: > Steven says: > > >> I don't think "what the authors might want" is the only factor here. > >> Personally, I think these programming challenge sites probably do more > >> harm than good, discouraging people that they're not good enough to be a > >> programmer because they can't solve the (often exceedingly tricky) > >> problems on their own. > > [cut] > > >> They're fine for people who *enjoy* this sort of challenge, but I > >> believe that for every one of them, there are probably a hundred or a > >> thousand programmers who do not enjoy these challenges, who are > >> discouraged by them, but who would learn a lot from being able to read > >> and re-use the solutions. > >> > >> That's just my opinion. People may disagree. > > > Hi Steven, > > Yes, that's a good point. I do need to be careful of myself: I have a > strong tendency to think in terms of black and white. > > Thanks for keeping me honest. > > > I think the kind and the quality of the problem is a big factor. If > it's of the vanilla, intro-to-programming variety, I think caution is > warranted. If it's a more difficult programming challenge problem, > then because it has enough density and complexity, the problem won't > just fall apart like cotton candy from discussing it, so that's ok > too. > > The source of the problem also contributes another dimension. If it's > coming from a book like Steven Skiena's "Programming Challenges", then > I think it's perfectly ok to share and reuse solutions. If it's > coming from some university homework problem set, I have some > reservations. > > > This topic is very much in the zeitgeist, by the way: > > https://thenextweb.com/dd/2017/05/30/lets-teach- > computer-science-students-to-cheat/#.tnw_YelJZVuo > > https://www.nytimes.com/2017/05/29/us/computer-science- > cheating.html?_r=2 > > > > Thank you all for your views. I was hesitating for the same reason. Now > I'm > > clear that I shouldn't go for a blog that gives straightforward solution > > for the challenges. > > Meena, I do want to add: if you do something like this, focus on the > process rather than the end-solution, because that's the part that's > worthwhile. > > > Good luck! > From ben+python at benfinney.id.au Sat Jun 3 01:47:34 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 03 Jun 2017 15:47:34 +1000 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder References: Message-ID: <85mv9py64p.fsf@benfinney.id.au> C W writes: > I am an R user learning Python. What is a good editor? Either of Vim or Emacs ? together with a good multi-tabbed terminal program ? make an excellent programmer IDE. -- \ ?When I was a baby I kept a diary. Recently I was re-reading | `\ it, it said ?Day 1: Still tired from the move. Day 2: Everybody | _o__) talks to me like I'm an idiot.?? ?Steven Wright | Ben Finney From __peter__ at web.de Sat Jun 3 03:24:32 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 03 Jun 2017 09:24:32 +0200 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder References: Message-ID: C W wrote: > Dear Python list, > > I am an R user learning Python. What is a good editor? > > 1) Pycharm > PyCharm evaluates the entire script, I just want to change a few lines in > the script. > For example, > > import matplotlib.pyplot as plt > import numpy as np > > x = np.arange(0, 1,0.1) > y = np.sin(2 * np.pi * x) > > plt.figure(1) > plt.clf() > plt.plot(x, y) > plt.show() > > Now, I want to do a scatter plot, but don't want to generate the data > again. I just want the "line by line" evaluation like in R and Matlab. > Basically, you can type in console and add to the existing variables. That sounds more like an interactive interpreter than an IDE. There is one such interpreter with bells, whistles, and notebooks https://ipython.org/ > 2) Spyder > Spyder looks a lot like RStudio, I like it! But, it does not have an app > icon in applications. I am baffled. I do ~/anaconda/bin/spyder every > time. > > Am I missing something or is this the way it is? > > Thank you very much! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From zachary.ware+pytut at gmail.com Sat Jun 3 03:32:30 2017 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Sat, 3 Jun 2017 02:32:30 -0500 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: On Fri, Jun 2, 2017 at 12:46 PM, C W wrote: > Dear Python list, > > I am an R user learning Python. What is a good editor? > > 1) Pycharm > PyCharm evaluates the entire script, I just want to change a few lines in > the script. > For example, > > import matplotlib.pyplot as plt > import numpy as np > > x = np.arange(0, 1,0.1) > y = np.sin(2 * np.pi * x) > > plt.figure(1) > plt.clf() > plt.plot(x, y) > plt.show() > > Now, I want to do a scatter plot, but don't want to generate the data > again. I just want the "line by line" evaluation like in R and Matlab. > Basically, you can type in console and add to the existing variables. >From the sound of it, you may be more interested in Jupyter Notebook (jupyter.org) than a "proper editor". Otherwise, I've been happy with a combination of PyCharm and vim. Hope this helps, -- Zach From mats at wichmann.us Fri Jun 2 20:14:06 2017 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 2 Jun 2017 18:14:06 -0600 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: <4a75cc92-7d67-2e4b-f988-bed2603de9ae@wichmann.us> On 06/02/2017 11:46 AM, C W wrote: > Dear Python list, > > I am an R user learning Python. What is a good editor? > > 1) Pycharm > PyCharm evaluates the entire script, I just want to change a few lines in > the script. > For example, > > import matplotlib.pyplot as plt > import numpy as np > > x = np.arange(0, 1,0.1) > y = np.sin(2 * np.pi * x) > > plt.figure(1) > plt.clf() > plt.plot(x, y) > plt.show() > > Now, I want to do a scatter plot, but don't want to generate the data > again. I just want the "line by line" evaluation like in R and Matlab. > Basically, you can type in console and add to the existing variables. If you just want an editor, use an editor, not an IDE. An IDE attempts to understand the totality of your project so it can give you suggestions on various things e.g. autocompletion for methods and so forth - as you describe above - and if that's not what you want, don't use one. Sadly, vim, which is The Best Editor Ever (see 23 million flamewars for why such statement can only be a feeble attempt at humor, not reality), is now trying to behave like an IDE, and it's doing things badly wrong. For example, if I type import in a "from" line, it helpfully inserts the word import... meaning those end up with syntax errors, "from foo import import bar". I don't know who is responsible for that idiocy and haven't taken the time to figure out how to shut off the misbehavior. You're not really talking about an editor with the rest of your description, though. You want to type something and have it acted on right away. The IDEs, and even the almost-IDE programs, can run the script with a quick key sequence, which is fairly close to what you're talking about. Sublime Text and Atom are two that are more editor than IDE, but still have IDE-like behavior. All this is just opinions... Another opinion is Jupyter notebook (formerly iPython notebook) might be closer to what you're looking for. From mats at wichmann.us Fri Jun 2 21:07:30 2017 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 2 Jun 2017 19:07:30 -0600 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: <20170603002723.GC17170@ando.pearwood.info> References: <20170603002723.GC17170@ando.pearwood.info> Message-ID: <7ea946a7-7d41-cde4-e4f7-8173d4fbe126@wichmann.us> > I don't think "what the authors might want" is the only factor here. > Personally, I think these programming challenge sites probably do more > harm than good, discouraging people that they're not good enough to be a > programmer because they can't solve the (often exceedingly tricky) > problems on their own. I think they're often dick-measuring contests, > for elite programmers to show off and sneer at "lesser mortals" who > can't solve the problems. > > In the real world, nobody has to solve these sorts of problems under the > constraints given. In real life programming, you get to look for > existing solutions, you get to consult with your colleagues, pass ideas > back and forth, etc. If you need a solution to X, and your colleague > already solved it for another project, you say "Hey Fred, I'm stealing > your code" and if Fred gets upset you talk to his project manager who > tells Fred to cooperate. Indeed... they're a slightly tamer variant of the even worse "clickbait" articles like "how to answer the 10 top Python interview questions", not a single one I've ever seen being something I'd expect to be part of a competent interview process. However, I still don't like the idea of answering people's quizzes. I won't violently disagree with Steven's viewpoint, however. From tmrsg11 at gmail.com Sat Jun 3 02:20:11 2017 From: tmrsg11 at gmail.com (Mike C) Date: Sat, 3 Jun 2017 06:20:11 +0000 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: <85mv9py64p.fsf@benfinney.id.au> References: , <85mv9py64p.fsf@benfinney.id.au> Message-ID: Hi Ben, I have not used Vim or Emacs for a very long time. I am spoiled by the friendly interface of RStudio. There is a high demand for Python in the industry, but there has not been a good IDE. I find that strange. _____________________________ From: Ben Finney > Sent: Saturday, June 3, 2017 1:50 AM Subject: Re: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder To: > C W > writes: > I am an R user learning Python. What is a good editor? Either of Vim or Emacs ? together with a good multi-tabbed terminal program ? make an excellent programmer IDE. -- \ ?When I was a baby I kept a diary. Recently I was re-reading | `\ it, it said ?Day 1: Still tired from the move. Day 2: Everybody | _o__) talks to me like I'm an idiot.?? ?Steven Wright | Ben Finney _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Sat Jun 3 04:06:07 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 3 Jun 2017 09:06:07 +0100 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: <85mv9py64p.fsf@benfinney.id.au> Message-ID: On 03/06/17 07:20, Mike C wrote: > There is a high demand for Python in the > industry, but there has not been a good IDE. There are a ton of IDEs for Python including the generic ones like VS, Eclipse and Netbeans. But... I've tried many of these and find I keep coming back to the simpler 3-window approach to Python development: - A code editor(multi-tabbed), - an open interpreter session and - an OS prompt for running/testing the program. I always find that faster and more effective than a complex IDE. I'm not against IDEs in general and for C++ and Java I find an IDE more or less essential. But the immediacy and simplicity of Python with its interpreter is very hard to beat. (It's the closest thing I've found to the Smalltalk workspace for productive programming) > I find that strange. It seems so until you try it. IDEs and Python just don't work that well for many people. (Of course there are also many folks who do like them and use them, but compared to other languages they are less used, because they offer less benefit.) -- 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 From jslozier at gmail.com Sat Jun 3 10:52:25 2017 From: jslozier at gmail.com (Jay Lozier) Date: Sat, 3 Jun 2017 10:52:25 -0400 Subject: [Tutor] New blog that has solution for python programs In-Reply-To: <7ea946a7-7d41-cde4-e4f7-8173d4fbe126@wichmann.us> References: <20170603002723.GC17170@ando.pearwood.info> <7ea946a7-7d41-cde4-e4f7-8173d4fbe126@wichmann.us> Message-ID: <2c270277-4483-89c3-e4c0-1819a95da7d0@gmail.com> On 06/02/2017 09:07 PM, Mats Wichmann wrote: >> I don't think "what the authors might want" is the only factor here. >> Personally, I think these programming challenge sites probably do more >> harm than good, discouraging people that they're not good enough to be a >> programmer because they can't solve the (often exceedingly tricky) >> problems on their own. I think they're often dick-measuring contests, >> for elite programmers to show off and sneer at "lesser mortals" who >> can't solve the problems. >> >> In the real world, nobody has to solve these sorts of problems under the >> constraints given. In real life programming, you get to look for >> existing solutions, you get to consult with your colleagues, pass ideas >> back and forth, etc. If you need a solution to X, and your colleague >> already solved it for another project, you say "Hey Fred, I'm stealing >> your code" and if Fred gets upset you talk to his project manager who >> tells Fred to cooperate. > Indeed... they're a slightly tamer variant of the even worse "clickbait" > articles like "how to answer the 10 top Python interview questions", not > a single one I've ever seen being something I'd expect to be part of a > competent interview process. > > However, I still don't like the idea of answering people's quizzes. I > won't violently disagree with Steven's viewpoint, however. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor "Hey Fred, I'm stealing your code" - in the group I am in we are expected to use each other's code. Reusing known, good, working code saves time, money, and effort. Also, we are expected to ask each other for advice as needed. From tmrsg11 at gmail.com Sat Jun 3 11:45:00 2017 From: tmrsg11 at gmail.com (C W) Date: Sat, 3 Jun 2017 11:45:00 -0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: <85mv9py64p.fsf@benfinney.id.au> Message-ID: I want to run something from script and edit in the console. For example, pseudo code 1) I run a Monte Carlo simulation for 5 min, I got simulation results. > my_sim = mcmc(1000) 2) I calculate the mean, var, sd, of the simulated numbers > mean = sum(my_sim)/100 > and so on... 3) I want to revise my mean and sd formula For step 3, you can just highlight that portion of the script with the modified formula. It's very convenient! In another word, I want to do trial and error, play with the code and see what comes out. Not running everything from the top every time. Thank you for all your suggestions, I appreciate it! On Sat, Jun 3, 2017 at 4:06 AM, Alan Gauld via Tutor wrote: > On 03/06/17 07:20, Mike C wrote: > > > There is a high demand for Python in the > > industry, but there has not been a good IDE. > There are a ton of IDEs for Python including the > generic ones like VS, Eclipse and Netbeans. > But... I've tried many of these and find I keep > coming back to the simpler 3-window approach to > Python development: > - A code editor(multi-tabbed), > - an open interpreter session and > - an OS prompt for running/testing the program. > > I always find that faster and more effective > than a complex IDE. > > I'm not against IDEs in general and for C++ and > Java I find an IDE more or less essential. But > the immediacy and simplicity of Python with its > interpreter is very hard to beat. (It's the closest > thing I've found to the Smalltalk workspace for > productive programming) > > > I find that strange. > > It seems so until you try it. IDEs and Python > just don't work that well for many people. (Of > course there are also many folks who do like them > and use them, but compared to other languages they > are less used, because they offer less benefit.) > > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From francois.dion at gmail.com Sat Jun 3 21:56:04 2017 From: francois.dion at gmail.com (Francois Dion) Date: Sat, 3 Jun 2017 21:56:04 -0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: PyCharm has support for cell based notebooks, interactive python console etc. You can set up the layout so it mirrors Rstudio. Spyder should have installed correctly. How did you install? Having said that, you should also look into JupyterLab: https://github.com/jupyterlab/jupyterlab, YHat's Rodeo: https://www.yhat.com/products/rodeo and of course, vim + tmux + tmuxp and something like sidecar: https://github.com/smashwilson/jupyter-sidecar Enthought's Canopy: https://store.enthought.com/downloads/#default All of them can be configured to replicate pretty much the same workflow as rstudio, although there's a cliff waiting when deploying this stuff to production. I've sat in many conferences and meetups where the speaker is demoing his code directly in Rstudio and, of course, gets to the point where there's an undefined variable. "oh yeah, I forgot about that..." Not a knock against R, just something to be aware of in systems that allow non linear code execution (like Jupyter notebooks, be it R, Python, Octave, Coconut or anything else that runs in it). Also, look into the rpy2 python module and feather-format. That way you can still go back and forth with R. Francois On Fri, Jun 2, 2017 at 1:46 PM, C W wrote: > Dear Python list, > > I am an R user learning Python. What is a good editor? > > 1) Pycharm > PyCharm evaluates the entire script, I just want to change a few lines in > the script. > For example, > > import matplotlib.pyplot as plt > import numpy as np > > x = np.arange(0, 1,0.1) > y = np.sin(2 * np.pi * x) > > plt.figure(1) > plt.clf() > plt.plot(x, y) > plt.show() > > Now, I want to do a scatter plot, but don't want to generate the data > again. I just want the "line by line" evaluation like in R and Matlab. > Basically, you can type in console and add to the existing variables. > > 2) Spyder > Spyder looks a lot like RStudio, I like it! But, it does not have an app > icon in applications. I am baffled. I do ~/anaconda/bin/spyder every time. > > Am I missing something or is this the way it is? > > Thank you very much! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info - @f_dion From ben+python at benfinney.id.au Sat Jun 3 21:56:05 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 04 Jun 2017 11:56:05 +1000 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder References: <85mv9py64p.fsf@benfinney.id.au> Message-ID: <85efv0y0qy.fsf@benfinney.id.au> C W writes: > In another word, I want to do trial and error, play with the code and > see what comes out. Not running everything from the top every time. Already suggested, but I will repeat: You will find that Jupyter Notebook is explicitly designed to make that easy. -- \ ?Now Maggie, I?ll be watching you too, in case God is busy | `\ creating tornadoes or not existing.? ?Homer, _The Simpsons_ | _o__) | Ben Finney From interzone at gmail.com Sat Jun 3 22:41:36 2017 From: interzone at gmail.com (Dylan Distasio) Date: Sat, 3 Jun 2017 22:41:36 -0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: <85efv0y0qy.fsf@benfinney.id.au> References: <85mv9py64p.fsf@benfinney.id.au> <85efv0y0qy.fsf@benfinney.id.au> Message-ID: +1 on Jupyter notebooks, but I also wanted to mention that Spyder will do what you want. You just need to highlight the code fragment of interest to rerun, and then hit ctrl-enter. It will proceed to run just that highlighted section of code. On Sat, Jun 3, 2017 at 9:56 PM, Ben Finney wrote: > C W writes: > > > In another word, I want to do trial and error, play with the code and > > see what comes out. Not running everything from the top every time. > > Already suggested, but I will repeat: You will find that Jupyter > Notebook is explicitly designed to make that > easy. > > -- > \ ?Now Maggie, I?ll be watching you too, in case God is busy | > `\ creating tornadoes or not existing.? ?Homer, _The Simpsons_ | > _o__) | > Ben Finney > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From tmrsg11 at gmail.com Sat Jun 3 23:39:37 2017 From: tmrsg11 at gmail.com (Mike C) Date: Sun, 4 Jun 2017 03:39:37 +0000 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: <85efv0y0qy.fsf@benfinney.id.au> References: <85mv9py64p.fsf@benfinney.id.au> , <85efv0y0qy.fsf@benfinney.id.au> Message-ID: Hi Ben, Yes, I read your suggestion. I should have added the following in my earlier message. Jupyter runs in a web browser like Chrome, and you feed it in line by line, so if I want to run a project with a hundred lines, it may take a few. Anyways, Jupyter is the consensus, and I am trying it. Thank you! ________________________________ From: Tutor on behalf of Ben Finney Sent: Saturday, June 3, 2017 9:56:05 PM To: tutor at python.org Subject: Re: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder C W writes: > In another word, I want to do trial and error, play with the code and > see what comes out. Not running everything from the top every time. Already suggested, but I will repeat: You will find that Jupyter Notebook is explicitly designed to make that easy. -- \ ?Now Maggie, I?ll be watching you too, in case God is busy | `\ creating tornadoes or not existing.? ?Homer, _The Simpsons_ | _o__) | Ben Finney _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From arj.python at gmail.com Sun Jun 4 04:44:13 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Sun, 4 Jun 2017 12:44:13 +0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: Wing. Wing IDE personal works awesome for me Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 3 Jun 2017 02:59, "C W" wrote: > Dear Python list, > > I am an R user learning Python. What is a good editor? > > 1) Pycharm > PyCharm evaluates the entire script, I just want to change a few lines in > the script. > For example, > > import matplotlib.pyplot as plt > import numpy as np > > x = np.arange(0, 1,0.1) > y = np.sin(2 * np.pi * x) > > plt.figure(1) > plt.clf() > plt.plot(x, y) > plt.show() > > Now, I want to do a scatter plot, but don't want to generate the data > again. I just want the "line by line" evaluation like in R and Matlab. > Basically, you can type in console and add to the existing variables. > > 2) Spyder > Spyder looks a lot like RStudio, I like it! But, it does not have an app > icon in applications. I am baffled. I do ~/anaconda/bin/spyder every time. > > Am I missing something or is this the way it is? > > Thank you very much! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From wolfrage8765 at gmail.com Sun Jun 4 08:10:34 2017 From: wolfrage8765 at gmail.com (wolfrage8765 at gmail.com) Date: Sun, 4 Jun 2017 08:10:34 -0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: Atom.io Editor is my current favorite after having swapped around a lot. http://www.marinamele.com/install-and-configure-atom-editor-for-python From s.molnar at sbcglobal.net Sun Jun 4 08:42:12 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Sun, 4 Jun 2017 08:42:12 -0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: <59340024.5000409@sbcglobal.net> On 06/04/2017 04:44 AM, Abdur-Rahmaan Janhangeer wrote: > Wing. Wing IDE personal works awesome for me > > Abdur-Rahmaan Janhangeer, > Mauritius > abdurrahmaanjanhangeer.wordpress.com > > On 3 Jun 2017 02:59, "C W" wrote: > >> Dear Python list, >> >> I am an R user learning Python. What is a good editor? >> >> 1) Pycharm >> PyCharm evaluates the entire script, I just want to change a few lines in >> the script. >> For example, >> >> import matplotlib.pyplot as plt >> import numpy as np >> >> x = np.arange(0, 1,0.1) >> y = np.sin(2 * np.pi * x) >> >> plt.figure(1) >> plt.clf() >> plt.plot(x, y) >> plt.show() >> >> Now, I want to do a scatter plot, but don't want to generate the data >> again. I just want the "line by line" evaluation like in R and Matlab. >> Basically, you can type in console and add to the existing variables. >> >> 2) Spyder >> Spyder looks a lot like RStudio, I like it! But, it does not have an app >> icon in applications. I am baffled. I do ~/anaconda/bin/spyder every time. >> >> Am I missing something or is this the way it is? >> >> Thank you very much! >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Here's how I start Spyder, in Xfce: /home/comp/Apps/anaconda3/bin/spyder -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From Ryan at allwegot.net Sun Jun 4 11:38:56 2017 From: Ryan at allwegot.net (Ryan Smith) Date: Sun, 04 Jun 2017 15:38:56 +0000 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: +1 for Wing IDE. I have been using it for about 6-7 months now and absolutely love it. Ryan On Sun, Jun 4, 2017 at 11:37 AM Ryan Smith wrote: > > On Sun, Jun 4, 2017 at 8:13 AM wolfrage8765 at gmail.com < > wolfrage8765 at gmail.com> wrote: > >> Atom.io Editor is my current favorite after having swapped around a lot. >> >> http://www.marinamele.com/install-and-configure-atom-editor-for-python >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > From nancyphng at sbcglobal.net Mon Jun 5 17:04:27 2017 From: nancyphng at sbcglobal.net (Nancy Pham-Nguyen) Date: Mon, 5 Jun 2017 21:04:27 +0000 (UTC) Subject: [Tutor] f.readlines(size) References: <684008151.2786205.1496696667226.ref@mail.yahoo.com> Message-ID: <684008151.2786205.1496696667226@mail.yahoo.com> Hi, I'm trying to understand the optional size argument in file.readlines method. The help(file) shows: ?| ?readlines(...)?| ? ? ?readlines([size]) -> list of strings, each a line from the file.?| ? ? ??| ? ? ?Call readline() repeatedly and return a list of the lines so read.?| ? ? ?The optional size argument, if given, is an approximate bound on the?| ? ? ?total number of bytes in the lines returned. >From the documentation:f.readlines() returns a list containing all the lines of data in the file. If given an optional parameter sizehint, it reads that many bytes from the file and enough more to complete a line, and returns the lines from that. This is often used to allow efficient reading of a large file by lines, but without having to load the entire file in memory. Only complete lines will be returned. I wrote the function below to try it, thinking that it would print multiple times, 3 lines at a time, but it printed all in one shot, just like when I din't specify the optional argument. Could someone explain what I've missed? See input file and output below. Thanks,Nancy ? def readLinesWithSize():? ? ? # bufsize = 65536 ? ? ? bufsize = 45? ? ? with open('input.txt') as f:? ? ? ? ?while True:? ? ? ? ? ? ?# print len(f.readlines(bufsize)) ? # this will print 33? ? ? ? ? ? ?print? ? ? ? ? ? ?lines = f.readlines(bufsize)? ? ? ? ? ? ?print lines? ? ? ? ? ? ?if not lines:? ? ? ? ? ? ? ? ?break? ? ? ? ? ? ?for line in lines:? ? ? ? ? ? ? ? ?pass? ? ??readLinesWithSize() Output: ['1CSCO,100,18.04\n', '2ANTM,200,45.03\n', '3CSCO,150,19.05\n', '4MSFT,250,80.56\n', '5IBM,500,22.01\n', '6ANTM,250,44.23\n', '7GOOG,200,501.45\n', '8CSCO,175,19.56\n', '9MSFT,75,80.81\n', '10GOOG,300,502.65\n', '11IBM,150,25.01\n', '12CSCO1,100,18.04\n', '13ANTM1,200,45.03\n', '14CSCO1,150,19.05\n', '15MSFT1,250,80.56\n', '16IBM1,500,22.01\n', '17ANTM1,250,44.23\n', '18GOOG1,200,501.45\n', '19CSCO1,175,19.56\n', '20MSFT1,75,80.81\n', '21GOOG1,300,502.65\n', '22IBM1,150,25.01\n', '23CSCO2,100,18.04\n', '24ANTM2,200,45.03\n', '25CSCO2,150,19.05\n', '26MSFT2,250,80.56\n', '27IBM2,500,22.01\n', '28ANTM2,250,44.23\n', '29GOOG2,200,501.45\n', '30CSCO2,175,19.56\n', '31MSFT2,75,80.81\n', '32GOOG2,300,502.65\n', '33IBM2,150,25.01\n'] [] The input file contains 33 lines of text, 15 or 16 letter each (15 - 16 bytes):1CSCO,100,18.042ANTM,200,45.033CSCO,150,19.054MSFT,250,80.565IBM,500,22.016ANTM,250,44.237GOOG,200,501.458CSCO,175,19.569MSFT,75,80.8110GOOG,300,502.6511IBM,150,25.0112CSCO1,100,18.0413ANTM1,200,45.0314CSCO1,150,19.0515MSFT1,250,80.5616IBM1,500,22.0117ANTM1,250,44.2318GOOG1,200,501.4519CSCO1,175,19.5620MSFT1,75,80.8121GOOG1,300,502.6522IBM1,150,25.0123CSCO2,100,18.0424ANTM2,200,45.0325CSCO2,150,19.0526MSFT2,250,80.5627IBM2,500,22.0128ANTM2,250,44.2329GOOG2,200,501.4530CSCO2,175,19.5631MSFT2,75,80.8132GOOG2,300,502.6533IBM2,150,25.0 From sch4444 at gmail.com Mon Jun 5 10:36:48 2017 From: sch4444 at gmail.com (Schtvveer Schvrveve) Date: Mon, 5 Jun 2017 16:36:48 +0200 Subject: [Tutor] Python - help with something most essential Message-ID: I need someone's help. I am not proficient in Python and I wish to understand something. I was in a job pre-screening process where I was asked to solve a simple problem. The problem was supposed to be solved in Python and it was supposed to take two arguments: filename and word. The program reads the file which is a .txt file containing a bunch of words and counts how many of those words in the file are anagrams of the argument. First I concocted this solution: import sys from collections import Counter def main(args): filename = args[1] word = args[2] print countAnagrams(word, filename) def countAnagrams(word, filename): fileContent = readFile(filename) counter = Counter(word) num_of_anagrams = 0 for i in range(0, len(fileContent)): if counter == Counter(fileContent[i]): num_of_anagrams += 1 return num_of_anagrams def readFile(filename): with open(filename) as f: content = f.readlines() content = [x.strip() for x in content] return content if __name__ == '__main__': main(sys.argv) Very quickly I received this comment: "Can you adjust your solution a bit so you less loops (as little as possible) and also reduce the memory usage footprint of you program?" I tried to rework the methods into this: def countAnagrams(word, filename): fileContent = readFile(filename) return sum(1 for _ in filter(lambda x: Counter(word) == Counter(x.strip()), fileContent)) def readFile(filename): with open(filename) as f: content = f.readlines() return content And I was rejected. I just wish to understand what I could have done for this to be better? I am a Python beginner, so I'm sure there are things I don't know, but I was a bit surprised at the abruptness of the rejection and I'm worried I'm doing something profoundly wrong. Thank you in advance From syedzaidi85 at hotmail.co.uk Mon Jun 5 16:06:01 2017 From: syedzaidi85 at hotmail.co.uk (syed zaidi) Date: Mon, 5 Jun 2017 20:06:01 +0000 Subject: [Tutor] Huge list comprehension Message-ID: hi, I would appreciate if you can help me suggesting a quick and efficient strategy for comparing multiple lists with one principal list I have about 125 lists containing about 100,000 numerical entries in each my principal list contains about 6 million entries. I want to compare each small list with main list and append yes/no or 0/1 in each new list corresponding to each of 125 lists The program is working but it takes ages to process huge files, Can someone pleases tell me how can I make this process fast. Right now it takes arounf 2 weeks to complete this task the code I have written and is working is as under: sample_name = [] main_op_list,principal_list = [],[] dictionary = {} with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r') as f: reader = csv.reader(f, dialect = 'excel', delimiter='\t') list2 = filter(None, reader) for i in range(len(list2)): col1 = list2[i][0] operon = list2[i][1] main_op_list.append(operon) col1 = col1.strip().split("_") sample_name = col1[0] if dictionary.get(sample_name): dictionary[sample_name].append(operon) else: dictionary[sample_name] = [] dictionary[sample_name].append(operon) locals().update(dictionary) ## converts dictionary keys to variables ##print DLF004 dict_values = dictionary.values() dict_keys = dictionary.keys() print dict_keys print len(dict_keys) main_op_list_np = np.array(main_op_list) DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1,DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1,DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1,DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] DOF004_1,DOF006_1,DOF007_1,DOF008_1,DOF009_1,DOF010_1,DOF011_1,DOF012_1,DOF013_1,DOF014_1,DOM001_1,DOM003_1,DOM005_1,DOM008_1,DOM010_1,DOM012_1,DOM013_1,DOM014_1,DOM015_1,DOM016_1,DOM017_1,DOM018_1,DOM019_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1,DOM025_1,DOM026_1 = [],[],[],[],[],[],[] NLF001_1,NLF002_1,NLF005_1,NLF006_1,NLF007_1,NLF008_1,NLF009_1,NLF010_1,NLF011_1,NLF012_1,NLF013_1,NLF014_1,NLF015_1,NLM001_1,NLM002_1,NLM003_1,NLM004_1,NLM005_1,NLM006_1,NLM007_1,NLM008_1,NLM009_1,NLM010_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1,NLM023_1,NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1,NLM029_1,NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1,NOF005_1,NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1,NOM004_1,NOM005_1,NOM007_1,NOM008_1,NOM009_1,NOM010_1,NOM012_1,NOM013_1,NOM015_1,NOM016_1,NOM017_1,NOM018_1,NOM019_1,NOM020_1,NOM022_1,NOM023_1,NOM025_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] NOM026_1,NOM027_1,NOM028_1,NOM029_1 = [],[],[],[] for i in main_op_list_np: if i in DLF002: DLF002_1.append('1') else:DLF002_1.append('0') if i in DLF004: DLF004_1.append('1') else:DLF004_1.append('0') if i in DLF005: DLF005_1.append('1') else:DLF005_1.append('0') if i in DLF006: DLF006_1.append('1') else:DLF006_1.append('0') if i in DLF007: DLF007_1.append('1') else:DLF007_1.append('0') if i in DLF008: DLF008_1.append('1') else:DLF008_1.append('0') ## if main_op_list[i] in DLF009: DLF009_1.append('1') ## else:DLF009_1.append('0') if i in DLF010: DLF010_1.append('1') else:DLF010_1.append('0') if i in DLF012: DLF012_1.append('1') else:DLF012_1.append('0') if i in DLF013: DLF013_1.append('1') else:DLF013_1.append('0') if i in DLF014: DLF014_1.append('1') else:DLF014_1.append('0') if i in DLM001: DLM001_1.append('1') else:DLM001_1.append('0') if i in DLM002: DLM002_1.append('1') else:DLM002_1.append('0') if i in DLM003: DLM003_1.append('1') else:DLM003_1.append('0') if i in DLM004: DLM004_1.append('1') else:DLM004_1.append('0') if i in DLM005: DLM005_1.append('1') else:DLM005_1.append('0') if i in DLM006: DLM006_1.append('1') else:DLM006_1.append('0') if i in DLM009: DLM009_1.append('1') else:DLM009_1.append('0') if i in DLM011: DLM011_1.append('1') else:DLM011_1.append('0') if i in DLM012: DLM012_1.append('1') else:DLM012_1.append('0') if i in DLM018: DLM018_1.append('1') else:DLM018_1.append('0') if i in DOF002: DOF002_1.append('1') else:DOF002_1.append('0') if i in DOF003: DOF003_1.append('1') else:DOF003_1.append('0') if i in DOF004: DOF004_1.append('1') else:DOF004_1.append('0') if i in DOF006: DOF006_1.append('1') else:DOF006_1.append('0') if i in DOF007: DOF007_1.append('1') else:DOF007_1.append('0') if i in DOF008: DOF008_1.append('1') else:DOF008_1.append('0') if i in DOF009: DOF009_1.append('1') else:DOF009_1.append('0') if i in DOF010: DOF010_1.append('1') else:DOF010_1.append('0') if i in DOF011: DOF011_1.append('1') else:DOF011_1.append('0') if i in DOF012: DOF012_1.append('1') else:DOF012_1.append('0') if i in DOF013: DOF013_1.append('1') else:DOF013_1.append('0') if i in DOF014: DOF014_1.append('1') else:DOF014_1.append('0') if i in DOM001: DOM001_1.append('1') else:DOM001_1.append('0') if i in DOM003: DOM003_1.append('1') else:DOM003_1.append('0') if i in DOM005: DOM005_1.append('1') else:DOM005_1.append('0') if i in DOM008: DOM008_1.append('1') else:DOM008_1.append('0') if i in DOM010: DOM010_1.append('1') else:DOM010_1.append('0') if i in DOM012: DOM012_1.append('1') else:DOM012_1.append('0') if i in DOM013: DOM013_1.append('1') else:DOM013_1.append('0') if i in DOM014: DOM014_1.append('1') else:DOM014_1.append('0') if i in DOM015: DOM015_1.append('1') else:DOM015_1.append('0') if i in DOM016: DOM016_1.append('1') else:DOM016_1.append('0') if i in DOM017: DOM017_1.append('1') else:DOM017_1.append('0') if i in DOM018: DOM018_1.append('1') else:DOM018_1.append('0') if i in DOM019: DOM019_1.append('1') else:DOM019_1.append('0') if i in DOM020: DOM020_1.append('1') else:DOM020_1.append('0') if i in DOM021: DOM021_1.append('1') else:DOM021_1.append('0') if i in DOM022: DOM022_1.append('1') else:DOM022_1.append('0') if i in DOM023: DOM023_1.append('1') else:DOM023_1.append('0') if i in DOM024: DOM024_1.append('1') else:DOM024_1.append('0') if i in DOM025: DOM025_1.append('1') else:DOM025_1.append('0') if i in DOM026: DOM026_1.append('1') else:DOM026_1.append('0') if i in NLF001: NLF001_1.append(' | 1') else:NLF001_1.append(' | 0') if i in NLF002: NLF002_1.append('1') else:NLF002_1.append('0') if i in NLF005: NLF005_1.append('1') else:NLF005_1.append('0') if i in NLF006: NLF006_1.append('1') else:NLF006_1.append('0') if i in NLF007: NLF007_1.append('1') else:NLF007_1.append('0') if i in NLF008: NLF008_1.append('1') else:NLF008_1.append('0') if i in NLF009: NLF009_1.append('1') else:NLF009_1.append('0') if i in NLF010: NLF010_1.append('1') else:NLF010_1.append('0') if i in NLF011: NLF011_1.append('1') else:NLF011_1.append('0') if i in NLF012: NLF012_1.append('1') else:NLF012_1.append('0') if i in NLF013: NLF013_1.append('1') else:NLF013_1.append('0') if i in NLF014: NLF014_1.append('1') else:NLF014_1.append('0') if i in NLF015: NLF015_1.append('1') else:NLF015_1.append('0') if i in NLM001: NLM001_1.append('1') else:NLM001_1.append('0') if i in NLM002: NLM002_1.append('1') else:NLM002_1.append('0') if i in NLM003: NLM003_1.append('1') else:NLM003_1.append('0') if i in NLM004: NLM004_1.append('1') else:NLM004_1.append('0') if i in NLM005: NLM005_1.append('1') else:NLM005_1.append('0') if i in NLM006: NLM006_1.append('1') else:NLM006_1.append('0') if i in NLM007: NLM007_1.append('1') else:NLM007_1.append('0') if i in NLM008: NLM008_1.append('1') else:NLM008_1.append('0') if i in NLM009: NLM009_1.append('1') else:NLM009_1.append('0') if i in NLM010: NLM010_1.append('1') else:NLM010_1.append('0') if i in NLM015: NLM015_1.append('1') else:NLM015_1.append('0') if i in NLM016: NLM016_1.append('1') else:NLM016_1.append('0') if i in NLM017: NLM017_1.append('1') else:NLM017_1.append('0') if i in NLM021: NLM021_1.append('1') else:NLM021_1.append('0') if i in NLM022: NLM022_1.append('1') else:NLM022_1.append('0') if i in NLM023: NLM023_1.append('1') else:NLM023_1.append('0') if i in NLM024: NLM024_1.append('1') else:NLM024_1.append('0') if i in NLM025: NLM025_1.append('1') else:NLM025_1.append('0') if i in NLM026: NLM026_1.append('1') else:NLM026_1.append('0') if i in NLM027: NLM027_1.append('1') else:NLM027_1.append('0') if i in NLM028: NLM028_1.append('1') else:NLM028_1.append('0') if i in NLM029: NLM029_1.append('1') else:NLM029_1.append('0') if i in NLM031: NLM031_1.append('1') else:NLM031_1.append('0') if i in NLM032: NLM032_1.append('1') else:NLM032_1.append('0') if i in NOF001: NOF001_1.append('1') else:NOF001_1.append('0') if i in NOF002: NOF002_1.append('1') else:NOF002_1.append('0') if i in NOF004: NOF004_1.append('1') else:NOF004_1.append('0') if i in NOF005: NOF005_1.append('1') else:NOF005_1.append('0') if i in NOF006: NOF006_1.append('1') else:NOF006_1.append('0') if i in NOF007: NOF007_1.append('1') else:NOF007_1.append('0') if i in NOF008: NOF008_1.append('1') else:NOF008_1.append('0') if i in NOF009: NOF009_1.append('1') else:NOF009_1.append('0') if i in NOF010: NOF010_1.append('1') else:NOF010_1.append('0') if i in NOF011: NOF011_1.append('1') else:NOF011_1.append('0') if i in NOF012: NOF012_1.append('1') else:NOF012_1.append('0') if i in NOF013: NOF013_1.append('1') else:NOF013_1.append('0') if i in NOF014: NOF014_1.append('1') else:NOF014_1.append('0') if i in NOM001: NOM001_1.append('1') else:NOM001_1.append('0') if i in NOM002: NOM002_1.append('1') else:NOM002_1.append('0') if i in NOM004: NOM004_1.append('1') else:NOM004_1.append('0') if i in NOM005: NOM005_1.append('1') else:NOM005_1.append('0') if i in NOM007: NOM007_1.append('1') else:NOM007_1.append('0') if i in NOM008: NOM008_1.append('1') else:NOM008_1.append('0') if i in NOM009: NOM009_1.append('1') else:NOM009_1.append('0') if i in NOM010: NOM010_1.append('1') else:NOM010_1.append('0') if i in NOM012: NOM012_1.append('1') else:NOM012_1.append('0') if i in NOM013: NOM013_1.append('1') else:NOM013_1.append('0') if i in NOM015: NOM015_1.append('1') else:NOM015_1.append('0') if i in NOM016: NOM016_1.append('1') else:NOM016_1.append('0') if i in NOM017: NOM017_1.append('1') else:NOM017_1.append('0') if i in NOM018: NOM018_1.append('1') else:NOM018_1.append('0') if i in NOM019: NOM019_1.append('1') else:NOM019_1.append('0') if i in NOM020: NOM020_1.append('1') else:NOM020_1.append('0') if i in NOM022: NOM022_1.append('1') else:NOM022_1.append('0') if i in NOM023: NOM023_1.append('1') else:NOM023_1.append('0') if i in NOM025: NOM025_1.append('1') else:NOM025_1.append('0') if i in NOM026: NOM026_1.append('1') else:NOM026_1.append('0') if i in NOM027: NOM027_1.append('1') else:NOM027_1.append('0') if i in NOM028: NOM028_1.append('1') else:NOM028_1.append('0') if i in NOM029: NOM029_1.append('1') else:NOM029_1.append('0') ## print 'saving' zoo = zip(main_op_list, DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1,DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1,DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1,DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1,DOF004_1,DOF006_1,DOF007_1,DOF008_1,DOF009_1,DOF010_1,DOF011_1,DOF012_1,DOF013_1,DOF014_1,DOM001_1,DOM003_1,DOM005_1,DOM008_1,DOM010_1,DOM012_1,DOM013_1,DOM014_1,DOM015_1,DOM016_1,DOM017_1,DOM018_1,DOM019_1,DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1,DOM025_1,DOM026_1,NLF001_1,NLF002_1,NLF005_1,NLF006_1,NLF007_1,NLF008_1,NLF009_1,NLF010_1,NLF011_1,NLF012_1,NLF013_1,NLF014_1,NLF015_1,NLM001_1,NLM002_1,NLM003_1,NLM004_1,NLM005_1,NLM006_1,NLM007_1,NLM008_1,NLM009_1,NLM010_1,NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1,NLM023_1,NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1,NLM029_1,NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1,NOF005_1,NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1,NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1,NOM004_1,NOM005_1,NOM007_1,NOM008_1,NOM009_1,NOM010_1,NOM012_1,NOM013_1,NOM015_1,NOM016_1,NOM017_1,NOM018_1,NOM019_1,NOM020_1,NOM022_1,NOM023_1,NOM025_1,NOM026_1,NOM027_1,NOM028_1,NOM029_1) with open("test.tab", 'w+') as outfile: writer =csv.writer(outfile, delimiter = '\t', lineterminator = '\n') writer.writerow([' ','DLF2','DLF4','DLF5','DLF6','DLF7','DLF8','DLF9','DLF10','DLF12','DLF13','DLF14','DLM1','DLM2','DLM3','DLM4','DLM5','DLM6','DLM9','DLM11','DLM12','DLM18','DOF2','DOF3','DOF4','DOF6','DOF7','DOF8','DOF9','DOF10','DOF11','DOF12','DOF13','DOF04','DOM1','DOM3','DOM5','DOM8','DOM10','DOM12','DOM13','DOM14','DOM15','DOM16','DOM17','DOM18','DOM19','DOM20','DOM21','DOM22','DOM23','DOM24','DOM25','DOM26','NLF1','NLF2','NLF5','NLF6','NLF7','NLF8','NLF9','NLF10','NLF11','NLF12','NLF13','NLF14','NLF15','NLM1','NLM2','NLM3','NLM4','NLM5','NLM6','NLM7','NLM8','NLM9','NLM10','NLM15','NLM16','NLM17','NLM21','NLM22','NLM23','NLM24','NLM25','NLM26','NLM27','NLM28','NLM29','NLM31','NLM32','NOF1','NOF2','NOF4','NOF5','NOF6','NOF7','NOF8','NOF9','NOF10','NOF11','NOF12','NOF13','NOF14','NOM1','NOM2','NOM4','NOM5','NOM7','NOM8','NOM9','NOM10','NOM12','NOM13','NOM15','NOM16','NOM17','NOM18','NOM19','NOM20','NOM22','NOM23','NOM25','NOM26','NOM27','NOM28','NOM29']) writer.writerows(zoo) outfile.close() print 'done' end_time = time.time() elapsed = end_time-start_time print "Time elapsed.", elapsed Thanks Best Regards Syed Shujaat Ali Zaidi PhD Scholar (Bioinformatics) MOE Key Laboratory of Bioinformatics Bioinformatics Division, TNLIST & Department of Automation FIT 1-107, Tsinghua University, Beijing 100084, China Lecturer (Bioinformatics) Department of Bio Sciences COMSATS Institute of Information Technology Islamabad, Pakistan From david at graniteweb.com Mon Jun 5 21:48:05 2017 From: david at graniteweb.com (David Rock) Date: Mon, 5 Jun 2017 20:48:05 -0500 Subject: [Tutor] Python - help with something most essential In-Reply-To: References: Message-ID: > On Jun 5, 2017, at 09:36, Schtvveer Schvrveve wrote: > > > And I was rejected. I just wish to understand what I could have done for > this to be better? > > I am a Python beginner, so I'm sure there are things I don't know, but I > was a bit surprised at the abruptness of the rejection and I'm worried I'm > doing something profoundly wrong. The main thing that jumps out to me is the memory issue they asked you to address was not addressed. In your readFile, you have with open(filename) as f: content = f.readlines() Which reads the entire file into memory. They specifically did not want you to do that. The implication is they were looking for a solution where you read the file maybe one line (or even one word) at a time and look for anagrams in smaller groups. ? David From __peter__ at web.de Tue Jun 6 02:31:03 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 06 Jun 2017 08:31:03 +0200 Subject: [Tutor] Python - help with something most essential References: Message-ID: Schtvveer Schvrveve wrote: > I need someone's help. I am not proficient in Python and I wish to > understand something. I was in a job pre-screening process where I was > asked to solve a simple problem. > > The problem was supposed to be solved in Python and it was supposed to > take two arguments: filename and word. The program reads the file which is > a .txt file containing a bunch of words and counts how many of those words > in the file are anagrams of the argument. > > First I concocted this solution: > > import sys > from collections import Counter > > def main(args): > filename = args[1] > word = args[2] > print countAnagrams(word, filename) > > def countAnagrams(word, filename): > > fileContent = readFile(filename) > > counter = Counter(word) > num_of_anagrams = 0 > > for i in range(0, len(fileContent)): > if counter == Counter(fileContent[i]): > num_of_anagrams += 1 > > return num_of_anagrams > > def readFile(filename): > > with open(filename) as f: > content = f.readlines() > > content = [x.strip() for x in content] > > return content > > if __name__ == '__main__': > main(sys.argv) > > Very quickly I received this comment: > > "Can you adjust your solution a bit so you less loops (as little as > possible) and also reduce the memory usage footprint of you program?" > > I tried to rework the methods into this: > > def countAnagrams(word, filename): > > fileContent = readFile(filename) > > return sum(1 for _ in filter(lambda x: Counter(word) == > Counter(x.strip()), fileContent)) > > def readFile(filename): > > with open(filename) as f: > content = f.readlines() > > return content > > And I was rejected. I just wish to understand what I could have done for > this to be better? > > I am a Python beginner, so I'm sure there are things I don't know, but I > was a bit surprised at the abruptness of the rejection and I'm worried I'm > doing something profoundly wrong. for i in range(0, len(stuff)): ... instead of for item in stuff: ... and content = file.readlines() # read the whole file into memory process(content) are pretty much the most obvious indicators that you are a total newbie in Python. Looks like they weren't willing to give you the time to iron that out on the job even though you knew about lambda, Counter, list comprehensions and generator expressions which are not newbie stuff. When upon their hint you did not address the root cause of the unbounded memory consumption they might have come to the conclusion that you were reproducing snippets you picked up somewhere and thus were cheating. From __peter__ at web.de Tue Jun 6 03:35:52 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 06 Jun 2017 09:35:52 +0200 Subject: [Tutor] f.readlines(size) References: <684008151.2786205.1496696667226.ref@mail.yahoo.com> <684008151.2786205.1496696667226@mail.yahoo.com> Message-ID: Nancy Pham-Nguyen wrote: > Hi, Hi Nancy, the only justification for the readlines() method is to serve as a trap to trick newbies into writing scripts that consume more memory than necessary. While the size argument offers a way around that, there are still next to no use cases for readlines. Iterating over a file directly is a very common operation and a lot of work to make it efficient was spent on it. Use it whenever possible. To read groups of lines consider # last chunk may be shorter with open(FILENAME) as f: while True: chunk = list(itertools.islice(f, 3)) if not chunk: break process_lines(chunk) or # last chunk may be filled with None values with open(FILENAME) as f: for chunk in itertools.zip_longest(f, f, f): # Py2: izip_longest process_lines(chunk) In both cases you will get chunks of three lines, the only difference being the handling of the last chunk. > I'm trying to understand the optional size argument in file.readlines > method. The help(file) shows: | readlines(...) | readlines([size]) > -> list of strings, each a line from the file. | | Call > readline() repeatedly and return a list of the lines so read. | The > optional size argument, if given, is an approximate bound on the | > total number of bytes in the lines returned. From the > documentation:f.readlines() returns a list containing all the lines of > data in the file. If given an optional parameter sizehint, it reads that > many bytes from the file and enough more to complete a line, and returns > the lines from that. This is often used to allow efficient reading of a > large file by lines, but without having to load the entire file in memory. > Only complete lines will be returned. I wrote the function below to try > it, thinking that it would print multiple times, 3 lines at a time, but it > printed all in one shot, just like when I din't specify the optional > argument. Could someone explain what I've missed? See input file and > output below. Thanks,Nancy > def readLinesWithSize(): > # bufsize = 65536 > bufsize = 45 > with open('input.txt') as f: while True: > # print len(f.readlines(bufsize)) # this will print 33 > print > lines = f.readlines(bufsize) print lines > if not lines: break for line in lines: > pass readLinesWithSize() Output: This seems to be messed up a little by a "helpful" email client. Therefore I'll give my own: $ cat readlines_demo.py LINESIZE=32 with open("tmp.txt", "w") as f: for i in range(30): f.write("{:02} {}\n".format(i, "x"*(LINESIZE-4))) BUFSIZE = LINESIZE*3-1 print("bufsize", BUFSIZE) with open("tmp.txt", "r") as f: while True: chunk = f.readlines(BUFSIZE) if not chunk: break print(sum(map(len, chunk)), "bytes:", chunk) $ python3 readlines_demo.py bufsize 95 96 bytes: ['00 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '01 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '02 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n'] 96 bytes: ['03 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '04 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '05 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n'] 96 bytes: ['06 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '07 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '08 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n'] ... So in Python 3 this does what you expect, readlines() stops collecting more lines once the total number of bytes exceeds those specified. """ readlines(...) method of _io.TextIOWrapper instance Return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint. """ In Python 2 the docstring is a little vague """ The optional size argument, if given, is an *approximate* *bound* on the total number of bytes in the lines returned. """ (emphasis mine) and it seems that small size values which defeat the goal of making the operation efficient are ignored: $ python readlines_demo.py ('bufsize', 95) (960, 'bytes:', ['00 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '01 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '28 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '29 ... xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n']) Playing around a bit on my system the minimum value with an effect seems to be about 2**13, but I haven't consulted the readlines source code to verify. From __peter__ at web.de Tue Jun 6 03:58:00 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 06 Jun 2017 09:58 +0200 Subject: [Tutor] Huge list comprehension References: Message-ID: syed zaidi wrote: > > hi, > > I would appreciate if you can help me suggesting a quick and efficient > strategy for comparing multiple lists with one principal list > > I have about 125 lists containing about 100,000 numerical entries in each > > my principal list contains about 6 million entries. > > I want to compare each small list with main list and append yes/no or 0/1 > in each new list corresponding to each of 125 lists > > > The program is working but it takes ages to process huge files, > Can someone pleases tell me how can I make this process fast. Right now it > takes arounf 2 weeks to complete this task > > > the code I have written and is working is as under: > > > sample_name = [] > > main_op_list,principal_list = [],[] > dictionary = {} > > with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r') > as f: > reader = csv.reader(f, dialect = 'excel', delimiter='\t') > list2 = filter(None, reader) > for i in range(len(list2)): > col1 = list2[i][0] > operon = list2[i][1] > main_op_list.append(operon) > col1 = col1.strip().split("_") > sample_name = col1[0] > if dictionary.get(sample_name): > dictionary[sample_name].append(operon) > else: > dictionary[sample_name] = [] > dictionary[sample_name].append(operon) > locals().update(dictionary) ## converts dictionary keys to variables Usually I'd refuse to go beyond the line above. DO NOT EVER WRITE CODE LIKE THAT. You have your data in a nice dict -- keep it there where it belongs. > ##print DLF004 > dict_values = dictionary.values() > dict_keys = dictionary.keys() > print dict_keys > print len(dict_keys) > main_op_list_np = np.array(main_op_list) > > DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1,DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1,DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1,DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1 > =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] This is mind-numbing... > for i in main_op_list_np: > if i in DLF002: DLF002_1.append('1') > else:DLF002_1.append('0') > if i in DLF004: DLF004_1.append('1') > else:DLF004_1.append('0') > if i in DLF005: DLF005_1.append('1') > else:DLF005_1.append('0') > if i in DLF006: DLF006_1.append('1') > else:DLF006_1.append('0') ... and this is, too. Remember, we are volunteers and keep your code samples small. Whether there are three if-else checks or one hundred -- the logic remains the same. Give us a small sample script and a small dataset to go with it, use dicts instead of dumping everything into the module namespace, explain the script's purpose in plain english, and identify the parts that take too long -- then I'll take another look. From cs at zip.com.au Mon Jun 5 23:58:41 2017 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 6 Jun 2017 13:58:41 +1000 Subject: [Tutor] f.readlines(size) In-Reply-To: <684008151.2786205.1496696667226@mail.yahoo.com> References: <684008151.2786205.1496696667226@mail.yahoo.com> Message-ID: <20170606035841.GA85228@cskk.homeip.net> On 05Jun2017 21:04, Nancy Pham-Nguyen wrote: >I'm trying to understand the optional size argument in file.readlines method. >The help(file) shows: >?| ?readlines(...)?| ? ? ?readlines([size]) -> list of strings, each a line >from the file.?| ? ? ??| ? ? ?Call readline() repeatedly and return a list of >the lines so read.?| ? ? ?The optional size argument, if given, is an >approximate bound on the?| ? ? ?total number of bytes in the lines returned. >From the documentation:f.readlines() returns a list containing all the lines of data in the file. >If given an optional parameter sizehint, it reads that many bytes from the >file >and enough more to complete a line, and returns the lines from that. >This is often used to allow efficient reading of a large file by lines, >but without having to load the entire file in memory. Only complete lines >will be returned. >I wrote the function below to try it, thinking that it would print multiple times, 3 lines at a time, but it printed all in one shot, just like when I din't specify the optional argument. Could someone explain what I've missed? See input file and output below. I'm using this to test: from __future__ import print_function import sys lines = sys.stdin.readlines(1023) print(len(lines)) print(sum(len(_) for _ in lines)) print(repr(lines)) I've fed it a 41760 byte input (the size isn't important except that it needs to be "big enough"). The output starts like this: 270 8243 and then the line listing. That 8243 looks interesting, being close to 8192, a power of 2. The documentation you quote says: The optional size argument, if given, is an approximate bound on the?total number of bytes in the lines returned. [...] it reads that many bytes from the file and enough more to complete a line, and returns the lines from that. It looks to me like readlines uses the sizehint somewhat liberally; the purpose as described in the doco is to read input efficiently without using an unbounded amount of memory. Imagine feeding readlines() a terabyte input file, without the sizehint. It would try to pull it all into memory. With the sizehint you get a simple form of batching of the input into smallish groups of lines. I would say, from my experiments here, that the underlying I/O is doing 8192 byte reads from the file as the default buffer. So although I've asked for 1023 bytes, readlines says something like: I want at least 1023 bytes; the I/O system loads 8192 bytes because that is its normal read size, then readlines picks up all the buffer. It does this so as to gather as many lines as readily available. It then asks for more data to complete the last line. The last line of my readlines() result is: %.class: %.java %.class-prereqs : $(("%.class-prereqs" G? From nancyphng at yahoo.com Mon Jun 5 18:53:23 2017 From: nancyphng at yahoo.com (Nancy Pham-Nguyen) Date: Mon, 5 Jun 2017 22:53:23 +0000 (UTC) Subject: [Tutor] f.readlines(size) In-Reply-To: <684008151.2786205.1496696667226@mail.yahoo.com> References: <684008151.2786205.1496696667226.ref@mail.yahoo.com> <684008151.2786205.1496696667226@mail.yahoo.com> Message-ID: <1193410876.2906703.1496703203555@mail.yahoo.com> Resend with my member's email address. Hi, I'm trying to understand the optional size argument in file.readlines method. The help(file) shows: ?| ?readlines(...)?| ? ? ?readlines([size]) -> list of strings, each a line from the file.?| ? ? ??| ? ? ?Call readline() repeatedly and return a list of the lines so read.?| ? ? ?The optional size argument, if given, is an approximate bound on the?| ? ? ?total number of bytes in the lines returned. >From the documentation:f.readlines() returns a list containing all the lines of data in the file. If given an optional parameter sizehint, it reads that many bytes from the file and enough more to complete a line, and returns the lines from that. This is often used to allow efficient reading of a large file by lines, but without having to load the entire file in memory. Only complete lines will be returned. I wrote the function below to try it, thinking that it would print multiple times, 3 lines at a time, but it printed all in one shot, just like when I din't specify the optional argument. Could someone explain what I've missed? See input file and output below. Thanks,Nancy ? def readLinesWithSize():? ? ? # bufsize = 65536 ? ? ? bufsize = 45? ? ? with open('input.txt') as f:? ? ? ? ?while True:? ? ? ? ? ? ?# print len(f.readlines(bufsize)) ? # this will print 33? ? ? ? ? ? ?print? ? ? ? ? ? ?lines = f.readlines(bufsize)? ? ? ? ? ? ?print lines? ? ? ? ? ? ?if not lines:? ? ? ? ? ? ? ? ?break? ? ? ? ? ? ?for line in lines:? ? ? ? ? ? ? ? ?pass? ? ??readLinesWithSize() Output: ['1CSCO,100,18.04\n', '2ANTM,200,45.03\n', '3CSCO,150,19.05\n', '4MSFT,250,80.56\n', '5IBM,500,22.01\n', '6ANTM,250,44.23\n', '7GOOG,200,501.45\n', '8CSCO,175,19.56\n', '9MSFT,75,80.81\n', '10GOOG,300,502.65\n', '11IBM,150,25.01\n', '12CSCO1,100,18.04\n', '13ANTM1,200,45.03\n', '14CSCO1,150,19.05\n', '15MSFT1,250,80.56\n', '16IBM1,500,22.01\n', '17ANTM1,250,44.23\n', '18GOOG1,200,501.45\n', '19CSCO1,175,19.56\n', '20MSFT1,75,80.81\n', '21GOOG1,300,502.65\n', '22IBM1,150,25.01\n', '23CSCO2,100,18.04\n', '24ANTM2,200,45.03\n', '25CSCO2,150,19.05\n', '26MSFT2,250,80.56\n', '27IBM2,500,22.01\n', '28ANTM2,250,44.23\n', '29GOOG2,200,501.45\n', '30CSCO2,175,19.56\n', '31MSFT2,75,80.81\n', '32GOOG2,300,502.65\n', '33IBM2,150,25.01\n'] [] The input file contains 33 lines of text, 15 or 16 letter each (15 - 16 bytes):1CSCO,100,18.042ANTM,200,45.033CSCO,150,19.054MSFT,250,80.565IBM,500,22.016ANTM,250,44.237GOOG,200,501.458CSCO,175,19.569MSFT,75,80.8110GOOG,300,502.6511IBM,150,25.0112CSCO1,100,18.0413ANTM1,200,45.0314CSCO1,150,19.0515MSFT1,250,80.5616IBM1,500,22.0117ANTM1,250,44.2318GOOG1,200,501.4519CSCO1,175,19.5620MSFT1,75,80.8121GOOG1,300,502.6522IBM1,150,25.0123CSCO2,100,18.0424ANTM2,200,45.0325CSCO2,150,19.0526MSFT2,250,80.5627IBM2,500,22.0128ANTM2,250,44.2329GOOG2,200,501.4530CSCO2,175,19.5631MSFT2,75,80.8132GOOG2,300,502.6533IBM2,150,25.0 From sebastian at fuentelibre.org Mon Jun 5 20:13:25 2017 From: sebastian at fuentelibre.org (Sebastian Silva) Date: Mon, 5 Jun 2017 19:13:25 -0500 Subject: [Tutor] best Python Web-Development Applications framework. In-Reply-To: References: Message-ID: <524d3645-80b6-938f-8d50-c700c44b1c71@fuentelibre.org> Hi, It's been almost a month since you asked but I have a novel suggestion for you to try: /Jappy/. Because of the nature of the web and existing browsers, you'll find that Python is applied predominantly server-side. While back-end programming can be interesting, I have recently become aware of how fun it is to program the browser with Python-like syntax [1]. I liked this approach so much that I wrote Jappy, a development environment for learning Python. This IDE can run locally without an Internet connection or from a static web host. Here's the sources with instructions: https://github.com/somosazucar/artisan And here's the IDE for demonstration to try in any browser: http://people.sugarlabs.org/~icarito/artisan/Jappy.activity/ Here's a brief list of implemented functionality: * Python 3 syntax and comparable performance * Tabbed Code editor with syntax highlighting and Solarized color scheme * Supports multiple files using Python's import syntax * Six examples demonstrating language and API features * Unicode support. Emojis you can use directly in your code :-) * Runs on Webkit2 / Chrome / Firefox browser engines (IE not tested) * Gives access to HTML5, CSS3 and Javascript * Saves session in Sugar or Sugarizer Journal if available * Export to .zip (compiled JS code + source) * Import from .zip or as individual files * Jappy library offers browser friendly print, inputAsync, clearScreen statements * Jappy itself is written in Python / RapydScript * Experimental standalone Android build and .XO bundle Tutor@ list is invited to please try it out and report feedback. I have started work to prepare a course to introduce Python to kids and have written this IDE for this purpose. However I enjoyed writing it and have plans for adding interesting functionality, as time allows. Happy learning! Sebastian [1] Python-like: RapydScript NG provides Python-3 to Javascript transcompilation directly in the browser. On 07/05/17 10:23, Jojo Mwebaze wrote: > Dear All, > > I am trying to figure out the best Python Web-Development Applications > framework. I trying to get started with building Web-Based Applications. > Kindly give advise which one is the best for a novice user > > Cheers, > Johnson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From mroswell at gmail.com Tue Jun 6 16:32:25 2017 From: mroswell at gmail.com (Margie Roswell) Date: Tue, 06 Jun 2017 20:32:25 +0000 Subject: [Tutor] best Python Web-Development Applications framework. In-Reply-To: References: Message-ID: I like gae-init, which is Flask, + Google Cloud + bootstrap + fontawesome, etc. On Sun, May 7, 2017, 11:27 AM Jojo Mwebaze wrote: > Dear All, > > I am trying to figure out the best Python Web-Development Applications > framework. I trying to get started with building Web-Based Applications. > Kindly give advise which one is the best for a novice user > > Cheers, > Johnson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From nancyphng at yahoo.com Wed Jun 7 03:52:24 2017 From: nancyphng at yahoo.com (Nancy Pham-Nguyen) Date: Wed, 7 Jun 2017 07:52:24 +0000 (UTC) Subject: [Tutor] f.readlines(size) In-Reply-To: References: <684008151.2786205.1496696667226.ref@mail.yahoo.com> <684008151.2786205.1496696667226@mail.yahoo.com> Message-ID: <1215702894.3399809.1496821944822@mail.yahoo.com> Hi Peter, Thanks a lot for pointing out the itertools.islice, itertools.izip_longest, and experimenting with readlines(size).I'm using Python 2 (you got me, a newbie learning readlines from Google's Python page). I used the itertools.islice and it worked like a charm. izip requires so many arguments if you want to read multiple lines at a time (for a large file). I notice now that my file was too small and so readlines(size) read the whole file. I increased my file size to 20,000 of those lines and it did read chunk of lines. The buffersize doesn't work the way it's explained but on some approximate bounds as you wrote.On my system, the number of lines read doesn't change for buffer size up to 8,192 (~2**8), same number of lines was read for buffer sizes between 8,193 and 16,374, then 16,375 to .... ?E.g. If the size I specified is in one of the range/bound, a certain number of lines will be read. Nancy From: Peter Otten <__peter__ at web.de> To: tutor at python.org Sent: Tuesday, June 6, 2017 12:36 AM Subject: Re: [Tutor] f.readlines(size) Nancy Pham-Nguyen wrote: > Hi, Hi Nancy, the only justification for the readlines() method is to serve as a trap to trick newbies into writing scripts that consume more memory than necessary. While the size argument offers a way around that, there are still next to no use cases for readlines. Iterating over a file directly is a very common operation and a lot of work to make it efficient was spent on it. Use it whenever possible. To read groups of lines consider # last chunk may be shorter with open(FILENAME) as f: ? ? while True: ? ? ? ? chunk = list(itertools.islice(f, 3)) ? ? ? ? if not chunk: ? ? ? ? ? ? break ? ? ? ? process_lines(chunk) or # last chunk may be filled with None values with open(FILENAME) as f: ? ? for chunk in itertools.zip_longest(f, f, f): # Py2: izip_longest ? ? ? ? process_lines(chunk) In both cases you will get chunks of three lines, the only difference being the handling of the last chunk. > I'm trying to understand the optional size argument in file.readlines > method. The help(file) shows: |? readlines(...) |? ? ? readlines([size]) > -> list of strings, each a line from the file. |? ? ? |? ? ? Call > readline() repeatedly and return a list of the lines so read. |? ? ? The > optional size argument, if given, is an approximate bound on the |? ? > total number of bytes in the lines returned. From the > documentation:f.readlines() returns a list containing all the lines of > data in the file. If given an optional parameter sizehint, it reads that > many bytes from the file and enough more to complete a line, and returns > the lines from that. This is often used to allow efficient reading of a > large file by lines, but without having to load the entire file in memory. > Only complete lines will be returned. I wrote the function below to try > it, thinking that it would print multiple times, 3 lines at a time, but it > printed all in one shot, just like when I din't specify the optional > argument. Could someone explain what I've missed? See input file and > output below. Thanks,Nancy > def readLinesWithSize(): >? ? # bufsize = 65536 >? ? bufsize = 45? ? ? >? ? with open('input.txt') as f:? ? ? ? while True:? ? ? ? >? ? ? ? # print len(f.readlines(bufsize))? # this will print 33? ? ? ? ? > print? ? ? ? ? ? > lines = f.readlines(bufsize)? ? ? ? ? ? print lines? ? >? ? ? ? if not lines:? ? ? ? ? ? ? ? break? ? ? ? ? ? for line in lines: >? ? ? ? ? ? ? ? pass? ? ? readLinesWithSize() Output: This seems to be messed up a little by a "helpful" email client. Therefore I'll give my own: $ cat readlines_demo.py LINESIZE=32 with open("tmp.txt", "w") as f: ? ? for i in range(30): ? ? ? ? f.write("{:02} {}\n".format(i, "x"*(LINESIZE-4))) BUFSIZE = LINESIZE*3-1 print("bufsize", BUFSIZE) with open("tmp.txt", "r") as f: ? ? while True: ? ? ? ? chunk = f.readlines(BUFSIZE) ? ? ? ? if not chunk: ? ? ? ? ? ? break ? ? ? ? print(sum(map(len, chunk)), "bytes:", chunk) $ python3 readlines_demo.py bufsize 95 96 bytes: ['00 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '01 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '02 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n'] 96 bytes: ['03 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '04 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '05 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n'] 96 bytes: ['06 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '07 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '08 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n'] ... So in Python 3 this does what you expect, readlines() stops collecting more lines once the total number of bytes exceeds those specified. """ readlines(...) method of _io.TextIOWrapper instance ? ? Return a list of lines from the stream. ? ? ? ? hint can be specified to control the number of lines read: no more ? ? lines will be read if the total size (in bytes/characters) of all ? ? lines so far exceeds hint. """ In Python 2 the docstring is a little vague """ The optional size argument, if given, is an *approximate* *bound* on the total number of bytes in the lines returned. """ (emphasis mine) and it seems that small size values which defeat the goal of making the operation efficient are ignored: $ python readlines_demo.py ('bufsize', 95) (960, 'bytes:', ['00 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '01 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '28 xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n', '29 ... xxxxxxxxxxxxxxxxxxxxxxxxxxxx\n']) Playing around a bit on my system the minimum value with an effect seems to be about 2**13, but I haven't consulted the readlines source code to verify. _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From nancyphng at sbcglobal.net Wed Jun 7 04:03:56 2017 From: nancyphng at sbcglobal.net (Nancy Pham-Nguyen) Date: Wed, 7 Jun 2017 08:03:56 +0000 (UTC) Subject: [Tutor] f.readlines(size) In-Reply-To: <20170606035841.GA85228@cskk.homeip.net> References: <684008151.2786205.1496696667226@mail.yahoo.com> <20170606035841.GA85228@cskk.homeip.net> Message-ID: <1496082337.3450616.1496822636735@mail.yahoo.com> Hi Cameron, Thanks for playing around and hinted about the 8192 bound. I got my question figured out, with your and Peter's help (Please read my reply to Peter). Cheers,Nancy? From: Cameron Simpson To: Nancy Pham-Nguyen Cc: "tutor at python.org" Sent: Tuesday, June 6, 2017 2:12 AM Subject: Re: [Tutor] f.readlines(size) On 05Jun2017 21:04, Nancy Pham-Nguyen wrote: >I'm trying to understand the optional size argument in file.readlines method. >The help(file) shows: >?| ?readlines(...)?| ? ? ?readlines([size]) -> list of strings, each a line >from the file.?| ? ? ??| ? ? ?Call readline() repeatedly and return a list of >the lines so read.?| ? ? ?The optional size argument, if given, is an >approximate bound on the?| ? ? ?total number of bytes in the lines returned. >From the documentation:f.readlines() returns a list containing all the lines of data in the file. >If given an optional parameter sizehint, it reads that many bytes from the >file >and enough more to complete a line, and returns the lines from that. >This is often used to allow efficient reading of a large file by lines, >but without having to load the entire file in memory. Only complete lines >will be returned. >I wrote the function below to try it, thinking that it would print multiple times, 3 lines at a time, but it printed all in one shot, just like when I din't specify the optional argument. Could someone explain what I've missed? See input file and output below. I'm using this to test: ? from __future__ import print_function ? import sys ? lines = sys.stdin.readlines(1023) ? print(len(lines)) ? print(sum(len(_) for _ in lines)) ? print(repr(lines)) I've fed it a 41760 byte input (the size isn't important except that it needs to be "big enough"). The output starts like this: ? 270 ? 8243 and then the line listing. That 8243 looks interesting, being close to 8192, a power of 2. The documentation you quote says: ? The optional size argument, if given, is an approximate bound on the?total ? number of bytes in the lines returned. [...] it reads that many bytes from ? the file and enough more to complete a line, and returns the lines from that. It looks to me like readlines uses the sizehint somewhat liberally; the purpose as described in the doco is to read input efficiently without using an unbounded amount of memory. Imagine feeding readlines() a terabyte input file, without the sizehint. It would try to pull it all into memory. With the sizehint you get a simple form of batching of the input into smallish groups of lines. I would say, from my experiments here, that the underlying I/O is doing 8192 byte reads from the file as the default buffer. So although I've asked for 1023 bytes, readlines says something like: I want at least 1023 bytes; the I/O system loads 8192 bytes because that is its normal read size, then readlines picks up all the buffer. It does this so as to gather as many lines as readily available. It then asks for more data to complete the last line. The last line of my readlines() result is: ? %.class: %.java %.class-prereqs : $(("%.class-prereqs" G? _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From memilanuk at gmail.com Thu Jun 8 13:42:46 2017 From: memilanuk at gmail.com (Monte Milanuk) Date: Thu, 8 Jun 2017 10:42:46 -0700 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: Have you looked at Rodeo (https://www.yhat.com/products/rodeo)? The UI looks a *lot* like R-Studio (for a reason)... From tmrsg11 at gmail.com Thu Jun 8 14:24:50 2017 From: tmrsg11 at gmail.com (C W) Date: Thu, 8 Jun 2017 14:24:50 -0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: Indeed, just installed it. Rodeo is a very close to RStudio. My review: It looks like Rodeo is still in earlier development stage, despite look alike, many features in RStudio don't exist in Rodeo. It lacks a keyboard configuration. Cmd + Enter combo works! It runs the current line of code, this will make up for all else. Hence, my hope is not lost. Overall: If you are used to RStudio and Matlab, this is the closest you will see. I am happy to discovery it. Thanks, Monte! To the earlier replies, and my opinion: Atom does not do line by line evaluation, you must compile it yourself, you can't modify things in Console, but re-run script. (I have tried on someone else's computer) IPython is great for presentation, but you need to start it up in Terminal, copy paste localhost address. You need to restart it if you are using it for Python 2 vs Python 3. Great, but tedious to work with. (I have it) Spyder does not have an icon in Application, you again need to start it up in Terminal with a few commands. It is slow to start up. (I have it) On Thu, Jun 8, 2017 at 1:42 PM, Monte Milanuk wrote: > Have you looked at Rodeo (https://www.yhat.com/products/rodeo)? The UI > looks a *lot* like R-Studio (for a reason)... > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Thu Jun 8 17:08:15 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 8 Jun 2017 22:08:15 +0100 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: References: Message-ID: On 08/06/17 19:24, C W wrote: > IPython is great for presentation, but you need to start it up in Terminal, > Spyder does not have an icon in Application, you again need to start it up > in Terminal with a few commands. It is slow to start up. (I have it) You should be able to create your own icons for both of these. Just drag them from finder to desktop and from there into Applications. (You might need to edit properties to set the interpreter - its a wee while since I used a Mac in anger - but I'm sure somebody here can help with that.) -- 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 From cs at zip.com.au Thu Jun 8 21:00:12 2017 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 9 Jun 2017 11:00:12 +1000 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: <4a75cc92-7d67-2e4b-f988-bed2603de9ae@wichmann.us> References: <4a75cc92-7d67-2e4b-f988-bed2603de9ae@wichmann.us> Message-ID: <20170609010012.GA89429@cskk.homeip.net> On 02Jun2017 18:14, Mats Wichmann wrote: >Sadly, vim, which is The Best Editor Ever (see 23 million flamewars for >why such statement can only be a feeble attempt at humor, not reality), >is now trying to behave like an IDE, and it's doing things badly wrong. >For example, if I type import in a "from" line, it helpfully inserts the >word import... meaning those end up with syntax errors, "from foo import >import bar". I don't know who is responsible for that idiocy and >haven't taken the time to figure out how to shut off the misbehavior. I'm sure it can be turned off; mine doesn't do this. I also turned off the auto-comment-continuation. Anyway, somewhat off topic. -- Cameron Simpson From arj.python at gmail.com Sat Jun 10 03:35:03 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Sat, 10 Jun 2017 11:35:03 +0400 Subject: [Tutor] Huge list comprehension In-Reply-To: References: Message-ID: take a look at numpy and don't necessarily give us the whole code. it becomes too long without purpose Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 6 Jun 2017 03:26, "syed zaidi" wrote: hi, I would appreciate if you can help me suggesting a quick and efficient strategy for comparing multiple lists with one principal list I have about 125 lists containing about 100,000 numerical entries in each my principal list contains about 6 million entries. I want to compare each small list with main list and append yes/no or 0/1 in each new list corresponding to each of 125 lists The program is working but it takes ages to process huge files, Can someone pleases tell me how can I make this process fast. Right now it takes arounf 2 weeks to complete this task the code I have written and is working is as under: sample_name = [] main_op_list,principal_list = [],[] dictionary = {} with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r') as f: reader = csv.reader(f, dialect = 'excel', delimiter='\t') list2 = filter(None, reader) for i in range(len(list2)): col1 = list2[i][0] operon = list2[i][1] main_op_list.append(operon) col1 = col1.strip().split("_") sample_name = col1[0] if dictionary.get(sample_name): dictionary[sample_name].append(operon) else: dictionary[sample_name] = [] dictionary[sample_name].append(operon) locals().update(dictionary) ## converts dictionary keys to variables ##print DLF004 dict_values = dictionary.values() dict_keys = dictionary.keys() print dict_keys print len(dict_keys) main_op_list_np = np.array(main_op_list) DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1, DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1, DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1, DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1 =[],[],[],[],[],[],[],[],[],[] ,[],[],[],[],[],[],[],[],[],[],[],[],[] DOF004_1,DOF006_1,DOF007_1,DOF008_1,DOF009_1,DOF010_1, DOF011_1,DOF012_1,DOF013_1,DOF014_1,DOM001_1,DOM003_1, DOM005_1,DOM008_1,DOM010_1,DOM012_1,DOM013_1,DOM014_1, DOM015_1,DOM016_1,DOM017_1,DOM018_1,DOM019_1 =[],[],[],[],[],[],[],[],[],[] ,[],[],[],[],[],[],[],[],[],[],[],[],[] DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1,DOM025_1,DOM026_1 = [],[],[],[],[],[],[] NLF001_1,NLF002_1,NLF005_1,NLF006_1,NLF007_1,NLF008_1, NLF009_1,NLF010_1,NLF011_1,NLF012_1,NLF013_1,NLF014_1, NLF015_1,NLM001_1,NLM002_1,NLM003_1,NLM004_1,NLM005_1, NLM006_1,NLM007_1,NLM008_1,NLM009_1,NLM010_1 =[],[],[],[],[],[],[],[],[],[] ,[],[],[],[],[],[],[],[],[],[],[],[],[] NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1,NLM023_1, NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1,NLM029_1, NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1,NOF005_1, NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1 =[],[],[],[],[],[],[],[],[],[] ,[],[],[],[],[],[],[],[],[],[],[],[],[] NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1, NOM004_1,NOM005_1,NOM007_1,NOM008_1,NOM009_1,NOM010_1, NOM012_1,NOM013_1,NOM015_1,NOM016_1,NOM017_1,NOM018_1, NOM019_1,NOM020_1,NOM022_1,NOM023_1,NOM025_1 =[],[],[],[],[],[],[],[],[],[] ,[],[],[],[],[],[],[],[],[],[],[],[],[] NOM026_1,NOM027_1,NOM028_1,NOM029_1 = [],[],[],[] for i in main_op_list_np: if i in DLF002: DLF002_1.append('1') else:DLF002_1.append('0') if i in DLF004: DLF004_1.append('1') else:DLF004_1.append('0') if i in DLF005: DLF005_1.append('1') else:DLF005_1.append('0') if i in DLF006: DLF006_1.append('1') else:DLF006_1.append('0') if i in DLF007: DLF007_1.append('1') else:DLF007_1.append('0') if i in DLF008: DLF008_1.append('1') else:DLF008_1.append('0') ## if main_op_list[i] in DLF009: DLF009_1.append('1') ## else:DLF009_1.append('0') if i in DLF010: DLF010_1.append('1') else:DLF010_1.append('0') if i in DLF012: DLF012_1.append('1') else:DLF012_1.append('0') if i in DLF013: DLF013_1.append('1') else:DLF013_1.append('0') if i in DLF014: DLF014_1.append('1') else:DLF014_1.append('0') if i in DLM001: DLM001_1.append('1') else:DLM001_1.append('0') if i in DLM002: DLM002_1.append('1') else:DLM002_1.append('0') if i in DLM003: DLM003_1.append('1') else:DLM003_1.append('0') if i in DLM004: DLM004_1.append('1') else:DLM004_1.append('0') if i in DLM005: DLM005_1.append('1') else:DLM005_1.append('0') if i in DLM006: DLM006_1.append('1') else:DLM006_1.append('0') if i in DLM009: DLM009_1.append('1') else:DLM009_1.append('0') if i in DLM011: DLM011_1.append('1') else:DLM011_1.append('0') if i in DLM012: DLM012_1.append('1') else:DLM012_1.append('0') if i in DLM018: DLM018_1.append('1') else:DLM018_1.append('0') if i in DOF002: DOF002_1.append('1') else:DOF002_1.append('0') if i in DOF003: DOF003_1.append('1') else:DOF003_1.append('0') if i in DOF004: DOF004_1.append('1') else:DOF004_1.append('0') if i in DOF006: DOF006_1.append('1') else:DOF006_1.append('0') if i in DOF007: DOF007_1.append('1') else:DOF007_1.append('0') if i in DOF008: DOF008_1.append('1') else:DOF008_1.append('0') if i in DOF009: DOF009_1.append('1') else:DOF009_1.append('0') if i in DOF010: DOF010_1.append('1') else:DOF010_1.append('0') if i in DOF011: DOF011_1.append('1') else:DOF011_1.append('0') if i in DOF012: DOF012_1.append('1') else:DOF012_1.append('0') if i in DOF013: DOF013_1.append('1') else:DOF013_1.append('0') if i in DOF014: DOF014_1.append('1') else:DOF014_1.append('0') if i in DOM001: DOM001_1.append('1') else:DOM001_1.append('0') if i in DOM003: DOM003_1.append('1') else:DOM003_1.append('0') if i in DOM005: DOM005_1.append('1') else:DOM005_1.append('0') if i in DOM008: DOM008_1.append('1') else:DOM008_1.append('0') if i in DOM010: DOM010_1.append('1') else:DOM010_1.append('0') if i in DOM012: DOM012_1.append('1') else:DOM012_1.append('0') if i in DOM013: DOM013_1.append('1') else:DOM013_1.append('0') if i in DOM014: DOM014_1.append('1') else:DOM014_1.append('0') if i in DOM015: DOM015_1.append('1') else:DOM015_1.append('0') if i in DOM016: DOM016_1.append('1') else:DOM016_1.append('0') if i in DOM017: DOM017_1.append('1') else:DOM017_1.append('0') if i in DOM018: DOM018_1.append('1') else:DOM018_1.append('0') if i in DOM019: DOM019_1.append('1') else:DOM019_1.append('0') if i in DOM020: DOM020_1.append('1') else:DOM020_1.append('0') if i in DOM021: DOM021_1.append('1') else:DOM021_1.append('0') if i in DOM022: DOM022_1.append('1') else:DOM022_1.append('0') if i in DOM023: DOM023_1.append('1') else:DOM023_1.append('0') if i in DOM024: DOM024_1.append('1') else:DOM024_1.append('0') if i in DOM025: DOM025_1.append('1') else:DOM025_1.append('0') if i in DOM026: DOM026_1.append('1') else:DOM026_1.append('0') if i in NLF001: NLF001_1.append(' | 1') else:NLF001_1.append(' | 0') if i in NLF002: NLF002_1.append('1') else:NLF002_1.append('0') if i in NLF005: NLF005_1.append('1') else:NLF005_1.append('0') if i in NLF006: NLF006_1.append('1') else:NLF006_1.append('0') if i in NLF007: NLF007_1.append('1') else:NLF007_1.append('0') if i in NLF008: NLF008_1.append('1') else:NLF008_1.append('0') if i in NLF009: NLF009_1.append('1') else:NLF009_1.append('0') if i in NLF010: NLF010_1.append('1') else:NLF010_1.append('0') if i in NLF011: NLF011_1.append('1') else:NLF011_1.append('0') if i in NLF012: NLF012_1.append('1') else:NLF012_1.append('0') if i in NLF013: NLF013_1.append('1') else:NLF013_1.append('0') if i in NLF014: NLF014_1.append('1') else:NLF014_1.append('0') if i in NLF015: NLF015_1.append('1') else:NLF015_1.append('0') if i in NLM001: NLM001_1.append('1') else:NLM001_1.append('0') if i in NLM002: NLM002_1.append('1') else:NLM002_1.append('0') if i in NLM003: NLM003_1.append('1') else:NLM003_1.append('0') if i in NLM004: NLM004_1.append('1') else:NLM004_1.append('0') if i in NLM005: NLM005_1.append('1') else:NLM005_1.append('0') if i in NLM006: NLM006_1.append('1') else:NLM006_1.append('0') if i in NLM007: NLM007_1.append('1') else:NLM007_1.append('0') if i in NLM008: NLM008_1.append('1') else:NLM008_1.append('0') if i in NLM009: NLM009_1.append('1') else:NLM009_1.append('0') if i in NLM010: NLM010_1.append('1') else:NLM010_1.append('0') if i in NLM015: NLM015_1.append('1') else:NLM015_1.append('0') if i in NLM016: NLM016_1.append('1') else:NLM016_1.append('0') if i in NLM017: NLM017_1.append('1') else:NLM017_1.append('0') if i in NLM021: NLM021_1.append('1') else:NLM021_1.append('0') if i in NLM022: NLM022_1.append('1') else:NLM022_1.append('0') if i in NLM023: NLM023_1.append('1') else:NLM023_1.append('0') if i in NLM024: NLM024_1.append('1') else:NLM024_1.append('0') if i in NLM025: NLM025_1.append('1') else:NLM025_1.append('0') if i in NLM026: NLM026_1.append('1') else:NLM026_1.append('0') if i in NLM027: NLM027_1.append('1') else:NLM027_1.append('0') if i in NLM028: NLM028_1.append('1') else:NLM028_1.append('0') if i in NLM029: NLM029_1.append('1') else:NLM029_1.append('0') if i in NLM031: NLM031_1.append('1') else:NLM031_1.append('0') if i in NLM032: NLM032_1.append('1') else:NLM032_1.append('0') if i in NOF001: NOF001_1.append('1') else:NOF001_1.append('0') if i in NOF002: NOF002_1.append('1') else:NOF002_1.append('0') if i in NOF004: NOF004_1.append('1') else:NOF004_1.append('0') if i in NOF005: NOF005_1.append('1') else:NOF005_1.append('0') if i in NOF006: NOF006_1.append('1') else:NOF006_1.append('0') if i in NOF007: NOF007_1.append('1') else:NOF007_1.append('0') if i in NOF008: NOF008_1.append('1') else:NOF008_1.append('0') if i in NOF009: NOF009_1.append('1') else:NOF009_1.append('0') if i in NOF010: NOF010_1.append('1') else:NOF010_1.append('0') if i in NOF011: NOF011_1.append('1') else:NOF011_1.append('0') if i in NOF012: NOF012_1.append('1') else:NOF012_1.append('0') if i in NOF013: NOF013_1.append('1') else:NOF013_1.append('0') if i in NOF014: NOF014_1.append('1') else:NOF014_1.append('0') if i in NOM001: NOM001_1.append('1') else:NOM001_1.append('0') if i in NOM002: NOM002_1.append('1') else:NOM002_1.append('0') if i in NOM004: NOM004_1.append('1') else:NOM004_1.append('0') if i in NOM005: NOM005_1.append('1') else:NOM005_1.append('0') if i in NOM007: NOM007_1.append('1') else:NOM007_1.append('0') if i in NOM008: NOM008_1.append('1') else:NOM008_1.append('0') if i in NOM009: NOM009_1.append('1') else:NOM009_1.append('0') if i in NOM010: NOM010_1.append('1') else:NOM010_1.append('0') if i in NOM012: NOM012_1.append('1') else:NOM012_1.append('0') if i in NOM013: NOM013_1.append('1') else:NOM013_1.append('0') if i in NOM015: NOM015_1.append('1') else:NOM015_1.append('0') if i in NOM016: NOM016_1.append('1') else:NOM016_1.append('0') if i in NOM017: NOM017_1.append('1') else:NOM017_1.append('0') if i in NOM018: NOM018_1.append('1') else:NOM018_1.append('0') if i in NOM019: NOM019_1.append('1') else:NOM019_1.append('0') if i in NOM020: NOM020_1.append('1') else:NOM020_1.append('0') if i in NOM022: NOM022_1.append('1') else:NOM022_1.append('0') if i in NOM023: NOM023_1.append('1') else:NOM023_1.append('0') if i in NOM025: NOM025_1.append('1') else:NOM025_1.append('0') if i in NOM026: NOM026_1.append('1') else:NOM026_1.append('0') if i in NOM027: NOM027_1.append('1') else:NOM027_1.append('0') if i in NOM028: NOM028_1.append('1') else:NOM028_1.append('0') if i in NOM029: NOM029_1.append('1') else:NOM029_1.append('0') ## print 'saving' zoo = zip(main_op_list, DLF002_1,DLF004_1,DLF005_1, DLF006_1,DLF007_1,DLF008_1,DLF009_1,DLF010_1,DLF012_1, DLF013_1,DLF014_1,DLM001_1,DLM002_1,DLM003_1,DLM004_1, DLM005_1,DLM006_1,DLM009_1,DLM011_1,DLM012_1,DLM018_1, DOF002_1,DOF003_1,DOF004_1,DOF006_1,DOF007_1,DOF008_1, DOF009_1,DOF010_1,DOF011_1,DOF012_1,DOF013_1,DOF014_1, DOM001_1,DOM003_1,DOM005_1,DOM008_1,DOM010_1,DOM012_1, DOM013_1,DOM014_1,DOM015_1,DOM016_1,DOM017_1,DOM018_1, DOM019_1,DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1, DOM025_1,DOM026_1,NLF001_1,NLF002_1,NLF005_1,NLF006_1, NLF007_1,NLF008_1,NLF009_1,NLF010_1,NLF011_1,NLF012_1, NLF013_1,NLF014_1,NLF015_1,NLM001_1,NLM002_1,NLM003_1, NLM004_1,NLM005_1,NLM006_1,NLM007_1,NLM008_1,NLM009_1, NLM010_1,NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1, NLM023_1,NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1, NLM029_1,NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1, NOF005_1,NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1, NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1, NOM004_1,NOM005_1,NOM007_1,NO M008_1,NOM009_1,NOM010_1,NOM012_1,NOM013_1,NOM015_1, NOM016_1,NOM017_1,NOM018_1,NOM019_1,NOM020_1,NOM022_1, NOM023_1,NOM025_1,NOM026_1,NOM027_1,NOM028_1,NOM029_1) with open("test.tab", 'w+') as outfile: writer =csv.writer(outfile, delimiter = '\t', lineterminator = '\n') writer.writerow([' ','DLF2','DLF4','DLF5','DLF6', 'DLF7','DLF8','DLF9','DLF10','DLF12','DLF13','DLF14','DLM1', 'DLM2','DLM3','DLM4','DLM5','DLM6','DLM9','DLM11','DLM12',' DLM18','DOF2','DOF3','DOF4','DOF6','DOF7','DOF8','DOF9',' DOF10','DOF11','DOF12','DOF13','DOF04','DOM1','DOM3','DOM5', 'DOM8','DOM10','DOM12','DOM13','DOM14','DOM15','DOM16',' DOM17','DOM18','DOM19','DOM20','DOM21','DOM22','DOM23',' DOM24','DOM25','DOM26','NLF1','NLF2','NLF5','NLF6','NLF7',' NLF8','NLF9','NLF10','NLF11','NLF12','NLF13','NLF14','NLF15' ,'NLM1','NLM2','NLM3','NLM4','NLM5','NLM6','NLM7','NLM8',' NLM9','NLM10','NLM15','NLM16','NLM17','NLM21','NLM22',' NLM23','NLM24','NLM25','NLM26','NLM27','NLM28','NLM29',' NLM31','NLM32','NOF1','NOF2','NOF4','NOF5','NOF6','NOF7',' NOF8','NOF9','NOF10','NOF11','NOF12','NOF13','NOF14','NOM1', 'NOM2','NOM4','NOM5','NOM7','NOM8','NOM9','NOM10','NOM12',' NOM13','NOM15','NOM16','NOM17','NOM18','NOM19','NOM20',' NOM22','NOM23','NOM25','NOM26','NOM27','NOM28','NOM29']) writer.writerows(zoo) outfile.close() print 'done' end_time = time.time() elapsed = end_time-start_time print "Time elapsed.", elapsed Thanks Best Regards Syed Shujaat Ali Zaidi PhD Scholar (Bioinformatics) MOE Key Laboratory of Bioinformatics Bioinformatics Division, TNLIST & Department of Automation FIT 1-107, Tsinghua University, Beijing 100084, China Lecturer (Bioinformatics) Department of Bio Sciences COMSATS Institute of Information Technology Islamabad, Pakistan _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Sat Jun 10 04:19:12 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 10 Jun 2017 09:19:12 +0100 Subject: [Tutor] Huge list comprehension In-Reply-To: References: Message-ID: On 10/06/17 08:35, Abdur-Rahmaan Janhangeer wrote: > take a look at numpy It seems he already has, np.array is in his code. It's just the imports that are missing I suspect. > and don't necessarily give us the whole code. it becomes too long without > purpose Yes although in this case it does serve to highlight the problems with his approach - as highlighted by Peter. -- 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 From vikasy at gmail.com Sat Jun 10 12:39:57 2017 From: vikasy at gmail.com (Vikas YADAV) Date: Sat, 10 Jun 2017 09:39:57 -0700 Subject: [Tutor] string reversal using [::-1] Message-ID: <00e001d2e208$32cb3680$9861a380$@gmail.com> Question: Why does "123"[::-1] result in "321"? MY thinking is [::-1] is same as [0:3:-1], that the empty places defaults to start and end index of the string object. So, if we start from 0 index and decrement index by 1 till we reach 3, how many index we should get? I think we should get infinite infinite number of indices (0,-1,-2,-3.). This is my confusion. I hope my question is clear. Thanks, Vikas From japhy at pearachute.com Sat Jun 10 18:08:44 2017 From: japhy at pearachute.com (Japhy Bartlett) Date: Sat, 10 Jun 2017 18:08:44 -0400 Subject: [Tutor] Python - help with something most essential In-Reply-To: References: Message-ID: It's really awkward the way you're using Counter here... you're making new instances in every lambda (which is not great for memory usage), and then not actually using the Counter functionality: return sum(1 for _ in filter(lambda x: Counter(word) == Counter(x.strip()), fileContent)) (the whole point of the Counter() is to get sums, you don't need to do any of this !!) I'm not sure that they cared about how you used file.readlines(), I think the memory comment was a hint about instantiating Counter()s anyhow, all of this can be much much simpler: """ sortedword = sorted(inputWord) # an array of letters in the word, in alphabetical order count = 0 with open(filename) as f: for word in f.read().split(' '): # iterate over every word in the file if sorted(word) == sortedword: count +=1 print count """ which could in turn probably be written as a one liner. So even though you cleaned the code up a bit, it's still quite a bit more complicated then it needs to be, which makes it seem like your fundamentals are not great either! On Tue, Jun 6, 2017 at 2:31 AM, Peter Otten <__peter__ at web.de> wrote: > Schtvveer Schvrveve wrote: > > > I need someone's help. I am not proficient in Python and I wish to > > understand something. I was in a job pre-screening process where I was > > asked to solve a simple problem. > > > > The problem was supposed to be solved in Python and it was supposed to > > take two arguments: filename and word. The program reads the file which > is > > a .txt file containing a bunch of words and counts how many of those > words > > in the file are anagrams of the argument. > > > > First I concocted this solution: > > > > import sys > > from collections import Counter > > > > def main(args): > > filename = args[1] > > word = args[2] > > print countAnagrams(word, filename) > > > > def countAnagrams(word, filename): > > > > fileContent = readFile(filename) > > > > counter = Counter(word) > > num_of_anagrams = 0 > > > > for i in range(0, len(fileContent)): > > if counter == Counter(fileContent[i]): > > num_of_anagrams += 1 > > > > return num_of_anagrams > > > > def readFile(filename): > > > > with open(filename) as f: > > content = f.readlines() > > > > content = [x.strip() for x in content] > > > > return content > > > > if __name__ == '__main__': > > main(sys.argv) > > > > Very quickly I received this comment: > > > > "Can you adjust your solution a bit so you less loops (as little as > > possible) and also reduce the memory usage footprint of you program?" > > > > I tried to rework the methods into this: > > > > def countAnagrams(word, filename): > > > > fileContent = readFile(filename) > > > > return sum(1 for _ in filter(lambda x: Counter(word) == > > Counter(x.strip()), fileContent)) > > > > def readFile(filename): > > > > with open(filename) as f: > > content = f.readlines() > > > > return content > > > > And I was rejected. I just wish to understand what I could have done for > > this to be better? > > > > I am a Python beginner, so I'm sure there are things I don't know, but I > > was a bit surprised at the abruptness of the rejection and I'm worried > I'm > > doing something profoundly wrong. > > for i in range(0, len(stuff)): > ... > > instead of > > for item in stuff: > ... > > and > > content = file.readlines() # read the whole file into memory > process(content) > > are pretty much the most obvious indicators that you are a total newbie in > Python. Looks like they weren't willing to give you the time to iron that > out on the job even though you knew about lambda, Counter, list > comprehensions and generator expressions which are not newbie stuff. > > When upon their hint you did not address the root cause of the unbounded > memory consumption they might have come to the conclusion that you were > reproducing snippets you picked up somewhere and thus were cheating. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Sat Jun 10 19:37:53 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 11 Jun 2017 00:37:53 +0100 Subject: [Tutor] string reversal using [::-1] In-Reply-To: <00e001d2e208$32cb3680$9861a380$@gmail.com> References: <00e001d2e208$32cb3680$9861a380$@gmail.com> Message-ID: On 10/06/17 17:39, Vikas YADAV wrote: > Question: Why does "123"[::-1] result in "321"? > > MY thinking is [::-1] is same as [0:3:-1], that the empty places defaults to > start and end index of the string object. Did you try that? You may be surprised. The wonderful thing about the >>> prompt is that it's as quick to try it out as to guess. Look in the archives for a recent post by Steven(29/5, "Counting a string backwards") that explains slicing, it may help. -- 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 From __peter__ at web.de Sun Jun 11 04:33:06 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 11 Jun 2017 10:33:06 +0200 Subject: [Tutor] Python - help with something most essential References: Message-ID: Japhy Bartlett wrote: > I'm not sure that they cared about how you used file.readlines(), I think > the memory comment was a hint about instantiating Counter()s Then they would have been clueless ;) Both Schtvveer's original script and his subsequent "Verschlimmbesserung" -- beautiful german word for making things worse when trying to improve them -- use only two Counters at any given time. The second version is very inefficient because it builds the same Counter over and over again -- but this does not affect peak memory usage much. Here's the original version that triggered the comment: [Schtvveer Schvrveve] > import sys > from collections import Counter > > def main(args): > filename = args[1] > word = args[2] > print countAnagrams(word, filename) > > def countAnagrams(word, filename): > > fileContent = readFile(filename) > > counter = Counter(word) > num_of_anagrams = 0 > > for i in range(0, len(fileContent)): > if counter == Counter(fileContent[i]): > num_of_anagrams += 1 > > return num_of_anagrams > > def readFile(filename): > > with open(filename) as f: > content = f.readlines() > > content = [x.strip() for x in content] > > return content > > if __name__ == '__main__': > main(sys.argv) > referenced as before.py below, and here's a variant that removes readlines(), range(), and the [x.strip() for x in content] list comprehension, the goal being minimal changes, not code as I would write it from scratch. # after.py import sys from collections import Counter def main(args): filename = args[1] word = args[2] print countAnagrams(word, filename) def countAnagrams(word, filename): fileContent = readFile(filename) counter = Counter(word) num_of_anagrams = 0 for line in fileContent: if counter == Counter(line): num_of_anagrams += 1 return num_of_anagrams def readFile(filename): # this relies on garbage collection to close the file # which should normally be avoided for line in open(filename): yield line.strip() if __name__ == '__main__': main(sys.argv) How to measure memoryview? I found and as test data I use files containing 10**5 and 10**6 integers. With that setup (snipping everything but memory usage from the time -v output): $ /usr/bin/time -v python before.py anagrams5.txt 123 6 Maximum resident set size (kbytes): 17340 $ /usr/bin/time -v python before.py anagrams6.txt 123 6 Maximum resident set size (kbytes): 117328 $ /usr/bin/time -v python after.py anagrams5.txt 123 6 Maximum resident set size (kbytes): 6432 $ /usr/bin/time -v python after.py anagrams6.txt 123 6 Maximum resident set size (kbytes): 6432 See the pattern? before.py uses O(N) memory, after.py O(1). Run your own tests if you need more datapoints or prefer a different method to measure memory consumption. From __peter__ at web.de Sun Jun 11 05:01:33 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 11 Jun 2017 11:01:33 +0200 Subject: [Tutor] string reversal using [::-1] References: <00e001d2e208$32cb3680$9861a380$@gmail.com> Message-ID: Vikas YADAV wrote: > Question: Why does "123"[::-1] result in "321"? > > > > MY thinking is [::-1] is same as [0:3:-1], that the empty places defaults > to start and end index of the string object. > > So, if we start from 0 index and decrement index by 1 till we reach 3, how > many index we should get? I think we should get infinite infinite number > of indices (0,-1,-2,-3.). > > > > This is my confusion. > > I hope my question is clear. It takes a slice object and a length to replace the missing aka None values with actual integers. You can experiment with this a bit in the interpreter: >>> class A: ... def __getitem__(self, index): ... return index ... >>> a = A() >>> a[::-1] slice(None, None, -1) >>> a[::-1].indices(10) (9, -1, -1) >>> a[::-1].indices(5) (4, -1, -1) >>> a[::].indices(5) (0, 5, 1) So what a missing value actually means depends on the context. Note that while I'm a long-time Python user I still smell danger when a negative step is involved. For everything but items[::-1] I prefer reversed(items[start:stop]) # an iterator, not an object of type(items) when possible. From mysecretrobotfactory at gmail.com Sun Jun 11 22:54:56 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sun, 11 Jun 2017 19:54:56 -0700 Subject: [Tutor] tkinter actively maintained? Message-ID: Hi all: is tkinter still being actively maintained? I only had to ask about this because I have never looked stuff like this up before. Is it ok to develop using it, is it going to be dropped in the near future? thanks! From alan.gauld at yahoo.co.uk Mon Jun 12 04:26:54 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 12 Jun 2017 09:26:54 +0100 Subject: [Tutor] tkinter actively maintained? In-Reply-To: References: Message-ID: On 12/06/17 03:54, Michael C wrote: > Hi all: > > is tkinter still being actively maintained? I only had to ask about this > because I have never looked stuff like this up before. Yes, Tkinter tracks the Tk releases so that most of the activity at present is in the ttk sub package. There is a moderately active active mailing list and you can take a look at the list archive to see the kind of issues they cover. https://mail.python.org/pipermail/tkinter-discuss/ That having been said, and despite the fact I use tkinter a lot, it is not the best UI toolkit if you want to build a comprehensive desktop app. GTk, Qt and Wxwidgets all offer much richer widget support and better integration with native look 'n feel. But Tkinter is great for quick and dirty UIs on top of CLI code and is easier to learn. -- 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 From arj.python at gmail.com Mon Jun 12 04:19:12 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Mon, 12 Jun 2017 12:19:12 +0400 Subject: [Tutor] Huge list comprehension In-Reply-To: References: Message-ID: i think that you created 1000 vars as you needed them with different names replacing these with dictionary keys might be the answer if you hava a specific pattern for the variable, it'll be fine as it'll just be string manipulation e.g. variables ={ } then for i in range(..): variables[*keyname here*] = [ ] Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 12 Jun 2017 08:54, "syed zaidi" wrote: > Thanks > One reason fornsharing the code was that I have to manually create over > 100 variables > Is there a way i can automate thst process? > > Get Outlook for Android > > From: Abdur-Rahmaan Janhangeer > Sent: Saturday, June 10, 3:35 PM > Subject: Re: [Tutor] Huge list comprehension > To: syed zaidi, tutor > > take a look at numpy > > and don't necessarily give us the whole code. it becomes too long without > purpose > > Abdur-Rahmaan Janhangeer, > Mauritius > abdurrahmaanjanhangeer.wordpress.com > > On 6 Jun 2017 03:26, "syed zaidi" wrote: > > > hi, > > I would appreciate if you can help me suggesting a quick and efficient > strategy for comparing multiple lists with one principal list > > I have about 125 lists containing about 100,000 numerical entries in each > > my principal list contains about 6 million entries. > > I want to compare each small list with main list and append yes/no or 0/1 > in each new list corresponding to each of 125 lists > > The program is working but it takes ages to process huge files, > Can someone pleases tell me how can I make this process fast. Right now it > takes arounf 2 weeks to complete this task > > the code I have written and is working is as under: > > sample_name = [] > > main_op_list,principal_list = [],[] > dictionary = {} > > with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r') > as f: > reader = csv.reader(f, dialect = 'excel', delimiter='\t') > list2 = filter(None, reader) > for i in range(len(list2)): > col1 = list2[i][0] > operon = list2[i][1] > main_op_list.append(operon) > col1 = col1.strip().split("_") > sample_name = col1[0] > if dictionary.get(sample_name): > dictionary[sample_name].append(operon) > else: > dictionary[sample_name] = [] > dictionary[sample_name].append(operon) > locals().update(dictionary) ## converts dictionary keys to variables > ##print DLF004 > dict_values = dictionary.values() > dict_keys = dictionary.keys() > print dict_keys > print len(dict_keys) > main_op_list_np = np.array(main_op_list) > > DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1, > DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1, > DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1, > DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1 > =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] > DOF004_1,DOF006_1,DOF007_1,DOF008_1,DOF009_1,DOF010_1, > DOF011_1,DOF012_1,DOF013_1,DOF014_1,DOM001_1,DOM003_1, > DOM005_1,DOM008_1,DOM010_1,DOM012_1,DOM013_1,DOM014_1, > DOM015_1,DOM016_1,DOM017_1,DOM018_1,DOM019_1 > =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] > DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1,DOM025_1,DOM026_1 = > [],[],[],[],[],[],[] > NLF001_1,NLF002_1,NLF005_1,NLF006_1,NLF007_1,NLF008_1, > NLF009_1,NLF010_1,NLF011_1,NLF012_1,NLF013_1,NLF014_1, > NLF015_1,NLM001_1,NLM002_1,NLM003_1,NLM004_1,NLM005_1, > NLM006_1,NLM007_1,NLM008_1,NLM009_1,NLM010_1 > =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] > NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1,NLM023_1, > NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1,NLM029_1, > NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1,NOF005_1, > NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1 > =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] > NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1, > NOM004_1,NOM005_1,NOM007_1,NOM008_1,NOM009_1,NOM010_1, > NOM012_1,NOM013_1,NOM015_1,NOM016_1,NOM017_1,NOM018_1, > NOM019_1,NOM020_1,NOM022_1,NOM023_1,NOM025_1 > =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] > NOM026_1,NOM027_1,NOM028_1,NOM029_1 = [],[],[],[] > > for i in main_op_list_np: > if i in DLF002: DLF002_1.append('1') > else:DLF002_1.append('0') > if i in DLF004: DLF004_1.append('1') > else:DLF004_1.append('0') > if i in DLF005: DLF005_1.append('1') > else:DLF005_1.append('0') > if i in DLF006: DLF006_1.append('1') > else:DLF006_1.append('0') > if i in DLF007: DLF007_1.append('1') > else:DLF007_1.append('0') > if i in DLF008: DLF008_1.append('1') > else:DLF008_1.append('0') > ## if main_op_list[i] in DLF009: DLF009_1.append('1') > ## else:DLF009_1.append('0') > if i in DLF010: DLF010_1.append('1') > else:DLF010_1.append('0') > if i in DLF012: DLF012_1.append('1') > else:DLF012_1.append('0') > if i in DLF013: DLF013_1.append('1') > else:DLF013_1.append('0') > if i in DLF014: DLF014_1.append('1') > else:DLF014_1.append('0') > if i in DLM001: DLM001_1.append('1') > else:DLM001_1.append('0') > if i in DLM002: DLM002_1.append('1') > else:DLM002_1.append('0') > if i in DLM003: DLM003_1.append('1') > else:DLM003_1.append('0') > if i in DLM004: DLM004_1.append('1') > else:DLM004_1.append('0') > if i in DLM005: DLM005_1.append('1') > else:DLM005_1.append('0') > if i in DLM006: DLM006_1.append('1') > else:DLM006_1.append('0') > if i in DLM009: DLM009_1.append('1') > else:DLM009_1.append('0') > if i in DLM011: DLM011_1.append('1') > else:DLM011_1.append('0') > if i in DLM012: DLM012_1.append('1') > else:DLM012_1.append('0') > if i in DLM018: DLM018_1.append('1') > else:DLM018_1.append('0') > if i in DOF002: DOF002_1.append('1') > else:DOF002_1.append('0') > if i in DOF003: DOF003_1.append('1') > else:DOF003_1.append('0') > if i in DOF004: DOF004_1.append('1') > else:DOF004_1.append('0') > if i in DOF006: DOF006_1.append('1') > else:DOF006_1.append('0') > if i in DOF007: DOF007_1.append('1') > else:DOF007_1.append('0') > if i in DOF008: DOF008_1.append('1') > else:DOF008_1.append('0') > if i in DOF009: DOF009_1.append('1') > else:DOF009_1.append('0') > if i in DOF010: DOF010_1.append('1') > else:DOF010_1.append('0') > if i in DOF011: DOF011_1.append('1') > else:DOF011_1.append('0') > if i in DOF012: DOF012_1.append('1') > else:DOF012_1.append('0') > if i in DOF013: DOF013_1.append('1') > else:DOF013_1.append('0') > if i in DOF014: DOF014_1.append('1') > else:DOF014_1.append('0') > if i in DOM001: DOM001_1.append('1') > else:DOM001_1.append('0') > if i in DOM003: DOM003_1.append('1') > else:DOM003_1.append('0') > if i in DOM005: DOM005_1.append('1') > else:DOM005_1.append('0') > if i in DOM008: DOM008_1.append('1') > else:DOM008_1.append('0') > if i in DOM010: DOM010_1.append('1') > else:DOM010_1.append('0') > if i in DOM012: DOM012_1.append('1') > else:DOM012_1.append('0') > if i in DOM013: DOM013_1.append('1') > else:DOM013_1.append('0') > if i in DOM014: DOM014_1.append('1') > else:DOM014_1.append('0') > if i in DOM015: DOM015_1.append('1') > else:DOM015_1.append('0') > if i in DOM016: DOM016_1.append('1') > else:DOM016_1.append('0') > if i in DOM017: DOM017_1.append('1') > else:DOM017_1.append('0') > if i in DOM018: DOM018_1.append('1') > else:DOM018_1.append('0') > if i in DOM019: DOM019_1.append('1') > else:DOM019_1.append('0') > if i in DOM020: DOM020_1.append('1') > else:DOM020_1.append('0') > if i in DOM021: DOM021_1.append('1') > else:DOM021_1.append('0') > if i in DOM022: DOM022_1.append('1') > else:DOM022_1.append('0') > if i in DOM023: DOM023_1.append('1') > else:DOM023_1.append('0') > if i in DOM024: DOM024_1.append('1') > else:DOM024_1.append('0') > if i in DOM025: DOM025_1.append('1') > else:DOM025_1.append('0') > if i in DOM026: DOM026_1.append('1') > else:DOM026_1.append('0') > if i in NLF001: NLF001_1.append(' | 1') > else:NLF001_1.append(' | 0') > if i in NLF002: NLF002_1.append('1') > else:NLF002_1.append('0') > if i in NLF005: NLF005_1.append('1') > else:NLF005_1.append('0') > if i in NLF006: NLF006_1.append('1') > else:NLF006_1.append('0') > if i in NLF007: NLF007_1.append('1') > else:NLF007_1.append('0') > if i in NLF008: NLF008_1.append('1') > else:NLF008_1.append('0') > if i in NLF009: NLF009_1.append('1') > else:NLF009_1.append('0') > if i in NLF010: NLF010_1.append('1') > else:NLF010_1.append('0') > if i in NLF011: NLF011_1.append('1') > else:NLF011_1.append('0') > if i in NLF012: NLF012_1.append('1') > else:NLF012_1.append('0') > if i in NLF013: NLF013_1.append('1') > else:NLF013_1.append('0') > if i in NLF014: NLF014_1.append('1') > else:NLF014_1.append('0') > if i in NLF015: NLF015_1.append('1') > else:NLF015_1.append('0') > if i in NLM001: NLM001_1.append('1') > else:NLM001_1.append('0') > if i in NLM002: NLM002_1.append('1') > else:NLM002_1.append('0') > if i in NLM003: NLM003_1.append('1') > else:NLM003_1.append('0') > if i in NLM004: NLM004_1.append('1') > else:NLM004_1.append('0') > if i in NLM005: NLM005_1.append('1') > else:NLM005_1.append('0') > if i in NLM006: NLM006_1.append('1') > else:NLM006_1.append('0') > if i in NLM007: NLM007_1.append('1') > else:NLM007_1.append('0') > if i in NLM008: NLM008_1.append('1') > else:NLM008_1.append('0') > if i in NLM009: NLM009_1.append('1') > else:NLM009_1.append('0') > if i in NLM010: NLM010_1.append('1') > else:NLM010_1.append('0') > if i in NLM015: NLM015_1.append('1') > else:NLM015_1.append('0') > if i in NLM016: NLM016_1.append('1') > else:NLM016_1.append('0') > if i in NLM017: NLM017_1.append('1') > else:NLM017_1.append('0') > if i in NLM021: NLM021_1.append('1') > else:NLM021_1.append('0') > if i in NLM022: NLM022_1.append('1') > else:NLM022_1.append('0') > if i in NLM023: NLM023_1.append('1') > else:NLM023_1.append('0') > if i in NLM024: NLM024_1.append('1') > else:NLM024_1.append('0') > if i in NLM025: NLM025_1.append('1') > else:NLM025_1.append('0') > if i in NLM026: NLM026_1.append('1') > else:NLM026_1.append('0') > if i in NLM027: NLM027_1.append('1') > else:NLM027_1.append('0') > if i in NLM028: NLM028_1.append('1') > else:NLM028_1.append('0') > if i in NLM029: NLM029_1.append('1') > else:NLM029_1.append('0') > if i in NLM031: NLM031_1.append('1') > else:NLM031_1.append('0') > if i in NLM032: NLM032_1.append('1') > else:NLM032_1.append('0') > if i in NOF001: NOF001_1.append('1') > else:NOF001_1.append('0') > if i in NOF002: NOF002_1.append('1') > else:NOF002_1.append('0') > if i in NOF004: NOF004_1.append('1') > else:NOF004_1.append('0') > if i in NOF005: NOF005_1.append('1') > else:NOF005_1.append('0') > if i in NOF006: NOF006_1.append('1') > else:NOF006_1.append('0') > if i in NOF007: NOF007_1.append('1') > else:NOF007_1.append('0') > if i in NOF008: NOF008_1.append('1') > else:NOF008_1.append('0') > if i in NOF009: NOF009_1.append('1') > else:NOF009_1.append('0') > if i in NOF010: NOF010_1.append('1') > else:NOF010_1.append('0') > if i in NOF011: NOF011_1.append('1') > else:NOF011_1.append('0') > if i in NOF012: NOF012_1.append('1') > else:NOF012_1.append('0') > if i in NOF013: NOF013_1.append('1') > else:NOF013_1.append('0') > if i in NOF014: NOF014_1.append('1') > else:NOF014_1.append('0') > if i in NOM001: NOM001_1.append('1') > else:NOM001_1.append('0') > if i in NOM002: NOM002_1.append('1') > else:NOM002_1.append('0') > if i in NOM004: NOM004_1.append('1') > else:NOM004_1.append('0') > if i in NOM005: NOM005_1.append('1') > else:NOM005_1.append('0') > if i in NOM007: NOM007_1.append('1') > else:NOM007_1.append('0') > if i in NOM008: NOM008_1.append('1') > else:NOM008_1.append('0') > if i in NOM009: NOM009_1.append('1') > else:NOM009_1.append('0') > if i in NOM010: NOM010_1.append('1') > else:NOM010_1.append('0') > if i in NOM012: NOM012_1.append('1') > else:NOM012_1.append('0') > if i in NOM013: NOM013_1.append('1') > else:NOM013_1.append('0') > if i in NOM015: NOM015_1.append('1') > else:NOM015_1.append('0') > if i in NOM016: NOM016_1.append('1') > else:NOM016_1.append('0') > if i in NOM017: NOM017_1.append('1') > else:NOM017_1.append('0') > if i in NOM018: NOM018_1.append('1') > else:NOM018_1.append('0') > if i in NOM019: NOM019_1.append('1') > else:NOM019_1.append('0') > if i in NOM020: NOM020_1.append('1') > else:NOM020_1.append('0') > if i in NOM022: NOM022_1.append('1') > else:NOM022_1.append('0') > if i in NOM023: NOM023_1.append('1') > else:NOM023_1.append('0') > if i in NOM025: NOM025_1.append('1') > else:NOM025_1.append('0') > if i in NOM026: NOM026_1.append('1') > else:NOM026_1.append('0') > if i in NOM027: NOM027_1.append('1') > else:NOM027_1.append('0') > if i in NOM028: NOM028_1.append('1') > else:NOM028_1.append('0') > if i in NOM029: NOM029_1.append('1') > else:NOM029_1.append('0') > > ## > print 'saving' > zoo = zip(main_op_list, DLF002_1,DLF004_1,DLF005_1, > DLF006_1,DLF007_1,DLF008_1,DLF009_1,DLF010_1,DLF012_1, > DLF013_1,DLF014_1,DLM001_1,DLM002_1,DLM003_1,DLM004_1, > DLM005_1,DLM006_1,DLM009_1,DLM011_1,DLM012_1,DLM018_1, > DOF002_1,DOF003_1,DOF004_1,DOF006_1,DOF007_1,DOF008_1, > DOF009_1,DOF010_1,DOF011_1,DOF012_1,DOF013_1,DOF014_1, > DOM001_1,DOM003_1,DOM005_1,DOM008_1,DOM010_1,DOM012_1, > DOM013_1,DOM014_1,DOM015_1,DOM016_1,DOM017_1,DOM018_1, > DOM019_1,DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1, > DOM025_1,DOM026_1,NLF001_1,NLF002_1,NLF005_1,NLF006_1, > NLF007_1,NLF008_1,NLF009_1,NLF010_1,NLF011_1,NLF012_1, > NLF013_1,NLF014_1,NLF015_1,NLM001_1,NLM002_1,NLM003_1, > NLM004_1,NLM005_1,NLM006_1,NLM007_1,NLM008_1,NLM009_1, > NLM010_1,NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1, > NLM023_1,NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1, > NLM029_1,NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1, > NOF005_1,NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1, > NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1, > NOM004_1,NOM005_1,NOM007_1,NO > M008_1,NOM009_1,NOM010_1,NOM012_1,NOM013_1,NOM015_1, > NOM016_1,NOM017_1,NOM018_1,NOM019_1,NOM020_1,NOM022_1, > NOM023_1,NOM025_1,NOM026_1,NOM027_1,NOM028_1,NOM029_1) > with open("test.tab", 'w+') as outfile: > writer =csv.writer(outfile, delimiter = '\t', lineterminator = '\n') > writer.writerow([' ','DLF2','DLF4','DLF5','DLF6' > ,'DLF7','DLF8','DLF9','DLF10','DLF12','DLF13','DLF14','DLM1' > ,'DLM2','DLM3','DLM4','DLM5','DLM6','DLM9','DLM11','DLM12',' > DLM18','DOF2','DOF3','DOF4','DOF6','DOF7','DOF8','DOF9',' > DOF10','DOF11','DOF12','DOF13','DOF04','DOM1','DOM3','DOM5', > 'DOM8','DOM10','DOM12','DOM13','DOM14','DOM15','DOM16',' > DOM17','DOM18','DOM19','DOM20','DOM21','DOM22','DOM23',' > DOM24','DOM25','DOM26','NLF1','NLF2','NLF5','NLF6','NLF7',' > NLF8','NLF9','NLF10','NLF11','NLF12','NLF13','NLF14','NLF15' > ,'NLM1','NLM2','NLM3','NLM4','NLM5','NLM6','NLM7','NLM8',' > NLM9','NLM10','NLM15','NLM16','NLM17','NLM21','NLM22',' > NLM23','NLM24','NLM25','NLM26','NLM27','NLM28','NLM29',' > NLM31','NLM32','NOF1','NOF2','NOF4','NOF5','NOF6','NOF7',' > NOF8','NOF9','NOF10','NOF11','NOF12','NOF13','NOF14','NOM1', > 'NOM2','NOM4','NOM5','NOM7','NOM8','NOM9','NOM10','NOM12',' > NOM13','NOM15','NOM16','NOM17','NOM18','NOM19','NOM20',' > NOM22','NOM23','NOM25','NOM26','NOM27','NOM28','NOM29']) > writer.writerows(zoo) > outfile.close() > print 'done' > end_time = time.time() > elapsed = end_time-start_time > print "Time elapsed.", elapsed > > Thanks > > Best Regards > > Syed Shujaat Ali Zaidi > PhD Scholar (Bioinformatics) > MOE Key Laboratory of Bioinformatics > Bioinformatics Division, TNLIST & Department of Automation > FIT 1-107, Tsinghua University, Beijing 100084, China > > Lecturer (Bioinformatics) > Department of Bio Sciences > COMSATS Institute of Information Technology > Islamabad, Pakistan > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > > > From arj.python at gmail.com Mon Jun 12 04:21:24 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Mon, 12 Jun 2017 12:21:24 +0400 Subject: [Tutor] Python - help with something most essential In-Reply-To: References: Message-ID: i might add that with open( . . . instead of foo = open( . . . also shows some maturity in py Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 11 Jun 2017 12:33, "Peter Otten" <__peter__ at web.de> wrote: > Japhy Bartlett wrote: > > > I'm not sure that they cared about how you used file.readlines(), I think > > the memory comment was a hint about instantiating Counter()s > > Then they would have been clueless ;) > > Both Schtvveer's original script and his subsequent "Verschlimmbesserung" > -- > beautiful german word for making things worse when trying to improve them > -- > use only two Counters at any given time. The second version is very > inefficient because it builds the same Counter over and over again -- but > this does not affect peak memory usage much. > > Here's the original version that triggered the comment: > > [Schtvveer Schvrveve] > > > import sys > > from collections import Counter > > > > def main(args): > > filename = args[1] > > word = args[2] > > print countAnagrams(word, filename) > > > > def countAnagrams(word, filename): > > > > fileContent = readFile(filename) > > > > counter = Counter(word) > > num_of_anagrams = 0 > > > > for i in range(0, len(fileContent)): > > if counter == Counter(fileContent[i]): > > num_of_anagrams += 1 > > > > return num_of_anagrams > > > > def readFile(filename): > > > > with open(filename) as f: > > content = f.readlines() > > > > content = [x.strip() for x in content] > > > > return content > > > > if __name__ == '__main__': > > main(sys.argv) > > > > referenced as before.py below, and here's a variant that removes > readlines(), range(), and the [x.strip() for x in content] list > comprehension, the goal being minimal changes, not code as I would write it > from scratch. > > # after.py > import sys > from collections import Counter > > def main(args): > filename = args[1] > word = args[2] > print countAnagrams(word, filename) > > def countAnagrams(word, filename): > > fileContent = readFile(filename) > counter = Counter(word) > num_of_anagrams = 0 > > for line in fileContent: > if counter == Counter(line): > num_of_anagrams += 1 > > return num_of_anagrams > > def readFile(filename): > # this relies on garbage collection to close the file > # which should normally be avoided > for line in open(filename): > yield line.strip() > > if __name__ == '__main__': > main(sys.argv) > > How to measure memoryview? I found > usage-of-a-linux-unix-process> and as test data I use files containing > 10**5 and 10**6 > integers. With that setup (snipping everything but memory usage from the > time -v output): > > $ /usr/bin/time -v python before.py anagrams5.txt 123 > 6 > Maximum resident set size (kbytes): 17340 > $ /usr/bin/time -v python before.py anagrams6.txt 123 > 6 > Maximum resident set size (kbytes): 117328 > > > $ /usr/bin/time -v python after.py anagrams5.txt 123 > 6 > Maximum resident set size (kbytes): 6432 > $ /usr/bin/time -v python after.py anagrams6.txt 123 > 6 > Maximum resident set size (kbytes): 6432 > > See the pattern? before.py uses O(N) memory, after.py O(1). > > Run your own tests if you need more datapoints or prefer a different method > to measure memory consumption. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From arj.python at gmail.com Mon Jun 12 04:25:35 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Mon, 12 Jun 2017 12:25:35 +0400 Subject: [Tutor] string reversal using [::-1] In-Reply-To: <00e001d2e208$32cb3680$9861a380$@gmail.com> References: <00e001d2e208$32cb3680$9861a380$@gmail.com> Message-ID: [QUOTED ENTIRELY FROM STEVE {steve at pearwood.info} IN ANSWER TO A SLICING QUESTION] The way to think about string indexing and slicing is that the index positions mark *between* the characters. Take your string: Machine learning is awesome! For brevity, I'll just use the first word: Machine Imagine slicing it between the characters. I'll mark the cuts with a vertical bar: |M|a|c|h|i|n|e| and add indexes. The indexes will only line correctly if you use a monspaced or fixed width font like Courier, otherwise things may not line up correctly. |M|a|c|h|i|n|e| 0 1 2 3 4 5 6 7 Here they are again starting from the right, I've spread things out a bit to fit in the minus signs: |M |a |c |h |i |n |e | -7 -6 -5 -4 -3 -2 -1 0 Notice that 0 gets used twice. Of course, that's impossible, because it would be ambiguous. If you give 0 as an index, how does Python know whether you mean 0 at the start or 0 or the end? So the simple rule Python uses is that 0 *always* means the start. When you give a single index, Python always uses the character immediately to the right of the cut: s = "Machine" s[0] => returns "M" s[-1] => returns "e" s[7] => raises an exception, because there is no character to the right When you give two indexes, using slice notation, Python returns the characters BETWEEN those cuts: s[0:7] => returns "Machine" s[1:-1] => returns "achin" Because 0 always means the start of the string, how do you slice to the end? You can use the length of the string (in this case, 7) or you can leave the ending position blank, and it defaults to the length of the string: s[1:] # means the same as [1:len(s)] You can leave the starting position blank too, it defaults to 0: s[:] # means the same as [0:len(s)] So remember that slices always cut *between* the index positions. Things get complicated when you include a step (or stride), especially when the step is negative. For step sizes other than 1, it is probably best to think of looping over the string: py> s = "Nobody expects the Spanish Inquisition!" py> s[-1:1:-2] '!otsun snp h tex db' is somewhat like: for i in range(len(s)-1, 1, -2): print s[i] -- Steve [QUOTED ENTIRELY FROM STEVE {steve at pearwood.info} IN ANSWER TO A SLICING QUESTION] Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 10 Jun 2017 21:31, "Vikas YADAV" wrote: > Question: Why does "123"[::-1] result in "321"? > > > > MY thinking is [::-1] is same as [0:3:-1], that the empty places defaults > to > start and end index of the string object. > > So, if we start from 0 index and decrement index by 1 till we reach 3, how > many index we should get? I think we should get infinite infinite number of > indices (0,-1,-2,-3.). > > > > This is my confusion. > > I hope my question is clear. > > > > Thanks, > > Vikas > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From syedzaidi85 at hotmail.co.uk Mon Jun 12 00:54:32 2017 From: syedzaidi85 at hotmail.co.uk (syed zaidi) Date: Mon, 12 Jun 2017 04:54:32 +0000 Subject: [Tutor] Huge list comprehension In-Reply-To: References: , Message-ID: Thanks One reason fornsharing the code was that I have to manually create over 100 variables Is there a way i can automate thst process? Get Outlook for Android From: Abdur-Rahmaan Janhangeer Sent: Saturday, June 10, 3:35 PM Subject: Re: [Tutor] Huge list comprehension To: syed zaidi, tutor take a look at numpy and don't necessarily give us the whole code. it becomes too long without purpose Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 6 Jun 2017 03:26, "syed zaidi" > wrote: hi, I would appreciate if you can help me suggesting a quick and efficient strategy for comparing multiple lists with one principal list I have about 125 lists containing about 100,000 numerical entries in each my principal list contains about 6 million entries. I want to compare each small list with main list and append yes/no or 0/1 in each new list corresponding to each of 125 lists The program is working but it takes ages to process huge files, Can someone pleases tell me how can I make this process fast. Right now it takes arounf 2 weeks to complete this task the code I have written and is working is as under: sample_name = [] main_op_list,principal_list = [],[] dictionary = {} with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r') as f: reader = csv.reader(f, dialect = 'excel', delimiter='\t') list2 = filter(None, reader) for i in range(len(list2)): col1 = list2[i][0] operon = list2[i][1] main_op_list.append(operon) col1 = col1.strip().split("_") sample_name = col1[0] if dictionary.get(sample_name): dictionary[sample_name].append(operon) else: dictionary[sample_name] = [] dictionary[sample_name].append(operon) locals().update(dictionary) ## converts dictionary keys to variables ##print DLF004 dict_values = dictionary.values() dict_keys = dictionary.keys() print dict_keys print len(dict_keys) main_op_list_np = np.array(main_op_list) DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1,DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1,DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1,DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] DOF004_1,DOF006_1,DOF007_1,DOF008_1,DOF009_1,DOF010_1,DOF011_1,DOF012_1,DOF013_1,DOF014_1,DOM001_1,DOM003_1,DOM005_1,DOM008_1,DOM010_1,DOM012_1,DOM013_1,DOM014_1,DOM015_1,DOM016_1,DOM017_1,DOM018_1,DOM019_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1,DOM025_1,DOM026_1 = [],[],[],[],[],[],[] NLF001_1,NLF002_1,NLF005_1,NLF006_1,NLF007_1,NLF008_1,NLF009_1,NLF010_1,NLF011_1,NLF012_1,NLF013_1,NLF014_1,NLF015_1,NLM001_1,NLM002_1,NLM003_1,NLM004_1,NLM005_1,NLM006_1,NLM007_1,NLM008_1,NLM009_1,NLM010_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1,NLM023_1,NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1,NLM029_1,NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1,NOF005_1,NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1,NOM004_1,NOM005_1,NOM007_1,NOM008_1,NOM009_1,NOM010_1,NOM012_1,NOM013_1,NOM015_1,NOM016_1,NOM017_1,NOM018_1,NOM019_1,NOM020_1,NOM022_1,NOM023_1,NOM025_1 =[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[] NOM026_1,NOM027_1,NOM028_1,NOM029_1 = [],[],[],[] for i in main_op_list_np: if i in DLF002: DLF002_1.append('1') else:DLF002_1.append('0') if i in DLF004: DLF004_1.append('1') else:DLF004_1.append('0') if i in DLF005: DLF005_1.append('1') else:DLF005_1.append('0') if i in DLF006: DLF006_1.append('1') else:DLF006_1.append('0') if i in DLF007: DLF007_1.append('1') else:DLF007_1.append('0') if i in DLF008: DLF008_1.append('1') else:DLF008_1.append('0') ## if main_op_list[i] in DLF009: DLF009_1.append('1') ## else:DLF009_1.append('0') if i in DLF010: DLF010_1.append('1') else:DLF010_1.append('0') if i in DLF012: DLF012_1.append('1') else:DLF012_1.append('0') if i in DLF013: DLF013_1.append('1') else:DLF013_1.append('0') if i in DLF014: DLF014_1.append('1') else:DLF014_1.append('0') if i in DLM001: DLM001_1.append('1') else:DLM001_1.append('0') if i in DLM002: DLM002_1.append('1') else:DLM002_1.append('0') if i in DLM003: DLM003_1.append('1') else:DLM003_1.append('0') if i in DLM004: DLM004_1.append('1') else:DLM004_1.append('0') if i in DLM005: DLM005_1.append('1') else:DLM005_1.append('0') if i in DLM006: DLM006_1.append('1') else:DLM006_1.append('0') if i in DLM009: DLM009_1.append('1') else:DLM009_1.append('0') if i in DLM011: DLM011_1.append('1') else:DLM011_1.append('0') if i in DLM012: DLM012_1.append('1') else:DLM012_1.append('0') if i in DLM018: DLM018_1.append('1') else:DLM018_1.append('0') if i in DOF002: DOF002_1.append('1') else:DOF002_1.append('0') if i in DOF003: DOF003_1.append('1') else:DOF003_1.append('0') if i in DOF004: DOF004_1.append('1') else:DOF004_1.append('0') if i in DOF006: DOF006_1.append('1') else:DOF006_1.append('0') if i in DOF007: DOF007_1.append('1') else:DOF007_1.append('0') if i in DOF008: DOF008_1.append('1') else:DOF008_1.append('0') if i in DOF009: DOF009_1.append('1') else:DOF009_1.append('0') if i in DOF010: DOF010_1.append('1') else:DOF010_1.append('0') if i in DOF011: DOF011_1.append('1') else:DOF011_1.append('0') if i in DOF012: DOF012_1.append('1') else:DOF012_1.append('0') if i in DOF013: DOF013_1.append('1') else:DOF013_1.append('0') if i in DOF014: DOF014_1.append('1') else:DOF014_1.append('0') if i in DOM001: DOM001_1.append('1') else:DOM001_1.append('0') if i in DOM003: DOM003_1.append('1') else:DOM003_1.append('0') if i in DOM005: DOM005_1.append('1') else:DOM005_1.append('0') if i in DOM008: DOM008_1.append('1') else:DOM008_1.append('0') if i in DOM010: DOM010_1.append('1') else:DOM010_1.append('0') if i in DOM012: DOM012_1.append('1') else:DOM012_1.append('0') if i in DOM013: DOM013_1.append('1') else:DOM013_1.append('0') if i in DOM014: DOM014_1.append('1') else:DOM014_1.append('0') if i in DOM015: DOM015_1.append('1') else:DOM015_1.append('0') if i in DOM016: DOM016_1.append('1') else:DOM016_1.append('0') if i in DOM017: DOM017_1.append('1') else:DOM017_1.append('0') if i in DOM018: DOM018_1.append('1') else:DOM018_1.append('0') if i in DOM019: DOM019_1.append('1') else:DOM019_1.append('0') if i in DOM020: DOM020_1.append('1') else:DOM020_1.append('0') if i in DOM021: DOM021_1.append('1') else:DOM021_1.append('0') if i in DOM022: DOM022_1.append('1') else:DOM022_1.append('0') if i in DOM023: DOM023_1.append('1') else:DOM023_1.append('0') if i in DOM024: DOM024_1.append('1') else:DOM024_1.append('0') if i in DOM025: DOM025_1.append('1') else:DOM025_1.append('0') if i in DOM026: DOM026_1.append('1') else:DOM026_1.append('0') if i in NLF001: NLF001_1.append(' | 1') else:NLF001_1.append(' | 0') if i in NLF002: NLF002_1.append('1') else:NLF002_1.append('0') if i in NLF005: NLF005_1.append('1') else:NLF005_1.append('0') if i in NLF006: NLF006_1.append('1') else:NLF006_1.append('0') if i in NLF007: NLF007_1.append('1') else:NLF007_1.append('0') if i in NLF008: NLF008_1.append('1') else:NLF008_1.append('0') if i in NLF009: NLF009_1.append('1') else:NLF009_1.append('0') if i in NLF010: NLF010_1.append('1') else:NLF010_1.append('0') if i in NLF011: NLF011_1.append('1') else:NLF011_1.append('0') if i in NLF012: NLF012_1.append('1') else:NLF012_1.append('0') if i in NLF013: NLF013_1.append('1') else:NLF013_1.append('0') if i in NLF014: NLF014_1.append('1') else:NLF014_1.append('0') if i in NLF015: NLF015_1.append('1') else:NLF015_1.append('0') if i in NLM001: NLM001_1.append('1') else:NLM001_1.append('0') if i in NLM002: NLM002_1.append('1') else:NLM002_1.append('0') if i in NLM003: NLM003_1.append('1') else:NLM003_1.append('0') if i in NLM004: NLM004_1.append('1') else:NLM004_1.append('0') if i in NLM005: NLM005_1.append('1') else:NLM005_1.append('0') if i in NLM006: NLM006_1.append('1') else:NLM006_1.append('0') if i in NLM007: NLM007_1.append('1') else:NLM007_1.append('0') if i in NLM008: NLM008_1.append('1') else:NLM008_1.append('0') if i in NLM009: NLM009_1.append('1') else:NLM009_1.append('0') if i in NLM010: NLM010_1.append('1') else:NLM010_1.append('0') if i in NLM015: NLM015_1.append('1') else:NLM015_1.append('0') if i in NLM016: NLM016_1.append('1') else:NLM016_1.append('0') if i in NLM017: NLM017_1.append('1') else:NLM017_1.append('0') if i in NLM021: NLM021_1.append('1') else:NLM021_1.append('0') if i in NLM022: NLM022_1.append('1') else:NLM022_1.append('0') if i in NLM023: NLM023_1.append('1') else:NLM023_1.append('0') if i in NLM024: NLM024_1.append('1') else:NLM024_1.append('0') if i in NLM025: NLM025_1.append('1') else:NLM025_1.append('0') if i in NLM026: NLM026_1.append('1') else:NLM026_1.append('0') if i in NLM027: NLM027_1.append('1') else:NLM027_1.append('0') if i in NLM028: NLM028_1.append('1') else:NLM028_1.append('0') if i in NLM029: NLM029_1.append('1') else:NLM029_1.append('0') if i in NLM031: NLM031_1.append('1') else:NLM031_1.append('0') if i in NLM032: NLM032_1.append('1') else:NLM032_1.append('0') if i in NOF001: NOF001_1.append('1') else:NOF001_1.append('0') if i in NOF002: NOF002_1.append('1') else:NOF002_1.append('0') if i in NOF004: NOF004_1.append('1') else:NOF004_1.append('0') if i in NOF005: NOF005_1.append('1') else:NOF005_1.append('0') if i in NOF006: NOF006_1.append('1') else:NOF006_1.append('0') if i in NOF007: NOF007_1.append('1') else:NOF007_1.append('0') if i in NOF008: NOF008_1.append('1') else:NOF008_1.append('0') if i in NOF009: NOF009_1.append('1') else:NOF009_1.append('0') if i in NOF010: NOF010_1.append('1') else:NOF010_1.append('0') if i in NOF011: NOF011_1.append('1') else:NOF011_1.append('0') if i in NOF012: NOF012_1.append('1') else:NOF012_1.append('0') if i in NOF013: NOF013_1.append('1') else:NOF013_1.append('0') if i in NOF014: NOF014_1.append('1') else:NOF014_1.append('0') if i in NOM001: NOM001_1.append('1') else:NOM001_1.append('0') if i in NOM002: NOM002_1.append('1') else:NOM002_1.append('0') if i in NOM004: NOM004_1.append('1') else:NOM004_1.append('0') if i in NOM005: NOM005_1.append('1') else:NOM005_1.append('0') if i in NOM007: NOM007_1.append('1') else:NOM007_1.append('0') if i in NOM008: NOM008_1.append('1') else:NOM008_1.append('0') if i in NOM009: NOM009_1.append('1') else:NOM009_1.append('0') if i in NOM010: NOM010_1.append('1') else:NOM010_1.append('0') if i in NOM012: NOM012_1.append('1') else:NOM012_1.append('0') if i in NOM013: NOM013_1.append('1') else:NOM013_1.append('0') if i in NOM015: NOM015_1.append('1') else:NOM015_1.append('0') if i in NOM016: NOM016_1.append('1') else:NOM016_1.append('0') if i in NOM017: NOM017_1.append('1') else:NOM017_1.append('0') if i in NOM018: NOM018_1.append('1') else:NOM018_1.append('0') if i in NOM019: NOM019_1.append('1') else:NOM019_1.append('0') if i in NOM020: NOM020_1.append('1') else:NOM020_1.append('0') if i in NOM022: NOM022_1.append('1') else:NOM022_1.append('0') if i in NOM023: NOM023_1.append('1') else:NOM023_1.append('0') if i in NOM025: NOM025_1.append('1') else:NOM025_1.append('0') if i in NOM026: NOM026_1.append('1') else:NOM026_1.append('0') if i in NOM027: NOM027_1.append('1') else:NOM027_1.append('0') if i in NOM028: NOM028_1.append('1') else:NOM028_1.append('0') if i in NOM029: NOM029_1.append('1') else:NOM029_1.append('0') ## print 'saving' zoo = zip(main_op_list, DLF002_1,DLF004_1,DLF005_1,DLF006_1,DLF007_1,DLF008_1,DLF009_1,DLF010_1,DLF012_1,DLF013_1,DLF014_1,DLM001_1,DLM002_1,DLM003_1,DLM004_1,DLM005_1,DLM006_1,DLM009_1,DLM011_1,DLM012_1,DLM018_1,DOF002_1,DOF003_1,DOF004_1,DOF006_1,DOF007_1,DOF008_1,DOF009_1,DOF010_1,DOF011_1,DOF012_1,DOF013_1,DOF014_1,DOM001_1,DOM003_1,DOM005_1,DOM008_1,DOM010_1,DOM012_1,DOM013_1,DOM014_1,DOM015_1,DOM016_1,DOM017_1,DOM018_1,DOM019_1,DOM020_1,DOM021_1,DOM022_1,DOM023_1,DOM024_1,DOM025_1,DOM026_1,NLF001_1,NLF002_1,NLF005_1,NLF006_1,NLF007_1,NLF008_1,NLF009_1,NLF010_1,NLF011_1,NLF012_1,NLF013_1,NLF014_1,NLF015_1,NLM001_1,NLM002_1,NLM003_1,NLM004_1,NLM005_1,NLM006_1,NLM007_1,NLM008_1,NLM009_1,NLM010_1,NLM015_1,NLM016_1,NLM017_1,NLM021_1,NLM022_1,NLM023_1,NLM024_1,NLM025_1,NLM026_1,NLM027_1,NLM028_1,NLM029_1,NLM031_1,NLM032_1,NOF001_1,NOF002_1,NOF004_1,NOF005_1,NOF006_1,NOF007_1,NOF008_1,NOF009_1,NOF010_1,NOF011_1,NOF012_1,NOF013_1,NOF014_1,NOM001_1,NOM002_1,NOM004_1,NOM005_1,NOM007_1,NO M008_1,NOM009_1,NOM010_1,NOM012_1,NOM013_1,NOM015_1,NOM016_1,NOM017_1,NOM018_1,NOM019_1,NOM020_1,NOM022_1,NOM023_1,NOM025_1,NOM026_1,NOM027_1,NOM028_1,NOM029_1) with open("test.tab", 'w+') as outfile: writer =csv.writer(outfile, delimiter = '\t', lineterminator = '\n') writer.writerow([' ','DLF2','DLF4','DLF5','DLF6','DLF7','DLF8','DLF9','DLF10','DLF12','DLF13','DLF14','DLM1','DLM2','DLM3','DLM4','DLM5','DLM6','DLM9','DLM11','DLM12','DLM18','DOF2','DOF3','DOF4','DOF6','DOF7','DOF8','DOF9','DOF10','DOF11','DOF12','DOF13','DOF04','DOM1','DOM3','DOM5','DOM8','DOM10','DOM12','DOM13','DOM14','DOM15','DOM16','DOM17','DOM18','DOM19','DOM20','DOM21','DOM22','DOM23','DOM24','DOM25','DOM26','NLF1','NLF2','NLF5','NLF6','NLF7','NLF8','NLF9','NLF10','NLF11','NLF12','NLF13','NLF14','NLF15','NLM1','NLM2','NLM3','NLM4','NLM5','NLM6','NLM7','NLM8','NLM9','NLM10','NLM15','NLM16','NLM17','NLM21','NLM22','NLM23','NLM24','NLM25','NLM26','NLM27','NLM28','NLM29','NLM31','NLM32','NOF1','NOF2','NOF4','NOF5','NOF6','NOF7','NOF8','NOF9','NOF10','NOF11','NOF12','NOF13','NOF14','NOM1','NOM2','NOM4','NOM5','NOM7','NOM8','NOM9','NOM10','NOM12','NOM13','NOM15','NOM16','NOM17','NOM18','NOM19','NOM20','NOM22','NOM23','NOM25','NOM26','NOM27','NOM28','NOM29']) writer.writerows(zoo) outfile.close() print 'done' end_time = time.time() elapsed = end_time-start_time print "Time elapsed.", elapsed Thanks Best Regards Syed Shujaat Ali Zaidi PhD Scholar (Bioinformatics) MOE Key Laboratory of Bioinformatics Bioinformatics Division, TNLIST & Department of Automation FIT 1-107, Tsinghua University, Beijing 100084, China Lecturer (Bioinformatics) Department of Bio Sciences COMSATS Institute of Information Technology Islamabad, Pakistan _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From ganwilliam at outlook.com Mon Jun 12 10:17:18 2017 From: ganwilliam at outlook.com (William Gan) Date: Mon, 12 Jun 2017 14:17:18 +0000 Subject: [Tutor] Fahrenheit to Celsius Conversion with if else statements Message-ID: Good day Everybody, I am practicing coding when I encountered a problem with the if and else statements in my code. Hope someone can help me understand my mistake. The following is my code to convert Fahrenheit to Celsius and vice-versa: print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.') unit = input('Enter C or F:') temp = int(input('Enter temperature:')) if unit == 'C': f = (temp + 32) * 9 / 5 print(str(temp) + ' C is equivalent to ' + "%.2f" % f + ' F.') else: c = (temp - 32) * 5 / 9 print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.') OUT: Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius. Enter C or F:f Enter temperature:212 212 F is equivalent to 100.00 C. However, when I entered C, the else block was executed instead. The if block was skipped. Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius. Enter C or F:c Enter temperature:100 100 F is equivalent to 37.78 C. I could not figure out my mistake. For advice, please. Thank you. Best regards william From pjgibson25 at gmail.com Mon Jun 12 11:52:56 2017 From: pjgibson25 at gmail.com (Peter Gibson) Date: Mon, 12 Jun 2017 11:52:56 -0400 Subject: [Tutor] Creating 2 Subarrays for large dataset Message-ID: Hello, I have a large, 4 column data set that is in the form of an array. In the last column, there is either a 1 or a 2, and they are not organized in any predictable manner (ex of an array of the last columns: 1,2,2,1,2,2,1,1,1,1,2,1,1, ect). I would like to cut this large data set into two different arrays, one where the final column has a 1 in it, and the other where the final column of the data has a 2 in it. Please let me know if there is a way to create two separate arrays from my large data set and also how I can name these so that I may further manipulate the subsets of data. Thank you, PJ Gibson From vikasy at gmail.com Mon Jun 12 10:57:27 2017 From: vikasy at gmail.com (Vikas YADAV) Date: Mon, 12 Jun 2017 07:57:27 -0700 Subject: [Tutor] string reversal using [::-1] In-Reply-To: References: <00e001d2e208$32cb3680$9861a380$@gmail.com> Message-ID: <002c01d2e38c$357e1e30$a07a5a90$@gmail.com> Thanks, Abdur for forwarding Steve's message. It is a very good explanation with examples. The following example does help: --------------------------------------------- py> s = "Nobody expects the Spanish Inquisition!" py> s[-1:1:-2] '!otsun snp h tex db' is somewhat like: for i in range(len(s)-1, 1, -2): print s[i] --------------------------------------------- So my question is: how would you write "s[::-1]" in terms of a for loop for illustration purpose? Vikas -----Original Message----- From: Abdur-Rahmaan Janhangeer [mailto:arj.python at gmail.com] Sent: Monday, June 12, 2017 1:26 AM To: Vikas YADAV Cc: tutor Subject: Re: [Tutor] string reversal using [::-1] [QUOTED ENTIRELY FROM STEVE {steve at pearwood.info} IN ANSWER TO A SLICING QUESTION] The way to think about string indexing and slicing is that the index positions mark *between* the characters. Take your string: Machine learning is awesome! For brevity, I'll just use the first word: Machine Imagine slicing it between the characters. I'll mark the cuts with a vertical bar: |M|a|c|h|i|n|e| and add indexes. The indexes will only line correctly if you use a monspaced or fixed width font like Courier, otherwise things may not line up correctly. |M|a|c|h|i|n|e| 0 1 2 3 4 5 6 7 Here they are again starting from the right, I've spread things out a bit to fit in the minus signs: |M |a |c |h |i |n |e | -7 -6 -5 -4 -3 -2 -1 0 Notice that 0 gets used twice. Of course, that's impossible, because it would be ambiguous. If you give 0 as an index, how does Python know whether you mean 0 at the start or 0 or the end? So the simple rule Python uses is that 0 *always* means the start. When you give a single index, Python always uses the character immediately to the right of the cut: s = "Machine" s[0] => returns "M" s[-1] => returns "e" s[7] => raises an exception, because there is no character to the right When you give two indexes, using slice notation, Python returns the characters BETWEEN those cuts: s[0:7] => returns "Machine" s[1:-1] => returns "achin" Because 0 always means the start of the string, how do you slice to the end? You can use the length of the string (in this case, 7) or you can leave the ending position blank, and it defaults to the length of the string: s[1:] # means the same as [1:len(s)] You can leave the starting position blank too, it defaults to 0: s[:] # means the same as [0:len(s)] So remember that slices always cut *between* the index positions. Things get complicated when you include a step (or stride), especially when the step is negative. For step sizes other than 1, it is probably best to think of looping over the string: py> s = "Nobody expects the Spanish Inquisition!" py> s[-1:1:-2] '!otsun snp h tex db' is somewhat like: for i in range(len(s)-1, 1, -2): print s[i] -- Steve [QUOTED ENTIRELY FROM STEVE {steve at pearwood.info} IN ANSWER TO A SLICING QUESTION] Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 10 Jun 2017 21:31, "Vikas YADAV" wrote: > Question: Why does "123"[::-1] result in "321"? > > > > MY thinking is [::-1] is same as [0:3:-1], that the empty places > defaults to start and end index of the string object. > > So, if we start from 0 index and decrement index by 1 till we reach 3, > how many index we should get? I think we should get infinite infinite > number of indices (0,-1,-2,-3.). > > > > This is my confusion. > > I hope my question is clear. > > > > Thanks, > > Vikas > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Mon Jun 12 13:33:46 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 12 Jun 2017 18:33:46 +0100 Subject: [Tutor] Huge list comprehension In-Reply-To: References: Message-ID: On 12/06/17 05:54, syed zaidi wrote: > One reason fornsharing the code was that I have to manually create over 100 variables > Is there a way i can automate thst process? I doubt very much that you *have* to create 100 varables, that's an implementation choice as part of your design. If you tell us what the problem is you are trying to solve we might be able to suggest a more manageable solution - such as a better data structure perhaps? -- 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 From alan.gauld at yahoo.co.uk Mon Jun 12 13:36:26 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 12 Jun 2017 18:36:26 +0100 Subject: [Tutor] Fahrenheit to Celsius Conversion with if else statements In-Reply-To: References: Message-ID: On 12/06/17 15:17, William Gan wrote: > print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.') > unit = input('Enter C or F:') > temp = int(input('Enter temperature:')) > > if unit == 'C': Note this only t5ests for 'C' - ie capital C. You might want to force the input to be uppercase first? if unit.upper() == 'C': > f = (temp + 32) * 9 / 5 > print(str(temp) + ' C is equivalent to ' + "%.2f" % f + ' F.') > else: > c = (temp - 32) * 5 / 9 > print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.') > > However, when I entered C, the else block was executed instead. The if block was skipped. > > Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius. > > Enter C or F:c Note you entered lowercase 'c' not 'C'. Very different. -- 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 From alan.gauld at yahoo.co.uk Mon Jun 12 13:51:02 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 12 Jun 2017 18:51:02 +0100 Subject: [Tutor] Creating 2 Subarrays for large dataset In-Reply-To: References: Message-ID: On 12/06/17 16:52, Peter Gibson wrote: > I have a large, 4 column data set that is in the form of an array. Do you mean 4 separate arrays or a single 4xN array? Or do you mean an N size array of 4 item tuples? Or are the 4 colums part of a class? There are lots of ways to interpret that statement. Also are you using NumPy arrays or standard Python arrays, or standard Python lists/tuples treated as arrays? These all have a bearing. It might help if you post a small sample of your real data structure? > last column, there is either a 1 or a 2, and they are not organized in any > predictable manner (ex of an array of the last columns: > 1,2,2,1,2,2,1,1,1,1,2,1,1, ect). > > I would like to cut this large data set into two different arrays, one > where the final column has a 1 in it, and the other where the final column > of the data has a 2 in it. A trivial way to do that (assuming 4 arrays called col1,col2,col3,col4) is to create two lists/arrays and iterate over the data filtering on col4: ones = [] twos = [] for n in col4: if n == 1: ones.append((col1[n],col2[n],col3[n])) else: twos.append((col1[n],col2[n],col3[n])) If you must have arrays rather than lists that's a relatively minor tweak. However, depending on your data structures there are likely more efficient ways to do it (e.g. sorting on column 4 then using slicing, or using a dict keyed on col4, etc). but it all depends on how large 'large' is, what the actual time constraints are, what the real data structures look like etc. Premature optimisation is the root of all evil... -- 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 From alan.gauld at yahoo.co.uk Mon Jun 12 13:54:37 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 12 Jun 2017 18:54:37 +0100 Subject: [Tutor] string reversal using [::-1] In-Reply-To: <002c01d2e38c$357e1e30$a07a5a90$@gmail.com> References: <00e001d2e208$32cb3680$9861a380$@gmail.com> <002c01d2e38c$357e1e30$a07a5a90$@gmail.com> Message-ID: On 12/06/17 15:57, Vikas YADAV wrote: > for i in range(len(s)-1, 1, -2): > print s[i] > --------------------------------------------- > > So my question is: how would you write "s[::-1]" in terms of a for loop for illustration purpose? Exactly as above but replace -2 with -1 for i in range(len(s)-1, 1, -1): print s[i] But that seems too obvious, am I missing something subtle in your question? -- 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 From anish198519851985 at gmail.com Mon Jun 12 19:31:48 2017 From: anish198519851985 at gmail.com (anish singh) Date: Mon, 12 Jun 2017 16:31:48 -0700 Subject: [Tutor] decorators in a class Message-ID: Trying to use decorators in my class. I am calling build_tree from the main function and i want to increment the arguments by a constant factor by using decorators. However as build_tree is a recursive function, I don't want to call it recursively with increased constant factor always. Probably decorators are not the ideal way for this but still how to go about it using decorators. Simple solution would be to just pass the parameters after incrementing with constant values in the main function. However, I want the caller of build_tree to not know that internally we increment the indexes in the class and work on that. I can also call a intermediate function and then call build_tree but then would that be the right way? def pow_of_2(n): n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 n += 1 return n def p_decorate(func): def func_wrapper(self, left, right, root): return func(self, left+self.n, right+self.n, root) return func_wrapper class segment_tree(object): def __init__(self, data): self.n = pow_of_2(len(data)) self.tree = [0]*self.n + data + [0]*(self.n - len(data)) @p_decorate def build_tree(self, left, right, root): if left == right: return self.tree[left] #below build_tree should not use decorated function, #how to achieve that? s = self.build_tree(left, (left+right)/2, 2*root) + self.build_tree(1+(left+right)/2, right, 2*root+1) self.tree[root] = s def __repr__(self): return " ".join(str(i) for i in self.tree) data = [1, 2, 3, 4] sg = segment_tree(data) sg.build_tree(0, 7, 1) print(sg) From ganwilliam at outlook.com Mon Jun 12 14:16:15 2017 From: ganwilliam at outlook.com (William Gan) Date: Mon, 12 Jun 2017 18:16:15 +0000 Subject: [Tutor] Fahrenheit to Celsius Conversion with if else statements In-Reply-To: References: Message-ID: Good day Alan, Very much thanks for your guidance. I have added or 'c' to the if statement. That is resolved. Through that correction I discovered my C to F code was wrong. The + 32 is supposed to be executed at the end. Thanks again. Cheers -----Original Message----- From: Alan Gauld [mailto:alan.gauld at yahoo.co.uk] Sent: Tuesday, June 13, 2017 1:36 AM To: tutor at python.org Subject: Re: [Tutor] Fahrenheit to Celsius Conversion with if else statements On 12/06/17 15:17, William Gan wrote: > print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to > Celsius.') unit = input('Enter C or F:') temp = int(input('Enter > temperature:')) > > if unit == 'C': Note this only t5ests for 'C' - ie capital C. You might want to force the input to be uppercase first? if unit.upper() == 'C': > f = (temp + 32) * 9 / 5 > print(str(temp) + ' C is equivalent to ' + "%.2f" % f + ' F.') > else: > c = (temp - 32) * 5 / 9 > print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.') > > However, when I entered C, the else block was executed instead. The if block was skipped. > > Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius. > > Enter C or F:c Note you entered lowercase 'c' not 'C'. Very different. -- 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 From dyoo at hashcollision.org Mon Jun 12 21:02:40 2017 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 12 Jun 2017 18:02:40 -0700 Subject: [Tutor] Huge list comprehension In-Reply-To: References: Message-ID: On Sun, Jun 11, 2017 at 9:54 PM, syed zaidi wrote: > Thanks > One reason fornsharing the code was that I have to manually create over 100 variables Don't do that. Anytime you have to manually repeat things over and over is a sign that you need a loop structure of some sort. Let's look at a little code. > with open("C:/Users/INVINCIBLE/Desktop/T2D_ALL_blastout_batch.txt", 'r') as f: [code cut ] > if dictionary.get(sample_name): > dictionary[sample_name].append(operon) > else: > dictionary[sample_name] = [] > dictionary[sample_name].append(operon) > locals().update(dictionary) ## converts dictionary keys to variables Ah. That last statement there is really big warning sign. Do *not* use locals(). locals() is almost never a good thing to use. It's dangerous and leads to suffering. Instead, just stick with your dictionary, and use the dictionary. If you need a mapping from a sample name to an array of strings, construct another dictionary to hold that information. Then most of the code here: > for i in main_op_list_np: > if i in DLF002: DLF002_1.append('1') > else:DLF002_1.append('0') > if i in DLF004: DLF004_1.append('1') > else:DLF004_1.append('0') > if i in DLF005: DLF005_1.append('1') > else:DLF005_1.append('0') > if i in DLF006: DLF006_1.append('1') > else:DLF006_1.append('0') > if i in DLF007: DLF007_1.append('1') > else:DLF007_1.append('0') > if i in DLF008: DLF008_1.append('1') > else:DLF008_1.append('0') ... will dissolve into a simple dictionary lookup, followed by an array append. Just to compare to a similar situation, consider the following. Let's say that we want to compute the letter frequency of a sentence. Here's one way we could do it: ######################## def histogram(message): a = 0 b = 0 c = 0 d = 0 # .... cut for letter in message: if letter == 'a': a = a + 1 else if letter == 'b': b = b + 1 # ... cut return { 'a': a, 'b': b, 'c': c, # ... cut } ######################## This is only a sketch. We can see how this would go, if we fill in the '...' with the obvious code. But it would also be a very bad approach. It's highly repetitive, and easy to mistpe: you might miss a letr. There's a much better approach. We can use a dictionary that maps from a letter to its given frequency. ######################### def histogram(message): frequencies = {} for letter in message: frequencies[letter] = frequencies.get(letter, 0) + 1 return frequencies ######################### Unlike the initial sketch, the version that takes advantage of dictionaries is short and simple, and we can run it: ############## >>> histogram('the quick brown fox') {' ': 3, 'c': 1, 'b': 1, 'e': 1, 'f': 1, 'i': 1, 'h': 1, 'k': 1, 'o': 2, 'n': 1, 'q': 1, 'r': 1, 'u': 1, 't': 1, 'w': 1, 'x': 1} >>> histogram('abacadabra') {'a': 5, 'c': 1, 'b': 2, 'r': 1, 'd': 1} ############## If you have questions, please feel free to ask. From __peter__ at web.de Tue Jun 13 03:09:12 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Jun 2017 09:09:12 +0200 Subject: [Tutor] decorators in a class References: Message-ID: anish singh wrote: > Trying to use decorators in my class. I am calling > build_tree from the main function and i want to increment > the arguments by a constant factor by using decorators. > However as build_tree is a recursive function, I don't want > to call it recursively with increased constant factor always. > > Probably decorators are not the ideal way for this but still > how to go about it using decorators. > > Simple solution would be to just pass the parameters after > incrementing with constant values in the main function. > However, I want the caller of build_tree to not know that > internally we increment the indexes in the class and work > on that. > I can also call a intermediate function and then call build_tree > but then would that be the right way? Your decorator is so specific that it will probably be used only once, and it interferes with your intention to call both the decorated and the undecorated version of the method. These are strong indications that decorators are not the right tool in this case. Instead I suggest that you use two methods: class SegmentTree: def build_tree(self, left, right, root): n = self.n return self._build_tree(left + n, right + n, root) def _build_tree(self, left, right, root): # recursive calls invoke _build_tree, not build_tree ... If you insist you can probably write that class SegmentTree: def _build_tree(self, left, right, root): # recursive calls invoke _build_tree, not build_tree ... build_tree = p_decorate(_build_tree) > def pow_of_2(n): > n -= 1 > n |= n >> 1 > n |= n >> 2 > n |= n >> 4 > n |= n >> 8 > n |= n >> 16 > n += 1 > return n > > def p_decorate(func): > def func_wrapper(self, left, right, root): > return func(self, left+self.n, right+self.n, root) > return func_wrapper > > class segment_tree(object): > def __init__(self, data): > self.n = pow_of_2(len(data)) > self.tree = [0]*self.n + data + [0]*(self.n - len(data)) > > @p_decorate > def build_tree(self, left, right, root): > if left == right: > return self.tree[left] > #below build_tree should not use decorated function, > #how to achieve that? > s = self.build_tree(left, (left+right)/2, 2*root) + > self.build_tree(1+(left+right)/2, right, 2*root+1) > self.tree[root] = s > > def __repr__(self): > return " ".join(str(i) for i in self.tree) > > data = [1, 2, 3, 4] > sg = segment_tree(data) > sg.build_tree(0, 7, 1) > print(sg) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From muddunurimahesh at gmail.com Tue Jun 13 05:09:16 2017 From: muddunurimahesh at gmail.com (Muddunuri Mahesh) Date: Tue, 13 Jun 2017 14:39:16 +0530 Subject: [Tutor] Query Message-ID: Where can i get the perfect tutorials for black scripting using python From alan.gauld at yahoo.co.uk Tue Jun 13 07:08:34 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 13 Jun 2017 12:08:34 +0100 Subject: [Tutor] Query In-Reply-To: References: Message-ID: On 13/06/17 10:09, Muddunuri Mahesh wrote: > Where can i get the perfect tutorials for black scripting using python I'm not sure what you mean by black scripting - and neither does google apparently... Other than that it is a gothic style of typescript font... But the perfect tutorial for anything does not exist so you are going to have to be more specific about what you want. Can you already program in any language? Can you already program in Python? What do you want to achieve? What kind of teaching style do you prefer - theory or hands-on? What kind of OS do you use? -- 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 From djvinlom at gmail.com Tue Jun 13 10:08:28 2017 From: djvinlom at gmail.com (DJ VIN Lom) Date: Tue, 13 Jun 2017 10:08:28 -0400 Subject: [Tutor] Raspberry pi 2 python help Message-ID: Can i create a script to have my pi change to a certian directory automaticlly after booting. I want it to be in a directory so when i ssh into it its ready and i dont need to spend time to do it. As well i dont want to carry a keyboard mouse and montor From francois.dion at gmail.com Tue Jun 13 11:01:25 2017 From: francois.dion at gmail.com (Francois Dion) Date: Tue, 13 Jun 2017 11:01:25 -0400 Subject: [Tutor] Raspberry pi 2 python help In-Reply-To: References: Message-ID: "These are not the scripts you are looking for" More seriously, you want to configure your shell. See the linux documentation project, beginner's guide to bash, chapter 3 in particular: http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html Until you login, your shell doesn't even exists, so you cant preemptively change it before the login. You can do it right at login time, through one of the files mentioned that chapter. Francois On Tue, Jun 13, 2017 at 10:08 AM, DJ VIN Lom wrote: > Can i create a script to have my pi change to a certian directory > automaticlly after booting. I want it to be in a directory so when i ssh > into it its ready and i dont need to spend time to do it. As well i dont > want to carry a keyboard mouse and montor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info - @f_dion From alan.gauld at yahoo.co.uk Tue Jun 13 12:08:54 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 13 Jun 2017 17:08:54 +0100 Subject: [Tutor] Raspberry pi 2 python help In-Reply-To: References: Message-ID: On 13/06/17 15:08, DJ VIN Lom wrote: > Can i create a script to have my pi change to a certian directory > automaticlly after booting. You have some serious misconceptions about how the Pi works. The Pi does not have any idea of a "directory" when it boots up. The whole concept of a home directory is related to users. When a user logs in the shell sets a variable to indicate that users home directory - you can have multiple users logging into your Pi at once and they each have a different home directory. But users log in after the Pi has booted - and it could be seconds, minutes, hours or days after before the first user logs in. Having established that it's your login that needs to be addressed we can look at how to set or change your home directory. One way is in your user definition in /etc/passwd. There is a field there that states where the user goes when they login and you can edit that. Secondly when your shell starts up it executes various startup scripts, depending on which shell you use. Assuming its bash you can put commands into .bashrc in your home directory, including a 'cd' to change to wherever you want. Finally, since you mention a Python script that concerns you then you can put commands into the script itself to change to any given directory before doing anything else. Use import os os.chdir(<"/your chosen/folder") > I want it to be in a directory so when i ssh > into it its ready and i dont need to spend time to do it. If it's about being in the right place at login you probably should just change the login directory in /etc/passwd. Although this begs the question why you don't want to use the default /home/userid directory? Maybe you should be creating links or aliases to the files you need? -- 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 From leamhall at gmail.com Tue Jun 13 12:11:49 2017 From: leamhall at gmail.com (leam hall) Date: Tue, 13 Jun 2017 12:11:49 -0400 Subject: [Tutor] Free Python Class on Coursera Message-ID: Hey everyone, There's a free pair of Python classes on Coursera. I'm fixing to take the second one "https://www.coursera.org/learn/program-code" that starts 26 Jun. If you've been here for a while and learned the basics, now is a good time to up your skills. If you are new here the first of the series is available. The next one starts 3 Jul and they run them a few times a year. https://www.coursera.org/learn/learn-to-program I took the first class some time back and enjoyed it. if anyone wants to join me on the second class, hop in! The class is free though Coursera wants to ask you for money anyway. Leam From mats at wichmann.us Tue Jun 13 11:07:15 2017 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 13 Jun 2017 09:07:15 -0600 Subject: [Tutor] Raspberry pi 2 python help In-Reply-To: References: Message-ID: On 06/13/2017 08:08 AM, DJ VIN Lom wrote: > Can i create a script to have my pi change to a certian directory > automaticlly after booting. I want it to be in a directory so when i ssh > into it its ready and i dont need to spend time to do it. As well i dont > want to carry a keyboard mouse and montor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > There are a ton of ways to do this, and none of the common ones involve python at all. The wording "have my pi change to a directory" doesn't match how it works... each new shell is a new context, so you need to set things upon entering that context - namely after ssh establishes the connection and the Pi launches a login shell in response. Examples: On the Pi, in your .bashrc, stick a cd command at the end On the Pi, in your .bashrc define a short alias for the cd you want, so what you type is very short On the Pi, set the home directory of the account you are going to ssh into to be the directory On your host end, feed a command to ssh that includes doing something at the other end, along this model: ssh -t dj at pi 'cd /some/path && exec bash -l' From s.molnar at sbcglobal.net Tue Jun 13 14:55:22 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Tue, 13 Jun 2017 14:55:22 -0400 Subject: [Tutor] Problem with Plot Legend Message-ID: <5940351A.6090407@sbcglobal.net> I am using Python3.6 in the Spyder3IDE and have a problem with the legend for a plot. I have attached the pg file. The code (in part) is: import numpy as np from mpl_toolkits.axes_grid1 import host_subplot import mpl_toolkits.axisartist as AA import matplotlib.pyplot as plt import matplotlib.patches as mpatches . . . (the portion of the code for Figure q1 has no problems) def two_scales(ax1, time, data1, data2, c1, c2) ax2 = ax1.twinx() ax1.plot(time, data1, 'r') ax1.set_xlabel("Distance ($\AA$)") ax1.set_ylabel('Atom Charge',color='r') ax2.plot(time, data2, 'b') ax2.set_ylabel('Orbital Energy',color='b') return ax1, ax2 t = data[:,0] s1 = data[:,3] s2 = data[:,5] # Create axes fig, ax = plt.subplots() ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b') # Change color of each axis def color_y_axis(ax, color): """Color your axes.""" for t in ax.get_yticklabels(): t.set_color(color) return None color_y_axis(ax1, 'r') color_y_axis(ax2, 'b') plt.title('Molecular Transforms') patch_red = mpatches.Patch(color='red',label='Atom Charge') patch_blue = mpatches.Patch(color='blue',label='Orbital Energy') plt.legend(handles = [patch_red,patch_blue]) plt.draw() plt.show() name_plt = name+'-fig2.png' fig.savefig(name_plt,bbox_inches='tight') The problem is that the legend is where I want it in the figure and contains what I want with one exception: The colored lines are too thick, the same width as the text. I've look in the literature and Googled for the solution, but I'm beginning to think that I don't know just what the question that I should be asking, hence no results. A pointer towards thech solution to this problem will be much appreciated. Thanks in advance. -- Stephen P. Molnar, Ph.D. Consultant www.molecular-modeling.net (614)312-7528 (c) Skype: smolnar1 From ganwilliam at outlook.com Wed Jun 14 10:20:56 2017 From: ganwilliam at outlook.com (William Gan) Date: Wed, 14 Jun 2017 14:20:56 +0000 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm Message-ID: Good day Everyone, I am seeking help on two issues. ISSUE 1: Yesterday I posted a problem on this tiny script I wrote for temperature conversion (as practice for a newbie). My error was pointed out to me that there is a difference in upper and lowercase letters. After correcting that error, I remember the tests I ran produced the correct outputs. However, today I modified only the print instruction a little to try to print out ? (in the second print clause). When I subsequently ran the script all the outputs were executed from the if clause, even when I input other letters (Please see below. I have removed the code to print degree C). print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.') unit = input('Enter C or F: ') temp = int(input('Enter temperature: ')) if unit == 'C' or 'c': f = temp * 9 / 5 + 32 print(str(temp) + ' C is equivalent to ' + '%.2f' % f + ' F.') elif unit == 'F' or 'f': c = (temp - 32) * 5 / 9 print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.') else: print('Please enter C or F in upper- or lowercase.') The if statement block is to convert Celsius to Fahrenheit. When I entered ?C? or ?c? and 100 for temperature, the output is correct: 100 C is equivalent to 212.00 F. The elif statement block is to convert Fahrenheit to Celsius. When I entered ?f? or another letter, in this case ?z? and ?g?, and 212 for temperature, I got: 212 C is equivalent to 413.60 F. I have looked at it many times today and could not see the error. Please advise. ISSUE 2: The second issue relates to the last statement above ?I have looked at it many times today and could not see the error?. I was hoping that someone, perhaps one with pedagogical experience and knowledge, could advise the following: 1. Is it possible that I may not have the right aptitude or mental paradigm to do computer programming? I think I don?t. My background is in finance, accounting and economics. When I have difficulty in certain concepts in these fields I could figure it out eventually, in reasonable time. However, I am having difficulty learning it. I have been learning for a few months already and I am not learning fast enough. 2. Nevertheless, I intend to keep learning and practicing, but wonder if there is an effective way to get a breakthrough into the programming paradigm? If so, kindly advise how and direct me to a suitable resource to do it. Many thanks. Best regards. From david at graniteweb.com Wed Jun 14 14:04:22 2017 From: david at graniteweb.com (David Rock) Date: Wed, 14 Jun 2017 13:04:22 -0500 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: Message-ID: <5BC8829E-C6B7-4A94-83BF-B941C5AFCBF5@graniteweb.com> > On Jun 14, 2017, at 09:20, William Gan wrote: > > However, today I modified only the print instruction a little to try to print out ? (in the second print clause). When I subsequently ran the script all the outputs were executed from the if clause, even when I input other letters (Please see below. I have removed the code to print degree C). > > > if unit == 'C' or 'c': > > f = temp * 9 / 5 + 32 > > print(str(temp) + ' C is equivalent to ' + '%.2f' % f + ' F.') > > elif unit == 'F' or 'f': > > c = (temp - 32) * 5 / 9 > > print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.') The problem is your if statement is flawed. if unit == 'C' or 'c?: This is not doing what you think it is. You are expecting: if unit == ?C? or unit == ?c? When doing an ?or? statement, each part of the logic on each side of the ?or? is evaluated, so if unit == ?C? is true, or if ?c? is true is what?s actually being checked. a bare character, ?c? will always evaluate to True, so the if is always true. What you actually want (checking if unit is ?C? or ?c?) can be done a few ways. The most common are: if unit == ?C? or unit ==?c?: or if unit in [?C?, ?c?]: > ISSUE 2: > > The second issue relates to the last statement above ?I have looked at it many times today and could not see the error?. > > > > I was hoping that someone, perhaps one with pedagogical experience and knowledge, could advise the following: > > 1. Is it possible that I may not have the right aptitude or mental paradigm to do computer programming? Unlikely. Programming is not about aptitude, it?s more about spending time understanding the rules. The above is a prime example of getting to understand the rules. Logic rules are notoriously specific; they check exactly what you tell them to check, which is not always what you think you are asking. It just takes time. > > 2. Nevertheless, I intend to keep learning and practicing, but wonder if there is an effective way to get a breakthrough into the programming paradigm? If so, kindly advise how and direct me to a suitable resource to do it. Really, just keep trying things. When you run into something like this, the most effective way to troubleshoot is narrow it down to the essential issue. In this case, ?why is the if statement always evaluating to true?? Look at the parts and re-read what each does (eg, reread how the ?or? operator works). You are doing fine. :-) ? David Rock david at graniteweb.com From alan.gauld at yahoo.co.uk Wed Jun 14 14:15:24 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 14 Jun 2017 19:15:24 +0100 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: Message-ID: On 14/06/17 15:20, William Gan wrote: > print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.') > > if unit == 'C' or 'c': You have hit a common error for beginners. reading this as a human it is quite clear what is meant but the computer sees it differently. It sees: if (unit == 'C') or 'c': Now, a non empty string like 'c' is always considered True in a boolean context so, the interpreter interprets it as: if (unit == 'C') or True: And since any 'or' test where one element is True is evaluated to True it reads as if True: and so the 'if' part is always executed. How to avoid this? There are several options: if unit == 'C' or unit == 'c': But that gets cumbersome if more than two values. Better is: if unit in ('C','c'): This is best if there are multiple true options not just upper/lower case or if unit.lower() == 'c': This is best is the strings are longer than a single letter. (You can use unit.upper() too, that's just an arbitrary choice) > 1. Is it possible that I may not have the right aptitude or mental paradigm to do computer programming? Possible, but unlikely, most folks with a basic math ability can pick up programming. You may not be a natural, and may never be a programming guru, but you should be able to get to the stage of competence. > However, I am having difficulty learning it. It is difficult, despite what some books would have you believe. If it wasn't difficult there would be no need to teach it as a university subject! You have to train your mind to think like a computer (as in the case above) and to break things down into often painfully detailed steps. But that is just practice. > I have been learning for a few months already and I am > not learning fast enough. Says who? I've been programming for over 40 years and am still learning new things every week. Don't give up, and keep asking questions. -- 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 From sebastian at fuentelibre.org Wed Jun 14 13:52:42 2017 From: sebastian at fuentelibre.org (Sebastian Silva) Date: Wed, 14 Jun 2017 12:52:42 -0500 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: Message-ID: Hi William, Glad to see the tutor list is being of help in your learning. On 14/06/17 09:20, William Gan wrote: > if unit == 'C' or 'c': In this case, it will always be true, because there are two conditions, either: * unit == 'C' or * 'c' As you can see, the second condition is not a comparison, but a string expression, that Python always evaluates to True (except for '' empty string). Thus, the combined condition is always true because the second expression is always True. The correct condition would be: if unit=='C' or unit=='c': Or perhaps more clear, also correct: if unit in ('C', 'c'): Or shorter: if unit in 'Cc': Hope it's useful, Regards, Sebastian From nulla.epistola at web.de Wed Jun 14 14:18:36 2017 From: nulla.epistola at web.de (Sibylle Koczian) Date: Wed, 14 Jun 2017 20:18:36 +0200 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: Message-ID: Am 14.06.2017 um 16:20 schrieb William Gan: > Good day Everyone, > > I am seeking help on two issues. > > ISSUE 1: > Yesterday I posted a problem on this tiny script I wrote for temperature conversion (as practice for a newbie). My error was pointed out to me that there is a difference in upper and lowercase letters. After correcting that error, I remember the tests I ran produced the correct outputs. > > However, today I modified only the print instruction a little to try to print out ? (in the second print clause). When I subsequently ran the script all the outputs were executed from the if clause, even when I input other letters (Please see below. I have removed the code to print degree C). > > print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.') > unit = input('Enter C or F: ') > temp = int(input('Enter temperature: ')) > > if unit == 'C' or 'c': > f = temp * 9 / 5 + 32 > print(str(temp) + ' C is equivalent to ' + '%.2f' % f + ' F.') > elif unit == 'F' or 'f': > c = (temp - 32) * 5 / 9 > print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.') > else: > print('Please enter C or F in upper- or lowercase.') > > The if statement block is to convert Celsius to Fahrenheit. > When I entered ?C? or ?c? and 100 for temperature, the output is correct: 100 C is equivalent to 212.00 F. > > The elif statement block is to convert Fahrenheit to Celsius. > When I entered ?f? or another letter, in this case ?z? and ?g?, and 212 for temperature, I got: 212 C is equivalent to 413.60 F. > > I have looked at it many times today and could not see the error. Please advise. > Reading other threads in this list might have helped more - this is quite a frequent beginner error. if myvar == val1 or val2: ... is parsed as if (myvar == val1) or val2: That is true if myvar == val1; it is also true if val2 has any value that Python regards as true. This last condition would only be false if val2 were 0, None, an empty list, an empty dictionary, the empty set or another object with some sort of null value. There is no comparison between myvar and val2. Correct usage would be: if myvar == val1 or myval == val2: or if myvar in (val1, val2): Because this sort of problem has appeared so often in this list I looked into the official tutorial. There is 5.7, More on Conditions, but I'm not sure if that's enough for a beginner. HTH Sibylle From __peter__ at web.de Wed Jun 14 14:30:56 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 14 Jun 2017 20:30:56 +0200 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm References: Message-ID: Sebastian Silva wrote: > Or shorter: > > if unit in 'Cc': Don't do that. You are in for nasty surprises: >>> def check(unit): ... if unit in "Cc": ... return "Celsius" ... return "unknown" ... >>> check("c") 'Celsius' >>> check("C") 'Celsius' >>> check("F") 'unknown' Fine so far. But now: >>> check("Cc") 'Celsius' >>> check("") 'Celsius' From mats at wichmann.us Wed Jun 14 15:22:51 2017 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 14 Jun 2017 13:22:51 -0600 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: Message-ID: <072e974f-e9a0-a036-3fd6-64a3e8abb588@wichmann.us> On 06/14/2017 12:18 PM, Sibylle Koczian wrote: > Correct usage would be: > > if myvar == val1 or myval == val2: > or > if myvar in (val1, val2): Just piling on here to say I find the second form very useful to collect arguments in a "friendly" way, if you don't have a reason to very rigidly constrain them. For example, if you have an on/off type switch in your arguments (or "input()" type calls), you can say something like if myarg in ('T', 't', 'True', 'true', 'Y', 'y', 'Yes', 'yes', '1', 'ON', 'On', 'on'): Since that's getting too long, we can smash the casing: if myarg.lower() in ('t', 'true', 'y', 'yes', '1', 'on'): Of course if you do any serious argument handling, it's better to use something like optparse (and earlier argparse) module so you're not reinventing a wheel which has been massively worked on already. From akleider at sonic.net Wed Jun 14 18:08:10 2017 From: akleider at sonic.net (Alex Kleider) Date: Wed, 14 Jun 2017 15:08:10 -0700 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: <072e974f-e9a0-a036-3fd6-64a3e8abb588@wichmann.us> References: <072e974f-e9a0-a036-3fd6-64a3e8abb588@wichmann.us> Message-ID: <489b6c6ac4fbcd0b932183c12dab184b@sonic.net> On 2017-06-14 12:22, Mats Wichmann wrote: > Of course if you do any serious argument handling, it's better to use > something like optparse (and earlier argparse) module so you're not > reinventing a wheel which has been massively worked on already. > At the suggestion of a posting on this list some years ago, I've been using docopt rather than optparse or argparse and have found it provides much more with much less work. >>> pip install docopt From neilc at norwich.edu Thu Jun 15 08:52:10 2017 From: neilc at norwich.edu (Neil Cerutti) Date: Thu, 15 Jun 2017 12:52:10 +0000 (UTC) Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm References: Message-ID: On 2017-06-14, Peter Otten <__peter__ at web.de> wrote: > Sebastian Silva wrote: > >> Or shorter: >> >> if unit in 'Cc': > > Don't do that. You are in for nasty surprises: > >>>> def check(unit): > ... if unit in "Cc": > ... return "Celsius" > ... return "unknown" > ... >>>> check("c") > 'Celsius' >>>> check("C") > 'Celsius' >>>> check("F") > 'unknown' > > Fine so far. But now: > >>>> check("Cc") > 'Celsius' >>>> check("") > 'Celsius' Woah! I bet I've got that bug in several of my programs. Thanks! -- Neil Cerutti From tsu.yubo at gmail.com Thu Jun 15 17:54:21 2017 From: tsu.yubo at gmail.com (Bo Yu) Date: Fri, 16 Jun 2017 05:54:21 +0800 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: Message-ID: <20170615215419.ckkec362an7e4ipc@debian> On Thu, Jun 15, 2017 at 12:52:10PM +0000, Neil Cerutti wrote: >On 2017-06-14, Peter Otten <__peter__ at web.de> wrote: >> Sebastian Silva wrote: >> >>> Or shorter: >>> >>> if unit in 'Cc': >> >> Don't do that. You are in for nasty surprises: >> >>>>> def check(unit): >> ... if unit in "Cc": >> ... return "Celsius" >> ... return "unknown" >> ... >>>>> check("c") >> 'Celsius' >>>>> check("C") >> 'Celsius' >>>>> check("F") >> 'unknown' >> >> Fine so far. But now: >> >>>>> check("Cc") >> 'Celsius' >>>>> check("") >> 'Celsius' In fact, >>> check("cC") 'unknown' Best > >Woah! I bet I've got that bug in several of my programs. Thanks! > >-- >Neil Cerutti > >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor From david at graniteweb.com Thu Jun 15 14:39:16 2017 From: david at graniteweb.com (David Rock) Date: Thu, 15 Jun 2017 13:39:16 -0500 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: <5BC8829E-C6B7-4A94-83BF-B941C5AFCBF5@graniteweb.com> Message-ID: <1BC9AC42-55E9-4E6B-8CF0-8A8663D1FB43@graniteweb.com> > On Jun 15, 2017, at 13:16, William Gan wrote: > > Hi David, > > Very much thanks for taking time to help. > > Your explanation has helped me understand that syntax issue better. I have resolved that error. > > Your counsel on the second issue has given me encouragement. Thank you. I?m glad it helped. For completeness, in case you didn?t notice, your elif statement has the same issue. elif unit == 'F' or 'f?: c = (temp - 32) * 5 / 9 print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.? ? David Rock david at graniteweb.com From ganwilliam at outlook.com Thu Jun 15 14:11:14 2017 From: ganwilliam at outlook.com (William Gan) Date: Thu, 15 Jun 2017 18:11:14 +0000 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: Message-ID: Hi Alan, Very much thanks again for your help. Your elucidation helped me gain better understanding on this issue. I have resolved that error. Thank you also for your counsel on this second issue. Best regards. -----Original Message----- From: Alan Gauld [mailto:alan.gauld at yahoo.co.uk] Sent: Thursday, June 15, 2017 2:15 AM To: tutor at python.org Subject: Re: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm On 14/06/17 15:20, William Gan wrote: > print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to > Celsius.') > > if unit == 'C' or 'c': You have hit a common error for beginners. reading this as a human it is quite clear what is meant but the computer sees it differently. It sees: if (unit == 'C') or 'c': Now, a non empty string like 'c' is always considered True in a boolean context so, the interpreter interprets it as: if (unit == 'C') or True: And since any 'or' test where one element is True is evaluated to True it reads as if True: and so the 'if' part is always executed. How to avoid this? There are several options: if unit == 'C' or unit == 'c': But that gets cumbersome if more than two values. Better is: if unit in ('C','c'): This is best if there are multiple true options not just upper/lower case or if unit.lower() == 'c': This is best is the strings are longer than a single letter. (You can use unit.upper() too, that's just an arbitrary choice) > 1. Is it possible that I may not have the right aptitude or mental paradigm to do computer programming? Possible, but unlikely, most folks with a basic math ability can pick up programming. You may not be a natural, and may never be a programming guru, but you should be able to get to the stage of competence. > However, I am having difficulty learning it. It is difficult, despite what some books would have you believe. If it wasn't difficult there would be no need to teach it as a university subject! You have to train your mind to think like a computer (as in the case above) and to break things down into often painfully detailed steps. But that is just practice. > I have been learning for a few months already and I am not learning > fast enough. Says who? I've been programming for over 40 years and am still learning new things every week. Don't give up, and keep asking questions. -- 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 From ganwilliam at outlook.com Thu Jun 15 14:00:17 2017 From: ganwilliam at outlook.com (William Gan) Date: Thu, 15 Jun 2017 18:00:17 +0000 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: References: Message-ID: Hi Sebastian, Very much thanks for your help. Your explanation and illustrations is clear. I was not aware of that syntax. I now understand and the issue is resolved. Thanks again. Cheers. -----Original Message----- From: Sebastian Silva [mailto:sebastian at fuentelibre.org] Sent: Thursday, June 15, 2017 1:53 AM To: William Gan ; tutor at python.org Subject: Re: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm Hi William, Glad to see the tutor list is being of help in your learning. On 14/06/17 09:20, William Gan wrote: > if unit == 'C' or 'c': In this case, it will always be true, because there are two conditions, either: * unit == 'C' or * 'c' As you can see, the second condition is not a comparison, but a string expression, that Python always evaluates to True (except for '' empty string). Thus, the combined condition is always true because the second expression is always True. The correct condition would be: if unit=='C' or unit=='c': Or perhaps more clear, also correct: if unit in ('C', 'c'): Or shorter: if unit in 'Cc': Hope it's useful, Regards, Sebastian From ganwilliam at outlook.com Thu Jun 15 14:16:11 2017 From: ganwilliam at outlook.com (William Gan) Date: Thu, 15 Jun 2017 18:16:11 +0000 Subject: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm In-Reply-To: <5BC8829E-C6B7-4A94-83BF-B941C5AFCBF5@graniteweb.com> References: <5BC8829E-C6B7-4A94-83BF-B941C5AFCBF5@graniteweb.com> Message-ID: Hi David, Very much thanks for taking time to help. Your explanation has helped me understand that syntax issue better. I have resolved that error. Your counsel on the second issue has given me encouragement. Thank you. Best regards. -----Original Message----- From: David Rock [mailto:david at graniteweb.com] Sent: Thursday, June 15, 2017 2:04 AM To: tutor at python.org Subject: Re: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm > On Jun 14, 2017, at 09:20, William Gan wrote: > > However, today I modified only the print instruction a little to try to print out ? (in the second print clause). When I subsequently ran the script all the outputs were executed from the if clause, even when I input other letters (Please see below. I have removed the code to print degree C). > > > if unit == 'C' or 'c': > > f = temp * 9 / 5 + 32 > > print(str(temp) + ' C is equivalent to ' + '%.2f' % f + ' F.') > > elif unit == 'F' or 'f': > > c = (temp - 32) * 5 / 9 > > print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.') The problem is your if statement is flawed. if unit == 'C' or 'c?: This is not doing what you think it is. You are expecting: if unit == ?C? or unit == ?c? When doing an ?or? statement, each part of the logic on each side of the ?or? is evaluated, so if unit == ?C? is true, or if ?c? is true is what?s actually being checked. a bare character, ?c? will always evaluate to True, so the if is always true. What you actually want (checking if unit is ?C? or ?c?) can be done a few ways. The most common are: if unit == ?C? or unit ==?c?: or if unit in [?C?, ?c?]: > ISSUE 2: > > The second issue relates to the last statement above ?I have looked at it many times today and could not see the error?. > > > > I was hoping that someone, perhaps one with pedagogical experience and knowledge, could advise the following: > > 1. Is it possible that I may not have the right aptitude or mental paradigm to do computer programming? Unlikely. Programming is not about aptitude, it?s more about spending time understanding the rules. The above is a prime example of getting to understand the rules. Logic rules are notoriously specific; they check exactly what you tell them to check, which is not always what you think you are asking. It just takes time. > > 2. Nevertheless, I intend to keep learning and practicing, but wonder if there is an effective way to get a breakthrough into the programming paradigm? If so, kindly advise how and direct me to a suitable resource to do it. Really, just keep trying things. When you run into something like this, the most effective way to troubleshoot is narrow it down to the essential issue. In this case, ?why is the if statement always evaluating to true?? Look at the parts and re-read what each does (eg, reread how the ?or? operator works). You are doing fine. :-) ? David Rock david at graniteweb.com From tmrsg11 at gmail.com Sun Jun 18 13:07:07 2017 From: tmrsg11 at gmail.com (C W) Date: Sun, 18 Jun 2017 13:07:07 -0400 Subject: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder In-Reply-To: <20170609010012.GA89429@cskk.homeip.net> References: <4a75cc92-7d67-2e4b-f988-bed2603de9ae@wichmann.us> <20170609010012.GA89429@cskk.homeip.net> Message-ID: I come back to report that after trying it myself. Rodeo is the winner! Spyder came close. There's even a post on moving the layouts around just like RStudio. http://discuss.yhat.com/t/move-around-the-layout/43 I hope they implement that soon. Thanks everyone for your advice! On Thu, Jun 8, 2017 at 9:00 PM, Cameron Simpson wrote: > On 02Jun2017 18:14, Mats Wichmann wrote: > >> Sadly, vim, which is The Best Editor Ever (see 23 million flamewars for >> why such statement can only be a feeble attempt at humor, not reality), >> is now trying to behave like an IDE, and it's doing things badly wrong. >> For example, if I type import in a "from" line, it helpfully inserts the >> word import... meaning those end up with syntax errors, "from foo import >> import bar". I don't know who is responsible for that idiocy and >> haven't taken the time to figure out how to shut off the misbehavior. >> > > I'm sure it can be turned off; mine doesn't do this. I also turned off the > auto-comment-continuation. > > Anyway, somewhat off topic. > > -- Cameron Simpson > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From evuraan at gmail.com Mon Jun 19 15:32:37 2017 From: evuraan at gmail.com (Evuraan) Date: Mon, 19 Jun 2017 12:32:37 -0700 Subject: [Tutor] __str__ on a subclass Message-ID: Greetings! #!/usr/bin/python3 class Employee: """Class with FirstName, LastName, Salary""" def __init__(self, FirstName,LastName, Salary): self.FirstName = FirstName self.LastName = LastName self.Salary = Salary def __str__(self): return '("{}" "{}" "{}")'.format(self.FirstName, self.LastName, self.Salary) class Developer(Employee): """Define a subclass, augment with ProgLang""" def __init__(self, FirstName,LastName, Salary, ProgLang): Employee.__init__(self, FirstName,LastName, Salary) self.ProgLang = ProgLang def dev_repr(self): return '("{}" "{}" "{}" "{}")'.format(self.FirstName, self.LastName, self.Salary, self.ProgLang) a = Employee("Abigail", "Buchard", 83000) print(a) dev_1 = Developer("Samson", "Sue", 63000, "Cobol",) print(dev_1) print(dev_1.dev_repr()) running that yields, ("Abigail" "Buchard" "83000") ("Samson" "Sue" "63000") ("Samson" "Sue" "63000" "Cobol") My doubt is, how can we set the __str__ method work on the Employee subclass so that it would show ProgLang too, like the print(dev_1.dev_repr())? Thanks in advance! From alan.gauld at yahoo.co.uk Mon Jun 19 18:27:38 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 19 Jun 2017 23:27:38 +0100 Subject: [Tutor] __str__ on a subclass In-Reply-To: References: Message-ID: On 19/06/17 20:32, Evuraan wrote: > class Employee: > """Class with FirstName, LastName, Salary""" > def __init__(self, FirstName,LastName, Salary): > def __str__(self): > return '("{}" "{}" "{}")'.format(self.FirstName, > self.LastName, self.Salary) > class Developer(Employee): > """Define a subclass, augment with ProgLang""" > def __init__(self, FirstName,LastName, Salary, ProgLang): > Employee.__init__(self, FirstName,LastName, Salary) > self.ProgLang = ProgLang > def dev_repr(self): > return '("{}" "{}" "{}" "{}")'.format(self.FirstName, > self.LastName, self.Salary, self.ProgLang) > a = Employee("Abigail", "Buchard", 83000) > print(a) > dev_1 = Developer("Samson", "Sue", 63000, "Cobol",) > print(dev_1) > print(dev_1.dev_repr()) > > running that yields, > > ("Abigail" "Buchard" "83000") > ("Samson" "Sue" "63000") > ("Samson" "Sue" "63000" "Cobol") > > My doubt is, how can we set the __str__ method work on the Employee > subclass so that it would show ProgLang too, like the > print(dev_1.dev_repr())? You can't show ProgLang in Employee because it doesn't exist. You can only show it on Developer. To make __str__() work for developer you need to define it as you did for Employee. You could just call self.dev_repr() Or you could call super.__str__() and append self.ProgLang. eg. return super().__str__() + " " + self.ProgLang Does that answer you question? -- 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 From mats at wichmann.us Mon Jun 19 17:01:32 2017 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 19 Jun 2017 15:01:32 -0600 Subject: [Tutor] __str__ on a subclass In-Reply-To: References: Message-ID: On 06/19/2017 01:32 PM, Evuraan wrote: > Greetings! > > > #!/usr/bin/python3 > class Employee: > """Class with FirstName, LastName, Salary""" > def __init__(self, FirstName,LastName, Salary): > self.FirstName = FirstName > self.LastName = LastName > self.Salary = Salary > def __str__(self): > return '("{}" "{}" "{}")'.format(self.FirstName, > self.LastName, self.Salary) > class Developer(Employee): > """Define a subclass, augment with ProgLang""" > def __init__(self, FirstName,LastName, Salary, ProgLang): > Employee.__init__(self, FirstName,LastName, Salary) > self.ProgLang = ProgLang > def dev_repr(self): > return '("{}" "{}" "{}" "{}")'.format(self.FirstName, > self.LastName, self.Salary, self.ProgLang) > a = Employee("Abigail", "Buchard", 83000) > print(a) > dev_1 = Developer("Samson", "Sue", 63000, "Cobol",) > print(dev_1) > print(dev_1.dev_repr()) > > running that yields, > > ("Abigail" "Buchard" "83000") > ("Samson" "Sue" "63000") > ("Samson" "Sue" "63000" "Cobol") > > My doubt is, how can we set the __str__ method work on the Employee > subclass so that it would show ProgLang too, like the > print(dev_1.dev_repr())? Use super() to call up to the base class, and then add the extra bits pertaining to the derived class. That is, add this to your Developer subclass: def __str__(self): sup = super().__str__() return '{} "{}")'.format(sup, self.ProgLang) You'd need to do a little bit of work to clean up the output, as the string representation returned by the base class is already closed with a paren (see second line of output below): ("Abigail" "Buchard" "83000") ("Samson" "Sue" "63000") "Cobol") ("Samson" "Sue" "63000" "Cobol") but it should show a way to approach this problem, anyway From cs at zip.com.au Mon Jun 19 18:00:07 2017 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 20 Jun 2017 08:00:07 +1000 Subject: [Tutor] __str__ on a subclass In-Reply-To: References: Message-ID: <20170619220007.GA53359@cskk.homeip.net> On 19Jun2017 12:32, Evuraan wrote: >Greetings! Hi! >#!/usr/bin/python3 >class Employee: > """Class with FirstName, LastName, Salary""" > def __init__(self, FirstName,LastName, Salary): > self.FirstName = FirstName > self.LastName = LastName > self.Salary = Salary > def __str__(self): > return '("{}" "{}" "{}")'.format(self.FirstName, >self.LastName, self.Salary) >class Developer(Employee): > """Define a subclass, augment with ProgLang""" > def __init__(self, FirstName,LastName, Salary, ProgLang): > Employee.__init__(self, FirstName,LastName, Salary) > self.ProgLang = ProgLang > def dev_repr(self): > return '("{}" "{}" "{}" "{}")'.format(self.FirstName, >self.LastName, self.Salary, self.ProgLang) >a = Employee("Abigail", "Buchard", 83000) >print(a) >dev_1 = Developer("Samson", "Sue", 63000, "Cobol",) >print(dev_1) >print(dev_1.dev_repr()) > >running that yields, > >("Abigail" "Buchard" "83000") >("Samson" "Sue" "63000") >("Samson" "Sue" "63000" "Cobol") > >My doubt is, how can we set the __str__ method work on the Employee >subclass so that it would show ProgLang too, like the >print(dev_1.dev_repr())? Assuming that when you say "the Employee subclass" above you mean "Developer", just like any other method you would override in a subclass. When you define Developer, define a __str__ method: class Developer(Employee): ... def __str__(self): return ..... same expression as dev_repr ... Broadly speaking, a subclass is "just like" the parent, except as you specify. So specify __str__, since you want it to be different. Cheers, Cameron Simpson From __peter__ at web.de Tue Jun 20 02:41:05 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 20 Jun 2017 08:41:05 +0200 Subject: [Tutor] __str__ on a subclass References: Message-ID: Evuraan wrote: > Greetings! > > > #!/usr/bin/python3 > class Employee: > """Class with FirstName, LastName, Salary""" > def __init__(self, FirstName,LastName, Salary): > self.FirstName = FirstName > self.LastName = LastName > self.Salary = Salary > def __str__(self): > return '("{}" "{}" "{}")'.format(self.FirstName, > self.LastName, self.Salary) > class Developer(Employee): > """Define a subclass, augment with ProgLang""" > def __init__(self, FirstName,LastName, Salary, ProgLang): > Employee.__init__(self, FirstName,LastName, Salary) > self.ProgLang = ProgLang > def dev_repr(self): > return '("{}" "{}" "{}" "{}")'.format(self.FirstName, > self.LastName, self.Salary, self.ProgLang) > a = Employee("Abigail", "Buchard", 83000) > print(a) > dev_1 = Developer("Samson", "Sue", 63000, "Cobol",) > print(dev_1) > print(dev_1.dev_repr()) > > running that yields, > > ("Abigail" "Buchard" "83000") > ("Samson" "Sue" "63000") > ("Samson" "Sue" "63000" "Cobol") > > My doubt is, how can we set the __str__ method work on the Employee > subclass so that it would show ProgLang too, like the > print(dev_1.dev_repr())? > > Thanks in advance! Here's an indirect approach that should be easy to understand and adapt -- provide a method that converts attributes into a tuple. Subclasses that introduce new attributes can override this method, and __str__() uses as_tuple() to get the instance data instead of accessing the attributes directly: $ cat tmp.py #!/usr/bin/python3 def quote(value): # have you thought about attributes containing a '"'? return '"{}"'.format(value) class Employee: """Class with FirstName, LastName, Salary""" def __init__(self, FirstName, LastName, Salary): self.FirstName = FirstName self.LastName = LastName self.Salary = Salary def as_tuple(self): return self.FirstName, self.LastName, self.Salary def __str__(self): strings = (quote(a) for a in self.as_tuple()) return "({})".format(" ".join(strings)) class Developer(Employee): """Define a subclass, augment with ProgLang""" def __init__(self, FirstName, LastName, Salary, ProgLang): super().__init__(FirstName, LastName, Salary) self.ProgLang = ProgLang def as_tuple(self): return super().as_tuple() + (self.ProgLang,) print(Employee("Abigail", "Buchard", 83000)) print(Developer("Samson", "Sue", 63000, "Cobol")) $ python3 tmp.py ("Abigail" "Buchard" "83000") ("Samson" "Sue" "63000" "Cobol") Another option builds on the assumption that attribute names match __init__ argument names (I've replaced quote() with repr() this time): $ cat tmp2.py #!/usr/bin/python3 import inspect class Employee: """Class with FirstName, LastName, Salary""" def __init__(self, FirstName, LastName, Salary): self.FirstName = FirstName self.LastName = LastName self.Salary = Salary def __str__(self): names = inspect.getargspec(self.__init__).args[1:] values = (getattr(self, name) for name in names) return "({})".format(" ".join(map(repr, values))) class Developer(Employee): """Define a subclass, augment with ProgLang""" def __init__(self, FirstName, LastName, Salary, ProgLang): super().__init__(FirstName, LastName, Salary) self.ProgLang = ProgLang print(Employee("Abigail", "Buchard", 83000)) print(Developer("Samson", "Sue", 63000, "Cobol")) $ python3 tmp2.py ('Abigail' 'Buchard' 83000) ('Samson' 'Sue' 63000 'Cobol') From cebirim at gmail.com Tue Jun 20 04:34:21 2017 From: cebirim at gmail.com (angela ebirim) Date: Tue, 20 Jun 2017 09:34:21 +0100 Subject: [Tutor] Tutor Digest, Vol 160, Issue 26 In-Reply-To: References: Message-ID: Hi, I'm trying to dynamically build a url in python and have tried using: #file.py import json import requests from urllib.parse import urljoin baseUrl = " http://data.parliament.uk/membersdataplatform/services/mnis/members/query" search_criteria = "House=Commons" outputs = "Constituencies" headers = {"content-type": "application/json"} *""" so the resulting url: resultUrl should be * http://data.parliament.uk/membersdataplatform/services/mnis/members/query/House=Commons/Constituencies *"""* *result = search_criteria + outputs* *resultUrl = urljoin(baseUrl + result)* *"""However when I do the following:"""* *r = requests.get(resultUrl, headers=headers)* *r.encoding = "utf-8"* *dump = r.text* *members = json.loads(test[1:])* *""" I get a JSONdecodeError which complains about the first character in r.text being a <. The problem is if I use the url as is:"""* *r = requests.get("* http://data.parliament.uk/membersdataplatform/services/mnis/members/query/House=Commons/Constituencies", headers) """ it works I would like to give the user the option to switch between House=Commons and House=Lords but can't yet because I haven't figured out how to dynamically build url""" What am I doing wrong? Hoping that someone could help me. Many thanks, On 19 June 2017 at 17:00, wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > Today's Topics: > > 1. Re: Coming from R, what's a good IDE editor? I've tried > PyCharm and Spyder (C W) > > > ---------- Forwarded message ---------- > From: C W > To: Cameron Simpson > Cc: Mats Wichmann , tutor at python.org > Bcc: > Date: Sun, 18 Jun 2017 13:07:07 -0400 > Subject: Re: [Tutor] Coming from R, what's a good IDE editor? I've tried > PyCharm and Spyder > I come back to report that after trying it myself. Rodeo is the winner! > Spyder came close. > > There's even a post on moving the layouts around just like RStudio. > http://discuss.yhat.com/t/move-around-the-layout/43 > > I hope they implement that soon. Thanks everyone for your advice! > > On Thu, Jun 8, 2017 at 9:00 PM, Cameron Simpson wrote: > > > On 02Jun2017 18:14, Mats Wichmann wrote: > > > >> Sadly, vim, which is The Best Editor Ever (see 23 million flamewars for > >> why such statement can only be a feeble attempt at humor, not reality), > >> is now trying to behave like an IDE, and it's doing things badly wrong. > >> For example, if I type import in a "from" line, it helpfully inserts the > >> word import... meaning those end up with syntax errors, "from foo import > >> import bar". I don't know who is responsible for that idiocy and > >> haven't taken the time to figure out how to shut off the misbehavior. > >> > > > > I'm sure it can be turned off; mine doesn't do this. I also turned off > the > > auto-comment-continuation. > > > > Anyway, somewhat off topic. > > > > -- Cameron Simpson > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > From mats at wichmann.us Tue Jun 20 11:25:59 2017 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 20 Jun 2017 09:25:59 -0600 Subject: [Tutor] urljoin (was: Re: Tutor Digest, Vol 160, Issue 26) In-Reply-To: References: Message-ID: <31ba544f-2f56-82c3-6fbe-7bd107eec1e0@wichmann.us> On 06/20/2017 02:34 AM, angela ebirim wrote: > Hi, > > I'm trying to dynamically build a url in python and have tried using: can see a couple of things... > > #file.py > > import json > import requests > from urllib.parse import urljoin > > baseUrl = " > http://data.parliament.uk/membersdataplatform/services/mnis/members/query" > search_criteria = "House=Commons" > outputs = "Constituencies" > > headers = {"content-type": "application/json"} > > > *""" so the resulting url: resultUrl should be * > http://data.parliament.uk/membersdataplatform/services/mnis/members/query/House=Commons/Constituencies > *"""* > > *result = search_criteria + outputs* if you expected a '/' between the two bits here you're going to have to add it yourself. > *resultUrl = urljoin(baseUrl + result)* you would need a comma here instead of a plus, since you're trying to provide two things to join (I think you'd get a syntax error as written, so maybe that's a transcription error, not something really wrong in your code) plus urljoin often doesn't work the way people expect. In your baseUrl above you end with /query, and the query piece is going to be stripped off when urljoin'ing... if the "base" is the path to some page, then what you join to it will end relative to that location, not to the page - a trailing slash would change the sense to what I think you're expecting. Consider something like this: baseUrl = "http://data.parliament.uk/membersdataplatform/services/mnis/members/query/" # house is the one you want to search on query = "House=%s/Constituencies" % house resultUrl = urljoin(baseUrl, query) But you don't really need urljoin for this... you can just format up the url string yourself, since you don't need any of the special behavior. From sunnlotus at aol.com Tue Jun 20 18:39:31 2017 From: sunnlotus at aol.com (Rex Florian) Date: Tue, 20 Jun 2017 18:39:31 -0400 Subject: [Tutor] Class Message-ID: <15cc7a92a4e-44d9-33d1@webprd-a32.mail.aol.com> Hello, Below is a class I am using to comprehend how class works. The code came from tutorialspoint.com and executes correctly but I do not understand why it works. The original example defined just v1 and v2. I decided to experiment and instantiated v3. The executed the print statement yields a correct answer which baffles me as to how. I also tried (v1 + v2 + v3 + v1) which works as well. Can someone explain how Python achieves the vector addition of more than 2 vectors without some kind of looping? class Vector: def __init__(self, a, b): self.a = a self.b = b def __str__(self): return 'Vector (%d, %d)' % (self.a, self.b) def __add__(self,other): return Vector(self.a + other.a, self.b + other.b) v1 = Vector(2,10) v2 = Vector(5,-2) v3 = Vector(16,-14) print(v1 + v2 + v3) From alan.gauld at yahoo.co.uk Wed Jun 21 04:31:36 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 21 Jun 2017 09:31:36 +0100 Subject: [Tutor] urljoin In-Reply-To: <31ba544f-2f56-82c3-6fbe-7bd107eec1e0@wichmann.us> References: <31ba544f-2f56-82c3-6fbe-7bd107eec1e0@wichmann.us> Message-ID: On 20/06/17 16:25, Mats Wichmann wrote: >> *""" so the resulting url: resultUrl should be * >> http://data.parliament.uk/membersdataplatform/services/mnis/members/query/House=Commons/Constituencies >> *"""* > But you don't really need urljoin for this... you can just format up the > url string yourself, since you don't need any of the special behavior. I'd tend to agree with Mats here. Since you just want to change one substring the standard srtring formatting function is probably easier to use here: base = r"http://data.parliament.uk/membersdataplatform/services/mnis/members/query/House={0}/Constituencies" house = "Commons" # or house = "Lords" url = base.format(house) -- 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 From alan.gauld at yahoo.co.uk Wed Jun 21 04:37:32 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 21 Jun 2017 09:37:32 +0100 Subject: [Tutor] Class In-Reply-To: <15cc7a92a4e-44d9-33d1@webprd-a32.mail.aol.com> References: <15cc7a92a4e-44d9-33d1@webprd-a32.mail.aol.com> Message-ID: On 20/06/17 23:39, Rex Florian via Tutor wrote: > Can someone explain how Python achieves the vector addition of more than 2 vectors > without some kind of looping? > > class Vector: > def __init__(self, a, b): > def __str__(): > def __add__(self,other): > return Vector(self.a + other.a, self.b + other.b) > print(v1 + v2 + v3) When you do the addition Python evaluates it from left to right so it is interpreted as: ((v1+v2) + v3) so Python does: v1.__add__(v2) Which returns a new vector, let's call it vt Python then does: vt.__add__(v3) which returns another new vector, lets call it result, which is what gets printed using print (result.__str__()) -- 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 From tahir.hafiz at gmail.com Wed Jun 21 16:26:30 2017 From: tahir.hafiz at gmail.com (Tahir Hafiz) Date: Wed, 21 Jun 2017 21:26:30 +0100 Subject: [Tutor] how-to generate specific lines of text from two python lists Message-ID: Hi All, My python skills are limited but I have managed to generate a couple of lists using python and the psycopg2 library by querying a postgress table and it's columns. I would like to use the two generated lists from the python script to create a file called upgrade_email_addresses.sql (and then later on use the psql "postgress cli" with the -f flag against this .sql file) which will be used to update the users database with the correct email addresses. Is there a way to generate a file from two lists? This is what I need to do ... I have two lists such like: listUsernames = ['adal', '', 'pascalb', 'ritchied', 'torvaldsl', ... ] listEmailaddress = ['Ada_Lovelace at bigcorp.com', 'Blaise_Pascal at bigcorp.com', 'Dennis_Ritchie at bigcorp.com', 'Linus_Torvalds at bigcorp.com', ... ] So in my python script I would like to generate a text file (upgrade_email_addresses.sql) in the file system say in /tmp or /home, that will contain the following lines by perhaps looping against the lists in some way to create the lines in the external file: UPDATE users set email='Ada_Lovelace at bigcorp.com' WHERE username='adal'; UPDATE users set email='Blaise_Pascal at bigcorp.com' WHERE username='pascalb'; UPDATE users set email='Dennis_Ritchie at bigcorp.com' WHERE username='ritchied'; UPDATE users set email='Linus_Torvalds at bigcorp.com' WHERE username='torvaldsl'; .... .... Any help would be much appreciated. I was thinking I could run the UPDATE queries in the psycopg2 console function directly in my python script but haven't been able to do that but now I'm thinking creating a upgrade_email_addresses.sql file and then calling psql cli against that would be easier. Regards Tahir From __peter__ at web.de Wed Jun 21 18:33:20 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 22 Jun 2017 00:33:20 +0200 Subject: [Tutor] how-to generate specific lines of text from two python lists References: Message-ID: Tahir Hafiz wrote: > Hi All, > > My python skills are limited but I have managed to generate a couple > of lists using python and the psycopg2 library by querying a postgress > table and it's columns. > I would like to use the two generated lists from the python script to > create a file called upgrade_email_addresses.sql (and then later on > use the psql "postgress cli" with the -f flag against this .sql file) > which will be used to update the users database with the correct email > addresses. > > Is there a way to generate a file from two lists? This is what I need to > do ... > > I have two lists such like: > listUsernames = ['adal', '', 'pascalb', 'ritchied', 'torvaldsl', ... ] > listEmailaddress = ['Ada_Lovelace at bigcorp.com', > 'Blaise_Pascal at bigcorp.com', 'Dennis_Ritchie at bigcorp.com', > 'Linus_Torvalds at bigcorp.com', ... ] You can iterate over multiple lists with zip(): for user, email in zip(listUsernames, listEmailaddress): print(user, email) However, you have to be very careful to keep them in sync: >>> listUsernames = ['adal', '', 'pascalb', 'ritchied', 'torvaldsl'] >>> listEmailaddress = ['Ada_Lovelace at bigcorp.com', ... 'Blaise_Pascal at bigcorp.com', 'Dennis_Ritchie at bigcorp.com', ... 'Linus_Torvalds at bigcorp.com'] >>> for user, email in zip(listUsernames, listEmailaddress): ... print(user, email) ... adal Ada_Lovelace at bigcorp.com Blaise_Pascal at bigcorp.com pascalb Dennis_Ritchie at bigcorp.com ritchied Linus_Torvalds at bigcorp.com Oops, the empty string in listUsernames caused user and email address to get misaligned. > So in my python script I would like to generate a text file > (upgrade_email_addresses.sql) in the file system say in /tmp or /home, > that will contain the following lines by perhaps looping against the > lists in some way to create the lines in the external file: > UPDATE users set email='Ada_Lovelace at bigcorp.com' WHERE username='adal'; > UPDATE users set email='Blaise_Pascal at bigcorp.com' WHERE > username='pascalb'; UPDATE users set email='Dennis_Ritchie at bigcorp.com' > WHERE username='ritchied'; UPDATE users set > email='Linus_Torvalds at bigcorp.com' WHERE username='torvaldsl'; .... > .... Again you have to be very careful to make sure that all user-supplied data is properly quoted to defend against sql-injection attacks. > Any help would be much appreciated. I was thinking I could run the > UPDATE queries in the psycopg2 console function directly in my python > script but haven't been able to do that but now I'm thinking creating > a upgrade_email_addresses.sql file and then calling psql cli against > that would be easier. Here's an example script using psycopg2: $ cat psql_demo.py import psycopg2 db = psycopg2.connect(database="foo", user="bar") def show(): cursor.execute("select username, email from users;") for row in cursor.fetchall(): print(*row, sep=", ") users = [ 'adal', 'pascalb', 'ritchied', 'torvaldsl' ] emailaddresses = [ 'Ada_Lovelace at bigcorp.com', 'Blaise_Pascal at bigcorp.com', 'Dennis_Ritchie at bigcorp.com', 'Linus_Torvalds at bigcorp.com' ] cursor = db.cursor() print("before update") show() cursor.executemany( "update users set email=%s where username=%s;", zip(emailaddresses, users) ) db.commit() print("after update") show() $ python3 psql_demo.py before update adal, None pascalb, None ritchied, None torvaldsl, None after update adal, Ada_Lovelace at bigcorp.com pascalb, Blaise_Pascal at bigcorp.com ritchied, Dennis_Ritchie at bigcorp.com torvaldsl, Linus_Torvalds at bigcorp.com From rikudou__sennin at live.com Wed Jun 21 10:54:32 2017 From: rikudou__sennin at live.com (adil gourinda) Date: Wed, 21 Jun 2017 14:54:32 +0000 Subject: [Tutor] bottle: request.form.get() Message-ID: Hi I was looking for the "request.form.get()" method in "Python Library" but I didn't find it, I looked also in "Requests 2.18.1 Documentation" but nothing found. Please where I can find the documentation about "request.form" methods because it is used to retrieve data from tag. Thank for your help From alan.gauld at yahoo.co.uk Thu Jun 22 03:45:07 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 22 Jun 2017 08:45:07 +0100 Subject: [Tutor] bottle: request.form.get() In-Reply-To: References: Message-ID: On 21/06/17 15:54, adil gourinda wrote: > I was looking for the "request.form.get()" method in "Python Library"> but I didn't find it, I looked also in "Requests 2.18.1 Documentation" You don't say which requests package you are using. You reference bottle in the subject line and Requests in the body... If it is the requests package in bottle then the bottle web site has the documentation: http://bottlepy.org/docs/dev/ The "Python Library" - assuming you mean the standard library documented on python.org - does not include documentation for non-standard packages like bottle. -- 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 From __peter__ at web.de Thu Jun 22 03:54:15 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 22 Jun 2017 09:54:15 +0200 Subject: [Tutor] bottle: request.form.get() References: Message-ID: adil gourinda wrote: > I was looking for the "request.form.get()" method in "Python Library" > but I didn't find it, I looked also in "Requests 2.18.1 Documentation" > but nothing found. > Please where I can find the documentation about "request.form" methods > because it is used to retrieve data from tag. Did you mean request.forms.get()? The bottle tutorial shows how you can use that at https://bottlepy.org/docs/dev/tutorial.html#html-form-handling and the get() method is described here: https://bottlepy.org/docs/dev/api.html#bottle.MultiDict.get From alan.gauld at yahoo.co.uk Thu Jun 22 04:03:40 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 22 Jun 2017 09:03:40 +0100 Subject: [Tutor] how-to generate specific lines of text from two python lists In-Reply-To: References: Message-ID: On 21/06/17 21:26, Tahir Hafiz wrote: > My python skills are limited but I have managed to generate a couple > of lists using python and the psycopg2 library by querying a postgress > table and it's columns. You say your python skills are limited but how about your SQL skills? The reason I ask is that it sounds like you could greatly reduce the amount of Python work by slightly increasing the SQL... Right tool for the job etc. You say you generated two lists from a postgres table and its columns? A single table? If that's the case you should be able to easily extract the two data elements into a single list of tuples like (address,name). Even if its more than one table you should still be able to do it if there is any kind of logical link between the two pieces of data? > I would like to use the two generated lists from the python script to > create a file called upgrade_email_addresses.sql (and then later on > use the psql "postgress cli" with the -f flag against this .sql file) > which will be used to update the users database with the correct email > addresses. Why not just do the updates directly from Python? If you can do a SELECT from Python you can also do the UPDATE. Just iterate over the list of tuples generated above and execute an update for each tuple. > Is there a way to generate a file from two lists? Of course, and Peter has addressed that. But I'd ask first whether you even need to do it? > Any help would be much appreciated. I was thinking I could run the > UPDATE queries in the psycopg2 console function directly in my python > script but haven't been able to do that I'm not sure what the console function is, but I'd think you could run the UPDATEs directly from the dbapi with something like: for entry in data: cursor.execute(query, entry) where data is your list of tuples and query is your update statement. If, for some reason, you cannot extract one list as a set of tuples then you need to join the two lists, but remember that SELECT does not return its results in any order unless you specify an ORDER BY clause. So your two lists may not match entry for entry. Would that be an issue? How would you identify which address goes with which name? -- 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 From automatichypemn at gmail.com Thu Jun 22 10:20:21 2017 From: automatichypemn at gmail.com (Jack Burns) Date: Thu, 22 Jun 2017 09:20:21 -0500 Subject: [Tutor] Setup failure Message-ID: <8C46B44B-2B67-4D89-999C-09F97684DFED@gmail.com> Hi, I am running a google server and trying to download python. But I receive an during setup, which is setup failure. 0x80070659 This installation is forbidden by system policy. Contact your system administrator. Please help! Thanks! From alan.gauld at alan-g.me.uk Thu Jun 22 18:50:31 2017 From: alan.gauld at alan-g.me.uk (Alan Gauld) Date: Thu, 22 Jun 2017 23:50:31 +0100 Subject: [Tutor] Fwd: Re: bottle: request.form.get() In-Reply-To: <4efb6f7b-7b0c-ceac-9eaa-42d536be197c@yahoo.co.uk> References: <4efb6f7b-7b0c-ceac-9eaa-42d536be197c@yahoo.co.uk> Message-ID: <927ed86f-033e-f9a7-a362-e845c35eb096@alan-g.me.uk> Please always use ReplyAll when responding to the tutor list. (Or ReplyList if your mail client supports it) (Having said that I forgot to add the list - oops! :) On 22/06/17 19:48, adil gourinda wrote: > 1) I expected a well made documentaion that your The Python web site provides documentation for the full standard library, which includes some basic web programming tools. But those are not as easy to use as others such as bottle. Bottle provides full documentation (including a tutorial) on their web site Between the two you should find what you need. If not send us (or the bottle support group) your code and some sample data and we can try to help. The bottle support is at: A mailing list https://groups.google.com/forum/#!forum/bottlepy and a chat channel: https://webchat.freenode.net/?channels=bottlepy Both are linked on the main bottle web site. > 2) My problem is that I don't know how to retrieve data from
> tag, but in general I don't know how to connect between python and > html in script. I am new in web-framework, if you can advise me to a > more easy framework than bottle > I don't know bottle personally but I believe it's based on Flask and as such should be as easy to use as any of the (many) other web frameworks. In other words you should probably persevere with it rather than jump to another framework that won't be any easier to learn. -- 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 From malaclypse2 at gmail.com Fri Jun 23 16:18:42 2017 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 23 Jun 2017 16:18:42 -0400 Subject: [Tutor] Tutor Digest, Vol 160, Issue 26 In-Reply-To: References: Message-ID: On Tue, Jun 20, 2017 at 4:34 AM, angela ebirim wrote: > ?? > baseUrl = " > http://data.parliament.uk/membersdataplatform/services/mnis/members/query" > search_criteria = " > ?? > House=Commons" > outputs = " > ?? > Constituencies" > > headers = {"content-type": "application/json"} > > > *""" so the resulting url: resultUrl should be * > http://data.parliament.uk/membersdataplatform/services/ > mnis/members/query/House=Commons/Constituencies > *"""* > > ?? > *result > ?? > = search_criteria + outputs* > * > ?? > resultUrl = > ?? > urljoin(baseUrl + result)* > > ?Your strings aren't what you think they are. When you run result ? ? = search_criteria + outputs That concatenates the two string together, so result is now " ? House=Commons ? Constituencies" The same thing happens again on the next line. You call ? urljoin(baseUrl + result), but that isn't doing quite what you think either. Since you tell python to add those two stings together, it does that first, then passes the result to urljoin. In both cases, you're losing the '/' that ought to separate the pieces of your URL. Try this instead: result ?? = search_criteria + "/" + outputs resultUrl = ??urljoin(baseUrl, result) That should get you what you're looking for. As a more general piece of debugging advice, when things start to go wrong it's usually worth printing out the variables you're working with. That way you can make sure that they match your expectations. If you'd done that in this case, you would have immediately seen that your resultUrl was being built incorrectly. Also, it was super helpful to have comments saying what your expected results were. That makes it easier for someone else to come along and immediately understand what you're trying to accomplish, and see where you might have gone wrong. -- Jerry From malaclypse2 at gmail.com Fri Jun 23 16:35:40 2017 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 23 Jun 2017 16:35:40 -0400 Subject: [Tutor] Tutor Digest, Vol 160, Issue 26 In-Reply-To: References: Message-ID: On Fri, Jun 23, 2017 at 4:18 PM, Jerry Hill wrote: > Try this instead: > > result > ?? > = search_criteria + "/" + outputs > resultUrl = > ??urljoin(baseUrl, result) > > > That should get you what you're looking for. > > Sorry, ?I should have tested more carefully. That does NOT actually get what you were looking for, since the urljoin strips off the '/query' part of your base url. I think the other advice that you've received, to bypass urljoin entirely, is probably the right way to go. I think using urljoin properly in this case is more complicated than it's worth. -- Jerry From tahir.hafiz at gmail.com Sun Jun 25 14:44:33 2017 From: tahir.hafiz at gmail.com (Tahir Hafiz) Date: Sun, 25 Jun 2017 19:44:33 +0100 Subject: [Tutor] how-to generate specific lines of text from two python lists In-Reply-To: References: Message-ID: Thanks Alan and Peter, Alan you are right this could be solved via an SQL statement but I was asked to finish the python script. Anyways, this worked and helped to solve the problem in the end: # Create iterator object, dictionary which can be used to iterate against. b_iter = iter(new_emails) print "Creating a list of usernames and email addresses from retreived database data:" if __name__ == "__main__": dictionary = dict(zip(usernames, new_emails)) my_list = [] for username in usernames: my_list.append({'username':username, 'email':next(b_iter)}) print my_list print print "Creating a file called update_emails.sql with UPDATE statements from the list." # Open a file in write mode and write the UPDATE sql statements to the file # Close the file once iterated against. with open('update_emails.sql', 'w') as f: for i in my_list: mystring = "UPDATE users set email='{0}' WHERE username='{1}';" new_mystring = mystring.format(i['email'], i['username']) f.write(new_mystring + '\n') f.close() On Thu, Jun 22, 2017 at 9:03 AM, Alan Gauld via Tutor wrote: > On 21/06/17 21:26, Tahir Hafiz wrote: > >> My python skills are limited but I have managed to generate a couple >> of lists using python and the psycopg2 library by querying a postgress >> table and it's columns. > > You say your python skills are limited but how about your SQL skills? > The reason I ask is that it sounds like you could greatly reduce the > amount of Python work by slightly increasing the SQL... Right tool > for the job etc. > > You say you generated two lists from a postgres table and its columns? A > single table? > > If that's the case you should be able to easily extract the two data > elements into a single list of tuples like (address,name). > > Even if its more than one table you should still be able to do it > if there is any kind of logical link between the two pieces of data? > >> I would like to use the two generated lists from the python script to >> create a file called upgrade_email_addresses.sql (and then later on >> use the psql "postgress cli" with the -f flag against this .sql file) >> which will be used to update the users database with the correct email >> addresses. > > Why not just do the updates directly from Python? If you can do > a SELECT from Python you can also do the UPDATE. Just iterate > over the list of tuples generated above and execute an update > for each tuple. > >> Is there a way to generate a file from two lists? > > Of course, and Peter has addressed that. > But I'd ask first whether you even need to do it? > >> Any help would be much appreciated. I was thinking I could run the >> UPDATE queries in the psycopg2 console function directly in my python >> script but haven't been able to do that > > I'm not sure what the console function is, but I'd think you > could run the UPDATEs directly from the dbapi with something > like: > > for entry in data: > cursor.execute(query, entry) > > where data is your list of tuples and query is your > update statement. > > > If, for some reason, you cannot extract one list as a > set of tuples then you need to join the two lists, but > remember that SELECT does not return its results in > any order unless you specify an ORDER BY clause. > So your two lists may not match entry for entry. > Would that be an issue? How would you identify which > address goes with which name? > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From danny.yoo at gmail.com Sun Jun 25 15:05:46 2017 From: danny.yoo at gmail.com (Danny Yoo) Date: Sun, 25 Jun 2017 12:05:46 -0700 Subject: [Tutor] how-to generate specific lines of text from two python lists In-Reply-To: References: Message-ID: As the other tutors have suggested, look into doing the SQL updates directly, rather than format strings of SQL commands. A major reason is because strings do not natively support the representation of nested, grammatical things like sentences or SQL commands. It's really easy to write a string formatting program that works on simple input but will break spectacularly on real-world data. Do a web search for the term "Bobby Tables" for a concrete explanation of the problem. From danny.yoo at gmail.com Sun Jun 25 15:14:48 2017 From: danny.yoo at gmail.com (Danny Yoo) Date: Sun, 25 Jun 2017 12:14:48 -0700 Subject: [Tutor] how-to generate specific lines of text from two python lists In-Reply-To: References: Message-ID: On Jun 25, 2017 12:05 PM, "Danny Yoo" wrote: As the other tutors have suggested, look into doing the SQL updates directly, rather than format strings of SQL commands. Ah, here's a good resource: http://bobby-tables.com Just to emphasize: the reason I'm pointing this out is to try to counterbalance the tendency to solve a problem without asking: is this a good idea in the first place? The community of database programmers, after long experience with SQL injection, have learned through much pain and suffering. Unfortunately, this knowledge isn't evenly distributed yet. :) From evuraan at gmail.com Mon Jun 26 02:18:35 2017 From: evuraan at gmail.com (Evuraan) Date: Sun, 25 Jun 2017 23:18:35 -0700 Subject: [Tutor] joining Indic and English Text Message-ID: Greetings! I've a case where I need to put lines with both Indic and English Text to a file ( and optionally to stdout): # bash; ml_text="??????" en_text="Malayalam" echo "$ml_text = $en_text" >> /tmp/output.txt $ cat /tmp/output.txt ?????? = Malayalam That line above is what's I am trying to do in python. When I attempt this in python3: ml_text = u"??????" en_text = "Malayalam" print("{} = {}".format(ml_text, en_text)) Or, a = subprocess.getstatusoutput("echo " + ml_text + " = " + en_text + " >> /tmp/somefile ") I sometimes (not always, that's the strange part for now..) get errors like: UnicodeEncodeError: 'ascii' codec can't encode character '\u0d2b' in position 42: ordinal not in range(128) Searches on that error seem to suggest an .encode('utf-8), print("{} = {}".format(ml_text.encode("utf-8"), en_text)) I am afraid that would munge up my output line as : b'\xe0\xb4\xae\xe0\xb4\xb2\xe0\xb4\xaf\xe0\xb4\xbe\xe0\xb4\xb3\xe0\xb4\x82' = Malayalam, instead of the desired: ?????? = Malayalam What am I doing wrong? My locale and LANG (en_US.UTF-8) etc seem to be setup. Thanks in advance! From __peter__ at web.de Mon Jun 26 03:35:22 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 26 Jun 2017 09:35:22 +0200 Subject: [Tutor] joining Indic and English Text References: Message-ID: Evuraan wrote: > Greetings! > > I've a case where I need to put lines with both Indic and English Text > to a file ( and optionally to stdout): With... > What am I doing wrong? My locale and LANG (en_US.UTF-8) etc seem to be > setup. > When I attempt this in python3: ...that... > ml_text = u"??????" > en_text = "Malayalam" > print("{} = {}".format(ml_text, en_text)) ...should work. > I sometimes (not always, that's the strange part for now..) get errors > like: UnicodeEncodeError: 'ascii' codec can't encode character '\u0d2b' in > position 42: ordinal not in range(128) I'm unable to guess how that happened. When you encounter that error again, can you post the exact code that triggered it and how exactly you invoked it? That will increase our chance to find the source of the problem. Did you perhaps the PYTHONIOENCODING environment variable? $ PYTHONIOENCODING=ascii python3.5 -c 'print("???")' Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) But that's close to breaking things intentionally... > Searches on that error seem to suggest an .encode('utf-8), > > print("{} = {}".format(ml_text.encode("utf-8"), en_text)) > > I am afraid that would munge up my output line as : > b'\xe0\xb4\xae\xe0\xb4\xb2\xe0\xb4\xaf\xe0\xb4\xbe\xe0\xb4\xb3\xe0\xb4\x82' > = Malayalam, instead of the desired: > ?????? = Malayalam You have to encode the whole text, not just the non-ascii part of it and to write into a file opened in binary mode. However, I'd refrain from encoding manually when Python's default handling of text should work out of the box. From anish198519851985 at gmail.com Mon Jun 26 04:48:40 2017 From: anish198519851985 at gmail.com (anish singh) Date: Mon, 26 Jun 2017 01:48:40 -0700 Subject: [Tutor] custom comparator with ordered list Message-ID: I need a custom comparator. dictionary = {a:[b,c], d:[e,f]} If both 'b' and 'e' belong to the same bin then it should be compared based on 'c' and 'f'. However, I want to also represent the result of the sorted operation in a ordered dictionary as order is important. My custom comparator is something like this: ''' x and y is a list of two elements each''' def cmpr(x, y): r = 3 if x[0]//r != y[0]//r: return x[0]//r < y[0]//r return x[1] < y[1] Please note it is not exactly comparing the first elements of the value but checking if they belong to the same bin and they do then it checks the second element as as shown above. Example: {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17], 6:[17, 17] } output should be: {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17], 6:[17, 17] } http://ideone.com/lXBdr2 From __peter__ at web.de Mon Jun 26 05:35:08 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 26 Jun 2017 11:35:08 +0200 Subject: [Tutor] custom comparator with ordered list References: Message-ID: anish singh wrote: > I need a custom comparator. > > dictionary = {a:[b,c], d:[e,f]} > > If both 'b' and 'e' belong to the same bin > then it should be compared based on 'c' and 'f'. > > However, I want to also represent the result of the > sorted operation in a ordered dictionary as order is > important. > > My custom comparator is something like this: > > > ''' x and y is a list of two elements each''' > def cmpr(x, y): > r = 3 > if x[0]//r != y[0]//r: > return x[0]//r < y[0]//r > return x[1] < y[1] This looks like it should be called less() rather than compare() as it doesn't differentiate between the x < y and x == y case. > Please note it is not exactly comparing the first elements > of the value but checking if they belong to the same bin > and they do then it checks the second element as as shown > above. The effect should be the same. > Example: > {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17], 6:[17, > 17] } > output should be: > {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17], 6:[17, > 17] } >>> input = {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17], 6:[17, ... 17] } >>> wanted = {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17], 6:[17, ... 17] } >>> output = {k: v for k, v in sorted(input.items(), key=lambda x: (x[1] [0]//3, x[1][1]))} >>> assert list(output.items()) == list(wanted.items()) As written it will work with CPython 3.6. However, for compatibility with other versions of Python I recommend that you replace the plain dicts above with collections.OrderedDict instances. Quoting https://docs.python.org/dev/whatsnew/3.6.html#whatsnew36-pep520 """ The order-preserving aspect of this new [dict] implementation is considered an implementation detail and should not be relied upon [...] """ From anish198519851985 at gmail.com Mon Jun 26 12:38:46 2017 From: anish198519851985 at gmail.com (Anish Kumar) Date: Mon, 26 Jun 2017 09:38:46 -0700 Subject: [Tutor] custom comparator with ordered list Message-ID: <0805C099-989E-4C4F-BBDF-48DF5D943802@gmail.com> > anish singh wrote: > >> I need a custom comparator. >> >> dictionary = {a:[b,c], d:[e,f]} >> >> If both 'b' and 'e' belong to the same bin >> then it should be compared based on 'c' and 'f'. >> >> However, I want to also represent the result of the >> sorted operation in a ordered dictionary as order is >> important. >> >> My custom comparator is something like this: >> >> >> ''' x and y is a list of two elements each''' >> def cmpr(x, y): >> r = 3 >> if x[0]//r != y[0]//r: >> return x[0]//r < y[0]//r >> return x[1] < y[1] > > This looks like it should be called less() rather than compare() as it > doesn't differentiate between the x < y and x == y case. > >> Please note it is not exactly comparing the first elements >> of the value but checking if they belong to the same bin >> and they do then it checks the second element as as shown >> above. > > The effect should be the same. Well no, take the case of [1,100] and [2,0] Both belong to same bin suppose then it should be sorted based on second index and I would expect [2,0] [1,100] as output. This is not happening currently with the original code I have sent. > >> Example: >> {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17], 6:[17, >> 17] } >> output should be: >> {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17], 6:[17, >> 17] } > >>>> input = {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, > 17], 6:[17, > ... 17] } >>>> wanted = {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, > 17], 6:[17, > ... 17] } >>>> output = {k: v for k, v in sorted(input.items(), key=lambda x: (x[1] > [0]//3, x[1][1]))} >>>> assert list(output.items()) == list(wanted.items()) > > As written it will work with CPython 3.6. However, for compatibility with > other versions of Python I recommend that you replace the plain dicts above > with collections.OrderedDict instances. Quoting > > https://docs.python.org/dev/whatsnew/3.6.html#whatsnew36-pep520 > > """ > The order-preserving aspect of this new [dict] implementation is considered > an implementation detail and should not be relied upon [...] > """ > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 160, Issue 33 > ************************************** From __peter__ at web.de Mon Jun 26 13:32:39 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 26 Jun 2017 19:32:39 +0200 Subject: [Tutor] custom comparator with ordered list References: <0805C099-989E-4C4F-BBDF-48DF5D943802@gmail.com> Message-ID: Anish Kumar wrote: > >> anish singh wrote: >> >>> I need a custom comparator. >>> >>> dictionary = {a:[b,c], d:[e,f]} >>> >>> If both 'b' and 'e' belong to the same bin >>> then it should be compared based on 'c' and 'f'. >>> >>> However, I want to also represent the result of the >>> sorted operation in a ordered dictionary as order is >>> important. >>> >>> My custom comparator is something like this: >>> >>> >>> ''' x and y is a list of two elements each''' >>> def cmpr(x, y): >>> r = 3 >>> if x[0]//r != y[0]//r: >>> return x[0]//r < y[0]//r >>> return x[1] < y[1] >> >> This looks like it should be called less() rather than compare() as it >> doesn't differentiate between the x < y and x == y case. >> >>> Please note it is not exactly comparing the first elements >>> of the value but checking if they belong to the same bin >>> and they do then it checks the second element as as shown >>> above. >> >> The effect should be the same. > > Well no, take the case of [1,100] and [2,0] > Both belong to same bin suppose then it should > be sorted based on second index and I would > expect [2,0] [1,100] as output. > This is not happening currently with the original > code I have sent. I think that is because you do not consider all three cases. Let's start with a function cmp() modeled after the Python 2 built-in def cmp(a, b): if a < b: return -1 elif a > b: return 1 return 0 Then your comparator could be fixed (I think) as follows def compare(x, y): def bin(a): return a[0] // 3 result = cmp(bin(x), bin(y)) if result: return result return cmp(x[1], y[1]) and that "fixed" version would be equivalent (I think) to def compare(x, y) def key(a): return (a[0] // 3, a[1]) return cmp((key(x), key(y)) That said, even if you use Python 2 you should use sorted() with a key function rather than a comparison -- as shown below. Did that work for you? >>> Example: >>> {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17], >>> {6:[17, >>> 17] } >>> output should be: >>> {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17], >>> {6:[17, >>> 17] } >> >>>>> input = {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, >> 17], 6:[17, >> ... 17] } >>>>> wanted = {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, >> 17], 6:[17, >> ... 17] } >>>>> output = {k: v for k, v in sorted(input.items(), key=lambda x: (x[1] >> [0]//3, x[1][1]))} >>>>> assert list(output.items()) == list(wanted.items()) >> >> As written it will work with CPython 3.6. However, for compatibility with >> other versions of Python I recommend that you replace the plain dicts >> above with collections.OrderedDict instances. Quoting >> >> https://docs.python.org/dev/whatsnew/3.6.html#whatsnew36-pep520 >> >> """ >> The order-preserving aspect of this new [dict] implementation is >> considered an implementation detail and should not be relied upon [...] >> """ From mats at wichmann.us Mon Jun 26 13:22:21 2017 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 26 Jun 2017 11:22:21 -0600 Subject: [Tutor] custom comparator with ordered list In-Reply-To: <0805C099-989E-4C4F-BBDF-48DF5D943802@gmail.com> References: <0805C099-989E-4C4F-BBDF-48DF5D943802@gmail.com> Message-ID: <9ecb3bcb-ceaa-8e2c-d6a2-edf78689ab64@wichmann.us> On 06/26/2017 10:38 AM, Anish Kumar wrote: > >> anish singh wrote: >> >>> I need a custom comparator. >>> >>> dictionary = {a:[b,c], d:[e,f]} >>> >>> If both 'b' and 'e' belong to the same bin if would help alot if your problem statement included a description of what "same bin" is. > Well no, take the case of [1,100] and [2,0] > Both belong to same bin how would we conclude that these "belong to same bin"? From mats at wichmann.us Mon Jun 26 13:18:36 2017 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 26 Jun 2017 11:18:36 -0600 Subject: [Tutor] how-to generate specific lines of text from two python lists In-Reply-To: References: Message-ID: <56d6e476-64aa-1a3a-b156-83b06a4f1607@wichmann.us> On 06/25/2017 12:44 PM, Tahir Hafiz wrote: > Thanks Alan and Peter, > > Alan you are right this could be solved via an SQL statement but I was > asked to finish the python script. > Anyways, this worked and helped to solve the problem in the end: > > # Create iterator object, dictionary which can be used to iterate against. > b_iter = iter(new_emails) > > > print "Creating a list of usernames and email addresses from retreived > database data:" > if __name__ == "__main__": > dictionary = dict(zip(usernames, new_emails)) > my_list = [] > for username in usernames: > > my_list.append({'username':username, 'email':next(b_iter)}) > > print my_list > > print > print "Creating a file called update_emails.sql with UPDATE statements from > the list." > # Open a file in write mode and write the UPDATE sql statements to the file > # Close the file once iterated against. > with open('update_emails.sql', 'w') as f: > for i in my_list: > mystring = "UPDATE users set email='{0}' WHERE username='{1}';" > new_mystring = mystring.format(i['email'], i['username']) > f.write(new_mystring + '\n') > f.close() I'd like to point out that with this solution you are creating a dictionary named "dictionary" but then never using it, and you then proceed to make a list consisting of dictionaries which each look like a single record. Far be it from me to tell you this is wrong since you report the effort is working out!!! But you do seem to be redoing work. Consider this as a simplified alternative, which actually uses the dictionary you create up front (ignoring the very valid "don't do it this way" comments you have already received, let the database connector handle the quoting). Also note you don't need to "close the file once iterated against", since that's the exact purpose of the 'with' statement - to handle that cleanup for you. print "Creating dictionary of usernames and email addresses from retreived database data:" if __name__ == "__main__": dictionary = dict(zip(usernames, new_emails)) print dictionary print print "Creating a file called update_emails.sql with UPDATE statements from the list." # Open a file in write mode and write the UPDATE sql statements to the file with open('update_emails.sql', 'w') as f: for key in dictionary.keys(): mystring = "UPDATE users set email='{0}' WHERE username='{1}';" new_mystring = mystring.format(dictionary[key], key) f.write(new_mystring + '\n') From michealdpeterson at gmail.com Tue Jun 27 01:32:16 2017 From: michealdpeterson at gmail.com (Micheal Dale Peterson) Date: Mon, 26 Jun 2017 22:32:16 -0700 Subject: [Tutor] Using files to read data Message-ID: <996421DD-F654-4EFB-9417-6BF2782D33BD@gmail.com> hello I?m new to python. I have been trying to teach myself without asking questions because i feel i learn more. But i am stuck on this for a bout a week now. I am trying to write something that stores x and y data with a time reference and then use it later to create some type of a graph. I have tried saving to text files. and the closest i have come is using the json save. this is the closest output i can get(posted below). Trying to read it back does me no good. i can print each thing individually. everyday i try i either get a bunch of added characters or it separates every character. Sorry i can?t post all of the ways i have tried. I have been at this for a week and have changed things a thousand times and made a mess of most my files at this point. haha. Any help would be appreciated. I am running OS X version 10.12.5 with python 3.6.1. thank you in advance my saved lists -------------- next part -------------- my code to get saved list -------------- next part -------------- From __peter__ at web.de Tue Jun 27 04:52:07 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 27 Jun 2017 10:52:07 +0200 Subject: [Tutor] Using files to read data References: <996421DD-F654-4EFB-9417-6BF2782D33BD@gmail.com> Message-ID: Micheal Dale Peterson wrote: > my saved lists Hello Micheal! Have a look at to learn what we see of your mail. Unfortunately attachments are stripped off. Please resend as plain text, with your code and data inlined in the body of the mail. (Make sure that the data is small, just enough that we can get an idea of its structure.) From alan.gauld at yahoo.co.uk Tue Jun 27 04:56:39 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 27 Jun 2017 09:56:39 +0100 Subject: [Tutor] Using files to read data In-Reply-To: <996421DD-F654-4EFB-9417-6BF2782D33BD@gmail.com> References: <996421DD-F654-4EFB-9417-6BF2782D33BD@gmail.com> Message-ID: On 27/06/17 06:32, Micheal Dale Peterson wrote: > I am trying to write something that stores x and y data > with a time reference and then use it later ... Lets stick to one thing at a time. How far did you get writing the data to a file? Did the file get created? Can you edit in the text editor? The simplest way to do it is probably x = 42 y = 66 time = "10:40:22.5" with open("Myfile.txt") as output: output.write(str(x)+'\n') output.write(str(y)+'\n') output.write(time + '\n') That only outputs one line to the file Myfile.txt, but you could put the write lines into a loop if you had a list of data items. > the closest i have come is using the json save. Using json is a reasonable approach for this type of problem. Can you show us the actual code you used to write the data? I assume it did actually get saved to a file in this case? > Trying to read it back does me no good. That's too vague to be any use. What does "no good" mean? Could you open the file from Python? Did you get any data at all? Show us some code, even if it doesn't work as you expected. > i can print each thing individually. What do you mean? Do you mean you can read each item from the file and print it? If so what is it that you can't do? Can you save it in a variable? Can you add the variable to a list for future use? > everyday i try i either get a bunch of added characters Again this this too vague to be helpful. Show us what kind of characters. Show us the code you are using to read the data and print it. > or it separates every character. Again show us code, and sample data so we can see. The problem could lie in how you read the file or in how you display the data. But without code we can't tell. > Sorry i can?t post all of the ways i have tried. Just post the latest attempts we can start there. > I am running OS X version 10.12.5 with python 3.6.1. Thanks for that, it helps. > my code to get saved list There's no code... Did you send it as a binary attachment maybe? The list server strips off all binary attachments Cut 'n paste the code from your text editor into the mail body. -- 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 From alan.gauld at yahoo.co.uk Tue Jun 27 07:01:49 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 27 Jun 2017 12:01:49 +0100 Subject: [Tutor] Fwd: Re: Using files to read data In-Reply-To: References: Message-ID: <29dd2a51-b360-fb2f-dccb-1a05772bfbd3@yahoo.co.uk> Forwarding to list Please always use ReplyAll or ReplyList when responding to list mail. -------- Forwarded Message -------- i apologize. i guess it didn?t attach correctly. my issue is how do i get it out of my file and use it. *the json file, its only about a fifth of it but it should serve its purpose* [ 0.9888888888888889, 0.011111111111111112, "Mon Jun 26 20:37:34 2017" ] [ 0.9777777777777777, 0.022222222222222223, "Mon Jun 26 20:37:34 2017" ] [ 0.9666666666666667, 0.03333333333333333, "Mon Jun 26 20:37:34 2017" ] [ 0.9555555555555556, 0.044444444444444446, "Mon Jun 26 20:37:34 2017" ] [ 0.9444444444444444, 0.05555555555555555, "Mon Jun 26 20:37:34 2017" ] [ 0.9333333333333333, 0.06666666666666667, "Mon Jun 26 20:37:34 2017" ] [ 0.9222222222222223, 0.07777777777777778, "Mon Jun 26 20:37:34 2017" ] [ 0.9111111111111111, 0.08888888888888889, "Mon Jun 26 20:37:34 2017" ] [ 0.9, 0.1, "Mon Jun 26 20:37:34 2017" ] [ 0.8888888888888888, 0.1111111111111111, "Mon Jun 26 20:37:34 2017" ] [ 0.8777777777777778, 0.12222222222222222, "Mon Jun 26 20:37:34 2017" ] [ 0.8666666666666667, 0.13333333333333333, "Mon Jun 26 20:37:34 2017" ] [ 0.8555555555555555, 0.14444444444444443, "Mon Jun 26 20:37:34 2017" ] [ 0.8444444444444444, 0.15555555555555556, "Mon Jun 26 20:37:34 2017? *the code that creates it* import json import time def xyz(heading, forward_time): """get x and y increments of 360 degrees""" if heading in range(1, 91): north = heading east = 90 - heading y = (north / 90) * forward_time x = (east / 90) * forward_time now_time = time.ctime() xyz = [x, y, now_time] xyz_save = "xyz_save.json" with open(xyz_save, "a") as stamp: json.dump(xyz, stamp, indent=0) stamp.write("\n") return xyz elif heading in range(91, 181): east = heading - 90 south = 180 - heading y = (south / 90) * forward_time x = (east / -90) * forward_time now_time = time.ctime() xyz = [x, y, now_time] xyz_save = "xyz_save.json" with open(xyz_save, "a") as stamp: json.dump(xyz, stamp, indent=0) stamp.write("\n") return xyz elif heading in range(181, 271): south = heading - 180 west = 270 - heading y = (south / -90) * forward_time x = (west / -90) * forward_time now_time = time.ctime() xyz = [x, y, now_time] xyz_save = "xyz_save.json" with open(xyz_save, "a") as stamp: json.dump(xyz, stamp, indent=0) stamp.write("\n") return xyz elif heading in range(271, 361): west = heading - 270 north = 360 - heading y = (north / -90) * forward_time x = (west / 90) * forward_time now_time = time.ctime() xyz = [x, y, now_time] xyz_save = "xyz_save.json" with open(xyz_save, "a") as stamp: json.dump(xyz, stamp, indent=0) stamp.write("\n") return xyz *One of multiple loads I?ve got. * * * ', '0', ':', '3', '7', ':', '3', '4', ' ', '2', '0', '1', '7', '"', '\n', ']', '\n', '[', '\n', '0', '.', '0', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', ',', '\n', '0', '.', '9', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', ',', '\n', '"', 'M', 'o', 'n', ' ', 'J', 'u', 'n', ' ', '2', '6', ' ', '2', '0', ':', '3', '7', ':', '3', '4', ' ', '2', '0', '1', '7', '"', '\n', ']', '\n', '[', '\n', '0', '.', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', ',', '\n', '0', '.', '9', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', ',', '\n', '"', 'M', 'o', 'n', ' ', 'J', 'u', 'n', ' ', '2', '6', ' ', '2', '0', ':', '3', '7', ':', '3', '4', ' ', '2', '0', '1', '7', '"', '\n', ']', '\n', '\n?] * * * * * * *the closest i have got is this* * * [ 0.07777777777777778, 0.9222222222222223, "Mon Jun 26 20:37:34 2017" ] [ 0.06666666666666667, 0.9333333333333333, "Mon Jun 26 20:37:34 2017" ] [ 0.05555555555555555, 0.9444444444444444, "Mon Jun 26 20:37:34 2017" ] [ 0.044444444444444446, 0.9555555555555556, "Mon Jun 26 20:37:34 2017" ] [ 0.03333333333333333, 0.9666666666666667, "Mon Jun 26 20:37:34 2017" ] [ 0.022222222222222223, 0.9777777777777777, "Mon Jun 26 20:37:34 2017" ] [ 0.011111111111111112, 0.9888888888888889, "Mon Jun 26 20:37:34 2017? *with this code* filename = "xyz_save.json" with open(filename) as f_obj: lines = f_obj.read() print(lines) * * * * * * *i guess my ultimate question is how do i take all the x and y values and use them.* *i apologize for my ignorance as this is my first post about something very new to me.* *i also appreciate your patience* * * * * * * > On Jun 27, 2017, at 1:56 AM, Alan Gauld via Tutor > wrote: > > On 27/06/17 06:32, Micheal Dale Peterson wrote: >> I am trying to write something that stores x and y data >> with a time reference and then use it later .. > > Lets stick to one thing at a time. > How far did you get writing the data to a file? > Did the file get created? Can you edit in the > text editor? > > The simplest way to do it is probably > > x = 42 > y = 66 > time = "10:40:22.5" > > with open("Myfile.txt") as output: > output.write(str(x)+'\n') > output.write(str(y)+'\n') > output.write(time + '\n') > > > That only outputs one line to the file Myfile.txt, > but you could put the write lines into a loop if > you had a list of data items. > >> the closest i have come is using the json save. > > Using json is a reasonable approach for this type of problem. > Can you show us the actual code you used to write the data? > I assume it did actually get saved to a file in this case? > >> Trying to read it back does me no good. > > That's too vague to be any use. What does "no good" mean? > Could you open the file from Python? > Did you get any data at all? > Show us some code, even if it doesn't work as you expected. > >> i can print each thing individually. > > What do you mean? Do you mean you can read each > item from the file and print it? If so what is > it that you can't do? Can you save it in a variable? > Can you add the variable to a list for future use? > >> everyday i try i either get a bunch of added characters > > Again this this too vague to be helpful. > Show us what kind of characters. > Show us the code you are using to read the data and print it. > >> or it separates every character. > > Again show us code, and sample data so we can see. > The problem could lie in how you read the file or > in how you display the data. But without code we can't tell. > >> Sorry i can?t post all of the ways i have tried. > > Just post the latest attempts we can start there. > >> I am running OS X version 10.12.5 with python 3.6.1. > > Thanks for that, it helps. > >> my code to get saved list > > There's no code... > > Did you send it as a binary attachment maybe? > The list server strips off all binary attachments > Cut 'n paste the code from your text editor into > the mail body. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Tue Jun 27 08:46:29 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 27 Jun 2017 14:46:29 +0200 Subject: [Tutor] Fwd: Re: Using files to read data References: <29dd2a51-b360-fb2f-dccb-1a05772bfbd3@yahoo.co.uk> Message-ID: Alan Gauld via Tutor wrote: > Forwarding to list > > Please always use ReplyAll or ReplyList when responding to list mail. > > > > -------- Forwarded Message -------- > > i apologize. i guess it didn?t attach correctly. > my issue is how do i get it out of my file and use it. > > *the json file, its only about a fifth of it but it should serve its > purpose* > [ > 0.9888888888888889, > 0.011111111111111112, > "Mon Jun 26 20:37:34 2017" > ] > [ > 0.9777777777777777, > 0.022222222222222223, > "Mon Jun 26 20:37:34 2017" > ] The problem with this data is that it's not proper json which allows only one toplevel data structure, e. g. [ > [ > 0.9888888888888889, > 0.011111111111111112, > "Mon Jun 26 20:37:34 2017" > ] , > [ > 0.9777777777777777, > 0.022222222222222223, > "Mon Jun 26 20:37:34 2017" > ] ] If you stick to that file format you need to split it into parts that are valid json before you can process them with the standard library's json module. If you can rely on your records always consisting of five lines the following should work import json records = [] def read_five_lines(f): return "".join(f.readline() for i in range(5)) with open("xyz_save.json") as f: while True: chunk = read_five_lines(f) if not chunk: # empty string --> reached end of file break records.append(json.loads(chunk)) print(records) Another option would be to change your writing script so that it writes one record per line, e. g. [0.24444444444444446, 0.8555555555555556, "Tue Jun 27 14:21:44 2017"] [-0.29333333333333333, 1.906666666666667, "Tue Jun 27 14:21:44 2017"] This can be achieved by omitting the indent=0 argument in your json.dump() calls. Then your reader can be simplified: import json records = [] with open("xyz_save.json") as f: for line in f: records.append(json.loads(line)) print(records) > if heading in range(1, 91): Did you know that >>> 1.5 in range(1, 91) False ? If you want to accept non-integral values you better write the above as if 0 <= heading < 90: # swap operators if want to exclude the lower # and include the upper bound ... or similar. > north = heading > east = 90 - heading > y = (north / 90) * forward_time > x = (east / 90) * forward_time > now_time = time.ctime() > > xyz = [x, y, now_time] > > xyz_save = "xyz_save.json" > > > with open(xyz_save, "a") as stamp: > json.dump(xyz, stamp, indent=0) > stamp.write("\n") > > return xyz > > elif heading in range(91, 181): > east = heading - 90 > south = 180 - heading > y = (south / 90) * forward_time > x = (east / -90) * forward_time > now_time = time.ctime() > > xyz = [x, y, now_time] > > xyz_save = "xyz_save.json" > > > with open(xyz_save, "a") as stamp: > json.dump(xyz, stamp, indent=0) > stamp.write("\n") > > return xyz There's *a* *lot* of repetition here. At the very least you should move the parts that are completely identical to the end of the if ... elif ... chain: if heading in range(1, 91): north = heading east = 90 - heading y = (north / 90) * forward_time x = (east / 90) * forward_time elif heading in range(91, 181): east = heading - 90 south = 180 - heading y = (south / 90) * forward_time x = (east / -90) * forward_time ... else: # handling the case where no condition matches print("unhandled heading", heading, file=sys.stderr) return now_time = time.ctime() xyz = [x, y, now_time] xyz_save = "xyz_save.json" with open(xyz_save, "a") as stamp: json.dump(xyz, stamp) stamp.write("\n") return xyz PS: > north = heading > east = 90 - heading > y = (north / 90) * forward_time > x = (east / 90) * forward_time I wonder whether there should be a sin() and cos() somewhere... From mats at wichmann.us Tue Jun 27 12:13:42 2017 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 27 Jun 2017 10:13:42 -0600 Subject: [Tutor] Fwd: Re: Using files to read data In-Reply-To: <29dd2a51-b360-fb2f-dccb-1a05772bfbd3@yahoo.co.uk> References: <29dd2a51-b360-fb2f-dccb-1a05772bfbd3@yahoo.co.uk> Message-ID: On 06/27/2017 05:01 AM, Alan Gauld via Tutor wrote: > Forwarding to list > > Please always use ReplyAll or ReplyList when responding to list mail. > > > > -------- Forwarded Message -------- > > i apologize. i guess it didn?t attach correctly. > my issue is how do i get it out of my file and use it. > > *the json file, its only about a fifth of it but it should serve its > purpose* > [ > 0.9888888888888889, > 0.011111111111111112, > "Mon Jun 26 20:37:34 2017" > ] A few thoughts here... suggest storing the time not as a datetime string, but as a time value. That is, use time.time() to generate it; you can always convert that value to a string later if needed. if you're going to write as json, then you should read as json, don't create your own reader. in order to get data serialized in a more usable form, you could define a class, and serialize the class instances, this way you get the variable name stored along with the data. Since json only understands a limited number of data types, one cheap approach is to fish out the dictionary of data from the instance object - json does understand dicts. I'm using vars() for that. So here's a cheap example, not fleshed out to use any of your code: import time import json class Point(object): def __init__(self, x, y): self.x = x self.y = y self.time = time.time() # create a few points A = Point(0.022222222222222223, 0.9777777777777777) B = Point(0.011111111111111112, 0.9888888888888889) print(json.dumps(vars(A))) print(json.dumps(vars(B))) You'll see you get formatted data that json.loads() ought to be able to make use of: {"y": 0.9777777777777777, "x": 0.022222222222222223, "time": 1498577730.524801} {"y": 0.9888888888888889, "x": 0.011111111111111112, "time": 1498577730.524802} You don't need a class for that, you can just build the dict yourself, it was just a cheap way to illustrate. Simulated read: >>> import json >>> point = json.loads('{"y": 0.9777777777777777, "x": 0.022222222222222223, "time": 1498577980.382325}') >>> print(point['x']) 0.0222222222222 >>> print(point['y']) 0.977777777778 >>> print(point(['time']) 1498577980.38 >>> I wouldn't open/close the json file you're writing to each time, that seems a bit wasteful. And do take on board some of the refactoring suggestions already made. Dividing the heading by 90 using integer division (//) will give you four possible values, 0-3, you can then base your definitions/decisions on that, instead of your "if heading in range(1, 91):" selections. range actually generates the values, which you don't really need. Best of luck! From anish198519851985 at gmail.com Tue Jun 27 13:51:59 2017 From: anish198519851985 at gmail.com (anish singh) Date: Tue, 27 Jun 2017 10:51:59 -0700 Subject: [Tutor] Tutor Digest, Vol 160, Issue 34 In-Reply-To: References: Message-ID: > >> anish singh wrote: > >> > >>> I need a custom comparator. > >>> > >>> dictionary = {a:[b,c], d:[e,f]} > >>> > >>> If both 'b' and 'e' belong to the same bin > >>> then it should be compared based on 'c' and 'f'. > >>> > >>> However, I want to also represent the result of the > >>> sorted operation in a ordered dictionary as order is > >>> important. > >>> > >>> My custom comparator is something like this: > >>> > >>> > >>> ''' x and y is a list of two elements each''' > >>> def cmpr(x, y): > >>> r = 3 > >>> if x[0]//r != y[0]//r: > >>> return x[0]//r < y[0]//r > >>> return x[1] < y[1] > >> > >> This looks like it should be called less() rather than compare() as it > >> doesn't differentiate between the x < y and x == y case. > >> > >>> Please note it is not exactly comparing the first elements > >>> of the value but checking if they belong to the same bin > >>> and they do then it checks the second element as as shown > >>> above. > >> > >> The effect should be the same. > > > > Well no, take the case of [1,100] and [2,0] > > Both belong to same bin suppose then it should > > be sorted based on second index and I would > > expect [2,0] [1,100] as output. > > This is not happening currently with the original > > code I have sent. > > I think that is because you do not consider all three cases. > Let's start with a function cmp() modeled after the Python 2 built-in > > def cmp(a, b): > if a < b: > return -1 > elif a > b: > return 1 > return 0 > > Then your comparator could be fixed (I think) as follows > > def compare(x, y): > def bin(a): return a[0] // 3 > > result = cmp(bin(x), bin(y)) > if result: > return result > return cmp(x[1], y[1]) > > and that "fixed" version would be equivalent (I think) to > > def compare(x, y) > def key(a): return (a[0] // 3, a[1]) > > return cmp((key(x), key(y)) > > That said, even if you use Python 2 you should use sorted() with a key > function rather than a comparison -- as shown below. Did that work for you? > Yes it did. Thanks. > > >>> Example: > >>> {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17], > >>> {6:[17, > >>> 17] } > >>> output should be: > >>> {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17], > >>> {6:[17, > >>> 17] } > >> > >>>>> input = {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, > >> 17], 6:[17, > >> ... 17] } > >>>>> wanted = {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], > 3:[16, > >> 17], 6:[17, > >> ... 17] } > >>>>> output = {k: v for k, v in sorted(input.items(), key=lambda x: (x[1] > >> [0]//3, x[1][1]))} > >>>>> assert list(output.items()) == list(wanted.items()) > >> > >> As written it will work with CPython 3.6. However, for compatibility > with > >> other versions of Python I recommend that you replace the plain dicts > >> above with collections.OrderedDict instances. Quoting > >> > >> https://docs.python.org/dev/whatsnew/3.6.html#whatsnew36-pep520 > >> > >> """ > >> The order-preserving aspect of this new [dict] implementation is > >> considered an implementation detail and should not be relied upon [...] > >> """ > > > > > ------------------------------ > > Message: 3 > Date: Mon, 26 Jun 2017 11:22:21 -0600 > From: Mats Wichmann > To: tutor at python.org > Subject: Re: [Tutor] custom comparator with ordered list > Message-ID: <9ecb3bcb-ceaa-8e2c-d6a2-edf78689ab64 at wichmann.us> > Content-Type: text/plain; charset=utf-8 > > On 06/26/2017 10:38 AM, Anish Kumar wrote: > > > >> anish singh wrote: > >> > >>> I need a custom comparator. > >>> > >>> dictionary = {a:[b,c], d:[e,f]} > >>> > >>> If both 'b' and 'e' belong to the same bin > > if would help alot if your problem statement included a description of > what "same bin" is. > > > Well no, take the case of [1,100] and [2,0] > > Both belong to same bin > > how would we conclude that these "belong to same bin"? > > > > > ------------------------------ > > Message: 4 > Date: Mon, 26 Jun 2017 11:18:36 -0600 > From: Mats Wichmann > To: tutor at python.org > Subject: Re: [Tutor] how-to generate specific lines of text from two > python lists > Message-ID: <56d6e476-64aa-1a3a-b156-83b06a4f1607 at wichmann.us> > Content-Type: text/plain; charset=utf-8 > > On 06/25/2017 12:44 PM, Tahir Hafiz wrote: > > Thanks Alan and Peter, > > > > Alan you are right this could be solved via an SQL statement but I was > > asked to finish the python script. > > Anyways, this worked and helped to solve the problem in the end: > > > > # Create iterator object, dictionary which can be used to iterate > against. > > b_iter = iter(new_emails) > > > > > > print "Creating a list of usernames and email addresses from retreived > > database data:" > > if __name__ == "__main__": > > dictionary = dict(zip(usernames, new_emails)) > > my_list = [] > > for username in usernames: > > > > my_list.append({'username':username, 'email':next(b_iter)}) > > > > print my_list > > > > print > > print "Creating a file called update_emails.sql with UPDATE statements > from > > the list." > > # Open a file in write mode and write the UPDATE sql statements to the > file > > # Close the file once iterated against. > > with open('update_emails.sql', 'w') as f: > > for i in my_list: > > mystring = "UPDATE users set email='{0}' WHERE username='{1}';" > > new_mystring = mystring.format(i['email'], i['username']) > > f.write(new_mystring + '\n') > > f.close() > > I'd like to point out that with this solution you are creating a > dictionary named "dictionary" but then never using it, and you then > proceed to make a list consisting of dictionaries which each look like a > single record. Far be it from me to tell you this is wrong since you > report the effort is working out!!! But you do seem to be redoing work. > > Consider this as a simplified alternative, which actually uses the > dictionary you create up front (ignoring the very valid "don't do it > this way" comments you have already received, let the database connector > handle the quoting). Also note you don't need to "close the file once > iterated against", since that's the exact purpose of the 'with' > statement - to handle that cleanup for you. > > print "Creating dictionary of usernames and email addresses from > retreived database data:" > if __name__ == "__main__": > dictionary = dict(zip(usernames, new_emails)) > print dictionary > > print > print "Creating a file called update_emails.sql with UPDATE > statements from the list." > # Open a file in write mode and write the UPDATE sql statements to > the file > with open('update_emails.sql', 'w') as f: > for key in dictionary.keys(): > mystring = "UPDATE users set email='{0}' WHERE username='{1}';" > new_mystring = mystring.format(dictionary[key], key) > f.write(new_mystring + '\n') > > > > > ------------------------------ > > Message: 5 > Date: Mon, 26 Jun 2017 22:32:16 -0700 > From: Micheal Dale Peterson > To: tutor at python.org > Subject: [Tutor] Using files to read data > Message-ID: <996421DD-F654-4EFB-9417-6BF2782D33BD at gmail.com> > Content-Type: text/plain; charset="utf-8" > > hello I?m new to python. I have been trying to teach myself without asking > questions because i feel i learn more. But i am stuck on this for a bout a > week now. I am trying to write something that stores x and y data with a > time reference and then use it later to create some type of a graph. I > have tried saving to text files. and the closest i have come is using the > json save. this is the closest output i can get(posted below). Trying to > read it back does me no good. i can print each thing individually. everyday > i try i either get a bunch of added characters or it separates every > character. Sorry i can?t post all of the ways i have tried. I have been at > this for a week and have changed things a thousand times and made a mess of > most my files at this point. haha. Any help would be appreciated. I am > running OS X version 10.12.5 with python 3.6.1. thank you in advance > > > my saved lists > > -------------- next part -------------- > > > my code to get saved list > -------------- next part -------------- > > > > > ------------------------------ > > Message: 6 > Date: Tue, 27 Jun 2017 10:52:07 +0200 > From: Peter Otten <__peter__ at web.de> > To: tutor at python.org > Subject: Re: [Tutor] Using files to read data > Message-ID: > Content-Type: text/plain; charset="ISO-8859-1" > > Micheal Dale Peterson wrote: > > > my saved lists > > Hello Micheal! > > Have a look at > > > > to learn what we see of your mail. > > Unfortunately attachments are stripped off. Please resend as plain text, > with your code and data inlined in the body of the mail. (Make sure that > the > data is small, just enough that we can get an idea of its structure.) > > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 160, Issue 34 > ************************************** > From allantanaka11 at yahoo.com Wed Jun 28 11:48:34 2017 From: allantanaka11 at yahoo.com (Allan Tanaka) Date: Wed, 28 Jun 2017 15:48:34 +0000 (UTC) Subject: [Tutor] [Python2.7] Convert (2,188,1) into (188,1) References: <1137976406.539519.1498664914573.ref@mail.yahoo.com> Message-ID: <1137976406.539519.1498664914573@mail.yahoo.com> Hi. I have array shape like: (2,188,1). I want to make it like this: (188,1). I try that using .reshape(188,1) but throws an error:?total size of an array must be unchanged Thank you for the help! From henriquecsj at gmail.com Wed Jun 28 12:44:42 2017 From: henriquecsj at gmail.com (Henrique C. S. Junior) Date: Wed, 28 Jun 2017 13:44:42 -0300 Subject: [Tutor] Choosing between dictionary or external file Message-ID: Dear colleagues, how are you? I'm a scientist that is just starting with Python. I've decided to write a small software to generate inputs used in a Quantum Chemistry package. basically, we have to choose from several lists of options. Right now, I'm using dictionaries to store information. Here is an example: ........................ basis_sets = { "Pople-style basis sets": { "3-21G": "Pople 3-21G (H-Cs)", "STO-3G": "Minimal basis set(H-I)", "3-21GSP": "Buenker 3-21GSP (H-Ar)", "4-22GSP": "Buenker 4-22GSP (H-Ar)", "6-31G": "Pople 6-31G and its modifications (H-Zn)", "m6-31G": "Modified 6-31G for 3d transition metals (Sc-Cu)", "6-311G": "Pople 6-311G and its modifications (H-Br)"}, "The def2 basis sets of the Karlsruhe group": { "def2-SVP": "Valence double-zeta basis set with 'new' polarization functions", "def2-SV(P)": "The above with slightly reduced polarization", "def2-TZVP": "Valence triple-zeta basis set with 'new' polarization " "functions. Note that this is quite similar to the older ('def') TZVPP " "for the main group elements and TZVP for hydrogen.", "def2-TZVP(-f)": "TZVP with f polarization removed from main group elements", "def2-TZVPP": "TZVPP basis set with 'new' polarization functions", "def2-QZVPP": "Accurate polarized quadruple-zeta basis"}, ........................ My question is: is this the correct approach? The dictionaries, after finished, probably will not have to be changed for a long time and my idea is to import them in the main code. A second question is regarding the creation os a GUI, what are your recommendations (keeping in mind that I'm not an experienced programmer) Glade, PyQt + QT Designer, Tkinter or something else (to run on Linux, Mac and Windows)? . Thank you for any suggestions -- *Henrique C. S. Junior* Industrial Chemist - UFRRJ M. Sc. Inorganic Chemistry - UFRRJ Data Processing Center - PMP Visite o Mundo Qu?mico From lapsap7+python at gmail.com Wed Jun 28 12:34:34 2017 From: lapsap7+python at gmail.com (STF) Date: Wed, 28 Jun 2017 18:34:34 +0200 Subject: [Tutor] Program to check Python 2 syntaxes incompatible to Python 3? Message-ID: Hi, After reading some articles about Python 2 vs Python 3 issues and web pages like: https://docs.python.org/3/whatsnew/3.0.html https://wiki.python.org/moin/Python2orPython3 I'm wondering if there's any program/tool to list out incompatible syntaxes in a Python 2 source file. I know the 2to3 tool exists, but I'm more interested to do some manual change to source code if possible. Thanks in advance. From alan.gauld at yahoo.co.uk Wed Jun 28 13:39:23 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 28 Jun 2017 18:39:23 +0100 Subject: [Tutor] Choosing between dictionary or external file In-Reply-To: References: Message-ID: On 28/06/17 17:44, Henrique C. S. Junior wrote: > using dictionaries to store information. Here is an example: > > ........................ > basis_sets = { > "Pople-style basis sets": { > "3-21G": "Pople 3-21G (H-Cs)", > "STO-3G": "Minimal basis set(H-I)", > "3-21GSP": "Buenker 3-21GSP (H-Ar)", > "4-22GSP": "Buenker 4-22GSP (H-Ar)", > "6-31G": "Pople 6-31G and its modifications (H-Zn)", > "m6-31G": "Modified 6-31G for 3d transition metals (Sc-Cu)", > "6-311G": "Pople 6-311G and its modifications (H-Br)"}, > > "The def2 basis sets of the Karlsruhe group": { > "def2-SVP": "Valence double-zeta basis set with 'new' polarization > ........................ > > My question is: is this the correct approach? The dictionaries, after > finished, probably will not have to be changed for a long time and my idea > is to import them in the main code. Using dictionaries is ok, but your keys are inordinately long. I'd probably define the strings as a set of variables(consts) that can then be used in the dictionaries and in your code. Otherwise you are likely to wind up with horrible looking code like: if "beunker" in lower(basic_sets["Pople-style basis sets"]["3-21GSP"]): ..... Which is verbose at the very least and likely to be error prone too. Whereas if you define something like POPLE = "Pople-style basis sets" ... basic_sets = {POPLE: {....}} The code becomes if "beunker" in lower(basic_sets[POPLE]["3-21GSP"]): ... Which, I think, is slightly easier on the eye. (And you can still use the longhand version if you think you need it) > A second question is regarding the creation os a GUI, what are your > recommendations (keeping in mind that I'm not an experienced programmer) > Glade, PyQt + QT Designer, Tkinter or something else (to run on Linux, Mac > and Windows)? For easy to learn wrapping of a CLI I'd suggest Tkinter but if you want to distribute the code to others then Qt probably looks nicer and has more features. But a steeper learning curve. -- 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 From kay.budo at gmail.com Wed Jun 28 16:27:53 2017 From: kay.budo at gmail.com (cookiestar227 - Cookie Productions) Date: Wed, 28 Jun 2017 21:27:53 +0100 Subject: [Tutor] Query regarding Regular Expression Message-ID: I am using Python version 3.6.0 and am learning Python as my very first programming language. Right now I am studying Regular Expressions. We are finding all of the matched substrings in a string using the "findall" method with the following syntax: re.findall(pattern, string[, flags]) Here is a link to the page tutorial I am using: http://www.python-course.eu/python3_re_advanced.php So far have understood everything except for the following example: >>> t = "A fat cat doesn't eat oat but a rat eats bats." >>> mo = re.findall("[force]at", t) >>> print(mo) ['fat', 'cat', 'eat', 'oat', 'rat', 'eat'] What I don't understand is the [force] part of the Regular Expression. I cannot find anywhere online where there is an explanation of what [force] means or what is does. Why does it match to 'eat' from 'eats'? But not 'bat' from 'bats'? I would prefer to use the following RE as it achieves my desired result: >>> mo = re.findall("[A-Za-z]at", t) >>> print(mo) ['fat', 'cat', 'eat', 'oat', 'rat', 'eat', 'bat'] Can anyone tell me what the [force] means and why it was used? Thanks! Kelly From alan.gauld at yahoo.co.uk Wed Jun 28 19:03:39 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 29 Jun 2017 00:03:39 +0100 Subject: [Tutor] Query regarding Regular Expression In-Reply-To: References: Message-ID: On 28/06/17 21:27, cookiestar227 - Cookie Productions wrote: > So far have understood everything except for the following example: > >>>> t = "A fat cat doesn't eat oat but a rat eats bats." >>>> mo = re.findall("[force]at", t) > What I don't understand is the [force] part of the Regular Expression. A sequence of characters inside square brackets means match any one of the characters. So [force]at matches: fat, oat, rat, cat, eat It does not ,atch bat because there is no b inside the brackets. The fact that force spells a real word is misleading, it could just as well be written [ocfre]at and it would do the same. > I would prefer to use the following RE as it achieves my desired result: > >>>> mo = re.findall("[A-Za-z]at", t) >>>> print(mo) > ['fat', 'cat', 'eat', 'oat', 'rat', 'eat', 'bat'] Fine, but it does a different job, as you discovered. The problem with regex is that very minor changes in pattern can have big differences in output. Or, as you've shown a big difference in pattern can make a very subtle difference in output. That's what makes regex so powerful and so very difficult to get right. -- 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 From skgoyal721 at gmail.com Wed Jun 28 22:14:07 2017 From: skgoyal721 at gmail.com (shubham goyal) Date: Thu, 29 Jun 2017 07:44:07 +0530 Subject: [Tutor] Query regarding output Message-ID: Hello all, This Question is asked in some exam. i am not able to figure it out. a = [0, 1, 2, 3] for a[-1] in a: print(a[-1]) its giving output 0 1 2 2 it should be 3 3 3 3 as a[-1] belongs to 3. can anyone help me figuring it out. Thanks From alan.gauld at yahoo.co.uk Thu Jun 29 05:02:14 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 29 Jun 2017 10:02:14 +0100 Subject: [Tutor] Query regarding output In-Reply-To: References: Message-ID: On 29/06/17 03:14, shubham goyal wrote: > This Question is asked in some exam. i am not able to figure it out. > > a = [0, 1, 2, 3] > for a[-1] in a: > print(a[-1]) > > its giving output 0 1 2 2 > > it should be 3 3 3 3 as a[-1] belongs to 3. > can anyone help me figuring it out. This is quite subtle and it took me a few minutes to figure it out myself. It might be clearer if we print all of 'a' instead of a[-1]: >>> for a[-1] in a: ... print(a) ... [0, 1, 2, 0] [0, 1, 2, 1] [0, 1, 2, 2] [0, 1, 2, 2] What is happening is that a[-1] is being assigned the value of each item in a in turn. The final iteration assigns a[-1] to itself, thus we repeat the 2. Another way to see it is to convert the for loop to a while loop: for variable in collection: process(variable) becomes collection = [some sequence] index = 0 while index < len(collection): variable = collection[index] process(variable) index += 1 Now substitute your values: collection = [0,1,2,3] index = 0 while index < len(collection): collection[-1] = collection[index] print(collection[-1]) index += 1 Does that help? -- 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 From __peter__ at web.de Thu Jun 29 06:51:40 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 29 Jun 2017 12:51:40 +0200 Subject: [Tutor] Query regarding output References: Message-ID: Alan Gauld via Tutor wrote: > On 29/06/17 03:14, shubham goyal wrote: > >> This Question is asked in some exam. i am not able to figure it out. >> >> a = [0, 1, 2, 3] >> for a[-1] in a: >> print(a[-1]) >> >> its giving output 0 1 2 2 >> >> it should be 3 3 3 3 as a[-1] belongs to 3. >> can anyone help me figuring it out. > > This is quite subtle and it took me a few minutes to figure > it out myself. > > It might be clearer if we print all of 'a' instead > of a[-1]: > >>>> for a[-1] in a: > ... print(a) > ... > [0, 1, 2, 0] > [0, 1, 2, 1] > [0, 1, 2, 2] > [0, 1, 2, 2] > > What is happening is that a[-1] is being assigned the value > of each item in a in turn. The final iteration assigns a[-1] > to itself, thus we repeat the 2. > > Another way to see it is to convert the for loop to > a while loop: > > for variable in collection: > process(variable) > > becomes > > collection = [some sequence] > index = 0 > while index < len(collection): > variable = collection[index] > process(variable) > index += 1 > > Now substitute your values: > > collection = [0,1,2,3] > index = 0 > while index < len(collection): > collection[-1] = collection[index] > print(collection[-1]) > index += 1 > > Does that help? The simplest rewrite is probably >>> a = [0, 1, 2, 3] >>> for tmp in a: ... a[-1] = tmp ... print(tmp) ... 0 1 2 2 which should clarify how the list is modified while iterating over it. The takeaway is that for ... in [value]: pass is equivalent to ... = value There are a few other implicit ways to assign a value, with varying generality: >>> with open("tmp.txt") as a[0]: pass ... works, but >>> import os as a[0] File "", line 1 import os as a[0] ^ SyntaxError: invalid syntax and >>> try: 1/0 ... except Exception as a[0]: pass File "", line 2 except Exception as a[0]: pass ^ SyntaxError: invalid syntax are forbidden by the language. The only case -- other than standard name binding -- I find useful is unpacking in a for loop: >>> for hi, lo in ["Aa", "Zz"]: ... print(hi, "-", lo) ... A - a Z - z From anupama.2312.bmsit at gmail.com Thu Jun 29 06:35:45 2017 From: anupama.2312.bmsit at gmail.com (anupama srinivas murthy) Date: Thu, 29 Jun 2017 16:05:45 +0530 Subject: [Tutor] Query regarding output In-Reply-To: References: Message-ID: for a[x] in a: print(a) Except for when x = 0, a[x] never takes on its original value throughout the iterations So for x = 0, we get: [0, 1, 2, 3] [1, 1, 2, 3] [2, 1, 2, 3] [3, 1, 2, 3] For x = 1: [0, 0, 2, 3] [0, 0, 2, 3] [0, 2, 2, 3] [0, 3, 2, 3] For x = 2: [0, 1, 0, 3] [0, 1, 1, 3] [0, 1, 1, 3] [0, 1, 3, 3] How is the array being transformed? On 29 June 2017 at 14:32, Alan Gauld via Tutor wrote: > On 29/06/17 03:14, shubham goyal wrote: > > > This Question is asked in some exam. i am not able to figure it out. > > > > a = [0, 1, 2, 3] > > for a[-1] in a: > > print(a[-1]) > > > > its giving output 0 1 2 2 > > > > it should be 3 3 3 3 as a[-1] belongs to 3. > > can anyone help me figuring it out. > > This is quite subtle and it took me a few minutes to figure > it out myself. > > It might be clearer if we print all of 'a' instead > of a[-1]: > > >>> for a[-1] in a: > ... print(a) > ... > [0, 1, 2, 0] > [0, 1, 2, 1] > [0, 1, 2, 2] > [0, 1, 2, 2] > > What is happening is that a[-1] is being assigned the value > of each item in a in turn. The final iteration assigns a[-1] > to itself, thus we repeat the 2. > > Another way to see it is to convert the for loop to > a while loop: > > for variable in collection: > process(variable) > > becomes > > collection = [some sequence] > index = 0 > while index < len(collection): > variable = collection[index] > process(variable) > index += 1 > > Now substitute your values: > > collection = [0,1,2,3] > index = 0 > while index < len(collection): > collection[-1] = collection[index] > print(collection[-1]) > index += 1 > > Does that help? > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Thank you Anupama From mats at wichmann.us Thu Jun 29 08:24:49 2017 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 29 Jun 2017 06:24:49 -0600 Subject: [Tutor] Query regarding output In-Reply-To: References: Message-ID: <3bf08769-d8dd-7650-4a99-207183d49f4a@wichmann.us> On 06/29/2017 03:02 AM, Alan Gauld via Tutor wrote: > On 29/06/17 03:14, shubham goyal wrote: > >> This Question is asked in some exam. i am not able to figure it out. >> >> a = [0, 1, 2, 3] >> for a[-1] in a: >> print(a[-1]) >> >> its giving output 0 1 2 2 >> >> it should be 3 3 3 3 as a[-1] belongs to 3. >> can anyone help me figuring it out. > > This is quite subtle and it took me a few minutes to figure > it out myself. > > It might be clearer if we print all of 'a' instead > of a[-1]: > >>>> for a[-1] in a: > ... print(a) > ... > [0, 1, 2, 0] > [0, 1, 2, 1] > [0, 1, 2, 2] > [0, 1, 2, 2] > > What is happening is that a[-1] is being assigned the value > of each item in a in turn. The final iteration assigns a[-1] > to itself, thus we repeat the 2. Ugh. I guess on an exam where they're trying to see if you can tease it out, as you may one day have to debug such a sequence... but don't write code like that. One of the reasons people like "functional programming" is it avoids these kind of side effects. From skgoyal721 at gmail.com Thu Jun 29 09:55:38 2017 From: skgoyal721 at gmail.com (shubham goyal) Date: Thu, 29 Jun 2017 19:25:38 +0530 Subject: [Tutor] Query regarding output In-Reply-To: References: <3bf08769-d8dd-7650-4a99-207183d49f4a@wichmann.us> Message-ID: Thanks all Great place to learn Python. On Jun 29, 2017 7:24 PM, "shubham goyal" wrote: Thankyou all. Great place to learn. On Jun 29, 2017 5:55 PM, "Mats Wichmann" wrote: > On 06/29/2017 03:02 AM, Alan Gauld via Tutor wrote: > > On 29/06/17 03:14, shubham goyal wrote: > > > >> This Question is asked in some exam. i am not able to figure it out. > >> > >> a = [0, 1, 2, 3] > >> for a[-1] in a: > >> print(a[-1]) > >> > >> its giving output 0 1 2 2 > >> > >> it should be 3 3 3 3 as a[-1] belongs to 3. > >> can anyone help me figuring it out. > > > > This is quite subtle and it took me a few minutes to figure > > it out myself. > > > > It might be clearer if we print all of 'a' instead > > of a[-1]: > > > >>>> for a[-1] in a: > > ... print(a) > > ... > > [0, 1, 2, 0] > > [0, 1, 2, 1] > > [0, 1, 2, 2] > > [0, 1, 2, 2] > > > > What is happening is that a[-1] is being assigned the value > > of each item in a in turn. The final iteration assigns a[-1] > > to itself, thus we repeat the 2. > > > Ugh. I guess on an exam where they're trying to see if you can tease it > out, as you may one day have to debug such a sequence... but don't write > code like that. One of the reasons people like "functional programming" > is it avoids these kind of side effects. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From cs at zip.com.au Thu Jun 29 19:28:12 2017 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 30 Jun 2017 09:28:12 +1000 Subject: [Tutor] Desktop Notifications on Linux In-Reply-To: References: Message-ID: <20170629232812.GA62227@cskk.homeip.net> On 10Apr2014 17:04, Dharmit Shah wrote: >On Thu, Apr 10, 2014 at 2:30 PM, Peter Otten <__peter__ at web.de> wrote: >> Dharmit Shah wrote: >>> I am trying to create a script that will go through the >>> /var/log/secure file on a Linux system and provide desktop >>> notifications for failed login attempts. [...] >>> For readability purposes, I have provided pastebin links. Let me know >>> if this is not the correct way. We like the code inline. If it is big, maybe tack it onto the bottom of the message. That way people can (a) read your message when offline, as I sometimes do on a train (the email is already downloaded to my laptop) and (b) the full context is present in the mail archives so that things make sense when perusing them. Peter Otten said: >> Maybe you are running the code as a user that has no "desktop"? [...] You again: >That does ring some bells. I am logged into my F20 system as non-root >user but since reading /var/log/secure file requires superuser >privileges, I am running it as sudo: > > sudo python secure.py > >That probably explains the issue I am facing. I will add the user to >the root group and see if it helps. As a matter of good practice, try to run as little as possible as root. Minimum priviledge means minimum scope for mistakes to do damage. If the secure file has a group granting only read access, your group additional is a reasonable thing to do. Failing that, consider something like this: sudo tail -F /var/log/secure | python secure.py and have secure.py just read from standard input. This is that advantage that (a) the only root thing is the tail command, which just reads and (b) your program can produce alerts in real time as they come in from the tail, without having to write painful "tail"-like logic within the program. Cheers, Cameron Simpson