From michael.selik at gmail.com Wed Jun 1 00:10:02 2016 From: michael.selik at gmail.com (Michael Selik) Date: Wed, 01 Jun 2016 04:10:02 +0000 Subject: [Tutor] Python OLS help In-Reply-To: References: Message-ID: You're welcome. A tip for the next bug: Google is pretty good at finding a discussion of the error if you paste the whole phrase as your search terms. In this case, I searched for "ValueError for numerical factors num_columns must be an int" and found the relevant Google Group thread. PS. I replied to the mailing list in case someone else has the same issue. These emails are archived and searchable. On Tue, May 31, 2016 at 10:07 PM Vadim Katsemba wrote: > I upgraded to patsy 0.4.1 and got the regression to run. Thank you very > much for your help! > > On Tue, May 31, 2016 at 8:03 PM, Michael Selik > wrote: > >> On Tue, May 31, 2016 at 5:45 PM Vadim Katsemba >> wrote: >> >>> I typed in lm = smf.ols(formula='LATITUDE~DIAMETER',data=dataf).fit(), >>> and I ended up getting this error: ValueError: For numerical factors, >>> num_columns must be an int. >>> >> >> You may be using an old version of Patsy, the module that allows you to >> specify your OLS formula like R-lang does. What version of patsy are you >> using? This seems to have been a problem with Patsy v0.4.0 and was fixed in >> v0.4.1 (as per an email thread I read [0]) >> >> [0] https://groups.google.com/forum/#!topic/pystatsmodels/KcSzNqDxv-Q >> > > From nitinchandra1 at gmail.com Wed Jun 1 08:15:01 2016 From: nitinchandra1 at gmail.com (nitin chandra) Date: Wed, 1 Jun 2016 17:45:01 +0530 Subject: [Tutor] UPDATE, pyhthon+postgre Message-ID: Hello All, I am writing a UPDATE statement, but am getting stuck with the following error. Traceback (most recent call last): File "/var/www/passtms/updateCutoff.cgi", line 70, in WHERE group_code = %s;""", (name,add,add1,city,state_county,country,basic,todayDT,todayTIME,grpCode)) DataError: invalid input syntax for type timestamp with time zone: "05:27PM" LINE 18: time_created='05:27PM' ^ Table created with the following lines :- date_created date DEFAULT ('now'::text)::date, time_created timestamp with time zone DEFAULT now() With the code below, I am getting data from a HTML Form, and then I am trying to update the single record in Pg Table grpCode = form.getvalue('nameradio') name = form.getvalue('name') add = form.getvalue('add') add1 = form.getvalue('add1') city = form.getvalue('city') todayDT = form.getvalue('todayDT') todayTIME = form.getvalue('todayTIME') cursor1.execute("""UPDATE employee SET name=%s, add=%s, add1=%s, city=%s, state_county=%s, country=%s, basic=%s, date_created=%s, time_created=%s WHERE group_code = %s;""", (name,add,add1,city,state_county,country,basic,todayDT,todayTIME,grpCode)) How do I resolve this issue? Thanks Nitin From johnf at jfcomputer.com Wed Jun 1 09:23:00 2016 From: johnf at jfcomputer.com (john) Date: Wed, 1 Jun 2016 06:23:00 -0700 Subject: [Tutor] UPDATE, pyhthon+postgre In-Reply-To: References: Message-ID: <574EE1B4.4080303@jfcomputer.com> cursor1.execute("""UPDATE employee SET name=%s, add=%s, add1=%s, city=%s, state_county=%s, country=%s, basic=%s, WHERE group_code = %s""" % (name,add,add1,city,state_county,country,basic,grpCode)) It appears that you are not required to provide any time or date since you created a default. Of course that assumes you want today and now. If that assumption is wrong then lookup "datetime". To make life a little easier lookup the module "dateutil". You would use either of those modules to convert your python input to the correct output for postgres (actually for any sql database). Hint: if the field requires a string you should place quotes around the placeholder as in '%s'. Johnf From nitinchandra1 at gmail.com Wed Jun 1 13:09:20 2016 From: nitinchandra1 at gmail.com (nitin chandra) Date: Wed, 1 Jun 2016 22:39:20 +0530 Subject: [Tutor] UPDATE, pyhthon+postgre In-Reply-To: <574EE1B4.4080303@jfcomputer.com> References: <574EE1B4.4080303@jfcomputer.com> Message-ID: Thank you Stephen, Johnf, This is what I did and it worked :- todayTime2 = datetime.datetime.now() todayTime1 = timezone('Asia/Calcutta').localize(todayTime2) time_created=%s WHERE group_code = %s;""", ( .....,...,..., str(todayTime1),....)) Thanks for the Hint, Johnf :) Nitin On 1 June 2016 at 18:53, john wrote: > cursor1.execute("""UPDATE employee SET > name=%s, > add=%s, > add1=%s, > city=%s, > state_county=%s, > country=%s, > basic=%s, > WHERE group_code = %s""" % > (name,add,add1,city,state_county,country,basic,grpCode)) > > It appears that you are not required to provide any time or date since you > created a default. Of course that assumes you want today and now. > > If that assumption is wrong then lookup "datetime". To make life a little > easier lookup the module "dateutil". You would use either of those modules > to convert your python input to the correct output for postgres (actually > for any sql database). > > Hint: if the field requires a string you should place quotes around the > placeholder as in '%s'. > > Johnf > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From wrw at mac.com Wed Jun 1 14:55:15 2016 From: wrw at mac.com (William Ray Wing) Date: Wed, 01 Jun 2016 14:55:15 -0400 Subject: [Tutor] Study Tips In-Reply-To: References: Message-ID: <79F9E5EB-9783-44DE-9402-D1930FFFC11E@mac.com> > On May 30, 2016, at 1:45 AM, Steve Lett wrote: > > Hi folks, > Just started learning python. I've been having a really hard time in > getting started, and still am! I have a slight learning difficulty, > including a stroke in Jan.2010. You wouldnt know even if u were here > looking at me! Praise God for the great salvation! > I have a slight learning difficulty. Has anyone got any tips on how to > study programming and what approach would be best for me? > Quick question - just to be sure everyone is on the same page. Is Python your first programming language? That is, are you comfortable with the process of decomposing a problem into a series of steps (an algorithm) that will yield an answer? Do you already know how to program in some OTHER language. If yes - the answers you have gotten are great. If NOT, then we should be talking on a whole different level. -Thanks, -Bill > Out of a long list of books that I have collected ( python, Blender, etc ) > I have decided to start on Introducing Python (hard copy 2nd Ed, ebook 3rd > ed) by Bill Lubanovic. Before that I was going to start with Python > Programming for the Absolute Beginner, Michael Dawson. > > Any thoughts on these issues and especially the study tips already > mentioned. > > Thank you, Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From anindya1912 at gmail.com Wed Jun 1 15:06:49 2016 From: anindya1912 at gmail.com (Anindya Mookerjea) Date: Wed, 1 Jun 2016 20:06:49 +0100 Subject: [Tutor] Study Tips In-Reply-To: <79F9E5EB-9783-44DE-9402-D1930FFFC11E@mac.com> References: <79F9E5EB-9783-44DE-9402-D1930FFFC11E@mac.com> Message-ID: Hi experts, I am going to start learning Python and have got no coding experience/knowledge whatsoever . So Python would be my first programming language Please suggest Thank you Anindya On Wednesday, June 1, 2016, William Ray Wing wrote: > > > On May 30, 2016, at 1:45 AM, Steve Lett > wrote: > > > > Hi folks, > > Just started learning python. I've been having a really hard time in > > getting started, and still am! I have a slight learning difficulty, > > including a stroke in Jan.2010. You wouldnt know even if u were here > > looking at me! Praise God for the great salvation! > > I have a slight learning difficulty. Has anyone got any tips on how to > > study programming and what approach would be best for me? > > > > Quick question - just to be sure everyone is on the same page. Is Python > your first programming language? That is, are you comfortable with the > process of decomposing a problem into a series of steps (an algorithm) that > will yield an answer? Do you already know how to program in some OTHER > language. If yes - the answers you have gotten are great. If NOT, then we > should be talking on a whole different level. > > -Thanks, > -Bill > > > > Out of a long list of books that I have collected ( python, Blender, etc > ) > > I have decided to start on Introducing Python (hard copy 2nd Ed, ebook > 3rd > > ed) by Bill Lubanovic. Before that I was going to start with Python > > Programming for the Absolute Beginner, Michael Dawson. > > > > Any thoughts on these issues and especially the study tips already > > mentioned. > > > > Thank you, 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 > -- Best regards Anindya From ahall at autodist.com Wed Jun 1 11:36:31 2016 From: ahall at autodist.com (Alex Hall) Date: Wed, 1 Jun 2016 11:36:31 -0400 Subject: [Tutor] OrderedDict? Message-ID: Hi list, I'm trying to find the OrderedDict documentation. I found one page, but it wasn't very clear on how to actually make an OrderedDict. Plus, an empty constructor in Python's interpreter returns an error that the class doesn't exist (Python 2.7.11). I also found the PEP, plus some home-grown suggestions on how to implement this, but so far that's it. Although I thought this was in 2.7, it doesn't appear on the Data Structures page at all. What am I missing? Thanks. -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From min786a at googlemail.com Wed Jun 1 07:43:25 2016 From: min786a at googlemail.com (marat murad) Date: Wed, 1 Jun 2016 12:43:25 +0100 Subject: [Tutor] Tutor Digest, Vol 147, Issue 52 In-Reply-To: References: Message-ID: Thanks for your replies,I think I understand it much better now. I Also I wanted to know what the 'None' does in the second program. Thanks in Advance Minhaj On Wed, Jun 1, 2016 at 1:59 AM, wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > Today's Topics: > > 1. Help with 'if' statement and the concept of None (marat murad) > 2. Python OLS help (Vadim Katsemba) > 3. Re: Help with 'if' statement and the concept of None (Alan Gauld) > 4. Re: Python OLS help (Alan Gauld) > 5. Re: Help with 'if' statement and the concept of None > (Steven D'Aprano) > 6. Re: Help with 'if' statement and the concept of None (Ben Finney) > > > ---------- Forwarded message ---------- > From: marat murad > To: tutor at python.org > Cc: > Date: Tue, 31 May 2016 16:16:21 +0100 > Subject: [Tutor] Help with 'if' statement and the concept of None > Hi > I'm learning how to code with python an I have purchased the book 'Python > Programming for the absolute beginner,third edition' by Michael Dawson. > There is one concept that is confusing me in chapter 3 page 71 there is a > program whose code I have pasted below. The author introduced a new way of > coding the Boolean NOT operator with the 'if' statement I have highlighted > the relevant area,apparently this if statement actually means if money != > 0,which I understood,but the program below this one which introduces the > idea of slicing also has a if statement similar to the first one,except the > second one accepts 0 value but the first one doesn't. > > print("Welcome to the Chateau D'food") > print("It seems we are quit full today.\n") > > money = int(input("How many dollars do you slip the Maitr D'? ")) > > *if money:* > print("Ah I think I can make something work") > else: > print("Please sit ,it may be a while") > > > input("\nPress enter to exit") > > > The slicing program > > word = "existential" > > print("Enter the beginning and ending index for the word existential") > print("press he enter key at 'Start' to exit") > > start= None > while start != "": > start=input("\nStart: ") > > * if start:* > start=int(start) > > finish=int(input("Finish: ")) > > print ("word[",start, ":",finish, "]is " , end="") > print(word[start:finish]) > > input("\nPress enter to exit") > > I hope i made sense. > > Thankyou > > > > ---------- Forwarded message ---------- > From: Vadim Katsemba > To: tutor at python.org > Cc: > Date: Tue, 31 May 2016 11:30:29 -0400 > Subject: [Tutor] Python OLS help > Hello there, I am having trouble running the Ordinary Least Squares (OLS) > regression on Spyder. I typed in lm = > smf.ols(formula='LATITUDE~DIAMETER',data=dataf).fit(), and I ended up > getting this error: ValueError: For numerical factors, num_columns must be > an int. How am I supposed to run the OLS, is there another module I need to > use? Any help would be appreciated. > > > > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Date: Tue, 31 May 2016 23:06:47 +0100 > Subject: Re: [Tutor] Help with 'if' statement and the concept of None > On 31/05/16 16:16, marat murad via Tutor wrote: > > > program whose code I have pasted below. The author introduced a new way > of > > coding the Boolean NOT operator with the 'if' statement I have > highlighted > > the relevant area,apparently this if statement actually means if money != > > 0,which I understood, > > Boolean values are either true or false. > Other values are given boolean equivalent values by Python. > eg. For strings an empty string is False, anything else is True. > For numbers 0 is False anything else is True. > > So in your example: > > > *if money:* > > print("Ah I think I can make something work") > > else: > > print("Please sit ,it may be a while") > > > > The first 'if' test is saying > > 'if money is equivalent to True' (anything other than zero) > > In effect that's the same as you said (it's like testing for != 0) > but the important difference is that it is relying > on the boolean *equivalence* of an integer value. > The same is true in your second example: > > > idea of slicing also has a if statement similar to the first one,except > the > > second one accepts 0 value but the first one doesn't. > > > > > start=input("\nStart: ") > > > > * if start:* > > start=int(start) > > Again this is really saying if start is True and for a string > (which is what input() returns), as I said above, True means > not empty. So the string '0' is not empty and therefore True. > It's not the value of the character that matters it's the fact > that there is a character there at all. > > All objects in Python have these boolean equivalent values, > for example an empty list, tuple,set or dictionary is also > considered False. As is the special value None. > > > I hope i made sense. > > Yes, although your subject also mentions the concept of None? > Did you have another question about that? > > 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 > > > > > > ---------- Forwarded message ---------- > From: Alan Gauld > To: tutor at python.org > Cc: > Date: Tue, 31 May 2016 23:10:49 +0100 > Subject: Re: [Tutor] Python OLS help > On 31/05/16 16:30, Vadim Katsemba wrote: > > Hello there, I am having trouble running the Ordinary Least Squares (OLS) > > regression on Spyder. > > I had no idea what Spyder was but a Google search says its an IDE > somewhat akin to matlab or IPython... It also has a discussion group: > > http://groups.google.com/group/spyderlib > > You may find someone on this list who knows it but you will likely > get a better response on the spyder forum. This list is really > for core python language questions and although we try to be > helpful on other matters a dedicated forum is usually better. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > > > > ---------- Forwarded message ---------- > From: "Steven D'Aprano" > To: tutor at python.org > Cc: > Date: Wed, 1 Jun 2016 10:12:08 +1000 > Subject: Re: [Tutor] Help with 'if' statement and the concept of None > On Tue, May 31, 2016 at 04:16:21PM +0100, marat murad via Tutor wrote: > > > money = int(input("How many dollars do you slip the Maitr D'? ")) > > *if money:* > > print("Ah I think I can make something work") > > else: > > print("Please sit ,it may be a while") > > All values in Python can be used where a true or false boolean value is > expected, such as in an "if" or a "while" statement. We call this a > "boolean context", meaning something which expects a true or false flag. > > So we have True and False (with the initial capital letter) as special > constants, but we also can treat every other value as if they were true > or false (without the initial capital). Sometimes we call them "truthy" > and "falsey", or "true-like" and "false-like" values. > > For Python built-ins, we have: > > Falsey (false-like) values: > > - zero: 0, 0.0, 0j, Fraction(0), etc. > - empty strings: "", b"" > - empty containers: [], (), {}, set() etc. > (empty list, empty tuple, empty dict, empty set) > - sequences with len() == 0 > - None > - False > > Truthy (true-like) values: > > - non-zero numbers: 1, 2.0, 3j, Fraction(4, 5), etc. > - non-empty strings: "Hello world!", b"\x0" > (yes, even the null-byte is non-empty, since it has length 1) > - non-empty containers > - sequences with len() != 0 > - classes, object() > - True > > We say that "false values represent nothing", like zero, empty strings, > None, and empty containers; while "true values represent something", > that is, anything which is not nothing. > > > So any time you have a value, say, x, we can test to see if x is a > truthy or falsey value: > > > values = [1, 5, 0, -2.0, 3.7, None, object(), (1, 2), [], True] > for x in values: > if x: > print(x, "is a truthy value") > else: > print(x, "is a falsey value") > > > > > -- > Steve > > > > ---------- Forwarded message ---------- > From: Ben Finney > To: tutor at python.org > Cc: > Date: Wed, 01 Jun 2016 10:58:58 +1000 > Subject: Re: [Tutor] Help with 'if' statement and the concept of None > marat murad via Tutor writes: > > > The author introduced a new way of coding the Boolean NOT operator > > with the 'if' statement I have highlighted the relevant > > area,apparently this if statement actually means if money != 0,which I > > understood > > Not quite. The statement actually means ?if ?money? evaluates as true?. > > Any Python value can be interrogated with the question ?Are you true or > false??, and that is what the ?if? statement does. > > This is different from asking ?Is this the True constant or the False > constant?? because for most values the answer is ?Neither?. > > In Python the question ?Is this value true, or false?? is usually > implemented as ?Is this value something, or nothing??. If the value is > conceptually nothing, it evaluates as false when interrogated in a > boolean context. > > See the Python documentation for a comprehensive list of false values > ; > any not-false value is true by default. > > If you learn the conceptual ?if it's not something, it's false?, then > you will have a fairly good intuition for how ?if somevalue? works. > > -- > \ ?None can love freedom heartily, but good men; the rest love | > `\ not freedom, but license.? ?John Milton | > _o__) | > Ben Finney > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > From steve.lett777 at gmail.com Wed Jun 1 16:16:03 2016 From: steve.lett777 at gmail.com (Steve Lett) Date: Thu, 2 Jun 2016 06:16:03 +1000 Subject: [Tutor] Study Tips In-Reply-To: References: Message-ID: Gday! On 31/05/2016 8:52 AM, "Steve Lett" wrote: > Thank you for the reply. > On 30/05/2016 3:45 PM, "Steve Lett" wrote: > >> Hi folks, >> Just started learning python. I've been having a really hard time in >> getting started, and still am! I have a slight learning difficulty, >> including a stroke in Jan.2010. You wouldnt know even if u were here >> looking at me! Praise God for the great salvation! >> I have a slight learning difficulty. Has anyone got any tips on how to >> study programming and what approach would be best for me? >> >> Out of a long list of books that I have collected ( python, Blender, etc >> ) I have decided to start on Introducing Python (hard copy 2nd Ed, ebook >> 3rd ed) by Bill Lubanovic. Before that I was going to start with Python >> Programming for the Absolute Beginner, Michael Dawson. >> >> Any thoughts on these issues and especially the study tips already >> mentioned. >> >> Thank you, Steve >> > From alan.gauld at yahoo.co.uk Wed Jun 1 18:40:10 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 1 Jun 2016 23:40:10 +0100 Subject: [Tutor] Study Tips In-Reply-To: References: <79F9E5EB-9783-44DE-9402-D1930FFFC11E@mac.com> Message-ID: On 01/06/16 20:06, Anindya Mookerjea wrote: > Hi experts, > > I am going to start learning Python and have got no coding > experience/knowledge whatsoever . So Python would be my first programming > language Start with one of the tutorials on the non-programmers page of python.org http://wiki.python.org/moin/BeginnersGuide/NonProgrammers If you are comfortable with IT things in general (editing files, navigating directories etc) then you could try mine(see below) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Wed Jun 1 18:45:42 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 1 Jun 2016 23:45:42 +0100 Subject: [Tutor] OrderedDict? In-Reply-To: References: Message-ID: On 01/06/16 16:36, Alex Hall wrote: > I'm trying to find the OrderedDict documentation. I found one page, but it > wasn't very clear on how to actually make an OrderedDict. Plus, an empty > constructor in Python's interpreter returns an error that the class doesn't > exist It's in the collections module: >>> import collections as coll >>> help(coll.OrderedDict) ... >>> od = coll.OrderedDict() >>> od OrderedDict() >>> 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 Wed Jun 1 18:52:28 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 1 Jun 2016 23:52:28 +0100 Subject: [Tutor] Use of None (was: Re: Tutor Digest, Vol 147, Issue 52) In-Reply-To: References: Message-ID: On 01/06/16 12:43, marat murad via Tutor wrote: > Thanks for your replies,I think I understand it much better now. I Also I > wanted to know what the 'None' does in the second program. Please make the subject something meaningful and delete all the excess messages. we have all seen them already and some people pay by the byte. As for none, it just means a reference to nothing. ie no object. So in your example: start= None while start != "": start=input("\nStart: ") ... The start = None could have been replaced by anything other than an empty string. It just creates an initial value that causes the while loop to start. You will often find None used like this: to create a named value that has no significance at that point in the program but will be used later. Another way of writing your example would be: while True: # loop forever start = input(...) if start = "": break # break exits the nearest loop ... 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 ahall at autodist.com Thu Jun 2 07:15:53 2016 From: ahall at autodist.com (Alex Hall) Date: Thu, 2 Jun 2016 07:15:53 -0400 Subject: [Tutor] OrderedDict? In-Reply-To: References: Message-ID: <4CFAF5D7-7261-4FD9-B092-1DBB80FE14BB@autodist.com> Thanks. I've never used that module before, and didn't realize it's not imported by default, which must be why my first tries failed. I've got it working now. > On Jun 1, 2016, at 18:45, Alan Gauld via Tutor wrote: > > On 01/06/16 16:36, Alex Hall wrote: > >> I'm trying to find the OrderedDict documentation. I found one page, but it >> wasn't very clear on how to actually make an OrderedDict. Plus, an empty >> constructor in Python's interpreter returns an error that the class doesn't >> exist > > It's in the collections module: > >>>> import collections as coll >>>> help(coll.OrderedDict) > ... >>>> od = coll.OrderedDict() >>>> od > OrderedDict() >>>> > > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From magyar1886 at gmail.com Thu Jun 2 09:40:44 2016 From: magyar1886 at gmail.com (Marco Soldavini) Date: Thu, 2 Jun 2016 15:40:44 +0200 Subject: [Tutor] Tkinter and threading Message-ID: Hello, probably this is a very naive question, but I've read some stuff on Tkinter and its infinite loop. Then about how can i bind actions to elements, for example buttons. What if I want to run another loop beside the graphical interface in the same python script? For example a state machine with a var state which can have some discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE) and a text element on the gui that reports that state. So a button cause a transition > the python loop (not the gui loop) detects the transition and changes the state var value > the gui refresh its value on screen. I wrote some code to try it without threading but it does not give the expected result as it seems the button update status action is already triggered. I am missing some basic point here from Tkinter import * state = "STOPPED" root = Tk() myContainer1 = Frame(root) myContainer1.pack() label1=Label(myContainer1, text=state, font = "Helvetica 16 bold italic") label1.pack() def change(new_state): label1.config(text=new_state) button1 = Button(myContainer1, text="START") button1.bind("", change("START")) button1.pack() button2 = Button(myContainer1) button2["text"]= "STOP" button2.pack() root.mainloop() Thanks marco From alan.gauld at yahoo.co.uk Thu Jun 2 12:24:33 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 2 Jun 2016 17:24:33 +0100 Subject: [Tutor] Tkinter and threading In-Reply-To: References: Message-ID: On 02/06/16 14:40, Marco Soldavini wrote: > What if I want to run another loop beside the graphical interface in > the same python script? You need to do it in a separate thread. Keep the Tkinter loop on your main thread and use it to trigger actions. > For example a state machine with a var state which can have some > discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE) > and a text element on the gui that reports that state. A state machine should not need a loop! It should be triggered by state changes alone. ie state should only be changed by calling a function. That function sets the state value and calls any processing function associated with the transition. (Whether before or after you set value depends on whether you are using Moore or Mealy semantics, in practice it makes little difference.) You might need a loop to detect (some of the) changes but setting the state machine should be entirely separate. > So a button cause a transition > the python loop (not the gui loop) > detects the transition and changes the state var value > the gui > refresh its value on screen. The button generates an event. The event causes an event-handling function to be called. That function may well cause a state transition that your state machine can detect. (But it could just as well call your state machine directly) > I wrote some code to try it without threading but it does not give the > expected result as it seems the button update status action is already > triggered. I am missing some basic point here If you want two parallel event loops you pretty much need threads. Without threads you need to develop some kind of event-driven callback solution - which may very well be preferable! -- 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 2 12:39:23 2016 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jun 2016 18:39:23 +0200 Subject: [Tutor] Tkinter and threading References: Message-ID: Marco Soldavini wrote: > Hello, > probably this is a very naive question, but I've read some stuff on > Tkinter and its infinite loop. > > Then about how can i bind actions to elements, for example buttons. > > What if I want to run another loop beside the graphical interface in > the same python script? I usually adapt http://effbot.org/zone/tkinter-threads.htm > For example a state machine with a var state which can have some > discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE) > and a text element on the gui that reports that state. > > So a button cause a transition > the python loop (not the gui loop) > detects the transition and changes the state var value > the gui > refresh its value on screen. > > I wrote some code to try it without threading but it does not give the > expected result as it seems the button update status action is already > triggered. I am missing some basic point here Indeed your problem has nothing to do with threads. > > from Tkinter import * > > state = "STOPPED" > > root = Tk() > > myContainer1 = Frame(root) > myContainer1.pack() > label1=Label(myContainer1, text=state, font = "Helvetica 16 bold italic") > label1.pack() > > def change(new_state): > label1.config(text=new_state) > > button1 = Button(myContainer1, text="START") > button1.bind("", change("START")) In the line above you invoke the change function and bind its result (None) to the button1 widget's event. Instead you should pass a function that will be invoked by Tkinter when the button is pressed: def start(event): change("START") button1.bind("", start) You can define simple functions inline, so button1.bind("", lambda event: change("START")) would also work. However, the usual way to bind actions to Button widgets is to pass the callback function as the command argument to the constructor: button = Button(container, command=your_func_that_takes_no_args) With that approach your script will become from Tkinter import * def change(new_state): label1.config(text=new_state) def start(): change("RUNNING") def stop(): change("STOPPED") root = Tk() myContainer1 = Frame(root) myContainer1.pack() label1 = Label(myContainer1, font="Helvetica 16 bold italic") label1.pack() stop() button1 = Button(myContainer1, text="Start", command=start) button1.pack() button2 = Button(myContainer1, text="Stop", command=stop) button2.pack() root.mainloop() From thomasolaoluwa at gmail.com Thu Jun 2 13:05:43 2016 From: thomasolaoluwa at gmail.com (Olaoluwa Thomas) Date: Thu, 2 Jun 2016 18:05:43 +0100 Subject: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop Message-ID: Hi Tutor, I'm trying to parse words in a file line by line and place all words into another list but I keep getting a list with nested lists. I would normally pore over it and go to google and fix my problems but this one escapes me and frankly, I'm tired of being stuck in the same place for almost a week. Here's the code: fname = raw_input('Enter file name:\n') try: fhand = open(fname) except: print 'File cannot be found or opened:', fname exit() lst = list() for line in fhand: words = line.split() #print words (this was a test that a portion of my code was working) lst.append(words) print lst A text file with the following contents "But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief" would give me the output in the attached screenshot [image: Inline image 2] whereas I want only one list containing strings not nested lists. Any help would be appreciated. *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* From alan.gauld at yahoo.co.uk Thu Jun 2 13:18:04 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 2 Jun 2016 18:18:04 +0100 Subject: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop In-Reply-To: References: Message-ID: On 02/06/16 18:05, Olaoluwa Thomas wrote: > lst = list() > for line in fhand: > words = line.split() words is now a list of words a test that a portion of my code was working) > lst.append(words) So you append that list to lst and get a list of lists. Try using + instead: lst += words > would give me the output in the attached screenshot > [image: Inline image 2] This is a text based list attachments often get stripped out by the server -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Thu Jun 2 13:44:16 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 3 Jun 2016 03:44:16 +1000 Subject: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop In-Reply-To: References: Message-ID: <20160602174416.GQ12028@ando.pearwood.info> On Thu, Jun 02, 2016 at 06:05:43PM +0100, Olaoluwa Thomas wrote: > fname = raw_input('Enter file name:\n') > try: > fhand = open(fname) > except: > print 'File cannot be found or opened:', fname > exit() > lst = list() > for line in fhand: > words = line.split() > #print words (this was a test that a portion of my code was working) > lst.append(words) If you printed words, you should have seen that it was a list. If you append a list to a list, what do you get? At the interactive prompt, try it: py> L = [1, 2, 3] py> L.append([4, 5, 6]) py> L [1, 2, 3, [4, 5, 6]] Append takes a single argument, and adds it *unchanged* to the end of the list. What you want is the extend method. It takes a list as argument, and appends each item individually: py> L.extend([7, 8, 9]) py> L [1, 2, 3, [4, 5, 6], 7, 8, 9] But if you didn't know about that, you could have done it the old-fashioned way: lst = list() for line in fhand: words = line.split() for word in words: lst.append(word) -- Steve From thomasolaoluwa at gmail.com Thu Jun 2 14:19:38 2016 From: thomasolaoluwa at gmail.com (Olaoluwa Thomas) Date: Thu, 2 Jun 2016 19:19:38 +0100 Subject: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop In-Reply-To: <20160602174416.GQ12028@ando.pearwood.info> References: <20160602174416.GQ12028@ando.pearwood.info> Message-ID: Thanks, everyone, for your help. The objective was to extract all words from each line and place them in a list IF they didn't already exist in it. I sorted it out by adding little bits of everyone's suggestions. Here's the code that fixed it. fname = raw_input('Enter file name:\n') try: fhand = open(fname) except: print 'File cannot be found or opened:', fname exit() lst = list() for line in fhand: words = line.split() for word in words: if word in lst: continue else: lst.append(word) lst.sort() print lst I named the file WordExtract.py since it does just that. :) *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* On Thu, Jun 2, 2016 at 6:44 PM, Steven D'Aprano wrote: > On Thu, Jun 02, 2016 at 06:05:43PM +0100, Olaoluwa Thomas wrote: > > > fname = raw_input('Enter file name:\n') > > try: > > fhand = open(fname) > > except: > > print 'File cannot be found or opened:', fname > > exit() > > lst = list() > > for line in fhand: > > words = line.split() > > #print words (this was a test that a portion of my code was working) > > lst.append(words) > > If you printed words, you should have seen that it was a list. > > If you append a list to a list, what do you get? At the interactive > prompt, try it: > > > py> L = [1, 2, 3] > py> L.append([4, 5, 6]) > py> L > [1, 2, 3, [4, 5, 6]] > > > Append takes a single argument, and adds it *unchanged* to the end of > the list. > > What you want is the extend method. It takes a list as argument, and > appends each item individually: > > > py> L.extend([7, 8, 9]) > py> L > [1, 2, 3, [4, 5, 6], 7, 8, 9] > > > But if you didn't know about that, you could have done it the > old-fashioned way: > > lst = list() > for line in fhand: > words = line.split() > for word in words: > lst.append(word) > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From oscar.j.benjamin at gmail.com Thu Jun 2 17:31:04 2016 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 2 Jun 2016 22:31:04 +0100 Subject: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop In-Reply-To: References: <20160602174416.GQ12028@ando.pearwood.info> Message-ID: On 2 June 2016 at 19:19, Olaoluwa Thomas wrote: > Thanks, everyone, for your help. > > The objective was to extract all words from each line and place them in a > list IF they didn't already exist in it. > I sorted it out by adding little bits of everyone's suggestions. Well done. It looks like you have it working now which is good. Now that you've told us about the extra bit that you only wanted to store *unique* words I thought I'd tell you about Python's set data type. A set is a container similar to a list but different. You can create a set by using curly brackets {} rather than square brackets [] for a list e.g.: >>> myset = {3,2,6} >>> myset set([2, 3, 6]) Notice first that when we print the set out it doesn't print the elements in the same order that we put them in. This is because a set doesn't care about the order of the elements. Also a set only ever stores one copy of each item that you add so >>> myset.add(-1) >>> myset set([2, 3, -1, 6]) >>> myset.add(6) >>> myset set([2, 3, -1, 6]) The add method adds an element but when we add an element that's already in the set it doesn't get added a second time: a set only stores unique elements (which is what you want to do). So you wrote: > lst = list() > for line in fhand: > words = line.split() > for word in words: > if word in lst: > continue > else: > lst.append(word) > lst.sort() > print lst Using a set we could instead write: unique_words = set() for line in fhand: words = line.split() for word in words: unique_words.add(word) lst = sorted(unique_words) print lst This is made simpler because we didn't need to check if the word was already in the set (the set.add method takes care of this for us). However since a set doesn't have an "order" it doesn't have a sort method. If we want a sorted list we can use the sorted function to get that from the set. Also there is a set method "update" which can add many elements at once given e.g. a list like words so we can do: unique_words = set() for line in fhand: words = line.split() unique_words.update(words) lst = sorted(unique_words) print lst I've mentioned two big differences between a set and a list: a set is unordered and only stores unique elements. There is another significant difference which is about how long it takes the computer to perform certain operations with sets vs lists. In particular when we do this if word in lst: continue else: lst.append(word) Testing if word is "in" a list can take a lot longer than testing if it is "in" a set. If you need to test many times whether different objects are "in" a list then it can often make your program a lot slower than if you used a set. You can understand this intuitively by thinking that "if word in lst" requires the computer to loop through all items of the list comparing them with word. With sets the computer has a cleverer way of doing this that is much faster when the set/list is large (look up hash tables if you're interested). -- Oscar From ahall at autodist.com Thu Jun 2 13:41:23 2016 From: ahall at autodist.com (Alex Hall) Date: Thu, 2 Jun 2016 13:41:23 -0400 Subject: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop In-Reply-To: References: Message-ID: Use lst.extend() instead of lst.append() and you should get what you're after. On Thu, Jun 2, 2016 at 1:05 PM, Olaoluwa Thomas wrote: > Hi Tutor, > > I'm trying to parse words in a file line by line and place all words into > another list but I keep getting a list with nested lists. > I would normally pore over it and go to google and fix my problems but this > one escapes me and frankly, I'm tired of being stuck in the same place for > almost a week. > > Here's the code: > fname = raw_input('Enter file name:\n') > try: > fhand = open(fname) > except: > print 'File cannot be found or opened:', fname > exit() > lst = list() > for line in fhand: > words = line.split() > #print words (this was a test that a portion of my code was working) > lst.append(words) > print lst > > A text file with the following contents > "But soft > what light through yonder window breaks > It is the east and Juliet is the sun > Arise fair sun and kill the envious moon > Who is already sick and pale with grief" > > would give me the output in the attached screenshot > [image: Inline image 2] > > whereas I want only one list containing strings not nested lists. > > Any help would be appreciated. > > *Warm regards,* > > *Olaoluwa O. Thomas,* > *+2347068392705* > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From andrycolt007 at gmail.com Thu Jun 2 16:43:37 2016 From: andrycolt007 at gmail.com (Andrei Colta) Date: Thu, 2 Jun 2016 23:43:37 +0300 Subject: [Tutor] Practice Exercises for Beginner ? Message-ID: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> Hi, Anyone can recommend practical work on learning python.. seems reading and reading does not helping. Thanks in advance, Andrei From magyar1886 at gmail.com Thu Jun 2 16:01:16 2016 From: magyar1886 at gmail.com (Marco Soldavini) Date: Thu, 2 Jun 2016 22:01:16 +0200 Subject: [Tutor] Tkinter and threading In-Reply-To: References: Message-ID: Thanks for you answers! On Thu, Jun 2, 2016 at 6:39 PM, Peter Otten <__peter__ at web.de> wrote: >> For example a state machine with a var state which can have some >> discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE) >> and a text element on the gui that reports that state. >> >> So a button cause a transition > the python loop (not the gui loop) >> detects the transition and changes the state var value > the gui >> refresh its value on screen. >> >> I wrote some code to try it without threading but it does not give the >> expected result as it seems the button update status action is already >> triggered. I am missing some basic point here > > Indeed your problem has nothing to do with threads. >> Yes i know it could be done without threads, but for study reason i want to separate the logic from representation (the HMI). That's how usually program industrial machines. HMI serves only to display and change variables, not to execute logic. I want to build a mockup of a machine I saw at work, with statuses and alarms. From robertvstepp at gmail.com Thu Jun 2 23:45:31 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 2 Jun 2016 22:45:31 -0500 Subject: [Tutor] Practice Exercises for Beginner ? In-Reply-To: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> References: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> Message-ID: On Thu, Jun 2, 2016 at 3:43 PM, Andrei Colta wrote: > Anyone can recommend practical work on learning python.. seems reading and reading does not helping. The usual advice: Find one or more projects that interest you and start trying to accomplish them, learning more Python along the way as you find the need. Surely when you started studying Python you had some hopes and goals towards which to apply your new knowledge? Go for it! Even if you don't know enough to do the full project you envision, you can start working on a piece of the puzzle. Say your goal is to write the greatest ever Dungeons and Dragons adventure game. That may be way too big for your current knowledge and skills, but I bet you could start in on some of it. For instance, you know you will have to have the program simulate the rolling of dice. Surely that would be doable? So write the needed function(s) that would accomplish that piece. Once accomplished, find a new piece you can work on. Etc. The idea is if you are interested in the project up front, then you will be motivated to follow it through to its happy completion, learning tons of Python and general programming skills along the way! Good luck on your journeys! -- boB From ahall at autodist.com Thu Jun 2 21:09:20 2016 From: ahall at autodist.com (Alex Hall) Date: Thu, 2 Jun 2016 21:09:20 -0400 Subject: [Tutor] Practice Exercises for Beginner ? In-Reply-To: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> References: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> Message-ID: Make something. :) I know it's pretty open-ended, but just think of an application you want to make, and try to make it in Python. Stick to command line at first, but then try WX or another GUI library. The more times you get stuck, the more you learn and the more you'll know for next time. Make Battleship, make a website in Flask or Django, make a unit converter, make chess, make an audio player... Find a functionality you know, and try to make Python do that function. That's really the best advice I've ever gotten for learning any new language. > On Jun 2, 2016, at 16:43, Andrei Colta wrote: > > Hi, > > Anyone can recommend practical work on learning python.. seems reading and reading does not helping. > > Thanks in advance, > Andrei > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Fri Jun 3 04:14:43 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 3 Jun 2016 09:14:43 +0100 Subject: [Tutor] Practice Exercises for Beginner ? In-Reply-To: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> References: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> Message-ID: On 02/06/16 21:43, Andrei Colta wrote: > Hi, > > Anyone can recommend practical work on learning python.. seems reading and reading does not helping. Other have recommended starting a project and that's always the best way. But if you are really stuck for ideas try the Python Challenge. It's a game where you solve puzzles by writing Python code and each solution gives you the url for the next puzzle. It's a great way to learn about the standard library. http://www.pythonchallenge.com/ -- 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 paradox at pobox.com Fri Jun 3 05:55:13 2016 From: paradox at pobox.com (Thomas C. Hicks) Date: Fri, 3 Jun 2016 17:55:13 +0800 Subject: [Tutor] Practice Exercises for Beginner ? :p: In-Reply-To: References: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> Message-ID: <51bc104e-0d77-424e-1b5b-c54e34427968@pobox.com> On Jun 2, 2016, at 16:43, Andrei Colta wrote: >> Hi, >> >> Anyone can recommend practical work on learning python.. seems reading and reading does not helping. >> >> Thanks in advance, >> Andrei >> I would echo those saying "make something" - for me the thing that really moved me forward was doing a data project, make the sqlite database, manipulate it, make a Flask site to access it, etc. Lots of resources on the web to guide you to do such things. Alternatively try some of the many coding challenge sites. My daughter is currently loving CodeAbbey. A trick to CodeAbbey - there are lots of tools in Python that make the challenges vanishingly easy (at least the early ones) but you will likely learn more if you force yourself to write those programs without Python's "batteries included" tools. SDG, tom From andrycolt007 at gmail.com Fri Jun 3 04:19:56 2016 From: andrycolt007 at gmail.com (Andrei Colta) Date: Fri, 3 Jun 2016 11:19:56 +0300 Subject: [Tutor] Practice Exercises for Beginner ? In-Reply-To: References: <4FE770AA-23FF-4A3F-962E-746610AB5944@gmail.com> Message-ID: <686A7B69-0FBE-4181-9CA2-B782D523F986@gmail.com> hi guys, Thanks all for input, if anything else.. please let me know. cheers, Andrei > On 03 Jun 2016, at 11:14, Alan Gauld via Tutor wrote: > > On 02/06/16 21:43, Andrei Colta wrote: >> Hi, >> >> Anyone can recommend practical work on learning python.. seems reading and reading does not helping. > > Other have recommended starting a project and that's always the best way. > > But if you are really stuck for ideas try the Python Challenge. > It's a game where you solve puzzles by writing Python code and > each solution gives you the url for the next puzzle. It's a great > way to learn about the standard library. > > http://www.pythonchallenge.com/ > > -- > 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 lighttoshow at yahoo.com Fri Jun 3 12:18:42 2016 From: lighttoshow at yahoo.com (Christian Carbone) Date: Fri, 3 Jun 2016 16:18:42 +0000 (UTC) Subject: [Tutor] Desperately need help to uninstall python from my mac References: <1749640999.753699.1464970722296.JavaMail.yahoo.ref@mail.yahoo.com> Message-ID: <1749640999.753699.1464970722296.JavaMail.yahoo@mail.yahoo.com> Hello, As the subject line indicates, I really need help uninstalling python on my mac.. Uninstalling python from a mac seems to be an absurdly overlooked topic. The one resource i have found addressing the issue, a stack overflow thread, doesn't give me the amount of direction i need to successfully uninstall. The reason i (think i) need to uninstall python is that when i open IDLE i get this warning: "WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.Visit http://www.python.org/download/mac/tcltk/ for current information.". I've heard i need to install a system appropriate tcl/tk (which i believe i have done) before reinstalling python, but seeing as i cannot uninstall python I do not see any way to resolve this issue. Python.org doesn't seem to have any information that addresses this problem, so I have come here in hopes of some guidance.? Thanks,-Christian From alan.gauld at yahoo.co.uk Fri Jun 3 15:10:24 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 3 Jun 2016 20:10:24 +0100 Subject: [Tutor] Desperately need help to uninstall python from my mac In-Reply-To: <1749640999.753699.1464970722296.JavaMail.yahoo@mail.yahoo.com> References: <1749640999.753699.1464970722296.JavaMail.yahoo.ref@mail.yahoo.com> <1749640999.753699.1464970722296.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 03/06/16 17:18, Christian Carbone via Tutor wrote: > As the subject line indicates, I really need help uninstalling python on my mac.. You almost certainly don;t want to to do that! Your Mac will stop working properly, it uses Python. That's why its installed by default on Macs. > The reason i (think i) need to uninstall python is that when i open > IDLE i get this warning: > "WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable. OK, So you have IDLE which is not ion a Mac by default so maybe you installed an extra version of Python and you want to uninstall that? Thats a very different thing that uninstalling Python entirely. But the warning only says the version of Tcl/Tk *may* be unstable. Does IDLE in fact work? If it doesn't can you replace the Tcl/Tk libraries rather than Python? > Visit http://www.python.org/download/mac/tcltk/ for current information.". Did you read that page? Did you do what it suggested and install an ActiveState version of Tcl/Tk for MAC? Which versions of MacOS and Python are you using? > I've heard i need to install a system appropriate tcl/tk (which i > believe i have done) before reinstalling python, What makes you believe it? Which version did you install? Where did you get it? > but seeing as i cannot uninstall python I do not see any way > to resolve this issue. Once we understand which version of Python you installed we can start to consider whether/how you need to uninstall it. What you should not do is try to uninstall the system version of Python used by MacOS. > Python.org doesn't seem to have any information that addresses > this problem, so I have come here in hopes of some guidance. Did you read this page: https://docs.python.org/3/using/mac.html It doesn't explicitly tell you how to uninstall it, but it does tell you what is installed, and where... -- 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 dirkjsoren at gmail.com Fri Jun 3 16:01:04 2016 From: dirkjsoren at gmail.com (DirkJSoren@gmail.com) Date: Fri, 3 Jun 2016 14:01:04 -0600 Subject: [Tutor] subprocess module seems to be failing... Message-ID: <5751E200.50203@gmail.com> Running Linux Mint 17.3 Python3.4.3 I'm following a Tutor concerning the subprocess module which said to open a Terminal SHELL and type: $ python3 >>> import subprocess >>> subprocess.call('ls', shell=True) Last night the subprocess.call portion was erroring saying their was no 'call' attribute. I tried asking dir(subprocess) and call wasn't listed there. I figured I would try again this morning, however, this is what I was greeted with shortly after booting up and launching my Terminal: [justme at ispy] ~$ python3 Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> import subprocess Traceback (most recent call last): File "", line 1, in File "/home/justme/python_work/subprocess.py", line 39 ^ SyntaxError: EOF while scanning triple-quoted string literal >>> Seems like the module is deteriorating or something, as yesterday it accepted the import command to include it! :) I found a subprocess.py in /usr/lib/python3.4 and grabbed it with gedit and looked at it line by line. It has tripple double quotes but also single double quotes throughout it in places, but they all seemed to have a companion quote to offset them...and the entire text area is one solid color which would seem to indicate it is being ignored by python... What should I do now? Thanks! From __peter__ at web.de Fri Jun 3 16:30:18 2016 From: __peter__ at web.de (Peter Otten) Date: Fri, 03 Jun 2016 22:30:18 +0200 Subject: [Tutor] subprocess module seems to be failing... References: <5751E200.50203@gmail.com> Message-ID: DirkJSoren at gmail.com wrote: > Running Linux Mint 17.3 > Python3.4.3 > > I'm following a Tutor concerning the subprocess module which said to > open a Terminal SHELL and type: > > $ python3 > >>> import subprocess > >>> subprocess.call('ls', shell=True) > > Last night the subprocess.call portion was erroring saying their was no > 'call' attribute. I tried asking > dir(subprocess) and call wasn't listed there. I figured I would try > again this morning, however, this > is what I was greeted with shortly after booting up and launching my > Terminal: > > [justme at ispy] ~$ python3 > Python 3.4.3 (default, Oct 14 2015, 20:28:29) > [GCC 4.8.4] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> > >>> import subprocess > Traceback (most recent call last): > File "", line 1, in > File "/home/justme/python_work/subprocess.py", line 39 Look carefully at the line above. You wrote a script and called it subprocess.py. Bad idea ;) Delete or rename your subprocess.py, and the one in the standard library will become visible again and work as smoothly as ever. > > ^ > SyntaxError: EOF while scanning triple-quoted string literal > >>> > > Seems like the module is deteriorating or something, as yesterday it > accepted the import command > to include it! :) > > I found a subprocess.py in /usr/lib/python3.4 and grabbed it with gedit > and looked at it line by line. It has tripple double quotes but also > single double quotes throughout it in places, but they all seemed to > have a companion quote to offset them...and the entire text area is one > solid color which would seem to indicate > it is being ignored by python... > > What should I do now? > > Thanks! > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From info at riesrommens.nl Fri Jun 3 14:55:03 2016 From: info at riesrommens.nl (Ries Rommens) Date: Fri, 3 Jun 2016 20:55:03 +0200 Subject: [Tutor] Desperately need help to uninstall python from my mac In-Reply-To: <1749640999.753699.1464970722296.JavaMail.yahoo@mail.yahoo.com> References: <1749640999.753699.1464970722296.JavaMail.yahoo.ref@mail.yahoo.com> <1749640999.753699.1464970722296.JavaMail.yahoo@mail.yahoo.com> Message-ID: <3c9aa9a9-5be4-a5d2-3e7c-33d6260e6fd0@riesrommens.nl> Hello Christian, You don't have to uninstall python before upgrading Tcl/Tk. Steps to follow: Close Idle, if activated. Browse to http://www.activestate.com/activetcl/downloads and find your Mac Disk Image (DMG), it's about halfway the webpage. I have used version 8.5.18.0. Download and install the DMG in the Apple way. (Right click on the icon, and click [Open], instead of doubleclick). Activate Idle after installing Tcl/Tk, and the warning has gone. Hope it helps, Ries Op 3-6-2016 om 18:18 schreef Christian Carbone via Tutor: > Hello, > As the subject line indicates, I really need help uninstalling python on my mac.. > Uninstalling python from a mac seems to be an absurdly overlooked topic. The one resource i have found addressing the issue, a stack overflow thread, doesn't give me the amount of direction i need to successfully uninstall. > > The reason i (think i) need to uninstall python is that when i open IDLE i get this warning: "WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.Visit http://www.python.org/download/mac/tcltk/ for current information.". > > I've heard i need to install a system appropriate tcl/tk (which i believe i have done) before reinstalling python, but seeing as i cannot uninstall python I do not see any way to resolve this issue. Python.org doesn't seem to have any information that addresses this problem, so I have come here in hopes of some guidance. > > Thanks,-Christian > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From dirkjsoren at gmail.com Fri Jun 3 17:57:36 2016 From: dirkjsoren at gmail.com (DirkJSoren@gmail.com) Date: Fri, 3 Jun 2016 15:57:36 -0600 Subject: [Tutor] subprocess module seems to be failing... In-Reply-To: References: <5751E200.50203@gmail.com> Message-ID: <5751FD50.1040801@gmail.com> Add sound of me beating my head with a bat! :) Thanks Peter! -I was a bit too precise in my naming conventions for later review! On 06/03/2016 02:30 PM, Peter Otten wrote: > DirkJSoren at gmail.com wrote: > >> Running Linux Mint 17.3 >> Python3.4.3 >> >> I'm following a Tutor concerning the subprocess module which said to >> open a Terminal SHELL and type: >> >> $ python3 >> >>> import subprocess >> >>> subprocess.call('ls', shell=True) >> >> Last night the subprocess.call portion was erroring saying their was no >> 'call' attribute. I tried asking >> dir(subprocess) and call wasn't listed there. I figured I would try >> again this morning, however, this >> is what I was greeted with shortly after booting up and launching my >> Terminal: >> >> [justme at ispy] ~$ python3 >> Python 3.4.3 (default, Oct 14 2015, 20:28:29) >> [GCC 4.8.4] on linux >> Type "help", "copyright", "credits" or "license" for more information. >> >>> >> >>> import subprocess >> Traceback (most recent call last): >> File "", line 1, in >> File "/home/justme/python_work/subprocess.py", line 39 > Look carefully at the line above. You wrote a script and called it > subprocess.py. Bad idea ;) > > Delete or rename your subprocess.py, and the one in the standard library > will become visible again and work as smoothly as ever. > >> ^ >> SyntaxError: EOF while scanning triple-quoted string literal >> >>> >> >> Seems like the module is deteriorating or something, as yesterday it >> accepted the import command >> to include it! :) >> >> I found a subprocess.py in /usr/lib/python3.4 and grabbed it with gedit >> and looked at it line by line. It has tripple double quotes but also >> single double quotes throughout it in places, but they all seemed to >> have a companion quote to offset them...and the entire text area is one >> solid color which would seem to indicate >> it is being ignored by python... >> >> What should I do now? >> >> Thanks! >> >> >> _______________________________________________ >> 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 unclesudz at gmail.com Fri Jun 3 21:01:48 2016 From: unclesudz at gmail.com (Alan Outhier) Date: Fri, 3 Jun 2016 18:01:48 -0700 Subject: [Tutor] Cmd line not correctly interpreted Message-ID: I'm working on a Python script to call "sox" to convert ;ogg files to .mp3s. In the following segment...: *1 outname=fname+".mp3"2 fname=ReSpace(fname) # substitute " " with "\ "3 outname=ReSpace(outname)4 cmdline="sox " + fname + " " +outname5 print cmdline6 rtncode=os.system(cmdline)7 if rtncode <> 0:8 print "Bad return code (" + str(rtncode) + ") from sox command"9 sys.exit()* ...I DO get the error trap (every time). Line 5 causes the following (for example) to be printed: *sox Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ the\ Blues Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ the\ Blues.mp3* *Bad return code (512) from sox command * I can however cop-past that output to bash and it works fine. I get similar (but *as expected* more complicated problems if I comment out lines 2 & 3. Please help! I'll send the whole program if requested. Many Thanks, Al From alan.gauld at yahoo.co.uk Sat Jun 4 04:05:17 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 4 Jun 2016 09:05:17 +0100 Subject: [Tutor] Cmd line not correctly interpreted In-Reply-To: References: Message-ID: On 04/06/16 02:01, Alan Outhier wrote: > I'm working on a Python script to call "sox" to convert ;ogg files to .mp3s. 1 outname=fname+".mp3" 2 fname=ReSpace(fname) # substitute " " with "\ " 3 outname=ReSpace(outname) 4 cmdline="sox " + fname + " " +outname 5 print cmdline 6 rtncode=os.system(cmdline) 7 if rtncode <> 0: 8 print "Bad return code (" + str(rtncode) + ") from sox command" 9 sys.exit()* > ...I DO get the error trap (every time). Line 5 causes the following (for > example) to be printed: > > *sox Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ the\ > Blues Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ > the\ Blues.mp3* > *Bad return code (512) from sox command * > I can however cop-past that output to bash and it works fine. It might be easier to use the subprocess call() function and pass the filenames in that way. Also you could try upping the verbosity of sox so it tells you more about the error. I'd be suspicious of the filename format and try using hard coded strings in raw format first: outname = r"Carnegie Hall Jazz Band/Carnegie Hall Jazz Band/Frame for the Blues.mp3" If that works it suggests your formatting function is not doing what it should. Another thing to try is to eliminate the complex filenames entirely by copying the file to foo.ogg in the local directory. See if that works. Then try a name with spaces in the current directory. Then a directory name without spaces. etc etc. By a process of elimination you can find out where things start to fall over. > Please help! I'll send the whole program if requested. The ReSpace() function might be helpful. But please send using plain text. Trying to reconstruct Python code as I did above is extremely error prone. -- 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 Sat Jun 4 04:13:44 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 4 Jun 2016 18:13:44 +1000 Subject: [Tutor] Cmd line not correctly interpreted In-Reply-To: References: Message-ID: <20160604081344.GX12028@ando.pearwood.info> On Fri, Jun 03, 2016 at 06:01:48PM -0700, Alan Outhier wrote: > I'm working on a Python script to call "sox" to convert ;ogg files to .mp3s. > > In the following segment...: > > *1 outname=fname+".mp3"2 fname=ReSpace(fname) # substitute " " with > "\ "3 outname=ReSpace(outname)4 cmdline="sox " + fname + " " > +outname5 print cmdline6 rtncode=os.system(cmdline)7 if rtncode <> > 0:8 print "Bad return code (" + str(rtncode) + ") from sox > command"9 sys.exit()* Your code is a mess. Try sending plain text instead of "rich text". When you send rich text, your mail client will mangle the line breaks and put them where ever it sees fit. Also, there's no need for line numbers. Especially not when there are only nine lines. > ...I DO get the error trap (every time). Line 5 causes the following (for > example) to be printed: > > > *sox Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ the\ > Blues Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ > the\ Blues.mp3* > *Bad return code (512) from sox command * According to the sox man page, it will only return 0, 1 or 2, not 512: Exit status is 0 for no error, 1 if there is a problem with the command-line parameters, or 2 if an error occurs during file processing. http://sox.sourceforge.net/sox.html#DIAGNOSTICS but I think that is a lie, as I can reproduce your error, without even using spaces: py> os.system("sox ab cd") sox: Can't open input file 'ab': No such file or directory 512 I'm surprised that you don't see the error message printed by sox. Are you redirecting stderr to a file or something? If so, please check the file. You are using a relative path starting with "Carnegie\ Hall...". You probably should use an absolute path. -- Steve From __peter__ at web.de Sat Jun 4 04:20:00 2016 From: __peter__ at web.de (Peter Otten) Date: Sat, 04 Jun 2016 10:20 +0200 Subject: [Tutor] Cmd line not correctly interpreted References: Message-ID: Alan Outhier wrote: > I'm working on a Python script to call "sox" to convert ;ogg files to > .mp3s. > > In the following segment...: To avoid garbled code as seen below please ensure that you post plain text. Thank you. > > *1 outname=fname+".mp3"2 fname=ReSpace(fname) # substitute " " > with > "\ "3 outname=ReSpace(outname)4 cmdline="sox " + fname + " " > +outname5 print cmdline6 rtncode=os.system(cmdline)7 if rtncode > <> > 0:8 print "Bad return code (" + str(rtncode) + ") from sox > command"9 sys.exit()* > > ...I DO get the error trap (every time). Line 5 causes the following (for > example) to be printed: > > > *sox Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ > the\ Blues Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ > for\ the\ Blues.mp3* > *Bad return code (512) from sox command * > > I can however cop-past that output to bash and it works fine. > > I get similar (but *as expected* more complicated problems if I comment > out lines 2 & 3. > > Please help! I'll send the whole program if requested. The best approach is to use the subprocess module and let Python do the work for you. For example import subprocess fname = ... # may contain spaces outname = fname + ".mp3" # Pass arguments as a list, no need to use a shell: subprocess.check_output(["sox", fname, outname]) If the sox invocation fails check_output will raise a CalledProcessError exception that you can examine for details, print or just let bubble up. From vincent.trost at yahoo.com Tue Jun 7 22:24:36 2016 From: vincent.trost at yahoo.com (Vincent Trost) Date: Wed, 8 Jun 2016 02:24:36 +0000 (UTC) Subject: [Tutor] Python Setup Help References: <1079152595.132133.1465352676067.JavaMail.yahoo.ref@mail.yahoo.com> Message-ID: <1079152595.132133.1465352676067.JavaMail.yahoo@mail.yahoo.com> Hi, I'm looking for help on the proper installation of Python and the Text editor, Atom. I'm a Data Science major at Penn State, and I knew I had Python 2.7.x already installed on my computer, but last semester in one of my classes we downloaded Enthought Canopy and used that for some scientific Python work. Time went by, and now it's summer and I've struck up an interest in web-scraping, but for the life of me I can't figure out how to set up a solid environment for Python 3.5.x on my computer. I've tried so many things that it's just become kind of a mess. I have a number of questions and problems with my installation that I'm sure all it would take is for someone who actually understands the terminal, installation process, and just how all this works to come and get me straightened out. If anyone would be willing to help me, I'd greatly appreciate it!! Thank you,Vince Trost From alan.gauld at yahoo.co.uk Wed Jun 8 04:22:04 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 8 Jun 2016 09:22:04 +0100 Subject: [Tutor] Python Setup Help In-Reply-To: <1079152595.132133.1465352676067.JavaMail.yahoo@mail.yahoo.com> References: <1079152595.132133.1465352676067.JavaMail.yahoo.ref@mail.yahoo.com> <1079152595.132133.1465352676067.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 08/06/16 03:24, Vincent Trost via Tutor wrote: > Hi, > I'm looking for help on the proper installation of Python and the Text editor, Atom. OK, Lets break those into two separate activities. Start with Python. > we downloaded Enthought Canopy and used that for some scientific Python work. Do you still have Canopy installed? Which version of Python was it? Do you need anything more recent than that? If not stick with Canopy. If you no longer need/want it you should be able to uninstall Canopy. > I've struck up an interest in web-scraping, but for the life of me > I can't figure out how to set up a solid environment for Python 3.5.x You don't need 3.5 to do web scraping. In fact your 2.7 install would be fine for that. > I've tried so many things that it's just become kind of a mess. We need a lot more specific details before we can offer concrete help. - Which OS are you using? - What are these "many things" that you've tried? (Which packages? From which site?) - What exactly is the "mess" that you have? - Do you get a working Python 3.5 prompt? - What do you actually need beyond a basic vanilla Python installation? (I'd suggest Beautiful Soup as a minimum extra for web scraping, but are there other packages you think you need? > I have a number of questions and problems with my installation that > I'm sure all it would take is for someone who actually understands > the terminal, installation process, and just how all this works > to come and get me straightened out. We don't really do home visits, it's a bit tricky when we are scattered all over the planet... Penn State is a long way from Scotland! But if you tell us what you are doing (in much more detail) we can hopefully offer some advice. Once you have Python installed we can think about installing Atom. BTW. Is there any special reason you want to use Atom rather than one of the more common editors? Have you used it before 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 ahall at autodist.com Wed Jun 8 09:54:23 2016 From: ahall at autodist.com (Alex Hall) Date: Wed, 8 Jun 2016 09:54:23 -0400 Subject: [Tutor] Urgent: unicode problems writing CSV file Message-ID: All, I'm working on a project that writes CSV files, and I have to get it done very soon. I've done this before, but I'm suddenly hitting a problem with unicode conversions. I'm trying to write data, but getting the standard cannot encode character: ordinal not in range(128) I've tried str(info).encode("utf8") str(info).decode(utf8") unicode(info, "utf8") csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword argument What else can I do? As I said, I really have to get this working soon, but I'm stuck on this stupid unicode thing. Any ideas will be great. Thanks. -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From __peter__ at web.de Wed Jun 8 13:08:07 2016 From: __peter__ at web.de (Peter Otten) Date: Wed, 08 Jun 2016 19:08:07 +0200 Subject: [Tutor] Urgent: unicode problems writing CSV file References: Message-ID: Alex Hall wrote: Marking your posts is generally considered impolite and will not help you get answers sooner than without it. > I'm working on a project that writes CSV files, and I have to get it done > very soon. I've done this before, but I'm suddenly hitting a problem with > unicode conversions. I'm trying to write data, but getting the standard > cannot encode character: ordinal not in range(128) > > I've tried > str(info).encode("utf8") > str(info).decode(utf8") > unicode(info, "utf8") > csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword > argument > > What else can I do? As I said, I really have to get this working soon, but > I'm stuck on this stupid unicode thing. Any ideas will be great. Thanks. What's the type of "info"? From joel.goldstick at gmail.com Wed Jun 8 13:12:56 2016 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 8 Jun 2016 13:12:56 -0400 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: References: Message-ID: On Wed, Jun 8, 2016 at 1:08 PM, Peter Otten <__peter__ at web.de> wrote: > Alex Hall wrote: > > Marking your posts is generally considered impolite and will not help you > get answers sooner than without it. > >> I'm working on a project that writes CSV files, and I have to get it done >> very soon. I've done this before, but I'm suddenly hitting a problem with >> unicode conversions. I'm trying to write data, but getting the standard >> cannot encode character: ordinal not in range(128) >> >> I've tried >> str(info).encode("utf8") >> str(info).decode(utf8") >> unicode(info, "utf8") >> csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword >> argument >> Can you show a small piece of code that you run with the full traceback. Show what your data to be printed looks like >> What else can I do? As I said, I really have to get this working soon, but >> I'm stuck on this stupid unicode thing. Any ideas will be great. Thanks. > > What's the type of "info"? > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From mail at timgolden.me.uk Wed Jun 8 13:19:33 2016 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 8 Jun 2016 18:19:33 +0100 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: References: Message-ID: <575853A5.7020204@timgolden.me.uk> On 08/06/2016 14:54, Alex Hall wrote: > All, > I'm working on a project that writes CSV files, and I have to get it done > very soon. I've done this before, but I'm suddenly hitting a problem with > unicode conversions. I'm trying to write data, but getting the standard > cannot encode character: ordinal not in range(128) > > I've tried > str(info).encode("utf8") > str(info).decode(utf8") > unicode(info, "utf8") > csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword > argument > > What else can I do? As I said, I really have to get this working soon, but > I'm stuck on this stupid unicode thing. Any ideas will be great. Thanks. > This is a little tricky. I assume that you're on Python 2.x (since open() isn't taking an encoding). Deep in the bowels of the CSV module's C implmentation is code which converts every item in the row it's receiving to a string. (Essentially does: [str(x) for x in row]). Which will assume ascii: there's no opportunity to specify an encoding. For things whose __str__ returns something ascii-ish, that's fine. But if your data does or is likely to contain non-ascii data, you'll need to preprocess it. How you do it, and how general-purpose that approach is will depend on your data. For the purposes of discussion, let's assume your data looks like this: unicode, int, int Then your encoder could do this: def encoder_of_rows(row): return [row[0].encode("utf-8"), str(row[1]), str(row[2])] and your csv processor could do this: rows = [...] with open("filename.csv", "wb") as f: writer = csv.writer(f) writer.writerows([encoder_of_rows(row) for row in rows]) but if could be more (or less) complex than that depending on your data and how much you know about it. TJG From steve at pearwood.info Wed Jun 8 13:43:29 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 9 Jun 2016 03:43:29 +1000 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: References: Message-ID: <20160608174329.GN12028@ando.pearwood.info> On Wed, Jun 08, 2016 at 09:54:23AM -0400, Alex Hall wrote: > All, > I'm working on a project that writes CSV files, and I have to get it done > very soon. I've done this before, but I'm suddenly hitting a problem with > unicode conversions. I'm trying to write data, but getting the standard > cannot encode character: ordinal not in range(128) I infer from your error that you are using Python 2. Is that right? You should say so, *especially* for Unicode problems, because Python 3 uses a very different (and much better) system for handling text strings. Also, there is no such thing as a "standard" error. All error messages are different, and they usually show lots of debugging information that you haven't yet learned to read. But we have, so please show us the full traceback! > I've tried > str(info).encode("utf8") > str(info).decode(utf8") One of the problems with Python 2 is that it allows two nonsense operations: str.encode and unicode.decode. The whole string handling thing in Python 2 is a bit of a mess. It's over 20 years old, and dates back to before Unicode even existed, so you'll have to excuse a bit of confusion. In Python 2: (1) str means *byte string*, NOT text string, and is limited to "chars" with ordinal values 0 to 255; (2) unicode means "text string"; (3) In an attempt to be helpful, Python 2 will try to automatically convert to and from bytes strings as needed. This works so long as all your characters are ASCII, but leads to chaos, confusion and error as soon as you have non-ASCII characters involved. Python 3 fixes these confusing features. Remember two facts: (1) To go from TEXT to BYTES (i.e. unicode -> str) use ENCODE; (2) To go from BYTES to TEXT (i.e. str -> unicode) use DECODE. but you must be careful to prevent Python doing those automatic conversions first. Looking at your code: str(info).encode("utf8") that's wrong, because it tries to go from str->unicode using encode. But using decode also gives the same error. That hints that the error is happening in the call to str() first. Firstly, we need to know what info is. Run this: print type(info) print repr(info) print str(info) and report any errors and output. I'm going to assume that info is a unicode object. Why? Because that will give the error you experience: py> info = u'abc?' py> str(info) Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode character u'\xb5' in position 3: ordinal not in range(128) The right way to convert unicode text to a byte str is with the encode method. Unless you have good reason to use another encoding, always use UTF-8 (which I see you are doing, great). py> info.encode('utf-8') 'abc\xc2\xb5' If all your Unicode text strings are valid and correct, that should be all you need, but if you are paranoid and fear "invalid" Unicode strings, which can theoretically happen (ask me how if you care), you can take a belt-and-braces approach and preemptively deal with errors by converting them to question marks. NOTE THAT THIS THROWS AWAY INFORMATION FROM YOUR UNICODE TEXT. If your paranoia exceeds your fear of losing information, you can instruct Python to use a ? any time there is an encoding error: info.encode('utf-8', errors='replace') So to recap: - you have a variable `info`, which I am guessing is unicode - you can convert it to a byte str with: info.encode('utf-8') or for the paranoid: info.encode('utf-8', errors='replace') Now that you have a byte string, you can just write it out to the CSV file. To read it back in, you read the CSV file, which returns a byte str, and then convert back to Unicode with: info = data.decode('utf-8') > unicode(info, "utf8") When you run this, what exception do you get? My guess is that you get the following TypeError: py> unicode(u'abc', 'utf-8') Traceback (most recent call last): File "", line 1, in TypeError: decoding Unicode is not supported > csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword > argument Python 3 allows you to set the encoding of files, Python 2 doesn't. In Python 2 you can use the io module, but note that this won't help you as (1) the csv module doesn't support Unicode, and (2) your problem lies elsewhere. P.S. don't feel bad if the whole Unicode thing is confusing you. Most people go through a period of confusion, because you have to unlearn nearly everything you thought you knew about text in computers before you can really get Unicode. -- Steve From ahall at autodist.com Wed Jun 8 13:18:11 2016 From: ahall at autodist.com (Alex Hall) Date: Wed, 8 Jun 2016 13:18:11 -0400 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: References: Message-ID: I never knew that specifying a question is related to a time-sensitive project is considered impolite. My apologies. The type of the 'info' variable can vary, as I'm pulling it from a database with Pyodbc. I eventually found something that works, though I'm not fully sure why or how. csvWriter.writerow([info.encode("utf8") if type(info)is unicode else info for info in resultInfo]) where resultInfo is an array holding the values from a row of database query results, in the order I want them in. On Wed, Jun 8, 2016 at 1:08 PM, Peter Otten <__peter__ at web.de> wrote: > Alex Hall wrote: > > Marking your posts is generally considered impolite and will not help you > get answers sooner than without it. > > I'm working on a project that writes CSV files, and I have to get it done > > very soon. I've done this before, but I'm suddenly hitting a problem with > > unicode conversions. I'm trying to write data, but getting the standard > > cannot encode character: ordinal not in range(128) > > > > I've tried > > str(info).encode("utf8") > > str(info).decode(utf8") > > unicode(info, "utf8") > > csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword > > argument > > > > What else can I do? As I said, I really have to get this working soon, > but > > I'm stuck on this stupid unicode thing. Any ideas will be great. Thanks. > > What's the type of "info"? > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From michael.selik at gmail.com Wed Jun 8 13:13:10 2016 From: michael.selik at gmail.com (Michael Selik) Date: Wed, 08 Jun 2016 17:13:10 +0000 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: References: Message-ID: On Wed, Jun 8, 2016 at 12:53 PM Alex Hall wrote: > All, > I'm working on a project that writes CSV files, and I have to get it done > very soon. I've done this before, but I'm suddenly hitting a problem with > unicode conversions. I'm trying to write data, but getting the standard > cannot encode character: ordinal not in range(128) > Have you tried ignoring invalid characters? >>> data = b'\x30\x40\xff\x50' >>> text = data.decode('utf-8') Traceback ... UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff >>> text = data.decode('utf-8', 'ignore') >>> print(text) 0 at P BTW, most programming volunteers don't like responding to things marked "Urgent". It's probably not really urgent unless someone's life is in danger. From steve at pearwood.info Wed Jun 8 13:57:21 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 9 Jun 2016 03:57:21 +1000 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: References: Message-ID: <20160608175721.GO12028@ando.pearwood.info> On Wed, Jun 08, 2016 at 01:18:11PM -0400, Alex Hall wrote: > I never knew that specifying a question is related to a time-sensitive > project is considered impolite. My apologies. Your urgency is not our urgency. We're volunteers offerring our time and experience for free. Whether you intended it or not, labelling the post "urgent" can come across as badgering us: "Hey, answer my question! For free! And do it now, not when it is convenient to you!" I'm sure that wasn't your intention, but that's how it can come across. > The type of the 'info' variable can vary, as I'm pulling it from a > database with Pyodbc. /face-palm Oh vey, that's terrible! Not your fault, but still terrible. > I eventually found something that works, though I'm > not fully sure why or how. > > csvWriter.writerow([info.encode("utf8") if type(info)is unicode else info > for info in resultInfo]) Let's break it up into pieces. You build a list: [blah blah blah for info in resultInfo] then write it to the csv file with writerow. That is straightforward. What's in the list? info.encode("utf8") if type(info)is unicode else info So Python looks at each item, `info`, decides if it is Unicode or not, and if it is Unicode it converts it to a byte str using encode, otherwise leaves it be. If it makes you feel better, this is almost exactly the solution I would have come up with in your shoes, except I'd probably write a helper function to make it a bit less mysterious: def to_str(obj): if isinstance(obj, unicode): return obj.encode('uft-8') elif isinstance(obj, str): return obj else: # Or maybe an error? return repr(obj) csvWriter.writerow([to_string(info) for info in resultInfo]) -- Steve From __peter__ at web.de Wed Jun 8 14:07:28 2016 From: __peter__ at web.de (Peter Otten) Date: Wed, 08 Jun 2016 20:07:28 +0200 Subject: [Tutor] Urgent: unicode problems writing CSV file References: Message-ID: Alex Hall wrote: > The type of the 'info' variable can vary, as I'm pulling it from a > database with Pyodbc. I eventually found something that works, though I'm > not fully sure why or how. As Tim says, the csv.writer in Python 2 applies str() to every value. If that value is a unicode instance this results in value.encode(sys.getdefaultencoding()) In every sanely configured Python installation the default encoding is "ascii", hence the UnicodeEncodeError. If you manually encode for unicode objects (with an encoding that can encode all its codepoints) > csvWriter.writerow([info.encode("utf8") if type(info)is unicode else info > for info in resultInfo]) the str(value) that follows sees a byte string, becomes a noop and will never fail. > where resultInfo is an array holding the values from a row of database > query results, in the order I want them in. From sjeik_appie at hotmail.com Wed Jun 8 15:22:14 2016 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 8 Jun 2016 19:22:14 +0000 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: <20160608175721.GO12028@ando.pearwood.info> References: , , , <20160608175721.GO12028@ando.pearwood.info> Message-ID: > Date: Thu, 9 Jun 2016 03:57:21 +1000 > From: steve at pearwood.info > To: tutor at python.org > Subject: Re: [Tutor] Urgent: unicode problems writing CSV file > > On Wed, Jun 08, 2016 at 01:18:11PM -0400, Alex Hall wrote: >> >> csvWriter.writerow([info.encode("utf8") if type(info)is unicode else info >> for info in resultInfo]) > > Let's break it up into pieces. You build a list: > > [blah blah blah for info in resultInfo] > > then write it to the csv file with writerow. That is straightforward. > > What's in the list? > > info.encode("utf8") if type(info)is unicode else info > > So Python looks at each item, `info`, decides if it is Unicode or not, > and if it is Unicode it converts it to a byte str using encode, > otherwise leaves it be. > > If it makes you feel better, this is almost exactly the solution I would > have come up with in your shoes, except I'd probably write a helper > function to make it a bit less mysterious: > > def to_str(obj): > if isinstance(obj, unicode): > return obj.encode('uft-8') > elif isinstance(obj, str): > return obj > else: > & Or maybe an error? > return repr(obj) > > csvWriter.writerow([to_string(info) for info in resultInfo]) > The docs also offer some code for working with Unicode/CSV, see the bottom of this page: https://docs.python.org/2/library/csv.html From ahall at autodist.com Wed Jun 8 14:12:40 2016 From: ahall at autodist.com (Alex Hall) Date: Wed, 8 Jun 2016 14:12:40 -0400 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: <575853A5.7020204@timgolden.me.uk> References: <575853A5.7020204@timgolden.me.uk> Message-ID: Thanks for all the responses, everyone, what you all said makes sense. I also understand what you mean by the tone of an "urgent" message coming across as demanding. On Wed, Jun 8, 2016 at 1:19 PM, Tim Golden wrote: > On 08/06/2016 14:54, Alex Hall wrote: > > All, > > I'm working on a project that writes CSV files, and I have to get it done > > very soon. I've done this before, but I'm suddenly hitting a problem with > > unicode conversions. I'm trying to write data, but getting the standard > > cannot encode character: ordinal not in range(128) > > > > I've tried > > str(info).encode("utf8") > > str(info).decode(utf8") > > unicode(info, "utf8") > > csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword > > argument > > > > What else can I do? As I said, I really have to get this working soon, > but > > I'm stuck on this stupid unicode thing. Any ideas will be great. Thanks. > > > > This is a little tricky. I assume that you're on Python 2.x (since > open() isn't taking an encoding). Deep in the bowels of the CSV module's > C implmentation is code which converts every item in the row it's > receiving to a string. (Essentially does: [str(x) for x in row]). Which > will assume ascii: there's no opportunity to specify an encoding. > > For things whose __str__ returns something ascii-ish, that's fine. But > if your data does or is likely to contain non-ascii data, you'll need to > preprocess it. How you do it, and how general-purpose that approach is > will depend on your data. For the purposes of discussion, let's assume > your data looks like this: > > unicode, int, int > > Then your encoder could do this: > > def encoder_of_rows(row): > return [row[0].encode("utf-8"), str(row[1]), str(row[2])] > > and your csv processor could do this: > > rows = [...] > with open("filename.csv", "wb") as f: > writer = csv.writer(f) > writer.writerows([encoder_of_rows(row) for row in rows]) > > > but if could be more (or less) complex than that depending on your data > and how much you know about it. > > TJG > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From jjk.saji at gmail.com Thu Jun 9 00:51:59 2016 From: jjk.saji at gmail.com (Joseph John) Date: Thu, 9 Jun 2016 08:51:59 +0400 Subject: [Tutor] import openpyxl throws error in-spite I have python-openpyxl installed. Message-ID: Hi All, I am trying to explore python, trying to read from excel file. I am using Ubuntu 16.04 As a first step I mae sure that I have the package installed ?python-openpyxl? Now in the python command line when I tried to import openpyxl it throws error. >>> import openpyxl Traceback (most recent call last): File "", line 1, in ImportError: No module named openpyxl >>> My environment are OS Ubuntu 14.04 LTS Python 2.7.9 -- 64-bit python-openpyxl version 1.7.0+ds1-1 I am not able to find why the system throws error when I am importing openpyxl, since I have python-openpyxl installed. Like to request guidance and feedback Thanks Joseph John From jjk.saji at gmail.com Thu Jun 9 02:56:31 2016 From: jjk.saji at gmail.com (Joseph John) Date: Thu, 9 Jun 2016 10:56:31 +0400 Subject: [Tutor] import openpyxl throws error in-spite I have python-openpyxl installed. In-Reply-To: References: Message-ID: Like to give the status I have messed up my Python installation, now I remember there was the default python installation from the OS installed and "Canopy" version which I had setup in my home directories. Because of this reason import openpyxl was not working for the shell ( I tested it by logging in as another user) In my my user space,I solved it by giving pip install openpyxl Thanks Joseph John On Thu, Jun 9, 2016 at 8:51 AM, Joseph John wrote: > Hi All, > I am trying to explore python, trying to read from excel file. > I am using Ubuntu 16.04 > As a first step I mae sure that I have the package installed > ?python-openpyxl? > Now in the python command line when I tried to import openpyxl it throws > error. > >>> import openpyxl > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named openpyxl > >>> > My environment are > OS Ubuntu 14.04 LTS > Python 2.7.9 -- 64-bit > python-openpyxl version 1.7.0+ds1-1 > > I am not able to find why the system throws error when I am importing > openpyxl, since I have python-openpyxl installed. > > Like to request guidance and feedback > Thanks > Joseph John > From jjk.saji at gmail.com Thu Jun 9 03:33:11 2016 From: jjk.saji at gmail.com (Joseph John) Date: Thu, 9 Jun 2016 11:33:11 +0400 Subject: [Tutor] Command statement work well on interactive mode, which running as pf file it throws error object has no attribute '__getitem__' Message-ID: Dear All, I am taking my initial steps to interact Python with excel files. I was success in importing the needed modules, and executing steps in interactive mode . All my initial commands such as import openpyxl wb = openpyxl.load_workbook('MyTutor.xlsx') wb.get_sheet_names() sheet= wb.get_sheet_by_name('SQL Results') sheet.title print sheet.title print sheet['A1'] All worked fine Now when I create a *.py file with all the above steps and run it, I am getting error Traceback (most recent call last): File "./ReadingDataFrom1X.py", line 10, in c = sheet['A1'] TypeError: 'Worksheet' object has no attribute '__getitem__' The contents of ?ReadingDataFrom1X.py? is as follows #!/usr/bin/python import openpyxl wb = openpyxl.load_workbook('MyTutor.xlsx') wb.get_sheet_names() sheet= wb.get_sheet_by_name('SQL Results') sheet.title print sheet.title print sheet['A1'] If the statement worked in the interactive mode, it should all work while executing the file created. Did some google search, did not come across any valid reason why the error message is showing. Would like to request for guidance and feed back, please let me know why all my command statements work on interactive mode and when I run it in a py file some commands throws error Thanks Joseph John From alan.gauld at yahoo.co.uk Thu Jun 9 04:07:40 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 9 Jun 2016 09:07:40 +0100 Subject: [Tutor] Command statement work well on interactive mode, which running as pf file it throws error object has no attribute '__getitem__' In-Reply-To: References: Message-ID: On 09/06/16 08:33, Joseph John wrote: > Now when I create a *.py file with all the above steps and run it, I am > getting error > Traceback (most recent call last): > File "./ReadingDataFrom1X.py", line 10, in > c = sheet['A1'] > TypeError: 'Worksheet' object has no attribute '__getitem__' > > > The contents of ?ReadingDataFrom1X.py? is as follows > #!/usr/bin/python > import openpyxl > wb = openpyxl.load_workbook('MyTutor.xlsx') > wb.get_sheet_names() > sheet= wb.get_sheet_by_name('SQL Results') > sheet.title > print sheet.title > print sheet['A1'] Not according to the error message. It says the error is on the line: c = sheet['A1'] But you don't have such a line. So the code you sent us is not the code you are running. Why that may be is the puzzle. Do you have more than one file called ReadingDataFrom1X.py? How are you running the code? Is it from an IDE such as IDLE? Or from the OS command line? That might make a difference. -- 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 jjk.saji at gmail.com Thu Jun 9 04:49:43 2016 From: jjk.saji at gmail.com (Joseph John) Date: Thu, 9 Jun 2016 12:49:43 +0400 Subject: [Tutor] Command statement work well on interactive mode, which running as pf file it throws error object has no attribute '__getitem__' In-Reply-To: References: Message-ID: On Thu, Jun 9, 2016 at 12:07 PM, Alan Gauld via Tutor wrote: > On 09/06/16 08:33, Joseph John wrote: > > > Now when I create a *.py file with all the above steps and run it, I am > > getting error > > Traceback (most recent call last): > > File "./ReadingDataFrom1X.py", line 10, in > > c = sheet['A1'] > > TypeError: 'Worksheet' object has no attribute '__getitem__' > > > > > > The contents of ?ReadingDataFrom1X.py? is as follows > > #!/usr/bin/python > > import openpyxl > > wb = openpyxl.load_workbook('MyTutor.xlsx') > > wb.get_sheet_names() > > sheet= wb.get_sheet_by_name('SQL Results') > > sheet.title > > print sheet.title > > print sheet['A1'] > > Not according to the error message. It says the error > is on the line: > > c = sheet['A1'] > > But you don't have such a line. > > So the code you sent us is not the code you are running. > Why that may be is the puzzle. > Do you have more than one file called ReadingDataFrom1X.py? > > How are you running the code? Is it from an IDE such as IDLE? > Or from the OS command line? That might make a difference. > > Hi Alan, Thanks for the reply. I have only the same file, what happened is that I was editing the file in a editor for troubleshooting and when I run the file it was not saved, I was trimming the code to the minimum Now the program file has only the #!/usr/bin/python import openpyxl wb = openpyxl.load_workbook('MyTutor.xlsx') wb.get_sheet_names() sheet= wb.get_sheet_by_name('SQL Results') sheet.title print sheet.title print sheet['A1'] and when I run it from the command line, it gets this error Traceback (most recent call last): File "./ReadingDataFrom1X.py", line 8, in print sheet['A1'] TypeError: 'Worksheet' object has no attribute '__getitem__' I also suspect that my user python installation has issues. I will test it I will test it on another user environment and see how it comes and give the feedback on a separate thread Thanks a lot Joseph John > > > -- > 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 jjk.saji at gmail.com Thu Jun 9 05:03:15 2016 From: jjk.saji at gmail.com (Joseph John) Date: Thu, 9 Jun 2016 13:03:15 +0400 Subject: [Tutor] Command statement work well on interactive mode, which running as pf file it throws error object has no attribute '__getitem__' In-Reply-To: References: Message-ID: On Thu, Jun 9, 2016 at 12:49 PM, Joseph John wrote: > > > On Thu, Jun 9, 2016 at 12:07 PM, Alan Gauld via Tutor > wrote: > >> On 09/06/16 08:33, Joseph John wrote: >> >> > Now when I create a *.py file with all the above steps and run it, I am >> > getting error >> > Traceback (most recent call last): >> > File "./ReadingDataFrom1X.py", line 10, in >> > c = sheet['A1'] >> > TypeError: 'Worksheet' object has no attribute '__getitem__' >> > >> > >> > The contents of ?ReadingDataFrom1X.py? is as follows >> > #!/usr/bin/python >> > import openpyxl >> > wb = openpyxl.load_workbook('MyTutor.xlsx') >> > wb.get_sheet_names() >> > sheet= wb.get_sheet_by_name('SQL Results') >> > sheet.title >> > print sheet.title >> > print sheet['A1'] >> >> Not according to the error message. It says the error >> is on the line: >> >> c = sheet['A1'] >> >> But you don't have such a line. >> >> So the code you sent us is not the code you are running. >> Why that may be is the puzzle. >> Do you have more than one file called ReadingDataFrom1X.py? >> >> How are you running the code? Is it from an IDE such as IDLE? >> Or from the OS command line? That might make a difference. >> >> Hi Alan, > Thanks for the reply. > I have only the same file, what happened is that I was editing the file in > a editor for troubleshooting and when I run the file it was not saved, I > was trimming the code to the minimum > Now the program file has only the > #!/usr/bin/python > import openpyxl > wb = openpyxl.load_workbook('MyTutor.xlsx') > wb.get_sheet_names() > sheet= wb.get_sheet_by_name('SQL Results') > sheet.title > print sheet.title > print sheet['A1'] > > and when I run it from the command line, it gets this error > Traceback (most recent call last): > File "./ReadingDataFrom1X.py", line 8, in > print sheet['A1'] > TypeError: 'Worksheet' object has no attribute '__getitem__' > > I also suspect that my user python installation has issues. I will test > it > I will test it on another user environment and see how it comes and give > the feedback on a separate thread > > Tired it on a fresh install of Ubuntu 14.04 64 bit , Python version 2.7.6 Same error I am getting File "./ReadingDataFrom1X.py", line 8, in print sheet['A1'] TypeError: 'Worksheet' object has no attribute '__getitem__ for the code itsupport at Server-A:~$ cat ReadingDataFrom1X.py #!/usr/bin/python import openpyxl wb = openpyxl.load_workbook('1XDataUserMDN.xlsx') wb.get_sheet_names() sheet= wb.get_sheet_by_name('SQL Results') sheet.title print sheet.title print sheet['A1'] #sheet['A1'].value #c = sheet['A1'] #'The user id is ' + c.value Thanks Joseph John > Thanks a lot > Joseph John > > > >> >> >> -- >> 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 cmgcomsol at gmail.com Thu Jun 9 11:47:37 2016 From: cmgcomsol at gmail.com (CMG Thrissur) Date: Thu, 9 Jun 2016 21:17:37 +0530 Subject: [Tutor] Urgent: unicode problems writing CSV file In-Reply-To: References: Message-ID: <57598F99.2000505@gmail.com> On Wednesday 08 June 2016 07:24 PM, Alex Hall wrote: > All, > I'm working on a project that writes CSV files, and I have to get it done > very soon. I've done this before, but I'm suddenly hitting a problem with > unicode conversions. I'm trying to write data, but getting the standard > cannot encode character: ordinal not in range(128) > > I've tried > str(info).encode("utf8") > str(info).decode(utf8") > unicode(info, "utf8") > csvFile = open("myFile.csv", "wb", encoding="utf-8") #invalid keyword > argument > > What else can I do? As I said, I really have to get this working soon, but > I'm stuck on this stupid unicode thing. Any ideas will be great. Thanks. > Hi, How about opening file with setting the encoding = utf8. This solved most of my problems. George From alan.gauld at yahoo.co.uk Fri Jun 10 12:45:13 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 10 Jun 2016 17:45:13 +0100 Subject: [Tutor] Command statement work well on interactive mode, which running as pf file it throws error object has no attribute '__getitem__' In-Reply-To: References: Message-ID: On 09/06/16 10:03, Joseph John wrote: > itsupport at Server-A:~$ cat ReadingDataFrom1X.py > #!/usr/bin/python > import openpyxl > wb = openpyxl.load_workbook('1XDataUserMDN.xlsx') > wb.get_sheet_names() > sheet= wb.get_sheet_by_name('SQL Results') > sheet.title > print sheet.title > print sheet['A1'] I can't see anything obvious and since this is not part of the standard library I suggest you try asking on the support site for openpyxl. http://groups.google.com/group/openpyxl-users hopefully they can figure out what's happening. -- 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 jsutar at gmail.com Fri Jun 10 18:43:12 2016 From: jsutar at gmail.com (Jignesh Sutar) Date: Fri, 10 Jun 2016 22:43:12 +0000 Subject: [Tutor] Loop in pre-defined blocks Message-ID: Is there a better way to code the below than to specify blocks as I have. Ideally I'd like to specify blocks simply as *blocks=(12,20,35)* blocks=[(1,12), (13,20), (25,35)] for i,j in enumerate(blocks): for x in xrange(blocks[i][0],blocks[i][1]+1): print i+1, x Thanks in advance. Jignesh From alan.gauld at yahoo.co.uk Fri Jun 10 18:57:58 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 10 Jun 2016 23:57:58 +0100 Subject: [Tutor] Loop in pre-defined blocks In-Reply-To: References: Message-ID: On 10/06/16 23:43, Jignesh Sutar wrote: > Is there a better way to code the below than to specify blocks as I have. > Ideally I'd like to specify blocks simply as *blocks=(12,20,35)* > > blocks=[(1,12), (13,20), (25,35)] > for i,j in enumerate(blocks): > for x in xrange(blocks[i][0],blocks[i][1]+1): > print i+1, x Can you explain in English what you are trying to do. Working through your algorithm in my head is too much like hard work. At the very least show us the output. Better still explain what it means - what the data represents and how the outputs relate to the inputs. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From jignesh.sutar at gmail.com Fri Jun 10 18:41:37 2016 From: jignesh.sutar at gmail.com (Jignesh Sutar) Date: Fri, 10 Jun 2016 22:41:37 +0000 Subject: [Tutor] Loop blocks Message-ID: Is there a better way to code the below than to specify blocks as I have. Ideally I'd like to specify blocks simply as *blocks=(12,20,35)* blocks=[(1,12), (13,20), (25,35)] for i,j in enumerate(blocks): for x in xrange(blocks[i][0],blocks[i][1]+1): print i+1, x Thanks in advance. Jignesh From jsutar at gmail.com Fri Jun 10 19:08:23 2016 From: jsutar at gmail.com (Jignesh Sutar) Date: Fri, 10 Jun 2016 23:08:23 +0000 Subject: [Tutor] Loop in pre-defined blocks In-Reply-To: References: Message-ID: Sorry, to be a little bit more descriptive. I'd like to loop from 1 to 35 but within this loop there are divisions which I need to prefix that particular division number. My output would look like this: 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 3 25 3 26 3 27 3 28 3 29 3 30 3 31 3 32 3 33 3 34 3 35 On Sat, 11 Jun 2016 at 00:02 Alan Gauld via Tutor wrote: > On 10/06/16 23:43, Jignesh Sutar wrote: > > Is there a better way to code the below than to specify blocks as I have. > > Ideally I'd like to specify blocks simply as *blocks=(12,20,35)* > > > > blocks=[(1,12), (13,20), (25,35)] > > for i,j in enumerate(blocks): > > for x in xrange(blocks[i][0],blocks[i][1]+1): > > print i+1, x > > > Can you explain in English what you are trying to do. > Working through your algorithm in my head is too much > like hard work. At the very least show us the output. > > Better still explain what it means - what the data > represents and how the outputs relate to the inputs. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Fri Jun 10 19:40:26 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 11 Jun 2016 00:40:26 +0100 Subject: [Tutor] Fwd: Re: Loop in pre-defined blocks In-Reply-To: References: Message-ID: <575B4FEA.7080406@yahoo.co.uk> Forwarding to tutor list. Always use Reply All when responding to list mail. > Sorry, to be a little bit more descriptive. I'd like to loop from 1 to 35 > but within this loop there are divisions which I need to prefix that > particular division number. > My output would look like this: >>>>>>>>>>>>>>> 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 3 25 3 26 3 27 3 28 3 29 3 30 3 31 3 32 3 33 3 34 3 35 >>>>>>>>>>>>>>> You can't specify the blocks as just (12,20,.35) since you are using non-contiguous blocks - you have a gap between 20 and 25. So your definition needs to be (1,12),(13,20),(25,35) to specify the missing rows. But you can arguably simplify the code a little: blocks = ((1,13),(13,21),(25,36)) for prefix, block in enumerate(blocks): for n in range(*block): print prefix+1, n its very similar to your code but using tuple expansion in range() cleans it up a little bit and the names hopefully make the intent clearer. 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 11 03:49:48 2016 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Jun 2016 09:49:48 +0200 Subject: [Tutor] Fwd: Re: Loop in pre-defined blocks References: <575B4FEA.7080406@yahoo.co.uk> Message-ID: Alan Gauld via Tutor wrote: > Forwarding to tutor list. Always use Reply All when responding to list > mail. > >> Sorry, to be a little bit more descriptive. I'd like to loop from 1 to 35 >> but within this loop there are divisions which I need to prefix that >> particular division number. > >> My output would look like this: >>>>>>>>>>>>>>>> > 1 1 > 1 2 > 1 3 > 1 4 > 1 5 > 1 6 > 1 7 > 1 8 > 1 9 > 1 10 > 1 11 > 1 12 > 2 13 > 2 14 > 2 15 > 2 16 > 2 17 > 2 18 > 2 19 > 2 20 > 3 25 > 3 26 > 3 27 > 3 28 > 3 29 > 3 30 > 3 31 > 3 32 > 3 33 > 3 34 > 3 35 >>>>>>>>>>>>>>>> > > You can't specify the blocks as just (12,20,.35) since you are using > non-contiguous blocks - you have a gap between 20 and 25. > > So your definition needs to be (1,12),(13,20),(25,35) to specify > the missing rows. But you can arguably simplify the code a little: > > blocks = ((1,13),(13,21),(25,36)) > for prefix, block in enumerate(blocks): > for n in range(*block): > print prefix+1, n > > its very similar to your code but using tuple expansion in range() > cleans it up a little bit and the names hopefully make the intent > clearer. As Alan says, you need to specify the gaps. A simple if hackish way is to use negative numbers: def expand(ends): start = 1 for end in ends: if end < 0: start = -end else: end += 1 yield (start, end) start = end blocks = [12, 20, -25, 35] for index, span in enumerate(expand(blocks), 1): for x in xrange(*span): print index, x From cs at zip.com.au Sat Jun 11 03:54:01 2016 From: cs at zip.com.au (cs at zip.com.au) Date: Sat, 11 Jun 2016 17:54:01 +1000 Subject: [Tutor] Loop blocks In-Reply-To: References: Message-ID: <20160611075401.GA65631@cskk.homeip.net> On 10Jun2016 22:41, Jignesh Sutar wrote: >Is there a better way to code the below than to specify blocks as I have. >Ideally I'd like to specify blocks simply as *blocks=(12,20,35)* > >blocks=[(1,12), (13,20), (25,35)] >for i,j in enumerate(blocks): > for x in xrange(blocks[i][0],blocks[i][1]+1): > print i+1, x For one thing I don't see you use "j" anywhere. Why not this: for i, block in enumerate(blocks): for x in xrange(block[0], block[1]+1): print i+1, x enumerate() returns a counter _and_ the element from what it iterates over. You can also start enumerate from 1 instead of zero; since the code above no longer used "i" as an index into "blocks" (because you get handed the block by enumerate) you could count from one to avoid computing "i+1". Finally, your question: Ideally I'd like to specify blocks simply as: blocks=(12,20,35) why not just do that? I don't see you using the leading number, and if it is meant to be the "i+1" in your code, you get that from enumerate already. Cheers, Cameron Simpson From jjk.saji at gmail.com Mon Jun 13 06:47:05 2016 From: jjk.saji at gmail.com (Joseph John) Date: Mon, 13 Jun 2016 14:47:05 +0400 Subject: [Tutor] Python ODBC driver for Oracle 9 Message-ID: Hi All, I am trying to connect Python to Oracle 9 DB, checked for python ODBC driver for oracle. What I could find point out to http://cx-oracle.sourceforge.net/ , but this driver supports from Oracle 11 on wards. What I need is driver for Pyhton to get connected to Oracle 9. Did lots of google search, did not find any proper links which is leadeing to the Oracle9i driver. And now I am trying to post here to get advice or url on how to get oracle 9 python driver. Thanks Joseph John From esawiek at gmail.com Mon Jun 13 03:46:01 2016 From: esawiek at gmail.com (Ek Esawi) Date: Mon, 13 Jun 2016 03:46:01 -0400 Subject: [Tutor] Loop in pre-defined blocks Message-ID: Here is a beginner code that might work for you. Best of luck. EK b=[12, 20, 35] for i in range(len(b)): if i==0: c=0 else: c=b[i-1] for j in range(c, b[i]): print(i+1,j+1) From rus.cahimb at gmail.com Mon Jun 13 12:50:46 2016 From: rus.cahimb at gmail.com (Ramanathan Muthaiah) Date: Mon, 13 Jun 2016 16:50:46 +0000 Subject: [Tutor] Howto display progress as cmd is being executed via subprocess ? Message-ID: Hello All, Am aware of the module called 'progressbar' that can do this magic of displaying progress as the cmd is executed. In fact, I have a test code to show progress and tried, it works. from progressbar import * import time def main(): progress = ProgressBar() for num in progress(range(80)): time.sleep(0.15) if __name__ == '__main__': main() And, I have this piece of code that executes set of cmds stored in 'cmd' variable. This too works without any issues until now. try: dlog = subprocess.check_output([cmd], stderr=subprocess.STDOUT, shell=True) except subprocess.CalledProcessError, e: dlog = e.output Question: Am lost as to how to combine the progressbar and subprocess code snippets to show the progress as the cmd is being executed. Any ideas on how this can be done ? -- regards Ramanathan.M From 79099012930 at yandex.ru Mon Jun 13 15:55:14 2016 From: 79099012930 at yandex.ru (=?utf-8?B?0JLQu9Cw0LQ=?=) Date: Mon, 13 Jun 2016 22:55:14 +0300 Subject: [Tutor] Hello everybody Message-ID: <3973191465847714@web16m.yandex.ru> Hi. I've just begin with Python? I'm 34. Is it late or what? If it is - I will cut it out. What you think guys? ** --** ** ******************, ********, PR-**************** Rich PR +79099012930 ** From michael.selik at gmail.com Mon Jun 13 18:20:08 2016 From: michael.selik at gmail.com (Michael Selik) Date: Mon, 13 Jun 2016 22:20:08 +0000 Subject: [Tutor] Loop in pre-defined blocks In-Reply-To: References: Message-ID: On Mon, Jun 13, 2016 at 1:33 PM Ek Esawi wrote: > Here is a beginner code that might work for you. Best of luck. EK > > b=[12, 20, 35] > > for i in range(len(b)): > if i==0: > c=0 > else: > c=b[i-1] > for j in range(c, b[i]): > print(i+1,j+1) > If you want to go a little further, you can use zip and enumerate. py> stops = [3, 5, 9] py> starts = [0] + stops[:-1] py> for i, (start, stop) in enumerate(zip(starts, stops)): ... for j in range(start, stop): ... print(i, j) ... 0 0 0 1 0 2 1 3 1 4 2 5 2 6 2 7 2 8 From alan.gauld at yahoo.co.uk Mon Jun 13 18:27:04 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 13 Jun 2016 23:27:04 +0100 Subject: [Tutor] Loop in pre-defined blocks In-Reply-To: References: Message-ID: On 13/06/16 08:46, Ek Esawi wrote: > Here is a beginner code that might work for you. Best of luck. EK > > b=[12, 20, 35] > > for i in range(len(b)): > if i==0: > c=0 > else: > c=b[i-1] > for j in range(c, b[i]): > print(i+1,j+1) The problem here is that it doesn't give the gaps in the output data that the OP requested. That's why we said they need the start and stop values in the ranges. -- 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 joel.goldstick at gmail.com Mon Jun 13 18:33:53 2016 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 13 Jun 2016 18:33:53 -0400 Subject: [Tutor] Hello everybody In-Reply-To: <3973191465847714@web16m.yandex.ru> References: <3973191465847714@web16m.yandex.ru> Message-ID: On Mon, Jun 13, 2016 at 3:55 PM, ???? <79099012930 at yandex.ru> wrote: > Hi. I've just begin with Python? I'm 34. Is it late or what? If it is - I > will cut it out. What you think guys? > ** > --** > ** ******************, > ********, PR-**************** Rich PR > +79099012930 > ** > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor You are an hour and a half too late! Late for what? welcome -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From alan.gauld at yahoo.co.uk Mon Jun 13 18:33:55 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 13 Jun 2016 23:33:55 +0100 Subject: [Tutor] Howto display progress as cmd is being executed via subprocess ? In-Reply-To: References: Message-ID: On 13/06/16 17:50, Ramanathan Muthaiah wrote: > Am aware of the module called 'progressbar' that can do this magic of > displaying progress as the cmd is executed. I'm not familiar with it, I don't believe its in the standard library? So I can only offer generalized advice. > def main(): > progress = ProgressBar() > for num in progress(range(80)): > time.sleep(0.15) I have no idea how that works since I don;t know anything about the progress API. Its apparently callable and takes a sequence and returns a sequence but what it does with those values I can't say. > And, I have this piece of code that executes set of cmds > This too works without any issues until now. > > try: > dlog = subprocess.check_output([cmd], > stderr=subprocess.STDOUT, shell=True) > except subprocess.CalledProcessError, e: > dlog = e.output > > Question: > > Am lost as to how to combine the progressbar and subprocess code snippets Here's where the generalized help comes in. There are two common approaches: 1) Split your work into bite sized chunks. Repeatedly call those chunks until done and update progress after each chunk. 2) Run your work in a separate thread and in the main thread update the progress bar either based on an estimated time to complete or by reading the process output in some way. How that translates to your particular ProgressBar I can't say. But it might give you ideas. Or maybe the start of a specific question to the module support forum/list or author -- 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 13 18:40:24 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 13 Jun 2016 23:40:24 +0100 Subject: [Tutor] Python ODBC driver for Oracle 9 In-Reply-To: References: Message-ID: On 13/06/16 11:47, Joseph John wrote: > I am trying to connect Python to Oracle 9 DB, checked for python ODBC > driver for oracle. Please be clear. Are you looking for an ODBC driver that will work with Oracle 9? Or are you looking for a Python DBAPI driver for Oracle 9? Those are two different things. > What I need is driver for Pyhton to get connected to Oracle 9. You can do it directly or via ODBC. For an old version of Oracle it might be easier to find an ODBC driver - that should have come from Oracle with the database. The current Python ODBC driver should then work with the Oracle ODBC driver. Finding a native Python DB driver for Oracle 9 might be harder although I'd expect one to still be available somewhere. A Google search threw this up as the first link: https://wiki.python.org/moin/Oracle And it claims to work with Oracle versions 8 to 11i. -- 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 13 18:45:48 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 13 Jun 2016 23:45:48 +0100 Subject: [Tutor] Hello everybody In-Reply-To: <3973191465847714@web16m.yandex.ru> References: <3973191465847714@web16m.yandex.ru> Message-ID: On 13/06/16 20:55, ???? wrote: > Hi. I've just begin with Python? I'm 34. Is it late or what? If it is - I > will cut it out. What you think guys? No you are just a young whippersnapper. I've had students use my tutorial in their 70s (and in their pre-teens too) But is this also your start in programming in general? Or can you already program in any other language? If the latter you will find python easy to pick up. If the former you have some extra work to learn the basic concepts as well as how Python implements those concepts. Finally, it will help if you are experienced in general computer use: navigating folders, manipulating files (especially to edit text files), using a command console, etc. -- 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 esawiek at gmail.com Mon Jun 13 19:24:53 2016 From: esawiek at gmail.com (Ek Esawi) Date: Mon, 13 Jun 2016 19:24:53 -0400 Subject: [Tutor] Loop in pre-defined blocks Message-ID: OPS! This code now produces desired results. I suppose that this works for smaller blocks. For larger blocks, it might be cumbersome. EK b=[12, 20, 35] for i in range(len(b)): if i==0: c=0 elif i==2: c=24 else: c=b[i-1] for j in range(c, b[i]): print(i+1,j+1) From k.omalley92 at gmail.com Mon Jun 13 19:14:36 2016 From: k.omalley92 at gmail.com (Katelyn O'Malley) Date: Mon, 13 Jun 2016 19:14:36 -0400 Subject: [Tutor] rock paper scissor lizard spock Message-ID: Hi I am just getting into python and I am trying to create a rock paper scissor lizard spock game for 3 people and I cannot figure out the scoring of the players I attached the code below, any help/ideas is much appreciated. From michael.selik at gmail.com Mon Jun 13 18:56:42 2016 From: michael.selik at gmail.com (Michael Selik) Date: Mon, 13 Jun 2016 22:56:42 +0000 Subject: [Tutor] Loop in pre-defined blocks In-Reply-To: References: Message-ID: On Mon, Jun 13, 2016 at 6:28 PM Alan Gauld via Tutor wrote: > On 13/06/16 08:46, Ek Esawi wrote: > > Here is a beginner code that might work for you. Best of luck. EK > > > > b=[12, 20, 35] > > > > for i in range(len(b)): > > if i==0: > > c=0 > > else: > > c=b[i-1] > > for j in range(c, b[i]): > > print(i+1,j+1) > > The problem here is that it doesn't give the gaps in the output > data that the OP requested. That's why we said they need the start > and stop values in the ranges. > My apologies. The good news is that makes the solution even easier. py> blocks=[(2,4), (10,13), (20,22)] py> for i, (start, stop) in enumerate(blocks): ... for j in range(start, stop): ... print(i, j) ... 0 2 0 3 1 10 1 11 1 12 2 20 2 21 From joel.goldstick at gmail.com Mon Jun 13 20:22:20 2016 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 13 Jun 2016 20:22:20 -0400 Subject: [Tutor] rock paper scissor lizard spock In-Reply-To: References: Message-ID: On Mon, Jun 13, 2016 at 7:14 PM, Katelyn O'Malley wrote: > Hi I am just getting into python and I am trying to create a rock paper > scissor lizard spock game for 3 people and I cannot figure out the scoring > of the players I attached the code below, any help/ideas is much > appreciated. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Welcome. This list doesn't show attachments. Cut and paste your code in your message -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From eryksun at gmail.com Tue Jun 14 06:58:27 2016 From: eryksun at gmail.com (eryk sun) Date: Tue, 14 Jun 2016 10:58:27 +0000 Subject: [Tutor] Howto display progress as cmd is being executed via subprocess ? In-Reply-To: References: Message-ID: On Mon, Jun 13, 2016 at 4:50 PM, Ramanathan Muthaiah wrote: > > subprocess.check_output([cmd], stderr=subprocess.STDOUT, shell=True) > ... > how to combine the progressbar and subprocess code snippets to show the > progress as the cmd is being executed. check_output waits for the process to exit, so you can't use it with a progress bar. However, if the program writes its progress to stdout, then you can use `p = subprocess.Popen(args, stdout=PIPE)`, and create a thread to read p.stdout, parse the output, and update the progress bar. Many programs detect when a standard stream isn't an interactive TTY and switch to full buffering. You need to avoid this if you're tracking progress or otherwise need to interact with a child process. If you're on a Unix system you can use the stdbuf utility program to try to force the child to disable buffering or use line buffering. If that doesn't work, or you're on Windows, maybe the program has a command-line option or environment variable setting that forces interactive mode, such as Python's `-i` option. If all else fails, try pexpect or winpexpect instead of subprocess. This works by making a program think it's connected to a terminal/console. FYI, with shell=True, you should use a command line instead of an args list. The POSIX implementation does the following: if isinstance(args, (str, bytes)): args = [args] else: args = list(args) if shell: args = ["/bin/sh", "-c"] + args Using just [cmd] doesn't cause a problem, but [cmd, arg1, arg2] is probably a mistake. This passes the arguments to the shell, which sets them as $N parameters. For example: >>> subprocess.call(['echo $0 $1', 'arg1', 'arg2'], shell=True) arg1 arg2 The Windows implementation does the following: if not isinstance(args, str): args = list2cmdline(args) if shell: startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW startupinfo.wShowWindow = _winapi.SW_HIDE comspec = os.environ.get("COMSPEC", "cmd.exe") args = '{} /c "{}"'.format (comspec, args) Thus on Windows even [cmd] is potentially wrong with shell=True. list2cmdline doesn't know how to escape special characters for cmd.exe. From k.omalley92 at gmail.com Tue Jun 14 06:18:57 2016 From: k.omalley92 at gmail.com (Katelyn O'Malley) Date: Tue, 14 Jun 2016 06:18:57 -0400 Subject: [Tutor] rock paper scissor lizard spock help Message-ID: Hi I am just getting into python and I am trying to create a rock paper scissor lizard spock game for 3 people and I cannot figure out the scoring of the players I attached the code below, any help/ideas is much appreciated. import random number_of_games = 3 print("Player 1 please choose rock , paper , scissors , lizard, or spock (in lower case please)") choice1 = input("What do you choose? ") player1_choice = str(choice1) print("Player 2 please choose rock , paper , scissors , lizard, or spock (in lower case please)") choice2 = input("What do you choose? ") player2_choice = str(choice2) print("Player 3 please choose rock , paper , scissors , lizard, or spock (in lower case please)") choice3 = input("What do you choose? ") player3_choice = str(choice3) def name_to_number(name): if name == "rock": name = 0 return name elif name == "spock": name = 1 return name elif name == "paper": name = 2 return name elif name == "lizard": name = 3 return name elif name == "scissors": name = 4 return name def number_to_name(number): if number == 0: number = "rock" return number elif number == 1: number = "spock" return number elif number == 2: number = "paper" return number elif number == 3: number = "lizard" return number elif number == 4: number = "scissors" return number player1_number = name_to_number(choice1) player2_number = name_to_number(choice2) player3_number = name_to_number(choice3) while number_of_games > 0: number_of_games -= 1 print("Player 1 choice is: " + player1_choice) print("Player 2 choice is: " + player2_choice) print("Player 3 choice is: " + player3_choice) if (int(player1_number) - player2_number) % 5 >=3: print("Player 1 wins!") elif difference >=2: print("Player 2 wins!") elif difference <=1: print ("Player 3 wins!") elif difference == 0: print("It's a tie! You have %d tries left..." % number_of_games) number_of_games = '' else: print("You lost to the Computer. You have %d games left..." % number_of_games) number_of_games = '' break while number_of_games >0: number_of_games -=1 choice1 = input("What do you choose? ") player1_choice = str(choice1) print("Player 1 choice is: " + player1_choice) choice2 = input("What do you choose? ") player2_choice = str(choice2) print("Player 2 choice is: " + player2_choice) choice3 = input("What do you choose? ") player3_choice = str(choice3) print("Player 3 choice is: " + player3_choice) if difference >=3: print("Player 1 wins!") elif difference >=2: print("Player 2 wins!") elif difference <=1: print ("Player 3 wins!") elif difference == 0: print("It's a tie! You have %d tries left..." % number_of_games) number_of_games = '' else: print("You lost to the Computer. You have %d games left..." % number_of_games) number_of_games = '' break while number_of_games >0: number_of_games -=1 choice1 = input("What do you choose? ") player1_choice = str(choice1) print("Player 1 choice is: " + player1_choice) choice2 = input("What do you choose? ") player2_choice = str(choice2) print("Player 2 choice is: " + player2_choice) choice3 = input("What do you choose? ") player3_choice = str(choice3) print("Player 3 choice is: " + player3_choice) if difference >=3: print("Player 1 wins!") elif difference >=2: print("Player 2 wins!") elif difference <=1: print ("Player 3 wins!") elif difference == 0: print("It's a tie! You have %d tries left..." % number_of_games) number_of_games = '' else: print("You lost to the Computer. You have %d games left..." % number_of_games) number_of_games = '' break From sjeik_appie at hotmail.com Tue Jun 14 16:03:31 2016 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Tue, 14 Jun 2016 20:03:31 +0000 Subject: [Tutor] capture output from a subprocess while it is being produced Message-ID: Hi, I have a Tkinter program where I can fire up a commandline program which could run for quite a while (let's say 15 minutes or more). While the program is running, I would like to show the output that I normally see in the terminal (actually, the target platform is windows but now I am on Linux) in a text entry widget. The main purpose of this is to show that "something is happening". Anyway, below I tried to make a simplified version, without Tkinter. I fire up the commandline process in a separate thread, and I try to append lines of the output to a deque. The 'ls' command returns 2964 lines on my system. So I expect the deque to contain less items, because every time the while loop in process() sleeps, it would miss a few items. But it doesn't! It's like the ls output is returned as one chunk. What am I doing wrong? Thanks! Albert-Jan from subprocess import Popen, PIPE from threading import Thread from collections import deque import sys import time import os.path def process(dq): """Run a commandline program, with lots of output""" cwd = os.path.dirname(sys.executable) proc = Popen("ls -lF", cwd=cwd, shell=True, stdout=PIPE, stderr=PIPE) #print len(proc.stdout.readlines()) # 2964 output = errors = None while True: try: #output, errors = proc.communicate() # "Wait for process to terminate." output = proc.stdout.read() # docs: "Use communicate() rather than .stdin.write, .stdout.read or .stderr.read" errors = proc.stderr.read() proc.stdout.flush() except ValueError: break finally: if errors: raise RuntimeError(str(errors)) if output: dq.append(output) time.sleep(0.0001) if not output: break def update(dq): """This is supposed to return a new, not necessarily contiguous, chunk of output""" if dq: result = dq.pop() print time.asctime().center(79, "*") print result return result if __name__ == "__main__": dq = deque() output_thread = Thread(target=process, args=(dq,), name="output_thread") #output_thread.setDaemon(True) output_thread.start() lines = [] while output_thread.is_alive(): update(dq) time.sleep(0.1) if not dq: break for item in dq: lines += item.splitlines() output_thread.join() print len(lines) From bgailer at gmail.com Tue Jun 14 19:26:49 2016 From: bgailer at gmail.com (Bob Gailer) Date: Tue, 14 Jun 2016 19:26:49 -0400 Subject: [Tutor] rock paper scissor lizard spock help In-Reply-To: References: Message-ID: On Jun 14, 2016 4:05 PM, "Katelyn O'Malley" wrote: > > Hi I am just getting into python and I am trying to create a rock paper > scissor lizard spock game for 3 people and I cannot figure out the scoring > of the players I attached the code below, any help/ideas is much > appreciated. Welcome to python and the tutor list. I don't know what you mean by "figure out the scoring". Please explain. Did you test the program? Please do that now, fix the problems that you will discover. I don't want to sound discouraging, but there are many problems here. I will list some. Unused items e.g. random. Undefined names e.g. difference. Unnecessary function calls e.g. str, int Repeated code. I assume this is your first attempt at programming. Have you gone through any tutorials? It would be good to start with something simpler. I suggest you sit with pencil and paper and walk through the program line by line. What happens If a player enters something other than a valid choice? We want you to succeed. But you must study proper coding techniques. Take a course, read a book, get a simpler program running. > > import random > > number_of_games = 3 > > print("Player 1 please choose rock , paper , scissors , lizard, or spock > (in lower case please)") > choice1 = input("What do you choose? ") > player1_choice = str(choice1) > print("Player 2 please choose rock , paper , scissors , lizard, or spock > (in lower case please)") > choice2 = input("What do you choose? ") > player2_choice = str(choice2) > print("Player 3 please choose rock , paper , scissors , lizard, or spock > (in lower case please)") > choice3 = input("What do you choose? ") > player3_choice = str(choice3) > def name_to_number(name): > if name == "rock": > name = 0 > return name > elif name == "spock": > name = 1 > return name > elif name == "paper": > name = 2 > return name > elif name == "lizard": > name = 3 > return name > elif name == "scissors": > name = 4 > return name > > def number_to_name(number): > if number == 0: > number = "rock" > return number > elif number == 1: > number = "spock" > return number > elif number == 2: > number = "paper" > return number > elif number == 3: > number = "lizard" > return number > elif number == 4: > number = "scissors" > return number > > player1_number = name_to_number(choice1) > player2_number = name_to_number(choice2) > player3_number = name_to_number(choice3) > > while number_of_games > 0: > number_of_games -= 1 > print("Player 1 choice is: " + player1_choice) > print("Player 2 choice is: " + player2_choice) > print("Player 3 choice is: " + player3_choice) > if (int(player1_number) - player2_number) % 5 >=3: > print("Player 1 wins!") > elif difference >=2: > print("Player 2 wins!") > elif difference <=1: > print ("Player 3 wins!") > elif difference == 0: > print("It's a tie! You have %d tries left..." % number_of_games) > number_of_games = '' > else: > print("You lost to the Computer. You have %d games left..." % > number_of_games) > number_of_games = '' > break > > while number_of_games >0: > number_of_games -=1 > choice1 = input("What do you choose? ") > player1_choice = str(choice1) > print("Player 1 choice is: " + player1_choice) > choice2 = input("What do you choose? ") > player2_choice = str(choice2) > print("Player 2 choice is: " + player2_choice) > choice3 = input("What do you choose? ") > player3_choice = str(choice3) > print("Player 3 choice is: " + player3_choice) > if difference >=3: > print("Player 1 wins!") > elif difference >=2: > print("Player 2 wins!") > elif difference <=1: > print ("Player 3 wins!") > elif difference == 0: > print("It's a tie! You have %d tries left..." % number_of_games) > number_of_games = '' > else: > print("You lost to the Computer. You have %d games left..." % > number_of_games) > number_of_games = '' > break > > while number_of_games >0: > number_of_games -=1 > choice1 = input("What do you choose? ") > player1_choice = str(choice1) > print("Player 1 choice is: " + player1_choice) > choice2 = input("What do you choose? ") > player2_choice = str(choice2) > print("Player 2 choice is: " + player2_choice) > choice3 = input("What do you choose? ") > player3_choice = str(choice3) > print("Player 3 choice is: " + player3_choice) > if difference >=3: > print("Player 1 wins!") > elif difference >=2: > print("Player 2 wins!") > elif difference <=1: > print ("Player 3 wins!") > elif difference == 0: > print("It's a tie! You have %d tries left..." % number_of_games) > number_of_games = '' > else: > print("You lost to the Computer. You have %d games left..." % > number_of_games) > number_of_games = '' > break > _______________________________________________ > 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 14 19:49:50 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 15 Jun 2016 00:49:50 +0100 Subject: [Tutor] capture output from a subprocess while it is being produced In-Reply-To: References: Message-ID: On 14/06/16 21:03, Albert-Jan Roskam wrote: > from subprocess import Popen, PIPE > from threading import Thread > from collections import deque > import sys > import time > import os.path > > > def process(dq): > """Run a commandline program, with lots of output""" > cwd = os.path.dirname(sys.executable) > proc = Popen("ls -lF", cwd=cwd, shell=True, stdout=PIPE, stderr=PIPE) > #print len(proc.stdout.readlines()) # 2964 > output = errors = None > while True: > try: > #output, errors = proc.communicate() # "Wait for process to terminate." > output = proc.stdout.read() # docs: "Use communicate() rather than .stdin.write, .stdout.read or .stderr.read" read() reads the entire "file", try using readline() instead. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Tue Jun 14 19:58:33 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 15 Jun 2016 00:58:33 +0100 Subject: [Tutor] rock paper scissor lizard spock help In-Reply-To: References: Message-ID: On 14/06/16 11:18, Katelyn O'Malley wrote: > Hi I am just getting into python and I am trying to create a rock paper > scissor lizard spock game for 3 people and I cannot figure out the scoring The scoring of any ganme is the bit that makes it unique, so we would need to know the scoring rules to know how you should proceed. however there are many things we could look at in your code, I will restrict myself to one: > def name_to_number(name): > if name == "rock": > name = 0 > return name > elif name == "spock": > name = 1 > return name > elif name == "paper": > name = 2 > return name > elif name == "lizard": > name = 3 > return name > elif name == "scissors": > name = 4 > return name This type of code can be greatly shortened and simplified by using a more appropriate data structure, such as a dictionary: def name_to_number(name): tokens = {"rock":0, "spock":1, "paper":2, "lizard":3, "scissors":4} return tokens[name] And something very similar for the number_to_name function below. 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 eryksun at gmail.com Wed Jun 15 00:48:03 2016 From: eryksun at gmail.com (eryk sun) Date: Wed, 15 Jun 2016 04:48:03 +0000 Subject: [Tutor] capture output from a subprocess while it is being produced In-Reply-To: References: Message-ID: On Tue, Jun 14, 2016 at 8:03 PM, Albert-Jan Roskam wrote: > > proc = Popen("ls -lF", cwd=cwd, shell=True, stdout=PIPE, stderr=PIPE) Don't use shell=True if you can avoid it. "ls" isn't a shell command. Use ['ls', '-lF']. The child process probably buffers its output when stdout isn't a terminal. A typical standard I/O buffer size is 4 KiB. Reading from the pipe will block until the child flushes the buffer to the pipe. It might fflush() at checkpoints, but otherwise this happens automatically when the buffer is full or when the child exits normally. On Linux you can use stdbuf to try forcing a program's stdout to use either no buffering (-o0) or line buffering (-oL) -- e.g. `stdbuf -o0 ls -lF`. That said, "ls" should use full buffering in a pipe. You need to test using a long-running process with intermittent output. You could use a Python script that sleeps for a random interval between writes to stdout and stderr. On Windows there's no program like stdbuf to control standard I/O buffering. If the target is a Python script, you can force interactive mode using the "-i" option or disable buffering using "-u" (but -u doesn't work for the 2.x REPL on Windows, since it switches to binary mode). > #output, errors = proc.communicate() # "Wait for process to terminate." > output = proc.stdout.read() # docs: "Use communicate() rather than .stdin.write, .stdout.read or .stderr.read" > errors = proc.stderr.read() Start a dedicated thread to read from each file. Otherwise you can end up with a deadlock. For example, the child could block while writing to a full stderr pipe, while the parent blocks while reading from an empty stdout pipe. Also, you should read by lines using readline(), or a byte at a time using read(1). > if errors: > raise RuntimeError(str(errors)) Data on stderr doesn't necessarily indicate an error. It could simply be logging, warnings, or debug output. An error is indicated by a non-zero exit code. Use proc.wait(). From jjk.saji at gmail.com Wed Jun 15 01:32:02 2016 From: jjk.saji at gmail.com (Joseph John) Date: Wed, 15 Jun 2016 09:32:02 +0400 Subject: [Tutor] Hello everybody In-Reply-To: <3973191465847714@web16m.yandex.ru> References: <3973191465847714@web16m.yandex.ru> Message-ID: On Mon, Jun 13, 2016 at 11:55 PM, ???? <79099012930 at yandex.ru> wrote: > Hi. I've just begin with Python? I'm 34. Is it late or what? If it is - > I > will cut it out. What you think guys? > ** > Here myself 48 crossed, just started taking python step by step Welcome to the herd > --** > ** ******************, > ********, PR-**************** Rich PR > +79099012930 > ** > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From steve10brink at comcast.net Wed Jun 15 13:31:50 2016 From: steve10brink at comcast.net (Steve Tenbrink) Date: Wed, 15 Jun 2016 10:31:50 -0700 Subject: [Tutor] Tutor Digest, Vol 148, Issue 20 In-Reply-To: Message-ID: I was 63 when I started. It's never too late. From robertvstepp at gmail.com Thu Jun 16 11:38:13 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 16 Jun 2016 10:38:13 -0500 Subject: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP() Message-ID: I am extremely gradually trying to dip my big toe into the waters of writing classes. I have a little bit of extra time at work today, so I thought I would start writing a class to replace a bunch of shell FTP scripts I've used in various programs that would be flexible enough to be reused wherever I need it. I immediately ran into problems, which I have (to the best of my knowledge) resolved. However, I am still in the early stages of writing the methods for this class, so I thought I would ask for any likely "gotchas" that might come my way. Python 2.4.4 on Solaris 10: from ftplib import FTP # Current values for Windows-based intranet FTP server: server_ip = 'my_default_IP' user = 'default_user' passwd = 'default_pw' class FTPFiles(FTP, object): """FTP files to Windows server location(s).""" def __init__(self, host=server_ip, user=user, passwd=passwd): """Initialize FTPFiles object. Normally the defaults will be used.""" super(FTPFiles, self).__init__(host, user, passwd) self.host = host self.user = user self.passwd = passwd def print_welcome_msg(self): """Print welcome message sent by FTP server.""" print self.getwelcome() if __name__ == '__main__': ftp = FTPFiles() ftp.print_welcome_msg() ftp.quit() What I learned today: 1) FTP from ftplib appears to be an old-style class. 2) I can't just use "class FTPFiles(FTP)" or I will be using old-style classes. 3) I need to use "class FTPFiles(FTP, object)" to use new-style classes and "object" must come AFTER "FTP". 4) I have to use "super(FTPFiles, self).__init__(host, user, passwd)" or I cannot successfully inherit from the FTP() class. Also, "self" apparently must come AFTER "FTPFiles" in the super expression. Questions: 1) Are there any more common "gotchas" that might be coming my way in trying to use a new-style class inheriting from an old-style class in ancient Py 2.4.4? 2) I don't have much knowledge about FTP processes. This is being used on a secure intranet environment. And I am using the ftplib for the first time. In its docs it mentioned that the quit() method is the "polite" way to close the connection, but that an exception may be raised if the responds with an error to the "QUIT" command the method sends. If this happens, will the connection close? Or should I do something like: try: ftp.quit() except: ftp.close() ? It seems here (If I should use the "try" block.) that a bare "except" is appropriate as I would think I would want to close the connection regardless of the type of error the "QUIT" command generates. 3) The point of the print_welcome_msg() method is to be sure that I have actually successfully connected to the FTP server. Is there a better way to check for connection success? 4) As this class stuff is mostly treading new ground for me, am I doing anything that I should not be doing or should do in a more preferred way? Keep in mind that I am just starting on this code today. As always, many thanks in advance! -- boB From alan.gauld at yahoo.co.uk Thu Jun 16 12:40:39 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 16 Jun 2016 17:40:39 +0100 Subject: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP() In-Reply-To: References: Message-ID: On 16/06/16 16:38, boB Stepp wrote: > class FTPFiles(FTP, object): > """FTP files to Windows server location(s).""" OK, First comment. You describe this as an action(verb) rather than a noun. Classes should represent nouns (objects) not actions. FTP represents a protocol connection with which you can do things (one of which is put/get files) you class should be the same. (Remember inheritance m,eans you are imp0lementing an "is a" relationship. So FTPFiles is an FTP...or should be.) > def __init__(self, host=server_ip, user=user, passwd=passwd): > """Initialize FTPFiles object. Normally the defaults will be used.""" > > super(FTPFiles, self).__init__(host, user, passwd) > self.host = host > self.user = user > self.passwd = passwd > > def print_welcome_msg(self): > """Print welcome message sent by FTP server.""" > print self.getwelcome() > 1) FTP from ftplib appears to be an old-style class. Using Python 2.4 that's not too surprising, FTP is an old module. > 4) I have to use "super(FTPFiles, self).__init__(host, user, passwd)" > or I cannot successfully inherit from the FTP() class. Also, "self" > apparently must come AFTER "FTPFiles" in the super expression. That's the v2 super(). v3 super is far supeerior. > 4) As this class stuff is mostly treading new ground for me, am I > doing anything that I should not be doing or should do in a more > preferred way? Keep in mind that I am just starting on this code > today. I'll leave someone with more ftplib experience to answer the other points but the question I'd ask is what are you planning to add to FTP? Creating a new class by inheritance implies that you are going to be extending (or specializing) its capabilities in some way. What are you planning to extend/specialize? For example are you going to limit it to manipulating files only? ie prevent listing directories say? You need to know what you are changing to warrant using inheritance. Inheritance is a powerful tool but it carries lots of potential for problems too, especially if you plan on mixing your new class in with the old one (or sharing it with others who will) - you need to be careful to abide by the Liskov substitution principle (LSP). -- 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 17 11:14:23 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 18 Jun 2016 01:14:23 +1000 Subject: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP() In-Reply-To: References: Message-ID: <20160617151422.GG27919@ando.pearwood.info> On Thu, Jun 16, 2016 at 10:38:13AM -0500, boB Stepp wrote: > I am extremely gradually trying to dip my big toe into the waters of > writing classes. [...] > What I learned today: > > 1) FTP from ftplib appears to be an old-style class. > > 2) I can't just use "class FTPFiles(FTP)" or I will be using old-style classes. > > 3) I need to use "class FTPFiles(FTP, object)" to use new-style > classes and "object" must come AFTER "FTP". > > 4) I have to use "super(FTPFiles, self).__init__(host, user, passwd)" > or I cannot successfully inherit from the FTP() class. Also, "self" > apparently must come AFTER "FTPFiles" in the super expression. > > Questions: > > 1) Are there any more common "gotchas" that might be coming my way in > trying to use a new-style class inheriting from an old-style class in > ancient Py 2.4.4? That's a very interesting question. I *think* the answer is "No", but I wouldn't put money on it. > 2) I don't have much knowledge about FTP processes. This is being > used on a secure intranet environment. And I am using the ftplib for > the first time. In its docs it mentioned that the quit() method is > the "polite" way to close the connection, but that an exception may be > raised if the responds with an error to the "QUIT" command the method > sends. If this happens, will the connection close? According to the source code, yes. > Or should I do something like: > > try: > ftp.quit() > except: > ftp.close() > > ? It seems here (If I should use the "try" block.) that a bare > "except" is appropriate as I would think I would want to close the > connection regardless of the type of error the "QUIT" command > generates. Bare except is nearly never appropriate. Consider: try: fpt.quit() except: ftp.close() > 3) The point of the print_welcome_msg() method is to be sure that I > have actually successfully connected to the FTP server. Is there a > better way to check for connection success? If the connect method doesn't raise an exception, it connected successfully. -- Steve From steve at pearwood.info Fri Jun 17 11:31:41 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 18 Jun 2016 01:31:41 +1000 Subject: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP() In-Reply-To: References: Message-ID: <20160617153141.GH27919@ando.pearwood.info> Some futher thoughts: On Thu, Jun 16, 2016 at 10:38:13AM -0500, boB Stepp wrote: > class FTPFiles(FTP, object): > """FTP files to Windows server location(s).""" > > def __init__(self, host=server_ip, user=user, passwd=passwd): > """Initialize FTPFiles object. Normally the defaults will be used.""" > > super(FTPFiles, self).__init__(host, user, passwd) > self.host = host > self.user = user > self.passwd = passwd Do you actually need to record these? Once they're used to establish a connection and login, what are they for? > def print_welcome_msg(self): > """Print welcome message sent by FTP server.""" > print self.getwelcome() The getwelcome method already prints the message, which is a bad design. But only if debugging is true. So I would approach this in one of two ways: (1) Delegate to the existing getwelcome method: def print_welcome_msg(self): save_flag = self.debugging self.debugging = True try: x = self.getwelcome() # prints the msg finally: self.debugging = save_flag (2) Re-implement the print functionality. def print_welcome_msg(self): print "*welcome*", self.welcome (3) If you really want to be paranoid, use: print "*welcome*", self.sanitize(self.welcome) although I don't think it's likely to make any real difference in practice. (The sanitize method makes a feeble attempt to hide the password.) > if __name__ == '__main__': > ftp = FTPFiles() > ftp.print_welcome_msg() > ftp.quit() This appears to be equivalent to: ftp = FTP(host, user, passwd) ftp.debugging = True x = ftp.getwelcome() ftp.quit() > What I learned today: > > 1) FTP from ftplib appears to be an old-style class. > > 2) I can't just use "class FTPFiles(FTP)" or I will be using old-style classes. Is this a problem? Old-style classes are good enough for many purposes. > 3) I need to use "class FTPFiles(FTP, object)" to use new-style > classes and "object" must come AFTER "FTP". > > 4) I have to use "super(FTPFiles, self).__init__(host, user, passwd)" > or I cannot successfully inherit from the FTP() class. Also, "self" > apparently must come AFTER "FTPFiles" in the super expression. For old-style classes, don't use super because it doesn't work. Instead just write: FTP.__init__(self, host, user, passwd) That means that you cannot engage in multiple inheritence with new-style classes, but MI is a serious pain to get right, and 99% of the time it is overkill, so you're not missing much. -- Steve From robertvstepp at gmail.com Fri Jun 17 11:41:43 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 17 Jun 2016 10:41:43 -0500 Subject: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP() In-Reply-To: References: Message-ID: On Thu, Jun 16, 2016 at 11:40 AM, Alan Gauld via Tutor wrote: > On 16/06/16 16:38, boB Stepp wrote: > >> class FTPFiles(FTP, object): >> """FTP files to Windows server location(s).""" > > OK, First comment. You describe this as an action(verb) > rather than a noun. Classes should represent nouns > (objects) not actions. FTP represents a protocol > connection with which you can do things (one of > which is put/get files) you class should be the same. > (Remember inheritance m,eans you are imp0lementing > an "is a" relationship. So FTPFiles is an FTP...or > should be.) I was struggling to come up with a good name here that would not cause me any name collision issues with the contents of ftplib.FTP(). I have for the moment changed the class name to "CustomFTP", which bothers me for some reason that I cannot put my finger on. However, this looks to become a non-issue; see below. >> 4) As this class stuff is mostly treading new ground for me, am I >> doing anything that I should not be doing or should do in a more >> preferred way? Keep in mind that I am just starting on this code >> today. > > I'll leave someone with more ftplib experience to answer the other > points but the question I'd ask is what are you planning to add to FTP? > Creating a new class by inheritance implies that you are going to be > extending (or specializing) its capabilities in some way. What are you > planning to extend/specialize? For example are you going to limit it to > manipulating files only? ie prevent listing directories say? My first objective was just to play around with inheritance as a learning opportunity. Now that I have gotten it to work and have somewhat a feel for the possibilities, I am actually going to abandon it for this specific project. I think I can use the FTP class unmodified and just write appropriate helper functions to get the specifics of what I want, passing my specific FTP instance object to these functions. > You need to know what you are changing to warrant using inheritance. > Inheritance is a powerful tool but it carries lots of potential for > problems too, especially if you plan on mixing your new class in with > the old one (or sharing it with others who will) - you need to be > careful to abide by the Liskov substitution principle (LSP). I looked up LSP last night. I can see how I can easily get burned even on something seemingly simple. One example, which I imagine is often used, is of a square class inheriting from a rectangle class. Squares have same sized sides; rectangles not necessarily so. So any size changing methods from the rectangle class inherited by the square class can potentially wreak havoc on squares. Am I getting the essence of the potential issues I might encounter? boB From robertvstepp at gmail.com Fri Jun 17 12:07:12 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 17 Jun 2016 11:07:12 -0500 Subject: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP() In-Reply-To: <20160617153141.GH27919@ando.pearwood.info> References: <20160617153141.GH27919@ando.pearwood.info> Message-ID: I guess I did not emphasize enough that I was just starting to think through this. The code I posted yesterday was more in the way of exploratory code, trying to understand how to implement inheritance, using new-style classes (I am trying to integrate the work with Python 3 at home as much as possible with what I am forced to in Python 2.4 at work where I have no choices as to Python version.). On Fri, Jun 17, 2016 at 10:31 AM, Steven D'Aprano wrote: > Some futher thoughts: > > On Thu, Jun 16, 2016 at 10:38:13AM -0500, boB Stepp wrote: >> class FTPFiles(FTP, object): >> """FTP files to Windows server location(s).""" >> >> def __init__(self, host=server_ip, user=user, passwd=passwd): >> """Initialize FTPFiles object. Normally the defaults will be used.""" >> >> super(FTPFiles, self).__init__(host, user, passwd) >> self.host = host >> self.user = user >> self.passwd = passwd > > Do you actually need to record these? Once they're used to establish a > connection and login, what are they for? No, I was just spelling everything out for myself while I was trying to figure out why my original code was not working. All the things I said I "learned" were not in my earlier versions. These lines have already been excised as I am getting closer to something I might actually want to implement. Also, now that I have had the opportunity to play around with inheritance, I don't see any need to specialize the FTP class as I mention in my response to Alan I just sent out. > >> def print_welcome_msg(self): >> """Print welcome message sent by FTP server.""" >> print self.getwelcome() > > The getwelcome method already prints the message, which is a bad design. > But only if debugging is true. So I would approach this in one of two > ways: The only point of this method was to ensure that I was actually connected to my server. During earlier versions, things were acting weird and I was not understanding what was going on. So I wanted to be certain I was in fact connected; it turned out I was not! This also has been excised today. > (1) Delegate to the existing getwelcome method: > > def print_welcome_msg(self): > save_flag = self.debugging > self.debugging = True > try: > x = self.getwelcome() # prints the msg > finally: > self.debugging = save_flag > > > (2) Re-implement the print functionality. > > def print_welcome_msg(self): > print "*welcome*", self.welcome > > > (3) If you really want to be paranoid, use: > > print "*welcome*", self.sanitize(self.welcome) > > > although I don't think it's likely to make any real difference in > practice. > > (The sanitize method makes a feeble attempt to hide the password.) Some interesting ideas to aid debugging. Thanks! >> What I learned today: >> >> 1) FTP from ftplib appears to be an old-style class. >> >> 2) I can't just use "class FTPFiles(FTP)" or I will be using old-style classes. > > Is this a problem? Old-style classes are good enough for many purposes. Not really. I am just trying to consolidate the studies I am doing at home with Python 3 as much as I can when I am working with Python 2. >> 3) I need to use "class FTPFiles(FTP, object)" to use new-style >> classes and "object" must come AFTER "FTP". >> >> 4) I have to use "super(FTPFiles, self).__init__(host, user, passwd)" >> or I cannot successfully inherit from the FTP() class. Also, "self" >> apparently must come AFTER "FTPFiles" in the super expression. > > For old-style classes, don't use super because it doesn't work. Instead > just write: > > FTP.__init__(self, host, user, passwd) Yeah, if I just stuck to old-style, things would have been easier. > That means that you cannot engage in multiple inheritence with new-style > classes, but MI is a serious pain to get right, and 99% of the time it > is overkill, so you're not missing much. Right now just the simpler OOP stuff is a "serious pain" while I am still in the relatively early stages of learning. ~(:>)) boB From alan.gauld at yahoo.co.uk Fri Jun 17 12:16:34 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 17 Jun 2016 17:16:34 +0100 Subject: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP() In-Reply-To: References: Message-ID: On 17/06/16 16:41, boB Stepp wrote: >> Inheritance is a powerful tool but it carries lots of potential for >> problems too,... > > I looked up LSP last night. I can see how I can easily get burned > even on something seemingly simple. One example, which I imagine is > often used, is of a square class inheriting from a rectangle class. > Squares have same sized sides; rectangles not necessarily so. So any > size changing methods from the rectangle class inherited by the square > class can potentially wreak havoc on squares. Am I getting the > essence of the potential issues I might encounter? Yes, that's one case. Another good example(in Java) is in the book "Effective Java". It uses the example of a superclass which is a collection and has add() and add_many() methods. Now let's say you want to create a counted collection so you override the add() method to add one to a total each time its called. Then you override add_many() to add the number of items in the params list. The problem is that, unknown to you, the inherited add_many() calls self.add() internally so you wind up double counting on add_many()... You need to know about the internals of the superclass to correctly implement your sub class, which breaks the concept of data hiding... There is no way round this its just one of the inherent(sic) issues with OOP, but a good reason to use delegation rather than inheritance if possible. Inheritance is a powerful tool but comes with sharp claws. (Its even worse in a static language like Java but even in Python there are plenty of opportunities to mess up). -- 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 michael.selik at gmail.com Fri Jun 17 11:57:25 2016 From: michael.selik at gmail.com (Michael Selik) Date: Fri, 17 Jun 2016 15:57:25 +0000 Subject: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP() In-Reply-To: References: Message-ID: On Fri, Jun 17, 2016 at 11:42 AM boB Stepp wrote: > On Thu, Jun 16, 2016 at 11:40 AM, Alan Gauld via Tutor > wrote: > > On 16/06/16 16:38, boB Stepp wrote: > > > >> class FTPFiles(FTP, object): > >> """FTP files to Windows server location(s).""" > > I was struggling to come up with a good name here that would not cause > me any name collision issues with the contents of ftplib.FTP(). That's why we have namespaces. Your ``FTP`` would not collide with ``ftplib.FTP``, because they are in separate modules. I looked up LSP last night. I can see how I can easily get burned > even on something seemingly simple. One example, which I imagine is > often used, is of a square class inheriting from a rectangle class. > Squares have same sized sides; rectangles not necessarily so. So any > size changing methods from the rectangle class inherited by the square > class can potentially wreak havoc on squares. Am I getting the > essence of the potential issues I might encounter? > Yes, that's the gist of it. It's very hard to anticipate what features your base class may need in the future, months or years down the road. Many of them may be inappropriate for one or more child classes. From robertvstepp at gmail.com Fri Jun 17 12:45:38 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 17 Jun 2016 11:45:38 -0500 Subject: [Tutor] Best way to do FTP login? Message-ID: I could do this: from ftplib import FTP ftp = FTP('ip_address', 'username', 'password') Or, I could do this: ftp = FTP('ip_address') ftp.login('username', 'password') Most of the examples I am seeing online use the second approach. Is there some reason why this is to be preferred? TIA! -- boB From lulu.jama2016 at gmail.com Fri Jun 17 15:18:47 2016 From: lulu.jama2016 at gmail.com (Lulu J) Date: Fri, 17 Jun 2016 15:18:47 -0400 Subject: [Tutor] help with comparing list of tuples in python 2 Message-ID: Hi there, My apologies if this is a trivial question but I am sort of new to python. Here is my problem: I have a list of dictionaries. Each dictionary has a word and its position in the text the positions are in the form of a tuple. Here is an example: [ {'position': (5, 4), 'term': u'happy',}, {'position': (5, 5), 'term': u'something'} ] for the potions, the first element is the paragraph number and the second is the word number in that paragraph(sequence from 1...n) What I would like to is find which words are next to each other. Meaning, they will be in the same paragraph and the difference of their word numbers is 1. Any help would be appreciated. Thank you From michael.selik at gmail.com Fri Jun 17 15:07:06 2016 From: michael.selik at gmail.com (Michael Selik) Date: Fri, 17 Jun 2016 19:07:06 +0000 Subject: [Tutor] Best way to do FTP login? In-Reply-To: References: Message-ID: On Fri, Jun 17, 2016 at 12:46 PM boB Stepp wrote: > ftp = FTP('ip_address', 'username', 'password') > > Or > > ftp = FTP('ip_address') > ftp.login('username', 'password') > > Most of the examples I am seeing online use the second approach. Is > there some reason why this is to be preferred? Not that I can see. Perhaps only to show that the login method exists. I suggest using it in a context manager. py> with FTP('ftp.example.com', 'username', 'password') as ftp: ... ftp.dir() From alan.gauld at yahoo.co.uk Fri Jun 17 18:36:05 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 17 Jun 2016 23:36:05 +0100 Subject: [Tutor] help with comparing list of tuples in python 2 In-Reply-To: References: Message-ID: On 17/06/16 20:18, Lulu J wrote: > I have a list of dictionaries. Each dictionary has a word and its position > in the text the positions are in the form of a tuple. > Here is an example: > [ > {'position': (5, 4), 'term': u'happy',}, > {'position': (5, 5), 'term': u'something'} > ] > > for the potions, the first element is the paragraph number and the second > is the word number in that paragraph(sequence from 1...n) > > What I would like to is find which words are next to each other. Meaning, > they will be in the same paragraph and the difference of their word numbers > is 1. You can sort them by providing a key function, for example, a simplified version:: >>> L = [{'k':1,'v':0},{'k':5,'v':9},{'k':2,'v':6},{'k':0,'v':12}] >>> sorted(L,key=lambda d: d['v']) [{'k': 1, 'v': 0}, {'k': 2, 'v': 6}, {'k': 5, 'v': 9}, {'k': 0, 'v': 12}] >>> sorted(L,key=lambda d: d['k']) [{'k': 0, 'v': 12}, {'k': 1, 'v': 0}, {'k': 2, 'v': 6}, {'k': 5, 'v': 9}] >>> Then filter your results to find an adjacent pair that have matching positions. (This may not be necessary if you have all of the words since they should naturally be placed adjacent to each other, but if you only have key words it will be needed) Something like (untested) neighbours = [item for index,item in enumerate(data) if item['position'][0] == data[index+1][position'][0] and item['position'][1] == data[index+1][position'][1]-1] But there are lots of possible gotchas here. - look out for the last item which will give you an index error! - what happens to words that appear more than once? Are they in your list more than once? - probably more :-( -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From cs at zip.com.au Fri Jun 17 18:36:13 2016 From: cs at zip.com.au (cs at zip.com.au) Date: Sat, 18 Jun 2016 08:36:13 +1000 Subject: [Tutor] Best way to do FTP login? In-Reply-To: References: Message-ID: <20160617223613.GA46205@cskk.homeip.net> On 17Jun2016 19:07, Michael Selik wrote: >On Fri, Jun 17, 2016 at 12:46 PM boB Stepp wrote: >> ftp = FTP('ip_address', 'username', 'password') >> >> Or >> >> ftp = FTP('ip_address') >> ftp.login('username', 'password') >> >> Most of the examples I am seeing online use the second approach. Is >> there some reason why this is to be preferred? > >Not that I can see. Perhaps only to show that the login method exists. >I suggest using it in a context manager. > > py> with FTP('ftp.example.com', 'username', 'password') as ftp: > ... ftp.dir() I would also recommend looking at the netrc module. This parses the well known file .netrc for login and password, avoiding putting it in your code. You can put clauses for multiple machine in the .netrc, which lets your code work against multiple machines without changes. Note that the .netrc file still keeps the login and password in the clear, but if you lock the permissions down on your .netrc so that only you can read it there is a degree of security. Cheers, Cameron Simpson From hershel at themillmans.net Sat Jun 18 02:25:40 2016 From: hershel at themillmans.net (Hershel Millman) Date: Fri, 17 Jun 2016 23:25:40 -0700 Subject: [Tutor] Turtle Message-ID: <5CE47628-2675-433C-A50C-90385830E4E3@themillmans.net> Hello tutors, I have been learning basic python on my Macintosh computer and everything I have tried so far has worked, except when I tried to use the turtle module. I then used my Linux computer and the turtle module still did not work. I tried to use the command line and type "sudo dnf install turtle", "sudo pip install turtle", and it told me "no package turtle available". I tried to install Python-tk and python3-tk, and I was told that no packages by those names were available. I also tried reinstalling python 2 and python 3, to no avail. I am running Python 2.7.1 using Mac OS X 10.6.8 on an iMac, Python 3.5.1 using Mac OS X 10.10.5, and the default python2 and Python3 that comes with fedora 24. On my Mac running Python 3, I have tried about 6 times to enter the command import turtle, and nothing has happened. However, on my most recent attempt, I changed nothing about the program and hit run, and for some reason it worked. All of the terms were still underlined in my pycharm window, and it told me that the commands were not found, and implied that they should not be working. However, after exiting pycharm, and reopening it, it worked again, even though it told me the commands were not valid. My question about running Python 3 on my Mac running 10.10.5: What do I need to do to make it so my computer recognizes that they are valid commands? My questions about running python 2.7 on my Mac running 10.6.8 and my computer running Python 2 and 3 on fedora 24: What do I have to do to get the turtle module? And once I have it, how do I use it? Thank you, Hershel From michael.selik at gmail.com Fri Jun 17 18:39:40 2016 From: michael.selik at gmail.com (Michael Selik) Date: Fri, 17 Jun 2016 22:39:40 +0000 Subject: [Tutor] help with comparing list of tuples in python 2 In-Reply-To: References: Message-ID: On Fri, Jun 17, 2016, 6:12 PM Lulu J wrote: > Hi there, > > My apologies if this is a trivial question but I am sort of new to python. > Here is my problem: > I have a list of dictionaries. Each dictionary has a word and its position > in the text the positions are in the form of a tuple. > Here is an example: > [ > {'position': (5, 4), 'term': u'happy',}, > {'position': (5, 5), 'term': u'something'} > ] > > for the potions, the first element is the paragraph number and the second > is the word number in that paragraph(sequence from 1...n) > > What I would like to is find which words are next to each other. Meaning, > they will be in the same paragraph and the difference of their word numbers > is 1. > Put the words in a dictionary, key is the location, value is the word. Sort the location-word pairs by location. Loop over the result pairwise. > From __peter__ at web.de Sat Jun 18 04:00:44 2016 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jun 2016 10:00:44 +0200 Subject: [Tutor] help with comparing list of tuples in python 2 References: Message-ID: Lulu J wrote: > Hi there, > > My apologies if this is a trivial question but I am sort of new to python. > Here is my problem: > I have a list of dictionaries. Each dictionary has a word and its position > in the text the positions are in the form of a tuple. > Here is an example: > [ > {'position': (5, 4), 'term': u'happy',}, > {'position': (5, 5), 'term': u'something'} > ] How did you get your data? Perhaps it is possible to store it in a different data structure in the first place, one that is a better fit for your problem. > for the potions, the first element is the paragraph number and the second > is the word number in that paragraph(sequence from 1...n) > > What I would like to is find which words are next to each other. Meaning, > they will be in the same paragraph and the difference of their word > numbers is 1. What is the actual question your script has to answer? Is it (1) What word precedes/follows the third word in the sixth paragraph? or (2) What words precede/follow the word u"happy"? or something else I didn't think of? For (2) here is a not-so-short self-contained example: $ cat neighbors.py import pprint import sys try: raw_input except NameError: raw_input = input # Python 3 SAMPLE = u"""\ The sky is blue Press the blue button This is pie in the sky """ def gen_terms(paragraphs): """Generate {"position": (para_index, word_index), "term": word} dicts. A minimalistic emulation of what your code does. """ for para_index, para in enumerate(paragraphs): for word_index, word in enumerate(para.lower().split()): yield {"position": (para_index, word_index), "term": word} def find_neighbors(word, position_to_word, word_to_position): before = set() # use collections.Counter() if you are after = set() # interested in frequencies for para_index, word_index in word_to_position[word]: try: left_word = position_to_word[para_index, word_index-1] except KeyError: pass # first word in paragraph else: before.add(left_word) try: right_word = position_to_word[para_index, word_index+1] except KeyError: pass # last word in paragraph else: after.add(right_word) return before, after def format_words(words): return ", ".join(sorted(words)) or "" def build_lookup_tables(terms): # map word to all positions in the text word_to_position = {} # map position to the corresponding word position_to_word = {} for term in terms: pos = term["position"] word = term["term"] word_to_position.setdefault(word, []).append(pos) position_to_word[pos] = word return word_to_position, position_to_word def normalize(word): try: word = word.decode() except AttributeError: pass # Python 3 return word.lower() def main(): verbose = "--verbose" in sys.argv if verbose: print("Original text:") print(SAMPLE) terms = list(gen_terms(SAMPLE.lower().splitlines())) if verbose: print("list of terms:") pprint.pprint(terms) print("") word_to_position, position_to_word = build_lookup_tables(terms) if verbose: print("word --> position") pprint.pprint(dict(word_to_position)) print("position --> word") pprint.pprint(position_to_word) print("") while True: word = normalize(raw_input("enter a word: ")) if not word: print("Bye!") break elif word not in word_to_position: print("Unknown word, enter one of these:") print(format_words(word_to_position)) print("") else: before, after = find_neighbors( word, position_to_word=position_to_word, word_to_position=word_to_position ) print(u"These words occur before '{}'".format(word)) print(format_words(before)) print(u"These words occur after '{}'".format(word)) print(format_words(after)) print("") if __name__ == "__main__": main() Let's run it in verbose mode so that you can see the data structures used: $ python neighbors.py --verbose Original text: The sky is blue Press the blue button This is pie in the sky list of terms: [{'position': (0, 0), 'term': u'the'}, {'position': (0, 1), 'term': u'sky'}, {'position': (0, 2), 'term': u'is'}, {'position': (0, 3), 'term': u'blue'}, {'position': (1, 0), 'term': u'press'}, {'position': (1, 1), 'term': u'the'}, {'position': (1, 2), 'term': u'blue'}, {'position': (1, 3), 'term': u'button'}, {'position': (2, 0), 'term': u'this'}, {'position': (2, 1), 'term': u'is'}, {'position': (2, 2), 'term': u'pie'}, {'position': (2, 3), 'term': u'in'}, {'position': (2, 4), 'term': u'the'}, {'position': (2, 5), 'term': u'sky'}] word --> position {u'blue': [(0, 3), (1, 2)], u'button': [(1, 3)], u'in': [(2, 3)], u'is': [(0, 2), (2, 1)], u'pie': [(2, 2)], u'press': [(1, 0)], u'sky': [(0, 1), (2, 5)], u'the': [(0, 0), (1, 1), (2, 4)], u'this': [(2, 0)]} position --> word {(0, 0): u'the', (0, 1): u'sky', (0, 2): u'is', (0, 3): u'blue', (1, 0): u'press', (1, 1): u'the', (1, 2): u'blue', (1, 3): u'button', (2, 0): u'this', (2, 1): u'is', (2, 2): u'pie', (2, 3): u'in', (2, 4): u'the', (2, 5): u'sky'} enter a word: foo Unknown word, enter one of these: blue, button, in, is, pie, press, sky, the, this enter a word: is These words occur before 'is' sky, this These words occur after 'is' blue, pie enter a word: press These words occur before 'press' These words occur after 'press' the enter a word: button These words occur before 'button' blue These words occur after 'button' enter a word: Bye! $ From steve at pearwood.info Sat Jun 18 05:00:19 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 18 Jun 2016 19:00:19 +1000 Subject: [Tutor] Turtle In-Reply-To: <5CE47628-2675-433C-A50C-90385830E4E3@themillmans.net> References: <5CE47628-2675-433C-A50C-90385830E4E3@themillmans.net> Message-ID: <20160618090019.GJ27919@ando.pearwood.info> On Fri, Jun 17, 2016 at 11:25:40PM -0700, Hershel Millman wrote: > Hello tutors, > > I have been learning basic python on my Macintosh computer and > everything I have tried so far has worked, except when I tried to use > the turtle module. I then used my Linux computer and the turtle module > still did not work. What does "did not work" mean? Did you get an error message? Blue Screen Of Death? Computer caught fire? I see later on that you are running PyCharm. Let's try this with the default Python interpreter. Open a terminal window. You should see a prompt ending with a dollar sign $ e.g. on my computer I see: [steve at ando ~]$ Type the command "python" without quotes, and hit Enter. You should get a message about the version of Python you are running, and a >>> prompt. This is the Python interpreter. Now enter: import turtle What happens? Do you get an error? If not, what happens if you enter these lines? turtle.shape('turtle') for i in range(4): turtle.right(90) turtle.forward(200) (Hit the TAB key at the beginning of the last two lines to get the indent. When you have finished typing, you will need to enter one extra time to have Python run the code.) Describe what happens. Do you get an error? If you do, copy and paste the ENTIRE error message, starting with the line "Traceback". > I tried to use the command line and type "sudo dnf > install turtle", "sudo pip install turtle", and it told me "no package > turtle available". That's because turtle is part of the standard library. It's already installed. If you run: locate turtle.py from your Linux or Mac OS X shell (not from Python!) you should see a list of at least one turtle.py files. > I tried to install Python-tk and python3-tk, and I > was told that no packages by those names were available. I also tried > reinstalling python 2 and python 3, to no avail. Reinstalling Python should be the *last* resort, not the first, or second. -- Steve From robertvstepp at gmail.com Sat Jun 18 15:04:46 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 18 Jun 2016 14:04:46 -0500 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? Message-ID: I have (Finally!) gotten a bit of time to look at Peter's answer to my Model-View-Controller question from May 29th, particularly his CircleImageView class to which he added a "#FIXME" comment. I thought it would be helpful to abbreviate his distance function in the interpreter while I played around with pencil and graph paper. I got: Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. py3: def d(row, col/2, radius=5): File "", line 1 def d(row, col/2, radius=5): ^ SyntaxError: invalid syntax And this surprised me. It seems that only identifiers are allowed as parameters in a function definition statement, and I cannot help but wonder why? It seems that in most other places in Python's syntax it will allow one to insert almost any kind of object or expression. TIA! -- boB From joel.goldstick at gmail.com Sat Jun 18 16:14:44 2016 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 18 Jun 2016 16:14:44 -0400 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: References: Message-ID: On Sat, Jun 18, 2016 at 3:04 PM, boB Stepp wrote: > I have (Finally!) gotten a bit of time to look at Peter's answer to my > Model-View-Controller question from May 29th, particularly his > CircleImageView class to which he added a "#FIXME" comment. I thought > it would be helpful to abbreviate his distance function in the > interpreter while I played around with pencil and graph paper. I got: > > Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 > 64 bit (AMD64)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > py3: def d(row, col/2, radius=5): > File "", line 1 > def d(row, col/2, radius=5): > ^ > SyntaxError: invalid syntax > > And this surprised me. It seems that only identifiers are allowed as > parameters in a function definition statement, and I cannot help but > wonder why? It seems that in most other places in Python's syntax it > will allow one to insert almost any kind of object or expression. > > TIA! > > -- > boB I'll take a stab. The function is defined once. The parameters name the arguments to be passed when the function is invoked. They can have defaults, but you are asking it to perform a calculation, which would only be done when the function is defined. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From robertvstepp at gmail.com Sat Jun 18 16:33:35 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 18 Jun 2016 15:33:35 -0500 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: References: Message-ID: On Sat, Jun 18, 2016 at 3:14 PM, Joel Goldstick wrote: > On Sat, Jun 18, 2016 at 3:04 PM, boB Stepp wrote: >> Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 >> 64 bit (AMD64)] on win32 >> Type "help", "copyright", "credits" or "license" for more information. >> py3: def d(row, col/2, radius=5): >> File "", line 1 >> def d(row, col/2, radius=5): >> ^ >> SyntaxError: invalid syntax >> >> And this surprised me. It seems that only identifiers are allowed as >> parameters in a function definition statement, and I cannot help but >> wonder why? It seems that in most other places in Python's syntax it >> will allow one to insert almost any kind of object or expression. > I'll take a stab. The function is defined once. The parameters name > the arguments to be passed when the function is invoked. They can > have defaults, but you are asking it to perform a calculation, which > would only be done when the function is defined. In retrospect, I probably should have figured this out. I know that defaults to parameters are assigned at function definition time and that arguments only get passed at function call time. If I use an expression, at function definition time there is no value to assign. So as long as Python uses this mechanism for handling function definition, I now don't see how expressions can be usable as parameters. Thanks Joel! boB From alan.gauld at yahoo.co.uk Sat Jun 18 20:14:44 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 19 Jun 2016 01:14:44 +0100 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: References: Message-ID: On 18/06/16 20:04, boB Stepp wrote: > py3: def d(row, col/2, radius=5): > File "", line 1 > def d(row, col/2, radius=5): > ^ > SyntaxError: invalid syntax > > And this surprised me. It seems that only identifiers are allowed as > parameters in a function definition statement, and I cannot help but > wonder why? What are the parameters (as opposed to the arguments)? They are names. They are keys in the local dictionary used by the function to look up the values passed in as arguments. You cannot use an expression as a key in a dictionary. If you try, Python tries to evaluate the expression and uses the result if its valid or throws a nameerror etc. 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 Sat Jun 18 22:10:28 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 Jun 2016 12:10:28 +1000 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: References: Message-ID: <20160619021027.GM27919@ando.pearwood.info> On Sat, Jun 18, 2016 at 02:04:46PM -0500, boB Stepp wrote: > I have (Finally!) gotten a bit of time to look at Peter's answer to my > Model-View-Controller question from May 29th, particularly his > CircleImageView class to which he added a "#FIXME" comment. I thought > it would be helpful to abbreviate his distance function in the > interpreter while I played around with pencil and graph paper. I got: > > Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 > 64 bit (AMD64)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > py3: def d(row, col/2, radius=5): > File "", line 1 > def d(row, col/2, radius=5): > ^ > SyntaxError: invalid syntax > > And this surprised me. I'm surprised that you're surprised. I don't even know what you expect a parameter col/2 would even mean. > It seems that only identifiers are allowed as > parameters in a function definition statement, and I cannot help but > wonder why? Because they are parameters, which by definition are variable names, i.e. identifiers. What else could they be? def foo(x, 1+2, y): # how do I refer to the second parameter here? foo(1000, 2000, 3000) # what happens to the second argument here? Can you explain what you expected def d(row, col/2) to mean? I have literally no idea. > It seems that in most other places in Python's syntax it > will allow one to insert almost any kind of object or expression. You can't use arbitrary expressions on the left hand side of assignment: 1 + 2 = "some value" x/2 = y Function parameters are a form of assignment. -- Steve From robertvstepp at gmail.com Sat Jun 18 22:58:03 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 18 Jun 2016 21:58:03 -0500 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: <20160619021027.GM27919@ando.pearwood.info> References: <20160619021027.GM27919@ando.pearwood.info> Message-ID: On Sat, Jun 18, 2016 at 9:10 PM, Steven D'Aprano wrote: > On Sat, Jun 18, 2016 at 02:04:46PM -0500, boB Stepp wrote: > Can you explain what you expected > > def d(row, col/2) > > to mean? I have literally no idea. You know Steve, as I was typing the beginning of a reply responding to a similar question you asked earlier in your response, I suddenly realized how ridiculous having a parameter of 'col/2' is! I'll just have either eat crow or attribute this to a brain fart. You pick! Cheers! -- boB From dyoo at hashcollision.org Sun Jun 19 00:04:10 2016 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 18 Jun 2016 21:04:10 -0700 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: References: <20160619021027.GM27919@ando.pearwood.info> Message-ID: > You know Steve, as I was typing the beginning of a reply responding to > a similar question you asked earlier in your response, I suddenly > realized how ridiculous having a parameter of 'col/2' is! I'll just > have either eat crow or attribute this to a brain fart. You pick! Just to play devil's advocate: it's not crazy. Essentially, what you're asking for is called "pattern matching", and it is done in a class of many programming languages. One of the more well known of these is Prolog. https://en.wikipedia.org/wiki/Prolog. Other forms of pattern matching show up in ML, and it *is* used in industry. (e.g. Microsoft's F#.) It's a bit out of scope to talk about this much here, but I just wanted to chime in here to say that you are not ridiculous. :P But Python does not have a robust pattern matching facility; the closest it has is a limited form of tuple matching: ###################### > def f((x,y), z): ... print x, y, z ... > f([1, 2], 3) 1 2 3 ###################### with very limited applicability and rarely used. I hope everyone is well! From robertvstepp at gmail.com Sun Jun 19 00:18:41 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 18 Jun 2016 23:18:41 -0500 Subject: [Tutor] Correct use of model-view-controller design pattern In-Reply-To: References: Message-ID: On Sun, May 29, 2016 at 9:28 AM, Peter Otten <__peter__ at web.de> wrote: > Here's a commandline example with two views (the circle-drawing routine has > to be fixed)... I have to admit that now that I have finally gotten around to your code, I am left scratching my head. My main issue is what am I aiming at for a final CLI display of a circle? This is not such a simple thing! First, what are the units for a circle's radius in a CLI? If it is 1 radius unit = 1 character, then your code is displaying too many characters. To me it seems more natural to use 1 character as the basic unit of measurement. But if that is done, then the output looks more like a non-circular ellipse with the longer axis being vertical. On my Windows PowerShell for the font I am using, each character space is 8 pixels wide and 14 pixels wide. I suspect this is why your code uses range(2*diameter) and column/2, to compensate for the built in stretching in the vertical direction due to how characters are displayed. So my current best version of your code modifies your distance function as well as your CircleImageView class. I am not totally happy with it. I think I am still missing something, but it tries to adhere to 1 measurement unit = 1 character based on my display's font. > def dist(ax, ay, bx, by): > return ((ax-bx)**2 + (ay-by)**2)**.5 I replaced this with: def dist(ax, ay, bx, by): # Each character is 8 pixels wide by 14 pixels high. width_px = 8 height_px = 14 return (((ax-bx)*width_px)**2 + ((ay-by)*height_px)**2)**0.5 I also flipped how you used ax, ay and bx, by as I associate rows with y-coordinates and columns with x-coordinates. > > class CircleImageView(CircleView): > > def changed(self, model): > self.update_radius(model.radius) > > def update_radius(self, radius): > # FIXME > diameter = 2*radius > cx = cy = radius > for row in range(diameter): > for column in range(2*diameter): > if dist(cx, cy, row, column/2) < radius: > print("*", end="") > else: > print(" ", end="") > print() And I replaced this (Renaming some identifiers to make it clearer to me) with: class CircleImageView(CircleView): def changed(self, model): self.update_radius_in_chars(model.radius_in_chars) def update_radius_in_chars(self, radius_in_chars): diameter_in_chars = 2 * radius_in_chars cx = cy = radius_in_chars for row in range(diameter_in_chars + 1): for column in range(diameter_in_chars + 1): if dist(cx, cy, column, row) <= dist(cx, 0, 0, 0): print("O", end="") else: print(" ", end="") print() I did a '+ 1' in the range statements since a character should be displayed in the center of the circle from which the radius is measured. I used '<= dist(cx, 0, 0, 0)'. First, if it is equal to the radius, I feel a character should be displayed. Second, I used 'cx' instead of 'cy' to keep the result from being stretched vertically. Finally, I felt I needed to use some form of absolute distance calculation, so the distance function converts everything to pixels, which is what everything ultimately is displayed in anyway on a monitor. Doing things this way, I feel my displayed character circles look better as the radius increases. Some edited (I just removed blank lines.) sample output (I changed from asterisks to capital ohs): OOOOOOO OOOOOOOOO OOOOOOOOOOO OOOOOOOOO OOOOOOO Circle radius_in_chars is now 5 Model-View-Controler Demo Enter an integer to set the circle radius or 'grow' to increase the radius or 'shrink' to decrease the radius or 'add' to add another CircleValueView radius or 'grow' or 'shrink' or 'add': 6 OOOOO OOOOOOOOO OOOOOOOOOOO OOOOOOOOOOOOO OOOOOOOOOOO OOOOOOOOO OOOOO radius or 'grow' or 'shrink' or 'add': 13 OOOOOOOOO OOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOO OOOOOOOOO I have to say, I did a lot of playing around on graph paper with a pencil and a compass. If I actually draw a circle with a compass and fill in marks for a distance from the circle's center, I get a very unsatisfactory result! I still have more conceptual work to do on your code. I still do not fully get decorators. In your code I get the impression that you are trying to be more formal in how you are writing your classes so as to have getter and setter methods, as well as having more explicit separation between model, view and controller. And I haven't made it to your tkinter version at all! BTW, thank you very much for putting in such effort to give me TWO versions of MVC code to explore! boB From dyoo at hashcollision.org Sun Jun 19 00:20:21 2016 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 18 Jun 2016 21:20:21 -0700 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: <20160619021027.GM27919@ando.pearwood.info> References: <20160619021027.GM27919@ando.pearwood.info> Message-ID: One other thing to add: the notion of pattern matching is often used in mathematics. For example, if we want to define a function that does something special to even numbers vs. odd numbers, it's not uncommon to see the following when we want to describe a function `f`: for `n` in the natural numbers: f(2n) = f(2n+1) = where it's notationally convenient to express a function as a collection of multiple cases, where we decide which case to use by pattern matching against the argument to find the appropriate case to use. We can contrast this style versus one which puts the case analysis in the body of the function itself. Again, it's just that we don't have direct support for this kind of thing in Python. On Sat, Jun 18, 2016 at 7:10 PM, Steven D'Aprano wrote: > On Sat, Jun 18, 2016 at 02:04:46PM -0500, boB Stepp wrote: >> I have (Finally!) gotten a bit of time to look at Peter's answer to my >> Model-View-Controller question from May 29th, particularly his >> CircleImageView class to which he added a "#FIXME" comment. I thought >> it would be helpful to abbreviate his distance function in the >> interpreter while I played around with pencil and graph paper. I got: >> >> Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 >> 64 bit (AMD64)] on win32 >> Type "help", "copyright", "credits" or "license" for more information. >> py3: def d(row, col/2, radius=5): >> File "", line 1 >> def d(row, col/2, radius=5): >> ^ >> SyntaxError: invalid syntax >> >> And this surprised me. > > I'm surprised that you're surprised. I don't even know what you expect a > parameter col/2 would even mean. > >> It seems that only identifiers are allowed as >> parameters in a function definition statement, and I cannot help but >> wonder why? > > Because they are parameters, which by definition are variable names, > i.e. identifiers. What else could they be? > > > def foo(x, 1+2, y): > # how do I refer to the second parameter here? > > foo(1000, 2000, 3000) # what happens to the second argument here? > > > Can you explain what you expected > > def d(row, col/2) > > to mean? I have literally no idea. > >> It seems that in most other places in Python's syntax it >> will allow one to insert almost any kind of object or expression. > > You can't use arbitrary expressions on the left hand side of > assignment: > > 1 + 2 = "some value" > x/2 = y > > Function parameters are a form of assignment. > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From robertvstepp at gmail.com Sun Jun 19 00:30:23 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 18 Jun 2016 23:30:23 -0500 Subject: [Tutor] Correct use of model-view-controller design pattern In-Reply-To: References: Message-ID: On Sat, Jun 18, 2016 at 11:18 PM, boB Stepp wrote: > On Sun, May 29, 2016 at 9:28 AM, Peter Otten <__peter__ at web.de> wrote: > >> Here's a commandline example with two views (the circle-drawing routine has >> to be fixed)... > > I have to admit that now that I have finally gotten around to your > code, I am left scratching my head. My main issue is what am I aiming > at for a final CLI display of a circle? This is not such a simple > thing! First, what are the units for a circle's radius in a CLI? If > it is 1 radius unit = 1 character, then your code is displaying too > many characters. To me it seems more natural to use 1 character as > the basic unit of measurement. But if that is done, then the output > looks more like a non-circular ellipse with the longer axis being > vertical. On my Windows PowerShell for the font I am using, each > character space is 8 pixels wide and 14 pixels wide... That should be "...and 14 pixels high ...". Guess I'm sleepy! boB From eryksun at gmail.com Sun Jun 19 01:35:46 2016 From: eryksun at gmail.com (eryk sun) Date: Sun, 19 Jun 2016 05:35:46 +0000 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: References: <20160619021027.GM27919@ando.pearwood.info> Message-ID: On Sun, Jun 19, 2016 at 4:04 AM, Danny Yoo wrote: > >> def f((x,y), z): > ... print x, y, z > ... >> f([1, 2], 3) > 1 2 3 Note that Python 3 doesn't support tuple parameter unpacking. See PEP 3113: https://www.python.org/dev/peps/pep-3113 From hershel at themillmans.net Sat Jun 18 23:46:53 2016 From: hershel at themillmans.net (Hershel Millman) Date: Sat, 18 Jun 2016 20:46:53 -0700 Subject: [Tutor] Fwd: Turtle References: <6B20D144-9937-430C-963D-CEF798CC4EBE@themillmans.net> Message-ID: <158F8E6B-9E1A-4744-A47C-84D0B95904E5@themillmans.net> Begin forwarded message: > From: Hershel Millman > Date: June 18, 2016 2:39:21 PM MST > To: Steven D'Aprano > Subject: Re: [Tutor] Turtle > > Steven, > > I followed your instruction and typed "import turtle" into the terminal on my mac, and nothing happened. > > When I entered the following, a new window was created in which the turtle behaved exactly like it was supposed to. > > for i in range(4): > ... turtle.right(90) > ... turtle.forward(200) > > In pycharm, when I enter the following, it replies with the following error message: > > from turtle import * > > def drawSquare(size=100): > turtle.pendown() > turtle.forward(size) > turtle.left(90) > turtle.forward(size) > turtle.left(90) > turtle.forward(size) > turtle.left(90) > turtle.forward(size) > turtle.left(90) > > drawSquare(50) > > input () > > Traceback (most recent call last): > File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 14, in > drawSquare(50) > File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 4, in drawSquare > turtle.pendown() > NameError: global name 'turtle' is not defined > > Process finished with exit code 1 > > Thank you, > > Hershel > > On Jun 18, 2016, at 2:00 AM, Steven D'Aprano wrote: > >> On Fri, Jun 17, 2016 at 11:25:40PM -0700, Hershel Millman wrote: >>> Hello tutors, >>> >>> I have been learning basic python on my Macintosh computer and >>> everything I have tried so far has worked, except when I tried to use >>> the turtle module. I then used my Linux computer and the turtle module >>> still did not work. >> >> What does "did not work" mean? >> >> Did you get an error message? Blue Screen Of Death? Computer caught >> fire? >> >> I see later on that you are running PyCharm. Let's try this with the >> default Python interpreter. Open a terminal window. You should see a >> prompt ending with a dollar sign $ e.g. on my computer I see: >> >> [steve at ando ~]$ >> >> Type the command "python" without quotes, and hit Enter. You should get >> a message about the version of Python you are running, and a >>> prompt. >> This is the Python interpreter. Now enter: >> >> import turtle >> >> What happens? Do you get an error? >> >> If not, what happens if you enter these lines? >> >> turtle.shape('turtle') >> for i in range(4): >> turtle.right(90) >> turtle.forward(200) >> >> (Hit the TAB key at the beginning of the last two lines to get the >> indent. When you have finished typing, you will need to enter one extra >> time to have Python run the code.) >> >> Describe what happens. Do you get an error? If you do, copy and paste >> the ENTIRE error message, starting with the line "Traceback". >> >> >> >>> I tried to use the command line and type "sudo dnf >>> install turtle", "sudo pip install turtle", and it told me "no package >>> turtle available". >> >> That's because turtle is part of the standard library. It's already >> installed. If you run: >> >> locate turtle.py >> >> from your Linux or Mac OS X shell (not from Python!) you should see a >> list of at least one turtle.py files. >> >> >>> I tried to install Python-tk and python3-tk, and I >>> was told that no packages by those names were available. I also tried >>> reinstalling python 2 and python 3, to no avail. >> >> Reinstalling Python should be the *last* resort, not the first, or >> second. >> >> >> >> -- >> 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 Sun Jun 19 03:59:51 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 19 Jun 2016 08:59:51 +0100 Subject: [Tutor] Correct use of model-view-controller design pattern In-Reply-To: References: Message-ID: On 19/06/16 05:18, boB Stepp wrote: > code, I am left scratching my head. My main issue is what am I aiming > at for a final CLI display of a circle? This is not such a simple > thing! First, what are the units for a circle's radius in a CLI? If > it is 1 radius unit = 1 character, then your code is displaying too > many characters. CLI "ASCII art" is dependent on the font used. If you change your font size to an 8x8 character set then it will be much easier (but your text will look awful!). That's why we tend to use GUIs to do imagery. :-) The effort to make a circle look circular in a CLI is vastly disproportionate to the benefit since you need to detect font sizes etc and have compensating parameters that you can multiply by. And if the user picks a non mono-space font things get even more difficult. Basically its hardly ever worth 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 Sun Jun 19 04:47:31 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 Jun 2016 18:47:31 +1000 Subject: [Tutor] Why are expressions not allowed as parameters in function definition statements? In-Reply-To: References: <20160619021027.GM27919@ando.pearwood.info> Message-ID: <20160619084731.GQ27919@ando.pearwood.info> On Sat, Jun 18, 2016 at 09:04:10PM -0700, Danny Yoo wrote: > > You know Steve, as I was typing the beginning of a reply responding to > > a similar question you asked earlier in your response, I suddenly > > realized how ridiculous having a parameter of 'col/2' is! I'll just > > have either eat crow or attribute this to a brain fart. You pick! > > Just to play devil's advocate: it's not crazy. Essentially, what > you're asking for is called "pattern matching", and it is done in a > class of many programming languages. Ah, I didn't think of pattern matching. Another good example is Haskell. But still, even with pattern matching, I'm not sure that col/2 would be a useful pattern. That would match any number. [...] > It's a bit out of scope to talk about this much here, but I just > wanted to chime in here to say that you are not ridiculous. :P But > Python does not have a robust pattern matching facility; the closest > it has is a limited form of tuple matching: > > ###################### > > def f((x,y), z): > ... print x, y, z > ... > > f([1, 2], 3) > 1 2 3 > ###################### That's not so much *matching tuples* as expanding any iterable object. It is equivalent to: def f(_tmp, z): x, y = _tmp del _tmp print x, y, z > with very limited applicability and rarely used. In Python 2, it's unusual. In Python 3, it's impossible, as the facility is removed from the language. -- Steve From alan.gauld at yahoo.co.uk Sun Jun 19 04:47:44 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 19 Jun 2016 09:47:44 +0100 Subject: [Tutor] Fwd: Turtle In-Reply-To: <158F8E6B-9E1A-4744-A47C-84D0B95904E5@themillmans.net> References: <6B20D144-9937-430C-963D-CEF798CC4EBE@themillmans.net> <158F8E6B-9E1A-4744-A47C-84D0B95904E5@themillmans.net> Message-ID: On 19/06/16 04:46, Hershel Millman wrote: >> In pycharm, when I enter the following, it replies with the following error message: >> >> from turtle import * >> Change that to import turtle and it should work. >> def drawSquare(size=100): >> turtle.pendown() >> turtle.forward(size) >> turtle.left(90) >> turtle.forward(size) >> turtle.left(90) >> turtle.forward(size) >> turtle.left(90) >> turtle.forward(size) >> turtle.left(90) >> >> drawSquare(50) >> >> input () -- 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 Sun Jun 19 06:09:50 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 Jun 2016 20:09:50 +1000 Subject: [Tutor] Fwd: Turtle In-Reply-To: <158F8E6B-9E1A-4744-A47C-84D0B95904E5@themillmans.net> References: <6B20D144-9937-430C-963D-CEF798CC4EBE@themillmans.net> <158F8E6B-9E1A-4744-A47C-84D0B95904E5@themillmans.net> Message-ID: <20160619100950.GR27919@ando.pearwood.info> On Sat, Jun 18, 2016 at 08:46:53PM -0700, Hershel Millman wrote: > > I followed your instruction and typed "import turtle" into the terminal on my mac, and nothing happened. If you're talking about the Python prompt, that's good. That means turtle is installed. Importing a module either succeeds, or gives a traceback. Compare the difference between these two imports: py> import turtle py> import octopus Traceback (most recent call last): File "", line 1, in ImportError: No module named 'octopus' [...] > > In pycharm, when I enter the following, it replies with the > > following error message: > > > > from turtle import * > > > > def drawSquare(size=100): > > turtle.pendown() [...] > > Traceback (most recent call last): > > File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 14, in > > drawSquare(50) > > File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 4, in drawSquare > > turtle.pendown() > > NameError: global name 'turtle' is not defined This is the difference between plain "import" and "from ... import". Here is a simplified example that hopefully will be clear: py> import math py> math.pi 3.141592653589793 py> pi Traceback (most recent call last): File "", line 1, in NameError: name 'pi' is not defined With a regular import, the math module is loaded and made available using the module name. To see inside the module, you have to use the dot operator (it's not really an operator, but I'll call it that) to get access to functions and variables inside the math module. Hence you must say "math.pi". If you say "pi" on its own, Python looks for a variable called pi, doesn't find one, and complains. If I quit out of Python and start again, in a fresh session: py> from math import pi py> math.pi Traceback (most recent call last): File "", line 1, in NameError: name 'math' is not defined py> pi 3.141592653589793 "from ... import" works the opposite way. It still loads the math module, but this time it DOESN'T make it available under that name, so "math.pi" fails. Instead, it cretaes a new variable called pi, set to math.pi. Finally: from math import * doesn't just import pi, but ALL the functions and variables from the math module. That is generally not a good idea. There are very occasional times were it is useful, but in general you should only import what you need: from math import pi, sin, cos or import the entire module: import math -- Steve From hershel at themillmans.net Sun Jun 19 19:21:28 2016 From: hershel at themillmans.net (Hershel Millman) Date: Sun, 19 Jun 2016 16:21:28 -0700 Subject: [Tutor] Fwd: Fwd: Turtle References: Message-ID: I entered "import turtle" instead of "from turtle import * ", but it looks as if it did not import the pendown command. Why is that? import turtle def drawSquare(size=100): turtle.pendown turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size) turtle.left(90) drawSquare(50) input () Traceback (most recent call last): File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 14, in drawSquare(50) File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 4, in drawSquare turtle.pendown AttributeError: 'module' object has no attribute 'pendown' Process finished with exit code 1 Begin forwarded message: > From: Alan Gauld via Tutor > Date: June 19, 2016 1:47:44 AM MST > To: tutor at python.org > Subject: Re: [Tutor] Fwd: Turtle > Reply-To: Alan Gauld > > On 19/06/16 04:46, Hershel Millman wrote: > >>> In pycharm, when I enter the following, it replies with the following error message: >>> >>> from turtle import * >>> > > Change that to > > import turtle > > and it should work. > > > >>> def drawSquare(size=100): >>> turtle.pendown() >>> turtle.forward(size) >>> turtle.left(90) >>> turtle.forward(size) >>> turtle.left(90) >>> turtle.forward(size) >>> turtle.left(90) >>> turtle.forward(size) >>> turtle.left(90) >>> >>> drawSquare(50) >>> >>> input () > > > -- > 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 steve at pearwood.info Sun Jun 19 21:24:29 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 20 Jun 2016 11:24:29 +1000 Subject: [Tutor] Fwd: Fwd: Turtle In-Reply-To: References: Message-ID: <20160620012429.GU27919@ando.pearwood.info> On Sun, Jun 19, 2016 at 04:21:28PM -0700, Hershel Millman wrote: > I entered "import turtle" instead of "from turtle import * ", but it > looks as if it did not import the pendown command. Why is that? Good question. Try this: import turtle print(turtle.__file__) That should print something like '/usr/local/lib/python3.3/turtle.py' or wherever you have installed Python to. If it is something like this: /Users/Hershel/PycharmProjects/Project 1/turtle.py then you have (accidentally) saved a new file called "turtle.py" and it is shadowing the standard library file and blocking it from being loading. Instead of importing the real turtle module, Python is importing your fake turtle module. To fix that, delete or rename your turtle.py module, quit PyCharm, and start it up again. Then you'll need to fix a small bug in your code: > import turtle > > def drawSquare(size=100): > turtle.pendown Add round brackets (parentheses) to the pendown: turtle.pendown() Without the brackets, it just names the function, it doesn't call it. In your case, it probably doesn't matter, since the turtle starts with the pen down by default. -- Steve From min786a at googlemail.com Mon Jun 20 03:16:36 2016 From: min786a at googlemail.com (Minhaj Ahmed) Date: Mon, 20 Jun 2016 08:16:36 +0100 Subject: [Tutor] My code isn't working properly Message-ID: Hi,I'm studying Michael Dawsons 'Python programming for absolute beginners',in chapter 5,page 129,the author writes a program that records high scores in a game. I have done exactly as the author has set out and yet my code isn't doing what the author says it should be doing. The code is printed below. scores=[] choice=None while choice !="0": print( """ High scores 0-Exit 1-Show scores 2-Add scores 3-Delete scores 4-Sort scores """ ) choice = input("Choice: ") print() if choice== "0": print("Goodbye") elif choice=="1": print("High scores") for score in scores: print(score) elif choice=="2": score=int(input("What score did you get?: ")) scores.append(score) elif choice=="3": score=int(input("Remove which score?: ")) if score in scores: scores.remove(score) else: print(score, "isnt in the high score list") elif choice=="4": scores.sort(reverse=True) else: print("Sorry but",choice,"isnt a valid choice") input("\nPress enter to exit") the only part of the program that seems to work is when i enter "0" to exit the program,any other number it just prints the beginning,which I know it should,but thats it. It doesn't ask for a new score when I enter "2" and even when I enter a supposedly invalid choice it still prints out the text in the while loop. Hope someone can help. Regards Minhaj From riaztbsm at gmail.com Mon Jun 20 01:38:19 2016 From: riaztbsm at gmail.com (riaz tabassum) Date: Mon, 20 Jun 2016 08:38:19 +0300 Subject: [Tutor] error in python code. Message-ID: Sir i have a problem to solve to python code. I have attached the pic of problem statement. code which i write is pasted here. "def Skew(Genome): skew = {} #initializing the dictionary skew[0]=0#initializing the dictionary for i in range(len(Genome)): if i==0: if Genome[i]=='G': skew[i]=skew[i]+1 elif Genome[i]=='C': skew[i]=skew[i]-1 else: skew[i]=skew[i] elif Genome[i]=='G': skew[i]=skew[i-1]+1 elif Genome[i]=='C': skew[i]=skew[i-1]-1 else: skew[i]=skew[i-1] # your code here return skew Genome="CGCAGATGTTTGCACGACTGTGACAC" print Skew(Genome).values() #Sample Output: #0', '-1', '0', '-1', '-1', '0', '0', '0', #'1', '1', '1', '1', '2', '1', '1', '0', '1', '1', #'0', '0', '1', '1', '2', '2', '1', '1', '0' " But i am facing the problem that i could not include the first index which is initialized to zero in my answer. the error is shown as Failed te #2. Test Dataset: CGCAGATGTTTGCACGACTGTGACAC Your output: ['-1', '0', '-1', '-1', '0', '0', '0', '1', '1', '1', '1', '2', '1', '1', '0', '1', '1', '0', '0', '1', '1', '2', '2', '1', '1', '0'] Correct output: ['0', '-1', '0', '-1', '-1', '0', '0', '0', '1', '1', '1', '1', '2', '1', '1', '0', '1', '1', '0', '0', '1', '1', '2', '2', '1', '1', '0'] From sopan.shewale at gmail.com Mon Jun 20 03:59:21 2016 From: sopan.shewale at gmail.com (Sopan Shewale) Date: Mon, 20 Jun 2016 00:59:21 -0700 Subject: [Tutor] My code isn't working properly In-Reply-To: References: Message-ID: You need to worry about indentation ;) & spells (.. you seriously want to use input instead of raw_input? ) See if following works for you? ------ #!/usr/bin/python scores = [] choice = None while choice !="0": print \ """ High scores 0-Exit 1-Show scores 2-Add scores 3-Delete scores 4-Sort scores """ choice = raw_input("Choice: ") print if choice== "0": print("Goodbye") elif choice=="1": print("High scores") for score in scores: print(score) elif choice=="2": score=int(raw_input("What score did you get?: ")) scores.append(score) elif choice=="3": score=int(raw_input("Remove which score?: ")) if score in scores: scores.remove(score) else: print(score, "isnt in the high score list") elif choice=="4": scores.sort(reverse=True) else: print("Sorry but",choice,"isnt a valid choice") raw_input("\nPress enter to exit") ----------- Cheers, --sopan shewale On Mon, Jun 20, 2016 at 12:16 AM, Minhaj Ahmed via Tutor wrote: > Hi,I'm studying Michael Dawsons 'Python programming for absolute > beginners',in chapter 5,page 129,the author writes a program that records > high scores in a game. I have done exactly as the author has set out and > yet my code isn't doing what the author says it should be doing. The code > is printed below. > > scores=[] > choice=None > while choice !="0": > print( > """ > High scores > 0-Exit > 1-Show scores > 2-Add scores > 3-Delete scores > 4-Sort scores > """ > ) > choice = input("Choice: ") > print() > if choice== "0": > print("Goodbye") > > > elif choice=="1": > print("High scores") > for score in scores: > print(score) > > > elif choice=="2": > score=int(input("What score did you get?: ")) > scores.append(score) > > elif choice=="3": > score=int(input("Remove which score?: ")) > if score in scores: > scores.remove(score) > else: > print(score, "isnt in the high score list") > elif choice=="4": > scores.sort(reverse=True) > else: > print("Sorry but",choice,"isnt a valid choice") > > input("\nPress enter to exit") > > the only part of the program that seems to work is when i enter "0" to exit > the program,any other number it just prints the beginning,which I know it > should,but thats it. It doesn't ask for a new score when I enter "2" and > even when I enter a supposedly invalid choice it still prints out the text > in the while loop. Hope someone can help. > > Regards > > Minhaj > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Mon Jun 20 04:01:38 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 20 Jun 2016 09:01:38 +0100 Subject: [Tutor] My code isn't working properly In-Reply-To: References: Message-ID: On 20/06/16 08:16, Minhaj Ahmed via Tutor wrote: > Hi,I'm studying Michael Dawsons 'Python programming for absolute > beginners',in chapter 5,page 129,the author writes a program that records > high scores in a game. I have done exactly as the author has set out and > yet my code isn't doing what the author says it should be doing. The code > is printed below. > > scores=[] > choice=None > while choice !="0": > print( There is a mistake in the indentation here that should give you an error. This means the code you have posted is not the actual code you are running. It may be a mail problem, in which case please resend your message making sure you post in plain text(not HTML) to preserve the layout. Because indentation is critical in Python we can't begin to guess what is wrong with your code until we see the correctly indented code. -- 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 20 04:03:51 2016 From: __peter__ at web.de (Peter Otten) Date: Mon, 20 Jun 2016 10:03:51 +0200 Subject: [Tutor] error in python code. References: Message-ID: riaz tabassum wrote: > Sir i have a problem to solve to python code. I have attached the pic of > problem statement. This is a text-only mailing list. Please describe the problem you are trying to solve in plain English. Thank you. > code which i write is pasted here. > "def Skew(Genome): > skew = {} > #initializing the dictionary > skew[0]=0#initializing the dictionary > > for i in range(len(Genome)): > if i==0: > > if Genome[i]=='G': > > skew[i]=skew[i]+1 > elif Genome[i]=='C': > > skew[i]=skew[i]-1 > else: > > skew[i]=skew[i] > > > elif Genome[i]=='G': > > skew[i]=skew[i-1]+1 > > > elif Genome[i]=='C': > skew[i]=skew[i-1]-1 > > else: > skew[i]=skew[i-1] Is the part above provided by your teacher? Then it's pobably correct and you can concentrate on > # your code here > return skew Hint: the order of the items in a dict is undefined and you may even get different output when you run the same script twice. As a simple way around this you can sort the keys and then create a list by looking up the corresponding values. Did you learn about list comprehensions? The sorted() function? Both may be useful. > Genome="CGCAGATGTTTGCACGACTGTGACAC" > print Skew(Genome).values() That line should probably be print Skew(Genome) as any data preprocessing is best done inside the Skew() function. > #Sample Output: > #0', '-1', '0', '-1', '-1', '0', '0', '0', > #'1', '1', '1', '1', '2', '1', '1', '0', '1', '1', > #'0', '0', '1', '1', '2', '2', '1', '1', '0' > " > > > But i am facing the problem that i could not include the first index > which is initialized to zero in my answer. > > > the error is shown as > > Failed te #2. > > Test Dataset: > CGCAGATGTTTGCACGACTGTGACAC > > Your output: > ['-1', '0', '-1', '-1', '0', '0', '0', '1', '1', '1', '1', '2', '1', > '1', '0', '1', '1', '0', '0', '1', '1', '2', '2', '1', '1', '0'] > > Correct output: > > ['0', '-1', '0', '-1', '-1', '0', '0', '0', '1', '1', '1', '1', '2', '1', > '1', '0', '1', '1', '0', '0', '1', '1', '2', '2', '1', '1', '0'] > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From sopan.shewale at gmail.com Mon Jun 20 04:25:13 2016 From: sopan.shewale at gmail.com (Sopan Shewale) Date: Mon, 20 Jun 2016 01:25:13 -0700 Subject: [Tutor] My code isn't working properly In-Reply-To: References: Message-ID: check it here - http://textsnip.com/6bd6jh On Mon, Jun 20, 2016 at 1:01 AM, Alan Gauld via Tutor wrote: > On 20/06/16 08:16, Minhaj Ahmed via Tutor wrote: > > Hi,I'm studying Michael Dawsons 'Python programming for absolute > > beginners',in chapter 5,page 129,the author writes a program that records > > high scores in a game. I have done exactly as the author has set out and > > yet my code isn't doing what the author says it should be doing. The code > > is printed below. > > > > scores=[] > > choice=None > > while choice !="0": > > print( > > There is a mistake in the indentation here that should give you an > error. This means the code you have posted is not the actual code > you are running. > > It may be a mail problem, in which case please resend your message > making sure you post in plain text(not HTML) to preserve the layout. > > Because indentation is critical in Python we can't begin to > guess what is wrong with your code until we see the correctly > indented code. > > > -- > 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 Mon Jun 20 04:36:21 2016 From: __peter__ at web.de (Peter Otten) Date: Mon, 20 Jun 2016 10:36:21 +0200 Subject: [Tutor] ASCII-Art circles, was Re: Why are expressions not allowed as parameters in function definition statements? References: Message-ID: boB Stepp wrote: > I have (Finally!) gotten a bit of time to look at Peter's answer to my > Model-View-Controller question from May 29th, particularly his > CircleImageView class to which he added a "#FIXME" comment. I'm now really sorry that I wrote that comment. It should have been something like # I know that the following code is inefficient and produces # underwhelming results. As its only purpose is to illustrate # the view part of MVC I won't spend one more minute on it. Don't let that stop you if the problem of drawing graphics primitives interests you. You can reuse the coordinates of one point on the circle eight times, and for inner points no calculation should be necessary. I don't know how to best deal with an aspect ratio != 1; as the extra minute is more than over you have to google it yourself ;) From robertvstepp at gmail.com Mon Jun 20 09:20:34 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 20 Jun 2016 08:20:34 -0500 Subject: [Tutor] ASCII-Art circles, was Re: Why are expressions not allowed as parameters in function definition statements? In-Reply-To: References: Message-ID: On Mon, Jun 20, 2016 at 3:36 AM, Peter Otten <__peter__ at web.de> wrote: > I'm now really sorry that I wrote that comment. It should have been > something like > > # I know that the following code is inefficient and produces > # underwhelming results. As its only purpose is to illustrate > # the view part of MVC I won't spend one more minute on it. Ah, Peter, you are a very bad boy! ~(:>)) At first I thought you had deliberately inserted a subtle teaching point that I was supposed to detect and correct. When I failed to find some subtlety of OOP or MVC, I switched to looking at the actual display of the circle. So I improved how it looked on my PC, but realized this was not a general solution and left it there. > Don't let that stop you if the problem of drawing graphics primitives > interests you. You can reuse the coordinates of one point on the circle > eight times, and for inner points no calculation should be necessary. > I don't know how to best deal with an aspect ratio != 1; as the extra minute > is more than over you have to google it yourself ;) I think I have played with ASCII art enough for now. And your code did give me (two) good examples demonstrating an implementation of MVC, which was the main point of the thread. I am content (For a moment at least!). Thanks! -- boB From robertvstepp at gmail.com Mon Jun 20 10:08:02 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 20 Jun 2016 09:08:02 -0500 Subject: [Tutor] My code isn't working properly In-Reply-To: References: Message-ID: On Mon, Jun 20, 2016 at 2:59 AM, Sopan Shewale wrote: > You need to worry about indentation ;) & spells (.. you seriously want to > use input instead of raw_input? ) The book the OP is using is Python 3-based. He should be using "input()" as he did. Otherwise, it does look like a lack of indentation for his statements that are part of the while loop are his problem. boB From sopan.shewale at gmail.com Mon Jun 20 12:25:52 2016 From: sopan.shewale at gmail.com (Sopan Shewale) Date: Mon, 20 Jun 2016 09:25:52 -0700 Subject: [Tutor] My code isn't working properly In-Reply-To: References: Message-ID: Yup! His problem must be indentation. Apart from him, everybody from mailing list is worried about his problem ;) Regards, On Mon, Jun 20, 2016 at 7:08 AM, boB Stepp wrote: > On Mon, Jun 20, 2016 at 2:59 AM, Sopan Shewale > wrote: > > You need to worry about indentation ;) & spells (.. you seriously want > to > > use input instead of raw_input? ) > > The book the OP is using is Python 3-based. He should be using > "input()" as he did. Otherwise, it does look like a lack of > indentation for his statements that are part of the while loop are his > problem. > > boB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From lulu.jama2016 at gmail.com Mon Jun 20 08:42:06 2016 From: lulu.jama2016 at gmail.com (Lulu J) Date: Mon, 20 Jun 2016 08:42:06 -0400 Subject: [Tutor] help with comparing list of tuples in python 2 In-Reply-To: References: Message-ID: Many thanks to those of you who responded to my question. I really appreciate. Just to answer some questions you guys raised. 1.The list only contains the key words that was matched as a result of a search. So words in this list aren't necessarily next to each other. They could be anywhere in the text and the words can appear multiple times 2. The list is already sorted by the position 3.The main goal is to count the number of matches but to count adjacent words only once. ie. two words or more next to each other counts as one match. Again, thank you for your time. L On Mon, Jun 20, 2016 at 8:34 AM, Muhubo Yusuf wrote: > > > From: Alan Gauld via Tutor > Date: Fri, Jun 17, 2016 at 6:36 PM > Subject: Re: [Tutor] help with comparing list of tuples in python 2 > To: tutor at python.org > > > On 17/06/16 20:18, Lulu J wrote: > > > I have a list of dictionaries. Each dictionary has a word and its > position > > in the text the positions are in the form of a tuple. > > Here is an example: > > [ > > {'position': (5, 4), 'term': u'happy',}, > > {'position': (5, 5), 'term': u'something'} > > ] > > > > for the potions, the first element is the paragraph number and the second > > is the word number in that paragraph(sequence from 1...n) > > > > What I would like to is find which words are next to each other. Meaning, > > they will be in the same paragraph and the difference of their word > numbers > > is 1. > > You can sort them by providing a key function, for example, > a simplified version:: > > >>> L = [{'k':1,'v':0},{'k':5,'v':9},{'k':2,'v':6},{'k':0,'v':12}] > >>> sorted(L,key=lambda d: d['v']) > [{'k': 1, 'v': 0}, {'k': 2, 'v': 6}, {'k': 5, 'v': 9}, {'k': 0, 'v': 12}] > >>> sorted(L,key=lambda d: d['k']) > [{'k': 0, 'v': 12}, {'k': 1, 'v': 0}, {'k': 2, 'v': 6}, {'k': 5, 'v': 9}] > >>> > > Then filter your results to find an adjacent pair that have matching > positions. (This may not be necessary if you have all of the words since > they should naturally be placed adjacent to each other, but > if you only have key words it will be needed) > > Something like (untested) > > neighbours = [item for index,item in enumerate(data) > if item['position'][0] == data[index+1][position'][0] and > item['position'][1] == data[index+1][position'][1]-1] > > But there are lots of possible gotchas here. > > - look out for the last item which will give you an index error! > - what happens to words that appear more than once? Are they > in your list more than once? > - probably more :-( > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > From alan.gauld at yahoo.co.uk Mon Jun 20 17:14:18 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 20 Jun 2016 22:14:18 +0100 Subject: [Tutor] My code isn't working properly In-Reply-To: References: Message-ID: On 20/06/16 09:25, Sopan Shewale wrote: > check it here - http://textsnip.com/6bd6jh Tried it and it worked fine in v2.7. To run in v3 I added a few parens after the prints and put the line raw_input = input at the top. I also removed the annoying prompt at the end of the loop. But basically it works. >> On 20/06/16 08:16, Minhaj Ahmed via Tutor wrote: >>> Hi,I'm studying Michael Dawsons 'Python programming for absolute >>> beginners',in chapter 5,page 129,the author writes a program that records >>> high scores in a game. I have done exactly as the author has set out and >>> yet my code isn't doing what the author says it should be doing. The code >>> is printed below. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Wed Jun 22 00:16:07 2016 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 21 Jun 2016 21:16:07 -0700 Subject: [Tutor] error in python code. In-Reply-To: References: Message-ID: On Sun, Jun 19, 2016 at 10:38 PM, riaz tabassum wrote: > Sir i have a problem to solve to python code. I have attached the pic of > problem statement. Hi Riaz, Often when you're asking a question, starting with presentation of the code is often the wrong order to attack a problem. The reason is because code just *is*. It executes. Although we can often detect certain patterns that are bug prone (as Peter notes with iterating over a dictionary), other than that, the code looks like it does something. It runs, so it must be right, right? No, of course not. But that's the part you need to explain, the "what" and the "why". What makes code "good" or "bad" is human evaluation, some expectation that we have about what we want to happen. These expectations are something that's usually stated in terms *outside* of the code. Specifically, when you say these two parts: > Test Dataset: > CGCAGATGTTTGCACGACTGTGACAC > Correct output: > > ['0', '-1', '0', '-1', '-1', '0', '0', '0', '1', '1', '1', '1', '2', '1', > '1', '0', '1', '1', '0', '0', '1', '1', '2', '2', '1', '1', '0'] then the immediate question is: why should that be the correct output? Why those numbers? To an untrained eye, it seems like an entirely arbitrary, meaningless sequence. What do the numbers mean? We can't trust the code to look for meaning, because presumably the code is broken. So we need something else. You need to state some definitions so that we are sharing common terminology. What does "skew" mean, for example? If you can explain the problem well enough, we should be able to compute the correct output by hand, without looking at your code. Hope that makes sense. Please feel free to ask questions. From dyoo at hashcollision.org Wed Jun 22 00:29:34 2016 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 21 Jun 2016 21:29:34 -0700 Subject: [Tutor] error in python code. In-Reply-To: References: Message-ID: > You need to state some definitions so that we are sharing common > terminology. What does "skew" mean, for example? Note to others: I'm guessing that this has something to do with GC skew as described in https://en.wikipedia.org/wiki/GC_skew. But I'd rather not guess. I'd like Riaz to explain in some detail what the purpose of the function is supposed to be. Otherwise, a technically correct solution to the problem that satisfies the test dataset is the following silly function: #################################### def Skew(genome): return ['0', '-1', '0', '-1', '-1', '0', '0', '0', '1', '1', '1', '1', '2', '1', '1', '0', '1', '1', '0', '0', '1', '1', '2', '2', '1', '1', '0'] ##################################### We know this can't be possibly right, but given what we know so far, that's actually a good solution until we're given a purpose statement of what the function is trying to compute. From esawiek at gmail.com Tue Jun 21 20:41:08 2016 From: esawiek at gmail.com (Ek Esawi) Date: Tue, 21 Jun 2016 20:41:08 -0400 Subject: [Tutor] Converting a sequence of dictionaries to a single dictionary Message-ID: Hi All-- I am trying to read a csv file via DictReader and produce a single dictionary without fieldnames; that is, I just want a single dictionary that contains tests as keys and grades as values as shown below in my sample file. If I loop through the reader, it produces a sequence of 2 entries dictionaries; each starts with the fieldnames, e.g. {Test:Test1,Grade:A}, etc. I want to create a single dictionary with only Tests and Grades; that?s {Test1:A,Test2:b,Test3:c etc.}. I tried several ways but nothing worked. File Test Grade Test1 A Test2 B Test3 C Code; import csv with open(Tests.csv, 'r') as Temp: reader = csv.DictReader(Temp) From alan.gauld at yahoo.co.uk Wed Jun 22 03:30:08 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 22 Jun 2016 08:30:08 +0100 Subject: [Tutor] Converting a sequence of dictionaries to a single dictionary In-Reply-To: References: Message-ID: On 22/06/16 01:41, Ek Esawi wrote: > I am trying to read a csv file via DictReader and produce a single > dictionary without fieldnames; that is, I just want a single dictionary > that contains tests as keys and grades as values as shown below in my > sample file. OK, As you've discovered that's not what the csv reeaders do. They produce a data structure per line. Thats because CSV files usually contain one discrete data record per line. If you want to pull all the records into a single dictionary you will need to do that yourself, probably using a standard tuple based reader. > I want to create a single dictionary with only Tests and Grades; that?s > {Test1:A,Test2:b,Test3:c etc.}. I tried several ways but nothing worked. > File > Test Grade > Test1 A > Test2 B > Test3 C So the reader will give you [('Test1','A'),('Test2','B'),...] So you need to take the list of tuples and put them in a dict. Luckily that's exactly what the dict() function does when given a list of tuples. >>> dict([(1,2),(3,4)]) {1: 2, 3: 4} >>> So you just need to call dict() on your reader data. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From hershel at themillmans.net Wed Jun 22 20:52:37 2016 From: hershel at themillmans.net (Hershel Millman) Date: Wed, 22 Jun 2016 17:52:37 -0700 Subject: [Tutor] Fwd: Fwd: Fwd: Turtle References: <20160620012429.GU27919@ando.pearwood.info> Message-ID: <62741304-1558-4444-8313-47D45F9EC66C@themillmans.net> I found the turtle module on my computer. >>> import turtle >>> print(turtle.__file__) /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/turtle.py I followed the chain of folders in my finder window, and was able to locate the file. I tried to rename the file and my computer told me I did not have the permissions to do so. When I searched my computer for another file named "turtle.py", I could not find one. What do I do? Thank you, Hershel Begin forwarded message: > From: Steven D'Aprano > Date: June 19, 2016 6:24:29 PM MST > To: tutor at python.org > Subject: Re: [Tutor] Fwd: Fwd: Turtle > > On Sun, Jun 19, 2016 at 04:21:28PM -0700, Hershel Millman wrote: > >> I entered "import turtle" instead of "from turtle import * ", but it >> looks as if it did not import the pendown command. Why is that? > > Good question. > > Try this: > > import turtle > print(turtle.__file__) > > That should print something like > > '/usr/local/lib/python3.3/turtle.py' > > or wherever you have installed Python to. If it is something like this: > > /Users/Hershel/PycharmProjects/Project 1/turtle.py > > then you have (accidentally) saved a new file called "turtle.py" and it > is shadowing the standard library file and blocking it from being > loading. Instead of importing the real turtle module, Python is > importing your fake turtle module. > > To fix that, delete or rename your turtle.py module, quit PyCharm, and > start it up again. > > Then you'll need to fix a small bug in your code: > >> import turtle >> >> def drawSquare(size=100): >> turtle.pendown > > Add round brackets (parentheses) to the pendown: > > turtle.pendown() > > Without the brackets, it just names the function, it doesn't call it. In > your case, it probably doesn't matter, since the turtle starts with the > pen down by default. > > > > -- > 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 Thu Jun 23 03:12:39 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 23 Jun 2016 08:12:39 +0100 Subject: [Tutor] : Turtle In-Reply-To: <62741304-1558-4444-8313-47D45F9EC66C@themillmans.net> References: <20160620012429.GU27919@ando.pearwood.info> <62741304-1558-4444-8313-47D45F9EC66C@themillmans.net> Message-ID: On 23/06/16 01:52, Hershel Millman wrote: > I found the turtle module on my computer. > >>>> import turtle >>>> print(turtle.__file__) > /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/turtle.py That's the right turtle module do not rename it. >>> I entered "import turtle" instead of "from turtle import * ", but it >>> looks as if it did not import the pendown command. Why is that? If that's the module you are importing it should work. What happens if you try: >>> import turtle >>> turtle.pendown() Do you still get the error? If so can you cut n paste the entire session, including the initial python startup message and the error message, into a mail for us? -- 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 ahall at autodist.com Thu Jun 23 17:08:11 2016 From: ahall at autodist.com (Alex Hall) Date: Thu, 23 Jun 2016 17:08:11 -0400 Subject: [Tutor] Best way to use excepthook here? Message-ID: Hey all, How would I go about sharing a reassignment of excepthook, where that assignment is made to a central function and could come from any of several files? I'm replacing a bunch of SQL jobs at work, and my boss said he doesn't care which language I use. Naturally, Python it is. :) All these jobs do different things, and run at different times and different intervals. Some update files on our FTP server, some email the previous day's shipping errors, some back up databases, and so on. The setup I'm going to try first is one file per job, with utils.py and a couple others shared between them all. That way, any job that needs to email can just import emailer and every job can access configuration data: import utils utils.config["emails"]["alexHall"] One thing that utils.py provides is logging: import utils logger = utils.ADLogger("Job Name") The ADLogger object sets the level, the file name, the formatter, and so on. It also has a function: logException(self, type, value, traceback). This simply logs the exception, since ADLogger is a subclass of logging.Logger. Finally, my question. Say job1 and job2 run within a minute of each other, but job1 takes a while to execute. Both jobs set their excepthook functions: logger = utils.ADLogger("job1") excepthook = logger.logException logger = ADLogger("Job 2") excepthook = logger.logExceptions Now, Python is running two scripts which each have their excepthook set to something different. What happens when both try to log an exception, or even if one tries to while the other is running? Will this work how I hope, or will there be confusion? Should I thread it somehow? I'd rather not have a central manager, as that's one more thing that might fail; I plan to set all the scripts up in Windows Task Scheduler. I can have a Python-based manager if I need to, but it'd take me a while to finish that (I started something similar months back and would have to finish and modify it). Am I worrying over nothing, or will I need to do something to make sure each job logs exceptions, and does it correctly, no matter who else is running at the same time? Thanks. -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From bharathswami at hotmail.com Thu Jun 23 14:00:40 2016 From: bharathswami at hotmail.com (Bharath Swaminathan) Date: Thu, 23 Jun 2016 18:00:40 +0000 Subject: [Tutor] Hi Message-ID: Can I run my python code in multiple processors? I have a dual core... From elizabeth-lennartson at uiowa.edu Thu Jun 23 10:02:22 2016 From: elizabeth-lennartson at uiowa.edu (Lennartson, Elizabeth M) Date: Thu, 23 Jun 2016 14:02:22 +0000 Subject: [Tutor] Opening EOS-5 HDF file - help! Message-ID: I am trying to open a HDF file in python. I downloaded python 2.7.8 through miniconda2. I also downloaded basemap-1.0.7, matplotlib-1.5.0, and numpy-1.11.0. I am trying to do this example: http://hdfeos.org/software/pyhdf.php. I can execute "import os" but when I try to execute "import matplotlib as mpl," I get the following error: File "", line 1, in ImportError: No module named matplotlib I am new to programming, so I am not sure what the issue is. Thanks for your help! From hershel at themillmans.net Thu Jun 23 17:13:56 2016 From: hershel at themillmans.net (Hershel Millman) Date: Thu, 23 Jun 2016 14:13:56 -0700 Subject: [Tutor] Fwd: : Turtle References: Message-ID: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> What I typed was: import turtle turtle.pendown() (And pendown was highlighted in pycharm, indicating that it was not a valid command.) The error message I received was: /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5 "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py" Traceback (most recent call last): File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 2, in turtle.pendown() AttributeError: 'module' object has no attribute 'pendown' Process finished with exit code 1 Thank you, Hershel Begin forwarded message: > From: Alan Gauld via Tutor > Date: June 23, 2016 12:12:39 AM MST > To: tutor at python.org > Subject: Re: [Tutor] : Turtle > Reply-To: Alan Gauld > > On 23/06/16 01:52, Hershel Millman wrote: >> I found the turtle module on my computer. >> >>>>> import turtle >>>>> print(turtle.__file__) >> /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/turtle.py > > That's the right turtle module do not rename it. > >>>> I entered "import turtle" instead of "from turtle import * ", but it >>>> looks as if it did not import the pendown command. Why is that? > > If that's the module you are importing it should work. > > What happens if you try: > >>>> import turtle >>>> turtle.pendown() > > Do you still get the error? > > If so can you cut n paste the entire session, including the initial > python startup message and the error message, into a mail for us? > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Thu Jun 23 19:44:15 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 00:44:15 +0100 Subject: [Tutor] Fwd: : Turtle In-Reply-To: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> References: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> Message-ID: On 23/06/16 22:13, Hershel Millman wrote: > What I typed was: > > import turtle > turtle.pendown() > > (And pendown was highlighted in pycharm, indicating that it was not a valid command.) Don't use pycharm. We need to eliminate as many variables as possible. Start python in a Terminal and just type the commands into the raw Python interpreter. Send a paste of the transcript. I expect to see something like: agauld at ubuntu:~$ python2 Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import turtle >>> turtle.__file__ '/usr/lib/python2.7/lib-tk/turtle.pyc' >>> turtle.pendown >>> turtle.pendown() >>> Can you reproduce that on your Mac? (Except maybe with an error message somewhere along the line?) > /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5 "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py" > Traceback (most recent call last): > File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 2, in > turtle.pendown() > AttributeError: 'module' object has no attribute 'pendown' I notice that this says you are using Python 2.5 but your last message suggested the module was in 2.6. I don't think that should make a difference for the turtle module but it might be significant... Lets see your transcript first. -- 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 23 19:51:38 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 00:51:38 +0100 Subject: [Tutor] Best way to use excepthook here? In-Reply-To: References: Message-ID: On 23/06/16 22:08, Alex Hall wrote: > setup I'm going to try first is one file per job, with utils.py and a > couple others shared between them all. > ... > Now, Python is running two scripts which each have their excepthook set to > something different. What happens when both try to log an exception, They both do their own thing because you have two entirely separate instances of the interpreter running, each in its own process. So they both set their own excepthook and is none the wiser of what the other one is doing. If you later decide to bring the jobs into a single program, maybe running each job in a separate thread then things get more tricky. But if you run each job as a separate script then there is no issue (unless they are both sharing a file/database but that's a standard OS locking type issue, nothing specific to Python. > or will there be confusion? Should I thread it somehow? Threading will make it much more complex. Better to keep separate processes unless there are so many your server runs out of resources... > worrying over nothing, or will I need to do something to make sure each job > logs exceptions, and does it correctly, no matter who else is running at > the same time? Thanks. I think you should be fine provided you log into separate files per job and are not trying to modify the same data in the database. But these are standard batch processing concerns, easiest solved by designing the job schedule to avoid conflicts. -- 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 23 20:00:51 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 01:00:51 +0100 Subject: [Tutor] Hi In-Reply-To: References: Message-ID: On 23/06/16 19:00, Bharath Swaminathan wrote: > Can I run my python code in multiple processors? I have a dual core... Your OS may run your python code on multiple processors but it's not something you can easily control in Python. Remember that your computer probably has hundreds of processes running at any one time and your python program(s) will only be a part of the total load. The OS schedules the jobs to run on whichever processor is available at any given time - it may well use different processors for the same process during the lifetime of that process. Now, if what you really want to know is whether the OS can split a single Python process over multiple CPU cores in parallel, that's a much more complex question and I think the current answer is probably not. (But others on this list know a lot more about that kind of thing than I do!) But it depends on the exact version of Python you are running and probably which OS you are using too. In my experience trying to guess how the OS will schedule your jobs is an exercise in frustration. Let the OS do its thing and only worry about those kinds of optimisation once you have proved it is really an issue (by measurement). -- 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 23 20:05:04 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 01:05:04 +0100 Subject: [Tutor] Opening EOS-5 HDF file - help! In-Reply-To: References: Message-ID: On 23/06/16 15:02, Lennartson, Elizabeth M wrote: > I am trying to open a HDF file in python. I downloaded python 2.7.8 through miniconda2. Given how many other modules you are loading I'd suggest installing a full anaconda distribution which has everything included as standard. > I also downloaded basemap-1.0.7, matplotlib-1.5.0, and numpy-1.11.0. You say you downloaded them, but did you install them? > File "", line 1, in > > ImportError: No module named matplotlib It suggests the install didn't work. The simplest fix is probably to just install a full anaconda. Failing that we'll need to know OS and the install process you used. Also, since Scipy etc is technically outside the scope of this list (standard library only) you may get more responses on the SciPy (or Anaconda/Miniconda) forums. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Thu Jun 23 21:15:43 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 24 Jun 2016 11:15:43 +1000 Subject: [Tutor] Fwd: : Turtle In-Reply-To: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> References: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> Message-ID: <20160624011541.GY27919@ando.pearwood.info> On Thu, Jun 23, 2016 at 02:13:56PM -0700, Hershel Millman wrote: > What I typed was: > > import turtle > turtle.pendown() What do you get if you print turtle.__file__? > (And pendown was highlighted in pycharm, indicating that it was not a valid command.) > > The error message I received was: > > /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5 Look at the version number: you are running Python 2.5. Now look at the result you got earlier: > >> /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/turtle.py Look at the version number: you are running Python 2.6. So you have AT LEAST two different Python versions on your computer. That's fine, I have *nine* versions on mine. But it does mean you have to be a little more careful to ensure you are using the right one. My *guess* is that your Mac has pre-installed version 2.5 with the operating system, and you have installed version 2.6 next to it, and now you sometimes get 2.5 and sometimes 2.6 depending on which icon you double-click. Or something like that. Or maybe PyCharm let's you pick a different version, and you haven't noticed. You can check the version from inside Python: import sys print sys.version My prediction is: * when you run Python 2.6, turtle will work fine, including the pendown command (but remember to use round brackets/parentheses): turtle.pendown() # okay in 2.6 * when you run Python 2.5, turtle will import, but there is no turtle.pendown command. Instead, it is called turtle.down. Documentation for 2.5: https://docs.python.org/release/2.5.4/lib/module-turtle.html Documentation for 2.6: https://docs.python.org/release/2.6/library/turtle.html -- Steve From __peter__ at web.de Fri Jun 24 00:28:41 2016 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jun 2016 06:28:41 +0200 Subject: [Tutor] Best way to use excepthook here? References: Message-ID: Alex Hall wrote: > Hey all, > How would I go about sharing a reassignment of excepthook, where that > assignment is made to a central function and could come from any of > several files? > > I'm replacing a bunch of SQL jobs at work, and my boss said he doesn't > care which language I use. Naturally, Python it is. :) > > All these jobs do different things, and run at different times and > different intervals. Some update files on our FTP server, some email the > previous day's shipping errors, some back up databases, and so on. The > setup I'm going to try first is one file per job, with utils.py and a > couple others shared between them all. That way, any job that needs to > email can just > import emailer > and every job can access configuration data: > import utils > utils.config["emails"]["alexHall"] > > One thing that utils.py provides is logging: > > import utils > logger = utils.ADLogger("Job Name") > > The ADLogger object sets the level, the file name, the formatter, and so > on. It also has a function: logException(self, type, value, traceback). > This simply logs the exception, since ADLogger is a subclass of > logging.Logger. > > Finally, my question. Say job1 and job2 run within a minute of each other, > but job1 takes a while to execute. Both jobs set their excepthook > functions: > > logger = utils.ADLogger("job1") > excepthook = logger.logException > > logger = ADLogger("Job 2") > excepthook = logger.logExceptions Did you mean sys.excepthook = logger.logExceptions ? > Now, Python is running two scripts which each have their excepthook set to > something different. What happens when both try to log an exception, or > even if one tries to while the other is running? Will this work how I > hope, or will there be confusion? Should I thread it somehow? I'd rather > not have a central manager, as that's one more thing that might fail; I > plan to set all the scripts up in Windows Task Scheduler. I can have a > Python-based manager if I need to, but it'd take me a while to finish that > (I started something similar months back and would have to finish and > modify it). Am I worrying over nothing, or will I need to do something to > make sure each job logs exceptions, and does it correctly, no matter who > else is running at the same time? Thanks. Even though setting it to different functions in different scripts should work as expected I'd rather not alter sys.excepthook. How about try: job1() # or main() or whatever except: logger.exception("Job 1 failed with") ? This doesn't rely on setting a central global variable. From hershel at themillmans.net Thu Jun 23 19:50:27 2016 From: hershel at themillmans.net (Hershel Millman) Date: Thu, 23 Jun 2016 16:50:27 -0700 Subject: [Tutor] Fwd: Fwd: : Turtle References: Message-ID: <512F42E4-403D-4D56-84BD-1E5B7743222A@themillmans.net> Millman-Family-Admins-iMac-2:~ Hershel$ python Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import turtle >>> turtle.__file__ '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/turtle.py' >>> turtle.pendown >>> turtle.pendown() >>> It seemed to work perfectly and do what it was supposed to. Is my problem then with PyCharm? Thanks. Begin forwarded message: > From: Alan Gauld via Tutor > Date: June 23, 2016 4:44:15 PM MST > To: tutor at python.org > Subject: Re: [Tutor] Fwd: : Turtle > Reply-To: Alan Gauld > > On 23/06/16 22:13, Hershel Millman wrote: >> What I typed was: >> >> import turtle >> turtle.pendown() >> >> (And pendown was highlighted in pycharm, indicating that it was not a valid command.) > > Don't use pycharm. We need to eliminate as many variables as possible. > Start python in a Terminal and just type the commands into the raw > Python interpreter. Send a paste of the transcript. > I expect to see something like: > > agauld at ubuntu:~$ python2 > Python 2.7.6 (default, Jun 22 2015, 17:58:13) > [GCC 4.8.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import turtle >>>> turtle.__file__ > '/usr/lib/python2.7/lib-tk/turtle.pyc' >>>> turtle.pendown > >>>> turtle.pendown() >>>> > > Can you reproduce that on your Mac? (Except maybe with an > error message somewhere along the line?) > >> /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5 "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py" >> Traceback (most recent call last): >> File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 2, in >> turtle.pendown() >> AttributeError: 'module' object has no attribute 'pendown' > > I notice that this says you are using Python 2.5 but your > last message suggested the module was in 2.6. > > I don't think that should make a difference for the turtle > module but it might be significant... Lets see your > transcript first. > > -- > 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 hershellearningcode at gmail.com Thu Jun 23 20:21:39 2016 From: hershellearningcode at gmail.com (Hershel Millman) Date: Thu, 23 Jun 2016 17:21:39 -0700 Subject: [Tutor] turtle on linux Message-ID: This is from my terminal on Fedora 24: [hmillman at localhost ~]$ python Python 2.7.10 (default, Sep 24 2015, 17:50:09) [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import turtle Traceback (most recent call last): File "", line 1, in ImportError: No module named turtle How do I solve this problem? Thank you, Hershel From hershellearningcode at gmail.com Thu Jun 23 22:02:31 2016 From: hershellearningcode at gmail.com (Hershel Millman) Date: Thu, 23 Jun 2016 19:02:31 -0700 Subject: [Tutor] Fwd: Fwd: : Turtle In-Reply-To: <20160624011541.GY27919@ando.pearwood.info> References: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> <20160624011541.GY27919@ando.pearwood.info> Message-ID: It tells me: Trace back (most recent call last): File "", line 1, in NameError: name 'turtle' is not defined Thanks! ---------- Forwarded message ---------- From: *Steven D'Aprano* Date: Thursday, June 23, 2016 Subject: [Tutor] Fwd: : Turtle To: tutor at python.org On Thu, Jun 23, 2016 at 02:13:56PM -0700, Hershel Millman wrote: > What I typed was: > > import turtle > turtle.pendown() What do you get if you print turtle.__file__? > (And pendown was highlighted in pycharm, indicating that it was not a valid command.) > > The error message I received was: > > /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5 Look at the version number: you are running Python 2.5. Now look at the result you got earlier: > >> /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/turtle.py Look at the version number: you are running Python 2.6. So you have AT LEAST two different Python versions on your computer. That's fine, I have *nine* versions on mine. But it does mean you have to be a little more careful to ensure you are using the right one. My *guess* is that your Mac has pre-installed version 2.5 with the operating system, and you have installed version 2.6 next to it, and now you sometimes get 2.5 and sometimes 2.6 depending on which icon you double-click. Or something like that. Or maybe PyCharm let's you pick a different version, and you haven't noticed. You can check the version from inside Python: import sys print sys.version My prediction is: * when you run Python 2.6, turtle will work fine, including the pendown command (but remember to use round brackets/parentheses): turtle.pendown() # okay in 2.6 * when you run Python 2.5, turtle will import, but there is no turtle.pendown command. Instead, it is called turtle.down. Documentation for 2.5: https://docs.python.org/release/2.5.4/lib/module-turtle.html Documentation for 2.6: https://docs.python.org/release/2.6/library/turtle.html -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From Joaquin.Alzola at lebara.com Thu Jun 23 19:17:04 2016 From: Joaquin.Alzola at lebara.com (Joaquin Alzola) Date: Thu, 23 Jun 2016 23:17:04 +0000 Subject: [Tutor] Fwd: : Turtle In-Reply-To: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> References: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> Message-ID: I tested with 2.6 and it works. >/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5 "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py" >Traceback (most recent call last): > File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 2, in > turtle.pendown() >AttributeError: 'module' object has no attribute 'pendown' Cannot access the documentation of the 2.5 to check if the pendown() is there. This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. From alan.gauld at yahoo.co.uk Fri Jun 24 03:21:18 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 08:21:18 +0100 Subject: [Tutor] Fwd: Fwd: : Turtle In-Reply-To: References: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> <20160624011541.GY27919@ando.pearwood.info> Message-ID: On 24/06/16 03:02, Hershel Millman wrote: > It tells me: > Trace back (most recent call last): > File "", line 1, in > NameError: name 'turtle' is not defined As I recall, the default install of Python on a Mac does not include Tkinter. Turtle uses Tkinter so I suspect turtle is not installed either. To confirm that try import Tkinter and see if that also gives an error. If I'm right you will need to ensure you use the 2.6 version for all your turtle stuff. -- 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 24 03:24:02 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 08:24:02 +0100 Subject: [Tutor] turtle on linux In-Reply-To: References: Message-ID: On 24/06/16 01:21, Hershel Millman wrote: > This is from my terminal on Fedora 24: > > [hmillman at localhost ~]$ python > Python 2.7.10 (default, Sep 24 2015, 17:50:09) > [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import turtle > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named turtle > > > How do I solve this problem? This may be similar to the Mac issue in that you have a non-Tk install. Try import Tkinter to see. The good news is that on Fedora you should be able to upgrade to a Tkinter enabled Python 2.7 using your package manager. -- 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 hershellearningcode at gmail.com Fri Jun 24 03:19:17 2016 From: hershellearningcode at gmail.com (Hershel Millman) Date: Fri, 24 Jun 2016 00:19:17 -0700 Subject: [Tutor] Fwd: Fwd: : Turtle In-Reply-To: References: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> Message-ID: When I try to use turtle in pycharm, it doesn't work, but when I try to use it in the terminal, it does work. Is it possible there is something wrong with my pycharm that is not letting it run properly? Thank you, Hershel ---------- Forwarded message ---------- From: *Joaquin Alzola* Date: Thursday, June 23, 2016 Subject: [Tutor] Fwd: : Turtle To: Hershel Millman , "tutor at python.org" < tutor at python.org> I tested with 2.6 and it works. >/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5 "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py" >Traceback (most recent call last): > File "/Users/Hershel/PycharmProjects/Project 1/practicefornotturtle.py", line 2, in > turtle.pendown() >AttributeError: 'module' object has no attribute 'pendown' Cannot access the documentation of the 2.5 to check if the pendown() is there. This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From hershellearningcode at gmail.com Fri Jun 24 03:51:39 2016 From: hershellearningcode at gmail.com (Hershel Millman) Date: Fri, 24 Jun 2016 00:51:39 -0700 Subject: [Tutor] Fwd: Fwd: : Turtle In-Reply-To: References: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> <20160624011541.GY27919@ando.pearwood.info> Message-ID: Python didn't come installed on my Mac, my dad had to install it. I typed import Tkinter into the terminal and received no error message, and when I ran it in pycharm, I also received no error message. I found a little program online and ran it to test the functionality of Tkinter, and it worked. from Tkinter import * class Application(Frame): def say_hi(self): print "hi there, everyone!" def createWidgets(self): self.QUIT = Button(self) self.QUIT["text"] = "QUIT" self.QUIT["fg"] = "red" self.QUIT["command"] = self.quit self.QUIT.pack({"side": "left"}) self.hi_there = Button(self) self.hi_there["text"] = "Hello", self.hi_there["command"] = self.say_hi self.hi_there.pack({"side": "left"}) def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createWidgets() root = Tk() app = Application(master=root) app.mainloop() root.destroy() and it finished with exit code 0. I just tried to use turtle in pycharm again, and for some reason it worked with exit code 0. I typed: from turtle import * speed('fastest') i = 0 while i < 1: forward(20) left(90) forward(20) left(90) forward(20) left(90) forward(20) left(91) i += 1 input() and it gave me no error message. However, when I typed the same thing, except instead of "from turtle import *", I typed "import turtle," I got this error message: /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5 "/Users/Hershel/PycharmProjects/Project 1/notturtle.py" Traceback (most recent call last): File "/Users/Hershel/PycharmProjects/Project 1/notturtle.py", line 2, in speed('fastest') NameError: name 'speed' is not defined Process finished with exit code 1 How do I make it so pycharm is using python version 2.6 instead of 2.5? (I have python 2.6 in the same "versions" folder as 2.5) Thank you, Hershel ---------- Forwarded message ---------- From: *Alan Gauld via Tutor* Date: Friday, June 24, 2016 Subject: [Tutor] Fwd: : Turtle To: tutor at python.org On 24/06/16 03:02, Hershel Millman wrote: > It tells me: > Trace back (most recent call last): > File "", line 1, in > NameError: name 'turtle' is not defined As I recall, the default install of Python on a Mac does not include Tkinter. Turtle uses Tkinter so I suspect turtle is not installed either. To confirm that try import Tkinter and see if that also gives an error. If I'm right you will need to ensure you use the 2.6 version for all your turtle stuff. -- 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 hershellearningcode at gmail.com Fri Jun 24 04:02:30 2016 From: hershellearningcode at gmail.com (Hershel Millman) Date: Fri, 24 Jun 2016 01:02:30 -0700 Subject: [Tutor] turtle on linux In-Reply-To: References: Message-ID: I tried to import Tkinter and I got this message: Traceback (most recent call last): File "", line 1, in ImportError: No module named Tkinter And since I am running Linux, you may be under the false impression that I know what I am doing when it comes to using Linux. I have no idea what you mean by "package manager", so if you could enlighten me, that would be immensely appreciated. Thank you, Hershel On Fri, Jun 24, 2016 at 12:24 AM, Alan Gauld via Tutor wrote: > On 24/06/16 01:21, Hershel Millman wrote: > > This is from my terminal on Fedora 24: > > > > [hmillman at localhost ~]$ python > > Python 2.7.10 (default, Sep 24 2015, 17:50:09) > > [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > >>>> import turtle > > Traceback (most recent call last): > > File "", line 1, in > > ImportError: No module named turtle > > > > > > How do I solve this problem? > > This may be similar to the Mac issue in that you have a > non-Tk install. Try > > import Tkinter > > to see. > > The good news is that on Fedora you should be able to > upgrade to a Tkinter enabled Python 2.7 using your > package manager. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Fri Jun 24 08:10:22 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 13:10:22 +0100 Subject: [Tutor] turtle on linux In-Reply-To: References: Message-ID: On 24/06/16 09:02, Hershel Millman wrote: > I tried to import Tkinter and I got this message: > > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named Tkinter > > > And since I am running Linux, you may be under the false impression that I > know what I am doing when it comes to using Linux. I have no idea what you > mean by "package manager", so if you could enlighten me, that would be > immensely appreciated. Every distribution of Linux comes with a package manager which is the tool used to load new applications. Like the App Store on an ipad. On most Linux versions Tkinter comes seperately from Python so you have to install both. For example on my Ubuntu based sysatem I use Synaptic as my package manager and within that I can find packages: python python-tk python3 python3-tk The first two are for v2.7, the second two are python v3 My guess is that the turtle module gets installed as part of the python-tk package so if you find that in your package manager you should be able to install it and turtle will then work. -- 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 24 08:14:32 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 13:14:32 +0100 Subject: [Tutor] turtle on linux In-Reply-To: References: Message-ID: On 24/06/16 09:02, Hershel Millman wrote: > mean by "package manager", so if you could enlighten me, that would be > immensely appreciated. I meant to add that on Fedora the package manager seems to be called PackageKit... There are also command line tools (yum, DNF and rpm) but I'm guessing you'll prefer a GUI! You will likely find it under your admin tools menu somewhere. -- 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 24 08:23:40 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 13:23:40 +0100 Subject: [Tutor] Fwd: Fwd: : Turtle In-Reply-To: References: <1058B10E-CE9E-4432-8CD8-F9B0E6572C8A@themillmans.net> <20160624011541.GY27919@ando.pearwood.info> Message-ID: On 24/06/16 08:51, Hershel Millman wrote: > Python didn't come installed on my Mac, my dad had to install it. Nope, it definitely would have been on there because MacOS uses it. So you definitely have two versions of python installed and it looks like one of them (probably the default) is v2.5 and uses the older turtle module and the other is v2.6 and has the latest version of turtle. Your challenge is going to be making sure you are running the right version. One way to check this is to print it out at the start of your program (as a debug step) by adding these lines: import sys print(sys.version) If it isn't v2.6 then that is your problem. As for pycharm... These pages may help. You can apparently set the interpreter version Pycharm uses both at a default level and on an individual project level. https://www.jetbrains.com/help/pycharm/2016.1/configuring-available-python-interpreters.html http://stackoverflow.com/questions/19679150/how-to-set-default-pycharm-interpreter 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 wprins at gmail.com Fri Jun 24 13:35:10 2016 From: wprins at gmail.com (Walter Prins) Date: Fri, 24 Jun 2016 18:35:10 +0100 Subject: [Tutor] Hi In-Reply-To: References: Message-ID: Hi Bharath, On 23 June 2016 at 19:00, Bharath Swaminathan wrote: > > Can I run my python code in multiple processors? I have a dual core... Notwithstanding Alan's answer, I'm going to directly answer your question: Yes, it can. However.... The degree and level of success you're going to have in fully utilising your dual cores (in other words parallelizing your Python program) are going to depend on the concurrency mechanism you choose. As a minor digression: Concurrency and Parallelism are not the same thing, though they're related. Concurrency "is about dealing with a lot of things at once" (e.g. it's about design, it's about structure) whereas parallelism "is about doing a lot of things at once" (e.g. it's about execution)[1] Please go watch the referenced video if this is at all unclear as it's actually and important distinction IMO. As an additional reference Russel Winder has given several talks over the years on threads, parallelism, concurrency and related topics which are well worth watching, please google around as I can't lay my hands on one I saw at Pycon which was particularly good. The following page seems to contain some useful links:[2] -- The one I saw was the "GIL isn't evil" presentation IIRC. Anyway, so, most people reach for threads as the default concurrency construct. Now, Python also supports threads (see the "threading" module: https://www.youtube.com/watch?v=cN_DpYBzKso) and you could use this as concurrency construct, but depending on what your program does you will likely find that it doesn't fully make use of your 2 cores like you want to. This is down to the design of the Python interpreter, and something called the Global Interpreter Lock (GIL). In most cases the GIL essentially has the result of serializing most of the execution of multiple thread, resulting in sub-optimal use of processor resources, when using real thread. For this reason multi-processing is highly preferable for Python and will allow you to effectively sidestep the GIL and make full use of all your cores (or even multiple machines if you want). The "multiprocessing" module (https://docs.python.org/2/library/multiprocessing.html) provides an API similar to that of the "threading" module but works with processes and is perhaps worth looking at. For other concurrency approaches, frameworks and libraries, you may want to look at https://wiki.python.org/moin/Concurrency/ There are quite a few. I want to highlight one particular Python concurrency module, called "ParrallelPython", the module name being "pp", which I've had great success with and highly recommend if your problem is suitable to it. The beauty of this module is that it automatically load balances according to the relative speeds of the processors/cores available. and can easily scale if more compute cores are added. So you can very easily also just spin up more nodes and providing they're suitable equipped with a "pp" based server will join and share the computational load regardless of the relative speeds/cores on each machine/processor. All the work just gets split equally based on relative speed. Really rather easy and satisfying to see and use. (Oh and by the way, "pp" also works seamless with PyPy, which in the stuff I did sped the programs up several orders of magnitude, should you need even more speed. If you don't know what PyPy is: PyPy is an alternative Jit-compiler version of Python. Basically your Python programs are compiled to machine code on the fly as they run and will in many cases, depending on what they're doing, be in some cases /several orders of magnitude/ faster than the same code on the C Python interpreter.) Anyway, that's enough for now. Have fun and ask again if anything's unclear. Walter [1] Rob Pike - 'Concurrency Is Not Parallelism', https://www.youtube.com/watch?v=cN_DpYBzKso [2] Presentations Relating to Parallelism,http://www.concertant.com/presentations_parallelism.html From wprins at gmail.com Fri Jun 24 13:40:09 2016 From: wprins at gmail.com (Walter Prins) Date: Fri, 24 Jun 2016 18:40:09 +0100 Subject: [Tutor] Hi In-Reply-To: References: Message-ID: Hi, On 23 June 2016 at 19:00, Bharath Swaminathan wrote: > Can I run my python code in multiple processors? I have a dual core... Like an idiot I forgot a link for the "pp" module, my apologies. Here it is: http://www.parallelpython.com/ If you have/use pip, you can simply enter (from an operating system shell/command prompt, and provided you've got an internet connection): pip2 install pp Walter From ahall at autodist.com Fri Jun 24 08:23:17 2016 From: ahall at autodist.com (Alex Hall) Date: Fri, 24 Jun 2016 08:23:17 -0400 Subject: [Tutor] Best way to use excepthook here? In-Reply-To: References: Message-ID: Thanks for the responses. It's great to know that nothing funny will happen even though each job is pulling from utils.py and a few other files. Since you don't need a fresh copy of every package for every script, I hoped my own packages would act the same way. I just wasn't sure if excepthook was some kind of central thing that would act differently, and where this is mission-critical to where I work, I didn't want to take chances. Oh, and yes, I did mean sys.excepthook, and I use that in my code. I'd just been googling "excepthook", so had that in my mind when I emailed. One related question. This more of a style choice, I know, but I'd like anyone's thoughts all the same. Currently, in my ADLogger.logExceptions function, I log the exception (obviously). But I also take the opportunity to generate and send an email, warning that the job raised an exception and attaching the log file as well as printing the traceback in the email. Loggers generally just log, and it feels kind of hacky to include email code in a function that primarily logs exceptions. At the same time, this is a perfect place to do it, because it gets called if an exception is raised, and if that happens, the job almost certainly failed so I need to know about it. I don't know of another way to do this since I don't have a Python-based central manager, so can't examine exit codes or anything else. As to wrapping everything in a try/except, I considered that as well, but this seems simpler and comes to the same thing. It's one less indentation level to worry about, too (Notepad doesn't auto-indent, and accessible editors that do don't always get it right). Plus, nesting try blocks could get confusing, and in some of these jobs I need tries to transform, say, non-existent dictionary values into 0 or some other constant. On Fri, Jun 24, 2016 at 12:28 AM, Peter Otten <__peter__ at web.de> wrote: > Alex Hall wrote: > > > Hey all, > > How would I go about sharing a reassignment of excepthook, where that > > assignment is made to a central function and could come from any of > > several files? > > > > I'm replacing a bunch of SQL jobs at work, and my boss said he doesn't > > care which language I use. Naturally, Python it is. :) > > > > All these jobs do different things, and run at different times and > > different intervals. Some update files on our FTP server, some email the > > previous day's shipping errors, some back up databases, and so on. The > > setup I'm going to try first is one file per job, with utils.py and a > > couple others shared between them all. That way, any job that needs to > > email can just > > import emailer > > and every job can access configuration data: > > import utils > > utils.config["emails"]["alexHall"] > > > > One thing that utils.py provides is logging: > > > > import utils > > logger = utils.ADLogger("Job Name") > > > > The ADLogger object sets the level, the file name, the formatter, and so > > on. It also has a function: logException(self, type, value, traceback). > > This simply logs the exception, since ADLogger is a subclass of > > logging.Logger. > > > > Finally, my question. Say job1 and job2 run within a minute of each > other, > > but job1 takes a while to execute. Both jobs set their excepthook > > functions: > > > > logger = utils.ADLogger("job1") > > excepthook = logger.logException > > > > logger = ADLogger("Job 2") > > excepthook = logger.logExceptions > > Did you mean > > sys.excepthook = logger.logExceptions > > ? > > > Now, Python is running two scripts which each have their excepthook set > to > > something different. What happens when both try to log an exception, or > > even if one tries to while the other is running? Will this work how I > > hope, or will there be confusion? Should I thread it somehow? I'd rather > > not have a central manager, as that's one more thing that might fail; I > > plan to set all the scripts up in Windows Task Scheduler. I can have a > > Python-based manager if I need to, but it'd take me a while to finish > that > > (I started something similar months back and would have to finish and > > modify it). Am I worrying over nothing, or will I need to do something to > > make sure each job logs exceptions, and does it correctly, no matter who > > else is running at the same time? Thanks. > > Even though setting it to different functions in different scripts should > work as expected I'd rather not alter sys.excepthook. > > How about > > try: > job1() # or main() or whatever > except: > logger.exception("Job 1 failed with") > > ? This doesn't rely on setting a central global variable. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From ahall at autodist.com Fri Jun 24 09:07:27 2016 From: ahall at autodist.com (Alex Hall) Date: Fri, 24 Jun 2016 09:07:27 -0400 Subject: [Tutor] For-else... Any other handy constructs hiding in Python? Message-ID: Hey all, I was looking at how to make my different jobs retry themselves, and found the for-else construct. I know loops, comprehensions, ifs, and the like, but I never knew for-else was available. Are there other constructs that I may have missed--shortcuts or seemingly odd pairings of control statements like this? Python has a lot of really cool shortcuts and features, like get/hasattr, the yield keyword, or comprehensions. I always like finding more to add to the toolbox. -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From Joaquin.Alzola at lebara.com Thu Jun 23 19:06:20 2016 From: Joaquin.Alzola at lebara.com (Joaquin Alzola) Date: Thu, 23 Jun 2016 23:06:20 +0000 Subject: [Tutor] Opening EOS-5 HDF file - help! In-Reply-To: References: Message-ID: >ImportError: No module named matplotlib Seems as the matplotlib is not install. Did you check the output at installation? Check matplotlib web since you need certain packages before installing the matplotlib itself. http://matplotlib.org/users/installing.html Check Required Dependencies. This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. From __peter__ at web.de Fri Jun 24 14:27:09 2016 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jun 2016 20:27:09 +0200 Subject: [Tutor] Best way to use excepthook here? References: Message-ID: Alex Hall wrote: > One related question. This more of a style choice, I know, but I'd like > anyone's thoughts all the same. Currently, in my ADLogger.logExceptions > function, I log the exception (obviously). But I also take the opportunity > to generate and send an email, warning that the job raised an exception > and attaching the log file as well as printing the traceback in the email. > Loggers generally just log, and it feels kind of hacky to include email > code in a function that primarily logs exceptions. At the same time, this > is a perfect place to do it, because it gets called if an exception is > raised, and if that happens, the job almost certainly failed so I need to > know about it. I don't know of another way to do this since I don't have a > Python-based central manager, so can't examine exit codes or anything > else. Sending emails should be the concern of a handler, not a logger. https://docs.python.org/dev/library/logging.handlers.html#smtphandler From alan.gauld at yahoo.co.uk Fri Jun 24 15:10:23 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 Jun 2016 20:10:23 +0100 Subject: [Tutor] For-else... Any other handy constructs hiding in Python? In-Reply-To: References: Message-ID: On 24/06/16 14:07, Alex Hall wrote: > but I never knew for-else was available. for-else is useful although I don't like else for it, I would have preferred something like for-also as being more meaningful. > Are there other constructs that I may have missed Probably. :-) Are you aware of try-else? It's not often seen and never strictly needed but it does sometimes make the code intention clearer. -- 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 esawiek at gmail.com Sat Jun 25 03:04:32 2016 From: esawiek at gmail.com (Ek Esawi) Date: Sat, 25 Jun 2016 03:04:32 -0400 Subject: [Tutor] Most efficient way to read large csv files with properly converted mixed data types. Message-ID: Hi All-- My work involves reading large csv files with mixed data types (integer, float, string, time and date). I was able to accomplish the task using (1) genfromtxt or (2) looping through each line in the file and split, strip, and assign data type to each entry. I am wondering if there is a better and more efficient alternative, especially to method 2 without using numpy or pandas. Alan Gauld mentioned namedtuples for another question. I read a little about collections and in particular namedtuples but was not sure how to apply theme here, if they are applicable to begin with. Thanks in advance--EKE An example of a file: A B C D E 1 2.3 ?aa? 10/01/2016 12:30 4 25.6 ?bb? 02/02/2015 1:30 From michael.selik at gmail.com Fri Jun 24 20:10:38 2016 From: michael.selik at gmail.com (Michael Selik) Date: Sat, 25 Jun 2016 00:10:38 +0000 Subject: [Tutor] For-else... Any other handy constructs hiding in Python? In-Reply-To: References: Message-ID: On Fri, Jun 24, 2016 at 11:58 AM Alex Hall wrote: > I know loops, comprehensions, ifs, and the like, > but I never knew for-else was available. Are there other constructs that I > may have missed? > Are you familiar with context managers? https://www.python.org/dev/peps/pep-0343/ From alan.gauld at yahoo.co.uk Sat Jun 25 03:31:57 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 25 Jun 2016 08:31:57 +0100 Subject: [Tutor] Most efficient way to read large csv files with properly converted mixed data types. In-Reply-To: References: Message-ID: On 25/06/16 08:04, Ek Esawi wrote: > genfromtxt or (2) looping through each line in the file and split, strip, > and assign data type to each entry. > > I am wondering if there is a better and more efficient alternative, > especially to method 2 without using numpy or pandas. The csv module will be more reliable and probably faster than using looping with split/strip. You will still need to do the data conversions from strings to native data however. Depending how you currently do that it may be possible to improve that process using a mapping to function (similar to what genfromtext does). > Alan Gauld mentioned namedtuples for another question. I read a little > about collections and in particular namedtuples > but was not sure how to apply theme here, if they > are applicable to begin with. named tuples provide an alternative to dictionaries for read-only data and are more readable than standard tuples, but whether they have a role to play in this particular case is not clear, we'd need to know a lot more about how you plan to access the data once its converted. -- 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 esawiek at gmail.com Sun Jun 26 02:21:11 2016 From: esawiek at gmail.com (Ek Esawi) Date: Sun, 26 Jun 2016 02:21:11 -0400 Subject: [Tutor] Most efficient way to read large csv files with properly converted mixed data types. Message-ID: The first step of my plan is to do basic statistical analysis. The 2nd step is to chose a sample from each file and do more advanced statistical analysis for which i plan to use R. EKE From langberg91 at gmail.com Sun Jun 26 08:16:08 2016 From: langberg91 at gmail.com (Severin Langberg) Date: Sun, 26 Jun 2016 14:16:08 +0200 Subject: [Tutor] Coding help 1D diffusion equation Message-ID: Hello! I have a problem with solving the 1D diffusion equation for Gaussian initial using odeint. Anyone care to help? Best, S From alan.gauld at yahoo.co.uk Sun Jun 26 08:52:37 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 26 Jun 2016 13:52:37 +0100 Subject: [Tutor] Coding help 1D diffusion equation In-Reply-To: References: Message-ID: On 26/06/16 13:16, Severin Langberg wrote: > I have a problem with solving the 1D diffusion equation for Gaussian > initial using odeint. Anyone care to help? You need to realize that most folks on this list have no idea what that means. You need to provide a bit more detail. What is the 1D diffusion equation? What does Gaussian initial mean? What is odeint? What exactly do you need help with? Do you know how to solve the equation mathematically? If not, you should probably ask on a math/science forum. If you know the math but can't translate it then let us see what the math looks like and what you have tried in Python and we might then be able to help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From marcus.luetolf at bluewin.ch Sun Jun 26 11:35:35 2016 From: marcus.luetolf at bluewin.ch (=?iso-8859-1?Q?marcus_l=FCtolf?=) Date: Sun, 26 Jun 2016 17:35:35 +0200 Subject: [Tutor] installing openpyxl Message-ID: <5bea01d1cfc0$653a6010$2faf2030$@bluewin.ch> dear Experts, first question : could someone please tell me what exactly I have to type in my a) Python 35 ? command line or b) desktopcomputer ( W10, 64bit)-command line in ordert to install openpyxl which I downloaded in C:\Users\marcus\Downloads on my computer. I have used all kinds of commands with ?pip install? at the end, all unsuccessful. Second question: (this might not be a topic for this forum but an answer would be most welcome) I try to write code for parsing the internet using a raspberry Pi B+ via remote access with puTTY from my desktop computer. I am unable to find the appropriate password, ?raspberry? is denied. Thanks for help. Marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From robertvstepp at gmail.com Mon Jun 27 22:52:50 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 27 Jun 2016 21:52:50 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit Message-ID: My eyes are glazing over from hours and hours of Googling on this. I cannot come to a rational conclusion. Perhaps someone can help me to do so. I currently have a 64-bit desktop PC that I built myself. It is running Win7 Pro 64-bit. I need to keep this OS as I need to run various chess software which can be quite CPU and RAM hogging. So an emulation layer like Wine would not be desirable. I don't want to run Linux in a virtual environment; I'd rather have a dual-boot setup. My intent is to transition to mostly using Linux as my main OS and only going back to Win7 when I must use certain software applications. Also, this would be a fine time to stop Easter-egging around the shell, etc., and more systematically learn *nix-stuff. And quite frankly I am getting quite tired of PowerShell and cmd.exe; at least compared to my Solaris 10 experiences. So: 1) I am not ready at this time to take on the challenge of *really* getting into understanding *nix by installing something like Arch Linux and having to configure almost *everything* myself from scratch. However, I do like Arch's install once and enjoy very frequent OS and package updates, huge package repository, etc. 2) (1) led me into looking at Manjaro Linux. This looks very attractive, but there have been enough tales of woe to multi-boot installations, that it gives me a bit of pause. Otherwise, I think I would immediately go this route. 3) I do not care about eye candy. If you could see my current Windows desktop, it is just a solid plain blue. In fact, a light-weight desktop environment would be preferable as long as it was quite functional. Currently XFCE looks attractive. I first ran into this while looking at (2), but it is popping up quite frequently in other Linux distros. 4) It would be nice if the most recent development tools were part of the most recent OS version, such as the latest stable Python 3, gVim, Git, etc. One of the commonly recurring questions I see on this list (and the main one)is that the pre-installed Python from the OS is a few iterations behind the current release, how can I get the latest and make two (or more) Python versions work together without getting confused as to which I'm using, etc. I'm sure I could work through those issues, but it would be nice if (a) The latest OS release had close to the latest Python 3 release and (b) it was really easy to upgrade to the current release without wreaking havoc with OS uses of Python. 5) I would like a stable Linux installation. I'd rather not have to frequently work hard to solve quirky issues. 6) Good documentation available would be a solid plus as well as a dedicated, helpful (to newbies like myself) community (Like Tutor!) that can easily tolerate sometimes very stupid questions without flaming me for my ignorance. ~(:>)) 7) It should be easy to install existing software packages without having to compile everything from source. It would be nice if (to me) hidden dependencies are made clear, though I realize that part of the *nix learning curve is figuring out how to handle these sorts of issues. 8) How troublesome is malware for Linux? I realize that it is not the normal target of crackers, but is it common enough that I need to install whatever the Linux equivalent is of anti-malware/virus software? 9) Despite having an i7 quad-core 3.6 GHz CPU with 32 GB RAM, it seems that Windows with all of the constant security updating, etc., tends to make my PC sluggish and I am tired of sifting through everything periodically to clear out the cruft and startup junk that loads. I *really* would like to have a snappy OS where everything *stays* snappy with minimal effort on my part. 10) I have a hard drive that has mostly text-based stuff, like Python programs, which is formatted NTFS. Can I share this with both Win7 and Linux? What about the differences in line endings? Am I going to have to be constantly converting back and forth? I guess that is most of it. It will be interesting to hear your thoughts! Thanks in advance! -- boB From steve at pearwood.info Mon Jun 27 23:48:08 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 28 Jun 2016 13:48:08 +1000 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: Message-ID: <20160628034806.GF27919@ando.pearwood.info> On Mon, Jun 27, 2016 at 09:52:50PM -0500, boB Stepp wrote: > My eyes are glazing over from hours and hours of Googling on this. I > cannot come to a rational conclusion. Perhaps someone can help me to > do so. I currently have a 64-bit desktop PC that I built myself. It > is running Win7 Pro 64-bit. I need to keep this OS as I need to run > various chess software which can be quite CPU and RAM hogging. So an > emulation layer like Wine would not be desirable. I don't want to run > Linux in a virtual environment; I'd rather have a dual-boot setup. What about running Win7 in a virtual machine? Otherwise, I like: Linux Mint. Good software repositories, more conservative than Ubuntu, not as stick-in-the-mud as Debian. Based on Debian/Ubuntu so the quality is good, mostly aimed at non-hard core Linux geeks. Also Debian. Not Ubuntu. I used to be a Fedora guy, but then they started changing too quickly for my tastes. If you didn't upgrade to the latest and greatest every fifteen minutes, you couldn't get help. So I moved to Centos, but that's a very conservative distribution, and its hard to packages for it unless you're prepared to build them yourself. > 1) I am not ready at this time to take on the challenge of *really* > getting into understanding *nix by installing something like Arch > Linux and having to configure almost *everything* myself from scratch. > However, I do like Arch's install once and enjoy very frequent OS and > package updates, huge package repository, etc. Most distros are like that. The difference with Arch is that package management means downloading and compiling source code, rather than having others compile it for you. > 2) (1) led me into looking at Manjaro Linux. I don't know that one. > 3) I do not care about eye candy. If you could see my current > Windows desktop, it is just a solid plain blue. In fact, a > light-weight desktop environment would be preferable as long as it was > quite functional. Currently XFCE looks attractive. XFCE works very well. You might like TDE (Trinity), which is a fork of KDE 3 after the KDE developers turned it into the abomination of KDE 4. > 4) It would be nice if the most recent development tools were part of > the most recent OS version, such as the latest stable Python 3, gVim, > Git, etc. One of the commonly recurring questions I see on this list > (and the main one)is that the pre-installed Python from the OS is a > few iterations behind the current release, how can I get the latest > and make two (or more) Python versions work together without getting > confused as to which I'm using, etc. That's actually not that hard. I'll reply to that in more detail later. > 5) I would like a stable Linux installation. I'd rather not have to > frequently work hard to solve quirky issues. Linux is *extremely* stable. The problem is, when things don't work, it's usually only the quirky issues that don't work. There's a couple of exceptions to this rule. Bluetooth is quirky on Linux, and support for hardware suspend is awful. > 6) Good documentation available would be a solid plus as well as a > dedicated, helpful (to newbies like myself) community (Like Tutor!) > that can easily tolerate sometimes very stupid questions without > flaming me for my ignorance. ~(:>)) Heh, good luck :-) Reading Stackoverflow is good for that. Contributing to SO, not so much. Stay away from the IRC channels, they tend to eat newbies alive. > 7) It should be easy to install existing software packages without > having to compile everything from source. It would be nice if (to me) > hidden dependencies are made clear, though I realize that part of the > *nix learning curve is figuring out how to handle these sorts of > issues. Dependency issues? What are those? That's what package management is for. Whether you use yum on Red Hat based systems (Centos, Fedora) or apt-get and aptitude on Debian based systems (Ubuntu, Linux Mint), you'll rarely care about dependencies. > 8) How troublesome is malware for Linux? I realize that it is not > the normal target of crackers, but is it common enough that I need to > install whatever the Linux equivalent is of anti-malware/virus > software? Malware? What's that? *wink* I'll follow up with a longer response later. > 9) Despite having an i7 quad-core 3.6 GHz CPU with 32 GB RAM, it > seems that Windows with all of the constant security updating, etc., > tends to make my PC sluggish and I am tired of sifting through > everything periodically to clear out the cruft and startup junk that > loads. I *really* would like to have a snappy OS where everything > *stays* snappy with minimal effort on my part. That will be called Linux :-) -- Steve From alan.gauld at yahoo.co.uk Tue Jun 28 04:15:03 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 28 Jun 2016 09:15:03 +0100 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: Message-ID: On 28/06/16 03:52, boB Stepp wrote: Steven has already repolied and my immediate responbse was almost word for word what he said, sop I'll start again and you can consider this an addendum to Steve's message :-) > 1) I am not ready at this time to take on the challenge of *really* > getting into understanding *nix by installing something like Arch > Linux and having to configure almost *everything* myself from scratch. Debian and Slackware are the nearest I've gotten to that. Now I use Mint because I don't want to do that... > However, I do like Arch's install once and enjoy very frequent OS and > package updates, huge package repository, etc. Other distros do that too. For example Mint has a variant that continuously updates. Personally I prefer to keep some control and only update when I want to. It improves stability too. > quite functional. Currently XFCE looks attractive. I first ran into > this while looking at (2), but it is popping up quite frequently in > other Linux distros. I use Mint on both my desktop and netbook. The desktop uses Cinnamon while the netbook uses XFCE. I hardly notice the difference. > 4) It would be nice if the most recent development tools were part of > the most recent OS version, such as the latest stable Python 3, gVim, > Git, etc. You usually get the latest but one version when a distro release first comes out. Distros tend to favour reliability over novelty. That's where the rolling updates tend to be better, if you want to be on the bleeding edge. I've just checked my Mint 17 package manager and 3.4 is still the latest available. > and make two (or more) Python versions work together without getting > confused as to which I'm using, etc. I'll let Steve answer this in more detail, my approach is very conservative and I have no plans to upgrade to 3.5 until it appears courtesy of Mint. If I did have such a need I'd wait till a deb package appeared somewhere and download that rather than build from scratch. But these days I'm lazy like that... > upgrade to the current release without wreaking havoc with OS uses of > Python. That should not be a problem. > 6) Good documentation available would be a solid plus as well as a > dedicated, helpful (to newbies like myself) community (Like Tutor!) The bigger (better known) the distro the better the support. This is especially true when dealing with commercial software(yes it doees exist for Linux!). For example I use Corel Openshot for photo processing and their support is limited to a couple of distros - and in my experience not great even there! But the forums will generally make up for any "official" ignorance! > 7) It should be easy to install existing software packages without > having to compile everything from source. Compiling is not necessarily a problem When I used Slackware it tended to download source and build it but the package manager did all of that for me, I very rarely had to type 'make' myself. > 8) How troublesome is malware for Linux? I've been running Linux since 1994 and using it as my main OS since 2008 and I've never experienced any malware. (The same is true on my Mac which I bought in 2000) On Windows I've had 3 instances to deal with (since 1991). If you take sensible precautions Malware should not be an issue. (I do have Clam anti-virus and run a scan once a week or so, but it has never found anything) > loads. I *really* would like to have a snappy OS where everything > *stays* snappy with minimal effort on my part. I'm running Mint 17 on my main desktop (4 cores, 3GHz, 8GB RAM, 1.5TB disk space) and on my ancient(2008) eeePC Netbook (1 Atom CPU @ 1GHz, !GB RAM, 20GB "disk" - actually flash memory). That's the exact same OS, apart from desktop, and performance is fine on both. > 10) I have a hard drive that has mostly text-based stuff, like Python > programs, which is formatted NTFS. Can I share this with both Win7 > and Linux? Yes, I do that with several disks and I had to check the disk tool to see which was which. > What about the differences in line endings? Am I going to > have to be constantly converting back and forth? Not unless you work on files across OS. If you work on Windows files on Windows and Linux on Linux it's not an issue. But if you work across OS it's worth finding tools (like VIM) that can handle it transparently. But there is no doubt it does occasionally bite you with less intelligent tools - especially command-line filters and the like, and even more so if they use regex. (But it's not an NTFS issue since FAT disks will be the same) I run Virtual Box for XP which I only use when testing new software that needs to run on it. I do have two dedicated PCs for running Windows, one runs Windows 10 which I use mainly to keep abreast of what's what in Windows land and the other runs Windows 7 for when I really need Photoshop/Lightroom/DxPro (increasingly rare). They both use the Linux box (over hardwired Gigabit LAN) for the bulk of their disk storage. And all 3 are tied together via a KVM box so I only have one monitor/mouse/keyboard. I also run Cygwin on both as a *nix comfort blanket! :-) I hope some of that is useful... -- 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 aarongr95.15 at gmail.com Tue Jun 28 01:54:05 2016 From: aarongr95.15 at gmail.com (Aaron Johnson) Date: Mon, 27 Jun 2016 22:54:05 -0700 Subject: [Tutor] Help Message-ID: I have a program that is telling my i need your digital snake, but i dont want a snake. Help From alan.gauld at yahoo.co.uk Tue Jun 28 04:39:27 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 28 Jun 2016 09:39:27 +0100 Subject: [Tutor] Help In-Reply-To: References: Message-ID: On 28/06/16 06:54, Aaron Johnson wrote: > I have a program that is telling my i need your digital snake, but i dont > want a snake. Help I'm not sure what you expect us to do based on the information you've given. Can you be more specific? What OS are you running? On what platform (phone, tablet,laptop, desktop?) What program says you need a snake (I assume you mean python?) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dpalao.python at gmail.com Tue Jun 28 04:52:28 2016 From: dpalao.python at gmail.com (David Palao) Date: Tue, 28 Jun 2016 10:52:28 +0200 Subject: [Tutor] Help In-Reply-To: References: Message-ID: 2016-06-28 7:54 GMT+02:00 Aaron Johnson : > I have a program that is telling my i need your digital snake, but i dont > want a snake. Help > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor I think you have to decide. I see two options: 1) keep obeying your program. That sounds a bit hazardous to me. Consider that the 3 laws of robotics apply to machines not to humans! 2) Ignore your program. You ask for help. My suggestion is: take 2). Best From akleider at sonic.net Tue Jun 28 10:58:22 2016 From: akleider at sonic.net (Alex Kleider) Date: Tue, 28 Jun 2016 07:58:22 -0700 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: <20160628034806.GF27919@ando.pearwood.info> References: <20160628034806.GF27919@ando.pearwood.info> Message-ID: On 2016-06-27 20:48, Steven D'Aprano wrote: > Also Debian. Not Ubuntu. Can you elaborate why you specifically exclude Ubuntu? From steve at pearwood.info Tue Jun 28 12:10:12 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Jun 2016 02:10:12 +1000 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: Message-ID: <20160628161011.GH27919@ando.pearwood.info> Following up from my earlier post... On Mon, Jun 27, 2016 at 09:52:50PM -0500, boB Stepp wrote: > 4) It would be nice if the most recent development tools were part of > the most recent OS version, such as the latest stable Python 3, gVim, > Git, etc. Generally, the major distros have something very close to the most recent version of the various tools, applications, OS, etc. For those who like to live on the bleeding edge, you can install something like Debian unstable, and what fun that is, but generally it's better to use the stable versions instead. > One of the commonly recurring questions I see on this list > (and the main one)is that the pre-installed Python from the OS is a > few iterations behind the current release, how can I get the latest > and make two (or more) Python versions work together without getting > confused as to which I'm using, etc. My usual advice is: - Don't touch the system Python. Consider it a black box and leave it alone. To be more precise, it's okay to *use it* to run software, it's even okay to use your system's package management tools to install new libraries for it (but they're unlikely to be the latest version), but anything that requires you to manually touch it, don't do it! - Instead, install a second (or as many as you want) seperate Pythons and use them. I always install from source, so if I can do it, anyone can :-) The instructions in the README file are pretty good, but if you need help, feel free to ask. That will be on-topic :-) Basically, I download whichever versions I want. Really old versions, like Python 1.5 and older, may be hard to compile. Moderately old ones, like 2.1 or 2.2, are hard to get support for readline and other goodies. But unless you're a geek like me, why are you worried about ancient versions of Python? So if you concentrate on Python 3, they should be more-or-less easy to compile. Now you have the system Python, which you run using "python", and the one you installed, which you run using "python3". Easy. If you have multiple versions of Python, say 3.5 and 3.6, you can just specify the version: "python3.5". Or you can provide the full path. Or you can set up a shell alias, for interactive use. In my .bashrc file I have a bunch of aliases and commands for making Python easier to use: # my system Python is Python 2.4 alias python='python2.7' # set some extra directories to look for Python modules export PYTHONPATH="..." # run the Python startup file on interactive startup export PYTHONSTARTUP=/home/steve/python/utilities/startup.py # but not in Python 1.5, because that's too old alias python1.5='env -u PYTHONSTARTUP python1.5' Because these commands only apply to me, when I type "python" at the shell prompt, I get Python 2.7, but when a vendor script runs "python", it gets the version it expects, namely 2.4. [...] > 7) It should be easy to install existing software packages without > having to compile everything from source. It would be nice if (to me) > hidden dependencies are made clear, though I realize that part of the > *nix learning curve is figuring out how to handle these sorts of > issues. As I mentioned before, that's the whole point of the package management system. Coming from Windows, you really don't know what you're missing. > 8) How troublesome is malware for Linux? I realize that it is not > the normal target of crackers, but is it common enough that I need to > install whatever the Linux equivalent is of anti-malware/virus > software? Technically, there is malware for Linux. The company I work for was once even hired to remove a Linux virus from a system. Back in 1998 or thereabouts. "Drive by downloads", viruses, spyware etc. that you're familiar with from Windows is practically non-existent in the Linux world. There's probably some malware that might attack your browser, but I've never heard of it, and if you run NoScript in Firefox, and block untrusted sites, you should be fine. However, if your Linux machine is visible from the Internet, you should expect to be constantly under attack from remote bots trying to log in. I have two firewalls between me and the Internet: - my router runs a firewall; - my Linux server runs a firewall. Make sure your Linux system is running a firewall. iptables or ipchains are the standard firewalls for Linux, although I daresay by now Ubuntu has invented its own... If you allow remote access to your Linux machine via ssh, make sure you disable root logins over ssh. I also prefer to run Fail2Ban, which bans (temporarily, or permanently) logins from IP addresses if they have too many failed attempts. > 9) Despite having an i7 quad-core 3.6 GHz CPU with 32 GB RAM, it > seems that Windows with all of the constant security updating, etc., > tends to make my PC sluggish and I am tired of sifting through > everything periodically to clear out the cruft and startup junk that > loads. I *really* would like to have a snappy OS where everything > *stays* snappy with minimal effort on my part. That will be Linux, with a couple of provisos: (a) If you have slow hard drives, AND you a disk I/O intensive process running, then things may go a bit sluggish until it has finished. Traditionally, Linux runs a bunch of jobs at 3 or 4am, such as updating the "locate" database, and scanning and RAID disks to ensure they are healthy. Being a night-owl, I'm often still up when they run, and for five or ten minutes things run a bit slower for me. (b) GUI web browsers eat performance. I gave up using Chrome because it performs like a three-legged dog dragging a Grand Piano through a swamp. Oh, it's okay if you run two or three or maybe even a dozen tabs, and regularly quit and restart, but if you're like me and you have two or three hundred tabs open and you don't restart for days or weeks at a time, Chrome is *poison* to your system. Firefox is better. I can easily run 300+ tabs in Firefox, for days at a time. That's mostly thanks to NoScript: it blocks unnecessary Javascript, which mostly means ads. On rare occasions where I find it necessary to unblock ad providers, Firefox performance plummets, and I invariably end up quiting it and restarting. (Sometimes closing the offending tab alone is not enough to kill the running Javascript.) Depending on just how much CPU and memory the thrice-damned ads are using, you may see other applications impacted. But Linux is designed to run *hundreds* of users on quite low end equipment, so even if Firefox is struggling, other applications with lower requirements may continue merrily. > 10) I have a hard drive that has mostly text-based stuff, like Python > programs, which is formatted NTFS. Can I share this with both Win7 > and Linux? What about the differences in line endings? Am I going to > have to be constantly converting back and forth? First, the easy part: line endings. No, you won't. Most Linux editors can easily deal with Windows line endings, and should detect them and automatically use them as needed. If they don't, you should be able to set a preference to use Windows line endings always. Now, the hard part... I've never tried to share an internal partition between Linux and Windows on a dual-boot system. I'm not sure how well it will work. I think you would have to set up your Windows system to use two distinct drives: C drive for the OS and maybe your personal home directory; D drive for the shared area. Being Windows, that will need to be NTFS. Anything else is too difficult. Then, in Linux, you would have to get it to mount at least the shared D area, and I'm not sure how that will work. I've never tried it. (Speaking of partitions, there's a fashion these days amount Linux distros to use a single partition for the entire system. I'm old-school and think that's a bad idea. At the very least, make /home a separate partition from the rest of / so that when you eventually upgrade, you can keep your home directory and blow away the old OS before installing a new one. Also, if you ever have to do disk recovery, it's much easier with multiple partitions.) -- Steve From steve at pearwood.info Tue Jun 28 12:58:14 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Jun 2016 02:58:14 +1000 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> Message-ID: <20160628165813.GI27919@ando.pearwood.info> On Tue, Jun 28, 2016 at 07:58:22AM -0700, Alex Kleider wrote: > > > On 2016-06-27 20:48, Steven D'Aprano wrote: > > >Also Debian. Not Ubuntu. > > Can you elaborate why you specifically exclude Ubuntu? I've been bitten by an Ubuntu install where half of the GUI apps were unstable and simply didn't work. They either wouldn't launch at all, or they'd launch and as soon as you tried to do something they'd crash. And no, it wasn't using the unstable repo. And then Ubuntu went to Unity, and a few other annoyances which individually wouldn't matter much, but the overall feel is just ... wrong. For instance, Mark Shuttleworth is now suggesting that Ubuntu is going to lead the way to a brave new world of package management "snap": http://kmkeen.com/maintainers-matter/ No thank you, I don't want to get my software directly from the vendor, at least not exclusively. I just get the feeling that Ubuntu is keen to disrupt working systems just for the sake of disruption, and that the community is filled with the Cascade of Attention-Deficit Teenagers that Jamie Zawinski thinks so highly of . And then I noticed that they have a *tutorial* to teach people how to sign their Code Of Conduct, said tutorial starting with "First, create a Launchpad account": https://wiki.ubuntu.com/Forums/CoCSA_Tutorial at which point I decided they've lost the plot. I think at this point probably the only thing which would get me to go back to Ubuntu is if they said "You know what, systemd actually is a terrible idea" and didn't replace it with something worse. -- Steve From robertvstepp at gmail.com Tue Jun 28 13:49:17 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 28 Jun 2016 12:49:17 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: Message-ID: On Mon, Jun 27, 2016 at 9:52 PM, boB Stepp wrote: [...] > 10) ... I forgot one concern last night: 11) My current graphics adapter is made by NVIDIA. Their drivers are proprietary. Is this going to be a deal breaker for Linux? 12) And what about wireless networking? My ASUS motherboard has builtin wireless, but the software used to manage its settings may be (I cannot remember for certain.) Windows-only. Will this be an issue? -- boB From david at graniteweb.com Tue Jun 28 14:46:03 2016 From: david at graniteweb.com (David Rock) Date: Tue, 28 Jun 2016 13:46:03 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: Message-ID: Here?s my take on a lot of this (it?s similar to what?s been said already, so this is more of a general philosophy of distros). There are basically three types of distros (you can subdivide 100 ways, but these are the primary categories) 1. slower-moving, very stable, binary installs 2. fast-moving, stable-ish, binary installs 3. fast-moving, stable-ish, source installs In a relative sense, linux stability is good regardless. I only point out ?very stable? because they are typically bulletproof on purpose, at the expense of some flexibility. #1 examples: Debian stable (codename jessie) Red Hat Enterprise Linux (RHEL) CentOS (a free RHEL repackaging) The primary issue with these slow-moving binary distros is they are stable by not introducing new things quickly, so the versions of software available in their software repositories are sometimes ancient. That?s not to say you can?t install something newer, but it won?t be ?standard.? If you are looking for latest and greatest, these might not be for you, but they are good for what they do. I support RHEL servers as my day job, but use debian at home. Since debian recently changed it?s stable branch, it does contain many ?reasonably new? versions of most software compared to RHEL, but you won?t see fast adoption of new stuff moving forward (besides security updates). #2 examples: debian testing (codename stretch) Fedora (this is where things get tested before they go into RHEL) Ubuntu (based on debian) Pros with #2: "latest and greatest? available in the official repositories Cons: "latest and greatest? available in the official repositories It?s a double-edged sword. The closer you get to the bleeding edge, the higher the risk of something being ?not quite right? but you are much more likely to find newer versions of software. It?s also worth noting; don?t let the ?testing? in debian testing scare you, it?s still a very stable distro. #3 examples: gentoo linux from scratch (LFS) These are interesting in that all code is built from source, rather than installed as binary packages. In the case of gentoo, that?s not really a problem, though. The package management tools take care of the work for you, it just means it takes longer to install a given package if your system is slower at compiling. LFS is not one I would recommend unless you really want to learn how to build a linux system _literally_ from scratch. I have also run gentoo at home for years, with very few issues, but it?s an example of getting closer to the bleeding edge. Another class of linux distribution you may want to consider is anything with a ?LiveCD.? These are full distributions designed to run off a bootable CD/DVD/UBS stick. There are LiveCD versions for several of the distributions out there and they may give you a better feel for what user experience you want before taking the plunge. They don?t install to your hard drive at all (although some have an option to if you wish later) and give you an easy look at how they work. I know you didn?t want to run linux in a VM, but I highly suggest that you _do_ for a while first. Again, that is an easy way to try out several distros and decide what you like _before_ committing to a dual-boot. It?s a lot easier to handle the dual-boot once you know which one you want to try. Going back later and switching to a different distro gets sticky. Based on what you?ve listed as requirements, debian is probably a solid choice (either stable; or testing if stable doesn?t have what you need). Since this is a Python mailing list, the current pythons available in stable are 2.7.9 and 3.4.2, which should give you a good indicator if that will suit your needs (i.e., if they are ?new enough? for you). testing is 2.7.11 and 3.5.1. It?s also worth noting that debian does allow you to run stable, and cherry-pick packages from testing; that?s a little advanced (but something to keep in mind). ? David Rock david at graniteweb.com From david at graniteweb.com Tue Jun 28 14:52:27 2016 From: david at graniteweb.com (David Rock) Date: Tue, 28 Jun 2016 13:52:27 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: Message-ID: > On Jun 28, 2016, at 12:49, boB Stepp wrote: > > I forgot one concern last night: > > 11) My current graphics adapter is made by NVIDIA. Their drivers are > proprietary. Is this going to be a deal breaker for Linux? Typically, no. At a minimum, you will still have native support for basic GUI display. There are also nvidia-supplied drivers available that can be used, but they are typically needed only for advanced 3D acceleration (if at all). My 15-year old laptop had an Nvidia Geforce 2 Go card and it was fine. > 12) And what about wireless networking? My ASUS motherboard has > builtin wireless, but the software used to manage its settings may be > (I cannot remember for certain.) Windows-only. Will this be an issue? Again, not likely a problem. Most hardware nowadays is reasonably well supported, although some advanced features may not be. The easiest thing to do is google for your distro name and the brand of device and see if there are any issues. If you follow my earlier advice on trying a liveCD of your chosen distro first, that will give you a really good idea if your hardware will work. ? David Rock david at graniteweb.com From alan.gauld at yahoo.co.uk Tue Jun 28 18:12:30 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 28 Jun 2016 23:12:30 +0100 Subject: [Tutor] Most efficient way to read large csv files with properly converted mixed data types. In-Reply-To: References: Message-ID: On 26/06/16 07:21, Ek Esawi wrote: > The first step of my plan is to do basic statistical analysis. The 2nd step > is to chose a sample from each file and do more advanced statistical > analysis for which i plan to use R. If you are just extracting data and repackaging it for R then the format doesn't matter too much. You certainly won;t need a named tuple. Either use the dictionary reader if you want to access fields by name (especially good if the incoming CSV file format is likely to change) or use the standard reader and use indexes to get the fields you need. Writing it out you can again use the csv writers. Which writer will depend on the format your stats package wants/prefers. -- 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 wolfgang.maier at biologie.uni-freiburg.de Tue Jun 28 19:02:57 2016 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Wed, 29 Jun 2016 01:02:57 +0200 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: <20160628165813.GI27919@ando.pearwood.info> References: <20160628034806.GF27919@ando.pearwood.info> <20160628165813.GI27919@ando.pearwood.info> Message-ID: On 28.06.2016 18:58, Steven D'Aprano wrote: > On Tue, Jun 28, 2016 at 07:58:22AM -0700, Alex Kleider wrote: >> >> >> On 2016-06-27 20:48, Steven D'Aprano wrote: >> >>> Also Debian. Not Ubuntu. >> >> Can you elaborate why you specifically exclude Ubuntu? > > I've been bitten by an Ubuntu install where half of the GUI apps were > unstable and simply didn't work. They either wouldn't launch at all, or > they'd launch and as soon as you tried to do something they'd crash. And > no, it wasn't using the unstable repo. > > And then Ubuntu went to Unity, and a few other annoyances which > individually wouldn't matter much, but the overall feel is just ... > wrong. For instance, Mark Shuttleworth is now suggesting that Ubuntu is > going to lead the way to a brave new world of package management "snap": > > http://kmkeen.com/maintainers-matter/ > > No thank you, I don't want to get my software directly from the vendor, > at least not exclusively. > > I just get the feeling that Ubuntu is keen to disrupt working systems > just for the sake of disruption, and that the community is filled with > the Cascade of Attention-Deficit Teenagers that Jamie Zawinski > thinks so highly of . > > And then I noticed that they have a *tutorial* to teach people how to > sign their Code Of Conduct, said tutorial starting with "First, create a > Launchpad account": > > https://wiki.ubuntu.com/Forums/CoCSA_Tutorial > > at which point I decided they've lost the plot. > ok, this *very* subjective Ubuntu-bashing by Steven, begs a contrary opinion: I'm running Ubuntu (currently 14.04, going to switch to 16.04 soon) on my laptop in a dual-boot configuration since several years now - without any problem worth mentioning here. The Windows was Windows 7 first, now since half a year is Windows 10 (which by itself has lots of issues still, but none related to dual booting). Not everybody likes Unity, but if you want a more traditional look and feel and you're worried about performance, you can always go for Ubuntu Mate, which I'm using on another system and have nothing to complain about. Regarding snap packages, the thing that concerns me about them is that canonical, once more, tries to develop something separately from the rest of the Linux world. The concept itself, however, is not a diabolic invention by them (as Steven likes to put it). Look at FlatPak, which is the cross-Linux equivalent of snap packages and sees now better support by the latest Fedora release (https://fedoramagazine.org/introducing-flatpak/). Ubuntu has an extremely large (for a Linux distribution) user base and as pointed out by others that's a clear advantage when you try to solve problems with it. Regarding file system access, I never bothered to set up a dedicated shared partition just for data exchange. Ubuntu reads and writes the NTFS-formatted regular Windows partition without any problem (since years as I said already). Of course, the other way around does not work so you have to remember to copy things over to the Windows partition if you want to have them available in Windows. As you can maybe guess from my rather moderate tone here, I am not very much into Linux distro wars. I'm using a Fedora machine at work (on a triple(!) boot machine (with Windows 7 and OS X)), which also works very well and if you go for *any* major distribution you will probably be fine. Just don't be afraid of Ubuntu because certain people have strong feelings about it. Best, Wolfgang From robertvstepp at gmail.com Tue Jun 28 19:16:11 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 28 Jun 2016 18:16:11 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: <20160628034806.GF27919@ando.pearwood.info> References: <20160628034806.GF27919@ando.pearwood.info> Message-ID: On Mon, Jun 27, 2016 at 10:48 PM, Steven D'Aprano wrote: > On Mon, Jun 27, 2016 at 09:52:50PM -0500, boB Stepp wrote: >> My eyes are glazing over from hours and hours of Googling on this. I >> cannot come to a rational conclusion. Perhaps someone can help me to >> do so. I currently have a 64-bit desktop PC that I built myself. It >> is running Win7 Pro 64-bit. I need to keep this OS as I need to run >> various chess software which can be quite CPU and RAM hogging. So an >> emulation layer like Wine would not be desirable. I don't want to run >> Linux in a virtual environment; I'd rather have a dual-boot setup. > > What about running Win7 in a virtual machine? What type of performance hit will I take when running CPU intensive processes? I don't yet have any real experiences with running virtual machines. If this is acceptable, I am willing to forget the dual-boot idea and just jump in the deep end with Linux. The only thing I would hate is reinstalling Win7 into the virtual environment and the endless sequence of updating ... > > Otherwise, I like: > > Linux Mint. Good software repositories, more conservative than Ubuntu, > not as stick-in-the-mud as Debian. Based on Debian/Ubuntu so the quality > is good, mostly aimed at non-hard core Linux geeks. Alan obviously likes this distro. And my teacher wife at the beginning of this summer break switched several of her class PCs to Mint. Be nice to be writing software for the same environment, so this might be a positive here. -- boB From wolfgang.maier at biologie.uni-freiburg.de Tue Jun 28 19:28:32 2016 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Wed, 29 Jun 2016 01:28:32 +0200 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> Message-ID: On 29.06.2016 01:16, boB Stepp wrote: > On Mon, Jun 27, 2016 at 10:48 PM, Steven D'Aprano wrote: >> What about running Win7 in a virtual machine? > > What type of performance hit will I take when running CPU intensive > processes? I don't yet have any real experiences with running virtual > machines. If this is acceptable, I am willing to forget the dual-boot > idea and just jump in the deep end with Linux. The only thing I would > hate is reinstalling Win7 into the virtual environment and the endless > sequence of updating ... > Of course, there is a performance hit when using a virtual machine. After all, you have a running Windows or Linux, whichever way round you do things, from which you start it. How much that matters, depends strongly on what you want to do though. After all, you could do performance-critical stuff in your real OS instead of inside the VW. I still like dual-boot better for home use. If you have multiple users or even just guests they might not all be used to having to start a VM to use Windows. >> >> Otherwise, I like: >> >> Linux Mint. Good software repositories, more conservative than Ubuntu, >> not as stick-in-the-mud as Debian. Based on Debian/Ubuntu so the quality >> is good, mostly aimed at non-hard core Linux geeks. > > Alan obviously likes this distro. And my teacher wife at the > beginning of this summer break switched several of her class PCs to > Mint. Be nice to be writing software for the same environment, so > this might be a positive here. > From david at graniteweb.com Tue Jun 28 19:34:45 2016 From: david at graniteweb.com (David Rock) Date: Tue, 28 Jun 2016 18:34:45 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> Message-ID: <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> > On Jun 28, 2016, at 18:16, boB Stepp wrote: > > On Mon, Jun 27, 2016 at 10:48 PM, Steven D'Aprano wrote: >> >> >> What about running Win7 in a virtual machine? > > What type of performance hit will I take when running CPU intensive > processes? I don't yet have any real experiences with running virtual > machines. Ultimately, not likely to be all that much. The bigger constraint with running VMs is often available ram. >> >> Otherwise, I like: >> >> Linux Mint. Good software repositories, more conservative than Ubuntu, >> not as stick-in-the-mud as Debian. Based on Debian/Ubuntu so the quality >> is good, mostly aimed at non-hard core Linux geeks. > > Alan obviously likes this distro. And my teacher wife at the > beginning of this summer break switched several of her class PCs to > Mint. Be nice to be writing software for the same environment, so > this might be a positive here. That?s as good a reason as any. :-) As I?m sure you have gathered by now, picking a distro is a lot like picking a brand of car. *Linux* underneath is largely similar across all the distros, what you are picking is the wrapper around it. It?s more about the package manager used, and the philosophy of the maintainers than anything. The only logical option is throw a dart and just try one. If you don?t like how they do things, throw another dart until you find what you like. This is the blessing and the curse of linux; endless variety. Regarding the package management, there are basically two models: RPM-based and dpkg-based (yes, there are others, but these are the two big players). RPM-based (often referred to as yum) is anything similar to Red Hat (fedora, CentOS, etc), dpkg-based (sometimes referred to as apt) is anything based on debian (ubuntu, mint, etc). How they work is fundamentally different, but any distro that uses the same package management will largely ?feel? like any other. If you value Alan?s opinion (and arguably, your wife?s is more important), try out Mint. You may or may not like it, but you won?t know until you try. I still say a dry run in a VM to get a feel for it would do wonders for you regardless. ? David Rock david at graniteweb.com From oscar.j.benjamin at gmail.com Tue Jun 28 19:54:00 2016 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 29 Jun 2016 00:54:00 +0100 Subject: [Tutor] Coding help 1D diffusion equation In-Reply-To: References: Message-ID: On 26 Jun 2016 13:39, "Severin Langberg" wrote: > > Hello! > > I have a problem with solving the 1D diffusion equation for Gaussian > initial using odeint. Anyone care to help? Odeint is for ordinary differential equations (ODEs). The diffusion equation is a partial differential equation (PDE). It is possible to use odeint this way but it would help if you could give more information about the equation you want to solve and the boundary conditions you have. From alan.gauld at yahoo.co.uk Tue Jun 28 20:13:51 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 29 Jun 2016 01:13:51 +0100 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> Message-ID: On 29/06/16 00:34, David Rock wrote: > If you value Alan?s opinion Just to be clear, I run mint because it works but I've used many distros in the past, starting with Slackware then Red Hat then Mandrake and Ubuntu with equally good results. I don't like Ubuntu's Unity UI so switched to Mint but it's a subjective rather than political/idealogical/technical decision. > I still say a dry run in a VM to get a feel for it would do wonders I totally agree. I regularly spin up new distros just to take a look (Vector, TinyLinux, Gentoo, CentOS, have all been on a VM near me :-) As for performance hit of a VM - I only really notice it on graphics intensive stuff. Let me put it another way: many commercial data centers now only run servers under VMs. The extra maintainability, security and safety outweighs the small performance hit. When you buy a "server" from a data center these days you are most likely buying a VM image running on a much bigger box. -- 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 fred at bristle.com Tue Jun 28 17:51:06 2016 From: fred at bristle.com (Fred Stluka) Date: Tue, 28 Jun 2016 17:51:06 -0400 Subject: [Tutor] For-else... Any other handy constructs hiding in Python? In-Reply-To: References: Message-ID: <628ec841-674d-a1ea-d929-f96203c54983@bristle.com> (Re-sending to list instead of just to Alex.) Alex, Are you aware of these? - try ... finally - try ... except ... else - continue - break - with - @decorator - x = a if b else c -- Like the ternary operator a?b:c in other languages - x = a or b -- Assigns a, defaulting to b if a is false, unlike other languages that might assign True or False -- Short than the equivalent: x = a if a else b Any other cool tricks out there, anyone? --Fred ------------------------------------------------------------------------ Fred Stluka -- mailto:fred at bristle.com -- http://bristle.com/~fred/ Bristle Software, Inc -- http://bristle.com -- Glad to be of service! Open Source: Without walls and fences, we need no Windows or Gates. ------------------------------------------------------------------------ On 6/24/16 9:07 AM, Alex Hall wrote: > Hey all, > I was looking at how to make my different jobs retry themselves, and found > the for-else construct. I know loops, comprehensions, ifs, and the like, > but I never knew for-else was available. Are there other constructs that I > may have missed--shortcuts or seemingly odd pairings of control statements > like this? Python has a lot of really cool shortcuts and features, like > get/hasattr, the yield keyword, or comprehensions. I always like finding > more to add to the toolbox. > From akleider at sonic.net Tue Jun 28 22:16:17 2016 From: akleider at sonic.net (Alex Kleider) Date: Tue, 28 Jun 2016 19:16:17 -0700 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: Message-ID: <359d2e278bcae3a67a2e00f475831149@sonic.net> On 2016-06-28 11:46, David Rock wrote: > Here?s my take on a lot of this (it?s similar to what?s been said > already, so this is more of a general philosophy of distros). Very interesting reading for which I thank you. I'd be interested in knowing if you'd make a distinction between 'the latest Ubuntu' and their LTS releases? My approach has been to use LTS releases only and not bother with the ones in between. Comments? a From wolfgang.maier at biologie.uni-freiburg.de Tue Jun 28 22:47:17 2016 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Wed, 29 Jun 2016 04:47:17 +0200 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: <359d2e278bcae3a67a2e00f475831149@sonic.net> References: <359d2e278bcae3a67a2e00f475831149@sonic.net> Message-ID: On 29.06.2016 04:16, Alex Kleider wrote: > > > On 2016-06-28 11:46, David Rock wrote: >> Here?s my take on a lot of this (it?s similar to what?s been said >> already, so this is more of a general philosophy of distros). > > Very interesting reading for which I thank you. > I'd be interested in knowing if you'd make a distinction between 'the > latest > Ubuntu' and their LTS releases? > My approach has been to use LTS releases only and not bother with the ones > in between. > > Comments? > a David kind of discussed the difference between them in an earlier post when he grouped distros into three categories, quoting him: 1. slower-moving, very stable, binary installs 2. fast-moving, stable-ish, binary installs 3. fast-moving, stable-ish, source installs In a relative sense, linux stability is good regardless. I only point out ?very stable? because they are typically bulletproof on purpose, at the expense of some flexibility. #1 examples: Debian stable (codename jessie) Red Hat Enterprise Linux (RHEL) CentOS (a free RHEL repackaging) Just add Ubuntu LTS releases here, while other Ubuntu releases fall in category #2. An LTS release never sees major version upgrades over its lifetime for its Linux kernel nor for other software package-managed by canonical so near the end of it things may be pretty outdated. That's not necessarily a big deal though. If your system works for you, why change it. I've sometimes upgraded to new releases just out of interest, but never because I felt I really had to. From robertvstepp at gmail.com Wed Jun 29 12:20:31 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 29 Jun 2016 11:20:31 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> Message-ID: On Tue, Jun 28, 2016 at 6:34 PM, David Rock wrote: > >> On Jun 28, 2016, at 18:16, boB Stepp wrote: >> >> On Mon, Jun 27, 2016 at 10:48 PM, Steven D'Aprano wrote: >>> >>> >>> What about running Win7 in a virtual machine? >> >> What type of performance hit will I take when running CPU intensive >> processes? I don't yet have any real experiences with running virtual >> machines. > > Ultimately, not likely to be all that much. The bigger constraint with running VMs is often available ram. Based on everyone's input (Which all was quite helpful!) I'm going to stick with the original idea of creating a dual-boot environment. Windows 7 is already installed and up-to-date along with my other Windows-based software that isn't directly Linux-compatible. Plus, I don't play games often, but it will be nice to have W7 available for that. And I will not get *any* unnecessary performance hits on any of the existing software. > If you value Alan?s opinion (and arguably, your wife?s is more important), try out Mint. You may or may not like it, but you won?t know until you try. I still say a dry run in a VM to get a feel for it would do wonders for you regardless. Mint will receive my initial attention. I prepared a USB flash drive last night with the Cinnamon Mint 17.3 iso with the intent of trying out Mint running from the USB thumb drive, but when I got to the rebooting stage to change my boot priority I got my first surprise: My Christmas present of a Corsair mechanical gaming keyboard was not _seen_ during the boot up sequence until *after* Windows started up. So I could not get into my BIOS area! I had not noticed this earlier as I have had no need to tweak my BIOS settings since acquiring this keyboard. This inspired some online research where I also found that this keyboard is not Linux compatible, though some clever people have created some work-arounds. So I have ordered today a new mechanical keyboard that *is* Linux (or anything else) compatible out of the box with no software installation required plus a new hard drive (Apparently Hitachi may be making the most reliable hard drives these days.). My intent is to keep Windows 7 where it is and install Mint (If I still like it after playing around with it off the USB drive.) to the new hard drive. I now wonder if my Logitech M570 wireless trackball mouse will work with Linux? Again, some online work says that Logitech does not support Linux for this product, but others who have done a dual-boot setup with Windows installed before Linux seem to have found that once the mouse is connected with Windows, it will be seen by Linux. This remains to be seen! If it doesn't, I guess I will have to get a new mouse, too!! Hopefully I will be playing around with Mint tomorrow some time. -- boB From david at graniteweb.com Wed Jun 29 13:02:24 2016 From: david at graniteweb.com (David Rock) Date: Wed, 29 Jun 2016 12:02:24 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> Message-ID: > On Jun 29, 2016, at 11:20, boB Stepp wrote: > > My Christmas present of a Corsair mechanical gaming keyboard was not > _seen_ during the boot up sequence until *after* Windows started up. > So I could not get into my BIOS area! I had not noticed this earlier Which keyboard do you have? Most Corsairs have a ?BIOS switch? for exactly this issue. ? David Rock david at graniteweb.com From alan.gauld at yahoo.co.uk Wed Jun 29 13:04:07 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 29 Jun 2016 18:04:07 +0100 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> Message-ID: On 29/06/16 17:20, boB Stepp wrote: > that Logitech does not support Linux for this product, but others who > have done a dual-boot setup with Windows installed before Linux seem > to have found that once the mouse is connected with Windows, it will > be seen by Linux. This remains to be seen! FWIW I use an old PS/2 Logitech trackball on Linux with no problems (apart from two of the 5 buttons not doing anything, but I never used them anyway even on Windows. For hardware compatibility issues its less important whether the vendors officially support Linux as whether Linux supports it. The best bet for that is to search the support forums for your distro (and related ones, so for Mint I look at Mint, Ubuntu and Debian in that order) Sometimes there are additional driver packages you can install. For things like keyboard and mice I always keep a cheapo USB pair around so I can always get up and running then I can usually persuade the distro to accept whatever fancier gear I want to use later. -- 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 colin.ross.dal at gmail.com Wed Jun 29 13:05:47 2016 From: colin.ross.dal at gmail.com (Colin Ross) Date: Wed, 29 Jun 2016 14:05:47 -0300 Subject: [Tutor] Reading in large .dat file Message-ID: Good afternoon, I have a .dat file that is 214 rows by 65 columns that I would like to read into python and plot the 1st column versus all other columns. My code: ######################################################################### import numpy as np import scipy import pylab as pl import matplotlib from matplotlib.ticker import ScalarFormatter, FormatStrFormatter import sys # Load in text from .dat file sed = np.loadtxt('spectra.dat', unpack = True) # Access column data col1 = sed[0] col2 = sed[1] col3 = sed[2] col4 = sed[3] col5 = sed[4] wavelength = col1 flux_1 = col2 flux_2 = col3 flux_3 = col4 flux_4 = col5 pl.plot(wavelength,flux_1) pl.plot(wavelength,flux_2) pl.plot(wavelength,flux_3) pl.plot(wavelength,flux_4) pl.xscale('log') pl.show() ######################################################################### This is fine if I want to write out a separate line for each of the 65 columns, but I would like to simplify the code by looping over the data. Can someone please help me formatting the loop correctly? Thank you. Colin From __peter__ at web.de Wed Jun 29 13:41:13 2016 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Jun 2016 19:41:13 +0200 Subject: [Tutor] Reading in large .dat file References: Message-ID: Colin Ross wrote: > Good afternoon, > > I have a .dat file that is 214 rows by 65 columns that I would like to > read into python and plot the 1st column versus all other columns. > > My code: > > ######################################################################### > > import numpy as np > import scipy > import pylab as pl > import matplotlib > from matplotlib.ticker import ScalarFormatter, FormatStrFormatter > import sys > > # Load in text from .dat file > > sed = np.loadtxt('spectra.dat', unpack = True) for flux in sed: pl.plot(wavelength, flux) > pl.xscale('log') > > pl.show() > > ######################################################################### > > This is fine if I want to write out a separate line for each of the 65 > columns, but I would like to simplify the code by looping over the data. > Can someone please help me formatting the loop correctly? To understand why the above works (assuming it does) consider that for many data types for item in data: ... # use item is equivalent to for index in range(len(data)): item = data[index] ... # use item Unrolling the above for len(data) == 3 gives: item = data[0] ... # use first item item = data[1] ... # use second item item = data[2] ... # use third item From robertvstepp at gmail.com Wed Jun 29 13:32:41 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 29 Jun 2016 12:32:41 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> Message-ID: On Wed, Jun 29, 2016 at 12:02 PM, David Rock wrote: > >> On Jun 29, 2016, at 11:20, boB Stepp wrote: >> >> My Christmas present of a Corsair mechanical gaming keyboard was not >> _seen_ during the boot up sequence until *after* Windows started up. >> So I could not get into my BIOS area! I had not noticed this earlier > > Which keyboard do you have? Most Corsairs have a ?BIOS switch? for exactly this issue. K95 RGB. I will have to look around for setting you mention. -- boB From david at graniteweb.com Wed Jun 29 14:12:46 2016 From: david at graniteweb.com (David Rock) Date: Wed, 29 Jun 2016 13:12:46 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> Message-ID: <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> > On Jun 29, 2016, at 12:32, boB Stepp wrote: > > On Wed, Jun 29, 2016 at 12:02 PM, David Rock wrote: >> >>> On Jun 29, 2016, at 11:20, boB Stepp wrote: >>> >>> My Christmas present of a Corsair mechanical gaming keyboard was not >>> _seen_ during the boot up sequence until *after* Windows started up. >>> So I could not get into my BIOS area! I had not noticed this earlier >> >> Which keyboard do you have? Most Corsairs have a ?BIOS switch? for exactly this issue. > > K95 RGB. I will have to look around for setting you mention. It should be a physical switch on the keyboard itself ? David Rock david at graniteweb.com From colin.ross.dal at gmail.com Wed Jun 29 14:16:08 2016 From: colin.ross.dal at gmail.com (Colin Ross) Date: Wed, 29 Jun 2016 15:16:08 -0300 Subject: [Tutor] Reading in large .dat file In-Reply-To: References: Message-ID: On Wed, Jun 29, 2016 at 2:41 PM, Peter Otten <__peter__ at web.de> wrote: > Colin Ross wrote: > > > Good afternoon, > > > > I have a .dat file that is 214 rows by 65 columns that I would like to > > read into python and plot the 1st column versus all other columns. > > > > My code: > > > > ######################################################################### > > > > import numpy as np > > import scipy > > import pylab as pl > > import matplotlib > > from matplotlib.ticker import ScalarFormatter, FormatStrFormatter > > import sys > > > > # Load in text from .dat file > > > > sed = np.loadtxt('spectra.dat', unpack = True) > > for flux in sed: > pl.plot(wavelength, flux) > > > pl.xscale('log') > > > > pl.show() > > > > ######################################################################### > > > > This is fine if I want to write out a separate line for each of the 65 > > columns, but I would like to simplify the code by looping over the data. > > Can someone please help me formatting the loop correctly? > Thank you for the fast response! I have tried this and it outputs the attached image. It seems to only plot 1 curve, when I would have expected 64? > > To understand why the above works (assuming it does) consider that for many > data types > > for item in data: > ... # use item > > is equivalent to > > for index in range(len(data)): > item = data[index] > ... # use item > > Unrolling the above for len(data) == 3 gives: > > item = data[0] > ... # use first item > item = data[1] > ... # use second item > item = data[2] > ... # use third item > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Wed Jun 29 15:21:14 2016 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Jun 2016 21:21:14 +0200 Subject: [Tutor] Reading in large .dat file References: Message-ID: Colin Ross wrote: > On Wed, Jun 29, 2016 at 2:41 PM, Peter Otten <__peter__ at web.de> wrote: > >> Colin Ross wrote: >> >> > Good afternoon, >> > >> > I have a .dat file that is 214 rows by 65 columns that I would like to >> > read into python and plot the 1st column versus all other columns. >> > >> > My code: >> > >> > ######################################################################### >> > >> > import numpy as np >> > import scipy >> > import pylab as pl >> > import matplotlib >> > from matplotlib.ticker import ScalarFormatter, FormatStrFormatter >> > import sys >> > >> > # Load in text from .dat file >> > >> > sed = np.loadtxt('spectra.dat', unpack = True) >> >> for flux in sed: >> pl.plot(wavelength, flux) >> >> > pl.xscale('log') >> > >> > pl.show() >> > >> > ######################################################################### >> > >> > This is fine if I want to write out a separate line for each of the 65 >> > columns, but I would like to simplify the code by looping over the >> > data. Can someone please help me formatting the loop correctly? >> > > Thank you for the fast response! I have tried this and it outputs the > attached image. This is a text-only mailing list, so the image didn't make it through. > It seems to only plot 1 curve, when I would have expected > 64? You probably got a traceback (error information on the commandline). It is always a good idea to include that. Example: $ python3 flux2.py Traceback (most recent call last): File "flux2.py", line 15, in pl.plot(wavelength,flux) NameError: name 'wavelength' is not defined The error indicates that there is no variable called wavelength. That's because I didn't note that you use the first column to define that, sorry. Here's a modified script that should work: import numpy as np import pylab as pl sed = np.loadtxt('spectra.dat', unpack=True) wavelength = sed[0] for flux in sed[1:]: pl.plot(wavelength, flux) pl.xscale('log') pl.show() The for loop now skips the first column. From colin.ross.dal at gmail.com Wed Jun 29 15:26:43 2016 From: colin.ross.dal at gmail.com (Colin Ross) Date: Wed, 29 Jun 2016 16:26:43 -0300 Subject: [Tutor] Reading in large .dat file In-Reply-To: References: Message-ID: On Wed, Jun 29, 2016 at 4:21 PM, Peter Otten <__peter__ at web.de> wrote: > Colin Ross wrote: > > > On Wed, Jun 29, 2016 at 2:41 PM, Peter Otten <__peter__ at web.de> wrote: > > > >> Colin Ross wrote: > >> > >> > Good afternoon, > >> > > >> > I have a .dat file that is 214 rows by 65 columns that I would like to > >> > read into python and plot the 1st column versus all other columns. > >> > > >> > My code: > >> > > >> > > ######################################################################### > >> > > >> > import numpy as np > >> > import scipy > >> > import pylab as pl > >> > import matplotlib > >> > from matplotlib.ticker import ScalarFormatter, FormatStrFormatter > >> > import sys > >> > > >> > # Load in text from .dat file > >> > > >> > sed = np.loadtxt('spectra.dat', unpack = True) > >> > >> for flux in sed: > >> pl.plot(wavelength, flux) > >> > >> > pl.xscale('log') > >> > > >> > pl.show() > >> > > >> > > ######################################################################### > >> > > >> > This is fine if I want to write out a separate line for each of the 65 > >> > columns, but I would like to simplify the code by looping over the > >> > data. Can someone please help me formatting the loop correctly? > >> > > > > Thank you for the fast response! I have tried this and it outputs the > > attached image. > > This is a text-only mailing list, so the image didn't make it through. > > > It seems to only plot 1 curve, when I would have expected > > 64? > > You probably got a traceback (error information on the commandline). It is > always a good idea to include that. Example: > > $ python3 flux2.py > Traceback (most recent call last): > File "flux2.py", line 15, in > pl.plot(wavelength,flux) > NameError: name 'wavelength' is not defined > > The error indicates that there is no variable called wavelength. That's > because I didn't note that you use the first column to define that, sorry. > Here's a modified script that should work: > > import numpy as np > import pylab as pl > > sed = np.loadtxt('spectra.dat', unpack=True) > > wavelength = sed[0] > for flux in sed[1:]: > pl.plot(wavelength, flux) > > pl.xscale('log') > pl.show() > > The for loop now skips the first column. > Ah yes, this works perfect now. I had added the wavelength = sed[0], but forgot to skip it in the loop! Thank you! > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From roel at roelschroeven.net Wed Jun 29 15:31:04 2016 From: roel at roelschroeven.net (Roel Schroeven) Date: Wed, 29 Jun 2016 21:31:04 +0200 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> Message-ID: boB Stepp schreef op 2016-06-29 01:16: > On Mon, Jun 27, 2016 at 10:48 PM, Steven D'Aprano wrote: >> What about running Win7 in a virtual machine? > > What type of performance hit will I take when running CPU intensive > processes? Purely CPU-wise, the performance hit is pretty small (normal instructions are executed directly on the CPU at full speed; only privileged instructions are trapped for special treatment). But: - I/O is slower in a virtual machine. - RAM is divided between your virtual machine(s) and the host, so each machine has less available for itself. - Graphics are slower. Regular desktop use is fine if the proper drivers are installed in the virtual machine, but don't expect to run the newest games smoothly. In many use cases, virtual machines are perfectly usable; it all depends on your specific use case and your hardware. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From robertvstepp at gmail.com Wed Jun 29 15:56:05 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 29 Jun 2016 14:56:05 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> Message-ID: On Wed, Jun 29, 2016 at 1:12 PM, David Rock wrote: > >> On Jun 29, 2016, at 12:32, boB Stepp wrote: >> >> On Wed, Jun 29, 2016 at 12:02 PM, David Rock wrote: >>> >>>> On Jun 29, 2016, at 11:20, boB Stepp wrote: >>>> >>>> My Christmas present of a Corsair mechanical gaming keyboard was not >>>> _seen_ during the boot up sequence until *after* Windows started up. >>>> So I could not get into my BIOS area! I had not noticed this earlier >>> >>> Which keyboard do you have? Most Corsairs have a ?BIOS switch? for exactly this issue. >> >> K95 RGB. I will have to look around for setting you mention. > > It should be a physical switch on the keyboard itself I had forgotten about that switch since its default worked fine when I initially connected the keyboard, and it is in a very inconspicuous location on the keyboard--out of sight, out of mind. Now I can get into BIOS. BIOS sees my USB flash drive as "UEFI: Lexar USB Flash Drive 1100", but it silently refuses to boot to the iso image installed on it. Something new to puzzle out! -- boB From robertvstepp at gmail.com Wed Jun 29 18:16:40 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 29 Jun 2016 22:16:40 +0000 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> Message-ID: On Wed, Jun 29, 2016 at 7:56 PM, boB Stepp wrote: > Now I can get into BIOS. BIOS sees my USB flash drive as "UEFI: Lexar > USB Flash Drive 1100", but it silently refuses to boot to the iso > image installed on it. Something new to puzzle out! OK, I'm into a live Mint Linux session off my USB flash drive. The problem I experienced was in incompatible BIOS settings. As far as I can tell, since my W7 installation is *not* UEFI, I needed to likewise have the flash drive setup. Once I did that I got into Linux. The first thing that came up was a warning: "Running in software rendering mode. Cinnamon is currently running without video hardware acceleration and, as a result, you may observe much higher than normal CPU usage. There could be a problem with your drivers ..." Mozilla Firefox appears to be the pre-installed browser. However, I was not connected to my home's wireless network. I found the icon for that and did the needed password and MAC Address stuff and got connected. Next I found the Driver Manager application, ran it, and it recommended applying "nvidia-352, Version 352.63-0ubunto0.14.04.1, NVIDIA binary driver-version 352.63". I did so and the initial warning I mentioned above went away. I typed both "python" and "python3" in the terminal window to see what is here: Python 2.7.6 and Python 3.4.3 [Does this mean we are *on* topic now? ~(:>))]. Question: Is Python 3 used by any of Mint's OS functions? Or does it only use Python 2? No Git is pre-installed, but it immediately tells me the command to type to get it! Cool!! Typing "vi" in terminal tells me that VIM 7.4.52 is the installed version. Kind of behind, but I am sure I can update this if I go with Mint. No gVim but it again offers a list of available packages and the command to type. My initial impressions are very good! I have gotten this far with just some poking around, no Internet searching required. I like the clean default appearance of Cinnamon. Running off the USB stick it seems quite snappy. So it will only be significantly better once it is installed to a hard drive. Windowing controls are oh so slightly different from Windows, but so far everything is just *obvious*. BTW, I am typing this from Mozilla Firefox (I haven't gotten around to thinking about setting up Thunderbird.) running in Mint while listening to a Pandora music stream from my Pandora One account. Everything is just working! Like it very much! Since my wife is planning on trying out Mint on multiple PCs in her classroom this coming school year, I think I will just go with Mint. I'm sure I will encounter some warts along the way, but in my experience with enough searching and thinking and asking ... almost all warts can be dealt with. Only gripe so far is that my fancy Corsair gaming keyboard has become a normal keyboard. As I said in an earlier email, some clever people have put together a package to access most of the keyboard's features. It is still in a very early version of 0.2 or so. However, I have not gotten much use out of my fancy keyboard to date. I mostly like it because it is a mechanical keyboard and care little for the other stuff. Even though David saved the day by reminding me about the "BIOS" setting on a keyboard switch, I think I will stick with the new quite functional but not fancy mechanical keyboard coming soon and give this gaming keyboard to one of my two children who will probably appreciate it more than me. Now when the new hard drive arrives tomorrow we'll see if I can get a good dual-boot of Windows 7 and Mint Cinnamon going! Thanks for all of the help even though this has been off-topic for this list! Meanwhile, more playing around with Mint!! Cheers! -- boB From alan.gauld at yahoo.co.uk Wed Jun 29 18:34:53 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 29 Jun 2016 23:34:53 +0100 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> Message-ID: On 29/06/16 23:16, boB Stepp wrote: > No Git is pre-installed, but it immediately tells me the command to > type to get it! Cool!! If you really want to see what's on offer open the Menu->Administration->Software Manager tool And browse away... :-) -- 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 Wed Jun 29 18:37:28 2016 From: david at graniteweb.com (David Rock) Date: Wed, 29 Jun 2016 17:37:28 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> Message-ID: <40898999-E4E4-49F1-8CCC-4D1466A34D35@graniteweb.com> > On Jun 29, 2016, at 17:16, boB Stepp wrote: > OK, I'm into a live Mint Linux session off my USB flash drive. The Cool. > I typed both "python" and "python3" in the terminal window to see what > is here: Python 2.7.6 and Python 3.4.3 [Does this mean we are *on* > topic now? ~(:>))]. Question: Is Python 3 used by any of Mint's OS > functions? Or does it only use Python 2? I don?t know off-hand, but unless you plan on doing work with Mint itself, I doubt it matters much beyond the academia of knowing for knowledge?s sake. Are you concerned about a version conflict with something you plan to do on the system? We are definitely getting back on topic if you want to talk about different versions of python and whether it?s better to just work with what?s there or install something different. > No Git is pre-installed, but it immediately tells me the command to > type to get it! Cool!! If I may suggest? GitKraken is pretty nice. https://www.gitkraken.com I?m not a fan of Git, but it makes it tolerable even for a stodgy SysAdmin like myself ;-) > Now when the new hard drive arrives tomorrow we'll see if I can get a > good dual-boot of Windows 7 and Mint Cinnamon going! Having the second disk will make this a breeze. You are avoiding the biggest complication of resizing partitions on the same disk. The one suggestion I would make about the install: when it asks if you want to use LVM, say yes. It adds a layer of flexibility with you disk layout that you will be sad you don?t have later. > Thanks for all of the help even though this has been off-topic for this list! > > Meanwhile, more playing around with Mint!! have fun! ? David Rock david at graniteweb.com From robertvstepp at gmail.com Wed Jun 29 18:58:08 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 29 Jun 2016 22:58:08 +0000 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: <40898999-E4E4-49F1-8CCC-4D1466A34D35@graniteweb.com> References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> <40898999-E4E4-49F1-8CCC-4D1466A34D35@graniteweb.com> Message-ID: On Wed, Jun 29, 2016 at 10:37 PM, David Rock wrote: >> I typed both "python" and "python3" in the terminal window to see what >> is here: Python 2.7.6 and Python 3.4.3 [Does this mean we are *on* >> topic now? ~(:>))]. Question: Is Python 3 used by any of Mint's OS >> functions? Or does it only use Python 2? > > I don?t know off-hand, but unless you plan on doing work with Mint itself, I doubt it matters much beyond the academia of knowing for knowledge?s sake. Are you concerned about a version conflict with something you plan to do on the system? We are definitely getting back on topic if you want to talk about different versions of python and whether it?s better to just work with what?s there or install something different. No, not at this time. I was just curious. One point I just discovered for those new to this and Linux: tkinter does *not* come pre-installed with the Python distributions; it will have to be installed separately. Of course, I'm sure that all of you long-time Linux users knew that already! >> Now when the new hard drive arrives tomorrow we'll see if I can get a >> good dual-boot of Windows 7 and Mint Cinnamon going! > > Having the second disk will make this a breeze. You are avoiding the biggest complication of resizing partitions on the same disk. The one suggestion I would make about the install: when it asks if you want to use LVM, say yes. It adds a layer of flexibility with you disk layout that you will be sad you don?t have later. I was thinking this would be a better way to go. Thanks for the tip about LVM. Logical Volume Management was mentioned in some of installation guides I read, but not all, so I actually was going to look into this more deeply. Thanks for saving me some research! Alan Gauld said: > If you really want to see what's on offer open the > > Menu->Administration->Software Manager tool > > And browse away... :-) Wow! "74, 905 packages are available" !!! And they all come neatly organized by topic! -- boB From alan.gauld at yahoo.co.uk Wed Jun 29 20:09:18 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 30 Jun 2016 01:09:18 +0100 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> <40898999-E4E4-49F1-8CCC-4D1466A34D35@graniteweb.com> Message-ID: On 29/06/16 23:58, boB Stepp wrote: > One point I just discovered for those new to this and Linux: tkinter > does *not* come pre-installed with the Python distributions; it will > have to be installed separately. Yes, but it's just another package in the package manager. python-tk Select it and hit Install. The same is true of the documentation BTW. It's a separate package. If you use the filter field and type in Python you'll see several hundred add on packages! Including almost all of the 3rd party ones you see discussed on tutor... -- 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 Wed Jun 29 21:16:11 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 30 Jun 2016 01:16:11 +0000 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> <40898999-E4E4-49F1-8CCC-4D1466A34D35@graniteweb.com> Message-ID: On Thu, Jun 30, 2016 at 12:09 AM, Alan Gauld via Tutor wrote: > On 29/06/16 23:58, boB Stepp wrote: > >> One point I just discovered for those new to this and Linux: tkinter >> does *not* come pre-installed with the Python distributions; it will >> have to be installed separately. > > Yes, but it's just another package in the package manager. python-tk > Select it and hit Install. I just now checked on IDLE, found it was not installed, and typed in the terminal: sudo apt-get install idle3 The interesting part is since IDLE needs tkinter, it installed that dependency as well. As far as I can tell after typing "help(tkinter)" in the Python interpreter, it looks like *all* of tkinter got installed. Is this in fact true? -- boB From david at graniteweb.com Wed Jun 29 21:21:33 2016 From: david at graniteweb.com (David Rock) Date: Wed, 29 Jun 2016 20:21:33 -0500 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <20160628034806.GF27919@ando.pearwood.info> <7EC430D1-C65E-4E71-9A38-C9E2BABD396D@graniteweb.com> <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> <40898999-E4E4-49F1-8CCC-4D1466A34D35@graniteweb.com> Message-ID: <99B64449-12B9-45C3-AF14-DBF1C617B7CA@graniteweb.com> > On Jun 29, 2016, at 20:16, boB Stepp wrote: > > > The interesting part is since IDLE needs tkinter, it installed that > dependency as well. As far as I can tell after typing "help(tkinter)" > in the Python interpreter, it looks like *all* of tkinter got > installed. Is this in fact true? Most likely, yes. Welcome to package management. This is what we were talking about earlier; the package management tools are really good and do wonderful things like find all the needed dependencies for a package that you install. In some cases, that can be many-many packages depending on the complexity. All you need to worry about is the thing you want, and let the system do the rest. :-) ? David Rock david at graniteweb.com From steve at pearwood.info Thu Jun 30 00:12:51 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Jun 2016 14:12:51 +1000 Subject: [Tutor] OT: Recommendations for a Linux distribution to dual-boot with Win7-64 bit In-Reply-To: References: <1A2C40AD-4A44-4B85-8C10-5EA271DA1D5D@graniteweb.com> <40898999-E4E4-49F1-8CCC-4D1466A34D35@graniteweb.com> Message-ID: <20160630041249.GK27919@ando.pearwood.info> On Thu, Jun 30, 2016 at 01:16:11AM +0000, boB Stepp wrote: > I just now checked on IDLE, found it was not installed, and typed in > the terminal: > > sudo apt-get install idle3 > > The interesting part is since IDLE needs tkinter, it installed that > dependency as well. As far as I can tell after typing "help(tkinter)" > in the Python interpreter, it looks like *all* of tkinter got > installed. Is this in fact true? Indeed. And if Tk/Tcl weren't installed, it would have installed them as well. (But they probably were already installed.) apt-get and aptitude will generally warn you that there are dependencies that need installing, and ask you for permission. I've never quite worked out why it sometimes just installs them, no questions asked, and other times asks first. Possibly something to do with the size of the dependencies? E.g. if the dependencies are less that X bytes in size, just go ahead and install? The really amazing thing is when there is a conflict between an installed package and a dependency you need. Then aptitude will offer you various choices, such as: - leave the existing package, don't install the new one; - leave the existing package, install the new one, but not the dependencies; - remove the existing package, install the new one and its dependencies. -- Steve