From ramanpreetbaidwan1436 at gmail.com Thu Oct 4 00:22:45 2018 From: ramanpreetbaidwan1436 at gmail.com (ramanpreet baidwan) Date: Wed, 3 Oct 2018 23:22:45 -0500 Subject: [Tutor] coding problem Message-ID: <5bb59594.1c69fb81.fef3a.e283@mx.google.com> Can anyone tell me how to code to display permutations in a table for all values of z=x^2+y by getting input from user for range of x and y? From rba2124 at gmail.com Wed Oct 3 23:20:35 2018 From: rba2124 at gmail.com (Roger B. Atkins) Date: Wed, 3 Oct 2018 20:20:35 -0700 Subject: [Tutor] Running programs that import 3rd party packages installed using pip. Message-ID: System: Windows 10, Anaconda, Python 3, Spyder3 Problem: Running programs that import requests, pyperclip, bs4 and/or other modules from 3rd party packages works fine within Spyder IDE, but not from command line, or Win/R. The error message indicates no such module. Therefore, my programs crash at the import statement. In contrast, programs using built in modules work fine everywhere. Questions: Which file needs to be found? Is this a path problem? Efforts to solve problem: After making sure I had correctly typed the module names, my second guess was that it is a path problem, so I used win explorer to locate the module files. I also went to the command line and used "pip show modname". The search using win explorer revealed module files in multiple directories, so part of the problem may be that I don't know which file the import statement needs to be able to find. Based on results using pip show modname, the modules were installed in: Anaconda3\lib\site-packages. In fact, as shown in Win Explorer, they are in sub folders under \site-packages, and there are 'regular' py files as well as compiled files. (I'm a beginner, and thought Python was interpreted?) I changed my system path variable to include: C:\Users\rba21\Anaconda3\lib\site-packages # Result: same error message C:\Users\rba21\Anaconda3\lib\site-packages\pyperclip # Result: same error message I tried adding the sub folders down to __pycache__ but got the same error message. I did some web searches, but didn't find anything that enabled me to fix the problem. From alan.gauld at yahoo.co.uk Thu Oct 4 03:44:44 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 Oct 2018 08:44:44 +0100 Subject: [Tutor] Running programs that import 3rd party packages installed using pip. In-Reply-To: References: Message-ID: On 04/10/18 04:20, Roger B. Atkins wrote: > System: Windows 10, Anaconda, Python 3, Spyder3 > > I changed my system path variable to include: > C:\Users\rba21\Anaconda3\lib\site-packages # Result: same error message When you say the "system path" do you mean the PYTHONPATH variable? It's PYTHONPATH not PATH that determines where Python looks for modules... Just a thought. -- 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 breamoreboy at gmail.com Thu Oct 4 03:54:39 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Thu, 4 Oct 2018 08:54:39 +0100 Subject: [Tutor] coding problem In-Reply-To: <5bb59594.1c69fb81.fef3a.e283@mx.google.com> References: <5bb59594.1c69fb81.fef3a.e283@mx.google.com> Message-ID: On 04/10/18 05:22, ramanpreet baidwan wrote: > Can anyone tell me how to code to display permutations in a table for all values of z=x^2+y by getting input from user for range of x and y? > Sorry but no as we do not do homework. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at yahoo.co.uk Thu Oct 4 04:09:13 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 Oct 2018 09:09:13 +0100 Subject: [Tutor] coding problem In-Reply-To: <5bb59594.1c69fb81.fef3a.e283@mx.google.com> References: <5bb59594.1c69fb81.fef3a.e283@mx.google.com> Message-ID: On 04/10/18 05:22, ramanpreet baidwan wrote: > Can anyone tell me how to code to display permutations in a table > for all values of z=x^2+y by getting input from user for range of x and y? There are several problems with your question. First it sounds suspiciously like homework and we won't do that for you. (Although we will give you some pointers) Second permutations sounds like the wrong word. I suspect you just want all the solutions for z displayed in a table against x,y pairs. Is that correct? Next, taken literally, the problem is impossible since x,y have an infinite number of values. So to have any chance you need to define the quantum steps between x and y - integers, 0.1, 0.01, etc. Otherwise you need to draw a graphical representation rather than use a table. Finally, its not clear what you are asking for help with. Do you not know how to present output in a table format? Do you not know how to calculate z? Do you not know how to prompt the user for inputs? Do you not know how to generate the x,y values from the users input? It's best if you can write some code then send us that along with the output (including full error message, if any). Then ask a specific question about what is going wrong and how you want us to 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 steve at pearwood.info Thu Oct 4 05:04:26 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 4 Oct 2018 19:04:26 +1000 Subject: [Tutor] coding problem In-Reply-To: <5bb59594.1c69fb81.fef3a.e283@mx.google.com> References: <5bb59594.1c69fb81.fef3a.e283@mx.google.com> Message-ID: <20181004090425.GQ21220@ando.pearwood.info> On Wed, Oct 03, 2018 at 11:22:45PM -0500, ramanpreet baidwan wrote: > Can anyone tell me how to code to display permutations in a table for > all values of z=x^2+y by getting input from user for range of x and y? Yes. Use a text editor to write your code in a text file. When you save the file, use .py as the file extension, not .txt. Do you know how to program? It sounds like you are doing homework. You should start by showing us the code you have already written. If you have written no code at all, tell us what you know how to do and ask *specific* questions. Terrible question: "Can somebody do my home work for me?" Bad question: "How do I write this code?" Good question: "How do I write a for loop?" Excellent question: "I tried to write a for-loop from 1 to 5, but it only went to 4. Here is my code. What did I do wrong?" If you ask excellent questions, we can give you excellent answers. Do you know how to use print? Do you know how to get input from the user? Can you write a for-loop over a range from x to y? -- Steve From mats at wichmann.us Thu Oct 4 09:51:52 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 4 Oct 2018 07:51:52 -0600 Subject: [Tutor] Running programs that import 3rd party packages installed using pip. In-Reply-To: References: Message-ID: On 10/03/2018 09:20 PM, Roger B. Atkins wrote: > System: Windows 10, Anaconda, Python 3, Spyder3 > > Problem: Running programs that import requests, pyperclip, bs4 and/or > other modules from 3rd party packages works fine within Spyder IDE, > but not from command line, or Win/R. The error message indicates no > such module. Therefore, my programs crash at the import statement. In > contrast, programs using built in modules work fine everywhere. > Questions: Which file needs to be found? Is this a path problem? > > Efforts to solve problem: After making sure I had correctly typed the > module names, my second guess was that it is a path problem, so I used > win explorer to locate the module files. I also went to the command > line and used "pip show modname". > The search using win explorer revealed module files in multiple > directories, so part of the problem may be that I don't know which > file the import statement needs to be able to find. > Based on results using pip show modname, the modules were installed in: > Anaconda3\lib\site-packages. > In fact, as shown in Win Explorer, they are in sub folders under > \site-packages, and there are 'regular' py files as well as compiled > files. (I'm a beginner, and thought Python was interpreted?) > > I changed my system path variable to include: > C:\Users\rba21\Anaconda3\lib\site-packages # Result: same error message > C:\Users\rba21\Anaconda3\lib\site-packages\pyperclip # Result: same > error message > I tried adding the sub folders down to __pycache__ but got the same > error message. > > I did some web searches, but didn't find anything that enabled me to > fix the problem. Write this simple program and run it from the "command line": import sys print(sys.path) those are the places Python will look for modules when importing. you can add to sys.path in your program, or if you define PYTHONPATH, its contents will show up in sys.path. if you now do the same thing in a program running inside Spyder, you'll probably see differences in sys.path. as Alan said, the system/user environment paths have nothing to do with Python's module importing. From rba2124 at gmail.com Thu Oct 4 09:56:14 2018 From: rba2124 at gmail.com (Roger B. Atkins) Date: Thu, 4 Oct 2018 06:56:14 -0700 Subject: [Tutor] Running programs that import 3rd party packages installed using pip. In-Reply-To: References: Message-ID: Thanks! That helps. I'll have to go back to the drawing board, but you've put me on the right "path". On Thu, Oct 4, 2018 at 12:47 AM Alan Gauld via Tutor wrote: > > On 04/10/18 04:20, Roger B. Atkins wrote: > > System: Windows 10, Anaconda, Python 3, Spyder3 > > > > > I changed my system path variable to include: > > C:\Users\rba21\Anaconda3\lib\site-packages # Result: same error message > > When you say the "system path" do you mean the PYTHONPATH > variable? It's PYTHONPATH not PATH that determines where > Python looks for modules... > > Just a thought. > > > -- > 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 rba2124 at gmail.com Thu Oct 4 10:15:01 2018 From: rba2124 at gmail.com (Roger B. Atkins) Date: Thu, 4 Oct 2018 07:15:01 -0700 Subject: [Tutor] Running programs that import 3rd party packages installed using pip. In-Reply-To: References: Message-ID: That's very helpful, thanks. After reading the Tutor information last night, I wrote a little program I named sysinfo. The code run line by line in a Spyder console yields: In [3]: sys.path Out[3]: ['', 'C:\\Users\\rba21\\Anaconda3\\python36.zip', 'C:\\Users\\rba21\\Anaconda3\\DLLs', 'C:\\Users\\rba21\\Anaconda3\\lib', 'C:\\Users\\rba21\\Anaconda3', 'C:\\Users\\rba21\\Anaconda3\\lib\\site-packages', 'C:\\Users\\rba21\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\rba21\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\rba21\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\rba21\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\rba21\\.ipython'] In [4]: sys.version Out[4]: '3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]' In [5]: sys.platform Out[5]: 'win32' I'll have to do more research to figure out which folders I need to add given that '\site-packages' is already included, but at least I won't be looking at the wrong path variable. On Thu, Oct 4, 2018 at 6:54 AM Mats Wichmann wrote: > > On 10/03/2018 09:20 PM, Roger B. Atkins wrote: > > System: Windows 10, Anaconda, Python 3, Spyder3 > > > > Problem: Running programs that import requests, pyperclip, bs4 and/or > > other modules from 3rd party packages works fine within Spyder IDE, > > but not from command line, or Win/R. The error message indicates no > > such module. Therefore, my programs crash at the import statement. In > > contrast, programs using built in modules work fine everywhere. > > Questions: Which file needs to be found? Is this a path problem? > > > > Efforts to solve problem: After making sure I had correctly typed the > > module names, my second guess was that it is a path problem, so I used > > win explorer to locate the module files. I also went to the command > > line and used "pip show modname". > > The search using win explorer revealed module files in multiple > > directories, so part of the problem may be that I don't know which > > file the import statement needs to be able to find. > > Based on results using pip show modname, the modules were installed in: > > Anaconda3\lib\site-packages. > > In fact, as shown in Win Explorer, they are in sub folders under > > \site-packages, and there are 'regular' py files as well as compiled > > files. (I'm a beginner, and thought Python was interpreted?) > > > > I changed my system path variable to include: > > C:\Users\rba21\Anaconda3\lib\site-packages # Result: same error message > > C:\Users\rba21\Anaconda3\lib\site-packages\pyperclip # Result: same > > error message > > I tried adding the sub folders down to __pycache__ but got the same > > error message. > > > > I did some web searches, but didn't find anything that enabled me to > > fix the problem. > > Write this simple program and run it from the "command line": > > import sys > print(sys.path) > > those are the places Python will look for modules when importing. you > can add to sys.path in your program, or if you define PYTHONPATH, its > contents will show up in sys.path. > > if you now do the same thing in a program running inside Spyder, you'll > probably see differences in sys.path. > > as Alan said, the system/user environment paths have nothing to do with > Python's module importing. > > > > > _______________________________________________ > 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 Oct 4 12:37:57 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 Oct 2018 17:37:57 +0100 Subject: [Tutor] Running programs that import 3rd party packages installed using pip. In-Reply-To: References: Message-ID: On 04/10/18 15:15, Roger B. Atkins wrote: > That's very helpful, thanks. After reading the Tutor information last > night, I wrote a little program I named sysinfo. The code run line by > line in a Spyder console yields: > > In [3]: sys.path > Out[3]: > ['', > 'C:\\Users\\rba21\\Anaconda3\\python36.zip', > 'C:\\Users\\rba21\\Anaconda3\\DLLs', > 'C:\\Users\\rba21\\Anaconda3\\lib', > 'C:\\Users\\rba21\\Anaconda3', > 'C:\\Users\\rba21\\Anaconda3\\lib\\site-packages', Note that the interesting bit is what it looks like outside of Spyder. We know it works OK within Spyder it's what gets defined outside Spyder that matters. -- 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 rls4jc at gmail.com Thu Oct 4 15:02:01 2018 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Thu, 4 Oct 2018 12:02:01 -0700 Subject: [Tutor] Numpy documentation Message-ID: I truly don't think I'm this stupid, but I can't even understand the fourth paragraph of the numpy documentation. https://docs.scipy.org/doc/numpy/user/quickstart.html says: In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3. [[ 1., 0., 0.], [ 0., 1., 2.]] (I think) I understand the 2 axes. [1,0,0] (I'm lazy and don't want to type the periods) is one axis and [0,1,2] is the second axis. But then things get goofy. The first axis has a length of 2. Is that because [1,0,0] and [0,1,2] are counted as one axis? (I think) I understand the second axis has a length of 3 because there are 3 elements within the [0,1,2] axis. Is that correct? But why does the first axis have a length of 2? Because the second zero doesn't count? Did they change the example and forgot to change the text? Thank you for your help as always. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation From alan.gauld at yahoo.co.uk Thu Oct 4 17:18:57 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 Oct 2018 22:18:57 +0100 Subject: [Tutor] Numpy documentation In-Reply-To: References: Message-ID: On 04/10/18 20:02, Roger Lea Scherer wrote: > In the example pictured below, the array has 2 axes. The first axis has a > length of 2, the second axis has a length of 3. > [[ 1., 0., 0.], > [ 0., 1., 2.]] > > (I think) I understand the 2 axes. [1,0,0] (I'm lazy and don't want to type > the periods) is one axis and [0,1,2] is the second axis. Nope. Its the other way round. The first axis is the "column" of two rows. Hence length 2. The second axis is the row with 3 elements in each. If you look at it as a table you can access the elements using x,y coordinates. The x coordinate (being first) denotes which row is indicated and the y axis being second denotes the element within the row. > ...But why does the first axis have a length of 2? Because there are two rows. -- 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 steve at pearwood.info Thu Oct 4 18:25:29 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 5 Oct 2018 08:25:29 +1000 Subject: [Tutor] Numpy documentation In-Reply-To: References: Message-ID: <20181004222529.GU21220@ando.pearwood.info> Hi Roger, My comments below, below yours. On Thu, Oct 04, 2018 at 12:02:01PM -0700, Roger Lea Scherer wrote: [...] > In the example pictured below, the array has 2 axes. The first axis has a > length of 2, the second axis has a length of 3. > [[ 1., 0., 0.], > [ 0., 1., 2.]] > > (I think) I understand the 2 axes. [1,0,0] (I'm lazy and don't want to type > the periods) is one axis and [0,1,2] is the second axis. You can copy and paste text to avoid extra typing. You seem to be are thinking along the lines of axles in a car, where the two axles run parallel: Axle one: --------- Axle two: --------- You should think of axes of a graph, which run perpendicular to each other, like the lines of a plus sign + or cross. So the array: [[ 1., 0., 0.], [ 0., 1., 2.]] is two dimensional, with 2 rows and 3 columns. In mathematics, we would call it a 2x3 matrix; in programming, it would be a 2x3 array. Axis one (rows): length 2 Axis two (columns): length 3 Regards, -- Steve From adameyring at gmail.com Fri Oct 5 11:50:40 2018 From: adameyring at gmail.com (Adam Eyring) Date: Fri, 5 Oct 2018 11:50:40 -0400 Subject: [Tutor] Pip issue Message-ID: Hi all, I just joined since I'm new to working in python for data management and have been stumped on using pip. My understanding is that it should be in my scripts folder of Windows Python3.6 (downloaded from python.org), but it's not, so it doesn't work to run "pip install ". I see there's get-pip.py, but the documentation on using it is confusing. Thanks for your help. Adam From wachobc at gmail.com Fri Oct 5 16:17:53 2018 From: wachobc at gmail.com (Chip Wachob) Date: Fri, 5 Oct 2018 16:17:53 -0400 Subject: [Tutor] Shifting arrays as though they are a 'word' Message-ID: Hello, I was not able to find any answers in the archive on this one. I'm wondering if this task can be done in a better way than what I've attempted.. I have an array of bytes. Up to 64, which makes for 512 bits. I am reading these bytes in serially, and once I have a collection of them, I want to shift them by 'n' bits. The size of the array and the number of bits are both variable up to the limit of 64/512. Now, I've played around with looping through the bytes and taking the LSByte and shifting it by 'n' bits using >> or << and that works for the first byte. But then I need to take the next byte in the sequence and shift it in the opposite direction by 8-n bits using << or >> (opposite the LSByte direction), respectively. Then I OR the two bytes and save them into the location of the LSByte and then move to the next byte in the sequence and so on. While this works most of the time, I sometimes get strange behavior at the 'fringes' of the bytes. Sometimes I end up with zero, or the shift seems to 'roll over'. I'm thinking that maybe there's a way to treat the array / list and shift allowing the bits to transfer from byte to byte as needed. Maybe by concatenating the bytes into one huge word and then breaking it apart again? I'm thinking that you folks out there know of a built-in function, or, an easier and more predictable way to accomplish the same. Thank you, From alan.gauld at yahoo.co.uk Fri Oct 5 18:27:44 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 5 Oct 2018 23:27:44 +0100 Subject: [Tutor] Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: On 05/10/18 21:17, Chip Wachob wrote: > > I have an array of bytes. Up to 64, which makes for 512 bits. > > I am reading these bytes in serially, and once I have a collection of > them, I want to shift them by 'n' bits. The size of the array and the > number of bits are both variable up to the limit of 64/512. bit trwiddling like this is always way more difficult than it sounds. You need to define very specifically how each operation will work, especially if you are shifting bytes in the middle of a larger sequence. For example, what happens to the bits that "fall off the end" - do they push the other surrounding bits up/down? Or do they get lost? Or are they combined in some way? Writing down the detailed specification for each operation usually takers longer than writing the code. In Python things are made more complex by the fact that most data containers are bigger than a byte and often have a lot of extra cruft attached. This makes it harder to know exactly what you are looking at. Often the best option is to use bit-masks to pull out exactly the bits you are interested in, otherwise carry bits and complement bits can confuse things. > Now, I've played around with looping through the bytes and taking the > LSByte and shifting it by 'n' bits using >> or << and that works for > the first byte. But then I need to take the next byte in the sequence > and shift it in the opposite direction by 8-n bits using << or >> > (opposite the LSByte direction), respectively. Then I OR the two > bytes and save them into the location of the LSByte I'd try to avoid that approach. Save the results into a new byte array if at all possible. Otherwise it becomes impossible to debug really quickly. > the next byte in the sequence and so on. While this works most of the > time, I sometimes get strange behavior at the 'fringes' of the bytes. Dfine "strange behaviour" - give a concrete example. > Sometimes I end up with zero, or the shift seems to 'roll over'. Define "roll over" - show us an example > I'm thinking that maybe there's a way to treat the array / list and > shift allowing the bits to transfer from byte to byte as needed. Can you show us what you want. For example here are three bytes: 0000000001111111100000000 Now, if you shift the middle byte to the left by 3 bits what should the end result be? a) 000001111111100000000000 Shift left, overwrite previous data, fill with zeros b) 000000001111100000000000 Shift left, preserve previous data, fill with zeros (and is that fill coming by dragging the right hand byte left and filling *it* with zeros or by preserving the right hand byte and filling the centre with zeros?) c) 000001111111111100000000 Shift left, overwrite previous data, fill with ones It's complicated and there are several other permutations beyond the 3 shown. You need to decide, there is no "correct" answer.. > Maybe by concatenating the bytes into one huge word and then breaking > it apart again? Maybe, but it all depends what you are trying to do. > I'm thinking that you folks out there know of a built-in function, or, > an easier and more predictable way to accomplish the same. Without a more detailed spec, no. Python is not really best suited to bitwise operations so it only has the most basic features: bitwise operators, multi base output/conversion and slicing. -- 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 cskk.id.au Fri Oct 5 18:51:48 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 6 Oct 2018 08:51:48 +1000 Subject: [Tutor] Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: <20181005225148.GA10286@cskk.homeip.net> On 05Oct2018 16:17, Chip Wachob wrote: >I have an array of bytes. Up to 64, which makes for 512 bits. > >I am reading these bytes in serially, and once I have a collection of >them, I want to shift them by 'n' bits. The size of the array and the >number of bits are both variable up to the limit of 64/512. > >Now, I've played around with looping through the bytes and taking the >LSByte and shifting it by 'n' bits using >> or << and that works for >the first byte. But then I need to take the next byte in the sequence >and shift it in the opposite direction by 8-n bits using << or >> >(opposite the LSByte direction), respectively. Shifting adjacent byte in opposite directions seems a little odd. Is that really what you need to do? >Then I OR the two >bytes and save them into the location of the LSByte and then move to >the next byte in the sequence and so on. While this works most of the >time, I sometimes get strange behavior at the 'fringes' of the bytes. >Sometimes I end up with zero, or the shift seems to 'roll over'. > >I'm thinking that maybe there's a way to treat the array / list and >shift allowing the bits to transfer from byte to byte as needed. >Maybe by concatenating the bytes into one huge word and then breaking >it apart again? Yes, not too hard. Python ints are bigints, so you can do operations on values of arbitrary size. So if you can convert your array of bytes into a single int, you can shift it: Python 3.6.4 (default, Dec 28 2017, 14:20:21) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 10000000000 <<17 1310720000000000 >>> Then convert back into bytes. The easy way is to just accrue the bytes into a value as you read them: n = 0 for b in the_bytes: n = n<<8 + b if you're reading "big endian" data (high ordinal bytes come first). So read them in, accruing the value into "n". Shift. Write them back out by decomposing "n" with successive division or successive shifts. Cheers, Cameron Simpson From steve at pearwood.info Sat Oct 6 02:42:58 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 6 Oct 2018 16:42:58 +1000 Subject: [Tutor] Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: <20181006064258.GX21220@ando.pearwood.info> On Fri, Oct 05, 2018 at 04:17:53PM -0400, Chip Wachob wrote: > Hello, > > I was not able to find any answers in the archive on this one. > > I'm wondering if this task can be done in a better way than what I've > attempted.. > > I have an array of bytes. Up to 64, which makes for 512 bits. > > I am reading these bytes in serially, and once I have a collection of > them, I want to shift them by 'n' bits. The size of the array and the > number of bits are both variable up to the limit of 64/512. Code speaks much more strongly than description. Can you demonstrate an example of: - the actual data you have; - the result you want; - what you've tried? The answers we give will be very different according to what you have, e.g.: data = [0, 1, 2, 3, 4, 5, 6, 7] # eight bytes in a list data = b"\0\x01\x02\x03\x04\x05\x06\x07" # eight bytes in a bytes object data = 0x0001020304050607 # eight bytes in an int -- Steve From __peter__ at web.de Sat Oct 6 04:10:10 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 06 Oct 2018 10:10:10 +0200 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' References: Message-ID: Chip Wachob wrote: > Hello, > > I was not able to find any answers in the archive on this one. > > I'm wondering if this task can be done in a better way than what I've > attempted.. > > I have an array of bytes. Up to 64, which makes for 512 bits. > > I am reading these bytes in serially, and once I have a collection of > them, I want to shift them by 'n' bits. The size of the array and the > number of bits are both variable up to the limit of 64/512. > > Now, I've played around with looping through the bytes and taking the > LSByte and shifting it by 'n' bits using >> or << and that works for > the first byte. But then I need to take the next byte in the sequence > and shift it in the opposite direction by 8-n bits using << or >> > (opposite the LSByte direction), respectively. Then I OR the two > bytes and save them into the location of the LSByte and then move to > the next byte in the sequence and so on. While this works most of the > time, I sometimes get strange behavior at the 'fringes' of the bytes. > Sometimes I end up with zero, or the shift seems to 'roll over'. > > I'm thinking that maybe there's a way to treat the array / list and > shift allowing the bits to transfer from byte to byte as needed. > Maybe by concatenating the bytes into one huge word and then breaking > it apart again? > > I'm thinking that you folks out there know of a built-in function, or, > an easier and more predictable way to accomplish the same. Here are two ways to implement the left shift: def bitshift(b, n, byteorder="big"): size = len(b) + (n + 7) // 8 shifted = int.from_bytes(b, byteorder) << n return shifted.to_bytes(size, byteorder) def bitshift2(b, n): nbytes, nbits = divmod(n, 8) if nbits: a = [0] for bb in b: hi, lo = divmod(bb << nbits, 256) a[-1] |= hi a.append(lo) b = bytes(a) return b + b"\x00" * nbytes assert bitshift(b"\xaa\xbb", 12) == b"\x0a\xab\xb0\x00" assert bitshift2(b"\xaa\xbb", 12) == b"\x0a\xab\xb0\x00" From ch.pascucci at gmail.com Sat Oct 6 04:53:10 2018 From: ch.pascucci at gmail.com (chiara pascucci) Date: Sat, 6 Oct 2018 09:53:10 +0100 Subject: [Tutor] guess my number game (reversed) In-Reply-To: References: Message-ID: Hi, sorry for "resurrecting" this thread. I have tried doing as suggested but with no luck. I now managed to make the programme work "partially". It works as desired for "too high" and "right" answer input, but when the users input "too low" the programme closes. here is what the code looks like right now. print ("Think of a number from 1 to 100, and I will try to guess it") input ("press enter when ready...") import random number = random.randint(1,100) print (number) answer = input ("right, too low, too high?") while answer != "right": if answer == "too high": number2 = random.randint (1,number) print (number2) elif answer == "too low": number3 = random.randit (number,100) print (number3) answer = input ("is this right, too high, or too low?") print ("I finally guessed your number") input ("\n\n press the enter key to exit") any help is really appreciated :) Chiara Il giorno ven 1 giu 2018 alle ore 18:15 Alan Gauld via Tutor < tutor at python.org> ha scritto: > On 01/06/18 14:00, chiara pascucci wrote: > > > the user's input. The programme works fine if the it guesses the number > > right on its first try, but when the users inputs "too low" or "too high" > > an infinite loop is returned. I thinkI have done something wrong in my > > while loop and that my sentry variable is somehow wrong > > Nope, it's simpler than that. > > You only ask the user for input once, outside the loop. > Move the input() statement inside the loop before the if > statements and all should be well. (You will also need to > initialise answer to some value - an empty string > say - before entering the while loop.) > > > print("think of a number from 1 to 100") > > print("I will try and guess it!") > > > > import random > > > > number = random.randint(1,100) > > print(number) > > > > answer = input("is this right, too high, or too low?") > > > > while answer != "right": > > if answer == "too high": > > number2 = random.randint(1,number) > > print(number2) > > elif answer == "too low": > > number3 = random.randint(number,100) > > print(number3) > > > > print("I finally guessed your number. Thank you for playing") > > HTH > -- > 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 Sat Oct 6 09:24:18 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 06 Oct 2018 15:24:18 +0200 Subject: [Tutor] guess my number game (reversed) References: Message-ID: chiara pascucci wrote: > Hi, > > sorry for "resurrecting" this thread. I have tried doing as suggested but > with no luck. > I now managed to make the programme work "partially". It works as desired > for "too high" and "right" answer input, but when the users input "too > low" the programme closes. > here is what the code looks like right now. > > print ("Think of a number from 1 to 100, and I will try to guess it") > input ("press enter when ready...") > > import random > > number = random.randint(1,100) > print (number) > > answer = input ("right, too low, too high?") > > while answer != "right": > if answer == "too high": > number2 = random.randint (1,number) > print (number2) > > elif answer == "too low": > number3 = random.randit (number,100) > print (number3) > > answer = input ("is this right, too high, or too low?") > > print ("I finally guessed your number") > > input ("\n\n press the enter key to exit") > > any help is really appreciated :) > > Chiara When you run your script from the command line it not just terminates, it also prints an error message and important debug information (called "traceback") that helps you fix the code: $ python3 guess_number_chiara.py Think of a number from 1 to 100, and I will try to guess it press enter when ready... 63 right, too low, too high?too low Traceback (most recent call last): File "guess_number_chiara.py", line 17, in number3 = random.randit (number,100) AttributeError: 'module' object has no attribute 'randit' The problem is in line 17 and there's a module that doesn't have a randit attribute. The only module mentioned in the line is random which indeed does not contain a randit() function, the actual function is named randint(). So the bug turns out to be a misspelt function name. From mats at wichmann.us Sat Oct 6 12:45:44 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 6 Oct 2018 10:45:44 -0600 Subject: [Tutor] Pip issue In-Reply-To: References: Message-ID: <190f202d-b4dc-0fcc-b7f9-a32c28ce90f6@wichmann.us> On 10/05/2018 09:50 AM, Adam Eyring wrote: > Hi all, > I just joined since I'm new to working in python for data management and > have been stumped on using pip. My understanding is that it should be in my > scripts folder of Windows Python3.6 (downloaded from python.org), but it's > not, so it doesn't work to run "pip install ". I see there's > get-pip.py, but the documentation on using it is confusing. Thanks for your > help. > Adam wherever you're told to pip install foo instead do python -m pip install foo it should take care of the path problems you're having, and is now the recommended way anyway since that way the things you install are sure to match the running python version. (the "python -m name" stanza means "python, please run 'name' as a module) From sydney.shall at kcl.ac.uk Mon Oct 8 06:15:21 2018 From: sydney.shall at kcl.ac.uk (Shall, Sydney) Date: Mon, 8 Oct 2018 10:15:21 +0000 Subject: [Tutor] Spyder - How to determine files that appear at start-up Message-ID: <128daa73-6f91-78d0-aa8f-03e0b7f083ec@kcl.ac.uk> My question concerns Spyder. I know that this is a Python list, but I also see that some people here also use Spyder. So, please forgive me. My problem is simple. I cannot find out how I can determine the set of files that open at start-up. I am annoyed by the fact that I must manually load my set of working files each time that I start Spyder. Thanks. Sydney _________ Professor Sydney Shall Department of Haematology/Oncology Phone: +(0)2078489200 E-Mail: sydney.shall [Correspondents outside the College should add @kcl.ac.uk] From wachobc at gmail.com Mon Oct 8 09:16:16 2018 From: wachobc at gmail.com (Chip Wachob) Date: Mon, 8 Oct 2018 09:16:16 -0400 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: All, Sorry for not being more clear. I think I was nearing my fill of Python for the day when I wrote. Now, refreshed, let me see if I can fill in the blanks a bit more. - Bits that are shifted in either direction off the end of the 64-byte(or 'n' size) collection are of no use. They are actually phantom bits created by the hardware that I'm trying to get rid of by using this shifting. And, the desired data needs to be properly aligned so I can work with it as well. - Any bits that are shifted 'in' from either end are zeros. - What follows is a mini version, the array could be any size up to 64 bytes input: 10010010 . 11101111 . 01010011 shift 'x' (for example, I'll use 1) output: 01001001 . 01110111 . 10101001 Some results that I've been getting appear as follows: - 'stuck bits' output: input : 01010011 . 01110001 . 11000011 output : 00101001 . 00111000 . 11100001 where the most significant bit of the word appears to 'stick' regardless of the shifting. This isn't the best example. - 'roll over' output" input : 00000001 . 11110000 . 10001000 output : 11111111 . 11111000 . 01000100 The most significant byte here has the 'roll over' issue. I'm guess this has something to do with sign extension of the byte in the larger Python storage medium. The above are not actual data that have been manipulated by the software but illustrations of observations. The real data is 'random' so I'm doing the best to recall from memory what happens. The data is being returned from the hardware as a bytearray but since I'm reading it a byte at a time, it ends up being stored as a list. I've been using for loops to cycle through the list. Ideally, I'd love to be able to say : # here's the array / list rx_data = [] # read through all the bytes # stash the results into the list for x in range (bytes_of_data): rx_data[x] = read_data()[0] # bit shift the entire lot rx_data = rx_data >> bits_to_shift. Which would provide me with the output described above. Code I've been working with is as follows: def bit_loop_report(disp, num_bits): if(disp): print "++Begin Report" num_bits = 0x00 num_bytes = 0x00 # define some constants to help in bit shifting rt_mask = array ('B', [0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F]) lt_mask = array ('B', [0xFF, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE]) # account for partial bytes worth of stops # determine number of bytes num_bytes = num_bits / 8 # if we have bits left over then save that count # between 1 and 7 inclusive if (num_bits > (num_bytes * 8)): num_bits = num_bits - (num_bytes * 8) print " num bits ", num_bits # if the number of bits left over is non-zero... if(num_bits != 0): # increment byte count to cover the additional bits num_bytes += 1 # calculate the actual number of extra bits rs_extra_bits = 8 - (num_bits % 8) print " extra bits ", rs_extra_bits if(disp): print "Loop size bytes = ", num_bytes # some vars we'll use bit_on = False disp_bit_on = "ON" disp_bit_off = "OFF" bit_state_human_read = "" print "\n\n Use Ctrl-C to Exit \n\n" # create an array to store stop states # last read last_read_ary = array('B', (0x00,)*num_bytes) r_last_read_ary = array('B', (0x00,)*num_bytes) # current read curr_read_ary = array('B', (0x00,)*num_bytes) r_curr_read_ary = array('B', (0x00,)*num_bytes) # hold the stop change results stop_chng_ary = array('B', (0x00,)*num_bytes) # if num_bits is non-zero, we have to shift # the data in the array accordingly done_shft_ary = array('B', (0x00,)*num_bytes) # clear everything out bit_loop_flush(disp) # clear the registers bit_loop_clear(disp) # latch the states bit_loop_strobe(disp) # read in the initial state of the loop # bytes are read in backwards because the # MSByte comes from the hardware first # (MSByte -> LSByte) and MSbit first. for first_read in range (0, num_bytes): # read a byte and stash into the array r_last_read_ary[first_read] = bit_loop_read(disp) # reverse the byte order last_read_ary = list(reversed(r_last_read_ary)) # now we enter a loop waiting for changes # to the data in the loop try: while (True): # zero out the variables read_stop = 0x00 temp_log = 0x00 changed = 0x00 # clear the registers bit_loop_clear(disp) # latch the states bit_loop_strobe(disp) # again, reading from hardware, backwards for byte_num in range (0, num_bytes): r_curr_read_ary[byte_num] = bit_loop_read(disp) # reverse the byte order curr_read_ary = list(reversed(r_curr_read_ary)) # go through the bytes and determine if there is a change for byte_num in range (0, num_bytes): stop_chng_ary[byte_num] = curr_read_ary[byte_num] ^ \ last_read_ary[byte_num] # create a flag to know if we've got a change # that we need to process if(stop_chng_ary[byte_num] != 0): changed += 1 if(changed != 0): if(disp): print " changed ", changed # here we are shifting the bits across the array element boundaries if(num_bits != 0): # loop will go through all bytes in the changed bytes array # remember that the 0th element is the MSByte and the nth # element is the LSByte for byte_num in range (0, (num_bytes - 1)): # shift done_shft_ary[byte_num] = stop_chng_ary[byte_num] >> rs_extra_bits if(disp): print byte_num, " done shift ", done_shft_ary # carry done_shft_ary[byte_num] |= (0xFF & (stop_chng_ary[byte_num + 1] << (8 - rs_extra_bits))) if(disp): print byte_num, " or'd ", done_shft_ary # MSByte just needs shift and no 'carry' done_shft_ary[num_bytes - 1] = stop_chng_ary[num_bytes - 1] >> rs_extra_bits # check through the shifted results for # bytes that indicate a change for num_mod in range (0, num_bytes): if(done_shft_ary[num_mod] != 0): if(disp): print "\n", done_shft_ary[num_mod] print " stop read = ", curr_read_ary print " stop change = ", stop_chng_ary print " done shift = ", done_shft_ary # determine if stop is on or off if(stop_chng_ary[num_mod] & curr_read_ary[num_mod]): bit_on = True bit_state_human_read = disp_bit_on else: bit_on = False bit_state_human_read = disp_bit_off # determine bit position if(stop_chng_ary[num_mod] <= 0): stop_change = 1 # log/log(2) is base translation temp_log = 1 + (math.log(done_shft_ary[num_mod])/math.log(2)) print "Position ", int(temp_log + (8 * num_mod)), \ "Stop State = ", bit_state_human_read if(disp): print " stop byte read = ", hex(curr_read_ary[num_mod]) print " num_mod = ", num_mod print " stop change = ", hex(stop_chng_ary[num_mod]) print " Stop change = ", ((num_mod * 8) + stop_chng_ary[num_mod]) print " Stop State = ", bit_state_human_read # update array with current state of stops last_read_ary = curr_read_ary[:] # copy ary to ary except KeyboardInterrupt: # watches for ctrl-c # provides a clean exit print "\n\n Test Complete \n\n" if(disp): print " -- Report Complete" return On 10/6/18, Peter Otten <__peter__ at web.de> wrote: > Chip Wachob wrote: > >> Hello, >> >> I was not able to find any answers in the archive on this one. >> >> I'm wondering if this task can be done in a better way than what I've >> attempted.. >> >> I have an array of bytes. Up to 64, which makes for 512 bits. >> >> I am reading these bytes in serially, and once I have a collection of >> them, I want to shift them by 'n' bits. The size of the array and the >> number of bits are both variable up to the limit of 64/512. >> >> Now, I've played around with looping through the bytes and taking the >> LSByte and shifting it by 'n' bits using >> or << and that works for >> the first byte. But then I need to take the next byte in the sequence >> and shift it in the opposite direction by 8-n bits using << or >> >> (opposite the LSByte direction), respectively. Then I OR the two >> bytes and save them into the location of the LSByte and then move to >> the next byte in the sequence and so on. While this works most of the >> time, I sometimes get strange behavior at the 'fringes' of the bytes. >> Sometimes I end up with zero, or the shift seems to 'roll over'. >> >> I'm thinking that maybe there's a way to treat the array / list and >> shift allowing the bits to transfer from byte to byte as needed. >> Maybe by concatenating the bytes into one huge word and then breaking >> it apart again? >> >> I'm thinking that you folks out there know of a built-in function, or, >> an easier and more predictable way to accomplish the same. > > Here are two ways to implement the left shift: > > def bitshift(b, n, byteorder="big"): > size = len(b) + (n + 7) // 8 > shifted = int.from_bytes(b, byteorder) << n > return shifted.to_bytes(size, byteorder) > > > def bitshift2(b, n): > nbytes, nbits = divmod(n, 8) > if nbits: > a = [0] > for bb in b: > hi, lo = divmod(bb << nbits, 256) > a[-1] |= hi > a.append(lo) > b = bytes(a) > return b + b"\x00" * nbytes > > > assert bitshift(b"\xaa\xbb", 12) == b"\x0a\xab\xb0\x00" > assert bitshift2(b"\xaa\xbb", 12) == b"\x0a\xab\xb0\x00" > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From oscar.j.benjamin at gmail.com Mon Oct 8 15:44:02 2018 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 8 Oct 2018 20:44:02 +0100 Subject: [Tutor] Spyder - How to determine files that appear at start-up In-Reply-To: <128daa73-6f91-78d0-aa8f-03e0b7f083ec@kcl.ac.uk> References: <128daa73-6f91-78d0-aa8f-03e0b7f083ec@kcl.ac.uk> Message-ID: On Mon, 8 Oct 2018 at 13:18, Shall, Sydney via Tutor wrote: > > My question concerns Spyder. I know that this is a Python list, but I > also see that some people here also use Spyder. So, please forgive me. > > My problem is simple. I cannot find out how I can determine the set of > files that open at start-up. I am annoyed by the fact that I must > manually load my set of working files each time that I start Spyder. Hi Sydney, It looks like this Stack Overflow question has some answers for you: https://stackoverflow.com/questions/14400993/spyder-default-module-import-list -- Oscar From steve at pearwood.info Mon Oct 8 20:04:35 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 9 Oct 2018 11:04:35 +1100 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: <20181009000435.GO3817@ando.pearwood.info> On Mon, Oct 08, 2018 at 09:16:16AM -0400, Chip Wachob wrote: > - What follows is a mini version, the array could be any size up to 64 bytes > > input: 10010010 . 11101111 . 01010011 > > shift 'x' (for example, I'll use 1) > > output: 01001001 . 01110111 . 10101001 The first two seem to be simple right bit-shift: 10010010 -> 01001001 11101111 -> 01110111 but the third seems to be something completely different, neither a left nor a right shift: 01010011 -> 10101001 A left-bit shift would give this: 10100110 and a right shift would give this: 00101001 So now I don't know what you want. [...] > The above are not actual data that have been manipulated by the > software but illustrations of observations. The real data is 'random' > so I'm doing the best to recall from memory what happens. Seriously, "this is what I vaguely remember happening" is no way to debug software. Show us *actual data* with *actual results* and maybe we can help, otherwise we're just pissing into the wind here. [...] > Ideally, I'd love to be able to say : > > # here's the array / list > rx_data = [] > > # read through all the bytes > # stash the results into the list > for x in range (bytes_of_data): > rx_data[x] = read_data()[0] > > # bit shift the entire lot > rx_data = rx_data >> bits_to_shift. > > Which would provide me with the output described above. Take your byte-array returned from read_data, the *lot* of it, not just the first byte. Convert to an int, and shift the int. py> b = bytearray(4) # work with four bytes py> b[1] = 255 # fill in some non-zero values py> b[2] = 127 py> b bytearray(b'\x00\xff\x7f\x00') py> n = int.from_bytes(b, 'big') py> hex(n) '0xff7f00' py> bin(n >> 1) '0b11111111011111110000000' py> bin(n << 1) '0b1111111101111111000000000' Does that help? -- Steve From mats at wichmann.us Mon Oct 8 20:28:18 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 8 Oct 2018 18:28:18 -0600 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: On 10/08/2018 07:16 AM, Chip Wachob wrote: > All, > > Sorry for not being more clear. I think I was nearing my fill of > Python for the day when I wrote. > > Now, refreshed, let me see if I can fill in the blanks a bit more. > > - Bits that are shifted in either direction off the end of the > 64-byte(or 'n' size) collection are of no use. They are actually > phantom bits created by the hardware that I'm trying to get rid of by > using this shifting. if you have stray bits, masking is usually a better solution than shifting. From __peter__ at web.de Tue Oct 9 03:27:15 2018 From: __peter__ at web.de (Peter Otten) Date: Tue, 09 Oct 2018 09:27:15 +0200 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' References: Message-ID: Chip Wachob wrote: > All, > > Sorry for not being more clear. I think I was nearing my fill of > Python for the day when I wrote. > > Now, refreshed, let me see if I can fill in the blanks a bit more. You have posted code sprinkled with prints and irrelevant details. Please rewrite the relevant part into a simple function, def bitshift_right(data, nshift): ... just the pure algorithm without any prints. Then find input data that produces an AssertionError in the snippet below: input = ... rshift = ... wanted_output = ... assert bitshift_right(input, rshift) == wanted_output I'm sure then someone is able and willing to point out the error in your implementation. In the mean time here's my effort which also provides an idea on how to wrap the shifting into a simple class: $ cat bitshift_oo.py format_bits = "{:08b}".format try: format_bits(42) except ValueError: # python 2 def format_bits(b): return bin(b)[2:].zfill(8) def to_bits(s, sep=""): return sep.join(map(format_bits, s)) def bitshift_right(b, n): nbytes, nbits = divmod(n, 8) length = len(b) if nbits: nbits = 8 - nbits a = [0] for bb in reversed(b): hi, lo = divmod(bb << nbits, 256) a[-1] |= lo a.append(hi) b = bytearray(a[::-1]) return (bytearray(nbytes) + b)[:length] class Bytes(bytearray): def __rshift__(self, n): return Bytes(bitshift_right(self, n)) def __str__(self): return to_bits(self, " ") if __name__ == "__main__": SAMPLE = Bytes(b"\xaa\xbb") for i in range(12): print(SAMPLE >> i) $ python bitshift_oo.py 10101010 10111011 01010101 01011101 00101010 10101110 00010101 01010111 00001010 10101011 00000101 01010101 00000010 10101010 00000001 01010101 00000000 10101010 00000000 01010101 00000000 00101010 00000000 00010101 $ PS: An easier approach would be to match the to_bits() function with a to_bytes() function. Then you can implement the shift as a simple string manipulation: def bitshift_right(b, n): bits = to_bits(b) shifted = ("0" * n + bits)[:len(bits)] return to_bytes(shifted) From wachobc at gmail.com Mon Oct 8 21:48:01 2018 From: wachobc at gmail.com (Chip Wachob) Date: Mon, 8 Oct 2018 21:48:01 -0400 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: <20181009000435.GO3817@ando.pearwood.info> References: <20181009000435.GO3817@ando.pearwood.info> Message-ID: First, Steven, Thank you for your insight. On 10/8/18, Steven D'Aprano wrote: > On Mon, Oct 08, 2018 at 09:16:16AM -0400, Chip Wachob wrote: > >> - What follows is a mini version, the array could be any size up to 64 >> bytes >> >> input: 10010010 . 11101111 . 01010011 >> >> shift 'x' (for example, I'll use 1) >> >> output: 01001001 . 01110111 . 10101001 > > The first two seem to be simple right bit-shift: > > 10010010 -> 01001001 > 11101111 -> 01110111 > > but the third seems to be something completely different, neither a left > nor a right shift: > > 01010011 -> 10101001 I _think_ that you got the input and output confused on the left-most byte. I checked to make sure that I had not made a typo, but I think that it should be 10101001 => 01010011 which is a right shift of one bit. > > A left-bit shift would give this: 10100110 > and a right shift would give this: 00101001 > > So now I don't know what you want. > > [...] >> The above are not actual data that have been manipulated by the >> software but illustrations of observations. The real data is 'random' >> so I'm doing the best to recall from memory what happens. > > Seriously, "this is what I vaguely remember happening" is no way to > debug software. Show us *actual data* with *actual results* and maybe we > can help, otherwise we're just pissing into the wind here. > I want to say also that I realize that my ethereal results were not very helpful. I'm having trouble actually capturing real data as it seems to be 'intermittent'. I fully expected the Brain Trust to come back with A) There's a much better way to do what you're doing, or B) Hey, there's a function for that, etc. I'm going to keep digging into my problem using these suggestions and get to the bottom of it. I'm sorry if you felt that I was wasting your time. > > [...] >> Ideally, I'd love to be able to say : >> >> # here's the array / list >> rx_data = [] >> >> # read through all the bytes >> # stash the results into the list >> for x in range (bytes_of_data): >> rx_data[x] = read_data()[0] >> >> # bit shift the entire lot >> rx_data = rx_data >> bits_to_shift. >> >> Which would provide me with the output described above. > > Take your byte-array returned from read_data, the *lot* of it, not just > the first byte. Convert to an int, and shift the int. I would be happy to take the 'lot' of it, except that the function returns only one byte at a time. It's a library / driver provided by the manufacturer. > > py> b = bytearray(4) # work with four bytes > py> b[1] = 255 # fill in some non-zero values > py> b[2] = 127 > py> b > bytearray(b'\x00\xff\x7f\x00') > py> n = int.from_bytes(b, 'big') I'm not familiar with this int.from_bytes. Being new to Python, I don't have a mental library of these nifty functions. How / where does one find a library of these wonderful functions so I can search to find something that might be really handy? Could I use the int.from_bytes on a 64-byte array, or would that end up breaking things? Then, I could shift the whole mess, but I would have to break it back apart to use the data. Which, I presume I could slice out of the larger 'word'. > py> hex(n) > '0xff7f00' > py> bin(n >> 1) > '0b11111111011111110000000' > py> bin(n << 1) > '0b1111111101111111000000000' > > Does that help? > It does help somewhat. > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From wachobc at gmail.com Mon Oct 8 21:55:41 2018 From: wachobc at gmail.com (Chip Wachob) Date: Mon, 8 Oct 2018 21:55:41 -0400 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: Mats, Thanks for the suggestion. Even if I did mask, I would still have to shift the data to line it up properly. And the shifting seems to be where I'm introducing my error(s). This bit stream is complicated in that I have to reverse the bytes as well since I'm reading off the 'end' of a bunch of registers. So, the MSByte comes to me first and goes into the [0] location and the extra bits get added to the '0th' end of the LSByte. It is a very unhandy hardware setup but one that I don't have any control over. All good insight. On 10/8/18, Mats Wichmann wrote: > On 10/08/2018 07:16 AM, Chip Wachob wrote: >> All, >> >> Sorry for not being more clear. I think I was nearing my fill of >> Python for the day when I wrote. >> >> Now, refreshed, let me see if I can fill in the blanks a bit more. >> >> - Bits that are shifted in either direction off the end of the >> 64-byte(or 'n' size) collection are of no use. They are actually >> phantom bits created by the hardware that I'm trying to get rid of by >> using this shifting. > > if you have stray bits, masking is usually a better solution than shifting. > _______________________________________________ > 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 Tue Oct 9 03:52:30 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 9 Oct 2018 08:52:30 +0100 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: <20181009000435.GO3817@ando.pearwood.info> Message-ID: On 09/10/18 02:48, Chip Wachob wrote: >> bytearray(b'\x00\xff\x7f\x00') >> py> n = int.from_bytes(b, 'big') > > I'm not familiar with this int.from_bytes. > > Being new to Python, I don't have a mental library of these nifty > functions. How / where does one find a library of these wonderful > functions so I can search to find something that might be really > handy? The interactive prompt. >>> dir(int) And the help() function >>> help(int) And finally the online documentation and search engine. But dir/help are usually faster. -- 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 Oct 9 04:13:01 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 9 Oct 2018 09:13:01 +0100 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: On 09/10/18 02:55, Chip Wachob wrote: > This bit stream is complicated in that I have to reverse the bytes as > well since I'm reading off the 'end' of a bunch of registers. So, the > MSByte comes to me first and goes into the [0] location and the extra > bits get added to the '0th' end of the LSByte. It is a very unhandy > hardware setup but one that I don't have any control over. I'm not sure I understand this fully. Do you mean that each byte has extra bits added or that only the last (LSByte) has extra bits? If it's the latter then it becomes a fairly trivial exercise of slicing the data off rather than shifting. But then you get left with a non-byte. If it's the former then I don't understand how you can determine "byte" boundaries! Also when you say you have to "reverse" the bytes do you mean the bits within each byte need to be reversed? Or merely that the bytes themselves come in the reverse order? If its the latter then the bytearray.reverse() method should suffice to fix that issue. Finally, just to clarify what's happening. It sounds like you have an API that delivers 8 bits(octets) at a time? (Not necessarily actual bytes, just groups of 8 bits, is that correct? And that potentially your actual data is not an even number of octets long (say 14 bits?) and thus you get a few random bits of cruft at the end. Is that the case? If so, how do you know the real length of the actual data? -- 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 wachobc at gmail.com Tue Oct 9 08:45:53 2018 From: wachobc at gmail.com (Chip Wachob) Date: Tue, 9 Oct 2018 08:45:53 -0400 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: <20181009000435.GO3817@ando.pearwood.info> Message-ID: Alan, Thank you. Another related question. How do I know I want to do dir(int) vs.. I don't know dir(long)? And, when I do dir(int) I get the following results. I must not be seeing the from_bytes in this list... >>> dir(int) ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real'] On 10/9/18, Alan Gauld via Tutor wrote: > On 09/10/18 02:48, Chip Wachob wrote: > >>> bytearray(b'\x00\xff\x7f\x00') >>> py> n = int.from_bytes(b, 'big') >> >> I'm not familiar with this int.from_bytes. >> >> Being new to Python, I don't have a mental library of these nifty >> functions. How / where does one find a library of these wonderful >> functions so I can search to find something that might be really >> handy? > > The interactive prompt. >>>> dir(int) > > And the help() function > >>>> help(int) > > And finally the online documentation and search engine. > But dir/help are usually faster. > > -- > 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 wachobc at gmail.com Tue Oct 9 09:21:31 2018 From: wachobc at gmail.com (Chip Wachob) Date: Tue, 9 Oct 2018 09:21:31 -0400 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: I'm sorry that my explanations are obtuse... Hopefully this will clarify things. Again, not knowing how much information is too much... I know I can be too verbose and that's not always a good thing. I have a bank of registers that I read via SPI. The SPI API is from the IC manufacturer. The maximum size of that bank is 512 bits. Or, 64 bytes. The register bank can be any valid integer between 1 and 512 long and changes each time the program is run. There is nothing that limits it to byte-sized counts. To determine the size of the bank, I can saturate the register with ones, followed by a stream of 0x00's. When I see the reads from the bank come back as something other than 0xFF, then I know I've 'pushed' zeros through the entire length of the registers. I keep a running count of bytes that contain 0xFF when I do this and if the last byte is something other than 0xFF or 0x00 then I add one to my byte count. This is the byte count for this register until the program is run again. The next time, the bit count is likely to be different. The input to the register array is at the least significant bit. The output is connected to the most significant bit. When I read a register, the SPI pumps out the MSByte first, which goes into my '0'th location in my list (array). (The data is read as MSbit first BTW, which is accurate given this configuration). When the read is complete, I have something that looks like this: data_in[0] = MSByte data_in[1] = MSByte -1 ... data_in[n-1] = LSByte -1 data_in[n] = LSByte* *The data_in[n] (LSByte) is the byte that can have extra '1's in it's least significant bit positions due to the pull-up on the data line.. Only the LSByte has the 'extra' bits. The other bytes are 'normal'. To handle the data in what I'll call a 'normal' order, I use the list.reverse() function that I discovered somewhere on the web. That puts the bytes in what I would call the 'proper' order. So then I have : data_in[0] = LSByte data_in[1] = LSByte - 1 data_in[2] = LSByte - 2 ... data_in[n-1] = MSByte -1 data_in[n] = MSByte I still have the extra bits that were added to the lower bits of data[0], I need to right shift to remove the 'added' ones. If this were only a byte sized register, I would do the shift and that would be sufficient but that is not likely. So, in the example of just one additional bit that needs removing: data_in[0] >>= 1 but, I need the least significant bit from data_in[1] to move into the most significant bit of the now-shifted data_in[0]. This of course has a domino affect down the line until I reach data_in[n]. Since I know the length of the register from my initialization process, I can ignore / mask the most significant bit of data_in[n] in my other calculations. My initial solution was to take data_in[1] and _left_ shift it by the amount of 8 - shift_count. scratch = data_in[n] <<= (8 - #) This moved the LSbit to the MSbit location of scratch. I then OR the data_in[0] shifted result and the scratch result to get my final data_in[0] value. data_in[0] = data_in[0] | scratch I really hope that this makes sense. The API is providing me with a bytearray-formatted result, but the API only reads one whole byte, or eight bits, at a time. So, I've been using only the [0] of the bytearray for my data. I hope that this answers your question about octets. I'm not entirely sure I follow the question well enough to answer. On 10/9/18, Alan Gauld via Tutor wrote: > On 09/10/18 02:55, Chip Wachob wrote: > >> This bit stream is complicated in that I have to reverse the bytes as >> well since I'm reading off the 'end' of a bunch of registers. So, the >> MSByte comes to me first and goes into the [0] location and the extra >> bits get added to the '0th' end of the LSByte. It is a very unhandy >> hardware setup but one that I don't have any control over. > > > I'm not sure I understand this fully. > Do you mean that each byte has extra > bits added or that only the last (LSByte) > has extra bits? > > If it's the latter then it becomes a fairly > trivial exercise of slicing the data off > rather than shifting. But then you get left > with a non-byte. > > If it's the former then I don't understand > how you can determine "byte" boundaries! > > Also when you say you have to "reverse" the > bytes do you mean the bits within each byte > need to be reversed? Or merely that the bytes > themselves come in the reverse order? If > its the latter then the bytearray.reverse() > method should suffice to fix that issue. > > Finally, just to clarify what's happening. > It sounds like you have an API that delivers > 8 bits(octets) at a time? (Not necessarily actual > bytes, just groups of 8 bits, is that correct? > And that potentially your actual data is not > an even number of octets long (say 14 bits?) > and thus you get a few random bits of cruft > at the end. > > Is that the case? If so, how do you know the > real length of the actual data? > > -- > 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 wachobc at gmail.com Tue Oct 9 09:38:35 2018 From: wachobc at gmail.com (Chip Wachob) Date: Tue, 9 Oct 2018 09:38:35 -0400 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: Peter, Thank you for taking the time to create the example. I'm new to Python, and, as a result, about a quarter of your example makes sense to me. The remainder I will have to start googling to see if I can get my head around it. On 10/9/18, Peter Otten <__peter__ at web.de> wrote: > Chip Wachob wrote: > >> All, >> >> Sorry for not being more clear. I think I was nearing my fill of >> Python for the day when I wrote. >> >> Now, refreshed, let me see if I can fill in the blanks a bit more. > > You have posted code sprinkled with prints and irrelevant details. > Please rewrite the relevant part into a simple function, > > def bitshift_right(data, nshift): > ... > > just the pure algorithm without any prints. Then find input data that > produces an AssertionError in the snippet below: > > input = ... > rshift = ... > wanted_output = ... > assert bitshift_right(input, rshift) == wanted_output > > I'm sure then someone is able and willing to point out the error in your > implementation. > > In the mean time here's my effort which also provides an idea on how to wrap > > the shifting into a simple class: > > $ cat bitshift_oo.py > format_bits = "{:08b}".format > try: > format_bits(42) > except ValueError: > # python 2 > def format_bits(b): > return bin(b)[2:].zfill(8) > > > def to_bits(s, sep=""): > return sep.join(map(format_bits, s)) > > > def bitshift_right(b, n): > nbytes, nbits = divmod(n, 8) > length = len(b) > if nbits: > nbits = 8 - nbits > a = [0] > for bb in reversed(b): > hi, lo = divmod(bb << nbits, 256) > a[-1] |= lo > a.append(hi) > b = bytearray(a[::-1]) > return (bytearray(nbytes) + b)[:length] > > > class Bytes(bytearray): > def __rshift__(self, n): > return Bytes(bitshift_right(self, n)) > > def __str__(self): > return to_bits(self, " ") > > > if __name__ == "__main__": > SAMPLE = Bytes(b"\xaa\xbb") > for i in range(12): > print(SAMPLE >> i) > $ python bitshift_oo.py > 10101010 10111011 > 01010101 01011101 > 00101010 10101110 > 00010101 01010111 > 00001010 10101011 > 00000101 01010101 > 00000010 10101010 > 00000001 01010101 > 00000000 10101010 > 00000000 01010101 > 00000000 00101010 > 00000000 00010101 > $ > > PS: An easier approach would be to match the to_bits() function with a > to_bytes() function. Then you can implement the shift as a simple string > manipulation: > > def bitshift_right(b, n): > bits = to_bits(b) > shifted = ("0" * n + bits)[:len(bits)] > return to_bytes(shifted) > > > _______________________________________________ > 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 Tue Oct 9 12:10:24 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 9 Oct 2018 17:10:24 +0100 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: <20181009000435.GO3817@ando.pearwood.info> Message-ID: On 09/10/18 13:45, Chip Wachob wrote: > Another related question. How do I know I want to do dir(int) vs.. I > don't know dir(long)? Because you want to convert the byte array into an int. And there is no long in Python... > And, when I do dir(int) I get the following results. I must not be > seeing the from_bytes in this list... > >>>> dir(int) > ['__abs__', '__add__', '__and__', '__class__', '__cmp__', Ah, I assume this means you are using Python v2.7? I assumed you were using v3. The bytes operations were, I think, added in v3. That complicates things, I think you are probably best converting your byte list into a string and then using the struct module to convert your bytearray into an int. (struct is a bit like C's sscanf function if that helps - I seem to recall you know C?) -- 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 Oct 9 12:18:32 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 9 Oct 2018 17:18:32 +0100 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: Message-ID: On 09/10/18 14:21, Chip Wachob wrote: > The API is providing me with a bytearray-formatted result, but the API > only reads one whole byte, or eight bits, at a time. Being slightly picky but an octet and a byte are subtly different. An octet is a group of 8 bits with no meaning attached. So an octet contains 8 bits which could be individual single bit flags, or they could be two groups of 4(nibbles) or a single group of 8(a byte). A byte is a more specific thing that only exists as a set of 8 bits joined together as a single unit of data. Most of the time it makes no significant difference so people often use the terms interchangeably but in this kind of scenario the difference is significant. You are reading octets and want to convert them into a bytearray - removing the extraneous bits in the process. The way I'd do that is convert to an int and then do a single shift right on the result. -- 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 breamoreboy at gmail.com Tue Oct 9 12:32:50 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Tue, 9 Oct 2018 17:32:50 +0100 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: <20181009000435.GO3817@ando.pearwood.info> Message-ID: On 09/10/18 17:10, Alan Gauld via Tutor wrote: > On 09/10/18 13:45, Chip Wachob wrote: > >> Another related question. How do I know I want to do dir(int) vs.. I >> don't know dir(long)? > > Because you want to convert the byte array into an int. > And there is no long in Python... > Really? :) mark at mark-HP-15-Notebook-PC:~$ python Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> dir(long) ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real'] -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at yahoo.co.uk Tue Oct 9 14:13:07 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 9 Oct 2018 19:13:07 +0100 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: <20181009000435.GO3817@ando.pearwood.info> Message-ID: On 09/10/18 17:32, Mark Lawrence wrote: > On 09/10/18 17:10, Alan Gauld via Tutor wrote: >> On 09/10/18 13:45, Chip Wachob wrote: >> >>> Another related question. How do I know I want to do dir(int) vs.. I >>> don't know dir(long)? >> >> Because you want to convert the byte array into an int. >> And there is no long in Python... >> > > Really? :) Quite. I should, of course, have said there is no long in Python v3... We have since established that Chip is using 2.7 so there is indeed a long type. I pretty much stopped using v2 about 3 years ago so I tend to forget about these details. -- 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 Tue Oct 9 14:57:28 2018 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 9 Oct 2018 12:57:28 -0600 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: <20181009000435.GO3817@ando.pearwood.info> Message-ID: <85c091c2-4293-f5a9-abc6-065f73e9e94f@wichmann.us> On 10/09/2018 12:13 PM, Alan Gauld via Tutor wrote: > Quite. > I should, of course, have said there is no long in Python v3... > > We have since established that Chip is using 2.7 so there > is indeed a long type. > > I pretty much stopped using v2 about 3 years ago so I > tend to forget about these details. > https://pythonclock.org/ tick tick From mariamhaji01 at gmail.com Tue Oct 9 15:09:17 2018 From: mariamhaji01 at gmail.com (Mariam Haji) Date: Tue, 9 Oct 2018 22:09:17 +0300 Subject: [Tutor] Advanced python recommendations Message-ID: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Hi guys, I am on the last exercises of learn python the hard by Zed.A Shaw and I am looking for recommendations on what to follow next or what book to try next to advance my python skills to intermediate level. Thank you in advance. Regards, Mariam From wachobc at gmail.com Tue Oct 9 12:30:20 2018 From: wachobc at gmail.com (Chip Wachob) Date: Tue, 9 Oct 2018 12:30:20 -0400 Subject: [Tutor] [spoiler] Re: Shifting arrays as though they are a 'word' In-Reply-To: References: <20181009000435.GO3817@ando.pearwood.info> Message-ID: Alan, Yes, 2.7 here. I wanted to use 3, but the API only supports 2.7. And, I'm not about to get into rewriting an API. :) So, octets it is. Thank you for the clarification. Yes, my background is C, let me look into your suggested method. It is the shifting across those byte boundaries that is really messing me up. If only they were all just bytes.. but then that wouldn't be any 'fun' would it? Thank you, On 10/9/18, Alan Gauld via Tutor wrote: > On 09/10/18 13:45, Chip Wachob wrote: > >> Another related question. How do I know I want to do dir(int) vs.. I >> don't know dir(long)? > > Because you want to convert the byte array into an int. > And there is no long in Python... > >> And, when I do dir(int) I get the following results. I must not be >> seeing the from_bytes in this list... >> >>>>> dir(int) >> ['__abs__', '__add__', '__and__', '__class__', '__cmp__', > > Ah, I assume this means you are using Python v2.7? > I assumed you were using v3. The bytes operations were, > I think, added in v3. > > That complicates things, I think you are probably best > converting your byte list into a string and then using > the struct module to convert your bytearray into an int. > (struct is a bit like C's sscanf function if > that helps - I seem to recall you know C?) > > -- > 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 alan.gauld at yahoo.co.uk Tue Oct 9 20:06:05 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 10 Oct 2018 01:06:05 +0100 Subject: [Tutor] Advanced python recommendations In-Reply-To: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Message-ID: On 09/10/18 20:09, Mariam Haji wrote: > Hi guys, I am on the last exercises of learn python the hard> by Zed.A Shaw and I am looking for recommendations on what > to follow next or what book to try next The main thing is to write a lot of code, so think of a project and build it. The experience of designing code and writing it, testing it and debugging it is worth far more than reading another book. If you get stuck ask here. Include any error messages you may get. Once you have written a couple of useful programs and maybe identified a specific area of interest (Web, games, Desktop, OS, etc) look for a book that covers those areas. If all else fails you could try Python Projects by yours truly & Laura Cassell. It covers the basics of OS, databases, GUIs and web... :-) -- 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 Tue Oct 9 20:24:49 2018 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 9 Oct 2018 18:24:49 -0600 Subject: [Tutor] Advanced python recommendations In-Reply-To: References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Message-ID: On 10/09/2018 06:06 PM, Alan Gauld via Tutor wrote: > On 09/10/18 20:09, Mariam Haji wrote: >> Hi guys, I am on the last exercises of learn python the hard> by Zed.A Shaw and I am looking for recommendations on what >> to follow next or what book to try next > > The main thing is to write a lot of code, so think of a > project and build it. The experience of designing code > and writing it, testing it and debugging it is worth > far more than reading another book. > > If you get stuck ask here. Include any error messages > you may get. > > Once you have written a couple of useful programs > and maybe identified a specific area of interest > (Web, games, Desktop, OS, etc) look for a book that > covers those areas. > > > If all else fails you could try Python Projects by > yours truly & Laura Cassell. It covers the basics > of OS, databases, GUIs and web... :-) > > +1 "Write a lot of code" Once you're doing that, there are plenty of resources for unraveling what you run into, including here :) There's a kind of cool video series which unfortunately has restricted availability - it's called Code Clinic, on lynda.com. https://www.lynda.com/SharedPlaylist/3bd14e75-f001-4f05-a34c-169289d7a29a the concept here is someone has thought of a series of achievable projects you can work on, and they've done one for each of a whole bunch of programming languages, Python definitely included. Sometimes "write a lot of code" sounds great, but what am I going to write? It's a subscription service that costs, and also is included if you have a LinkedIn premium membership, but you can get a month's free trial, so it just might be worth a look in. If you know another language well, and it's included, it might be an interesting comparison to see how an expert solves the problem in that language vs. the Python approach (after you've tried your own, of course). From robertvstepp at gmail.com Tue Oct 9 21:22:56 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 9 Oct 2018 20:22:56 -0500 Subject: [Tutor] Advanced python recommendations In-Reply-To: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Message-ID: On Tue, Oct 9, 2018 at 6:54 PM Mariam Haji wrote: > > Hi guys, I am on the last exercises of learn python the hard by Zed.A Shaw and I am looking for recommendations on what to follow next or what book to try next to advance my python skills to intermediate level. If you are a fan of Zed Shaw's approach, I noticed while at Barnes & Noble a while back that he has released a sequel to the book you cited, but only for the Python 3 version. You may be interested in that. But I imagine taking time to imagine, detail and write the code for projects would help you the most, as the others have said. -- boB From mikeschmitt128 at outlook.com Tue Oct 9 23:20:37 2018 From: mikeschmitt128 at outlook.com (Michael Schmitt) Date: Wed, 10 Oct 2018 03:20:37 +0000 Subject: [Tutor] help please Message-ID: To whom it may concern: I am trying to teach myself Python and ran into a problem. This is my code # name of rivers and country rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' } # prints river name for rivers in rivers.keys(): print (rivers) #prints country for rivers in rivers.values(): print (rivers) # prints statement " The (river) is in the country of (country) for rivers in rivers: print ("The " + rivers.keys() + "is in the country of " + rivers.vaules()) I am getting the following error for rivers in rivers.values(): AttributeError: 'str' object has no attribute 'values' Thanks for the help. Sincerely, Michael S. Schmitt [https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif] Virus-free. www.avast.com From __peter__ at web.de Wed Oct 10 04:44:21 2018 From: __peter__ at web.de (Peter Otten) Date: Wed, 10 Oct 2018 10:44:21 +0200 Subject: [Tutor] help please References: Message-ID: Michael Schmitt wrote: > To whom it may concern: > > > I am trying to teach myself Python and ran into a problem. This is my code > I am getting the following error > for rivers in rivers.values(): > AttributeError: 'str' object has no attribute 'values' > # prints river name > for rivers in rivers.keys(): > print (rivers) Look closely at the loop above. What is the value of the "rivers" variable after the first iteration? To resolve the problem simply use a different name as the loop variable. Pro tip: When you loop over a dict you get the keys by default. So: for river in rivers: print(river) > # prints statement " The (river) is in the country of (country) > for rivers in rivers: > print ("The " + rivers.keys() + "is in the country of " + rivers.vaules()) > Here's a logic (and a spelling) error: rivers.keys() comprises all rivers and rivers.values() all countries in the dict. You want to loop over rivers.items() which gives you (river, country) pairs. Tip: print() automatically inserts spaces between its arguments. So: for river, country in rivers.items(): print("River", river, "is in", country) From breamoreboy at gmail.com Wed Oct 10 05:41:48 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Wed, 10 Oct 2018 10:41:48 +0100 Subject: [Tutor] Advanced python recommendations In-Reply-To: References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Message-ID: On 10/10/18 02:22, boB Stepp wrote: > On Tue, Oct 9, 2018 at 6:54 PM Mariam Haji wrote: >> >> Hi guys, I am on the last exercises of learn python the hard by Zed.A Shaw and I am looking for recommendations on what to follow next or what book to try next to advance my python skills to intermediate level. > > If you are a fan of Zed Shaw's approach, I noticed while at Barnes & > Noble a while back that he has released a sequel to the book you > cited, but only for the Python 3 version. You may be interested in > that. > > But I imagine taking time to imagine, detail and write the code for > projects would help you the most, as the others have said. > > After the disgraceful way that Zed Shaw wrote about Python 3 https://learnpythonthehardway.org/book/nopython3.html I wouldn't touch his stuff with a 100 foot long disinfected barge pole. Just a few months after this article he came out with the Python 3 book you reference above, presumably because he was losing cash. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From arj.python at gmail.com Wed Oct 10 03:53:41 2018 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Wed, 10 Oct 2018 11:53:41 +0400 Subject: [Tutor] help please In-Reply-To: References: Message-ID: i think it should have been for river in rivers instead of for rivers in rivers Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ Mauritius From arj.python at gmail.com Wed Oct 10 06:26:24 2018 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Wed, 10 Oct 2018 14:26:24 +0400 Subject: [Tutor] Advanced python recommendations In-Reply-To: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Message-ID: apart from already said, see popular python projects, and read the source as you would read a book. you'll discover amazing tricks, it'll broaden your horizon. hanging around those who achieved a good level will make you level up. Abdur-Rahmaan Janhangeer Mauritius From cmgcomsol at gmail.com Wed Oct 10 04:15:40 2018 From: cmgcomsol at gmail.com (Mirage Web Studio) Date: Wed, 10 Oct 2018 13:45:40 +0530 Subject: [Tutor] help please In-Reply-To: References: Message-ID: You are using the same variable name twice. You may use "rivers" for the dict and "river" for values. Also use descriptive names for variables. For eg if you correct the above mistake, the next one will be this line for rivers in rivers.values(): print (rivers) and sorry for top positing. On Wed 10 Oct, 2018, 12:38 Michael Schmitt, wrote: > To whom it may concern: > > > I am trying to teach myself Python and ran into a problem. This is my code > > > # name of rivers and country > > rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' } > > # prints river name > for rivers in rivers.keys(): > print (rivers) > > #prints country > for rivers in rivers.values(): > print (rivers) > > # prints statement " The (river) is in the country of (country) > for rivers in rivers: > print ("The " + rivers.keys() + "is in the country of " + > rivers.vaules()) > > I am getting the following error > for rivers in rivers.values(): > AttributeError: 'str' object has no attribute 'values' > > Thanks for the help. > > Sincerely, > > Michael S. Schmitt > > > [ > https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif > ]< > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon> > Virus-free. www.avast.com< > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From deepakdixit0001 at gmail.com Wed Oct 10 03:13:46 2018 From: deepakdixit0001 at gmail.com (Deepak Dixit) Date: Wed, 10 Oct 2018 12:43:46 +0530 Subject: [Tutor] help please In-Reply-To: References: Message-ID: On Wed, Oct 10, 2018, 12:37 PM Michael Schmitt wrote: > To whom it may concern: > > > I am trying to teach myself Python and ran into a problem. This is my code > > > # name of rivers and country > > rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' } > > # prints river name > for rivers in rivers.keys(): > print (rivers) > > #prints country > for rivers in rivers.values(): > print (rivers) > > # prints statement " The (river) is in the country of (country) > Why are you using "for rivers in rivers". Replace this for loop with :- for river in rivers: print ("The " + river + "is in the country of " + rivers.get(river)) for rivers in rivers: > print ("The " + rivers.keys() + "is in the country of " + > rivers.vaules()) > > I am getting the following error > for rivers in rivers.values(): > AttributeError: 'str' object has no attribute 'values' > > Thanks for the help. > > Sincerely, > > Michael S. Schmitt > > > [ > https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif > ]< > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon> > Virus-free. www.avast.com< > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mariamhaji01 at gmail.com Wed Oct 10 05:53:59 2018 From: mariamhaji01 at gmail.com (Mariam Haji) Date: Wed, 10 Oct 2018 12:53:59 +0300 Subject: [Tutor] Advanced python recommendations In-Reply-To: References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Message-ID: Thank you all for the above tips. I actually did python 2 a friend sent it to me. And my current challenge with the projects bit is how to pseudo-code and which approach to use as I am not very familiar with the entire python syntax and how I can use it and as well as python algorithms. So like I get a problem to create a simple loop or remove duplicates from a list and that just doesn't process in my head and I always end up having to google, then build from there. But thank you, I guess i just have to be patient it's been 3 months since I started coding and development I am more of a front-end developer. I guess I just need to learn how to break down the problems and know what to use where. On Wed, Oct 10, 2018 at 12:44 PM Mark Lawrence wrote: > On 10/10/18 02:22, boB Stepp wrote: > > On Tue, Oct 9, 2018 at 6:54 PM Mariam Haji > wrote: > >> > >> Hi guys, I am on the last exercises of learn python the hard by Zed.A > Shaw and I am looking for recommendations on what to follow next or what > book to try next to advance my python skills to intermediate level. > > > > If you are a fan of Zed Shaw's approach, I noticed while at Barnes & > > Noble a while back that he has released a sequel to the book you > > cited, but only for the Python 3 version. You may be interested in > > that. > > > > But I imagine taking time to imagine, detail and write the code for > > projects would help you the most, as the others have said. > > > > > > After the disgraceful way that Zed Shaw wrote about Python 3 > https://learnpythonthehardway.org/book/nopython3.html I wouldn't touch > his stuff with a 100 foot long disinfected barge pole. Just a few > months after this article he came out with the Python 3 book you > reference above, presumably because he was losing cash. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- *Regards,* *Mariam.* From alan.gauld at yahoo.co.uk Wed Oct 10 12:46:03 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 10 Oct 2018 17:46:03 +0100 Subject: [Tutor] Advanced python recommendations In-Reply-To: References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Message-ID: On 10/10/18 10:53, Mariam Haji wrote: >...my current challenge > with the projects bit is how to pseudo-code and which approach to use as I > am not very familiar with the entire python syntax and how I can use it And that's the whole point of doing projects. You need to really be comfortable with the basics of breaking a problem down into code sized chunks and debugging basic syntax before you start thinking about mo9re advanced stuff. Think of a regular task you could automate, or invent a game, whatever takes your fancy. Then work on it and as you get stuck send your code here and we will try to help you fix it. Remember keep it simple to start with. Even just copying the files in a work folder to a backup folder, or a little music or photo catalogue tool. And start small, get the basics working before adding extra features - test each feature as you add it, don't wait till you have a mountain of code to debug. -- 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 Wed Oct 10 13:08:15 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 10 Oct 2018 11:08:15 -0600 Subject: [Tutor] Advanced python recommendations In-Reply-To: References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> Message-ID: <44eeccec-af5d-8a1f-9f66-bc7640c75d2c@wichmann.us> On 10/10/2018 10:46 AM, Alan Gauld via Tutor wrote: > On 10/10/18 10:53, Mariam Haji wrote: >> ...my current challenge >> with the projects bit is how to pseudo-code and which approach to use as I >> am not very familiar with the entire python syntax and how I can use it > > And that's the whole point of doing projects. You need to > really be comfortable with the basics of breaking a problem > down into code sized chunks and debugging basic syntax > before you start thinking about mo9re advanced stuff. > > Think of a regular task you could automate, or invent > a game, whatever takes your fancy. Then work on it and > as you get stuck send your code here and we will try > to help you fix it. > > Remember keep it simple to start with. Even just copying > the files in a work folder to a backup folder, or a > little music or photo catalogue tool. > > And start small, get the basics working before adding > extra features - test each feature as you add it, don't > wait till you have a mountain of code to debug. > This is actually the concept of test driven development (TDD), which I'm not a huge proponent of personally, but kind of useful for this: - write a test for some behavior you need, and get the test "working" so that it fails because you don't have the code for the behavior yet - code up the behavior until the test passes - rinse, repeat it really forces you not to write things in too complex a way up front! From robertvstepp at gmail.com Wed Oct 10 15:20:03 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 10 Oct 2018 14:20:03 -0500 Subject: [Tutor] Advanced python recommendations In-Reply-To: <44eeccec-af5d-8a1f-9f66-bc7640c75d2c@wichmann.us> References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> <44eeccec-af5d-8a1f-9f66-bc7640c75d2c@wichmann.us> Message-ID: On Wed, Oct 10, 2018 at 12:09 PM Mats Wichmann wrote: > This is actually the concept of test driven development (TDD), which I'm > not a huge proponent of personally, but kind of useful for this: I'm curious: What are the things you find less than satisfactory for TDD? -- boB From mats at wichmann.us Wed Oct 10 15:54:35 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 10 Oct 2018 13:54:35 -0600 Subject: [Tutor] Advanced python recommendations In-Reply-To: References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> <44eeccec-af5d-8a1f-9f66-bc7640c75d2c@wichmann.us> Message-ID: <167ae6bc-7b74-c9a8-14c4-3d6ebda1049c@wichmann.us> On 10/10/2018 01:20 PM, boB Stepp wrote: > On Wed, Oct 10, 2018 at 12:09 PM Mats Wichmann wrote: > >> This is actually the concept of test driven development (TDD), which I'm >> not a huge proponent of personally, but kind of useful for this: > > I'm curious: What are the things you find less than satisfactory for TDD? Probably that I'm an old fogey who learned programming a different way :) And, it's off topic, so I'll just quickly say: "TDD as religion" seems to me to lead to an absence of thinking about overall system design, and system testing. An excess focus on the unit, because that's what you get when you build from unit tests on up, doesn't necessarily get you a good System, and the vast majority of your users will interact with your software at the system level.... I'm fine with people using it as a general approach, just not as unbreakable dogma - the same way I feel about most software development methodology fads, actually. From alan.gauld at yahoo.co.uk Thu Oct 11 11:37:15 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Oct 2018 16:37:15 +0100 Subject: [Tutor] Advanced python recommendations In-Reply-To: <167ae6bc-7b74-c9a8-14c4-3d6ebda1049c@wichmann.us> References: <0E685046-9EA4-46E8-A964-432CA107EE93@gmail.com> <44eeccec-af5d-8a1f-9f66-bc7640c75d2c@wichmann.us> <167ae6bc-7b74-c9a8-14c4-3d6ebda1049c@wichmann.us> Message-ID: On 10/10/18 20:54, Mats Wichmann wrote: >>> This is actually the concept of test driven development (TDD), which I'm >>> not a huge proponent of personally, but kind of useful for this: >> >> I'm curious: What are the things you find less than satisfactory for TDD? > "TDD as religion" seems to me to lead to an absence of thinking about > overall system design, and system testing. An excess focus on the unit, I'll +1 that. I believe in TDD in principle - or more accurately I believe in thorough unit testing. Whether the testing drives the development is another story with pros and cons of its own. But the biggest downside of TDD, as often preached, is too strong a focus on unit tests. There then comes an assumption that if all the units work then the system must work. Which is not true at all. Not even close. So unit tests are essential, but they are a small part of the overall testing regime needed to deliver any significant system. -- 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 holly.ortaliz at gmail.com Wed Oct 10 23:19:51 2018 From: holly.ortaliz at gmail.com (Holly Jo) Date: Wed, 10 Oct 2018 22:19:51 -0500 Subject: [Tutor] Help please Message-ID: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> I have no clue what I?m doing wrong, I?m a new student food_1=input("Sushi") food_2=input("Quesdilla") new_food=food_1+food_2 print("Combining these foods will you,",new_food) input("Press enter to continue") Sent from Mail for Windows 10 From lulu_767 at hotmail.com Wed Oct 10 18:17:23 2018 From: lulu_767 at hotmail.com (Lulu) Date: Wed, 10 Oct 2018 22:17:23 +0000 Subject: [Tutor] Asynchronous http requests in Python Message-ID: Hi everyone, I am using python 2.7 and I wanted to ask the list the best way to issue multiple concurrent http requests from a Django application: Any input, suggestions are appreciated Thank you Sent from my iPhone From rajnish.sinha at infoprolearning.com Thu Oct 11 09:58:26 2018 From: rajnish.sinha at infoprolearning.com (Rajnish Sinha) Date: Thu, 11 Oct 2018 19:28:26 +0530 Subject: [Tutor] Tutor Digest, Vol 176, Issue 13 In-Reply-To: References: Message-ID: Try for river in rivers: print ("The " + river + " is in the country of " + rivers[river]) On Wed, Oct 10, 2018 at 9:30 PM 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: help please (Abdur-Rahmaan Janhangeer) > 2. Re: Advanced python recommendations (Abdur-Rahmaan Janhangeer) > 3. Re: help please (Mirage Web Studio) > 4. Re: help please (Deepak Dixit) > 5. Re: Advanced python recommendations (Mariam Haji) > > > > ---------- Forwarded message ---------- > From: Abdur-Rahmaan Janhangeer > To: Michael Schmitt > Cc: tutor > Bcc: > Date: Wed, 10 Oct 2018 11:53:41 +0400 > Subject: Re: [Tutor] help please > i think it should have been > > for river in rivers instead of > for rivers in rivers > > Abdur-Rahmaan Janhangeer > https://github.com/Abdur-rahmaanJ > Mauritius > > > > > ---------- Forwarded message ---------- > From: Abdur-Rahmaan Janhangeer > To: Mariam Haji > Cc: Tutor at python.org > Bcc: > Date: Wed, 10 Oct 2018 14:26:24 +0400 > Subject: Re: [Tutor] Advanced python recommendations > apart from already said, see popular python projects, and read the source > as you would read a book. you'll discover amazing tricks, it'll broaden > your horizon. hanging around those who achieved a good level will make you > level up. > > Abdur-Rahmaan Janhangeer > Mauritius > > > > > ---------- Forwarded message ---------- > From: Mirage Web Studio > To: Michael Schmitt , Python Tutor Mailing > List > Cc: > Bcc: > Date: Wed, 10 Oct 2018 13:45:40 +0530 > Subject: Re: [Tutor] help please > You are using the same variable name twice. > You may use "rivers" for the dict and "river" for values. > Also use descriptive names for variables. For eg if you correct the above > mistake, the next one will be this line > > for rivers in rivers.values(): > print (rivers) > > > and sorry for top positing. > > > On Wed 10 Oct, 2018, 12:38 Michael Schmitt, > wrote: > > > To whom it may concern: > > > > > > I am trying to teach myself Python and ran into a problem. This is my > code > > > > > > # name of rivers and country > > > > rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' } > > > > # prints river name > > for rivers in rivers.keys(): > > print (rivers) > > > > #prints country > > for rivers in rivers.values(): > > print (rivers) > > > > # prints statement " The (river) is in the country of (country) > > for rivers in rivers: > > print ("The " + rivers.keys() + "is in the country of " + > > rivers.vaules()) > > > > I am getting the following error > > for rivers in rivers.values(): > > AttributeError: 'str' object has no attribute 'values' > > > > Thanks for the help. > > > > Sincerely, > > > > Michael S. Schmitt > > > > > > [ > > > https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif > > ]< > > > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon > > > > Virus-free. www.avast.com< > > > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > > > ---------- Forwarded message ---------- > From: Deepak Dixit > To: Michael Schmitt > Cc: tutor at python.org > Bcc: > Date: Wed, 10 Oct 2018 12:43:46 +0530 > Subject: Re: [Tutor] help please > On Wed, Oct 10, 2018, 12:37 PM Michael Schmitt > > wrote: > > > To whom it may concern: > > > > > > I am trying to teach myself Python and ran into a problem. This is my > code > > > > > > # name of rivers and country > > > > rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' } > > > > # prints river name > > for rivers in rivers.keys(): > > print (rivers) > > > > #prints country > > for rivers in rivers.values(): > > print (rivers) > > > > # prints statement " The (river) is in the country of (country) > > > > Why are you using "for rivers in rivers". > Replace this for loop with :- > > for river in rivers: > print ("The " + river + "is in the country of " + rivers.get(river)) > > for rivers in rivers: > > print ("The " + rivers.keys() + "is in the country of " + > > rivers.vaules()) > > > > I am getting the following error > > for rivers in rivers.values(): > > AttributeError: 'str' object has no attribute 'values' > > > > Thanks for the help. > > > > Sincerely, > > > > Michael S. Schmitt > > > > > > [ > > > https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif > > ]< > > > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon > > > > Virus-free. www.avast.com< > > > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > > > ---------- Forwarded message ---------- > From: Mariam Haji > To: > Cc: tutor at python.org > Bcc: > Date: Wed, 10 Oct 2018 12:53:59 +0300 > Subject: Re: [Tutor] Advanced python recommendations > Thank you all for the above tips. > I actually did python 2 a friend sent it to me. And my current challenge > with the projects bit is how to pseudo-code and which approach to use as I > am not very familiar with the entire python syntax and how I can use it and > as well as python algorithms. > > So like I get a problem to create a simple loop or remove duplicates from a > list and that just doesn't process in my head and I always end up having to > google, then build from there. > > But thank you, I guess i just have to be patient it's been 3 months since I > started coding and development I am more of a front-end developer. > > I guess I just need to learn how to break down the problems and know what > to use where. > > On Wed, Oct 10, 2018 at 12:44 PM Mark Lawrence > wrote: > > > On 10/10/18 02:22, boB Stepp wrote: > > > On Tue, Oct 9, 2018 at 6:54 PM Mariam Haji > > wrote: > > >> > > >> Hi guys, I am on the last exercises of learn python the hard by Zed.A > > Shaw and I am looking for recommendations on what to follow next or what > > book to try next to advance my python skills to intermediate level. > > > > > > If you are a fan of Zed Shaw's approach, I noticed while at Barnes & > > > Noble a while back that he has released a sequel to the book you > > > cited, but only for the Python 3 version. You may be interested in > > > that. > > > > > > But I imagine taking time to imagine, detail and write the code for > > > projects would help you the most, as the others have said. > > > > > > > > > > After the disgraceful way that Zed Shaw wrote about Python 3 > > https://learnpythonthehardway.org/book/nopython3.html I wouldn't touch > > his stuff with a 100 foot long disinfected barge pole. Just a few > > months after this article he came out with the Python 3 book you > > reference above, presumably because he was losing cash. > > > > -- > > My fellow Pythonistas, ask not what our language can do for you, ask > > what you can do for our language. > > > > Mark Lawrence > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > -- > *Regards,* > *Mariam.* > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > -- Regards, *Rajnish Sinha* Project Manager *----------------------------------------------------------* *InfoPro Learning Inc. * 103 Morgan Lane | Suite 102 |Plainsboro, NJ 08536 T: (609) 606-9010, Extn: 1549 F: (609) 750-0981 E: rajnish.sinha at infoprolearning.com W: www.infoprolearning.com *eLearning Solutions **I** Training Delivery **I* *Consulting* *I* *Mobile Learning** I* *Gnosis LMS** I** Custom Application Development* From sidvenmal at gmail.com Thu Oct 11 09:25:21 2018 From: sidvenmal at gmail.com (Siddharth V) Date: Thu, 11 Oct 2018 18:55:21 +0530 Subject: [Tutor] Regarding python functions Message-ID: Dear Sir/Ma'am, I'm a beginner who has started to learn this wonderful and easy software language through the help of my school... I am not clear with the functions in python, which we can create while programming in order to make our program efficient and neat... Please brief me with those at the earliest... Thanking you, Yours truly, Siddharth From alan.gauld at yahoo.co.uk Thu Oct 11 12:59:23 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Oct 2018 17:59:23 +0100 Subject: [Tutor] Help please In-Reply-To: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> References: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> Message-ID: On 11/10/18 04:19, Holly Jo wrote: > > I have no clue what I?m doing wrong, I?m a new student > > food_1=input("Sushi") > food_2=input("Quesdilla") > new_food=food_1+food_2 > print("Combining these foods will you,",new_food) > input("Press enter to continue") Please always tell us what has gone wrong. What you expected and what you got. If there is an error message send the full error text. It also helps if you tell us which Python version and OS you are using. Based on the above I'm guessing you may be running this Python v3 code using Python v2. One of the changes between 3 and 2 is how input() works. If that's not the case then you need to provide more details, as requested above. -- 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 Thu Oct 11 13:02:22 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Oct 2018 18:02:22 +0100 Subject: [Tutor] Regarding python functions In-Reply-To: References: Message-ID: On 11/10/18 14:25, Siddharth V wrote: > I am not clear with the functions in python, which we can create while > programming in order to make our program efficient and neat... > Please brief me with those at the earliest... Its not clear exactly what kind of help you need so for now I will point you to the topic in my tutorial that covers functions - both using and defining them. http://www.alan-g.me.uk/l2p2/tutfunc.htm If that does not help please come back with more specific questions. Include any code you have tried and any error messages you received. -- 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 Thu Oct 11 13:10:21 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 Oct 2018 18:10:21 +0100 Subject: [Tutor] Asynchronous http requests in Python In-Reply-To: References: Message-ID: On 10/10/18 23:17, Lulu wrote: > Hi everyone, > I am using python 2.7 and I wanted to ask the list the best way> to issue multiple concurrent http requests from a Django application: This may be better addressed on the Django mailing list since Django may well have its own support for this. However in regular Python code there are multiple options and the best solution depends on the exact requirements on how these are to be processed. modules to research include threading, multiprocessing, and asyncio. But there are several other options besides. Which one is "best" depends on exactly what you need to do, your server environment, even your OS... That's why the Django users may be a more useful resource. -- 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 noflaco at gmail.com Thu Oct 11 12:51:20 2018 From: noflaco at gmail.com (Carlton Banks) Date: Thu, 11 Oct 2018 18:51:20 +0200 Subject: [Tutor] Help please In-Reply-To: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> References: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> Message-ID: What are you trying to do? tor. 11. okt. 2018 18.33 skrev Holly Jo : > > I have no clue what I?m doing wrong, I?m a new student > > food_1=input("Sushi") > food_2=input("Quesdilla") > new_food=food_1+food_2 > print("Combining these foods will you,",new_food) > input("Press enter to continue") > > > Sent from Mail for Windows 10 > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From noflaco at gmail.com Thu Oct 11 12:54:00 2018 From: noflaco at gmail.com (Carlton Banks) Date: Thu, 11 Oct 2018 18:54:00 +0200 Subject: [Tutor] Help please In-Reply-To: References: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> Message-ID: https://www.w3schools.com/python/ref_func_input.asp tor. 11. okt. 2018 18.51 skrev Carlton Banks : > What are you trying to do? > > tor. 11. okt. 2018 18.33 skrev Holly Jo : > >> >> I have no clue what I?m doing wrong, I?m a new student >> >> food_1=input("Sushi") >> food_2=input("Quesdilla") >> new_food=food_1+food_2 >> print("Combining these foods will you,",new_food) >> input("Press enter to continue") >> >> >> Sent from Mail for Windows 10 >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > From adameyring at gmail.com Thu Oct 11 23:31:57 2018 From: adameyring at gmail.com (Adam Eyring) Date: Thu, 11 Oct 2018 23:31:57 -0400 Subject: [Tutor] Help please In-Reply-To: References: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> Message-ID: The program works as is in Python3. For Python2, change input to raw_input and see if that makes it work (I know it worked for me when I had Python2). Also, it looks better to use " + " instead of a comma: print("Combining these foods will you," + new_food) Also, colons and spaces are good practices when using input boxes, such as food_1=raw_input("Sushi: ") On Thu, Oct 11, 2018 at 1:23 PM Carlton Banks wrote: > https://www.w3schools.com/python/ref_func_input.asp > > tor. 11. okt. 2018 18.51 skrev Carlton Banks : > > > What are you trying to do? > > > > tor. 11. okt. 2018 18.33 skrev Holly Jo : > > > >> > >> I have no clue what I?m doing wrong, I?m a new student > >> > >> food_1=input("Sushi") > >> food_2=input("Quesdilla") > >> new_food=food_1+food_2 > >> print("Combining these foods will you,",new_food) > >> input("Press enter to continue") > >> > >> > >> Sent from Mail for Windows 10 > >> > >> _______________________________________________ > >> 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 > From adameyring at gmail.com Thu Oct 11 23:46:07 2018 From: adameyring at gmail.com (Adam Eyring) Date: Thu, 11 Oct 2018 23:46:07 -0400 Subject: [Tutor] Tutor Digest, Vol 176, Issue 13 In-Reply-To: References: Message-ID: Also, try (works for me in Python3) rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' } for key, value in rivers.items(): print (key) for key, value in rivers.items(): print (value) for key, value in rivers.items(): print ("The " + key + " is in the country of " + value) On Thu, Oct 11, 2018 at 12:41 PM Rajnish Sinha < rajnish.sinha at infoprolearning.com> wrote: > Try > for river in rivers: > print ("The " + river + " is in the country of " + rivers[river]) > > On Wed, Oct 10, 2018 at 9:30 PM 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: help please (Abdur-Rahmaan Janhangeer) > > 2. Re: Advanced python recommendations (Abdur-Rahmaan Janhangeer) > > 3. Re: help please (Mirage Web Studio) > > 4. Re: help please (Deepak Dixit) > > 5. Re: Advanced python recommendations (Mariam Haji) > > > > > > > > ---------- Forwarded message ---------- > > From: Abdur-Rahmaan Janhangeer > > To: Michael Schmitt > > Cc: tutor > > Bcc: > > Date: Wed, 10 Oct 2018 11:53:41 +0400 > > Subject: Re: [Tutor] help please > > i think it should have been > > > > for river in rivers instead of > > for rivers in rivers > > > > Abdur-Rahmaan Janhangeer > > https://github.com/Abdur-rahmaanJ > > Mauritius > > > > > > > > > > ---------- Forwarded message ---------- > > From: Abdur-Rahmaan Janhangeer > > To: Mariam Haji > > Cc: Tutor at python.org > > Bcc: > > Date: Wed, 10 Oct 2018 14:26:24 +0400 > > Subject: Re: [Tutor] Advanced python recommendations > > apart from already said, see popular python projects, and read the source > > as you would read a book. you'll discover amazing tricks, it'll broaden > > your horizon. hanging around those who achieved a good level will make > you > > level up. > > > > Abdur-Rahmaan Janhangeer > > Mauritius > > > > > > > > > > ---------- Forwarded message ---------- > > From: Mirage Web Studio > > To: Michael Schmitt , Python Tutor Mailing > > List > > Cc: > > Bcc: > > Date: Wed, 10 Oct 2018 13:45:40 +0530 > > Subject: Re: [Tutor] help please > > You are using the same variable name twice. > > You may use "rivers" for the dict and "river" for values. > > Also use descriptive names for variables. For eg if you correct the above > > mistake, the next one will be this line > > > > for rivers in rivers.values(): > > print (rivers) > > > > > > and sorry for top positing. > > > > > > On Wed 10 Oct, 2018, 12:38 Michael Schmitt, > > wrote: > > > > > To whom it may concern: > > > > > > > > > I am trying to teach myself Python and ran into a problem. This is my > > code > > > > > > > > > # name of rivers and country > > > > > > rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' } > > > > > > # prints river name > > > for rivers in rivers.keys(): > > > print (rivers) > > > > > > #prints country > > > for rivers in rivers.values(): > > > print (rivers) > > > > > > # prints statement " The (river) is in the country of (country) > > > for rivers in rivers: > > > print ("The " + rivers.keys() + "is in the country of " + > > > rivers.vaules()) > > > > > > I am getting the following error > > > for rivers in rivers.values(): > > > AttributeError: 'str' object has no attribute 'values' > > > > > > Thanks for the help. > > > > > > Sincerely, > > > > > > Michael S. Schmitt > > > > > > > > > [ > > > > > > https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif > > > ]< > > > > > > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon > > > > > > Virus-free. www.avast.com< > > > > > > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link > > > > > > > _______________________________________________ > > > Tutor maillist - Tutor at python.org > > > To unsubscribe or change subscription options: > > > https://mail.python.org/mailman/listinfo/tutor > > > > > > > > > > > > > ---------- Forwarded message ---------- > > From: Deepak Dixit > > To: Michael Schmitt > > Cc: tutor at python.org > > Bcc: > > Date: Wed, 10 Oct 2018 12:43:46 +0530 > > Subject: Re: [Tutor] help please > > On Wed, Oct 10, 2018, 12:37 PM Michael Schmitt < > mikeschmitt128 at outlook.com > > > > > wrote: > > > > > To whom it may concern: > > > > > > > > > I am trying to teach myself Python and ran into a problem. This is my > > code > > > > > > > > > # name of rivers and country > > > > > > rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' } > > > > > > # prints river name > > > for rivers in rivers.keys(): > > > print (rivers) > > > > > > #prints country > > > for rivers in rivers.values(): > > > print (rivers) > > > > > > # prints statement " The (river) is in the country of (country) > > > > > > > Why are you using "for rivers in rivers". > > Replace this for loop with :- > > > > for river in rivers: > > print ("The " + river + "is in the country of " + rivers.get(river)) > > > > for rivers in rivers: > > > print ("The " + rivers.keys() + "is in the country of " + > > > rivers.vaules()) > > > > > > I am getting the following error > > > for rivers in rivers.values(): > > > AttributeError: 'str' object has no attribute 'values' > > > > > > Thanks for the help. > > > > > > Sincerely, > > > > > > Michael S. Schmitt > > > > > > > > > [ > > > > > > https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif > > > ]< > > > > > > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon > > > > > > Virus-free. www.avast.com< > > > > > > https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link > > > > > > > _______________________________________________ > > > Tutor maillist - Tutor at python.org > > > To unsubscribe or change subscription options: > > > https://mail.python.org/mailman/listinfo/tutor > > > > > > > > > > > > > ---------- Forwarded message ---------- > > From: Mariam Haji > > To: > > Cc: tutor at python.org > > Bcc: > > Date: Wed, 10 Oct 2018 12:53:59 +0300 > > Subject: Re: [Tutor] Advanced python recommendations > > Thank you all for the above tips. > > I actually did python 2 a friend sent it to me. And my current challenge > > with the projects bit is how to pseudo-code and which approach to use as > I > > am not very familiar with the entire python syntax and how I can use it > and > > as well as python algorithms. > > > > So like I get a problem to create a simple loop or remove duplicates > from a > > list and that just doesn't process in my head and I always end up having > to > > google, then build from there. > > > > But thank you, I guess i just have to be patient it's been 3 months > since I > > started coding and development I am more of a front-end developer. > > > > I guess I just need to learn how to break down the problems and know what > > to use where. > > > > On Wed, Oct 10, 2018 at 12:44 PM Mark Lawrence > > wrote: > > > > > On 10/10/18 02:22, boB Stepp wrote: > > > > On Tue, Oct 9, 2018 at 6:54 PM Mariam Haji > > > wrote: > > > >> > > > >> Hi guys, I am on the last exercises of learn python the hard by > Zed.A > > > Shaw and I am looking for recommendations on what to follow next or > what > > > book to try next to advance my python skills to intermediate level. > > > > > > > > If you are a fan of Zed Shaw's approach, I noticed while at Barnes & > > > > Noble a while back that he has released a sequel to the book you > > > > cited, but only for the Python 3 version. You may be interested in > > > > that. > > > > > > > > But I imagine taking time to imagine, detail and write the code for > > > > projects would help you the most, as the others have said. > > > > > > > > > > > > > > After the disgraceful way that Zed Shaw wrote about Python 3 > > > https://learnpythonthehardway.org/book/nopython3.html I wouldn't touch > > > his stuff with a 100 foot long disinfected barge pole. Just a few > > > months after this article he came out with the Python 3 book you > > > reference above, presumably because he was losing cash. > > > > > > -- > > > My fellow Pythonistas, ask not what our language can do for you, ask > > > what you can do for our language. > > > > > > Mark Lawrence > > > > > > _______________________________________________ > > > Tutor maillist - Tutor at python.org > > > To unsubscribe or change subscription options: > > > https://mail.python.org/mailman/listinfo/tutor > > > > > > > > > -- > > *Regards,* > > *Mariam.* > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > https://mail.python.org/mailman/listinfo/tutor > > > > > -- > > Regards, > > > > *Rajnish Sinha* > > Project Manager > > *----------------------------------------------------------* > > > > *InfoPro Learning Inc. * > > 103 Morgan Lane | Suite 102 |Plainsboro, NJ 08536 > > T: (609) 606-9010, Extn: 1549 > > F: (609) 750-0981 > > E: rajnish.sinha at infoprolearning.com > > W: www.infoprolearning.com > > *eLearning Solutions **I** Training Delivery **I* *Consulting* *I* *Mobile > Learning** I* *Gnosis LMS** I** Custom Application Development* > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From tylertravis35603 at icloud.com Thu Oct 11 21:41:08 2018 From: tylertravis35603 at icloud.com (Tyler Travis) Date: Thu, 11 Oct 2018 20:41:08 -0500 Subject: [Tutor] Python Message-ID: I fully understand the concept of what I am trying to do and I know that you can type out the algorithms to find mean, median, and mode but I used a shortcut by import the statistics function. Which yields the same results and the program passes with a 100% in the grading software However, it does not print the expected output, but rather some random sequence of characters and numbers. I do not understand why. Do you know why? From breamoreboy at gmail.com Fri Oct 12 06:37:33 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 12 Oct 2018 11:37:33 +0100 Subject: [Tutor] Python In-Reply-To: References: Message-ID: On 12/10/18 02:41, Tyler Travis via Tutor wrote: > I fully understand the concept of what I am trying to do and I know that you can type out the algorithms to find mean, median, and mode but I used a shortcut by import the statistics function. Which yields the same results and the program passes with a 100% in the grading software However, it does not print the expected output, but rather some random sequence of characters and numbers. I do not understand why. Do you know why? > No, as I do not see any code, data, Python version or OS. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From breamoreboy at gmail.com Fri Oct 12 06:38:46 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 12 Oct 2018 11:38:46 +0100 Subject: [Tutor] Help please In-Reply-To: References: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> Message-ID: On 12/10/18 04:31, Adam Eyring wrote: > The program works as is in Python3. For Python2, change input to raw_input > and see if that makes it work (I know it worked for me when I had Python2). > Also, it looks better to use " + " instead of a comma: > print("Combining these foods will you," + new_food) > > Also, colons and spaces are good practices when using input boxes, such as > food_1=raw_input("Sushi: ") > Please don't top post as it makes reading long threads really irritating. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From steve at pearwood.info Fri Oct 12 06:51:32 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 12 Oct 2018 21:51:32 +1100 Subject: [Tutor] Python In-Reply-To: References: Message-ID: <20181012105131.GF3817@ando.pearwood.info> On Thu, Oct 11, 2018 at 08:41:08PM -0500, Tyler Travis via Tutor wrote: > I fully understand the concept of what I am trying to do I'm very glad that at least one of us understands what you are trying to do, but I have no clue what it is. > and I know > that you can type out the algorithms to find mean, median, and mode > but I used a shortcut by import the statistics function. Which yields > the same results The same results as what? > and the program passes with a 100% in the grading software Great! > However, it does not print the expected output, but rather > some random sequence of characters and numbers. I do not understand > why. Do you know why? Your computer is cursed? Your cat walked over the keyboard? I don't understand how your two statements can be reconciled. You say that the program pass the grading software. Surely that means it must print the expected output. (How else will the grading software know it gives the correct results?) But then you say it prints gibberish. So I imagine you are doing this: print(mean([1, 2, 8, 9]) # expected output is "5" # output actually printed is "dz!gwd{7p3^)n3d%hw8" # and the grading software says "Correct, full marks!" which makes no sense to me. By the way, I'm the author of the statistics module, so I'm very glad to hear that you are using it, but if the point of the exercise is to write your own (simple?) versions of mean, median and mode, isn't it cheating to use the pre-written ones? -- Steve From alan.gauld at yahoo.co.uk Fri Oct 12 11:06:53 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 12 Oct 2018 16:06:53 +0100 Subject: [Tutor] Help please In-Reply-To: References: <5bbec159.1c69fb81.4104e.9f58@mx.google.com> Message-ID: On 12/10/18 04:31, Adam Eyring wrote: > Also, it looks better to use " + " instead of a comma: > print("Combining these foods will you," + new_food) It may "look better" but be aware that they don't do the same thing and the plus sign is a lot less efficient computationally since it creates a new string for each addition. For a simple case like this it won't matter but if you had a lot of short strings being added together in a loop it could slow things down quite a bit. The other problem with the plus sign is that it requires all arguments to be strings whereas the comma separated list gets automatically converted to a string by Python (by calling str(x) ) so is in general more reliable. -- 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 Fri Oct 12 11:12:15 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 12 Oct 2018 16:12:15 +0100 Subject: [Tutor] Python In-Reply-To: References: Message-ID: On 12/10/18 02:41, Tyler Travis via Tutor wrote: > I used a shortcut by import the statistics function. ... > it does not print the expected output, Did you by any chance include your code as an attachment? If so this mail server will strip it out as a security risk. Please post any code in the body of your email (using plain text) please. If you didn't send your code at all then please do so as we are not mind readers! :-) -- 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 mariamhaji01 at gmail.com Sat Oct 13 04:25:20 2018 From: mariamhaji01 at gmail.com (Mariam Haji) Date: Sat, 13 Oct 2018 11:25:20 +0300 Subject: [Tutor] python game error Message-ID: Hi guys, I am still on the learn python the hard way. I built the below on python 2.7 and my OS is windows 7. I am getting this error: Traceback (most recent call last): File "ex43.py", line 205, in a_game.play() File "ex43.py", line 21, in play next_scene_name = current_scene.enter() AttributeError: 'NoneType' object has no attribute 'enter' Main code is as, an I have highlighted line 21 and 205 in green below. from sys import exit from random import randint class Scene(object): def enter(self): print "This scene is not yet configured. Subclass it and implement enter()." exit(1) class Engine(object): def __init__(self, scene_map): self.scene_map = scene_map def play(self): current_scene = self.scene_map.opening_scene() while True: print "\n--------" next_scene_name = current_scene.enter() current_scene = self.scene_map.next_scene(next_scene_name) class Death(Scene): quips = [ "You died. You Kinda suck at this.", "Your mum would be proud if she were smarter.", "Such a looser.", "I have a small puppy that's better at this." ] def enter(self): print Death.quips[randint(0, len(self.quips)-1)] exit(1) class CentralCorridor(Scene): def enter(self): print "The Gothons of Planet Percal #25 have invaded your ship and destroyed" print "Your entire crew, you are the only surviving memeber and your last" print "Mission is to get the neutron destruct a bomb from the weapons Armory" print "Put it in the bridge and blow the ship up after getting into an " print "escape pod" print "\n" print "You're running down the central corridor to the Weapons Armory when" print "a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clow costume" print "flowing around his hate filled body. He's blocking the door to the" print "Armoury and about to pull a weapon to blast you." print "Do you shoot!, dodge!, or tell a joke? enter below." action = raw_input("> ") if action == "shoot!": print "Quick on the draw you yank out your blaster and fire it at the Gothon." print "His clown costume is flowing and moving around his body, which throws" print "off your aim. Your laser hits his costume but misses him entirely. This" print "completely ruins his brand new costume his mother bought him, which" print "makes him fly into a rage and blast ou repeatedly in the face until" print "you are dead. Then he eats you" print 'death' elif action == "dodge!": print "Like a world class boxer you dodge, weave, slip and slide right" print "as the Gothon's blaster cranks a laser past your head" print "In the middle of your artful dodge your foot slips and you" print "bang your head on the metal wall and you pass out" print "You wake up shortly after, only to die as the Gothon stomps on" print "your head and eats you" print 'death' elif action == "tell a joke": print "Lucky for you they made you learn Gothon insults in the academy" print "you tell the one Gothon joke you know" print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr." print "The Gothon stops, tries not to laugh, he busts out laughing and can't move." print "While he is laughing you run up and shoot him square in the head" print "putting him down, then jump through the Weapon Armory door." return 'laser_weapon_armoury' else: print "DOES NOT COMPUTE!" return 'central_corridor' class LaserWeaponArmory(Scene): def enter(self): print "You do a drive roll into the weapon Armory, crouch and scan the room" print "for more Gothons that might be hiding. It's dead quiet, too quiet" print "You stand up and run to the far side of the room and find the" print "neutron bomb in it's container. There's a keypad lock on the box" print "Wrong 10 times then the lock closes forever and you can't" print "get the bomb. The code is 3 digits" code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9)) guess = raw_input("[keypad]> ") guesses = 0 while guess != code and guesses < 10: print "BZZZEEDDDD!" guesses += 1 guess = raw_input("[keypad]> ") if guess == code: print "The container clicks open and the seal breaks, letting gas out." print "You grab the neutron bomb and run as fast as you can to the" print "bridge where you must place it in the right spot" return 'the_bridge' else: print "The lock buzzes one last time then you hear a sickening" print "melting sound as the mechanism is fused together" print "You decide to sit there and finally the Gothons blow up the" print "ship form their ship and you die." return 'death' class TheBridge(Scene): def enter(self): print "you bust into the bridge with the neutron destruct bomb" print "under your arm and surprises 5 Gothons who are trying to" print "take control of the ship. Each of them has an even uglier" print "clown costume then the last. They haven't pulled their" print "weapons out yet, as they see the active bomb under your" print "and don't want to set it off." action = raw_input("> ") if action == "throw the bomb": print "In panic you throw the bomb at the group of Gothons" print "and make the a leap for the door. Right as you drop it a" print "Gothon shoots you right in the back killing you." print "As you die, you see another Gothon frantically try to disarm" print "the bomb, you will die knowing they will probably blow up when" print "it goes off" return 'death' elif action == "slowly place the bomb": print "You point your blaster at the bomb under your arm" print "and the Gothons put their hands up and start to sweat" print "You inch back to the door open it and then carefully" print "place the bomb on the floor, pointing your blaster at it." print "You then jump back through the door, punch the close button" print "and blast the lock so the Gothons can't get out." print "Now that the bomb is placed you run to the escape pod to" print "get off this tin can." return 'escape_pod' else: print "DOES NOT COMPUTE!" return "the_bridge" class EscapePod(Scene): def enter(self): print "You rush throuh the ship desperately trying to make it to" print "the escape pod before the whole ship explodes. It seems like" print "hardly any Gothond are on the ship, so your run is clear of" print "interference. You get to the chamber with the escape pods. and" print "now need to pick one to take. Some of them could be damaged" print "but you don't have time to look. There's 5 pods. Which one" print "do you take." good_pod = randint(1,5) guess = raw_input("[pod #]> ") if int(guess) != good_pod: print "You jump into pod %s and hit the eject button." %guess print "The pod escapes out into the void of space, then" print "implodes as the hull ruptures, crushing your bidy" print "into jam jelly" return 'death' else: print "You jumo into pod %s and hit the eject button." %guess print "The pod easily slides out into space heading to" print "the planet below. As it flies to the planet, you look" print "back star, taking out the Gothon ship at the same" print "time. You won!" return 'finished' class Map(object): scenes = { 'central_corridor': CentralCorridor(), 'laser_weapon_armory': LaserWeaponArmory(), 'the_bridge': TheBridge(), 'escape_pod': EscapePod(), 'death': Death() } def __init__(self, start_scene): self.start_scene = start_scene def next_scene(self, scene_name): return Map.scenes.get(scene_name) def opening_scene(self): return self.next_scene(self.start_scene) a_map = Map('central_corridor') a_game = Engine(a_map) a_game.play() Please help me sort the error and how do I create a keyword to exit the game mid way? -- *Regards,* *Mariam.* From mariamhaji01 at gmail.com Sat Oct 13 04:32:09 2018 From: mariamhaji01 at gmail.com (Mariam Haji) Date: Sat, 13 Oct 2018 11:32:09 +0300 Subject: [Tutor] Statistics with python Message-ID: Hi guys, I have a statistics question where I am to calculate the probability of a sample dataset. I have done the coding bit but I am not sure if I did the right thing, I just need confirmation before I submit it. the question is as: If a sample of 50 patients is taken from a dataset what is the probability that we will get a patient above the age of 56? So I know my sample mean is 50 and my no is 56 to get std I manually did 50/?56 (that's 50/square root of 56) I got the answer as 6.66 So my mean is 50 and std is 6.66 Then I did the below to get the z score and probability using scipy.stats as st m=50 s=6.66 z1 = (56-m)/s p1 = st.norm.sf(z1) print ('Probability of patient above the age of 56 is:', (p1)) Probability of patient above the age of 56 is: 0.183820506093897 Am i missing something or did I do it right? Thanks in Advance. -- *Regards,* *Mariam.* From steve at pearwood.info Sat Oct 13 06:37:43 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 13 Oct 2018 21:37:43 +1100 Subject: [Tutor] python game error In-Reply-To: References: Message-ID: <20181013103742.GI3817@ando.pearwood.info> On Sat, Oct 13, 2018 at 11:25:20AM +0300, Mariam Haji wrote: > Hi guys, > > I am still on the learn python the hard way. > I built the below on python 2.7 and my OS is windows 7. > > I am getting this error: > Traceback (most recent call last): > File "ex43.py", line 205, in > a_game.play() > File "ex43.py", line 21, in play > next_scene_name = current_scene.enter() > AttributeError: 'NoneType' object has no attribute 'enter' The current_scene variable currently is set to None, instead of athe value that you think it has. Try inserting the line print repr(current_scene), type(current_scene) just before line 21. Then you need to work out why it is set to None, instead of whatever you expect it to be. By the way, you shouldn't expect us to hunt through dozens or hundreds of lines of code trying to debug your program for you. We're volunteers, not being paid to do this. You should give us a *minimum* (small!) example of your program. That might mean keeping two copies, one which has all the irrelevant code removed. (Like the dozens of calls to print -- not one single one of those lines of code could possibly have anything to do with the bug in your program.) Removing that irrelevant code, cutting it down to a minimum, may even help you find the bug yourself! Please read this for more information: http://sscce.org/ Even though this is written for Java programmers, the principles also apply to Python. > Main code is as, an I have highlighted line 21 and 205 in green below. Have you? That explains why the indentation is messed up for me, making the code impossible to understand correctly. And the green doesn't show up in my email program. Code is PLAIN TEXT, with no formatting. When you add formatting, like colours, bold or italics, fancy fonts, animmated paperclips, etc, your email program will destroy the indentation and make it unusable. -- Steven From steve at pearwood.info Sat Oct 13 06:56:07 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 13 Oct 2018 21:56:07 +1100 Subject: [Tutor] Statistics with python In-Reply-To: References: Message-ID: <20181013105607.GJ3817@ando.pearwood.info> On Sat, Oct 13, 2018 at 11:32:09AM +0300, Mariam Haji wrote: > the question is as: > If a sample of 50 patients is taken from a dataset what is the probability > that we will get a patient above the age of 56? That depends on the data set. How is it distributed? There are dozens of likely distributions: Poisson, Cauchy, binomial, multinomial, hypergeometric, Zipf, Weibull, Pareto, Student's t, uniform, and many, many, many, MANY more: https://en.wikipedia.org/wiki/List_of_probability_distributions > So I know my sample mean is 50 and my no is 56 > to get std I manually did 50/?56 (that's 50/square root of 56) I got the > answer as 6.66 If you use too many abbreviations, especially the wrong abbreviations, it is hard to understand you. Do you mean that n, the number of samples ("no") is 56? What's std? Standard deviation, standard error, Sexually Transmitted Disease? Something else? It looks like you are dividing the sample mean by the square root of the sample size, but I don't know why. > So my mean is 50 and std is 6.66 > > Then I did the below to get the z score and probability using scipy.stats > as st > > m=50 > s=6.66 > > z1 = (56-m)/s You're subtracting the average data point from the number of samples, which is meaningless. And the value of s seems to be meaningless too. It is possible that I have misunderstood you. Please try explaining your steps more carefully, without using abbreviations we might not get. I've tried to guess what you mean as best I can, but I might not have succeeded. Thank you. -- Steve From __peter__ at web.de Sat Oct 13 10:08:14 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 13 Oct 2018 16:08:14 +0200 Subject: [Tutor] python game error References: Message-ID: Mariam Haji wrote: > Hi guys, > > I am still on the learn python the hard way. > I built the below on python 2.7 and my OS is windows 7. > > I am getting this error: > Traceback (most recent call last): > File "ex43.py", line 205, in > a_game.play() > File "ex43.py", line 21, in play > next_scene_name = current_scene.enter() > AttributeError: 'NoneType' object has no attribute 'enter' The value of current_scene is None here. If you dig a bit in your code looking for places where that can happen you end up with > class Map(object): > scenes = { > 'central_corridor': CentralCorridor(), > 'laser_weapon_armory': LaserWeaponArmory(), > 'the_bridge': TheBridge(), > 'escape_pod': EscapePod(), > 'death': Death() > } > > def __init__(self, start_scene): > self.start_scene = start_scene > > def next_scene(self, scene_name): > return Map.scenes.get(scene_name) Map.scenes is a dict, and its get() method returns None when the lookup for a key fails. If you pass an unknown scene name to the next_scene() method it returns None rather than a Scene instance. As a first step to fix this I recommend that you use the item lookup operator instead of the method: def next_scene(self, scene_name): return Map.scenes[scene_name] This will fail immediately, and the error message should be more informative. I got: Traceback (most recent call last): File "./game_mariam.py", line 181, in a_game.play() File "./game_mariam.py", line 20, in play current_scene = self.scene_map.next_scene(next_scene_name) File "./game_mariam.py", line 173, in next_scene return Map.scenes[scene_name] KeyError: 'laser_weapon_armoury' That is, you need to decide if you want British or American orthography. From bgailer at gmail.com Sat Oct 13 11:40:08 2018 From: bgailer at gmail.com (Bob Gailer) Date: Sat, 13 Oct 2018 11:40:08 -0400 Subject: [Tutor] python game error In-Reply-To: References: Message-ID: suggestions: 1-Use triple-quoted strings: print """take the short-cut!""' 2 - make the program much simpler to start with. The usual approach to developing programs like this is to start simple get the simple things working right then add more complicated scene descriptions. Even better: separate data from logic. Create a sqlite database in which you store the information about the various scenes. Then write a program that does nothing but access the various database elements. This will result in much much easier to read and maintain code, and much much easier to read and maintain the description of the scenes. I realize this may sound like a big chunk but it is well worth the effort to learn how to do it. From oscar.j.benjamin at gmail.com Sat Oct 13 15:22:20 2018 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 13 Oct 2018 20:22:20 +0100 Subject: [Tutor] Statistics with python In-Reply-To: References: Message-ID: On Sat, 13 Oct 2018 at 11:23, Mariam Haji wrote: > > Hi guys, Hi Mariam > the question is as: > If a sample of 50 patients is taken from a dataset what is the probability > that we will get a patient above the age of 56? I can think of several ways of interpreting this: (a): You have a dataset consisting of 50 patients. You want to know the probability that a patient chosen from that sample will be above the age of 56. (b): You have a dataset consisting of 50 patients. You consider it to be representative of a larger population of people. You would like to use your dataset to estimate the probability that a patient chosen from the larger population will be above the age of 56. (c): You have a larger dataset consisting of more than 50 patients. You want to know that probability that a sample of 50 patients chosen from the larger dataset will contain at least (or exactly?) one person above the age of 56. (d): You have a larger dataset, but you will only analyse a sample of 50 patients from it. You want to use statistics on that sample to estimate the probability that a patient chosen from the larger dataset will be above the age of 56. I can list more interpretations but I think it would be better to wait for you to clarify. > So I know my sample mean is 50 Do you mean that you separately know that the sample mean is 50? Or do you mean that you know it's 50 because of what you stated above? Above you stated that you have a sample *size* of 50 and that doesn't imply that the sample *mean* is 50. > and my no is 56 > to get std I manually did 50/?56 (that's 50/square root of 56) I got the > answer as 6.66 It's possible that you are not using the correct terminology here but otherwise this isn't correct. If you had a sample *standard deviation* of 50 and a sample *size* of 50 then 50/sqrt(56) would give you the standard error. I am not sure that you did actually want to do that though. > So my mean is 50 and std is 6.66 I'm not sure that this is correct... > Then I did the below to get the z score and probability using scipy.stats > as st > > m=50 > s=6.66 > > z1 = (56-m)/s > > p1 = st.norm.sf(z1) > print ('Probability of patient above the age of 56 is:', (p1)) > > Probability of patient above the age of 56 is: 0.183820506093897 The part above looks correct if we assume we are choosing a single patient whose age is normally distributed with mean 50 and standard deviation 6.66. I'm not sure these assumptions are correct though. -- Oscar From bgailer at gmail.com Sun Oct 14 11:08:00 2018 From: bgailer at gmail.com (bob gailer) Date: Sun, 14 Oct 2018 11:08:00 -0400 Subject: [Tutor] python game error In-Reply-To: References: Message-ID: On 10/13/2018 4:25 AM, Mariam Haji wrote: ... Your problem intrigued me enough to spend some time "fixing" your program so it will compile with no errors and run at least the initial case where I entered "shoot!" Here are the problems I found: (line numbers refer to your original code) - spelling error? return 'laser_weapon_armoury' - fixed - line 122 there is no if preceding the elif - I added one - line 160 returns 'finished'. There is no corresponding entry in Map.scenes ?? - I added one and a corresponding class Finished. - the logic for allowing 10 guesses of the code is flawed. It only allows 2 guesses. ? I will leave it up to you to figure out why. In addition I - used """ for all multi? line prints; a personal preference - it makes entry and reading easier. - updated code so it will run under python 3. This involves: ? - changing print statements to print function calls ? - assigning input to raw_input for version 3 - added raise to Map.next_scene to handle undefined scenes Try it out. > how do I create a keyword to exit the game mid way? You don't create keywords. To support mid-game exiting I added an abort method to Finished. Use: Finished.abort('reason') ---------- program ---------- from sys import exit, version from random import randint if versison[0] == '3': ??? raw_input = input class Scene(object): ??? def enter(self): ??????? print("This scene is not yet configured. Subclass it and implement enter().") ??????? exit(1) class Engine(object): ??? def __init__(self, scene_map): ??????? self.scene_map = scene_map ??? def play(self): ??????? current_scene = self.scene_map.opening_scene() ??????? while True: ??????????? print("\n--------") ??????????? next_scene_name = current_scene.enter() ??????????? current_scene = self.scene_map.next_scene(next_scene_name) class Death(Scene): ??? quips = [ "You died. You Kinda suck at this.", "Your mum would be proud if she were smarter.", "Such a looser.", "I have a small puppy that's better at this." ] ??? def enter(self): ??????? print(Death.quips[randint(0, len(self.quips)-1)]) ??????? exit(1) class CentralCorridor(Scene): ??? def enter(self): ??????? print("""The Gothons of Planet Percal #25 have invaded your ship and destroyed Your entire crew, you are the only surviving memeber and your last Mission is to get the neutron destruct a bomb from the weapons Armory Put it in the bridge and blow the ship up after getting into an escape pod You're running down the central corridor to the Weapons Armory when a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clow costume flowing around his hate filled body. He's blocking the door to the Armoury and about to pull a weapon to blast you. Do you shoot!, dodge!, or tell a joke? enter below.""") ??????? action = raw_input("> ") ??????? if action == "shoot!": ??????????? print("""Quick on the draw you yank out your blaster and fire it at the Gothon. His clown costume is flowing and moving around his body, which throws off your aim. Your laser hits his costume but misses him entirely. This completely ruins his brand new costume his mother bought him, which makes him fly into a rage and blast ou repeatedly in the face until you are dead. Then he eats you""") ??????????? return 'death' ??????? elif action == "dodge!": ??????????? print("""Like a world class boxer you dodge, weave, slip and slide right as the Gothon's blaster cranks a laser past your head In the middle of your artful dodge your foot slips and you bang your head on the metal wall and you pass out You wake up shortly after, only to die as the Gothon stomps on your head and eats you""") ??????????? return 'death' ??????? elif action == "tell a joke": ??????????? print("""Lucky for you they made you learn Gothon insults in the academy you tell the one Gothon joke you know Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr. The Gothon stops, tries not to laugh, he busts out laughing and can't move. While he is laughing you run up and shoot him square in the head putting him down, then jump through the Weapon Armory door.""") ??????????? return 'laser_weapon_armory' ??????? else: ??????????? print("DOES NOT COMPUTE!") ??????????? return 'central_corridor' class LaserWeaponArmory(Scene): ??? def enter(self): ??????? print("""You do a drive roll into the weapon Armory, crouch and scan the room for more Gothons that might be hiding. It's dead quiet, too quiet You stand up and run to the far side of the room and find the neutron bomb in it's container. There's a keypad lock on the box Wrong 10 times then the lock closes forever and you can't get the bomb. The code is 3 digits""") ??????? code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9)) ??????? guess = raw_input("[keypad]> ") ??????? guesses = 0 ??????? while guess != code and guesses < 10: ??????????? print("BZZZEEDDDD!") ??????????? guesses += 1 ??????????? guess = raw_input("[keypad]> ") ??????????? if guess == code: ??????????????? print("""The container clicks open and the seal breaks, letting gas out. You grab the neutron bomb and run as fast as you can to the bridge where you must place it in the right spot""") ??????????????? return 'the_bridge' ??????????? else: ??????????????? print("""The lock buzzes one last time then you hear a sickening melting sound as the mechanism is fused together You decide to sit there and finally the Gothons blow up the ship form their ship and you die.""") ??????????????? return 'death' class TheBridge(Scene): ??? def enter(self): ??????? print("""you bust into the bridge with the neutron destruct bomb under your arm and surprises 5 Gothons who are trying to take control of the ship. Each of them has an even uglier clown costume then the last. They haven't pulled their weapons out yet, as they see the active bomb under your and don't want to set it off.) action = raw_input("> ") if action == "throw the bomb": In panic you throw the bomb at the group of Gothons and make the a leap for the door. Right as you drop it a Gothon shoots you right in the back killing you. As you die, you see another Gothon frantically try to disarm the bomb, you will die knowing they will probably blow up when it goes off""") ??????? if action == 'foo': ??????????? return 'death' ??????? elif action == "slowly place the bomb": ??????????? print("""You point your blaster at the bomb under your arm" and the Gothons put their hands up and start to sweat" You inch back to the door open it and then carefully" place the bomb on the floor, pointing your blaster at it." You then jump back through the door, punch the close button" and blast the lock so the Gothons can't get out." Now that the bomb is placed you run to the escape pod to" get off this tin can.""") ??????????? return 'escape_pod' ??????? else: ??????????? print("DOES NOT COMPUTE!") ??????????? return "the_bridge" class EscapePod(Scene): ??? def enter(self): ??????? print("""You rush throuh the ship desperately trying to make it to" the escape pod before the whole ship explodes. It seems like" hardly any Gothond are on the ship, so your run is clear of" interference. You get to the chamber with the escape pods. and" now need to pick one to take. Some of them could be damaged" but you don't have time to look. There's 5 pods. Which one" do you take.""") ??????? good_pod = randint(1,5) ??????? guess = raw_input("[pod #]> ") ??????? if int(guess) != good_pod: ??????????? print("""You jump into pod %s and hit the eject button." %guess The pod escapes out into the void of space, then" implodes as the hull ruptures, crushing your bidy" into jam jelly""") ??????????? return 'death' ??????? else: ??????????? print("""You jumo into pod %s and hit the eject button." %guess The pod easily slides out into space heading to" the planet below. As it flies to the planet, you look" back star, taking out the Gothon ship at the same" time. You won!""") ??????????? return 'finished' class Finished(Scene): ??? def enter(self): ??????? print("We ended succesfully") ??????? sys.exit() ??? def abort(self, reason='no reason given'): ??????? print("aborting - " + reason) ??????? sys.exit() class Map(object): ??? scenes = { ??? 'central_corridor': CentralCorridor(), ??? 'laser_weapon_armory': LaserWeaponArmory(), ??? 'the_bridge': TheBridge(), ??? 'escape_pod': EscapePod(), ??? 'death': Death(), ??? 'finished': Finished() ??? } ??? def __init__(self, start_scene): ??????? self.start_scene = start_scene ??? def next_scene(self, scene_name): ??????? scene =? Map.scenes.get(scene_name) ??????? if scene: return scene ??????? raise ValueError('unknown scene ' + scene_name) ??? def opening_scene(self): ??????? return self.next_scene(self.start_scene) a_map = Map('central_corridor') a_game = Engine(a_map) a_game.play() From bgailer at gmail.com Sun Oct 14 12:08:47 2018 From: bgailer at gmail.com (bob gailer) Date: Sun, 14 Oct 2018 12:08:47 -0400 Subject: [Tutor] python game error In-Reply-To: References: Message-ID: More comments: ------------ User Friendly? ------------ I hope this game is not intended for actual use. No one will be able to guess the correct actions in a reasonable time. or 3 digit random code given 10 tries for any one code. I for one would give up pretty quickly. The original colossal cave game is an excellent example of a user-friendly text adventure game. If you are using a Windows computer you can get a version from the Microsoft Store It - gives explicit directions - keeps responses to a few words most chosen from a limited list or names of visible objects. ------------ Python coding "trick"1? ------------ when I build a map I omit the () after the class e.g. 'death' = Death, ... and apply them to the item retrieved from the map. use a decorator to build the map dictionary: # the decorator function: def add_to_map(cls, map={}): # map is initialized to a {} when function is "compiled" ??? if cls: ??? ??? map[cls.__name__] = cls # add entry to dictionary ??? ??? return cls ??? else: return map # apply decorator to class definitions # this will add 'Death': @add_to_map class Death(Scene): ??? class_definition ?# ditto for all other classes based on Scene - then class Map: ??? scenes? = add_to_map() # note when map is called with no arguments it returns the dictionary ------------ Python coding "trick" 2 ------------ instead of: ??????? print(Death.quips[randint(0, len(self.quips)-1)]) try: ??????? ??? ??? ??? print(random.choice(Death.quips) ------------ Python coding "trick" 3 ------------ action = raw_input('>').title() # to make user input agree with class names ------------ Python coding "trick" 4 ------------ From mats at wichmann.us Sun Oct 14 13:42:39 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 14 Oct 2018 11:42:39 -0600 Subject: [Tutor] python game error In-Reply-To: References: Message-ID: On 10/14/2018 10:08 AM, bob gailer wrote: > ------------ Python coding "trick"1? ------------ > when I build a map I omit the () after the class e.g. 'death' = Death, > ... and apply them to the item retrieved from the map. > > use a decorator to build the map dictionary: > > # the decorator function: > def add_to_map(cls, map={}): # map is initialized to a {} when function > is "compiled" > ??? if cls: > ??? ??? map[cls.__name__] = cls # add entry to dictionary > ??? ??? return cls > ??? else: return map Hint here: don't use 'map' as your own variable name, since it's a built-in function. It's not that it won't work, but is likely to cause confusion someday. Most "code inspection" systems will complain that you are shadowing or redefining a builtin (terminology may vary). The list of builtins - thus names to avoid - is here: https://docs.python.org/3/library/functions.html From oscar.j.benjamin at gmail.com Sun Oct 14 16:18:05 2018 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sun, 14 Oct 2018 21:18:05 +0100 Subject: [Tutor] Statistics with python In-Reply-To: References: Message-ID: I'm replying back to the tutor list. Can you reply there rather than directly to me please? Also I've moved your response below mine as that is the preferred style on this list. My answer is below. On Sun, 14 Oct 2018 at 16:05, Mariam Haji wrote: > > On Sat, Oct 13, 2018 at 10:24 PM Oscar Benjamin wrote: >> >> On Sat, 13 Oct 2018 at 11:23, Mariam Haji wrote: >> > >> > Hi guys, >> >> Hi Mariam >> >> > the question is as: >> > If a sample of 50 patients is taken from a dataset what is the probability >> > that we will get a patient above the age of 56? >> >> I can think of several ways of interpreting this: >> >> (a): You have a dataset consisting of 50 patients. You want to know >> the probability that a patient chosen from that sample will be above >> the age of 56. >> >> (b): You have a dataset consisting of 50 patients. You consider it to >> be representative of a larger population of people. You would like to >> use your dataset to estimate the probability that a patient chosen >> from the larger population will be above the age of 56. >> >> (c): You have a larger dataset consisting of more than 50 patients. >> You want to know that probability that a sample of 50 patients chosen >> from the larger dataset will contain at least (or exactly?) one person >> above the age of 56. >> >> (d): You have a larger dataset, but you will only analyse a sample of >> 50 patients from it. You want to use statistics on that sample to >> estimate the probability that a patient chosen from the larger dataset >> will be above the age of 56. >> >> I can list more interpretations but I think it would be better to wait >> for you to clarify. > > My dataset consists of 300+ patients and I want to analyze analyse a sample of 50 patients from it. > Yto know the probability that a patient chosen from the larger dataset > will be above the age of 56. Is this a homework problem or an actual problem? If I had 300+ patients I would think that the best way to work out the probability that a patient chosen from those 300+ was over the age of 56 would be to count how many are over the age of 56. Likewise if I wanted to estimate how many would be over the age of 56 using a smaller sample of 50 patients then I would also just count how many are over the age of 56 in that smaller sample. I'm going to guess that this is a homework problem and that you have been asked to assume that the ages are normally distributed (which they would not be in reality). Your calculation for the standard deviation given in your earlier email doesn't make any sense. You should calculate this using a function that calculates the standard deviation. There is one in the numpy module: >>> import numpy >>> ages = [35, 45, 55, 70] >>> numpy.mean(ages) 51.25 >>> numpy.std(ages) 12.93010054098575 -- Oscar From bgailer at gmail.com Sun Oct 14 18:59:05 2018 From: bgailer at gmail.com (bob gailer) Date: Sun, 14 Oct 2018 18:59:05 -0400 Subject: [Tutor] python game error In-Reply-To: References: Message-ID: <0f0d2fc7-9fc2-89cf-43dc-a1200c86e1b1@gmail.com> On 10/14/2018 1:42 PM, Mats Wichmann wrote: > Hint here: don't use 'map' as your own variable name, since it's a > built-in function. Absolutely, I am always warning others about this gotcha. In this case map is local to add_to_map so it does not affect then global namespace. The reason I used it here was because the OP was using map (actually Map). Duh! Bob From bgailer at gmail.com Sun Oct 14 19:04:28 2018 From: bgailer at gmail.com (bob gailer) Date: Sun, 14 Oct 2018 19:04:28 -0400 Subject: [Tutor] python game error In-Reply-To: References: Message-ID: More comments on code: ???????? guess = raw_input("[pod #]> ") ??????? if int(guess) != good_pod: If user enters something that will not convert to integer an exception will be raised. For example >>> int('a') Traceback (most recent call last): ? File "", line 1, in ValueError: invalid literal for int() with base 10: 'a' Either use try - except - or ??????? good_pod = str(randint(1,5)) ??????? guess = raw_input("[pod #]> ") ??????? if guess != good_pod: - or if you import choice from random ??????? good_pod = choice('12345') Migrating to a database (separating data from logic). Why? - it keeps the code simple. - makes adding / editing scenes easier. Given a database (game.db) with 2 tables: ?? ?? ? ??? table??????????? ??? ??? columns ??? scene_entry??? ??? scene, text, prompt ??? scene_action?? ??? scene, action, text, next_scene Example: CentralCorridor, The Gothons of Planet Percal #25 have invaded ..., Do you shoot!, dodge!, or tell a joke? CentralCorridor, shoot!, Quick on the draw you yank out your ...., Death CentralCorridor, dodge!, Like a world class boxer you dodge ...., Death CentralCorridor, tell a joke!, Lucky for you they made you,...., Laser_weapon_armory The generic structure of a game program: ??? next_scene = 'CentralCorridor' ??? while next_scene != 'Finished': ??? ??? get text, prompt from scene_entry ??? ??? print entry text ??? ??? prompt user for action ??? ??? get text, next_scene from scene_action ??? ??? print text A simple python game program utilizing the game database follows. You would first create c:/games/game.db using a tool like SQLite Studio, or request a copy from me. It is up to you to fill in the rest of the various table rows. What's missing? Code to handle the code and good_pod guesses. That will come later. ------ program ------ import sqlite3 as sq def sel(cols, rest, vals=(,)): ??? # construct, execute a sql select statement from the arguments ??? # get and return one row (there should be at most one) ??? sql = "select " + cols + " from " + rest + ";" ??? curs = conn.execute(sql, vals) ??? x = curs.fetchone() ??? if x: return x ??? raise ValueError(sql, vals) def game(next_scene): ??? while next_scene != 'finished': ??????? text, prompt = sel("text, prompt", "scene_entry where scene = ?", (next_scene,)) ??????? print(text) ??????? action = input(prompt + '>') # tell a joke! ??????? text, next_scene = sel("text, next_scene", "scene_action where scene = ? and action= ?", (next_scene, action)) ??????? print(text) conn = sq.connect("c:/games/game.db") game('CentralCorridor') ------ end program ------ From __peter__ at web.de Mon Oct 15 03:57:27 2018 From: __peter__ at web.de (Peter Otten) Date: Mon, 15 Oct 2018 09:57:27 +0200 Subject: [Tutor] python game error References: Message-ID: bob gailer wrote: > ------------ Python coding "trick"1 ------------ > when I build a map I omit the () after the class e.g. 'death' = Death, > ... and apply them to the item retrieved from the map. > > use a decorator to build the map dictionary: > > # the decorator function: > def add_to_map(cls, map={}): # map is initialized to a {} when function > is "compiled" > if cls: > map[cls.__name__] = cls # add entry to dictionary > return cls > else: return map > > # apply decorator to class definitions > # this will add 'Death': > @add_to_map > class Death(Scene): > class_definition > > # ditto for all other classes based on Scene - then > > class Map: > scenes = add_to_map() # note when map is called with no arguments > it returns the dictionary Hm, you have now replaced the dead simple > class Map(object): > scenes = { > 'central_corridor': CentralCorridor(), > 'laser_weapon_armory': LaserWeaponArmory(), > 'the_bridge': TheBridge(), > 'escape_pod': EscapePod(), > 'death': Death() > } with a class decorator, a dunder name, a mutable default argument -- lots of clever Python specific stuff that I wouldn't put on a beginner's list of top priorities. Don't use unless you spotted at least one bug in the code sample at first glance :) By the way, you do not need a map (dict) at all to implement a game like this, you may return the next scene directly. A sketch: class Bridge: def enter(self): ... action = ... if action == "jump off the bridge": return Death("You are eaten by the piranhas") else: ... From alan.gauld at yahoo.co.uk Mon Oct 15 05:02:33 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 15 Oct 2018 10:02:33 +0100 Subject: [Tutor] python game error In-Reply-To: References: Message-ID: On 15/10/18 08:57, Peter Otten wrote: > By the way, you do not need a map (dict) at all to implement a game like > this, you may return the next scene directly. A sketch: > > class Bridge: > def enter(self): > ... > action = ... > if action == "jump off the bridge": > return Death("You are eaten by the piranhas") > else: > ... That was my initial thought when I saw this but thee is one caveat. The original design creates a single instance of a scene and returns that on each access. The suggestion above creates a new instance on every call. So if it is important to use the same instance each time then the map is a better solution. (Although you could instead create a class variable holding the first instance of itself then use a class constructor to either create the instance or access the class variable...) -- 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 Mon Oct 15 05:27:13 2018 From: __peter__ at web.de (Peter Otten) Date: Mon, 15 Oct 2018 11:27:13 +0200 Subject: [Tutor] python game error References: Message-ID: Alan Gauld via Tutor wrote: > On 15/10/18 08:57, Peter Otten wrote: > >> By the way, you do not need a map (dict) at all to implement a game like >> this, you may return the next scene directly. A sketch: >> >> class Bridge: >> def enter(self): >> ... >> action = ... >> if action == "jump off the bridge": >> return Death("You are eaten by the piranhas") >> else: >> ... > > That was my initial thought when I saw this but thee is one > caveat. The original design creates a single instance of a > scene and returns that on each access. The suggestion above > creates a new instance on every call. So if it is important > to use the same instance each time then the map is a > better solution. > > (Although you could instead create a class variable holding > the first instance of itself then use a class constructor > to either create the instance or access the class variable...) > Or, to keep it really simple, return global class instances: DEATH_BY_TORNADO = Death("The storm swept you off the bridge") >> class Bridge: >> def enter(self): >> ... >> action = ... if action == "tornado": return DEATH_BY_TORNADO One advantage of both variants is that tools like pylint are likely to catch spelling errors like the one that prompted the initial question. From sjeik_appie at hotmail.com Mon Oct 15 06:58:05 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Mon, 15 Oct 2018 10:58:05 +0000 Subject: [Tutor] what does the forward slash mean in this function signature? Message-ID: Hello, In Python 3.6 (Windows) I often see a forward slash in a function signature, see below. What does it mean? I vaguely remember reading something about new possbilities in python 3, something like "def foo(x, *, y)". Perhaps it's related to that? >>> help({}.fromkeys) Help on built-in function fromkeys: fromkeys(iterable, value=None, /) method of builtins.type instance Returns a new dict with keys from iterable and values equal to value. Thanks! Albert-Jan From eryksun at gmail.com Mon Oct 15 08:10:03 2018 From: eryksun at gmail.com (eryk sun) Date: Mon, 15 Oct 2018 07:10:03 -0500 Subject: [Tutor] what does the forward slash mean in this function signature? In-Reply-To: References: Message-ID: On Mon, Oct 15, 2018 at 6:00 AM Albert-Jan Roskam wrote: > > In Python 3.6 (Windows) I often see a forward slash in a function signature, see below. > What does it mean? I vaguely remember reading something about new possbilities in > python 3, something like "def foo(x, *, y)". Perhaps it's related to that? > > >>> help({}.fromkeys) > Help on built-in function fromkeys: > > fromkeys(iterable, value=None, /) method of builtins.type instance > Returns a new dict with keys from iterable and values equal to value. This syntax is for position-only function parameters. It's used by Argument Clinic [1], a preprocessor used internally to develop CPython. Position-only parameters have also been proposed for the language grammar, first in PEP 457 and again in PEP 570 [2]. [1]: https://docs.python.org/3/howto/clinic.html#converting-your-first-function [2]: https://www.python.org/dev/peps/pep-0570 From mariamhaji01 at gmail.com Mon Oct 15 22:38:05 2018 From: mariamhaji01 at gmail.com (Mariam Haji) Date: Mon, 15 Oct 2018 19:38:05 -0700 Subject: [Tutor] Statistics with python In-Reply-To: References: Message-ID: Hi, I think this makes more sense to me to get the std. I will go with this route since there is a number of age groups and not just 56. Thanks guys for all your help. >>> import numpy >>> ages = [35, 45, 55, 70] >>> numpy.mean(ages) 51.25 >>> numpy.std(ages) 12.93010054098575 On Sun, Oct 14, 2018 at 1:20 PM Oscar Benjamin wrote: > I'm replying back to the tutor list. Can you reply there rather than > directly to me please? > > Also I've moved your response below mine as that is the preferred > style on this list. My answer is below. > > On Sun, 14 Oct 2018 at 16:05, Mariam Haji wrote: > > > > On Sat, Oct 13, 2018 at 10:24 PM Oscar Benjamin < > oscar.j.benjamin at gmail.com> wrote: > >> > >> On Sat, 13 Oct 2018 at 11:23, Mariam Haji > wrote: > >> > > >> > Hi guys, > >> > >> Hi Mariam > >> > >> > the question is as: > >> > If a sample of 50 patients is taken from a dataset what is the > probability > >> > that we will get a patient above the age of 56? > >> > >> I can think of several ways of interpreting this: > >> > >> (a): You have a dataset consisting of 50 patients. You want to know > >> the probability that a patient chosen from that sample will be above > >> the age of 56. > >> > >> (b): You have a dataset consisting of 50 patients. You consider it to > >> be representative of a larger population of people. You would like to > >> use your dataset to estimate the probability that a patient chosen > >> from the larger population will be above the age of 56. > >> > >> (c): You have a larger dataset consisting of more than 50 patients. > >> You want to know that probability that a sample of 50 patients chosen > >> from the larger dataset will contain at least (or exactly?) one person > >> above the age of 56. > >> > >> (d): You have a larger dataset, but you will only analyse a sample of > >> 50 patients from it. You want to use statistics on that sample to > >> estimate the probability that a patient chosen from the larger dataset > >> will be above the age of 56. > >> > >> I can list more interpretations but I think it would be better to wait > >> for you to clarify. > > > > My dataset consists of 300+ patients and I want to analyze analyse a > sample of 50 patients from it. > > Yto know the probability that a patient chosen from the larger dataset > > will be above the age of 56. > > Is this a homework problem or an actual problem? > > If I had 300+ patients I would think that the best way to work out the > probability that a patient chosen from those 300+ was over the age of > 56 would be to count how many are over the age of 56. Likewise if I > wanted to estimate how many would be over the age of 56 using a > smaller sample of 50 patients then I would also just count how many > are over the age of 56 in that smaller sample. > > I'm going to guess that this is a homework problem and that you have > been asked to assume that the ages are normally distributed (which > they would not be in reality). > > Your calculation for the standard deviation given in your earlier > email doesn't make any sense. You should calculate this using a > function that calculates the standard deviation. There is one in the > numpy module: > > >>> import numpy > >>> ages = [35, 45, 55, 70] > >>> numpy.mean(ages) > 51.25 > >>> numpy.std(ages) > 12.93010054098575 > > -- > Oscar > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- *Regards,* *Mariam.* From rls4jc at gmail.com Mon Oct 15 18:11:12 2018 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Mon, 15 Oct 2018 15:11:12 -0700 Subject: [Tutor] pip install Message-ID: Hey all, I'm trying to set up a virtual environment. I've used Windows Powershell on my desktop with Windows 10. I was able only to get to 2.5 minutes of a 9.75 minute video before getting frozen, not just stuck. I was unable to activate the batch file even though everything appeared to be where it should be. I then found out about Linux Bash Shell for Windows and used that, I next successfully installed virtualenv, but when I went to run it it said command virtualenv not found but can be installed with sudo apt install virtualenv, which I did, then tried to run 'virtualenv pdxCrime' (without the apostrophes) and it errors out with command virtualenv not found, did you mean: command 'virtualenv' from deb virtualenv Try: sudo apt install My bash shell is Ubuntu not debian. Not sure if that makes a difference. I don't understand what is. I don't understand why virtualenv isn't found when it was successfully installed and every piece of documentation I've found tells me to use that word or command. The only reason I ask this forum is because the instructor of the video hasn't responded to my email yet and this seems to be related to python somehow although I'm a little foggy about that. If this is not the forum please tell me where to go (be nice). Thank you. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation From mats at wichmann.us Tue Oct 16 09:57:24 2018 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 16 Oct 2018 07:57:24 -0600 Subject: [Tutor] pip install In-Reply-To: References: Message-ID: On 10/15/2018 04:11 PM, Roger Lea Scherer wrote: > Hey all, > > I'm trying to set up a virtual environment. I've used Windows Powershell on > my desktop with Windows 10. I was able only to get to 2.5 minutes of a 9.75 > minute video before getting frozen, not just stuck. I was unable to > activate the batch file even though everything appeared to be where it > should be. I then found out about Linux Bash Shell for Windows and used > that, I next successfully installed virtualenv, but when I went to run it > it said command virtualenv not found but can be installed with sudo apt > install virtualenv, which I did, then tried to run 'virtualenv pdxCrime' > (without the apostrophes) and it errors out with > > command virtualenv not found, did you mean: > > command 'virtualenv' from deb virtualenv > > Try: sudo apt install > > My bash shell is Ubuntu not debian. Not sure if that makes a difference. Ubuntu, Debian, Linux Mint, and a number of others share the characteristic that they use the "Debian Package Manager" dpkg, and the packages themselves are colloquially called "debs". So no, it doesn't make a difference. > > I don't understand what is. I don't understand why virtualenv > isn't found when it was successfully installed and every piece of > documentation I've found tells me to use that word or command. > > The only reason I ask this forum is because the instructor of the video > hasn't responded to my email yet and this seems to be related to python > somehow although I'm a little foggy about that. If this is not the forum > please tell me where to go (be nice). > > Thank you. > Try this page in the standard docs - you don't necessarily even have to use the command named 'virtualenv': https://docs.python.org/3/library/venv.html From sjeik_appie at hotmail.com Tue Oct 16 11:04:04 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Tue, 16 Oct 2018 15:04:04 +0000 Subject: [Tutor] what does the forward slash mean in this function signature? In-Reply-To: References: , Message-ID: From: eryk sun Sent: Monday, October 15, 2018 12:10 PM To: tutor at python.org Cc: sjeik_appie at hotmail.com Subject: Re: [Tutor] what does the forward slash mean in this function signature? ? On Mon, Oct 15, 2018 at 6:00 AM Albert-Jan Roskam wrote: > > In Python 3.6 (Windows) I often see a forward slash in a function signature, see below. > What does it mean? I vaguely remember reading something about new possbilities in > python 3, something like "def foo(x, *, y)". Perhaps it's related to that? > > >>> help({}.fromkeys) > Help on built-in function fromkeys: > > fromkeys(iterable, value=None, /) method of builtins.type instance >???? Returns a new dict with keys from iterable and values equal to value. This syntax is for position-only function parameters. It's used by Argument Clinic [1], a preprocessor used internally to develop CPython. Position-only parameters have also been proposed for the language grammar, first in PEP 457 and again in PEP 570 [2]. [1]: https://docs.python.org/3/howto/clinic.html#converting-your-first-function [2]: https://www.python.org/dev/peps/pep-0570 ========> Hi Eryk, Thank you, that's interesting to know. I wasn't even aware of position-only parameters until I read about them. Of course I've used ord() many times, I just thought it has a keyword argument. Why even bother to have positional-only params? I agree that ord('a') reads more easily than ord(character='a'), but other than that I don't see an advantage to enforce it. The pages you mentioned also made me read about function annotations and type hints. Do you know why type hints were designed while there are already are parseable docstring notations (sphinx :param foo: int, numpydoc (much prettier IMHO), and others)? The type hints remind of Cython, but "Type annotations should not be confused with variable declarations in statically typed languages. The goal of annotation syntax is to provide an easy way to specify structured type metadata for third party tools." [1] Thanks! Albert-Jan [1] https://www.python.org/dev/peps/pep-0526/ From noflaco at gmail.com Tue Oct 16 13:29:58 2018 From: noflaco at gmail.com (Carlton Banks) Date: Tue, 16 Oct 2018 19:29:58 +0200 Subject: [Tutor] pip install In-Reply-To: References: Message-ID: I never had any success with Windows and python, and even the Ubuntu shell which Windows do offer as a feature isnt that good in my opinion. But a big googling gave me this tutorial.. http://timmyreilly.azurewebsites.net/python-pip-virtualenv-installation-on-windows Might be bit more useful ? 16. okt. 2018 10.31 AM skrev "Roger Lea Scherer" : Hey all, I'm trying to set up a virtual environment. I've used Windows Powershell on my desktop with Windows 10. I was able only to get to 2.5 minutes of a 9.75 minute video before getting frozen, not just stuck. I was unable to activate the batch file even though everything appeared to be where it should be. I then found out about Linux Bash Shell for Windows and used that, I next successfully installed virtualenv, but when I went to run it it said command virtualenv not found but can be installed with sudo apt install virtualenv, which I did, then tried to run 'virtualenv pdxCrime' (without the apostrophes) and it errors out with command virtualenv not found, did you mean: command 'virtualenv' from deb virtualenv Try: sudo apt install My bash shell is Ubuntu not debian. Not sure if that makes a difference. I don't understand what is. I don't understand why virtualenv isn't found when it was successfully installed and every piece of documentation I've found tells me to use that word or command. The only reason I ask this forum is because the instructor of the video hasn't responded to my email yet and this seems to be related to python somehow although I'm a little foggy about that. If this is not the forum please tell me where to go (be nice). Thank you. -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From sydney.shall at kcl.ac.uk Wed Oct 17 10:18:00 2018 From: sydney.shall at kcl.ac.uk (Shall, Sydney) Date: Wed, 17 Oct 2018 15:18:00 +0100 Subject: [Tutor] problem with creating paths Message-ID: I am a novice (at programming). I use MAC OS 10.13.6 Anaconda. Python 3.5.4 Spyder 3.5.6 I have just re-written a moderately complex program (a Class) on the advice of Alan and Steven. The rewriting proved to be very useful. The working program uses instances of the Class with User chosen parameters. The output data seems correct to me. So I then began redoing all the tests. The first Methods tested gave OK. But I have just started testing a new Method and I get a Universal error in my tests. It says that the output file is already present. These files (paths) are correctly deleted by the 'teardown' Method, when only the earlier portion of the program is tested. But are not deleted with the last method tested. After searching I have found this unexpected output illustrated in the copy-paste below. .... test The type of the paths is : The values of the paths are : ( [ '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/', '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/20181017D', '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/20181017D/A_POCI_Input_Data', . . . . '/Users/sydney/AnacondaProjects/reproduction/Current_Version /Results/20181017D/B_Cycle_Zero/Text_Files', . . . '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/20181017D/C_Final_Results/Plots/Population_Data/Ratios'], '/Users/sydney/.Trash/20181017D/B_Cycle_Zero/Text_Files') .... There are two items that are 'wrong' in this output. 1. The property 'paths' is defined in the program as a list and the items are added using paths.append(), yet the test says that when tested it is a tuple. 2. The tuple arises by the addition of the last entry in the file, AFTER the closing bracket of the list which is the first item in the tuple. When I test the length of 'paths' I get a value of 2! I apologise for the lengthy explanation, but I am at a loss. I have looked for an error that might have added an item as a + and I find nothing. The character of the final item is also puzzling to me. I would much appreciate any guidance as to how I should search for the fault or error. Sydney _________ From steve at pearwood.info Wed Oct 17 10:39:53 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 18 Oct 2018 01:39:53 +1100 Subject: [Tutor] problem with creating paths In-Reply-To: References: Message-ID: <20181017143953.GP3817@ando.pearwood.info> On Wed, Oct 17, 2018 at 03:18:00PM +0100, Shall, Sydney via Tutor wrote: [...] > After searching I have found this unexpected output illustrated in the > copy-paste below. > > .... > test > > > The type of the paths is : Believe Python when it tells you something is a tuple. Trust me, it knows! > The values of the paths are : > > > ([ > '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/', > '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/20181017D', > '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/20181017D/A_POCI_Input_Data', [...] > '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/20181017D/B_Cycle_Zero/Text_Files', [...] > '/Users/sydney/AnacondaProjects/reproduction/Current_Version/Results/20181017D/C_Final_Results/Plots/Population_Data/Ratios'], > '/Users/sydney/.Trash/20181017D/B_Cycle_Zero/Text_Files') Here is a good trick for debugging. Start by simplifying your data, to make it easier to see the needle in the haystack. Long paths like the above present you with a wall of text, so (temporarily) replace each path with a single character to cut down on the visual complexity. That makes it easier to see what is going on. Replacing each path with a single letter gives me: (['a', 'b', 'c', [...] 'x', 'y'], 'z') Notice that your value is a tuple of two items: Item One is a list, ['a', 'b', 'c', [...] 'x', 'y'] Item Two is a string, 'z' > There are two items that are 'wrong' in this output. > > 1. The property 'paths' is defined in the program as a list and the > items are added using paths.append(), yet the test says that when tested > it is a tuple. > 2. The tuple arises by the addition of the last entry in the file, AFTER > the closing bracket of the list which is the first item in the tuple. > > When I test the length of 'paths' I get a value of 2! That's because it is a tuple of two items. > I apologise for the lengthy explanation, but I am at a loss. > > I have looked for an error that might have added an item as a + and I > find nothing. Without seeing your code, there's no way of telling how you constructed this value. You intended a list, and built a list (somehow!), but then you did *something* to replace it with a tuple. Perhaps you did: paths = [] for some_path in something_or_rather: paths.append(some_path) then later on: paths = (paths, another_path) but there's a million ways you could have got the same result. And of course you could have used any variable name... I'm assuming it is called "paths", but you should substitute whatever name (or names!) you actually used. > I would much appreciate any guidance as to how I should search for the > fault or error. Start by looking for any line of code that starts with: paths = and see if and where you replaced the list with a tuple. If that gets you nowhere, start looking for *every* reference to "paths" and see what they do. If *that* gets you nowhere, start adding debugging code to your program. Put assertions like this: assert isinstance(paths, list) in various parts of the code, then run the program and see where it fails. That tells you that *at that point* paths is no longer a list. E.g. something like this: # build the paths... paths = [] for blah blah blah blah: paths.append(whatever) assert isinstance(paths, list) # line 50 (say) do_this() do_that() assert isinstance(paths, list) # line 53 do_something_else() and_another_thing() assert isinstance(paths, list) # line 56 If the first two assertions on line 50 and 53 pass, but the third at line 56 fails, you know that the bug is introduced somewhere between line 53 and 56. -- Steve From sydney.shall at kcl.ac.uk Wed Oct 17 10:48:32 2018 From: sydney.shall at kcl.ac.uk (Shall, Sydney) Date: Wed, 17 Oct 2018 15:48:32 +0100 Subject: [Tutor] problem with creating paths Message-ID: <17f0f5af-4824-272a-0fed-13d498f10da0@kcl.ac.uk> I can now add to my previous email the following observation. If I do not delete the output file and redo the test I get the following as the 'extra' entry in paths: '/Users/sydney/AnacondaProjects/capital_reproduction/Current_Version/Results/20181017D/B_Cycle_Zero/Text_Files') If however, I delete the output file and then redo the test I get the following as the 'extra' entry in paths: '/Users/sydney/.Trash/20181017D/B_Cycle_Zero/Text_Files') This seems to be consistent. The upper incorrect entry is item path19 in the list part of paths. I have studied every example of 'path19' in the program and I cannot find an explanation. help! Sydney _________ Professor Sydney Shall Department of Haematology/Oncology Phone: +(0)2078489200 E-Mail: sydney.shall [Correspondents outside the College should add @kcl.ac.uk] From __peter__ at web.de Wed Oct 17 10:58:04 2018 From: __peter__ at web.de (Peter Otten) Date: Wed, 17 Oct 2018 16:58:04 +0200 Subject: [Tutor] problem with creating paths References: Message-ID: Shall, Sydney via Tutor wrote: > There are two items that are 'wrong' in this output. > > 1. The property 'paths' is defined in the program as a list and the > items are added using paths.append(), yet the test says that when tested > it is a tuple. >>> paths = ["foo", "bar"], >>> paths += "baz", # ("baz",) leads to the same result >>> paths (['foo', 'bar'], 'baz') This might happen if you have accidental trailing commas in two places, but that seems rather unlikely. Maybe you have a function with a star def f(*args): ... # args is a tuple, even if you pass one argument or no arguments # to f(). ? > 2. The tuple arises by the addition of the last entry in the file, AFTER > the closing bracket of the list which is the first item in the tuple. Sorry, I don't understand that sentence. > When I test the length of 'paths' I get a value of 2! That's because you have a tuple with two entries, one list and one string. > I apologise for the lengthy explanation, but I am at a loss. > > I have looked for an error that might have added an item as a + and I > find nothing. > The character of the final item is also puzzling to me. > > I would much appreciate any guidance as to how I should search for the > fault or error. Look at the assignments. If you find the pattern I mentioned above remove the commas. If you don't see anything suspicious make a test script containing only the manipulation of the paths variable. Make sure it replicates the behaviour of the bigger script; then show it to us. From sydney.shall at kcl.ac.uk Wed Oct 17 12:07:12 2018 From: sydney.shall at kcl.ac.uk (Shall, Sydney) Date: Wed, 17 Oct 2018 17:07:12 +0100 Subject: [Tutor] problem with creating paths In-Reply-To: References: Message-ID: Firstly, I would like to thank Steven for reminding me of the assert statement. I should have remembered this. It allowed me to isolate the problem, which predictably (for me) was very elementary. I am too embarrassed to say how simple the error was. However, my original problem was not solved by correcting this error. So, I will now try and narrow down the location of the problem and then if I cannot solve it, I shall return for more good advice. Many thanks to Steven and to Peter. Sydney From mats at wichmann.us Wed Oct 17 13:18:17 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 17 Oct 2018 11:18:17 -0600 Subject: [Tutor] problem with creating paths In-Reply-To: References: Message-ID: On 10/17/2018 10:07 AM, Shall, Sydney via Tutor wrote: > Firstly, I would like to thank Steven for reminding me of the assert > statement. I should have remembered this. It allowed me to isolate the > problem, which predictably (for me) was very elementary. I am too > embarrassed to say how simple the error was. > > However, my original problem was not solved by correcting this error. > > So, I will now try and narrow down the location of the problem and then > if I cannot solve it, I shall return for more good advice. > > Many thanks to Steven and to Peter. I'll weigh in with a mini- (and unasked-for-) lecture here: this is often the point at which someone says "boy, I wish Python were strongly typed, so things didn't change types in flight". But in fact, the list didn't change types, it's still a list. In fact we even know where that list is: it's the first element of that tuple you ended up with. The _name_ you gave to that list carries no typing meaning, however (although you can give it type hints that an external tool could use to warn you that you are changing something). So you have somewhere given that name to a completely different object, a tuple which contains your list and another element. So clearly what you're looking for is the place that happens. So here's a sketch of how you might use type hinting to find this, to bring it back to something practical: === types.py: from typing import List, Tuple a: List[str] = [ '/a/path', '/b/path', ] print(type(a)) print(a) a = (a, '/c/path') print(type(a)) print(a) === this works just fine: $ python3 types.py ['/a/path', '/b/path'] (['/a/path', '/b/path'], '/c/path') === but a hinting tool can see a possible issue: $ mypy types.py types.py:10: error: Incompatible types in assignment (expression has type "Tuple[List[str], str]", variable has type "List[str]") From sydney.shall at kcl.ac.uk Wed Oct 17 13:21:18 2018 From: sydney.shall at kcl.ac.uk (Shall, Sydney) Date: Wed, 17 Oct 2018 18:21:18 +0100 Subject: [Tutor] problem with creating paths In-Reply-To: References: Message-ID: On 17/10/2018 18:18, Mats Wichmann wrote: > On 10/17/2018 10:07 AM, Shall, Sydney via Tutor wrote: >> Firstly, I would like to thank Steven for reminding me of the assert >> statement. I should have remembered this. It allowed me to isolate the >> problem, which predictably (for me) was very elementary. I am too >> embarrassed to say how simple the error was. >> >> However, my original problem was not solved by correcting this error. >> >> So, I will now try and narrow down the location of the problem and then >> if I cannot solve it, I shall return for more good advice. >> >> Many thanks to Steven and to Peter. > > I'll weigh in with a mini- (and unasked-for-) lecture here: > > this is often the point at which someone says "boy, I wish Python were > strongly typed, so things didn't change types in flight". > > But in fact, the list didn't change types, it's still a list. In fact we > even know where that list is: it's the first element of that tuple you > ended up with. The _name_ you gave to that list carries no typing > meaning, however (although you can give it type hints that an external > tool could use to warn you that you are changing something). So you > have somewhere given that name to a completely different object, a tuple > which contains your list and another element. So clearly what you're > looking for is the place that happens. > > So here's a sketch of how you might use type hinting to find this, to > bring it back to something practical: > > > === types.py: > from typing import List, Tuple > > a: List[str] = [ > '/a/path', > '/b/path', > ] > print(type(a)) > print(a) > > a = (a, '/c/path') > print(type(a)) > print(a) > > === this works just fine: > $ python3 types.py > > ['/a/path', '/b/path'] > > (['/a/path', '/b/path'], '/c/path') > > === but a hinting tool can see a possible issue: > $ mypy types.py > types.py:10: error: Incompatible types in assignment (expression has > type "Tuple[List[str], str]", variable has type "List[str]") Thanks for this. Most helpful. Sydney > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=01%7C01%7Csydney.shall%40kcl.ac.uk%7Ca58896d72c374cda3c1508d63454b783%7C8370cf1416f34c16b83c724071654356%7C0&sdata=OHZtedYdy0UHKDagLO1TI%2BUIjEJuzRjZjD4HRdSQmNI%3D&reserved=0 > -- _________ Professor Sydney Shall Department of Haematology/Oncology Phone: +(0)2078489200 E-Mail: sydney.shall [Correspondents outside the College should add @kcl.ac.uk] From breamoreboy at gmail.com Wed Oct 17 15:10:51 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Wed, 17 Oct 2018 20:10:51 +0100 Subject: [Tutor] problem with creating paths In-Reply-To: References: Message-ID: On 17/10/18 18:18, Mats Wichmann wrote: > On 10/17/2018 10:07 AM, Shall, Sydney via Tutor wrote: >> Firstly, I would like to thank Steven for reminding me of the assert >> statement. I should have remembered this. It allowed me to isolate the >> problem, which predictably (for me) was very elementary. I am too >> embarrassed to say how simple the error was. >> >> However, my original problem was not solved by correcting this error. >> >> So, I will now try and narrow down the location of the problem and then >> if I cannot solve it, I shall return for more good advice. >> >> Many thanks to Steven and to Peter. > > I'll weigh in with a mini- (and unasked-for-) lecture here: > > this is often the point at which someone says "boy, I wish Python were > strongly typed, so things didn't change types in flight". > This is where I say Python *IS* strongly, although dynamically typed. Why do people have such a problem with this? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From stephen.m.smith at comcast.net Wed Oct 17 17:25:17 2018 From: stephen.m.smith at comcast.net (Stephen Smith) Date: Wed, 17 Oct 2018 17:25:17 -0400 Subject: [Tutor] Performance Issue Message-ID: <00b901d4665f$e71c60f0$b55522d0$@comcast.net> I have written a screen scraping program that watches a clock (on the app's server) and at 7:00:00 AM dashes to make a reservation on line. It works fine. However, i have spent time trying to improve its performance. I am using selenium, with chrome driver. Here is what i have learned. I have tried various methods to find (by link_text, by_xpath, etc.) and click on the element in question (shown below). When i find the element with no click, the find process takes about .02 seconds. When i find it with a click (i need to select the element and move to the next iframe) it takes over a second. I get these same results no matter which find_element_by variation i use and i get the same times in headless or normal mode. Here is my theory - finding the element is relatively simple in the html already loaded into my machine - hence .02 seconds. However, when i click on the element, processing goes out to the server which does some stuff and i get a new iframe displayed, all of which takes time. So i have sort of concluded that perhaps I can't take a big chunk of that time out (literally the same statement without the click option takes 2% of the time), but am hoping perhaps someone has another thought. I had thought maybe i could jump to the second ifame locally, but can't see a way to do this. I also have considered something other than selenium, but since i think the problem lies on the server side, not sure it is worth the time. Thanks in advance for any ideas. The program is quite large, but here is the relevant section: # Back from NAP - REALLY CLOSE to 7:00 AM select the date desired and go to the next page (really iframe) - using prepared xpath try: br.find_element_by_link_text(str(day_to_book)).click() #sleep(refresh_factor) except NoSuchElementException: self.queue.put("- (" + thread + ") Attempted date selection too early? " + str(datetime.datetime.now()\ + datetime.timedelta(seconds = second_difference))[-11:-4]) Return Here is the relevant html (in this case I have copied the html for the 31st of the month, but all dates look the same which is why find_element_by_link_text [with day_to_book = 31) is easy to use. Again, my code works fine - I am trying to see if there is a way to improve performance with some trick I can't come up with. From alan.gauld at yahoo.co.uk Wed Oct 17 20:12:28 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 Oct 2018 01:12:28 +0100 Subject: [Tutor] Performance Issue In-Reply-To: <00b901d4665f$e71c60f0$b55522d0$@comcast.net> References: <00b901d4665f$e71c60f0$b55522d0$@comcast.net> Message-ID: On 17/10/18 22:25, Stephen Smith wrote: > I have written a screen scraping program that watches a clock (on the app's > server) and at 7:00:00 AM dashes to make a reservation on line. It works > fine. However, i have spent time trying to improve its performance. I am > using selenium, with chrome driver. When doing performance tuning the first thing to answer is what does improved performance mean. For example in a Word Processor improving the speed that an input character appears on screen by 10% is unlikely to be a worthwhile exercise. But improving the time taken to do a global search/replace by 10% might well be worthwhile. So what do you want to improve about an app that spends most of its time waiting for a change on a remote server (presumably by polling?) Is it the speed/frequency of polling? The speed of reading the response? The speed of processing the response? And knowing what you want to improve have you measured it to see where the time is being spent? Is it in the client request? The transmission to the server? the server processing? the transmission from the server? the reading of that response? or the processing of that response? You need to time each of those phases accurately to find out which bits are worth improving. > Here is what i have learned. I have tried various methods to find (by > link_text, by_xpath, etc.) and click on the element in question (shown > below). When i find the element with no click, the find process takes about > .02 seconds. When i find it with a click (i need to select the element and > move to the next iframe) it takes over a second. I get these same results no > matter which find_element_by variation i use and i get the same times in > headless or normal mode. > > Here is my theory - finding the element is relatively simple in the html > already loaded into my machine - hence .02 seconds. However, when i click on > the element, processing goes out to the server which does some stuff and i > get a new iframe displayed, all of which takes time. Absolutely. network access is likely to be measured in 10ths of a second rather than hundredths. And processing the request may well entail a server database call (which may itself be on a separate machine from the web server with a corresponding LAN message delay), then there's the creation and transmission of the HTML (unless your server provides an API with JSON responses - but then you don't need clicks etc!) And iFrames make that worse since every iframe effectively gets treated as a separate html document. Then when your client receives the data it has to reparse the html into a document structure before performing the search. > concluded that perhaps I can't take a big chunk of that time out You probably can, but only if you have access to the server code and the network infrastructure and deep enough pockets for a server upgrade or a new proxy server. Assuming that's not the case then no, you need to look at other options. But your first step has to be to measure the various stages of the request. If the problem lies in the transmission time across the network there is probably not much you can do. If its in the database access (trickier to measure if you don't have the server code - you need to create some simultaneous equations using multiple test scenarios) then you might be able to construct better queries (eg look at a different page or only query the target iframe). > considered something other than selenium, but since i think the problem lies > on the server side, not sure it is worth the time. It depends on the nature of the page. The best solution, by far, is not to do web scraping. Its always the worst case solution and to be avoided if at all possible. Try to find an API with JSON or XML responses. Also, are you sure you need to use the clock on the page? Isn't the server clock adequate? In which case the response time should be in every message header so there's no need for web scraping at all... Finally, I think there is an active Selenium discussion forum so you could try there for more ideas. -- 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 Thu Oct 18 06:53:13 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 Oct 2018 11:53:13 +0100 Subject: [Tutor] Performance Issue In-Reply-To: <00d501d466c6$8f38f760$adaae620$@comcast.net> Message-ID: Cc'ing list. Please use reply all on responses to tutor. If you have no control over the server, eh access to logs etc, then the best you can do it record the time just before sending the request and immediately you get the reply. That part is outside your control. If the remaining time is worth optimising then look to your code. As to the server time, it should be in the http headers so you don't need to parse the html, just read the headers. Much faster. HRH, Alan g. On 18 Oct 2018 10:40 am, User2002 wrote: Thank you for your thoughtful reply. There are some good ideas in there for me. I have asked and there either is no API or they do not want outsiders to have access to it, so I think that is a dead end. For my purposes, improved performance focuses on significantly reducing the time required to successfully execute the br.find_element_by_link_text(str(day_to_book)).click() command. If my overall time to successfully book a reservation is 1.8 seconds and roughly 2 seconds is spent on this single instruction, then a .5 second improvement represents a 25% reduction. I have a second command (same type, just on the next iframe) that is similarly slow. So fixing both could represent a 50% reduction. Given the demand for the reservations (there are literally hundreds of people out there pounding their keyboard/clicking on fields) every second counts. (With an automated ability to book a reservation, I am probably faster than anyone's ability to click on a field, wait for a reply, reposition the cursor, click again, etc., but I am the point where this has become something I am fully invested in and would like to take as far as I can.) Most of your ideas center around the notion of knowing more about where the time delay occurs in the processing steps that occur outside on my world - communication back and forth to the server, etc. I must confess, I have no idea of how to do this. How can I measure what goes on outside my machine and measure the component parts? If you have an idea in this area or could refer me to where I could go to read and learn, I'd be very grateful. Finally, regarding your notion of web scraping, server clock, etc. Literally the only thing I 'scrape' is the server time to ensure I click on the date field at exactly 7:00:00. Once I get to that point, I click on a date field, then I click on a time field and I am done - no scraping occurs once it reaches 7:00:00. So I am not sure there are improvements to be made in that area. -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Wednesday, October 17, 2018 8:12 PM To: tutor at python.org Subject: Re: [Tutor] Performance Issue On 17/10/18 22:25, Stephen Smith wrote: > I have written a screen scraping program that watches a clock (on the > app's > server) and at 7:00:00 AM dashes to make a reservation on line. It > works fine. However, i have spent time trying to improve its > performance. I am using selenium, with chrome driver. When doing performance tuning the first thing to answer is what does improved performance mean. For example in a Word Processor improving the speed that an input character appears on screen by 10% is unlikely to be a worthwhile exercise. But improving the time taken to do a global search/replace by 10% might well be worthwhile. So what do you want to improve about an app that spends most of its time waiting for a change on a remote server (presumably by polling?) Is it the speed/frequency of polling? The speed of reading the response? The speed of processing the response? And knowing what you want to improve have you measured it to see where the time is being spent? Is it in the client request? The transmission to the server? the server processing? the transmission from the server? the reading of that response? or the processing of that response? You need to time each of those phases accurately to find out which bits are worth improving. > Here is what i have learned. I have tried various methods to find (by > link_text, by_xpath, etc.) and click on the element in question (shown > below). When i find the element with no click, the find process takes > about > .02 seconds. When i find it with a click (i need to select the element > and move to the next iframe) it takes over a second. I get these same > results no matter which find_element_by variation i use and i get the > same times in headless or normal mode. > > Here is my theory - finding the element is relatively simple in the > html already loaded into my machine - hence .02 seconds. However, when > i click on the element, processing goes out to the server which does > some stuff and i get a new iframe displayed, all of which takes time. Absolutely. network access is likely to be measured in 10ths of a second rather than hundredths. And processing the request may well entail a server database call (which may itself be on a separate machine from the web server with a corresponding LAN message delay), then there's the creation and transmission of the HTML (unless your server provides an API with JSON responses - but then you don't need clicks etc!) And iFrames make that worse since every iframe effectively gets treated as a separate html document. Then when your client receives the data it has to reparse the html into a document structure before performing the search. > concluded that perhaps I can't take a big chunk of that time out You probably can, but only if you have access to the server code and the network infrastructure and deep enough pockets for a server upgrade or a new proxy server. Assuming that's not the case then no, you need to look at other options. But your first step has to be to measure the various stages of the request. If the problem lies in the transmission time across the network there is probably not much you can do. If its in the database access (trickier to measure if you don't have the server code - you need to create some simultaneous equations using multiple test scenarios) then you might be able to construct better queries (eg look at a different page or only query the target iframe). > considered something other than selenium, but since i think the > problem lies on the server side, not sure it is worth the time. It depends on the nature of the page. The best solution, by far, is not to do web scraping. Its always the worst case solution and to be avoided if at all possible. Try to find an API with JSON or XML responses. Also, are you sure you need to use the clock on the page? Isn't the server clock adequate? In which case the response time should be in every message header so there's no need for web scraping at all... Finally, I think there is an active Selenium discussion forum so you could try there for more ideas. -- 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 user2002 at comcast.net Thu Oct 18 07:08:42 2018 From: user2002 at comcast.net (User2002) Date: Thu, 18 Oct 2018 07:08:42 -0400 Subject: [Tutor] Performance Issue In-Reply-To: References: <00d501d466c6$8f38f760$adaae620$@comcast.net> Message-ID: <00df01d466d2$eecd1510$cc673f30$@comcast.net> Thanks to all for your indulgence and help? From: Alan Gauld Sent: Thursday, October 18, 2018 6:53 AM To: User2002 Cc: tutor at python.org Subject: RE: [Tutor] Performance Issue Cc'ing list. Please use reply all on responses to tutor. If you have no control over the server, eh access to logs etc, then the best you can do it record the time just before sending the request and immediately you get the reply. That part is outside your control. If the remaining time is worth optimising then look to your code. As to the server time, it should be in the http headers so you don't need to parse the html, just read the headers. Much faster. HRH, Alan g. On 18 Oct 2018 10:40 am, User2002 > wrote: Thank you for your thoughtful reply. There are some good ideas in there for me. I have asked and there either is no API or they do not want outsiders to have access to it, so I think that is a dead end. For my purposes, improved performance focuses on significantly reducing the time required to successfully execute the br.find_element_by_link_text(str(day_to_book)).click() command. If my overall time to successfully book a reservation is 1.8 seconds and roughly 2 seconds is spent on this single instruction, then a .5 second improvement represents a 25% reduction. I have a second command (same type, just on the next iframe) that is similarly slow. So fixing both could represent a 50% reduction. Given the demand for the reservations (there are literally hundreds of people out there pounding their keyboard/clicking on fields) every second counts. (With an automated ability to book a reservation, I am probably faster than anyone's ability to click on a field, wait for a reply, reposition the cursor, click again, etc., but I am the point where this has become something I am fully invested in and would like to take as far as I can.) Most of your ideas center around the notion of knowing more about where the time delay occurs in the processing steps that occur outside on my world - communication back and forth to the server, etc. I must confess, I have no idea of how to do this. How can I measure what goes on outside my machine and measure the component parts? If you have an idea in this area or could refer me to where I could go to read and learn, I'd be very grateful. Finally, regarding your notion of web scraping, server clock, etc. Literally the only thing I 'scrape' is the server time to ensure I click on the date field at exactly 7:00:00. Once I get to that point, I click on a date field, then I click on a time field and I am done - no scraping occurs once it reaches 7:00:00. So I am not sure there are improvements to be made in that area. -----Original Message----- From: Tutor > On Behalf Of Alan Gauld via Tutor Sent: Wednesday, October 17, 2018 8:12 PM To: tutor at python.org Subject: Re: [Tutor] Performance Issue On 17/10/18 22:25, Stephen Smith wrote: > I have written a screen scraping program that watches a clock (on the > app's > server) and at 7:00:00 AM dashes to make a reservation on line. It > works fine. However, i have spent time trying to improve its > performance. I am using selenium, with chrome driver. When doing performance tuning the first thing to answer is what does improved performance mean. For example in a Word Processor improving the speed that an input character appears on screen by 10% is unlikely to be a worthwhile exercise. But improving the time taken to do a global search/replace by 10% might well be worthwhile. So what do you want to improve about an app that spends most of its time waiting for a change on a remote server (presumably by polling?) Is it the speed/frequency of polling? The speed of reading the response? The speed of processing the response? And knowing what you want to improve have you measured it to see where the time is being spent? Is it in the client request? The transmission to the server? the server processing? the transmission from the server? the reading of that response? or the processing of that response? You need to time each of those phases accurately to find out which bits are worth improving. > Here is what i have learned. I have tried various methods to find (by > link_text, by_xpath, etc.) and click on the element in question (shown > below). When i find the element with no click, the find process takes > about > .02 seconds. When i find it with a click (i need to select the element > and move to the next iframe) it takes over a second. I get these same > results no matter which find_element_by variation i use and i get the > same times in headless or normal mode. > > Here is my theory - finding the element is relatively simple in the > html already loaded into my machine - hence .02 seconds. However, when > i click on the element, processing goes out to the server which does > some stuff and i get a new iframe displayed, all of which takes time. Absolutely. network access is likely to be measured in 10ths of a second rather than hundredths. And processing the request may well entail a server database call (which may itself be on a separate machine from the web server with a corresponding LAN message delay), then there's the creation and transmission of the HTML (unless your server provides an API with JSON responses - but then you don't need clicks etc!) And iFrames make that worse since every iframe effectively gets treated as a separate html document. Then when your client receives the data it has to reparse the html into a document structure before performing the search. > concluded that perhaps I can't take a big chunk of that time out You probably can, but only if you have access to the server code and the network infrastructure and deep enough pockets for a server upgrade or a new proxy server. Assuming that's not the case then no, you need to look at other options. But your first step has to be to measure the various stages of the request. If the problem lies in the transmission time across the network there is probably not much you can do. If its in the database access (trickier to measure if you don't have the server code - you need to create some simultaneous equations using multiple test scenarios) then you might be able to construct better queries (eg look at a different page or only query the target iframe). > considered something other than selenium, but since i think the > problem lies on the server side, not sure it is worth the time. It depends on the nature of the page. The best solution, by far, is not to do web scraping. Its always the worst case solution and to be avoided if at all possible. Try to find an API with JSON or XML responses. Also, are you sure you need to use the clock on the page? Isn't the server clock adequate? In which case the response time should be in every message header so there's no need for web scraping at all... Finally, I think there is an active Selenium discussion forum so you could try there for more ideas. -- 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 wpmartin at gmail.com Fri Oct 19 12:09:37 2018 From: wpmartin at gmail.com (Pat Martin) Date: Fri, 19 Oct 2018 09:09:37 -0700 Subject: [Tutor] (no subject) Message-ID: TLDR; How do you figure out if code is inefficient (if it isn't necessarily obvious) and how do you find a more efficient solution? I use code wars sometimes to get some practice with Python, there was a challenge to compare two strings and if string1 had enough characters to be rearranged to make string2 return True, otherwise return False. I wrote a script that was like this: for i in string1: if i not in string2: return False string2.replace(i,"",1) return True This worked but I kept getting that my function was too inefficient and it took too long. I did a search for the problem and found someone was using collections.Counter. This basically takes the string and returns the number of times each character occurs in it. Then just compare the count of one string to another to see if there is enough of each letter to make the other string. This seems like an elegant way to do it. My question is, how do I know something is inefficient and more importantly how do I go about finding a more efficient solution? I have just discovered dir() and it has really helped in finding methods for items but that doesn't help when finding actual packages especially if I don't know exactly what I am looking for. Sorry about the long email, not sure if there is even an answer to this question except maybe write more code and get more experience? Pat From wpmartin at gmail.com Fri Oct 19 12:12:54 2018 From: wpmartin at gmail.com (Pat Martin) Date: Fri, 19 Oct 2018 09:12:54 -0700 Subject: [Tutor] How to find optimisations for code Message-ID: Sorry my first email didn't have a subject line TLDR; How do you figure out if code is inefficient (if it isn't necessarily obvious) and how do you find a more efficient solution? I use code wars sometimes to get some practice with Python, there was a challenge to compare two strings and if string1 had enough characters to be rearranged to make string2 return True, otherwise return False. I wrote a script that was like this: for i in string1: if i not in string2: return False string2.replace(i,"",1) return True This worked but I kept getting that my function was too inefficient and it took too long. I did a search for the problem and found someone was using collections.Counter. This basically takes the string and returns the number of times each character occurs in it. Then just compare the count of one string to another to see if there is enough of each letter to make the other string. This seems like an elegant way to do it. My question is, how do I know something is inefficient and more importantly how do I go about finding a more efficient solution? I have just discovered dir() and it has really helped in finding methods for items but that doesn't help when finding actual packages especially if I don't know exactly what I am looking for. Sorry about the long email, not sure if there is even an answer to this question except maybe write more code and get more experience? Pat From mats at wichmann.us Fri Oct 19 12:40:06 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 19 Oct 2018 10:40:06 -0600 Subject: [Tutor] How to find optimisations for code In-Reply-To: References: Message-ID: On 10/19/2018 10:12 AM, Pat Martin wrote: > Sorry my first email didn't have a subject line > > TLDR; How do you figure out if code is inefficient (if it isn't necessarily > obvious) and how do you find a more efficient solution? I think you've hit it in your last sentence ("except maybe write more code and get more experience"): experience will let you recognize patterns. > I use code wars sometimes to get some practice with Python, there was a > challenge to compare two strings and if string1 had enough characters to be > rearranged to make string2 return True, otherwise return False. > > I wrote a script that was like this: > > for i in string1: > if i not in string2: > return False > string2.replace(i,"",1) > return True > > This worked but I kept getting that my function was too inefficient and it > took too long. I did a search for the problem and found someone was using > collections.Counter. This basically takes the string and returns the number > of times each character occurs in it. Then just compare the count of one > string to another to see if there is enough of each letter to make the > other string. This seems like an elegant way to do it. notwithstanding that the challenge is a little contrived... here's something you will come to recognize. You use the string replace method, which is described thus, pasting from the official docs: """ str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. """ Strings are not modifiable, so there is no replace-in-place. Each time you call string2.replace you build a new string... and then do nothing with that string, as you never assign a name to it. So that line clearly makes your code inefficient - you do some work that is just thrown away. And it's not clear what the purpose of this replacement is, anyway. Further it's not clear that you have the right answer. What if string1 contains one 'r' and string2 contains three? For the one 'r', the test that it is also in string2 succeeds... but nowhere do you find out that you actually needed to have three in order to be able to "rearrange to make string2". Solving this problem might benefit from thinking about tests first: if you write a test where you know the answer is going to be negative, a second where it is going to be positive, and a third that is a non-trivial instance of positive (like the multiple-letter count), then you have something to code your solution against. After it works, you can then think about refactoring: is there a better way? This will kind of naturally lead you to thinking in terms of efficiency. Lesson 2: for very many tasks, someone has already done it, and you can benefit by using some existing code, from the standard library or a module installed separately. That's often the difference between a programming challenge, which may want you to code it yourself to show you can, and real-world problem solving, where you are rewarded (in time) by efficiently reusing existing code. From __peter__ at web.de Fri Oct 19 13:02:06 2018 From: __peter__ at web.de (Peter Otten) Date: Fri, 19 Oct 2018 19:02:06 +0200 Subject: [Tutor] How to find optimisations for code References: Message-ID: Pat Martin wrote: > Sorry my first email didn't have a subject line > > TLDR; How do you figure out if code is inefficient (if it isn't > necessarily obvious) and how do you find a more efficient solution? > > I use code wars sometimes to get some practice with Python, there was a > challenge to compare two strings and if string1 had enough characters to > be rearranged to make string2 return True, otherwise return False. > > I wrote a script that was like this: > > for i in string1: > if i not in string2: > return False > string2.replace(i,"",1) > return True > > This worked but I kept getting that my function was too inefficient and it > took too long. I did a search for the problem and found someone was using > collections.Counter. This basically takes the string and returns the > number of times each character occurs in it. Then just compare the count > of one string to another to see if there is enough of each letter to make > the other string. This seems like an elegant way to do it. > > My question is, how do I know something is inefficient and more > importantly how do I go about finding a more efficient solution? > > I have just discovered dir() and it has really helped in finding methods > for items but that doesn't help when finding actual packages especially if > I don't know exactly what I am looking for. > > Sorry about the long email, not sure if there is even an answer to this > question except maybe write more code and get more experience? In Python my rule of thumb is: replace lists and loops with dict and set lookups whenever possible. One small challenge with this pattern is that not all loops look like a loop: char in some_string # "slow"; have to loop through the whole string char in set_of_chars # "fast"; calculate some hash value and then likely # jump to the right set entry From __peter__ at web.de Fri Oct 19 13:08:39 2018 From: __peter__ at web.de (Peter Otten) Date: Fri, 19 Oct 2018 19:08:39 +0200 Subject: [Tutor] How to find optimisations for code References: Message-ID: Mats Wichmann wrote: > On 10/19/2018 10:12 AM, Pat Martin wrote: >> Sorry my first email didn't have a subject line >> >> TLDR; How do you figure out if code is inefficient (if it isn't >> necessarily obvious) and how do you find a more efficient solution? > > I think you've hit it in your last sentence ("except maybe write more > code and get more experience"): experience will let you recognize > patterns. > >> I use code wars sometimes to get some practice with Python, there was a >> challenge to compare two strings and if string1 had enough characters to >> be rearranged to make string2 return True, otherwise return False. >> >> I wrote a script that was like this: >> >> for i in string1: >> if i not in string2: >> return False >> string2.replace(i,"",1) >> return True >> >> This worked but I kept getting that my function was too inefficient and >> it took too long. I did a search for the problem and found someone was >> using collections.Counter. This basically takes the string and returns >> the number of times each character occurs in it. Then just compare the >> count of one string to another to see if there is enough of each letter >> to make the other string. This seems like an elegant way to do it. > > notwithstanding that the challenge is a little contrived... here's > something you will come to recognize. You use the string replace > method, which is described thus, pasting from the official docs: > > """ > str.replace(old, new[, count]) > > Return a copy of the string with all occurrences of substring old > replaced by new. If the optional argument count is given, only the first > count occurrences are replaced. > """ > > Strings are not modifiable, so there is no replace-in-place. Each time > you call string2.replace you build a new string... and then do nothing > with that string, as you never assign a name to it. So that line clearly > makes your code inefficient - you do some work that is just thrown away. > And it's not clear what the purpose of this replacement is, anyway. This is probably retyped from memory. If the line were string2 = string2.replace(i,"",1) it would address your concern below about repeated chars. >>> def contains(s, t): ... for c in s: ... if c not in t: return False ... t = t.replace(c, "", 1) # remove one occurence of c from t ... return True ... >>> contains("ata", "attax") True >>> contains("tata", "attax") True >>> contains("tatat", "attax") # not enough 't's False > Further it's not clear that you have the right answer. What if string1 > contains one 'r' and string2 contains three? For the one 'r', the test > that it is also in string2 succeeds... but nowhere do you find out that > you actually needed to have three in order to be able to "rearrange to > make string2". > > Solving this problem might benefit from thinking about tests first: if > you write a test where you know the answer is going to be negative, a > second where it is going to be positive, and a third that is a > non-trivial instance of positive (like the multiple-letter count), then > you have something to code your solution against. After it works, you > can then think about refactoring: is there a better way? This will kind > of naturally lead you to thinking in terms of efficiency. > > Lesson 2: for very many tasks, someone has already done it, and you can > benefit by using some existing code, from the standard library or a > module installed separately. That's often the difference between a > programming challenge, which may want you to code it yourself to show > you can, and real-world problem solving, where you are rewarded (in > time) by efficiently reusing existing code. > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From wpmartin at gmail.com Fri Oct 19 13:47:18 2018 From: wpmartin at gmail.com (Pat Martin) Date: Fri, 19 Oct 2018 10:47:18 -0700 Subject: [Tutor] How to find optimisations for code In-Reply-To: References: Message-ID: That's correct sorry, I reassigned with replace back to string2, I forgot that in my retyping of the snippet. That makes sense, when it comes to strings and it taking so long. Thanks for explaining that. On Fri, Oct 19, 2018 at 10:09 AM Peter Otten <__peter__ at web.de> wrote: > Mats Wichmann wrote: > > > On 10/19/2018 10:12 AM, Pat Martin wrote: > >> Sorry my first email didn't have a subject line > >> > >> TLDR; How do you figure out if code is inefficient (if it isn't > >> necessarily obvious) and how do you find a more efficient solution? > > > > I think you've hit it in your last sentence ("except maybe write more > > code and get more experience"): experience will let you recognize > > patterns. > > > >> I use code wars sometimes to get some practice with Python, there was a > >> challenge to compare two strings and if string1 had enough characters to > >> be rearranged to make string2 return True, otherwise return False. > >> > >> I wrote a script that was like this: > >> > >> for i in string1: > >> if i not in string2: > >> return False > >> string2.replace(i,"",1) > >> return True > >> > >> This worked but I kept getting that my function was too inefficient and > >> it took too long. I did a search for the problem and found someone was > >> using collections.Counter. This basically takes the string and returns > >> the number of times each character occurs in it. Then just compare the > >> count of one string to another to see if there is enough of each letter > >> to make the other string. This seems like an elegant way to do it. > > > > notwithstanding that the challenge is a little contrived... here's > > something you will come to recognize. You use the string replace > > method, which is described thus, pasting from the official docs: > > > > """ > > str.replace(old, new[, count]) > > > > Return a copy of the string with all occurrences of substring old > > replaced by new. If the optional argument count is given, only the first > > count occurrences are replaced. > > """ > > > > Strings are not modifiable, so there is no replace-in-place. Each time > > you call string2.replace you build a new string... and then do nothing > > with that string, as you never assign a name to it. So that line clearly > > makes your code inefficient - you do some work that is just thrown away. > > And it's not clear what the purpose of this replacement is, anyway. > > This is probably retyped from memory. If the line were > > string2 = string2.replace(i,"",1) > > it would address your concern below about repeated chars. > > >>> def contains(s, t): > ... for c in s: > ... if c not in t: return False > ... t = t.replace(c, "", 1) # remove one occurence of c from t > ... return True > ... > >>> contains("ata", "attax") > True > >>> contains("tata", "attax") > True > >>> contains("tatat", "attax") # not enough 't's > False > > > Further it's not clear that you have the right answer. What if string1 > > contains one 'r' and string2 contains three? For the one 'r', the test > > that it is also in string2 succeeds... but nowhere do you find out that > > you actually needed to have three in order to be able to "rearrange to > > make string2". > > > > Solving this problem might benefit from thinking about tests first: if > > you write a test where you know the answer is going to be negative, a > > second where it is going to be positive, and a third that is a > > non-trivial instance of positive (like the multiple-letter count), then > > you have something to code your solution against. After it works, you > > can then think about refactoring: is there a better way? This will kind > > of naturally lead you to thinking in terms of efficiency. > > > > Lesson 2: for very many tasks, someone has already done it, and you can > > benefit by using some existing code, from the standard library or a > > module installed separately. That's often the difference between a > > programming challenge, which may want you to code it yourself to show > > you can, and real-world problem solving, where you are rewarded (in > > time) by efficiently reusing existing code. > > > > > > > > _______________________________________________ > > 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 > From wpmartin at gmail.com Fri Oct 19 13:50:22 2018 From: wpmartin at gmail.com (Pat Martin) Date: Fri, 19 Oct 2018 10:50:22 -0700 Subject: [Tutor] How to find optimisations for code In-Reply-To: References: Message-ID: So should we always use sets or dictionaries if possible? Are these more efficient because of not keeping track of their position? On Fri, Oct 19, 2018 at 10:47 AM Pat Martin wrote: > That's correct sorry, I reassigned with replace back to string2, I forgot > that in my retyping of the snippet. > > That makes sense, when it comes to strings and it taking so long. Thanks > for explaining that. > > On Fri, Oct 19, 2018 at 10:09 AM Peter Otten <__peter__ at web.de> wrote: > >> Mats Wichmann wrote: >> >> > On 10/19/2018 10:12 AM, Pat Martin wrote: >> >> Sorry my first email didn't have a subject line >> >> >> >> TLDR; How do you figure out if code is inefficient (if it isn't >> >> necessarily obvious) and how do you find a more efficient solution? >> > >> > I think you've hit it in your last sentence ("except maybe write more >> > code and get more experience"): experience will let you recognize >> > patterns. >> > >> >> I use code wars sometimes to get some practice with Python, there was a >> >> challenge to compare two strings and if string1 had enough characters >> to >> >> be rearranged to make string2 return True, otherwise return False. >> >> >> >> I wrote a script that was like this: >> >> >> >> for i in string1: >> >> if i not in string2: >> >> return False >> >> string2.replace(i,"",1) >> >> return True >> >> >> >> This worked but I kept getting that my function was too inefficient and >> >> it took too long. I did a search for the problem and found someone was >> >> using collections.Counter. This basically takes the string and returns >> >> the number of times each character occurs in it. Then just compare the >> >> count of one string to another to see if there is enough of each letter >> >> to make the other string. This seems like an elegant way to do it. >> > >> > notwithstanding that the challenge is a little contrived... here's >> > something you will come to recognize. You use the string replace >> > method, which is described thus, pasting from the official docs: >> > >> > """ >> > str.replace(old, new[, count]) >> > >> > Return a copy of the string with all occurrences of substring old >> > replaced by new. If the optional argument count is given, only the first >> > count occurrences are replaced. >> > """ >> > >> > Strings are not modifiable, so there is no replace-in-place. Each time >> > you call string2.replace you build a new string... and then do nothing >> > with that string, as you never assign a name to it. So that line clearly >> > makes your code inefficient - you do some work that is just thrown away. >> > And it's not clear what the purpose of this replacement is, anyway. >> >> This is probably retyped from memory. If the line were >> >> string2 = string2.replace(i,"",1) >> >> it would address your concern below about repeated chars. >> >> >>> def contains(s, t): >> ... for c in s: >> ... if c not in t: return False >> ... t = t.replace(c, "", 1) # remove one occurence of c from t >> ... return True >> ... >> >>> contains("ata", "attax") >> True >> >>> contains("tata", "attax") >> True >> >>> contains("tatat", "attax") # not enough 't's >> False >> >> > Further it's not clear that you have the right answer. What if string1 >> > contains one 'r' and string2 contains three? For the one 'r', the test >> > that it is also in string2 succeeds... but nowhere do you find out that >> > you actually needed to have three in order to be able to "rearrange to >> > make string2". >> > >> > Solving this problem might benefit from thinking about tests first: if >> > you write a test where you know the answer is going to be negative, a >> > second where it is going to be positive, and a third that is a >> > non-trivial instance of positive (like the multiple-letter count), then >> > you have something to code your solution against. After it works, you >> > can then think about refactoring: is there a better way? This will kind >> > of naturally lead you to thinking in terms of efficiency. >> > >> > Lesson 2: for very many tasks, someone has already done it, and you can >> > benefit by using some existing code, from the standard library or a >> > module installed separately. That's often the difference between a >> > programming challenge, which may want you to code it yourself to show >> > you can, and real-world problem solving, where you are rewarded (in >> > time) by efficiently reusing existing code. >> > >> > >> > >> > _______________________________________________ >> > 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 >> > From __peter__ at web.de Fri Oct 19 14:26:09 2018 From: __peter__ at web.de (Peter Otten) Date: Fri, 19 Oct 2018 20:26:09 +0200 Subject: [Tutor] How to find optimisations for code References: Message-ID: Pat Martin wrote: > So should we always use sets or dictionaries if possible? Are these more > efficient because of not keeping track of their position? Not always. If you want to know how often one entry/char occurs in a linear storage like a list, a string, or a file, then you can loop over the data: num_c = some_string.count(c) # the loop is hidden in the count() method pairs = (line.partition("=")[::2] for line in file) value = next(value for key, value in pairs if key == wanted_key) However, if you expect frequent similar questions go with a lookup table: freq = Counter(some_string) for c in strings.ascii_lowercase: print(c, "occurs", freq[c], "times") lookup = dict(pairs) for key in wanted_keys: print(key, "is", lookup.get(key, "unknown")) Before you start measure if the effort may be worthwhile, i. e. whether the linear approach causes a relevant delay. When you're done doublecheck if you got the desired improvement, i. e. whether you optimised the right place. From sjeik_appie at hotmail.com Fri Oct 19 14:58:54 2018 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 19 Oct 2018 18:58:54 +0000 Subject: [Tutor] (no subject) In-Reply-To: Message-ID: On 19 Oct 2018 18:09, Pat Martin wrote: TLDR; How do you figure out if code is inefficient (if it isn't necessarily obvious) and how do you find a more efficient ----- Hi, check this: https://docs.python.org/3/library/profile.html. Also, the timeit module is handy for smaller snippets. From alan.gauld at yahoo.co.uk Fri Oct 19 18:07:15 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Oct 2018 23:07:15 +0100 Subject: [Tutor] How to find optimisations for code In-Reply-To: References: Message-ID: On 19/10/18 17:12, Pat Martin wrote: > TLDR; How do you figure out if code is inefficient (if it isn't necessarily > obvious) and how do you find a more efficient solution? Others have addressed most of the issues but I'd just add the caveat that you can spend forever trying to make your code "more efficient". Usually, in the real world, you don't need to. So how do you figure out if your code is inefficient? Run it and if it doesn't work fast enough for your purpose then try to speed it up. If it does then move onto the next project. > how do I go about finding a more efficient solution? Google, a good reference book and experience. But always, always, measure because there has been more time wasted on unnecessary optimisations than on any other feature of programming. -- 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 cskk.id.au Fri Oct 19 21:26:16 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 20 Oct 2018 12:26:16 +1100 Subject: [Tutor] How to find optimisations for code In-Reply-To: References: Message-ID: <20181020012616.GA3403@cskk.homeip.net> On 19Oct2018 23:07, Alan Gauld wrote: >On 19/10/18 17:12, Pat Martin wrote: >> TLDR; How do you figure out if code is inefficient (if it isn't >> necessarily obvious) and how do you find a more efficient solution? > >Others have addressed most of the issues but I'd just add the caveat >that you can spend forever trying to make your code "more efficient". >Usually, in the real world, you don't need to. > >So how do you figure out if your code is inefficient? >Run it and if it doesn't work fast enough for your purpose then >try to speed it up. If it does then move onto the next project. > >> how do I go about finding a more efficient solution? > >Google, a good reference book and experience. Particularly, get some knowledge of the cost of various algorithms and data structures. There are outright inefficient things to do, but many others have costs and benefits: things fast in time but costly in space, and vice versa, and things efficient for some kinds of data but known to be bad for others. Have a quick read of this: https://en.wikipedia.org/wiki/Big_O_notation which talks about a common notation for costs. Skip the formal section with the math and go to the Orders of Common Functions section. https://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions You'll find people talk about big O notation a lot with algorithms and their efficiency. For example, a dict _lookup_ is O(1): constant time per lookup. But that doesn't mean always use a dict, because there's a cost to creating one in the first place. What you use it for matters. >But always, always, measure because there has been more >time wasted on unnecessary optimisations than on any other >feature of programming. I just want to add a little nuance to all of this, because it to me it reads a little like (a) if it works and you get things done in a timely manner it is efficient and (b) if it isn't fast enough you can always make it faster. Alan hasn't actually said that, but one could get that impression. To my mind: Alan's point about "fast enough" is perfectly valid: in the real world, if your problem has been solved then further work on efficiency might cost more to implement than you gain after doing it. His other point about measurement is also very important. It is possible to guess incorrectly about where performance problems lie, particularly with higher level languages because their internals has costs not apparent to the eye (because you can't see the internals). So from an engineering point of view, it is important to measure where a problematic programme spends its time and devote effort to those parts consuming the most time. The standard example is the tight loop: for a in sequence1: for b in sequence2: do thing A do thing B The cost of "do thing A" is more important than the cost of "do thing B" because likely A runs many many times more often than B. So even if B has some known inefficiency you can see, which will take some work to improve, effort spent on A will probably be more useful (if that's feasible). With a complex programme this isn't always obvious; in the example above the 2 pieces of code are side by side. The point about measurement is that profiling tools are useful here: when you _don't_ have an obvious primary target to improve but do need to improve efficiency, a profiling tool can measure where a programme spends its time in aggregate. That can point the way to code more beneficial to inspect. It at least gets you objective information about where your programme spends its time. It is limited by the data you give your programme: toy example input data are not as good as real world data. Finally, some things are as efficient as they get. You _can't_ always make things even more efficient. Cheers, Cameron Simpson From steve at pearwood.info Sat Oct 20 02:05:16 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 20 Oct 2018 17:05:16 +1100 Subject: [Tutor] How to find optimisations for code In-Reply-To: References: Message-ID: <20181020060515.GS3817@ando.pearwood.info> On Fri, Oct 19, 2018 at 09:12:54AM -0700, Pat Martin wrote: > Sorry my first email didn't have a subject line > > TLDR; How do you figure out if code is inefficient (if it isn't necessarily > obvious) and how do you find a more efficient solution? You know it is inefficient if it takes longer to run than it ought to. The tricky questions are: - if you don't *measure* the time, how do you know how long it takes? - how do you know how long it ought to take? other than wishful thinking? - how do you find a more efficient solution? That's comes from experience, practice, and thinking about the problem you are trying to solve. The last one is especially tricky, because it may involve coming up with a solution to a problem that has never been solved before. That requires creativity and insight. But before you do any of that, remember the two critical rules of optimization, from the classic 1975 book "Principles of Program Design" by Michael A. Jackson: Rule 1: Don?t do it. Rule 2 (for experts only): Don?t do it yet. Donald Knuth wrote: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." And to quote W.A. Wulf: "More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason ? including blind stupidity." > I use code wars sometimes to get some practice with Python, there was a > challenge to compare two strings and if string1 had enough characters to be > rearranged to make string2 return True, otherwise return False. Think about the problem to be solved. How do we know that one string could be rearranged to the other? Each string much have: - the same individual letters, e.g. "binary" and "brainy"; - the same total number of letters; - the same number of *each* letter. We don't need to check that the individual letters are the same, because checking the counts will suffice. If they are not the same, one string will have (let's say) two A's while the other will have none, and the counts will be different. If the two strings have different length, we know they can't be anagrams of each other: if len(string1) != len(string2): return False Only then do we need to compare the counts of each letter: for c in string1: if string1.count(c) != string2.count(c): return False Can that be improved? Perhaps -- for short strings, that ought to be pretty speedy, but for very long strings it has to inspect every letter over and over again... for the first letter in string1: count how many times it appears: - compare it to the first letter; - compare it to the second letter; - compare it to the third letter; etc for the second letter in string1: count how many times it appears: - compare it to the first letter; - compare it to the second letter; - compare it to the third letter; etc etc. So you can see, each letter gets inspected over and over again. If the strings are long, that does a lot of work. True, it is done at the speed of optimized C code (the string.count() method is pretty fast), but we can do better by using the Counter, which only needs to inspect each letter once: if collections.Counter(string1) != collections.Counter(string2): return False Can we do better? Maybe... building the counters only needs to inspect each letter once. But Counters have a bit of overhead, they're not trivial code. There's a conceptually simpler way which *might* (or might not!) be faster in practice: if sorted(string1) != sorted(string2): return False Think about why that test works. If you have truly huge strings, then the Counter will be better, for sure. But for moderate-sized, or small, strings, I wouldn't want to guess which is faster. Even though technically sorted does more work, it probably has less overhead. It might very well win for some cases. > Sorry about the long email, not sure if there is even an answer to this > question except maybe write more code and get more experience? Indeed. Nothing beats experience. But you can also stand on the shoulders of those who came before you, in the words of Sir Isaac Newton, one of the greatest mathematicians and physicists of any time: "If I have seen further, it is because I have stood on the shoulders of giants" which was a not-at-all veiled dig against his rival and equal genius, Sir Robert Hooke, who was short and bent. (Newton was a genius, but he was not a nice guy, and he had many rivalries and feuds. In fairness, so did Hooke.) You should start here: https://www.joelonsoftware.com/2001/12/11/back-to-basics/ and think about how that relates to your original algorithm. As far as programming in Python goes, some general advice: - whenever you can do something by calling a built-in function or method, it will probably be faster than doing it in Python code you wrote yourself; - so if you want fast code, try to find a way to use built-ins whenever possible; - looking up values in a set or dict is optimized to be very fast; - appending to the end of a list is fast; - but inserting at the beginning is slow; - "fast" and "slow" is relative: don't worry about "slow" operations until you know that they actually make a difference. (Remember the quote from W.A. Wulf.) - and watch out for those Shlemiel the painter?s algorithms! -- Steve From __peter__ at web.de Sat Oct 20 03:01:53 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 20 Oct 2018 09:01:53 +0200 Subject: [Tutor] How to find optimisations for code References: <20181020060515.GS3817@ando.pearwood.info> Message-ID: Steven D'Aprano wrote: > We don't need to check that the individual letters are the same, because > checking the counts will suffice. If they are not the same, one string > will have (let's say) two A's while the other will have none, and the > counts will be different. Another great optimisation is solving the actual problem ;) The OP isn't looking for anagrams, he wants to know if string2 can be spelt with the characters in string1: abba, abbbbbacus --> True (don't care about the extra b, c, u, s) abba, baab --> True (don't care about character order) abba, bab --> False (bab is missing an a) From robertvstepp at gmail.com Sat Oct 20 10:53:39 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 20 Oct 2018 09:53:39 -0500 Subject: [Tutor] When are "__init__.py" files needed and not needed in a project? Message-ID: Linux Mint 19 Cinnamon, Python 3.6.6 I would have sworn that I had read, either on this list or the main Python list, that in the most recent versions of Python 3 that "__init__.py" files were no longer needed in nested project file structures. But when I attempted to run tests for the first time on my new Solitaire Scorekeeper project (Finally getting around to this!), I got: bob at Dream-Machine1:~/Projects/solitaire_scorekeeper$ python3 -m unittest ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK So no tests were run. So it immediately occurred to me to add an empty "__init__.py" file to my "tests" subfolder and got what I was currently expecting: bob at Dream-Machine1:~/Projects/solitaire_scorekeeper$ python3 -m unittest E ====================================================================== ERROR: test_get_gamenames_bad_path (tests.tests_main.TestGameNamesMapperMethods) Test that when the method, get_gamenames(), is passed a path to a ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/bob/Projects/solitaire_scorekeeper/tests/tests_main.py", line 20, in test_get_gamenames_bad_path self.assertEqual(gamenames.gamenames(), {}) NameError: name 'self' is not defined ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1) I was expecting this error and will shortly correct it. So my question remains, when are "__init__.py" files needed and when are they not? In case it helps, my current project structure is: ~/Projects data/ docs/ tests/ .git/ main.py .gitignore TIA! -- boB From avigross at verizon.net Sat Oct 20 12:16:56 2018 From: avigross at verizon.net (Avi Gross) Date: Sat, 20 Oct 2018 12:16:56 -0400 Subject: [Tutor] How to find optimisations for code In-Reply-To: References: <20181020060515.GS3817@ando.pearwood.info> Message-ID: <001201d46890$52a45df0$f7ed19d0$@verizon.net> There are many ways to do some things. I present a fairly straightforward method for consideration. Synopsis. Use two dictionaries to keep track of how many times a letter can be found in the source word and the target word. Loop over the target letters and as soon as you find a letter that needs more copies of that letter than are available in the source, return False. Otherwise, return True. Slight optimization, return False immediately if the target is too long. Note: Remove the print statements used to show what it does when trying for efficiency. Note I do not claim this is fast. But for longer strings this requires one pass over each string to set the dictionary counters and often less than one pass over the target string to determine Truth. I can visualize many variants but efficiency of each may vary with the data, version of Python and so on. Python CODE using version 3.7.0 : """ Function to check if string str_target is a strict subset of str_source - but with multiple copies of a letter being unique in what is thus not a set. In English, can the letters in str_target be found in the required quantities with possibly some left over when examining str_source. For example, 'level' has two l's and two e's and can be made from 'multilevel' but not from 'evil' which has the needed e v and l but not two copies. """ def sub_word(str_source, str_target): """ Return True/False if letters in source are enough to make target Method: make dictionaries of number of letters in each. See if enough. """ # Check for trivial False condition: word too long if len(str_target) > len(str_source) : return False # Initialize empty dictionaries to hold leter counts. source = {} target = {} # Count letters in source thgen target, initializing # to zero if not yet present. for letter in str_source.lower(): source[letter] = source.get(letter, 0) + 1 for letter in str_target.lower(): target[letter] = target.get(letter, 0) + 1 # During bebug phase, show what the dictionaries contain print("SOURCE dict:", source) print("TARGET dict:", target) # Iterate over letters in target and return False # if the number of instances needed exceeds what # can be found in the source. for key in target: if target[key] > source.get(key, 0) : return False # if you reached here, return True as all letters needed # have been found. return True Examples of use: >>> sub_word("multilevel", "level") SOURCE dict: {'m': 1, 'u': 1, 'l': 3, 't': 1, 'i': 1, 'e': 2, 'v': 1} TARGET dict: {'l': 2, 'e': 2, 'v': 1} True >>> sub_word("shorT", "MuchTooLong") False >>> sub_word("supercalifragilisticexpialidocious", "california") SOURCE dict: {'s': 3, 'u': 2, 'p': 2, 'e': 2, 'r': 2, 'c': 3, 'a': 3, 'l': 3, 'i': 7, 'f': 1, 'g': 1, 't': 1, 'x': 1, 'd': 1, 'o': 2} TARGET dict: {'c': 1, 'a': 2, 'l': 1, 'i': 2, 'f': 1, 'o': 1, 'r': 1, 'n': 1} False >>> sub_word("supercalifragilisticexpialidocious", "fragile") SOURCE dict: {'s': 3, 'u': 2, 'p': 2, 'e': 2, 'r': 2, 'c': 3, 'a': 3, 'l': 3, 'i': 7, 'f': 1, 'g': 1, 't': 1, 'x': 1, 'd': 1, 'o': 2} TARGET dict: {'f': 1, 'r': 1, 'a': 1, 'g': 1, 'i': 1, 'l': 1, 'e': 1} True >>> sub_word("devil", "vile") SOURCE dict: {'d': 1, 'e': 1, 'v': 1, 'i': 1, 'l': 1} TARGET dict: {'v': 1, 'i': 1, 'l': 1, 'e': 1} True -----Original Message----- From: Tutor On Behalf Of Peter Otten Sent: Saturday, October 20, 2018 3:02 AM To: tutor at python.org Subject: Re: [Tutor] How to find optimisations for code Steven D'Aprano wrote: > We don't need to check that the individual letters are the same, > because checking the counts will suffice. If they are not the same, > one string will have (let's say) two A's while the other will have > none, and the counts will be different. Another great optimisation is solving the actual problem ;) The OP isn't looking for anagrams, he wants to know if string2 can be spelt with the characters in string1: abba, abbbbbacus --> True (don't care about the extra b, c, u, s) abba, baab --> True (don't care about character order) abba, bab --> False (bab is missing an a) _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Sat Oct 20 14:35:46 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 20 Oct 2018 20:35:46 +0200 Subject: [Tutor] When are "__init__.py" files needed and not needed in a project? References: Message-ID: boB Stepp wrote: > Linux Mint 19 Cinnamon, Python 3.6.6 > > I would have sworn that I had read, either on this list or the main > Python list, that in the most recent versions of Python 3 that > "__init__.py" files were no longer needed in nested project file > structures. If you omit the __init__.py you get a "namespace package". Namespace packages can combine multiple directories into one package, but will be shaded by "normal" packages. > But when I attempted to run tests for the first time on > my new Solitaire Scorekeeper project (Finally getting around to > this!), I got: > > bob at Dream-Machine1:~/Projects/solitaire_scorekeeper$ python3 -m unittest > > ---------------------------------------------------------------------- > Ran 0 tests in 0.000s > > OK > > So no tests were run. So it immediately occurred to me to add an > empty "__init__.py" file to my "tests" subfolder and got what I was > currently expecting: > > bob at Dream-Machine1:~/Projects/solitaire_scorekeeper$ python3 -m unittest > E > ====================================================================== > ERROR: test_get_gamenames_bad_path > (tests.tests_main.TestGameNamesMapperMethods) Test that when the method, > get_gamenames(), is passed a path to a > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "/home/bob/Projects/solitaire_scorekeeper/tests/tests_main.py", > line 20, in test_get_gamenames_bad_path > self.assertEqual(gamenames.gamenames(), {}) > NameError: name 'self' is not defined > > ---------------------------------------------------------------------- > Ran 1 test in 0.000s > > FAILED (errors=1) > > I was expecting this error and will shortly correct it. So my > question remains, when are "__init__.py" files needed and when are > they not? I am struggling with the differences between the two types of packages myself, so my first guess was that you accidentally found out that test discovery doesn't work for namespace packages. However https://docs.python.org/dev/library/unittest.html#test-discovery expicitly states """ Changed in version 3.4: Test discovery supports namespace packages. """ Perhaps you should file a bug report. > In case it helps, my current project structure is: > > ~/Projects > data/ > docs/ > tests/ > .git/ > main.py > .gitignore > > TIA! > From robertvstepp at gmail.com Sat Oct 20 17:52:03 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 20 Oct 2018 16:52:03 -0500 Subject: [Tutor] When are "__init__.py" files needed and not needed in a project? In-Reply-To: References: Message-ID: On Sat, Oct 20, 2018 at 1:36 PM Peter Otten <__peter__ at web.de> wrote: > > boB Stepp wrote: > > > Linux Mint 19 Cinnamon, Python 3.6.6 [snip] > > I was expecting this error and will shortly correct it. So my > > question remains, when are "__init__.py" files needed and when are > > they not? > > I am struggling with the differences between the two types of packages > myself, so my first guess was that you accidentally found out that test > discovery doesn't work for namespace packages. However > > https://docs.python.org/dev/library/unittest.html#test-discovery > > expicitly states > > """ > Changed in version 3.4: Test discovery supports namespace packages. > """ > > Perhaps you should file a bug report. I just waded through PEP 420 describing "implicit namespace packages". I think I have a cursory understanding now, but not enough to feel confidant to file a bug report. It *seems* that with my project structure below (Note the slight correction.) if I initiate test discovery in the top level directory (The one holding main.py.) and have no __init__.py files anywhere, that my initial effort should have run the one test with the resulting error instead of running 0 tests. Can anything reading this duplicate my issue? If yes, then I will endeavour to file a bug report. But otherwise, I would suspect something screwy that I have done and am currently unaware of what it may be. > > In case it helps, my current project structure is: > > > > ~/Projects > > data/ > > docs/ > > tests/ > > .git/ > > main.py > > .gitignore Just noticed that I mistyped the top level of my project structure. It should be: ~/Projects/solitaire_scorekeeper/ etc. -- boB From asad.hasan2004 at gmail.com Sat Oct 20 16:07:17 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Sun, 21 Oct 2018 01:37:17 +0530 Subject: [Tutor] sftp in python without using pysftp or paramiko Message-ID: hi All , I am looking to create a python script which logs in to the server and copies the fie from ftp to local system .: sftp abc at us.com 2100 password : xxx sftp> cd /to_dir sftp > get file1 sftp >exit Please advice . -- Asad Hasan +91 9582111698 From alan.gauld at yahoo.co.uk Sat Oct 20 19:44:23 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 21 Oct 2018 00:44:23 +0100 Subject: [Tutor] sftp in python without using pysftp or paramiko In-Reply-To: References: Message-ID: On 20/10/18 21:07, Asad wrote: > hi All , > > I am looking to create a python script which logs in to the server and > copies the fie from ftp to local system .: The obvious answer is use pysftp. But your subject says you don't want to do that. So the next option is for you to write your own version of pysftp, duplicating all the things the author of that module did. Of course for a non generic solution you only need a fraction of the code, and you could use something like pyexpect or an ssh module to reduce the effort. > sftp abc at us.com 2100 > > password : xxx > > sftp> cd /to_dir > sftp > get file1 > sftp >exit > > Please advice . I'd advise using the available modules. But if that really is impossible use the next level down. Reinventing the wheel is a painful waste of time that usually only serves to increase your respect for the inventor of said wheel. -- 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 robertvstepp at gmail.com Sat Oct 20 23:00:24 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 20 Oct 2018 22:00:24 -0500 Subject: [Tutor] What is the best way for a program suite to know where it is installed? Message-ID: I was just re-reading the entire thread at https://www.mail-archive.com/tutor at python.org/msg77864.html And I have asked similar questions previous to that thread. I still do not have a satisfactory answer for the subject line's question that both makes sense to me and seems to be good programming practice. And that works for any OS that the program suite is installed under. A constantly recurring situation I am having is a programming project with a nested directory structure. Usually I will have "tests" and "data" folders nested under the top level project folder. There may be others as well depending on the complexity of the project I am attempting. As far as I can tell, I cannot make any assumptions about what the current working directory is when a user starts the program. So how can one of my programs *know* what the absolute path is to, say, the top level of the program suite? If I could always reliably determine this by my program's code, every other location could be determined from the known nested structure of the program suite. [But I have a dim recollection that either Ben or Cameron thought relying on the "known nested structure" is brittle at best.] So far the best method I've come up with is to make use of "__file__" for the initiating program file. But from past discussions I am not certain this is the *best* way. Is the *best* way going to get me into best techniques for installing and distributing programs? If so, this strikes me as a huge mess to dive into! TIA! -- boB From akleider at sonic.net Sun Oct 21 00:21:34 2018 From: akleider at sonic.net (Alex Kleider) Date: Sat, 20 Oct 2018 21:21:34 -0700 Subject: [Tutor] When are "__init__.py" files needed and not needed in a project? In-Reply-To: References: Message-ID: On 2018-10-20 14:52, boB Stepp wrote: >> > In case it helps, my current project structure is: >> > >> > ~/Projects >> > data/ >> > docs/ >> > tests/ >> > .git/ >> > main.py >> > .gitignore I'm curious to know where under the above structure you keep your code files? (...or is all your code within main.py?) From robertvstepp at gmail.com Sun Oct 21 00:43:21 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 20 Oct 2018 23:43:21 -0500 Subject: [Tutor] When are "__init__.py" files needed and not needed in a project? In-Reply-To: References: Message-ID: On Sat, Oct 20, 2018 at 11:21 PM Alex Kleider wrote: > > On 2018-10-20 14:52, boB Stepp wrote: > > > >> > In case it helps, my current project structure is: > >> > > >> > ~/Projects/solitaire_scorekeeper/ # I left off the actual project folder in my original email > >> > data/ > >> > docs/ > >> > tests/ > >> > .git/ > >> > main.py > >> > .gitignore > > > I'm curious to know where under the above structure you keep your code > files? (...or is all your code within main.py?) For this project, I am hoping that in the end there will not be a lot of code, so I am currently putting all code in main.py. If this gets too unwieldy I will move all refactored source code files into a new "src" subdirectory. -- boB From robertvstepp at gmail.com Sun Oct 21 02:13:50 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 21 Oct 2018 01:13:50 -0500 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? Message-ID: Use case: I want to allow a user of my Solitaire Scorekeeper program to be able to give any name he wants to each game of solitaire he wishes to record. My thought for permanent storage of each game's parameters is to use a dictionary to map the user-chosen game names to a unique json filename. This way I hope no shenanigans can occur with weird characters/non-printing characters. My initial thought was to just have a sequence of game names with incrementing numerical suffixes: game_0, game_1, ... , game_n. But this would require the program to keep track of what the next available numerical suffix is. Additionally, if a user chooses to delete a game, then there would be a gap in the numerical sequence of the game filenames. I find such a gap aesthetically displeasing and would probably go to additional efforts to reuse such deleted filenames, so there would be no such "gaps". So I am now wondering if using tempfile.NamedTemporaryFile(delete=False) would solve this problem nicely? As I am not very familiar with this library, are there any unforeseen issues I should be made aware of? Would this work equally well on all operating systems? TIA! -- boB From robertvstepp at gmail.com Sun Oct 21 03:04:05 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 21 Oct 2018 02:04:05 -0500 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: On Sun, Oct 21, 2018 at 1:47 AM Quentin Agren wrote: > > Hi Robert, > > Far from being an expert, my two cents on this: > > - As I understand it, you should at least use the 'dir' parameter to NamedTemporaryFile, otherwise your files will be created in a '/tmp/'-like directory that may be wiped clean by the OS now and then. I am planning to set both the "dir" and "suffix" parameters if I go this route and probably the "mode" parameter, too. > - I seems that the only functionality you desire from tempfile is the generation of a random file name (and maybe ensuring that no collision occurs). You could use the 'random' standard library module to generate such names easily (which is about what tempfile does under the hood) It is important for me to have no name collisions however unlikely such an event might be. > import random > CHARS = 'abcdefghijklmnopqrstuvw1234567890' > def random_name(length): > return ''.join([random.choice(CHARS) for _ in range(length)]) It occurred to me to do this type of approach. But why write this myself when a standard library module may give me what I want with only one or two lines of code? When I searched online others have recommended using the uuid library to generate names. But one still has to check that it does not already exist (However unlikely.) and format the final filename. And I would have to do the same for the code snippet you supplied, adding a few additional lines of code. But thank you for your input! It may turn out that there is something undesirable that I am unaware of in the NamedTemporaryFile approach, other than what I really want is a NamedPermanentFile approach, but the standard library naming suggest otherwise! ~(:>)) boB From mlathkar at gmail.com Sun Oct 21 02:52:30 2018 From: mlathkar at gmail.com (Malhar Lathkar) Date: Sun, 21 Oct 2018 12:22:30 +0530 Subject: [Tutor] organisation of folder structure of Python's installation directory Message-ID: Hi all. I am trying to understand how Python's installation directory is organized. I find lib as well as libs folder. what's the difference? I couldn't find exact explanation of purpose of various other sub folders like include, scripts, share etc. Can you help me understand this? Please share documentation link if any. thanks. -- Malhar Lathkar [image: in.linkedin.com/in/malharlathkar] From quentin.agren at gmail.com Sun Oct 21 01:24:08 2018 From: quentin.agren at gmail.com (Quentin Agren) Date: Sun, 21 Oct 2018 07:24:08 +0200 Subject: [Tutor] Some questions about importlib Message-ID: Hi, My name Quentin, and this is my first post to this list so please redirect me if this is not the proper audience. I have been studying the 'importlib' standard library package this past week, and although I find it very readable I am puzzled by some questions that I'd like to share. - [Chicken and egg] Does the 'import' statement in Python make use of 'importlib'? If so, how are the imports in 'importlib' itself carried out? (for example in __init__.py) - Why does 'importlib' use 'nt' or 'psoix' (see '_bootstrap_external._setup()') rather than the portable 'os', and reimplement some of the latter's functionality, for example '_path_join', or '_path_split'. Another example is in 'SourceFileLoader.set_data()' where the logic of 'os.mkdirs' is reproduced to create all necessary intermediary directories in a file path. - Similarly, would not using the 'struct' module simplify the packing/unpacking of bytecode. For example by defining BYTECODE_HEADER_FMT = '4sII' (Python 3.6) Thanks for any insights you may share and have a nice sunday, Quentin From quentin.agren at gmail.com Sun Oct 21 02:47:28 2018 From: quentin.agren at gmail.com (Quentin Agren) Date: Sun, 21 Oct 2018 08:47:28 +0200 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: Hi Robert, Far from being an expert, my two cents on this: - As I understand it, you should at least use the 'dir' parameter to NamedTemporaryFile, otherwise your files will be created in a '/tmp/'-like directory that may be wiped clean by the OS now and then. - I seems that the only functionality you desire from tempfile is the generation of a random file name (and maybe ensuring that no collision occurs). You could use the 'random' standard library module to generate such names easily (which is about what tempfile does under the hood) import random CHARS = 'abcdefghijklmnopqrstuvw1234567890' def random_name(length): return ''.join([random.choice(CHARS) for _ in range(length)]) Cheers, Quentin Le dim. 21 oct. 2018 ? 08:15, boB Stepp a ?crit : > Use case: I want to allow a user of my Solitaire Scorekeeper program > to be able to give any name he wants to each game of solitaire he > wishes to record. My thought for permanent storage of each game's > parameters is to use a dictionary to map the user-chosen game names to > a unique json filename. This way I hope no shenanigans can occur with > weird characters/non-printing characters. > > My initial thought was to just have a sequence of game names with > incrementing numerical suffixes: game_0, game_1, ... , game_n. But > this would require the program to keep track of what the next > available numerical suffix is. Additionally, if a user chooses to > delete a game, then there would be a gap in the numerical sequence of > the game filenames. I find such a gap aesthetically displeasing and > would probably go to additional efforts to reuse such deleted > filenames, so there would be no such "gaps". > > So I am now wondering if using > tempfile.NamedTemporaryFile(delete=False) would solve this problem > nicely? As I am not very familiar with this library, are there any > unforeseen issues I should be made aware of? Would this work equally > well on all operating systems? > > TIA! > -- > boB > _______________________________________________ > 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 Sun Oct 21 03:59:25 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 21 Oct 2018 08:59:25 +0100 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: On 21/10/18 07:13, boB Stepp wrote: > My initial thought was to just have a sequence of game names with > incrementing numerical suffixes: game_0, game_1, ... , game_n. But > this would require the program to keep track of what the next > available numerical suffix is. The traditional approach to such scenarios (eg. for log files) is to append the datetime. If that is not enough granularity you can additionally append a numeric suffix that applies only during a single session(ie a global variable) and is only applied when the datetimes are identical - very rare. > tempfile.NamedTemporaryFile(delete=False) would solve this problem Can't help there, I've never used it. :-) -- 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 cskk.id.au Sun Oct 21 04:31:13 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 21 Oct 2018 19:31:13 +1100 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: <20181021083113.GA19964@cskk.homeip.net> On 21Oct2018 01:13, boB Stepp wrote: >Use case: I want to allow a user of my Solitaire Scorekeeper program >to be able to give any name he wants to each game of solitaire he >wishes to record. My thought for permanent storage of each game's >parameters is to use a dictionary to map the user-chosen game names to >a unique json filename. This way I hope no shenanigans can occur with >weird characters/non-printing characters. > >My initial thought was to just have a sequence of game names with >incrementing numerical suffixes: game_0, game_1, ... , game_n. But >this would require the program to keep track of what the next >available numerical suffix is. Additionally, if a user chooses to >delete a game, then there would be a gap in the numerical sequence of >the game filenames. I find such a gap aesthetically displeasing and >would probably go to additional efforts to reuse such deleted >filenames, so there would be no such "gaps". > >So I am now wondering if using >tempfile.NamedTemporaryFile(delete=False) would solve this problem >nicely? As I am not very familiar with this library, are there any >unforeseen issues I should be made aware of? Would this work equally >well on all operating systems? The doco reads that way to me. However, NamedTemporaryFile is a (nice) wrapper for tempfile.mkstemp(). Why not use that directly? Cheers, Cameron Simpson From __peter__ at web.de Sun Oct 21 04:55:08 2018 From: __peter__ at web.de (Peter Otten) Date: Sun, 21 Oct 2018 10:55:08 +0200 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? References: Message-ID: boB Stepp wrote: > Use case: I want to allow a user of my Solitaire Scorekeeper program > to be able to give any name he wants to each game of solitaire he > wishes to record. My thought for permanent storage of each game's > parameters is to use a dictionary to map the user-chosen game names to > a unique json filename. This way I hope no shenanigans can occur with > weird characters/non-printing characters. > > My initial thought was to just have a sequence of game names with > incrementing numerical suffixes: game_0, game_1, ... , game_n. But > this would require the program to keep track of what the next > available numerical suffix is. Additionally, if a user chooses to > delete a game, then there would be a gap in the numerical sequence of > the game filenames. I find such a gap aesthetically displeasing and > would probably go to additional efforts to reuse such deleted > filenames, so there would be no such "gaps". > > So I am now wondering if using > tempfile.NamedTemporaryFile(delete=False) would solve this problem > nicely? As I am not very familiar with this library, are there any > unforeseen issues I should be made aware of? Would this work equally > well on all operating systems? I think this is cool thinking outside of the box. I would not have "dared" this, but now you suggest it I cannot see anything wrong with your approach. From __peter__ at web.de Sun Oct 21 06:22:43 2018 From: __peter__ at web.de (Peter Otten) Date: Sun, 21 Oct 2018 12:22:43 +0200 Subject: [Tutor] Some questions about importlib References: Message-ID: Quentin Agren wrote: > Hi, > > My name Quentin, and this is my first post to this list so please redirect > me if this is not the proper audience. > > I have been studying the 'importlib' standard library package this past > week, and although I find it very readable I am puzzled by some questions > that I'd like to share. > > - [Chicken and egg] Does the 'import' statement in Python make use of > 'importlib'? If so, how are the imports in 'importlib' itself carried out? > (for example in __init__.py) Google found http://sayspy.blogspot.com/2012/02/how-i-bootstrapped-importlib.html > - Why does 'importlib' use 'nt' or 'psoix' (see > '_bootstrap_external._setup()') rather than the portable 'os', and > reimplement some of the latter's functionality, for example '_path_join', > or '_path_split'. Another example is in 'SourceFileLoader.set_data()' > where the logic of 'os.mkdirs' is reproduced to create all necessary > intermediary directories in a file path. > > - Similarly, would not using the 'struct' module simplify the > packing/unpacking of bytecode. For example by defining BYTECODE_HEADER_FMT > = '4sII' (Python 3.6) If the bootstrap process still works as Brett Cannon described it back in 2012 struct and os cannot be used because they aren't built-in modules. >>> import sys >>> "posix" in sys.builtin_module_names True >>> "os" in sys.builtin_module_names False >>> "struct" in sys.builtin_module_names False From mats at wichmann.us Sun Oct 21 10:32:00 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 21 Oct 2018 08:32:00 -0600 Subject: [Tutor] organisation of folder structure of Python's installation directory In-Reply-To: References: Message-ID: <28580758-38b0-bcfe-9e91-597c2a17ca6d@wichmann.us> On 10/21/18 12:52 AM, Malhar Lathkar wrote: > Hi all. > > I am trying to understand how Python's installation directory is organized. > I find lib as well as libs folder. what's the difference? I couldn't find > exact explanation of purpose of various other sub folders like include, > scripts, share etc. Can you help me understand this? Please share > documentation link if any. > > thanks. > on a Windows installation, Lib is the python code for the standard library, while DLLs contains any backing binary objects for the standard library; meanwhile libs is what is necessary to to link with the python libraries themselves, as when developing a C-language extension. include is the header files used for compiling such extensions - so these two are not part of the Python runtime, but have this special purpose. (note sometimes when you "python -m pip install" it may try to compile some code, so this is not always just for you yourself to write extensions). The rest should be relatively straightforward. The layout can be quite different on other systems. A /usr/share directory on Linux is used here for shareable, non-binary pieces like documentation and license files. The python layout on Linux is normally packaged along the lines of the Filesystem Hierarchy Standard: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html From oscar.j.benjamin at gmail.com Sun Oct 21 17:08:16 2018 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sun, 21 Oct 2018 22:08:16 +0100 Subject: [Tutor] What is the best way for a program suite to know where it is installed? In-Reply-To: References: Message-ID: (Resending to the list - sorry for the duplicate, Bob) On Sun, 21 Oct 2018 at 04:02, boB Stepp wrote: > > I was just re-reading the entire thread at > https://www.mail-archive.com/tutor at python.org/msg77864.html > And I have asked similar questions previous to that thread. I still > do not have a satisfactory answer for the subject line's question that > both makes sense to me and seems to be good programming practice. And > that works for any OS that the program suite is installed under. > > A constantly recurring situation I am having is a programming project > with a nested directory structure. Usually I will have "tests" and > "data" folders nested under the top level project folder. There may > be others as well depending on the complexity of the project I am > attempting. Is this project a library or an application? If it is a library then it is important to do things in the way that users of your library will expect so that they can reasonably install and use your library or package it up along with other libraries as part of an application. If it is an application then the requirements are weaker - it really depends how you package and distribute your application which is something that is under your control. > As far as I can tell, I cannot make any assumptions about > what the current working directory is when a user starts the program. That depends what kind of an application/library it is and how it is started. For a library you certainly shouldn't make assumptions about this. Command line programs that are installed on PATH can be run from any directory. GUI programs could be run with anything as the current working directory. It may not make sense for your program to be installed on PATH though and you might document that a user should run your program by running a particular command in a particular directory. There might be other reasons for doing this such as ensuring that any Python modules are importable. Generally speaking though it shouldn't be necessary to depend in any way on the current working directory just to locate your applications data files. > So how can one of my programs *know* what the absolute path is to, > say, the top level of the program suite? I assume here that you mean "how can code in a particular Python module find the base directory that my application/library is installed in?". The simplest answer is to use __path__/__file__. If you have many Python files organised into packages and subpackages etc then you should do this in one place near the top. So if you have pgk config.py data.txt subpkg1 submod1.py then config.py can have the code to check __file__ and work out the path of the directory containing the pkg folder. The other modules can all import that from pkg.config. > If I could always reliably > determine this by my program's code, every other location could be > determined from the known nested structure of the program suite. [But > I have a dim recollection that either Ben or Cameron thought relying > on the "known nested structure" is brittle at best.] One way in which this can go wrong is if you are distributing a library. It is possible that your library could end up being embedded in a zip-file so that the files don't exist unzipped on disk. For the library case it certainly used to be recommended to use pkg_resources to https://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access So for the example above you would use something like: data_bytes = pkg_resources.resource_string('pkg', 'data.txt') > So far the best method I've come up with is to make use of "__file__" > for the initiating program file. But from past discussions I am not > certain this is the *best* way. Is the *best* way going to get me > into best techniques for installing and distributing programs? If so, > this strikes me as a huge mess to dive into! I think that for a simple application this is fine. You are in control of how your application is packaged and installed so you can ensure that this will always work if someone runs your application in the intended way. So you can do something like: project_root = os.path.abspath(os.path.dirname(__file__)) data_file_path = os.path.join(project_root, 'data'.txt') In the other thread you seemed confused that the effect of abspath (or similarly realpath) for a given input string depends on the current directory. That's because the contents of the string __file__ may also depend on the current working directory. The point of abspath/realpath is that these effects should cancel so that the resulting string does not depend on the current working directory. -- Oscar From cs at cskk.id.au Sun Oct 21 19:02:58 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 22 Oct 2018 10:02:58 +1100 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: <20181021230258.GA30714@cskk.homeip.net> On 21Oct2018 10:55, Peter Otten <__peter__ at web.de> wrote: >boB Stepp wrote: >> So I am now wondering if using >> tempfile.NamedTemporaryFile(delete=False) would solve this problem >> nicely? As I am not very familiar with this library, are there any >> unforeseen issues I should be made aware of? Would this work equally >> well on all operating systems? > >I think this is cool thinking outside of the box. > >I would not have "dared" this, but now you suggest it I cannot see anything >wrong with your approach. The doco for mktemp (do not use! use mkstemp or the NamedTemporaryFile classes instead!) explicitly mentions using delete=False. Cheers, Cameron Simpson From steve at pearwood.info Sun Oct 21 20:20:02 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 22 Oct 2018 11:20:02 +1100 Subject: [Tutor] sftp in python without using pysftp or paramiko In-Reply-To: References: Message-ID: <20181022002002.GZ3817@ando.pearwood.info> On Sun, Oct 21, 2018 at 12:44:23AM +0100, Alan Gauld via Tutor wrote: > On 20/10/18 21:07, Asad wrote: > > hi All , > > > > I am looking to create a python script which logs in to the server and > > copies the fie from ftp to local system .: > > The obvious answer is use pysftp. > But your subject says you don't want to do that. It could be a good learning exercise. As you say: > Reinventing the wheel is a painful > waste of time that usually only serves to > increase your respect for the inventor of > said wheel. But its not a waste of time if you learn valuable skills from it. *Somebody* has the write and maintain the low-level stuff, and if we all did nothing but use the high-level libraries, who would maintain them? I'm reminded of the situation in scientific computing, where there are many important libraries written in Fortran which nobody can maintain because nobody understands how they work. Asad: show us the code you've started with. You are far more likely to get a useful response to *concrete* questions about a specific problem, than a vague "show me what to do" general question. -- Steve From wolfgang.maier at biologie.uni-freiburg.de Mon Oct 22 06:25:12 2018 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Mon, 22 Oct 2018 12:25:12 +0200 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: On 21.10.18 08:13, boB Stepp wrote: > Use case: I want to allow a user of my Solitaire Scorekeeper program > to be able to give any name he wants to each game of solitaire he > wishes to record. My thought for permanent storage of each game's > parameters is to use a dictionary to map the user-chosen game names to > a unique json filename. This way I hope no shenanigans can occur with > weird characters/non-printing characters. > > My initial thought was to just have a sequence of game names with > incrementing numerical suffixes: game_0, game_1, ... , game_n. But > this would require the program to keep track of what the next > available numerical suffix is. Additionally, if a user chooses to > delete a game, then there would be a gap in the numerical sequence of > the game filenames. I find such a gap aesthetically displeasing and > would probably go to additional efforts to reuse such deleted > filenames, so there would be no such "gaps". > > So I am now wondering if using > tempfile.NamedTemporaryFile(delete=False) would solve this problem > nicely? As I am not very familiar with this library, are there any > unforeseen issues I should be made aware of? Would this work equally > well on all operating systems? > > TIA! > This sounds like a good though surprising use case. The only odd thing about this is the misleading name then of the function, plus there is the (vague) possibility that the stdlib module might evolve and no longer support this undocumented (given that the first sentence in the module description reads: "This module creates temporary files and directories.") use case. I would probably address these issues and the handling of the dir parameter via a partial function like this: from functools import partial SavedGameFile = partial( tempfile.NamedTemporaryFile, dir=my_saved_games_dir ) # if you like also set the suffix here SavedGameFile.__doc__ = """\ Create and return a saved game file, ... """ This way you have a good name for the function, and you can redefine the function, if you ever want/need to move away from NamedTemporaryFile, without having to rewrite every function call. Wolfgang From robertvstepp at gmail.com Mon Oct 22 10:24:34 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 22 Oct 2018 09:24:34 -0500 Subject: [Tutor] Fwd: Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: Forwarding to the Tutor list. Herr Maier offers a good idea that would take away much of a remaining issue -- the name "Temporary". I need to look into the functools library to see what "partial" does. ---------- Forwarded message --------- From: Wolfgang Maier Date: Mon, Oct 22, 2018 at 5:25 AM Subject: Re: Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? To: boB Stepp On 21.10.18 08:13, boB Stepp wrote: > Use case: I want to allow a user of my Solitaire Scorekeeper program > to be able to give any name he wants to each game of solitaire he > wishes to record. My thought for permanent storage of each game's > parameters is to use a dictionary to map the user-chosen game names to > a unique json filename. This way I hope no shenanigans can occur with > weird characters/non-printing characters. > > My initial thought was to just have a sequence of game names with > incrementing numerical suffixes: game_0, game_1, ... , game_n. But > this would require the program to keep track of what the next > available numerical suffix is. Additionally, if a user chooses to > delete a game, then there would be a gap in the numerical sequence of > the game filenames. I find such a gap aesthetically displeasing and > would probably go to additional efforts to reuse such deleted > filenames, so there would be no such "gaps". > > So I am now wondering if using > tempfile.NamedTemporaryFile(delete=False) would solve this problem > nicely? As I am not very familiar with this library, are there any > unforeseen issues I should be made aware of? Would this work equally > well on all operating systems? > > TIA! > This sounds like a good though surprising use case. The only odd thing about this is the misleading name then of the function, plus there is the (vague) possibility that the stdlib module might evolve and no longer support this undocumented (given that the first sentence in the module description reads: "This module creates temporary files and directories.") use case. I would probably address these issues and the handling of the dir parameter via a partial function like this: from functools import partial SavedGameFile = partial( tempfile.NamedTemporaryFile, dir=my_saved_games_dir ) # if you like also set the suffix here SavedGameFile.__doc__ = """\ Create and return a saved game file, ... """ This way you have a good name for the function, and you can redefine the function, if you ever want/need to move away from NamedTemporaryFile, without having to rewrite every function call. Wolfgang -- boB From mats at wichmann.us Mon Oct 22 10:46:16 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 22 Oct 2018 08:46:16 -0600 Subject: [Tutor] What is the best way for a program suite to know where it is installed? In-Reply-To: References: Message-ID: <49e421a2-80c0-c9d3-5a94-5eb52ba70987@wichmann.us> On 10/20/18 9:00 PM, boB Stepp wrote: > I was just re-reading the entire thread at > https://www.mail-archive.com/tutor at python.org/msg77864.html > And I have asked similar questions previous to that thread. I still > do not have a satisfactory answer for the subject line's question that > both makes sense to me and seems to be good programming practice. And > that works for any OS that the program suite is installed under. > > A constantly recurring situation I am having is a programming project > with a nested directory structure. Usually I will have "tests" and > "data" folders nested under the top level project folder. There may > be others as well depending on the complexity of the project I am > attempting. As far as I can tell, I cannot make any assumptions about > what the current working directory is when a user starts the program. > So how can one of my programs *know* what the absolute path is to, > say, the top level of the program suite? If I could always reliably > determine this by my program's code, every other location could be > determined from the known nested structure of the program suite. [But > I have a dim recollection that either Ben or Cameron thought relying > on the "known nested structure" is brittle at best.] > > So far the best method I've come up with is to make use of "__file__" > for the initiating program file. But from past discussions I am not > certain this is the *best* way. Is the *best* way going to get me > into best techniques for installing and distributing programs? If so, > this strikes me as a huge mess to dive into! > > TIA! > As you've concluded by now, this isn't a completely trivial exercise, and you'll possibly get asked the question "why do you think you need to know that?". Probably though this is considered one of the more reliable ways: import os if __name__ == '__main__': script_path = os.path.dirname(os.path.realpath(__file__)) print("Main module at", script_path) From mats at wichmann.us Mon Oct 22 12:56:38 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 22 Oct 2018 10:56:38 -0600 Subject: [Tutor] Fwd: Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: <8ac1ef76-63aa-8006-d2aa-492a450f8fc9@wichmann.us> On 10/22/18 8:24 AM, boB Stepp wrote: > Forwarding to the Tutor list. Herr Maier offers a good idea that > would take away much of a remaining issue -- the name "Temporary". I > need to look into the functools library to see what "partial" does. if you really don't care what the file is called because you will maintain a map which leads you to the filename, you might as well use a uuid. From mlathkar at gmail.com Mon Oct 22 11:58:55 2018 From: mlathkar at gmail.com (Malhar Lathkar) Date: Mon, 22 Oct 2018 21:28:55 +0530 Subject: [Tutor] organisation of folder structure of Python'sinstallation directory In-Reply-To: <1540132972755.1149562669@boxbe> References: <1540132972755.1149562669@boxbe> Message-ID: Is it possible to install just python runtime environment only similar to JRE? On Sun, 21 Oct 2018, 20:12 Mats Wichmann, wrote: > [image: Boxbe] Mats Wichmann ( > mats at wichmann.us) is not on your Guest List > > | Approve sender > > | Approve domain > > On 10/21/18 12:52 AM, Malhar Lathkar wrote: > > Hi all. > > > > I am trying to understand how Python's installation directory is > organized. > > I find lib as well as libs folder. what's the difference? I couldn't find > > exact explanation of purpose of various other sub folders like include, > > scripts, share etc. Can you help me understand this? Please share > > documentation link if any. > > > > thanks. > > > > on a Windows installation, Lib is the python code for the standard > library, while DLLs contains any backing binary objects for the standard > library; meanwhile libs is what is necessary to to link with the python > libraries themselves, as when developing a C-language extension. include > is the header files used for compiling such extensions - so these two > are not part of the Python runtime, but have this special purpose. > (note sometimes when you "python -m pip install" it may try to compile > some code, so this is not always just for you yourself to write > extensions). > > The rest should be relatively straightforward. > > The layout can be quite different on other systems. A /usr/share > directory on Linux is used here for shareable, non-binary pieces like > documentation and license files. The python layout on Linux is normally > packaged along the lines of the Filesystem Hierarchy Standard: > > https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mats at wichmann.us Mon Oct 22 15:15:03 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 22 Oct 2018 13:15:03 -0600 Subject: [Tutor] organisation of folder structure of Python'sinstallation directory In-Reply-To: References: <1540132972755.1149562669@boxbe> Message-ID: <58dafe9f-98a6-b6fc-5a68-4694ca7eac05@wichmann.us> On 10/22/18 9:58 AM, Malhar Lathkar wrote: > Is it possible to install just python runtime environment only similar to > JRE? You could probably arrange it if you build your own, but there doesn't seem much point. I'm looking at a Windows system here with several Python versions installed; for a recent Python, it takes 126,500kb of which the two pieces related to compilation, libs and Include, take 2500kb - less than 2% of the total. From robertvstepp at gmail.com Mon Oct 22 20:26:46 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 22 Oct 2018 19:26:46 -0500 Subject: [Tutor] What is the best way for a program suite to know where it is installed? In-Reply-To: <49e421a2-80c0-c9d3-5a94-5eb52ba70987@wichmann.us> References: <49e421a2-80c0-c9d3-5a94-5eb52ba70987@wichmann.us> Message-ID: On Mon, Oct 22, 2018 at 9:47 AM Mats Wichmann wrote: > > On 10/20/18 9:00 PM, boB Stepp wrote: > > So far the best method I've come up with is to make use of "__file__" > > for the initiating program file. But from past discussions I am not > > certain this is the *best* way. Is the *best* way going to get me > > into best techniques for installing and distributing programs? If so, > > this strikes me as a huge mess to dive into! > > > > TIA! > > > > As you've concluded by now, this isn't a completely trivial exercise, > and you'll possibly get asked the question "why do you think you need to > know that?". Actually no one has yet asked me this question, and you have me intrigued. Importing the various program modules/module contents is no issue. Where I believe I need to know the paths to things are to get to data folders, config files, and occasionally utility programs that I have written that are on my hard drive, but not copied to my current program suite. Unless there is some built-into-Python-easy way to find such things, I do not see any other alternative than determining the path to the desired files. Is there a better way? > Probably though this is considered one of the more reliable ways: > > import os > > if __name__ == '__main__': > script_path = os.path.dirname(os.path.realpath(__file__)) > print("Main module at", script_path) Despite the past thread I mentioned in this thread's originating post, I have switched to using pathlib.Path for doing this sort of thing. Something I read on the main list in the past year I found convincing, though I now don't remember now why I was so convinced. -- boB From robertvstepp at gmail.com Mon Oct 22 20:30:45 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 22 Oct 2018 19:30:45 -0500 Subject: [Tutor] Fwd: Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: <8ac1ef76-63aa-8006-d2aa-492a450f8fc9@wichmann.us> References: <8ac1ef76-63aa-8006-d2aa-492a450f8fc9@wichmann.us> Message-ID: On Mon, Oct 22, 2018 at 11:57 AM Mats Wichmann wrote: > > On 10/22/18 8:24 AM, boB Stepp wrote: > > Forwarding to the Tutor list. Herr Maier offers a good idea that > > would take away much of a remaining issue -- the name "Temporary". I > > need to look into the functools library to see what "partial" does. > > > if you really don't care what the file is called because you will > maintain a map which leads you to the filename, you might as well use a > uuid. Wouldn't I have to write a check to ensure such a named file (However unlikely that would be.) did not exist before creating it? And if yes, would not that get into the same potential security issue that cause tempfile.mktemp() to be deprecated? -- boB From wrw at mac.com Mon Oct 22 21:26:46 2018 From: wrw at mac.com (William Ray Wing) Date: Mon, 22 Oct 2018 21:26:46 -0400 Subject: [Tutor] Fwd: Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: <8ac1ef76-63aa-8006-d2aa-492a450f8fc9@wichmann.us> Message-ID: <280F4451-350C-47CD-9566-D27C7CE27EDB@mac.com> > On Oct 22, 2018, at 8:30 PM, boB Stepp wrote: > > On Mon, Oct 22, 2018 at 11:57 AM Mats Wichmann wrote: >> >> On 10/22/18 8:24 AM, boB Stepp wrote: >>> Forwarding to the Tutor list. Herr Maier offers a good idea that >>> would take away much of a remaining issue -- the name "Temporary". I >>> need to look into the functools library to see what "partial" does. >> >> >> if you really don't care what the file is called because you will >> maintain a map which leads you to the filename, you might as well use a >> uuid. > > Wouldn't I have to write a check to ensure such a named file (However > unlikely that would be.) did not exist before creating it? And if > yes, would not that get into the same potential security issue that > cause tempfile.mktemp() to be deprecated? > The whole point of UUIDs is to make the probability of a UUID collision so infinitesimally small as to make that hypothetical collision simply not worth worrying about, even when they are created on different systems. Since a big chunk of a UUID is a high precision time stamp, any UUIDs created on a single system are pretty much guaranteed to be unique. Bill > -- > boB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From eryksun at gmail.com Mon Oct 22 23:06:50 2018 From: eryksun at gmail.com (eryk sun) Date: Mon, 22 Oct 2018 22:06:50 -0500 Subject: [Tutor] What is the best way for a program suite to know where it is installed? In-Reply-To: References: <49e421a2-80c0-c9d3-5a94-5eb52ba70987@wichmann.us> Message-ID: On 10/22/18, boB Stepp wrote: > > Importing the various program modules/module contents is > no issue. Where I believe I need to know the paths to things are to > get to data folders, config files, and occasionally utility programs > that I have written that are on my hard drive, but not copied to my > current program suite. It's common to use __file__ for a portable application, except you should use sys.executable if it's a frozen executable, i.e. if sys.frozen exists. For an installed application, limit this approach to the application's immutable resources. Don't use the installation directory to store modifiable data. You may not have write access (e.g. a system installation and the user lacks root/admin access, or it's an immutable directory or read-only disk mount). From cs at cskk.id.au Mon Oct 22 23:19:20 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 23 Oct 2018 14:19:20 +1100 Subject: [Tutor] What is the best way for a program suite to know where it is installed? In-Reply-To: References: Message-ID: <20181023031920.GA74310@cskk.homeip.net> On 22Oct2018 19:26, boB Stepp wrote: >On Mon, Oct 22, 2018 at 9:47 AM Mats Wichmann wrote: >> As you've concluded by now, this isn't a completely trivial exercise, >> and you'll possibly get asked the question "why do you think you need to >> know that?". > >Actually no one has yet asked me this question, and you have me >intrigued. Importing the various program modules/module contents is >no issue. Where I believe I need to know the paths to things are to >get to data folders, config files, and occasionally utility programs >that I have written that are on my hard drive, but not copied to my >current program suite. Data folders inbuilt to your app? (I'm going to call it an "app" purely for convenience.) Not sure there's a universally good answer for this. You can include data files in your package I think (haven't tried this, as far as PyPI/pip goes), which means you could locate them from your source code. Happy to hear more on this from others. Config files: I'm coming to be of the opinion, based on a couple of things I'm writiing at the moment, that apps should (a) come with a builtin default config (possibly computed form the environment the find themselves executed in) and that (b) that config should be expressed in a class instance which can transcribe itself to a file in the right format. Why? It solves the "where is it" problem: it is in the code, as some function which constructs the app's config instance in the absense of a config file. It solves the "how does the user get their first config file for later hacking/customisation"? If the config file is not in the well known place (~/.your-apprc or ~/.config/something, etc) then having made your default config you can transcribe it to the default place. Now the user has a file they can modify. It also solves the "incomplete config file" problem: if you can construct a default config, you can usually handle an incomplete config as "construct the default, then alter based on the contents of the config file". Which lets the user keep a tiny config file modifying only the stuff which needs tweaking. Utilities: I my opinion, unless they shift with you app it is the end user's job to have these in the execution path ($PATH on UNIX). If you app/package provides them you need a bin directory, with the same issues as your data files. Cheers, Cameron Simpson From avigross at verizon.net Mon Oct 22 22:46:44 2018 From: avigross at verizon.net (Avi Gross) Date: Mon, 22 Oct 2018 22:46:44 -0400 Subject: [Tutor] Fwd: Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: <8ac1ef76-63aa-8006-d2aa-492a450f8fc9@wichmann.us> Message-ID: <002e01d46a7a$a2bb8660$e8329320$@verizon.net> Since this topic is not focused on efficiency, why exactly does it matter if your function should check if a file exists and then avoid a collision? What you are describing sounds a bit like a hash function. In many cases, when you perform the computations you may find the slot you hash to is already occupied. A common solution is to check and if needed make some adjustment like adding 1 or using a second hash function or changing the thing being hashed in a deterministic way like changing "key" to "keykey" and even "keykeykey" before hashing till you get an empty slot. Note in Python, you can simply use a dictionary and have no idea how the hash is done. Since you are describing a need for a permanent file, many suggested ways of making a unique file name can repeat and thus not be unique. The process ID wraps around and maybe restarts every time the machine boots, I suspect. Dates repeat unless they include the year as in 20181022221903 to the nearest second. If you are concerned about two files being created in the same second, there is a trivial solution. Before or after creating the file, sleep for a time period like a second so any "second" saved file is guaranteed to come at least a second later, in the above scheme. I heard a concern about what happens if you just use sequence numbers as in file00003 then file00004 if the user later deletes some. The concern was subtle about what happens if a file is later deleted and your algorithm later will search for the first available empty slot. To each their own but it strikes me as fairly easy in Python to not check EVERY filename but skip past the last one. You have functions that return a list of filenames in a directory as a list of strings. In my example, you could take the list and replace "file" with nothing to get [ "00001", "00002", "00004", ... ] Then in memory, efficiently, without opening a single file, you can sort the strings, pop off the last one, convert it to an int, add one, make a new file name like "file00111" that is guaranteed not to exist unless some other program sneaks it in, and continue. There are lots of ideas you can try. This would only need to happen perhaps once per game and you can encapsulate the logic in your own function so all your code will say is something like: save_game_info(...) Of course if someone else had already come up with similar functionality, use it. Oddly, you may not be aware that your method indicates thinking too much inside the box. Instead of saving files on your own, consider creating a data structure and letting known modules save it in the file system and retrieve it later. There are packages like pickle and shelve. https://docs.python.org/3/library/pickle.html https://docs.python.org/3/library/shelve.html So if you load an existing object, such as a dictionary or list before a game starts, and after each game extend the object and then save it to disk this way, then you don't need to deal with details as long as the program knows where it is stored. The person running the program may not trivially be able to access the data and is not likely to delete easily. The actual file names may depend on what the package does and you may find other such packages better fit your needs and let you easily save all kinds of info like scores and time elapsed and number of moves as you are saving entire data structures you create and control. Just some thoughts. Avi -----Original Message----- From: Tutor On Behalf Of boB Stepp Sent: Monday, October 22, 2018 8:31 PM To: tutor Subject: Re: [Tutor] Fwd: Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? On Mon, Oct 22, 2018 at 11:57 AM Mats Wichmann wrote: > > On 10/22/18 8:24 AM, boB Stepp wrote: > > Forwarding to the Tutor list. Herr Maier offers a good idea that > > would take away much of a remaining issue -- the name "Temporary". > > I need to look into the functools library to see what "partial" does. > > > if you really don't care what the file is called because you will > maintain a map which leads you to the filename, you might as well use > a uuid. Wouldn't I have to write a check to ensure such a named file (However unlikely that would be.) did not exist before creating it? And if yes, would not that get into the same potential security issue that cause tempfile.mktemp() to be deprecated? -- boB _______________________________________________ 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 Oct 23 05:24:28 2018 From: __peter__ at web.de (Peter Otten) Date: Tue, 23 Oct 2018 11:24:28 +0200 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? References: <20181021230258.GA30714@cskk.homeip.net> Message-ID: Cameron Simpson wrote: > On 21Oct2018 10:55, Peter Otten <__peter__ at web.de> wrote: >>boB Stepp wrote: >>> So I am now wondering if using >>> tempfile.NamedTemporaryFile(delete=False) would solve this problem >>> nicely? As I am not very familiar with this library, are there any >>> unforeseen issues I should be made aware of? Would this work equally >>> well on all operating systems? >> >>I think this is cool thinking outside of the box. >> >>I would not have "dared" this, but now you suggest it I cannot see >>anything wrong with your approach. > > The doco for mktemp (do not use! use mkstemp or the NamedTemporaryFile > classes instead!) explicitly mentions using delete=False. Well, "permanent temporary file" does sound odd. By the way, NamedTemporaryFile returns a proxy instead of the file itself. In some rare cases that could be a problem. Would mktemp() really be dangerous if you used it like this, def new_game(directory): for _retry in range(3): filename = mktemp("game_", ".json", dir=directory) try: return open(filename, "x") except FileExistsError: pass raise FileExistsError with the "x" mode? From cs at cskk.id.au Tue Oct 23 07:49:52 2018 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 23 Oct 2018 22:49:52 +1100 Subject: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files? In-Reply-To: References: Message-ID: <20181023114952.GA85365@cskk.homeip.net> On 23Oct2018 11:24, Peter Otten <__peter__ at web.de> wrote: >Cameron Simpson wrote: >> The doco for mktemp (do not use! use mkstemp or the >> NamedTemporaryFile >> classes instead!) explicitly mentions using delete=False. > >Well, "permanent temporary file" does sound odd. > >By the way, NamedTemporaryFile returns a proxy instead of the file itself. >In some rare cases that could be a problem. > >Would mktemp() really be dangerous if you used it like this, > >def new_game(directory): > for _retry in range(3): > filename = mktemp("game_", ".json", dir=directory) > try: > return open(filename, "x") > except FileExistsError: > pass > raise FileExistsError > >with the "x" mode? In terms of a race, maybe not. But in terms of security? Probably. Consider: the issue with mktemp is that it can be switched out before use. So: Alice: mktemp() -> filename Mallory: guess filename, put a symlink there pointing at a file which doesn't exist, but which has an effect if it does. For example, in ancient windows, an autorun.ini file. Or cooler, on UNIX, a file in /etc/cron.d. Alice: write to filename, not accidentally _creating_ the target of the symlink, now writing a file somewhere unwanted. Now, the examples above pretend that Alice has root privileges so that Mallory affects a root run system. But for Alice, it is just as bad if Mallory just subverts her personal account via some other pathname. Also, there's the issue of privacy: open(), in your example, will use the default umask, which may be more open than one wants. And so on... Cheers, Cameron Simpson From __peter__ at web.de Tue Oct 23 09:02:01 2018 From: __peter__ at web.de (Peter Otten) Date: Tue, 23 Oct 2018 15:02:01 +0200 Subject: [Tutor] What is the best way for a program suite to know where it is installed? References: <49e421a2-80c0-c9d3-5a94-5eb52ba70987@wichmann.us> Message-ID: eryk sun wrote: > On 10/22/18, boB Stepp wrote: >> >> Importing the various program modules/module contents is >> no issue. Where I believe I need to know the paths to things are to >> get to data folders, config files, and occasionally utility programs >> that I have written that are on my hard drive, but not copied to my >> current program suite. > > It's common to use __file__ for a portable application, except you > should use sys.executable if it's a frozen executable, i.e. if > sys.frozen exists. > > For an installed application, limit this approach to the application's > immutable resources. Don't use the installation directory to store > modifiable data. You may not have write access (e.g. a system > installation and the user lacks root/admin access, or it's an > immutable directory or read-only disk mount). I've not tried it, but for per-app read-only data importlib.resources appears to be the right tool. https://docs.python.org/dev/library/importlib.html#module-importlib.resources From jf_byrnes at comcast.net Thu Oct 25 17:07:06 2018 From: jf_byrnes at comcast.net (Jim) Date: Thu, 25 Oct 2018 16:07:06 -0500 Subject: [Tutor] Python 3.6 update? Message-ID: Mint 18.1 Default python3 = 3.5 Python 3.6 installed for use by a virtual environment. Update manager just informed me of an update for python 3.6 to 3.6.7-1. When I started to install it, I got a dialog saying: this upgrade will trigger additional changes. The following 2 packages will be removed python3-dev & python3-venv. I am wondering what will happen to the virtual environment I have installed that uses python 3.6. Will the 2 packages be replaced and the virtual environment continue to work or is there some other steps I will need to take. Regards, Jim From mats at wichmann.us Thu Oct 25 18:30:08 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 25 Oct 2018 16:30:08 -0600 Subject: [Tutor] Python 3.6 update? In-Reply-To: References: Message-ID: <98594068-fbb5-f301-d1fb-a91843d31b90@wichmann.us> On 10/25/2018 03:07 PM, Jim wrote: > Mint 18.1 > Default python3 = 3.5 > Python 3.6 installed for use by a virtual environment. > > > > Update manager just informed me of an update for python 3.6 to 3.6.7-1. > When I started to install it, I got a dialog saying: > > this upgrade will trigger additional changes. The following 2 packages > will be removed? python3-dev & python3-venv. > > I am wondering what will happen to the virtual environment I have > installed that uses python 3.6. Will the 2 packages be replaced and the > virtual environment continue to work or is there some other steps I will > need to take. > > Regards,? Jim we can't tell that from here. Not sure whose bright idea it was to package venv separately, since it's supposed to be a "standard" part of Python 3 since 3.3 (I guess it's "batteries included, but you still have to install the batteries" :). But removing the package doesn't affect a created virtual environment. What can affect a created virtual environment is how it was created - there are a few options, some combinations of which could leave things going missing from underneath if the Python it's created from is upgraded. venv has an upgrade option that might help with that. Mint forums (and probably Ubuntu forums) could be further help... usually somebody's been through it before. From benjamin.placella at maine.edu Thu Oct 25 18:13:31 2018 From: benjamin.placella at maine.edu (Ben Placella) Date: Thu, 25 Oct 2018 18:13:31 -0400 Subject: [Tutor] Python Help (shits killing me) Message-ID: So I have to make a fibonacci sequence, and I'm not sure what is wrong with my code #This program illustrates the fibonacci sequence nterms=int(input("Please enter how many terms you would like to know: ")) n1 = 1 n2 = 1 count = 0 if nterms <= 0: print("Please enter a positive integer") elif nterms == 1: print("Fibonacci sequence upto",nterms,":") print(n1) else: print("Fibonacci sequence upto",nterms,":") while count < nterms: print(n1,end=' , ') nth = n1 + n n1 = n2 n2 = nth count += 1 attached is a photo of what the output SHOULD look like From benjamin.placella at maine.edu Thu Oct 25 18:14:41 2018 From: benjamin.placella at maine.edu (Ben Placella) Date: Thu, 25 Oct 2018 18:14:41 -0400 Subject: [Tutor] Python Help Message-ID: I need to write code that runs a cost calculating program with many different variables and I honestly don't understand it, my code is: beefmeals=int(input("Enter number of beef meals: ")) shitmeals=int(input("Enter number of vegan meals: ")) party=beefmeals+shitmeals print(party) if party<=50 a=75 print("Room cost $75") elif party <=150 b=150 print("Room cost $150") else c=250 print("Room cost $250") roomtax=party*0.065 print(roomtax) print("Beef Meals", beefmeals) $beef=(beefmeals*15.95) print($beef) print("Beef cost", $$beef) print("Vegan Meals", shitmeals) $shit=(shitmeals*10.95) print($shit) cost=($beef+$shit) grat=cost*0.18) print(grat) GT=(grat+$beef+$shit+(a,b,c)) print(GT) This is what the output is supposed to be: From andrew.vanvalkenburg at gmail.com Fri Oct 26 06:15:30 2018 From: andrew.vanvalkenburg at gmail.com (Andrew Van Valkenburg) Date: Fri, 26 Oct 2018 06:15:30 -0400 Subject: [Tutor] Python Help (shits killing me) In-Reply-To: References: Message-ID: nth = n1 + n I don't see where n is defined anywhere. Should be n2? On Fri, Oct 26, 2018 at 6:09 AM Ben Placella wrote: > So I have to make a fibonacci sequence, and I'm not sure what is wrong with > my code > #This program illustrates the fibonacci sequence > nterms=int(input("Please enter how many terms you would like to know: ")) > n1 = 1 > n2 = 1 > count = 0 > if nterms <= 0: > print("Please enter a positive integer") > elif nterms == 1: > print("Fibonacci sequence upto",nterms,":") > print(n1) > else: > print("Fibonacci sequence upto",nterms,":") > while count < nterms: > print(n1,end=' , ') > nth = n1 + n > n1 = n2 > n2 = nth > count += 1 > > attached is a photo of what the output SHOULD look like > _______________________________________________ > 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 Fri Oct 26 06:21:31 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Oct 2018 11:21:31 +0100 Subject: [Tutor] Python Help (shits killing me) In-Reply-To: References: Message-ID: Plain text is preferred for code since otherwise the mail system removes all indentation making the code hard to understand. On 25/10/2018 23:13, Ben Placella wrote: > So I have to make a fibonacci sequence, and I'm not sure what is wrong with > my code > #This program illustrates the fibonacci sequence > nterms=int(input("Please enter how many terms you would like to know: ")) > n1 = 1 > n2 = 1 > count = 0 > if nterms <= 0: > print("Please enter a positive integer") > elif nterms == 1: > print("Fibonacci sequence upto",nterms,":") > print(n1) > else: > print("Fibonacci sequence upto",nterms,":") > while count < nterms: > print(n1,end=' , ') > nth = n1 + n I suspect that n should be n2? > n1 = n2 > n2 = nth BTW in Python you can do that set of assignments more concisely as n1,n2 = n2, n1+n2 with no temporary variable required. > count += 1 And rather than using a while loop and manually maintaining a count you could just use a for loop: for count in range(nterms): It's slightly more reliable and a lot more "pythonic". > attached is a photo of what the output SHOULD look like This is a text only list so images or other binary files are stripped off by the server. Please, in future, cut n' paste any output 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 steve at pearwood.info Fri Oct 26 06:47:27 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 26 Oct 2018 21:47:27 +1100 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: <20181026104726.GQ3817@ando.pearwood.info> On Thu, Oct 25, 2018 at 06:14:41PM -0400, Ben Placella wrote: > I need to write code that runs a cost calculating program with many > different variables and I honestly don't understand it, my code is: > beefmeals=int(input("Enter number of beef meals: ")) > shitmeals=int(input("Enter number of vegan meals: ")) What version of Python are you using? > party=beefmeals+shitmeals > print(party) > if party<=50 > a=75 You have lost the indentation, making your code invalid. Also, you are probably getting a SyntaxError when you try to run your code. As always, please COPY AND PASTE (don't retype it from memory, or take a photo) the entire traceback, starting from the line "Traceback..." and ending with the error message. > print("Room cost $75") > elif party <=150 > b=150 > print("Room cost $150") > else > c=250 > print("Room cost $250") That's two more syntax errors. > $beef=(beefmeals*15.95) Dollar signs aren't used for Python variables. From steve at pearwood.info Fri Oct 26 06:43:01 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 26 Oct 2018 21:43:01 +1100 Subject: [Tutor] Python Help (shits killing me) In-Reply-To: References: Message-ID: <20181026104300.GP3817@ando.pearwood.info> On Thu, Oct 25, 2018 at 06:13:31PM -0400, Ben Placella wrote: > So I have to make a fibonacci sequence, and I'm not sure what is wrong with > my code [...] > attached is a photo of what the output SHOULD look like No it isn't. For security (anti-spam, anti-virus) reasons, this mailing list deletes non-text attachments. Code is text. Unless you edit your code with Photoshop, don't take screenshots or photos of the code. COPY AND PASTE the code, and any expected results, or error messages, as TEXT. Text can be copied, edited, and pasted into the interpreter and run. Photos of text can't be edited, or run in the interpeter. Screen-readers can't read them, making them invisible to the blind or visually impaired. Photos of text are useless for programming. When you run your code, what error do you get? COPY AND PASTE the entire traceback, starting from the line "Traceback: ..." and including the error message at the end. -- Steve From alan.gauld at yahoo.co.uk Fri Oct 26 07:14:12 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Oct 2018 12:14:12 +0100 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: On 25/10/2018 23:14, Ben Placella wrote: Please always post code in plain text not HTML or Rich text. Otherwise we lose all the formatting which is important in Python. > beefmeals=int(input("Enter number of beef meals: ")) > shitmeals=int(input("Enter number of vegan meals: ")) > party=beefmeals+shitmeals > print(party) > if party<=50 Python control statements are terminated by a colon(:) > $beef=(beefmeals*15.95) $ signs in front of variable names are an error in Python -- 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 bgailer at gmail.com Fri Oct 26 07:26:06 2018 From: bgailer at gmail.com (Bob Gailer) Date: Fri, 26 Oct 2018 07:26:06 -0400 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: On Oct 26, 2018 6:11 AM, "Ben Placella" wrote: > > I need to write code that runs a cost calculating program with many > different variables and I honestly don't understand it Could you be more specific? What exactly don't you understand, or even better what do you understand? my code is: How could you have written so much code without understanding it? > beefmeals=int(input("Enter number of beef meals: ")) > shitmeals=int(input("Enter number of vegan meals: ")) > party=beefmeals+shitmeals > print(party) > if party<=50 Something is missing from that last statement. Can you tell what it is? Do you know how to find out? Hint use help. Hint 2 it is also missing from the elif and else statements. > a=75 > print("Room cost $75") > elif party <=150 > b=150 > print("Room cost $150") > else > c=250 > print("Room cost $250") > roomtax=party*0.065 > print(roomtax) > print("Beef Meals", beefmeals) > $beef=(beefmeals*15.95) > print($beef) > print("Beef cost", $$beef) > print("Vegan Meals", shitmeals) > $shit=(shitmeals*10.95) > print($shit) > cost=($beef+$shit) > grat=cost*0.18) > print(grat) > GT=(grat+$beef+$shit+(a,b,c)) There is a convention in Python that and all uppercase name is a constant. This is not a requirement. > print(GT) > > This is what the output is supposed to be: I don't see any output here. Alan''s responses may help you figure that out. From asad.hasan2004 at gmail.com Fri Oct 26 07:33:01 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Fri, 26 Oct 2018 17:03:01 +0530 Subject: [Tutor] How to print lines within two timestamp In-Reply-To: References: <80064E7A-7ED8-4898-B978-8BF72D05F643@me.com> <88f88ec5-afaa-8e1f-34c3-0a64edd32ceb@gmail.com> <1B9C3D36-CF02-4EFC-81F6-0BC90C923501@gmail.com> <756EF8A1-D02B-4151-B390-52036C93DC4E@gmail.com> Message-ID: Hi , Yes i have the code : import re import datetime from datetime import timedelta Header = "*****************************************************" f3 = open ( r"D:\QI\logA.txt", 'r' ) string = f3.read () regex = re.compile ( "\n" ) st = regex.sub ( " ", string ) st1 = st.split ( " " ) if re.search ('ERR-1:', st ): x = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", st ) j = x[0][0] + " "+ x[0][1]+" " + x[0][2] +":"+ x[0][3]+":" + x[0][4]+" " + x[0][5] h = x[1][0] + " "+ x[1][1]+" "+ x[1][2] +":" + x[1][3] +":"+ x[1][4] +" "+ x[1][5] y = datetime.datetime.strptime ( j, '%b %d %H:%M:%S %Y' ) print y k = datetime.datetime.strptime ( h, '%b %d %H:%M:%S %Y' ) print k f4 = open ( r"D:\QI\logC11.txt", 'r' ) string1 = f4.read () reg = re.compile ( "\n" ) newst = reg.sub ( " ", string1 ) newst1 = newst.split ( " " ) if re.search ( "ERR-2", newst ): a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", newst ) for i in range ( len ( a ) ): newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' ) if newtime > y and newtime < k: print "Install patch1" if re.search ( "ERR-3", newst ): a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", newst ) for i in range ( len ( a ) ): newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' ) if newtime > y and newtime < k: print newtime, y, k print "Install patch2" ============================================================================================== output i get : *Install patch1 - wrong solution 2018-10-22 10:21:23 2018-10-22 10:21:15 2018-10-22 10:21:25 Install patch2 - correct solution * *It should have only searched between timestamps **2018-10-22 10:21:15 2018-10-22 10:21:25* *in logC11.txt what I am doing wrong please adice stuck on this for long.* -------------- next part -------------- Patching tool version 12.2.0.1.0 Production on Mon Oct 22 10:21:15 2018 verify_queryable_inventory returned ERR-1: Latest xml inventory is not loaded into table Prereq check failed, exiting without installing any patches. Patching tool complete on Mon Oct 22 10:21:25 2018 -------------- next part -------------- LOG file opened at 04/26/18 06:11:52 ERR-2: OS message: No child processes operation "wait", location "skudmi:prp:6" LOG file opened at 10/22/18 09:10:25 ERR-3: patchObjectPossible causes are:" Trim whitespace same as SQL Loader LOG file opened at 10/22/18 10:21:23 ERR-3: patchObjectPossible causes are:" Trim whitespace same as SQL Loader LOG file opened at 10/22/18 10:22:25 ERR-3: patchObjectPossible causes are:" Trim whitespace same as SQL Loader From alan.gauld at yahoo.co.uk Fri Oct 26 13:45:17 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Oct 2018 18:45:17 +0100 Subject: [Tutor] How to print lines within two timestamp In-Reply-To: References: <80064E7A-7ED8-4898-B978-8BF72D05F643@me.com> <88f88ec5-afaa-8e1f-34c3-0a64edd32ceb@gmail.com> <1B9C3D36-CF02-4EFC-81F6-0BC90C923501@gmail.com> <756EF8A1-D02B-4151-B390-52036C93DC4E@gmail.com> Message-ID: On 26/10/2018 12:33, Asad wrote: > Hi , > > Yes i have the code : It woiyukld help us to help you if you provided some clues as to what it was doing. A good start would be some comments - especially around the regexes. Don't make us parse them without some idea of what you are expecting. Also moremeaningful variable names than h,j,k etc > import re > import datetime > from datetime import timedelta You don't appear to use timedelta? > Header = "*****************************************************" > > f3 = open ( r"D:\QI\logA.txt", 'r' ) > string = f3.read () > regex = re.compile ( "\n" ) > st = regex.sub ( " ", string ) I suspect regular string methods would be simpler here. > st1 = st.split ( " " ) > > if re.search ('ERR-1:', st ): > x = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", st ) > j = x[0][0] + " "+ x[0][1]+" " + x[0][2] +":"+ x[0][3]+":" + > x[0][4]+" " + x[0][5] > h = x[1][0] + " "+ x[1][1]+" "+ x[1][2] +":" + x[1][3] +":"+ > x[1][4] +" "+ x[1][5] I'm not sure what exactly this is doing, but I suspect datetime.strftime might do it better. > y = datetime.datetime.strptime ( j, '%b %d %H:%M:%S %Y' ) > print y > k = datetime.datetime.strptime ( h, '%b %d %H:%M:%S %Y' ) > print k > > f4 = open ( r"D:\QI\logC11.txt", 'r' ) > > string1 = f4.read () > reg = re.compile ( "\n" ) > newst = reg.sub ( " ", string1 ) > newst1 = newst.split ( " " ) > > if re.search ( "ERR-2", newst ): > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", newst ) > for i in range ( len ( a ) ): Would this not be simpler as for result in a: and use result instead of a[i] > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' ) > if newtime > y and newtime < k: You should be able to write this as if y < newtime < k: > print "Install patch1" > > if re.search ( "ERR-3", newst ): > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", newst ) > for i in range ( len ( a ) ): > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' ) > if newtime > y and newtime < k: > print newtime, y, k > print "Install patch2" > > ============================================================================================== > > output i get : Can you show us the full output? It should start with your header line? > *Install patch1 - wrong solution > 2018-10-22 10:21:23 2018-10-22 10:21:15 2018-10-22 10:21:25 > Install patch2 - correct solution * > > > *It should have only searched between timestamps **2018-10-22 10:21:15 > 2018-10-22 10:21:25* Going by the above output that's exactly what it did? And it found 2018-10-22 10:21:23 I'm not clear what you expected. -- 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 bgailer at gmail.com Fri Oct 26 15:03:36 2018 From: bgailer at gmail.com (Bob Gailer) Date: Fri, 26 Oct 2018 15:03:36 -0400 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: On Oct 26, 2018 1:20 PM, "Adam Eyring" wrote: > > Try this cleaned up version with colons in the right places, dollar signs removed, and other corrections: Does it do what you want? > beefmeals=int(input("Enter number of beef meals: ")) > shitmeals=int(input("Enter number of vegan meals: ")) > party = beefmeals + shitmeals > print("Total meals", party) > a = 0 > b = 0 > c = 0 There is no need for three variables here. You only need one to represent room cost. If you make that change then you will also not need to initialize the room cost variable. Makes the code simpler to maintain and read and understand. > if party <= 50: > > a=75 > print("Room cost $75") If you use one variable for room cost then you can use just one print just above the room tax line. > elif party <= 150: > > b=150 > print("Room cost $150") > else: > c=250 > print("Room cost $250") > roomtax = party * 0.065 > print("Room tx", roomtax) > print("Beef Meals", beefmeals) > beef = (beefmeals * 15.95) > print("Beef cost", beef) > print("Vegan Meals", shitmeals) > shit = (shitmeals * 10.95) > print("Vegan cost", shit) > cost=(beef + shit) > grat= cost * 0.18 > print("Gratuity", grat) > GT = print("Grand total", grat + beef + shit + a + b + c) The print function always returns None. Therefore the effect of this statement is to assign None to GT. Also note that you don't use GT later on. From adameyring at gmail.com Fri Oct 26 13:20:15 2018 From: adameyring at gmail.com (Adam Eyring) Date: Fri, 26 Oct 2018 13:20:15 -0400 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: Try this cleaned up version with colons in the right places, dollar signs removed, and other corrections: beefmeals=int(input("Enter number of beef meals: ")) shitmeals=int(input("Enter number of vegan meals: ")) party = beefmeals + shitmeals print("Total meals", party) a = 0 b = 0 c = 0 if party <= 50: a=75 print("Room cost $75") elif party <= 150: b=150 print("Room cost $150") else: c=250 print("Room cost $250") roomtax = party * 0.065 print("Room tx", roomtax) print("Beef Meals", beefmeals) beef = (beefmeals * 15.95) print("Beef cost", beef) print("Vegan Meals", shitmeals) shit = (shitmeals * 10.95) print("Vegan cost", shit) cost=(beef + shit) grat= cost * 0.18 print("Gratuity", grat) GT = print("Grand total", grat + beef + shit + a + b + c) On Fri, Oct 26, 2018 at 7:28 AM Bob Gailer wrote: > On Oct 26, 2018 6:11 AM, "Ben Placella" > wrote: > > > > I need to write code that runs a cost calculating program with many > > different variables and I honestly don't understand it > > Could you be more specific? What exactly don't you understand, or even > better what do you understand? > > my code is: > > How could you have written so much code without understanding it? > > > beefmeals=int(input("Enter number of beef meals: ")) > > shitmeals=int(input("Enter number of vegan meals: ")) > > party=beefmeals+shitmeals > > print(party) > > if party<=50 > > Something is missing from that last statement. Can you tell what it is? Do > you know how to find out? Hint use help. > > Hint 2 it is also missing from the elif and else statements. > > > a=75 > > print("Room cost $75") > > elif party <=150 > > b=150 > > print("Room cost $150") > > else > > c=250 > > print("Room cost $250") > > roomtax=party*0.065 > > print(roomtax) > > print("Beef Meals", beefmeals) > > $beef=(beefmeals*15.95) > > print($beef) > > print("Beef cost", $$beef) > > print("Vegan Meals", shitmeals) > > $shit=(shitmeals*10.95) > > print($shit) > > cost=($beef+$shit) > > grat=cost*0.18) > > print(grat) > > GT=(grat+$beef+$shit+(a,b,c)) > > There is a convention in Python that and all uppercase name is a constant. > This is not a requirement. > > > print(GT) > > > > This is what the output is supposed to be: > > I don't see any output here. Alan''s responses may help you figure that > out. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From adameyring at gmail.com Fri Oct 26 16:04:01 2018 From: adameyring at gmail.com (Adam Eyring) Date: Fri, 26 Oct 2018 16:04:01 -0400 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: On Fri, Oct 26, 2018 at 3:03 PM Bob Gailer wrote: > On Oct 26, 2018 1:20 PM, "Adam Eyring" wrote: > > > > Try this cleaned up version with colons in the right places, dollar > signs removed, and other corrections: > > Does it do what you want? > > > beefmeals=int(input("Enter number of beef meals: ")) > > shitmeals=int(input("Enter number of vegan meals: ")) > > party = beefmeals + shitmeals > > print("Total meals", party) > > a = 0 > > b = 0 > > c = 0 > > There is no need for three variables here. You only need one to represent > room cost. If you make that change then you will also not need to > initialize the room cost variable. Makes the code simpler to maintain and > read and understand. > > > if party <= 50: > > > > a=75 > > print("Room cost $75") > > If you use one variable for room cost then you can use just one print just > above the room tax line. > > > elif party <= 150: > > > > b=150 > > print("Room cost $150") > > else: > > c=250 > > print("Room cost $250") > > roomtax = party * 0.065 > > print("Room tx", roomtax) > > print("Beef Meals", beefmeals) > > beef = (beefmeals * 15.95) > > print("Beef cost", beef) > > print("Vegan Meals", shitmeals) > > shit = (shitmeals * 10.95) > > print("Vegan cost", shit) > > cost=(beef + shit) > > grat= cost * 0.18 > > print("Gratuity", grat) > > GT = print("Grand total", grat + beef + shit + a + b + c) > > The print function always returns None. Therefore the effect of this > statement is to assign None to GT. Also note that you don't use GT later on. > You're right - GT is not needed. The print does work with or without "GT =" in Python 3.6.5, though. From alan.gauld at yahoo.co.uk Fri Oct 26 18:30:09 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Oct 2018 23:30:09 +0100 Subject: [Tutor] How to print lines within two timestamp In-Reply-To: References: <80064E7A-7ED8-4898-B978-8BF72D05F643@me.com> <88f88ec5-afaa-8e1f-34c3-0a64edd32ceb@gmail.com> <1B9C3D36-CF02-4EFC-81F6-0BC90C923501@gmail.com> <756EF8A1-D02B-4151-B390-52036C93DC4E@gmail.com> Message-ID: On 26/10/2018 18:45, Alan Gauld via Tutor wrote: > It woiyukld No idea what happened there. Should be "would" of course! -- 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 Fri Oct 26 18:34:17 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Oct 2018 23:34:17 +0100 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: On 26/10/2018 18:20, Adam Eyring wrote: > beef = (beefmeals * 15.95) Note that the parens here are completely redundant. They don't break anything but neither do they contribute anything. WE already have LISP(*) for those who love parens, no need for (so many of) them in Python (*)Lots of Irrelevant Silly Parentheses :)))) -- 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 Sat Oct 27 00:07:43 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 27 Oct 2018 06:07:43 +0200 Subject: [Tutor] How to print lines within two timestamp References: <80064E7A-7ED8-4898-B978-8BF72D05F643@me.com> <88f88ec5-afaa-8e1f-34c3-0a64edd32ceb@gmail.com> <1B9C3D36-CF02-4EFC-81F6-0BC90C923501@gmail.com> <756EF8A1-D02B-4151-B390-52036C93DC4E@gmail.com> Message-ID: Alan Gauld via Tutor wrote: > On 26/10/2018 18:45, Alan Gauld via Tutor wrote: > >> It woiyukld > > No idea what happened there. Should be "would" of course! Of coiyukrse! Nobody thoiyukght otherwiiyske :) From asad.hasan2004 at gmail.com Sat Oct 27 03:02:51 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Sat, 27 Oct 2018 12:32:51 +0530 Subject: [Tutor] How to print lines within two timestamp In-Reply-To: References: Message-ID: On Sat, Oct 27, 2018 at 4:01 AM 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. How to print lines within two timestamp (Asad) > 2. Re: How to print lines within two timestamp (Alan Gauld) > 3. Re: Python Help (Bob Gailer) > 4. Re: Python Help (Adam Eyring) > 5. Re: Python Help (Adam Eyring) > > > > ---------- Forwarded message ---------- > From: Asad > To: tutor at python.org > Cc: > Bcc: > Date: Fri, 26 Oct 2018 17:03:01 +0530 > Subject: [Tutor] How to print lines within two timestamp > Hi , > > Yes i have the code : > > import re > import datetime > from datetime import timedelta > > f3 = open ( r"D:\QI\logA.txt", 'r' ) > string = f3.read () > regex = re.compile ( "\n" ) > st = regex.sub ( " ", string ) > st1 = st.split ( " " ) > > if re.search ('ERR-1:', st ): > x = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", > st ) > j = x[0][0] + " "+ x[0][1]+" " + x[0][2] +":"+ x[0][3]+":" + > x[0][4]+" " + x[0][5] > h = x[1][0] + " "+ x[1][1]+" "+ x[1][2] +":" + x[1][3] +":"+ > x[1][4] +" "+ x[1][5] > y = datetime.datetime.strptime ( j, '%b %d %H:%M:%S %Y' ) > print y > k = datetime.datetime.strptime ( h, '%b %d %H:%M:%S %Y' ) > print k > > f4 = open ( r"D:\QI\logC11.txt", 'r' ) > > string1 = f4.read () > reg = re.compile ( "\n" ) > newst = reg.sub ( " ", string1 ) > newst1 = newst.split ( " " ) > > if re.search ( "ERR-2", newst ): > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > newst ) > for i in range ( len ( a ) ): > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' ) > if newtime > y and newtime < k: > print "Install patch1" > > if re.search ( "ERR-3", newst ): > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > newst ) > for i in range ( len ( a ) ): > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' ) > if newtime > y and newtime < k: > print newtime, y, k > print "Install patch2" > > > ============================================================================================== > > output i get : > > *Install patch1 - wrong solution > 2018-10-22 10:21:23 2018-10-22 10:21:15 2018-10-22 10:21:25 > Install patch2 - correct solution * > > > *It should have only searched between timestamps **2018-10-22 10:21:15 > 2018-10-22 10:21:25* > > *in logC11.txt what I am doing wrong please adice stuck on this for long.* > > > > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Fri, 26 Oct 2018 18:45:17 +0100 > Subject: Re: [Tutor] How to print lines within two timestamp > On 26/10/2018 12:33, Asad wrote: > > Hi , > > > > Yes i have the code : > > It woiyukld help us to help you if you provided some clues as to what it > was doing. > A good start would be some comments - especially around the regexes. > Don't make us parse them without some idea of what you are expecting. > Also moremeaningful variable names than h,j,k etc > > > import re > > import datetime > > from datetime import timedelta > > You don't appear to use timedelta? > Answer: Yes I remove from datetime import timedelta > > > Header = "*****************************************************" > > > > f3 = open ( r"D:\QI\logA.txt", 'r' ) > > string = f3.read () > > regex = re.compile ( "\n" ) > > st = regex.sub ( " ", string ) > > I suspect regular string methods would be simpler here. > answer : can you please provide the code to replace above > > > st1 = st.split ( " " ) > > > > if re.search ('ERR-1:', st ): > > x = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", > st ) > > j = x[0][0] + " "+ x[0][1]+" " + x[0][2] +":"+ x[0][3]+":" + > > x[0][4]+" " + x[0][5] > > h = x[1][0] + " "+ x[1][1]+" "+ x[1][2] +":" + x[1][3] +":"+ > > x[1][4] +" "+ x[1][5] > > I'm not sure what exactly this is doing, but I suspect > datetime.strftime might do it better. > Answer: Its extracting all the dates in the format :Oct 22 10:21:15 2018 from logA.txt and storing it because this logA.txt is primary file which has the start and end date of the incident . > > > y = datetime.datetime.strptime ( j, '%b %d %H:%M:%S %Y' ) > > print y > > k = datetime.datetime.strptime ( h, '%b %d %H:%M:%S %Y' ) > > print k > > > > f4 = open ( r"D:\QI\logC11.txt", 'r' ) > > > > string1 = f4.read () > > reg = re.compile ( "\n" ) > > newst = reg.sub ( " ", string1 ) > > newst1 = newst.split ( " " ) > > > > if re.search ( "ERR-2", newst ): > > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > newst ) > > for i in range ( len ( a ) ): > > Would this not be simpler as > > for result in a: > > Answer : Correct suggestion will try > and use result instead of a[i] > > > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' > ) > > if newtime > y and newtime < k: > > You should be able to write this as > > if y < newtime < k: > Answer : Correct > > > print "Install patch1" > > > > if re.search ( "ERR-3", newst ): > > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > newst ) > > for i in range ( len ( a ) ): > > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' > ) > > if newtime > y and newtime < k: > > print newtime, y, k > > print "Install patch2" > > > > > ============================================================================================== > > > > output i get : > > > Can you show us the full output? It should start with your header line? > This is the complete output > > > *Install patch1 - wrong solution > > 2018-10-22 10:21:23 2018-10-22 10:21:15 2018-10-22 10:21:25 > > Install patch2 - correct solution * > > > > > > *It should have only searched between timestamps **2018-10-22 10:21:15 > > 2018-10-22 10:21:25* > > Going by the above output that's exactly what it did? > And it found 2018-10-22 10:21:23 > I'm not clear what you expected. > Code did as it should have done . I need to change the code according to my requirement . My requirement is to start(y) and end timestamp (k) from logA.txt which the code is getting now correctly then open logC11.txt see the timestamp nearest to start(y) which its doing now 2018-10-22 10:21:23 and then look for errors(ERR-2 and ERR-3) in the lines in logC11.txt starting from 2018-10-22 10:21:23 and before the timestamp (k) ==> here i need help my code is searching for ERR-2 and ERR-3 occurance in logC11.txt anywhere in the file I want it to search in a specific window of 2018-10-22 10:21:23 and less than timestamp (k) so the my present coded has printed the output : *Install patch1 - wrong solution This it will print if it executes the following block : if re.search ( "ERR-2", newst ): a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", newst ) for i in range ( len ( a ) ): newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' ) if newtime > y and newtime < k: print "Install patch1 Now if you see ERR -2 in logC11.txt it occured at 26th april instead it should have ignored it because the time windows received from logA.txt y (2018-10-22 10:21:15) and k (2018-10-22 10:21:25) : LOG file opened at 04/26/18 06:11:52 ERR-2: OS message: No child processes operation "wait", location "skudmi:prp:6" In short I would like to only search in lines : LOG file opened at 10/22/18 10:21:23 ERR-3: patchObjectPossible causes are:" Trim whitespace same as SQL Loader How do I acheive this ? Please advice , Thanks in advance, > > -- > 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 > > > > > > > ---------- Forwarded message ---------- > From: Bob Gailer > To: Adam Eyring > Cc: benjamin.placella at maine.edu, tutor at python.org > Bcc: > Date: Fri, 26 Oct 2018 15:03:36 -0400 > Subject: Re: [Tutor] Python Help > On Oct 26, 2018 1:20 PM, "Adam Eyring" wrote: > > > > Try this cleaned up version with colons in the right places, dollar signs > removed, and other corrections: > > Does it do what you want? > > > beefmeals=int(input("Enter number of beef meals: ")) > > shitmeals=int(input("Enter number of vegan meals: ")) > > party = beefmeals + shitmeals > > print("Total meals", party) > > a = 0 > > b = 0 > > c = 0 > > There is no need for three variables here. You only need one to represent > room cost. If you make that change then you will also not need to > initialize the room cost variable. Makes the code simpler to maintain and > read and understand. > > > if party <= 50: > > > > a=75 > > print("Room cost $75") > > If you use one variable for room cost then you can use just one print just > above the room tax line. > > > elif party <= 150: > > > > b=150 > > print("Room cost $150") > > else: > > c=250 > > print("Room cost $250") > > roomtax = party * 0.065 > > print("Room tx", roomtax) > > print("Beef Meals", beefmeals) > > beef = (beefmeals * 15.95) > > print("Beef cost", beef) > > print("Vegan Meals", shitmeals) > > shit = (shitmeals * 10.95) > > print("Vegan cost", shit) > > cost=(beef + shit) > > grat= cost * 0.18 > > print("Gratuity", grat) > > GT = print("Grand total", grat + beef + shit + a + b + c) > > The print function always returns None. Therefore the effect of this > statement is to assign None to GT. Also note that you don't use GT later > on. > > > > > ---------- Forwarded message ---------- > From: Adam Eyring > To: bgailer at gmail.com > Cc: benjamin.placella at maine.edu, tutor at python.org > Bcc: > Date: Fri, 26 Oct 2018 13:20:15 -0400 > Subject: Re: [Tutor] Python Help > Try this cleaned up version with colons in the right places, dollar signs > removed, and other corrections: > > beefmeals=int(input("Enter number of beef meals: ")) > shitmeals=int(input("Enter number of vegan meals: ")) > party = beefmeals + shitmeals > print("Total meals", party) > a = 0 > b = 0 > c = 0 > if party <= 50: > a=75 > print("Room cost $75") > elif party <= 150: > b=150 > print("Room cost $150") > else: > c=250 > print("Room cost $250") > roomtax = party * 0.065 > print("Room tx", roomtax) > print("Beef Meals", beefmeals) > beef = (beefmeals * 15.95) > print("Beef cost", beef) > print("Vegan Meals", shitmeals) > shit = (shitmeals * 10.95) > print("Vegan cost", shit) > cost=(beef + shit) > grat= cost * 0.18 > print("Gratuity", grat) > GT = print("Grand total", grat + beef + shit + a + b + c) > > On Fri, Oct 26, 2018 at 7:28 AM Bob Gailer wrote: > > > On Oct 26, 2018 6:11 AM, "Ben Placella" > > wrote: > > > > > > I need to write code that runs a cost calculating program with many > > > different variables and I honestly don't understand it > > > > Could you be more specific? What exactly don't you understand, or even > > better what do you understand? > > > > my code is: > > > > How could you have written so much code without understanding it? > > > > > beefmeals=int(input("Enter number of beef meals: ")) > > > shitmeals=int(input("Enter number of vegan meals: ")) > > > party=beefmeals+shitmeals > > > print(party) > > > if party<=50 > > > > Something is missing from that last statement. Can you tell what it is? > Do > > you know how to find out? Hint use help. > > > > Hint 2 it is also missing from the elif and else statements. > > > > > a=75 > > > print("Room cost $75") > > > elif party <=150 > > > b=150 > > > print("Room cost $150") > > > else > > > c=250 > > > print("Room cost $250") > > > roomtax=party*0.065 > > > print(roomtax) > > > print("Beef Meals", beefmeals) > > > $beef=(beefmeals*15.95) > > > print($beef) > > > print("Beef cost", $$beef) > > > print("Vegan Meals", shitmeals) > > > $shit=(shitmeals*10.95) > > > print($shit) > > > cost=($beef+$shit) > > > grat=cost*0.18) > > > print(grat) > > > GT=(grat+$beef+$shit+(a,b,c)) > > > > There is a convention in Python that and all uppercase name is a > constant. > > This is not a requirement. > > > > > print(GT) > > > > > > This is what the output is supposed to be: > > > > I don't see any output here. Alan''s responses may help you figure that > > out. > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > > > ---------- Forwarded message ---------- > From: Adam Eyring > To: bgailer at gmail.com > Cc: benjamin.placella at maine.edu, tutor at python.org > Bcc: > Date: Fri, 26 Oct 2018 16:04:01 -0400 > Subject: Re: [Tutor] Python Help > On Fri, Oct 26, 2018 at 3:03 PM Bob Gailer wrote: > > > On Oct 26, 2018 1:20 PM, "Adam Eyring" wrote: > > > > > > Try this cleaned up version with colons in the right places, dollar > > signs removed, and other corrections: > > > > Does it do what you want? > > > > > beefmeals=int(input("Enter number of beef meals: ")) > > > shitmeals=int(input("Enter number of vegan meals: ")) > > > party = beefmeals + shitmeals > > > print("Total meals", party) > > > a = 0 > > > b = 0 > > > c = 0 > > > > There is no need for three variables here. You only need one to represent > > room cost. If you make that change then you will also not need to > > initialize the room cost variable. Makes the code simpler to maintain and > > read and understand. > > > > > if party <= 50: > > > > > > a=75 > > > print("Room cost $75") > > > > If you use one variable for room cost then you can use just one print > just > > above the room tax line. > > > > > elif party <= 150: > > > > > > b=150 > > > print("Room cost $150") > > > else: > > > c=250 > > > print("Room cost $250") > > > roomtax = party * 0.065 > > > print("Room tx", roomtax) > > > print("Beef Meals", beefmeals) > > > beef = (beefmeals * 15.95) > > > print("Beef cost", beef) > > > print("Vegan Meals", shitmeals) > > > shit = (shitmeals * 10.95) > > > print("Vegan cost", shit) > > > cost=(beef + shit) > > > grat= cost * 0.18 > > > print("Gratuity", grat) > > > GT = print("Grand total", grat + beef + shit + a + b + c) > > > > The print function always returns None. Therefore the effect of this > > statement is to assign None to GT. Also note that you don't use GT later > on. > > > > You're right - GT is not needed. The print does work with or without "GT > =" in Python 3.6.5, though. > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > -- Asad Hasan +91 9582111698 From alan.gauld at yahoo.co.uk Sat Oct 27 04:33:10 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Oct 2018 09:33:10 +0100 Subject: [Tutor] How to print lines within two timestamp In-Reply-To: References: Message-ID: On 27/10/2018 08:02, Asad wrote: >>> string = f3.read () >>> regex = re.compile ( "\n" ) >>> st = regex.sub ( " ", string ) >> >> I suspect regular string methods would be simpler here. >> answer : can you please provide the code to replace above st = string.replace("\n"," ") >>> if re.search ('ERR-1:', st ): >>> x = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", >> st ) >>> j = x[0][0] + " "+ x[0][1]+" " + x[0][2] +":"+ x[0][3]+":" + >>> x[0][4]+" " + x[0][5] >>> h = x[1][0] + " "+ x[1][1]+" "+ x[1][2] +":" + x[1][3] +":"+ >>> x[1][4] +" "+ x[1][5] >> >> I'm not sure what exactly this is doing, but I suspect >> datetime.strftime might do it better. >> Answer: Its extracting all the dates in the format :Oct 22 10:21:15 2018 Sorry, I saw the re.search but missed the findall(). My bad. >> Can you show us the full output? It should start with your header line? >> > This is the complete output Again my bad. I thought you had a single line header, but that must have been another post, oops! > My requirement is to start(y) and end timestamp (k) from logA.txt which > the code is getting now correctly then open logC11.txt see the timestamp > nearest to start(y) which its doing now > 2018-10-22 10:21:23 and then look for errors(ERR-2 and ERR-3) in the lines > in logC11.txt starting from 2018-10-22 10:21:23 and before the timestamp > (k) ==> here i need help my code is searching for ERR-2 and ERR-3 occurance > in logC11.txt anywhere in the file I want it to search in a specific window > of 2018-10-22 10:21:23 and less than timestamp (k) The traditional way to do this is to use a sentinel variable. That is you scan the lines until you reach your start condition and set the variable to True. You then process all the lines until your end condition where you set the sentinel to false (or exit the loop). During your processing you only proceed to analyses the lines if the sentinel is True. In pseudo code: start,end = find_times("logA.txt") for line in open("logC11.txt"): if start in line: found = True if end in line: break if found: process(line) Obviously with time values its a little bit more complex than a simple 'in' test but you get the idea I hope. HTH -- 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 angel.purr.bott at gmail.com Sat Oct 27 07:09:17 2018 From: angel.purr.bott at gmail.com (Jesse Stockman) Date: Sun, 28 Oct 2018 00:09:17 +1300 Subject: [Tutor] (no subject) Message-ID: <5bd4475e.1c69fb81.41858.27cd@mx.google.com> Hi there I need to draw a patten with turtle in python 3.7 but I cant get it to work here are the specs of the pattern and my code so far can you please help ? Specifications of the pattern o The radius of the three heads is 10. o The length of legs is 30. o The length of the sizes of the two triangles (the body of runner-up and third-place) is 40. They are all equal-size triangles. The winner?s body is a 40x40 square. o The width of the three blocks of the podium is 60. The heights are 90, 60, and 40 respectively. And my code so far from turtle import * x = 0 y = 0 radius = 0 x1 = 0 x2 = 0 y1 = 0 y2 = 0 color = 0 width = 0 hight =0 def main(): speed(0) pensize(3) pencolor("black") winner_color = "red" second_color = "orange" third_color = "purple" draw_podium(winner_color, second_color, third_color) darw_third(third_color) draw_winner(winner_color) draw_second(second_color) def move_to(x, y): x = int(input("input X coordinate: ")) y = int(input("input y coordinate: ")) penup() goto(x, y) pendown() def draw_head(x, y, radius): radius = int(input("input radius: ")) move_to(x, y) circle(radius) def draw_line(x1, y1, x2, y2): x1 = int(input("line start X: ")) y1 = int(input("line start Y: ")) x2 = int(input("line end X: ")) y2 = int(input("line end Y: ")) penup() goto(x1,y1) pendown() goto(x2,y2) def draw_block(x, y, hight, width, color): move_to(x, y) hight = int(input("input hight: ")) width = int(input("input width: ")) color(winner_color) begin_fill() forward(width) left(90) forward(hight) left(90) forward(width) left(90) forward(hight) end_fill() main() draw_block(x, y, hight, width, color) exitonclick() please help me thank you kind regards Tarantulam Sent from Mail for Windows 10 From asad.hasan2004 at gmail.com Sat Oct 27 05:28:34 2018 From: asad.hasan2004 at gmail.com (Asad) Date: Sat, 27 Oct 2018 14:58:34 +0530 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: Hi All , I found a logic : 1.initialize print flag to false 2. Save starting and ending times in variables 3. loop to read or process sequential lines in the file a. See if line contains time b. If it does, extract time c. Compare time in line to starting time d. If line time is greater than or equal to start time, set print flag to true e. If line time is greater than ending time, set print flag to false f. If print flag is true print line g. End of loop ? process next line start_time = 2018-10-22 10:21:15 end_time = 2018-10-22 10:21:25 f4 = open ( r"logC11.txt", 'r' ) for line in f4.readlines(): a = re.findall( r'\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]', line ) print a #b = a.group(0) newtime = datetime.datetime.strptime ( a[0], '%m/%d/%y %H:%M:%S' ) print newtime if newtime > y and newtime < k: flag = True else: flag = False if flag : print line else : continue But it errors out : C:\Python27\python.exe D:/QI/test_qopatch.py Traceback (most recent call last): 2018-10-22 10:21:15 File "D:/QI/test_qopatch.py", line 32, in 2018-10-22 10:21:25 newtime = datetime.datetime.strptime ( a[0], '%m/%d/%y %H:%M:%S' ) ['04/26/18 06:11:52'] 2018-04-26 06:11:52 [] IndexError: list index out of range Process finished with exit code 1 Please provide the code Thanks, On Sat, Oct 27, 2018 at 1:35 PM 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: How to print lines within two timestamp (Alan Gauld) > 2. Re: Python Help (Alan Gauld) > 3. Re: How to print lines within two timestamp (Peter Otten) > 4. How to print lines within two timestamp (Asad) > > > > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Fri, 26 Oct 2018 23:30:09 +0100 > Subject: Re: [Tutor] How to print lines within two timestamp > On 26/10/2018 18:45, Alan Gauld via Tutor wrote: > > > It woiyukld > > No idea what happened there. Should be "would" of course! > > -- > 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 > > > > > > > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Bcc: > Date: Fri, 26 Oct 2018 23:34:17 +0100 > Subject: Re: [Tutor] Python Help > On 26/10/2018 18:20, Adam Eyring wrote: > > > beef = (beefmeals * 15.95) > > Note that the parens here are completely redundant. > They don't break anything but neither do they > contribute anything. > > WE already have LISP(*) for those who love parens, > no need for (so many of) them in Python > > (*)Lots of Irrelevant Silly Parentheses :)))) > > -- > 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 > > > > > > > ---------- Forwarded message ---------- > From: Peter Otten <__peter__ at web.de> > To: tutor at python.org > Cc: > Bcc: > Date: Sat, 27 Oct 2018 06:07:43 +0200 > Subject: Re: [Tutor] How to print lines within two timestamp > Alan Gauld via Tutor wrote: > > > On 26/10/2018 18:45, Alan Gauld via Tutor wrote: > > > >> It woiyukld > > > > No idea what happened there. Should be "would" of course! > > Of coiyukrse! Nobody thoiyukght otherwiiyske :) > > > > > > ---------- Forwarded message ---------- > From: Asad > To: tutor at python.org > Cc: > Bcc: > Date: Sat, 27 Oct 2018 12:32:51 +0530 > Subject: [Tutor] How to print lines within two timestamp > On Sat, Oct 27, 2018 at 4:01 AM 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. How to print lines within two timestamp (Asad) > > 2. Re: How to print lines within two timestamp (Alan Gauld) > > 3. Re: Python Help (Bob Gailer) > > 4. Re: Python Help (Adam Eyring) > > 5. Re: Python Help (Adam Eyring) > > > > > > > > ---------- Forwarded message ---------- > > From: Asad > > To: tutor at python.org > > Cc: > > Bcc: > > Date: Fri, 26 Oct 2018 17:03:01 +0530 > > Subject: [Tutor] How to print lines within two timestamp > > Hi , > > > > Yes i have the code : > > > > import re > > import datetime > > from datetime import timedelta > > > > f3 = open ( r"D:\QI\logA.txt", 'r' ) > > string = f3.read () > > regex = re.compile ( "\n" ) > > st = regex.sub ( " ", string ) > > st1 = st.split ( " " ) > > > > if re.search ('ERR-1:', st ): > > x = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", > > st ) > > j = x[0][0] + " "+ x[0][1]+" " + x[0][2] +":"+ x[0][3]+":" + > > x[0][4]+" " + x[0][5] > > h = x[1][0] + " "+ x[1][1]+" "+ x[1][2] +":" + x[1][3] +":"+ > > x[1][4] +" "+ x[1][5] > > y = datetime.datetime.strptime ( j, '%b %d %H:%M:%S %Y' ) > > print y > > k = datetime.datetime.strptime ( h, '%b %d %H:%M:%S %Y' ) > > print k > > > > f4 = open ( r"D:\QI\logC11.txt", 'r' ) > > > > string1 = f4.read () > > reg = re.compile ( "\n" ) > > newst = reg.sub ( " ", string1 ) > > newst1 = newst.split ( " " ) > > > > if re.search ( "ERR-2", newst ): > > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > > newst ) > > for i in range ( len ( a ) ): > > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' > ) > > if newtime > y and newtime < k: > > print "Install patch1" > > > > if re.search ( "ERR-3", newst ): > > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > > newst ) > > for i in range ( len ( a ) ): > > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' > ) > > if newtime > y and newtime < k: > > print newtime, y, k > > print "Install patch2" > > > > > > > ============================================================================================== > > > > output i get : > > > > *Install patch1 - wrong solution > > 2018-10-22 10:21:23 2018-10-22 10:21:15 2018-10-22 10:21:25 > > Install patch2 - correct solution * > > > > > > *It should have only searched between timestamps **2018-10-22 10:21:15 > > 2018-10-22 10:21:25* > > > > *in logC11.txt what I am doing wrong please adice stuck on this for > long.* > > > > > > > > ---------- Forwarded message ---------- > > From: Alan Gauld > > To: tutor at python.org > > Cc: > > Bcc: > > Date: Fri, 26 Oct 2018 18:45:17 +0100 > > Subject: Re: [Tutor] How to print lines within two timestamp > > On 26/10/2018 12:33, Asad wrote: > > > Hi , > > > > > > Yes i have the code : > > > > It woiyukld help us to help you if you provided some clues as to what it > > was doing. > > A good start would be some comments - especially around the regexes. > > Don't make us parse them without some idea of what you are expecting. > > Also moremeaningful variable names than h,j,k etc > > > > > import re > > > import datetime > > > from datetime import timedelta > > > > You don't appear to use timedelta? > > > Answer: Yes I remove from datetime import timedelta > > > > > > > Header = "*****************************************************" > > > > > > f3 = open ( r"D:\QI\logA.txt", 'r' ) > > > string = f3.read () > > > regex = re.compile ( "\n" ) > > > st = regex.sub ( " ", string ) > > > > I suspect regular string methods would be simpler here. > > answer : can you please provide the code to replace above > > > > > > > st1 = st.split ( " " ) > > > > > > if re.search ('ERR-1:', st ): > > > x = re.findall ( > "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", > > st ) > > > j = x[0][0] + " "+ x[0][1]+" " + x[0][2] +":"+ x[0][3]+":" + > > > x[0][4]+" " + x[0][5] > > > h = x[1][0] + " "+ x[1][1]+" "+ x[1][2] +":" + x[1][3] +":"+ > > > x[1][4] +" "+ x[1][5] > > > > I'm not sure what exactly this is doing, but I suspect > > datetime.strftime might do it better. > > Answer: Its extracting all the dates in the format :Oct 22 10:21:15 2018 > > from logA.txt and storing it because this logA.txt is primary file > which has the start and end date of the incident . > > > > > > > y = datetime.datetime.strptime ( j, '%b %d %H:%M:%S %Y' ) > > > print y > > > k = datetime.datetime.strptime ( h, '%b %d %H:%M:%S %Y' ) > > > print k > > > > > > f4 = open ( r"D:\QI\logC11.txt", 'r' ) > > > > > > string1 = f4.read () > > > reg = re.compile ( "\n" ) > > > newst = reg.sub ( " ", string1 ) > > > newst1 = newst.split ( " " ) > > > > > > if re.search ( "ERR-2", newst ): > > > a = re.findall ( > "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > > newst ) > > > for i in range ( len ( a ) ): > > > > Would this not be simpler as > > > > for result in a: > > > > Answer : Correct suggestion will try > > > > and use result instead of a[i] > > > > > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y > %H:%M:%S' > > ) > > > if newtime > y and newtime < k: > > > > You should be able to write this as > > > > if y < newtime < k: > > Answer : Correct > > > > > print "Install patch1" > > > > > > if re.search ( "ERR-3", newst ): > > > a = re.findall ( > "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > > newst ) > > > for i in range ( len ( a ) ): > > > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y > %H:%M:%S' > > ) > > > if newtime > y and newtime < k: > > > print newtime, y, k > > > print "Install patch2" > > > > > > > > > ============================================================================================== > > > > > > output i get : > > > > > > Can you show us the full output? It should start with your header line? > > > This is the complete output > > > > > > > *Install patch1 - wrong solution > > > 2018-10-22 10:21:23 2018-10-22 10:21:15 2018-10-22 10:21:25 > > > Install patch2 - correct solution * > > > > > > > > > *It should have only searched between timestamps **2018-10-22 10:21:15 > > > 2018-10-22 10:21:25* > > > > Going by the above output that's exactly what it did? > > And it found 2018-10-22 10:21:23 > > > I'm not clear what you expected. > > > > Code did as it should have done . I need to change the code according to > my requirement . > My requirement is to start(y) and end timestamp (k) from logA.txt which > the code is getting now correctly then open logC11.txt see the timestamp > nearest to start(y) which its doing now > 2018-10-22 10:21:23 and then look for errors(ERR-2 and ERR-3) in the lines > in logC11.txt starting from 2018-10-22 10:21:23 and before the timestamp > (k) ==> here i need help my code is searching for ERR-2 and ERR-3 occurance > in logC11.txt anywhere in the file I want it to search in a specific window > of 2018-10-22 10:21:23 and less than timestamp (k) > > so the my present coded has printed the output : > *Install patch1 - wrong solution > This it will print if it executes the following block : > > if re.search ( "ERR-2", newst ): > a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", > newst ) > for i in range ( len ( a ) ): > newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' ) > if newtime > y and newtime < k: > print "Install patch1 > > Now if you see ERR -2 in logC11.txt it occured at 26th april instead it > should have ignored it because the time windows received from logA.txt y > (2018-10-22 10:21:15) and k (2018-10-22 10:21:25) : > > LOG file opened at 04/26/18 06:11:52 > > ERR-2: OS message: No child processes > operation "wait", location "skudmi:prp:6" > > > In short I would like to only search in lines : > > LOG file opened at 10/22/18 10:21:23 > > > ERR-3: patchObjectPossible causes are:" > > Trim whitespace same as SQL Loader > > > How do I acheive this ? > > Please advice , > > Thanks in advance, > > > > > > > -- > > 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 > > > > > > > > > > > > > > ---------- Forwarded message ---------- > > From: Bob Gailer > > To: Adam Eyring > > Cc: benjamin.placella at maine.edu, tutor at python.org > > Bcc: > > Date: Fri, 26 Oct 2018 15:03:36 -0400 > > Subject: Re: [Tutor] Python Help > > On Oct 26, 2018 1:20 PM, "Adam Eyring" wrote: > > > > > > Try this cleaned up version with colons in the right places, dollar > signs > > removed, and other corrections: > > > > Does it do what you want? > > > > > beefmeals=int(input("Enter number of beef meals: ")) > > > shitmeals=int(input("Enter number of vegan meals: ")) > > > party = beefmeals + shitmeals > > > print("Total meals", party) > > > a = 0 > > > b = 0 > > > c = 0 > > > > There is no need for three variables here. You only need one to represent > > room cost. If you make that change then you will also not need to > > initialize the room cost variable. Makes the code simpler to maintain and > > read and understand. > > > > > if party <= 50: > > > > > > a=75 > > > print("Room cost $75") > > > > If you use one variable for room cost then you can use just one print > just > > above the room tax line. > > > > > elif party <= 150: > > > > > > b=150 > > > print("Room cost $150") > > > else: > > > c=250 > > > print("Room cost $250") > > > roomtax = party * 0.065 > > > print("Room tx", roomtax) > > > print("Beef Meals", beefmeals) > > > beef = (beefmeals * 15.95) > > > print("Beef cost", beef) > > > print("Vegan Meals", shitmeals) > > > shit = (shitmeals * 10.95) > > > print("Vegan cost", shit) > > > cost=(beef + shit) > > > grat= cost * 0.18 > > > print("Gratuity", grat) > > > GT = print("Grand total", grat + beef + shit + a + b + c) > > > > The print function always returns None. Therefore the effect of this > > statement is to assign None to GT. Also note that you don't use GT later > > on. > > > > > > > > > > ---------- Forwarded message ---------- > > From: Adam Eyring > > To: bgailer at gmail.com > > Cc: benjamin.placella at maine.edu, tutor at python.org > > Bcc: > > Date: Fri, 26 Oct 2018 13:20:15 -0400 > > Subject: Re: [Tutor] Python Help > > Try this cleaned up version with colons in the right places, dollar signs > > removed, and other corrections: > > > > beefmeals=int(input("Enter number of beef meals: ")) > > shitmeals=int(input("Enter number of vegan meals: ")) > > party = beefmeals + shitmeals > > print("Total meals", party) > > a = 0 > > b = 0 > > c = 0 > > if party <= 50: > > a=75 > > print("Room cost $75") > > elif party <= 150: > > b=150 > > print("Room cost $150") > > else: > > c=250 > > print("Room cost $250") > > roomtax = party * 0.065 > > print("Room tx", roomtax) > > print("Beef Meals", beefmeals) > > beef = (beefmeals * 15.95) > > print("Beef cost", beef) > > print("Vegan Meals", shitmeals) > > shit = (shitmeals * 10.95) > > print("Vegan cost", shit) > > cost=(beef + shit) > > grat= cost * 0.18 > > print("Gratuity", grat) > > GT = print("Grand total", grat + beef + shit + a + b + c) > > > > On Fri, Oct 26, 2018 at 7:28 AM Bob Gailer wrote: > > > > > On Oct 26, 2018 6:11 AM, "Ben Placella" > > > wrote: > > > > > > > > I need to write code that runs a cost calculating program with many > > > > different variables and I honestly don't understand it > > > > > > Could you be more specific? What exactly don't you understand, or even > > > better what do you understand? > > > > > > my code is: > > > > > > How could you have written so much code without understanding it? > > > > > > > beefmeals=int(input("Enter number of beef meals: ")) > > > > shitmeals=int(input("Enter number of vegan meals: ")) > > > > party=beefmeals+shitmeals > > > > print(party) > > > > if party<=50 > > > > > > Something is missing from that last statement. Can you tell what it is? > > Do > > > you know how to find out? Hint use help. > > > > > > Hint 2 it is also missing from the elif and else statements. > > > > > > > a=75 > > > > print("Room cost $75") > > > > elif party <=150 > > > > b=150 > > > > print("Room cost $150") > > > > else > > > > c=250 > > > > print("Room cost $250") > > > > roomtax=party*0.065 > > > > print(roomtax) > > > > print("Beef Meals", beefmeals) > > > > $beef=(beefmeals*15.95) > > > > print($beef) > > > > print("Beef cost", $$beef) > > > > print("Vegan Meals", shitmeals) > > > > $shit=(shitmeals*10.95) > > > > print($shit) > > > > cost=($beef+$shit) > > > > grat=cost*0.18) > > > > print(grat) > > > > GT=(grat+$beef+$shit+(a,b,c)) > > > > > > There is a convention in Python that and all uppercase name is a > > constant. > > > This is not a requirement. > > > > > > > print(GT) > > > > > > > > This is what the output is supposed to be: > > > > > > I don't see any output here. Alan''s responses may help you figure that > > > out. > > > _______________________________________________ > > > Tutor maillist - Tutor at python.org > > > To unsubscribe or change subscription options: > > > https://mail.python.org/mailman/listinfo/tutor > > > > > > > > > > > > > ---------- Forwarded message ---------- > > From: Adam Eyring > > To: bgailer at gmail.com > > Cc: benjamin.placella at maine.edu, tutor at python.org > > Bcc: > > Date: Fri, 26 Oct 2018 16:04:01 -0400 > > Subject: Re: [Tutor] Python Help > > On Fri, Oct 26, 2018 at 3:03 PM Bob Gailer wrote: > > > > > On Oct 26, 2018 1:20 PM, "Adam Eyring" wrote: > > > > > > > > Try this cleaned up version with colons in the right places, dollar > > > signs removed, and other corrections: > > > > > > Does it do what you want? > > > > > > > beefmeals=int(input("Enter number of beef meals: ")) > > > > shitmeals=int(input("Enter number of vegan meals: ")) > > > > party = beefmeals + shitmeals > > > > print("Total meals", party) > > > > a = 0 > > > > b = 0 > > > > c = 0 > > > > > > There is no need for three variables here. You only need one to > represent > > > room cost. If you make that change then you will also not need to > > > initialize the room cost variable. Makes the code simpler to maintain > and > > > read and understand. > > > > > > > if party <= 50: > > > > > > > > a=75 > > > > print("Room cost $75") > > > > > > If you use one variable for room cost then you can use just one print > > just > > > above the room tax line. > > > > > > > elif party <= 150: > > > > > > > > b=150 > > > > print("Room cost $150") > > > > else: > > > > c=250 > > > > print("Room cost $250") > > > > roomtax = party * 0.065 > > > > print("Room tx", roomtax) > > > > print("Beef Meals", beefmeals) > > > > beef = (beefmeals * 15.95) > > > > print("Beef cost", beef) > > > > print("Vegan Meals", shitmeals) > > > > shit = (shitmeals * 10.95) > > > > print("Vegan cost", shit) > > > > cost=(beef + shit) > > > > grat= cost * 0.18 > > > > print("Gratuity", grat) > > > > GT = print("Grand total", grat + beef + shit + a + b + c) > > > > > > The print function always returns None. Therefore the effect of this > > > statement is to assign None to GT. Also note that you don't use GT > later > > on. > > > > > > > You're right - GT is not needed. The print does work with or without "GT > > =" in Python 3.6.5, though. > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > https://mail.python.org/mailman/listinfo/tutor > > > > > -- > Asad Hasan > +91 9582111698 > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > -- Asad Hasan +91 9582111698 From alan.gauld at yahoo.co.uk Sat Oct 27 08:21:49 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Oct 2018 13:21:49 +0100 Subject: [Tutor] How to print lines within two timestamps In-Reply-To: References: Message-ID: On 27/10/2018 10:28, Asad wrote: > C:\Python27\python.exe D:/QI/test_qopatch.py > Traceback (most recent call last): > 2018-10-22 10:21:15 > File "D:/QI/test_qopatch.py", line 32, in > 2018-10-22 10:21:25 > newtime = datetime.datetime.strptime ( a[0], '%m/%d/%y %H:%M:%S' ) > ['04/26/18 06:11:52'] > 2018-04-26 06:11:52 > [] > IndexError: list index out of range Notice that the 2nd last line says you have an empty list. So a[0] will produce an index error since there is no element 0. You need to check for an empty list before the assignment. (Or catch the index error in a try/except) Notice the help for re.findall() says ############################ findall(pattern, string, flags=0) Return a list of all non-overlapping matches in the string. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result. ########################### See the last line? BTW Please delete the digest items that are not relevant. These messages go to the full 400+ members of the list and many pay by the byte. They don't want to see stuff they have already received unless it's relevant. -- 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 jf_byrnes at comcast.net Sat Oct 27 12:10:40 2018 From: jf_byrnes at comcast.net (Jim) Date: Sat, 27 Oct 2018 11:10:40 -0500 Subject: [Tutor] Python 3.6 update? In-Reply-To: <98594068-fbb5-f301-d1fb-a91843d31b90@wichmann.us> References: <98594068-fbb5-f301-d1fb-a91843d31b90@wichmann.us> Message-ID: On 10/25/18 5:30 PM, Mats Wichmann wrote: > On 10/25/2018 03:07 PM, Jim wrote: >> Mint 18.1 >> Default python3 = 3.5 >> Python 3.6 installed for use by a virtual environment. >> >> >> >> Update manager just informed me of an update for python 3.6 to 3.6.7-1. >> When I started to install it, I got a dialog saying: >> >> this upgrade will trigger additional changes. The following 2 packages >> will be removed? python3-dev & python3-venv. >> >> I am wondering what will happen to the virtual environment I have >> installed that uses python 3.6. Will the 2 packages be replaced and the >> virtual environment continue to work or is there some other steps I will >> need to take. >> >> Regards,? Jim > > we can't tell that from here. > > Not sure whose bright idea it was to package venv separately, since it's > supposed to be a "standard" part of Python 3 since 3.3 (I guess it's > "batteries included, but you still have to install the batteries" :). > But removing the package doesn't affect a created virtual environment. > > What can affect a created virtual environment is how it was created - > there are a few options, some combinations of which could leave things > going missing from underneath if the Python it's created from is > upgraded. venv has an upgrade option that might help with that. > > Mint forums (and probably Ubuntu forums) could be further help... > usually somebody's been through it before. Asked on ubuntu forumn. thanks, Jim From robertvstepp at gmail.com Sat Oct 27 12:11:20 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 27 Oct 2018 11:11:20 -0500 Subject: [Tutor] (no subject) In-Reply-To: <5bd4475e.1c69fb81.41858.27cd@mx.google.com> References: <5bd4475e.1c69fb81.41858.27cd@mx.google.com> Message-ID: Greetings! On Sat, Oct 27, 2018 at 6:48 AM Jesse Stockman wrote: > > Hi there > > I need to draw a patten with turtle in python 3.7 but I cant get it to work ... What is the problem? Describe what you expected to happen, what did happen, and *copy and paste* the Traceback that Python generates. I had a bit of time, so I ran your code as you had it in your email, and I got the following: Traceback (most recent call last): File "draw_body.py", line 69, in main() File "draw_body.py", line 26, in main draw_podium(winner_color, second_color, third_color) NameError: name 'draw_podium' is not defined If you look at your code, your main() function calls this draw_podium() function, but you never give us that function in your email. Likewise, darw_third() [Note the spelling error here.], draw_winner(), and draw_second() are not defined anywhere in the code you provided. Looks like your next step is to write those functions. BTW "hight" should be "height", but as long as you are consistent, that spelling error should not cause any issues. One thing you can do to check for further issues with the code you *have* written so far, is to provide the missing functions with "pass" in each function's body, and then run your program and see if any other errors emerge. E.g., draw_podium(winner_color, second_color, third_color): pass draw_third(third_color): pass Etc. Hope this helps! > ...here are the specs of the pattern and my code so far can you please help > > ? Specifications of the pattern o The radius of the three heads is 10. > o The length of legs is 30. o The length of the sizes of the two triangles (the body of runner-up and third-place) is 40. They are all equal-size triangles. The winner?s body is a 40x40 square. o The width of the three blocks of the podium is 60. The heights are 90, 60, and 40 respectively. > > And my code so far > > from turtle import * > > x = 0 > y = 0 > radius = 0 > x1 = 0 > x2 = 0 > y1 = 0 > y2 = 0 > color = 0 > width = 0 > hight =0 > > > > def main(): > speed(0) > pensize(3) > pencolor("black") > > winner_color = "red" > second_color = "orange" > third_color = "purple" > draw_podium(winner_color, second_color, third_color) > darw_third(third_color) > draw_winner(winner_color) > draw_second(second_color) > > def move_to(x, y): > x = int(input("input X coordinate: ")) > y = int(input("input y coordinate: ")) > penup() > goto(x, y) > pendown() > > def draw_head(x, y, radius): > radius = int(input("input radius: ")) > move_to(x, y) > circle(radius) > > def draw_line(x1, y1, x2, y2): > x1 = int(input("line start X: ")) > y1 = int(input("line start Y: ")) > x2 = int(input("line end X: ")) > y2 = int(input("line end Y: ")) > penup() > goto(x1,y1) > pendown() > goto(x2,y2) > > def draw_block(x, y, hight, width, color): > move_to(x, y) > hight = int(input("input hight: ")) > width = int(input("input width: ")) > color(winner_color) > begin_fill() > forward(width) > left(90) > forward(hight) > left(90) > forward(width) > left(90) > forward(hight) > end_fill() > > > main() > draw_block(x, y, hight, width, color) > > > exitonclick() -- boB From bgailer at gmail.com Sat Oct 27 12:27:21 2018 From: bgailer at gmail.com (Bob Gailer) Date: Sat, 27 Oct 2018 12:27:21 -0400 Subject: [Tutor] (no subject) In-Reply-To: <5bd4475e.1c69fb81.41858.27cd@mx.google.com> References: <5bd4475e.1c69fb81.41858.27cd@mx.google.com> Message-ID: On Oct 27, 2018 7:48 AM, "Jesse Stockman" wrote: > > Hi there > > I need to draw a patten with turtle in python 3.7 but I cant get it to work here are the specs of the pattern and my code so far can you please help Thank you for asking for help. It would help us if you were more specific. "Can't get it to work" doesn't tell us much. Does the program run or do you get an error? If you get an error, also known as a traceback, copy the entire traceback and paste it into your reply. Otherwise show us your input and output. Tell us where it differs from what you expected. Again use copy and paste to show your results. > > ? Specifications of the pattern o The radius of the three heads is 10. > o The length of legs is 30. o The length of the sizes of the two triangles (the body of runner-up and third-place) is 40. They are all equal-size triangles. The winner?s body is a 40x40 square. o The width of the three blocks of the podium is 60. The heights are 90, 60, and 40 respectively. > > And my code so far > > from turtle import * > > x = 0 > y = 0 > radius = 0 > x1 = 0 > x2 = 0 > y1 = 0 > y2 = 0 > color = 0 > width = 0 > hight =0 > > > > def main(): > speed(0) > pensize(3) > pencolor("black") > > winner_color = "red" > second_color = "orange" > third_color = "purple" > draw_podium(winner_color, second_color, third_color) > darw_third(third_color) > draw_winner(winner_color) > draw_second(second_color) > > def move_to(x, y): > x = int(input("input X coordinate: ")) > y = int(input("input y coordinate: ")) > penup() > goto(x, y) > pendown() > > def draw_head(x, y, radius): > radius = int(input("input radius: ")) > move_to(x, y) > circle(radius) > > def draw_line(x1, y1, x2, y2): > x1 = int(input("line start X: ")) > y1 = int(input("line start Y: ")) > x2 = int(input("line end X: ")) > y2 = int(input("line end Y: ")) > penup() > goto(x1,y1) > pendown() > goto(x2,y2) > > def draw_block(x, y, hight, width, color): > move_to(x, y) > hight = int(input("input hight: ")) > width = int(input("input width: ")) > color(winner_color) > begin_fill() > forward(width) > left(90) > forward(hight) > left(90) > forward(width) > left(90) > forward(hight) > end_fill() > > > main() > draw_block(x, y, hight, width, color) > > > exitonclick() > > > please help me > thank you > kind regards > Tarantulam > > > Sent from Mail for Windows 10 > > _______________________________________________ > 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 Oct 27 08:34:38 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Oct 2018 13:34:38 +0100 Subject: [Tutor] Turtle graphics - was Re: (no subject) In-Reply-To: <5bd4475e.1c69fb81.41858.27cd@mx.google.com> References: <5bd4475e.1c69fb81.41858.27cd@mx.google.com> Message-ID: <42f5ffe0-7e33-75a6-749b-1f656472ebbb@yahoo.co.uk> On 27/10/2018 12:09, Jesse Stockman wrote: > Hi there Hi, Please always provide a meaningful subject line. Also always include the output of your program, especially if its an error message - these are full of useful information. > I need to draw a patten with turtle in python 3.7 but > I cant get it to work here are the specs of the pattern > and my code so far can you please help > > ? Specifications of the pattern o The radius of the three heads is 10. ... Since the specs provide the sizes you don't need all the input() calls, just use the data. It's much faster to run and test that way. > def main(): > speed(0) > pensize(3) > pencolor("black") > > winner_color = "red" > second_color = "orange" > third_color = "purple" > draw_podium(winner_color, second_color, third_color) Where is draw_podium defined? > darw_third(third_color) Check the spelling here? And also, where is draw_third() defined (or even darw_third) > draw_winner(winner_color) > draw_second(second_color) You can't call functions that don't exist. They must either be provided by an imported module or defined by you. > def move_to(x, y):... > > def draw_head(x, y, radius):... > def draw_line(x1, y1, x2, y2):... > def draw_block(x, y, hight, width, color):... > main() > draw_block(x, y, hight, width, color) > exitonclick() -- 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 akleider at sonic.net Sat Oct 27 18:21:41 2018 From: akleider at sonic.net (Alex Kleider) Date: Sat, 27 Oct 2018 15:21:41 -0700 Subject: [Tutor] books (for postage) Message-ID: I have the following gathering dust on my shelf: The Python Standard Library by Example by Doug Hellmann 3rd printing May 2012 Dive Into Python by Mark Pilgrim 2004 Learn Python the Hard Way (2nd Edition) by Zed A. Shaw June 2011 If anyone wants any or all of them, I'm happy to send them on for what ever it costs to mail. I could mail from either the US (94924 area code) or from Canada (V9L 6T2)- which ever works out cheapest (which may depend on the destination although in general I believe costs are cheaper in the States.) -- Alex Kleider (sent from my current gizmo) From michielnijs2010 at hotmail.com Sat Oct 27 15:41:20 2018 From: michielnijs2010 at hotmail.com (michiel nijs) Date: Sat, 27 Oct 2018 19:41:20 +0000 Subject: [Tutor] Problem with task, please help! Message-ID: Hello, I am an engineering student and we have to use Python. We don't have lessons so we all have to learn it on our own. We got a task and I am stuck on this: "Count the number of parcels for forrest ground and count the number of parcels for orchard ground". I think I have to use the for-loop but I don't know how. This is what i already have: import numpy as np import csv from matplotlib import pyplot as plt def getlanduse(): with open('LandUse.csv', encoding='latin-1') as csvfile: readCSV = csv.DictReader(csvfile, delimiter=';') return list(readCSV) In the file i renamed the things we need from dutch to english :) Thank you very much! From alan.gauld at yahoo.co.uk Sun Oct 28 05:27:29 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 28 Oct 2018 09:27:29 +0000 Subject: [Tutor] Problem with task, please help! In-Reply-To: References: Message-ID: On 27/10/2018 20:41, michiel nijs wrote: > I am an engineering student and we have to use Python. > We don't have lessons so we all have to learn it on our own. What, no lessons at all?! Or just no lessons on Python? Have you done any kind of programming before? > We got a task and I am stuck on this: > "Count the number of parcels for forrest ground and > count the number of parcels for orchard ground". When starting out on a problem its always good to consider how you would solve it without a computer, just using pen and paper. Can you write down the algorithm steps you would use to do this manually? > I think I have to use the for-loop but I don't know how. Do you mean you don't know how to use a for loop? In that case read any Python tutorial on looping. (Try mine in the .sig if you like) Or do you mean you don't know how to apply it here? If the latter then you have already written the code to read the data from the file. Now you need to loop over that data analysing each line. Can you write a function (isForest() ) that extracts the data you want from a line and returns true if it is a forest? And another similar (isOrchard() ) that returns true if an orchard? Now, in your loop, you just need to test whether it's a forest and increment the counter if it is. And if it's not, test if it's an orchard and increment the other counter is it is. It will look something like: forests, orchards = 0,0 for line in getLandUse(): if isForest(line): forests += 1 if isOrchard(line) orchards += 1 print (forests, orchards) That's not the most efficient solution but it should be easy to understand and debug. > import numpy as np > import csv > from matplotlib import pyplot as plt You only really need csv for this task. > def getlanduse(): > with open('LandUse.csv', encoding='latin-1') as csvfile: > readCSV = csv.DictReader(csvfile, delimiter=';') > return list(readCSV) HTH -- 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 quentin.agren at gmail.com Mon Oct 29 10:10:19 2018 From: quentin.agren at gmail.com (Quentin Agren) Date: Mon, 29 Oct 2018 15:10:19 +0100 Subject: [Tutor] Module (.py file) that behaves like package Message-ID: Hi, By playing around with importlib I noticed that if you define `__path__ = [os.path.dirname(__file__)]` in a module named spam.py, then you could `import spam.spam.spam` or `from spam import spam`. In other words, spam.py behaves like a package although its loader, if asked, tells that it is not, and that accordingly you cannot `from . import spam` (in the same file, after setting `__path__`) Is this intended behavior? (Maybe I should just get out more...) Thanks! Quentin From alan.gauld at yahoo.co.uk Mon Oct 29 13:25:39 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 Oct 2018 17:25:39 +0000 Subject: [Tutor] Module (.py file) that behaves like package In-Reply-To: References: Message-ID: On 29/10/2018 14:10, Quentin Agren wrote: > ... (Maybe I should just get out more...) That depends on whether you can foresee a use for importlib in your code. If so then getting to know it isn't a bad thing. If not, do you really want to be looking back on your 60th birthday thinking "Did I really spend a week of my life investigating importlib?" :-) FWIW In 20 years of using Python I've never used importlib in any of my real-world programs... I'm sure somebody has, but not me. So I'd spend my time investigating modules I may actually use, like itertools, functools, threading, multiprocessing or asyncio... Life's too short! -- 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 quentin.agren at gmail.com Mon Oct 29 13:36:56 2018 From: quentin.agren at gmail.com (Quentin Agren) Date: Mon, 29 Oct 2018 18:36:56 +0100 Subject: [Tutor] Module (.py file) that behaves like package In-Reply-To: References: Message-ID: Hi Alan, > That depends on whether you can foresee a use for importlib in > your code. Slightly disagreeing here. I think reading importlib source is a great way of understanding how the python import system works (if you don't want to read C, that is). I think a learnt a lot and it helped me to understand some frustrating errors I encountered in my day to day programming, for example regarding module reloading (e.g. using autoreload in ipython). I was more wondering if the example I pointed out was really contrived or if this inconsistency in behavior could be an issue to fix. > "Did I really spend a week of my > life investigating importlib?" > To quote Ella Fitzgerald: "It's ain't what you do, it's the wat that you do it" ;) FWIW In 20 years of using Python I've never used > importlib in any of my real-world programs... > I'm sure somebody has, but not me. So I'd spend > my time investigating modules I may actually > use, like itertools, functools, threading, > multiprocessing or asyncio... Life's too short! > Definitely so much to learn... Thanks! Quentin