From alan.gauld at btinternet.com Fri Jun 1 05:02:47 2018 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 1 Jun 2018 10:02:47 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <07bf5176-8268-4b1f-364f-4f9e46f217c1@btinternet.com> On 31/05/18 07:06, erich callahana wrote: > I?m not sure how to access this window > Unfortunately this is a text mailing list and the server strips binary attachments for security reasons. Either send us a link to a web page or describe what it is you are trying to do. Include the program name, the OS, and the Python version you are using. -- 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 ch.pascucci at gmail.com Fri Jun 1 09:00:39 2018 From: ch.pascucci at gmail.com (chiara pascucci) Date: Fri, 1 Jun 2018 15:00:39 +0200 Subject: [Tutor] guess my number game (reversed) Message-ID: Hello. I am very new to learning Python, and I would need some help with a little game I am trying to write. I am trying to write a game where the user is asked to think of a number from 1 to a 100, and the computer tries to guess it. Now, after every guess I would want the programme to ask whether its guess is right, too low, or too high, and then take another guess based on 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 -but I can't seem to be able to fix it! Any help or commments would be very appreciated. Below is the code written so far Many thanks Chiara 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") input("\n\nPress enter key to exit") From alan.gauld at yahoo.co.uk Fri Jun 1 13:12:02 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 1 Jun 2018 18:12:02 +0100 Subject: [Tutor] guess my number game (reversed) In-Reply-To: References: Message-ID: 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 From farooghkarimizadeh at gmail.com Fri Jun 1 12:36:40 2018 From: farooghkarimizadeh at gmail.com (Farooq Karimi Zadeh) Date: Fri, 1 Jun 2018 21:06:40 +0430 Subject: [Tutor] guess my number game (reversed) In-Reply-To: References: Message-ID: Add a new variable named "number of guesses", increase it each time and break the loop when it reaches, for example 10. This means user can have 10 guesses max. If they couldn't guess the number, they are losers! You also could do the opposite: have number of guesses=10 in the beginning and decrease it by 1 each time till it reaches zero. On 01/06/2018, chiara pascucci wrote: > Hello. > > I am very new to learning Python, and I would need some help with a little > game I am trying to write. I am trying to write a game where the user is > asked to think of a number from 1 to a 100, and the computer tries to guess > it. Now, after every guess I would want the programme to ask whether its > guess is right, too low, or too high, and then take another guess based on > 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 -but I can't seem > to be able to fix it! > Any help or commments would be very appreciated. > Below is the code written so far > > Many thanks > > Chiara > > 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") > > > input("\n\nPress enter key to exit") > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From jeremyo at vt.edu Mon Jun 4 11:57:54 2018 From: jeremyo at vt.edu (Jeremy Ogorzalek) Date: Mon, 4 Jun 2018 11:57:54 -0400 Subject: [Tutor] Question Regarding startswith() Message-ID: Not sure this is how this is done, but here goes. When I try to run the code in the SGP4 module, I get the following error, and it originates in the io.py script: File "C:\ProgramData\Anaconda3\lib\site-packages\sgp4\io.py", line 131, in twoline2rv assert line.startswith('1 ') TypeError: startswith first arg must be bytes or a tuple of bytes, not str But as far as I can tell in the documentation online (multiple sources) the first argument of startswith() is indeed supposed to be a string! It should be the string of text you are searching for! So, what gives? I've also tried to make the code happy by converting the string to bytes, tuple, etc.... and still get the same error. Any help would be much appreciated. Thanks! Jeremy Ogorzalek M.S., Aerospace Engineering Virginia Tech 443-812-3121 From alan.gauld at yahoo.co.uk Mon Jun 4 13:46:54 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 4 Jun 2018 18:46:54 +0100 Subject: [Tutor] Question Regarding startswith() In-Reply-To: References: Message-ID: On 04/06/18 16:57, Jeremy Ogorzalek wrote: > Not sure this is how this is done, but here goes. > > When I try to run the code in the SGP4 module, I get the following error, > and it originates in the io.py script: I have no idea what you are talking about and do not know what the SGP4 or io.py files are. However... > File "C:\ProgramData\Anaconda3\lib\site-packages\sgp4\io.py", line 131, > in twoline2rv > assert line.startswith('1 ') > > TypeError: startswith first arg must be bytes or a tuple of bytes, not str > > But as far as I can tell in the documentation online (multiple sources) the > first argument of startswith() is indeed supposed to be a string! It depends on which startswith() method you are looking at. If line is a string you are correct but... if line is a bytes object then its startwith() method expects a bytes argument. So I suspect that line is of type bytes(*) and you must convert it to a string or use a bytes first argument. (*)You can check by adding a print("type of line: ", type(line)) in your code. 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 __peter__ at web.de Mon Jun 4 15:56:24 2018 From: __peter__ at web.de (Peter Otten) Date: Mon, 04 Jun 2018 21:56:24 +0200 Subject: [Tutor] Question Regarding startswith() References: Message-ID: Jeremy Ogorzalek wrote: > Not sure this is how this is done, but here goes. > > When I try to run the code in the SGP4 module, I get the following error, > and it originates in the io.py script: > > > File "C:\ProgramData\Anaconda3\lib\site-packages\sgp4\io.py", line 131, > in twoline2rv > assert line.startswith('1 ') > > TypeError: startswith first arg must be bytes or a tuple of bytes, not str > > But as far as I can tell in the documentation online (multiple sources) > the first argument of startswith() is indeed supposed to be a string! It > should be the string of text you are searching for! So, what gives? I've > also tried to make the code happy by converting the string to bytes, > tuple, etc.... and still get the same error. Any help would be much > appreciated. Thanks! Do you read the first two arguments of the twoline2rv() function from a file? If so you probably open that file binary mode, but you have to open it in text mode to get strings. If that's not possible because the file contains other binary data you may convert the byte strings to unicode explicitly: >>> line = b"1 some bytes" >>> line.startswith("1 ") Traceback (most recent call last): File "", line 1, in TypeError: startswith first arg must be bytes or a tuple of bytes, not str >>> line = line.decode() >>> line.startswith("1 ") True From rls4jc at gmail.com Tue Jun 5 16:46:06 2018 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Tue, 5 Jun 2018 13:46:06 -0700 Subject: [Tutor] Turtle drawing Message-ID: I give up again. Here's my code which gives no error, but draws something that looks sort of like a dinosaur. I've included the first few lines of the text file mystery so you can see the format I received from the instructions. Please help. Thank you. *****Mystery excerpt***** UP -218 185 DOWN -240 189 -246 188 -248 183 -246 178 ********** # Draw a picture based on the txt file mystery # When UP go to the next line, read x,y coordinates, go to that point # When DOWN go to the next line, read x,y coordinates, go to that point and continue until UP import turtle wn = turtle.Screen() # Set up the window and its attributes wn.bgcolor("beige") hadir = turtle.Turtle() # create hadir hadir.color('orange') hadir.speed(0) datums = open("C:/Users/Roger/Documents/GitHub/LaunchCode/mystery.txt", "r") for aline in datums: splitted = aline.split() try: coords = [int(i) for i in splitted] x = coords[0] y = coords[1] hadir.goto(x, y) except: while splitted == "UP": hadir.pu() while splitted == "DOWN": hadir.pd() turtle.exitonclick() -- Roger Lea Scherer 623.255.7719 From alan.gauld at yahoo.co.uk Tue Jun 5 18:12:57 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 5 Jun 2018 23:12:57 +0100 Subject: [Tutor] Turtle drawing In-Reply-To: References: Message-ID: On 05/06/18 21:46, Roger Lea Scherer wrote: > datums = open("C:/Users/Roger/Documents/GitHub/LaunchCode/mystery.txt", "r") > > for aline in datums: > splitted = aline.split() > try:... > except: > while splitted == "UP": > hadir.pu() splitted will never equal 'UP' or 'DOWN' because the result of split() is always a list - even if its empty. So I suspect you are not actually executing any up/down operations. Try using splitted[0] instead. The other problem is that you use a while loop but never change the condition values so, if it ever did pass the test, you'd wind up with an infinite loop! You only need an if clause not a while. 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 rls4jc at gmail.com Thu Jun 7 18:21:13 2018 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Thu, 7 Jun 2018 15:21:13 -0700 Subject: [Tutor] Iteration issues In-Reply-To: References: Message-ID: I've given up again. I've tried the python documentation, I've tried to implement the above suggestions, I've tried the translate thing with a table that I did not include this time because I couldn't get it to work, I've looked at StackOverflow, Quora and another website, so many things seem so promising, but I just can't get them to work nor figure out why they won't. I've started from scratch again. ********** import string gettysburg = open("C:/Users/Roger/Documents/GitHub/LaunchCode/gettysburg.txt", "r") puncless = "" for char in gettysburg: if char in string.punctuation: gettysburg.replace(char, "") else: puncless += char print(puncless) ********** The "puncless" part works fine, except it does not replace the punctuation like I expect and want. Thank you for all your help already, but I need some more if you have any. :) Thanks. On Sat, May 12, 2018 at 12:40 AM, Peter Otten <__peter__ at web.de> wrote: > Neil Cerutti wrote: > > > punctuation_removal_table = str.maketrans({c: None for c in > > string.punctuation}) > > Alternative spellings: > > >>> from string import punctuation > >>> (str.maketrans({c: None for c in punctuation}) > ... == str.maketrans(dict.fromkeys(punctuation)) > ... == str.maketrans("", "", punctuation)) > True > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Roger Lea Scherer 623.255.7719 From alan.gauld at yahoo.co.uk Thu Jun 7 18:52:20 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 7 Jun 2018 23:52:20 +0100 Subject: [Tutor] Iteration issues In-Reply-To: References: Message-ID: On 07/06/18 23:21, Roger Lea Scherer wrote: > they won't. I've started from scratch again. > > ********** > import string > > gettysburg = > open("C:/Users/Roger/Documents/GitHub/LaunchCode/gettysburg.txt", "r") > > puncless = "" > for char in gettysburg: I think this is wrong. gettysburg is a file and the iterator for a file returns a string, so "char" should probably be "line". Either that or you need to call read() on the file first. > if char in string.punctuation: > gettysburg.replace(char, "") But this is also dodgy since you are changing the thing you are itrerating over. That's a bit like cutting off the branch of a tree that you are sitting on - bad things can happen. And to further confuse matters strings are immutable so the replace returns a new string which you throw away. But you don't really need to replace the char in the original you just need to ignore it: for char in gettysburg.read(): if char not in string.punctuation: puncless += char print(puncless) -- 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 Thu Jun 7 18:56:22 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 7 Jun 2018 16:56:22 -0600 Subject: [Tutor] Iteration issues In-Reply-To: References: Message-ID: On 06/07/2018 04:21 PM, Roger Lea Scherer wrote: > I've given up again. I've tried the python documentation, I've tried to > implement the above suggestions, I've tried the translate thing with a > table that I did not include this time because I couldn't get it to work, > I've looked at StackOverflow, Quora and another website, so many things > seem so promising, but I just can't get them to work nor figure out why > they won't. I've started from scratch again. > > ********** > import string > > gettysburg = > open("C:/Users/Roger/Documents/GitHub/LaunchCode/gettysburg.txt", "r") > > puncless = "" > for char in gettysburg: > if char in string.punctuation: > gettysburg.replace(char, "") > else: > puncless += char > print(puncless) > > ********** > > The "puncless" part works fine, except it does not replace the punctuation > like I expect and want. You're doing multiple things, and they're not agreeing. somestring.replace() returns a new string with the replacement done. but you don't ever do anything with that new string. also you're writing as if you do the replacement character-by-character, but replace() operates on the whole string (or if you give it the extra count arguments, on only the first count instances of your substring), and strings themselves are immutable, so it is never changed. replace() is also harmless if the substring is not found, so you could try this, for example: for char in string.punctuation: gettysburg = gettysburg.replace(char, "") print(gettysburg) each time through that you get a *new* gettysburg with replacement done on it, with the old one being lost due to no more references - you are not actually ever modifying gettysburg itself. > > Thank you for all your help already, but I need some more if you have any. > :) Thanks. > > On Sat, May 12, 2018 at 12:40 AM, Peter Otten <__peter__ at web.de> wrote: > >> Neil Cerutti wrote: >> >>> punctuation_removal_table = str.maketrans({c: None for c in >>> string.punctuation}) >> >> Alternative spellings: >> >>>>> from string import punctuation >>>>> (str.maketrans({c: None for c in punctuation}) >> ... == str.maketrans(dict.fromkeys(punctuation)) >> ... == str.maketrans("", "", punctuation)) >> True >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > > From caspertaan at gmail.com Sat Jun 9 08:39:48 2018 From: caspertaan at gmail.com (Casper Rasmussen) Date: Sat, 9 Jun 2018 14:39:48 +0200 Subject: [Tutor] Need help with a login system Message-ID: So I am trying to make a simple login system just for fun and found this video that has helped me a lot but there is a line I am not really sure how works. Screenshot at the bottom The line I don't understand is: user = {} I understand the how list works but this list I can't figure out. The reason why I want to know how it works is so I can put in users before the program launches. The program works fine right now but I as said I can't figure out how to put in users before the program launches. (Mine is not identical to the screenshot but because I am danish I figured it would only be confusing if I send a screenshot with danish texts in it) Many thanks in advance Best regards Casper From steve at pearwood.info Sat Jun 9 09:03:09 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 9 Jun 2018 23:03:09 +1000 Subject: [Tutor] Need help with a login system In-Reply-To: References: Message-ID: <20180609130308.GS12683@ando.pearwood.info> On Sat, Jun 09, 2018 at 02:39:48PM +0200, Casper Rasmussen wrote: > So I am trying to make a simple login system just for fun and found this > video that has helped me a lot but there is a line I am not really sure how > works. > Screenshot at the bottom Unless you edit your code with Photoshop, don't use screenshots to send pictures of code. Send the code itself. Just copy and paste it. Not only are binary attachments like images, videos, sound files etc deleted by the mailing list, but screen shots make it hard for the visually impaired and blind to contribute to the discussion. They also make it impossible for the recipients to work with the posted code, since it isn't text that can be copied and tested and edited, just pixels. > The line I don't understand is: > > user = {} > > I understand the how list works but this list I can't figure out. That's because it isn't a list. https://docs.python.org/3/tutorial/datastructures.html#dictionaries -- Steve From joseph.slater at wright.edu Tue Jun 12 21:52:49 2018 From: joseph.slater at wright.edu (Slater, Joseph C.) Date: Wed, 13 Jun 2018 01:52:49 +0000 Subject: [Tutor] Import module function using variable Message-ID: <19F3B56C-2963-4D65-A0C6-5E21D39B1C36@wright.edu> Dear friends, I am trying to import a function in a module by variable name. The specific example is that my function allows the user to select which function my code will use (in this case, which optimizer in scipy). There is a default for a named variable. I have thing working in a quite unclean way- I import them all and then look them up in globals. I'm not trying to clean it up. Essentially I'd like to method = 'newton_krylov' chosen_optimize_func = (from scipy.optimize import method) Of course, even I know this shouldn't work. What I tried is solver_method = __import__('scipy.optimize.' + method, fromlist=['']) which yields: --------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) in () ----> 1 solver_method = __import__('scipy.optimize.' + method, fromlist=['']) ModuleNotFoundError : No module named 'scipy.optimize.newton_krylov' Well, no, it's not a module but a function. Any thoughts on how to do this? Thank You- Joe From alan.gauld at yahoo.co.uk Wed Jun 13 03:37:12 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 13 Jun 2018 08:37:12 +0100 Subject: [Tutor] Import module function using variable In-Reply-To: <19F3B56C-2963-4D65-A0C6-5E21D39B1C36@wright.edu> References: <19F3B56C-2963-4D65-A0C6-5E21D39B1C36@wright.edu> Message-ID: On 13/06/18 02:52, Slater, Joseph C. wrote: > I am trying to import a function in a module by variable name. I'd suggest getattr(): >>> import sys >>> xt = getattr(sys,exit) >>> xt() Alternatively, if its a fixed set of options (and it sound as if it is) set up a dictionary: funcs = {'exit': sys.exit, size: sys.getsizeof) xt = funcs('exit') xt() Which has the advantage of providing a menu of friendly function names to the user: for func in funcs: print(func) fn = input('choose a function from the list above') And allows you to validate the selection if fn in funcs.keys(): f = funcs[fn] else: print(...) 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 __peter__ at web.de Wed Jun 13 03:37:40 2018 From: __peter__ at web.de (Peter Otten) Date: Wed, 13 Jun 2018 09:37:40 +0200 Subject: [Tutor] Import module function using variable References: <19F3B56C-2963-4D65-A0C6-5E21D39B1C36@wright.edu> Message-ID: Slater, Joseph C. wrote: > Dear friends, > > I am trying to import a function in a module by variable name. The > specific example is that my function allows the user to select which > function my code will use (in this case, which optimizer in scipy). There > is a default for a named variable. I have thing working in a quite unclean > way- I import them all and then look them up in globals. I'm not trying to > clean it up. > > Essentially I'd like to > > method = 'newton_krylov' > > chosen_optimize_func = (from scipy.optimize import method) > > Of course, even I know this shouldn't work. What I tried is > > solver_method = __import__('scipy.optimize.' + method, fromlist=['']) > > which yields: > --------------------------------------------------------------------------- > ModuleNotFoundError > Traceback (most recent call last) > > in () > ----> 1 solver_method = __import__('scipy.optimize.' + method, > fromlist=['']) > > > > ModuleNotFoundError > : No module named 'scipy.optimize.newton_krylov' > > > Well, no, it's not a module but a function. Indeed, the function is a name in a module, or an attribute of the module object. You need two steps to access it. First import the module, then use getattr() to extract the function, preferrably >>> import scipy.optimize >>> method = 'newton_krylov' >>> chosen_optimize_func = getattr(scipy.optimize, method) >>> chosen_optimize_func but if the module varies, too, use import_module(): >>> import importlib >>> getattr(importlib.import_module("scipy.optimize"), "newton_krylov") From niharika1883 at gmail.com Wed Jun 13 06:03:08 2018 From: niharika1883 at gmail.com (Niharika Jakhar) Date: Wed, 13 Jun 2018 12:03:08 +0200 Subject: [Tutor] tab separated file handling Message-ID: hi everyone! I am working with a tsv file which has NA and empty values. I have used csv package to make a list of list of the data. I want to remove NA and empty values. This is what I wrote: #removes row with NA values for rows in self.dataline: for i in rows: if i == 'NA' or i == '': self.dataline.remove(rows) This is what the terminal says: self.dataline.remove(rows) ValueError: list.remove(x): x not in list This is how the file looks like: d23 87 9 NA 67 5 657 NA 76 8 87 78 90 800 er 21 8 908 9008 9 7 5 46 3 5 757 7 5 From joel.goldstick at gmail.com Wed Jun 13 08:22:33 2018 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 13 Jun 2018 08:22:33 -0400 Subject: [Tutor] tab separated file handling In-Reply-To: References: Message-ID: On Wed, Jun 13, 2018 at 6:03 AM, Niharika Jakhar wrote: > hi everyone! > I am working with a tsv file which has NA and empty values. > I have used csv package to make a list of list of the data. > I want to remove NA and empty values. > > This is what I wrote: > > > #removes row with NA values > for rows in self.dataline: > for i in rows: > if i == 'NA' or i == '': > self.dataline.remove(rows) > > > This is what the terminal says: > > self.dataline.remove(rows) > ValueError: list.remove(x): x not in list > > > This is how the file looks like: > > d23 87 9 NA 67 5 657 NA 76 8 87 78 90 800 > er 21 8 908 9008 9 7 5 46 3 5 757 7 5 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor for i in rows: Gives you the complete line as a list. From there your test should be: if "NA" in i: # throw away this line -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From __peter__ at web.de Wed Jun 13 09:03:37 2018 From: __peter__ at web.de (Peter Otten) Date: Wed, 13 Jun 2018 15:03:37 +0200 Subject: [Tutor] tab separated file handling References: Message-ID: Niharika Jakhar wrote: > hi everyone! > I am working with a tsv file which has NA and empty values. > I have used csv package to make a list of list of the data. > I want to remove NA and empty values. > > This is what I wrote: > > > #removes row with NA values > for rows in self.dataline: > for i in rows: > if i == 'NA' or i == '': > self.dataline.remove(rows) > > > This is what the terminal says: > > self.dataline.remove(rows) > ValueError: list.remove(x): x not in list > > > This is how the file looks like: > > d23 87 9 NA 67 5 657 NA 76 8 87 78 90 800 > er 21 8 908 9008 9 7 5 46 3 5 757 7 5 I believe your biggest problem is the choice of confusing names ;) Let's assume you start with a "table" as a list of rows where one "row" is a list of "value"s. Then for row in table: for value in row: if value == "NA" or value == "": table.remove(value) must fail because in table there are only lists, each representing one row, but no strings. However, when you fix it in the obvious way for row in table: for value in row: if value == "NA" or value == "": row.remove(value) you run into another problem: >>> row = ["a", "b", "NA", "c", "d"] >>> for value in row: ... print("checking", value) ... if value == "NA": row.remove(value) ... checking a checking b checking NA checking d Do you see the bug? "c" is never tested. It might be another "NA" that survives the removal. You should never modify lists you are iterating over. There are a few workarounds, but in your case I think the best approach is not to put them in the table in the first place: with open("whatever.tsv") as f: reader = csv.reader(f, delimiter="\t") table = [ [value for value in row if value not in {"NA", ""}] for row in reader ] Here the inner list comprehension rejects "NA" and "" immediately. That will of course lead to rows of varying length. If your intention was to skip rows containing NA completely, use with open("whatever.tsv") as f: reader = csv.reader(f, delimiter="\t") table = [ row for row in reader if "NA" not in row and "" not in row ] or ILLEGAL = {"NA", ""} table = [ row for row in reader if not ILLEGAL.intersection(row) ] From neilc at norwich.edu Wed Jun 13 09:38:07 2018 From: neilc at norwich.edu (Neil Cerutti) Date: Wed, 13 Jun 2018 13:38:07 +0000 (UTC) Subject: [Tutor] tab separated file handling References: Message-ID: On 2018-06-13, Niharika Jakhar wrote: > hi everyone! > I am working with a tsv file which has NA and empty values. > I have used csv package to make a list of list of the data. > I want to remove NA and empty values. Should the the data 40 50 NA 12 mean the same thing as 40 50 12? -- Neil Cerutti From mats at wichmann.us Wed Jun 13 10:35:48 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 13 Jun 2018 08:35:48 -0600 Subject: [Tutor] Import module function using variable In-Reply-To: References: <19F3B56C-2963-4D65-A0C6-5E21D39B1C36@wright.edu> Message-ID: <72a5c021-7ad1-3e3a-b710-bfbdf870d741@wichmann.us> On 06/13/2018 01:37 AM, Peter Otten wrote: > Slater, Joseph C. wrote: > >> Dear friends, as others have said, I'll also say: your problem statement is fuzzy. if your data looks like this: > d23 87 9 NA 67 5 657 NA 76 8 87 78 90 800 > er 21 8 908 9008 9 7 5 46 3 5 757 7 5 is it meaningful that there are 14 items in a "row"? or is the row still valid if you remove some items from it? That is, does the position in the row matter? If it does, then you should replace NA by some value Python can recognize - for example None - instead of trying to delete the element. tab-separated is a pretty poor choice of data format if fields can be blank - you can't spot blanks easily, unless you're fortunate enough that each "column" can be displayed in less than a tab-width so it always lines up. that's a human problem, not a computer problem, but could impede your ability to visually inspect that things look sane. Using a non-whitespace separator character would be better. or... is this just a list of values and there is really no row concept? In that case, don't bring rows into it, and drop the invalid entries on reading. From mats at wichmann.us Wed Jun 13 10:43:38 2018 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 13 Jun 2018 08:43:38 -0600 Subject: [Tutor] Import module function using variable In-Reply-To: <72a5c021-7ad1-3e3a-b710-bfbdf870d741@wichmann.us> References: <19F3B56C-2963-4D65-A0C6-5E21D39B1C36@wright.edu> <72a5c021-7ad1-3e3a-b710-bfbdf870d741@wichmann.us> Message-ID: <4994f157-f82e-aeba-35aa-250bc33fb69d@wichmann.us> On 06/13/2018 08:35 AM, Mats Wichmann wrote: > On 06/13/2018 01:37 AM, Peter Otten wrote: >> Slater, Joseph C. wrote: >> >>> Dear friends, > > as others have said, I'll also say: your problem statement is fuzzy. Sorry folks, this was obviously in reply to a different thread, operator error! ([Tutor] tab separated file handling) From joseph.slater at wright.edu Wed Jun 13 09:13:30 2018 From: joseph.slater at wright.edu (Slater, Joseph C.) Date: Wed, 13 Jun 2018 13:13:30 +0000 Subject: [Tutor] Import module function using variable In-Reply-To: References: <19F3B56C-2963-4D65-A0C6-5E21D39B1C36@wright.edu> Message-ID: > On Jun 13, 2018, at 3:37 AM, Peter Otten <__peter__ at web.de> wrote: > > Slater, Joseph C. wrote: > >> Dear friends, >> >> I am trying to import a function in a module by variable name. The >> specific example is that my function allows the user to select which >> function my code will use (in this case, which optimizer in scipy). There >> is a default for a named variable. I have thing working in a quite unclean >> way- I import them all and then look them up in globals. I'm not trying to >> clean it up. >> >> Essentially I'd like to >> >> method = 'newton_krylov' >> >> chosen_optimize_func = (from scipy.optimize import method) >> >> Of course, even I know this shouldn't work. What I tried is >> >> solver_method = __import__('scipy.optimize.' + method, fromlist=['']) >> >> which yields: >> > --------------------------------------------------------------------------- >> ModuleNotFoundError >> Traceback (most recent call last) >> >> in () >> ----> 1 solver_method = __import__('scipy.optimize.' + method, >> fromlist=['']) >> >> >> >> ModuleNotFoundError >> : No module named 'scipy.optimize.newton_krylov' >> >> >> Well, no, it's not a module but a function. > > Indeed, the function is a name in a module, or an attribute of the module > object. You need two steps to access it. First import the module, then use > getattr() to extract the function, preferrably > >>>> import scipy.optimize >>>> method = 'newton_krylov' >>>> chosen_optimize_func = getattr(scipy.optimize, method) >>>> chosen_optimize_func > > > but if the module varies, too, use import_module(): > >>>> import importlib >>>> getattr(importlib.import_module("scipy.optimize"), "newton_krylov") > 4 years of Python with 4 packages on pypi and getattr was unknown to me. Thank you for your graciousness. This should have been quite trivial to obtain. FWIW: no prompting for this code. It wouldn't make sense. However, I do, at some point, need to include checking to ensure that users provide a response that is an available one. So far, this seems to be necessary as the default has been so consistently the best choice as to beg the question why I even offer another choice. Best Regards- Joe https://people.wright.edu/joseph.slater From jf_byrnes at comcast.net Wed Jun 13 20:55:01 2018 From: jf_byrnes at comcast.net (Jim) Date: Wed, 13 Jun 2018 19:55:01 -0500 Subject: [Tutor] Virtual environment can't find uno Message-ID: Running Linux Mint 18. I have python 3.6 running in a virtual environment. I want to use a package called oosheet to work with libreoffice calc. When I try to import it I get the following error: >>> import oosheet Traceback (most recent call last): File "", line 1, in File "/home/jfb/EVs/env36/lib/python3.6/site-packages/oosheet/__init__.py", line 38, in import uno, re, zipfile, types, inspect, tempfile, shutil, subprocess ModuleNotFoundError: No module named 'uno' If I do the same thing in the system installed python3 it imports and runs with no errors. python3-uno was installed using apt-get. How do I get python 3.6 in the virtual environment to find uno? Thanks, Jim From deepakdixit0001 at gmail.com Thu Jun 14 03:04:57 2018 From: deepakdixit0001 at gmail.com (Deepak Dixit) Date: Thu, 14 Jun 2018 12:34:57 +0530 Subject: [Tutor] How default arg of function works Message-ID: I am learning python and working with function. Here is my test program :- program.py ------------------------------------ def test1(nums=[]): return nums def test2(nums=[]): nums.append(len(nums)); return nums print "Calling test1" print '=' * 40 print 'test1()', test1() print 'test1([1,2])', test1([1,2]) print 'test1()', test1() print 'test1([1,1,1])', test1([1,1,1]) print 'test1()', test1() print "Calling test2" print '=' * 40 print 'test2()', test2() print 'test2([1,2,3])', test2([1,2,3]) print 'test2([1,2])', test2([1,2]) print 'test2()', test2() print 'test2()', test2() -------------------------------------------------- # python program.py Calling test1 ======================================== test1() [ ] test1([1,2]) [1, 2] test1() [ ] test1([1,1,1]) [1, 1, 1] test1() [ ] Calling test2 ======================================== test2() [0] test2([1,2,3]) [1, 2, 3, 3] test2([1,2]) [1, 2, 2] test2() [0, 1] test2() [0, 1, 2] I am assuming that in test1() we are not doing any modification in the passed list and because of that its working as per my expectation. But when I am calling test2() with params and without params then both are using different references. Why ? Can you please help me to understand this. -- With Regards, Deepak Kumar Dixit From alan.gauld at yahoo.co.uk Thu Jun 14 03:28:37 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 14 Jun 2018 08:28:37 +0100 Subject: [Tutor] How default arg of function works In-Reply-To: References: Message-ID: On 14/06/18 08:04, Deepak Dixit wrote: > def test2(nums=[]): > nums.append(len(nums)); > return nums > > print 'test2()', test2() > print 'test2([1,2,3])', test2([1,2,3]) > print 'test2([1,2])', test2([1,2]) > print 'test2()', test2() > print 'test2()', test2() > > Calling test2 > ======================================== > test2() [0] > test2([1,2,3]) [1, 2, 3, 3] > test2([1,2]) [1, 2, 2] > test2() [0, 1] > test2() [0, 1, 2] > > I am assuming that in test1() we are not doing any modification in the > passed list and because of that its working as per my expectation. Correct. You are still dealing with 2 separate objects(see below) but you don't modify anything so it looks like it works as you expected. > But when I am calling test2() with params and without params then both are > using different references. Why ? Can you please help me to understand > this. When you create a default value for a function parameter Python creates an actual object and uses that object for every invocation of the function that uses the default. If the object is mutable (a list or class instance, say) then any modifications to that object will stick and be present on subsequent calls. Thus in your case the first call of the function the list is empty and has len() 0, the second call it still has the zero stored so len() is now 1, and so on. But when you call the function without using the default Python does not involve the default object at all, it uses whichever object you provide. The same applies to your tst1 function. Each time you use the default call the same empty list object is being printed. But because you don't modify it it always appears as an empty list. -- 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 deepakdixit0001 at gmail.com Thu Jun 14 03:40:29 2018 From: deepakdixit0001 at gmail.com (Deepak Dixit) Date: Thu, 14 Jun 2018 13:10:29 +0530 Subject: [Tutor] How default arg of function works In-Reply-To: References: Message-ID: You mean that for default args and passed args of mutable type, python uses different object and same reference will be used for further calling of the function. Now one more thing I want to ask you that how can I get deep understanding of python like how list, dictionary works internally and other topics , is it only possible after more and more practice. Any suggestions from your side will be really helpful for me. On Thu, Jun 14, 2018, 1:00 PM Alan Gauld via Tutor wrote: > On 14/06/18 08:04, Deepak Dixit wrote: > > > def test2(nums=[]): > > nums.append(len(nums)); > > return nums > > > > print 'test2()', test2() > > print 'test2([1,2,3])', test2([1,2,3]) > > print 'test2([1,2])', test2([1,2]) > > print 'test2()', test2() > > print 'test2()', test2() > > > > Calling test2 > > ======================================== > > test2() [0] > > test2([1,2,3]) [1, 2, 3, 3] > > test2([1,2]) [1, 2, 2] > > test2() [0, 1] > > test2() [0, 1, 2] > > > > I am assuming that in test1() we are not doing any modification in the > > passed list and because of that its working as per my expectation. > > Correct. You are still dealing with 2 separate objects(see below) > but you don't modify anything so it looks like it works as you > expected. > > > But when I am calling test2() with params and without params then both > are > > using different references. Why ? Can you please help me to understand > > this. > > When you create a default value for a function parameter Python > creates an actual object and uses that object for every invocation > of the function that uses the default. If the object is mutable > (a list or class instance, say) then any modifications to that > object will stick and be present on subsequent calls. > > Thus in your case the first call of the function the list is > empty and has len() 0, the second call it still has the zero > stored so len() is now 1, and so on. > > But when you call the function without using the default > Python does not involve the default object at all, it uses > whichever object you provide. > > The same applies to your tst1 function. Each time you use > the default call the same empty list object is being printed. > But because you don't modify it it always appears as > an empty list. > > -- > 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 guest0x013 at gmail.com Thu Jun 14 06:37:02 2018 From: guest0x013 at gmail.com (Freedom Peacemaker) Date: Thu, 14 Jun 2018 12:37:02 +0200 Subject: [Tutor] accessing buttons (tkinter) made with loop Message-ID: Hello Tutor, currently im working with tkinter application. Main idea was to create 25 buttons with for loop. Each button text was random number and user needs to click them in order form lowest to highest. When button is clicked its being disabled with coresponding color (green-ok, red-not). This is my code: http://pastebin.pl/view/1ac9ad13 I was looking for help in Google but now i cant figure out, how to move forward. I am stuck. Buttons with numbers are created, and all buttons (key) with their numbers (value) are added to buttons dictionary. So objects are created with coresponding values. And now is my problem. I tried with use() function /it gives me only errors painting few buttons to green, one not painted and rest is red/ but i have no idea how i could access configuration of those buttons. I think im close but still can see. Can You guide me? Best Regards Pi From alan.gauld at yahoo.co.uk Thu Jun 14 06:56:49 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 14 Jun 2018 11:56:49 +0100 Subject: [Tutor] How default arg of function works In-Reply-To: References: Message-ID: On 14/06/18 08:40, Deepak Dixit wrote: > You mean that for default args and passed args of mutable type, python uses > different object and same reference will be used for further calling of the > function. Yes, the default argument object is created when the function is defined (ie before it is even called the first time) and the same reference to that obje3ct is always used for the default argument. When you provide the argument yourself Python uses whatever object you pass. > Now one more thing I want to ask you that how can I get deep understanding > of python like how list, dictionary works internally and other topics , is > it only possible after more and more practice. - You can observe the behaviour as you have been doing. - You can use the disassembly module (dis) to look at the object code generated by the compiler. - And, the ultimate truth, you can read the C source code if you know C. And of course you can ask questions here. We have several contributers who understand Python internals quite well. -- 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 deepakdixit0001 at gmail.com Thu Jun 14 07:01:24 2018 From: deepakdixit0001 at gmail.com (Deepak Dixit) Date: Thu, 14 Jun 2018 16:31:24 +0530 Subject: [Tutor] How default arg of function works In-Reply-To: References: Message-ID: Thanks a lot for this information. On Thu, Jun 14, 2018, 4:28 PM Alan Gauld via Tutor wrote: > On 14/06/18 08:40, Deepak Dixit wrote: > > You mean that for default args and passed args of mutable type, python > uses > > different object and same reference will be used for further calling of > the > > function. > > Yes, the default argument object is created when the > function is defined (ie before it is even called the > first time) and the same reference to that obje3ct is > always used for the default argument. > > When you provide the argument yourself Python uses > whatever object you pass. > > > Now one more thing I want to ask you that how can I get deep > understanding > > of python like how list, dictionary works internally and other topics , > is > > it only possible after more and more practice. > > - You can observe the behaviour as you have been doing. > - You can use the disassembly module (dis) to look at > the object code generated by the compiler. > - And, the ultimate truth, you can read the C source code > if you know C. > > And of course you can ask questions here. We have several > contributers who understand Python internals quite well. > > -- > 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 Thu Jun 14 09:51:08 2018 From: __peter__ at web.de (Peter Otten) Date: Thu, 14 Jun 2018 15:51:08 +0200 Subject: [Tutor] accessing buttons (tkinter) made with loop References: Message-ID: Freedom Peacemaker wrote: > Hello Tutor, > currently im working with tkinter application. Main idea was to create 25 > buttons with for loop. Each button text was random number and user needs > to click them in order form lowest to highest. When button is clicked its > being disabled with coresponding color (green-ok, red-not). This is my > code: > > http://pastebin.pl/view/1ac9ad13 That's small enough to incorporate into your message: > from tkinter import * > import random > > def use(): > if buttons[guzik] == min(buttons.values()): > guzik.config(state = 'disabled', bg='green') > #buttons.pop(guzik) # error > else: > guzik.config(state = 'disabled', bg='red') If you make that the buttons' command 'guzik' will always refer to the last button because that's what the global guzik variable is bound to when the for loop below ends. One way to set guzik to a specific button is called "closure" (other options are default arguments and callable classes): def make_use(guzik): def use(): if buttons[guzik] == min(buttons.values()): guzik.config(state = 'disabled', bg='green') else: guzik.config(state = 'disabled', bg='red') del buttons[guzik] return use Now the local 'guzik' variable inside make_use() is not changed when the make_use function ends, and for every call of make_use() the inner use() function sees the specific value from the enclosing scope. > num = [x for x in range(1000)] > buttons = {} > > root = Tk() > > for i in range(25): > guzik = Button(root, > text = random.sample(num,1), height = 1, width = 7) > guzik.grid(row = int(i/5), column = i%5) > buttons[guzik] = int(guzik.cget('text')) guzik["command"] = make_use(guzik) > > print(buttons) > > root.mainloop() > > > I was looking for help in Google but now i cant figure out, how to move > forward. I am stuck. > > Buttons with numbers are created, and all buttons (key) with their numbers > (value) are added to buttons dictionary. So objects are created with > coresponding values. And now is my problem. I tried with use() function > /it gives me only errors painting few buttons to green, one not painted > and rest is red/ but i have no idea how i could access configuration of > those buttons. I think im close but still can see. Can You guide me? > > Best Regards > Pi From mats at wichmann.us Thu Jun 14 11:51:51 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 14 Jun 2018 09:51:51 -0600 Subject: [Tutor] Virtual environment can't find uno In-Reply-To: References: Message-ID: <0f1c1cbc-bb52-0b3a-319e-f9f2d3c72fc6@wichmann.us> On 06/13/2018 06:55 PM, Jim wrote: > Running Linux Mint 18. > > I have python 3.6 running in a virtual environment. > > I want to use a package called oosheet to work with libreoffice calc. > When I try to import it I get the following error: > >>>> import oosheet > Traceback (most recent call last): > ? File "", line 1, in > ? File > "/home/jfb/EVs/env36/lib/python3.6/site-packages/oosheet/__init__.py", > line 38, in > ??? import uno, re, zipfile, types, inspect, tempfile, shutil, subprocess > ModuleNotFoundError: No module named 'uno' > > If I do the same thing in the system installed python3 it imports and > runs with no errors. > > python3-uno was installed using apt-get. > > How do I get python 3.6 in the virtual environment to find uno? You should be able to pip install uno in the virtualenv, which might be best. After all, once you're using a virtualenv, you've already started down the road of picking depends from upstream, so why not :) From mats at wichmann.us Thu Jun 14 12:09:44 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 14 Jun 2018 10:09:44 -0600 Subject: [Tutor] How default arg of function works In-Reply-To: References: Message-ID: <126d5a4e-d032-9b7a-10d6-39c151425068@wichmann.us> On 06/14/2018 05:01 AM, Deepak Dixit wrote: > Thanks a lot for this information. > > On Thu, Jun 14, 2018, 4:28 PM Alan Gauld via Tutor wrote: >> Yes, the default argument object is created when the >> function is defined (ie before it is even called the >> first time) and the same reference to that obje3ct is >> always used for the default argument. this turns out to be one of the "surprises" in Python that catches quite a few people. once you know how things work under the covers, it makes sense why it is so, but it's a little unusual on the surface. def (to define a function) is actually an executable statement that is run when encountered. The result is a function object, bound to the name you def'd. One of the long-time Python luminaries wrote about this ages ago, I just went and hunted up the link in case you're interested (not that lots of other people haven't written about it, but Fredrik Lundh's comments are usually worth a read, and he's got some advice for you if you're seeking to get less surprising behavior (that is, don't use an empty list as the default, instead use a placeholder like None that you can check for): http://www.effbot.org/zone/default-values.htm From jf_byrnes at comcast.net Thu Jun 14 13:57:55 2018 From: jf_byrnes at comcast.net (Jim) Date: Thu, 14 Jun 2018 12:57:55 -0500 Subject: [Tutor] Virtual environment can't find uno In-Reply-To: <0f1c1cbc-bb52-0b3a-319e-f9f2d3c72fc6@wichmann.us> References: <0f1c1cbc-bb52-0b3a-319e-f9f2d3c72fc6@wichmann.us> Message-ID: On 06/14/2018 10:51 AM, Mats Wichmann wrote: > On 06/13/2018 06:55 PM, Jim wrote: >> Running Linux Mint 18. >> >> I have python 3.6 running in a virtual environment. >> >> I want to use a package called oosheet to work with libreoffice calc. >> When I try to import it I get the following error: >> >>>>> import oosheet >> Traceback (most recent call last): >> ? File "", line 1, in >> ? File >> "/home/jfb/EVs/env36/lib/python3.6/site-packages/oosheet/__init__.py", >> line 38, in >> ??? import uno, re, zipfile, types, inspect, tempfile, shutil, subprocess >> ModuleNotFoundError: No module named 'uno' >> >> If I do the same thing in the system installed python3 it imports and >> runs with no errors. >> >> python3-uno was installed using apt-get. >> >> How do I get python 3.6 in the virtual environment to find uno? > > You should be able to pip install uno in the virtualenv, which might be > best. After all, once you're using a virtualenv, you've already started > down the road of picking depends from upstream, so why not :) > > Is it available for a pip install? I looked on pypi and didn't see it. It may be incompatible with 3.6. I was looking at the dependencies with synaptic and found. Depends Python3(>= 3.5~), Depends Python3(<= 3.6). Anyway I had forgotten I have a virtual environment with 3.5 in it, so I tried that and it works. Thanks, Jim From mats at wichmann.us Thu Jun 14 14:03:22 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 14 Jun 2018 12:03:22 -0600 Subject: [Tutor] Virtual environment can't find uno In-Reply-To: References: <0f1c1cbc-bb52-0b3a-319e-f9f2d3c72fc6@wichmann.us> Message-ID: On 06/14/2018 11:57 AM, Jim wrote: > Is it available for a pip install? I looked on pypi and didn't see it. > > It may be incompatible with 3.6. I was looking at the dependencies with > synaptic and found. Depends Python3(>= 3.5~), Depends Python3(<= 3.6). > > Anyway I had forgotten I have a virtual environment with 3.5 in it, so I > tried that and it works. > > Thanks,? Jim hmmm, the github repository the rather minimal PyPi page points to is https://github.com/elbow-jason/Uno-deprecated that doesn't give one a warm fuzzy feeling :) don't know the story here, maybe someone else does. From dbosah at buffalo.edu Thu Jun 14 14:32:46 2018 From: dbosah at buffalo.edu (Daniel Bosah) Date: Thu, 14 Jun 2018 14:32:46 -0400 Subject: [Tutor] Recursion depth exceeded in python web crawler Message-ID: I am trying to modify code from a web crawler to scrape for keywords from certain websites. However, Im trying to run the web crawler before I modify it, and I'm running into issues. When I ran this code - *import threading* *from Queue import Queue* *from spider import Spider* *from domain import get_domain_name* *from general import file_to_set* *PROJECT_NAME = "SPIDER"* *HOME_PAGE = "https://www.cracked.com/ "* *DOMAIN_NAME = get_domain_name(HOME_PAGE)* *QUEUE_FILE = '/home/me/research/queue.txt'* *CRAWLED_FILE = '/home/me/research/crawled.txt'* *NUMBER_OF_THREADS = 1* *#Captialize variables and make them class variables to make them const variables* *threadqueue = Queue()* *Spider(PROJECT_NAME,HOME_PAGE,DOMAIN_NAME)* *def crawl():* * change = file_to_set(QUEUE_FILE)* * if len(change) > 0:* * print str(len(change)) + 'links in the queue'* * create_jobs()* *def create_jobs():* * for link in file_to_set(QUEUE_FILE):* * threadqueue.put(link) #.put = put item into the queue* * threadqueue.join()* * crawl()* *def create_spiders():* * for _ in range(NUMBER_OF_THREADS): #_ basically if you dont want to act on the iterable* * vari = threading.Thread(target = work)* * vari.daemon = True #makes sure that it dies when main exits* * vari.start()* *#def regex():* * #for i in files_to_set(CRAWLED_FILE):* * #reg(i,LISTS) #MAKE FUNCTION FOR REGEX# i is url's, LISTs is list or set of keywords* *def work():* * while True:* * url = threadqueue.get()# pops item off queue* * Spider.crawl_pages(threading.current_thread().name,url)* * threadqueue.task_done()* *create_spiders()* *crawl()* That used this class: *from HTMLParser import HTMLParser* *from urlparse import urlparse* *class LinkFinder(HTMLParser):* * def _init_(self, base_url,page_url):* * super()._init_()* * self.base_url= base_url* * self.page_url = page_url* * self.links = set() #stores the links* * def error(self,message):* * pass* * def handle_starttag(self,tag,attrs):* * if tag == 'a': # means a link* * for (attribute,value) in attrs:* * if attribute == 'href': #href relative url i.e not having www* * url = urlparse.urljoin(self.base_url,value)* * self.links.add(url)* * def return_links(self):* * return self.links()* And this spider class: *from urllib import urlopen #connects to webpages from python* *from link_finder import LinkFinder* *from general import directory, text_maker, file_to_set, conversion_to_set* *class Spider():* * project_name = 'Reader'* * base_url = ''* * Queue_file = ''* * crawled_file = ''* * queue = set()* * crawled = set()* * def __init__(self,project_name, base_url,domain_name):* * Spider.project_name = project_name* * Spider.base_url = base_url* * Spider.domain_name = domain_name* * Spider.Queue_file = '/home/me/research/queue.txt'* * Spider.crawled_file = '/home/me/research/crawled.txt'* * self.boot()* * self.crawl_pages('Spider 1 ', base_url)* * @staticmethod * * def boot():* * directory(Spider.project_name)* * text_maker(Spider.project_name,Spider.base_url)* * Spider.queue = file_to_set(Spider.Queue_file)* * Spider.crawled = file_to_set(Spider.crawled_file)* * @staticmethod * * def crawl_pages(thread_name, page_url):* * if page_url not in Spider.crawled:* * print thread_name + 'crawling ' + page_url* * print 'queue' + str(len(Spider.queue)) + '|crawled' + str(len(Spider.crawled))* * Spider.add_links_to_queue(Spider.gather_links(page_url))* * Spider.crawled.add(page_url)* * Spider.update_files()* * @staticmethod* * def gather_links(page_url):* * html_string = ''* * try:* * response = urlopen(page_url)* * if 'text/html' in response.getheader('Content Type'):* * read = response.read()* * html_string = read.decode('utf-8')* * finder = LinkFinder(Spider.base_url,page_url)* * finder.feed(html_string)* * except:* * print 'Error: cannot crawl page'* * return set()* * return finder.return_links()* * @staticmethod* * def add_links_to_queue(links):* * for i in links:* * if i in Spider.queue:* * continue* * if i in Spider.crawled:* * continue* * # if Spider.domain_name != get_domain_name(url):* * # continue* * Spider.queue.add()* * @staticmethod* * def update_files():* * conversion_to_set(Spider.queue,Spider.Queue_file)* * conversion_to_set(Spider.crawled,Spider.crawled_file*) and these functions: *from urlparse import urlparse* *#get subdomain name (name.example.com )* *def subdomain_name(url):* * try:* * return urlparse(url).netloc* * except:* * return ''* *def get_domain_name(url):* * try:* * variable = subdomain_name.split(',')* * return variable[-2] + ',' + variable[-1] #returns 2nd to last and last instances of variable* * except:* * return '''* (there are more functions, but those are housekeeping functions) The interpreter returned this error: *RuntimeError: maximum recursion depth exceeded while calling a Python object* After calling crawl() and create_jobs() a bunch of times? How can I resolve this? Thanks From tmrsg11 at gmail.com Thu Jun 14 12:31:44 2018 From: tmrsg11 at gmail.com (C W) Date: Thu, 14 Jun 2018 12:31:44 -0400 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? Message-ID: Hello everyone, I'm working on matplotlib, could someone explain the difference between these two? Axes class: https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes Axes and tick API: https://matplotlib.org/api/axis_api.html I began at reading axes class, but discovered axes API by accident. Why are there two documentations on the same thing? Why not merge? I mean, one is already confusing enough. ;) Axes is already branching off of plt,plot(), and now there is a branch within axes. I didn't dare to look, but may even be sub-branches? Thank you! From steve at pearwood.info Thu Jun 14 21:39:47 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 15 Jun 2018 11:39:47 +1000 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: Message-ID: <20180615013947.GO12683@ando.pearwood.info> On Thu, Jun 14, 2018 at 12:31:44PM -0400, C W wrote: > Hello everyone, > > I'm working on matplotlib, could someone explain the difference between > these two? > > Axes class: https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes > Axes and tick API: https://matplotlib.org/api/axis_api.html > > I began at reading axes class, but discovered axes API by accident. Why are > there two documentations on the same thing? Why not merge? I mean, one is > already confusing enough. ;) *shrug* Because the designers of matplotlib suck at designing a good, easy to use, easy to understand, simple API? Because they're not good at writing documentation? I dunno. It is hard to write an API which is both *obvious* and *powerful*, but the few times I tried using matplotlib I found it baroque and confusing. -- Steve From steve at pearwood.info Thu Jun 14 21:36:00 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 15 Jun 2018 11:36:00 +1000 Subject: [Tutor] Recursion depth exceeded in python web crawler In-Reply-To: References: Message-ID: <20180615013600.GN12683@ando.pearwood.info> On Thu, Jun 14, 2018 at 02:32:46PM -0400, Daniel Bosah wrote: > I am trying to modify code from a web crawler to scrape for keywords from > certain websites. However, Im trying to run the web crawler before I > modify it, and I'm running into issues. > > When I ran this code - [snip enormous code-dump] > The interpreter returned this error: > > *RuntimeError: maximum recursion depth exceeded while calling a Python > object* Since this is not your code, you should report it as a bug to the maintainers of the web crawler software. They wrote it, and it sounds like it is buggy. Quoting the final error message on its own is typically useless, because we have no context as to where it came from. We don't know and cannot guess what object was called. Without that information, we're blind and cannot do more than guess or offer the screamingly obvious advice "find and fix the recursion error". When an error does occur, Python provides you with a lot of useful information about the context of the error: the traceback. As a general rule, you should ALWAYS quote the entire traceback, starting from the line beginning "Traceback: ..." not just the final error message. Unfortunately, in the case of RecursionError, that information can be a firehose of hundreds of identical lines, which is less useful than it sounds. The most recent versions of Python redacts that and shows something similar to this: Traceback (most recent call last): File "", line 1, in File "", line 2, in f [ previous line repeats 998 times ] RecursionError: maximum recursion depth exceeded but in older versions you should manually cut out the enormous flood of lines (sorry). If the lines are NOT identical, then don't delete them! The bottom line is, without some context, it is difficult for us to tell where the bug is. Another point: whatever you are using to post your messages (Gmail?) is annoyingly adding asterisks to the start and end of each line. I see your quoted code like this: [direct quote] *import threading* *from Queue import Queue* *from spider import Spider* *from domain import get_domain_name* *from general import file_to_set* Notice the * at the start and end of each line? That makes the code invalid Python. You should check how you are posting to the list, and if you have "Rich Text" or some other formatting turned on, turn it off. (My guess is that you posted the code in BOLD or perhaps some colour other than black, and your email program "helpfully" added asterisks to it to make it stand out.) Unfortunately modern email programs, especially web-based ones like Gmail and Outlook.com, make it *really difficult* for technical forums like this. They are so intent on making email "pretty" (generally pretty ugly) for regular users, they punish technically minded users who need to focus on the text not the presentation. -- Steve From breamoreboy at gmail.com Thu Jun 14 22:01:05 2018 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 15 Jun 2018 03:01:05 +0100 Subject: [Tutor] Recursion depth exceeded in python web crawler In-Reply-To: References: Message-ID: On 14/06/18 19:32, Daniel Bosah wrote: > I am trying to modify code from a web crawler to scrape for keywords from > certain websites. However, Im trying to run the web crawler before I > modify it, and I'm running into issues. > > When I ran this code - > > > > > *import threading* > *from Queue import Queue* > *from spider import Spider* > *from domain import get_domain_name* > *from general import file_to_set* > > > *PROJECT_NAME = "SPIDER"* > *HOME_PAGE = "https://www.cracked.com/ "* > *DOMAIN_NAME = get_domain_name(HOME_PAGE)* > *QUEUE_FILE = '/home/me/research/queue.txt'* > *CRAWLED_FILE = '/home/me/research/crawled.txt'* > *NUMBER_OF_THREADS = 1* > *#Captialize variables and make them class variables to make them const > variables* > > *threadqueue = Queue()* > > *Spider(PROJECT_NAME,HOME_PAGE,DOMAIN_NAME)* > > *def crawl():* > * change = file_to_set(QUEUE_FILE)* > * if len(change) > 0:* > * print str(len(change)) + 'links in the queue'* > * create_jobs()* > > *def create_jobs():* > * for link in file_to_set(QUEUE_FILE):* > * threadqueue.put(link) #.put = put item into the queue* > * threadqueue.join()* > * crawl()* > *def create_spiders():* > * for _ in range(NUMBER_OF_THREADS): #_ basically if you dont want to > act on the iterable* > * vari = threading.Thread(target = work)* > * vari.daemon = True #makes sure that it dies when main exits* > * vari.start()* > > *#def regex():* > * #for i in files_to_set(CRAWLED_FILE):* > * #reg(i,LISTS) #MAKE FUNCTION FOR REGEX# i is url's, LISTs is > list or set of keywords* > *def work():* > * while True:* > * url = threadqueue.get()# pops item off queue* > * Spider.crawl_pages(threading.current_thread().name,url)* > * threadqueue.task_done()* > > *create_spiders()* > > *crawl()* > > > That used this class: > > *from HTMLParser import HTMLParser* > *from urlparse import urlparse* > > *class LinkFinder(HTMLParser):* > * def _init_(self, base_url,page_url):* > * super()._init_()* > * self.base_url= base_url* > * self.page_url = page_url* > * self.links = set() #stores the links* > * def error(self,message):* > * pass* > * def handle_starttag(self,tag,attrs):* > * if tag == 'a': # means a link* > * for (attribute,value) in attrs:* > * if attribute == 'href': #href relative url i.e not > having www* > * url = urlparse.urljoin(self.base_url,value)* > * self.links.add(url)* > * def return_links(self):* > * return self.links()* It's very unpythonic to define getters like return_links, just access self.links directly. > > > And this spider class: > > > > *from urllib import urlopen #connects to webpages from python* > *from link_finder import LinkFinder* > *from general import directory, text_maker, file_to_set, conversion_to_set* > > > *class Spider():* > * project_name = 'Reader'* > * base_url = ''* > * Queue_file = ''* > * crawled_file = ''* > * queue = set()* > * crawled = set()* > > > * def __init__(self,project_name, base_url,domain_name):* > * Spider.project_name = project_name* > * Spider.base_url = base_url* > * Spider.domain_name = domain_name* > * Spider.Queue_file = '/home/me/research/queue.txt'* > * Spider.crawled_file = '/home/me/research/crawled.txt'* > * self.boot()* > * self.crawl_pages('Spider 1 ', base_url)* It strikes me as completely pointless to define this class when every variable is at the class level and every method is defined as a static method. Python isn't Java :) [code snipped] > > and these functions: > > > > *from urlparse import urlparse* > > *#get subdomain name (name.example.com )* > > *def subdomain_name(url):* > * try:* > * return urlparse(url).netloc* > * except:* > * return ''* It's very bad practice to use a bare except like this as it hides any errors and prevents you from using CTRL-C to break out of your code. > > *def get_domain_name(url):* > * try:* > * variable = subdomain_name.split(',')* > * return variable[-2] + ',' + variable[-1] #returns 2nd to last and > last instances of variable* > * except:* > * return '''* The above line is a syntax error. > > > (there are more functions, but those are housekeeping functions) > > > The interpreter returned this error: > > *RuntimeError: maximum recursion depth exceeded while calling a Python > object* > > > After calling crawl() and create_jobs() a bunch of times? > > How can I resolve this? > > Thanks Just a quick glance but crawl calls create_jobs which calls crawl... -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From kzamani44 at gmail.com Fri Jun 15 09:57:47 2018 From: kzamani44 at gmail.com (kenneth yashim) Date: Fri, 15 Jun 2018 16:57:47 +0300 Subject: [Tutor] SLICING Message-ID: please im new to python or any other programming language. please i want to understand how slicing works [start,stop,step] >>> 'abc'[0:3] 'abc' >>>'abc'[0:-1] 'ab' why does 'abc'[2:1] or 'abc'[2:1] print ' ' instead of 'c'??? From tmrsg11 at gmail.com Fri Jun 15 12:35:36 2018 From: tmrsg11 at gmail.com (C W) Date: Fri, 15 Jun 2018 12:35:36 -0400 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: <20180615013947.GO12683@ando.pearwood.info> References: <20180615013947.GO12683@ando.pearwood.info> Message-ID: I read through some documentations yesterday after posting. I will share what I think. There are three parts to matplotlib: backend, artist, and scripting. The artist layer is the only one that really matters for matplotlib users. Why haven't the developers fixed the problem? Considering Python's growing popularity, hasn't anyone brought this up before me? Was there a plan? Thanks! On Thu, Jun 14, 2018 at 9:39 PM, Steven D'Aprano wrote: > On Thu, Jun 14, 2018 at 12:31:44PM -0400, C W wrote: > > Hello everyone, > > > > I'm working on matplotlib, could someone explain the difference between > > these two? > > > > Axes class: https://matplotlib.org/api/axes_api.html#matplotlib.axes. > Axes > > Axes and tick API: https://matplotlib.org/api/axis_api.html > > > > I began at reading axes class, but discovered axes API by accident. Why > are > > there two documentations on the same thing? Why not merge? I mean, one is > > already confusing enough. ;) > > *shrug* > > Because the designers of matplotlib suck at designing a good, easy to > use, easy to understand, simple API? Because they're not good at writing > documentation? I dunno. > > It is hard to write an API which is both *obvious* and *powerful*, but > the few times I tried using matplotlib I found it baroque and confusing. > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mats at wichmann.us Fri Jun 15 14:32:03 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 15 Jun 2018 12:32:03 -0600 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: <20180615013947.GO12683@ando.pearwood.info> Message-ID: <0F655DD7-A7AA-4F09-97C5-E59787741332@wichmann.us> many systems are constrained by history. matplotlib started when the vile (yes, thats just a personal opinion) matlab proved too cumbersome and someone decided to try again in python... but knowing matlab, he picked up ideas from there. since then all kinds of stuff has been bolted on. it's powerful but awkward and not very modern and has a steep learning curve. there are other options if you don't like it. try a search engine: you are certainly not the first complainer On June 15, 2018 10:35:36 AM MDT, C W wrote: >I read through some documentations yesterday after posting. > >I will share what I think. There are three parts to matplotlib: >backend, >artist, and scripting. The artist layer is the only one that really >matters >for matplotlib users. > >Why haven't the developers fixed the problem? Considering Python's >growing >popularity, hasn't anyone brought this up before me? Was there a plan? > >Thanks! > > > > > >On Thu, Jun 14, 2018 at 9:39 PM, Steven D'Aprano >wrote: > >> On Thu, Jun 14, 2018 at 12:31:44PM -0400, C W wrote: >> > Hello everyone, >> > >> > I'm working on matplotlib, could someone explain the difference >between >> > these two? >> > >> > Axes class: >https://matplotlib.org/api/axes_api.html#matplotlib.axes. >> Axes >> > Axes and tick API: https://matplotlib.org/api/axis_api.html >> > >> > I began at reading axes class, but discovered axes API by accident. >Why >> are >> > there two documentations on the same thing? Why not merge? I mean, >one is >> > already confusing enough. ;) >> >> *shrug* >> >> Because the designers of matplotlib suck at designing a good, easy to >> use, easy to understand, simple API? Because they're not good at >writing >> documentation? I dunno. >> >> It is hard to write an API which is both *obvious* and *powerful*, >but >> the few times I tried using matplotlib I found it baroque and >confusing. >> >> >> >> -- >> Steve >> _______________________________________________ >> 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 -- Sent from my Android device with K-9 Mail. Please excuse my brevity. From alan.gauld at yahoo.co.uk Fri Jun 15 14:44:40 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 15 Jun 2018 19:44:40 +0100 Subject: [Tutor] SLICING In-Reply-To: References: Message-ID: On 15/06/18 14:57, kenneth yashim wrote: > please im new to python or any other programming language. please i want to > understand how slicing works > > [start,stop,step] > >>>> 'abc'[0:3] > 'abc' > >>>> 'abc'[0:-1] > 'ab' > > why does 'abc'[2:1] or 'abc'[2:1] print ' ' instead of 'c'??? Because stop is lower than start and the step is implicitly +1, so the slice doesn't include anything. 'abc'[2:] will yield 'c' because it implicitly specifies stop as the end of the string. 'abc'[2:1:-1] will also yield 'c' because the -1 step value reverses the direction. 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 alan.gauld at yahoo.co.uk Fri Jun 15 14:48:10 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 15 Jun 2018 19:48:10 +0100 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: <20180615013947.GO12683@ando.pearwood.info> Message-ID: On 15/06/18 17:35, C W wrote: > Why haven't the developers fixed the problem? This is open source, developed by volunteers to meet their own needs primarily and others by happy coincidence. If the existing solution meets the needs of the developers they have no incentive to spend time to "fix" it. You(*) may want it fixed and if you submit a patch they may well incorporate it. That's how open source works. (*)You or anyone else who is sufficiently irritated or motivated to spend their time fixing 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 steve at pearwood.info Fri Jun 15 20:26:39 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 16 Jun 2018 10:26:39 +1000 Subject: [Tutor] SLICING In-Reply-To: References: Message-ID: <20180616002639.GA14437@ando.pearwood.info> On Fri, Jun 15, 2018 at 04:57:47PM +0300, kenneth yashim wrote: > please im new to python or any other programming language. please i want to > understand how slicing works > > [start,stop,step] > > >>> 'abc'[0:3] > 'abc' > > >>>'abc'[0:-1] > 'ab' > > why does 'abc'[2:1] or 'abc'[2:1] print ' ' instead of 'c'??? They don't return a space. They return an empty string, because the starting point comes AFTER the ending point. The slice from 2 to 1 means: - move to the right; - start at position number two - stop when you get to, or pass, position number one Since you have already passed position number one, the slice stops collecting immediately, and you have an empty slice. Slices cut the string at positions *between* the characters. If you have the string s="abcdef", imagine marking the gaps between the characters like this: |a|b|c|d|e|f| and numbering the gaps 0, 1, 2, 3, 4, 5, 6. So the slice s[2:5] cuts at the gaps numbered 2 and 5: |a|b||c|d|e||f| giving you 'cde' as the returned slice. If the ending position is to the left of the starting position, an empty slice is returned. -- Steve From steve at pearwood.info Fri Jun 15 20:33:54 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 16 Jun 2018 10:33:54 +1000 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: <20180615013947.GO12683@ando.pearwood.info> Message-ID: <20180616003354.GB14437@ando.pearwood.info> On Fri, Jun 15, 2018 at 12:35:36PM -0400, C W wrote: > Why haven't the developers fixed the problem? matplotlib is free, open source software. If volunteers don't "fix the problem", who is paying for the work to be done? Are you volunteering? Or willing to pay somebody to do the work? $30,000 - $50,000 would probably pay for one developer to work on mathplotlib full time for three months. Or maybe the developers don't think it is a problem that needs fixing. Maybe they're happy with it the way it is. Or they don't like it any more than you do, but they are constrained by the need to keep backwards compatibility. -- Steve From tmrsg11 at gmail.com Sat Jun 16 00:49:49 2018 From: tmrsg11 at gmail.com (Mike C) Date: Sat, 16 Jun 2018 04:49:49 +0000 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: <20180616003354.GB14437@ando.pearwood.info> References: <20180615013947.GO12683@ando.pearwood.info> , <20180616003354.GB14437@ando.pearwood.info> Message-ID: I can only compare to the R language I've used. If there is an issue, say a function freezes at startup, one user brings it up to the list, when the respective maintainer sees the bug, it is usually addressed on the next release. In terms of funding. Isn't Python heavily used in industry, so, financial contribution should've been huge, no? ________________________________ From: Tutor on behalf of Steven D'Aprano Sent: Friday, June 15, 2018 8:33:54 PM To: tutor at python.org Subject: Re: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? On Fri, Jun 15, 2018 at 12:35:36PM -0400, C W wrote: > Why haven't the developers fixed the problem? matplotlib is free, open source software. If volunteers don't "fix the problem", who is paying for the work to be done? Are you volunteering? Or willing to pay somebody to do the work? $30,000 - $50,000 would probably pay for one developer to work on mathplotlib full time for three months. Or maybe the developers don't think it is a problem that needs fixing. Maybe they're happy with it the way it is. Or they don't like it any more than you do, but they are constrained by the need to keep backwards compatibility. -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mats at wichmann.us Sat Jun 16 11:52:47 2018 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 16 Jun 2018 09:52:47 -0600 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: <20180615013947.GO12683@ando.pearwood.info> <20180616003354.GB14437@ando.pearwood.info> Message-ID: <0d684d85-7bde-791e-93d0-12e51dc2791c@wichmann.us> On 06/15/2018 10:49 PM, Mike C wrote: > I can only compare to the R language I've used. If there is an issue, say a function freezes at startup, one user brings it up to the list, when the respective maintainer sees the bug, it is usually addressed on the next release. Sure. So that suggests you ought to bring this up to the matplotlib community, and see if they are similarly responsive. We here are not connected to it, we only seek to give general advice to extent we are able to. > In terms of funding. Isn't Python heavily used in industry, so, financial contribution should've been huge, no? There's not really a connection... matplotlib is written in Python, yes, but as a discrete project is only going to attract interest from those who have a use for it, not from the broader "everybody who uses Python for something" community. As would be the case for any independent open source project, irrespective of what programming language it is written in. And that is indeed one of the differences between open source and commercial development... if there's not a company behind it, the nature of responses is likely to be different. Which is not to say commercial companies prioritize things the way you'd want either. I have recently proposed to one company that shall remain unnamed because this is only an example, not any attempt to cast blame, an improvement they could make to their project through a system they provide to propose features, and to vote on features others have proposed. That feature now has 20 votes and I was excited a lot of people agreed with me. I just heard through independent channels that that company's developers don't even look at suggestions until the votes number in the thousands. Guess it's not happening... From steve at pearwood.info Sat Jun 16 19:08:27 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 17 Jun 2018 09:08:27 +1000 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: <20180615013947.GO12683@ando.pearwood.info> Message-ID: <20180616230827.GH14437@ando.pearwood.info> On Sat, Jun 16, 2018 at 04:49:49AM +0000, Mike C wrote: > I can only compare to the R language I've used. If there is an issue, > say a function freezes at startup, one user brings it up to the list, > when the respective maintainer sees the bug, it is usually addressed > on the next release. You don't think that there might be a *slight* difference between these two situations? "Hi, maintainers of a highly popular language, here is an easily reproducable and clear bug in your language. Please fix it." versus: "Hi, people on a low-volume mailing list for beginners, there's a third-party library written by people completely unaffiliated with either you or the core developers of the language, but in my subjective opinion, its documentation sucks. Why isn't it fixed?" > In terms of funding. Isn't Python heavily used in industry, so, > financial contribution should've been huge, no? You might be surprised: - how not huge they are; - how much work it is to convince industry to pay for software they can get for free; - and how many other expenses there are, e.g. paying for admin staff, legal costs, hosting costs, funding community groups, etc. I'm not saying the Python Software Foundation is crying poor, but neither do they have unlimited piles of cash they can just throw around at random. Having said that, if a project like matplotlib went to them with a concrete proposal requiring funding, not just some nebulous "give us a bunch of cash and we'll make the docs more betterer!" it would probably be treated with all due consideration. -- Steve From alan.gauld at yahoo.co.uk Sat Jun 16 19:13:25 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 17 Jun 2018 00:13:25 +0100 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: <20180615013947.GO12683@ando.pearwood.info> <20180616003354.GB14437@ando.pearwood.info> Message-ID: On 16/06/18 05:49, Mike C wrote: > I can only compare to the R language I've used. If there is an issue, > say a function freezes at startup, one user brings it up to the list,> when the respective maintainer sees the bug, it is usually addressed Which is fine if there is a team working onthe project full time - as there would be on a commercial project - perhaps by sponsorship. But many(most?) open source projects are not sponsored., they are a small (often just one or two) individuals working in their spare time. > In terms of funding. Isn't Python heavily used in industry, Yes and several companies sponsor development of the core python language. As such major issues tend to be addressed rapidly. But... matplotlib is not part of that core language. It is a part of ScyPy which is not used by such large numbers of industrial companies (and is more directly of interest to researchers and academics rather than commercial developers - a group best known for lack of funds!) and as such is less likely to be responsive, especially when the issues are not bugs or functionality affecting - they are just usability irritations. -- 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 tmrsg11 at gmail.com Sat Jun 16 13:31:05 2018 From: tmrsg11 at gmail.com (C W) Date: Sat, 16 Jun 2018 13:31:05 -0400 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: <0d684d85-7bde-791e-93d0-12e51dc2791c@wichmann.us> References: <20180615013947.GO12683@ando.pearwood.info> <20180616003354.GB14437@ando.pearwood.info> <0d684d85-7bde-791e-93d0-12e51dc2791c@wichmann.us> Message-ID: Somehow I missed your first post, Mats. I certainly agree with you earlier point. After searching, I see there is a Matplotlib list. I will direct questions to there, hopefully that will bring some attention. Thanks! On Sat, Jun 16, 2018 at 11:52 AM, Mats Wichmann wrote: > On 06/15/2018 10:49 PM, Mike C wrote: > > I can only compare to the R language I've used. If there is an issue, > say a function freezes at startup, one user brings it up to the list, when > the respective maintainer sees the bug, it is usually addressed on the next > release. > > > Sure. So that suggests you ought to bring this up to the matplotlib > community, and see if they are similarly responsive. We here are not > connected to it, we only seek to give general advice to extent we are > able to. > > > > In terms of funding. Isn't Python heavily used in industry, so, > financial contribution should've been huge, no? > > > There's not really a connection... matplotlib is written in Python, yes, > but as a discrete project is only going to attract interest from those > who have a use for it, not from the broader "everybody who uses Python > for something" community. As would be the case for any independent open > source project, irrespective of what programming language it is written > in. And that is indeed one of the differences between open source and > commercial development... if there's not a company behind it, the nature > of responses is likely to be different. Which is not to say commercial > companies prioritize things the way you'd want either. I have recently > proposed to one company that shall remain unnamed because this is only > an example, not any attempt to cast blame, an improvement they could > make to their project through a system they provide to propose features, > and to vote on features others have proposed. That feature now has 20 > votes and I was excited a lot of people agreed with me. I just heard > through independent channels that that company's developers don't even > look at suggestions until the votes number in the thousands. Guess it's > not happening... > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From tmrsg11 at gmail.com Sat Jun 16 22:20:38 2018 From: tmrsg11 at gmail.com (C W) Date: Sat, 16 Jun 2018 22:20:38 -0400 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: <20180615013947.GO12683@ando.pearwood.info> <20180616003354.GB14437@ando.pearwood.info> Message-ID: I have found the matplotlib list. Cheers! On Sat, Jun 16, 2018 at 7:13 PM, Alan Gauld via Tutor wrote: > On 16/06/18 05:49, Mike C wrote: > > I can only compare to the R language I've used. If there is an issue, > > say a function freezes at startup, one user brings it up to the list,> > when the respective maintainer sees the bug, it is usually addressed > > Which is fine if there is a team working onthe project full time > - as there would be on a commercial project - perhaps by sponsorship. > But many(most?) open source projects are not sponsored., they are > a small (often just one or two) individuals working in their spare > time. > > > In terms of funding. Isn't Python heavily used in industry, > > Yes and several companies sponsor development of the core > python language. As such major issues tend to be addressed rapidly. > But... matplotlib is not part of that core language. > > It is a part of ScyPy which is not used by such large > numbers of industrial companies (and is more directly > of interest to researchers and academics rather than > commercial developers - a group best known for lack of > funds!) and as such is less likely to be responsive, > especially when the issues are not bugs or functionality > affecting - they are just usability irritations. > > > -- > 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 adeadmarshal at gmail.com Sun Jun 17 18:59:19 2018 From: adeadmarshal at gmail.com (Ali M) Date: Mon, 18 Jun 2018 03:29:19 +0430 Subject: [Tutor] I get an error while search in my Entry field Message-ID: Hi, i get an error while searching my Entry field and i don't understand what that means. code: #! /usr/bin/env python3 #GeologyDict by Ali M import sqlite3 as sqlite import tkinter as tk from tkinter import Text from tkinter import Entry from tkinter import Scrollbar from tkinter import ttk #GUI Widgets class EsperantoDict: def __init__(self, master): master.title("EsperantoDict") master.resizable(False, False) master.configure(background='#EAFFCD') self.search_var = tk.StringVar() self.search_var.trace("w", lambda name, index, mode: self.update_list()) self.style = ttk.Style() self.style.configure("TFrame", background='#EAFFCD') self.style.configure("TButton", background='#EAFFCD') self.style.configure("TLabel", background='#EAFFCD') self.frame_header = ttk.Frame(master, relief=tk.FLAT) self.frame_header.pack(side=tk.TOP, padx=5, pady=5) self.logo = tk.PhotoImage(file=r'C:\EsperantoDict\eo.png') self.small_logo = self.logo.subsample(10, 10) ttk.Label(self.frame_header, image=self.small_logo).grid(row=0, column=0, stick="ne", padx=5, pady=5, rowspan=2) ttk.Label(self.frame_header, text='EsperantoDict', font=('Arial', 18, 'bold')).grid(row=0, column=1) self.frame_content = ttk.Frame(master) self.frame_content.pack() self.entry_search = ttk.Entry(self.frame_content, textvariable=self.search_var) self.entry_search.grid(row=0, column=0) self.entry_search.bind('', self.entry_delete) self.button_search = ttk.Button(self.frame_content, text="Search") self.aks = tk.PhotoImage(file=r'C:\EsperantoDict\search.png') self.small_aks = self.aks.subsample(3, 3) self.button_search.config(image=self.small_aks, compound=tk.LEFT) self.button_search.grid(row=0, column=1, columnspan=2) self.listbox = tk.Listbox(self.frame_content, height=28) self.listbox.grid(row=1, column=0) self.scrollbar = ttk.Scrollbar(self.frame_content, orient=tk.VERTICAL, command=self.listbox.yview) self.scrollbar.grid(row=1, column=1, sticky='ns') self.listbox.config(yscrollcommand=self.scrollbar.set) self.listbox.bind('<>', self.enter_meaning) self.textbox = tk.Text(self.frame_content, width=60, height=27) self.textbox.grid(row=1, column=2) # SQLite self.db = sqlite.connect(r'C:\EsperantoDict\test.db') self.cur = self.db.cursor() self.cur.execute('SELECT Esperanto FROM Words') for row in self.cur: self.listbox.insert(tk.END, row) # SQLite def enter_meaning(self, tag): if self.listbox.curselection(): results = self.cur.execute("SELECT English FROM Words WHERE rowID = 1") for row in results: self.textbox.insert(tk.END, row) def update_list(self): search_term = self.search_var.get() self.listbox.delete(0, tk.END) for item in self.listbox: if search_term.lower() in item.lower(): self.listbox.insert(tk.END, item) def entry_delete(self, tag): self.entry_search.delete(0, tk.END) return None def main(): root = tk.Tk() esperantodict = EsperantoDict(root) root.mainloop() if __name__ == '__main__': main() #db tbl name: Words ##db first field name: Esperanto ##db second field name: English error: Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\deadmarshal\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1702, in __call__ return self.func(*args) File "C:/EsperantoDict/EsperantoDict.py", line 21, in self.search_var.trace("w", lambda name, index, mode: self.update_list()) File "C:/EsperantoDict/EsperantoDict.py", line 77, in update_list for item in self.listbox: File "C:\Users\deadmarshal\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1486, in cget return self.tk.call(self._w, 'cget', '-' + key) TypeError: must be str, not int From tmrsg11 at gmail.com Sun Jun 17 14:02:07 2018 From: tmrsg11 at gmail.com (C W) Date: Sun, 17 Jun 2018 14:02:07 -0400 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? Message-ID: Dear Python experts, I never figured out when to call a method with parenthesis, when is it not? It seems inconsistent. For example, If I do > data.isnull() numberair_pressure_9amair_temp_9amavg_wind_direction_9amavg_wind_speed_9am max_wind_direction_9ammax_wind_speed_9amrain_accumulation_9am rain_duration_9amrelative_humidity_9amrelative_humidity_3pm 0 False False False False False False False False False False False 1 False False False False False False False False False False False 2 False False False False False False False False False False False Or I can do without parenthesis, > data.isnull data.columns Index(['number', 'air_pressure_9am', 'air_temp_9am', 'avg_wind_direction_9am', 'avg_wind_speed_9am', 'max_wind_direction_9am', 'max_wind_speed_9am', 'rain_accumulation_9am', 'rain_duration_9am', 'relative_humidity_9am', 'relative_humidity_3pm'], dtype='object') # with parenthesis throws an error > data.columns() ---------------------------------------------------------------------------TypeError Traceback (most recent call last) in ()----> 1 data.columns() TypeError: 'Index' object is not callable I always thought columns(), isnull() are methods to objects, so, you would need to pass parameters even when it's empty. But, apparently I am wrong on this. What is going on? Thank you very much! From alan.gauld at yahoo.co.uk Mon Jun 18 04:24:17 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 18 Jun 2018 09:24:17 +0100 Subject: [Tutor] I get an error while search in my Entry field In-Reply-To: References: Message-ID: On 17/06/18 23:59, Ali M wrote: > def update_list(self): > search_term = self.search_var.get() > self.listbox.delete(0, tk.END) > for item in self.listbox: The above pair of lines look odd. You delete everything on the listbox then try to iterate over it? Is that really what you want? Also are you sure you can iterate over a listbox widget? I was not aware of that capability and can see nothing about iterators in the listbox help screen... I usually use the get() method to fetch the contents before trying to iterate over them: for item in myList.get(0,tk.END): # use item -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Mon Jun 18 04:33:37 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 18 Jun 2018 09:33:37 +0100 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? In-Reply-To: References: Message-ID: On 17/06/18 19:02, C W wrote: > I never figured out when to call a method with parenthesis, when is it not? You call anything in Python (function, method, class, etc) using parentheses. The parentheses are what makes it a call. When you don't use parentheses you are referencing the callable object. >> data.isnull() That calls the isnull method of data >> data.isnull That references the isnull method, so you could store it in a variable for future use: checkNull = data.isnull # do other stuff. if checkNull(): # call data.isnull() via the variable # do more stuff > air_temp_9am avg_wind_direction_9am \ > 0 0 918.060000 74.822000 271.100000 But I've no idea why you get this output... > Obviously, the second case does not make any senses. The fact that its a method makes sense, the "data" makes no sense. > But, in data.columns, > it is only correct to do without parenthesis as below: > >> data.columns > > Index(['number', 'air_pressure_9am', 'air_temp_9am', 'avg_wind_direction_9am', So data.columns is not a method but an attribute. The attribute apparently references an Index object. > # with parenthesis throws an error >> data.columns() We've already established that data.columns is a reference to an Index object and you can't call an Index object, it is not "callable" Traceback (most recent call > last) in ()----> 1 > data.columns() > TypeError: 'Index' object is not callable Which is what Python is telling you. > I always thought columns(), isnull() are methods to objects, isnull is apparenmtly a method. columns is apparenmtly an attribute. They are different things and you need to read the documentation (or code) to see which is which. You can call a method. You can only call an attribute if it stores a callable object. And if you use a method name without parens you get a reference to the method back. -- 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 Jun 18 04:46:26 2018 From: __peter__ at web.de (Peter Otten) Date: Mon, 18 Jun 2018 10:46:26 +0200 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? References: Message-ID: C W wrote: > Dear Python experts, > > I never figured out when to call a method with parenthesis, when is it > not? It seems inconsistent. > > For example, > If I do > >> data.isnull() > > numberair_pressure_9amair_temp_9amavg_wind_direction_9amavg_wind_speed_9am > max_wind_direction_9ammax_wind_speed_9amrain_accumulation_9am > rain_duration_9amrelative_humidity_9amrelative_humidity_3pm > 0 False False False False False False False False False False False > 1 False False False False False False False False False False False > 2 False False False False False False False False False False False > > Or I can do without parenthesis, >> data.isnull > > air_temp_9am avg_wind_direction_9am \ > 0 0 918.060000 74.822000 271.100000 > 1 1 917.347688 71.403843 101.935179 > 2 2 923.040000 60.638000 51.000000 > > > Obviously, the second case does not make any senses. Let's try with our own class: >>> class A: ... def foo(self): return "bar" ... >>> a = A() >>> a.foo() 'bar' With parens the foo method is executed and returns the expected result whose repr() is then printed by the interactive interpreter. >>> a.foo > Without parens the repr() of the method is printed just like the repr of any other value would be. That repr() has the form and in your case REPR_OF_INSTANCE happens to be a lengthy dump of the DataFrame. To verify our findings let's define a custom __repr__ for out A: >>> class A: ... def foo(self): return "bar" ... def __repr__(self): return "yadda " * 10 ... >>> A().foo From __peter__ at web.de Mon Jun 18 05:22:21 2018 From: __peter__ at web.de (Peter Otten) Date: Mon, 18 Jun 2018 11:22:21 +0200 Subject: [Tutor] I get an error while search in my Entry field References: Message-ID: Alan Gauld via Tutor wrote: > On 17/06/18 23:59, Ali M wrote: > >> def update_list(self): >> search_term = self.search_var.get() >> self.listbox.delete(0, tk.END) >> for item in self.listbox: > > The above pair of lines look odd. > You delete everything on the listbox then try to iterate > over it? Is that really what you want? More likely the words have to be reread from the database. > Also are you sure you can iterate over a listbox widget? > I was not aware of that capability and can see nothing > about iterators in the listbox help screen... You cannot iterate over Listbox widgets, it's just that for every object with a __getitem__() method Python will try and feed it consecutive integers >>> class A: ... def __getitem__(self, index): ... if index > 3: raise IndexError ... return index * index ... >>> list(A()) [0, 1, 4, 9] a feature that probably predates the iterator protocol. However, the tkinter widgets expect option names like "background", or "borderstyle", and respond with the somewhat cryptic TypeError when the 0 is passed instead. > I usually use the get() method to fetch the contents > before trying to iterate over them: > > for item in myList.get(0,tk.END): > # use item > From steve at pearwood.info Mon Jun 18 06:12:36 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 18 Jun 2018 20:12:36 +1000 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? In-Reply-To: References: Message-ID: <20180618101236.GN14437@ando.pearwood.info> On Sun, Jun 17, 2018 at 02:02:07PM -0400, C W wrote: > Dear Python experts, > > I never figured out when to call a method with parenthesis, when is it not? > It seems inconsistent. You *always* CALL a method (or function) with parentheses. But sometimes you can grab hold of a method (or function) *without* calling it, by referring to its name without parentheses. The trick is to understand that in Python, functions and methods are what they call "First-class citizens" or first-class values, just like numbers, strings, lists etc. https://en.wikipedia.org/wiki/First-class_citizen Let's take a simple example: def add_one(x=0): return x + 1 To CALL the function, it always needs parentheses (even if it doesn't require an argument): py> add_one(10) 11 py> add_one() 1 But to merely refer to the function by name, you don't use parentheses. Then you can treat the function as any other value, and print it, assign it to a variable, pass it to a different function, or stick it in a list. Anything you can do with anything else, really. For example: py> myfunction = add_one # assign to a variable py> print(myfunction) py> L = [1, "a", add_one, None] # put it inside a list py> print(L) [1, 'a', , None] If it isn't clear to you why anyone would want to do this in the first place, don't worry too much about it, it is a moderately advanced technique. But it is essential for such things as: - function introspection; - callback functions used in GUI programming; - functional programming; etc. For example, you may have seen the map() function. Here it is in action: py> list(map(add_one, [1, 10, 100, 1000])) [2, 11, 101, 1001] Can you work out what it does? If not, try opening the interactive interpreter, and enter: help(map) and see if that helps. Or ask here :-) -- Steve From steve at pearwood.info Mon Jun 18 06:22:55 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 18 Jun 2018 20:22:55 +1000 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? In-Reply-To: References: Message-ID: <20180618102254.GO14437@ando.pearwood.info> Another thought comes to mind... On Sun, Jun 17, 2018 at 02:02:07PM -0400, C W wrote: > Obviously, the second case does not make any senses. But, in data.columns, > it is only correct to do without parenthesis as below: > > > data.columns [...] > # with parenthesis throws an error > > data.columns() That's because data.columns isn't a method. Since it isn't a method, it is just a regular attribute (or possibly a read-only attribute) that you can retrieve the contents, but not call. Some languages call these "instance variables". I don't like that term, but in this case the analogy with variables is good. Consider two named objects, one which is a function, one which is not: py> len py> len("hello world") # try calling len 11 py> length = 99 py> length() # try calling length Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable The reason for this should be fairly obvious, but I trust you can see that *in general* there is no fool-proof way of knowing which names refer to callable functions and which refer to non-callable values. You have to know the context of the code, read the documentation or manual, or just try it and see what happens. The same can apply to methods and other attributes: class MyClass: length = 99 def len(self, object): # Re-invent the wheel, badly. count = 0 for item in object: count += 1 return count And now to see the same behaviour as for ordinary variables: py> instance = MyClass() py> instance.length 99 py> instance.length() Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable py> print(instance.len) > py> instance.len("hello world") 11 Does this help you understand what you are seeing with the data.columns example? -- Steve From mats at wichmann.us Mon Jun 18 10:50:24 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 18 Jun 2018 08:50:24 -0600 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? In-Reply-To: References: Message-ID: On 06/17/2018 12:02 PM, C W wrote: > Dear Python experts, > > I never figured out when to call a method with parenthesis, when is it not? > It seems inconsistent. > > For example, > If I do > >> data.isnull() > > numberair_pressure_9amair_temp_9amavg_wind_direction_9amavg_wind_speed_9am > max_wind_direction_9ammax_wind_speed_9amrain_accumulation_9am > rain_duration_9amrelative_humidity_9amrelative_humidity_3pm > 0 False False False False False False False False False False False > 1 False False False False False False False False False False False > 2 False False False False False False False False False False False > > Or I can do without parenthesis, >> data.isnull > > air_temp_9am avg_wind_direction_9am \ > 0 0 918.060000 74.822000 271.100000 > 1 1 917.347688 71.403843 101.935179 > 2 2 923.040000 60.638000 51.000000 > > > Obviously, the second case does not make any senses. But, in data.columns, > it is only correct to do without parenthesis as below: > >> data.columns > > Index(['number', 'air_pressure_9am', 'air_temp_9am', 'avg_wind_direction_9am', > 'avg_wind_speed_9am', 'max_wind_direction_9am', 'max_wind_speed_9am', > 'rain_accumulation_9am', 'rain_duration_9am', 'relative_humidity_9am', > 'relative_humidity_3pm'], > dtype='object') > > > # with parenthesis throws an error >> data.columns() I think you've already got all you need, let me just try one more brief summary, in case it helps - Python is not like certain other languages. 1. a def statement is a runnable statement that creates an object (class statements are also runnable and also create an object) 2. you can only call objects that are callable (you have already seen the error you get if you try) 3. you call by using the () syntax 4. you can refer to an object by a name bound to it without calling it. IF you do so in a context that implies you are "printing" it, Python will invoke a special method (called __repr__) to attempt to create a human-readable representation of the object. I only mention that because what it prints out may not be what you expect - but you should know that what is printing is a transformation of object -> helpful string. Try some experiments with this to get comfortable. From rls4jc at gmail.com Mon Jun 18 18:12:11 2018 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Mon, 18 Jun 2018 15:12:11 -0700 Subject: [Tutor] Recursion Message-ID: My foggy understanding of recursion is probably the reason I can't figure this out. When turtle draws this program there is an orange line in the green which I would prefer not to have. I've tried all I could think of, but can't get the orange line to go away, or maybe more accurately, not to be drawn. The program goes to the end of the recursion and then starts drawing? which seems wrong, because the trunk of the tree is drawn first. Maybe: How does it know to draw the second orange line? and how does it know when to stop with only two branches from the trunk? Thank you as always. ----------------- import turtle def tree(branchLen, width, t): if branchLen > 5: t.pensize(width) t.forward(branchLen) t.right(20) tree(branchLen-15, width-5, t) t.left(40) tree(branchLen-15, width-5, t) t.pencolor("orange") t.right(20) t.backward(branchLen) t.pencolor("green") def main(): t = turtle.Turtle() myWin = turtle.Screen() t.speed(0) t.left(90) t.up() t.backward(100) t.down() t.color("green") tree(75, 20, t) myWin.exitonclick() main() -- ? Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, ? ? Responsibility, ? ? Learner, Ideation From alan.gauld at yahoo.co.uk Mon Jun 18 20:13:12 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 19 Jun 2018 01:13:12 +0100 Subject: [Tutor] Recursion In-Reply-To: References: Message-ID: On 18/06/18 23:12, Roger Lea Scherer wrote: > My foggy understanding of recursion is probably the reason I can't figure > this out. When turtle draws this program there is an orange line in the > green which I would prefer not to have. I've tried all I could think of, > but can't get the orange line to go away, or maybe more accurately, not to > be drawn. Remove the line in tree() that sets the color to orange? > The program goes to the end of the recursion and then starts drawing? which > seems wrong, because the trunk of the tree is drawn first. You call the recursing function before and after drawing so it draws on the last call and on the way back up to the first call, like this: tree draw tree draw tree draw tree draw draw draw draw > it know to draw the second orange line? I'm not sure what you mean by second but you draw orange in every call to tree. > and how does it know when to stop > with only two branches from the trunk? Again I'm not sure what's happening because I'm not running the code. > def tree(branchLen, width, t): > if branchLen > 5: > t.pensize(width) > t.forward(branchLen) This should draw in green > t.right(20) > tree(branchLen-15, width-5, t) this draws in green and orange > t.left(40) > tree(branchLen-15, width-5, t) this draws green and orange > t.pencolor("orange") > t.right(20) > t.backward(branchLen) this draws orange > t.pencolor("green") 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 steve at pearwood.info Mon Jun 18 20:26:41 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 19 Jun 2018 10:26:41 +1000 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? In-Reply-To: References: Message-ID: <20180619002641.GQ14437@ando.pearwood.info> On Mon, Jun 18, 2018 at 08:50:24AM -0600, Mats Wichmann wrote: > Python is not like certain other languages. But it is like *other* certain other languages. :-) Python's execution model is different from C, Pascal or Fortran, but it is quite similar to Ruby, Lua and Javascript. -- Steve From hgfernan at gmail.com Tue Jun 19 06:46:16 2018 From: hgfernan at gmail.com (Hilton Fernandes) Date: Tue, 19 Jun 2018 07:46:16 -0300 Subject: [Tutor] In matplotlib, why are there axes classes vs. axes API? Why not list them under one documentation? In-Reply-To: References: <20180615013947.GO12683@ando.pearwood.info> <20180616003354.GB14437@ando.pearwood.info> Message-ID: Dear all, while MatPlotLib can have a myriad of options -- and that can be really confusing --, there is sample code for almost everything in their site. So, I had a great time playing with it. And from what I remember from visiting their site, there are two versions of many of the resources: one deriived from the Matlab API for plotting -- hence the name --, and one more Pythonic. So, I believe that the good people developing MatPlotLib is already taking care of the clumsiness of the original form of the library, and rephrasing it in good programming style. All the best, Hilton On Sat, Jun 16, 2018 at 11:20 PM, C W wrote: > I have found the matplotlib list. > > Cheers! > > On Sat, Jun 16, 2018 at 7:13 PM, Alan Gauld via Tutor > wrote: > > > On 16/06/18 05:49, Mike C wrote: > > > I can only compare to the R language I've used. If there is an issue, > > > say a function freezes at startup, one user brings it up to the list,> > > when the respective maintainer sees the bug, it is usually addressed > > > > Which is fine if there is a team working onthe project full time > > - as there would be on a commercial project - perhaps by sponsorship. > > But many(most?) open source projects are not sponsored., they are > > a small (often just one or two) individuals working in their spare > > time. > > > > > In terms of funding. Isn't Python heavily used in industry, > > > > Yes and several companies sponsor development of the core > > python language. As such major issues tend to be addressed rapidly. > > But... matplotlib is not part of that core language. > > > > It is a part of ScyPy which is not used by such large > > numbers of industrial companies (and is more directly > > of interest to researchers and academics rather than > > commercial developers - a group best known for lack of > > funds!) and as such is less likely to be responsive, > > especially when the issues are not bugs or functionality > > affecting - they are just usability irritations. > > > > > > -- > > 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 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Tue Jun 19 12:18:34 2018 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 20 Jun 2018 02:18:34 +1000 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? In-Reply-To: References: <20180619002641.GQ14437@ando.pearwood.info> Message-ID: <20180619161833.GW14437@ando.pearwood.info> On Tue, Jun 19, 2018 at 11:52:08AM -0400, C W wrote: > Thank you all. I'm relatively new to OOP, I think that's where the problem > is. It's different from C or any C alike language. Apart from C-like languages like C++, Objective C, C# and others with objects :-) Actually in this specific instance, what seems to be tripping you up is not the *object oriented* flavour of the code, but the *functional programming* flavour of the code, specifically that functions are values at all. (Whether they are "objects" or not is not very important.) https://en.wikipedia.org/wiki/Functional_programming In this case, it isn't *much* functional programming. It is just the idea that functions are values, just like strings and lists and floats. -- Steve From tmrsg11 at gmail.com Tue Jun 19 11:52:08 2018 From: tmrsg11 at gmail.com (C W) Date: Tue, 19 Jun 2018 11:52:08 -0400 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? In-Reply-To: <20180619002641.GQ14437@ando.pearwood.info> References: <20180619002641.GQ14437@ando.pearwood.info> Message-ID: Thank you all. I'm relatively new to OOP, I think that's where the problem is. It's different from C or any C alike language. I'm still figuring out what's under the hood with OOP. Thanks! On Mon, Jun 18, 2018 at 8:26 PM, Steven D'Aprano wrote: > On Mon, Jun 18, 2018 at 08:50:24AM -0600, Mats Wichmann wrote: > > > Python is not like certain other languages. > > But it is like *other* certain other languages. > > :-) > > Python's execution model is different from C, Pascal or Fortran, but it > is quite similar to Ruby, Lua and Javascript. > > > > -- > Steve > _______________________________________________ > 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 Jun 19 13:16:18 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 19 Jun 2018 18:16:18 +0100 Subject: [Tutor] What's the difference between calling a method with parenthesis vs. without it? In-Reply-To: References: <20180619002641.GQ14437@ando.pearwood.info> Message-ID: On 19/06/18 16:52, C W wrote: > Thank you all. I'm relatively new to OOP, I think that's where the problem > is. It's different from C or any C alike language. True, but even in C you can have pointers to functions which work in a similar fashion. > I'm still figuring out what's under the hood with OOP. This particular feature has nothing to do with OOP, it happens at the procedural level too. Consider: >>> aVar = 42 >>> aVar 42 >>> aVar() Traceback (most recent call last): File "", line 1, in aVar() TypeError: 'int' object is not callable >>> def aFunc(): return 66 >>> aFunc() 66 >>> aFunc >>> So using parens to access a variable yields an error,. Using parens on a function is fine Not using parens on a variable returns the value Not using parens on a function returns the function And finally: >>> anotherVar = aFunc >>> anotherVar >>> anotherVar() 66 >>> We make a variable reference a function. Now the variable acts just like the function it references. That's because variables in Python are just names that reference a value and the variable "acts" like whatever kind of value it references. But it has nothing to do with OOP. It is much more fundamental than that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dbosah at buffalo.edu Wed Jun 20 15:32:56 2018 From: dbosah at buffalo.edu (Daniel Bosah) Date: Wed, 20 Jun 2018 15:32:56 -0400 Subject: [Tutor] Parsing and collecting keywords from a webpage Message-ID: # coding: latin-1 from bs4 import BeautifulSoup from urllib.request import urlopen import re #new point to add... make rest of function then compare a list of monuments notaries ( such as blvd, road, street, etc.) to a list of words containing them. if contained, pass into new set ( ref notes in case) def regex(url): html = urlopen(url).read() soup = BeautifulSoup(html,"lxml") # why does lmxl fix it? sets = [] john = [u'Julia Alvarez', u'Arambilet', u'Frank Baez',u'Josefina Baez' u'Rei Berroa',u'Manuel del Cabral', u'Junot D?az', u'Luis Arambilet' u'Manuel del Cabral', u'Manuel del Cabral' u'A?da Cartagena Portalat?n', u'Roberto Cass?', 'Raquel Cepeda',u'Tulio Manuel Cestero',u'Hilma Contreras', u'Angie Cruz', u'Judith Dupr?',u'Virginia Elena Ortea',u'Le?n F?lix Batista', u'Arturo F?liz-Camilo',u'Fabio Fiallo',u'Freddy Ginebra',u'Cristino G?mez',u'Jos? Luis Gonz?lez',u'Pedro Henr?quez Ure?a',u'Federico Henr?quez y Carvajal',u'Angela Hern?ndez Nu?ez',u'Juan Isidro Jim?nez Grull?n',u'Rita Indiana',u'Mariano Lebr?n Savi??n',u'Marcio Veloz Maggiolo',u'Andr?s L. Mateo', u'F?lix Evaristo Mej?a',u'Miguel D. Mena',u'Leopoldo Minaya', u'Juan Duarte',u'Rafael Alburquerque',u'Pedro Franco Bad?a ',u'Buenaventura B?ez M?ndez',u'Joaqu?n Balaguer Ricardo',u'Ram?n Emeterio Betances',u'Salvador Jorge Blanco',u'Tom?s Bobadilla',u'Juan Bosch y Gavi?o',u'Francisco Alberto Caama?o De??',u'Fernando Cabrera',u'Ram?n C?ceres',u'Margarita Cede?o de Fern?ndez',u'David Collado', u'Lorraine Cort?s-V?zquez',u'Adriano Espaillat',u'Juan Pablo Duarte',u'Rafael Espinal',u'Rafael Estrella Ure?a',u'Carlos Felipe Morales', u'Leonel Fern?ndez Reyna',u'Pedro Florentino',u'Maximiliano G?mez',u'M?ximo G?mez',u'Petronila Ang?lica G?mez',u'Antonio Guzm?n Fern?ndez',u'Ulises Heureaux',u'Antonio Imbert Barrera',u'Gregorio Luper?n',u'Miguel Martinez',u'Danilo Medina',u'Hip?lito Mej?a',u'Ram?n Mat?as Mella',u'Patria Mirabal',u'Minerva Mirabal',u'Mar?a Teresa Mirabal',u'Adolfo Alejandro Nouel',u'Jos? Nu?ez-Melo',u'Jos? Francisco Pe?a G?mez', u'Joseline Pe?a-Melnyk',u'Cesar A. Perales',u'Thomas Perez',u'Donald Reid Cabral',u'Ydanis Rodr?guez',u'Jos? Antonio (Pepillo) Salcedo',u'Pepillo',u'Roberto Salcedo, Sr.',u'Juan S?nchez Ram?rez',u'Francisco del Rosario S?nchez',u'Jos? Santana', u'Pedro Santana Familias',u'Jos? Del Castillo Savi??n',u'Angel Taveras', u'Rafael Le?nidas Trujillo',u'Ramfis Trujillo',u'Francisco Urena',u'Fernando Valerio', u'Elias Wessin y Wessin blvd'] jake = [u'Pedro Mir',u'Domingo Moreno Jimenes',u'Mateo Morrison',u'Jos? N??ez de C?ceres',u'Arturo Rodr?guez Fern?ndez',u'Mu-Kien Adriana Sang',u'Rosa Silverio',u'Alfredo Fern?ndez Sim?',u'Salom? Ure?a',u'Jael Uribe',u'Bernardo Vega',u'Julio Vega Batlle',u'Alanna Lockward',u'Delia Weber', u'blvd'] #llist of words , only set paul = jake + john new_list = [x.encode('latin-1') for x in sorted(paul)] search = "(" + b"|".join(new_list).decode() + ")" + "" #re.complie needs string as first argument, so adds string to be first argument, and joins the strings together with john # print (type(search)) pattern = re.compile(search)#compiles search to be a regex object reg = pattern.findall(str(soup))#calls findall on pattern, which findall returns all non-overllapping matches of the pattern in string, returning a list of strings for i in reg: if i in reg and paul: # this loop checks to see if elements are in both the regexed parsed list and the list. If i is in both, it is added to list. sets.append(str(i)) with open('sets.txt', 'w') as f: f.write(str(sets)) f.close() def regexparse(regex): monum = [u'road', u'blvd',u'street', u'town', u'city',u'Bernardo Vega'] setss = [] f = open('sets.txt', 'rt') f = list(f) for i in f: if i in f and i in monum: setss.append(i) #with open ('regex.txt','w') as q: #q.write(str(setss)) # q.close() print (setss) if __name__ == '__main__': regexparse(regex(' https://en.wikipedia.org/wiki/List_of_people_from_the_Dominican_Republic')) What this code is doing is basically going through a webpage using BeautifulSoup and regex to compare a regexed list of words ( in regex ) to a list of keywords and then writing them to a textfile. The next function (regexparse) then goes and has a empty list (setss), then reads the textfile from the previous function. What I want to do, in a for loop, is check to see if words in monum and the textfile ( from the regex function ) are shared, and if so , those shared words get added to the empty list(setss) , then written to a file ( this code is going to be added to a web crawler, and is basically going to be adding words and phrases to a txtfile as it crawls through the internet. ). However, every time I run the current code, I get all the textfile(sets.txt) from the previous ( regex ) function, even though all I want are words and pharse shared between the textfile from regex and the monum list from regexparse. How can I fix this? From glennmschultz at me.com Wed Jun 20 16:04:23 2018 From: glennmschultz at me.com (Glenn Schultz) Date: Wed, 20 Jun 2018 20:04:23 +0000 (GMT) Subject: [Tutor] adding numpy to pandas Message-ID: <369ce654-856e-4bbd-a8cb-737d93cca278@me.com> All, I have a pandas dataframe and a predict result (numpy array) of a classifier [[0,1],[1,0]]. ?What I would like to do is as the positive to the pandas dataframe. ?I use predict[:,1] to slice the postive from numpy which gives me a row of the result. ?but I cannot concat to the pandas df['result'] = predict[:,1] does not work and I have tried various ways to do this with no result. ?I am missing something here. ?Any help is appreciated. Glenn From alan.gauld at yahoo.co.uk Wed Jun 20 19:16:41 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 21 Jun 2018 00:16:41 +0100 Subject: [Tutor] Parsing and collecting keywords from a webpage In-Reply-To: References: Message-ID: On 20/06/18 20:32, Daniel Bosah wrote: > # coding: latin-1 > from bs4 import BeautifulSoup > from urllib.request import urlopen > import re > > #new point to add... make rest of function then compare a list of monuments > notaries ( such as blvd, road, street, etc.) to a list of words containing > them. if contained, pass into new set ( ref notes in case) > > > def regex(url): > > html = urlopen(url).read() > soup = BeautifulSoup(html,"lxml") # why does lmxl fix it? Fix what? You haven't given us any clue what you are talking about. Did you have a problem? If so what? And in what way did lmxl fix it? > What this code is doing is basically going through a webpage using > BeautifulSoup and regex to compare a regexed list of words ( in regex ) to > a list of keywords and then writing them to a textfile. The next function > (regexparse) then goes and has a empty list (setss), then reads the > textfile from the previous function. What I want to do, in a for loop, is > check to see if words in monum and the textfile ( from the regex function ) > are shared, and if so , those shared words get added to the empty > list(setss) , then written to a file ( this code is going to be added to a > web crawler, and is basically going to be adding words and phrases to a > txtfile as it crawls through the internet. ). > > However, every time I run the current code, I get all the > textfile(sets.txt) from the previous ( regex ) function, even though all I > want are words and pharse shared between the textfile from regex and the > monum list from regexparse. How can I fix this? So did lmxl fix it? Since you are posting the question I assume not? Can you clarify what exactly you are asking? -- 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 Jun 21 04:55:50 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 21 Jun 2018 09:55:50 +0100 Subject: [Tutor] Parsing and collecting keywords from a webpage In-Reply-To: References: Message-ID: On 20/06/18 20:32, Daniel Bosah wrote: > reg = pattern.findall(str(soup)) > > for i in reg: > if i in reg and paul: # this loop checks to see if elements are in > both the regexed parsed list and the list. No it doesn't. It checks if i is in reg and if paul is non empty - which it always is. So this if test is really just testing if i is in reg. This is also always truie since the for loop is iterating over reg. So you are effectively saying if True and True or if True. What you really wanted was something like if i in reg and i in paul: But since you know i is in reg you can drop that bit to get if i in paul: > sets.append(str(i)) Because the if is always true you always add i to sets > with open('sets.txt', 'w') as f: > f.write(str(sets)) > f.close() Why not just wait to the end? Writing the entire sets stucture to a file each time is very wasteful. Alternatively use the append mode and just write the new item to the file. Also you don't need f.close if you use a with statement. > However, every time I run the current code, I get all the > textfile(sets.txt) from the previous ( regex ) function, even though all I > want are words and pharse shared between the textfile from regex and the > monum list from regexparse. How can I fix this? I think that's due to the incorrect if expression above. But I didn't check the rest of the code... However, I do wonder about your use of soup as your search string. Isn't soup the parsed html structure? Is that really what you want to search with your regex? But I'm no BS expert, so there might be some magic at work there. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Thu Jun 21 05:03:25 2018 From: __peter__ at web.de (Peter Otten) Date: Thu, 21 Jun 2018 11:03:25 +0200 Subject: [Tutor] Parsing and collecting keywords from a webpage References: Message-ID: Daniel Bosah wrote: > new_list = [x.encode('latin-1') for x in sorted(paul)] I don't see why you would need bytes > search = "(" + b"|".join(new_list).decode() + ")" + "" #re.complie needs when your next step is to decode it. I'm not sure why it even works as the default encoding is usually UTF-8. > u'Jos? Antonio (Pepillo) Salcedo' Those parens combined with > > search = "(" + b"|".join(new_list).decode() + ")" + "" #re.complie needs > string as first argument, so adds string to be first argument, and joins > the strings together with john > > # print (type(search)) > pattern = re.compile(search)#compiles search to be a regex object > reg = pattern.findall(str(soup))#calls findall on pattern, which findall will cause findall() to return a list of 2-tuples: >>> re.compile("(" + "|".join(["foo", "bar(baz)"]) + ")").findall("yadda foo yadda bar(baz)") [('foo', '')] Applying re.escape() can prevent that: >>> re.compile("(" + "|".join(re.escape(s) for s in ["foo", "bar(baz)"]) + ")").findall("yadda foo yadda bar(baz)") ['foo', 'bar(baz)'] > if i in reg and paul: # this loop checks to see if elements are in > both the regexed parsed list and the list. If i is in both, it is added to > list. No it doesn't, it is equivalent to if (i in reg) and bool(paul): ... or, since paul is a list if (i in reg) and len(paul) > 0: ... for non-empty lists effectively if i in reg: ... > sets.append(str(i)) > with open('sets.txt', 'w') as f: > f.write(str(sets)) This writes a single line of the form ['first', 'second item', ...] > f.close() No need to close() the file explicitly -- with open() already implies that and operates more reliably (the file will be closed even if an exception is raised in th e with-suite). > def regexparse(regex): > monum = [u'road', u'blvd',u'street', u'town', u'city',u'Bernardo > Vega'] setss = [] > > f = open('sets.txt', 'rt') > f = list(f) >From my explanation above follows that the list f contains a single string (and one that does not occur in monum) so that setss should always be empty. > for i in f: > if i in f and i in monum: > setss.append(i) > #with open ('regex.txt','w') as q: > #q.write(str(setss)) > # q.close() > print (setss) > > > if __name__ == '__main__': > regexparse(regex(' > https://en.wikipedia.org/wiki/List_of_people_from_the_Dominican_Republic')) > > > What this code is doing is basically going through a webpage using > BeautifulSoup and regex to compare a regexed list of words ( in regex ) to > a list of keywords and then writing them to a textfile. The next function > (regexparse) then goes and has a empty list (setss), then reads the > textfile from the previous function. What I want to do, in a for loop, is > check to see if words in monum and the textfile ( from the regex function > ) are shared, and if so , those shared words get added to the empty > list(setss) , then written to a file ( this code is going to be added to a > web crawler, and is basically going to be adding words and phrases to a > txtfile as it crawls through the internet. ). > > However, every time I run the current code, I get all the > textfile(sets.txt) from the previous ( regex ) function, even though all I > want are words and pharse shared between the textfile from regex and the > monum list from regexparse. How can I fix this? Don't write a complete script and then cross your fingers hoping that it will work as expected -- that rarely happens even to people with more experience; they just find their errors more quickly ;). Instead start with the first step, add print calls generously, and only continue working on the next step when you are sure that the first does exactly what you want. Once your scripts get more complex replace visual inspection via print() with a more formal approach https://docs.python.org/dev/library/unittest.html From mats at wichmann.us Thu Jun 21 11:30:37 2018 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 21 Jun 2018 09:30:37 -0600 Subject: [Tutor] adding numpy to pandas In-Reply-To: <369ce654-856e-4bbd-a8cb-737d93cca278@me.com> References: <369ce654-856e-4bbd-a8cb-737d93cca278@me.com> Message-ID: <54f56c85-c360-6c37-11ac-34e90b12ce0c@wichmann.us> On 06/20/2018 02:04 PM, Glenn Schultz wrote: > All, > > I have a pandas dataframe and a predict result (numpy array) of a > classifier [[0,1],[1,0]]. ?What I would like to do is as the positive to > the pandas dataframe. ?I use predict[:,1] to slice the postive from > numpy which gives me a row of the result. ?but I cannot concat to the > pandas df['result'] = predict[:,1] does not work and I have tried > various ways to do this with no result. ?I am missing something here. You should take a look here: https://pandas.pydata.org/community.html History has indicated that the Python tutor group isn't overloaded with Pandas experts. You may still get an answer here, but that page suggests the preferred places from the community to interact with to get good answers. There's also a Google Groups which doesn't seem to be mentioned on the page: https://groups.google.com/forum/#!forum/pydata From __peter__ at web.de Thu Jun 21 13:23:51 2018 From: __peter__ at web.de (Peter Otten) Date: Thu, 21 Jun 2018 19:23:51 +0200 Subject: [Tutor] adding numpy to pandas References: <369ce654-856e-4bbd-a8cb-737d93cca278@me.com> <54f56c85-c360-6c37-11ac-34e90b12ce0c@wichmann.us> Message-ID: Mats Wichmann wrote: > On 06/20/2018 02:04 PM, Glenn Schultz wrote: >> All, >> >> I have a pandas dataframe and a predict result (numpy array) of a >> classifier [[0,1],[1,0]]. What I would like to do is as the positive to >> the pandas dataframe. I use predict[:,1] to slice the postive from >> numpy which gives me a row of the result. but I cannot concat to the >> pandas df['result'] = predict[:,1] does not work and I have tried >> various ways to do this with no result. I am missing something here. > > You should take a look here: > > https://pandas.pydata.org/community.html > > History has indicated that the Python tutor group isn't overloaded with > Pandas experts. You may still get an answer here, but that page suggests > the preferred places from the community to interact with to get good > answers. There's also a Google Groups which doesn't seem to be > mentioned on the page: > > https://groups.google.com/forum/#!forum/pydata Regardless of the chosen forum, try to be as precise as possible with your problem description. It really can't get any worse than "does not work". I tried but failed to reproduce your problem from what little information you provide: >>> a = np.array([[0,1],[1,0]]) >>> df = pd.DataFrame([[1,2], [3,4]], columns=["a", "b"]) >>> df["result"] = a[:,1] >>> df a b result 0 1 2 1 1 3 4 0 Please take the time to read http://sscce.org/ to learn how you can improve your question. You'll be rewarded with better answers from us or by the real experts elsewhere. Thank you. From jamesalundy at hotmail.com Thu Jun 21 11:35:18 2018 From: jamesalundy at hotmail.com (James Lundy) Date: Thu, 21 Jun 2018 15:35:18 +0000 Subject: [Tutor] Working example of using SSL: Message-ID: Can any one get me started with SSL by providing a working example with some documentation? God Bless: James Lundy jalundy at computer.org From mats at wichmann.us Fri Jun 22 10:53:40 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 22 Jun 2018 08:53:40 -0600 Subject: [Tutor] Working example of using SSL: In-Reply-To: References: Message-ID: <9736e2bd-94c2-99be-4fb5-76abaeadcead@wichmann.us> On 06/21/2018 09:35 AM, James Lundy wrote: > Can any one get me started with SSL by providing a working example with some documentation? The Python standard documentation has some simple examples: https://docs.python.org/3/library/ssl.html#examples But it helps if you share what you want to *do* with ssl, that might help people think about what examples would make sense. That's a roundabout way of saying this list works better if you ask specific questions: I want to do A, I tried this bit of code B, and I don't understand why I am not getting the results C that I expect, instead I get this mystifying D. From agabler at usc.edu Sat Jun 23 12:56:21 2018 From: agabler at usc.edu (Andrew Gabler) Date: Sat, 23 Jun 2018 11:56:21 -0500 Subject: [Tutor] Installing Scrapy using Pip Message-ID: Hi, I am trying to install Scrapy using pip and keep getting an error message. I have tried to fix the problem in a variety of ways, ensuring that my setuptools are installed and up-to-date and creating a virtualenv to install to. However, I keep getting the error message, and I am at a loss for what to do. Any help would be greatly appreciated. ? From alan.gauld at yahoo.co.uk Sat Jun 23 15:01:49 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 23 Jun 2018 20:01:49 +0100 Subject: [Tutor] Installing Scrapy using Pip In-Reply-To: References: Message-ID: On 23/06/18 17:56, Andrew Gabler wrote: > I am trying to install Scrapy using pip and keep getting an error message. It would help a lot if you would show us the error message. Otherwise we are just guessing. -- 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 adeadmarshal at gmail.com Sun Jun 24 13:26:12 2018 From: adeadmarshal at gmail.com (Ali M) Date: Sun, 24 Jun 2018 21:56:12 +0430 Subject: [Tutor] getting back my listbox items Message-ID: Hi, when i do search in my Entrybox the list lowers down to that typed item the code of which i wrote in the update_list function. how should i get listbox items back again after it has been lowered? and another question: if i want to format some specific text in my Text widget i should use BeautifulSoup or can i do that with add_tag method? i used add_tag method in my enter_meaning function but it formats the first 4 letters of anyword, but i want the words which are in the listofwords to be formatted. import sqlite3 as sqlite import tkinter as tk from tkinter import ttk #GUI Widgets class EsperantoDict: def __init__(self, master): master.title("EsperantoDict") master.iconbitmap("Esperanto.ico") master.resizable(False, False) master.configure(background='#EAFFCD') self.style = ttk.Style() self.search_var = tk.StringVar() self.search_var.trace("w", lambda name, index, mode: self.update_list()) self.style = ttk.Style() self.style.configure("TFrame", background='#EAFFCD') self.style.configure("TButton", background='#C6FF02') self.style.configure("TLabel", background='#EAFFCD') self.frame_header = ttk.Frame(master, relief=tk.FLAT) self.frame_header.config(style="TFrame") self.frame_header.pack(side=tk.TOP, padx=5, pady=5) self.logo = tk.PhotoImage(file=r'C:\EsperantoDict\eo.png') self.small_logo = self.logo.subsample(10, 10) ttk.Label(self.frame_header, image=self.small_logo).grid(row=0, column=0, stick="ne", padx=5, pady=5, rowspan=2) ttk.Label(self.frame_header, text='EsperantoDict', font=('Arial', 18, 'bold')).grid(row=0, column=1) self.frame_content = ttk.Frame(master) self.frame_content.config(style="TFrame") self.frame_content.pack() self.entry_search = ttk.Entry(self.frame_content, textvariable=self.search_var, width=30) self.entry_search.bind('', self.entry_delete) self.entry_search.bind('', self.entry_insert) self.entry_search.grid(row=0, column=0, padx=5) self.entry_search.focus() self.button_search = ttk.Button(self.frame_content, text="Search") self.photo_search = tk.PhotoImage(file=r'C:\EsperantoDict\search.png') self.small_photo_search = self.photo_search.subsample(3, 3) self.button_search.config(image=self.small_photo_search, compound=tk.LEFT, style="TButton") self.button_search.grid(row=0, column=2, columnspan=1, sticky='nw', padx=5) self.listbox = tk.Listbox(self.frame_content, height=30, width=30) self.listbox.grid(row=1, column=0, padx=5) self.scrollbar = ttk.Scrollbar(self.frame_content, orient=tk.VERTICAL, command=self.listbox.yview) self.scrollbar.grid(row=1, column=1, sticky='nsw') self.listbox.config(yscrollcommand=self.scrollbar.set) self.listbox.bind('<>', self.enter_meaning) self.textbox = tk.Text(self.frame_content, relief=tk.GROOVE, width=60, height=30, borderwidth=2) self.textbox.config(wrap='word') self.textbox.grid(row=1, column=2, sticky='w', padx=5) # SQLite self.db = sqlite.connect(r'C:\EsperantoDict\test.db') self.cur = self.db.cursor() self.cur.execute('SELECT Esperanto FROM Words') for row in self.cur: self.listbox.insert(tk.END, row) for row in range(0, self.listbox.size(), 2): self.listbox.itemconfigure(row, background="#f0f0ff") def update_list(self): search_term = self.search_var.get() for item in self.listbox.get(0, tk.END): if search_term.lower() in item: self.listbox.delete(0, tk.END) self.listbox.insert(tk.END, item) # SQLite def enter_meaning(self, tag): for index in self.listbox.curselection(): esperanto = self.listbox.get(index) results = self.cur.execute("SELECT English FROM Words WHERE Esperanto = ?", (esperanto)) for row in results: self.textbox.delete(1.0, tk.END) self.textbox.insert(tk.END, row) listofwords = ("gram", "med") self.textbox.tag_add('listofwords', '1.0', '1.4') self.textbox.tag_configure('listofwords', background='yellow', font='helvetica 14 bold', relief='raised') def entry_delete(self, tag): self.entry_search.delete(0, tk.END) return None def entry_insert(self, tag): self.entry_search.delete(0, tk.END) self.entry_search.insert(0, "Type to Search") return None def main(): root = tk.Tk() esperantodict = EsperantoDict(root) root.mainloop() if __name__ == '__main__': main() # db tbl name: Words # db first field name: Esperanto # db second field name: English From pmalimba at gmail.com Sun Jun 24 17:21:45 2018 From: pmalimba at gmail.com (Paula Malimba) Date: Sun, 24 Jun 2018 23:21:45 +0200 Subject: [Tutor] egg timer Message-ID: Hello, I'm new to programming and am studying the book Begin to Code with Python. I'm stuck in lesson 3, trying to make an egg timer. I keep having a syntax error. Please help!!! Thanks, Paula From alan.gauld at yahoo.co.uk Sun Jun 24 18:52:29 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 24 Jun 2018 23:52:29 +0100 Subject: [Tutor] egg timer In-Reply-To: References: Message-ID: On 24/06/18 22:21, Paula Malimba wrote: > I'm new to programming and am studying the book Begin to Code with Python. > I'm stuck in lesson 3, trying to make an egg timer. I keep having a syntax > error. Most of us won't have that book and so can't see the code. More importantly none of us can see *your* code. Please always cut n' paste your code into the emails. (please, don't use attachments because they often get stripped by the mail server as a security risk.) Also the error message, from where it says Traceback to the end. It may look like gobbledegook to you but it does actually have a lot of useful information that can help us see what went wrong. Without those two things we are pretty much unable to help, so please repost with the missing info. -- 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 Sun Jun 24 18:51:03 2018 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 24 Jun 2018 17:51:03 -0500 Subject: [Tutor] egg timer In-Reply-To: References: Message-ID: Greetings, Paula! On Sun, Jun 24, 2018 at 4:52 PM Paula Malimba wrote: > > Hello, > > I'm new to programming and am studying the book Begin to Code with Python. > I'm stuck in lesson 3, trying to make an egg timer. I keep having a syntax > error. > > Please help!!! Please pretend for a moment that you received your own email above. Read it carefully. If you were trying to help this person, "Paula", how would you answer the following questions? 1) Hmm. She is having a syntax error. This could be one of a large number of possible syntax errors, each having a different cause. Which syntax error might she be having? 2) She is doing lesson 3 from the book "Begin to Code with Python". I don't own that particular book. I wonder what that lesson is about and what specific problem(s) she is trying to solve? 3) How is she running her program? Is she running it directly from the Python terminal? From a ".py" file? 4) I wonder what version of Python she is running? Python 2.X or 3.X? This might make a difference in how I answer her. 5) And what operating system is she using? This could be a factor, too. Paula, I am *not* trying to be condescending! But your email as sent is so vague I (and everyone else) has no idea how to help you. As an example of the kind of information we need, I artificially generated a syntax error of my own. If I were asking for help I would probably say something like the following: Hi folks! I am working on an egg timer challenge from the book "Begin to Code with Python". In lesson 3 it asks me to query the user to enter how many minutes he/she wants his/her egg boiled. I wrote the following code in my Python interpreter: py3: time = input("Please tell me how many minutes you want your egg to boil?) , but I got the following error: File "", line 1 time = input("Please tell me how many minutes you want your egg to boil?) ^ SyntaxError: EOL while scanning string literal I am using Python 3.6.5 on Windows 7-64 bit. I was trying to get the amount of time in minutes the user wanted me to boil the egg in my "time" variable, but I got the above error. What am I doing wrong? Do you think you might be able to answer my question? Hope this helps us to help you, and we really do want to help! Cheers! -- boB From alan.gauld at yahoo.co.uk Sun Jun 24 18:58:59 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 24 Jun 2018 23:58:59 +0100 Subject: [Tutor] getting back my listbox items In-Reply-To: References: Message-ID: On 24/06/18 18:26, Ali M wrote: Just a quick observation, its too late to read it in detail... > def update_list(self): > search_term = self.search_var.get() > for item in self.listbox.get(0, tk.END): > if search_term.lower() in item: > self.listbox.delete(0, tk.END) > self.listbox.insert(tk.END, item) This function deletes the entire list if the search term is included in the item. It does this every time. So any items inserted at the end in earlier iterations will be deleted by later finds. Is that really what you want? > def entry_delete(self, tag): > self.entry_search.delete(0, tk.END) > return None Why do you have tag as a parameter when you don't use it? > def entry_insert(self, tag): > self.entry_search.delete(0, tk.END) > self.entry_search.insert(0, "Type to Search") > return None same here? G'night. :-) -- 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 nmadhok at g.clemson.edu Sun Jun 24 18:41:10 2018 From: nmadhok at g.clemson.edu (Nitin Madhok) Date: Sun, 24 Jun 2018 18:41:10 -0400 Subject: [Tutor] egg timer In-Reply-To: References: Message-ID: <7DD775A5-20EB-4C87-9F0F-D4BA6CA0EF3E@g.clemson.edu> Paula, Can you send the snippet of code where you?re having the syntax error? Thanks, Nitin Madhok > On Jun 24, 2018, at 5:21 PM, Paula Malimba wrote: > > Hello, > > I'm new to programming and am studying the book Begin to Code with Python. > I'm stuck in lesson 3, trying to make an egg timer. I keep having a syntax > error. > > Please help!!! > > Thanks, > Paula > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From giulia.marcoux at gmail.com Mon Jun 25 15:35:22 2018 From: giulia.marcoux at gmail.com (Giulia Marcoux) Date: Mon, 25 Jun 2018 12:35:22 -0700 Subject: [Tutor] CSV Read Message-ID: Hello, I am a student learning python, and i am running into some difficulties. I am tryign to read a csv file that has different delimiters for different rows: Example: Format:V1.1 Model: R Step Size: 10mm Distance: 10cm Gain: 1000 X,Y 1,3 2,5 6,5 5,7 For the X,Y data, I have no problem getting it into the right cells, however I cannot separate the values after the colon for the first few rows. Its either one or the other. I am using pandas to read my CSV file. So far I have: d=pd.read_csv('read.csv',delimiter=',',skiprows=0) When I replace the delimiter for ':', it no longer works, and if i put sep=',|:' it does not work either. In the end I need to plot the x,y values and i will need some of the info in the rows above like the gain to manipulate the y data. If you have any tips for me I would be very grateful! Thanks, Giulia From alan.gauld at yahoo.co.uk Mon Jun 25 19:13:05 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 26 Jun 2018 00:13:05 +0100 Subject: [Tutor] CSV Read In-Reply-To: References: Message-ID: On 25/06/18 20:35, Giulia Marcoux wrote: > Hello, > > I am a student learning python, and i am running into some difficulties. I > am tryign to read a csv file that has different delimiters for different > rows: Example: > > Format:V1.1 > Model: R > Step Size: 10mm > Distance: 10cm > Gain: 1000 Can you split your file here? That is could you just read the data from the first 5 lines (assuming its always in that kind of format) then spit the rest into a separate file (or stringbuffer?) and use CSV to analyze that? > X,Y > 1,3 > 2,5 > 6,5 > 5,7 > > For the X,Y data, I have no problem getting it into the right cells, > however I cannot separate the values after the colon for the first few > rows. Its either one or the other. A lot depends on the exact details of your file format. If the sample you've given is consistent with exactly the same header info in each (or at least some clear separating indicator - like X,Y in this case) Then its easy to split the data into two streams and process them separately. If the different formats are interlaced throughout the files things get more tricky. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Mon Jun 25 19:30:22 2018 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 25 Jun 2018 17:30:22 -0600 Subject: [Tutor] CSV Read In-Reply-To: References: Message-ID: <287f9121-f1da-509c-21e6-818a022371e6@wichmann.us> On 06/25/2018 01:35 PM, Giulia Marcoux wrote: > Hello, > > I am a student learning python, and i am running into some difficulties. I > am tryign to read a csv file that has different delimiters for different > rows: Example: > > Format:V1.1 > Model: R > Step Size: 10mm > Distance: 10cm > Gain: 1000 > > X,Y > 1,3 > 2,5 > 6,5 > 5,7 > > For the X,Y data, I have no problem getting it into the right cells, > however I cannot separate the values after the colon for the first few > rows. Its either one or the other. So you don't have a "csv file". You have a file with csv data, but also with headers. As Alan says, you want to divide it up so you've consumed the non-csv data and have only the csv data left (notice X,Y isn't part of your dataset either, even though it's in the format - it is column headers). Then it should be easy to process. From glenuk at gmail.com Fri Jun 29 11:05:46 2018 From: glenuk at gmail.com (Glen) Date: Fri, 29 Jun 2018 16:05:46 +0100 Subject: [Tutor] RAD GUI Development (like Visual Studio/VBA) Message-ID: Hey guys, Can someone advise on a RAD, drag and drop style graphical form/dialog creator? Akin to VBA or Visual Studio that will work with Python? I currently use the PyCharm IDE, which is fantastic. But I don't have the time or the skill to craft it all by hand. Thanks, From mats at wichmann.us Fri Jun 29 12:02:45 2018 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 29 Jun 2018 10:02:45 -0600 Subject: [Tutor] RAD GUI Development (like Visual Studio/VBA) In-Reply-To: References: Message-ID: <08eeba0a-64e1-d009-ac91-77b7046c184f@wichmann.us> On 06/29/2018 09:05 AM, Glen wrote: > Hey guys, > > Can someone advise on a RAD, drag and drop style graphical form/dialog > creator? Akin to VBA or Visual Studio that will work with Python? > > I currently use the PyCharm IDE, which is fantastic. But I don't have the > time or the skill to craft it all by hand. The difficulty with this question is there are so many GUI toolkits for Python, you first have to figure out which one you want to target. There's Tkinter, which is one of the batteries in the Python "batteries included" concept; however many articles and blogs which talk about what to use instead start with a statement that Tk is ugly, so I presume it must be true if so oft repeated. There is also Qt by way of PyQt (or PySide), Gtk+ (pyGObject), wxPython, PyForms, PyGui, fltk, Kivy, just to rattle off a few - I'm sure there are more. In my limited experience, I know some people were quite happy with using Qt Designer and then generating Python code from the result - there's a tool for that (pyuic?). But last I heard about doing this was years and years ago and the state of the art has probably advanced. From sergio_r at mail.com Fri Jun 29 08:18:34 2018 From: sergio_r at mail.com (Sergio Rojas) Date: Fri, 29 Jun 2018 14:18:34 +0200 Subject: [Tutor] CSV Read In-Reply-To: References: Message-ID: On 25/06/18 20:35, Giulia Marcoux wrote: > Hello, > > I am a student learning python, and i am running into some difficulties. I > am tryign to read a csv file that has different delimiters for different > rows: Example: > > Format:V1.1 > Model: R > Step Size: 10mm > Distance: 10cm > Gain: 1000 > X,Y > 1,3 > 2,5 > 6,5 > 5,7 > Hi Giulia, When dealing with unformatted data files, a first quick approach is to read in using standard python routines to read the file and then proceed via looping the lines of the file. Here is a very crude code to deal with your sample (in what follows >>>> represents indentations): # Say you have named your data file "thefile.dat" thefilename = "./thefile.dat" # Assign the filename to a variable # read the whole file with open(thefilename, 'r') as theFile: >>> contentfile = theFile.read() # print(contentfile) # peruse the file content. Find patterns to extract data # split the file in lines according to the newline character "\n" templines = [] for line in contentfile.splitlines(): >>>> templines.append(line.strip()) #Following the pattern extract the data thelines = [] for line in templines: >>>if ':' in line: >>>>>>temp = line.split(":") >>>>>>for i in range(len(temp)): >>>>>>>>try: >>>>>>>>>>>>temp[i] = float(temp[i]) >>>>>>>>except: >>>>>>>>>>>>temp[i] = temp[i] >>>>>>print(temp) >>>>>>thelines.append(temp) >>>elif ',' in line: >>>>>>temp = line.split(",") >>>>>>for i in range(len(temp)): >>>>>>>>try: >>>>>>>>>>>>temp[i] = float(temp[i]) >>>>>>>>except: >>>>>>>>>>>>>>>temp[i] = temp[i] >>>>>>print(temp) >>>>>>thelines.append(temp) >>>else: # not necessary >>>>>>>>pass # print(thelines) # check a few lines # Improve the code to deal with large files (big data stuff) # your turn ... # make a pandas data frame from the data import pandas as pd import numpy as np mypdframe = pd.DataFrame(np.array(thelines)) print(mypdframe) Hope this set your learning of python not too stressful, so you could appreciate its power. Sergio Check out the free first draft of the book: Prealgebra via Python Programming https://www.researchgate.net/publication/325473565 Companion web site: https://github.com/rojassergio/Prealgebra-via-Python-Programming From sollier.mathieu at gmail.com Fri Jun 29 05:08:01 2018 From: sollier.mathieu at gmail.com (Mathieu Sollier) Date: Fri, 29 Jun 2018 11:08:01 +0200 Subject: [Tutor] programmer raspberry Message-ID: <5b35f6f4.1c69fb81.e25d8.4816@mx.google.com> Bonjour, J?essaye de programmer un Raspberry pi 3 mod?le B+ compos? d?un ?cran tactile devant notamment allum? mon pc via cette interface. Auriez vous des conseils pour r?aliser cela?? et pour l?interface graphique?? Cordialement. Provenance?: Courrier pour Windows 10 From alan.gauld at yahoo.co.uk Fri Jun 29 18:08:16 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 29 Jun 2018 23:08:16 +0100 Subject: [Tutor] programmer raspberry In-Reply-To: <5b35f6f4.1c69fb81.e25d8.4816@mx.google.com> References: <5b35f6f4.1c69fb81.e25d8.4816@mx.google.com> Message-ID: On 29/06/18 10:08, Mathieu Sollier wrote: > Bonjour, > J?essaye de programmer un Raspberry pi 3 mod?le B+ compos? d?un ?cran tactile devant notamment allum? mon pc via cette interface. > Auriez vous des conseils pour r?aliser cela?? et pour l?interface graphique?? > Cordialement. > Provenance?: Courrier pour Windows 10 For the non-french speakers I ran that through Google translate....: ----------------------- Hello, I try to program a Raspberry pi 3 model B + composed of a touch screen in front of lit my pc via this interface. Do you have any tips for doing this? and for the graphical interface? Cordially. Provenance: Mail for Windows 10 ----------------- But it didn't help me much, maybe somebody else knows? -- 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 Jun 29 18:25:13 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 29 Jun 2018 23:25:13 +0100 Subject: [Tutor] CSV Read In-Reply-To: References: Message-ID: On 29/06/18 13:18, Sergio Rojas wrote: > # read the whole file > with open(thefilename, 'r') as theFile: >>>> contentfile = theFile.read() > > # split the file in lines according to the newline character "\n" > templines = [] > for line in contentfile.splitlines(): >>>>> templines.append(line.strip()) > > #Following the pattern extract the data > thelines = [] > for line in templines: You might as well just use readlines()... Or better still use for on the file itself: with open(thefilename, 'r') as theFile for line in theFile: .... >>>> if ':' in line: >>>>>>> temp = line.split(":") >>>>>>> for i in range(len(temp)): >>>>>>>>> try: >>>>>>>>>>>>> temp[i] = float(temp[i]) >>>>>>>>> except: >>>>>>>>>>>>> temp[i] = temp[i] def makeFloats(sep, line): temp = line/split(sep) for i,v in enumerate(temp) try: temp[i] = float(v) except ValueError,TypeError: pass return temp if ':' in line: line = makeFloats(':', line) if(',' in line: line = makeFloats(',', line) # into pandas next... Although I'm not totally sure that is what the OP wanted. What happens about the header lines for example? -- 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 Jun 29 18:34:21 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 29 Jun 2018 23:34:21 +0100 Subject: [Tutor] RAD GUI Development (like Visual Studio/VBA) In-Reply-To: References: Message-ID: On 29/06/18 16:05, Glen wrote: > Can someone advise on a RAD, drag and drop style graphical form/dialog > creator? Akin to VBA or Visual Studio that will work with Python? There is nothing even close to the VB GUI builder for Python. That's because Python has to cover many GUI frameworks, VB only has to deal with one. Which is why VB is pretty much useless for anything other than Windows... But there are some GUI tools available: - Glade - PyQt/Side? - SpecTcl - Tkinter - Dabo - WxPython (customised for simple DB apps) I've tried all of them and only Dabo worked for me, but Davo wasn't really suitable for my particular project at the time. But YMMV.... There are some others I haven't tried too - Kivy, Blackadder(??) And if you want to try using Jython or MacPython(?) you can use the native GUI builders for those: - Eclipse/Netbeans (Java) - XDeveloper (MacOS) - I tried this once and it kind of works... -- 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 Jun 30 03:01:21 2018 From: __peter__ at web.de (Peter Otten) Date: Sat, 30 Jun 2018 09:01:21 +0200 Subject: [Tutor] CSV Read References: Message-ID: Giulia Marcoux wrote: > Hello, > > I am a student learning python, and i am running into some difficulties. I > am tryign to read a csv file that has different delimiters for different > rows: Example: > > Format:V1.1 > Model: R > Step Size: 10mm > Distance: 10cm > Gain: 1000 > > X,Y > 1,3 > 2,5 > 6,5 > 5,7 > > For the X,Y data, I have no problem getting it into the right cells, > however I cannot separate the values after the colon for the first few > rows. Its either one or the other. I am using pandas to read my CSV file. > So far I have: > > d=pd.read_csv('read.csv',delimiter=',',skiprows=0) > > When I replace the delimiter for ':', it no longer works, and if i put > sep=',|:' it does not work either. In the end I need to plot the x,y > values and i will need some of the info in the rows above like the gain to > manipulate the y data. If you have any tips for me I would be very > grateful! Rather than forcing both blocks into the same data structure I would read the header with the "key: value" lines into a separate object. If the header and the X,Y pairs are separated by an empty line read until that and then feed the file object to pandas.read_csv() to read those pairs: import itertools import pandas as pd def header(f): """Pass on lines from f; stop at the first whitespace-only line. """ return itertools.takewhile(str.strip, f) def pairs(lines, sep=":"): """Split lines into (key, value) pairs. Remove leading/trailing whitespace from value. """ for line in lines: key, value = line.split(sep, 1) yield key, value.strip() with open("data.txt") as f: metadata = dict(pairs(header(f))) df = pd.read_csv(f) print(metadata) print(df) From chris_roysmith at internode.on.net Fri Jun 29 22:55:16 2018 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Sat, 30 Jun 2018 12:55:16 +1000 Subject: [Tutor] how to change the command "string" on a tkinter Button? Message-ID: <140dc3c6-2150-737a-fc09-df458a769833@internode.on.net> Hi, OS is Linux, Python version is 3.6.5 I am trying to change the command of a tkinter Button in my program. Eventually I want to be able to do this to many buttons. My attempt at code brings up no error messages, but the button appears to do nothing. I really have no idea how to do this, but this is what I wrote. ==================================== #!/usr/bin/python3 from tkinter import * class form(object): ??? def __init__(self, x, reply, master, z,bu): ??????? self.x=x ??????? self.reply=reply ??????? self.master=master ??????? self.z=z ??????? self.bu=bu ??? def change(x, reply, z, b): ??????? #f contains the alternative command (as a string) ??????? f=["lambda x=vars, txt=reply,fu=0 bu=b1 :form.change(x, txt, fu, bu)", "lambda x=vars, txt=first, fu=1 bu=b1: form.change(x, txt, fu, bu)"] ??????? for i in range(4): ??????????? x[i].set(reply[i]) ??????? #attempt to change command clause ??????? set.button(f[z]) ??? def draw(master): ??????? vars = [] ??????? label = [] ??????? button = StringVar ??????? for i in range(4): ??????????? var = StringVar() ??????????? vars.append(var) ??????????? label.append("") ??????????? label[i] = Label( master, textvariable=var, relief=RAISED ).grid(row=i, column=0) ??????????? vars[i].set(first[i]) ??????? b1=Button(master, text="change", command=button).grid(row=i+1, column=0) reply=["now I don't know", "Happy birthday", "Many happy returns", "Next it's my turn",1] first=["What's your name?", "My name is Fred", "I have just had my birthday", "your's is next!",2] master=Tk() form.draw(master) master.mainloop() ================================= How should I do this, I had worked around the problem by destroying? the window and building it again, but it was pointed out that I have an unusual coding style doing this. All hints appreciated! Regards, Chris Roy-Smith From sydney.shall at kcl.ac.uk Sat Jun 30 06:50:13 2018 From: sydney.shall at kcl.ac.uk (Shall, Sydney) Date: Sat, 30 Jun 2018 12:50:13 +0200 Subject: [Tutor] RAD GUI Development (like Visual Studio/VBA) In-Reply-To: References: Message-ID: <5d3a0b08-c451-9657-0972-7a58108e32c5@kcl.ac.uk> On 30/06/2018 00:34, Alan Gauld via Tutor wrote: > On 29/06/18 16:05, Glen wrote: > >> Can someone advise on a RAD, drag and drop style graphical form/dialog >> creator? Akin to VBA or Visual Studio that will work with Python? > > There is nothing even close to the VB GUI builder for Python. > > That's because Python has to cover many GUI frameworks, > VB only has to deal with one. Which is why VB is pretty > much useless for anything other than Windows... > > But there are some GUI tools available: > - Glade - PyQt/Side? > - SpecTcl - Tkinter > - Dabo - WxPython (customised for simple DB apps) > > I've tried all of them and only Dabo worked for me, > but Davo wasn't really suitable for my particular > project at the time. But YMMV.... > > There are some others I haven't tried too > - Kivy, Blackadder(??) > > And if you want to try using Jython or MacPython(?) > you can use the native GUI builders for those: > - Eclipse/Netbeans (Java) > - XDeveloper (MacOS) - I tried this once and it kind of works... > Alan, Could you expand a bit on the use of XDeveloper. I have need of some such utility for my project and I use a MAC OS X. Thanks. -- _________ Professor Sydney Shall Department of Haematology/Oncology Phone: +(0)2078489200 E-Mail: sydney.shall [Correspondents outside the College should add @kcl.ac.uk] From alan.gauld at yahoo.co.uk Sat Jun 30 12:17:46 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 30 Jun 2018 17:17:46 +0100 Subject: [Tutor] how to change the command "string" on a tkinter Button? In-Reply-To: <140dc3c6-2150-737a-fc09-df458a769833@internode.on.net> References: <140dc3c6-2150-737a-fc09-df458a769833@internode.on.net> Message-ID: On 30/06/18 03:55, Chris Roy-Smith wrote: > I am trying to change the command of a tkinter Button in my program. > Eventually I want to be able to do this to many buttons. Since I'm not 100% sure if you mean the command or the label or both here is a simple example that does both... ############################################ import tkinter as tk def cmd1(): print('This is command 1') def cmd2(): print('This is number 2') def swapCmd(): c = b1['command'] # kluge to get the function name from Tcl id if str(c).endswith("cmd1"): b1['command'] = cmd2 else: b1['command'] = cmd1 def swapText(): t = b1['text'] if t == "Cool": b1['text'] = "Hot" else: b1['text'] = "Cool" # make GUI top = tk.Tk() win = tk.Frame(top) win.pack() b1 = tk.Button(win,text="Cool", command=cmd1) b1.pack() b2 = tk.Button(win, text="Swap text", command=swapText) b2.pack() b3 = tk.Button(win, text="Swap cmd", command=swapCmd) b3.pack() top.mainloop() ########################################### > I really have no idea how to do this, but this is what I wrote. The class is a bit odd. I think you may need to review how you define methods. > ==================================== > > #!/usr/bin/python3 > from tkinter import * > > class form(object): In Python 3 you don't need to explicitly inherit object, its done automatically. > ??? def __init__(self, x, reply, master, z,bu): > ??????? self.x=x > ??????? self.reply=reply > ??????? self.master=master > ??????? self.z=z > ??????? self.bu=bu This is fine but your later code never seems to create an instance of form so this code never runs. > ??? def change(x, reply, z, b): Note that this has no self parameter so is not really a method (or more specifically if you call it as a method x will be treated as self...) > ??????? #f contains the alternative command (as a string) > ??????? f=["lambda x=vars, txt=reply,fu=0 bu=b1 :form.change(x, txt, > fu, bu)", "lambda x=vars, txt=first, fu=1 bu=b1: form.change(x, txt, fu, > bu)"] Why store the commands as strings? Just store the actual command objects. Functions(including lambdas) are objects in Python just like any other object. > ??????? for i in range(4): > ??????????? x[i].set(reply[i]) Where does the 4 come from? Its not at all clear what x is supposed to be? And you never call it. > ??????? #attempt to change command clause > ??????? set.button(f[z]) And where is z defined? And set.button should refer to the button attribute of set. But set has no such attribute. I don;t know what you are trying to do here? > ??? def draw(master): Again this is not a method of form, it has no self. Its just a function inside a class. > ??????? vars = [] > ??????? label = [] > ??????? button = StringVar You just set button to be the StringVar class. I suspect you wanted an instance so should have parens? > ??????? for i in range(4): > ??????????? var = StringVar() > ??????????? vars.append(var) > ??????????? label.append("") > ??????????? label[i] = Label( master, textvariable=var, relief=RAISED > ).grid(row=i, column=0) grid always returns None so you are just adding Nones to your list. > ??????????? vars[i].set(first[i]) Since you are still inside the loop you could just use var directly: var.set(first[i]) > ??????? b1=Button(master, text="change", command=button).grid(row=i+1, > column=0) You have set command to be the StringVar class. When you execute the button it will create an instance of StringVar which will then be deleted. > reply=["now I don't know", "Happy birthday", "Many happy returns", "Next > it's my turn",1] > first=["What's your name?", "My name is Fred", "I have just had my > birthday", "your's is next!",2] > master=Tk() > > form.draw(master) > master.mainloop() > > ================================= > > How should I do this, Hopefully my code will give you a clue. I'm not totally clear what the code above is supposed to be doing since it never changes the GUI, rather the draw function creates some buttons and the rest of your code is never used. -- 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 Sat Jun 30 13:21:51 2018 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 30 Jun 2018 18:21:51 +0100 Subject: [Tutor] RAD GUI Development (like Visual Studio/VBA) In-Reply-To: <5d3a0b08-c451-9657-0972-7a58108e32c5@kcl.ac.uk> References: <5d3a0b08-c451-9657-0972-7a58108e32c5@kcl.ac.uk> Message-ID: On 30/06/18 11:50, Shall, Sydney wrote: >> And if you want to try using Jython or MacPython(?) >> you can use the native GUI builders for those: >> - Eclipse/Netbeans (Java) >> - XDeveloper (MacOS) - I tried this once and it kind of works... >> > Alan, > > Could you expand a bit on the use of XDeveloper. > I have need of some such utility for my project and I use a MAC OS X. I've only gone through the tutorial and made some tweaks to the resulting code but it seemed to work pretty much as usual for Cocoa projects using Objective C. You layout the UI then connect the widgets to your code (which is in Python of course). The PyObjC project is here: https://pythonhosted.org/pyobjc/ The tutorial I used is here: https://pythonhosted.org/pyobjc/tutorials/firstapp.html Note that it is old, but I only have an iBook from 2001 running MacOS Lion, so age wasn't an issue for me! -- 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 david at graniteweb.com Sat Jun 30 21:02:39 2018 From: david at graniteweb.com (David Rock) Date: Sat, 30 Jun 2018 20:02:39 -0500 Subject: [Tutor] CSV Read In-Reply-To: References: Message-ID: <370A75A4-32B0-45C0-9A3C-4DED3B0CC34C@graniteweb.com> > On Jun 30, 2018, at 02:01, Peter Otten <__peter__ at web.de> wrote: > > > Rather than forcing both blocks into the same data structure I would read > the header with the "key: value" lines into a separate object. > > If the header and the X,Y pairs are separated by an empty line read until > that and then feed the file object to pandas.read_csv() to read those pairs: Ultimately, everything presented so far is conjecture without getting clarification from the original poster on what they actually need and what the dataset looks like. Does every file look the same? is it always a header and data separated by empty lines? Hopefully we will get some feedback. ? David Rock david at graniteweb.com