From rosuav at gmail.com Sun Jan 1 00:39:27 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 1 Jan 2017 16:39:27 +1100 Subject: Cleaning up conditionals In-Reply-To: <004c01d263e3$5420fde0$27b23dae@sambora> References: <004c01d263e3$5420fde0$27b23dae@sambora> Message-ID: On Sun, Jan 1, 2017 at 2:58 PM, Deborah Swanson wrote: > I'm not sure I understand what you did here, at least not well enough to > try it. > > What conditional can I do between the 2 rows of listings (the list names > l1 and l2) that will give me which row has the value to copy from and > which one is empty? I don't see how that can happen if you don't give > the subscript [v] to each of l1 and l2, at a minimum. Unless python will > distribute the [v] inside the preceding conditional? Amazing, if true, > but then we still need what the conditional is. > > And what is new_value? It could be either l1[v] or l2[v], depending on > which one is not empty, and I don't see how the answer magically pops > into it. Not saying that it doesn't, but what should I call new_value > in the real estate listings example I want to use it in? There are no > new values in this situation, only values that need to be copied into > their empty counterparts in the other row. (It may or may not help to > read the synopsis of what I'm doing that I wrote up in my last post to > Peter Otten.) Start with this simple statement: foo[2] = "spam" Obviously this will subscript 'foo' with 2 and set that to the string "spam". Easy. But instead of the word 'foo', we could use any expression at all. def return_foo(): return foo return_foo()[2] = "spam" This is a little more surprising, but it does work. And if you can use a function call to provide the list, you can use anything else - even a conditional expression. (foo if True else [])[2] = "spam" It's perfectly legal, but I do NOT recommend it :) ChrisA From steve+python at pearwood.info Sun Jan 1 01:24:58 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 01 Jan 2017 17:24:58 +1100 Subject: Cleaning up conditionals References: <004c01d263e3$5420fde0$27b23dae@sambora> Message-ID: <5868a0bc$0$1622$c3e8da3$5496439d@news.astraweb.com> On Sun, 1 Jan 2017 02:58 pm, Deborah Swanson wrote: >> It's possible to select either l1 or l2 using an expression, >> and then subscript that with [v]. However, this does not >> usually make for readable code, so I don't recommend it. >> >> (l1 if whatever else l2)[v] = new_value >> >> ChrisA > > I'm not sure I understand what you did here, at least not well enough to > try it. The evolution of a Python programmer :-) (1) Step One: the naive code. if condition: l1[v] = new_value else: l2[v] = new_value (2) Step Two: add a temporary variable to avoid repeating the assignment if condition: temp = l1 else: temp = l2 temp[v] = new_value (3) Step Three: change the if...else statement to an expression temp = l1 if condition else l2 temp[v] = new_value (4) Step Four: no need for the temporary variable (l1 if condition else l2)[v] = new_value -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From jussi.piitulainen at helsinki.fi Sun Jan 1 02:44:30 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sun, 01 Jan 2017 09:44:30 +0200 Subject: Cleaning up conditionals References: <002001d263b1$a9e3de50$27b23dae@sambora> Message-ID: Deborah Swanson writes: > Jussi Piitulainen wrote: >> Sent: Saturday, December 31, 2016 8:30 AM >> Deborah Swanson writes: >> >> > Is it possible to use some version of the "a = expression1 if >> > condition else expression2" syntax with an elif? And for >> > expression1 and expression2 to be single statements? That's the >> > kind of shortcutting I'd like to do, and it seems like python might >> > be able to do something like this. >> >> I missed this question when I read the thread earlier. The >> answer is simply to make expression2 be another conditional >> expression. I tend to write the whole chain in parentheses. >> This allows multi-line layouts like the following alternatives: >> >> a = ( first if len(first) > 0 >> else second if len(second) > 0 >> else make_stuff_up() ) >> >> a = ( first if len(first) > 0 else >> second if len(second) > 0 else >> make_stuff_up() ) >> >> Expression1 and expression2 cannot be statements. Python >> makes a formal distinction between statements that have an >> effect and expressions that have a value. All components of a >> conditional expression must be expressions. A function call >> can behave either way but I think it good style that the >> calls in expresions return values. > > While I'm sure these terniaries will be useful for future problems, I > couldn't make the second one work for my current problem. (Note that those two things are just different layouts for the exact same conditional expression.) > I got as far as: > > a = l1[v] if len(l1[v] > 0 else > l2[v] if len(l2[v] > 0 else (Parentheses needed, otherwise the first line is expected to be a whole statement and then the unfinished expression in it is considered malformed.) > And didn't finish it because I couldn't see what a should be. I want > it to be l2[v] if the first clause is true, and l1[v] if the second. > If I was computing a value, this would work beautifully, but I don't > see how it can if I'm choosing a list element to assign to. Maybe I > just can't see it. Do you here mean condition when you say clause? Then, if the first condition is true, any other condition is not considered at all. When you come to the final else-branch, you know that all conditions in the chain were false. I thought you originally wanted to keep l1[v] if it was non-empty, which is what the code here says, but this time your prose seems different. Anyhow, since you want a value when all conditions in the chain of conditions are false, you want a value to use when the field is empty in both records. To change nothing, store the old empty value back; or you can supply your own default here. With your particular conditions of non-emptiness, which is taken to be truth, you can achieve variations of this result with any of the following statements: w = ( l1[v] if len(l1[v]) > 0 else l2[v] if len(l2[v]) > 0 else l1[v] ) x = l1[v] if l1[v] else l2[v] if l2[v] else l1[v] y = l1[v] or l2[v] or l1[v] z = l1[v] or l2[v] The last one, which I originally suggested (and still prefer when otherwise appropriate), is subtly different from the others. That difference should be irrelevant. From jussi.piitulainen at helsinki.fi Sun Jan 1 03:01:14 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sun, 01 Jan 2017 10:01:14 +0200 Subject: Cleaning up conditionals References: <004c01d263e3$5420fde0$27b23dae@sambora> <5868a0bc$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steve D'Aprano writes: > On Sun, 1 Jan 2017 02:58 pm, Deborah Swanson wrote: > >>> It's possible to select either l1 or l2 using an expression, >>> and then subscript that with [v]. However, this does not >>> usually make for readable code, so I don't recommend it. >>> >>> (l1 if whatever else l2)[v] = new_value >>> >>> ChrisA >> >> I'm not sure I understand what you did here, at least not well enough >> to try it. > > > The evolution of a Python programmer :-) > > > (1) Step One: the naive code. > > if condition: > l1[v] = new_value > else: > l2[v] = new_value > > > (2) Step Two: add a temporary variable to avoid repeating the > assignment > > if condition: > temp = l1 > else: > temp = l2 > temp[v] = new_value > > > (3) Step Three: change the if...else statement to an expression > > temp = l1 if condition else l2 > temp[v] = new_value > > > (4) Step Four: no need for the temporary variable > > (l1 if condition else l2)[v] = new_value (l1 if bool(l1[v]) < bool(l2[v]) else l2 if bool(l1[v]) > bool(l2[v]) else l1)[v] = (l2 if bool(l1[v]) < bool(l2[v]) else l1 if bool(l1[v]) > bool(l2[v]) else l1)[v] Merry new year :) From python at deborahswanson.net Sun Jan 1 05:09:33 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 1 Jan 2017 02:09:33 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <001101d26417$26d40420$27b23dae@sambora> Sorry guys. I've read all your responses and I kind of get their general drift, but I'm about four sheets to the wind right now, and no way would I make it more than a step or two in playing around with these things and trying to get my head around them before it would all descend into a cacaphony of gibberish. Python really is a thing of beauty in a class all its own. I've been doing computers since 1972. I started out doing machine code on IBM's 7090 octal machine, then I stepped up to assembler on the hexadecimal IBM 360/40, a mainframe that needed a machine room the size of two classrooms to house it and all its peripherals. (Disk drives were the size of huge round birthday cakes, tape drives were each housed in 4' x 4' cabinets, and the printers had to go out in the hallway because they couldn't even fit in the same room with the computer.) The first high level language I stepped up to was PL/1, a real behemoth of its time, a one-foot-in-front-of-the-other procedural languaage from its insides out, and a full set of all its manuals filled a small library. Funny though, for all its "primitivity" compared to modern languages, we did artificial intelligence, symbolic calculus (no numbers, just complex integrals and differentials of function names, with x, y and z for arguments) and weather modelling with it. And I've learned smatterings of Java, C and C++, but nowhere have I seen the kinds of magic that can be done with python. I've been exposed to applying functions to a list of values, so I fully believe that what you present is valid python and it works. I just don't fully understand how and will have to work with it. So, in the morning I'll scrape my braincells together and work with what you've given me, and see if I can understand it and make your examples work. Well worth the detour from finding the perfect place to move to (a project that has become a little less urgent due to recent events). So stay tuned. I'm not ignoring you, I just need a good night's sleep and then maybe a good long while before I have anything to show for doing anything with this, or even intelligent questions. Thank you all in advance, I'm expecting to learn a lot from this little exercise. From rustompmody at gmail.com Sun Jan 1 09:20:47 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Sun, 1 Jan 2017 06:20:47 -0800 (PST) Subject: Cleaning up conditionals In-Reply-To: References: <001101d26417$26d40420$27b23dae@sambora> Message-ID: <8f9a3558-add3-4e4b-9470-4c93881b185d@googlegroups.com> On Sunday, January 1, 2017 at 3:39:14 PM UTC+5:30, Deborah Swanson wrote: > Sorry guys. I've read all your responses and I kind of get their general > drift, but I'm about four sheets to the wind right now, and no way would > I make it more than a step or two in playing around with these things > and trying to get my head around them before it would all descend into a > cacaphony of gibberish. Its true that in most programming languages one is likely to see, a conditional (if) statement is likely harder to grok than a conditional expression ie if the expression is allowed at all!! Just as recursion is supposedly harder than repetition, List comprehensions harder than for-loops etc etc This is true culturally and historically but not necessarily. >From the more conceptual viewpoint they are duals; see table at http://blog.languager.org/2016/01/primacy.html#expstat Of course if one has grown up with assignment (and even more so the print statement) being (imagined to be) easy and natural and obvious, this logically unnecessary but culturally inevitable sense of difficulty will be there... unfortunately. tl;dr Above is unlikely to help reduce the confusion. The essential ideas behind the jargon called 'functional programming' would go a good way towards that. From rustompmody at gmail.com Sun Jan 1 09:32:32 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Sun, 1 Jan 2017 06:32:32 -0800 (PST) Subject: Cleaning up conditionals In-Reply-To: <8f9a3558-add3-4e4b-9470-4c93881b185d@googlegroups.com> References: <001101d26417$26d40420$27b23dae@sambora> <8f9a3558-add3-4e4b-9470-4c93881b185d@googlegroups.com> Message-ID: <54b5f5ab-5af7-4e9d-b056-8bdebf4193b9@googlegroups.com> On Sunday, January 1, 2017 at 7:51:12 PM UTC+5:30, Rustom Mody wrote: > On Sunday, January 1, 2017 at 3:39:14 PM UTC+5:30, Deborah Swanson wrote: > > Sorry guys. I've read all your responses and I kind of get their general > > drift, but I'm about four sheets to the wind right now, and no way would > > I make it more than a step or two in playing around with these things > > and trying to get my head around them before it would all descend into a > > cacaphony of gibberish. > > Its true that in most programming languages one is likely to see, > a conditional (if) statement is likely harder to grok than a conditional > expression ie if the expression is allowed at all!! Er... Meant the other way round! From grant.b.edwards at gmail.com Sun Jan 1 12:30:11 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 1 Jan 2017 17:30:11 +0000 (UTC) Subject: learning and experimenting python. References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2016-12-31, Ian Kelly wrote: > On Dec 31, 2016 3:12 AM, wrote: > > That's true. > > Please include quoted context in your replies. I have no idea who or what > you're responding to. I'd just like to thank everybody for replying to einstein1410's posts so that those of us who have plonked posts from googlegroups don't miss out on his/her valuable contributions. -- Grant From python at deborahswanson.net Sun Jan 1 17:58:40 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 1 Jan 2017 14:58:40 -0800 Subject: Cleaning up conditionals In-Reply-To: <5868a0bc$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: <002001d26482$98ad3560$27b23dae@sambora> Steve D'Aprano wrote > Sent: Saturday, December 31, 2016 10:25 PM > > On Sun, 1 Jan 2017 02:58 pm, Deborah Swanson wrote: > > >> It's possible to select either l1 or l2 using an > expression, and then > >> subscript that with [v]. However, this does not usually make for > >> readable code, so I don't recommend it. > >> > >> (l1 if whatever else l2)[v] = new_value > >> > >> ChrisA > > > > I'm not sure I understand what you did here, at least not > well enough > > to try it. > > > The evolution of a Python programmer :-) ;) > (1) Step One: the naive code. > > if condition: > l1[v] = new_value > else: > l2[v] = new_value > > > (2) Step Two: add a temporary variable to avoid repeating the > assignment > > if condition: > temp = l1 > else: > temp = l2 > temp[v] = new_value > > > (3) Step Three: change the if...else statement to an expression > > temp = l1 if condition else l2 > temp[v] = new_value > > > (4) Step Four: no need for the temporary variable > > (l1 if condition else l2)[v] = new_value > > > > > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered > up, and sure enough, things got worse. This is a very nice explanation of why Chris' statement is valid python and I completely understand it, though I didn't see how it worked when I wrote the message you're quoting. Unfortunately this isn't the part that still gives me trouble. The real troublemaker here for me, is what should new_value be defined to be? It will be either l1[v] or l2[v], depending on which one is non-empty. But I still don't see any way this terniary assigns the correct value to the correct field. If we back up to your Step One, it's clear that this dilemma is present from the get-go. (1) Step One: the naive code. if condition: l1[v] = new_value else: l2[v] = new_value Here the assumption is that new_value is the same for both if and else. But it isn't. Here's the actual desired outcome: if condition: l1[v] = l2[v] else: l2[v] = l1[v] That's why I've been saying that (l1 if whatever else l2)[v] = new_value works beautifully if you are computing a value, but it doesn't work at all if you're selecting a row to copy the field value from the other row to. From python at deborahswanson.net Sun Jan 1 18:12:24 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 1 Jan 2017 15:12:24 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <002301d26484$83cce670$27b23dae@sambora> Chris Angelico wrote on Saturday, December 31, 2016 9:39 PM > > On Sun, Jan 1, 2017 at 2:58 PM, Deborah Swanson > wrote: > > I'm not sure I understand what you did here, at least not > well enough > > to try it. > > > > What conditional can I do between the 2 rows of listings (the list > > names l1 and l2) that will give me which row has the value to copy > > from and which one is empty? I don't see how that can happen if you > > don't give the subscript [v] to each of l1 and l2, at a minimum. > > Unless python will distribute the [v] inside the preceding > > conditional? Amazing, if true, but then we still need what the > > conditional is. > > > > And what is new_value? It could be either l1[v] or l2[v], > depending on > > which one is not empty, and I don't see how the answer > magically pops > > into it. Not saying that it doesn't, but what should I > call new_value > > in the real estate listings example I want to use it in? > There are no > > new values in this situation, only values that need to be > copied into > > their empty counterparts in the other row. (It may or may > not help to > > read the synopsis of what I'm doing that I wrote up in my > last post to > > Peter Otten.) > > Start with this simple statement: > > foo[2] = "spam" > > Obviously this will subscript 'foo' with 2 and set that to > the string "spam". Easy. But instead of the word 'foo', we > could use any expression at all. > > def return_foo(): > return foo > > return_foo()[2] = "spam" > > This is a little more surprising, but it does work. > > And if you can use a function call to provide the list, you > can use anything else - even a conditional expression. > > (foo if True else [])[2] = "spam" > > It's perfectly legal, but I do NOT recommend it :) > > ChrisA I see what you're doing here and I have applied functions to values and variables before, so I know it's legal and that it works. Thanks for giving me this other way that the same principle can be used with conditionals. I know I keep repeating myself, but aside from the advisability (or not) of using this form, the real problem is that "spam" will only be the right answer in one of the two cases. The job of this conditional is to choose which value to put into which field. From python at deborahswanson.net Sun Jan 1 18:50:25 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 1 Jan 2017 15:50:25 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <002801d26489$d3073dd0$27b23dae@sambora> > -----Original Message----- > From: Python-list > [mailto:python-list-bounces+python=deborahswanson.net at python.o > rg] On Behalf Of Jussi Piitulainen > Sent: Saturday, December 31, 2016 11:45 PM > To: python-list at python.org > Subject: Re: Cleaning up conditionals > Importance: High > > > Deborah Swanson writes: > > > Jussi Piitulainen wrote: > >> Sent: Saturday, December 31, 2016 8:30 AM > >> Deborah Swanson writes: > >> > >> > Is it possible to use some version of the "a = expression1 if > >> > condition else expression2" syntax with an elif? And for > >> > expression1 and expression2 to be single statements? That's the > >> > kind of shortcutting I'd like to do, and it seems like > python might > >> > be able to do something like this. > >> > >> I missed this question when I read the thread earlier. The > >> answer is simply to make expression2 be another conditional > >> expression. I tend to write the whole chain in parentheses. > >> This allows multi-line layouts like the following alternatives: > >> > >> a = ( first if len(first) > 0 > >> else second if len(second) > 0 > >> else make_stuff_up() ) > >> > >> a = ( first if len(first) > 0 else > >> second if len(second) > 0 else > >> make_stuff_up() ) > >> > >> Expression1 and expression2 cannot be statements. Python > >> makes a formal distinction between statements that have an > >> effect and expressions that have a value. All components of a > >> conditional expression must be expressions. A function call > >> can behave either way but I think it good style that the > >> calls in expresions return values. > > > > While I'm sure these terniaries will be useful for future > problems, I > > couldn't make the second one work for my current problem. > > (Note that those two things are just different layouts for > the exact same conditional expression.) > > > I got as far as: > > > > a = l1[v] if len(l1[v] > 0 else > > l2[v] if len(l2[v] > 0 else > > (Parentheses needed, otherwise the first line is expected to > be a whole statement and then the unfinished expression in it > is considered > malformed.) Thanks, I missed that. (Never ran the code because I couldn't finish it.) > > And didn't finish it because I couldn't see what a should > be. I want > > it to be l2[v] if the first clause is true, and l1[v] if > the second. > > If I was computing a value, this would work beautifully, > but I don't > > see how it can if I'm choosing a list element to assign to. Maybe I > > just can't see it. > > Do you here mean condition when you say clause? Then, if the > first condition is true, any other condition is not > considered at all. When you come to the final else-branch, > you know that all conditions in the chain were false. This is fine, in general we wouldn't know if either l1[v] or l2[v] are non-empty, and it would be possible that both are empty. But this function is only called if they're not equal, which means one of them could be empty, or that the two fields are both non-empty and different from each other (my second test). So I had two problems implementing this code. The first one was that the final else was a dead end and I didn't see what to replace it with (though I can see now what it should be). But the biggest problem is what a is. It should be either the location l1[v] or the location l2[v], but by making it a scalar, a is not in either row. It's just a disconnected variable, so the statement doesn't assign anything to a field in either row. > I thought you originally wanted to keep l1[v] if it was > non-empty, which is what the code here says, but this time > your prose seems different. Anyhow, since you want a value > when all conditions in the chain of conditions are false, you > want a value to use when the field is empty in both records. > To change nothing, store the old empty value back; or you can > supply your own default here. > > With your particular conditions of non-emptiness, which is > taken to be truth, you can achieve variations of this result > with any of the following statements: > > w = ( l1[v] if len(l1[v]) > 0 else > l2[v] if len(l2[v]) > 0 else > l1[v] ) > > x = l1[v] if l1[v] else l2[v] if l2[v] else l1[v] > > y = l1[v] or l2[v] or l1[v] > > z = l1[v] or l2[v] > > The last one, which I originally suggested (and still prefer > when otherwise appropriate), is subtly different from the > others. That difference should be irrelevant. I agree, if the goal was to capture one of the field values in a scalar value. Maybe it would help if I give a couple rows of (made up) data to show what I mean (first row is field titles): ......Description Date State/co Kind Notes l1 2 br, Elk Plains 12-26 WA/pi house garage, w/d l2 2 br, Elk Plains 12-29 In this case, I want to copy the values from l1 to l2. Or I could have, say, if I didn't see the one on 12-26 until after 12-29: l1 2 br, Elk Plains 12-26 l2 2 br, Elk Plains 12-29 WA/pi house garage, w/d In this case, I want to copy the values from l2 to l1. While I still don't see how any of your versions would solve this problem, it remains true that your form would be excellent for assigning to a scalar variable, which is what they claim to do. From greg.ewing at canterbury.ac.nz Sun Jan 1 18:52:12 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 02 Jan 2017 12:52:12 +1300 Subject: Cleaning up conditionals In-Reply-To: References: <002101d263b6$3ae79960$27b23dae@sambora> Message-ID: > On Sat, 31 Dec 2016 14:35:46 -0800, "Deborah Swanson" > declaimed the following: > >> if len(l1[v]) == 0 and len(l2[v]) != 0: >> l1[v] = l2[v] >> elif len(l2[v]) == 0 and len(l1[v]) != 0: >> l2[v] = l1[v] >> elif l1[v] != l2[v]: >> ret += ", " + labels[v] + " diff" if len(ret) > 0 else >> labels[v] + " diff" There are a couple of things I would probably do with this: 1. Drop the len() calls and just test the lists directly. 2. Eliminate some redundant indexing operations. l1v = l1[v] l2v = l2[v] if l2v and not l1v: l1[v] = l2v elif l1v and not l2v: l2[v] = l1v elif l1v != l2v: ret += ", " + labels[v] + " diff" if len(ret) > 0 else labels[v] + " diff" Jussi Piitulainen wrote: > (l1 if bool(l1[v]) < bool(l2[v]) else > l2 if bool(l1[v]) > bool(l2[v]) else > l1)[v] = (l2 if bool(l1[v]) < bool(l2[v]) else > l1 if bool(l1[v]) > bool(l2[v]) else > l1)[v] > If you're aiming for readability, that's *not* the way to go! Not every 7-line function needs to be written on one line. :-) -- Greg From bc at freeuk.com Sun Jan 1 18:56:25 2017 From: bc at freeuk.com (BartC) Date: Sun, 1 Jan 2017 23:56:25 +0000 Subject: Cleaning up conditionals In-Reply-To: <54b5f5ab-5af7-4e9d-b056-8bdebf4193b9@googlegroups.com> References: <001101d26417$26d40420$27b23dae@sambora> <8f9a3558-add3-4e4b-9470-4c93881b185d@googlegroups.com> <54b5f5ab-5af7-4e9d-b056-8bdebf4193b9@googlegroups.com> Message-ID: On 01/01/2017 14:32, Rustom Mody wrote: > On Sunday, January 1, 2017 at 7:51:12 PM UTC+5:30, Rustom Mody wrote: >> On Sunday, January 1, 2017 at 3:39:14 PM UTC+5:30, Deborah Swanson wrote: >>> Sorry guys. I've read all your responses and I kind of get their general >>> drift, but I'm about four sheets to the wind right now, and no way would >>> I make it more than a step or two in playing around with these things >>> and trying to get my head around them before it would all descend into a >>> cacaphony of gibberish. >> >> Its true that in most programming languages one is likely to see, >> a conditional (if) statement is likely harder to grok than a conditional >> expression ie if the expression is allowed at all!! > > Er... Meant the other way round! Python's version is certainly a little harder to understand! What is it again: X if C else Y ? Here, if C is false, then only Y is evaluated, even though the first thing encountered is X! More than a little unintuitive. -- Bartc From tjreedy at udel.edu Sun Jan 1 21:25:25 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 1 Jan 2017 21:25:25 -0500 Subject: New re feature in 3.6: local flags and example use Message-ID: The re module defines several flags that affect the compilation of a pattern string. For instance, re.I == re.IGNORECASE results in case-insensitive matching. But what if you want part of a pattern to be case sensitive and part not? For instance, the IDLE colorizer needs to match keywords and builtin names with their exact case: 'if' is a keywork, 'If', 'iF', and 'IF' are not. However, case does not matter string prefixes: 'fr', 'fR', 'Fr', and 'FR' are all the same to the tokenizer. So the string prefix part of the colorize pattern was recently upgraded to r"(\br|R|u|U|f|F|fr|Fr|fR|FR|rf|rF|Rf|RF|b|B|br|Br|bR|BR|rb|rB|Rb|RB)?" 3.6 added syntax for 'local flags'. ''' (?imsx-imsx:...) (Zero or more letters from the set 'i', 'm', 's', 'x', optionally followed by '-' followed by one or more letters from the same set.) The letters set or removes the corresponding flags: re.I (ignore case), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the part of the expression. (The flags are described in Module Contents.) ''' Here is the replacement for the above, curtesy of Serhiy Storchaka. r"(?i:\br|u|f|fr|rf|b|br|rb)?" -- Terry Jan Reedy From python at deborahswanson.net Sun Jan 1 21:30:03 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 1 Jan 2017 18:30:03 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <002b01d264a0$2063c4c0$27b23dae@sambora> Dennis Lee Bieber wrote, on Sunday, January 01, 2017 6:07 PM > > On Sun, 1 Jan 2017 15:50:25 -0800, "Deborah Swanson" > declaimed the following: > > >Maybe it would help if I give a couple rows of (made up) > data to show > >what I mean (first row is field titles): > > > >......Description Date State/co Kind > >Notes > >l1 2 br, Elk Plains 12-26 WA/pi house > >garage, w/d > >l2 2 br, Elk Plains 12-29 > > > >In this case, I want to copy the values from l1 to l2. > > > >Or I could have, say, if I didn't see the one on 12-26 until after > >12-29: > >l1 2 br, Elk Plains 12-26 > >l2 2 br, Elk Plains 12-29 WA/pi house > >garage, w/d > > > >In this case, I want to copy the values from l2 to l1. > > > > And what happens if the data (where ever you get it > from to be in one > file) looks like > > l1 2 br, Elk Plains 12-26 WA/pi > l2 2 br, Elk Plains 12-29 > house garage, w/d > > As I understand your logic, you intend to make the two > entries identical EXCEPT for the DATE. What purpose does this > duplication serve? Or, put another way -- why is preserving > distinctive dates important while everything else is subject > to being blended. Yes, that's exactly what I'm doing in this bit of code, making all the listings that are identical except for the date be identical except for the date. You may or may not have tried this approach to finding the ideal house, but this is my third house hunt, using essentially the same approach, but in a different language each time. (Python is by far the best version. A dictionary with city names as keys, and several data items as values has literally cut the task in half.) The usefulness of keeping track of these duplicate listings is history of the rental being offered. In a real estate market that's on fire like we have here in the Pacific NW, any house that's relisted more than a couple times has something wrong with it or it would have been snatched up, or it's way out in the boonies. In any event, if I do decide to look at it, my bargaining position is improved by knowing that they've been trying to rent it for a long friggin time. When I find a new one that looks promising, I may reject it if I see that it's been on the market a long time (and I have another bit of code that tells me that instantly). You can design your project any way you want to! > I still get this feeling you should have master file, > regardless of whether fields are empty, and treat subsequent > data as updates to the master file -- not as some confusing > single file with some sort of duplicate contents. > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ You're certainly entitled to your opinion, but my method is time-tested for my purposes. From ian.g.kelly at gmail.com Sun Jan 1 23:45:26 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 1 Jan 2017 22:45:26 -0600 Subject: New re feature in 3.6: local flags and example use In-Reply-To: References: Message-ID: On Sun, Jan 1, 2017 at 8:25 PM, Terry Reedy wrote: \> 3.6 added syntax for 'local flags'. > ''' > (?imsx-imsx:...) > > (Zero or more letters from the set 'i', 'm', 's', 'x', optionally > followed by '-' followed by one or more letters from the same set.) The > letters set or removes the corresponding flags: re.I (ignore case), re.M > (multi-line), re.S (dot matches all), and re.X (verbose), for the part of > the expression. (The flags are described in Module Contents.) > ''' > Here is the replacement for the above, curtesy of Serhiy Storchaka. > > r"(?i:\br|u|f|fr|rf|b|br|rb)?" What is the difference between the letters before the hyphen and those after? I guess that placing the letter before means to set the flag and after means to unset it, but it's not clear from the documentation. From best_lay at yahoo.com Mon Jan 2 01:06:50 2017 From: best_lay at yahoo.com (Wildman) Date: Mon, 02 Jan 2017 00:06:50 -0600 Subject: learning and experimenting python. References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, 01 Jan 2017 10:41:22 -0800, einstein1410 wrote: > What contribution I had made especially valuable? Ask your mommy what sarcasm means. -- GNU/Linux user #557453 The cow died so I don't need your bull! From ian.g.kelly at gmail.com Mon Jan 2 01:15:58 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 2 Jan 2017 00:15:58 -0600 Subject: learning and experimenting python. In-Reply-To: References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Jan 1, 2017 at 11:30 AM, Grant Edwards wrote: > On 2016-12-31, Ian Kelly wrote: >> On Dec 31, 2016 3:12 AM, wrote: >> >> That's true. >> >> Please include quoted context in your replies. I have no idea who or what >> you're responding to. > > I'd just like to thank everybody for replying to einstein1410's posts > so that those of us who have plonked posts from googlegroups don't > miss out on his/her valuable contributions. If you want your own decision to cease interacting with him to be forced on everybody here then I suggest you petition the group admins to ban him rather than merely plonk him yourself. Since they're unlikely to do that however, I suppose you'll just have to find a way to cope, much as others of us have done when occasionally exposed to quoted material by perennial trolls such as Ranting Rick. For a starter, since this is the only thread he's posted in, you could mute it. From ian.g.kelly at gmail.com Mon Jan 2 01:40:33 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 2 Jan 2017 00:40:33 -0600 Subject: learning and experimenting python. In-Reply-To: References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jan 2, 2017 at 12:15 AM, Ian Kelly wrote: > Since they're unlikely to do that however, Then again, I see that einstein1410 made a couple of rather aggressive posts 11 hours ago that haven't made it to my email, so maybe he did manage to get himself banned. From mail at timgolden.me.uk Mon Jan 2 02:14:19 2017 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 2 Jan 2017 07:14:19 +0000 Subject: learning and experimenting python. In-Reply-To: References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: <21b1c09e-0556-693b-b743-85e927426a59@timgolden.me.uk> On 02/01/17 06:40, Ian Kelly wrote: > On Mon, Jan 2, 2017 at 12:15 AM, Ian Kelly wrote: >> Since they're unlikely to do that however, > > Then again, I see that einstein1410 made a couple of rather aggressive > posts 11 hours ago that haven't made it to my email, so maybe he did > manage to get himself banned. > We are holding (and, so far, dropping) posts from that address. FWIW I concur with an earlier poster that the person in question is simply ill-informed rather than deliberately provocative. I have sympathy with their initial ignorance; less with their apparent inability to learn from other people's well-intentioned comments. Probably best to let the thing drop rather than let it escalate into a sniping war. If anyone reading this knows the poster in person, please help them to see how their posts have come across and whey they received the reaction they did. TJG From whoisaritra at gmail.com Mon Jan 2 04:04:59 2017 From: whoisaritra at gmail.com (Aritra Bhattacharjee) Date: Mon, 2 Jan 2017 01:04:59 -0800 (PST) Subject: Unable to Debug Message-ID: I am new to python programming. I wrote a code to search for the product names on a page of snapdeal.com . Code: import urllib.request from bs4 import BeautifulSoup as BS url = 'https://www.snapdeal.com/products/electronics-headphones?sort=plrty' response = urllib.request.urlopen(url).read() soup = BS(response, 'html.parser') #for dataprice in soup.find_all('span', class_="lfloat product-price"): # print(dataprice) product_name={} i=0 for title in soup.find_all('p', class_="product-title"): product_name[i]=title.string i += 1 for i in range(1,21): print(product_name[i]) Output: Traceback (most recent call last): Motorola Pulse 2 Over Ear Wired Headphones With Mic (Black) Bose SoundLink On-Ear Bluetooth Headphones - Black & Blue Sony MDR-ZX110A Headphones Without Mic (White) Philips SBCHL140/98 Over Ear Headphone Without Mic File "C:/Users/Aritra Bhattacharjee/PycharmProjects/PythonWebModules/Web Scraper.py", line 17, in Intex Desire BT Over Ear Wired With Mic Headphone Black print(product_name[i]) JBL T450 On Ear Wired Headphones With Mic Black KeyError: 20 Motorola Pulse Max Over Ear Wired Headphones With Mic (Black) Philips SHB7250WT/00 Over Ear Wireless Headphones With Mic White Sony MDR-XB650BT On-Ear Extra Bass(XB) Headphones with Bluetooth & NFC (Black) Intex JAZZ Over Ear Wired With Mic Headphone Black Skullcandy S5GBW-J539 On Ear Wireless Headphones With Mic Black JBL C300SI Over Ear Wired Without Mic Headphone Black Zoook Rocker iFit Bluetooth Wireless Headphones With Mic Black Signature VM-46 Over Ear Wired Headphone Without Mic White Sony MDR-G45 Over Ear Wired Without Mic Headphone- Black Motorola Pulse Max Over Ear Wired Headphones With Mic (White) Bose SoundTrue Around-Ear Headphones with Mic (Navy Blue) for Samsung and Android Devices JBL T450 On Ear Wired Headphones With Mic Blue Motorola Pulse 2 Over Ear Wired Headphones With Mic (White) The Output shows some error that I could not understand why...Thanks in Advance From vincent.vande.vyvre at telenet.be Mon Jan 2 04:32:34 2017 From: vincent.vande.vyvre at telenet.be (Vincent Vande Vyvre) Date: Mon, 2 Jan 2017 10:32:34 +0100 Subject: Unable to Debug In-Reply-To: References: Message-ID: Le 02/01/17 ? 10:04, Aritra Bhattacharjee a ?crit : > I am new to python programming. I wrote a code to search for the product names on a page of snapdeal.com . > > Code: > import urllib.request > from bs4 import BeautifulSoup as BS > > url = 'https://www.snapdeal.com/products/electronics-headphones?sort=plrty' > > response = urllib.request.urlopen(url).read() > soup = BS(response, 'html.parser') > > #for dataprice in soup.find_all('span', class_="lfloat product-price"): > # print(dataprice) > product_name={} > i=0 > for title in soup.find_all('p', class_="product-title"): > product_name[i]=title.string > i += 1 > for i in range(1,21): > print(product_name[i]) > > > Output: > Traceback (most recent call last): > Motorola Pulse 2 Over Ear Wired Headphones With Mic (Black) > Bose SoundLink On-Ear Bluetooth Headphones - Black & Blue > Sony MDR-ZX110A Headphones Without Mic (White) > Philips SBCHL140/98 Over Ear Headphone Without Mic > File "C:/Users/Aritra Bhattacharjee/PycharmProjects/PythonWebModules/Web Scraper.py", line 17, in > Intex Desire BT Over Ear Wired With Mic Headphone Black > print(product_name[i]) > JBL T450 On Ear Wired Headphones With Mic Black > KeyError: 20 > Motorola Pulse Max Over Ear Wired Headphones With Mic (Black) > Philips SHB7250WT/00 Over Ear Wireless Headphones With Mic White > Sony MDR-XB650BT On-Ear Extra Bass(XB) Headphones with Bluetooth & NFC (Black) > Intex JAZZ Over Ear Wired With Mic Headphone Black > Skullcandy S5GBW-J539 On Ear Wireless Headphones With Mic Black > JBL C300SI Over Ear Wired Without Mic Headphone Black > Zoook Rocker iFit Bluetooth Wireless Headphones With Mic Black > Signature VM-46 Over Ear Wired Headphone Without Mic White > Sony MDR-G45 Over Ear Wired Without Mic Headphone- Black > Motorola Pulse Max Over Ear Wired Headphones With Mic (White) > Bose SoundTrue Around-Ear Headphones with Mic (Navy Blue) for Samsung and Android Devices > JBL T450 On Ear Wired Headphones With Mic Blue > Motorola Pulse 2 Over Ear Wired Headphones With Mic (White) > > > The Output shows some error that I could not understand why...Thanks in Advance Try with: for i in product_name: print(i) Vincent From __peter__ at web.de Mon Jan 2 04:37:45 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 02 Jan 2017 10:37:45 +0100 Subject: Unable to Debug References: Message-ID: Aritra Bhattacharjee wrote: > I am new to python programming. I wrote a code to search for the product > names on a page of snapdeal.com . > > Code: > import urllib.request > from bs4 import BeautifulSoup as BS > > url = > 'https://www.snapdeal.com/products/electronics-headphones?sort=plrty' > > response = urllib.request.urlopen(url).read() > soup = BS(response, 'html.parser') > > #for dataprice in soup.find_all('span', class_="lfloat product-price"): > # print(dataprice) > product_name={} > i=0 > for title in soup.find_all('p', class_="product-title"): > product_name[i]=title.string > i += 1 > for i in range(1,21): > print(product_name[i]) > > > Output: > Traceback (most recent call last): > Motorola Pulse 2 Over Ear Wired Headphones With Mic (Black) > Bose SoundLink On-Ear Bluetooth Headphones - Black & Blue > Sony MDR-ZX110A Headphones Without Mic (White) > Philips SBCHL140/98 Over Ear Headphone Without Mic > File "C:/Users/Aritra Bhattacharjee/PycharmProjects/PythonWebModules/Web > Scraper.py", line 17, in > Intex Desire BT Over Ear Wired With Mic Headphone Black > print(product_name[i]) > JBL T450 On Ear Wired Headphones With Mic Black > KeyError: 20 For the script above the output should end with the line above [snip] > The Output shows some error that I could not understand why...Thanks in > Advance You define a dict product_name and then successively add entries with > i=0 > for title in soup.find_all('p', class_="product-title"): > product_name[i]=title.string > i += 1 This means that the first key will be 0, the second will be 1, and so on up to 19. In the for loop you are looking for the keys 1 to 20, > for i in range(1,21): > print(product_name[i]) so the first match will not be printed, and when you ask for product_name[20] you will get the KeyError and the traceback. How to fix your code? You could rewrite the for loop as for i in range(len(product_name)): print(product_name[i]) which because of len() will work for pages that feature more or less than 20 products. If you plan to remove products from the dict you have to cope with missing indices. On way to do this is iterate to over the values alone: for name in product_name.values(): print(name) If you want to preserve the order on the html page you can iterate over the sorted key/value pairs: for i, name in sorted(product_name.items()): print(i, name) From rus.cahimb at gmail.com Mon Jan 2 04:43:42 2017 From: rus.cahimb at gmail.com (rus.cahimb at gmail.com) Date: Mon, 2 Jan 2017 01:43:42 -0800 (PST) Subject: Unable to Debug In-Reply-To: References: Message-ID: <1f64b59e-9d6b-487b-b367-850ccfb6b9ff@googlegroups.com> On Monday, January 2, 2017 at 2:35:22 PM UTC+5:30, Aritra Bhattacharjee wrote: > I am new to python programming. I wrote a code to search for the product names on a page of snapdeal.com . [RM]: Welcome and have fun. > for i in range(1,21): > print(product_name[i]) > > > Output: > Traceback (most recent call last): > Motorola Pulse 2 Over Ear Wired Headphones With Mic (Black) > Bose SoundLink On-Ear Bluetooth Headphones - Black & Blue > Sony MDR-ZX110A Headphones Without Mic (White) > Philips SBCHL140/98 Over Ear Headphone Without Mic > File "C:/Users/Aritra Bhattacharjee/PycharmProjects/PythonWebModules/Web Scraper.py", line 17, in > Intex Desire BT Over Ear Wired With Mic Headphone Black > print(product_name[i]) > JBL T450 On Ear Wired Headphones With Mic Black > KeyError: 20 . . . . . . . . > The Output shows some error that I could not understand why...Thanks in Advance [RM]: You should read tracebacks stack from the bottom. Initially there will be hiccups but over a period of time you will learn. >From your code snippet: for loop is supplied with a range of 1 to 21 and while the key value (variable, i) is 20, print fails there. In the previous loop, where you are using methods in BeautifulSoup, are you sure that many items are added to the list, product_name. If I were you, in the loop where the list is expanded by adding the product title names, will rename the variable, i, as 'product_counter'. And, in the last for loop, this counter can be used in the rangem like, (0, product_counter). All the best, /Ram From david.froger.ml at mailoo.org Mon Jan 2 04:43:44 2017 From: david.froger.ml at mailoo.org (David Froger) Date: Mon, 02 Jan 2017 10:43:44 +0100 Subject: Unable to Debug In-Reply-To: References: Message-ID: <148335022401.30175.5274499245150761836@mael> Hello Aritra, Your standard output and standard error are mixed (I don't know why), so error message is not immediate to read. It is: > Traceback (most recent call last): > File "C:/Users/Aritra Bhattacharjee/PycharmProjects/PythonWebModules/Web Scraper.py", line 17, in > print(product_name[i]) > KeyError: 20 There no such `20` title in `product_name`, so we can deduce that `soup.find_all` found only 19 titles. You can to instead: product_name = [] # a list instead of a dict is enought for title in soup.find_all('p', class_="product-title"): product_name.append(title.string) # or this should work too: # product_name = list(soup.find_all('p', class_="product-title")) for title in product_name: # no need of i variable print(title) Quoting Aritra Bhattacharjee (2017-01-02 10:04:59) > I am new to python programming. I wrote a code to search for the product names on a page of snapdeal.com . > > Code: > import urllib.request > from bs4 import BeautifulSoup as BS > > url = 'https://www.snapdeal.com/products/electronics-headphones?sort=plrty' > > response = urllib.request.urlopen(url).read() > soup = BS(response, 'html.parser') > > #for dataprice in soup.find_all('span', class_="lfloat product-price"): > # print(dataprice) > product_name={} > i=0 > for title in soup.find_all('p', class_="product-title"): > product_name[i]=title.string > i += 1 > for i in range(1,21): > print(product_name[i]) > > > Output: > Motorola Pulse 2 Over Ear Wired Headphones With Mic (Black) > Bose SoundLink On-Ear Bluetooth Headphones - Black & Blue > Sony MDR-ZX110A Headphones Without Mic (White) > Philips SBCHL140/98 Over Ear Headphone Without Mic > Intex Desire BT Over Ear Wired With Mic Headphone Black > JBL T450 On Ear Wired Headphones With Mic Black > Motorola Pulse Max Over Ear Wired Headphones With Mic (Black) > Philips SHB7250WT/00 Over Ear Wireless Headphones With Mic White > Sony MDR-XB650BT On-Ear Extra Bass(XB) Headphones with Bluetooth & NFC (Black) > Intex JAZZ Over Ear Wired With Mic Headphone Black > Skullcandy S5GBW-J539 On Ear Wireless Headphones With Mic Black > JBL C300SI Over Ear Wired Without Mic Headphone Black > Zoook Rocker iFit Bluetooth Wireless Headphones With Mic Black > Signature VM-46 Over Ear Wired Headphone Without Mic White > Sony MDR-G45 Over Ear Wired Without Mic Headphone- Black > Motorola Pulse Max Over Ear Wired Headphones With Mic (White) > Bose SoundTrue Around-Ear Headphones with Mic (Navy Blue) for Samsung and Android Devices > JBL T450 On Ear Wired Headphones With Mic Blue > Motorola Pulse 2 Over Ear Wired Headphones With Mic (White) > > > The Output shows some error that I could not understand why...Thanks in Advance > -- > https://mail.python.org/mailman/listinfo/python-list From jussi.piitulainen at helsinki.fi Mon Jan 2 04:44:09 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Mon, 02 Jan 2017 11:44:09 +0200 Subject: Cleaning up conditionals References: <002801d26489$d3073dd0$27b23dae@sambora> Message-ID: Deborah Swanson writes: > Jussi Piitulainen wrote: [snip] >> With your particular conditions of non-emptiness, which is taken to >> be truth, you can achieve variations of this result with any of the >> following statements: >> >> w = ( l1[v] if len(l1[v]) > 0 else >> l2[v] if len(l2[v]) > 0 else >> l1[v] ) >> >> x = l1[v] if l1[v] else l2[v] if l2[v] else l1[v] >> >> y = l1[v] or l2[v] or l1[v] >> >> z = l1[v] or l2[v] >> >> The last one, which I originally suggested (and still prefer >> when otherwise appropriate), is subtly different from the >> others. That difference should be irrelevant. > > I agree, if the goal was to capture one of the field values in a > scalar value. To store into a list, specify a position in the list as the target. My idea here has been to simply do this to all the relevant positions in both lists, even when it means storing the old value back. See below, concretely, with your two examples and the mixed one from Dennis Lee Bieber, where I introduced a small difference of my own so that corresponding non-empty fields differ. I have made it output Python comments and inserted them at appropriate places. The same function, merge, fills the empty fields from the other list in all three cases using the method z from above. It does no harm when a field is already non-empty. def merge(l1, l2): fields = range(5) for v in fields: l1[v] = l1[v] or l2[v] l2[v] = l2[v] or l1[v] l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d' ] l2 = [ '2 br, Elk Plains', '12-29', '', '', '' ] print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n') # Before: # ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d'] # ['2 br, Elk Plains', '12-29', '', '', ''] # After: # ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d'] # ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d'] l1 = [ '2 br, Elk Plains', '12-26', '', '', '' ] l2 = [ '2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d' ] print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n') # Before: # ['2 br, Elk Plains', '12-26', '', '', ''] # ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d'] # After: # ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d'] # ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d'] l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', '', '' ] l2 = [ '2 br, Elf Plains', '12-29', '', 'house', 'garage, w/d' ] print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n') # Before: # ['2 br, Elk Plains', '12-26', 'WA/pi', '', ''] # ['2 br, Elf Plains', '12-29', '', 'house', 'garage, w/d'] # After: # ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d'] # ['2 br, Elf Plains', '12-29', 'WA/pi', 'house', 'garage, w/d'] From python at deborahswanson.net Mon Jan 2 04:47:22 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 2 Jan 2017 01:47:22 -0800 Subject: Unable to Debug In-Reply-To: Message-ID: <000f01d264dd$37f0e590$27b23dae@sambora> Aritra Bhattacharjee wrote, on January 02, 2017 1:05 AM: > I am new to python programming. I wrote a code to search for > the product names on a page of snapdeal.com . > > Code: > import urllib.request > from bs4 import BeautifulSoup as BS > > url = > 'https://www.snapdeal.com/products/electronics> -headphones?sort=plrty' > > response = urllib.request.urlopen(url).read() > soup = BS(response, 'html.parser') > > #for dataprice in soup.find_all('span', class_="lfloat > product-price"): > # print(dataprice) > product_name={} > i=0 > for title in soup.find_all('p', class_="product-title"): > product_name[i]=title.string > i += 1 > for i in range(1,21): > print(product_name[i]) > > > Output: > Traceback (most recent call last): > Motorola Pulse 2 Over Ear Wired Headphones With Mic (Black) > Bose SoundLink On-Ear Bluetooth Headphones - Black & Blue > Sony MDR-ZX110A Headphones Without Mic (White) Philips > SBCHL140/98 Over Ear Headphone Without Mic > File "C:/Users/Aritra > Bhattacharjee/PycharmProjects/PythonWebModules/Web > Scraper.py", line 17, in Intex Desire BT Over Ear > Wired With Mic Headphone Black > print(product_name[i]) > JBL T450 On Ear Wired Headphones With Mic Black > KeyError: 20 > Motorola Pulse Max Over Ear Wired Headphones With Mic (Black) > Philips SHB7250WT/00 Over Ear Wireless Headphones With Mic > White Sony MDR-XB650BT On-Ear Extra Bass(XB) Headphones with > Bluetooth & NFC (Black) Intex JAZZ Over Ear Wired With Mic > Headphone Black Skullcandy S5GBW-J539 On Ear Wireless > Headphones With Mic Black JBL C300SI Over Ear Wired Without > Mic Headphone Black Zoook Rocker iFit Bluetooth Wireless > Headphones With Mic Black Signature VM-46 Over Ear Wired > Headphone Without Mic White Sony MDR-G45 Over Ear Wired > Without Mic Headphone- Black Motorola Pulse Max Over Ear > Wired Headphones With Mic (White) Bose SoundTrue Around-Ear > Headphones with Mic (Navy Blue) for Samsung and Android > Devices JBL T450 On Ear Wired Headphones With Mic Blue > Motorola Pulse 2 Over Ear Wired Headphones With Mic (White) > > > The Output shows some error that I could not understand > why...Thanks in Advance This looks like Beautiful Soup. I've only used it once, and can just barely count it as a successful use. But what stands out to me is the "KeyError: 20", which closely follows after the "print(product_name[i])" statement. If product_name is a dictionary (and I see that it is), the key error says that none of product_name's keys has the value that was in 'i' when the error was thrown, ie. the key equal to 20 isn't in the dictionary. Frequently this occurs in a loop that continues beyond the length of the dictionary. The error was thrown in: for i in range(1,21): print(product_name[i]) so my guess is that you didn't have 20 keys in product_name, and the error was thrown when you tried to print product_name[20]. You could try just editting the loop so it only goes up to 20, since all the keys up to 20 were found (and range will stop the loop at one less than the given upper bound): for i in range(1,20): print(product_name[i]) although I wonder why it isn't: for i in range(0,19): print(product_name[i]) since the first item in the dictionary is index 0. Unless you don't want the first one to print for some reason. Hope this helps. D. From python at deborahswanson.net Mon Jan 2 05:37:19 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 2 Jan 2017 02:37:19 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <001501d264e4$32189a80$27b23dae@sambora> Jussi Piitulainen wrote, on January 02, 2017 1:44 AM > > Deborah Swanson writes: > > Jussi Piitulainen wrote: > > [snip] > > >> With your particular conditions of non-emptiness, which is > taken to > >> be truth, you can achieve variations of this result with > any of the > >> following statements: > >> > >> w = ( l1[v] if len(l1[v]) > 0 else > >> l2[v] if len(l2[v]) > 0 else > >> l1[v] ) > >> > >> x = l1[v] if l1[v] else l2[v] if l2[v] else l1[v] > >> > >> y = l1[v] or l2[v] or l1[v] > >> > >> z = l1[v] or l2[v] > >> > >> The last one, which I originally suggested (and still prefer > >> when otherwise appropriate), is subtly different from the > >> others. That difference should be irrelevant. > > > > I agree, if the goal was to capture one of the field values in a > > scalar value. > > To store into a list, specify a position in the list as the target. Aha. Now I see your plan. Pardon me, but the single letter variable you were assigning to, with no prior definition, seemed to me to be a scalar. I just couldn't see how that could work. But a list, specifically a list of the 2 rows with identical fields except for the date, makes perfect sense. > My idea here has been to simply do this to all the relevant > positions in both lists, even when it means storing the old > value back. Not a problem, since it's the same value. > See below, concretely, with your two examples and the mixed > one from Dennis Lee Bieber, where I introduced a small > difference of my own so that corresponding non-empty fields > differ. I have made it output Python comments and inserted > them at appropriate places. > > The same function, merge, fills the empty fields from the > other list in all three cases using the method z from above. > It does no harm when a field is already non-empty. > > def merge(l1, l2): > fields = range(5) > for v in fields: > l1[v] = l1[v] or l2[v] > l2[v] = l2[v] or l1[v] > > l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d' ] > l2 = [ '2 br, Elk Plains', '12-29', '', '', '' ] > > print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') > merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n') > > # Before: > # ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, > w/d'] # ['2 br, Elk Plains', '12-29', '', '', ''] # After: # > ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, > w/d'] # ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d'] > > l1 = [ '2 br, Elk Plains', '12-26', '', '', '' ] > l2 = [ '2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d' ] > > print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') > merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n') > > # Before: > # ['2 br, Elk Plains', '12-26', '', '', ''] > # ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, > w/d'] # After: # ['2 br, Elk Plains', '12-26', 'WA/pi', > 'house', 'garage, w/d'] # ['2 br, Elk Plains', '12-29', > 'WA/pi', 'house', 'garage, w/d'] > > l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', '', '' ] > l2 = [ '2 br, Elf Plains', '12-29', '', 'house', 'garage, w/d' ] > > print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') > merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n') > > # Before: > # ['2 br, Elk Plains', '12-26', 'WA/pi', '', ''] > # ['2 br, Elf Plains', '12-29', '', 'house', 'garage, w/d'] > # After: > # ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, > w/d'] # ['2 br, Elf Plains', '12-29', 'WA/pi', 'house', 'garage, w/d'] Very clever, and exactly what I need to accomplish. Well, the first part of it anyway. My second test is if neither of two corresponding fields is empty and they're different, to dump which fields are different into a memo field, but perhaps that could be the first test and then use your merge for the ones that are identical except for the missing data. An example of what I mean is: l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', ''] l2 = [ '2 br, Elf Plains', '12-29', 'OR/co', 'house', 'garage, w/d' ] which could happen if I didn't know that both Washington and Oregon have an Elk Plains. It would mean 2 functions, but if I can work in the logistics of dealing with all the duplicates (except for the date) in one throw, 2 functions would be worth it. And not a big deal even if I just stick to 2 rows at a time. In fact, just looking at the example I made up above, looks like it would be better to test for fields that are different first, and pass on merging the rows if any differences are found. They can always be merged in a later run after I reconcile the descrepancies. I'll mess around with it tomorrow, but I'll bet this works, and works better than what I have now. From tonycamgar at gmail.com Mon Jan 2 06:38:53 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Mon, 2 Jan 2017 03:38:53 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. Message-ID: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Hello, I am having a hard time deciding what IDE or IDE-like code editor should I use. This can be overwhelming. So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, IntelliJ with Python plugin. The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. My screen should be mostly ?made of code? as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool and python oriented. The problem with Vim is the learning curve, so I know the very basic stuff, but obviously not enough for coding and I do not have time to learn it, it is a pity because there are awesome plugins that turns Vim into a lightweight powerful IDE-like. So now it is not an option but I will reconsider it in the future, learning little by little. Also, I am not very fan GUI guy if the task can be accomplished through the terminal. However, I don?t understand why people underrate GUIs, that said I normally use shortcuts for the most frequent tasks and when I have to do something that is not that frequent then I do it with the mouse, for the latter case in vim you would need to look for that specific command every time. Sublime is my current and preferred code editor. I installed Anaconda, Git integration and a couple of additional plugins that make sublime very powerful. Also, what I like about sublime compared to the full featured IDEs, besides the minimalism, is how you can perform code navigation back and forth so fast, I mean this is something that you can also do with the others but for some subjective reason I specifically love how sublime does it. The code completion in sublime I do not find it very intelligence, the SublimeCodeIntel is better than the one that Anaconda uses but the completions are not as verbose as in the IDEs. Now, I am thinking about giving a try to Visual Studio Code Edition (take a look, it sounds good https://marketplace.visualstudio.com/items?itemName=donjayamanne.python). I need an editor for professional software development. What would you recommend to me? From antoon.pardon at rece.vub.ac.be Mon Jan 2 09:09:21 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 2 Jan 2017 15:09:21 +0100 Subject: Cleaning up conditionals In-Reply-To: <003501d262fc$90a70030$27b23dae@sambora> References: <003501d262fc$90a70030$27b23dae@sambora> Message-ID: Op 31-12-16 om 01:26 schreef Deborah Swanson: >> On 31 December 2016 at 10:00, Deborah Swanson >> wrote: >>> Oops, indentation was messed up when I copied it into the email. >> The indentation of your latest message looks completely >> broken now, you can see it here: >> https://mail.python.org/pipermail/python-list/2016-December/71 > 7758.html > > Hm, I don't know what pipermail does with whitespace but the formatting > in the message at > https://mail.python.org/pipermail/python-list/2016-December/717758.html > is not the same as the message I sent from Outlook. > > Again, it should have been: > > if len(l1[st]) == 0: > if len(l2[st]) > 0: > l1[st] = l2[st] > elif len(l2[st]) == 0: > if len(l1[st]) > 0: > l2[st] = l1[st] I just would like to point out that you could just eliminate each sub-if and just write this as: if len(l1[st]) == 0: l1[st] = l2[st] elif len(l2[st]) == 0: l2[st] = l1[st] If we look at the first branch the eliminated test would prevent the assignment in case l2[st] is empty. But we already know l1[st] is empty. So we are just eliminating the replacement of an empty field with an empty field. There is no harm in that and trying to eliminate it just makes your test unnecessarily complex. -- Antoon Pardon From dr.roman.graf at gmail.com Mon Jan 2 09:14:06 2017 From: dr.roman.graf at gmail.com (dr.roman.graf at gmail.com) Date: Mon, 2 Jan 2017 06:14:06 -0800 (PST) Subject: Django broken pipe error In-Reply-To: References: <1030ce59-d2cb-4d90-b826-30637004f591@googlegroups.com> <843bd5a8-f127-4fdc-8817-0b1645c1872d@googlegroups.com> Message-ID: <9d08e536-0a08-4f2b-b7e7-abea4b415c1d@googlegroups.com> On Monday, December 12, 2016 at 6:38:39 PM UTC+1, justin walters wrote: > On Mon, Dec 12, 2016 at 7:27 AM, roma wrote: > > > Thanks Justin, > > > > I believe, the whole database story has no influence on the broken pipe > > error. I've commented out the whole block and leave only return line: > > return HttpResponse(res, content_type="text/plain; charset=utf-8") > > The error is still present. And I have no influence on that. > > > > I call python from js client: > > > > var newTrendReport = new App.TrendReport(); > > newTrendReport.set('search_phrase', search_phrase); > > newTrendReport.set('time_from', time_from); > > newTrendReport.set('time_to', time_to); > > newTrendReport.set('time_scale', time_scale); > > newTrendReport.set('category', category); > > newTrendReport.startExport( > > > > function(response){ > > console.log("Successfully calculated trend report."); > > App.trendPage = new App.TrendPageView(); > > App.trendPage.render(response); > > }, > > ); > > > > go throw: > > > > App.TrendReport = Backbone.Model.extend({ > > urlRoot: "/api/trend_reports/", > > defaults: { > > search_phrase: "", > > time_from: "", > > time_to: "", > > time_scale: "", > > category: "" > > }, > > > > startExportSuffix: "/export_report/", > > > > startExport: function( successCallback, errorCallback ) { > > console.log("start trend calculation"); > > var that = this; > > var ajaxUrl = this.startExportSuffix; > > var options = { > > method: "POST", > > data: this.attributes, > > contentType: "application/json;charset=UTF-8", > > dataType: "json", > > > > error: errorCallback, > > success: successCallback > > }; > > console.log("start trend export sync"); > > App.ajax(ajaxUrl, options); > > } > > > > }); > > > > and come in export_report method. > > > > My urls.py: > > > > url(r'^export_report', ensure_csrf_cookie(views.export_report), > > name="export_report"), > > -- > > https://mail.python.org/mailman/listinfo/python-list > > > > I'm not super familiar with the way backbone does http requests, but > something seems off about the startExport function. > It seems to me that you are sending a post request to "/export_report/" > which is an endpoint that I'm guessing is nested > in an include from "/api/trend_reports/". However, it looks like the error > you're getting above says you aren't sending > the request to "https://root.com/api/trend_reports/export_report/". > Instead, you are sending the request to "https://root.com/export_report/" . > Though, I'm also not sure that's the case because that would normally throw > a 404. > > I also noticed that you set content type to 'application/json' in your js, > but the view function returns a 'text/plain' content type. Is > There a reason for this? > > The data key in your js http function is set to the attributes variable > which, as far as I can tell, does not exist. > > There's a lot going on here, but I think you can probably narrow it down to > something in your backbone code or the way > backbone handles http requests as the error you are getting is caused by > the client prematurely closing the socket. > It's possible backbone will stop reading the response since it isn't the > same content-type as the request. Thanks a lot Justin, The problem was solved when I employed standard Framework methods for creation of new database object: in JS: var trendModel = new App.TrendModel(); trendModel.set("phrase", search_phrase); trendModel.set("from_time", time_from); trendModel.set(... trendModel.save(... in PY: def create(self, request): ... I've also created extra template and additional View. PageView for some unclear reason didn't support creation of new object - this event just disappeared. Regards, Roman From marcwbrooks at gmail.com Mon Jan 2 10:47:30 2017 From: marcwbrooks at gmail.com (Marc Brooks) Date: Mon, 2 Jan 2017 10:47:30 -0500 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: I'd recommend you be willing to put in the time and effort to learn the tools you want to use, if you want to do professional software development. Pick one, use it for a month (at least 100+ hours of hands on keyboard coding). Sublime, Vi are great for Python, since Python doesn't require as much as some other languages, but you sill need to put in the time to learn the tool. On Mon, Jan 2, 2017 at 6:38 AM, Antonio Caminero Garcia < tonycamgar at gmail.com> wrote: > Hello, I am having a hard time deciding what IDE or IDE-like code editor > should I use. This can be overwhelming. > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, > IntelliJ with Python plugin. > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, > Pycharm) is that they look like a space craft dashboard and that > unwarranted resources consumption and the unnecessary icons. I want my IDE > to be minimalistic but powerful. My screen should be mostly ?made of code? > as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool > and python oriented. > > The problem with Vim is the learning curve, so I know the very basic > stuff, but obviously not enough for coding and I do not have time to learn > it, it is a pity because there are awesome plugins that turns Vim into a > lightweight powerful IDE-like. So now it is not an option but I will > reconsider it in the future, learning little by little. Also, I am not very > fan GUI guy if the task can be accomplished through the terminal. However, > I don?t understand why people underrate GUIs, that said I normally use > shortcuts for the most frequent tasks and when I have to do something that > is not that frequent then I do it with the mouse, for the latter case in > vim you would need to look for that specific command every time. > > Sublime is my current and preferred code editor. I installed Anaconda, Git > integration and a couple of additional plugins that make sublime very > powerful. Also, what I like about sublime compared to the full featured > IDEs, besides the minimalism, is how you can perform code navigation back > and forth so fast, I mean this is something that you can also do with the > others but for some subjective reason I specifically love how sublime does > it. The code completion in sublime I do not find it very intelligence, the > SublimeCodeIntel is better than the one that Anaconda uses but the > completions are not as verbose as in the IDEs. > > Now, I am thinking about giving a try to Visual Studio Code Edition (take > a look, it sounds good https://marketplace.visualstudio.com/items? > itemName=donjayamanne.python). I need an editor for professional software > development. What would you recommend to me? > -- > https://mail.python.org/mailman/listinfo/python-list > From torriem at gmail.com Mon Jan 2 11:24:06 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 2 Jan 2017 09:24:06 -0700 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <53f1736a-2e99-619b-828b-5ec70b9c09d2@gmail.com> On 01/02/2017 04:38 AM, Antonio Caminero Garcia wrote: > The problem with Vim is the learning curve, so I know the very basic > stuff, but obviously not enough for coding and I do not have time to > learn it, it is a pity because there are awesome plugins that turns > Vim into a lightweight powerful IDE-like. So now it is not an option > but I will reconsider it in the future, learning little by little. > Also, I am not very fan GUI guy if the task can be accomplished > through the terminal. However, I don?t understand why people > underrate GUIs, that said I normally use shortcuts for the most > frequent tasks and when I have to do something that is not that > frequent then I do it with the mouse, for the latter case in vim you > would need to look for that specific command every time. Really, the basic stuff is enough to be very productive in vim. In fact just knowing how to save and quit is half the battle! A little cheat sheet for vim by your keyboard would be plenty I think. If all you knew was how to change modes, insert, append, change word, yank, delete, and paste, that is 99% of what you'd use every day. You can use normal arrow keys, home, end, and page up and page down for cursor movement in vim, so even if you can't remember ^,$, gg, or GG, you'll do fine. Eventually you can begin to add in other things, like modifiers to c (change). There probably are a lot of nice plugins for ViM, but I use none of them. I just don't find them that useful. I don't seem to need any IDE help with Python. From walters.justin01 at gmail.com Mon Jan 2 12:22:18 2017 From: walters.justin01 at gmail.com (justin walters) Date: Mon, 2 Jan 2017 09:22:18 -0800 Subject: Django broken pipe error In-Reply-To: <9d08e536-0a08-4f2b-b7e7-abea4b415c1d@googlegroups.com> References: <1030ce59-d2cb-4d90-b826-30637004f591@googlegroups.com> <843bd5a8-f127-4fdc-8817-0b1645c1872d@googlegroups.com> <9d08e536-0a08-4f2b-b7e7-abea4b415c1d@googlegroups.com> Message-ID: On Mon, Jan 2, 2017 at 6:14 AM, wrote: > > Thanks a lot Justin, > > The problem was solved when I employed standard Framework methods for > creation of new database object: > > in JS: > var trendModel = new App.TrendModel(); > trendModel.set("phrase", search_phrase); > trendModel.set("from_time", time_from); > trendModel.set(... > trendModel.save(... > > in PY: > def create(self, request): > ... > > I've also created extra template and additional View. PageView for some > unclear reason didn't support creation of new object - this event just > disappeared. > > Regards, > > Roman > -- > https://mail.python.org/mailman/listinfo/python-list > Glad to hear that you solved the problem! From walters.justin01 at gmail.com Mon Jan 2 12:34:36 2017 From: walters.justin01 at gmail.com (justin walters) Date: Mon, 2 Jan 2017 09:34:36 -0800 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: On Mon, Jan 2, 2017 at 3:38 AM, Antonio Caminero Garcia < tonycamgar at gmail.com> wrote: > Hello, I am having a hard time deciding what IDE or IDE-like code editor > should I use. This can be overwhelming. > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, > IntelliJ with Python plugin. > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, > Pycharm) is that they look like a space craft dashboard and that > unwarranted resources consumption and the unnecessary icons. I want my IDE > to be minimalistic but powerful. My screen should be mostly ?made of code? > as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool > and python oriented. > > The problem with Vim is the learning curve, so I know the very basic > stuff, but obviously not enough for coding and I do not have time to learn > it, it is a pity because there are awesome plugins that turns Vim into a > lightweight powerful IDE-like. So now it is not an option but I will > reconsider it in the future, learning little by little. Also, I am not very > fan GUI guy if the task can be accomplished through the terminal. However, > I don?t understand why people underrate GUIs, that said I normally use > shortcuts for the most frequent tasks and when I have to do something that > is not that frequent then I do it with the mouse, for the latter case in > vim you would need to look for that specific command every time. > > Sublime is my current and preferred code editor. I installed Anaconda, Git > integration and a couple of additional plugins that make sublime very > powerful. Also, what I like about sublime compared to the full featured > IDEs, besides the minimalism, is how you can perform code navigation back > and forth so fast, I mean this is something that you can also do with the > others but for some subjective reason I specifically love how sublime does > it. The code completion in sublime I do not find it very intelligence, the > SublimeCodeIntel is better than the one that Anaconda uses but the > completions are not as verbose as in the IDEs. > > Now, I am thinking about giving a try to Visual Studio Code Edition (take > a look, it sounds good https://marketplace.visualstudio.com/items? > itemName=donjayamanne.python). I need an editor for professional software > development. What would you recommend to me? > -- > https://mail.python.org/mailman/listinfo/python-list > Have yo tried emacs? It's similar to Vim in that it relies very heavily on keyboard shortcuts and such. However, you may like the shortcuts a bit more or find them easier to learn. I side with Marc Brooks in that I believe you should definitely be willing to put in the time to learn an editor of your choice. Becoming an expert at using an editor will make you a lot more productive. Personally, I use Pycharm for most of my projects as I deal with large amounts of different files that can be thousands of lines long. All of the code completion and structure indexing really helps when you need to remember the structure of large applications. Pycharm's debugger integration is also totally awesome. I usually use the debugger to run my tests to get more informative tracebacks or to add breakpoints to failing tests. The git integration is very useful as well because I personally hate Git's CLI. For some small projects I'll use Atom as it gives me a sublime-esque interface without forcing me to use proprietary software. Otherwise I'll use nano for small, single file projects. Have you looked into ipython notebook? It's not exactly an IDE, but it does have built in code completion and makes' it really simple to document your code. From best_lay at yahoo.com Mon Jan 2 12:53:24 2017 From: best_lay at yahoo.com (Wildman) Date: Mon, 02 Jan 2017 11:53:24 -0600 Subject: learning and experimenting python. References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, 01 Jan 2017 23:02:34 -0800, einstein1410 wrote: > I really don't care the person like you. > Leave my posts, if don't like it. > Why wasting your as well as my time. > Just get lost man, or shut up. _ _ |_| |_| | | /^^^\ | | _| |_ (| "o" |) _| |_ _| | | | _ (_---_) _ | | | |_ | | | | |' | _| |_ | `| | | | | | | / \ | | \ / / /(. .)\ \ \ / \ / / / | . | \ \ \ / \ \/ / ||Y|| \ \/ / \__/ || || \__/ () () || || ooO Ooo -- GNU/Linux user #557453 The cow died so I don't need your bull! From irmen.NOSPAM at xs4all.nl Mon Jan 2 13:14:02 2017 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Mon, 2 Jan 2017 19:14:02 +0100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <586a986a$0$21519$e4fe514c@news.xs4all.nl> On 2-1-2017 12:38, Antonio Caminero Garcia wrote: > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) > is that they look like a space craft dashboard and that unwarranted resources > consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. > My screen should be mostly ?made of code? as usually happens in Vim, Sublime or Atom. > However, Pycharm is really cool and python oriented. [...] > Sublime is my current and preferred code editor. If you like Sublime, and its minimalistic 'Distraction Free Mode', you'll be delighted to know that this exact same feature is in PyCharm as well. Select it (View->Enter distraction free mode), and gone is all the screen clutter and even the menu bar if you so desire. You can focus on just your code. And all of PyCharm's features are still there a mouse click or keyboard shortcut away. Irmen From python at deborahswanson.net Mon Jan 2 13:40:19 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 2 Jan 2017 10:40:19 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <006401d26527$ab881d80$27b23dae@sambora> Dennis Lee Bieber wrote, on January 02, 2017 8:30 AM: > > On Sun, 1 Jan 2017 18:30:03 -0800, "Deborah Swanson" > declaimed the following: > > >Dennis Lee Bieber wrote, on Sunday, January 01, 2017 6:07 PM > >> > >> > >> l1 2 br, Elk Plains 12-26 WA/pi > >> l2 2 br, Elk Plains 12-29 > >> house garage, w/d > >> > > >Yes, that's exactly what I'm doing in this bit of code, > making all the > >listings that are identical except for the date be identical > except for > >the date. You may or may not have tried this approach to finding the > > Out of curiosity -- don't those listings have street > addresses? There must be lots of "2 br" units in the city. > > >ideal house, but this is my third house hunt, using essentially the > >same approach, but in a different language each time. > (Python is by far > >the best version. A dictionary with city names as keys, and several > >data items as values has literally cut the task in half.) > > > > Part of the hassle in your schema is that you are > treating the type of unit (2 br) AND the city (as shown in > your sample; I'd hope a full address is available) as a > single data field. Similarly, you have some obscure code > attached to the state. > > > Maybe I've spent too much time with having to optimize > data structures over the last 30 years, but for my view I'd > end up with a relational database (most Python distributions > include SQLite3, so you don't have to manage a full > client-server system [reminds me, I still need to recreate > MySQL on my system, after a drive failure and OS upgrade]). > Something like: > > create table Properties > ( > ID integer auto-increment primary key, > State char, > City char not null, > Address char not null, > Type char, > Code char, > unique (State, City, Address) > ) > > create table Listings > ( ID integer auto-increment primary key, > Property integer references Properties(ID), > Date datetime, > Notes varchar, > unique (Property, Date) > ) > > Since I don't know what your "code" (the "pi" above) > represents, I don't know if it should be in Properties or > Listings, nor if it is a constraint for uniqueness. Type is > "2 br". I put both in the Properties table as you had them > attached to the city and state sample data. I'd have > preferred to put a "not null" on State, but since that is one > of the fields your example allows to be blank (I presume on > the basis that a local town paper may assume the listing is > for said state)... > > The "unique" constraints mean that any combination of > "State, City, Address" only appears once, same for "Property, > Date" combination. > > This would be the master data, which you'd only perform > updates upon. > > For an update you would formulate a SELECT using the > fields you have for State, City, Address (so, if no State, > you'd select on just City, > Address) if multiple records are returned (only possible if > you are missing one of the three fields) you'd have to log > them as ambiguous with the new listing and needing to be hand > adjusted -- eg; you need to add the missing field by hand); > If a single record is returned you can perform an UPDATE for > Type/Code. If no record is returned you insert a new record > with the fields you have. > THEN, using the unique ID of the Properties record, you > INSERT a new listing record with the date and notes -- if the > insert is rejected (duplicate Property/Date) it indicates you > may have already processed that listing before. You can't > check for that until you've determined the ID of the Property itself. > > Determining if a property has multiple listings becomes a SELECT > > select p.State, p.City, p.Address, p.Type, p.Code, > count(l.ID) from Properties as p inner join Listings as l on > p.ID = l.Property group by p.ID order by p.State, p.City, p.Address > > > Yes, you might have to run periodic checks for missing > data fields when the field is allowed to be NULL and is part > of the uniqueness constraint. This would occur, for the > provided schema, if you had inserted a property with a null > State, and then later had a listing with the state provided > -- as the update/insert logic would come back that there is > no entry for (State, City, Address) -- it would not see the > (noState, City, Address) entry. If the State field were set > to "not null" you'd get an error at that time that could be > logged, allowing you to clean up the new data and reprocess > it -- instead of running a series of SELECT statements with > each one leaving out one of the "null allowed" fields > > select p.ID, p.State, p.City, p.Address > from Properties as p > order by p.City, p.Address > > to get all records with the same city/address (with some > work, and using subselects, the report could be reduced by > first selecting only the records where State is NULL, then > using just those city/addresses for the report selection. > It's been a while, but something like (consider all of this > pseudocode) > > select p.ID, p.State, p.City, p.Address > from Properties as p > inner join (select distinct n.City, n.Address > from Properties as n > where n.State is null) as n2 > where p.City = n2.City and p.Address = n2.Address > order by p.City, p.Address, p.State > > would return all city/address combination where at least one > record has an empty state. > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ I was actually planning to shift from Excel for permanent data storage to SQLite, but as a future development and not something I'm going to do right now. It took me 14 months to find the house I'm living in now, and in the current real estate market it may take that long or longer to find another one as nice this time. But if I get it all down to 15 min of work each night that will be fine. Haha, as long as I can afford my new and higher rent! Also, I just started this project and it's been kind of wild and woolly in the early stages, which is why I need to be concerned about so many oddball cases that won't occur when I have the operation running smoothly. I'll be continuing to rewrite and fine tune my code for some time, and I'd planned to do the migration to SQLite, and learn how to use SQLite and how to use it with python after the core code is stable. I know a smattering of SQL, but I'm not proficient with it, so I'm just taking it all one step at a time. But I'm keeping this message from you in my project file, so when the time comes I'm sure I'll refer to all the helpful information you've given in it. And I'll also be concerned about the data integrity issues you mention, so that I can get it down to a clean, stable operation that's reliable and takes very little time to maintain. All down the road though, not quite yet. As to your specific data curiosities, the '/co' part of 'st/co' is the county, so I have state/county pairs in that field. In this case, 'pi' is the 2 letter code for Pierce County. I may break state and county out into 2 separate fields at some point, but it would only be advisable if I'm actually using the county for anything other than just seeing where it is when I look at the complete location data. State and county do go together, and it made building the locations dictionary by hand easier to bundle them together, but at some point I'll have all the location data and won't be working on it anymore, and that might be the time to separate them. I'm using Craiglist, and oddly enough they don't make any provision for the address. So if one is available at all it's tucked away in 3 or 4 different places. I will be adding address and phone number fields after I get the bulk of it under control, but I will only be doing the detective work on places I might actually go to look at. Also, I'll probably add local realtor's listings to my Craigslist base, but that too is a future development. And I've kept the photos in past projects, and would like to do that this time too, but that will probably be one of the last things I do. About the time that I start getting serious about going out and looking at these places. Thanks for all your suggestions, especially about using SQLite, which I will be doing relatively soon. I'm pretty sure there's a way to store the photos in SQLite. From ian.g.kelly at gmail.com Mon Jan 2 15:46:09 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 2 Jan 2017 14:46:09 -0600 Subject: learning and experimenting python. In-Reply-To: References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Jan 2, 2017 10:57 AM, "Wildman via Python-list" wrote: On Sun, 01 Jan 2017 23:02:34 -0800, einstein1410 wrote: > I really don't care the person like you. > Leave my posts, if don't like it. > Why wasting your as well as my time. > Just get lost man, or shut up. [Obscene gesture trimmed] Way to deescalate. This is no more welcome than the post you're replying to. From m at funkyhat.org Mon Jan 2 16:30:25 2017 From: m at funkyhat.org (Matt Wheeler) Date: Mon, 02 Jan 2017 21:30:25 +0000 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <53f1736a-2e99-619b-828b-5ec70b9c09d2@gmail.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <53f1736a-2e99-619b-828b-5ec70b9c09d2@gmail.com> Message-ID: On Mon, 2 Jan 2017 at 16:24 Michael Torrie wrote: > Really, the basic stuff is enough to be very productive in vim. In fact > just knowing how to save and quit is half the battle! A little cheat > sheet for vim by your keyboard would be plenty I think. If all you knew > was how to change modes, insert, append, change word, yank, delete, and > paste, that is 99% of what you'd use every day. You can use normal > arrow keys, home, end, and page up and page down for cursor movement in > vim, so even if you can't remember ^,$, gg, or GG, you'll do fine. > Eventually you can begin to add in other things, like modifiers to c > (change). > I second this. Make sure you've got all the nice Vim stuff enabled (set nocompatible, set mouse=a etc.). And if you're not comfortable to begin with using normal-mode commands, just stick with the mouse, arrow keys & insert mode. Once you get comfortable with that, perhaps set a target to learn one or two normal-mode commands a week and go from there. I found as soon as I'd learnt to use the direction commands & save I was already more productive in vim than Notepad++ for example, and I just got faster from there. > There probably are a lot of nice plugins for ViM, but I use none of > them. I just don't find them that useful. I don't seem to need any IDE > help with Python. > On the other hand I use bags of plugins. I particularly recommend Jedi if your computer is fast enough (it's a bit of a resource hog), and syntastic as a great way to integrate style checkers & linters into vim. -- -- Matt Wheeler http://funkyh.at From cs at zip.com.au Mon Jan 2 16:54:25 2017 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 3 Jan 2017 08:54:25 +1100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: Message-ID: <20170102215425.GA98599@cskk.homeip.net> On 02Jan2017 21:30, Matt Wheeler wrote: >On Mon, 2 Jan 2017 at 16:24 Michael Torrie wrote: >> Really, the basic stuff is enough to be very productive in vim. In fact >> just knowing how to save and quit is half the battle! A little cheat >> sheet for vim by your keyboard would be plenty I think. [...] When I was learning vi I'd often spend a day learning a single keystroke. Not because they're hard, but because I wanted it in my typing muscle memory. This approach controlled the numberof new things I was trying to learn (roughly one thing at a time) while still steadily accumulating vi skills. [...] >Once you get comfortable with that, perhaps set a target to learn one or >two normal-mode commands a week and go from there. Indeed, like that! >> There probably are a lot of nice plugins for ViM, but I use none of >> them. I just don't find them that useful. I don't seem to need any IDE >> help with Python. > >On the other hand I use bags of plugins. I particularly recommend Jedi if >your computer is fast enough (it's a bit of a resource hog), and syntastic >as a great way to integrate style checkers & linters into vim. I've been a traditional vi die hard for too long. I moved to vim (as my default) some years ago for: utf-8 support, syntax colouring, filename completion. Recently I'm in a shiny new job with shinier newer people and am starting down the Dark Path of plugins. Presently I'm using ctrlp, which is a great way to open files in a deep/wide code tree, partiularly one which is still unfamiliar. I guess my point here is that, as with others, you don't need to be expert with a particular editor; once past the basics you will be productive and you can steadily accrue skill with it. Regarding IDEs, my environment is a shell terminal and a vim terminal and a browser for doco. Tiled windows (exactly how depends on your platform - I'm on a Mac at present and using Divvy to position windows). Cheers, Cameron Simpson From juan0christian at gmail.com Mon Jan 2 17:13:22 2017 From: juan0christian at gmail.com (Juan C.) Date: Mon, 2 Jan 2017 20:13:22 -0200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: On Mon, Jan 2, 2017 at 9:38 AM, Antonio Caminero Garcia < tonycamgar at gmail.com> wrote: > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. My screen should be mostly ?made of code? as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool and python oriented. I use Sublime Text for small scripts and PyCharm Professional for bigger projects and I don't find it resource heavy and the interface is simple enough. You can pretty much hide all menus and sidebars on PyCharm and you'd get pure code (it also has a "Distraction Free" mode), I suggest you to read Pycharm's official doc to know a little more about it. My advice is to NOT overthink it. IDEs and code editor are just tools, just pick one you like and you're done. From python at deborahswanson.net Mon Jan 2 17:52:00 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 2 Jan 2017 14:52:00 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <002401d2654a$d4b0af60$27b23dae@sambora> Dennis Lee Bieber wrote on January 02, 2017 8:30 AM > > On Sun, 1 Jan 2017 18:30:03 -0800, "Deborah Swanson" > declaimed the following: > > Out of curiosity -- don't those listings have street > addresses? There must be lots of "2 br" units in the city. > > Part of the hassle in your schema is that you are > treating the type of unit (2 br) AND the city (as shown in > your sample; I'd hope a full address is available) as a > single data field. Similarly, you have some obscure code > attached to the state. haha - what schema? But seriously, I've loosely had one in mind all along, and there are currently 14 columns for each listing. I've just only mentioned the fields involved in the conditional problem I was having. One of those fields (br) is for bedrooms, and another couple I'll add later will be for address, phone, and local features, which may be pulled from my locations dictionary. The Descriptions I made up for my example are very simple short ones. Real webpage titles from Craigslist are frequently very long, complex and full of typos. I go fishing in them for most of the data, but I don't plan on making any effort to clean them up. I think it was you who asked another question that I didn't answer at the time, of why I used those weird 2-letter codes for the field names. It's rather stupidly simple. Since the "database" will be in flux for some time, with columns being moved around, added and deleted, I came up with this little scheme that executes right after the csv has been opened and read into ls[], to avoid having to change any of the subscripts in my code whenever the columns changed: flds = len(ls[0]) cl, ur, d1, de, lo, st, mi, ki, re, da, br, no, yn, ma, ar = range(0,flds) ls[0] is the title row, so the range is automatically sized to the number of columns in the current csv. If I open a csv with more columns than last time, I hit an IndexError right off, which is a handy reminder if I've forgotten to update the field codes. I'd welcome anyone's improvement on this scheme, if it accomplishes the same result. Also, 'kind' is for the kind of property. I want a house, but I'd consider a cottage, trailer or manufactured home if it's nice and has a garage or a carport. And I want 2 bedrooms (one for my office), but I'm keeping track of 1 brs & even nice studio cabins, just in case I really need to get out of here and into something I can afford quickly. I've also tried to screen the plexes (duplex, triplex, six-plex and what all), apartments, condos and other undesirable "kinds" out of my searches, but they still slip into the results anyway. Craigslist's search function leaves much to be desired. So 'kind' also serves as a place to snag them for later deletion. And it's a red flag when one listing for a property comes up with 'kind' = 'house' and another comes up 'duplex'. For some reason people like to pretend their duplexes are houses, and sometimes they change their titles from duplex to house after they've had to relist it a couple of times. Or they don't say what kind of property it is, and I have to pull up the ad and look at it. Details, details, details. ;) D. From greg.ewing at canterbury.ac.nz Mon Jan 2 18:27:53 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 03 Jan 2017 12:27:53 +1300 Subject: Cleaning up conditionals In-Reply-To: References: <002401d2654a$d4b0af60$27b23dae@sambora> Message-ID: Deborah Swanson wrote: > flds = len(ls[0]) > cl, ur, d1, de, lo, st, mi, ki, re, da, br, no, yn, ma, ar = > range(0,flds) You might like to consider converting the row into a namedtuple, then you could refer to the fields using attribute names instead of indexes. You could even use the header row to set up the field names in the namedtuple, so if you reorder the columns in the file, the code would automatically adapt. -- Greg From tonycamgar at gmail.com Mon Jan 2 18:36:06 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Mon, 2 Jan 2017 15:36:06 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <53f1736a-2e99-619b-828b-5ec70b9c09d2@gmail.com> Message-ID: <4e27a90b-7963-4214-b897-bc1ef702c78d@googlegroups.com> On Monday, January 2, 2017 at 8:24:29 AM UTC-8, Michael Torrie wrote: > On 01/02/2017 04:38 AM, Antonio Caminero Garcia wrote: > > The problem with Vim is the learning curve, so I know the very basic > > stuff, but obviously not enough for coding and I do not have time to > > learn it, it is a pity because there are awesome plugins that turns > > Vim into a lightweight powerful IDE-like. So now it is not an option > > but I will reconsider it in the future, learning little by little. > > Also, I am not very fan GUI guy if the task can be accomplished > > through the terminal. However, I don?t understand why people > > underrate GUIs, that said I normally use shortcuts for the most > > frequent tasks and when I have to do something that is not that > > frequent then I do it with the mouse, for the latter case in vim you > > would need to look for that specific command every time. > > Really, the basic stuff is enough to be very productive in vim. In fact > just knowing how to save and quit is half the battle! A little cheat > sheet for vim by your keyboard would be plenty I think. If all you knew > was how to change modes, insert, append, change word, yank, delete, and > paste, that is 99% of what you'd use every day. You can use normal > arrow keys, home, end, and page up and page down for cursor movement in > vim, so even if you can't remember ^,$, gg, or GG, you'll do fine. > Eventually you can begin to add in other things, like modifiers to c > (change). > > There probably are a lot of nice plugins for ViM, but I use none of > them. I just don't find them that useful. I don't seem to need any IDE > help with Python. yeah, for me I think of the IDE (and computers in general must be seen like that) as a coworker or as paring programming experience. So I agree I have been developing in Python without IDE a long time and I know if I had some features borrow from full featured IDEs will definitely help me out.I will give a try to Vim asap, now I am trying Visual Studio now and it seems that is all I want. From python at deborahswanson.net Mon Jan 2 18:46:16 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 11:46:16 +1200 Subject: Clickable hyperlinks Message-ID: <1391343616@f38.n261.z1.binkp.net> Excel has a formula: =HYPERLINK(url,description) that will put a clickable link into a cell. Does python have an equivalent function? Probably the most common use for it would be output to the console, similar to a print statement, but clickable. From python at deborahswanson.net Mon Jan 2 18:57:30 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 11:57:30 +1200 Subject: Numpy error Message-ID: <1433087703@f38.n261.z1.binkp.net> > ImportError: > /home/conrado/Canopy/appdata/canopy-1.5.5.3123.rh5-x86_64/lib/ > libgfortran.so.3: > version `GFORTRAN_1.4' not found (required by /lib64/liblapack.so.3) Looks like you need to install the 'GFORTRAN_1.4' plugin into Canopy. I don't know where you'll find it, but Canopy's main website would be a good place to start. Or google "GFORTRAN_1.4 Canopy download". jorge.conrado at cptec.inpe.br wrote, on January 03, 2017 6:45 AM: > > Hi, > > > I alredy used Python and now I have the message: > > import numpy as np > Traceback (most recent call last): > File "", line 1, in > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/__init__.py", > line 153, in > from . import add_newdocs > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/add_newdocs.py", > line 13, in > from numpy.lib import add_newdoc > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/lib/__init__.py", > line 18, in > from .polynomial import * > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/lib/polynomial.py", > line 19, in > from numpy.linalg import eigvals, lstsq, inv > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/linalg/__ini > t__.py", > line 50, in > from .linalg import * > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/linalg/linalg.py", > line 29, in > from numpy.linalg import lapack_lite, _umath_linalg > ImportError: > /home/conrado/Canopy/appdata/canopy-1.5.5.3123.rh5-x86_64/lib/ > libgfortran.so.3: > version `GFORTRAN_1.4' not found (required by /lib64/liblapack.so.3) > > > I did: pip istall --user numpy > > > Requirement already satisfied: numpy in > /home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-p > ackages/numpy-1.8.0-py2.7-linux-x86_64.egg > > > What can I do to solve this message. > > > Thanks, > > > Conrado > -- > https://mail.python.org/mailman/listinfo/python-list > From python at deborahswanson.net Mon Jan 2 19:06:42 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 12:06:42 +1200 Subject: Screwing Up looping in Generator Message-ID: <1976089172@f38.n261.z1.binkp.net> Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > Hi > > This is simple, but its getting me confused. > > I have a csv writer that opens a file and loops each line of > the file for each file and then closes, writing one file. > > I want to alter the behaviour to be a written file for each > input file. I saw a roundrobin example however it failed for > me as you cannot get len(generator) to use a while loop on. > it exhausts > > should I use the same for again after the with open? > > rootobs in this code is my generator and I am already looping > it however > def data_attr(roots): > """Get the root object and iter items.""" > for file in rootobs: > base = os.path.basename(file.name) > write_to = os.path.join("output", > os.path.splitext(base)[0] + ".csv") > with open(write_to, 'w', newline='') as csvf: > race_writer = csv.writer(csvf, delimiter=',') > race_writer.writerow( > ["meet_id", "meet_venue", "meet_date", "meet_rail", > ... > # other categories here > ... > "jockeysurname", "jockeyfirstname"]) > for xml_data in roots: > ... > # parsing code > for noms in race_child: > if noms.tag == 'nomination': > race_writer.writerow( > [meet_id, meet_venue, meet_date, > ... > #parsing info removed > noms.get("jockeyfirstname")]) > > Cheers > Sayth What's the code for your generator? And I don't see where you call 'next'. From python at deborahswanson.net Mon Jan 2 19:17:00 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 12:17:00 +1200 Subject: Screwing Up looping in Generator Message-ID: <1844516036@f38.n261.z1.binkp.net> > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > Hi > > > > This is simple, but its getting me confused. > > > > I have a csv writer that opens a file and loops each line of > > the file for each file and then closes, writing one file. > > > > I want to alter the behaviour to be a written file for each > > input file. I saw a roundrobin example however it failed for > > me as you cannot get len(generator) to use a while loop on. > > it exhausts > > > > should I use the same for again after the with open? > > > > rootobs in this code is my generator and I am already looping > > it however > > def data_attr(roots): > > """Get the root object and iter items.""" > > for file in rootobs: > > base = os.path.basename(file.name) > > write_to = os.path.join("output", > > os.path.splitext(base)[0] + ".csv") > > with open(write_to, 'w', newline='') as csvf: > > race_writer = csv.writer(csvf, delimiter=',') > > race_writer.writerow( > > ["meet_id", "meet_venue", "meet_date", "meet_rail", > > ... > > # other categories here > > ... > > "jockeysurname", "jockeyfirstname"]) > > for xml_data in roots: > > ... > > # parsing code > > for noms in race_child: > > if noms.tag == 'nomination': > > race_writer.writerow( > > [meet_id, meet_venue, meet_date, > > ... > > #parsing info removed > > > noms.get("jockeyfirstname")]) > > > > Cheers > > Sayth > > What's the code for your generator? And I don't see where you > call 'next'. I think you're expecting for file in rootobs to get the next yield for you from rootobs, but unless someone corrects me, I don't think you can expect a 'for' statement to do that. You need to have a 'next' statement inside your for loop to get the next yield from the generator. But I might not understand exactly what you're asking. From cs at zip.com.au Mon Jan 2 19:33:15 2017 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 3 Jan 2017 11:33:15 +1100 Subject: trouble with cmd.Cmd and prompting Message-ID: <20170103003315.GA29437@cskk.homeip.net> I'm using cmd.Cmd to write a little FTP-like command line to interface to a storage system of mine and encountering weird behaviour. When I enter a command the next prompt appears _before_ the associated operation runs, or so it appears. Look at this: [~/hg/css-venti(hg:venti)]fleet*> dev ./bin/vt -M -C - -S '[archive]' ftp ~/var/mnt/archive.vt stores = [] S = Store(MappingStore(ConfigWatcher('/Users/cameron/.vtrc')[archive])) ~/var/mnt/archive.vt:/> ls ~/var/mnt/archive.vt:/> precmd: line='ls' ---------- OnyX.dmg postcmd: stop=None, line='ls' See how the cmd.Cmd prompt "~/var/mnt/archive.vt:/> " appears again right after the "ls", and before its output: precmd: line='ls' ---------- OnyX.dmg postcmd: stop=None, line='ls' The precmd and postcmd lines are debugging, emitted from my precmd and postcmd methods in the subclass. Has anyone seen this kind of thing before? The salient parts of my class are shown below. It's not complete and I'm happy to post the full thing if people need to see it. The behaviour is the same regardless of the command; even an empty command shows it: ~/var/mnt/archive.vt:/> precmd: line='' postcmd: stop=None, line='' ~/var/mnt/archive.vt:/> precmd: line='' postcmd: stop=None, line='' Any insights or suggestions welcome. Code below. Cheers, Cameron Simpson from cmd import Cmd import readline class FTP(Cmd): def __init__(self, D, sep=None, FS=None, prompt=None): Cmd.__init__(self) self._prompt = prompt if sep is None: sep = '/' # NB: _not_ os.sep self.root = D self.cwd = D self.sep = sep self.fs = FS self._set_prompt() def _set_prompt(self): prompt = self._prompt pwd = '/' + self.op_pwd() self.prompt = ( pwd if prompt is None else ":".join( (prompt, pwd) ) ) + '> ' def precmd(self, line): X("precmd: line=%r", line) return line def postcmd(self, stop, line): X("postcmd: stop=%s, line=%r", stop, line) self._set_prompt() return stop def emptyline(self): pass def do_EOF(self, args): ''' Quit on end of input. ''' return True def do_quit(self, args): ''' Usage: quit ''' return True def do_ls(self, args): ''' Usage: ls [paths...] ''' argv = shlex.split(args) if not argv: argv = sorted(self.cwd.entries.keys()) for name in argv: with Pfx(name): E, P, tail = resolve(self.cwd, name) if tail: error("not found: unresolved path elements: %r", tail) else: M = E.meta S = M.stat() u, g, perms = M.unix_perms typemode = M.unix_typemode typechar = ( '-' if typemode == stat.S_IFREG else 'd' if typemode == stat.S_IFDIR else 's' if typemode == stat.S_IFLNK else '?' ) print("%s%s%s%s %s" % ( typechar, rwx((typemode>>6)&7), rwx((typemode>>3)&7), rwx((typemode)&7), name )) From breamoreboy at gmail.com Mon Jan 2 19:51:06 2017 From: breamoreboy at gmail.com (breamoreboy) Date: Tue, 03 Jan 2017 12:51:06 +1200 Subject: pip install -r requirements.txt fails with Python 3.6 on Windows 10 Message-ID: <611115075@f38.n261.z1.binkp.net> On Tuesday, January 3, 2017 at 8:08:37 PM UTC, Uri Even-Chen wrote: > Thank you, I'll consider to update our requirements to latest versions of > all packages. Last time I checked in 22th December 2016 and all our > requirements were the latest versions. In the meantime we can keep using > Python 3.5. By the way, Travis CI tests passed with the same requirements > and Python 3.6 (and 3.5 and 3.4). How did it install the requirements > there? Does it depend on the operating system? > > I see now that Python 3.6.0 was released on 2016-12-23. > > By the way we use Ubuntu 16.04 in production with Python 3.5.2, so it's not > that important to support Python 3.6 right now. What are the reasons to > upgrade Python to 3.6? > > Thanks, > Uri. > > > *Uri Even-Chen* > [image: photo] Phone: +972-54-3995700 > Website: http://www.speedysoftware.com/uri/en/ > > > Go here http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow to get what you need. In all my years of downloading from this site I've never, ever had a problem. Kindest regards. Mark Lawrence. From python at deborahswanson.net Mon Jan 2 19:53:00 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 12:53:00 +1200 Subject: Screwing Up looping in Generator Message-ID: <344519320@f38.n261.z1.binkp.net> > > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > > > Hi > > > > > > This is simple, but its getting me confused. > > > > > > I have a csv writer that opens a file and loops each line of the > > > file for each file and then closes, writing one file. > > > > > > I want to alter the behaviour to be a written file for each input > > > file. I saw a roundrobin example however it failed for me as you > > > cannot get len(generator) to use a while loop on. it exhausts > > > > > > should I use the same for again after the with open? > > > > > > rootobs in this code is my generator and I am already looping it > > > however def data_attr(roots): > > > """Get the root object and iter items.""" > > > for file in rootobs: > > > base = os.path.basename(file.name) > > > write_to = os.path.join("output", > > > os.path.splitext(base)[0] + ".csv") > > > with open(write_to, 'w', newline='') as csvf: > > > race_writer = csv.writer(csvf, delimiter=',') > > > race_writer.writerow( > > > ["meet_id", "meet_venue", "meet_date", > "meet_rail", > > > ... > > > # other categories here > > > ... > > > "jockeysurname", "jockeyfirstname"]) > > > for xml_data in roots: > > > ... > > > # parsing code > > > for noms in race_child: > > > if noms.tag == 'nomination': > > > race_writer.writerow( > > > [meet_id, meet_venue, > meet_date, > > > ... > > > #parsing info removed > > > > > noms.get("jockeyfirstname")]) > > > > > > Cheers > > > Sayth > > > > What's the code for your generator? And I don't see where you > > call 'next'. > > I think you're expecting > > for file in rootobs > > to get the next yield for you from rootobs, but unless > someone corrects me, I don't think you can expect a 'for' > statement to do that. You need to have a 'next' statement > inside your for loop to get the next yield from the generator. > > But I might not understand exactly what you're asking. You probably want something like : for f in rootobs: file = next base = os.path.basename(file.name) . . . (etc) Notice I changed your iterating variable name to 'f', so you can use 'file' throughout your code after you get the next one from rootobs. As written, you'll get a StopIteration exception when rootobs runs out of files, which you can catch with a try/except. Or you can just let the code end there if you're done. From python at deborahswanson.net Mon Jan 2 19:53:11 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 2 Jan 2017 16:53:11 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <003901d2655b$c2676130$27b23dae@sambora> Gregory Ewing wrote, on Monday, January 02, 2017 3:28 PM > > Deborah Swanson wrote: > > flds = len(ls[0]) > > cl, ur, d1, de, lo, st, mi, ki, re, da, br, no, yn, ma, ar = > > range(0,flds) > > You might like to consider converting the row into a > namedtuple, then you could refer to the fields using > attribute names instead of indexes. > > You could even use the header row to set up the field > names in the namedtuple, so if you reorder the columns > in the file, the code would automatically adapt. > > -- > Greg I actually tried putting them into a list early on, but the problem was that it didn't do its main job, which is to help me avoid having to rewrite code everytime the columns change. Once you put the field names or codes into any kind of sequence, they can no longer be used as scalar indices without rewriting code to use their new indices in the tuple or list. Not really any kind of major improvement over counting out each one's position in the row. Unless you know of, and are suggesting, a way to index a sequence with strings instead of integers, so the code could remain untouched with string indices when changes to the columns are made. Certainly might be possible, I simply haven't and probably wouldn't have thought of it since what I have works and I've got tons of other stuff to do. So here's the new question: Is it possible to index a sequence with strings instead of integers? (Dictionaries excluded because dicts are not sequentially ordered in python 3.5 and earlier. Although, I've read that they will be in 3.6 or beyond.) Also, for such a scheme to be superior to what I already have, it needs to be very nearly self-maintaining. I only touch the 2nd of those two lines when I'm adding or deleting columns. From jeanpierreda at gmail.com Mon Jan 2 19:57:08 2017 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Tue, 03 Jan 2017 12:57:08 +1200 Subject: Clickable hyperlinks Message-ID: <1059698736@f38.n261.z1.binkp.net> Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to make an URL clickable is to use a terminal that makes URLs clickable, and print the URL: print("%s: %s" % (description, url)) -- Devin On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson wrote: > Excel has a formula: > > =HYPERLINK(url,description) > > that will put a clickable link into a cell. > > Does python have an equivalent function? Probably the most common use > for it would be output to the console, similar to a print statement, but > clickable. > > -- > https://mail.python.org/mailman/listinfo/python-list > From tim at akwebsoft.com Mon Jan 2 20:04:54 2017 From: tim at akwebsoft.com (Tim Johnson) Date: Mon, 2 Jan 2017 16:04:54 -0900 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <20170103010454.GA18910@mail.akwebsoft.com> * Antonio Caminero Garcia [170102 02:50]: <....> > Now, I am thinking about giving a try to Visual Studio Code > Edition (take a look, it sounds good > https://marketplace.visualstudio.com/items?itemName=donjayamanne.python). > I need an editor for professional software development. What would > you recommend to me? The best thing - as has been emphasised by others regarding this topic - is to establish tools, stick with them and learn them well. I use two approaches on linux: 1)From Gnome terminal I run MC (midnight commander) as my default file manager with vim (in terminal mode) as the MC default editor. This method is used for ad-hoc editing of python source code, but also for system editing in general. 2)I use emacs with elpy mode in GUI mode for large-scale work. Elpy is so helpful, I'm almost embarassed to admit being a pythonist. To compound the embarassment, the elpy developer is extremely helpful and very generous. :) -> I've used gvim (GUI mode) extensively in the past. I find vim more "nimble", thus my preferance for quick-and-dirty edits. Emacs, on the other hand, is enormously extendable and I have implemented extensive keymapping. For me, augmenting keymapping with the emacs help system trumps vim's more nimble approach. In addition, I've implemented an auxilary help system using emacs' built-in browser so that I can call up category - based "cheat-sheats" with simple html highlighting and hyperlinking. My caveat is that both vim and emacs are a tough learning curve. Vimscript extends vim, elisp extends emacs. In both cases, one is essentially learning an additional programming language. One's personal preference for input devices should also be considered, IMHO : I prefer the keyboard over pointing devices and a trackball over a mouse for pointing device. I use a small-footprint 68-key tenkeyless keyboard with a standalone keypad with my left hand (trackball is on the right). I've also programmed the keypad extensively for emacs. The bottom line, as others have stated, is to consistently stick with some approach that fits one's personal preferences. We are fortunate to have so many options. -- Tim http://www.akwebsoft.com, http://www.tj49.com From python at deborahswanson.net Mon Jan 2 20:19:16 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 13:19:16 +1200 Subject: Screwing Up looping in Generator Message-ID: <3293268733@f38.n261.z1.binkp.net> > > > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > > > > > Hi > > > > > > > > This is simple, but its getting me confused. > > > > > > > > I have a csv writer that opens a file and loops each line of the > > > > file for each file and then closes, writing one file. > > > > > > > > I want to alter the behaviour to be a written file for > each input > > > > file. I saw a roundrobin example however it failed for > me as you > > > > cannot get len(generator) to use a while loop on. it exhausts > > > > > > > > should I use the same for again after the with open? > > > > > > > > rootobs in this code is my generator and I am already looping it > > > > however def data_attr(roots): > > > > """Get the root object and iter items.""" > > > > for file in rootobs: > > > > base = os.path.basename(file.name) > > > > write_to = os.path.join("output", > > > > os.path.splitext(base)[0] + ".csv") > > > > with open(write_to, 'w', newline='') as csvf: > > > > race_writer = csv.writer(csvf, delimiter=',') > > > > race_writer.writerow( > > > > ["meet_id", "meet_venue", "meet_date", > > "meet_rail", > > > > ... > > > > # other categories here > > > > ... > > > > "jockeysurname", "jockeyfirstname"]) > > > > for xml_data in roots: > > > > ... > > > > # parsing code > > > > for noms in race_child: > > > > if noms.tag == 'nomination': > > > > race_writer.writerow( > > > > [meet_id, meet_venue, > > meet_date, > > > > ... > > > > #parsing info removed > > > > > > > noms.get("jockeyfirstname")]) > > > > > > > > Cheers > > > > Sayth > > > > > > What's the code for your generator? And I don't see where > you call > > > 'next'. > > > > I think you're expecting > > > > for file in rootobs > > > > to get the next yield for you from rootobs, but unless > > someone corrects me, I don't think you can expect a 'for' > > statement to do that. You need to have a 'next' statement > > inside your for loop to get the next yield from the generator. > > > > But I might not understand exactly what you're asking. > > You probably want something like : > > for f in rootobs: > file = next > base = os.path.basename(file.name) > . > . > . > (etc) > > Notice I changed your iterating variable name to 'f', so you > can use 'file' throughout your code after you get the next > one from rootobs. > > As written, you'll get a StopIteration exception when rootobs > runs out of files, which you can catch with a try/except. Or > you can just let the code end there if you're done. Well rats, I'm embarrassed. It's been awhile since I've used a generator and I forgot that you have to create the generator object first and use it to call the next function. And I really don't think you can use a generator as your range in a for loop. So I'd use a 'while True', and break out of the loop when you hit the StopIteration exception: files = rootobs() while True: try: file = files.next() except StopIteration: break base = os.path.basename(file.name) . . . (etc) (Now I'm just going to shut up, until somebody else says something.) From python at deborahswanson.net Mon Jan 2 20:34:48 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 13:34:48 +1200 Subject: Clickable hyperlinks Message-ID: <2633604084@f38.n261.z1.binkp.net> Devin Jeanpierre wrote, on January 03, 2017 12:57 PM >Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to >make an URL clickable is to use a terminal that makes URLs clickable, and >print the URL: > > >print("%s: %s" % (description, url)) > > > > >-- Devin I'm sorry, I should have said a GUI console because I wouldn't expect a text-based console to produce clickable links. But it appears that a simple GUI console can't do it either. I have 3 GUI consoles and in all 3, when I ask: print("%s: %s" % ("python.org list", "https://mail.python.org/mailman/listinfo/python-list")) I get: python.org list: https://mail.python.org/mailman/listinfo/python-list (Outlook made the url clickable here, the python GUI consoles just output plain text.) Pretty clearly the output is plain text because your format parameters are %s. Maybe the trick, if there is one, is in a format parameter for urls. On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson wrote: Excel has a formula: =HYPERLINK(url,description) that will put a clickable link into a cell. Does python have an equivalent function? Probably the most common use for it would be output to the console, similar to a print statement, but clickable. -- https://mail.python.org/mailman/listinfo/python-list From breamoreboy at gmail.com Mon Jan 2 20:41:44 2017 From: breamoreboy at gmail.com (breamoreboy) Date: Tue, 03 Jan 2017 13:41:44 +1200 Subject: How Best to Coerce Python Objects to Integers? Message-ID: <3683906776@f38.n261.z1.binkp.net> Hi all, I'd suggest that this http://blog.pyspoken.com/2017/01/02/how-best-to-c oerce-python-objects-to-integers/ is not one of the greatest articles ever written about Python exception handling. Other opinions are welcome. Kindest regards. Mark Lawrence. From steve+python at pearwood.info Mon Jan 2 20:57:40 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Tue, 03 Jan 2017 12:57:40 +1100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <586b0517$0$1584$c3e8da3$5496439d@news.astraweb.com> On Mon, 2 Jan 2017 10:38 pm, Antonio Caminero Garcia wrote: > Hello, I am having a hard time deciding what IDE or IDE-like code editor > should I use. This can be overwhelming. Linux is my IDE. https://sanctum.geek.nz/arabesque/series/unix-as-ide/ I dislike the Unix-style Vim/Emacs text editors, I prefer a traditional GUI-based editor. So my "IDE" is: - Firefox, for doing searches and looking up documentation; - an GUI programmer's editor, preferably one with a tab-based interface, such as geany or kate; - a tab-based terminal. Both geany and kate offer auto-completion based on previously seen words. They won't auto-complete function or method signatures, but in my my experience this is the "ninety percent" solution: word-based auto-complete provides 90% of the auto-complete functionality without the cost of full signature-based auto-complete. In the terminal, I have at least three tabs open: one open to the Python interactive interpreter, for testing code snippets and help(obj); one where I run my unit tests ("python -m unittest myproject_tests"); and one where I do any assorted other tasks, such as file management, checking code into the repo, etc. I've played with mypy a few times, but not used it seriously in any projects. If I did, I would run that from the command line too, like the unit tests. Likewise for any linters or equivalent. > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, > IntelliJ with Python plugin. > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, > Pycharm) is that they look like a space craft dashboard and that > unwarranted resources consumption and the unnecessary icons. Indeed. If they provide any useful functionality I don't already have, I've never come across it. The only thing I'd like to try is an editor that offers semantic highlighting instead of syntax highlighting: https://medium.com/@evnbr/coding-in-color-3a6db2743a1e I once tried Spyder as an IDE, and found that it was so bloated and slow it couldn't even keep up with my typing. I'm not even a touch typist! I'd start to type a line like: except ValueError as err: and by the time my fingers were hitting the colon, Spyder was displaying `excep` in red flagged with an icon indicating a syntax error. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From python at deborahswanson.net Mon Jan 2 21:14:50 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 14:14:50 +1200 Subject: Screwing Up looping in Generator Message-ID: <3060726271@f38.n261.z1.binkp.net> Terry Reedy > > On 1/3/2017 3:53 PM, Deborah Swanson wrote: > > >> I think you're expecting > >> > >> for file in rootobs > >> > >> to get the next yield for you from rootobs, but unless someone > >> corrects me, I don't think you can expect a 'for' statement to do > >> that. You need to have a 'next' statement inside your for > loop to get > >> the next yield from the generator. > > As I read this, out of context, it is wrong. It it very > unusual to call > next on the current iterator (here next(rootobs)), inside a for loop. > > > You probably want something like : > > > > for f in rootobs: > > file = next > > This is definitely wrong, as it makes 'find' an alias for the next() > function. > > > base = os.path.basename(file.name) > > and file.name will be an AttributeError. > > --- > If one wants to iterate through files and lines within files, which I > believe I saw in this thread, one should have a for loop > within a for loop. > > -- > Terry Jan Reedy Yes, my first attempts were screwups. I didn't remember generator usage correctly, but I believe my last answer was correct: ...you have to create the generator object first and use it to call the next function. And I really don't think you can use a generator as your range in a for loop. So I'd use a 'while True', and break out of the loop when you hit the StopIteration exception: files = rootobs() while True: try: file = files.next() except StopIteration: break base = os.path.basename(file.name) . . . (etc) From larry at hastings.org Mon Jan 2 21:20:56 2017 From: larry at hastings.org (Larry Hastings) Date: Mon, 2 Jan 2017 18:20:56 -0800 Subject: [RELEASED] Python 3.4.6rc1 and Python 3.5.3rc1 are now available Message-ID: <4437910e-0f87-2d2d-062e-8e4fc0be2a58@hastings.org> On behalf of the Python development community and the Python 3.4 and Python 3.5 release teams, I'm pleased to announce the availability of Python 3.4.6rc1 and Python 3.5.6rc1. Python 3.4 is now in "security fixes only" mode. This is the final stage of support for Python 3.4. Python 3.4 now only receives security fixes, not bug fixes, and Python 3.4 releases are source code only--no more official binary installers will be produced. Python 3.5 is still in active "bug fix" mode. Python 3.5.3rc1 contains many incremental improvements over Python 3.5.2. Both these releases are "release candidates". They should not be considered the final releases, although the final releases should contain only minor differences. Python users are encouraged to test with these releases and report any problems they encounter. You can find Python 3.4.6rc1 here: https://www.python.org/downloads/release/python-346rc1/ And you can find Python 3.5.3rc1 here: https://www.python.org/downloads/release/python-353rc1/ Python 3.4.6 final and Python 3.5.3 final are both scheduled for release on January 16th, 2017. Happy New Year, //arry/ From ethan at stoneleaf.us Mon Jan 2 21:42:52 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 03 Jan 2017 14:42:52 +1200 Subject: How Best to Coerce Python Objects to Integers? Message-ID: <42841261@f38.n261.z1.binkp.net> On 01/03/2017 01:41 PM, breamoreboy at gmail.com wrote: > Hi all, I'd suggest that this [1] is not one of the greatest articles > ever written about Python exception handling. Other opinions are welcome. Aside from calling "except Exception" a "naked except" I think it's decent. He walks through the problem, explains the rationale, and only has one line of code guarded by the except clause. -- ~Ethan~ [1] http://blog.pyspoken.com/2017/01/02/how-best-to-coerce-python-objects-to-in tegers/ From python at deborahswanson.net Mon Jan 2 22:05:20 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 15:05:20 +1200 Subject: Screwing Up looping in Generator Message-ID: <3955896007@f38.n261.z1.binkp.net> Chris Angelico wrote, on January 03, 2017 2:31 PM > > On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson > wrote: > > while True: > > try: > > file = files.next() > > except StopIteration: > > break > > Small side point: Try to avoid calling a generator object's > .next() method directly. Normally, when you _do_ want to do > this, you should be calling next(files). In Python 3, the > magic method is now __next__(), which emphasizes that it's > for defining, not calling. > > As others have pointed out, though, the for loop is the > correct tool for this job. > > ChrisA Ok, I learned how to use generators in Python 2.7.8, which may be different from Python 3 for generators. But I learned from MIT's online introduction to python course, and they certainly seem to know python well. So what is the correct way to call the generator's next yield in Python 3? We only learned to use the next function. If you don't use the next function, what do you use? And yes, we usually used for loops for generators, unless you don't know when the generator will be exhausted. As in this case, where the number of files the generator can provide is unknown. Then we used the while True, break on StopIteration method. From ethan at stoneleaf.us Mon Jan 2 22:09:52 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 03 Jan 2017 15:09:52 +1200 Subject: How Best to Coerce Python Objects to Integers? Message-ID: <980704833@f38.n261.z1.binkp.net> On 01/03/2017 02:47 PM, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: >> Aside from calling "except Exception" a "naked except" > > If you read the comments, you'll see that he originally had an actual > bare except clause, but then improved the code somewhat in response to > a recommendation that SystemExit etc not be caught. I did read the comments and noticed the reasons for the improved code; fixing the article to not use the "naked except" phrase would be another improvement. And, of course, whether or not "except Exception" is too broad depends on the use-case. -- ~Ethan~ From python at deborahswanson.net Mon Jan 2 22:21:10 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 15:21:10 +1200 Subject: Screwing Up looping in Generator Message-ID: <3193713134@f38.n261.z1.binkp.net> -----Original Message----- From: Matt Wheeler [mailto:m at funkyhat.org] Sent: Tuesday, January 03, 2017 1:47 PM To: python at deborahswanson.net; Sayth Renshaw; python-list at python.org Subject: Re: Screwing Up looping in Generator On Tue, 3 Jan 2017 at 20:17 Deborah Swanson wrote: > What's the code for your generator? And I don't see where you > call 'next'. I think you're expecting for file in rootobs to get the next yield for you from rootobs, but unless someone corrects me, I don't think you can expect a 'for' statement to do that. You need to have a 'next' statement inside your for loop to get the next yield from the generator. Yes, you absolutely can expect a for statement to do that. It will accept any iterable as its `y`. >> And here is someone correcting me, which I sort of suspected there might be. Yes, >> range is an iterator, but I didn't know a generator could also be used as an iterator. >> Makes perfect sense though, a generator does dispense its yields in the sequence >> defined by the generator. `for x in y: stuff(x)` is effectively syntactic sugar for: iterator = iter(y) while True: try: x = next(iterator) except(StopIteration): break stuff(x) Manually calling `next()` inside a `while True:` is quite an unusual thing to do. >> And you don't need to do it very often. The only use case I know is this one, where the number of yields the generator can output is unknown (here, the number of files the generator can access is unknown). You might recall that the original poster wanted to use a while, but didn't know how to terminate it. At least that's how I interpreted what he was saying. range() is not part of the for syntax at all, it's completely separate, it simply returns an iterator which the for loop can use, like any other. -- >> I see that now, I just didn't know a generator could be used as an iterator. -- Matt Wheeler http://funkyh.at From no.email at nospam.invalid Mon Jan 2 22:22:10 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 03 Jan 2017 15:22:10 +1200 Subject: Simulating int arithmetic with wrap-around References: <1362463823@f38.n261.z1.binkp.net> Message-ID: <3917850917@f38.n261.z1.binkp.net> Steve D'Aprano writes: > Again, assume both operands are in range for an N-bit signed integer. What's > a good way to efficiently, or at least not too inefficiently, do the > calculations in Python? My first thought is towards the struct module, especially if you want to handle a bunch of such integers at the same time. Or maybe the array module or some combination. Or a C extension. From python at deborahswanson.net Mon Jan 2 22:32:04 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 15:32:04 +1200 Subject: Clickable hyperlinks Message-ID: <1124150781@f38.n261.z1.binkp.net> Grant Edwards wrote, on January 03, 2017 3:13 PM > > On 2017-01-03, Deborah Swanson wrote: > > > I'm sorry, I should have said a GUI console because I > wouldn't expect > > a text-based console to produce clickable links. > > What's a "GUI console"? > > -- > Grant Edwards grant.b.edwards Yow! I > want you to MEMORIZE > at the > collected poems of > gmail.com EDNA ST > VINCENT MILLAY The GUI consoles I have are in Pycharm, the IDLE that comes with Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when I open them, so they're capable of opening links, but whether that means their output space is capable of handling clickable links I don't know. I do know printing a full url with the %s specifier or entering a url and clicking enter just gives you the plain text url. Obviously, not all GUI consoles are enabled recognize and make clickable links from correctly formatted urls. I was hoping there was some functionality in python to make clickable links. Could be a package, if the core language doesn't have it. From no.email at nospam.invalid Mon Jan 2 22:33:08 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 03 Jan 2017 15:33:08 +1200 Subject: Cleaning up conditionals Message-ID: <93733970@f38.n261.z1.binkp.net> "Deborah Swanson" writes: > I'm still wondering if these 4 lines can be collapsed to one or two > lines. In the trade that's what we call a "code smell", a sign that code (even if it works) should probably be re-thought after taking a step back to understand what it is really trying to do. What you seem to want is to take two strings, and if exactly one of them is empty, then change the empty one to be the same as the non-empty one. Leaving aside the subscripts and just calling them x and y, x, y = x or y, y or x is a concise way to do it. That's also smelly since 1) it's overly cute, and 2) it's not clear to me why you'd want to do this operation. It seems like an odd thing to want so maybe there's some different way to solve the surrounding problem. If you want to describe the actual application and context where this appears, that might get some helpful comments. From cr2001 at hotmail.co.nz Mon Jan 2 22:49:14 2017 From: cr2001 at hotmail.co.nz (cr2001) Date: Tue, 03 Jan 2017 15:49:14 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <1223999572@f38.n261.z1.binkp.net> Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? #mynumber.py # this game uses a home made function import random #think of a number computer_number = number.randint(1,100) #create the function is_same() def is_same(target, number: if target == number: result="win" elif target > number: result="low" else: result="high" return result # start the game print("hello. \nI have thought of a number between 1 and 100.") #collect the user's guess as an interger guess = int(input("Can you guess it? ")) #Use our function higher_or_lower = is_same(computer_number, guess) #run the game untill the user is correct while higher_or_lower != "win" if higher_or_lower == "to low" guess = int(input("Sorry, you are too low. Try again.")) else: guess = int(input("Sorry your are too high. Try again.")) higher_or_lower = is_same(computer_number, guess) #end of game input("Correct!\nWell Done\n\n\nPress RETURN to exit.") From greg.ewing at canterbury.ac.nz Mon Jan 2 22:57:56 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 03 Jan 2017 16:57:56 +1300 Subject: Cleaning up conditionals In-Reply-To: References: <003901d2655b$c2676130$27b23dae@sambora> Message-ID: Deborah Swanson wrote: > Unless you know of, and are suggesting, a way to index a sequence with > strings instead of integers, so the code could remain untouched with > string indices when changes to the columns are made. I'm talking about this: https://docs.python.org/3/library/collections.html#collections.namedtuple It's like a tuple, but the fields also have names, so you can access them like attributes using dot-notation. > Also, for such a scheme to be superior to what I already have, it needs > to be very nearly self-maintaining. If you derive the attribute names from the header row, it would be extremely self-maintaining. You would be able to reorder columns without touching the code at all. -- Greg From cr2001 at hotmail.co.nz Mon Jan 2 23:02:14 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 16:02:14 +1200 Subject: Hey, I'm new to python so don't judge. References: <1223999572@f38.n261.z1.binkp.net> Message-ID: <2842576650@f38.n261.z1.binkp.net> When i check the code it comes up with invalid syntax and my writing line gets re directed here def is_same(target, number: if target == number: result="win" elif target > number: result="low" else: result="high" return result From cr2001 at hotmail.co.nz Mon Jan 2 23:16:36 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 16:16:36 +1200 Subject: Hey, I'm new to python so don't judge. References: <731385204@f38.n261.z1.binkp.net> Message-ID: <2481169416@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 1:03:18 PM UTC+13, Erik wrote: > On 03/01/17 23:56, Chris Angelico wrote: > > On Wed, Jan 4, 2017 at 10:49 AM, wrote: > >> #think of a number > >> computer_number = number.randint(1,100) > > > > What's wrong is that you aren't showing us the exception you get on > > this line. *Copy and paste* that exception - the whole thing. It's > > very helpful. > > I doubt it's getting that far (I can see at least one syntax error in > the code pasted). > > cr2001: I echo Chris's sentiment though - what is the error you are > seeing (in it's entirety)? > > E. My apologizes but i'm quite new and would need instructions to what information you need me to get. From ethan at stoneleaf.us Mon Jan 2 23:25:25 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 02 Jan 2017 20:25:25 -0800 Subject: learning and experimenting python. In-Reply-To: References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> Message-ID: <586B27B5.9040107@stoneleaf.us> On 01/02/2017 09:53 AM, Wildman via Python-list wrote: [rude ascii art omitted] That is a completely inappropriate response. -- ~Ethan~ From python.list at tim.thechases.com Mon Jan 2 23:26:16 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 03 Jan 2017 16:26:16 +1200 Subject: Clickable hyperlinks Message-ID: <3265342128@f38.n261.z1.binkp.net> On 2017-01-03 11:46, Deborah Swanson wrote: > Excel has a formula: > > =HYPERLINK(url,description) > > that will put a clickable link into a cell. > > Does python have an equivalent function? Probably the most common > use for it would be output to the console, similar to a print > statement, but clickable. Depends on what you're outputting. In your context, you're creating content (and metadata) for a cell in an Excel workbook. If that's the case you might have to use something like the xlwt module to create an Excel-style worksheet and adjust the properties of the cell to include the hyperlink property. Or you can write out a .csv file with a hyperlink in a cell, which I believe Excel can interpret as a hyperlink. Or write an HTML document with the corresponding HTML tag in it. Or you can just print it to stdout as normal as some terminals detect them and auto-linkify them. But you have to specify where you want this link to appear to know how to solve the problem. -tkc From cr2001 at hotmail.co.nz Mon Jan 2 23:27:32 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 16:27:32 +1200 Subject: Hey, I'm new to python so don't judge. References: <1279173106@f38.n261.z1.binkp.net> Message-ID: <306187502@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 1:17:11 PM UTC+13, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 11:03 AM, Erik wrote: > > I doubt it's getting that far (I can see at least one syntax error in the > > code pasted). > > True true. In any case, the point is to copy and paste the error > message. Callum, please, copy and paste it. > > ChrisA I'm sorry if I'm doing something wrong but all that is happening is when i try to run it a popup says Invalid syntax From cr2001 at hotmail.co.nz Mon Jan 2 23:30:52 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 16:30:52 +1200 Subject: Hey, I'm new to python so don't judge. References: <1486132539@f38.n261.z1.binkp.net> Message-ID: <320478962@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 1:26:26 PM UTC+13, Erik wrote: > Hi Callum, > > On 04/01/17 00:02, Callum Robinson wrote: > > When i check the code it comes up with invalid syntax and my writing > line gets re directed here > > > > def is_same(target, number: > > if target == number: > > result="win" > > elif target > number: > > result="low" > > else: > > result="high" > > return result > > OK, good. That implies it's something wrong with the function definition > ('def'). Look at that very carefully :) (*) > > E. > > (*) My emoticon may give you a hint ... I feel like im missing something so blatantly obvious. From cr2001 at hotmail.co.nz Mon Jan 2 23:32:40 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 16:32:40 +1200 Subject: Hey, I'm new to python so don't judge. References: <1486132539@f38.n261.z1.binkp.net> Message-ID: <2895515730@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 1:26:26 PM UTC+13, Erik wrote: > Hi Callum, > > On 04/01/17 00:02, Callum Robinson wrote: > > When i check the code it comes up with invalid syntax and my writing > line gets re directed here > > > > def is_same(target, number: > > if target == number: > > result="win" > > elif target > number: > > result="low" > > else: > > result="high" > > return result > > OK, good. That implies it's something wrong with the function definition > ('def'). Look at that very carefully :) (*) > > E. > > (*) My emoticon may give you a hint ... I forgot a bloody bracket xD and now theirs a new error ill try to figure this out on my own. From tjreedy at udel.edu Mon Jan 2 23:48:48 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 03 Jan 2017 16:48:48 +1200 Subject: pip install -r requirements.txt fails with Python 3.6 on Windows Message-ID: <2091819101@f38.n261.z1.binkp.net> On 1/3/2017 3:07 PM, Uri Even-Chen wrote: > What are the reasons to upgrade Python to 3.6? The same as for any new version: New features -- see What's New in 3.6. New bug fixes. New performance improvements. Reasons against: The effort to make sure all dependencies are available for 3.6* Possible regressions. * For Windows, any of the 381 wheels available from http://www.lfd.uci.edu/~gohlke/pythonlibs/ should be available in 3.6 versions (unless there was a major problem in recompiling). -- Terry Jan Reedy From python at deborahswanson.net Mon Jan 2 23:50:02 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 16:50:02 +1200 Subject: Clickable hyperlinks Message-ID: <2264884113@f38.n261.z1.binkp.net> Erik wrote, on January 03, 2017 3:30 PM > To: python-list at python.org > Subject: Re: Clickable hyperlinks > > Hi. > > On 03/01/17 19:46, Deborah Swanson wrote: > > Excel has a formula: > > When you start a new topic on the list, could you please write a new > message rather than replying to an existing message and changing the > title/subject? > > For those reading the list in a threaded email client, this > message is > shown as a continuation of your "Cleaning up conditionals" > thread, and > that whole thread in turn shows up as a continuation of the "mentor > training python Romania with certification" discussion (which you had > presumably "reply"ed to originally) ... > > Thanks. E. Certainly. I've been on many other lists before (but none since about 2011), and no one complained of or even mentioned this problem. But if it's a problem with some email readers now, I can easily just start a new message. Just being lazy and following old habits, I guess. ;) From tjreedy at udel.edu Mon Jan 2 23:57:52 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 03 Jan 2017 16:57:52 +1200 Subject: Screwing Up looping in Generator Message-ID: <612772844@f38.n261.z1.binkp.net> On 1/3/2017 3:53 PM, Deborah Swanson wrote: >> I think you're expecting >> >> for file in rootobs >> >> to get the next yield for you from rootobs, but unless >> someone corrects me, I don't think you can expect a 'for' >> statement to do that. You need to have a 'next' statement >> inside your for loop to get the next yield from the generator. As I read this, out of context, it is wrong. It it very unusual to call next on the current iterator (here next(rootobs)), inside a for loop. > You probably want something like : > > for f in rootobs: > file = next This is definitely wrong, as it makes 'find' an alias for the next() function. > base = os.path.basename(file.name) and file.name will be an AttributeError. -+- If one wants to iterate through files and lines within files, which I believe I saw in this thread, one should have a for loop within a for loop. -- Terry Jan Reedy From python at deborahswanson.net Tue Jan 3 00:01:18 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 17:01:18 +1200 Subject: Screwing Up looping in Generator Message-ID: <1029216183@f38.n261.z1.binkp.net> Erik wrote, on January 03, 2017 3:45 PM > > Hi, > > On 03/01/17 22:14, Deborah Swanson wrote: > > ...you have to create the generator object first and use it to call > > the next function. And I really don't think you can use a > generator as > > your range in a for loop. So I'd use a 'while True', and > break out of > > the loop when you hit the StopIteration exception: > > > > files = rootobs() > > > > while True: > > try: > > file = files.next() > > except StopIteration: > > break > > > > base = os.path.basename(file.name) > > . > > . > > . > > (etc) > > What you have done there is taken an understanding of the underlying > machinery that allows the 'for' loop to be syntactic sugar over any > iterable and spelled it out, instead of just using 'for'! Without all > that, your example is: > > for file in rootobs(): > base = os.path.basename(file.name) > . > . > . > (etc) > > [In fact, the machinery would also cope with the return value from > rootobs() being an iterable but not an iterator by using "files = > iter(rootobjs)"]. > > > > You seem to be reading up on how the stuff works under the > covers (i.e., > from the point of view of an implementer of a class or > library) and then > suggesting that that's what the *caller* of that class or > library needs > to do. They don't - for a caller, 'for x in seq:' is all they need to > know - the mechanics are handled by the interpreter coupled with the > dunder methods that the class may implement. > > > E. Ok, I'm in complete agreement with everything you said up to the last paragraph, which I don't disagree with, I just don't see your point. If you've read my last few posts you'll have seen me acknowledging that normally a for loop is used, but a while and break on StopIteration is a method that's useful when the output of the generator is unknown. I haven't read through much documentation on generators, but I have taken a course from MIT, in which a Computer Science professor gave us several methods for working with generators, of which the while and break on StopIteration method is one. The original poster wanted to use a while, and seemed to be saying he didn't know how many files the generator he has would yield, so that was why I recommended the while loop. Normally I would use a for loop too. D. From cr2001 at hotmail.co.nz Tue Jan 3 00:04:28 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 17:04:28 +1200 Subject: Hey, I'm new to python so don't judge. References: <2189830552@f38.n261.z1.binkp.net> Message-ID: <4092501290@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 1:45:22 PM UTC+13, Erik wrote: > Hi Callum, > > On 04/01/17 00:30, Callum Robinson wrote: > > I feel like im missing something so blatantly obvious. > > That's because you are ;). I don't want to come across as patronising, > but I want you to see it for yourself, so, here's a function definition > similar to yours that doesn't have the same syntax error that yours does: > > def foo(spam, ham): > if spam == ham: > return "same" > return "different" > > See the difference? > > E. I've figured out that out but I have a new issue. I like what you are doing making me figure this out as it helps me remember. I'll post the new code and the issue. If you could give me a hint that would be great. ------------------------------------------ Issue ------------------------------------------ Traceback (most recent call last): File "D:/Python/random.py", line 6, in computer_number = number.randint(1, 100) NameError: name 'number' is not defined ----------------------------------------- Here is the most recent code ----------------------------------------- # mynumber.py # this game uses a home made function import random #think of a number computer_number = number.randint(1, 100) #create the function is_same() def is_same(target, number): if target == number: result="Win" elif target > number: result="Low" else: result="High" return result # start the game print("hello. \nI have thought of a number between 1 and 100.") #collect the user's guess as an interger guess = int(input("Can you guess it? ")) #Use our function higher_or_lower = is_same(computer_number, guess) #run the game untill the user is correct while higher_or_lower != "win": if higher_or_lower == "low": guess = int(input("Sorry, you are too low. Try again.")) else: guess = int(input("Sorry your are too high. Try again.")) higher_or_lower = is_same(computer_number, guess) #end of game input("Correct!\nWell Done\n\n\nPress RETURN to exit.") From python at deborahswanson.net Tue Jan 3 00:12:22 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 17:12:22 +1200 Subject: Screwing Up looping in Generator Message-ID: <186423320@f38.n261.z1.binkp.net> Erik wrote, on January 03, 2017 3:53 PM > > On 03/01/17 23:05, Deborah Swanson wrote: > > And yes, we usually used for loops for generators, unless you don't > > know when the generator will be exhausted. As in this case, > where the > > number of files the generator can provide is unknown. Then > we used the > > while True, break on StopIteration method. > > Out of interest, *why* was it deemed necessary to do > something different > if you don't know how many items the generator will generate? Was any > rationale given for that? > > for x in foo: > bar(x) > > ... where foo is any iterable (something that has a __iter__ method > defined - including generators) will just bind each value in > turn to 'x' > and will exit when the StopIteration exception is raised under the > covers by the iterator that is iterating over the iterable. > > Some generators are infinite (and their iterator will never raise a > StopIteration exception). > > E. The main reason you might want to catch the StopIteration exception is to do something else before your code simply stops running. If all you're doing is run a generator til it's out of gas, and that's all you want it to do, then there's no need to catch anything. There's lots of situations where this is exactly what you want. It might even be the most frequent use of generators, though I wouldn't know. From tonycamgar at gmail.com Tue Jan 3 00:15:47 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Mon, 2 Jan 2017 21:15:47 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <586b0517$0$1584$c3e8da3$5496439d@news.astraweb.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <586b0517$0$1584$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2b56d5b8-7cef-4efd-80d9-1d25a50c3f95@googlegroups.com> On Monday, January 2, 2017 at 5:57:51 PM UTC-8, Steve D'Aprano wrote: > On Mon, 2 Jan 2017 10:38 pm, Antonio Caminero Garcia wrote: > > > Hello, I am having a hard time deciding what IDE or IDE-like code editor > > should I use. This can be overwhelming. > > Linux is my IDE. > > https://sanctum.geek.nz/arabesque/series/unix-as-ide/ > > > I dislike the Unix-style Vim/Emacs text editors, I prefer a traditional > GUI-based editor. So my "IDE" is: > > - Firefox, for doing searches and looking up documentation; > > - an GUI programmer's editor, preferably one with a tab-based > interface, such as geany or kate; > > - a tab-based terminal. > > Both geany and kate offer auto-completion based on previously seen words. > They won't auto-complete function or method signatures, but in my my > experience this is the "ninety percent" solution: word-based auto-complete > provides 90% of the auto-complete functionality without the cost of full > signature-based auto-complete. > > In the terminal, I have at least three tabs open: one open to the Python > interactive interpreter, for testing code snippets and help(obj); one where > I run my unit tests ("python -m unittest myproject_tests"); and one where I > do any assorted other tasks, such as file management, checking code into > the repo, etc. > > I've played with mypy a few times, but not used it seriously in any > projects. If I did, I would run that from the command line too, like the > unit tests. Likewise for any linters or equivalent. > > > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, > > IntelliJ with Python plugin. > > > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, > > Pycharm) is that they look like a space craft dashboard and that > > unwarranted resources consumption and the unnecessary icons. > > Indeed. If they provide any useful functionality I don't already have, I've > never come across it. The only thing I'd like to try is an editor that > offers semantic highlighting instead of syntax highlighting: > > https://medium.com/@evnbr/coding-in-color-3a6db2743a1e > > I once tried Spyder as an IDE, and found that it was so bloated and slow it > couldn't even keep up with my typing. I'm not even a touch typist! I'd > start to type a line like: > > except ValueError as err: > > > and by the time my fingers were hitting the colon, Spyder was displaying > `excep` in red flagged with an icon indicating a syntax error. > > > > > -- > Steve > ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure > enough, things got worse. Thanks for remind the Unix capabilities as IDE, that post was cool to read. From cr2001 at hotmail.co.nz Tue Jan 3 00:25:12 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 17:25:12 +1200 Subject: Hey, I'm new to python so don't judge. References: <3844402364@f38.n261.z1.binkp.net> Message-ID: <637094488@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 2:16:08 PM UTC+13, Steve D'Aprano wrote: > On Wed, 4 Jan 2017 12:04 pm, Callum Robinson wrote: > > > Traceback (most recent call last): > > File "D:/Python/random.py", line 6, in > > computer_number = number.randint(1, 100) > > NameError: name 'number' is not defined > > > That's exactly what we need to see! The full traceback, thank you! > > You're asking Python to get the variable "number", and call the randint > method. But: > > - you don't have a variable called "number"; > > NameError: name 'number' is not defined > > > - and even if you did, that's not how you get a random number. What you want > is: > > computer_number = random.randint(1, 100) > > > > > > > -- > Steve > ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure > enough, things got worse. Hey man thanks, the sad thing is i have no idea why i put that in. I must be having a terrible day. From flebber.crue at gmail.com Tue Jan 3 00:35:58 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Tue, 03 Jan 2017 17:35:58 +1200 Subject: Screwing Up looping in Generator References: <3009695678@f38.n261.z1.binkp.net> Message-ID: <393525457@f38.n261.z1.binkp.net> So can I call the generator twice and receive the same file twice in 2 for loops? Once to get the files name and the second to process? for file in rootobs: base = os.path.basename(file.name) write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") with open(write_to, 'w', newline='') as csvf: for file in rootobs: # create and write csv Cheers Sayth From cr2001 at hotmail.co.nz Tue Jan 3 00:37:24 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 17:37:24 +1200 Subject: Hey, I'm new to python so don't judge. References: <1223999572@f38.n261.z1.binkp.net> Message-ID: <4260468770@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson wrote: > Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? > > #mynumber.py > # this game uses a home made function > import random > > #think of a number > computer_number = number.randint(1,100) > > #create the function is_same() > def is_same(target, number: > if target == number: > result="win" > elif target > number: > result="low" > else: > result="high" > return result > > # start the game > print("hello. \nI have thought of a number between 1 and 100.") > > #collect the user's guess as an interger > guess = int(input("Can you guess it? ")) > #Use our function > higher_or_lower = is_same(computer_number, guess) > #run the game untill the user is correct > while higher_or_lower != "win" > if higher_or_lower == "to low" > guess = int(input("Sorry, you are too low. Try again.")) > else: > guess = int(input("Sorry your are too high. Try again.")) > > higher_or_lower = is_same(computer_number, guess) > > #end of game > input("Correct!\nWell Done\n\n\nPress RETURN to exit.") Hey again, i'm sorry for bothering you with so many questions i just did not want to keep asking my teacher who is on holiday these. I have another issue where the code runs but i can guess every number from 1-100 but it always says Sorry your are too high. Try again. I don't understand what i have done to cause this. From tonycamgar at gmail.com Tue Jan 3 00:42:40 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Mon, 2 Jan 2017 21:42:40 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: Guys really thank you for your answers. Basically now I am more emphasizing in learning in depth a tool and get stick to it so I can get a fast workflow. Eventually I will learn Vim and its python developing setup, I know people who have been programming using Vim for almost 20 years and they did not need to change editor (that is really awesome). From flebber.crue at gmail.com Tue Jan 3 00:54:34 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Tue, 03 Jan 2017 17:54:34 +1200 Subject: Screwing Up looping in Generator References: <393525457@f38.n261.z1.binkp.net> Message-ID: <650696286@f38.n261.z1.binkp.net> On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw wrote: > So can I call the generator twice and receive the same file twice in 2 for loops? > > Once to get the files name and the second to process? > > for file in rootobs: > base = os.path.basename(file.name) > write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") > with open(write_to, 'w', newline='') as csvf: > for file in rootobs: > # create and write csv > > Cheers > > Sayth I just need it to write after each file however the with open(#file) as csvf: Keeps it all open until every file processed in an output file with the name of the first file in the generator. Sayth From python at deborahswanson.net Tue Jan 3 01:05:12 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 18:05:12 +1200 Subject: Screwing Up looping in Generator Message-ID: <3212211156@f38.n261.z1.binkp.net> Chris Angelico wrote, on January 03, 2017 3:35 PM > > On Wed, Jan 4, 2017 at 10:05 AM, Deborah Swanson > wrote: > > Ok, I learned how to use generators in Python 2.7.8, which may be > > different from Python 3 for generators. But I learned from MIT's > > online introduction to python course, and they certainly > seem to know > > python well. So what is the correct way to call the > generator's next > > yield in Python 3? We only learned to use the next function. If you > > don't use the next function, what do you use? > > The built-in next function, not the next method. > > # don't do this > gen.next() > > # do this > next(gen) > > ChrisA You speak the truth! I never doubted, but since I still have 2.7.8 on my system I decided to try it out. For a simple little Fibbonacci number generator: def genFib (): fibn_1 = 1 #fib(n-1) fibn_2 = 0 #fib(n-2) while True: # fib(n) = fib(n-1) + fib(n-2) next = fibn_1 + fibn_2 yield next fibn_2 = fibn_1 fibn_1 = next and at the console: >>> fib = genFib() >>> fib.next() 2.7.8 works, and cranks out as many Fibbonacci numbers as you could want. But in 3.4.3 you get: Traceback (most recent call last): File "", line 1, in fib.next() AttributeError: 'generator' object has no attribute 'next' Then, going the other way, next(fib) works in both versions. From flebber.crue at gmail.com Tue Jan 3 01:09:34 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Tue, 03 Jan 2017 18:09:34 +1200 Subject: Screwing Up looping in Generator References: <650696286@f38.n261.z1.binkp.net> Message-ID: <1791039168@f38.n261.z1.binkp.net> Untested as i wrote this in notepad at work but, if i first use the generator to create a set of filenames and then iterate it then call the generator anew to process file may work? Good idea or better available? def get_list_of_names(generator_arg): name_set = set() for name in generator_arg: base = os.path.basename(name.name) filename = os.path.splitext(base)[0] name_set.add(filename) return name_set for file_name in name_set: directory = "output" with open(os.path.join(directory, filename, 'w', newline='') as csvf: for file in rootobs: # create and write csv Sayth From python at deborahswanson.net Tue Jan 3 01:10:16 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 18:10:16 +1200 Subject: Screwing Up looping in Generator Message-ID: <152304910@f38.n261.z1.binkp.net> Sayth Renshaw wrote, on January 03, 2017 5:36 PM > > So can I call the generator twice and receive the same file > twice in 2 for loops? > > Once to get the files name and the second to process? > > for file in rootobs: > base = os.path.basename(file.name) > write_to = os.path.join("output", > os.path.splitext(base)[0] + ".csv") > with open(write_to, 'w', newline='') as csvf: > for file in rootobs: > # create and write csv > > Cheers > > Sayth I don't see why not, if you let the first one run to completion and then do it again a second time. Assuming your generator doesn't somehow delete or modify the file specifications as it yields them. It would be helpful to see the code for rootobs, if you have it. D. From python at deborahswanson.net Tue Jan 3 01:21:58 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 18:21:58 +1200 Subject: Screwing Up looping in Generator Message-ID: <1429338932@f38.n261.z1.binkp.net> Sayth Renshaw wrote, on January 03, 2017 5:55 PM > > On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw wrote: > > So can I call the generator twice and receive the same file > twice in 2 > > for loops? > > > > Once to get the files name and the second to process? > > > > for file in rootobs: > > base = os.path.basename(file.name) > > write_to = os.path.join("output", > os.path.splitext(base)[0] + ".csv") > > with open(write_to, 'w', newline='') as csvf: > > for file in rootobs: > > # create and write csv > > > > Cheers > > > > Sayth > > I just need it to write after each file however the > > with open(#file) as csvf: > > Keeps it all open until every file processed in an output > file with the name of the first file in the generator. > In that case, I think you just need to devise your output file name scheme, and it looks like you want to use 'os.path.splitext(base)[0] + ".csv")'. Then output each file in the same for loop, before you go back for another input file. Just read in the file, do whatever you want to it, and then write it to the new file name, all in the same loop. I don't see any need to loop through rootobs a second time. From torriem at gmail.com Tue Jan 3 01:22:58 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 03 Jan 2017 18:22:58 +1200 Subject: Clickable hyperlinks Message-ID: <1685359403@f38.n261.z1.binkp.net> On 01/03/2017 04:32 PM, Deborah Swanson wrote: > The GUI consoles I have are in Pycharm, the IDLE that comes with > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when > I open them, so they're capable of opening links, but whether that means > their output space is capable of handling clickable links I don't know. Hmm I still don't understand what you mean by "GUI console." Just because a program asks for internet access does not mean anything about whether or not your python program can display a clickable link in a console window. Besides that, a clickable link would probably ask the OS to open it with the default application, not cause your program to access the internet. The standard out pipe (where stuff goes when you call print() or write to sys.stdout) from a python program usually goes to a console or a terminal emulator. PyCharm and IDLE both provide windows to display this output (they emulate a terminal). But maybe you're misunderstanding what this actually is. These windows just display a stream of bytes that come out of your program. Certain escape codes can be emitted that can instruct the console or terminal emulator to do things like set text color or display text at a certain position. But there's certainly no special way to instruct the terminal emulator to make a section of text into a hyperlink. Maybe if hyperlinks had existed years ago when terminal escape codes were being defined we'd have a "hyperlink" code that all consoles and terminals would understand. A few years ago I saw some proposals to add an escape code to the ANSI scheme that would encode hyperlinks, but nothing ever came of it because, honestly, it would be too much hassle to roll this out to ever terminal emulator out there (to say nothing of real terminals). > I do know printing a full url with the %s specifier or entering a url > and clicking enter just gives you the plain text url. Obviously, not all > GUI consoles are enabled recognize and make clickable links from > correctly formatted urls. On my Linux machine, the terminal emulators I've used all make a regular url printed out into a clickable link (or at least a right-clickable link). This is just something they try to do with all things that look like urls. Sometimes it's helpful, often it's annoying. > I was hoping there was some functionality in python to make clickable > links. Could be a package, if the core language doesn't have it. No, there is not. If you made a full GUI app using a toolkit like GTK or Qt, you can indeed place hyperlinks on your forms and the operating system will automatically connect them to the web browser. But not in text-mode terminal apps. From cr2001 at hotmail.co.nz Tue Jan 3 01:24:16 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 18:24:16 +1200 Subject: Hey, I'm new to python so don't judge. References: <3183934424@f38.n261.z1.binkp.net> Message-ID: <802561195@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote: > On 2017-01-04 01:37, Callum Robinson wrote: > > On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson wrote: > >> Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? > >> > >> #mynumber.py > >> # this game uses a home made function > >> import random > >> > >> #think of a number > >> computer_number = number.randint(1,100) > >> > >> #create the function is_same() > >> def is_same(target, number: > >> if target == number: > >> result="win" > >> elif target > number: > >> result="low" > >> else: > >> result="high" > >> return result > >> > >> # start the game > >> print("hello. \nI have thought of a number between 1 and 100.") > >> > >> #collect the user's guess as an interger > >> guess = int(input("Can you guess it? ")) > >> #Use our function > >> higher_or_lower = is_same(computer_number, guess) > >> #run the game untill the user is correct > >> while higher_or_lower != "win" > >> if higher_or_lower == "to low" > >> guess = int(input("Sorry, you are too low. Try again.")) > >> else: > >> guess = int(input("Sorry your are too high. Try again.")) > >> > >> higher_or_lower = is_same(computer_number, guess) > >> > >> #end of game > >> input("Correct!\nWell Done\n\n\nPress RETURN to exit.") > > > > Hey again, i'm sorry for bothering you with so many questions i just did not want to keep asking my teacher who is on holiday these. > > > > I have another issue where the code runs but i can guess every number from 1-100 but it always says Sorry your are too high. Try again. I don't understand what i have done to cause this. > > > What values can 'is_same' return? > > Which of those values are you checking for in the loop? I'm sorry but i do not completely understand what you are stating From cs at zip.com.au Tue Jan 3 01:39:03 2017 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 3 Jan 2017 17:39:03 +1100 Subject: trouble with cmd.Cmd and prompting In-Reply-To: References: Message-ID: <20170103063903.GA63893@cskk.homeip.net> On 03Jan2017 00:14, Dennis Lee Bieber wrote: >On Tue, 3 Jan 2017 11:33:15 +1100, Cameron Simpson >declaimed the following: >>I'm using cmd.Cmd to write a little FTP-like command line to interface to a >>storage system of mine and encountering weird behaviour. When I enter a command >>the next prompt appears _before_ the associated operation runs, or so it >>appears. > >>Has anyone seen this kind of thing before? > > Haven't used the module but there is something I find intriguing in the >help system >-=-=-=-=- > Cmd.precmd(line) > Hook method executed just before the command line is interpreted, but >after the input prompt is generated and issued. >-=-=-=-=- >"... AFTER the input prompt is ... issued" > > I don't know, but that sure sounds to me like the cmd object tends to >process one line behind... Though that behavior is not shown in the turtle >example in the help system. Hmm. Interesting. I had read that text to imply (based on what I imagined _should_ happen) that the flow of control went: cmdloop: issue prompt> line=input() # or readline line=precmd(line) stop=onecmd(line) # which calls do_blah... stop=postcmd(stop,line) if stop: break but your reading of it suggests that this is possible: issue prompt> cmdloop: line=input() # or readline issue prompt> line=precmd(line) stop=onecmd(line) # which calls do_blah... stop=postcmd(stop,line) if stop: break The text for Cmd.cmdloop starts with this: Repeatedly issue a prompt, accept input, parse an initial prefix off the received input, and dispatch to action methods, passing them the remainder of the line as argument. which argues for the former, and was what I naively expected. I guess I'd better dig out the source; I dislike going that far, not merely out of laziness, but also because the source is not the spec. Thanks for the suggestion, Cameron Simpson From python at deborahswanson.net Tue Jan 3 01:40:14 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 18:40:14 +1200 Subject: Screwing Up looping in Generator Message-ID: <796810093@f38.n261.z1.binkp.net> Erik wrote, on January 03, 2017 5:26 PM > Hi, > > On 04/01/17 01:12, Deborah Swanson wrote: > > The main reason you might want to catch the StopIteration > exception is > > to do something else before your code simply stops running. If all > > you're doing is run a generator til it's out of gas, and that's all > > you want it to do, then there's no need to catch anything. > > Ah! OK, I see where the lines are being crossed now ;) Although > StopIteration is an exception, it is something that the 'for/iter' > machinery handles for you under the covers. Each 'for' statement > effectively has a 'try' block around it that catches > 'StopIteration' and > just terminates that particular 'for' loop and continues on with the > remainder of your script. > > Raising a 'StopIteration' is an internal mechanism used to determine > when an iterator (i.e., the thing a 'for' loop is looping over) has > exhausted itself. It's not something a regular user is ever > expected to > know about let alone catch. > > When execution falls out of the bottom of a generator, > StopIteration is > raise (compared to a regular function or method returning 'None'). > > E. Looks like those MIT professors knew what they were teaching us after all! Sneaky, but they never once hinted that this was any deep dark secret. Just a way to deal with generators that will stop after a finite and unknown number of yields, and you want your code to keep going after the generator quits. I thought I was just regurgitating a standard approach, and surprised to get so much push back on it. Seems like python uses a lot of its external functionality (what we normally code with) for internal behavior too. In this case, and for empty returns, they're raising exceptions, catching them and then doing what they want to. I suppose, if you don't want the method returning None, you could catch that exception and return something else. Or return nothing at all, which is what I usually want to do when those pesky Nones crop up. But I'm not entirely sure how you would make it return nothing at all. From cr2001 at hotmail.co.nz Tue Jan 3 01:47:42 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 03 Jan 2017 18:47:42 +1200 Subject: Hey, I'm new to python so don't judge. References: <595817138@f38.n261.z1.binkp.net> Message-ID: <1642603984@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 3:35:53 PM UTC+13, Erik wrote: > On 04/01/17 02:24, Callum Robinson wrote: > > On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote: > >> What values can 'is_same' return? > >> > >> Which of those values are you checking for in the loop? > > > > I'm sorry but i do not completely understand what you are stating > > You need to think about the specific things (their type, their exact > values) that your functions might return. Printing some trace output is > a classic way of debugging your program. If, after this line: > > >>>> higher_or_lower = is_same(computer_number, guess) > > ... you added: > > print (higher_or_lower) > > ... what values do you then see being output? How will those values be > processed by the conditions you see that work on the "higher_or_lower" > variable? > > E. I did it and this is what it states when i run it hello. I have thought of a number between 1 and 100. Can you guess it? 5 Low Sorry , you are too high. Try again. Does this mean the number i entered is to low but the code is still stating it is to high? From rustompmody at gmail.com Tue Jan 3 01:48:51 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Mon, 2 Jan 2017 22:48:51 -0800 (PST) Subject: learning and experimenting python. In-Reply-To: References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> <586B27B5.9040107@stoneleaf.us> Message-ID: <72177ffd-b44b-484a-959e-722c79f6706f@googlegroups.com> On Tuesday, January 3, 2017 at 9:55:35 AM UTC+5:30, Ethan Furman wrote: > On 01/02/2017 09:53 AM, Wildman via Python-list wrote: > > [rude ascii art omitted] > > That is a completely inappropriate response. > > -- > ~Ethan~ Besides 1. Without fix-pitch the ASCII art is undecipherable. [I assume the person in question posted from google groups so most likely he sees it in proportional] 2. He seems to have deleted his posts from GG https://groups.google.com/forum/#!topic/comp.lang.python/KLdkUGOV5lU From python at deborahswanson.net Tue Jan 3 02:04:36 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 19:04:36 +1200 Subject: Clickable hyperlinks Message-ID: <1619621956@f38.n261.z1.binkp.net> Steve D'Aprano wrote, on January 03, 2017 4:56 PM > On Wed, 4 Jan 2017 10:32 am, Deborah Swanson wrote: > > > > The GUI consoles I have are in Pycharm, the IDLE that comes with > > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access > > when I open them, so they're capable of opening links, but whether > > that means their output space is capable of handling > clickable links I > > don't know. > > > > I do know printing a full url with the %s specifier or > entering a url > > and clicking enter just gives you the plain text url. > Obviously, not > > all GUI consoles are enabled recognize and make clickable > links from > > correctly formatted urls. > > > > I was hoping there was some functionality in python to make > clickable > > links. Could be a package, if the core language doesn't have it. > > Unfortunately there is no such thing as an application- > independent "clickable link". I'm getting that. > Excel can make clickable links, because it only has to deal > with a single suite of related applications: Excel, Word, and > the rest of Microsoft Office. Likewise LibreOffice. > > But Python has to deal with an infinite number of potential > or hypothetical consoles, and there is no standard for > "clickable links" that all, or even most, consoles understand. > > Python can't force the console to treat something as a > clickable link, if the console has no capacity for clickable > links. Nor can Python predict what format the console uses to > recognise a link. > > The best you can do is to experiment with a couple of simple > formats and see which, if any, your console understands: > > # HTML > Example. > > # URL in angle brackets > Example > > # URL alone > http://www.example.com > > # I can't remember what these are called > > # Markup > [Example](http://www.example.com) > > # Rest > `Example `_ > > > > > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered > up, and sure enough, things got worse. I tried all of your examples in IDLE, and they all get syntax errors. I'd expect the same from PyCharm, though I didn't try it. I think I need to write a class to do it in a particular application, preferably in PyCharm, though I'd need some kind of internet access engine to do it anywhere. I can look up how Firefox does it, pretty sure I have that somewhere. Maybe there's a way... Maybe it's a stupid idea, but originally I wanted to output links and click on them while I was still debugging in PyCharm, without having to save the csv and reopen it in Excel, to see what was in a listing* while I was still debugging. PyCharm does have links in its console output that take you to positions in the code when you click on them, so it seems like all the basic infrastructure is there. I just have to figure out how to do it for internet urls that I output. * listing is a real estate listing with a url, part of a project I'm working on, in case you haven't read the "Cleaning up conditionals" thread. From python at deborahswanson.net Tue Jan 3 02:35:31 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 2 Jan 2017 23:35:31 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <000401d26593$f7056710$27b23dae@sambora> Gregory Ewing wrote, on January 02, 2017 7:58 PM > > Deborah Swanson wrote: > > Unless you know of, and are suggesting, a way to index a > sequence with > > strings instead of integers, so the code could remain > untouched with > > string indices when changes to the columns are made. > > I'm talking about this: > >https://docs.python.org/3/library/collections.html#collections.namedtup le >It's like a tuple, but the fields also have names, so >you can access them like attributes using dot-notation. > Also, for such a scheme to be superior to what I already have, it > needs to be very nearly self-maintaining. >If you derive the attribute names from the header row, >it would be extremely self-maintaining. You would be able >to reorder columns without touching the code at all. >-- >Greg I've never worked with collections before, and you're the second person on this thread to suggest using them. I'm very intrigued by both your use of collections and Peter Otten's. I think I'm right that core python sequences can't be indexed in any fashion by strings, because strings aren't iterable. I suppose it might be possible for strings to be iterable in some sort of ascii char code order, but that seems like it could get real whacky very fast, and not terribly useful. But a library of functions designed to augment core python certainly would be capable of many things. So I will be checking out collections and your suggested use of namedtuples very soon. I may not need to fix this part of my code, but I would like to learn a new way to do it. And it would be nice to replace the long string of 2-letter variables I currently have with a data structure. They're not really in the way, but they do clutter up the variable list in PyCharm. And using the header row titles as indices would automatically make my code a lot more readable. Thanks! ;) D. From python at deborahswanson.net Tue Jan 3 02:46:44 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 19:46:44 +1200 Subject: Clickable hyperlinks Message-ID: <3686502656@f38.n261.z1.binkp.net> David wrote, on January 03, 2017 6:36 PM > > On 4 January 2017 at 11:50, Deborah Swanson > wrote: > > Erik wrote, on January 03, 2017 3:30 PM > >> > >> When you start a new topic on the list, could you please > write a new > >> message rather than replying to an existing message and > changing the > >> title/subject? > >> > > Certainly. I've been on many other lists before (but none > since about > > 2011), and no one complained of or even mentioned this > problem. But if > > it's a problem with some email readers now, I can easily > just start a > > new message. Just being lazy and following old habits, I guess. ;) > > To be clear, the problem is not "with some email readers". Actually it is, or at least it doesn't happen in all email readers. Mine, for instance, never breaks up threads. > The problem is that it breaks the thread management features > that are built into every email message, making it impossible > to display threads correctly. Again, I think this is in some modern email readers. Apparently older ones were more robust. > As can be seen here for example: > https://mail.python.org/pipermail/python-> list/2017-January/thread.html > > Note how most threads start in the leftmost column and > replies are indented. But this does not occur where you began > the new subject: "Clickable hyperlinks". Yes, pipermail does seem to have a few bugs, this is the second one I've seen. > Thanks. I did say in the message you're replying to that I will try to remember to start new threads with brand new messages. (Even though I think pipermail's behavior is a bug, that's what many people read the list from.) From wlfraed at ix.netcom.com Tue Jan 3 02:59:08 2017 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Tue, 03 Jan 2017 19:59:08 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <2068038870@f38.n261.z1.binkp.net> On Tue, 3 Jan 2017 16:02:15 -0800 (PST), Callum Robinson declaimed the following: >When i check the code it comes up with invalid syntax and my writing line gets re directed here > >def is_same(target, number: > if target == number: > result="win" > elif target > number: > result="low" > else: > result="high" > return result Count your parentheses... You should have the same number of ) as you have ( -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ From tjreedy at udel.edu Tue Jan 3 03:06:18 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 03 Jan 2017 20:06:18 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <105591444@f38.n261.z1.binkp.net> On 1/3/2017 7:02 PM, Callum Robinson wrote: > When i check the code it comes up with invalid syntax and my writing line gets re directed here > > def is_same(target, number: > if target == number: > result="win" > elif target > number: > result="low" > else: > result="high" > return result When I paste this into a file in IDLE and try to run the file, 'if' is highlighted in red. This means that there is an error somewhere at or before the 'if'. -- Terry Jan Reedy From rosuav at gmail.com Tue Jan 3 03:14:10 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 3 Jan 2017 19:14:10 +1100 Subject: Cleaning up conditionals In-Reply-To: <000401d26593$f7056710$27b23dae@sambora> References: <000401d26593$f7056710$27b23dae@sambora> Message-ID: On Tue, Jan 3, 2017 at 6:35 PM, Deborah Swanson wrote: > I think I'm right that core python sequences can't be indexed in any > fashion by strings, because strings aren't iterable. I suppose it might > be possible for strings to be iterable in some sort of ascii char code > order, but that seems like it could get real whacky very fast, and not > terribly useful. It's not because they're not iterable, but because there's a fundamental difference between a key-value pair (dict) and a sequence. You can combine the two in several different ways, none of which is the "one most obvious". Python's OrderedDict retains the order of its keys; in contrast, a namedtuple is a "record" type with a fixed set of keys that correspond to positions in the record. In your case, I think a namedtuple would be a good fit, but there's no general concept of indexing a sequence with strings. ChrisA From python at deborahswanson.net Tue Jan 3 03:46:30 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 03 Jan 2017 20:46:30 +1200 Subject: Clickable hyperlinks Message-ID: <973176847@f38.n261.z1.binkp.net> Steven D'Aprano wrote, on January 03, 2017 8:04 PM > > On Wednesday 04 January 2017 14:04, Deborah Swanson wrote: > > > Steve D'Aprano wrote, on January 03, 2017 4:56 PM > [...] > >> Python can't force the console to treat something as a clickable > >> link, if the console has no capacity for clickable links. Nor can > >> Python predict what format the console uses to recognise a link. > >> > >> The best you can do is to experiment with a couple of > simple formats > >> and see which, if any, your console understands: > >> > >> # HTML > >> Example. > >> > >> # URL in angle brackets > >> Example > >> > >> # URL alone > >> http://www.example.com > >> > >> # I can't remember what these are called > >> > >> > >> # Markup > >> [Example](http://www.example.com) > >> > >> # Rest > >> `Example `_ > [...] > > > I tried all of your examples in IDLE, and they all get > syntax errors. > > I'd expect the same from PyCharm, though I didn't try it. > > Syntax errors? How can you get syntax errors from *output* text? > > The above are just text, no different from: > > Hello World! I closed the IDLE window these were on, but the error arrow was pointing to punctuation in each case, a colon in one case, angle bracket in another. Sorta seems like IDLE is trying to do something with them and not taking them as simple plain text. But it's not making hyperlinks, so I'm not sure how much I care exactly what it's doing. > Of course you have to put quotes around them to enter them in > your source code. > We don't expect this to work: > > print(Hello World!) > > > you have to use a string literal with quotes: > > print('Hello World!') > > > Same for all of the above. I didn't try printing them before, but I just did. Got: >>> print([Example](http://www.example.com) SyntaxError: invalid syntax (arrow pointing at the colon) > py> print('Example.') > Example. > > > That's the Python interactive interpreter (with a custom > prompt, I prefer "py>" > rather than the default ">>>"). Or I can do this from the > operating system's > shell prompt: > > steve at runes:~$ python -c "print 'http://www.example.com'" http://www.example.com If I do this in GNOME Terminal 2.30.2, the URL http... is a clickable link. But that's specific to the terminal. Other terminals may or may not recognise it. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson I need to get a new power supply in my Linux-capable machine, but for now I'm stuck with Winblows on a very old PC. As I've mentioned in other posts on this thread, I'm now thinking that I need to write a class to do this, and find out how Firefox and url aware terminals in Linux do it. There must be a way. From torriem at gmail.com Tue Jan 3 03:51:36 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 03 Jan 2017 20:51:36 +1200 Subject: Clickable hyperlinks Message-ID: <231289290@f38.n261.z1.binkp.net> On 01/03/2017 08:28 PM, Deborah Swanson wrote: > I think you're making this too complicated. I meant a console in a GUI > application. Ahh. Well, a "console in a GUI application" is whatever you make it[1]. There's no single "GUI console" hence my confusion and the confusion expressed by the other poster. I was under the impression you are talking about printing something to standard out with, for example, print(). Is this so, or are you using a GUI toolkit to construct your application. What GUI toolkit are you using? As I said, in Qt or GTK there are various ways to display hyperlinks. For example, Qt lets you place a hyperlink in a form, or inside of a text entry/display widget. I still get the impression that you're working with standard out, using print(). If so, then no, there's not going to be a way to force the OS to make the output clickable, at least on Windows. > Not true. Pycharm uses links in it's console output, they just aren't > internet links. They link back to lines of code being referred to. I think the problem here is the terminology with specific meaning in Windows and Linux. I'm referring to either the Win32 console window (which is where cmd.exe runs), or a terminal emulator in Linux, which is where you can interact with the bash shell and run command-line programs. When people normally run python apps that are not graphical, they normally do it from the Windows console (via cmd.exe) or in Linux from Bash running in a terminal emulator. Graphical apps do their own thing as far as displaying data and making windows with clickable links in them. [1] PyCharm and IDLE make "console" windows that are really normal GUI windows and they direct the output from Python apps there. They may also choose to display clickable links for things like errors. But once the app is run outside of PyCharm, the output of the app would go to either a Windows console window, or a terminal in Linux. If you wanted your app to make it's own window and display clickable links, you're back to looking at a GUI toolkit (which is what PyCharm and IDLE are built with) like Qt, GTK, Tk, wxWidgets, or something else. From torriem at gmail.com Tue Jan 3 04:05:18 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 03 Jan 2017 21:05:18 +1200 Subject: Clickable hyperlinks Message-ID: <1957399330@f38.n261.z1.binkp.net> On 01/03/2017 08:46 PM, Deborah Swanson wrote: > Actually it is, or at least it doesn't happen in all email readers. > Mine, for instance, never breaks up threads. Mine doesn't either, which illustrates the issue. This message, for example appears under a long thread that started out life as "mentor training python Romania with certification" and then changed to "Cleaning up conditionals" and then changed to "Clickable hyperlinks." All in one thread. My client doesn't break them up because they all tie together via the message-id header. And most of us would not like our client to break a thread just because the subject changes. Often in long conversations there are twists and turns in the discussion and sometimes side-paths are explored, and the subject often is changed to reflect this. With a truly threaded email reader (one that shows a tree of messages, not just chronological order), this works out very well. So if a discussion has a natural evolution into various other topics, it is often considered okay to just change the subject but continue the thread. Other times, it's better to start a new thread. Where that line is is hard to say! > I did say in the message you're replying to that I will try to remember > to start new threads with brand new messages. (Even though I think > pipermail's behavior is a bug, that's what many people read the list > from.) Sounds good. I don't know of anyone that reads on the pipermail archive, except in response to web searches. Most people use clients of some kind, NNTP or email. And those that group messages according to message-id (most clients except for ones that try to be smart like Gmail web or Outlook) will show all the topics I mentioned before as one giant thread, which is by design (that's what message-id headers are for). From python at deborahswanson.net Tue Jan 3 04:18:26 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 01:18:26 -0800 Subject: Cleaning up conditionals In-Reply-To: Message-ID: <000f01d265a2$58022a90$27b23dae@sambora> Chris Angelico wrote, on January 03, 2017 12:14 AM > > On Tue, Jan 3, 2017 at 6:35 PM, Deborah Swanson > wrote: > > I think I'm right that core python sequences can't be > indexed in any > > fashion by strings, because strings aren't iterable. I suppose it > > might be possible for strings to be iterable in some sort of ascii > > char code order, but that seems like it could get real whacky very > > fast, and not terribly useful. > > It's not because they're not iterable, but because there's a > fundamental difference between a key-value pair (dict) and a > sequence. You can combine the two in several different ways, > none of which is the "one most obvious". Python's OrderedDict > retains the order of its keys; in contrast, a namedtuple is a > "record" type with a fixed set of keys that correspond to > positions in the record. In your case, I think a namedtuple > would be a good fit, but there's no general concept of > indexing a sequence with strings. > > ChrisA I hope I can get to namedtuples tomorrow, they do sound like an excellent data structure for the purpose of using field titles as subscripts. I'd never heard of them before, so I'm looking forward to finding out what they are and how they work. OrderedDict is the new development I'd read about, but I think the key order is just the order keys were added to the dict. The nice thing about that though is when you loop over a dict you will always get the key/value pairs in the same order, where with the standard dict they come out in arbitrary order. Very disconcerting if you're watching the loop as it progresses over muiltiple executions. I imagine you could sort an OrderedDict so the keys originally added randomly would be put into some kind of good order. From D.Strohl at F5.com Tue Jan 3 04:34:56 2017 From: D.Strohl at F5.com (Dan Strohl) Date: Tue, 03 Jan 2017 21:34:56 +1200 Subject: Clickable hyperlinks Message-ID: <2213819274@f38.n261.z1.binkp.net> The best bet (unless you know that you are outputting to a specific place, like html or excel) is to always include the "https://" or "http://" since most of the consoles / terminals that support clickable links are parsing them based on "seeing" the initial "http://". If your output just looks like "mysite.com/coolpage.html", many systems will simply ignore them. At one point I had made a class that you could pass the link to, and then you could request different output based on your needs... so basically something like: >> url = url_class("mysite.com/coolpage.html") >> print(url) "http://mysite.com/coolpage.html") >> print(url.plain) "mysite.com/coolpage.html" >> print(url.html('My Site")) 'My Site' (or whatever... I think I actually just sub-classed url.parse or something) -----Original Message----- From: Python-list [mailto:python-list-bounces+d.strohl=f5.com at python.org] On Behalf Of Devin Jeanpierre Sent: Tuesday, January 03, 2017 12:57 PM To: python at deborahswanson.net Cc: comp.lang.python Subject: Re: Clickable hyperlinks Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to make an URL clickable is to use a terminal that makes URLs clickable, and print the URL: print("%s: %s" % (description, url)) -- Devin On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson wrote: > Excel has a formula: > > =HYPERLINK(url,description) > > that will put a clickable link into a cell. > > Does python have an equivalent function? Probably the most common use > for it would be output to the console, similar to a print statement, > but clickable. > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list From m at funkyhat.org Tue Jan 3 04:46:56 2017 From: m at funkyhat.org (Matt Wheeler) Date: Tue, 03 Jan 2017 21:46:56 +1200 Subject: Screwing Up looping in Generator Message-ID: <2039106472@f38.n261.z1.binkp.net> On Tue, 3 Jan 2017 at 20:17 Deborah Swanson wrote: > > What's the code for your generator? And I don't see where you > > call 'next'. > > I think you're expecting > > for file in rootobs > > to get the next yield for you from rootobs, but unless someone corrects > me, I don't think you can expect a 'for' statement to do that. You need > to have a 'next' statement inside your for loop to get the next yield > from the generator. > Yes, you absolutely can expect a for statement to do that. It will accept any iterable as its `y`. `for x in y: stuff(x)` is effectively syntactic sugar for: iterator = iter(y) while True: try: x = next(iterator) except(StopIteration): break stuff(x) Manually calling `next()` inside a `while True:` is quite an unusual thing to do. range() is not part of the for syntax at all, it's completely separate, it simply returns an iterator which the for loop can use, like any other. -- -- Matt Wheeler http://funkyh.at From D.Strohl at F5.com Tue Jan 3 04:47:58 2017 From: D.Strohl at F5.com (Dan Strohl) Date: Tue, 03 Jan 2017 21:47:58 +1200 Subject: Clickable hyperlinks Message-ID: <8394371@f38.n261.z1.binkp.net> Keeping mind how this all works... Python is providing the data, the console/terminal/app handles how that data is displayed. There is no specification for text output to be hyperlinked (that I know about at least), so while some apps may handle specific coding to tell them that "this text should be a hyperlink", most will either parse the text looking for hyper-linkable string, or more commonly, just output the text as text. If you control the app that is displaying info to the user (such as using QT or another GUI tool), or generating html yourself, you can control if a text string is a hyperlink or not, and what to do with the hyperlink when clicked. So, if you want clickable links, you would need to find a console/terminal that supported them. Some references: https://opensource.com/life/15/11/top-open-source-terminal-emulators (see Gnome) http://unix.stackexchange.com/questions/63417/is-there-a-terminal-app-that-allo ws-filenames-to-be-clickable http://superuser.com/questions/654116/configure-os-x-terminal-to-detect-urls-an d-make-them-clickable http://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/url-launching.html -----Original Message----- From: Python-list [mailto:python-list-bounces+d.strohl=f5.com at python.org] On Behalf Of Deborah Swanson Sent: Tuesday, January 03, 2017 1:35 PM To: 'Devin Jeanpierre' Cc: 'comp.lang.python' Subject: RE: Re: Clickable hyperlinks Devin Jeanpierre wrote, on January 03, 2017 12:57 PM >Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to >make an URL clickable is to use a terminal that makes URLs clickable, and >print the URL: > > >print("%s: %s" % (description, url)) > > > > >-- Devin I'm sorry, I should have said a GUI console because I wouldn't expect a text-based console to produce clickable links. But it appears that a simple GUI console can't do it either. I have 3 GUI consoles and in all 3, when I ask: print("%s: %s" % ("python.org list", "https://mail.python.org/mailman/listinfo/python-list")) I get: python.org list: https://mail.python.org/mailman/listinfo/python-list (Outlook made the url clickable here, the python GUI consoles just output plain text.) Pretty clearly the output is plain text because your format parameters are %s. Maybe the trick, if there is one, is in a format parameter for urls. On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson wrote: Excel has a formula: =HYPERLINK(url,description) that will put a clickable link into a cell. Does python have an equivalent function? Probably the most common use for it would be output to the console, similar to a print statement, but clickable. -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list From m at funkyhat.org Tue Jan 3 04:49:22 2017 From: m at funkyhat.org (Matt Wheeler) Date: Tue, 03 Jan 2017 21:49:22 +1200 Subject: Screwing Up looping in Generator Message-ID: <3875811198@f38.n261.z1.binkp.net> On Tue, 3 Jan 2017 at 21:46 Matt Wheeler wrote: > range() is not part of the for syntax at all, it's completely separate, it > simply returns an iterator which the for loop can use, like any other. > *iterable -- -- Matt Wheeler http://funkyh.at From rustompmody at gmail.com Tue Jan 3 04:57:08 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Tue, 03 Jan 2017 21:57:08 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I References: <1794692783@f38.n261.z1.binkp.net> Message-ID: <498925202@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 5:42:34 AM UTC+5:30, Dietmar Schwertberger wrote: > On 02.01.2017 12:38, Antonio Caminero Garcia wrote: > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. > You did not try Wing IDE? It looks less like a spacecraft. Maybe you > like it. > Maybe the difference is that Wing is from Python people while the ones > you listed are from Java people. > For something completely different (microcontroller programming in C) I > just switched to a Eclipse derived IDE and I don't like it too much as > the tool does not focus on the problem scope. > > From your posts I'm not sure whether you want an editor or an IDE, > where for me the main difference is the debugger and code completion. > I would not want to miss the IDE features any more, even though in my > first 15 years of Python I thought that a debugger is optional with > Python ... > > Regards, > > Dietmar Im surprised no one's talked of idle (or Ive missed it?) From rosuav at gmail.com Tue Jan 3 04:58:08 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 3 Jan 2017 20:58:08 +1100 Subject: Cleaning up conditionals In-Reply-To: <000f01d265a2$58022a90$27b23dae@sambora> References: <000f01d265a2$58022a90$27b23dae@sambora> Message-ID: On Tue, Jan 3, 2017 at 8:18 PM, Deborah Swanson wrote: > OrderedDict is the new development I'd read about, but I think the key > order is just the order keys were added to the dict. The nice thing > about that though is when you loop over a dict you will always get the > key/value pairs in the same order, where with the standard dict they > come out in arbitrary order. Very disconcerting if you're watching the > loop as it progresses over muiltiple executions. I imagine you could > sort an OrderedDict so the keys originally added randomly would be put > into some kind of good order. If you want a "SortedDict", you can get that fairly easily just by iterating over sorted(d) rather than simply d. You can also subclass the dictionary and change how it iterates, if you like. ChrisA From uri at speedy.net Tue Jan 3 05:07:38 2017 From: uri at speedy.net (Uri Even-Chen) Date: Tue, 03 Jan 2017 22:07:38 +1200 Subject: pip install -r requirements.txt fails with Python 3.6 on Windows Message-ID: <356961045@f38.n261.z1.binkp.net> Thank you, I'll consider to update our requirements to latest versions of all packages. Last time I checked in 22th December 2016 and all our requirements were the latest versions. In the meantime we can keep using Python 3.5. By the way, Travis CI tests passed with the same requirements and Python 3.6 (and 3.5 and 3.4). How did it install the requirements there? Does it depend on the operating system? I see now that Python 3.6.0 was released on 2016-12-23. By the way we use Ubuntu 16.04 in production with Python 3.5.2, so it's not that important to support Python 3.6 right now. What are the reasons to upgrade Python to 3.6? Thanks, Uri. *Uri Even-Chen* [image: photo] Phone: +972-54-3995700 Email: uri at speedy.net Website: http://www.speedysoftware.com/uri/en/ From wlfraed at ix.netcom.com Tue Jan 3 05:15:02 2017 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Tue, 03 Jan 2017 22:15:02 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <1104592092@f38.n261.z1.binkp.net> On Tue, 3 Jan 2017 16:27:33 -0800 (PST), Callum Robinson declaimed the following: >On Wednesday, January 4, 2017 at 1:17:11 PM UTC+13, Chris Angelico wrote: >> On Wed, Jan 4, 2017 at 11:03 AM, Erik wrote: >> > I doubt it's getting that far (I can see at least one syntax error in the >> > code pasted). >> >> True true. In any case, the point is to copy and paste the error >> message. Callum, please, copy and paste it. >> >> ChrisA > >I'm sorry if I'm doing something wrong but all that is happening is when i try to run it a popup says Invalid syntax And that statement tells us you are trying to run from within some IDE/editor which is trapping Python exceptions and producing a dialog box for them. Instead, save your script (if you haven't yet) as a file (whatever.py). Open a command line interpreter/shell. Navigate (cd ...) to where you saved the file Type "python whatever.py" Copy and paste the results of the CLI/Shell window. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ From orgnut at yahoo.com Tue Jan 3 05:27:10 2017 From: orgnut at yahoo.com (Larry Hudson) Date: Tue, 03 Jan 2017 22:27:10 +1200 Subject: Hey, I'm new to python so don't judge. References: <306187502@f38.n261.z1.binkp.net> Message-ID: <3188129962@f38.n261.z1.binkp.net> On 01/03/2017 04:27 PM, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 1:17:11 PM UTC+13, Chris Angelico wrote: >> On Wed, Jan 4, 2017 at 11:03 AM, Erik wrote: >>> I doubt it's getting that far (I can see at least one syntax error in the >>> code pasted). >> >> True true. In any case, the point is to copy and paste the error >> message. Callum, please, copy and paste it. >> >> ChrisA > > I'm sorry if I'm doing something wrong but all that is happening is when i try to run it a popup says Invalid syntax > Exactly HOW are you running this? If you are getting a popup, I suspect you are using an on-line version in a browser. To get the proper Python error messages (called Tracebacks) you MUST run the program in a terminal on your own computer. These tracebacks are VERY informative (once you get used to them). :-) And these tracebacks are what WE need to see to help you. You DO have Python installed, don't you? -- -=- Larry -=- From wlfraed at ix.netcom.com Tue Jan 3 05:31:22 2017 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Tue, 03 Jan 2017 22:31:22 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <1440717869@f38.n261.z1.binkp.net> On Tue, 3 Jan 2017 18:47:43 -0800 (PST), Callum Robinson declaimed the following: > >hello. >I have thought of a number between 1 and 100. >Can you guess it? >5 >Low >Sorry , you are too high. Try again. > >Does this mean the number i entered is to low but the code is still stating it is to high? Is that a cut&paste from some console window, or are you just retyping what you think it did? Nothing in your previous code produces the word "Low" -- it will produce "low" (notice the case). And where in your code do you test for that return word? To simplify things -- don't return words from your function... change it to return +1 for high, -1 for low, and 0 for "win". Then figure out how to modify the main loop to use those integers... -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ From tonycamgar at gmail.com Tue Jan 3 05:54:10 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Tue, 03 Jan 2017 22:54:10 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I References: <1794692783@f38.n261.z1.binkp.net> Message-ID: <694630219@f38.n261.z1.binkp.net> On Tuesday, January 3, 2017 at 4:12:34 PM UTC-8, Dietmar Schwertberger wrote: > On 02.01.2017 12:38, Antonio Caminero Garcia wrote: > You did not try Wing IDE? It looks less like a spacecraft. Maybe you > like it. > Maybe the difference is that Wing is from Python people while the ones > you listed are from Java people. That sounds interesting. By the look of it I think I am going to give it a try. > For something completely different (microcontroller programming in C) I > just switched to a Eclipse derived IDE and I don't like it too much as > the tool does not focus on the problem scope. If it happens to be Arduino I normally use a sublime plugin called Stino https://github.com/Robot-Will/Stino (1337 people starred that cool number :D) > From your posts I'm not sure whether you want an editor or an IDE, > where for me the main difference is the debugger and code completion. I want editor with those IDE capabilities and git integration, with optionally cool stuff as for example remote debugging. > I would not want to miss the IDE features any more, even though in my > first 15 years of Python I thought that a debugger is optional with > Python ... Unfortunately most of the time I am still using print and input functions. I know that sucks, I did not use the pdb module, I guess that IDE debuggers leverage such module. > Regards, > > Dietmar Thank you so much for your answer. From grant.b.edwards at gmail.com Tue Jan 3 06:13:10 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 03 Jan 2017 23:13:10 +1200 Subject: Clickable hyperlinks Message-ID: <2633620841@f38.n261.z1.binkp.net> On 2017-01-03, Deborah Swanson wrote: > I'm sorry, I should have said a GUI console because I wouldn't expect a > text-based console to produce clickable links. What's a "GUI console"? -- Grant Edwards grant.b.edwards Yow! I want you to MEMORIZE at the collected poems of gmail.com EDNA ST VINCENT MILLAY ... BACKWARDS!! From robertvstepp at gmail.com Tue Jan 3 06:18:16 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 03 Jan 2017 23:18:16 +1200 Subject: Clickable hyperlinks Message-ID: <533172374@f38.n261.z1.binkp.net> On Tue, Jan 3, 2017 at 10:46 PM, Deborah Swanson wrote: > > > I didn't try printing them before, but I just did. Got: > > >>> print([Example](http://www.example.com) > > SyntaxError: invalid syntax (arrow pointing at the colon) As Steve had said, you need to put everything inside quotes. Also, you are missing a matching paren. Thus: py3: print("[Example](http://www.example.com)") [Example](http://www.example.com) As to whether anything will be "clickable" or not, what has already been said about different types of terminals applies. -- boB From maillist at schwertberger.de Tue Jan 3 06:20:36 2017 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Tue, 03 Jan 2017 23:20:36 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I Message-ID: <1794692783@f38.n261.z1.binkp.net> On 02.01.2017 12:38, Antonio Caminero Garcia wrote: > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. You did not try Wing IDE? It looks less like a spacecraft. Maybe you like it. Maybe the difference is that Wing is from Python people while the ones you listed are from Java people. For something completely different (microcontroller programming in C) I just switched to a Eclipse derived IDE and I don't like it too much as the tool does not focus on the problem scope. From your posts I'm not sure whether you want an editor or an IDE, where for me the main difference is the debugger and code completion. I would not want to miss the IDE features any more, even though in my first 15 years of Python I thought that a debugger is optional with Python ... Regards, Dietmar From python at lucidity.plus.com Tue Jan 3 06:30:26 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 03 Jan 2017 23:30:26 +1200 Subject: Clickable hyperlinks Message-ID: <1719453483@f38.n261.z1.binkp.net> Hi. On 03/01/17 19:46, Deborah Swanson wrote: > Excel has a formula: When you start a new topic on the list, could you please write a new message rather than replying to an existing message and changing the title/subject? For those reading the list in a threaded email client, this message is shown as a continuation of your "Cleaning up conditionals" thread, and that whole thread in turn shows up as a continuation of the "mentor training python Romania with certification" discussion (which you had presumably "reply"ed to originally) ... Thanks. E. From python at lucidity.plus.com Tue Jan 3 06:44:50 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 03 Jan 2017 23:44:50 +1200 Subject: Screwing Up looping in Generator Message-ID: <1135185308@f38.n261.z1.binkp.net> Hi, On 03/01/17 22:14, Deborah Swanson wrote: > ...you have to create the generator object first and use it to call the > next function. And I really don't think you can use a generator as your > range in a for loop. So I'd use a 'while True', and break out of the > loop when you hit the StopIteration exception: > > files = rootobs() > > while True: > try: > file = files.next() > except StopIteration: > break > > base = os.path.basename(file.name) > . > . > . > (etc) What you have done there is taken an understanding of the underlying machinery that allows the 'for' loop to be syntactic sugar over any iterable and spelled it out, instead of just using 'for'! Without all that, your example is: for file in rootobs(): base = os.path.basename(file.name) . . . (etc) [In fact, the machinery would also cope with the return value from rootobs() being an iterable but not an iterator by using "files = iter(rootobjs)"]. You seem to be reading up on how the stuff works under the covers (i.e., from the point of view of an implementer of a class or library) and then suggesting that that's what the *caller* of that class or library needs to do. They don't - for a caller, 'for x in seq:' is all they need to know - the mechanics are handled by the interpreter coupled with the dunder methods that the class may implement. E. From python at lucidity.plus.com Tue Jan 3 06:53:16 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 03 Jan 2017 23:53:16 +1200 Subject: Screwing Up looping in Generator Message-ID: <2712032730@f38.n261.z1.binkp.net> On 03/01/17 23:05, Deborah Swanson wrote: > And yes, we usually used for loops for generators, unless you don't know > when the generator will be exhausted. As in this case, where the number > of files the generator can provide is unknown. Then we used the while > True, break on StopIteration method. Out of interest, *why* was it deemed necessary to do something different if you don't know how many items the generator will generate? Was any rationale given for that? for x in foo: bar(x) ... where foo is any iterable (something that has a __iter__ method defined - including generators) will just bind each value in turn to 'x' and will exit when the StopIteration exception is raised under the covers by the iterator that is iterating over the iterable. Some generators are infinite (and their iterator will never raise a StopIteration exception). E. From python at lucidity.plus.com Tue Jan 3 07:03:02 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 00:03:02 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <731385204@f38.n261.z1.binkp.net> On 03/01/17 23:56, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 10:49 AM, wrote: >> #think of a number >> computer_number = number.randint(1,100) > > What's wrong is that you aren't showing us the exception you get on > this line. *Copy and paste* that exception - the whole thing. It's > very helpful. I doubt it's getting that far (I can see at least one syntax error in the code pasted). cr2001: I echo Chris's sentiment though - what is the error you are seeing (in it's entirety)? E. From wlfraed at ix.netcom.com Tue Jan 3 07:17:52 2017 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Wed, 04 Jan 2017 00:17:52 +1200 Subject: Clickable hyperlinks Message-ID: <40458775@f38.n261.z1.binkp.net> On Tue, 3 Jan 2017 20:46:31 -0800, "Deborah Swanson" declaimed the following: > >I didn't try printing them before, but I just did. Got: > >>>> print([Example](http://www.example.com) > >SyntaxError: invalid syntax (arrow pointing at the colon) > As I mentioned to someone else earlier... Count your parentheses... You need a ) for each ( AND you need " (or ') around the strings. As you entered it, you have invoked a print operation, passing it: A list containing a reference to a (undefined) variable "Example", and then you attempt call that list as a function passing it some unrecognized keyword "http" with a colon that Python normally uses indicate the start of a code block; said block being "//www.example.com". Try print("[Example](http://www.example.com)") -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ From python at lucidity.plus.com Tue Jan 3 07:22:16 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 00:22:16 +1200 Subject: How Best to Coerce Python Objects to Integers? Message-ID: <1909730512@f38.n261.z1.binkp.net> On 03/01/17 22:47, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: >> Aside from calling "except Exception" a "naked except" > > If you read the comments, you'll see that he originally had an actual > bare except clause, but then improved the code somewhat in response to > a recommendation that SystemExit etc not be caught. But, as stated at the top of the article, his brief was: "The strings come from a file that a human has typed in, so even though most of the values are good, a few will have errors ('25C') that int() will reject.". What he *should* have done is just validated his input strings before presenting the string to int() - i.e., process the input with knowledge that is specific to the problem domain before calling the general-purpose function. He mentions temperature sensors, so perhaps stripping a trailing 'c' or 'C' is a reasonable thing to do (or even, if there's a trailing 'f' or 'F', performing a numerical conversion after the value is known). Instead, he tried to patch around int() rejecting the strings. And then decided that he'd patch around int() rejecting things that weren't even strings even though that's not what the function has (apparently) been specified to receive. The "bulletproof" result will convert "25C" to None even though 25 is probably a reasonable result for that string in his domain problem domain. E. From python at lucidity.plus.com Tue Jan 3 07:26:12 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 00:26:12 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <1486132539@f38.n261.z1.binkp.net> Hi Callum, On 04/01/17 00:02, Callum Robinson wrote: > When i check the code it comes up with invalid syntax and my writing line gets re directed here > > def is_same(target, number: > if target == number: > result="win" > elif target > number: > result="low" > else: > result="high" > return result OK, good. That implies it's something wrong with the function definition ('def'). Look at that very carefully :) (*) E. (*) My emoticon may give you a hint ... From python at lucidity.plus.com Tue Jan 3 07:45:08 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 00:45:08 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <2189830552@f38.n261.z1.binkp.net> Hi Callum, On 04/01/17 00:30, Callum Robinson wrote: > I feel like im missing something so blatantly obvious. That's because you are ;). I don't want to come across as patronising, but I want you to see it for yourself, so, here's a function definition similar to yours that doesn't have the same syntax error that yours does: def foo(spam, ham): if spam == ham: return "same" return "different" See the difference? E. From python at lucidity.plus.com Tue Jan 3 07:47:54 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 00:47:54 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <3434470369@f38.n261.z1.binkp.net> On 04/01/17 00:32, Callum Robinson wrote: > I forgot a bloody bracket xD Cool, you got it ;) It's the sort of thing your brain will see instantly once you've done it a few times :D > and now theirs a new error ill try to figure this out on my own. You need to look back to Chris's original reply, I suspect (his reply was pointing out a runtime issue that will happen once the syntax issue is resolved) ... E. From smitty1e at gmail.com Tue Jan 3 07:55:06 2017 From: smitty1e at gmail.com (smitty1e) Date: Tue, 3 Jan 2017 04:55:06 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: On Monday, January 2, 2017 at 6:39:03 AM UTC-5, Antonio Caminero Garcia wrote: > Hello, I am having a hard time deciding what IDE or IDE-like code editor should I use. This can be overwhelming. > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, IntelliJ with Python plugin. > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. My screen should be mostly ?made of code? as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool and python oriented. > > The problem with Vim is the learning curve, so I know the very basic stuff, but obviously not enough for coding and I do not have time to learn it, it is a pity because there are awesome plugins that turns Vim into a lightweight powerful IDE-like. So now it is not an option but I will reconsider it in the future, learning little by little. Also, I am not very fan GUI guy if the task can be accomplished through the terminal. However, I don?t understand why people underrate GUIs, that said I normally use shortcuts for the most frequent tasks and when I have to do something that is not that frequent then I do it with the mouse, for the latter case in vim you would need to look for that specific command every time. > > Sublime is my current and preferred code editor. I installed Anaconda, Git integration and a couple of additional plugins that make sublime very powerful. Also, what I like about sublime compared to the full featured IDEs, besides the minimalism, is how you can perform code navigation back and forth so fast, I mean this is something that you can also do with the others but for some subjective reason I specifically love how sublime does it. The code completion in sublime I do not find it very intelligence, the SublimeCodeIntel is better than the one that Anaconda uses but the completions are not as verbose as in the IDEs. > > Now, I am thinking about giving a try to Visual Studio Code Edition (take a look, it sounds good https://marketplace.visualstudio.com/items?itemName=donjayamanne.python). I need an editor for professional software development. What would you recommend to me? I am told that means other than Emacs exist to edit code and interact with systems, but I don't worry about them. Happy New Year, Chris From python at deborahswanson.net Tue Jan 3 08:00:54 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 04 Jan 2017 01:00:54 +1200 Subject: Clickable hyperlinks Message-ID: <759429036@f38.n261.z1.binkp.net> Michael Torrie wrote, on January 03, 2017 8:05 PM > > On 01/03/2017 08:46 PM, Deborah Swanson wrote: > > Actually it is, or at least it doesn't happen in all email readers. > > Mine, for instance, never breaks up threads. > > Mine doesn't either, which illustrates the issue. This > message, for example appears under a long thread that started > out life as "mentor training python Romania with > certification" and then changed to "Cleaning up conditionals" > and then changed to "Clickable hyperlinks." All in one > thread. My client doesn't break them up because they all tie > together via the message-id header. > > And most of us would not like our client to break a thread > just because the subject changes. Often in long conversations > there are twists and turns in the discussion and sometimes > side-paths are explored, and the subject often is changed to > reflect this. With a truly threaded email reader (one that > shows a tree of messages, not just chronological order), this > works out very well. So if a discussion has a natural > evolution into various other topics, it is often considered > okay to just change the subject but continue the thread. > Other times, it's better to start a new thread. Where that > line is is hard to say! > > > I did say in the message you're replying to that I will try to > > remember to start new threads with brand new messages. > (Even though I > > think pipermail's behavior is a bug, that's what many > people read the > > list > > from.) > > Sounds good. > > I don't know of anyone that reads on the pipermail archive, > except in response to web searches. Most people use clients > of some kind, NNTP or email. And those that group messages > according to message-id (most clients except for ones that > try to be smart like Gmail web or Outlook) will show all the > topics I mentioned before as one giant thread, which is by > design (that's what message-id headers are for). I suppose. Times change of course, which always suits some and not others. Personally, I think putting messages that have different titles all in one thread is a bad design, but as I've said a couple of times now I intend to comply with the new rules. But compliance doesn't imply agreement. I prefer the old system, which ordered threads by titles, but there's obviously no pleasing everyone on this issue. From uri at speedy.net Tue Jan 3 08:19:17 2017 From: uri at speedy.net (Uri Even-Chen) Date: Tue, 3 Jan 2017 15:19:17 +0200 Subject: pip install -r requirements.txt fails with Python 3.6 on Windows 10 Message-ID: (.venv) C:\Uri\Speedy Net\Git\speedy-net [public]>pip install -r requirements.txt Requirement already satisfied: Django==1.10.4 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 2)) Requirement already satisfied: django-crispy-forms==1.6.1 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 3)) Requirement already satisfied: django-debug-toolbar==1.6 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 4)) Requirement already satisfied: django-environ==0.4.1 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 5)) Requirement already satisfied: django-friendship==1.5.0 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 6)) Requirement already satisfied: django-modeltranslation==0.12 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 7)) Requirement already satisfied: factory-boy==2.8.1 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 8)) Requirement already satisfied: Faker==0.7.7 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 9)) Collecting Pillow==3.4.2 (from -r requirements.txt (line 10)) Using cached Pillow-3.4.2.tar.gz Requirement already satisfied: python-dateutil==2.6.0 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 11)) Collecting rules==1.2 (from -r requirements.txt (line 12)) Using cached rules-1.2.tar.gz Requirement already satisfied: six==1.10.0 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 13)) Collecting sorl-thumbnail==12.4a1 (from -r requirements.txt (line 14)) Using cached sorl_thumbnail-12.4a1-py2.py3-none-any.whl Requirement already satisfied: sqlparse==0.2.2 in c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r requirements.txt (line 15)) Installing collected packages: Pillow, rules, sorl-thumbnail Running setup.py install for Pillow ... error Complete output from command "c:\uri\speedy net\git\speedy-net [public]\.venv\scripts\python.exe" -u -c "import setuptools, tokenize;__file__='C:\\Users\\Uri\\AppData\\Local\\Temp\\pip-build-3dj7ngjs\\Pillow\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\Uri\AppData\Local\Temp\pip-1464cmd1-record\install-record.txt --single-version-externally-managed --compile --install-headers "c:\uri\speedy net\git\speedy-net [public]\.venv\include\site\python3.6\Pillow": Single threaded build for windows running install running build running build_py creating build creating build\lib.win32-3.6 creating build\lib.win32-3.6\PIL copying PIL\BdfFontFile.py -> build\lib.win32-3.6\PIL copying PIL\BmpImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\BufrStubImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\ContainerIO.py -> build\lib.win32-3.6\PIL copying PIL\CurImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\DcxImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\DdsImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\EpsImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\ExifTags.py -> build\lib.win32-3.6\PIL copying PIL\features.py -> build\lib.win32-3.6\PIL copying PIL\FitsStubImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\FliImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\FontFile.py -> build\lib.win32-3.6\PIL copying PIL\FpxImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\FtexImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\GbrImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\GdImageFile.py -> build\lib.win32-3.6\PIL copying PIL\GifImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\GimpGradientFile.py -> build\lib.win32-3.6\PIL copying PIL\GimpPaletteFile.py -> build\lib.win32-3.6\PIL copying PIL\GribStubImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\Hdf5StubImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\IcnsImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\IcoImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\Image.py -> build\lib.win32-3.6\PIL copying PIL\ImageChops.py -> build\lib.win32-3.6\PIL copying PIL\ImageCms.py -> build\lib.win32-3.6\PIL copying PIL\ImageColor.py -> build\lib.win32-3.6\PIL copying PIL\ImageDraw.py -> build\lib.win32-3.6\PIL copying PIL\ImageDraw2.py -> build\lib.win32-3.6\PIL copying PIL\ImageEnhance.py -> build\lib.win32-3.6\PIL copying PIL\ImageFile.py -> build\lib.win32-3.6\PIL copying PIL\ImageFilter.py -> build\lib.win32-3.6\PIL copying PIL\ImageFont.py -> build\lib.win32-3.6\PIL copying PIL\ImageGrab.py -> build\lib.win32-3.6\PIL copying PIL\ImageMath.py -> build\lib.win32-3.6\PIL copying PIL\ImageMode.py -> build\lib.win32-3.6\PIL copying PIL\ImageMorph.py -> build\lib.win32-3.6\PIL copying PIL\ImageOps.py -> build\lib.win32-3.6\PIL copying PIL\ImagePalette.py -> build\lib.win32-3.6\PIL copying PIL\ImagePath.py -> build\lib.win32-3.6\PIL copying PIL\ImageQt.py -> build\lib.win32-3.6\PIL copying PIL\ImageSequence.py -> build\lib.win32-3.6\PIL copying PIL\ImageShow.py -> build\lib.win32-3.6\PIL copying PIL\ImageStat.py -> build\lib.win32-3.6\PIL copying PIL\ImageTk.py -> build\lib.win32-3.6\PIL copying PIL\ImageTransform.py -> build\lib.win32-3.6\PIL copying PIL\ImageWin.py -> build\lib.win32-3.6\PIL copying PIL\ImImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\ImtImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\IptcImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\Jpeg2KImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\JpegImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\JpegPresets.py -> build\lib.win32-3.6\PIL copying PIL\McIdasImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\MicImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\MpegImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\MpoImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\MspImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\OleFileIO.py -> build\lib.win32-3.6\PIL copying PIL\PaletteFile.py -> build\lib.win32-3.6\PIL copying PIL\PalmImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\PcdImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\PcfFontFile.py -> build\lib.win32-3.6\PIL copying PIL\PcxImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\PdfImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\PixarImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\PngImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\PpmImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\PsdImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\PSDraw.py -> build\lib.win32-3.6\PIL copying PIL\PyAccess.py -> build\lib.win32-3.6\PIL copying PIL\SgiImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\SpiderImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\SunImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\TarIO.py -> build\lib.win32-3.6\PIL copying PIL\TgaImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\TiffImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\TiffTags.py -> build\lib.win32-3.6\PIL copying PIL\WalImageFile.py -> build\lib.win32-3.6\PIL copying PIL\WebPImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\WmfImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\XbmImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\XpmImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\XVThumbImagePlugin.py -> build\lib.win32-3.6\PIL copying PIL\_binary.py -> build\lib.win32-3.6\PIL copying PIL\_tkinter_finder.py -> build\lib.win32-3.6\PIL copying PIL\_util.py -> build\lib.win32-3.6\PIL copying PIL\__init__.py -> build\lib.win32-3.6\PIL running egg_info writing Pillow.egg-info\PKG-INFO writing dependency_links to Pillow.egg-info\dependency_links.txt writing top-level names to Pillow.egg-info\top_level.txt warning: manifest_maker: standard file '-c' not found reading manifest file 'Pillow.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching '*.sh' no previously-included directories found matching 'docs\_static' warning: no previously-included files found matching '.coveragerc' warning: no previously-included files found matching '.editorconfig' warning: no previously-included files found matching '.landscape.yaml' warning: no previously-included files found matching 'appveyor.yml' warning: no previously-included files found matching 'build_children.sh' warning: no previously-included files found matching 'tox.ini' warning: no previously-included files matching '.git*' found anywhere in distribution warning: no previously-included files matching '*.pyc' found anywhere in distribution warning: no previously-included files matching '*.so' found anywhere in distribution writing manifest file 'Pillow.egg-info\SOURCES.txt' copying PIL\OleFileIO-README.md -> build\lib.win32-3.6\PIL running build_ext Traceback (most recent call last): File "", line 1, in File "C:\Users\Uri\AppData\Local\Temp\pip-build-3dj7ngjs\Pillow\setup.py", line 753, in zip_safe=not debug_build(), ) File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages\setuptools\command\install.py", line 61, in run return orig.install.run(self) File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\command\install.py", line 545, in run self.run_command('build') File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name) File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\command\build_ext.py", line 339, in run self.build_extensions() File "C:\Users\Uri\AppData\Local\Temp\pip-build-3dj7ngjs\Pillow\setup.py", line 521, in build_extensions ' using --disable-%s, aborting' % (f, f)) ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting ---------------------------------------- Command ""c:\uri\speedy net\git\speedy-net [public]\.venv\scripts\python.exe" -u -c "import setuptools, tokenize;__file__='C:\\Users\\Uri\\AppData\\Local\\Temp\\pip-build-3dj7ngjs\\Pillow\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\Uri\AppData\Local\Temp\pip-1464cmd1-record\install-record.txt --single-version-externally-managed --compile --install-headers "c:\uri\speedy net\git\speedy-net [public]\.venv\include\site\python3.6\Pillow"" failed with error code 1 in C:\Users\Uri\AppData\Local\Temp\pip-build-3dj7ngjs\Pillow\ (.venv) C:\Uri\Speedy Net\Git\speedy-net [public]>pip install -r requirements.txt What is the problem? Our requirements file is here: https://github.com/urievenchen/speedy-net/blob/master/requirements.txt It doesn't fail with Python 3.5.2 *Uri Even-Chen* [image: photo] Phone: +972-54-3995700 Email: uri at speedy.net Website: http://www.speedysoftware.com/uri/en/ From python at lucidity.plus.com Tue Jan 3 08:25:38 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 01:25:38 +1200 Subject: Screwing Up looping in Generator Message-ID: <3009695678@f38.n261.z1.binkp.net> Hi, On 04/01/17 01:12, Deborah Swanson wrote: > The main reason you might want to catch the StopIteration exception is > to do something else before your code simply stops running. If all > you're doing is run a generator til it's out of gas, and that's all you > want it to do, then there's no need to catch anything. Ah! OK, I see where the lines are being crossed now ;) Although StopIteration is an exception, it is something that the 'for/iter' machinery handles for you under the covers. Each 'for' statement effectively has a 'try' block around it that catches 'StopIteration' and just terminates that particular 'for' loop and continues on with the remainder of your script. Raising a 'StopIteration' is an internal mechanism used to determine when an iterator (i.e., the thing a 'for' loop is looping over) has exhausted itself. It's not something a regular user is ever expected to know about let alone catch. When execution falls out of the bottom of a generator, StopIteration is raise (compared to a regular function or method returning 'None'). E. From python at deborahswanson.net Tue Jan 3 08:32:12 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 04 Jan 2017 01:32:12 +1200 Subject: Clickable hyperlinks Message-ID: <4225562807@f38.n261.z1.binkp.net> Steven D'Aprano wrote, on January 03, 2017 9:40 PM > > On Wednesday 04 January 2017 15:46, Deborah Swanson wrote: > > > Steven D'Aprano wrote, on January 03, 2017 8:04 PM > [...] > >> Of course you have to put quotes around them to enter them in your > >> source code. We don't expect this to work: > >> > >> print(Hello World!) > >> > >> > >> you have to use a string literal with quotes: > >> > >> print('Hello World!') > >> > >> > >> Same for all of the above. > > > I didn't try printing them before, but I just did. Got: > > > >>>> print([Example](http://www.example.com) > > > > SyntaxError: invalid syntax (arrow pointing at the colon) > > You missed the part where I said you have to put them in quotes. > > Like any other string in Python, you have to use quotation > marks around it for > Python to understand it as a string. None of these things will work: > > print( Hello World! ) > > print( What do you want to do today? ) > > print( 3 2 1 blast off ) > > print( http://www.example.com ) > > > This isn't specific to print. This won't work either: > > message = Hello World! > > In *all* of these cases, you have to tell Python you're > dealing with a string, > and you do that with quotation marks: > > message = "Hello World!" > print( 'What do you want to do today?' ) > count_down = '3 2 1 blast off' > url = 'http://www.example.com' > > > > > -- > Steven Thanks, Steven. Yes, of course if you want to print strings you must enclose them in quotes. I think you learn that in Week 1 of any introductory course on Python. But we aren't trying to print strings here, the point is to produce clickable links. I didn't enclose them with quotes because I didn't see any point in printing plain text when I wanted clickable links. I actually didn't understand why you thought I should print them, but it never would have occurred to me that you wanted me to print out a bunch of silly plain text strings, apparently just for the heck of it. At this point, if I pursue this any farther, it will be to look into how Firefox takes webpage titles and urls out of its sqlite database and makes objects you can click on to open the webpages. That's the basic technology I'd need to find a way to talk (write) python into doing. If it's even worth it. On a practical level it's not worth it, way too much work for the teensy advantage of having it to use. It might be some giggles to figure out how to do it and maybe I will sometime just for funsies. My original question was whether python had anything to provide this functionality, and the answer appears to be a resounding NO!!! Answer received, and thanks to all who contributed. From python at mrabarnett.plus.com Tue Jan 3 09:05:34 2017 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 04 Jan 2017 02:05:34 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <3183934424@f38.n261.z1.binkp.net> On 2017-01-04 01:37, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson wrote: >> Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? >> >> #mynumber.py >> # this game uses a home made function >> import random >> >> #think of a number >> computer_number = number.randint(1,100) >> >> #create the function is_same() >> def is_same(target, number: >> if target == number: >> result="win" >> elif target > number: >> result="low" >> else: >> result="high" >> return result >> >> # start the game >> print("hello. \nI have thought of a number between 1 and 100.") >> >> #collect the user's guess as an interger >> guess = int(input("Can you guess it? ")) >> #Use our function >> higher_or_lower = is_same(computer_number, guess) >> #run the game untill the user is correct >> while higher_or_lower != "win" >> if higher_or_lower == "to low" >> guess = int(input("Sorry, you are too low. Try again.")) >> else: >> guess = int(input("Sorry your are too high. Try again.")) >> >> higher_or_lower = is_same(computer_number, guess) >> >> #end of game >> input("Correct!\nWell Done\n\n\nPress RETURN to exit.") > > Hey again, i'm sorry for bothering you with so many questions i just did not want to keep asking my teacher who is on holiday these. > > I have another issue where the code runs but i can guess every number from 1-100 but it always says Sorry your are too high. Try again. I don't understand what i have done to cause this. > What values can 'is_same' return? Which of those values are you checking for in the loop? From python at lucidity.plus.com Tue Jan 3 09:09:40 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 02:09:40 +1200 Subject: How Best to Coerce Python Objects to Integers? Message-ID: <517822311@f38.n261.z1.binkp.net> On 04/01/17 01:10, Steve D'Aprano wrote: > On Wed, 4 Jan 2017 11:22 am, Erik wrote: >> What he *should* have done is just validated his input strings before >> presenting the string to int() - i.e., process the input with knowledge >> that is specific to the problem domain before calling the >> general-purpose function. > > That's the Look Before You Leap solution. But in this case, given the > scenario described (a text file with a few typos), the best way is to ask > for forgiveness rather than permission: Yes, probably, in this case ;) OK, in the case where the function you're calling is sane (and Python's int() is - it won't blindly accept "0x101" as a hex value, for example) then it's probably right that leaping first and then, on failure, processing the value and leaping again is the right thing to do. [I tend to work in an environment where things I'm calling may not be sane (and in some cases I may never know), so I will usually consider LBYL as a way of CMA ;)]. In this whole discussion there has been no mention of what happens when the function returns None, though. > Another thought: if he is receiving human generated input, there is an > argument to be made for accepting "look alikes" -- e.g. maybe the data was > entered by Aunt Tilly, who was a typist in the 1960s and can't break the > habit of using l or I interchangeably for 1, and O for 0. Sure - and that's what I meant by processing the string according to his problem "domain". If he has Aunt Tillys doing his data input, then l->1 and 0->O may be a reasonable thing (I recently did a project where things like 0-> converting Icelandic's Eth and Thorn runic letters to 'D' and 'P' - though 0-> lly wrong ;) - was a reasonable character translation because that's what 0-> le actually typed in). E. From python at lucidity.plus.com Tue Jan 3 09:35:34 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 02:35:34 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <595817138@f38.n261.z1.binkp.net> On 04/01/17 02:24, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote: >> What values can 'is_same' return? >> >> Which of those values are you checking for in the loop? > > I'm sorry but i do not completely understand what you are stating You need to think about the specific things (their type, their exact values) that your functions might return. Printing some trace output is a classic way of debugging your program. If, after this line: >>>> higher_or_lower = is_same(computer_number, guess) ... you added: print (higher_or_lower) ... what values do you then see being output? How will those values be processed by the conditions you see that work on the "higher_or_lower" variable? E. From jorge.conrado at cptec.inpe.br Tue Jan 3 09:45:29 2017 From: jorge.conrado at cptec.inpe.br (jorge.conrado at cptec.inpe.br) Date: Tue, 03 Jan 2017 12:45:29 -0200 Subject: Numpy error Message-ID: Hi, I alredy used Python and now I have the message: import numpy as np Traceback (most recent call last): File "", line 1, in File "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/__init__.py", line 153, in from . import add_newdocs File "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/add_newdocs.py", line 13, in from numpy.lib import add_newdoc File "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/lib/__init__.py", line 18, in from .polynomial import * File "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/lib/polynomial.py", line 19, in from numpy.linalg import eigvals, lstsq, inv File "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/linalg/__init__.py", line 50, in from .linalg import * File "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/linalg/linalg.py", line 29, in from numpy.linalg import lapack_lite, _umath_linalg ImportError: /home/conrado/Canopy/appdata/canopy-1.5.5.3123.rh5-x86_64/lib/libgfortran.so.3: version `GFORTRAN_1.4' not found (required by /lib64/liblapack.so.3) I did: pip istall --user numpy Requirement already satisfied: numpy in /home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy-1.8.0-py2.7-linux-x86_64.egg What can I do to solve this message. Thanks, Conrado From flebber.crue at gmail.com Tue Jan 3 09:54:25 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Tue, 3 Jan 2017 06:54:25 -0800 (PST) Subject: Screwing Up looping in Generator Message-ID: <7c38d03e-566c-4d36-9e41-84f913b2e097@googlegroups.com> Hi This is simple, but its getting me confused. I have a csv writer that opens a file and loops each line of the file for each file and then closes, writing one file. I want to alter the behaviour to be a written file for each input file. I saw a roundrobin example however it failed for me as you cannot get len(generator) to use a while loop on. it exhausts should I use the same for again after the with open? rootobs in this code is my generator and I am already looping it however def data_attr(roots): """Get the root object and iter items.""" for file in rootobs: base = os.path.basename(file.name) write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") with open(write_to, 'w', newline='') as csvf: race_writer = csv.writer(csvf, delimiter=',') race_writer.writerow( ["meet_id", "meet_venue", "meet_date", "meet_rail", ... # other categories here ... "jockeysurname", "jockeyfirstname"]) for xml_data in roots: ... # parsing code for noms in race_child: if noms.tag == 'nomination': race_writer.writerow( [meet_id, meet_venue, meet_date, ... #parsing info removed noms.get("jockeyfirstname")]) Cheers Sayth From python at lucidity.plus.com Tue Jan 3 10:11:26 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 03:11:26 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <2093310810@f38.n261.z1.binkp.net> On 04/01/17 02:47, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 3:35:53 PM UTC+13, Erik wrote: > I did it and this is what it states when i run it > > hello. > I have thought of a number between 1 and 100. > Can you guess it? > 5 > Low > Sorry , you are too high. Try again. > > Does this mean the number i entered is to low but the code is still stating it is to high? The last code you posted will return "low", "high" or "win" from the "is_same()" function. How does that relate to the "Low" that you are now printing? You must have changed something. If you are testing strings as return values then they must be _exactly_ the same - including case and spaces etc. Always remember that your computer is an idiot. It's the fastest idiot that you'll ever know, and it could become your best friend. But it IS an idiot ;) E. From python at deborahswanson.net Tue Jan 3 10:15:52 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 04 Jan 2017 03:15:52 +1200 Subject: Clickable hyperlinks Message-ID: <158870337@f38.n261.z1.binkp.net> Steve D'Aprano wrote, on January 04, 2017 2:09 AM > > On Wed, 4 Jan 2017 08:00 pm, Deborah Swanson wrote: > > [speaking of threading emails] > > > I suppose. Times change of course, which always suits some and not > > others. Personally, I think putting messages that have different > > titles all in one thread is a bad design, but as I've said > a couple of > > times now I intend to comply with the new rules. But compliance > > doesn't imply agreement. I prefer the old system, which ordered > > threads by titles, but there's obviously no pleasing > everyone on this > > issue. > > Indeed :-) > > However, as far as I am aware, the use of threading by > following the In-Reply-To and References header lines goes > back to at least 1982, which makes them pretty old, and > certainly pre-dates Gmail and Outlook by many years. They're > also official standards which ALL email programs are supposed > to follow, so if Gmail and Outlook fail to follow the > standard, well, I wouldn't be surprised. I don't absolutely > know for a fact that threading in this way is older than > threading by subject line, but I'm fairly confident that what > you are calling the "new rules" are actually the much older rules. > > Are there any old-timers here who were using email prior to > 1982 that would care to comment? > > > Here's a discussion by Jamie Zawinski, who wrote Netscape > Navigator, before it became Mozila and Firefox, so he knows > what he's talking about: > https://www.jwz.org/doc/threading.html The concept here is not so much that people start a new topic and change the subject, but that sometimes the topic just naturally evolves to the point that a change in subject is sensible. Take this thread for example: the topic has drifted from "Clickable hyperlinks" to talking about email threading. I should be able to change the subject without breaking the thread: Clickable hyperlinks +- RE: Clickable hyperlinks ? +- Re: RE: Clickable hyperlinks ? L- RE: Clickable hyperlinks ? L- Threading [was Re: Clickable hyperlinks] ? L- Re: Threading +- RE: Clickable hyperlinks L- Re: Clickable hyperlinks Adding a "Re:" or "RE" to the subject doesn't change the thread, and neither does changing the subject line in a more substantial way. Of course, I can always *choose* to sort by subject line, or date. Personally I hardly ever sort by thread, so it is no skin off my nose what you do. But when you hit "Reply" to a message, you inherit the "Reference" and "In-Reply-To" headers from the previous message, so at least some people will see it threaded in an existing thread rather than starting a brand new one. -- Steve "Cheer up," they said, "things could be worse." So I cheered up, and sure enough, things got worse. Interesting history lesson. I actually wouldn't know which came first, since I've only been online since 1992 (offline computing since 1972). I do know that I've been on a lot of mailing lists like this one since 1992 (remember Fidonet?), some with actively posting members in the thousands. And I have never heard anyone whine and complain about threading issues that had anything to do with the message title until yesterday. So it goes. I'll repeat again that I'll comply with the rules of this group that are brand spanking new to me, and I've been around a corner or two. From python at deborahswanson.net Tue Jan 3 10:25:34 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 04 Jan 2017 03:25:34 +1200 Subject: Clickable hyperlinks Message-ID: <1117208239@f38.n261.z1.binkp.net> Steve D'Aprano wrote, on January 04, 2017 2:20 AM > > On Wed, 4 Jan 2017 03:46 pm, Deborah Swanson wrote: > > > As I've mentioned in other posts on this thread, I'm now > thinking that > > I need to write a class to do this, and find out how > Firefox and url > > aware terminals in Linux do it. There must be a way. > > > A GUI application can interpret text any way it chooses. > Firefox takes a HTML file and renders it, using whatever GUI > library it chooses. That GUI library understands that text like: > > Hello World! > > should be shown in bold face, and text like: > > Example > > should be shown as the word "Example" underlined and in some > colour, and when you click on it the browser will navigate to > the URL given. Firefox can do this because it controls the > environment it runs in. > > Same for Excel, which also controls the environment it runs in. > > That's *not* the case for Python, which is at the mercy of > whatever console or terminal application it is running in. > > However, you can use Python to write GUI applications. Then it becomes > *your* responsibility to create the window, populate it with > any buttons or text or scroll bars you want, and you can > choose to interpret text any way you like -- including as > clickable Hyperlinks. > > The bottom line is, there is no "a way" to do this. There are > a thousand, ten thousand, ways to do it. Every web browser, > every terminal, every URL-aware application, can choose its > own way to do it. There's no one single way that works > everywhere, but if you are working in a console or terminal, > just printing the URL is likely to be interpreted by the > console as a clickable link: > > print("http://www.example.com") > > > > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered > up, and sure enough, things got worse. I can appreciate all that and pretty much knew most of it already. At this point I'm not so much concerned with how to put characters on a white space that are clickable, we've pretty much established at least a couple dozen messages ago that there's nothing for python to get hold of there because of the vast number of places people might want to put clickable links. (I actually read every message on a thread that interests me enough to participate.) I think the entry point to this problem is to find out how to connect to the internet when you click that link. Then I think the nuts and bolts of what symbols to put where for a particular target would fall into place. From python at deborahswanson.net Tue Jan 3 10:43:02 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 04 Jan 2017 03:43:02 +1200 Subject: Clickable hyperlinks Message-ID: <3628262856@f38.n261.z1.binkp.net> Steve D'Aprano wrote, on January 04, 2017 2:39 AM > > On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote: > > > Thanks, Steven. Yes, of course if you want to print strings > you must > > enclose them in quotes. I think you learn that in Week 1 of any > > introductory course on Python. > > > > But we aren't trying to print strings here, the point is to produce > > clickable links. I didn't enclose them with quotes because I didn't > > see any point in printing plain text when I wanted > clickable links. I > > actually didn't understand why you thought I should print > them, but it > > never would have occurred to me that you wanted me to print out a > > bunch of silly plain text strings, apparently just for the > heck of it. > > What we have here is a failure to communicate :-) > > I apologise for ruffling your feathers, but its difficult to > judge another person's level of knowledge. In someways you're > obviously quite knowledgeable about Python, but in other ways > you're still learning (as we all are!) and I'm afraid I may > have misjudged exactly what your level of knowledge was. > Sorry about that. > > I'm not suggesting that you print "silly plain text strings" > just for the heck of it. You've asked how to get a clickable > link using Python. There is only one way I know of to get a > clickable link using Python: > > Write out a link as plain text to another application which > then interprets the plain text as a clickable link. > > You *might* be able to get clickable links in Python by > writing an entire GUI application, using (say) the tkinter > library, or one of the third-party GUI libraries like > wxPython, kivy, pyqt, or others, but I haven't a clue how. > But even there, your links will start off as text, which > means you will still need to surround them with quotes to > make them strings. > > > Aside: you've actually raised a fascinating question. I > wonder whether there are any programming languages that > understand URLs as native data types, so that *source code* > starting with http:// etc is understood in the same way that > source code starting with [ is seen as a list or { as a dict? > > > But back to your problem: short of writing you own GUI > application, in which case you can do *anything you like*, you can: > > - write out a HTML file containing the URLs you want, in href= ... tags, then open the HTML file in a web browser > and let the web browser interpret the HTML tags as clickable links; > > > - write out an Excel spreadsheet, using whatever format Excel > expects, open the spreadsheet in Excel, and let Excel > interpret the mystery format as a clickable link; > > - print the URL to the console, and see if the console is > smart enough to interpret it as a clickable link. > > > I'm sorry that I can't give you a one-sentence answer "just > use such-and-such a function, that does exactly what you want > in a platform-independent manner" :-( > > > > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered > up, and sure enough, things got worse. Well, well. People mix and people misunderstand and misjudge each other. It's the way of things with people. I just needed to tell you how it all looked from my side. So are we done with that? I certainly hope so. I'm quite well aware by now that there is no one-sentence answer to my original question, if there's any coherent answer at all. Them's the breaks. Live with it or live without it, it doesn't care. I do appreciate the education I've gotten on this thread about the issues involved. But I rather imagine that near term anyway, I'll only be pondering it now and then, and maybe poking around a bit. Who knows, maybe I'll eventually invent the solution and make my first million dollars from it. haha, yeah right. So it goes. Deborah From __peter__ at web.de Tue Jan 3 10:57:26 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 03 Jan 2017 16:57:26 +0100 Subject: trouble with cmd.Cmd and prompting References: <20170103063903.GA63893@cskk.homeip.net> Message-ID: Cameron Simpson wrote: > On 03Jan2017 00:14, Dennis Lee Bieber wrote: >>On Tue, 3 Jan 2017 11:33:15 +1100, Cameron Simpson >>declaimed the following: >>>I'm using cmd.Cmd to write a little FTP-like command line to interface to >>>a storage system of mine and encountering weird behaviour. When I enter a >>>command the next prompt appears _before_ the associated operation runs, >>>or so it appears. >> >>>Has anyone seen this kind of thing before? >> >>Haven't used the module but there is something I find intriguing in the >>help system >>-=-=-=-=- >> Cmd.precmd(line) >> Hook method executed just before the command line is interpreted, but >>after the input prompt is generated and issued. >>-=-=-=-=- >>"... AFTER the input prompt is ... issued" >> >>I don't know, but that sure sounds to me like the cmd object tends to >>process one line behind... Though that behavior is not shown in the turtle >>example in the help system. > > Hmm. Interesting. I had read that text to imply (based on what I imagined > _should_ happen) that the flow of control went: > > cmdloop: > issue prompt> > line=input() # or readline > line=precmd(line) > stop=onecmd(line) # which calls do_blah... > stop=postcmd(stop,line) > if stop: > break > > but your reading of it suggests that this is possible: > > issue prompt> > cmdloop: > line=input() # or readline > issue prompt> > line=precmd(line) > stop=onecmd(line) # which calls do_blah... > stop=postcmd(stop,line) > if stop: > break > > The text for Cmd.cmdloop starts with this: > > Repeatedly issue a prompt, accept input, parse an initial prefix off the > received input, and dispatch to action methods, passing them the > remainder of the line as argument. > > which argues for the former, and was what I naively expected. > > I guess I'd better dig out the source; I dislike going that far, not > merely out of laziness, but also because the source is not the spec. > > Thanks for the suggestion, > Cameron Simpson I don't believe Dennis' reading of the docs is correct, and the relevant part of the source (Python 3.4) does not show anything to support it: while not stop: if self.cmdqueue: line = self.cmdqueue.pop(0) else: if self.use_rawinput: try: line = input(self.prompt) except EOFError: line = 'EOF' else: self.stdout.write(self.prompt) self.stdout.flush() line = self.stdin.readline() if not len(line): line = 'EOF' else: line = line.rstrip('\r\n') line = self.precmd(line) stop = self.onecmd(line) stop = self.postcmd(stop, line) self.postloop() Is there something asynchronous in your command? Perhaps you can post a minimal runnable example that shows the odd behaviour. From best_lay at yahoo.com Tue Jan 3 11:00:56 2017 From: best_lay at yahoo.com (Wildman) Date: Tue, 03 Jan 2017 10:00:56 -0600 Subject: learning and experimenting python. References: <6886d439-751c-42d2-adf2-fbf494221d48@googlegroups.com> <58678152$0$1622$c3e8da3$5496439d@news.astraweb.com> <586B27B5.9040107@stoneleaf.us> Message-ID: On Mon, 02 Jan 2017 20:25:25 -0800, Ethan Furman wrote: > On 01/02/2017 09:53 AM, Wildman via Python-list wrote: > > [rude ascii art omitted] > > That is a completely inappropriate response. Yes it was. I tend to get upset when told to shut up and go away for no good reason. -- GNU/Linux user #557453 The cow died so I don't need your bull! From redstone-cold at 163.com Tue Jan 3 11:15:06 2017 From: redstone-cold at 163.com (iMath) Date: Wed, 04 Jan 2017 04:15:06 +1200 Subject: Introducing my Python/PyQt5 powered Bing wallpaper open source project Message-ID: <1309505310@f38.n261.z1.binkp.net> Hello everyone , I'd like to introduce my Python/PyQt5 powered Bing wallpaper open source project. ? ? BingNiceWallpapers https://github.com/redstoneleo/BingNiceWallpapers BingNiceWallpapers can get background images from http://www.bing.com/?mkt=zh-CN and set them as your desktop wallpaper by just a single click on system tray icon. For Windows binary installer and more information, please refer to http://mathjoy.lofter.com/post/42208d_7cabcf7 (written in simplified Chinese) Features Changing wallpaper in a single click on system tray icon or after a specified time interval Windows and Linux Gnome desktop support Delete or save the current wallpaper Welcome to help with the project ! From python at lucidity.plus.com Tue Jan 3 11:22:58 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 04 Jan 2017 04:22:58 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <2440616603@f38.n261.z1.binkp.net> On 04/01/17 03:25, Steven D'Aprano wrote: > On Wednesday 04 January 2017 12:25, Callum Robinson wrote: > >> Hey man thanks, the sad thing is i have no idea why i put that in. I must be >> having a terrible day. > > Don't worry about it. The difference between a beginner and an expert is *not* > that experts make fewer mistakes, but that experts know how to fix those > mistakes so quickly that they don't even notice them. Hmm. An expert at what? Telling a beginner how to spell something is not the same as coaxing that same beginner to think about something or approach a problem from a particular angle and come to their own solution. We all could have told him the answer up-front. What has that achieved? E. From songofacandy at gmail.com Tue Jan 3 11:35:40 2017 From: songofacandy at gmail.com (INADA Naoki) Date: Wed, 4 Jan 2017 01:35:40 +0900 Subject: pip install -r requirements.txt fails with Python 3.6 on Windows 10 In-Reply-To: References: Message-ID: Pillow 3.4.2 provides binary wheel for Python 3.5, but not for 3.6. So your pip can just install wheel on Python 3.5, but it is required to build on Python 3.6. And your machine doesn't have zlib which is required to build Pillow. Easiest solution may just update your requirements to Pillow==4.0.0. Pillow 4.0.0 provides binary wheel for Python 3.6, from 2017-01-02. https://pypi.python.org/pypi/Pillow/4.0.0 On Tue, Jan 3, 2017 at 10:19 PM, Uri Even-Chen wrote: > (.venv) C:\Uri\Speedy Net\Git\speedy-net [public]>pip install -r > requirements.txt > Requirement already satisfied: Django==1.10.4 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 2)) > Requirement already satisfied: django-crispy-forms==1.6.1 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 3)) > Requirement already satisfied: django-debug-toolbar==1.6 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 4)) > Requirement already satisfied: django-environ==0.4.1 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 5)) > Requirement already satisfied: django-friendship==1.5.0 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 6)) > Requirement already satisfied: django-modeltranslation==0.12 in > c:\uri\speedy net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 7)) > Requirement already satisfied: factory-boy==2.8.1 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 8)) > Requirement already satisfied: Faker==0.7.7 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 9)) > Collecting Pillow==3.4.2 (from -r requirements.txt (line 10)) > Using cached Pillow-3.4.2.tar.gz > Requirement already satisfied: python-dateutil==2.6.0 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 11)) > Collecting rules==1.2 (from -r requirements.txt (line 12)) > Using cached rules-1.2.tar.gz > Requirement already satisfied: six==1.10.0 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 13)) > Collecting sorl-thumbnail==12.4a1 (from -r requirements.txt (line 14)) > Using cached sorl_thumbnail-12.4a1-py2.py3-none-any.whl > Requirement already satisfied: sqlparse==0.2.2 in c:\uri\speedy > net\git\speedy-net [public]\.venv\lib\site-packages (from -r > requirements.txt (line 15)) > Installing collected packages: Pillow, rules, sorl-thumbnail > Running setup.py install for Pillow ... error > Complete output from command "c:\uri\speedy net\git\speedy-net > [public]\.venv\scripts\python.exe" -u -c "import setuptools, > tokenize;__file__='C:\\Users\\Uri\\AppData\\Local\\Temp\\pip-build-3dj7ngjs\\Pillow\\setup.py';f=getattr(tokenize, > 'open', open)(__file__);code=f.read().replace('\r\n', > '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record > C:\Users\Uri\AppData\Local\Temp\pip-1464cmd1-record\install-record.txt > --single-version-externally-managed --compile --install-headers > "c:\uri\speedy net\git\speedy-net > [public]\.venv\include\site\python3.6\Pillow": > Single threaded build for windows > running install > running build > running build_py > creating build > creating build\lib.win32-3.6 > creating build\lib.win32-3.6\PIL > copying PIL\BdfFontFile.py -> build\lib.win32-3.6\PIL > copying PIL\BmpImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\BufrStubImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\ContainerIO.py -> build\lib.win32-3.6\PIL > copying PIL\CurImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\DcxImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\DdsImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\EpsImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\ExifTags.py -> build\lib.win32-3.6\PIL > copying PIL\features.py -> build\lib.win32-3.6\PIL > copying PIL\FitsStubImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\FliImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\FontFile.py -> build\lib.win32-3.6\PIL > copying PIL\FpxImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\FtexImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\GbrImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\GdImageFile.py -> build\lib.win32-3.6\PIL > copying PIL\GifImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\GimpGradientFile.py -> build\lib.win32-3.6\PIL > copying PIL\GimpPaletteFile.py -> build\lib.win32-3.6\PIL > copying PIL\GribStubImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\Hdf5StubImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\IcnsImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\IcoImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\Image.py -> build\lib.win32-3.6\PIL > copying PIL\ImageChops.py -> build\lib.win32-3.6\PIL > copying PIL\ImageCms.py -> build\lib.win32-3.6\PIL > copying PIL\ImageColor.py -> build\lib.win32-3.6\PIL > copying PIL\ImageDraw.py -> build\lib.win32-3.6\PIL > copying PIL\ImageDraw2.py -> build\lib.win32-3.6\PIL > copying PIL\ImageEnhance.py -> build\lib.win32-3.6\PIL > copying PIL\ImageFile.py -> build\lib.win32-3.6\PIL > copying PIL\ImageFilter.py -> build\lib.win32-3.6\PIL > copying PIL\ImageFont.py -> build\lib.win32-3.6\PIL > copying PIL\ImageGrab.py -> build\lib.win32-3.6\PIL > copying PIL\ImageMath.py -> build\lib.win32-3.6\PIL > copying PIL\ImageMode.py -> build\lib.win32-3.6\PIL > copying PIL\ImageMorph.py -> build\lib.win32-3.6\PIL > copying PIL\ImageOps.py -> build\lib.win32-3.6\PIL > copying PIL\ImagePalette.py -> build\lib.win32-3.6\PIL > copying PIL\ImagePath.py -> build\lib.win32-3.6\PIL > copying PIL\ImageQt.py -> build\lib.win32-3.6\PIL > copying PIL\ImageSequence.py -> build\lib.win32-3.6\PIL > copying PIL\ImageShow.py -> build\lib.win32-3.6\PIL > copying PIL\ImageStat.py -> build\lib.win32-3.6\PIL > copying PIL\ImageTk.py -> build\lib.win32-3.6\PIL > copying PIL\ImageTransform.py -> build\lib.win32-3.6\PIL > copying PIL\ImageWin.py -> build\lib.win32-3.6\PIL > copying PIL\ImImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\ImtImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\IptcImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\Jpeg2KImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\JpegImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\JpegPresets.py -> build\lib.win32-3.6\PIL > copying PIL\McIdasImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\MicImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\MpegImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\MpoImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\MspImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\OleFileIO.py -> build\lib.win32-3.6\PIL > copying PIL\PaletteFile.py -> build\lib.win32-3.6\PIL > copying PIL\PalmImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\PcdImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\PcfFontFile.py -> build\lib.win32-3.6\PIL > copying PIL\PcxImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\PdfImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\PixarImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\PngImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\PpmImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\PsdImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\PSDraw.py -> build\lib.win32-3.6\PIL > copying PIL\PyAccess.py -> build\lib.win32-3.6\PIL > copying PIL\SgiImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\SpiderImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\SunImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\TarIO.py -> build\lib.win32-3.6\PIL > copying PIL\TgaImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\TiffImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\TiffTags.py -> build\lib.win32-3.6\PIL > copying PIL\WalImageFile.py -> build\lib.win32-3.6\PIL > copying PIL\WebPImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\WmfImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\XbmImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\XpmImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\XVThumbImagePlugin.py -> build\lib.win32-3.6\PIL > copying PIL\_binary.py -> build\lib.win32-3.6\PIL > copying PIL\_tkinter_finder.py -> build\lib.win32-3.6\PIL > copying PIL\_util.py -> build\lib.win32-3.6\PIL > copying PIL\__init__.py -> build\lib.win32-3.6\PIL > running egg_info > writing Pillow.egg-info\PKG-INFO > writing dependency_links to Pillow.egg-info\dependency_links.txt > writing top-level names to Pillow.egg-info\top_level.txt > warning: manifest_maker: standard file '-c' not found > > reading manifest file 'Pillow.egg-info\SOURCES.txt' > reading manifest template 'MANIFEST.in' > warning: no files found matching '*.sh' > no previously-included directories found matching 'docs\_static' > warning: no previously-included files found matching '.coveragerc' > warning: no previously-included files found matching '.editorconfig' > warning: no previously-included files found matching '.landscape.yaml' > warning: no previously-included files found matching 'appveyor.yml' > warning: no previously-included files found matching 'build_children.sh' > warning: no previously-included files found matching 'tox.ini' > warning: no previously-included files matching '.git*' found anywhere > in distribution > warning: no previously-included files matching '*.pyc' found anywhere > in distribution > warning: no previously-included files matching '*.so' found anywhere in > distribution > writing manifest file 'Pillow.egg-info\SOURCES.txt' > copying PIL\OleFileIO-README.md -> build\lib.win32-3.6\PIL > running build_ext > Traceback (most recent call last): > File "", line 1, in > File > "C:\Users\Uri\AppData\Local\Temp\pip-build-3dj7ngjs\Pillow\setup.py", line > 753, in > zip_safe=not debug_build(), ) > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\core.py", > line 148, in setup > dist.run_commands() > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", > line 955, in run_commands > self.run_command(cmd) > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", > line 974, in run_command > cmd_obj.run() > File "c:\uri\speedy net\git\speedy-net > [public]\.venv\lib\site-packages\setuptools\command\install.py", line 61, > in run > return orig.install.run(self) > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\command\install.py", > line 545, in run > self.run_command('build') > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\cmd.py", > line 313, in run_command > self.distribution.run_command(command) > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", > line 974, in run_command > cmd_obj.run() > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\command\build.py", > line 135, in run > self.run_command(cmd_name) > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\cmd.py", > line 313, in run_command > self.distribution.run_command(command) > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", > line 974, in run_command > cmd_obj.run() > File > "C:\Users\Uri\AppData\Local\Programs\Python\Python36-32\lib\distutils\command\build_ext.py", > line 339, in run > self.build_extensions() > File > "C:\Users\Uri\AppData\Local\Temp\pip-build-3dj7ngjs\Pillow\setup.py", line > 521, in build_extensions > ' using --disable-%s, aborting' % (f, f)) > ValueError: zlib is required unless explicitly disabled using > --disable-zlib, aborting > > ---------------------------------------- > Command ""c:\uri\speedy net\git\speedy-net > [public]\.venv\scripts\python.exe" -u -c "import setuptools, > tokenize;__file__='C:\\Users\\Uri\\AppData\\Local\\Temp\\pip-build-3dj7ngjs\\Pillow\\setup.py';f=getattr(tokenize, > 'open', open)(__file__);code=f.read().replace('\r\n', > '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record > C:\Users\Uri\AppData\Local\Temp\pip-1464cmd1-record\install-record.txt > --single-version-externally-managed --compile --install-headers > "c:\uri\speedy net\git\speedy-net > [public]\.venv\include\site\python3.6\Pillow"" failed with error code 1 in > C:\Users\Uri\AppData\Local\Temp\pip-build-3dj7ngjs\Pillow\ > > (.venv) C:\Uri\Speedy Net\Git\speedy-net [public]>pip install -r > requirements.txt > > What is the problem? Our requirements file is here: > https://github.com/urievenchen/speedy-net/blob/master/requirements.txt > > It doesn't fail with Python 3.5.2 > > *Uri Even-Chen* > [image: photo] Phone: +972-54-3995700 > Email: uri at speedy.net > Website: http://www.speedysoftware.com/uri/en/ > > > > -- > https://mail.python.org/mailman/listinfo/python-list From dan at tombstonezero.net Tue Jan 3 13:07:04 2017 From: dan at tombstonezero.net (Dan Sommers) Date: Wed, 04 Jan 2017 06:07:04 +1200 Subject: Clickable hyperlinks References: <4238055976@f38.n261.z1.binkp.net> Message-ID: <4221224472@f38.n261.z1.binkp.net> On Wed, 04 Jan 2017 16:40:00 +1100, Steven D'Aprano wrote: > On Wednesday 04 January 2017 15:46, Deborah Swanson wrote: > >> Steven D'Aprano wrote, on January 03, 2017 8:04 PM > [...] >>> Of course you have to put quotes around them to enter them in >>> your source code. >>> We don't expect this to work: >>> >>> print(Hello World!) >>> >>> you have to use a string literal with quotes: >>> >>> print('Hello World!') >>> >>> Same for all of the above. > >> I didn't try printing them before, but I just did. Got: >> >>>>> print([Example](http://www.example.com) >> >> SyntaxError: invalid syntax (arrow pointing at the colon) > > You missed the part where I said you have to put them in quotes. ObPython: "When I've got these antlers on I am dictating and when I take them off I am not dictating." :-) From kevin.p.dwyer at gmail.com Tue Jan 3 13:41:40 2017 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Wed, 04 Jan 2017 06:41:40 +1200 Subject: Mutable global state and threads Message-ID: <1468831840@f38.n261.z1.binkp.net> Hello List, I came across some threading code in Some Other place recently and wanted to sanity-check my assumptions. The code (below) creates a number of threads; each thread takes the last (index -1) value from a global list of integers, increments it by one and appends the new value to the list. The originator of the code expected that when all the threads completed, the list would be an ascending sequence of integers, for example if the original list was [0] and two threads mutated it twice each, the final state would be [0, 1, 2, 3, 4]. Here is a version of the code (slightly simplified and modified to allow changing the number of threads and mutations). import sys import threading class myThread(threading.Thread): def __init__(self, nmutations): threading.Thread.__init__(self) self.nmutations = nmutations def run(self): mutate(self.nmutations) # print (L) return def mutate(nmutations): n = nmutations while n: L.append(L[-1 ]+ 1) n -= 1 return def main(nthreads=2, nmutations=2): global L L = [0] threads = [myThread(nmutations) for i in range(nthreads)] for t in threads: t.start() for t in threads: t.join() print(L) assert L == list(range((nthreads * nmutations) + 1)) if __name__ == '__main__': nthreads, nmutations = int(sys.argv[1]), int(sys.argv[2]) main(nthreads, nmutations) Firstly, is it true that the statement L.append(L[-1 ]+ 1) is not atomic, that is the thread might evaluate L[-1] and then yield, allowing another thread to mutate L, before incrementing and appending? Secondly, the original code printed the list at the end of a thread's run method to examine the state of the list. I don't think this would work quite as expected, because the thread might yield after mutating the list but before printing, so the list could have been mutated before the print was executed. Is there a way to display the state of the list before any further mutations take place? (Disclaimer: I understand that sanity, mutable global state and threads are unlikely bedfellows and so promise never to try anything like this in production code). Cheers, Kev From chris.p.clark at ba.com Tue Jan 3 13:55:06 2017 From: chris.p.clark at ba.com (Chris Clark) Date: Tue, 3 Jan 2017 18:55:06 +0000 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <3C10AD96D068B9F8.6D96E7A5-7712-484F-98D0-8CCAEF99FD72@mail.outlook.com> Tried every python ide going, they either grind to a halt or just look messy. Best one I ever used and stick with is drpython, years old, probably not maintained but does everything I want at a blistering speed and just looks perfect. On Mon, Jan 2, 2017 at 11:41 AM +0000, "Antonio Caminero Garcia" > wrote: Hello, I am having a hard time deciding what IDE or IDE-like code editor should I use. This can be overwhelming. So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, IntelliJ with Python plugin. The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. My screen should be mostly ?made of code? as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool and python oriented. The problem with Vim is the learning curve, so I know the very basic stuff, but obviously not enough for coding and I do not have time to learn it, it is a pity because there are awesome plugins that turns Vim into a lightweight powerful IDE-like. So now it is not an option but I will reconsider it in the future, learning little by little. Also, I am not very fan GUI guy if the task can be accomplished through the terminal. However, I don?t understand why people underrate GUIs, that said I normally use shortcuts for the most frequent tasks and when I have to do something that is not that frequent then I do it with the mouse, for the latter case in vim you would need to look for that specific command every time. Sublime is my current and preferred code editor. I installed Anaconda, Git integration and a couple of additional plugins that make sublime very powerful. Also, what I like about sublime compared to the full featured IDEs, besides the minimalism, is how you can perform code navigation back and forth so fast, I mean this is something that you can also do with the others but for some subjective reason I specifically love how sublime does it. The code completion in sublime I do not find it very intelligence, the SublimeCodeIntel is better than the one that Anaconda uses but the completions are not as verbose as in the IDEs. Now, I am thinking about giving a try to Visual Studio Code Edition (take a look, it sounds good https://marketplace.visualstudio.com/items?itemName=donjayamanne.python). I need an editor for professional software development. What would you recommend to me? -- https://mail.python.org/mailman/listinfo/python-list This message is private and confidential and may also be legally privileged. If you have received this message in error, please email it back to the sender and immediately permanently delete it from your computer system. Please do not read, print, re-transmit, store or act in reliance on it or any attachments. British Airways may monitor email traffic data and also the content of emails, where permitted by law, for the purposes of security and staff training and in order to prevent or detect unauthorised use of the British Airways email system. Virus checking of emails (including attachments) is the responsibility of the recipient. British Airways Plc is a public limited company registered in England and Wales. Registered number: 1777777. Registered office: Waterside, PO Box 365, Harmondsworth, West Drayton, Middlesex, England, UB7 0GB. Additional terms and conditions are available on our website: www.ba.com From tim at akwebsoft.com Tue Jan 3 14:13:11 2017 From: tim at akwebsoft.com (Tim Johnson) Date: Tue, 3 Jan 2017 10:13:11 -0900 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <20170103191311.GB18910@mail.akwebsoft.com> * Antonio Caminero Garcia [170102 20:56]: > Guys really thank you for your answers. Basically now I am more > emphasizing in learning in depth a tool and get stick to it so I > can get a fast workflow. Eventually I will learn Vim and its > python developing setup, I know people who have been programming > using Vim for almost 20 years and they did not need to change > editor (that is really awesome). Bye the way, one thing I like about the GUI based vim is that it supports tabs, where emacs does not. And check out the vim plugins for python.... Good Luck! -- Tim http://www.akwebsoft.com, http://www.tj49.com From tim at akwebsoft.com Tue Jan 3 14:15:44 2017 From: tim at akwebsoft.com (Tim Johnson) Date: Wed, 04 Jan 2017 07:15:44 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I Message-ID: <870104349@f38.n261.z1.binkp.net> * Paul Rudin [170103 23:17]: > Tim Johnson writes: > > > * Antonio Caminero Garcia [170102 20:56]: > >> Guys really thank you for your answers. Basically now I am more > >> emphasizing in learning in depth a tool and get stick to it so I > >> can get a fast workflow. Eventually I will learn Vim and its > >> python developing setup, I know people who have been programming > >> using Vim for almost 20 years and they did not need to change > >> editor (that is really awesome). > > > > Bye the way, one thing I like about the GUI based vim is that it > > supports tabs, where emacs does not. > > M-x package-install tabbar :) Thank you. list-packages is my friend ... -- Tim http://www.akwebsoft.com, http://www.tj49.com From python at deborahswanson.net Tue Jan 3 14:46:16 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 11:46:16 -0800 Subject: Clickable hyperlinks In-Reply-To: <001501d264e4$32189a80$27b23dae@sambora> Message-ID: <001e01d265fa$0c639d50$27b23dae@sambora> Excel has a formula: =HYPERLINK(url,description) that will put a clickable link into a cell. Does python have an equivalent function? Probably the most common use for it would be output to the console, similar to a print statement, but clickable. From python at deborahswanson.net Tue Jan 3 14:57:31 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 11:57:31 -0800 Subject: Numpy error In-Reply-To: Message-ID: <002201d265fb$9eb870d0$27b23dae@sambora> > ImportError: > /home/conrado/Canopy/appdata/canopy-1.5.5.3123.rh5-x86_64/lib/ > libgfortran.so.3: > version `GFORTRAN_1.4' not found (required by /lib64/liblapack.so.3) Looks like you need to install the 'GFORTRAN_1.4' plugin into Canopy. I don't know where you'll find it, but Canopy's main website would be a good place to start. Or google "GFORTRAN_1.4 Canopy download". jorge.conrado at cptec.inpe.br wrote, on January 03, 2017 6:45 AM: > > Hi, > > > I alredy used Python and now I have the message: > > import numpy as np > Traceback (most recent call last): > File "", line 1, in > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/__init__.py", > line 153, in > from . import add_newdocs > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/add_newdocs.py", > line 13, in > from numpy.lib import add_newdoc > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/lib/__init__.py", > line 18, in > from .polynomial import * > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/lib/polynomial.py", > line 19, in > from numpy.linalg import eigvals, lstsq, inv > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/linalg/__ini > t__.py", > line 50, in > from .linalg import * > File > "/home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site- > packages/numpy-1.8.0-py2.7-linux-x86_64.egg/numpy/linalg/linalg.py", > line 29, in > from numpy.linalg import lapack_lite, _umath_linalg > ImportError: > /home/conrado/Canopy/appdata/canopy-1.5.5.3123.rh5-x86_64/lib/ > libgfortran.so.3: > version `GFORTRAN_1.4' not found (required by /lib64/liblapack.so.3) > > > I did: pip istall --user numpy > > > Requirement already satisfied: numpy in > /home/conrado/Enthought/Canopy_64bit/User/lib/python2.7/site-p > ackages/numpy-1.8.0-py2.7-linux-x86_64.egg > > > What can I do to solve this message. > > > Thanks, > > > Conrado > -- > https://mail.python.org/mailman/listinfo/python-list > From darcy at vex.net Tue Jan 3 15:03:08 2017 From: darcy at vex.net (D'Arcy Cain) Date: Wed, 04 Jan 2017 08:03:08 +1200 Subject: Clickable hyperlinks Message-ID: <3051536369@f38.n261.z1.binkp.net> Deborah - please trim your quoted text. On 2017-01-04 04:32 AM, Deborah Swanson wrote: > Thanks, Steven. Yes, of course if you want to print strings you must > enclose them in quotes. I think you learn that in Week 1 of any > introductory course on Python. Closer to minute one. When I investigated Python years ago the first thing I learned was; print "Hello, world" > But we aren't trying to print strings here, the point is to produce > clickable links. I didn't enclose them with quotes because I didn't see > any point in printing plain text when I wanted clickable links. I I'm not sure what your links are composed of but mine all look like sequences of characters or "strings." It sounds like you are trying to make URL a first class type like strings. integers, floats, etc. I can't think of any language that treats URLs as first class objects. Even HTML needs quotes: Go here > actually didn't understand why you thought I should print them, but it You want to output them to something. That often involves printing them to a particular handler. > never would have occurred to me that you wanted me to print out a bunch > of silly plain text strings, apparently just for the heck of it. Is that really what you got from his message? > At this point, if I pursue this any farther, it will be to look into how > Firefox takes webpage titles and urls out of its sqlite database and > makes objects you can click on to open the webpages. That's the basic > technology I'd need to find a way to talk (write) python into doing. I can assure you that FF prints the string at some point. It may wrap it in HTML tags first but printing is what it does. Also, the URLs are stored as strings. SQLite has no URL type. If it did then it would still store it as a string somewhere. PostGreSQL would let you create a URL type if you wanted but you would still need to wrap it in quotes (single in this case) when you created the entry. > If it's even worth it. On a practical level it's not worth it, way too > much work for the teensy advantage of having it to use. It might be some > giggles to figure out how to do it and maybe I will sometime just for > funsies. In all the messages in this thread I still don't understand what this "teensy advantage" is supposed to be. Do you want to be able to do this: make_web_link(http://...) instead of: make_web_link("http://...") -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From paul.nospam at rudin.co.uk Tue Jan 3 15:04:44 2017 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 04 Jan 2017 08:04:44 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do References: <4053936668@f38.n261.z1.binkp.net> Message-ID: <2996724295@f38.n261.z1.binkp.net> Tim Johnson writes: > * Antonio Caminero Garcia [170102 20:56]: >> Guys really thank you for your answers. Basically now I am more >> emphasizing in learning in depth a tool and get stick to it so I >> can get a fast workflow. Eventually I will learn Vim and its >> python developing setup, I know people who have been programming >> using Vim for almost 20 years and they did not need to change >> editor (that is really awesome). > > Bye the way, one thing I like about the GUI based vim is that it > supports tabs, where emacs does not. M-x package-install tabbar From paul.nospam at rudin.co.uk Tue Jan 3 15:04:44 2017 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 04 Jan 2017 08:04:44 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do Message-ID: <1210886351@f38.n261.z1.binkp.net> Tim Johnson writes: > * Antonio Caminero Garcia [170102 20:56]: >> Guys really thank you for your answers. Basically now I am more >> emphasizing in learning in depth a tool and get stick to it so I >> can get a fast workflow. Eventually I will learn Vim and its >> python developing setup, I know people who have been programming >> using Vim for almost 20 years and they did not need to change >> editor (that is really awesome). > > Bye the way, one thing I like about the GUI based vim is that it > supports tabs, where emacs does not. M-x package-install tabbar From python at deborahswanson.net Tue Jan 3 15:06:43 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 12:06:43 -0800 Subject: Screwing Up looping in Generator In-Reply-To: <7c38d03e-566c-4d36-9e41-84f913b2e097@googlegroups.com> Message-ID: <002801d265fc$e7c66ab0$27b23dae@sambora> Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > Hi > > This is simple, but its getting me confused. > > I have a csv writer that opens a file and loops each line of > the file for each file and then closes, writing one file. > > I want to alter the behaviour to be a written file for each > input file. I saw a roundrobin example however it failed for > me as you cannot get len(generator) to use a while loop on. > it exhausts > > should I use the same for again after the with open? > > rootobs in this code is my generator and I am already looping > it however > def data_attr(roots): > """Get the root object and iter items.""" > for file in rootobs: > base = os.path.basename(file.name) > write_to = os.path.join("output", > os.path.splitext(base)[0] + ".csv") > with open(write_to, 'w', newline='') as csvf: > race_writer = csv.writer(csvf, delimiter=',') > race_writer.writerow( > ["meet_id", "meet_venue", "meet_date", "meet_rail", > ... > # other categories here > ... > "jockeysurname", "jockeyfirstname"]) > for xml_data in roots: > ... > # parsing code > for noms in race_child: > if noms.tag == 'nomination': > race_writer.writerow( > [meet_id, meet_venue, meet_date, > ... > #parsing info removed > noms.get("jockeyfirstname")]) > > Cheers > Sayth What's the code for your generator? And I don't see where you call 'next'. From uri at speedy.net Tue Jan 3 15:07:38 2017 From: uri at speedy.net (Uri Even-Chen) Date: Tue, 3 Jan 2017 22:07:38 +0200 Subject: pip install -r requirements.txt fails with Python 3.6 on Windows 10 In-Reply-To: References: Message-ID: Thank you, I'll consider to update our requirements to latest versions of all packages. Last time I checked in 22th December 2016 and all our requirements were the latest versions. In the meantime we can keep using Python 3.5. By the way, Travis CI tests passed with the same requirements and Python 3.6 (and 3.5 and 3.4). How did it install the requirements there? Does it depend on the operating system? I see now that Python 3.6.0 was released on 2016-12-23. By the way we use Ubuntu 16.04 in production with Python 3.5.2, so it's not that important to support Python 3.6 right now. What are the reasons to upgrade Python to 3.6? Thanks, Uri. *Uri Even-Chen* [image: photo] Phone: +972-54-3995700 Email: uri at speedy.net Website: http://www.speedysoftware.com/uri/en/ From python at deborahswanson.net Tue Jan 3 15:17:01 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 12:17:01 -0800 Subject: Screwing Up looping in Generator Message-ID: <002d01d265fe$5840b560$27b23dae@sambora> > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > Hi > > > > This is simple, but its getting me confused. > > > > I have a csv writer that opens a file and loops each line of > > the file for each file and then closes, writing one file. > > > > I want to alter the behaviour to be a written file for each > > input file. I saw a roundrobin example however it failed for > > me as you cannot get len(generator) to use a while loop on. > > it exhausts > > > > should I use the same for again after the with open? > > > > rootobs in this code is my generator and I am already looping > > it however > > def data_attr(roots): > > """Get the root object and iter items.""" > > for file in rootobs: > > base = os.path.basename(file.name) > > write_to = os.path.join("output", > > os.path.splitext(base)[0] + ".csv") > > with open(write_to, 'w', newline='') as csvf: > > race_writer = csv.writer(csvf, delimiter=',') > > race_writer.writerow( > > ["meet_id", "meet_venue", "meet_date", "meet_rail", > > ... > > # other categories here > > ... > > "jockeysurname", "jockeyfirstname"]) > > for xml_data in roots: > > ... > > # parsing code > > for noms in race_child: > > if noms.tag == 'nomination': > > race_writer.writerow( > > [meet_id, meet_venue, meet_date, > > ... > > #parsing info removed > > > noms.get("jockeyfirstname")]) > > > > Cheers > > Sayth > > What's the code for your generator? And I don't see where you > call 'next'. I think you're expecting for file in rootobs to get the next yield for you from rootobs, but unless someone corrects me, I don't think you can expect a 'for' statement to do that. You need to have a 'next' statement inside your for loop to get the next yield from the generator. But I might not understand exactly what you're asking. From paul.nospam at rudin.co.uk Tue Jan 3 15:22:02 2017 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 04 Jan 2017 08:22:02 +1200 Subject: Clickable hyperlinks References: <973176847@f38.n261.z1.binkp.net> Message-ID: <4043841764@f38.n261.z1.binkp.net> "Deborah Swanson" writes: > > I didn't try printing them before, but I just did. Got: > >>>> print([Example](http://www.example.com) > > SyntaxError: invalid syntax (arrow pointing at the colon) > With respect, if you typed that at python then it's probably a good idea to take a step back and work through the excellent tutorial. https://docs.python.org/3/tutorial/ From breamoreboy at gmail.com Tue Jan 3 15:51:06 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Tue, 3 Jan 2017 12:51:06 -0800 (PST) Subject: pip install -r requirements.txt fails with Python 3.6 on Windows 10 In-Reply-To: References: Message-ID: <9267d08b-23ac-4173-af90-cd4fa7bd7a28@googlegroups.com> On Tuesday, January 3, 2017 at 8:08:37 PM UTC, Uri Even-Chen wrote: > Thank you, I'll consider to update our requirements to latest versions of > all packages. Last time I checked in 22th December 2016 and all our > requirements were the latest versions. In the meantime we can keep using > Python 3.5. By the way, Travis CI tests passed with the same requirements > and Python 3.6 (and 3.5 and 3.4). How did it install the requirements > there? Does it depend on the operating system? > > I see now that Python 3.6.0 was released on 2016-12-23. > > By the way we use Ubuntu 16.04 in production with Python 3.5.2, so it's not > that important to support Python 3.6 right now. What are the reasons to > upgrade Python to 3.6? > > Thanks, > Uri. > > > *Uri Even-Chen* > [image: photo] Phone: +972-54-3995700 > Website: http://www.speedysoftware.com/uri/en/ > > > Go here http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow to get what you need. In all my years of downloading from this site I've never, ever had a problem. Kindest regards. Mark Lawrence. From python at deborahswanson.net Tue Jan 3 15:53:01 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 12:53:01 -0800 Subject: Screwing Up looping in Generator In-Reply-To: <002d01d265fe$5840b560$27b23dae@sambora> Message-ID: <003501d26603$5fe3def0$27b23dae@sambora> > > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > > > Hi > > > > > > This is simple, but its getting me confused. > > > > > > I have a csv writer that opens a file and loops each line of the > > > file for each file and then closes, writing one file. > > > > > > I want to alter the behaviour to be a written file for each input > > > file. I saw a roundrobin example however it failed for me as you > > > cannot get len(generator) to use a while loop on. it exhausts > > > > > > should I use the same for again after the with open? > > > > > > rootobs in this code is my generator and I am already looping it > > > however def data_attr(roots): > > > """Get the root object and iter items.""" > > > for file in rootobs: > > > base = os.path.basename(file.name) > > > write_to = os.path.join("output", > > > os.path.splitext(base)[0] + ".csv") > > > with open(write_to, 'w', newline='') as csvf: > > > race_writer = csv.writer(csvf, delimiter=',') > > > race_writer.writerow( > > > ["meet_id", "meet_venue", "meet_date", > "meet_rail", > > > ... > > > # other categories here > > > ... > > > "jockeysurname", "jockeyfirstname"]) > > > for xml_data in roots: > > > ... > > > # parsing code > > > for noms in race_child: > > > if noms.tag == 'nomination': > > > race_writer.writerow( > > > [meet_id, meet_venue, > meet_date, > > > ... > > > #parsing info removed > > > > > noms.get("jockeyfirstname")]) > > > > > > Cheers > > > Sayth > > > > What's the code for your generator? And I don't see where you > > call 'next'. > > I think you're expecting > > for file in rootobs > > to get the next yield for you from rootobs, but unless > someone corrects me, I don't think you can expect a 'for' > statement to do that. You need to have a 'next' statement > inside your for loop to get the next yield from the generator. > > But I might not understand exactly what you're asking. You probably want something like : for f in rootobs: file = next base = os.path.basename(file.name) . . . (etc) Notice I changed your iterating variable name to 'f', so you can use 'file' throughout your code after you get the next one from rootobs. As written, you'll get a StopIteration exception when rootobs runs out of files, which you can catch with a try/except. Or you can just let the code end there if you're done. From jeanpierreda at gmail.com Tue Jan 3 15:57:08 2017 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Tue, 3 Jan 2017 12:57:08 -0800 Subject: Clickable hyperlinks In-Reply-To: <001e01d265fa$0c639d50$27b23dae@sambora> References: <001501d264e4$32189a80$27b23dae@sambora> <001e01d265fa$0c639d50$27b23dae@sambora> Message-ID: Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to make an URL clickable is to use a terminal that makes URLs clickable, and print the URL: print("%s: %s" % (description, url)) -- Devin On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson wrote: > Excel has a formula: > > =HYPERLINK(url,description) > > that will put a clickable link into a cell. > > Does python have an equivalent function? Probably the most common use > for it would be output to the console, similar to a print statement, but > clickable. > > -- > https://mail.python.org/mailman/listinfo/python-list > From darcy at vex.net Tue Jan 3 15:58:02 2017 From: darcy at vex.net (D'Arcy Cain) Date: Wed, 04 Jan 2017 08:58:02 +1200 Subject: Clickable hyperlinks Message-ID: <59259788@f38.n261.z1.binkp.net> On 2017-01-04 08:44 AM, Rodrigo Bistolfi wrote: > 2017-01-04 7:39 GMT-03:00 Steve D'Aprano : >> Aside: you've actually raised a fascinating question. I wonder whether >> there >> are any programming languages that understand URLs as native data types, so >> that *source code* starting with http:// etc is understood in the same way >> that source code starting with [ is seen as a list or { as a dict? > > Some Smalltalk implementations have something that comes close: > > st> 'https://python.org' asUrl retrieveContents But notice that even here the URL has to be defined as a string. To be a first class object you would need to do something like this: url = https://python.org/ The only time you would see that is in config files and languages like shell where everything is a string so no quotes necessary. They are implied. > `asUrl` would be a string method returning a URL instance, which also has a > convenient method `retrieveContents` wrapping an http client. Not hard to > do with Python, I think this could be an interesting exercise for a learner. Sure but the issue is, what to do with it. In any case, it's still just a wrapper around various string methods. You still need to give the class a string to create the object. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From python at deborahswanson.net Tue Jan 3 16:19:17 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 13:19:17 -0800 Subject: Screwing Up looping in Generator Message-ID: <003c01d26607$0adbd8f0$27b23dae@sambora> > > > Sayth Renshaw wrote, on January 03, 2017 6:54 AM > > > > > > > > Hi > > > > > > > > This is simple, but its getting me confused. > > > > > > > > I have a csv writer that opens a file and loops each line of the > > > > file for each file and then closes, writing one file. > > > > > > > > I want to alter the behaviour to be a written file for > each input > > > > file. I saw a roundrobin example however it failed for > me as you > > > > cannot get len(generator) to use a while loop on. it exhausts > > > > > > > > should I use the same for again after the with open? > > > > > > > > rootobs in this code is my generator and I am already looping it > > > > however def data_attr(roots): > > > > """Get the root object and iter items.""" > > > > for file in rootobs: > > > > base = os.path.basename(file.name) > > > > write_to = os.path.join("output", > > > > os.path.splitext(base)[0] + ".csv") > > > > with open(write_to, 'w', newline='') as csvf: > > > > race_writer = csv.writer(csvf, delimiter=',') > > > > race_writer.writerow( > > > > ["meet_id", "meet_venue", "meet_date", > > "meet_rail", > > > > ... > > > > # other categories here > > > > ... > > > > "jockeysurname", "jockeyfirstname"]) > > > > for xml_data in roots: > > > > ... > > > > # parsing code > > > > for noms in race_child: > > > > if noms.tag == 'nomination': > > > > race_writer.writerow( > > > > [meet_id, meet_venue, > > meet_date, > > > > ... > > > > #parsing info removed > > > > > > > noms.get("jockeyfirstname")]) > > > > > > > > Cheers > > > > Sayth > > > > > > What's the code for your generator? And I don't see where > you call > > > 'next'. > > > > I think you're expecting > > > > for file in rootobs > > > > to get the next yield for you from rootobs, but unless > > someone corrects me, I don't think you can expect a 'for' > > statement to do that. You need to have a 'next' statement > > inside your for loop to get the next yield from the generator. > > > > But I might not understand exactly what you're asking. > > You probably want something like : > > for f in rootobs: > file = next > base = os.path.basename(file.name) > . > . > . > (etc) > > Notice I changed your iterating variable name to 'f', so you > can use 'file' throughout your code after you get the next > one from rootobs. > > As written, you'll get a StopIteration exception when rootobs > runs out of files, which you can catch with a try/except. Or > you can just let the code end there if you're done. Well rats, I'm embarrassed. It's been awhile since I've used a generator and I forgot that you have to create the generator object first and use it to call the next function. And I really don't think you can use a generator as your range in a for loop. So I'd use a 'while True', and break out of the loop when you hit the StopIteration exception: files = rootobs() while True: try: file = files.next() except StopIteration: break base = os.path.basename(file.name) . . . (etc) (Now I'm just going to shut up, until somebody else says something.) From rosuav at gmail.com Tue Jan 3 16:30:48 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 09:30:48 +1200 Subject: Screwing Up looping in Generator Message-ID: <655490577@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson wrote: > while True: > try: > file = files.next() > except StopIteration: > break Small side point: Try to avoid calling a generator object's .next() method directly. Normally, when you _do_ want to do this, you should be calling next(files). In Python 3, the magic method is now __next__(), which emphasizes that it's for defining, not calling. As others have pointed out, though, the for loop is the correct tool for this job. ChrisA From python at deborahswanson.net Tue Jan 3 16:34:49 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 13:34:49 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <003f01d26609$36cd78e0$27b23dae@sambora> Devin Jeanpierre wrote, on January 03, 2017 12:57 PM >Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to >make an URL clickable is to use a terminal that makes URLs clickable, and >print the URL: > > >print("%s: %s" % (description, url)) > > > > >-- Devin I'm sorry, I should have said a GUI console because I wouldn't expect a text-based console to produce clickable links. But it appears that a simple GUI console can't do it either. I have 3 GUI consoles and in all 3, when I ask: print("%s: %s" % ("python.org list", "https://mail.python.org/mailman/listinfo/python-list")) I get: python.org list: https://mail.python.org/mailman/listinfo/python-list (Outlook made the url clickable here, the python GUI consoles just output plain text.) Pretty clearly the output is plain text because your format parameters are %s. Maybe the trick, if there is one, is in a format parameter for urls. On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson wrote: Excel has a formula: =HYPERLINK(url,description) that will put a clickable link into a cell. Does python have an equivalent function? Probably the most common use for it would be output to the console, similar to a print statement, but clickable. -- https://mail.python.org/mailman/listinfo/python-list From D.Strohl at F5.com Tue Jan 3 16:34:57 2017 From: D.Strohl at F5.com (Dan Strohl) Date: Tue, 3 Jan 2017 21:34:57 +0000 Subject: Clickable hyperlinks In-Reply-To: References: <001501d264e4$32189a80$27b23dae@sambora> <001e01d265fa$0c639d50$27b23dae@sambora> Message-ID: <70378eddf25145b7b7d58783cdb9234b@F5.com> The best bet (unless you know that you are outputting to a specific place, like html or excel) is to always include the "https://" or "http://" since most of the consoles / terminals that support clickable links are parsing them based on "seeing" the initial "http://". If your output just looks like "mysite.com/coolpage.html", many systems will simply ignore them. At one point I had made a class that you could pass the link to, and then you could request different output based on your needs... so basically something like: >> url = url_class("mysite.com/coolpage.html") >> print(url) "http://mysite.com/coolpage.html") >> print(url.plain) "mysite.com/coolpage.html" >> print(url.html('My Site")) 'My Site' (or whatever... I think I actually just sub-classed url.parse or something) -----Original Message----- From: Python-list [mailto:python-list-bounces+d.strohl=f5.com at python.org] On Behalf Of Devin Jeanpierre Sent: Tuesday, January 03, 2017 12:57 PM To: python at deborahswanson.net Cc: comp.lang.python Subject: Re: Clickable hyperlinks Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to make an URL clickable is to use a terminal that makes URLs clickable, and print the URL: print("%s: %s" % (description, url)) -- Devin On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson wrote: > Excel has a formula: > > =HYPERLINK(url,description) > > that will put a clickable link into a cell. > > Does python have an equivalent function? Probably the most common use > for it would be output to the console, similar to a print statement, > but clickable. > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Tue Jan 3 16:38:50 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 09:38:50 +1200 Subject: How Best to Coerce Python Objects to Integers? Message-ID: <547107055@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 8:41 AM, wrote: > Hi all, I'd suggest that this http://blog.pyspoken.com/2017/01/02/how-best-to -coerce-python-objects-to-integers/ is not one of the greatest articles ever written about Python exception handling. Other opinions are welcome. > """ So there you have it. I? ?m happy with this function. It feels bulletproof. It contains a naked except, but that only covers one simple line of code that? ?s unlikely to hide anything nasty. """ Yep! It's perfect. He has successfully made a function that won't leak any exceptions. Congratulations! The novice believes that the first priority is to stop the program from crashing. The expert understands that crashing (especially with an exception, but even a segfault) is actually very helpful and useful. I love the logic that a bare 'except' is okay as long as it's only covering one single line of code. There's almost enough truth in that to be meaningful, while still completely missing the point that a NameError is almost certainly a bug no matter where it crops up. ChrisA From wrw at mac.com Tue Jan 3 16:41:24 2017 From: wrw at mac.com (William Ray Wing) Date: Wed, 04 Jan 2017 09:41:24 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do Message-ID: <2588366108@f38.n261.z1.binkp.net> > On Jan 4, 2017, at 1:54 AM, Antonio Caminero Garcia wrote: > > On Tuesday, January 3, 2017 at 4:12:34 PM UTC-8, Dietmar Schwertberger wrote: >> On 02.01.2017 12:38, Antonio Caminero Garcia wrote: >> You did not try Wing IDE? It looks less like a spacecraft. Maybe you >> like it. >> Maybe the difference is that Wing is from Python people while the ones >> you listed are from Java people. > > That sounds interesting. By the look of it I think I am going to give it a try. > > [byte] > I want editor with those IDE capabilities and git integration, with optionally cool stuff as for example remote debugging. I use Wing, and I think you will like it. It *is* pythonic, and for what it is worth, offers remote debugging as one of its more recently added features. -Bill From breamoreboy at gmail.com Tue Jan 3 16:41:44 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Tue, 3 Jan 2017 13:41:44 -0800 (PST) Subject: How Best to Coerce Python Objects to Integers? Message-ID: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> Hi all, I'd suggest that this http://blog.pyspoken.com/2017/01/02/how-best-to-coerce-python-objects-to-integers/ is not one of the greatest articles ever written about Python exception handling. Other opinions are welcome. Kindest regards. Mark Lawrence. From random832 at fastmail.com Tue Jan 3 16:43:04 2017 From: random832 at fastmail.com (Random832) Date: Wed, 04 Jan 2017 09:43:04 +1200 Subject: Simulating int arithmetic with wrap-around Message-ID: <1046720962@f38.n261.z1.binkp.net> On Fri, Dec 30, 2016, at 09:47, Steve D'Aprano wrote: > Again, assume both operands are in range for an N-bit signed integer. > What's > a good way to efficiently, or at least not too inefficiently, do the > calculations in Python? I'd do something like: bit_mask = (1 << bits) - 1 # 0xFFFF sign_bit = 1 << (bits - 1) # 0x8000 sign_ext = ~bit_mask # ...FFFFF0000 def signed(value): if value & sign_bit: return value | sign_ext else: return value & bit_mask def unsigned(value): return value & bit_mask And also avoid doing it on intermediate steps where it can be shown to not affect the result or allow the value to grow too large. From m at funkyhat.org Tue Jan 3 16:46:57 2017 From: m at funkyhat.org (Matt Wheeler) Date: Tue, 03 Jan 2017 21:46:57 +0000 Subject: Screwing Up looping in Generator In-Reply-To: <002d01d265fe$5840b560$27b23dae@sambora> References: <002d01d265fe$5840b560$27b23dae@sambora> Message-ID: On Tue, 3 Jan 2017 at 20:17 Deborah Swanson wrote: > > What's the code for your generator? And I don't see where you > > call 'next'. > > I think you're expecting > > for file in rootobs > > to get the next yield for you from rootobs, but unless someone corrects > me, I don't think you can expect a 'for' statement to do that. You need > to have a 'next' statement inside your for loop to get the next yield > from the generator. > Yes, you absolutely can expect a for statement to do that. It will accept any iterable as its `y`. `for x in y: stuff(x)` is effectively syntactic sugar for: iterator = iter(y) while True: try: x = next(iterator) except(StopIteration): break stuff(x) Manually calling `next()` inside a `while True:` is quite an unusual thing to do. range() is not part of the for syntax at all, it's completely separate, it simply returns an iterator which the for loop can use, like any other. -- -- Matt Wheeler http://funkyh.at From rosuav at gmail.com Tue Jan 3 16:47:16 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 09:47:16 +1200 Subject: How Best to Coerce Python Objects to Integers? Message-ID: <4059713349@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: > Aside from calling "except Exception" a "naked except" If you read the comments, you'll see that he originally had an actual bare except clause, but then improved the code somewhat in response to a recommendation that SystemExit etc not be caught. ChrisA From D.Strohl at F5.com Tue Jan 3 16:47:58 2017 From: D.Strohl at F5.com (Dan Strohl) Date: Tue, 3 Jan 2017 21:47:58 +0000 Subject: Clickable hyperlinks In-Reply-To: <003f01d26609$36cd78e0$27b23dae@sambora> References: <003f01d26609$36cd78e0$27b23dae@sambora> Message-ID: <93636ab19e434c029e2cdf1f14e8809a@F5.com> Keeping mind how this all works... Python is providing the data, the console/terminal/app handles how that data is displayed. There is no specification for text output to be hyperlinked (that I know about at least), so while some apps may handle specific coding to tell them that "this text should be a hyperlink", most will either parse the text looking for hyper-linkable string, or more commonly, just output the text as text. If you control the app that is displaying info to the user (such as using QT or another GUI tool), or generating html yourself, you can control if a text string is a hyperlink or not, and what to do with the hyperlink when clicked. So, if you want clickable links, you would need to find a console/terminal that supported them. Some references: https://opensource.com/life/15/11/top-open-source-terminal-emulators (see Gnome) http://unix.stackexchange.com/questions/63417/is-there-a-terminal-app-that-allows-filenames-to-be-clickable http://superuser.com/questions/654116/configure-os-x-terminal-to-detect-urls-and-make-them-clickable http://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/url-launching.html -----Original Message----- From: Python-list [mailto:python-list-bounces+d.strohl=f5.com at python.org] On Behalf Of Deborah Swanson Sent: Tuesday, January 03, 2017 1:35 PM To: 'Devin Jeanpierre' Cc: 'comp.lang.python' Subject: RE: Re: Clickable hyperlinks Devin Jeanpierre wrote, on January 03, 2017 12:57 PM >Sadly, no. :( Consoles (and stdout) are just text, not hypertext. The way to >make an URL clickable is to use a terminal that makes URLs clickable, and >print the URL: > > >print("%s: %s" % (description, url)) > > > > >-- Devin I'm sorry, I should have said a GUI console because I wouldn't expect a text-based console to produce clickable links. But it appears that a simple GUI console can't do it either. I have 3 GUI consoles and in all 3, when I ask: print("%s: %s" % ("python.org list", "https://mail.python.org/mailman/listinfo/python-list")) I get: python.org list: https://mail.python.org/mailman/listinfo/python-list (Outlook made the url clickable here, the python GUI consoles just output plain text.) Pretty clearly the output is plain text because your format parameters are %s. Maybe the trick, if there is one, is in a format parameter for urls. On Tue, Jan 3, 2017 at 11:46 AM, Deborah Swanson wrote: Excel has a formula: =HYPERLINK(url,description) that will put a clickable link into a cell. Does python have an equivalent function? Probably the most common use for it would be output to the console, similar to a print statement, but clickable. -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list From tjreedy at udel.edu Tue Jan 3 16:48:48 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 3 Jan 2017 16:48:48 -0500 Subject: pip install -r requirements.txt fails with Python 3.6 on Windows 10 In-Reply-To: References: Message-ID: On 1/3/2017 3:07 PM, Uri Even-Chen wrote: > What are the reasons to upgrade Python to 3.6? The same as for any new version: New features -- see What's New in 3.6. New bug fixes. New performance improvements. Reasons against: The effort to make sure all dependencies are available for 3.6* Possible regressions. * For Windows, any of the 381 wheels available from http://www.lfd.uci.edu/~gohlke/pythonlibs/ should be available in 3.6 versions (unless there was a major problem in recompiling). -- Terry Jan Reedy From m at funkyhat.org Tue Jan 3 16:49:22 2017 From: m at funkyhat.org (Matt Wheeler) Date: Tue, 03 Jan 2017 21:49:22 +0000 Subject: Screwing Up looping in Generator In-Reply-To: References: <002d01d265fe$5840b560$27b23dae@sambora> Message-ID: On Tue, 3 Jan 2017 at 21:46 Matt Wheeler wrote: > range() is not part of the for syntax at all, it's completely separate, it > simply returns an iterator which the for loop can use, like any other. > *iterable -- -- Matt Wheeler http://funkyh.at From rgaddi at highlandtechnology.invalid Tue Jan 3 16:50:26 2017 From: rgaddi at highlandtechnology.invalid (Rob Gaddi) Date: Wed, 04 Jan 2017 09:50:26 +1200 Subject: Simulating int arithmetic with wrap-around References: <1195249011@f38.n261.z1.binkp.net> Message-ID: <3975862436@f38.n261.z1.binkp.net> On 01/03/2017 10:00 PM, Gregory Ewing wrote: > Paul Rubin wrote: >> My first thought is towards the struct module, especially if you want to >> handle a bunch of such integers at the same time. Or maybe the array >> module or some combination. > > Or possibly numpy. > Agreed. If you had to do a lot of calculations on arbitrary sized signed/unsigned ints, figuring how to parallelize them into numpy arrays would save you a ton of time. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. From tjreedy at udel.edu Tue Jan 3 16:57:52 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 3 Jan 2017 16:57:52 -0500 Subject: Screwing Up looping in Generator In-Reply-To: <003501d26603$5fe3def0$27b23dae@sambora> References: <002d01d265fe$5840b560$27b23dae@sambora> <003501d26603$5fe3def0$27b23dae@sambora> Message-ID: On 1/3/2017 3:53 PM, Deborah Swanson wrote: >> I think you're expecting >> >> for file in rootobs >> >> to get the next yield for you from rootobs, but unless >> someone corrects me, I don't think you can expect a 'for' >> statement to do that. You need to have a 'next' statement >> inside your for loop to get the next yield from the generator. As I read this, out of context, it is wrong. It it very unusual to call next on the current iterator (here next(rootobs)), inside a for loop. > You probably want something like : > > for f in rootobs: > file = next This is definitely wrong, as it makes 'find' an alias for the next() function. > base = os.path.basename(file.name) and file.name will be an AttributeError. --- If one wants to iterate through files and lines within files, which I believe I saw in this thread, one should have a for loop within a for loop. -- Terry Jan Reedy From python at deborahswanson.net Tue Jan 3 17:14:50 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 14:14:50 -0800 Subject: Screwing Up looping in Generator In-Reply-To: Message-ID: <006101d2660e$cdca05b0$27b23dae@sambora> Terry Reedy > > On 1/3/2017 3:53 PM, Deborah Swanson wrote: > > >> I think you're expecting > >> > >> for file in rootobs > >> > >> to get the next yield for you from rootobs, but unless someone > >> corrects me, I don't think you can expect a 'for' statement to do > >> that. You need to have a 'next' statement inside your for > loop to get > >> the next yield from the generator. > > As I read this, out of context, it is wrong. It it very > unusual to call > next on the current iterator (here next(rootobs)), inside a for loop. > > > You probably want something like : > > > > for f in rootobs: > > file = next > > This is definitely wrong, as it makes 'find' an alias for the next() > function. > > > base = os.path.basename(file.name) > > and file.name will be an AttributeError. > > --- > If one wants to iterate through files and lines within files, which I > believe I saw in this thread, one should have a for loop > within a for loop. > > -- > Terry Jan Reedy Yes, my first attempts were screwups. I didn't remember generator usage correctly, but I believe my last answer was correct: ...you have to create the generator object first and use it to call the next function. And I really don't think you can use a generator as your range in a for loop. So I'd use a 'while True', and break out of the loop when you hit the StopIteration exception: files = rootobs() while True: try: file = files.next() except StopIteration: break base = os.path.basename(file.name) . . . (etc) From maillist at schwertberger.de Tue Jan 3 17:20:37 2017 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Tue, 3 Jan 2017 23:20:37 +0100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> On 02.01.2017 12:38, Antonio Caminero Garcia wrote: > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. You did not try Wing IDE? It looks less like a spacecraft. Maybe you like it. Maybe the difference is that Wing is from Python people while the ones you listed are from Java people. For something completely different (microcontroller programming in C) I just switched to a Eclipse derived IDE and I don't like it too much as the tool does not focus on the problem scope. From your posts I'm not sure whether you want an editor or an IDE, where for me the main difference is the debugger and code completion. I would not want to miss the IDE features any more, even though in my first 15 years of Python I thought that a debugger is optional with Python ... Regards, Dietmar From python.list at tim.thechases.com Tue Jan 3 17:26:16 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 3 Jan 2017 16:26:16 -0600 Subject: Clickable hyperlinks In-Reply-To: <001e01d265fa$0c639d50$27b23dae@sambora> References: <001501d264e4$32189a80$27b23dae@sambora> <001e01d265fa$0c639d50$27b23dae@sambora> Message-ID: <20170103162616.7761ffcc@bigbox.christie.dr> On 2017-01-03 11:46, Deborah Swanson wrote: > Excel has a formula: > > =HYPERLINK(url,description) > > that will put a clickable link into a cell. > > Does python have an equivalent function? Probably the most common > use for it would be output to the console, similar to a print > statement, but clickable. Depends on what you're outputting. In your context, you're creating content (and metadata) for a cell in an Excel workbook. If that's the case you might have to use something like the xlwt module to create an Excel-style worksheet and adjust the properties of the cell to include the hyperlink property. Or you can write out a .csv file with a hyperlink in a cell, which I believe Excel can interpret as a hyperlink. Or write an HTML document with the corresponding HTML tag in it. Or you can just print it to stdout as normal as some terminals detect them and auto-linkify them. But you have to specify where you want this link to appear to know how to solve the problem. -tkc From rosuav at gmail.com Tue Jan 3 17:30:48 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 09:30:48 +1100 Subject: Screwing Up looping in Generator In-Reply-To: <003c01d26607$0adbd8f0$27b23dae@sambora> References: <003c01d26607$0adbd8f0$27b23dae@sambora> Message-ID: On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson wrote: > while True: > try: > file = files.next() > except StopIteration: > break Small side point: Try to avoid calling a generator object's .next() method directly. Normally, when you _do_ want to do this, you should be calling next(files). In Python 3, the magic method is now __next__(), which emphasizes that it's for defining, not calling. As others have pointed out, though, the for loop is the correct tool for this job. ChrisA From rosuav at gmail.com Tue Jan 3 17:34:58 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 10:34:58 +1200 Subject: Screwing Up looping in Generator Message-ID: <2629345252@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 10:05 AM, Deborah Swanson wrote: > Ok, I learned how to use generators in Python 2.7.8, which may be > different from Python 3 for generators. But I learned from MIT's online > introduction to python course, and they certainly seem to know python > well. So what is the correct way to call the generator's next yield in > Python 3? We only learned to use the next function. If you don't use the > next function, what do you use? The built-in next function, not the next method. # don't do this gen.next() # do this next(gen) ChrisA From rosuav at gmail.com Tue Jan 3 17:38:50 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 09:38:50 +1100 Subject: How Best to Coerce Python Objects to Integers? In-Reply-To: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> Message-ID: On Wed, Jan 4, 2017 at 8:41 AM, wrote: > Hi all, I'd suggest that this http://blog.pyspoken.com/2017/01/02/how-best-to-coerce-python-objects-to-integers/ is not one of the greatest articles ever written about Python exception handling. Other opinions are welcome. > """ So there you have it. I?m happy with this function. It feels bulletproof. It contains a naked except, but that only covers one simple line of code that?s unlikely to hide anything nasty. """ Yep! It's perfect. He has successfully made a function that won't leak any exceptions. Congratulations! The novice believes that the first priority is to stop the program from crashing. The expert understands that crashing (especially with an exception, but even a segfault) is actually very helpful and useful. I love the logic that a bare 'except' is okay as long as it's only covering one single line of code. There's almost enough truth in that to be meaningful, while still completely missing the point that a NameError is almost certainly a bug no matter where it crops up. ChrisA From steve+python at pearwood.info Tue Jan 3 17:39:20 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 10:39:20 +1200 Subject: How Best to Coerce Python Objects to Integers? References: <4059713349@f38.n261.z1.binkp.net> Message-ID: <1336731314@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 09:47 am, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: >> Aside from calling "except Exception" a "naked except" > > If you read the comments, you'll see that he originally had an actual > bare except clause, but then improved the code somewhat in response to > a recommendation that SystemExit etc not be caught. But why? That makes no sense. If his intention is to return None on failure, no matter what happens, then he *should* catch SystemExit. Otherwise: class Unint: def __int__(self): raise SystemExit int_or_else(Unint) will exit instead of returning None. Surely that's not what he wants, given that he wants to cover up other programming errors like NameError and TypeError? The problem here is not so much the use of try...except but the *intention* that "Anything whatsoever should be coerced to int". If you have something like: left_margin = int_or_else(ftp_server) that's surely a programming error that needs fixing, rather than something that should just return a default value. -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From ethan at stoneleaf.us Tue Jan 3 17:42:52 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 03 Jan 2017 14:42:52 -0800 Subject: How Best to Coerce Python Objects to Integers? In-Reply-To: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> Message-ID: <586C28EC.8010607@stoneleaf.us> On 01/03/2017 01:41 PM, breamoreboy at gmail.com wrote: > Hi all, I'd suggest that this [1] is not one of the greatest articles > ever written about Python exception handling. Other opinions are welcome. Aside from calling "except Exception" a "naked except" I think it's decent. He walks through the problem, explains the rationale, and only has one line of code guarded by the except clause. -- ~Ethan~ [1] http://blog.pyspoken.com/2017/01/02/how-best-to-coerce-python-objects-to-integers/ From rbistolfi at gmail.com Tue Jan 3 17:44:34 2017 From: rbistolfi at gmail.com (Rodrigo Bistolfi) Date: Wed, 04 Jan 2017 10:44:34 +1200 Subject: Clickable hyperlinks Message-ID: <1325525650@f38.n261.z1.binkp.net> 2017-01-04 7:39 GMT-03:00 Steve D'Aprano : > On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote: > > Aside: you've actually raised a fascinating question. I wonder whether > there > are any programming languages that understand URLs as native data types, so > that *source code* starting with http:// etc is understood in the same way > that source code starting with [ is seen as a list or { as a dict? > ... > Some Smalltalk implementations have something that comes close: st> 'https://python.org' asUrl retrieveContents `asUrl` would be a string method returning a URL instance, which also has a convenient method `retrieveContents` wrapping an http client. Not hard to do with Python, I think this could be an interesting exercise for a learner. From rosuav at gmail.com Tue Jan 3 17:47:17 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 09:47:17 +1100 Subject: How Best to Coerce Python Objects to Integers? In-Reply-To: <586C28EC.8010607@stoneleaf.us> References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> <586C28EC.8010607@stoneleaf.us> Message-ID: On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: > Aside from calling "except Exception" a "naked except" If you read the comments, you'll see that he originally had an actual bare except clause, but then improved the code somewhat in response to a recommendation that SystemExit etc not be caught. ChrisA From rosuav at gmail.com Tue Jan 3 17:56:42 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 10:56:42 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <3575573748@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 10:49 AM, wrote: > Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? > > #mynumber.py > # this game uses a home made function > import random > > #think of a number > computer_number = number.randint(1,100) What's wrong is that you aren't showing us the exception you get on this line. *Copy and paste* that exception - the whole thing. It's very helpful. ChrisA From rosuav at gmail.com Tue Jan 3 17:59:18 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 10:59:18 +1200 Subject: How Best to Coerce Python Objects to Integers? Message-ID: <3493567800@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 10:39 AM, Steve D'Aprano wrote: > The problem here is not so much the use of try...except but the *intention* > that "Anything whatsoever should be coerced to int". If you have something > like: > > left_margin = int_or_else(ftp_server) > > that's surely a programming error that needs fixing, rather than something > that should just return a default value. Agreed. There are certainly times when you want to say "coerce any *string* to an int", and there are times when you want to say "coerce any string to int or None" (maybe you want to take the average of a whole lot of values, ignoring the ones that say "N/A"), but I don't know of any time I want to say "coerce any programming error to int or None", unless you count the exit code of a process. I used the word "improved" rather loosely. ChrisA From python at deborahswanson.net Tue Jan 3 18:05:20 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 15:05:20 -0800 Subject: Screwing Up looping in Generator In-Reply-To: Message-ID: <006f01d26615$db87c460$27b23dae@sambora> Chris Angelico wrote, on January 03, 2017 2:31 PM > > On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson > wrote: > > while True: > > try: > > file = files.next() > > except StopIteration: > > break > > Small side point: Try to avoid calling a generator object's > .next() method directly. Normally, when you _do_ want to do > this, you should be calling next(files). In Python 3, the > magic method is now __next__(), which emphasizes that it's > for defining, not calling. > > As others have pointed out, though, the for loop is the > correct tool for this job. > > ChrisA Ok, I learned how to use generators in Python 2.7.8, which may be different from Python 3 for generators. But I learned from MIT's online introduction to python course, and they certainly seem to know python well. So what is the correct way to call the generator's next yield in Python 3? We only learned to use the next function. If you don't use the next function, what do you use? And yes, we usually used for loops for generators, unless you don't know when the generator will be exhausted. As in this case, where the number of files the generator can provide is unknown. Then we used the while True, break on StopIteration method. From flebber.crue at gmail.com Tue Jan 3 18:09:40 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Wed, 04 Jan 2017 11:09:40 +1200 Subject: Screwing Up looping in Generator References: <2751329663@f38.n261.z1.binkp.net> Message-ID: <3187615694@f38.n261.z1.binkp.net> For completeness I was close this is the working code. def get_list_of_names(generator_arg): name_set = set() for name in generator_arg: base = os.path.basename(name.name) filename = os.path.splitext(base)[0] name_set.add(filename) return name_set def data_attr(roots): """Get the root object and iter items.""" for name in names: directory = "output" write_name = name + ".csv" with open(os.path.join(directory, write_name), 'w', newline='') as csvf: race_writer = csv.writer(csvf, delimiter=',' ) thanks for your time and assistance. It's much appreciated Sayth From ethan at stoneleaf.us Tue Jan 3 18:09:52 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 03 Jan 2017 15:09:52 -0800 Subject: How Best to Coerce Python Objects to Integers? In-Reply-To: References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> <586C28EC.8010607@stoneleaf.us> Message-ID: <586C2F40.3050806@stoneleaf.us> On 01/03/2017 02:47 PM, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: >> Aside from calling "except Exception" a "naked except" > > If you read the comments, you'll see that he originally had an actual > bare except clause, but then improved the code somewhat in response to > a recommendation that SystemExit etc not be caught. I did read the comments and noticed the reasons for the improved code; fixing the article to not use the "naked except" phrase would be another improvement. And, of course, whether or not "except Exception" is too broad depends on the use-case. -- ~Ethan~ From grant.b.edwards at gmail.com Tue Jan 3 18:13:11 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 3 Jan 2017 23:13:11 +0000 (UTC) Subject: Clickable hyperlinks References: <003f01d26609$36cd78e0$27b23dae@sambora> Message-ID: On 2017-01-03, Deborah Swanson wrote: > I'm sorry, I should have said a GUI console because I wouldn't expect a > text-based console to produce clickable links. What's a "GUI console"? -- Grant Edwards grant.b.edwards Yow! I want you to MEMORIZE at the collected poems of gmail.com EDNA ST VINCENT MILLAY ... BACKWARDS!! From rosuav at gmail.com Tue Jan 3 18:16:56 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 11:16:56 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <1279173106@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 11:03 AM, Erik wrote: > I doubt it's getting that far (I can see at least one syntax error in the > code pasted). True true. In any case, the point is to copy and paste the error message. Callum, please, copy and paste it. ChrisA From python at deborahswanson.net Tue Jan 3 18:21:10 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 15:21:10 -0800 Subject: Screwing Up looping in Generator In-Reply-To: Message-ID: <007401d26618$11f605f0$27b23dae@sambora> -----Original Message----- From: Matt Wheeler [mailto:m at funkyhat.org] Sent: Tuesday, January 03, 2017 1:47 PM To: python at deborahswanson.net; Sayth Renshaw; python-list at python.org Subject: Re: Screwing Up looping in Generator On Tue, 3 Jan 2017 at 20:17 Deborah Swanson wrote: > What's the code for your generator? And I don't see where you > call 'next'. I think you're expecting for file in rootobs to get the next yield for you from rootobs, but unless someone corrects me, I don't think you can expect a 'for' statement to do that. You need to have a 'next' statement inside your for loop to get the next yield from the generator. Yes, you absolutely can expect a for statement to do that. It will accept any iterable as its `y`. >> And here is someone correcting me, which I sort of suspected there might be. Yes, >> range is an iterator, but I didn't know a generator could also be used as an iterator. >> Makes perfect sense though, a generator does dispense its yields in the sequence >> defined by the generator. `for x in y: stuff(x)` is effectively syntactic sugar for: iterator = iter(y) while True: try: x = next(iterator) except(StopIteration): break stuff(x) Manually calling `next()` inside a `while True:` is quite an unusual thing to do. >> And you don't need to do it very often. The only use case I know is this one, where the number of yields the generator can output is unknown (here, the number of files the generator can access is unknown). You might recall that the original poster wanted to use a while, but didn't know how to terminate it. At least that's how I interpreted what he was saying. range() is not part of the for syntax at all, it's completely separate, it simply returns an iterator which the for loop can use, like any other. -- >> I see that now, I just didn't know a generator could be used as an iterator. -- Matt Wheeler http://funkyh.at From no.email at nospam.invalid Tue Jan 3 18:22:10 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 03 Jan 2017 15:22:10 -0800 Subject: Simulating int arithmetic with wrap-around References: <58667385$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8737gz4tlp.fsf@nightsong.com> Steve D'Aprano writes: > Again, assume both operands are in range for an N-bit signed integer. What's > a good way to efficiently, or at least not too inefficiently, do the > calculations in Python? My first thought is towards the struct module, especially if you want to handle a bunch of such integers at the same time. Or maybe the array module or some combination. Or a C extension. From steve+python at pearwood.info Tue Jan 3 18:26:08 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 11:26:08 +1200 Subject: How Best to Coerce Python Objects to Integers? References: <980704833@f38.n261.z1.binkp.net> Message-ID: <1809108326@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 10:09 am, Ethan Furman wrote: > And, of course, whether or not "except Exception" is too broad depends on > the use-case. I'm having trouble thinking of *any* use-case where this would be useful. His intention, it seems, is to write a function which simply cannot fail, presumably so that he can write code which consumes the output and just keeps going no matter what he throws at it: graph = make_graph() incoming_data = [1, 12, 7, "hello", None, [], {}, 5] for obj in incoming_data: graph.draw_point(int_or_else(obj)) But of course that's impossible: class Evil: def __int__(self): os.abort() Or for that matter: class Evil: def __int__(self): time.sleep(2147483647) So his code still can fail under some circumstances. And so it should. Why is my input data including such evil objects? I should find out why. I'm having trouble seeing how it could be anything but a programming bug. He gives the rationale for this function: A scenario that? ?s more realistic than the Unintable class might be a class that wraps an industrial sensor. Calling int() on an instance normally returns a value representing pressure or temperature. However, it might reasonably raise a SensorNotReadyError. Okay. Let's suppose it can return either an integer as a string, some arbitrary non-numeric string to indicate a sensor error, or raises SensorNotReadyError. Then this would be an appropriate function to use: def int_or_else(value): try: return int(value) except ValueError: assert isinstance(value, str) # consider logging the error? return None except SensorNotReadyError: return None Now when he connects up to the sensor and starts reading values, it will work, but if his input gets contaminated with arbitrary junk objects (a sign of a programming bug in his code) he will find out about it. One possible use-case might be something like Excel, where there are two data types: numbers, and text, and numeric operations on text will just skip them altogether. If you were to build an object-oriented spreadsheet, where the cells can contain any object not just numbers and text, then you could potentially have a situation like: Column A Row 1: 1 Row 2: ftp_server() Row 3: 2 Row 4: 3 Row 5: =sum(A1:A4) and you (arguably) want the result to be 6 rather than some error. Or do you? I can make a good case for skipping text cells, as Excel does, but I'm not sure that ftp_server should be skipped. So I'll count that as a use-case, but a dubious one. Another possible use-case might be the REPL for an interpreter, where you want the REPL to keep going no matter what exceptions take place. But I don't think this is the right way to do that, and it's not how the Python REPL works either. Other than that, I'm not seeing any use-case where this sort of thing is anything but a bad idea. -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From python at lucidity.plus.com Tue Jan 3 18:30:26 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 3 Jan 2017 23:30:26 +0000 Subject: Clickable hyperlinks In-Reply-To: <001e01d265fa$0c639d50$27b23dae@sambora> References: <001e01d265fa$0c639d50$27b23dae@sambora> Message-ID: <4e756eb9-842b-98fe-3fa1-3b05baebe71a@lucidity.plus.com> Hi. On 03/01/17 19:46, Deborah Swanson wrote: > Excel has a formula: When you start a new topic on the list, could you please write a new message rather than replying to an existing message and changing the title/subject? For those reading the list in a threaded email client, this message is shown as a continuation of your "Cleaning up conditionals" thread, and that whole thread in turn shows up as a continuation of the "mentor training python Romania with certification" discussion (which you had presumably "reply"ed to originally) ... Thanks. E. From python at deborahswanson.net Tue Jan 3 18:32:04 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 15:32:04 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <007b01d26619$97de4eb0$27b23dae@sambora> Grant Edwards wrote, on January 03, 2017 3:13 PM > > On 2017-01-03, Deborah Swanson wrote: > > > I'm sorry, I should have said a GUI console because I > wouldn't expect > > a text-based console to produce clickable links. > > What's a "GUI console"? > > -- > Grant Edwards grant.b.edwards Yow! I > want you to MEMORIZE > at the > collected poems of > gmail.com EDNA ST > VINCENT MILLAY The GUI consoles I have are in Pycharm, the IDLE that comes with Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when I open them, so they're capable of opening links, but whether that means their output space is capable of handling clickable links I don't know. I do know printing a full url with the %s specifier or entering a url and clicking enter just gives you the plain text url. Obviously, not all GUI consoles are enabled recognize and make clickable links from correctly formatted urls. I was hoping there was some functionality in python to make clickable links. Could be a package, if the core language doesn't have it. From no.email at nospam.invalid Tue Jan 3 18:33:09 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 03 Jan 2017 15:33:09 -0800 Subject: Cleaning up conditionals References: <003501d262fc$90a70030$27b23dae@sambora> Message-ID: <87y3yr3eiy.fsf@nightsong.com> "Deborah Swanson" writes: > I'm still wondering if these 4 lines can be collapsed to one or two > lines. In the trade that's what we call a "code smell", a sign that code (even if it works) should probably be re-thought after taking a step back to understand what it is really trying to do. What you seem to want is to take two strings, and if exactly one of them is empty, then change the empty one to be the same as the non-empty one. Leaving aside the subscripts and just calling them x and y, x, y = x or y, y or x is a concise way to do it. That's also smelly since 1) it's overly cute, and 2) it's not clear to me why you'd want to do this operation. It seems like an odd thing to want so maybe there's some different way to solve the surrounding problem. If you want to describe the actual application and context where this appears, that might get some helpful comments. From rosuav at gmail.com Tue Jan 3 18:34:59 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 10:34:59 +1100 Subject: Screwing Up looping in Generator In-Reply-To: <006f01d26615$db87c460$27b23dae@sambora> References: <006f01d26615$db87c460$27b23dae@sambora> Message-ID: On Wed, Jan 4, 2017 at 10:05 AM, Deborah Swanson wrote: > Ok, I learned how to use generators in Python 2.7.8, which may be > different from Python 3 for generators. But I learned from MIT's online > introduction to python course, and they certainly seem to know python > well. So what is the correct way to call the generator's next yield in > Python 3? We only learned to use the next function. If you don't use the > next function, what do you use? The built-in next function, not the next method. # don't do this gen.next() # do this next(gen) ChrisA From steve+python at pearwood.info Tue Jan 3 18:39:20 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 10:39:20 +1100 Subject: How Best to Coerce Python Objects to Integers? References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> <586C28EC.8010607@stoneleaf.us> Message-ID: <586c3629$0$1606$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 09:47 am, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: >> Aside from calling "except Exception" a "naked except" > > If you read the comments, you'll see that he originally had an actual > bare except clause, but then improved the code somewhat in response to > a recommendation that SystemExit etc not be caught. But why? That makes no sense. If his intention is to return None on failure, no matter what happens, then he *should* catch SystemExit. Otherwise: class Unint: def __int__(self): raise SystemExit int_or_else(Unint) will exit instead of returning None. Surely that's not what he wants, given that he wants to cover up other programming errors like NameError and TypeError? The problem here is not so much the use of try...except but the *intention* that "Anything whatsoever should be coerced to int". If you have something like: left_margin = int_or_else(ftp_server) that's surely a programming error that needs fixing, rather than something that should just return a default value. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From python at lucidity.plus.com Tue Jan 3 18:44:50 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 3 Jan 2017 23:44:50 +0000 Subject: Screwing Up looping in Generator In-Reply-To: <006101d2660e$cdca05b0$27b23dae@sambora> References: <006101d2660e$cdca05b0$27b23dae@sambora> Message-ID: Hi, On 03/01/17 22:14, Deborah Swanson wrote: > ...you have to create the generator object first and use it to call the > next function. And I really don't think you can use a generator as your > range in a for loop. So I'd use a 'while True', and break out of the > loop when you hit the StopIteration exception: > > files = rootobs() > > while True: > try: > file = files.next() > except StopIteration: > break > > base = os.path.basename(file.name) > . > . > . > (etc) What you have done there is taken an understanding of the underlying machinery that allows the 'for' loop to be syntactic sugar over any iterable and spelled it out, instead of just using 'for'! Without all that, your example is: for file in rootobs(): base = os.path.basename(file.name) . . . (etc) [In fact, the machinery would also cope with the return value from rootobs() being an iterable but not an iterator by using "files = iter(rootobjs)"]. You seem to be reading up on how the stuff works under the covers (i.e., from the point of view of an implementer of a class or library) and then suggesting that that's what the *caller* of that class or library needs to do. They don't - for a caller, 'for x in seq:' is all they need to know - the mechanics are handled by the interpreter coupled with the dunder methods that the class may implement. E. From cr2001 at hotmail.co.nz Tue Jan 3 18:49:14 2017 From: cr2001 at hotmail.co.nz (cr2001 at hotmail.co.nz) Date: Tue, 3 Jan 2017 15:49:14 -0800 (PST) Subject: Hey, I'm new to python so don't judge. Message-ID: Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? #mynumber.py # this game uses a home made function import random #think of a number computer_number = number.randint(1,100) #create the function is_same() def is_same(target, number: if target == number: result="win" elif target > number: result="low" else: result="high" return result # start the game print("hello. \nI have thought of a number between 1 and 100.") #collect the user's guess as an interger guess = int(input("Can you guess it? ")) #Use our function higher_or_lower = is_same(computer_number, guess) #run the game untill the user is correct while higher_or_lower != "win" if higher_or_lower == "to low" guess = int(input("Sorry, you are too low. Try again.")) else: guess = int(input("Sorry your are too high. Try again.")) higher_or_lower = is_same(computer_number, guess) #end of game input("Correct!\nWell Done\n\n\nPress RETURN to exit.") From python at lucidity.plus.com Tue Jan 3 18:53:16 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 3 Jan 2017 23:53:16 +0000 Subject: Screwing Up looping in Generator In-Reply-To: <006f01d26615$db87c460$27b23dae@sambora> References: <006f01d26615$db87c460$27b23dae@sambora> Message-ID: <8feb9bc0-cc89-97d6-589a-fe9b1072ff88@lucidity.plus.com> On 03/01/17 23:05, Deborah Swanson wrote: > And yes, we usually used for loops for generators, unless you don't know > when the generator will be exhausted. As in this case, where the number > of files the generator can provide is unknown. Then we used the while > True, break on StopIteration method. Out of interest, *why* was it deemed necessary to do something different if you don't know how many items the generator will generate? Was any rationale given for that? for x in foo: bar(x) ... where foo is any iterable (something that has a __iter__ method defined - including generators) will just bind each value in turn to 'x' and will exit when the StopIteration exception is raised under the covers by the iterator that is iterating over the iterable. Some generators are infinite (and their iterator will never raise a StopIteration exception). E. From steve+python at pearwood.info Tue Jan 3 18:56:16 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 11:56:16 +1200 Subject: Clickable hyperlinks References: <1124150781@f38.n261.z1.binkp.net> Message-ID: <1026903396@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 10:32 am, Deborah Swanson wrote: > The GUI consoles I have are in Pycharm, the IDLE that comes with > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when > I open them, so they're capable of opening links, but whether that means > their output space is capable of handling clickable links I don't know. > > I do know printing a full url with the %s specifier or entering a url > and clicking enter just gives you the plain text url. Obviously, not all > GUI consoles are enabled recognize and make clickable links from > correctly formatted urls. > > I was hoping there was some functionality in python to make clickable > links. Could be a package, if the core language doesn't have it. Unfortunately there is no such thing as an application- independent "clickable link". Excel can make clickable links, because it only has to deal with a single suite of related applications: Excel, Word, and the rest of Microsoft Office. Likewise LibreOffice. But Python has to deal with an infinite number of potential or hypothetical consoles, and there is no standard for "clickable links" that all, or even most, consoles understand. Python can't force the console to treat something as a clickable link, if the console has no capacity for clickable links. Nor can Python predict what format the console uses to recognise a link. The best you can do is to experiment with a couple of simple formats and see which, if any, your console understands: # HTML Example. # URL in angle brackets Example # URL alone http://www.example.com # I can't remember what these are called # Markup [Example](http://www.example.com) # Rest `Example `_ -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From rosuav at gmail.com Tue Jan 3 18:56:42 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 10:56:42 +1100 Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: On Wed, Jan 4, 2017 at 10:49 AM, wrote: > Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? > > #mynumber.py > # this game uses a home made function > import random > > #think of a number > computer_number = number.randint(1,100) What's wrong is that you aren't showing us the exception you get on this line. *Copy and paste* that exception - the whole thing. It's very helpful. ChrisA From rosuav at gmail.com Tue Jan 3 18:59:18 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 10:59:18 +1100 Subject: How Best to Coerce Python Objects to Integers? In-Reply-To: <586c3629$0$1606$c3e8da3$5496439d@news.astraweb.com> References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> <586C28EC.8010607@stoneleaf.us> <586c3629$0$1606$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Jan 4, 2017 at 10:39 AM, Steve D'Aprano wrote: > The problem here is not so much the use of try...except but the *intention* > that "Anything whatsoever should be coerced to int". If you have something > like: > > left_margin = int_or_else(ftp_server) > > that's surely a programming error that needs fixing, rather than something > that should just return a default value. Agreed. There are certainly times when you want to say "coerce any *string* to an int", and there are times when you want to say "coerce any string to int or None" (maybe you want to take the average of a whole lot of values, ignoring the ones that say "N/A"), but I don't know of any time I want to say "coerce any programming error to int or None", unless you count the exit code of a process. I used the word "improved" rather loosely. ChrisA From steve+python at pearwood.info Tue Jan 3 19:00:36 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 12:00:36 +1200 Subject: Hey, I'm new to python so don't judge. References: <2481169416@f38.n261.z1.binkp.net> Message-ID: <706562471@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 11:16 am, Callum Robinson wrote: > My apologizes but i'm quite new and would need instructions to what > information you need me to get. Do you know how to copy and paste from the terminal window? Somewhere on the screen you see something like: x = 23 + ) ^ SyntaxError: invalid syntax You should copy and paste *all* of the error message, as much as you can see. -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From cr2001 at hotmail.co.nz Tue Jan 3 19:02:15 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 16:02:15 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: When i check the code it comes up with invalid syntax and my writing line gets re directed here def is_same(target, number: if target == number: result="win" elif target > number: result="low" else: result="high" return result From python at lucidity.plus.com Tue Jan 3 19:03:03 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 00:03:03 +0000 Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: On 03/01/17 23:56, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 10:49 AM, wrote: >> #think of a number >> computer_number = number.randint(1,100) > > What's wrong is that you aren't showing us the exception you get on > this line. *Copy and paste* that exception - the whole thing. It's > very helpful. I doubt it's getting that far (I can see at least one syntax error in the code pasted). cr2001: I echo Chris's sentiment though - what is the error you are seeing (in it's entirety)? E. From steve+python at pearwood.info Tue Jan 3 19:10:00 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 12:10:00 +1200 Subject: How Best to Coerce Python Objects to Integers? References: <1909730512@f38.n261.z1.binkp.net> Message-ID: <2445380791@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 11:22 am, Erik wrote: > On 03/01/17 22:47, Chris Angelico wrote: >> On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: >>> Aside from calling "except Exception" a "naked except" >> >> If you read the comments, you'll see that he originally had an actual >> bare except clause, but then improved the code somewhat in response to >> a recommendation that SystemExit etc not be caught. > > But, as stated at the top of the article, his brief was: "The strings > come from a file that a human has typed in, so even though most of the > values are good, a few will have errors ('25C') that int() will reject.". Right. And from there he starts worrying about the case where the inputs aren't strings at all, or they're weird exotic objects with nasty __int__ methods. That's overkill and can only hide programming errors. > What he *should* have done is just validated his input strings before > presenting the string to int() - i.e., process the input with knowledge > that is specific to the problem domain before calling the > general-purpose function. That's the Look Before You Leap solution. But in this case, given the scenario described (a text file with a few typos), the best way is to ask for forgiveness rather than permission: def int_or_else(value): try: return int(value) else ValueError: pass Why is this better? In the given scenario, errors are rare. Most values are good, with only a few typos, so it is wasteful to parse the string twice, once to validate it and once to generate the int. Besides, there's probably no Python code you can write which will validate an int as fast as the int() function itself. [...] > Instead, he tried to patch around int() rejecting the strings. And then > decided that he'd patch around int() rejecting things that weren't even > strings even though that's not what the function has (apparently) been > specified to receive. Indeed. Another thought: if he is receiving human generated input, there is an argument to be made for accepting "look alikes" -- e.g. maybe the data was entered by Aunt Tilly, who was a typist in the 1960s and can't break the habit of using l or I interchangeably for 1, and O for 0. -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From cs at zip.com.au Tue Jan 3 19:10:30 2017 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 04 Jan 2017 12:10:30 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I Message-ID: <1881369740@f38.n261.z1.binkp.net> On 03Jan2017 12:57, Steve D'Aprano wrote: >I dislike the Unix-style Vim/Emacs text editors, I prefer a traditional >GUI-based editor. So my "IDE" is: >- Firefox, for doing searches and looking up documentation; >- an GUI programmer's editor, preferably one with a tab-based > interface, such as geany or kate; >- a tab-based terminal. "traditional GUI-based editor" For those of us who spent a lot of our earlier time on terminals (actual physical terminals) we consider GUIs "new fangled". Just narking, Cameron Simpson From steve+python at pearwood.info Tue Jan 3 19:15:56 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 12:15:56 +1200 Subject: Hey, I'm new to python so don't judge. References: <4092501290@f38.n261.z1.binkp.net> Message-ID: <3844402364@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 12:04 pm, Callum Robinson wrote: > Traceback (most recent call last): > File "D:/Python/random.py", line 6, in > computer_number = number.randint(1, 100) > NameError: name 'number' is not defined That's exactly what we need to see! The full traceback, thank you! You're asking Python to get the variable "number", and call the randint method. But: - you don't have a variable called "number"; NameError: name 'number' is not defined - and even if you did, that's not how you get a random number. What you want is: computer_number = random.randint(1, 100) -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From cr2001 at hotmail.co.nz Tue Jan 3 19:16:36 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 16:16:36 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: On Wednesday, January 4, 2017 at 1:03:18 PM UTC+13, Erik wrote: > On 03/01/17 23:56, Chris Angelico wrote: > > On Wed, Jan 4, 2017 at 10:49 AM, wrote: > >> #think of a number > >> computer_number = number.randint(1,100) > > > > What's wrong is that you aren't showing us the exception you get on > > this line. *Copy and paste* that exception - the whole thing. It's > > very helpful. > > I doubt it's getting that far (I can see at least one syntax error in > the code pasted). > > cr2001: I echo Chris's sentiment though - what is the error you are > seeing (in it's entirety)? > > E. My apologizes but i'm quite new and would need instructions to what information you need me to get. From rosuav at gmail.com Tue Jan 3 19:16:57 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 11:16:57 +1100 Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: On Wed, Jan 4, 2017 at 11:03 AM, Erik wrote: > I doubt it's getting that far (I can see at least one syntax error in the > code pasted). True true. In any case, the point is to copy and paste the error message. Callum, please, copy and paste it. ChrisA From cs at zip.com.au Tue Jan 3 19:20:18 2017 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 04 Jan 2017 12:20:18 +1200 Subject: trouble with cmd.Cmd and prompting Message-ID: <1514283610@f38.n261.z1.binkp.net> On 03Jan2017 16:57, Peter Otten <__peter__ at web.de> wrote: >Cameron Simpson wrote: >> On 03Jan2017 00:14, Dennis Lee Bieber wrote: >>>On Tue, 3 Jan 2017 11:33:15 +1100, Cameron Simpson >>>declaimed the following: >>>>I'm using cmd.Cmd to write a little FTP-like command line to interface to >>>>a storage system of mine and encountering weird behaviour. When I enter a >>>>command the next prompt appears _before_ the associated operation runs, [...] >>>Haven't used the module but there is something I find intriguing in the >>>help system >>>-=-=-=-=- >>> Cmd.precmd(line) >>> Hook method executed just before the command line is interpreted, but >>>after the input prompt is generated and issued. >>>-=-=-=-=- >>>"... AFTER the input prompt is ... issued" [...] >I don't believe Dennis' reading of the docs is correct, and the relevant >part of the source (Python 3.4) does not show anything to support it: [... code that runs as I had imagined ...] >Is there something asynchronous in your command? Perhaps you can post a >minimal runnable example that shows the odd behaviour. In the commands themselves? No, but the storage system has lots of asynchronous activity. I'm bothered by the fact that an empty command also shows this behaviour. I will try to get a minimal example others can run. Cheers, Cameron Simpson From python at lucidity.plus.com Tue Jan 3 19:22:17 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 00:22:17 +0000 Subject: How Best to Coerce Python Objects to Integers? In-Reply-To: References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> <586C28EC.8010607@stoneleaf.us> Message-ID: <8e7195db-e33f-18e3-e1d9-74f9a458e69b@lucidity.plus.com> On 03/01/17 22:47, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: >> Aside from calling "except Exception" a "naked except" > > If you read the comments, you'll see that he originally had an actual > bare except clause, but then improved the code somewhat in response to > a recommendation that SystemExit etc not be caught. But, as stated at the top of the article, his brief was: "The strings come from a file that a human has typed in, so even though most of the values are good, a few will have errors ('25C') that int() will reject.". What he *should* have done is just validated his input strings before presenting the string to int() - i.e., process the input with knowledge that is specific to the problem domain before calling the general-purpose function. He mentions temperature sensors, so perhaps stripping a trailing 'c' or 'C' is a reasonable thing to do (or even, if there's a trailing 'f' or 'F', performing a numerical conversion after the value is known). Instead, he tried to patch around int() rejecting the strings. And then decided that he'd patch around int() rejecting things that weren't even strings even though that's not what the function has (apparently) been specified to receive. The "bulletproof" result will convert "25C" to None even though 25 is probably a reasonable result for that string in his domain problem domain. E. From steve+python at pearwood.info Tue Jan 3 19:26:08 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 11:26:08 +1100 Subject: How Best to Coerce Python Objects to Integers? References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> <586C28EC.8010607@stoneleaf.us> <586C2F40.3050806@stoneleaf.us> Message-ID: <586c4122$0$1621$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 10:09 am, Ethan Furman wrote: > And, of course, whether or not "except Exception" is too broad depends on > the use-case. I'm having trouble thinking of *any* use-case where this would be useful. His intention, it seems, is to write a function which simply cannot fail, presumably so that he can write code which consumes the output and just keeps going no matter what he throws at it: graph = make_graph() incoming_data = [1, 12, 7, "hello", None, [], {}, 5] for obj in incoming_data: graph.draw_point(int_or_else(obj)) But of course that's impossible: class Evil: def __int__(self): os.abort() Or for that matter: class Evil: def __int__(self): time.sleep(2147483647) So his code still can fail under some circumstances. And so it should. Why is my input data including such evil objects? I should find out why. I'm having trouble seeing how it could be anything but a programming bug. He gives the rationale for this function: A scenario that?s more realistic than the Unintable class might be a class that wraps an industrial sensor. Calling int() on an instance normally returns a value representing pressure or temperature. However, it might reasonably raise a SensorNotReadyError. Okay. Let's suppose it can return either an integer as a string, some arbitrary non-numeric string to indicate a sensor error, or raises SensorNotReadyError. Then this would be an appropriate function to use: def int_or_else(value): try: return int(value) except ValueError: assert isinstance(value, str) # consider logging the error? return None except SensorNotReadyError: return None Now when he connects up to the sensor and starts reading values, it will work, but if his input gets contaminated with arbitrary junk objects (a sign of a programming bug in his code) he will find out about it. One possible use-case might be something like Excel, where there are two data types: numbers, and text, and numeric operations on text will just skip them altogether. If you were to build an object-oriented spreadsheet, where the cells can contain any object not just numbers and text, then you could potentially have a situation like: Column A Row 1: 1 Row 2: ftp_server() Row 3: 2 Row 4: 3 Row 5: =sum(A1:A4) and you (arguably) want the result to be 6 rather than some error. Or do you? I can make a good case for skipping text cells, as Excel does, but I'm not sure that ftp_server should be skipped. So I'll count that as a use-case, but a dubious one. Another possible use-case might be the REPL for an interpreter, where you want the REPL to keep going no matter what exceptions take place. But I don't think this is the right way to do that, and it's not how the Python REPL works either. Other than that, I'm not seeing any use-case where this sort of thing is anything but a bad idea. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From python at lucidity.plus.com Tue Jan 3 19:26:12 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 00:26:12 +0000 Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: Hi Callum, On 04/01/17 00:02, Callum Robinson wrote: > When i check the code it comes up with invalid syntax and my writing line gets re directed here > > def is_same(target, number: > if target == number: > result="win" > elif target > number: > result="low" > else: > result="high" > return result OK, good. That implies it's something wrong with the function definition ('def'). Look at that very carefully :) (*) E. (*) My emoticon may give you a hint ... From cr2001 at hotmail.co.nz Tue Jan 3 19:27:33 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 16:27:33 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: <40adacf3-b4de-4243-8cb5-6ea5fb74cf18@googlegroups.com> On Wednesday, January 4, 2017 at 1:17:11 PM UTC+13, Chris Angelico wrote: > On Wed, Jan 4, 2017 at 11:03 AM, Erik wrote: > > I doubt it's getting that far (I can see at least one syntax error in the > > code pasted). > > True true. In any case, the point is to copy and paste the error > message. Callum, please, copy and paste it. > > ChrisA I'm sorry if I'm doing something wrong but all that is happening is when i try to run it a popup says Invalid syntax From cr2001 at hotmail.co.nz Tue Jan 3 19:30:53 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 16:30:53 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: <5e69d496-f5d3-4aab-ae78-3d1d57d3aee4@googlegroups.com> On Wednesday, January 4, 2017 at 1:26:26 PM UTC+13, Erik wrote: > Hi Callum, > > On 04/01/17 00:02, Callum Robinson wrote: > > When i check the code it comes up with invalid syntax and my writing > line gets re directed here > > > > def is_same(target, number: > > if target == number: > > result="win" > > elif target > number: > > result="low" > > else: > > result="high" > > return result > > OK, good. That implies it's something wrong with the function definition > ('def'). Look at that very carefully :) (*) > > E. > > (*) My emoticon may give you a hint ... I feel like im missing something so blatantly obvious. From cr2001 at hotmail.co.nz Tue Jan 3 19:32:40 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 16:32:40 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: On Wednesday, January 4, 2017 at 1:26:26 PM UTC+13, Erik wrote: > Hi Callum, > > On 04/01/17 00:02, Callum Robinson wrote: > > When i check the code it comes up with invalid syntax and my writing > line gets re directed here > > > > def is_same(target, number: > > if target == number: > > result="win" > > elif target > number: > > result="low" > > else: > > result="high" > > return result > > OK, good. That implies it's something wrong with the function definition > ('def'). Look at that very carefully :) (*) > > E. > > (*) My emoticon may give you a hint ... I forgot a bloody bracket xD and now theirs a new error ill try to figure this out on my own. From python at lucidity.plus.com Tue Jan 3 19:45:08 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 00:45:08 +0000 Subject: Hey, I'm new to python so don't judge. In-Reply-To: <5e69d496-f5d3-4aab-ae78-3d1d57d3aee4@googlegroups.com> References: <5e69d496-f5d3-4aab-ae78-3d1d57d3aee4@googlegroups.com> Message-ID: <71b9f7e5-c255-435f-e7d6-3cc7abce35b4@lucidity.plus.com> Hi Callum, On 04/01/17 00:30, Callum Robinson wrote: > I feel like im missing something so blatantly obvious. That's because you are ;). I don't want to come across as patronising, but I want you to see it for yourself, so, here's a function definition similar to yours that doesn't have the same syntax error that yours does: def foo(spam, ham): if spam == ham: return "same" return "different" See the difference? E. From python at lucidity.plus.com Tue Jan 3 19:47:55 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 00:47:55 +0000 Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: On 04/01/17 00:32, Callum Robinson wrote: > I forgot a bloody bracket xD Cool, you got it ;) It's the sort of thing your brain will see instantly once you've done it a few times :D > and now theirs a new error ill try to figure this out on my own. You need to look back to Chris's original reply, I suspect (his reply was pointing out a runtime issue that will happen once the syntax issue is resolved) ... E. From python at deborahswanson.net Tue Jan 3 19:50:03 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 16:50:03 -0800 Subject: Clickable hyperlinks In-Reply-To: <4e756eb9-842b-98fe-3fa1-3b05baebe71a@lucidity.plus.com> Message-ID: <008e01d26624$7c84a7d0$27b23dae@sambora> Erik wrote, on January 03, 2017 3:30 PM > To: python-list at python.org > Subject: Re: Clickable hyperlinks > > Hi. > > On 03/01/17 19:46, Deborah Swanson wrote: > > Excel has a formula: > > When you start a new topic on the list, could you please write a new > message rather than replying to an existing message and changing the > title/subject? > > For those reading the list in a threaded email client, this > message is > shown as a continuation of your "Cleaning up conditionals" > thread, and > that whole thread in turn shows up as a continuation of the "mentor > training python Romania with certification" discussion (which you had > presumably "reply"ed to originally) ... > > Thanks. E. Certainly. I've been on many other lists before (but none since about 2011), and no one complained of or even mentioned this problem. But if it's a problem with some email readers now, I can easily just start a new message. Just being lazy and following old habits, I guess. ;) From steve+python at pearwood.info Tue Jan 3 19:56:17 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 11:56:17 +1100 Subject: Clickable hyperlinks References: <007b01d26619$97de4eb0$27b23dae@sambora> Message-ID: <586c4834$0$1591$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 10:32 am, Deborah Swanson wrote: > The GUI consoles I have are in Pycharm, the IDLE that comes with > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when > I open them, so they're capable of opening links, but whether that means > their output space is capable of handling clickable links I don't know. > > I do know printing a full url with the %s specifier or entering a url > and clicking enter just gives you the plain text url. Obviously, not all > GUI consoles are enabled recognize and make clickable links from > correctly formatted urls. > > I was hoping there was some functionality in python to make clickable > links. Could be a package, if the core language doesn't have it. Unfortunately there is no such thing as an application- independent "clickable link". Excel can make clickable links, because it only has to deal with a single suite of related applications: Excel, Word, and the rest of Microsoft Office. Likewise LibreOffice. But Python has to deal with an infinite number of potential or hypothetical consoles, and there is no standard for "clickable links" that all, or even most, consoles understand. Python can't force the console to treat something as a clickable link, if the console has no capacity for clickable links. Nor can Python predict what format the console uses to recognise a link. The best you can do is to experiment with a couple of simple formats and see which, if any, your console understands: # HTML Example. # URL in angle brackets Example # URL alone http://www.example.com # I can't remember what these are called # Markup [Example](http://www.example.com) # Rest `Example `_ -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Tue Jan 3 20:00:36 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 12:00:36 +1100 Subject: Hey, I'm new to python so don't judge. References: Message-ID: <586c4936$0$1591$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 11:16 am, Callum Robinson wrote: > My apologizes but i'm quite new and would need instructions to what > information you need me to get. Do you know how to copy and paste from the terminal window? Somewhere on the screen you see something like: x = 23 + ) ^ SyntaxError: invalid syntax You should copy and paste *all* of the error message, as much as you can see. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From python at deborahswanson.net Tue Jan 3 20:01:18 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 17:01:18 -0800 Subject: Screwing Up looping in Generator In-Reply-To: Message-ID: <009101d26626$0ecd9470$27b23dae@sambora> Erik wrote, on January 03, 2017 3:45 PM > > Hi, > > On 03/01/17 22:14, Deborah Swanson wrote: > > ...you have to create the generator object first and use it to call > > the next function. And I really don't think you can use a > generator as > > your range in a for loop. So I'd use a 'while True', and > break out of > > the loop when you hit the StopIteration exception: > > > > files = rootobs() > > > > while True: > > try: > > file = files.next() > > except StopIteration: > > break > > > > base = os.path.basename(file.name) > > . > > . > > . > > (etc) > > What you have done there is taken an understanding of the underlying > machinery that allows the 'for' loop to be syntactic sugar over any > iterable and spelled it out, instead of just using 'for'! Without all > that, your example is: > > for file in rootobs(): > base = os.path.basename(file.name) > . > . > . > (etc) > > [In fact, the machinery would also cope with the return value from > rootobs() being an iterable but not an iterator by using "files = > iter(rootobjs)"]. > > > > You seem to be reading up on how the stuff works under the > covers (i.e., > from the point of view of an implementer of a class or > library) and then > suggesting that that's what the *caller* of that class or > library needs > to do. They don't - for a caller, 'for x in seq:' is all they need to > know - the mechanics are handled by the interpreter coupled with the > dunder methods that the class may implement. > > > E. Ok, I'm in complete agreement with everything you said up to the last paragraph, which I don't disagree with, I just don't see your point. If you've read my last few posts you'll have seen me acknowledging that normally a for loop is used, but a while and break on StopIteration is a method that's useful when the output of the generator is unknown. I haven't read through much documentation on generators, but I have taken a course from MIT, in which a Computer Science professor gave us several methods for working with generators, of which the while and break on StopIteration method is one. The original poster wanted to use a while, and seemed to be saying he didn't know how many files the generator he has would yield, so that was why I recommended the while loop. Normally I would use a for loop too. D. From cr2001 at hotmail.co.nz Tue Jan 3 20:04:29 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 17:04:29 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: <5e69d496-f5d3-4aab-ae78-3d1d57d3aee4@googlegroups.com> <71b9f7e5-c255-435f-e7d6-3cc7abce35b4@lucidity.plus.com> Message-ID: <3021b6e8-d843-413f-a62b-f51461ed1b49@googlegroups.com> On Wednesday, January 4, 2017 at 1:45:22 PM UTC+13, Erik wrote: > Hi Callum, > > On 04/01/17 00:30, Callum Robinson wrote: > > I feel like im missing something so blatantly obvious. > > That's because you are ;). I don't want to come across as patronising, > but I want you to see it for yourself, so, here's a function definition > similar to yours that doesn't have the same syntax error that yours does: > > def foo(spam, ham): > if spam == ham: > return "same" > return "different" > > See the difference? > > E. I've figured out that out but I have a new issue. I like what you are doing making me figure this out as it helps me remember. I'll post the new code and the issue. If you could give me a hint that would be great. ------------------------------------------ Issue ------------------------------------------ Traceback (most recent call last): File "D:/Python/random.py", line 6, in computer_number = number.randint(1, 100) NameError: name 'number' is not defined ----------------------------------------- Here is the most recent code ----------------------------------------- # mynumber.py # this game uses a home made function import random #think of a number computer_number = number.randint(1, 100) #create the function is_same() def is_same(target, number): if target == number: result="Win" elif target > number: result="Low" else: result="High" return result # start the game print("hello. \nI have thought of a number between 1 and 100.") #collect the user's guess as an interger guess = int(input("Can you guess it? ")) #Use our function higher_or_lower = is_same(computer_number, guess) #run the game untill the user is correct while higher_or_lower != "win": if higher_or_lower == "low": guess = int(input("Sorry, you are too low. Try again.")) else: guess = int(input("Sorry your are too high. Try again.")) higher_or_lower = is_same(computer_number, guess) #end of game input("Correct!\nWell Done\n\n\nPress RETURN to exit.") From tjreedy at udel.edu Tue Jan 3 20:06:18 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 3 Jan 2017 20:06:18 -0500 Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: On 1/3/2017 7:02 PM, Callum Robinson wrote: > When i check the code it comes up with invalid syntax and my writing line gets re directed here > > def is_same(target, number: > if target == number: > result="win" > elif target > number: > result="low" > else: > result="high" > return result When I paste this into a file in IDLE and try to run the file, 'if' is highlighted in red. This means that there is an error somewhere at or before the 'if'. -- Terry Jan Reedy From steve+python at pearwood.info Tue Jan 3 20:10:01 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 12:10:01 +1100 Subject: How Best to Coerce Python Objects to Integers? References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> <586C28EC.8010607@stoneleaf.us> <8e7195db-e33f-18e3-e1d9-74f9a458e69b@lucidity.plus.com> Message-ID: <586c4b6b$0$1612$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 11:22 am, Erik wrote: > On 03/01/17 22:47, Chris Angelico wrote: >> On Wed, Jan 4, 2017 at 9:42 AM, Ethan Furman wrote: >>> Aside from calling "except Exception" a "naked except" >> >> If you read the comments, you'll see that he originally had an actual >> bare except clause, but then improved the code somewhat in response to >> a recommendation that SystemExit etc not be caught. > > But, as stated at the top of the article, his brief was: "The strings > come from a file that a human has typed in, so even though most of the > values are good, a few will have errors ('25C') that int() will reject.". Right. And from there he starts worrying about the case where the inputs aren't strings at all, or they're weird exotic objects with nasty __int__ methods. That's overkill and can only hide programming errors. > What he *should* have done is just validated his input strings before > presenting the string to int() - i.e., process the input with knowledge > that is specific to the problem domain before calling the > general-purpose function. That's the Look Before You Leap solution. But in this case, given the scenario described (a text file with a few typos), the best way is to ask for forgiveness rather than permission: def int_or_else(value): try: return int(value) else ValueError: pass Why is this better? In the given scenario, errors are rare. Most values are good, with only a few typos, so it is wasteful to parse the string twice, once to validate it and once to generate the int. Besides, there's probably no Python code you can write which will validate an int as fast as the int() function itself. [...] > Instead, he tried to patch around int() rejecting the strings. And then > decided that he'd patch around int() rejecting things that weren't even > strings even though that's not what the function has (apparently) been > specified to receive. Indeed. Another thought: if he is receiving human generated input, there is an argument to be made for accepting "look alikes" -- e.g. maybe the data was entered by Aunt Tilly, who was a typist in the 1960s and can't break the habit of using l or I interchangeably for 1, and O for 0. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From cs at zip.com.au Tue Jan 3 20:10:30 2017 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 4 Jan 2017 12:10:30 +1100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <586b0517$0$1584$c3e8da3$5496439d@news.astraweb.com> References: <586b0517$0$1584$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20170104011030.GA46099@cskk.homeip.net> On 03Jan2017 12:57, Steve D'Aprano wrote: >I dislike the Unix-style Vim/Emacs text editors, I prefer a traditional >GUI-based editor. So my "IDE" is: >- Firefox, for doing searches and looking up documentation; >- an GUI programmer's editor, preferably one with a tab-based > interface, such as geany or kate; >- a tab-based terminal. "traditional GUI-based editor" For those of us who spent a lot of our earlier time on terminals (actual physical terminals) we consider GUIs "new fangled". Just narking, Cameron Simpson From python at deborahswanson.net Tue Jan 3 20:12:22 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 17:12:22 -0800 Subject: Screwing Up looping in Generator In-Reply-To: <8feb9bc0-cc89-97d6-589a-fe9b1072ff88@lucidity.plus.com> Message-ID: <009201d26627$9aee9390$27b23dae@sambora> Erik wrote, on January 03, 2017 3:53 PM > > On 03/01/17 23:05, Deborah Swanson wrote: > > And yes, we usually used for loops for generators, unless you don't > > know when the generator will be exhausted. As in this case, > where the > > number of files the generator can provide is unknown. Then > we used the > > while True, break on StopIteration method. > > Out of interest, *why* was it deemed necessary to do > something different > if you don't know how many items the generator will generate? Was any > rationale given for that? > > for x in foo: > bar(x) > > ... where foo is any iterable (something that has a __iter__ method > defined - including generators) will just bind each value in > turn to 'x' > and will exit when the StopIteration exception is raised under the > covers by the iterator that is iterating over the iterable. > > Some generators are infinite (and their iterator will never raise a > StopIteration exception). > > E. The main reason you might want to catch the StopIteration exception is to do something else before your code simply stops running. If all you're doing is run a generator til it's out of gas, and that's all you want it to do, then there's no need to catch anything. There's lots of situations where this is exactly what you want. It might even be the most frequent use of generators, though I wouldn't know. From steve+python at pearwood.info Tue Jan 3 20:15:57 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 12:15:57 +1100 Subject: Hey, I'm new to python so don't judge. References: <5e69d496-f5d3-4aab-ae78-3d1d57d3aee4@googlegroups.com> <71b9f7e5-c255-435f-e7d6-3cc7abce35b4@lucidity.plus.com> <3021b6e8-d843-413f-a62b-f51461ed1b49@googlegroups.com> Message-ID: <586c4ccf$0$1602$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 12:04 pm, Callum Robinson wrote: > Traceback (most recent call last): > File "D:/Python/random.py", line 6, in > computer_number = number.randint(1, 100) > NameError: name 'number' is not defined That's exactly what we need to see! The full traceback, thank you! You're asking Python to get the variable "number", and call the randint method. But: - you don't have a variable called "number"; NameError: name 'number' is not defined - and even if you did, that's not how you get a random number. What you want is: computer_number = random.randint(1, 100) -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From cs at zip.com.au Tue Jan 3 20:20:18 2017 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 4 Jan 2017 12:20:18 +1100 Subject: trouble with cmd.Cmd and prompting In-Reply-To: References: Message-ID: <20170104012018.GA72609@cskk.homeip.net> On 03Jan2017 16:57, Peter Otten <__peter__ at web.de> wrote: >Cameron Simpson wrote: >> On 03Jan2017 00:14, Dennis Lee Bieber wrote: >>>On Tue, 3 Jan 2017 11:33:15 +1100, Cameron Simpson >>>declaimed the following: >>>>I'm using cmd.Cmd to write a little FTP-like command line to interface to >>>>a storage system of mine and encountering weird behaviour. When I enter a >>>>command the next prompt appears _before_ the associated operation runs, [...] >>>Haven't used the module but there is something I find intriguing in the >>>help system >>>-=-=-=-=- >>> Cmd.precmd(line) >>> Hook method executed just before the command line is interpreted, but >>>after the input prompt is generated and issued. >>>-=-=-=-=- >>>"... AFTER the input prompt is ... issued" [...] >I don't believe Dennis' reading of the docs is correct, and the relevant >part of the source (Python 3.4) does not show anything to support it: [... code that runs as I had imagined ...] >Is there something asynchronous in your command? Perhaps you can post a >minimal runnable example that shows the odd behaviour. In the commands themselves? No, but the storage system has lots of asynchronous activity. I'm bothered by the fact that an empty command also shows this behaviour. I will try to get a minimal example others can run. Cheers, Cameron Simpson From torriem at gmail.com Tue Jan 3 20:22:59 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 3 Jan 2017 18:22:59 -0700 Subject: Clickable hyperlinks In-Reply-To: <007b01d26619$97de4eb0$27b23dae@sambora> References: <007b01d26619$97de4eb0$27b23dae@sambora> Message-ID: On 01/03/2017 04:32 PM, Deborah Swanson wrote: > The GUI consoles I have are in Pycharm, the IDLE that comes with > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when > I open them, so they're capable of opening links, but whether that means > their output space is capable of handling clickable links I don't know. Hmm I still don't understand what you mean by "GUI console." Just because a program asks for internet access does not mean anything about whether or not your python program can display a clickable link in a console window. Besides that, a clickable link would probably ask the OS to open it with the default application, not cause your program to access the internet. The standard out pipe (where stuff goes when you call print() or write to sys.stdout) from a python program usually goes to a console or a terminal emulator. PyCharm and IDLE both provide windows to display this output (they emulate a terminal). But maybe you're misunderstanding what this actually is. These windows just display a stream of bytes that come out of your program. Certain escape codes can be emitted that can instruct the console or terminal emulator to do things like set text color or display text at a certain position. But there's certainly no special way to instruct the terminal emulator to make a section of text into a hyperlink. Maybe if hyperlinks had existed years ago when terminal escape codes were being defined we'd have a "hyperlink" code that all consoles and terminals would understand. A few years ago I saw some proposals to add an escape code to the ANSI scheme that would encode hyperlinks, but nothing ever came of it because, honestly, it would be too much hassle to roll this out to ever terminal emulator out there (to say nothing of real terminals). > I do know printing a full url with the %s specifier or entering a url > and clicking enter just gives you the plain text url. Obviously, not all > GUI consoles are enabled recognize and make clickable links from > correctly formatted urls. On my Linux machine, the terminal emulators I've used all make a regular url printed out into a clickable link (or at least a right-clickable link). This is just something they try to do with all things that look like urls. Sometimes it's helpful, often it's annoying. > I was hoping there was some functionality in python to make clickable > links. Could be a package, if the core language doesn't have it. No, there is not. If you made a full GUI app using a toolkit like GTK or Qt, you can indeed place hyperlinks on your forms and the operating system will automatically connect them to the web browser. But not in text-mode terminal apps. From cr2001 at hotmail.co.nz Tue Jan 3 20:25:13 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 17:25:13 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: <586c4ccf$0$1602$c3e8da3$5496439d@news.astraweb.com> References: <5e69d496-f5d3-4aab-ae78-3d1d57d3aee4@googlegroups.com> <71b9f7e5-c255-435f-e7d6-3cc7abce35b4@lucidity.plus.com> <3021b6e8-d843-413f-a62b-f51461ed1b49@googlegroups.com> <586c4ccf$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wednesday, January 4, 2017 at 2:16:08 PM UTC+13, Steve D'Aprano wrote: > On Wed, 4 Jan 2017 12:04 pm, Callum Robinson wrote: > > > Traceback (most recent call last): > > File "D:/Python/random.py", line 6, in > > computer_number = number.randint(1, 100) > > NameError: name 'number' is not defined > > > That's exactly what we need to see! The full traceback, thank you! > > You're asking Python to get the variable "number", and call the randint > method. But: > > - you don't have a variable called "number"; > > NameError: name 'number' is not defined > > > - and even if you did, that's not how you get a random number. What you want > is: > > computer_number = random.randint(1, 100) > > > > > > > -- > Steve > ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure > enough, things got worse. Hey man thanks, the sad thing is i have no idea why i put that in. I must be having a terrible day. From python at lucidity.plus.com Tue Jan 3 20:25:38 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 01:25:38 +0000 Subject: Screwing Up looping in Generator In-Reply-To: <009201d26627$9aee9390$27b23dae@sambora> References: <009201d26627$9aee9390$27b23dae@sambora> Message-ID: Hi, On 04/01/17 01:12, Deborah Swanson wrote: > The main reason you might want to catch the StopIteration exception is > to do something else before your code simply stops running. If all > you're doing is run a generator til it's out of gas, and that's all you > want it to do, then there's no need to catch anything. Ah! OK, I see where the lines are being crossed now ;) Although StopIteration is an exception, it is something that the 'for/iter' machinery handles for you under the covers. Each 'for' statement effectively has a 'try' block around it that catches 'StopIteration' and just terminates that particular 'for' loop and continues on with the remainder of your script. Raising a 'StopIteration' is an internal mechanism used to determine when an iterator (i.e., the thing a 'for' loop is looping over) has exhausted itself. It's not something a regular user is ever expected to know about let alone catch. When execution falls out of the bottom of a generator, StopIteration is raise (compared to a regular function or method returning 'None'). E. From flebber.crue at gmail.com Tue Jan 3 20:35:59 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Tue, 3 Jan 2017 17:35:59 -0800 (PST) Subject: Screwing Up looping in Generator In-Reply-To: References: <009201d26627$9aee9390$27b23dae@sambora> Message-ID: So can I call the generator twice and receive the same file twice in 2 for loops? Once to get the files name and the second to process? for file in rootobs: base = os.path.basename(file.name) write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") with open(write_to, 'w', newline='') as csvf: for file in rootobs: # create and write csv Cheers Sayth From bouncingcats at gmail.com Tue Jan 3 20:36:00 2017 From: bouncingcats at gmail.com (David) Date: Wed, 04 Jan 2017 13:36:00 +1200 Subject: Clickable hyperlinks Message-ID: <309467853@f38.n261.z1.binkp.net> On 4 January 2017 at 11:50, Deborah Swanson wrote: > Erik wrote, on January 03, 2017 3:30 PM >> >> When you start a new topic on the list, could you please write a new >> message rather than replying to an existing message and changing the >> title/subject? >> > Certainly. I've been on many other lists before (but none since about > 2011), and no one complained of or even mentioned this problem. But if > it's a problem with some email readers now, I can easily just start a > new message. Just being lazy and following old habits, I guess. ;) To be clear, the problem is not "with some email readers". The problem is that it breaks the thread management features that are built into every email message, making it impossible to display threads correctly. As can be seen here for example: https://mail.python.org/pipermail/python-list/2017-January/thread.html Note how most threads start in the leftmost column and replies are indented. But this does not occur where you began the new subject: "Clickable hyperlinks". Thanks. From cr2001 at hotmail.co.nz Tue Jan 3 20:37:24 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 17:37:24 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: Message-ID: <26855fa4-dc40-43ae-9eaa-15dc1de97842@googlegroups.com> On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson wrote: > Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? > > #mynumber.py > # this game uses a home made function > import random > > #think of a number > computer_number = number.randint(1,100) > > #create the function is_same() > def is_same(target, number: > if target == number: > result="win" > elif target > number: > result="low" > else: > result="high" > return result > > # start the game > print("hello. \nI have thought of a number between 1 and 100.") > > #collect the user's guess as an interger > guess = int(input("Can you guess it? ")) > #Use our function > higher_or_lower = is_same(computer_number, guess) > #run the game untill the user is correct > while higher_or_lower != "win" > if higher_or_lower == "to low" > guess = int(input("Sorry, you are too low. Try again.")) > else: > guess = int(input("Sorry your are too high. Try again.")) > > higher_or_lower = is_same(computer_number, guess) > > #end of game > input("Correct!\nWell Done\n\n\nPress RETURN to exit.") Hey again, i'm sorry for bothering you with so many questions i just did not want to keep asking my teacher who is on holiday these. I have another issue where the code runs but i can guess every number from 1-100 but it always says Sorry your are too high. Try again. I don't understand what i have done to cause this. From steve+comp.lang.python at pearwood.info Tue Jan 3 20:43:46 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 13:43:46 +1200 Subject: Clickable hyperlinks References: <973176847@f38.n261.z1.binkp.net> Message-ID: <4238055976@f38.n261.z1.binkp.net> On Wednesday 04 January 2017 15:46, Deborah Swanson wrote: > Steven D'Aprano wrote, on January 03, 2017 8:04 PM [...] >> Of course you have to put quotes around them to enter them in >> your source code. >> We don't expect this to work: >> >> print(Hello World!) >> >> >> you have to use a string literal with quotes: >> >> print('Hello World!') >> >> >> Same for all of the above. > I didn't try printing them before, but I just did. Got: > >>>> print([Example](http://www.example.com) > > SyntaxError: invalid syntax (arrow pointing at the colon) You missed the part where I said you have to put them in quotes. Like any other string in Python, you have to use quotation marks around it for Python to understand it as a string. None of these things will work: print( Hello World! ) print( What do you want to do today? ) print( 3 2 1 blast off ) print( http://www.example.com ) This isn't specific to print. This won't work either: message = Hello World! In *all* of these cases, you have to tell Python you're dealing with a string, and you do that with quotation marks: message = "Hello World!" print( 'What do you want to do today?' ) count_down = '3 2 1 blast off' url = 'http://www.example.com' -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From rhodri at kynesim.co.uk Tue Jan 3 20:47:14 2017 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 04 Jan 2017 13:47:14 +1200 Subject: Screwing Up looping in Generator Message-ID: <2751329663@f38.n261.z1.binkp.net> On 04/01/17 02:10, Deborah Swanson wrote: > Sayth Renshaw wrote, on January 03, 2017 5:36 PM >> >> So can I call the generator twice and receive the same file >> twice in 2 for loops? >> >> Once to get the files name and the second to process? >> >> for file in rootobs: >> base = os.path.basename(file.name) >> write_to = os.path.join("output", >> os.path.splitext(base)[0] + ".csv") >> with open(write_to, 'w', newline='') as csvf: >> for file in rootobs: >> # create and write csv >> >> Cheers >> >> Sayth > > I don't see why not, if you let the first one run to completion and then > do it again a second time. Assuming your generator doesn't somehow > delete or modify the file specifications as it yields them. It would be > helpful to see the code for rootobs, if you have it. Ahem. If Sayth is using the correct terminology and rootobs actually is a generator (not, say, a list or tuple), then no it won't work. Once a generator is exhausted, it's exhausted. Besides, the nested for-loops over the same iterable is a dead giveaway that something is wrong. -- Rhodri James *-* Kynesim Ltd From flebber.crue at gmail.com Tue Jan 3 20:54:34 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Tue, 3 Jan 2017 17:54:34 -0800 (PST) Subject: Screwing Up looping in Generator In-Reply-To: References: <009201d26627$9aee9390$27b23dae@sambora> Message-ID: <1e433232-7db7-4f1c-8637-50a3da469b20@googlegroups.com> On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw wrote: > So can I call the generator twice and receive the same file twice in 2 for loops? > > Once to get the files name and the second to process? > > for file in rootobs: > base = os.path.basename(file.name) > write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") > with open(write_to, 'w', newline='') as csvf: > for file in rootobs: > # create and write csv > > Cheers > > Sayth I just need it to write after each file however the with open(#file) as csvf: Keeps it all open until every file processed in an output file with the name of the first file in the generator. Sayth From python at deborahswanson.net Tue Jan 3 21:05:13 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 18:05:13 -0800 Subject: Screwing Up looping in Generator In-Reply-To: Message-ID: <009d01d2662e$fd3a4970$27b23dae@sambora> Chris Angelico wrote, on January 03, 2017 3:35 PM > > On Wed, Jan 4, 2017 at 10:05 AM, Deborah Swanson > wrote: > > Ok, I learned how to use generators in Python 2.7.8, which may be > > different from Python 3 for generators. But I learned from MIT's > > online introduction to python course, and they certainly > seem to know > > python well. So what is the correct way to call the > generator's next > > yield in Python 3? We only learned to use the next function. If you > > don't use the next function, what do you use? > > The built-in next function, not the next method. > > # don't do this > gen.next() > > # do this > next(gen) > > ChrisA You speak the truth! I never doubted, but since I still have 2.7.8 on my system I decided to try it out. For a simple little Fibbonacci number generator: def genFib (): fibn_1 = 1 #fib(n-1) fibn_2 = 0 #fib(n-2) while True: # fib(n) = fib(n-1) + fib(n-2) next = fibn_1 + fibn_2 yield next fibn_2 = fibn_1 fibn_1 = next and at the console: >>> fib = genFib() >>> fib.next() 2.7.8 works, and cranks out as many Fibbonacci numbers as you could want. But in 3.4.3 you get: Traceback (most recent call last): File "", line 1, in fib.next() AttributeError: 'generator' object has no attribute 'next' Then, going the other way, next(fib) works in both versions. From python at mrabarnett.plus.com Tue Jan 3 21:05:35 2017 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 4 Jan 2017 02:05:35 +0000 Subject: Hey, I'm new to python so don't judge. In-Reply-To: <26855fa4-dc40-43ae-9eaa-15dc1de97842@googlegroups.com> References: <26855fa4-dc40-43ae-9eaa-15dc1de97842@googlegroups.com> Message-ID: <07f87e48-6bb3-ad25-6f4f-7d350c5b794a@mrabarnett.plus.com> On 2017-01-04 01:37, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson wrote: >> Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? >> >> #mynumber.py >> # this game uses a home made function >> import random >> >> #think of a number >> computer_number = number.randint(1,100) >> >> #create the function is_same() >> def is_same(target, number: >> if target == number: >> result="win" >> elif target > number: >> result="low" >> else: >> result="high" >> return result >> >> # start the game >> print("hello. \nI have thought of a number between 1 and 100.") >> >> #collect the user's guess as an interger >> guess = int(input("Can you guess it? ")) >> #Use our function >> higher_or_lower = is_same(computer_number, guess) >> #run the game untill the user is correct >> while higher_or_lower != "win" >> if higher_or_lower == "to low" >> guess = int(input("Sorry, you are too low. Try again.")) >> else: >> guess = int(input("Sorry your are too high. Try again.")) >> >> higher_or_lower = is_same(computer_number, guess) >> >> #end of game >> input("Correct!\nWell Done\n\n\nPress RETURN to exit.") > > Hey again, i'm sorry for bothering you with so many questions i just did not want to keep asking my teacher who is on holiday these. > > I have another issue where the code runs but i can guess every number from 1-100 but it always says Sorry your are too high. Try again. I don't understand what i have done to cause this. > What values can 'is_same' return? Which of those values are you checking for in the loop? From flebber.crue at gmail.com Tue Jan 3 21:09:35 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Tue, 3 Jan 2017 18:09:35 -0800 (PST) Subject: Screwing Up looping in Generator In-Reply-To: <1e433232-7db7-4f1c-8637-50a3da469b20@googlegroups.com> References: <009201d26627$9aee9390$27b23dae@sambora> <1e433232-7db7-4f1c-8637-50a3da469b20@googlegroups.com> Message-ID: <08c82541-86f7-4811-abeb-1205d2064506@googlegroups.com> Untested as i wrote this in notepad at work but, if i first use the generator to create a set of filenames and then iterate it then call the generator anew to process file may work? Good idea or better available? def get_list_of_names(generator_arg): name_set = set() for name in generator_arg: base = os.path.basename(name.name) filename = os.path.splitext(base)[0] name_set.add(filename) return name_set for file_name in name_set: directory = "output" with open(os.path.join(directory, filename, 'w', newline='') as csvf: for file in rootobs: # create and write csv Sayth From python at lucidity.plus.com Tue Jan 3 21:09:40 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 02:09:40 +0000 Subject: How Best to Coerce Python Objects to Integers? In-Reply-To: <586c4b6b$0$1612$c3e8da3$5496439d@news.astraweb.com> References: <3d35c052-a4c2-44f9-b294-cb1a77d77477@googlegroups.com> <586C28EC.8010607@stoneleaf.us> <8e7195db-e33f-18e3-e1d9-74f9a458e69b@lucidity.plus.com> <586c4b6b$0$1612$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 04/01/17 01:10, Steve D'Aprano wrote: > On Wed, 4 Jan 2017 11:22 am, Erik wrote: >> What he *should* have done is just validated his input strings before >> presenting the string to int() - i.e., process the input with knowledge >> that is specific to the problem domain before calling the >> general-purpose function. > > That's the Look Before You Leap solution. But in this case, given the > scenario described (a text file with a few typos), the best way is to ask > for forgiveness rather than permission: Yes, probably, in this case ;) OK, in the case where the function you're calling is sane (and Python's int() is - it won't blindly accept "0x101" as a hex value, for example) then it's probably right that leaping first and then, on failure, processing the value and leaping again is the right thing to do. [I tend to work in an environment where things I'm calling may not be sane (and in some cases I may never know), so I will usually consider LBYL as a way of CMA ;)]. In this whole discussion there has been no mention of what happens when the function returns None, though. > Another thought: if he is receiving human generated input, there is an > argument to be made for accepting "look alikes" -- e.g. maybe the data was > entered by Aunt Tilly, who was a typist in the 1960s and can't break the > habit of using l or I interchangeably for 1, and O for 0. Sure - and that's what I meant by processing the string according to his problem "domain". If he has Aunt Tillys doing his data input, then l->1 and 0->O may be a reasonable thing (I recently did a project where things like converting Icelandic's Eth and Thorn runic letters to 'D' and 'P' - though morally wrong ;) - was a reasonable character translation because that's what people actually typed in). E. From python at deborahswanson.net Tue Jan 3 21:10:16 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 18:10:16 -0800 Subject: Screwing Up looping in Generator In-Reply-To: Message-ID: <009e01d2662f$b1bf0e30$27b23dae@sambora> Sayth Renshaw wrote, on January 03, 2017 5:36 PM > > So can I call the generator twice and receive the same file > twice in 2 for loops? > > Once to get the files name and the second to process? > > for file in rootobs: > base = os.path.basename(file.name) > write_to = os.path.join("output", > os.path.splitext(base)[0] + ".csv") > with open(write_to, 'w', newline='') as csvf: > for file in rootobs: > # create and write csv > > Cheers > > Sayth I don't see why not, if you let the first one run to completion and then do it again a second time. Assuming your generator doesn't somehow delete or modify the file specifications as it yields them. It would be helpful to see the code for rootobs, if you have it. D. From zxpatric at gmail.com Tue Jan 3 21:14:10 2017 From: zxpatric at gmail.com (zxpatric) Date: Wed, 04 Jan 2017 14:14:10 +1200 Subject: Work between multiple processes Message-ID: <2510477911@f38.n261.z1.binkp.net> Hi everyone, I ran into a case that I need to create a work process of an application (Jython so has to call using java.exe) which will collect the data based on what main process indicates. (1) I tried multiprocessing package, no luck. Java.exe can't be called from Process class? (2) I tried subprocess. subprocess.communicate function will wait for the work process to terminate so to return. either (1) or (2) doesn't work out well. Please suggest. Global system queue? Thanks, Patrick. From steve+comp.lang.python at pearwood.info Tue Jan 3 21:16:58 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 14:16:58 +1200 Subject: Hey, I'm new to python so don't judge. References: <802561195@f38.n261.z1.binkp.net> Message-ID: <1096384302@f38.n261.z1.binkp.net> On Wednesday 04 January 2017 13:24, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote: >> On 2017-01-04 01:37, Callum Robinson wrote: >> > On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson >> > wrote: >> >> Im doing a new task from my teacher but i can't seem to find what is >> >> wrong with this code. Can anyone help? >> >> >> >> #mynumber.py >> >> # this game uses a home made function >> >> import random >> >> >> >> #think of a number >> >> computer_number = number.randint(1,100) >> >> >> >> #create the function is_same() >> >> def is_same(target, number: >> >> if target == number: >> >> result="win" >> >> elif target > number: >> >> result="low" >> >> else: >> >> result="high" >> >> return result >> >> >> >> # start the game >> >> print("hello. \nI have thought of a number between 1 and 100.") >> >> >> >> #collect the user's guess as an interger >> >> guess = int(input("Can you guess it? ")) >> >> #Use our function >> >> higher_or_lower = is_same(computer_number, guess) >> >> #run the game untill the user is correct >> >> while higher_or_lower != "win" >> >> if higher_or_lower == "to low" >> >> guess = int(input("Sorry, you are too low. Try again.")) >> >> else: >> >> guess = int(input("Sorry your are too high. Try again.")) >> >> >> >> higher_or_lower = is_same(computer_number, guess) >> >> >> >> #end of game >> >> input("Correct!\nWell Done\n\n\nPress RETURN to exit.") >> > >> > Hey again, i'm sorry for bothering you with so many questions i just did >> > not want to keep asking my teacher who is on holiday these. >> > >> > I have another issue where the code runs but i can guess every number from >> > 1-100 but it always says Sorry your are too high. Try again. I don't >> > understand what i have done to cause this. >> > >> What values can 'is_same' return? >> >> Which of those values are you checking for in the loop? > > I'm sorry but i do not completely understand what you are stating That's okay, you're still learning :-) We've all been where you are, even if some of us have forgotten what its like. Look at your is_same() function. It can return three different things, which gets stored in the higher_or_lower variable: - "win" - "low" - "high" But now look at how you check the result: while higher_or_lower != "win" if higher_or_lower == "to low" guess = int(input("Sorry, you are too low. Try again.")) else: guess = int(input("Sorry your are too high. Try again.")) I see a typo that will prevent your code from running: you are missing a colon after the first line, it should say: while higher_or_lower != "win": Likewise for the "if ..." line, it also needs to end with a colon. So the first thing is that when asking for help, be *extra careful* that the code you show us is the same as the code you are actually running. (Otherwise we waste our time trying to debug code that you aren't even using!) You should always COPY AND PASTE your code, not retype it. But let's assume that your actual code does include the needed colons, so it will run. What values do you check for? - "to low" and that's it. Look again at the values that higher_or_lower can actually be. Is there any way that higher_or_lower gets the value "to low"? No. Remember that Python can't read your mind and realise that when you check for "to low", you actually mean just "low". So there is no way that the first if... clause will be triggered, so it always falls through to the else clause and prints "Sorry your are too high. Try again." (P.S. you mean "Sorry you are too high", not "your are".) -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at deborahswanson.net Tue Jan 3 21:21:59 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 18:21:59 -0800 Subject: Screwing Up looping in Generator In-Reply-To: <1e433232-7db7-4f1c-8637-50a3da469b20@googlegroups.com> Message-ID: <009f01d26631$544d04d0$27b23dae@sambora> Sayth Renshaw wrote, on January 03, 2017 5:55 PM > > On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw wrote: > > So can I call the generator twice and receive the same file > twice in 2 > > for loops? > > > > Once to get the files name and the second to process? > > > > for file in rootobs: > > base = os.path.basename(file.name) > > write_to = os.path.join("output", > os.path.splitext(base)[0] + ".csv") > > with open(write_to, 'w', newline='') as csvf: > > for file in rootobs: > > # create and write csv > > > > Cheers > > > > Sayth > > I just need it to write after each file however the > > with open(#file) as csvf: > > Keeps it all open until every file processed in an output > file with the name of the first file in the generator. > In that case, I think you just need to devise your output file name scheme, and it looks like you want to use 'os.path.splitext(base)[0] + ".csv")'. Then output each file in the same for loop, before you go back for another input file. Just read in the file, do whatever you want to it, and then write it to the new file name, all in the same loop. I don't see any need to loop through rootobs a second time. From cr2001 at hotmail.co.nz Tue Jan 3 21:24:16 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 18:24:16 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: <26855fa4-dc40-43ae-9eaa-15dc1de97842@googlegroups.com> <07f87e48-6bb3-ad25-6f4f-7d350c5b794a@mrabarnett.plus.com> Message-ID: <9a5a2843-4d4b-43de-b580-85889447e284@googlegroups.com> On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote: > On 2017-01-04 01:37, Callum Robinson wrote: > > On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson wrote: > >> Im doing a new task from my teacher but i can't seem to find what is wrong with this code. Can anyone help? > >> > >> #mynumber.py > >> # this game uses a home made function > >> import random > >> > >> #think of a number > >> computer_number = number.randint(1,100) > >> > >> #create the function is_same() > >> def is_same(target, number: > >> if target == number: > >> result="win" > >> elif target > number: > >> result="low" > >> else: > >> result="high" > >> return result > >> > >> # start the game > >> print("hello. \nI have thought of a number between 1 and 100.") > >> > >> #collect the user's guess as an interger > >> guess = int(input("Can you guess it? ")) > >> #Use our function > >> higher_or_lower = is_same(computer_number, guess) > >> #run the game untill the user is correct > >> while higher_or_lower != "win" > >> if higher_or_lower == "to low" > >> guess = int(input("Sorry, you are too low. Try again.")) > >> else: > >> guess = int(input("Sorry your are too high. Try again.")) > >> > >> higher_or_lower = is_same(computer_number, guess) > >> > >> #end of game > >> input("Correct!\nWell Done\n\n\nPress RETURN to exit.") > > > > Hey again, i'm sorry for bothering you with so many questions i just did not want to keep asking my teacher who is on holiday these. > > > > I have another issue where the code runs but i can guess every number from 1-100 but it always says Sorry your are too high. Try again. I don't understand what i have done to cause this. > > > What values can 'is_same' return? > > Which of those values are you checking for in the loop? I'm sorry but i do not completely understand what you are stating From steve+comp.lang.python at pearwood.info Tue Jan 3 21:25:34 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 14:25:34 +1200 Subject: Hey, I'm new to python so don't judge. References: <637094488@f38.n261.z1.binkp.net> Message-ID: <4159531582@f38.n261.z1.binkp.net> On Wednesday 04 January 2017 12:25, Callum Robinson wrote: > Hey man thanks, the sad thing is i have no idea why i put that in. I must be > having a terrible day. Don't worry about it. The difference between a beginner and an expert is *not* that experts make fewer mistakes, but that experts know how to fix those mistakes so quickly that they don't even notice them. I know people who are seemingly incapable of typing more than three words in the row without two typos, but they manage to be excellent programmers. They'll typo some code: comptuer_number = number.radnint(1, 100) try to run it, realise their mistake and fix it: comptuer_number = random.radnint(1, 100) then run it again and realise there is at least one more mistake, and fix it: comptuer_number = random.randint(1, 100) and then a third time: computer_number = random.randint(1, 100) while a beginner is still puzzling over their first mistake. Don't stress about it, it is all just part of the learning process. All code starts off full of bugs. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at lucidity.plus.com Tue Jan 3 21:35:35 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 02:35:35 +0000 Subject: Hey, I'm new to python so don't judge. In-Reply-To: <9a5a2843-4d4b-43de-b580-85889447e284@googlegroups.com> References: <26855fa4-dc40-43ae-9eaa-15dc1de97842@googlegroups.com> <07f87e48-6bb3-ad25-6f4f-7d350c5b794a@mrabarnett.plus.com> <9a5a2843-4d4b-43de-b580-85889447e284@googlegroups.com> Message-ID: On 04/01/17 02:24, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote: >> What values can 'is_same' return? >> >> Which of those values are you checking for in the loop? > > I'm sorry but i do not completely understand what you are stating You need to think about the specific things (their type, their exact values) that your functions might return. Printing some trace output is a classic way of debugging your program. If, after this line: >>>> higher_or_lower = is_same(computer_number, guess) ... you added: print (higher_or_lower) ... what values do you then see being output? How will those values be processed by the conditions you see that work on the "higher_or_lower" variable? E. From bouncingcats at gmail.com Tue Jan 3 21:36:00 2017 From: bouncingcats at gmail.com (David) Date: Wed, 4 Jan 2017 13:36:00 +1100 Subject: Clickable hyperlinks In-Reply-To: <008e01d26624$7c84a7d0$27b23dae@sambora> References: <4e756eb9-842b-98fe-3fa1-3b05baebe71a@lucidity.plus.com> <008e01d26624$7c84a7d0$27b23dae@sambora> Message-ID: On 4 January 2017 at 11:50, Deborah Swanson wrote: > Erik wrote, on January 03, 2017 3:30 PM >> >> When you start a new topic on the list, could you please write a new >> message rather than replying to an existing message and changing the >> title/subject? >> > Certainly. I've been on many other lists before (but none since about > 2011), and no one complained of or even mentioned this problem. But if > it's a problem with some email readers now, I can easily just start a > new message. Just being lazy and following old habits, I guess. ;) To be clear, the problem is not "with some email readers". The problem is that it breaks the thread management features that are built into every email message, making it impossible to display threads correctly. As can be seen here for example: https://mail.python.org/pipermail/python-list/2017-January/thread.html Note how most threads start in the leftmost column and replies are indented. But this does not occur where you began the new subject: "Clickable hyperlinks". Thanks. From python at deborahswanson.net Tue Jan 3 21:40:15 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 18:40:15 -0800 Subject: Screwing Up looping in Generator In-Reply-To: Message-ID: <00a201d26633$e1ee6cf0$27b23dae@sambora> Erik wrote, on January 03, 2017 5:26 PM > Hi, > > On 04/01/17 01:12, Deborah Swanson wrote: > > The main reason you might want to catch the StopIteration > exception is > > to do something else before your code simply stops running. If all > > you're doing is run a generator til it's out of gas, and that's all > > you want it to do, then there's no need to catch anything. > > Ah! OK, I see where the lines are being crossed now ;) Although > StopIteration is an exception, it is something that the 'for/iter' > machinery handles for you under the covers. Each 'for' statement > effectively has a 'try' block around it that catches > 'StopIteration' and > just terminates that particular 'for' loop and continues on with the > remainder of your script. > > Raising a 'StopIteration' is an internal mechanism used to determine > when an iterator (i.e., the thing a 'for' loop is looping over) has > exhausted itself. It's not something a regular user is ever > expected to > know about let alone catch. > > When execution falls out of the bottom of a generator, > StopIteration is > raise (compared to a regular function or method returning 'None'). > > E. Looks like those MIT professors knew what they were teaching us after all! Sneaky, but they never once hinted that this was any deep dark secret. Just a way to deal with generators that will stop after a finite and unknown number of yields, and you want your code to keep going after the generator quits. I thought I was just regurgitating a standard approach, and surprised to get so much push back on it. Seems like python uses a lot of its external functionality (what we normally code with) for internal behavior too. In this case, and for empty returns, they're raising exceptions, catching them and then doing what they want to. I suppose, if you don't want the method returning None, you could catch that exception and return something else. Or return nothing at all, which is what I usually want to do when those pesky Nones crop up. But I'm not entirely sure how you would make it return nothing at all. From cr2001 at hotmail.co.nz Tue Jan 3 21:47:43 2017 From: cr2001 at hotmail.co.nz (Callum Robinson) Date: Tue, 3 Jan 2017 18:47:43 -0800 (PST) Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: <26855fa4-dc40-43ae-9eaa-15dc1de97842@googlegroups.com> <07f87e48-6bb3-ad25-6f4f-7d350c5b794a@mrabarnett.plus.com> <9a5a2843-4d4b-43de-b580-85889447e284@googlegroups.com> Message-ID: On Wednesday, January 4, 2017 at 3:35:53 PM UTC+13, Erik wrote: > On 04/01/17 02:24, Callum Robinson wrote: > > On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote: > >> What values can 'is_same' return? > >> > >> Which of those values are you checking for in the loop? > > > > I'm sorry but i do not completely understand what you are stating > > You need to think about the specific things (their type, their exact > values) that your functions might return. Printing some trace output is > a classic way of debugging your program. If, after this line: > > >>>> higher_or_lower = is_same(computer_number, guess) > > ... you added: > > print (higher_or_lower) > > ... what values do you then see being output? How will those values be > processed by the conditions you see that work on the "higher_or_lower" > variable? > > E. I did it and this is what it states when i run it hello. I have thought of a number between 1 and 100. Can you guess it? 5 Low Sorry , you are too high. Try again. Does this mean the number i entered is to low but the code is still stating it is to high? From steve+comp.lang.python at pearwood.info Tue Jan 3 21:50:34 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 14:50:34 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do References: <1881369740@f38.n261.z1.binkp.net> Message-ID: <1246545673@f38.n261.z1.binkp.net> On Wednesday 04 January 2017 12:10, Cameron Simpson wrote: > On 03Jan2017 12:57, Steve D'Aprano wrote: >>I dislike the Unix-style Vim/Emacs text editors, I prefer a traditional >>GUI-based editor. So my "IDE" is: >>- Firefox, for doing searches and looking up documentation; >>- an GUI programmer's editor, preferably one with a tab-based >> interface, such as geany or kate; >>- a tab-based terminal. > > "traditional GUI-based editor" > > For those of us who spent a lot of our earlier time on terminals (actual > physical terminals) we consider GUIs "new fangled". > > Just narking, > Cameron Simpson Heh, GUI editors have been around since at least 1984, if not older, which makes them older than half the programmers in the world. I'm not sure what an *un*traditional GUI-based editor would look like. Maybe one that used a ribbon-based interface, like MS Office? Or perhaps Leo? http://leoeditor.com/ [My resolution for 2017: stop talking about Leo and actually download the damn thing and try it out.] -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From steve+comp.lang.python at pearwood.info Tue Jan 3 21:50:34 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 14:50:34 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do Message-ID: <2012295452@f38.n261.z1.binkp.net> On Wednesday 04 January 2017 12:10, Cameron Simpson wrote: > On 03Jan2017 12:57, Steve D'Aprano wrote: >>I dislike the Unix-style Vim/Emacs text editors, I prefer a traditional >>GUI-based editor. So my "IDE" is: >>- Firefox, for doing searches and looking up documentation; >>- an GUI programmer's editor, preferably one with a tab-based >> interface, such as geany or kate; >>- a tab-based terminal. > > "traditional GUI-based editor" > > For those of us who spent a lot of our earlier time on terminals (actual > physical terminals) we consider GUIs "new fangled". > > Just narking, > Cameron Simpson Heh, GUI editors have been around since at least 1984, if not older, which makes them older than half the programmers in the world. I'm not sure what an *un*traditional GUI-based editor would look like. Maybe one that used a ribbon-based interface, like MS Office? Or perhaps Leo? http://leoeditor.com/ [My resolution for 2017: stop talking about Leo and actually download the damn thing and try it out.] -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at deborahswanson.net Tue Jan 3 21:58:42 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 04 Jan 2017 14:58:42 +1200 Subject: Clickable hyperlinks Message-ID: <786148622@f38.n261.z1.binkp.net> Chris Angelico wrote, on January 04, 2017 4:16 AM > > On Wed, Jan 4, 2017 at 10:43 PM, Deborah Swanson > wrote: > > I'm quite well aware by now that there is no one-sentence > answer to my > > original question, if there's any coherent answer at all. > Them's the > > breaks. Live with it or live without it, it doesn't care. > > Yeah, there's no simple answer; however, you'll find that > Python on many platforms is entirely capable of popping a URL > up in the user's default browser. Check this out: > > >>> import antigravity > > This uses the 'webbrowser' module, which knows about a number > of different ways to open a browser, and will attempt them > all. So if you can figure out the UI part of things, actually > making the link pop up in a browser isn't too hard; for > instance, if you're doing OAuth at the command line and need > the user to go and authenticate, you can simply > webbrowser.open("http://......./") and it'll DTRT. > Thank you, thank you! Finally, at least one person on this list knows about something (anything) in the python world that is internet aware. It's also occurred to me that Beautifulsoup downloads data from a url, so that code must have access to some kind of an internet engine too. I googled antigravity and found a number of interesting links. The History of Python: import antigravity http://python-history.blogspot.com/2010/06/import-antigravity.html Among other things, it was added to Python 3 in 2010, so it's been around a little while. And a comment mentions that "The antigravity module is also included in Python 2.7." And a reddit poster tells us that "if you type 'import antigravity' into a Python command line your default browser opens the XKCD comic 'Python' in a tab." https://www.reddit.com/r/ProgrammerHumor/comments/1hvb5n/til_if_you_type _import_antigravity_into_a_python/ An "import antigravity" video at https://www.youtube.com/watch?v=_V0V6Rk6Fp4 And its page in the Package Index: https://pypi.python.org/pypi/antigravity/0.1, with a Page Not Found Error for the Home Page. So it doesn't look like there's any alternative but to download it and look at the code. Yes, I'd gotten as far as figuring out that you don't need a clickable link. Code that opens a url in a browse would do the job just fine. Or the webbrowser.open("http://......./") in a Linux terminal you suggest. (I just have to get my Linux machine up and running again to try it.) All in all, given that clickable urls in a console is a non-starter, this hits the nail on the head. Many thanks again! Deborah From steve+comp.lang.python at pearwood.info Tue Jan 3 22:04:02 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 15:04:02 +1200 Subject: Clickable hyperlinks References: <1619621956@f38.n261.z1.binkp.net> Message-ID: <3040162315@f38.n261.z1.binkp.net> On Wednesday 04 January 2017 14:04, Deborah Swanson wrote: > Steve D'Aprano wrote, on January 03, 2017 4:56 PM [...] >> Python can't force the console to treat something as a >> clickable link, if the console has no capacity for clickable >> links. Nor can Python predict what format the console uses to >> recognise a link. >> >> The best you can do is to experiment with a couple of simple >> formats and see which, if any, your console understands: >> >> # HTML >> Example. >> >> # URL in angle brackets >> Example >> >> # URL alone >> http://www.example.com >> >> # I can't remember what these are called >> >> >> # Markup >> [Example](http://www.example.com) >> >> # Rest >> `Example `_ [...] > I tried all of your examples in IDLE, and they all get syntax errors. > I'd expect the same from PyCharm, though I didn't try it. Syntax errors? How can you get syntax errors from *output* text? The above are just text, no different from: Hello World! Of course you have to put quotes around them to enter them in your source code. We don't expect this to work: print(Hello World!) you have to use a string literal with quotes: print('Hello World!') Same for all of the above. py> print('Example.') Example. That's the Python interactive interpreter (with a custom prompt, I prefer "py>" rather than the default ">>>"). Or I can do this from the operating system's shell prompt: steve at runes:~$ python -c "print 'http://www.example.com'" http://www.example.com If I do this in GNOME Terminal 2.30.2, the URL http... is a clickable link. But that's specific to the terminal. Other terminals may or may not recognise it. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at deborahswanson.net Tue Jan 3 22:04:36 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 19:04:36 -0800 Subject: Clickable hyperlinks In-Reply-To: <586c4834$0$1591$c3e8da3$5496439d@news.astraweb.com> Message-ID: <00a501d26637$485f8660$27b23dae@sambora> Steve D'Aprano wrote, on January 03, 2017 4:56 PM > On Wed, 4 Jan 2017 10:32 am, Deborah Swanson wrote: > > > > The GUI consoles I have are in Pycharm, the IDLE that comes with > > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access > > when I open them, so they're capable of opening links, but whether > > that means their output space is capable of handling > clickable links I > > don't know. > > > > I do know printing a full url with the %s specifier or > entering a url > > and clicking enter just gives you the plain text url. > Obviously, not > > all GUI consoles are enabled recognize and make clickable > links from > > correctly formatted urls. > > > > I was hoping there was some functionality in python to make > clickable > > links. Could be a package, if the core language doesn't have it. > > Unfortunately there is no such thing as an application- > independent "clickable link". I'm getting that. > Excel can make clickable links, because it only has to deal > with a single suite of related applications: Excel, Word, and > the rest of Microsoft Office. Likewise LibreOffice. > > But Python has to deal with an infinite number of potential > or hypothetical consoles, and there is no standard for > "clickable links" that all, or even most, consoles understand. > > Python can't force the console to treat something as a > clickable link, if the console has no capacity for clickable > links. Nor can Python predict what format the console uses to > recognise a link. > > The best you can do is to experiment with a couple of simple > formats and see which, if any, your console understands: > > # HTML > Example. > > # URL in angle brackets > Example > > # URL alone > http://www.example.com > > # I can't remember what these are called > > # Markup > [Example](http://www.example.com) > > # Rest > `Example `_ > > > > > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered > up, and sure enough, things got worse. I tried all of your examples in IDLE, and they all get syntax errors. I'd expect the same from PyCharm, though I didn't try it. I think I need to write a class to do it in a particular application, preferably in PyCharm, though I'd need some kind of internet access engine to do it anywhere. I can look up how Firefox does it, pretty sure I have that somewhere. Maybe there's a way... Maybe it's a stupid idea, but originally I wanted to output links and click on them while I was still debugging in PyCharm, without having to save the csv and reopen it in Excel, to see what was in a listing* while I was still debugging. PyCharm does have links in its console output that take you to positions in the code when you click on them, so it seems like all the basic infrastructure is there. I just have to figure out how to do it for internet urls that I output. * listing is a real estate listing with a url, part of a project I'm working on, in case you haven't read the "Cleaning up conditionals" thread. From python at lucidity.plus.com Tue Jan 3 22:11:26 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 03:11:26 +0000 Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: <26855fa4-dc40-43ae-9eaa-15dc1de97842@googlegroups.com> <07f87e48-6bb3-ad25-6f4f-7d350c5b794a@mrabarnett.plus.com> <9a5a2843-4d4b-43de-b580-85889447e284@googlegroups.com> Message-ID: <38efa069-5beb-bbc9-72d6-09280cd4f79e@lucidity.plus.com> On 04/01/17 02:47, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 3:35:53 PM UTC+13, Erik wrote: > I did it and this is what it states when i run it > > hello. > I have thought of a number between 1 and 100. > Can you guess it? > 5 > Low > Sorry , you are too high. Try again. > > Does this mean the number i entered is to low but the code is still stating it is to high? The last code you posted will return "low", "high" or "win" from the "is_same()" function. How does that relate to the "Low" that you are now printing? You must have changed something. If you are testing strings as return values then they must be _exactly_ the same - including case and spaces etc. Always remember that your computer is an idiot. It's the fastest idiot that you'll ever know, and it could become your best friend. But it IS an idiot ;) E. From steve+comp.lang.python at pearwood.info Tue Jan 3 22:16:58 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 14:16:58 +1100 Subject: Hey, I'm new to python so don't judge. References: <26855fa4-dc40-43ae-9eaa-15dc1de97842@googlegroups.com> <07f87e48-6bb3-ad25-6f4f-7d350c5b794a@mrabarnett.plus.com> <9a5a2843-4d4b-43de-b580-85889447e284@googlegroups.com> Message-ID: <586c692c$0$2860$c3e8da3$76491128@news.astraweb.com> On Wednesday 04 January 2017 13:24, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote: >> On 2017-01-04 01:37, Callum Robinson wrote: >> > On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson >> > wrote: >> >> Im doing a new task from my teacher but i can't seem to find what is >> >> wrong with this code. Can anyone help? >> >> >> >> #mynumber.py >> >> # this game uses a home made function >> >> import random >> >> >> >> #think of a number >> >> computer_number = number.randint(1,100) >> >> >> >> #create the function is_same() >> >> def is_same(target, number: >> >> if target == number: >> >> result="win" >> >> elif target > number: >> >> result="low" >> >> else: >> >> result="high" >> >> return result >> >> >> >> # start the game >> >> print("hello. \nI have thought of a number between 1 and 100.") >> >> >> >> #collect the user's guess as an interger >> >> guess = int(input("Can you guess it? ")) >> >> #Use our function >> >> higher_or_lower = is_same(computer_number, guess) >> >> #run the game untill the user is correct >> >> while higher_or_lower != "win" >> >> if higher_or_lower == "to low" >> >> guess = int(input("Sorry, you are too low. Try again.")) >> >> else: >> >> guess = int(input("Sorry your are too high. Try again.")) >> >> >> >> higher_or_lower = is_same(computer_number, guess) >> >> >> >> #end of game >> >> input("Correct!\nWell Done\n\n\nPress RETURN to exit.") >> > >> > Hey again, i'm sorry for bothering you with so many questions i just did >> > not want to keep asking my teacher who is on holiday these. >> > >> > I have another issue where the code runs but i can guess every number from >> > 1-100 but it always says Sorry your are too high. Try again. I don't >> > understand what i have done to cause this. >> > >> What values can 'is_same' return? >> >> Which of those values are you checking for in the loop? > > I'm sorry but i do not completely understand what you are stating That's okay, you're still learning :-) We've all been where you are, even if some of us have forgotten what its like. Look at your is_same() function. It can return three different things, which gets stored in the higher_or_lower variable: - "win" - "low" - "high" But now look at how you check the result: while higher_or_lower != "win" if higher_or_lower == "to low" guess = int(input("Sorry, you are too low. Try again.")) else: guess = int(input("Sorry your are too high. Try again.")) I see a typo that will prevent your code from running: you are missing a colon after the first line, it should say: while higher_or_lower != "win": Likewise for the "if ..." line, it also needs to end with a colon. So the first thing is that when asking for help, be *extra careful* that the code you show us is the same as the code you are actually running. (Otherwise we waste our time trying to debug code that you aren't even using!) You should always COPY AND PASTE your code, not retype it. But let's assume that your actual code does include the needed colons, so it will run. What values do you check for? - "to low" and that's it. Look again at the values that higher_or_lower can actually be. Is there any way that higher_or_lower gets the value "to low"? No. Remember that Python can't read your mind and realise that when you check for "to low", you actually mean just "low". So there is no way that the first if... clause will be triggered, so it always falls through to the else clause and prints "Sorry your are too high. Try again." (P.S. you mean "Sorry you are too high", not "your are".) -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From steve+comp.lang.python at pearwood.info Tue Jan 3 22:25:35 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 14:25:35 +1100 Subject: Hey, I'm new to python so don't judge. References: <5e69d496-f5d3-4aab-ae78-3d1d57d3aee4@googlegroups.com> <71b9f7e5-c255-435f-e7d6-3cc7abce35b4@lucidity.plus.com> <3021b6e8-d843-413f-a62b-f51461ed1b49@googlegroups.com> <586c4ccf$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: <586c6b31$0$11110$c3e8da3@news.astraweb.com> On Wednesday 04 January 2017 12:25, Callum Robinson wrote: > Hey man thanks, the sad thing is i have no idea why i put that in. I must be > having a terrible day. Don't worry about it. The difference between a beginner and an expert is *not* that experts make fewer mistakes, but that experts know how to fix those mistakes so quickly that they don't even notice them. I know people who are seemingly incapable of typing more than three words in the row without two typos, but they manage to be excellent programmers. They'll typo some code: comptuer_number = number.radnint(1, 100) try to run it, realise their mistake and fix it: comptuer_number = random.radnint(1, 100) then run it again and realise there is at least one more mistake, and fix it: comptuer_number = random.randint(1, 100) and then a third time: computer_number = random.randint(1, 100) while a beginner is still puzzling over their first mistake. Don't stress about it, it is all just part of the learning process. All code starts off full of bugs. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From grant.b.edwards at gmail.com Tue Jan 3 22:35:16 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Wed, 04 Jan 2017 15:35:16 +1200 Subject: Clickable hyperlinks Message-ID: <4013467634@f38.n261.z1.binkp.net> On 2017-01-03, Deborah Swanson wrote: > Grant Edwards wrote, on January 03, 2017 3:13 PM >> >> On 2017-01-03, Deborah Swanson wrote: >> >> > I'm sorry, I should have said a GUI console because I >> wouldn't expect >> > a text-based console to produce clickable links. >> >> What's a "GUI console"? > The GUI consoles I have are in Pycharm, the IDLE that comes with > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when > I open them, so they're capable of opening links, but whether that means > their output space is capable of handling clickable links I don't know. Thanks, that's a bit clearer. For those of us from the Unix world "console" means something else. > I do know printing a full url with the %s specifier or entering a url > and clicking enter just gives you the plain text url. Obviously, not all > GUI consoles are enabled recognize and make clickable links from > correctly formatted urls. > > I was hoping there was some functionality in python to make clickable > links. Could be a package, if the core language doesn't have it. There is no definition for what a "clickable link" is unless you're sending HTML to a web browser. That means there's no way to create funcationality to make one. -- Grant Edwards grant.b.edwards Yow! I'm not an Iranian!! at I voted for Dianne gmail.com Feinstein!! From grant.b.edwards at gmail.com Tue Jan 3 22:42:28 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Wed, 04 Jan 2017 15:42:28 +1200 Subject: Clickable hyperlinks Message-ID: <4217700806@f38.n261.z1.binkp.net> On 2017-01-04, Michael Torrie wrote: > On my Linux machine, the terminal emulators I've used all make a regular > url printed out into a clickable link (or at least a right-clickable > link). This is just something they try to do with all things that look > like urls. Sometimes it's helpful, often it's annoying. What I have done is defined a window manager root menu entry that opens a web browser on the current text selection. That lets me "click" on a link in any application that suport the standard X11 text-selection mechanism (which is almost all of them). >> I was hoping there was some functionality in python to make clickable >> links. Could be a package, if the core language doesn't have it. > > No, there is not. If you made a full GUI app using a toolkit like > GTK or Qt, you can indeed place hyperlinks on your forms and the > operating system will automatically connect them to the web browser. > But not in text-mode terminal apps. For me it's double-click to select the url in the terminal window or PDF viewer or whaterver, then right-click on the root window and pick the menu entry that says 'Firefox [sel]' or 'Chrome [sel]'. It's a few extra steps, but it works for almostly any application that displays text. -- Grant Edwards grant.b.edwards Yow! Where do your SOCKS at go when you lose them in gmail.com th' WASHER? From python at deborahswanson.net Tue Jan 3 22:46:45 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 19:46:45 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <00ab01d2663d$2c556d80$27b23dae@sambora> David wrote, on January 03, 2017 6:36 PM > > On 4 January 2017 at 11:50, Deborah Swanson > wrote: > > Erik wrote, on January 03, 2017 3:30 PM > >> > >> When you start a new topic on the list, could you please > write a new > >> message rather than replying to an existing message and > changing the > >> title/subject? > >> > > Certainly. I've been on many other lists before (but none > since about > > 2011), and no one complained of or even mentioned this > problem. But if > > it's a problem with some email readers now, I can easily > just start a > > new message. Just being lazy and following old habits, I guess. ;) > > To be clear, the problem is not "with some email readers". Actually it is, or at least it doesn't happen in all email readers. Mine, for instance, never breaks up threads. > The problem is that it breaks the thread management features > that are built into every email message, making it impossible > to display threads correctly. Again, I think this is in some modern email readers. Apparently older ones were more robust. > As can be seen here for example: > https://mail.python.org/pipermail/python-> list/2017-January/thread.html > > Note how most threads start in the leftmost column and > replies are indented. But this does not occur where you began > the new subject: "Clickable hyperlinks". Yes, pipermail does seem to have a few bugs, this is the second one I've seen. > Thanks. I did say in the message you're replying to that I will try to remember to start new threads with brand new messages. (Even though I think pipermail's behavior is a bug, that's what many people read the list from.) From steve+comp.lang.python at pearwood.info Tue Jan 3 22:50:35 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 14:50:35 +1100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. References: <586b0517$0$1584$c3e8da3$5496439d@news.astraweb.com> <20170104011030.GA46099@cskk.homeip.net> Message-ID: <586c710d$0$11099$c3e8da3@news.astraweb.com> On Wednesday 04 January 2017 12:10, Cameron Simpson wrote: > On 03Jan2017 12:57, Steve D'Aprano wrote: >>I dislike the Unix-style Vim/Emacs text editors, I prefer a traditional >>GUI-based editor. So my "IDE" is: >>- Firefox, for doing searches and looking up documentation; >>- an GUI programmer's editor, preferably one with a tab-based >> interface, such as geany or kate; >>- a tab-based terminal. > > "traditional GUI-based editor" > > For those of us who spent a lot of our earlier time on terminals (actual > physical terminals) we consider GUIs "new fangled". > > Just narking, > Cameron Simpson Heh, GUI editors have been around since at least 1984, if not older, which makes them older than half the programmers in the world. I'm not sure what an *un*traditional GUI-based editor would look like. Maybe one that used a ribbon-based interface, like MS Office? Or perhaps Leo? http://leoeditor.com/ [My resolution for 2017: stop talking about Leo and actually download the damn thing and try it out.] -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From torriem at gmail.com Tue Jan 3 22:51:37 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 3 Jan 2017 20:51:37 -0700 Subject: Clickable hyperlinks In-Reply-To: <00a801d2663a$a76e0f70$27b23dae@sambora> References: <00a801d2663a$a76e0f70$27b23dae@sambora> Message-ID: <2c2aa0ca-ac6f-448c-95cc-0750f67cc041@gmail.com> On 01/03/2017 08:28 PM, Deborah Swanson wrote: > I think you're making this too complicated. I meant a console in a GUI > application. Ahh. Well, a "console in a GUI application" is whatever you make it[1]. There's no single "GUI console" hence my confusion and the confusion expressed by the other poster. I was under the impression you are talking about printing something to standard out with, for example, print(). Is this so, or are you using a GUI toolkit to construct your application. What GUI toolkit are you using? As I said, in Qt or GTK there are various ways to display hyperlinks. For example, Qt lets you place a hyperlink in a form, or inside of a text entry/display widget. I still get the impression that you're working with standard out, using print(). If so, then no, there's not going to be a way to force the OS to make the output clickable, at least on Windows. > Not true. Pycharm uses links in it's console output, they just aren't > internet links. They link back to lines of code being referred to. I think the problem here is the terminology with specific meaning in Windows and Linux. I'm referring to either the Win32 console window (which is where cmd.exe runs), or a terminal emulator in Linux, which is where you can interact with the bash shell and run command-line programs. When people normally run python apps that are not graphical, they normally do it from the Windows console (via cmd.exe) or in Linux from Bash running in a terminal emulator. Graphical apps do their own thing as far as displaying data and making windows with clickable links in them. [1] PyCharm and IDLE make "console" windows that are really normal GUI windows and they direct the output from Python apps there. They may also choose to display clickable links for things like errors. But once the app is run outside of PyCharm, the output of the app would go to either a Windows console window, or a terminal in Linux. If you wanted your app to make it's own window and display clickable links, you're back to looking at a GUI toolkit (which is what PyCharm and IDLE are built with) like Qt, GTK, Tk, wxWidgets, or something else. From ceh329 at gmail.com Tue Jan 3 23:02:52 2017 From: ceh329 at gmail.com (Charles Heizer) Date: Wed, 04 Jan 2017 16:02:52 +1200 Subject: MySQL schema sync or diff Message-ID: <520540773@f38.n261.z1.binkp.net> Hello, I have a MySQL database that is not managed (yet) and I would like to get an output or diff against my new model file. I'm using flask-sqlalchemy. Are there any modules that would help me discover the differences so that I can script a migration to begin using flask-migrate? Thanks! From steve+comp.lang.python at pearwood.info Tue Jan 3 23:04:02 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 15:04:02 +1100 Subject: Clickable hyperlinks References: <586c4834$0$1591$c3e8da3$5496439d@news.astraweb.com> <00a501d26637$485f8660$27b23dae@sambora> Message-ID: <586c7434$0$1617$c3e8da3$5496439d@news.astraweb.com> On Wednesday 04 January 2017 14:04, Deborah Swanson wrote: > Steve D'Aprano wrote, on January 03, 2017 4:56 PM [...] >> Python can't force the console to treat something as a >> clickable link, if the console has no capacity for clickable >> links. Nor can Python predict what format the console uses to >> recognise a link. >> >> The best you can do is to experiment with a couple of simple >> formats and see which, if any, your console understands: >> >> # HTML >> Example. >> >> # URL in angle brackets >> Example >> >> # URL alone >> http://www.example.com >> >> # I can't remember what these are called >> >> >> # Markup >> [Example](http://www.example.com) >> >> # Rest >> `Example `_ [...] > I tried all of your examples in IDLE, and they all get syntax errors. > I'd expect the same from PyCharm, though I didn't try it. Syntax errors? How can you get syntax errors from *output* text? The above are just text, no different from: Hello World! Of course you have to put quotes around them to enter them in your source code. We don't expect this to work: print(Hello World!) you have to use a string literal with quotes: print('Hello World!') Same for all of the above. py> print('Example.') Example. That's the Python interactive interpreter (with a custom prompt, I prefer "py>" rather than the default ">>>"). Or I can do this from the operating system's shell prompt: steve at runes:~$ python -c "print 'http://www.example.com'" http://www.example.com If I do this in GNOME Terminal 2.30.2, the URL http... is a clickable link. But that's specific to the terminal. Other terminals may or may not recognise it. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From torriem at gmail.com Tue Jan 3 23:05:18 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 3 Jan 2017 21:05:18 -0700 Subject: Clickable hyperlinks In-Reply-To: <00ab01d2663d$2c556d80$27b23dae@sambora> References: <00ab01d2663d$2c556d80$27b23dae@sambora> Message-ID: <03e6b340-fa99-cfe7-8c5d-ba2dcd0b7aa0@gmail.com> On 01/03/2017 08:46 PM, Deborah Swanson wrote: > Actually it is, or at least it doesn't happen in all email readers. > Mine, for instance, never breaks up threads. Mine doesn't either, which illustrates the issue. This message, for example appears under a long thread that started out life as "mentor training python Romania with certification" and then changed to "Cleaning up conditionals" and then changed to "Clickable hyperlinks." All in one thread. My client doesn't break them up because they all tie together via the message-id header. And most of us would not like our client to break a thread just because the subject changes. Often in long conversations there are twists and turns in the discussion and sometimes side-paths are explored, and the subject often is changed to reflect this. With a truly threaded email reader (one that shows a tree of messages, not just chronological order), this works out very well. So if a discussion has a natural evolution into various other topics, it is often considered okay to just change the subject but continue the thread. Other times, it's better to start a new thread. Where that line is is hard to say! > I did say in the message you're replying to that I will try to remember > to start new threads with brand new messages. (Even though I think > pipermail's behavior is a bug, that's what many people read the list > from.) Sounds good. I don't know of anyone that reads on the pipermail archive, except in response to web searches. Most people use clients of some kind, NNTP or email. And those that group messages according to message-id (most clients except for ones that try to be smart like Gmail web or Outlook) will show all the topics I mentioned before as one giant thread, which is by design (that's what message-id headers are for). From python at deborahswanson.net Tue Jan 3 23:07:08 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 04 Jan 2017 16:07:08 +1200 Subject: Clickable hyperlinks Message-ID: <2359161402@f38.n261.z1.binkp.net> D'Arcy Cain wrote, on Wednesday, January 04, 2017 5:03 AM > > Deborah - please trim your quoted text. Yes, I will. Some lists want to have it all to review in one message, some want it trimmed to just the lines you are responding to. I was just waiting to see what this list wants. > On 2017-01-04 04:32 AM, Deborah Swanson wrote: > > But we aren't trying to print strings here, the point is to produce > > clickable links. I didn't enclose them with quotes because I didn't > > see any point in printing plain text when I wanted > clickable links. I > > I'm not sure what your links are composed of but mine all look like > sequences of characters or "strings." It sounds like you are > trying to > make URL a first class type like strings. integers, floats, etc. I > can't think of any language that treats URLs as first class objects. > Even HTML needs quotes: > > Go here It seemed reasonable that you might be able to print urls, which is why I tried the experiment with all of Steven's suggested formats. But I was highly skeptical that any would work without some kind of modifiers to a bare print statement. > > actually didn't understand why you thought I should print > them, but it > > You want to output them to something. That often involves > printing them > to a particular handler. Yes, that's one of the things I would expect if we could print them. > > never would have occurred to me that you wanted me to print out a > > bunch of silly plain text strings, apparently just for the > heck of it. > > Is that really what you got from his message? Please forgive me, and I hope Steven forgives me too, but I was sick to death of all the beating on a dead horse (using Python to make clickable links in a console, any console). I'd taken heart when he first suggested his print experiment, because it was a plausible approach. But I lost my temper when he upbraided me in this message for failing to enclose my strings in quotes, in a most patronizing kind of way, when printing out plain text was absolutely nowhere on the progress toward a solution scale. I've been quite impressed with Steven's knowledge and talent, and after fending off the throng of unseeing naysayers all afternoon, it was just a little too much. I really should have closed my email reader hours before I read and replied to this message. Shoulda, coulda, woulda. > I can assure you that FF prints the string at some point. It > may wrap > it in HTML tags first but printing is what it does. Also, > the URLs are > stored as strings. SQLite has no URL type. If it did then it would > still store it as a string somewhere. PostGreSQL would let > you create a > URL type if you wanted but you would still need to wrap it in quotes > (single in this case) when you created the entry. I have no doubt that some variant of printing is involved. Transporting urls to the internet is an output process. FF's sqlite implementation does store urls as a text field in at least 2 tables. I would be interested in how FF takes those text urls and opens web pages with them, although I've learned and figured out just today some ways that Python can also do it. Turns out clickable links were a red herring. If Steven's original suggestion included anything but a bare print statement, like the use of a special specifier or linking the print statement to some module, the use of quoted strings would have at least been worthy of consideration. But we all know what print("http://www.wherever.com") would print, and it would be utterly worthless for the purpose at hand. Trying the print statement without the quotes was a least a possibility, if there was any awareness in the print code of urls and what to do with them. That was the whole point of this fishing expedition, as I saw it. To see if there was any undocumented or narrowly known-of features in the print code. > In all the messages in this thread I still don't understand what this > "teensy advantage" is supposed to be. Do you want to be able > to do this: > > make_web_link(http://...) > > instead of: > > make_web_link("http://...") > > -- > D'Arcy J.M. Cain > System Administrator, Vex.Net > http://www.Vex.Net/ IM:darcy at Vex.Net > VoIP: sip:darcy at Vex.Net You probably didn't see my oneliner on the "why do it" part in the swarm of messages on this thread yesterday. In it I mentioned that the use would be to open urls in the data I'm working with while I'm debugging the code that uses them. I want to see what pages they open, without having to leave my IDE. (Obviously I'd have to open another .py file, but that would be easier and quicker than the alternatives.) I never intended my original question to be any more than a frivolous toss out into the sea, to see if anyone knew an answer. I was flat out astonished when it blew up into the mini-monster that it did. Is make_web_link("http://...") valid python code? That's exactly the kind of answer I was looking for, and I will try it (or look it up if it needs something imported) as soon as I send this off. Thank you. It's possible you caught just the tail end of a hot mess without seeing all the irrational vitriol and nonsense that led up to this message. Lucky you. Deborah From python at lucidity.plus.com Tue Jan 3 23:22:59 2017 From: python at lucidity.plus.com (Erik) Date: Wed, 4 Jan 2017 04:22:59 +0000 Subject: Hey, I'm new to python so don't judge. In-Reply-To: <586c6b31$0$11110$c3e8da3@news.astraweb.com> References: <5e69d496-f5d3-4aab-ae78-3d1d57d3aee4@googlegroups.com> <71b9f7e5-c255-435f-e7d6-3cc7abce35b4@lucidity.plus.com> <3021b6e8-d843-413f-a62b-f51461ed1b49@googlegroups.com> <586c4ccf$0$1602$c3e8da3$5496439d@news.astraweb.com> <586c6b31$0$11110$c3e8da3@news.astraweb.com> Message-ID: On 04/01/17 03:25, Steven D'Aprano wrote: > On Wednesday 04 January 2017 12:25, Callum Robinson wrote: > >> Hey man thanks, the sad thing is i have no idea why i put that in. I must be >> having a terrible day. > > Don't worry about it. The difference between a beginner and an expert is *not* > that experts make fewer mistakes, but that experts know how to fix those > mistakes so quickly that they don't even notice them. Hmm. An expert at what? Telling a beginner how to spell something is not the same as coaxing that same beginner to think about something or approach a problem from a particular angle and come to their own solution. We all could have told him the answer up-front. What has that achieved? E. From python at deborahswanson.net Tue Jan 3 23:46:31 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Tue, 3 Jan 2017 20:46:31 -0800 Subject: Clickable hyperlinks In-Reply-To: <586c7434$0$1617$c3e8da3$5496439d@news.astraweb.com> Message-ID: <00b301d26645$8553e490$27b23dae@sambora> Steven D'Aprano wrote, on January 03, 2017 8:04 PM > > On Wednesday 04 January 2017 14:04, Deborah Swanson wrote: > > > Steve D'Aprano wrote, on January 03, 2017 4:56 PM > [...] > >> Python can't force the console to treat something as a clickable > >> link, if the console has no capacity for clickable links. Nor can > >> Python predict what format the console uses to recognise a link. > >> > >> The best you can do is to experiment with a couple of > simple formats > >> and see which, if any, your console understands: > >> > >> # HTML > >> Example. > >> > >> # URL in angle brackets > >> Example > >> > >> # URL alone > >> http://www.example.com > >> > >> # I can't remember what these are called > >> > >> > >> # Markup > >> [Example](http://www.example.com) > >> > >> # Rest > >> `Example `_ > [...] > > > I tried all of your examples in IDLE, and they all get > syntax errors. > > I'd expect the same from PyCharm, though I didn't try it. > > Syntax errors? How can you get syntax errors from *output* text? > > The above are just text, no different from: > > Hello World! I closed the IDLE window these were on, but the error arrow was pointing to punctuation in each case, a colon in one case, angle bracket in another. Sorta seems like IDLE is trying to do something with them and not taking them as simple plain text. But it's not making hyperlinks, so I'm not sure how much I care exactly what it's doing. > Of course you have to put quotes around them to enter them in > your source code. > We don't expect this to work: > > print(Hello World!) > > > you have to use a string literal with quotes: > > print('Hello World!') > > > Same for all of the above. I didn't try printing them before, but I just did. Got: >>> print([Example](http://www.example.com) SyntaxError: invalid syntax (arrow pointing at the colon) > py> print('Example.') > Example. > > > That's the Python interactive interpreter (with a custom > prompt, I prefer "py>" > rather than the default ">>>"). Or I can do this from the > operating system's > shell prompt: > > steve at runes:~$ python -c "print 'http://www.example.com'" http://www.example.com If I do this in GNOME Terminal 2.30.2, the URL http... is a clickable link. But that's specific to the terminal. Other terminals may or may not recognise it. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson I need to get a new power supply in my Linux-capable machine, but for now I'm stuck with Winblows on a very old PC. As I've mentioned in other posts on this thread, I'm now thinking that I need to write a class to do this, and find out how Firefox and url aware terminals in Linux do it. There must be a way. From wrw at mac.com Wed Jan 4 00:01:30 2017 From: wrw at mac.com (William Ray Wing) Date: Wed, 04 Jan 2017 17:01:30 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do Message-ID: <4292265760@f38.n261.z1.binkp.net> > On Jan 4, 2017, at 3:44 PM, Dietmar Schwertberger wrote: > > On 04.01.2017 15:41, William Ray Wing wrote: >> I use Wing, and I think you will like it. It *is* pythonic, and for what it is worth, offers remote debugging as one of its more recently added features. > Obviously, you had no other choice than using Wing ;-) I should have said something. First, and to the best of my knowledge, I have no relationship with the Wing developers other than being a satisfied customer. Second, seven years ago, when I was reading IDE reviews and testing the more highly rated products, Wing just bubbled up to the top of the sieve I was using (features, ease of use, and the way it fit my idea of ? ?natural? ?, pretty much everyone's standard list). > > The remote debugging has been around for some years. I have been using it quite often to debug on my Raspberry Pi, Nokia N900 and Jolla Phone, all running some Linux system. It works well. It is or was a bit complicated to set up. I think this has been improved with Wing 6, but I did not need it in the last weeks, so I don't know. They claim it has been, but like you, I haven? ?t had need to test it on the new release. Thanks, Bill > > Regards, > > Dietmar > -- > https://mail.python.org/mailman/listinfo/python-list From robertvstepp at gmail.com Wed Jan 4 00:18:16 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 3 Jan 2017 23:18:16 -0600 Subject: Clickable hyperlinks In-Reply-To: <00b301d26645$8553e490$27b23dae@sambora> References: <586c7434$0$1617$c3e8da3$5496439d@news.astraweb.com> <00b301d26645$8553e490$27b23dae@sambora> Message-ID: On Tue, Jan 3, 2017 at 10:46 PM, Deborah Swanson wrote: > > > I didn't try printing them before, but I just did. Got: > > >>> print([Example](http://www.example.com) > > SyntaxError: invalid syntax (arrow pointing at the colon) As Steve had said, you need to put everything inside quotes. Also, you are missing a matching paren. Thus: py3: print("[Example](http://www.example.com)") [Example](http://www.example.com) As to whether anything will be "clickable" or not, what has already been said about different types of terminals applies. -- boB From steve+comp.lang.python at pearwood.info Wed Jan 4 00:40:00 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 04 Jan 2017 16:40 +1100 Subject: Clickable hyperlinks References: <586c7434$0$1617$c3e8da3$5496439d@news.astraweb.com> <00b301d26645$8553e490$27b23dae@sambora> Message-ID: <586c8ab3$0$1506$c3e8da3$5496439d@news.astraweb.com> On Wednesday 04 January 2017 15:46, Deborah Swanson wrote: > Steven D'Aprano wrote, on January 03, 2017 8:04 PM [...] >> Of course you have to put quotes around them to enter them in >> your source code. >> We don't expect this to work: >> >> print(Hello World!) >> >> >> you have to use a string literal with quotes: >> >> print('Hello World!') >> >> >> Same for all of the above. > I didn't try printing them before, but I just did. Got: > >>>> print([Example](http://www.example.com) > > SyntaxError: invalid syntax (arrow pointing at the colon) You missed the part where I said you have to put them in quotes. Like any other string in Python, you have to use quotation marks around it for Python to understand it as a string. None of these things will work: print( Hello World! ) print( What do you want to do today? ) print( 3 2 1 blast off ) print( http://www.example.com ) This isn't specific to print. This won't work either: message = Hello World! In *all* of these cases, you have to tell Python you're dealing with a string, and you do that with quotation marks: message = "Hello World!" print( 'What do you want to do today?' ) count_down = '3 2 1 blast off' url = 'http://www.example.com' -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From torriem at gmail.com Wed Jan 4 00:47:38 2017 From: torriem at gmail.com (Michael Torrie) Date: Wed, 04 Jan 2017 17:47:38 +1200 Subject: Clickable hyperlinks Message-ID: <2867895765@f38.n261.z1.binkp.net> On 01/04/2017 03:58 PM, Deborah Swanson wrote: > Thank you, thank you! Finally, at least one person on this list knows > about something (anything) in the python world that is internet aware. > It's also occurred to me that Beautifulsoup downloads data from a url, > so that code must have access to some kind of an internet engine too. Except that you never mentioned anything about this in your posts before. It seemed to me you were asking about printing out clickable hyperlinks with python. Calling the OS to launch a browser to view a url is a different beast which is why no one mentioned it before. If you don't tell us what you're actually trying to do (your end goal), things are more frustrating for everyone. If you had said early on you just want to be able to send the user to a particular url in a web browser, what Chris suggested would have been said a long time ago, rather than focusing on console output which is what you were asking about and focusing attention on. Just so you know, BeautifulSoup does not do any internet access itself; it's only an HTML parser. You have to fetch web pages using something like python's urllib and then feed the data to BeautifulSoup. urllib is not a web browser though. It can pretend to be a browser as far as the server is concerned but it knows nothing about javascript or rendering. It just retrieves bytes which you can then feed to BeautifulSoup or some other parser. > Yes, I'd gotten as far as figuring out that you don't need a clickable > link. Code that opens a url in a browse would do the job just fine. Good! I just wish you would have mentioned this much earlier as your end goal. > Or the webbrowser.open("http://......./") in a Linux terminal you suggest. > (I just have to get my Linux machine up and running again to try it.) The webbrowser module does not require console or terminal output. It will work on Windows or Linux if I'm not mistaken. It asks the OS to launch the default browser and load the indicated url. > All in all, given that clickable urls in a console is a non-starter, > this hits the nail on the head. Many thanks again! From rustompmody at gmail.com Wed Jan 4 00:57:08 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Tue, 3 Jan 2017 21:57:08 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> Message-ID: <0cb04e45-c91b-46ea-8f42-eec26fab7ca3@googlegroups.com> On Wednesday, January 4, 2017 at 5:42:34 AM UTC+5:30, Dietmar Schwertberger wrote: > On 02.01.2017 12:38, Antonio Caminero Garcia wrote: > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. > You did not try Wing IDE? It looks less like a spacecraft. Maybe you > like it. > Maybe the difference is that Wing is from Python people while the ones > you listed are from Java people. > For something completely different (microcontroller programming in C) I > just switched to a Eclipse derived IDE and I don't like it too much as > the tool does not focus on the problem scope. > > From your posts I'm not sure whether you want an editor or an IDE, > where for me the main difference is the debugger and code completion. > I would not want to miss the IDE features any more, even though in my > first 15 years of Python I thought that a debugger is optional with > Python ... > > Regards, > > Dietmar Im surprised no one's talked of idle (or Ive missed it?) From greg.ewing at canterbury.ac.nz Wed Jan 4 01:00:10 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 04 Jan 2017 19:00:10 +1300 Subject: Simulating int arithmetic with wrap-around In-Reply-To: <8737gz4tlp.fsf@nightsong.com> References: <58667385$0$1609$c3e8da3$5496439d@news.astraweb.com> <8737gz4tlp.fsf@nightsong.com> Message-ID: Paul Rubin wrote: > My first thought is towards the struct module, especially if you want to > handle a bunch of such integers at the same time. Or maybe the array > module or some combination. Or possibly numpy. -- Greg From dan at tombstonezero.net Wed Jan 4 01:07:05 2017 From: dan at tombstonezero.net (Dan Sommers) Date: Wed, 4 Jan 2017 06:07:05 -0000 (UTC) Subject: Clickable hyperlinks References: <586c7434$0$1617$c3e8da3$5496439d@news.astraweb.com> <00b301d26645$8553e490$27b23dae@sambora> <586c8ab3$0$1506$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, 04 Jan 2017 16:40:00 +1100, Steven D'Aprano wrote: > On Wednesday 04 January 2017 15:46, Deborah Swanson wrote: > >> Steven D'Aprano wrote, on January 03, 2017 8:04 PM > [...] >>> Of course you have to put quotes around them to enter them in >>> your source code. >>> We don't expect this to work: >>> >>> print(Hello World!) >>> >>> you have to use a string literal with quotes: >>> >>> print('Hello World!') >>> >>> Same for all of the above. > >> I didn't try printing them before, but I just did. Got: >> >>>>> print([Example](http://www.example.com) >> >> SyntaxError: invalid syntax (arrow pointing at the colon) > > You missed the part where I said you have to put them in quotes. ObPython: "When I've got these antlers on I am dictating and when I take them off I am not dictating." :-) From tjreedy at udel.edu Wed Jan 4 01:21:54 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 04 Jan 2017 18:21:54 +1200 Subject: Hey, I'm new to python so don't judge. Message-ID: <2025125030@f38.n261.z1.binkp.net> On 1/3/2017 10:15 PM, Dennis Lee Bieber wrote: > And that statement tells us you are trying to run from within some > IDE/editor which is trapping Python exceptions and producing a dialog > box for them. IDLE does this when one runs code from the editor, because it cannot/should not inject error messages into the editor buffer... AND it replaces the ^ with red highlighting of the code pointed to. No information is lost. Apparently, some beginners do not see the connection between the SyntaxError box and the red highlighting. I think I should add something to the box. Maybe 'The error was detected at the point of the red highlighting.' > Instead, save your script (if you haven't yet) as a file > (whatever.py). > > Open a command line interpreter/shell. > > Navigate (cd ...) to where you saved the file > > Type "python whatever.py" What a nuisance. > Copy and paste the results of the CLI/Shell window. Or one can hit F5 to run the code or Alt-X to just check the syntax. A beginner should do this every few lines, and it should be as easy as possible to check. If one needs to ask about a syntax error, one can copy the code up and including the highlighted part. Example: "When I run this code in IDLE def is_same(target, number: if I get a SyntaxError at 'if'." If the OP had known to do this, the error might have been seen without posting. -- Terry Jan Reedy From orgnut at yahoo.com Wed Jan 4 01:27:11 2017 From: orgnut at yahoo.com (Larry Hudson) Date: Tue, 3 Jan 2017 22:27:11 -0800 Subject: Hey, I'm new to python so don't judge. In-Reply-To: <40adacf3-b4de-4243-8cb5-6ea5fb74cf18@googlegroups.com> References: <40adacf3-b4de-4243-8cb5-6ea5fb74cf18@googlegroups.com> Message-ID: On 01/03/2017 04:27 PM, Callum Robinson wrote: > On Wednesday, January 4, 2017 at 1:17:11 PM UTC+13, Chris Angelico wrote: >> On Wed, Jan 4, 2017 at 11:03 AM, Erik wrote: >>> I doubt it's getting that far (I can see at least one syntax error in the >>> code pasted). >> >> True true. In any case, the point is to copy and paste the error >> message. Callum, please, copy and paste it. >> >> ChrisA > > I'm sorry if I'm doing something wrong but all that is happening is when i try to run it a popup says Invalid syntax > Exactly HOW are you running this? If you are getting a popup, I suspect you are using an on-line version in a browser. To get the proper Python error messages (called Tracebacks) you MUST run the program in a terminal on your own computer. These tracebacks are VERY informative (once you get used to them). :-) And these tracebacks are what WE need to see to help you. You DO have Python installed, don't you? -- -=- Larry -=- From rosuav at gmail.com Wed Jan 4 01:27:32 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 18:27:32 +1200 Subject: Mutable global state and threads Message-ID: <2586819455@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 5:41 PM, Kev Dwyer wrote: > Hello List, > > I came across some threading code in Some Other place recently and wanted to > sanity-check my assumptions. > > The code (below) creates a number of threads; each thread takes the last > (index -1) value from a global list of integers, increments it by one and > appends the new value to the list. > > The originator of the code expected that when all the threads completed, the > list would be an ascending sequence of integers, for example if the original > list was [0] and two threads mutated it twice each, the final state would be > [0, 1, 2, 3, 4]. > > Here is a version of the code (slightly simplified and modified to allow > changing the number of threads and mutations). > > > import sys > import threading > > > class myThread(threading.Thread): > > def __init__(self, nmutations): > threading.Thread.__init__(self) > self.nmutations = nmutations > > def run(self): > mutate(self.nmutations) > # print (L) > return > > def mutate(nmutations): > n = nmutations > while n: > L.append(L[-1 ]+ 1) > n -= 1 > return > > > def main(nthreads=2, nmutations=2): > global L > L = [0] > threads = [myThread(nmutations) for i in range(nthreads)] You can drop the myThread class and instead instantiate threading.Thread directly: threads = [threading.Thread(target=mutate, args=(nmutations,)) for i in range(nthreads)] > Firstly, is it true that the statement > > L.append(L[-1 ]+ 1) > > is not atomic, that is the thread might evaluate L[-1] and then yield, > allowing another thread to mutate L, before incrementing and appending? That is indeed true. If the code were all run sequentially (setting nthreads to 1), the last element in the final list would be equal to nthreads*nmutations, and you can mess around with the numbers to find out exactly how far short it is. Python guarantees that certain primitive operations (such as the list append itself) won't be broken, but anything that involves application code can be split up. So you can be confident that you'll end up with a list of integers and not a segfault, but they might not even be consecutive. > Secondly, the original code printed the list at the end of a thread's run > method to examine the state of the list. I don't think this would work > quite as expected, because the thread might yield after mutating the list > but before printing, so the list could have been mutated before the print > was executed. Is there a way to display the state of the list before any > further mutations take place? At the end of one thread's run, there are other threads still running. So you're correct again; another thread could change the list. What you could possibly do mess with locking, but before I advise that, I'd have to see what you're trying to accomplish - toy examples are hard to mess with. Bear in mind that simple code like this can't actually benefit from threads, as Python (or at least, CPython) won't run two of them at once. > (Disclaimer: I understand that sanity, mutable global state and threads are > unlikely bedfellows and so promise never to try anything like this in > production code). Well, I lost my sanity shortly after becoming a programmer, so I've happily used mutable globals in threaded programs. :) You can use them quite happily as long as you know what you're doing. But if you find yourself tossing in heaps of locks to try to reclaim your sanity, you may want to consider asyncio instead. Instead of locking and unlocking to say "don't yield here", you explicitly say "yield here". The downside is that you have to have *everything* cope with that - and not everything can. (For a long time, it was easy to establish a socket connection asynchronously, but hard to look up "www.python.org" without potentially stalling.) They're two models that can both be used to solve a lot of the same problems. By and large, your analysis is correct. Have fun threading! :) ChrisA From kevin.p.dwyer at gmail.com Wed Jan 4 01:41:40 2017 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Wed, 04 Jan 2017 06:41:40 +0000 Subject: Mutable global state and threads Message-ID: Hello List, I came across some threading code in Some Other place recently and wanted to sanity-check my assumptions. The code (below) creates a number of threads; each thread takes the last (index -1) value from a global list of integers, increments it by one and appends the new value to the list. The originator of the code expected that when all the threads completed, the list would be an ascending sequence of integers, for example if the original list was [0] and two threads mutated it twice each, the final state would be [0, 1, 2, 3, 4]. Here is a version of the code (slightly simplified and modified to allow changing the number of threads and mutations). import sys import threading class myThread(threading.Thread): def __init__(self, nmutations): threading.Thread.__init__(self) self.nmutations = nmutations def run(self): mutate(self.nmutations) # print (L) return def mutate(nmutations): n = nmutations while n: L.append(L[-1 ]+ 1) n -= 1 return def main(nthreads=2, nmutations=2): global L L = [0] threads = [myThread(nmutations) for i in range(nthreads)] for t in threads: t.start() for t in threads: t.join() print(L) assert L == list(range((nthreads * nmutations) + 1)) if __name__ == '__main__': nthreads, nmutations = int(sys.argv[1]), int(sys.argv[2]) main(nthreads, nmutations) Firstly, is it true that the statement L.append(L[-1 ]+ 1) is not atomic, that is the thread might evaluate L[-1] and then yield, allowing another thread to mutate L, before incrementing and appending? Secondly, the original code printed the list at the end of a thread's run method to examine the state of the list. I don't think this would work quite as expected, because the thread might yield after mutating the list but before printing, so the list could have been mutated before the print was executed. Is there a way to display the state of the list before any further mutations take place? (Disclaimer: I understand that sanity, mutable global state and threads are unlikely bedfellows and so promise never to try anything like this in production code). Cheers, Kev From gilmeh.serdah at nothing.here.invalid Wed Jan 4 01:52:02 2017 From: gilmeh.serdah at nothing.here.invalid (Gilmeh Serda) Date: Wed, 04 Jan 2017 18:52:02 +1200 Subject: Clickable hyperlinks References: <1391343616@f38.n261.z1.binkp.net> Message-ID: <612325293@f38.n261.z1.binkp.net> On Tue, 03 Jan 2017 11:46:16 -0800, Deborah Swanson wrote: > Does python have an equivalent function? Probably the most common use > for it would be output to the console, similar to a print statement, but > clickable. Write it as HTML code save to temp file and call the browser which loads the file. -- Gilmeh From tonycamgar at gmail.com Wed Jan 4 01:54:11 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Tue, 3 Jan 2017 22:54:11 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> Message-ID: <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> On Tuesday, January 3, 2017 at 4:12:34 PM UTC-8, Dietmar Schwertberger wrote: > On 02.01.2017 12:38, Antonio Caminero Garcia wrote: > You did not try Wing IDE? It looks less like a spacecraft. Maybe you > like it. > Maybe the difference is that Wing is from Python people while the ones > you listed are from Java people. That sounds interesting. By the look of it I think I am going to give it a try. > For something completely different (microcontroller programming in C) I > just switched to a Eclipse derived IDE and I don't like it too much as > the tool does not focus on the problem scope. If it happens to be Arduino I normally use a sublime plugin called Stino https://github.com/Robot-Will/Stino (1337 people starred that cool number :D) > From your posts I'm not sure whether you want an editor or an IDE, > where for me the main difference is the debugger and code completion. I want editor with those IDE capabilities and git integration, with optionally cool stuff as for example remote debugging. > I would not want to miss the IDE features any more, even though in my > first 15 years of Python I thought that a debugger is optional with > Python ... Unfortunately most of the time I am still using print and input functions. I know that sucks, I did not use the pdb module, I guess that IDE debuggers leverage such module. > Regards, > > Dietmar Thank you so much for your answer. From tjreedy at udel.edu Wed Jan 4 01:57:52 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 04 Jan 2017 18:57:52 +1200 Subject: Clickable hyperlinks Message-ID: <3081631347@f38.n261.z1.binkp.net> On 1/4/2017 4:32 AM, Deborah Swanson wrote: > My original question was whether python had anything to provide this > functionality, and the answer appears to be a resounding NO!!! I would say 'Yes, but with user effort'. To have a string interpreted as a clickable link, you send the string to software capable of creating a clickable link, plus the information 'this is a clickable link'*. There are two ways to tag a string as a link. One is to use markup around the url in the string itself. '' and html are example. Python provides multiple to make this easy. The other is to tag the string with a separate argument. Python provides tkinter, which wraps tk Text widgets, which have a powerful tag system. One can define a Link tag that will a) cause text to be displayed, for instance, blue and underlined and b) cause clicks on the text to generate a web request. One could then use mytext.insert('insert', 'http://www.example.com', Link) Browser must do something similar when they encounter when they encounter html link tags. * If the software directly recognizes a bare url such as 'http://www.example.com' as a link, without further indication, then it should have a way to disable conversion to a clickable link. -- Terry Jan Reedy From greg.ewing at canterbury.ac.nz Wed Jan 4 02:00:10 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 04 Jan 2017 19:00:10 +1200 Subject: Simulating int arithmetic with wrap-around References: <3917850917@f38.n261.z1.binkp.net> Message-ID: <1195249011@f38.n261.z1.binkp.net> Paul Rubin wrote: > My first thought is towards the struct module, especially if you want to > handle a bunch of such integers at the same time. Or maybe the array > module or some combination. Or possibly numpy. -- Greg From rustompmody at gmail.com Wed Jan 4 02:22:28 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Wed, 04 Jan 2017 19:22:28 +1200 Subject: Clickable hyperlinks References: <2359161402@f38.n261.z1.binkp.net> Message-ID: <2761968576@f38.n261.z1.binkp.net> This thread does lead to the question: Is the Url type in python less first-class than it could be? In scheme I could point to something like this https://docs.racket-lang.org/net/url.html Is there something equivalent in python? From rosuav at gmail.com Wed Jan 4 02:27:32 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 18:27:32 +1100 Subject: Mutable global state and threads In-Reply-To: References: Message-ID: On Wed, Jan 4, 2017 at 5:41 PM, Kev Dwyer wrote: > Hello List, > > I came across some threading code in Some Other place recently and wanted to > sanity-check my assumptions. > > The code (below) creates a number of threads; each thread takes the last > (index -1) value from a global list of integers, increments it by one and > appends the new value to the list. > > The originator of the code expected that when all the threads completed, the > list would be an ascending sequence of integers, for example if the original > list was [0] and two threads mutated it twice each, the final state would be > [0, 1, 2, 3, 4]. > > Here is a version of the code (slightly simplified and modified to allow > changing the number of threads and mutations). > > > import sys > import threading > > > class myThread(threading.Thread): > > def __init__(self, nmutations): > threading.Thread.__init__(self) > self.nmutations = nmutations > > def run(self): > mutate(self.nmutations) > # print (L) > return > > def mutate(nmutations): > n = nmutations > while n: > L.append(L[-1 ]+ 1) > n -= 1 > return > > > def main(nthreads=2, nmutations=2): > global L > L = [0] > threads = [myThread(nmutations) for i in range(nthreads)] You can drop the myThread class and instead instantiate threading.Thread directly: threads = [threading.Thread(target=mutate, args=(nmutations,)) for i in range(nthreads)] > Firstly, is it true that the statement > > L.append(L[-1 ]+ 1) > > is not atomic, that is the thread might evaluate L[-1] and then yield, > allowing another thread to mutate L, before incrementing and appending? That is indeed true. If the code were all run sequentially (setting nthreads to 1), the last element in the final list would be equal to nthreads*nmutations, and you can mess around with the numbers to find out exactly how far short it is. Python guarantees that certain primitive operations (such as the list append itself) won't be broken, but anything that involves application code can be split up. So you can be confident that you'll end up with a list of integers and not a segfault, but they might not even be consecutive. > Secondly, the original code printed the list at the end of a thread's run > method to examine the state of the list. I don't think this would work > quite as expected, because the thread might yield after mutating the list > but before printing, so the list could have been mutated before the print > was executed. Is there a way to display the state of the list before any > further mutations take place? At the end of one thread's run, there are other threads still running. So you're correct again; another thread could change the list. What you could possibly do mess with locking, but before I advise that, I'd have to see what you're trying to accomplish - toy examples are hard to mess with. Bear in mind that simple code like this can't actually benefit from threads, as Python (or at least, CPython) won't run two of them at once. > (Disclaimer: I understand that sanity, mutable global state and threads are > unlikely bedfellows and so promise never to try anything like this in > production code). Well, I lost my sanity shortly after becoming a programmer, so I've happily used mutable globals in threaded programs. :) You can use them quite happily as long as you know what you're doing. But if you find yourself tossing in heaps of locks to try to reclaim your sanity, you may want to consider asyncio instead. Instead of locking and unlocking to say "don't yield here", you explicitly say "yield here". The downside is that you have to have *everything* cope with that - and not everything can. (For a long time, it was easy to establish a socket connection asynchronously, but hard to look up "www.python.org" without potentially stalling.) They're two models that can both be used to solve a lot of the same problems. By and large, your analysis is correct. Have fun threading! :) ChrisA From flebber.crue at gmail.com Wed Jan 4 02:32:22 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Wed, 04 Jan 2017 19:32:22 +1200 Subject: Is there a good process or library for validating changes to XML format Message-ID: <2750102362@f38.n261.z1.binkp.net> Afternoon Is there a good library or way I could use to check that the author of the XML doc I am using doesn't make small changes to structure over releases? Not fully over this with XML but thought that XSD may be what I need, if I search "python XSD" I get a main result for PyXB and generateDS (https://pythonhosted.org/generateDS/). Both seem to be libraries for generating bindings to structures for parsing so maybe I am searching the wrong thing. What is the right thing to search? Cheers Sayth From paul.nospam at rudin.co.uk Wed Jan 4 03:04:45 2017 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 04 Jan 2017 08:04:45 +0000 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <20170103191311.GB18910@mail.akwebsoft.com> Message-ID: <87wpeb45eq.fsf@rudin.co.uk> Tim Johnson writes: > * Antonio Caminero Garcia [170102 20:56]: >> Guys really thank you for your answers. Basically now I am more >> emphasizing in learning in depth a tool and get stick to it so I >> can get a fast workflow. Eventually I will learn Vim and its >> python developing setup, I know people who have been programming >> using Vim for almost 20 years and they did not need to change >> editor (that is really awesome). > > Bye the way, one thing I like about the GUI based vim is that it > supports tabs, where emacs does not. M-x package-install tabbar From python at deborahswanson.net Wed Jan 4 03:19:32 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 04 Jan 2017 20:19:32 +1200 Subject: Clickable hyperlinks Message-ID: <2915184730@f38.n261.z1.binkp.net> Chris Angelico wrote, on January 04, 2017 4:16 AM > > Yeah, there's no simple answer; however, you'll find that > Python on many platforms is entirely capable of popping a URL > up in the user's default browser. Check this out: > > >>> import antigravity I downloaded the code from the Package Index, but there really wasn't much in it. This is the entire .py file: STRIP_URL = "http://xkcd.com/353/" def start(): return STRIP_URL And setup.py is equally disappointing: from distutils.core import setup setup( name='antigravity', version='0.1', description='A really simple module that allow everyone to do "import antigravity"', author='Fabien Schwob', author_email='antigravity at x-phuture.com', url='http://fabien.schwob.org/antigravity/', packages=['antigravity'], ) > This uses the 'webbrowser' module, which knows about a number > of different ways to open a browser, and will attempt them > all. So if you can figure out the UI part of things, actually > making the link pop up in a browser isn't too hard; for > instance, if you're doing OAuth at the command line and need > the user to go and authenticate, you can simply > webbrowser.open("http://......./") and it'll DTRT. > > ChrisA All the action of antigravity must be done by the import statement. When import opens a module that immediately returns a url, it must have a mechanism to open it in a browser. It would be very easy to do the same thing with my own .py and import it into another .py. Or, take a look at import's code and figure out how it opens a url in a browser. I imagine it's the 'webbrowser' module you mention. If it tries several methods, just pick one that will work for you. Or, take a look at this Index of Packages Matching 'webbrowser' (~50 packages) https://pypi.python.org/pypi?%3Aaction=search&term=webbrowser&submit=sea rch D'Arcy was right, there's lots in python that's internet aware, though that wasn't the question I knew to ask. From paul.nospam at rudin.co.uk Wed Jan 4 03:22:03 2017 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 04 Jan 2017 08:22:03 +0000 Subject: Clickable hyperlinks References: <586c7434$0$1617$c3e8da3$5496439d@news.astraweb.com> <00b301d26645$8553e490$27b23dae@sambora> Message-ID: <87mvf744lw.fsf@rudin.co.uk> "Deborah Swanson" writes: > > I didn't try printing them before, but I just did. Got: > >>>> print([Example](http://www.example.com) > > SyntaxError: invalid syntax (arrow pointing at the colon) > With respect, if you typed that at python then it's probably a good idea to take a step back and work through the excellent tutorial. https://docs.python.org/3/tutorial/ From wlfraed at ix.netcom.com Wed Jan 4 03:41:06 2017 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Wed, 04 Jan 2017 20:41:06 +1200 Subject: Clickable hyperlinks Message-ID: <3269602639@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 14:58:42 -0800, "Deborah Swanson" declaimed the following: >Thank you, thank you! Finally, at least one person on this list knows >about something (anything) in the python world that is internet aware. >It's also occurred to me that Beautifulsoup downloads data from a url, >so that code must have access to some kind of an internet engine too. > Uhm... There is a big difference between "clickable links" and "internet aware". Look at the Library reference manual. There is a section on "Internet Data Handling", and another on "Internet Protocols and Support". For just retrieving things identified by a URL, there are both urllib and urllib2; and for more specific there is httplib, ftplib, poplib, etc. Clickable links is a user interface matter -- and as mentioned numerous times, depends upon the features of the interface, not of Python (unless you are writing a full-fledged GUI application in which case you have to tag entities as clickable and handle the user clicking on that entity, followed by actually using one of the above library routines to fetch the contents at the clickable's target) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ From steve+python at pearwood.info Wed Jan 4 03:41:52 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 20:41:52 +1200 Subject: Screwing Up looping in Generator References: <1791039168@f38.n261.z1.binkp.net> Message-ID: <1214507658@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 01:09 pm, Sayth Renshaw wrote: > Untested as i wrote this in notepad at work but, if i first use the > generator to create a set of filenames and then iterate it then call the > generator anew to process file may work? It "may" work. Or it "may not" work. It is hard to tell because we don't have enough context to understand your code. Let me see if I can guess what you are doing: > Good idea or better available? > > def get_list_of_names(generator_arg): > name_set = set() > for name in generator_arg: > base = os.path.basename(name.name) > filename = os.path.splitext(base)[0] > name_set.add(filename) > return name_set What does "generator_arg" do? Since you iterate over it, it could be a list, a tuple, any other sequence, or an iterator. Why does it have to be a generator? What is "name.name"? I *think* that your intention is to take a list of objects that hold filenames: ["C://My Documents/data.txt", "C://My Documents/folder/image.jpg", "C://file.txt", "D://installer.exe", "E://folder/image.gif"] strip off the path, strip off the file extensions, remove any duplicates, giving: set(["data", "image", "file", "installer"]) (Notice that image.jpg and image.gif count as duplicates.) If that is what you want, then I think get_list_of_names will work, except: - the name is WRONG: it returns a set, not a list; - the name does not describe what the function does; - there's no documentation - the name of the argument is misleading, it doesn't have to be a generator. Other than that, I think the code does what I think you want. > for file_name in name_set: > directory = "output" > with open(os.path.join(directory, filename, 'w', newline='') as > csvf: > for file in rootobs: > # create and write csv Your indentation is wrong. What's "rootobs"? The code you show here does not have enough detail for me to even try to guess what it does. -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From python at deborahswanson.net Wed Jan 4 04:00:55 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 01:00:55 -0800 Subject: Clickable hyperlinks In-Reply-To: <03e6b340-fa99-cfe7-8c5d-ba2dcd0b7aa0@gmail.com> Message-ID: <000001d26669$0fc31c90$27b23dae@sambora> Michael Torrie wrote, on January 03, 2017 8:05 PM > > On 01/03/2017 08:46 PM, Deborah Swanson wrote: > > Actually it is, or at least it doesn't happen in all email readers. > > Mine, for instance, never breaks up threads. > > Mine doesn't either, which illustrates the issue. This > message, for example appears under a long thread that started > out life as "mentor training python Romania with > certification" and then changed to "Cleaning up conditionals" > and then changed to "Clickable hyperlinks." All in one > thread. My client doesn't break them up because they all tie > together via the message-id header. > > And most of us would not like our client to break a thread > just because the subject changes. Often in long conversations > there are twists and turns in the discussion and sometimes > side-paths are explored, and the subject often is changed to > reflect this. With a truly threaded email reader (one that > shows a tree of messages, not just chronological order), this > works out very well. So if a discussion has a natural > evolution into various other topics, it is often considered > okay to just change the subject but continue the thread. > Other times, it's better to start a new thread. Where that > line is is hard to say! > > > I did say in the message you're replying to that I will try to > > remember to start new threads with brand new messages. > (Even though I > > think pipermail's behavior is a bug, that's what many > people read the > > list > > from.) > > Sounds good. > > I don't know of anyone that reads on the pipermail archive, > except in response to web searches. Most people use clients > of some kind, NNTP or email. And those that group messages > according to message-id (most clients except for ones that > try to be smart like Gmail web or Outlook) will show all the > topics I mentioned before as one giant thread, which is by > design (that's what message-id headers are for). I suppose. Times change of course, which always suits some and not others. Personally, I think putting messages that have different titles all in one thread is a bad design, but as I've said a couple of times now I intend to comply with the new rules. But compliance doesn't imply agreement. I prefer the old system, which ordered threads by titles, but there's obviously no pleasing everyone on this issue. From steve+python at pearwood.info Wed Jan 4 04:08:52 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 21:08:52 +1200 Subject: Clickable hyperlinks References: <759429036@f38.n261.z1.binkp.net> Message-ID: <3354116128@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 08:00 pm, Deborah Swanson wrote: [speaking of threading emails] > I suppose. Times change of course, which always suits some and not > others. Personally, I think putting messages that have different titles > all in one thread is a bad design, but as I've said a couple of times > now I intend to comply with the new rules. But compliance doesn't imply > agreement. I prefer the old system, which ordered threads by titles, but > there's obviously no pleasing everyone on this issue. Indeed :-) However, as far as I am aware, the use of threading by following the In-Reply-To and References header lines goes back to at least 1982, which makes them pretty old, and certainly pre-dates Gmail and Outlook by many years. They're also official standards which ALL email programs are supposed to follow, so if Gmail and Outlook fail to follow the standard, well, I wouldn't be surprised. I don't absolutely know for a fact that threading in this way is older than threading by subject line, but I'm fairly confident that what you are calling the "new rules" are actually the much older rules. Are there any old-timers here who were using email prior to 1982 that would care to comment? Here's a discussion by Jamie Zawinski, who wrote Netscape Navigator, before it became Mozila and Firefox, so he knows what he's talking about: https://www.jwz.org/doc/threading.html The concept here is not so much that people start a new topic and change the subject, but that sometimes the topic just naturally evolves to the point that a change in subject is sensible. Take this thread for example: the topic has drifted from "Clickable hyperlinks" to talking about email threading. I should be able to change the subject without breaking the thread: Clickable hyperlinks ????? RE: Clickable hyperlinks ??? ????? Re: RE: Clickable hyperlinks ??? ????? RE: Clickable hyperlinks ??? ????? Threading [was Re: Clickable hyperlinks] ??? ????? Re: Threading ????? RE: Clickable hyperlinks ????? Re: Clickable hyperlinks Adding a "Re:" or "RE" to the subject doesn't change the thread, and neither does changing the subject line in a more substantial way. Of course, I can always *choose* to sort by subject line, or date. Personally I hardly ever sort by thread, so it is no skin off my nose what you do. But when you hit "Reply" to a message, you inherit the "Reference" and "In-Reply-To" headers from the previous message, so at least some people will see it threaded in an existing thread rather than starting a brand new one. -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Wed Jan 4 04:20:28 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 21:20:28 +1200 Subject: Clickable hyperlinks References: <973176847@f38.n261.z1.binkp.net> Message-ID: <3543245575@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 03:46 pm, Deborah Swanson wrote: > As I've mentioned in other posts on this thread, I'm now thinking that I > need to write a class to do this, and find out how Firefox and url aware > terminals in Linux do it. There must be a way. A GUI application can interpret text any way it chooses. Firefox takes a HTML file and renders it, using whatever GUI library it chooses. That GUI library understands that text like: Hello World! should be shown in bold face, and text like: Example should be shown as the word "Example" underlined and in some colour, and when you click on it the browser will navigate to the URL given. Firefox can do this because it controls the environment it runs in. Same for Excel, which also controls the environment it runs in. That's *not* the case for Python, which is at the mercy of whatever console or terminal application it is running in. However, you can use Python to write GUI applications. Then it becomes *your* responsibility to create the window, populate it with any buttons or text or scroll bars you want, and you can choose to interpret text any way you like -- including as clickable Hyperlinks. The bottom line is, there is no "a way" to do this. There are a thousand, ten thousand, ways to do it. Every web browser, every terminal, every URL-aware application, can choose its own way to do it. There's no one single way that works everywhere, but if you are working in a console or terminal, just printing the URL is likely to be interpreted by the console as a clickable link: print("http://www.example.com") -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From python at deborahswanson.net Wed Jan 4 04:32:12 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 01:32:12 -0800 Subject: Clickable hyperlinks In-Reply-To: <586c8ab3$0$1506$c3e8da3$5496439d@news.astraweb.com> Message-ID: <000601d2666d$6e7eae30$27b23dae@sambora> Steven D'Aprano wrote, on January 03, 2017 9:40 PM > > On Wednesday 04 January 2017 15:46, Deborah Swanson wrote: > > > Steven D'Aprano wrote, on January 03, 2017 8:04 PM > [...] > >> Of course you have to put quotes around them to enter them in your > >> source code. We don't expect this to work: > >> > >> print(Hello World!) > >> > >> > >> you have to use a string literal with quotes: > >> > >> print('Hello World!') > >> > >> > >> Same for all of the above. > > > I didn't try printing them before, but I just did. Got: > > > >>>> print([Example](http://www.example.com) > > > > SyntaxError: invalid syntax (arrow pointing at the colon) > > You missed the part where I said you have to put them in quotes. > > Like any other string in Python, you have to use quotation > marks around it for > Python to understand it as a string. None of these things will work: > > print( Hello World! ) > > print( What do you want to do today? ) > > print( 3 2 1 blast off ) > > print( http://www.example.com ) > > > This isn't specific to print. This won't work either: > > message = Hello World! > > In *all* of these cases, you have to tell Python you're > dealing with a string, > and you do that with quotation marks: > > message = "Hello World!" > print( 'What do you want to do today?' ) > count_down = '3 2 1 blast off' > url = 'http://www.example.com' > > > > > -- > Steven Thanks, Steven. Yes, of course if you want to print strings you must enclose them in quotes. I think you learn that in Week 1 of any introductory course on Python. But we aren't trying to print strings here, the point is to produce clickable links. I didn't enclose them with quotes because I didn't see any point in printing plain text when I wanted clickable links. I actually didn't understand why you thought I should print them, but it never would have occurred to me that you wanted me to print out a bunch of silly plain text strings, apparently just for the heck of it. At this point, if I pursue this any farther, it will be to look into how Firefox takes webpage titles and urls out of its sqlite database and makes objects you can click on to open the webpages. That's the basic technology I'd need to find a way to talk (write) python into doing. If it's even worth it. On a practical level it's not worth it, way too much work for the teensy advantage of having it to use. It might be some giggles to figure out how to do it and maybe I will sometime just for funsies. My original question was whether python had anything to provide this functionality, and the answer appears to be a resounding NO!!! Answer received, and thanks to all who contributed. From steve+python at pearwood.info Wed Jan 4 04:39:14 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 21:39:14 +1200 Subject: Clickable hyperlinks References: <4225562807@f38.n261.z1.binkp.net> Message-ID: <2175282048@f38.n261.z1.binkp.net> On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote: > Thanks, Steven. Yes, of course if you want to print strings you must > enclose them in quotes. I think you learn that in Week 1 of any > introductory course on Python. > > But we aren't trying to print strings here, the point is to produce > clickable links. I didn't enclose them with quotes because I didn't see > any point in printing plain text when I wanted clickable links. I > actually didn't understand why you thought I should print them, but it > never would have occurred to me that you wanted me to print out a bunch > of silly plain text strings, apparently just for the heck of it. What we have here is a failure to communicate :-) I apologise for ruffling your feathers, but its difficult to judge another person's level of knowledge. In someways you're obviously quite knowledgeable about Python, but in other ways you're still learning (as we all are!) and I'm afraid I may have misjudged exactly what your level of knowledge was. Sorry about that. I'm not suggesting that you print "silly plain text strings" just for the heck of it. You've asked how to get a clickable link using Python. There is only one way I know of to get a clickable link using Python: Write out a link as plain text to another application which then interprets the plain text as a clickable link. You *might* be able to get clickable links in Python by writing an entire GUI application, using (say) the tkinter library, or one of the third-party GUI libraries like wxPython, kivy, pyqt, or others, but I haven't a clue how. But even there, your links will start off as text, which means you will still need to surround them with quotes to make them strings. Aside: you've actually raised a fascinating question. I wonder whether there are any programming languages that understand URLs as native data types, so that *source code* starting with http:// etc is understood in the same way that source code starting with [ is seen as a list or { as a dict? But back to your problem: short of writing you own GUI application, in which case you can do *anything you like*, you can: - write out a HTML file containing the URLs you want, in tags, then open the HTML file in a web browser and let the web browser interpret the HTML tags as clickable links; - write out an Excel spreadsheet, using whatever format Excel expects, open the spreadsheet in Excel, and let Excel interpret the mystery format as a clickable link; - print the URL to the console, and see if the console is smart enough to interpret it as a clickable link. I'm sorry that I can't give you a one-sentence answer "just use such-and-such a function, that does exactly what you want in a platform-independent manner" :-( -- Steve ? ?Cheer up,? ? they said, ? ?things could be worse.? ? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Wed Jan 4 04:41:53 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 20:41:53 +1100 Subject: Screwing Up looping in Generator References: <009201d26627$9aee9390$27b23dae@sambora> <1e433232-7db7-4f1c-8637-50a3da469b20@googlegroups.com> <08c82541-86f7-4811-abeb-1205d2064506@googlegroups.com> Message-ID: <586cc362$0$1606$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 01:09 pm, Sayth Renshaw wrote: > Untested as i wrote this in notepad at work but, if i first use the > generator to create a set of filenames and then iterate it then call the > generator anew to process file may work? It "may" work. Or it "may not" work. It is hard to tell because we don't have enough context to understand your code. Let me see if I can guess what you are doing: > Good idea or better available? > > def get_list_of_names(generator_arg): > name_set = set() > for name in generator_arg: > base = os.path.basename(name.name) > filename = os.path.splitext(base)[0] > name_set.add(filename) > return name_set What does "generator_arg" do? Since you iterate over it, it could be a list, a tuple, any other sequence, or an iterator. Why does it have to be a generator? What is "name.name"? I *think* that your intention is to take a list of objects that hold filenames: ["C://My Documents/data.txt", "C://My Documents/folder/image.jpg", "C://file.txt", "D://installer.exe", "E://folder/image.gif"] strip off the path, strip off the file extensions, remove any duplicates, giving: set(["data", "image", "file", "installer"]) (Notice that image.jpg and image.gif count as duplicates.) If that is what you want, then I think get_list_of_names will work, except: - the name is WRONG: it returns a set, not a list; - the name does not describe what the function does; - there's no documentation - the name of the argument is misleading, it doesn't have to be a generator. Other than that, I think the code does what I think you want. > for file_name in name_set: > directory = "output" > with open(os.path.join(directory, filename, 'w', newline='') as > csvf: > for file in rootobs: > # create and write csv Your indentation is wrong. What's "rootobs"? The code you show here does not have enough detail for me to even try to guess what it does. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From maillist at schwertberger.de Wed Jan 4 04:44:44 2017 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Wed, 04 Jan 2017 21:44:44 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I Message-ID: <4239897872@f38.n261.z1.binkp.net> On 04.01.2017 15:41, William Ray Wing wrote: > I use Wing, and I think you will like it. It *is* pythonic, and for what it is worth, offers remote debugging as one of its more recently added features. Obviously, you had no other choice than using Wing ;-) The remote debugging has been around for some years. I have been using it quite often to debug on my Raspberry Pi, Nokia N900 and Jolla Phone, all running some Linux system. It works well. It is or was a bit complicated to set up. I think this has been improved with Wing 6, but I did not need it in the last weeks, so I don't know. Regards, Dietmar From darcy at vex.net Wed Jan 4 05:00:08 2017 From: darcy at vex.net (D'Arcy Cain) Date: Wed, 04 Jan 2017 22:00:08 +1200 Subject: Clickable hyperlinks Message-ID: <1485144662@f38.n261.z1.binkp.net> On 2017-01-04 05:58 PM, Deborah Swanson wrote: >> the user to go and authenticate, you can simply >> webbrowser.open("http://......./") and it'll DTRT. > > Thank you, thank you! Finally, at least one person on this list knows > about something (anything) in the python world that is internet aware. Lots of things in Python are Internet aware. That's not the question you asked though. > It's also occurred to me that Beautifulsoup downloads data from a url, > so that code must have access to some kind of an internet engine too. Nope. You have to feed it HTML. You either need to generate that or capture it from somewhere. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From steve+python at pearwood.info Wed Jan 4 05:08:52 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 21:08:52 +1100 Subject: Clickable hyperlinks References: <03e6b340-fa99-cfe7-8c5d-ba2dcd0b7aa0@gmail.com> <000001d26669$0fc31c90$27b23dae@sambora> Message-ID: <586cc9b5$0$1590$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 08:00 pm, Deborah Swanson wrote: [speaking of threading emails] > I suppose. Times change of course, which always suits some and not > others. Personally, I think putting messages that have different titles > all in one thread is a bad design, but as I've said a couple of times > now I intend to comply with the new rules. But compliance doesn't imply > agreement. I prefer the old system, which ordered threads by titles, but > there's obviously no pleasing everyone on this issue. Indeed :-) However, as far as I am aware, the use of threading by following the In-Reply-To and References header lines goes back to at least 1982, which makes them pretty old, and certainly pre-dates Gmail and Outlook by many years. They're also official standards which ALL email programs are supposed to follow, so if Gmail and Outlook fail to follow the standard, well, I wouldn't be surprised. I don't absolutely know for a fact that threading in this way is older than threading by subject line, but I'm fairly confident that what you are calling the "new rules" are actually the much older rules. Are there any old-timers here who were using email prior to 1982 that would care to comment? Here's a discussion by Jamie Zawinski, who wrote Netscape Navigator, before it became Mozila and Firefox, so he knows what he's talking about: https://www.jwz.org/doc/threading.html The concept here is not so much that people start a new topic and change the subject, but that sometimes the topic just naturally evolves to the point that a change in subject is sensible. Take this thread for example: the topic has drifted from "Clickable hyperlinks" to talking about email threading. I should be able to change the subject without breaking the thread: Clickable hyperlinks ?? RE: Clickable hyperlinks ? ?? Re: RE: Clickable hyperlinks ? ?? RE: Clickable hyperlinks ? ?? Threading [was Re: Clickable hyperlinks] ? ?? Re: Threading ?? RE: Clickable hyperlinks ?? Re: Clickable hyperlinks Adding a "Re:" or "RE" to the subject doesn't change the thread, and neither does changing the subject line in a more substantial way. Of course, I can always *choose* to sort by subject line, or date. Personally I hardly ever sort by thread, so it is no skin off my nose what you do. But when you hit "Reply" to a message, you inherit the "Reference" and "In-Reply-To" headers from the previous message, so at least some people will see it threaded in an existing thread rather than starting a brand new one. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From maillist at schwertberger.de Wed Jan 4 05:09:30 2017 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Wed, 04 Jan 2017 22:09:30 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I Message-ID: <2715841046@f38.n261.z1.binkp.net> On 04.01.2017 07:54, Antonio Caminero Garcia wrote: > Unfortunately most of the time I am still using print and input functions. I know that sucks, I did not use the pdb module, I guess that IDE debuggers leverage such module. pdb is actually quite useful. On my Windows PCs I can invoke python on any .py file with the -i command line switch by right clicking in the Explorer and selecting "Debug". Now when the script crashes, I can inspect variables without launching a full-scale IDE or starting the script from the command line. For such quick fixes I have also a context menu entry "Edit" for editing with Pythonwin, which is still quite OK as editor and has no licensing restrictions or installation requirements. This is a nice option when you deploy your installation to many PCs over the network. For the print functions vs. debugger: The most useful application for a debugger like Wing is not for bug-fixing, but to set a break point and then interactively develop on the debugger console and with the IDE editor's autocompletion using introspection on the live objects. This is very helpful for hardware interfacing, network protocols or GUI programs. It really boosted my productivity in a way I could not believe before. This is something most people forget when they evaluate programming languages. It's not the language or syntax that counts, but the overall environment. Probably the only other really interactive language and environment is Forth. > If it happens to be Arduino I normally use a sublime plugin called Stino > https://github.com/Robot-Will/Stino > (1337 people starred that cool number :D) Well, it is CodeWarrior which was quite famous at the time of the 68k Macs. The company was bought by Motorola and the IDE is still around for Freescale/NXP/Qualcomm microcontrollers like the HCS08 8 bit series. Around ten years ago the original CodeWarrior IDE was migrated to something Eclipse based. When I last evaluated HCS08 vs. Arduino, the HCS08 won due to the better debug interface and native USB support. HCS08 is still quite cool, but when it comes to documentation, learning curve, tools etc. the Arduinos win.... Regards, Dietmar From steve+python at pearwood.info Wed Jan 4 05:20:28 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 21:20:28 +1100 Subject: Clickable hyperlinks References: <586c7434$0$1617$c3e8da3$5496439d@news.astraweb.com> <00b301d26645$8553e490$27b23dae@sambora> Message-ID: <586ccc6d$0$1596$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 03:46 pm, Deborah Swanson wrote: > As I've mentioned in other posts on this thread, I'm now thinking that I > need to write a class to do this, and find out how Firefox and url aware > terminals in Linux do it. There must be a way. A GUI application can interpret text any way it chooses. Firefox takes a HTML file and renders it, using whatever GUI library it chooses. That GUI library understands that text like: Hello World! should be shown in bold face, and text like: Example should be shown as the word "Example" underlined and in some colour, and when you click on it the browser will navigate to the URL given. Firefox can do this because it controls the environment it runs in. Same for Excel, which also controls the environment it runs in. That's *not* the case for Python, which is at the mercy of whatever console or terminal application it is running in. However, you can use Python to write GUI applications. Then it becomes *your* responsibility to create the window, populate it with any buttons or text or scroll bars you want, and you can choose to interpret text any way you like -- including as clickable Hyperlinks. The bottom line is, there is no "a way" to do this. There are a thousand, ten thousand, ways to do it. Every web browser, every terminal, every URL-aware application, can choose its own way to do it. There's no one single way that works everywhere, but if you are working in a console or terminal, just printing the URL is likely to be interpreted by the console as a clickable link: print("http://www.example.com") -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From darcy at vex.net Wed Jan 4 05:24:34 2017 From: darcy at vex.net (D'Arcy Cain) Date: Wed, 04 Jan 2017 22:24:34 +1200 Subject: Clickable hyperlinks Message-ID: <572149747@f38.n261.z1.binkp.net> On 2017-01-04 07:07 PM, Deborah Swanson wrote: > D'Arcy Cain wrote, on Wednesday, January 04, 2017 5:03 AM >> In all the messages in this thread I still don't understand what this >> "teensy advantage" is supposed to be. Do you want to be able >> to do this: >> >> make_web_link(http://...) >> >> instead of: >> >> make_web_link("http://...") [...] > Is make_web_link("http://...") valid python code? That's exactly the It's just a made up name. My point was that the first was syntactically incorrect and the second, while not a real method, was at least parseable. I think I saw someone else mention this but it bears repeating. When you ask a question make sure you are presenting the problem and not your solution to a secret problem. Always tell us the actual problem you are trying to solve. You will get much better answers. Think of it this way. You drop a ring down a drain. You can ask two questions, "How do I remove a drain trap?" or "How do I recover a ring that I dropped down the drain?" If you ask the first question you will get lots of advice on tools and buckets, etc. People will assume that the drain is blocked. Ask the second question and someone might mention a magnet and a piece of string. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From steve+python at pearwood.info Wed Jan 4 05:39:15 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 04 Jan 2017 21:39:15 +1100 Subject: Clickable hyperlinks References: <586c8ab3$0$1506$c3e8da3$5496439d@news.astraweb.com> <000601d2666d$6e7eae30$27b23dae@sambora> Message-ID: <586cd0d5$0$1595$c3e8da3$5496439d@news.astraweb.com> On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote: > Thanks, Steven. Yes, of course if you want to print strings you must > enclose them in quotes. I think you learn that in Week 1 of any > introductory course on Python. > > But we aren't trying to print strings here, the point is to produce > clickable links. I didn't enclose them with quotes because I didn't see > any point in printing plain text when I wanted clickable links. I > actually didn't understand why you thought I should print them, but it > never would have occurred to me that you wanted me to print out a bunch > of silly plain text strings, apparently just for the heck of it. What we have here is a failure to communicate :-) I apologise for ruffling your feathers, but its difficult to judge another person's level of knowledge. In someways you're obviously quite knowledgeable about Python, but in other ways you're still learning (as we all are!) and I'm afraid I may have misjudged exactly what your level of knowledge was. Sorry about that. I'm not suggesting that you print "silly plain text strings" just for the heck of it. You've asked how to get a clickable link using Python. There is only one way I know of to get a clickable link using Python: Write out a link as plain text to another application which then interprets the plain text as a clickable link. You *might* be able to get clickable links in Python by writing an entire GUI application, using (say) the tkinter library, or one of the third-party GUI libraries like wxPython, kivy, pyqt, or others, but I haven't a clue how. But even there, your links will start off as text, which means you will still need to surround them with quotes to make them strings. Aside: you've actually raised a fascinating question. I wonder whether there are any programming languages that understand URLs as native data types, so that *source code* starting with http:// etc is understood in the same way that source code starting with [ is seen as a list or { as a dict? But back to your problem: short of writing you own GUI application, in which case you can do *anything you like*, you can: - write out a HTML file containing the URLs you want, in tags, then open the HTML file in a web browser and let the web browser interpret the HTML tags as clickable links; - write out an Excel spreadsheet, using whatever format Excel expects, open the spreadsheet in Excel, and let Excel interpret the mystery format as a clickable link; - print the URL to the console, and see if the console is smart enough to interpret it as a clickable link. I'm sorry that I can't give you a one-sentence answer "just use such-and-such a function, that does exactly what you want in a platform-independent manner" :-( -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From hpj at urpla.net Wed Jan 4 06:15:02 2017 From: hpj at urpla.net (Hans-Peter Jansen) Date: Wed, 04 Jan 2017 23:15:02 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I Message-ID: <1885822774@f38.n261.z1.binkp.net> On Montag, 2. Januar 2017 03:38:53 Antonio Caminero Garcia wrote: > Hello, I am having a hard time deciding what IDE or IDE-like code editor > should I use. This can be overwhelming. > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, > IntelliJ with Python plugin. Well, since nobody mentioned it, yet: eric is doing quite nice here. With on the fly error checking, jedi and qscintilla calltips and autocompletion, git integration (using a plugin), graphical debugger, it's grown to a capable IDE over the years. Given, it's a fully open source, PyQt based project, it also shows the powers of Python3 and PyQt. Pete From rosuav at gmail.com Wed Jan 4 06:15:46 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 04 Jan 2017 23:15:46 +1200 Subject: Clickable hyperlinks Message-ID: <3406130062@f38.n261.z1.binkp.net> On Wed, Jan 4, 2017 at 10:43 PM, Deborah Swanson wrote: > I'm quite well aware by now that there is no one-sentence answer to my > original question, if there's any coherent answer at all. Them's the > breaks. Live with it or live without it, it doesn't care. Yeah, there's no simple answer; however, you'll find that Python on many platforms is entirely capable of popping a URL up in the user's default browser. Check this out: >>> import antigravity This uses the 'webbrowser' module, which knows about a number of different ways to open a browser, and will attempt them all. So if you can figure out the UI part of things, actually making the link pop up in a browser isn't too hard; for instance, if you're doing OAuth at the command line and need the user to go and authenticate, you can simply webbrowser.open("http://......./") and it'll DTRT. ChrisA From python at deborahswanson.net Wed Jan 4 06:15:52 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 03:15:52 -0800 Subject: Clickable hyperlinks In-Reply-To: <586cc9b5$0$1590$c3e8da3$5496439d@news.astraweb.com> Message-ID: <001201d2667b$e97c1740$27b23dae@sambora> Steve D'Aprano wrote, on January 04, 2017 2:09 AM > > On Wed, 4 Jan 2017 08:00 pm, Deborah Swanson wrote: > > [speaking of threading emails] > > > I suppose. Times change of course, which always suits some and not > > others. Personally, I think putting messages that have different > > titles all in one thread is a bad design, but as I've said > a couple of > > times now I intend to comply with the new rules. But compliance > > doesn't imply agreement. I prefer the old system, which ordered > > threads by titles, but there's obviously no pleasing > everyone on this > > issue. > > Indeed :-) > > However, as far as I am aware, the use of threading by > following the In-Reply-To and References header lines goes > back to at least 1982, which makes them pretty old, and > certainly pre-dates Gmail and Outlook by many years. They're > also official standards which ALL email programs are supposed > to follow, so if Gmail and Outlook fail to follow the > standard, well, I wouldn't be surprised. I don't absolutely > know for a fact that threading in this way is older than > threading by subject line, but I'm fairly confident that what > you are calling the "new rules" are actually the much older rules. > > Are there any old-timers here who were using email prior to > 1982 that would care to comment? > > > Here's a discussion by Jamie Zawinski, who wrote Netscape > Navigator, before it became Mozila and Firefox, so he knows > what he's talking about: > https://www.jwz.org/doc/threading.html The concept here is not so much that people start a new topic and change the subject, but that sometimes the topic just naturally evolves to the point that a change in subject is sensible. Take this thread for example: the topic has drifted from "Clickable hyperlinks" to talking about email threading. I should be able to change the subject without breaking the thread: Clickable hyperlinks +- RE: Clickable hyperlinks ? +- Re: RE: Clickable hyperlinks ? L- RE: Clickable hyperlinks ? L- Threading [was Re: Clickable hyperlinks] ? L- Re: Threading +- RE: Clickable hyperlinks L- Re: Clickable hyperlinks Adding a "Re:" or "RE" to the subject doesn't change the thread, and neither does changing the subject line in a more substantial way. Of course, I can always *choose* to sort by subject line, or date. Personally I hardly ever sort by thread, so it is no skin off my nose what you do. But when you hit "Reply" to a message, you inherit the "Reference" and "In-Reply-To" headers from the previous message, so at least some people will see it threaded in an existing thread rather than starting a brand new one. -- Steve "Cheer up," they said, "things could be worse." So I cheered up, and sure enough, things got worse. Interesting history lesson. I actually wouldn't know which came first, since I've only been online since 1992 (offline computing since 1972). I do know that I've been on a lot of mailing lists like this one since 1992 (remember Fidonet?), some with actively posting members in the thousands. And I have never heard anyone whine and complain about threading issues that had anything to do with the message title until yesterday. So it goes. I'll repeat again that I'll comply with the rules of this group that are brand spanking new to me, and I've been around a corner or two. From python at deborahswanson.net Wed Jan 4 06:25:34 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 03:25:34 -0800 Subject: Clickable hyperlinks In-Reply-To: <586ccc6d$0$1596$c3e8da3$5496439d@news.astraweb.com> Message-ID: <001301d2667d$445ff450$27b23dae@sambora> Steve D'Aprano wrote, on January 04, 2017 2:20 AM > > On Wed, 4 Jan 2017 03:46 pm, Deborah Swanson wrote: > > > As I've mentioned in other posts on this thread, I'm now > thinking that > > I need to write a class to do this, and find out how > Firefox and url > > aware terminals in Linux do it. There must be a way. > > > A GUI application can interpret text any way it chooses. > Firefox takes a HTML file and renders it, using whatever GUI > library it chooses. That GUI library understands that text like: > > Hello World! > > should be shown in bold face, and text like: > > Example > > should be shown as the word "Example" underlined and in some > colour, and when you click on it the browser will navigate to > the URL given. Firefox can do this because it controls the > environment it runs in. > > Same for Excel, which also controls the environment it runs in. > > That's *not* the case for Python, which is at the mercy of > whatever console or terminal application it is running in. > > However, you can use Python to write GUI applications. Then it becomes > *your* responsibility to create the window, populate it with > any buttons or text or scroll bars you want, and you can > choose to interpret text any way you like -- including as > clickable Hyperlinks. > > The bottom line is, there is no "a way" to do this. There are > a thousand, ten thousand, ways to do it. Every web browser, > every terminal, every URL-aware application, can choose its > own way to do it. There's no one single way that works > everywhere, but if you are working in a console or terminal, > just printing the URL is likely to be interpreted by the > console as a clickable link: > > print("http://www.example.com") > > > > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered > up, and sure enough, things got worse. I can appreciate all that and pretty much knew most of it already. At this point I'm not so much concerned with how to put characters on a white space that are clickable, we've pretty much established at least a couple dozen messages ago that there's nothing for python to get hold of there because of the vast number of places people might want to put clickable links. (I actually read every message on a thread that interests me enough to participate.) I think the entry point to this problem is to find out how to connect to the internet when you click that link. Then I think the nuts and bolts of what symbols to put where for a particular target would fall into place. From python at deborahswanson.net Wed Jan 4 06:43:02 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 03:43:02 -0800 Subject: Clickable hyperlinks In-Reply-To: <586cd0d5$0$1595$c3e8da3$5496439d@news.astraweb.com> Message-ID: <001401d2667f$b569b850$27b23dae@sambora> Steve D'Aprano wrote, on January 04, 2017 2:39 AM > > On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote: > > > Thanks, Steven. Yes, of course if you want to print strings > you must > > enclose them in quotes. I think you learn that in Week 1 of any > > introductory course on Python. > > > > But we aren't trying to print strings here, the point is to produce > > clickable links. I didn't enclose them with quotes because I didn't > > see any point in printing plain text when I wanted > clickable links. I > > actually didn't understand why you thought I should print > them, but it > > never would have occurred to me that you wanted me to print out a > > bunch of silly plain text strings, apparently just for the > heck of it. > > What we have here is a failure to communicate :-) > > I apologise for ruffling your feathers, but its difficult to > judge another person's level of knowledge. In someways you're > obviously quite knowledgeable about Python, but in other ways > you're still learning (as we all are!) and I'm afraid I may > have misjudged exactly what your level of knowledge was. > Sorry about that. > > I'm not suggesting that you print "silly plain text strings" > just for the heck of it. You've asked how to get a clickable > link using Python. There is only one way I know of to get a > clickable link using Python: > > Write out a link as plain text to another application which > then interprets the plain text as a clickable link. > > You *might* be able to get clickable links in Python by > writing an entire GUI application, using (say) the tkinter > library, or one of the third-party GUI libraries like > wxPython, kivy, pyqt, or others, but I haven't a clue how. > But even there, your links will start off as text, which > means you will still need to surround them with quotes to > make them strings. > > > Aside: you've actually raised a fascinating question. I > wonder whether there are any programming languages that > understand URLs as native data types, so that *source code* > starting with http:// etc is understood in the same way that > source code starting with [ is seen as a list or { as a dict? > > > But back to your problem: short of writing you own GUI > application, in which case you can do *anything you like*, you can: > > - write out a HTML file containing the URLs you want, in href= ... tags, then open the HTML file in a web browser > and let the web browser interpret the HTML tags as clickable links; > > > - write out an Excel spreadsheet, using whatever format Excel > expects, open the spreadsheet in Excel, and let Excel > interpret the mystery format as a clickable link; > > - print the URL to the console, and see if the console is > smart enough to interpret it as a clickable link. > > > I'm sorry that I can't give you a one-sentence answer "just > use such-and-such a function, that does exactly what you want > in a platform-independent manner" :-( > > > > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered > up, and sure enough, things got worse. Well, well. People mix and people misunderstand and misjudge each other. It's the way of things with people. I just needed to tell you how it all looked from my side. So are we done with that? I certainly hope so. I'm quite well aware by now that there is no one-sentence answer to my original question, if there's any coherent answer at all. Them's the breaks. Live with it or live without it, it doesn't care. I do appreciate the education I've gotten on this thread about the issues involved. But I rather imagine that near term anyway, I'll only be pondering it now and then, and maybe poking around a bit. Who knows, maybe I'll eventually invent the solution and make my first million dollars from it. haha, yeah right. So it goes. Deborah From redstone-cold at 163.com Wed Jan 4 07:15:06 2017 From: redstone-cold at 163.com (iMath) Date: Wed, 4 Jan 2017 04:15:06 -0800 (PST) Subject: Introducing my Python/PyQt5 powered Bing wallpaper open source project Message-ID: Hello everyone , I'd like to introduce my Python/PyQt5 powered Bing wallpaper open source project. ? BingNiceWallpapers https://github.com/redstoneleo/BingNiceWallpapers BingNiceWallpapers can get background images from http://www.bing.com/?mkt=zh-CN and set them as your desktop wallpaper by just a single click on system tray icon. For Windows binary installer and more information, please refer to http://mathjoy.lofter.com/post/42208d_7cabcf7 (written in simplified Chinese) Features Changing wallpaper in a single click on system tray icon or after a specified time interval Windows and Linux Gnome desktop support Delete or save the current wallpaper Welcome to help with the project ! From rosuav at gmail.com Wed Jan 4 07:15:46 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 4 Jan 2017 23:15:46 +1100 Subject: Clickable hyperlinks In-Reply-To: <001401d2667f$b569b850$27b23dae@sambora> References: <586cd0d5$0$1595$c3e8da3$5496439d@news.astraweb.com> <001401d2667f$b569b850$27b23dae@sambora> Message-ID: On Wed, Jan 4, 2017 at 10:43 PM, Deborah Swanson wrote: > I'm quite well aware by now that there is no one-sentence answer to my > original question, if there's any coherent answer at all. Them's the > breaks. Live with it or live without it, it doesn't care. Yeah, there's no simple answer; however, you'll find that Python on many platforms is entirely capable of popping a URL up in the user's default browser. Check this out: >>> import antigravity This uses the 'webbrowser' module, which knows about a number of different ways to open a browser, and will attempt them all. So if you can figure out the UI part of things, actually making the link pop up in a browser isn't too hard; for instance, if you're doing OAuth at the command line and need the user to go and authenticate, you can simply webbrowser.open("http://......./") and it'll DTRT. ChrisA From darcy at vex.net Wed Jan 4 08:03:09 2017 From: darcy at vex.net (D'Arcy Cain) Date: Wed, 4 Jan 2017 08:03:09 -0500 Subject: Clickable hyperlinks In-Reply-To: <000601d2666d$6e7eae30$27b23dae@sambora> References: <000601d2666d$6e7eae30$27b23dae@sambora> Message-ID: Deborah - please trim your quoted text. On 2017-01-04 04:32 AM, Deborah Swanson wrote: > Thanks, Steven. Yes, of course if you want to print strings you must > enclose them in quotes. I think you learn that in Week 1 of any > introductory course on Python. Closer to minute one. When I investigated Python years ago the first thing I learned was; print "Hello, world" > But we aren't trying to print strings here, the point is to produce > clickable links. I didn't enclose them with quotes because I didn't see > any point in printing plain text when I wanted clickable links. I I'm not sure what your links are composed of but mine all look like sequences of characters or "strings." It sounds like you are trying to make URL a first class type like strings. integers, floats, etc. I can't think of any language that treats URLs as first class objects. Even HTML needs quotes: Go here > actually didn't understand why you thought I should print them, but it You want to output them to something. That often involves printing them to a particular handler. > never would have occurred to me that you wanted me to print out a bunch > of silly plain text strings, apparently just for the heck of it. Is that really what you got from his message? > At this point, if I pursue this any farther, it will be to look into how > Firefox takes webpage titles and urls out of its sqlite database and > makes objects you can click on to open the webpages. That's the basic > technology I'd need to find a way to talk (write) python into doing. I can assure you that FF prints the string at some point. It may wrap it in HTML tags first but printing is what it does. Also, the URLs are stored as strings. SQLite has no URL type. If it did then it would still store it as a string somewhere. PostGreSQL would let you create a URL type if you wanted but you would still need to wrap it in quotes (single in this case) when you created the entry. > If it's even worth it. On a practical level it's not worth it, way too > much work for the teensy advantage of having it to use. It might be some > giggles to figure out how to do it and maybe I will sometime just for > funsies. In all the messages in this thread I still don't understand what this "teensy advantage" is supposed to be. Do you want to be able to do this: make_web_link(http://...) instead of: make_web_link("http://...") -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From rbistolfi at gmail.com Wed Jan 4 08:44:34 2017 From: rbistolfi at gmail.com (Rodrigo Bistolfi) Date: Wed, 4 Jan 2017 10:44:34 -0300 Subject: Clickable hyperlinks In-Reply-To: <586cd0d5$0$1595$c3e8da3$5496439d@news.astraweb.com> References: <586c8ab3$0$1506$c3e8da3$5496439d@news.astraweb.com> <000601d2666d$6e7eae30$27b23dae@sambora> <586cd0d5$0$1595$c3e8da3$5496439d@news.astraweb.com> Message-ID: 2017-01-04 7:39 GMT-03:00 Steve D'Aprano : > On Wed, 4 Jan 2017 08:32 pm, Deborah Swanson wrote: > > Aside: you've actually raised a fascinating question. I wonder whether > there > are any programming languages that understand URLs as native data types, so > that *source code* starting with http:// etc is understood in the same way > that source code starting with [ is seen as a list or { as a dict? > ... > Some Smalltalk implementations have something that comes close: st> 'https://python.org' asUrl retrieveContents `asUrl` would be a string method returning a URL instance, which also has a convenient method `retrieveContents` wrapping an http client. Not hard to do with Python, I think this could be an interesting exercise for a learner. From rhodri at kynesim.co.uk Wed Jan 4 08:47:14 2017 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 4 Jan 2017 13:47:14 +0000 Subject: Screwing Up looping in Generator In-Reply-To: <009e01d2662f$b1bf0e30$27b23dae@sambora> References: <009e01d2662f$b1bf0e30$27b23dae@sambora> Message-ID: On 04/01/17 02:10, Deborah Swanson wrote: > Sayth Renshaw wrote, on January 03, 2017 5:36 PM >> >> So can I call the generator twice and receive the same file >> twice in 2 for loops? >> >> Once to get the files name and the second to process? >> >> for file in rootobs: >> base = os.path.basename(file.name) >> write_to = os.path.join("output", >> os.path.splitext(base)[0] + ".csv") >> with open(write_to, 'w', newline='') as csvf: >> for file in rootobs: >> # create and write csv >> >> Cheers >> >> Sayth > > I don't see why not, if you let the first one run to completion and then > do it again a second time. Assuming your generator doesn't somehow > delete or modify the file specifications as it yields them. It would be > helpful to see the code for rootobs, if you have it. Ahem. If Sayth is using the correct terminology and rootobs actually is a generator (not, say, a list or tuple), then no it won't work. Once a generator is exhausted, it's exhausted. Besides, the nested for-loops over the same iterable is a dead giveaway that something is wrong. -- Rhodri James *-* Kynesim Ltd From darcy at vex.net Wed Jan 4 08:58:02 2017 From: darcy at vex.net (D'Arcy Cain) Date: Wed, 4 Jan 2017 08:58:02 -0500 Subject: Clickable hyperlinks In-Reply-To: References: <586c8ab3$0$1506$c3e8da3$5496439d@news.astraweb.com> <000601d2666d$6e7eae30$27b23dae@sambora> <586cd0d5$0$1595$c3e8da3$5496439d@news.astraweb.com> Message-ID: <78f5dfcb-0b76-96c3-03ce-32d69f5561bd@vex.net> On 2017-01-04 08:44 AM, Rodrigo Bistolfi wrote: > 2017-01-04 7:39 GMT-03:00 Steve D'Aprano : >> Aside: you've actually raised a fascinating question. I wonder whether >> there >> are any programming languages that understand URLs as native data types, so >> that *source code* starting with http:// etc is understood in the same way >> that source code starting with [ is seen as a list or { as a dict? > > Some Smalltalk implementations have something that comes close: > > st> 'https://python.org' asUrl retrieveContents But notice that even here the URL has to be defined as a string. To be a first class object you would need to do something like this: url = https://python.org/ The only time you would see that is in config files and languages like shell where everything is a string so no quotes necessary. They are implied. > `asUrl` would be a string method returning a URL instance, which also has a > convenient method `retrieveContents` wrapping an http client. Not hard to > do with Python, I think this could be an interesting exercise for a learner. Sure but the issue is, what to do with it. In any case, it's still just a wrapper around various string methods. You still need to give the class a string to create the object. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From wrw at mac.com Wed Jan 4 09:41:25 2017 From: wrw at mac.com (William Ray Wing) Date: Wed, 04 Jan 2017 09:41:25 -0500 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> Message-ID: > On Jan 4, 2017, at 1:54 AM, Antonio Caminero Garcia wrote: > > On Tuesday, January 3, 2017 at 4:12:34 PM UTC-8, Dietmar Schwertberger wrote: >> On 02.01.2017 12:38, Antonio Caminero Garcia wrote: >> You did not try Wing IDE? It looks less like a spacecraft. Maybe you >> like it. >> Maybe the difference is that Wing is from Python people while the ones >> you listed are from Java people. > > That sounds interesting. By the look of it I think I am going to give it a try. > > [byte] > I want editor with those IDE capabilities and git integration, with optionally cool stuff as for example remote debugging. I use Wing, and I think you will like it. It *is* pythonic, and for what it is worth, offers remote debugging as one of its more recently added features. -Bill From random832 at fastmail.com Wed Jan 4 09:43:04 2017 From: random832 at fastmail.com (Random832) Date: Wed, 04 Jan 2017 09:43:04 -0500 Subject: Simulating int arithmetic with wrap-around In-Reply-To: <58667385$0$1609$c3e8da3$5496439d@news.astraweb.com> References: <58667385$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1483540984.1580961.837095401.1ADA7773@webmail.messagingengine.com> On Fri, Dec 30, 2016, at 09:47, Steve D'Aprano wrote: > Again, assume both operands are in range for an N-bit signed integer. > What's > a good way to efficiently, or at least not too inefficiently, do the > calculations in Python? I'd do something like: bit_mask = (1 << bits) - 1 # 0xFFFF sign_bit = 1 << (bits - 1) # 0x8000 sign_ext = ~bit_mask # ...FFFFF0000 def signed(value): if value & sign_bit: return value | sign_ext else: return value & bit_mask def unsigned(value): return value & bit_mask And also avoid doing it on intermediate steps where it can be shown to not affect the result or allow the value to grow too large. From grant.b.edwards at gmail.com Wed Jan 4 10:35:17 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Wed, 4 Jan 2017 15:35:17 +0000 (UTC) Subject: Clickable hyperlinks References: <007b01d26619$97de4eb0$27b23dae@sambora> Message-ID: On 2017-01-03, Deborah Swanson wrote: > Grant Edwards wrote, on January 03, 2017 3:13 PM >> >> On 2017-01-03, Deborah Swanson wrote: >> >> > I'm sorry, I should have said a GUI console because I >> wouldn't expect >> > a text-based console to produce clickable links. >> >> What's a "GUI console"? > The GUI consoles I have are in Pycharm, the IDLE that comes with > Anaconda, and Spyder. PyCharm and IDLE both ask for internet access when > I open them, so they're capable of opening links, but whether that means > their output space is capable of handling clickable links I don't know. Thanks, that's a bit clearer. For those of us from the Unix world "console" means something else. > I do know printing a full url with the %s specifier or entering a url > and clicking enter just gives you the plain text url. Obviously, not all > GUI consoles are enabled recognize and make clickable links from > correctly formatted urls. > > I was hoping there was some functionality in python to make clickable > links. Could be a package, if the core language doesn't have it. There is no definition for what a "clickable link" is unless you're sending HTML to a web browser. That means there's no way to create funcationality to make one. -- Grant Edwards grant.b.edwards Yow! I'm not an Iranian!! at I voted for Dianne gmail.com Feinstein!! From grant.b.edwards at gmail.com Wed Jan 4 10:42:29 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Wed, 4 Jan 2017 15:42:29 +0000 (UTC) Subject: Clickable hyperlinks References: <007b01d26619$97de4eb0$27b23dae@sambora> Message-ID: On 2017-01-04, Michael Torrie wrote: > On my Linux machine, the terminal emulators I've used all make a regular > url printed out into a clickable link (or at least a right-clickable > link). This is just something they try to do with all things that look > like urls. Sometimes it's helpful, often it's annoying. What I have done is defined a window manager root menu entry that opens a web browser on the current text selection. That lets me "click" on a link in any application that suport the standard X11 text-selection mechanism (which is almost all of them). >> I was hoping there was some functionality in python to make clickable >> links. Could be a package, if the core language doesn't have it. > > No, there is not. If you made a full GUI app using a toolkit like > GTK or Qt, you can indeed place hyperlinks on your forms and the > operating system will automatically connect them to the web browser. > But not in text-mode terminal apps. For me it's double-click to select the url in the terminal window or PDF viewer or whaterver, then right-click on the root window and pick the menu entry that says 'Firefox [sel]' or 'Chrome [sel]'. It's a few extra steps, but it works for almostly any application that displays text. -- Grant Edwards grant.b.edwards Yow! Where do your SOCKS at go when you lose them in gmail.com th' WASHER? From tim at akwebsoft.com Wed Jan 4 11:15:44 2017 From: tim at akwebsoft.com (Tim Johnson) Date: Wed, 4 Jan 2017 07:15:44 -0900 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <87wpeb45eq.fsf@rudin.co.uk> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <20170103191311.GB18910@mail.akwebsoft.com> <87wpeb45eq.fsf@rudin.co.uk> Message-ID: <20170104161544.GA6597@mail.akwebsoft.com> * Paul Rudin [170103 23:17]: > Tim Johnson writes: > > > * Antonio Caminero Garcia [170102 20:56]: > >> Guys really thank you for your answers. Basically now I am more > >> emphasizing in learning in depth a tool and get stick to it so I > >> can get a fast workflow. Eventually I will learn Vim and its > >> python developing setup, I know people who have been programming > >> using Vim for almost 20 years and they did not need to change > >> editor (that is really awesome). > > > > Bye the way, one thing I like about the GUI based vim is that it > > supports tabs, where emacs does not. > > M-x package-install tabbar :) Thank you. list-packages is my friend ... -- Tim http://www.akwebsoft.com, http://www.tj49.com From rgaddi at highlandtechnology.invalid Wed Jan 4 12:50:26 2017 From: rgaddi at highlandtechnology.invalid (Rob Gaddi) Date: Wed, 4 Jan 2017 09:50:26 -0800 Subject: Simulating int arithmetic with wrap-around In-Reply-To: References: <58667385$0$1609$c3e8da3$5496439d@news.astraweb.com> <8737gz4tlp.fsf@nightsong.com> Message-ID: On 01/03/2017 10:00 PM, Gregory Ewing wrote: > Paul Rubin wrote: >> My first thought is towards the struct module, especially if you want to >> handle a bunch of such integers at the same time. Or maybe the array >> module or some combination. > > Or possibly numpy. > Agreed. If you had to do a lot of calculations on arbitrary sized signed/unsigned ints, figuring how to parallelize them into numpy arrays would save you a ton of time. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. From flebber.crue at gmail.com Wed Jan 4 14:09:41 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Wed, 4 Jan 2017 11:09:41 -0800 (PST) Subject: Screwing Up looping in Generator In-Reply-To: References: <009e01d2662f$b1bf0e30$27b23dae@sambora> Message-ID: For completeness I was close this is the working code. def get_list_of_names(generator_arg): name_set = set() for name in generator_arg: base = os.path.basename(name.name) filename = os.path.splitext(base)[0] name_set.add(filename) return name_set def data_attr(roots): """Get the root object and iter items.""" for name in names: directory = "output" write_name = name + ".csv" with open(os.path.join(directory, write_name), 'w', newline='') as csvf: race_writer = csv.writer(csvf, delimiter=',' ) thanks for your time and assistance. It's much appreciated Sayth From maillist at schwertberger.de Wed Jan 4 15:44:45 2017 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Wed, 4 Jan 2017 21:44:45 +0100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> Message-ID: <9ca1868f-7d81-9163-a403-008a85f69c7b@schwertberger.de> On 04.01.2017 15:41, William Ray Wing wrote: > I use Wing, and I think you will like it. It *is* pythonic, and for what it is worth, offers remote debugging as one of its more recently added features. Obviously, you had no other choice than using Wing ;-) The remote debugging has been around for some years. I have been using it quite often to debug on my Raspberry Pi, Nokia N900 and Jolla Phone, all running some Linux system. It works well. It is or was a bit complicated to set up. I think this has been improved with Wing 6, but I did not need it in the last weeks, so I don't know. Regards, Dietmar From maillist at schwertberger.de Wed Jan 4 16:09:30 2017 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Wed, 4 Jan 2017 22:09:30 +0100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> Message-ID: On 04.01.2017 07:54, Antonio Caminero Garcia wrote: > Unfortunately most of the time I am still using print and input functions. I know that sucks, I did not use the pdb module, I guess that IDE debuggers leverage such module. pdb is actually quite useful. On my Windows PCs I can invoke python on any .py file with the -i command line switch by right clicking in the Explorer and selecting "Debug". Now when the script crashes, I can inspect variables without launching a full-scale IDE or starting the script from the command line. For such quick fixes I have also a context menu entry "Edit" for editing with Pythonwin, which is still quite OK as editor and has no licensing restrictions or installation requirements. This is a nice option when you deploy your installation to many PCs over the network. For the print functions vs. debugger: The most useful application for a debugger like Wing is not for bug-fixing, but to set a break point and then interactively develop on the debugger console and with the IDE editor's autocompletion using introspection on the live objects. This is very helpful for hardware interfacing, network protocols or GUI programs. It really boosted my productivity in a way I could not believe before. This is something most people forget when they evaluate programming languages. It's not the language or syntax that counts, but the overall environment. Probably the only other really interactive language and environment is Forth. > If it happens to be Arduino I normally use a sublime plugin called Stino > https://github.com/Robot-Will/Stino > (1337 people starred that cool number :D) Well, it is CodeWarrior which was quite famous at the time of the 68k Macs. The company was bought by Motorola and the IDE is still around for Freescale/NXP/Qualcomm microcontrollers like the HCS08 8 bit series. Around ten years ago the original CodeWarrior IDE was migrated to something Eclipse based. When I last evaluated HCS08 vs. Arduino, the HCS08 won due to the better debug interface and native USB support. HCS08 is still quite cool, but when it comes to documentation, learning curve, tools etc. the Arduinos win.... Regards, Dietmar From wrw at mac.com Wed Jan 4 17:01:31 2017 From: wrw at mac.com (William Ray Wing) Date: Wed, 04 Jan 2017 17:01:31 -0500 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <9ca1868f-7d81-9163-a403-008a85f69c7b@schwertberger.de> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> <9ca1868f-7d81-9163-a403-008a85f69c7b@schwertberger.de> Message-ID: <1BDA202B-83E2-4C6F-9FD4-64E28561BD6D@mac.com> > On Jan 4, 2017, at 3:44 PM, Dietmar Schwertberger wrote: > > On 04.01.2017 15:41, William Ray Wing wrote: >> I use Wing, and I think you will like it. It *is* pythonic, and for what it is worth, offers remote debugging as one of its more recently added features. > Obviously, you had no other choice than using Wing ;-) I should have said something. First, and to the best of my knowledge, I have no relationship with the Wing developers other than being a satisfied customer. Second, seven years ago, when I was reading IDE reviews and testing the more highly rated products, Wing just bubbled up to the top of the sieve I was using (features, ease of use, and the way it fit my idea of ?natural?, pretty much everyone's standard list). > > The remote debugging has been around for some years. I have been using it quite often to debug on my Raspberry Pi, Nokia N900 and Jolla Phone, all running some Linux system. It works well. It is or was a bit complicated to set up. I think this has been improved with Wing 6, but I did not need it in the last weeks, so I don't know. They claim it has been, but like you, I haven?t had need to test it on the new release. Thanks, Bill > > Regards, > > Dietmar > -- > https://mail.python.org/mailman/listinfo/python-list From zxpatric at gmail.com Wed Jan 4 17:14:10 2017 From: zxpatric at gmail.com (zxpatric at gmail.com) Date: Wed, 4 Jan 2017 14:14:10 -0800 (PST) Subject: Work between multiple processes Message-ID: <892336f7-0adb-431b-aed0-b871d14ac16c@googlegroups.com> Hi everyone, I ran into a case that I need to create a work process of an application (Jython so has to call using java.exe) which will collect the data based on what main process indicates. (1) I tried multiprocessing package, no luck. Java.exe can't be called from Process class? (2) I tried subprocess. subprocess.communicate function will wait for the work process to terminate so to return. either (1) or (2) doesn't work out well. Please suggest. Global system queue? Thanks, Patrick. From hpj at urpla.net Wed Jan 4 17:15:03 2017 From: hpj at urpla.net (Hans-Peter Jansen) Date: Wed, 04 Jan 2017 23:15:03 +0100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <10677094.xGjz0IE2Dp@xrated> On Montag, 2. Januar 2017 03:38:53 Antonio Caminero Garcia wrote: > Hello, I am having a hard time deciding what IDE or IDE-like code editor > should I use. This can be overwhelming. > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, > IntelliJ with Python plugin. Well, since nobody mentioned it, yet: eric is doing quite nice here. With on the fly error checking, jedi and qscintilla calltips and autocompletion, git integration (using a plugin), graphical debugger, it's grown to a capable IDE over the years. Given, it's a fully open source, PyQt based project, it also shows the powers of Python3 and PyQt. Pete From rosuav at gmail.com Wed Jan 4 17:49:14 2017 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 05 Jan 2017 10:49:14 +1200 Subject: Clickable hyperlinks Message-ID: <3004643574@f38.n261.z1.binkp.net> On Thu, Jan 5, 2017 at 9:58 AM, Deborah Swanson wrote: > Chris Angelico wrote, on January 04, 2017 4:16 AM >> This uses the 'webbrowser' module, which knows about a number >> of different ways to open a browser, and will attempt them >> all. So if you can figure out the UI part of things, actually >> making the link pop up in a browser isn't too hard; for >> instance, if you're doing OAuth at the command line and need >> the user to go and authenticate, you can simply >> webbrowser.open("http://......./") and it'll DTRT. >> > > Thank you, thank you! Finally, at least one person on this list knows > about something (anything) in the python world that is internet aware. > It's also occurred to me that Beautifulsoup downloads data from a url, > so that code must have access to some kind of an internet engine too. We've been all talking at cross purposes a bit in this thread. Most of us thought you were talking about the *user interface* of a clickable link, but if you're talking about the *mechanics* of HTTP downloads, Python has excellent facilities. I'd recommend checking out the third-party 'requests' module on PyPI. > I googled antigravity and found a number of interesting links. > > The History of Python: import antigravity > http://python-history.blogspot.com/2010/06/import-antigravity.html > > Among other things, it was added to Python 3 in 2010, so it's been > around a little while. And a comment mentions that "The antigravity > module is also included in Python 2.7." > > And a reddit poster tells us that "if you type 'import antigravity' into > a Python command line your default browser opens the XKCD comic 'Python' > in a tab." > https://www.reddit.com/r/ProgrammerHumor/comments/1hvb5n/til_if_you_type > _import_antigravity_into_a_python/ > > An "import antigravity" video at > https://www.youtube.com/watch?v=_V0V6Rk6Fp4 Hehe, yeah. It's a big joke that started because XKCD mentioned the language. But actually, the source code for antigravity.py itself isn't significant; all it does is call on the webbrowser module: https://docs.python.org/3/library/webbrowser.html > Yes, I'd gotten as far as figuring out that you don't need a clickable > link. Code that opens a url in a browse would do the job just fine. Or > the webbrowser.open("http://......./") in a Linux terminal you suggest. > (I just have to get my Linux machine up and running again to try it.) > > All in all, given that clickable urls in a console is a non-starter, > this hits the nail on the head. Many thanks again! Cool! Glad I could help out a bit. There are a few different things you can consider: 1) Open up a browser tab and let the user see the result 2) Make the request programmatically and access the text of the page for further processing 3) Invoke a hidden web browser, browse to a URL, submit form data, etc, as a means of testing a web server. All three are possible. Take your pick! ChrisA From python at deborahswanson.net Wed Jan 4 17:58:42 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 14:58:42 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <002401d266de$18bc4450$27b23dae@sambora> Chris Angelico wrote, on January 04, 2017 4:16 AM > > On Wed, Jan 4, 2017 at 10:43 PM, Deborah Swanson > wrote: > > I'm quite well aware by now that there is no one-sentence > answer to my > > original question, if there's any coherent answer at all. > Them's the > > breaks. Live with it or live without it, it doesn't care. > > Yeah, there's no simple answer; however, you'll find that > Python on many platforms is entirely capable of popping a URL > up in the user's default browser. Check this out: > > >>> import antigravity > > This uses the 'webbrowser' module, which knows about a number > of different ways to open a browser, and will attempt them > all. So if you can figure out the UI part of things, actually > making the link pop up in a browser isn't too hard; for > instance, if you're doing OAuth at the command line and need > the user to go and authenticate, you can simply > webbrowser.open("http://......./") and it'll DTRT. > Thank you, thank you! Finally, at least one person on this list knows about something (anything) in the python world that is internet aware. It's also occurred to me that Beautifulsoup downloads data from a url, so that code must have access to some kind of an internet engine too. I googled antigravity and found a number of interesting links. The History of Python: import antigravity http://python-history.blogspot.com/2010/06/import-antigravity.html Among other things, it was added to Python 3 in 2010, so it's been around a little while. And a comment mentions that "The antigravity module is also included in Python 2.7." And a reddit poster tells us that "if you type 'import antigravity' into a Python command line your default browser opens the XKCD comic 'Python' in a tab." https://www.reddit.com/r/ProgrammerHumor/comments/1hvb5n/til_if_you_type _import_antigravity_into_a_python/ An "import antigravity" video at https://www.youtube.com/watch?v=_V0V6Rk6Fp4 And its page in the Package Index: https://pypi.python.org/pypi/antigravity/0.1, with a Page Not Found Error for the Home Page. So it doesn't look like there's any alternative but to download it and look at the code. Yes, I'd gotten as far as figuring out that you don't need a clickable link. Code that opens a url in a browse would do the job just fine. Or the webbrowser.open("http://......./") in a Linux terminal you suggest. (I just have to get my Linux machine up and running again to try it.) All in all, given that clickable urls in a console is a non-starter, this hits the nail on the head. Many thanks again! Deborah From tjreedy at udel.edu Wed Jan 4 18:21:55 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 4 Jan 2017 18:21:55 -0500 Subject: Hey, I'm new to python so don't judge. In-Reply-To: References: <40adacf3-b4de-4243-8cb5-6ea5fb74cf18@googlegroups.com> Message-ID: On 1/3/2017 10:15 PM, Dennis Lee Bieber wrote: > And that statement tells us you are trying to run from within some > IDE/editor which is trapping Python exceptions and producing a dialog > box for them. IDLE does this when one runs code from the editor, because it cannot/should not inject error messages into the editor buffer... AND it replaces the ^ with red highlighting of the code pointed to. No information is lost. Apparently, some beginners do not see the connection between the SyntaxError box and the red highlighting. I think I should add something to the box. Maybe 'The error was detected at the point of the red highlighting.' > Instead, save your script (if you haven't yet) as a file > (whatever.py). > > Open a command line interpreter/shell. > > Navigate (cd ...) to where you saved the file > > Type "python whatever.py" What a nuisance. > Copy and paste the results of the CLI/Shell window. Or one can hit F5 to run the code or Alt-X to just check the syntax. A beginner should do this every few lines, and it should be as easy as possible to check. If one needs to ask about a syntax error, one can copy the code up and including the highlighted part. Example: "When I run this code in IDLE def is_same(target, number: if I get a SyntaxError at 'if'." If the OP had known to do this, the error might have been seen without posting. -- Terry Jan Reedy From rosuav at gmail.com Wed Jan 4 18:49:15 2017 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 5 Jan 2017 10:49:15 +1100 Subject: Clickable hyperlinks In-Reply-To: <002401d266de$18bc4450$27b23dae@sambora> References: <002401d266de$18bc4450$27b23dae@sambora> Message-ID: On Thu, Jan 5, 2017 at 9:58 AM, Deborah Swanson wrote: > Chris Angelico wrote, on January 04, 2017 4:16 AM >> This uses the 'webbrowser' module, which knows about a number >> of different ways to open a browser, and will attempt them >> all. So if you can figure out the UI part of things, actually >> making the link pop up in a browser isn't too hard; for >> instance, if you're doing OAuth at the command line and need >> the user to go and authenticate, you can simply >> webbrowser.open("http://......./") and it'll DTRT. >> > > Thank you, thank you! Finally, at least one person on this list knows > about something (anything) in the python world that is internet aware. > It's also occurred to me that Beautifulsoup downloads data from a url, > so that code must have access to some kind of an internet engine too. We've been all talking at cross purposes a bit in this thread. Most of us thought you were talking about the *user interface* of a clickable link, but if you're talking about the *mechanics* of HTTP downloads, Python has excellent facilities. I'd recommend checking out the third-party 'requests' module on PyPI. > I googled antigravity and found a number of interesting links. > > The History of Python: import antigravity > http://python-history.blogspot.com/2010/06/import-antigravity.html > > Among other things, it was added to Python 3 in 2010, so it's been > around a little while. And a comment mentions that "The antigravity > module is also included in Python 2.7." > > And a reddit poster tells us that "if you type 'import antigravity' into > a Python command line your default browser opens the XKCD comic 'Python' > in a tab." > https://www.reddit.com/r/ProgrammerHumor/comments/1hvb5n/til_if_you_type > _import_antigravity_into_a_python/ > > An "import antigravity" video at > https://www.youtube.com/watch?v=_V0V6Rk6Fp4 Hehe, yeah. It's a big joke that started because XKCD mentioned the language. But actually, the source code for antigravity.py itself isn't significant; all it does is call on the webbrowser module: https://docs.python.org/3/library/webbrowser.html > Yes, I'd gotten as far as figuring out that you don't need a clickable > link. Code that opens a url in a browse would do the job just fine. Or > the webbrowser.open("http://......./") in a Linux terminal you suggest. > (I just have to get my Linux machine up and running again to try it.) > > All in all, given that clickable urls in a console is a non-starter, > this hits the nail on the head. Many thanks again! Cool! Glad I could help out a bit. There are a few different things you can consider: 1) Open up a browser tab and let the user see the result 2) Make the request programmatically and access the text of the page for further processing 3) Invoke a hidden web browser, browse to a URL, submit form data, etc, as a means of testing a web server. All three are possible. Take your pick! ChrisA From tjreedy at udel.edu Wed Jan 4 18:57:53 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 4 Jan 2017 18:57:53 -0500 Subject: Clickable hyperlinks In-Reply-To: <000601d2666d$6e7eae30$27b23dae@sambora> References: <586c8ab3$0$1506$c3e8da3$5496439d@news.astraweb.com> <000601d2666d$6e7eae30$27b23dae@sambora> Message-ID: On 1/4/2017 4:32 AM, Deborah Swanson wrote: > My original question was whether python had anything to provide this > functionality, and the answer appears to be a resounding NO!!! I would say 'Yes, but with user effort'. To have a string interpreted as a clickable link, you send the string to software capable of creating a clickable link, plus the information 'this is a clickable link'*. There are two ways to tag a string as a link. One is to use markup around the url in the string itself. '' and html are example. Python provides multiple to make this easy. The other is to tag the string with a separate argument. Python provides tkinter, which wraps tk Text widgets, which have a powerful tag system. One can define a Link tag that will a) cause text to be displayed, for instance, blue and underlined and b) cause clicks on the text to generate a web request. One could then use mytext.insert('insert', 'http://www.example.com', Link) Browser must do something similar when they encounter when they encounter html link tags. * If the software directly recognizes a bare url such as 'http://www.example.com' as a link, without further indication, then it should have a way to disable conversion to a clickable link. -- Terry Jan Reedy From ceh329 at gmail.com Wed Jan 4 19:02:52 2017 From: ceh329 at gmail.com (Charles Heizer) Date: Wed, 4 Jan 2017 16:02:52 -0800 (PST) Subject: MySQL schema sync or diff Message-ID: <68944b94-64d5-41a2-8d85-9a6471f5caab@googlegroups.com> Hello, I have a MySQL database that is not managed (yet) and I would like to get an output or diff against my new model file. I'm using flask-sqlalchemy. Are there any modules that would help me discover the differences so that I can script a migration to begin using flask-migrate? Thanks! From python at deborahswanson.net Wed Jan 4 19:07:08 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 16:07:08 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <002901d266e7$a8583fc0$27b23dae@sambora> D'Arcy Cain wrote, on Wednesday, January 04, 2017 5:03 AM > > Deborah - please trim your quoted text. Yes, I will. Some lists want to have it all to review in one message, some want it trimmed to just the lines you are responding to. I was just waiting to see what this list wants. > On 2017-01-04 04:32 AM, Deborah Swanson wrote: > > But we aren't trying to print strings here, the point is to produce > > clickable links. I didn't enclose them with quotes because I didn't > > see any point in printing plain text when I wanted > clickable links. I > > I'm not sure what your links are composed of but mine all look like > sequences of characters or "strings." It sounds like you are > trying to > make URL a first class type like strings. integers, floats, etc. I > can't think of any language that treats URLs as first class objects. > Even HTML needs quotes: > > Go here It seemed reasonable that you might be able to print urls, which is why I tried the experiment with all of Steven's suggested formats. But I was highly skeptical that any would work without some kind of modifiers to a bare print statement. > > actually didn't understand why you thought I should print > them, but it > > You want to output them to something. That often involves > printing them > to a particular handler. Yes, that's one of the things I would expect if we could print them. > > never would have occurred to me that you wanted me to print out a > > bunch of silly plain text strings, apparently just for the > heck of it. > > Is that really what you got from his message? Please forgive me, and I hope Steven forgives me too, but I was sick to death of all the beating on a dead horse (using Python to make clickable links in a console, any console). I'd taken heart when he first suggested his print experiment, because it was a plausible approach. But I lost my temper when he upbraided me in this message for failing to enclose my strings in quotes, in a most patronizing kind of way, when printing out plain text was absolutely nowhere on the progress toward a solution scale. I've been quite impressed with Steven's knowledge and talent, and after fending off the throng of unseeing naysayers all afternoon, it was just a little too much. I really should have closed my email reader hours before I read and replied to this message. Shoulda, coulda, woulda. > I can assure you that FF prints the string at some point. It > may wrap > it in HTML tags first but printing is what it does. Also, > the URLs are > stored as strings. SQLite has no URL type. If it did then it would > still store it as a string somewhere. PostGreSQL would let > you create a > URL type if you wanted but you would still need to wrap it in quotes > (single in this case) when you created the entry. I have no doubt that some variant of printing is involved. Transporting urls to the internet is an output process. FF's sqlite implementation does store urls as a text field in at least 2 tables. I would be interested in how FF takes those text urls and opens web pages with them, although I've learned and figured out just today some ways that Python can also do it. Turns out clickable links were a red herring. If Steven's original suggestion included anything but a bare print statement, like the use of a special specifier or linking the print statement to some module, the use of quoted strings would have at least been worthy of consideration. But we all know what print("http://www.wherever.com") would print, and it would be utterly worthless for the purpose at hand. Trying the print statement without the quotes was a least a possibility, if there was any awareness in the print code of urls and what to do with them. That was the whole point of this fishing expedition, as I saw it. To see if there was any undocumented or narrowly known-of features in the print code. > In all the messages in this thread I still don't understand what this > "teensy advantage" is supposed to be. Do you want to be able > to do this: > > make_web_link(http://...) > > instead of: > > make_web_link("http://...") > > -- > D'Arcy J.M. Cain > System Administrator, Vex.Net > http://www.Vex.Net/ IM:darcy at Vex.Net > VoIP: sip:darcy at Vex.Net You probably didn't see my oneliner on the "why do it" part in the swarm of messages on this thread yesterday. In it I mentioned that the use would be to open urls in the data I'm working with while I'm debugging the code that uses them. I want to see what pages they open, without having to leave my IDE. (Obviously I'd have to open another .py file, but that would be easier and quicker than the alternatives.) I never intended my original question to be any more than a frivolous toss out into the sea, to see if anyone knew an answer. I was flat out astonished when it blew up into the mini-monster that it did. Is make_web_link("http://...") valid python code? That's exactly the kind of answer I was looking for, and I will try it (or look it up if it needs something imported) as soon as I send this off. Thank you. It's possible you caught just the tail end of a hot mess without seeing all the irrational vitriol and nonsense that led up to this message. Lucky you. Deborah From torriem at gmail.com Wed Jan 4 19:47:39 2017 From: torriem at gmail.com (Michael Torrie) Date: Wed, 4 Jan 2017 17:47:39 -0700 Subject: Clickable hyperlinks In-Reply-To: <002401d266de$18bc4450$27b23dae@sambora> References: <002401d266de$18bc4450$27b23dae@sambora> Message-ID: <7417e761-dce6-bf09-acf9-074cbda24d81@gmail.com> On 01/04/2017 03:58 PM, Deborah Swanson wrote: > Thank you, thank you! Finally, at least one person on this list knows > about something (anything) in the python world that is internet aware. > It's also occurred to me that Beautifulsoup downloads data from a url, > so that code must have access to some kind of an internet engine too. Except that you never mentioned anything about this in your posts before. It seemed to me you were asking about printing out clickable hyperlinks with python. Calling the OS to launch a browser to view a url is a different beast which is why no one mentioned it before. If you don't tell us what you're actually trying to do (your end goal), things are more frustrating for everyone. If you had said early on you just want to be able to send the user to a particular url in a web browser, what Chris suggested would have been said a long time ago, rather than focusing on console output which is what you were asking about and focusing attention on. Just so you know, BeautifulSoup does not do any internet access itself; it's only an HTML parser. You have to fetch web pages using something like python's urllib and then feed the data to BeautifulSoup. urllib is not a web browser though. It can pretend to be a browser as far as the server is concerned but it knows nothing about javascript or rendering. It just retrieves bytes which you can then feed to BeautifulSoup or some other parser. > Yes, I'd gotten as far as figuring out that you don't need a clickable > link. Code that opens a url in a browse would do the job just fine. Good! I just wish you would have mentioned this much earlier as your end goal. > Or the webbrowser.open("http://......./") in a Linux terminal you suggest. > (I just have to get my Linux machine up and running again to try it.) The webbrowser module does not require console or terminal output. It will work on Windows or Linux if I'm not mistaken. It asks the OS to launch the default browser and load the indicated url. > All in all, given that clickable urls in a console is a non-starter, > this hits the nail on the head. Many thanks again! From steve+comp.lang.python at pearwood.info Wed Jan 4 20:51:40 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Thu, 05 Jan 2017 13:51:40 +1200 Subject: Hey, I'm new to python so don't judge. References: <2025125030@f38.n261.z1.binkp.net> Message-ID: <1029557784@f38.n261.z1.binkp.net> On Thursday 05 January 2017 10:21, Terry Reedy wrote: > On 1/3/2017 10:15 PM, Dennis Lee Bieber wrote: > >> And that statement tells us you are trying to run from within some >> IDE/editor which is trapping Python exceptions and producing a dialog >> box for them. > > IDLE does this when one runs code from the editor, because it > cannot/should not inject error messages into the editor buffer... > AND it replaces the ^ with red highlighting of the code pointed to. No > information is lost. Apparently, some beginners do not see the > connection between the SyntaxError box and the red highlighting. Some people may not even be able to distinguish red from other colours. Those with red-green colourblindness will probably see the red as a sort of muddy brown that hardly stands out as different from usual black text. http://wearecolorblind.com/ One should never use colour alone as the only indicator of status. http://stackoverflow.com/questions/1498669/gui-design-for-color-blindness http://davemeeker.com/color-blind-considerations-for-ui-design/ > I > think I should add something to the box. Maybe 'The error was detected > at the point of the red highlighting.' I just tested the REPL in idle for 2.7.4, and I get this: >>> print Hello World SyntaxError: invalid syntax where "print" is green text on a white background, and "World" is black text on a red background. That may be unfriendly to the colourblind, and makes coping and pasting the error less helpful. I suggest: - check the colours in a colourblindness simulator, and pay attention to the contrast; is the text still clear? - include the ^ caret as the primary status indicator, delegating colour to secondary; this makes errors in IDLE a little more like the same experience in the standard REPL. If I open a Python file containing: print Hello World and choose "Check Module", IDLE highlights the word "World" in red and displays a dialog showing: There's an error in your program: invalid syntax Suggestion: Change the dialog box to display a read-only text field showing the traceback: File "idletest.py", line 1 print Hello World ^ SyntaxError: invalid syntax and add a button "Copy traceback" to allow the user to copy the text of the traceback and paste it into an email. (Strictly speaking, the Copy button is redundant if the user can select the text in the field, but beginners may not realise the text can be selected.) >> Instead, save your script (if you haven't yet) as a file >> (whatever.py). >> >> Open a command line interpreter/shell. >> >> Navigate (cd ...) to where you saved the file >> >> Type "python whatever.py" > > What a nuisance. *shrug* If you're a Linux user, chances are you already have a shell open and ready to go. And with tab completion, you don't have to type as much as you might think. It just becomes second nature after a while. I type something like "pypathwhat" and the shell will autocomplete directory and filenames. Anyway, this isn't an argument over which is better, IDLE or the system terminal. They both have their advantages and disadvantages, and in practice the real answer to "which is better?" is always "whichever one you are used to". -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at deborahswanson.net Wed Jan 4 21:29:00 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Thu, 05 Jan 2017 14:29:00 +1200 Subject: Clickable hyperlinks Message-ID: <3272602342@f38.n261.z1.binkp.net> Terry Reedy wrote, on January 04, 2017 10:18 PM > > On 1/5/2017 12:11 AM, Deborah Swanson wrote: > > Terry Reedy wrote, on January 04, 2017 3:58 PM > > >> To have a string interpreted as a clickable link, you send the string to > >> software capable of creating a clickable link, plus the information > >> 'this is a clickable link'*. There are two ways to tag a string as a > >> link. One is to use markup around the url in the string itself. > >> '' and html are example. Python provides multiple to make this > >> easy. The other is to tag the string with a separate argument. > >> Python provides tkinter, which wraps tk Text widgets, which have a > >> powerful tag system. One can define a Link tag that will a) cause text > >> to be displayed, for instance, blue and underlined and b) cause clicks on > >> the text to generate a web request. One could then use > >> mytext.insert('insert', 'http://www.example.com', Link) Browser > >> must do something similar when they encounter when they encounter > >> html link tags. > > > > I've actually moved on from my original question to one of opening a > > url in a browser with python, which seems to be a much more easily > > achieved goal. > > > But someone else mentioned tkinter, and I looked at it awhile ago but > > haven't used it for anything. That really could be the way to go if > > you want to make clickable links, although you still need some kind of > > internet engine to open the url in a browser. > > IDLE allows a user to add help menu entries that, when clicked on, open > either a local file or an internet url. For instance, adding the pair > 'Pillow' and "https://pillow.readthedocs.io/en/latest/" in > the Settings > dialog adda "Pillow" to the help menu (after the standard stuff). > Clicking on Help => Pillow opens > "https://pillow.readthedocs.io/en/latest/" in the default browswer. > IDLE just used the webbrowser module to do this. No use re-inventing > the wheel. If instead "Pillow" were a link in text, the click handler > should do something similar. Yes, unless someone suggests something better, the webbrowser module looks like the way to go for opening urls in a browser. > > > You say, "There are two ways to tag a string as a link. One is to use > > markup around the url in the string itself. '' and html are > > examples. Python provides multiple ways to make this easy." > > > > Can you tell me where I'd begin to look for these? Are they in the > > core language, or in packages? > > I was referring to using either % or .format string formatting. Both > are in the core and described somewhere in the Library manual. '%' > should be in the Symbols page of the Index and 'format' on > the 'F' page. > > -- > Terry Jan Reedy I looked up % in the Symbols page, but I didn't see any specifier related to urls. It would be nice if there was something like a %u for url format, but it isn't in there. I also tried print("http//python.org") ^ but got 'SyntaxError: invalid syntax', with the red arrow pointing at the first angle bracket. I also tried print("http//python.org") ^ and got the same syntax error, but I'm not sure if that's how you meant html should be used. I also tried to look up 'format', but there's no such entry in the Index. There are a lot of entries that begin with 'format', but none of them mention urls or anything link related. 'format_field() (string.Formatter method)' looked like a possibility, but again, I didn't see anything to format a link with. Maybe I didn't look hard enough, or didn't see something that _would_ work. As I've said, at this point I've moved on to directly opening a url in a browser with the webbrowser module. All I originally wanted to do was be able to open a url without leaving my IDE while I was debugging data that has urls in it. Clickable links are really just eye candy that I don't need if I can get the same result programmatically. But I was curious whether there are any ways to tag a string as a link in a print statement. If there are, I didn't find any, but thanks for your suggestions! Deborah From steve+comp.lang.python at pearwood.info Wed Jan 4 21:51:41 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Thu, 05 Jan 2017 13:51:41 +1100 Subject: Hey, I'm new to python so don't judge. References: <40adacf3-b4de-4243-8cb5-6ea5fb74cf18@googlegroups.com> Message-ID: <586db4c9$0$2921$c3e8da3$76491128@news.astraweb.com> On Thursday 05 January 2017 10:21, Terry Reedy wrote: > On 1/3/2017 10:15 PM, Dennis Lee Bieber wrote: > >> And that statement tells us you are trying to run from within some >> IDE/editor which is trapping Python exceptions and producing a dialog >> box for them. > > IDLE does this when one runs code from the editor, because it > cannot/should not inject error messages into the editor buffer... > AND it replaces the ^ with red highlighting of the code pointed to. No > information is lost. Apparently, some beginners do not see the > connection between the SyntaxError box and the red highlighting. Some people may not even be able to distinguish red from other colours. Those with red-green colourblindness will probably see the red as a sort of muddy brown that hardly stands out as different from usual black text. http://wearecolorblind.com/ One should never use colour alone as the only indicator of status. http://stackoverflow.com/questions/1498669/gui-design-for-color-blindness http://davemeeker.com/color-blind-considerations-for-ui-design/ > I > think I should add something to the box. Maybe 'The error was detected > at the point of the red highlighting.' I just tested the REPL in idle for 2.7.4, and I get this: >>> print Hello World SyntaxError: invalid syntax where "print" is green text on a white background, and "World" is black text on a red background. That may be unfriendly to the colourblind, and makes coping and pasting the error less helpful. I suggest: - check the colours in a colourblindness simulator, and pay attention to the contrast; is the text still clear? - include the ^ caret as the primary status indicator, delegating colour to secondary; this makes errors in IDLE a little more like the same experience in the standard REPL. If I open a Python file containing: print Hello World and choose "Check Module", IDLE highlights the word "World" in red and displays a dialog showing: There's an error in your program: invalid syntax Suggestion: Change the dialog box to display a read-only text field showing the traceback: File "idletest.py", line 1 print Hello World ^ SyntaxError: invalid syntax and add a button "Copy traceback" to allow the user to copy the text of the traceback and paste it into an email. (Strictly speaking, the Copy button is redundant if the user can select the text in the field, but beginners may not realise the text can be selected.) >> Instead, save your script (if you haven't yet) as a file >> (whatever.py). >> >> Open a command line interpreter/shell. >> >> Navigate (cd ...) to where you saved the file >> >> Type "python whatever.py" > > What a nuisance. *shrug* If you're a Linux user, chances are you already have a shell open and ready to go. And with tab completion, you don't have to type as much as you might think. It just becomes second nature after a while. I type something like "pypathwhat" and the shell will autocomplete directory and filenames. Anyway, this isn't an argument over which is better, IDLE or the system terminal. They both have their advantages and disadvantages, and in practice the real answer to "which is better?" is always "whichever one you are used to". -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at deborahswanson.net Wed Jan 4 21:53:02 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Thu, 05 Jan 2017 14:53:02 +1200 Subject: Clickable hyperlinks Message-ID: <4018729974@f38.n261.z1.binkp.net> Rhodri James wrote, on January 05, 2017 3:53 AM > > On 05/01/17 04:52, Deborah Swanson wrote: > > My original question was in fact whether there was a way to make > > clickable hyperlinks in a console. I was persuaded after about 10 > > replies that the answer was no, > > Then you were persuaded wrong; the actual answer was "this isn't a > meaningful question since it's based on incorrect assumptions." > Translating that to "No" is just as much a mistake as > translating it to > "Yes." > > -- > Rhodri James *-* Kynesim Ltd Actually, your statement "this isn't a meaningful question since it's based on incorrect assumptions" is false. PyCharm outputs clickable links to the console, but they aren't web links, they simply link to lines of code. I'd seen that PyCharm can make links in the console that aren't web enabled, so it seemed, and in fact it is, reasonable to assume that it could be done for urls. I just wanted to know if anyone knew how to do it. Granted, the suggestion to use tkinter to enable the links came up much later than in the first 10 or so replies, and since tkinter makes clickable links possible, that's another reason my question wasn't based on false assumptions. It simply appears that the early responders to my question went off on a tangent of what is or is not technologically possible, and all of the approaches under consideration were in fact dead ends. But clickable links turns out to be just eye candy, and the real result I wanted, which is opening urls in a browser from my IDE, is much more quickly and easily done programmatically. Although I didn't see this before I asked my question, and only saw it after reading quite a few replies. Perhaps though, I should have said "I was persuaded after about 10 replies that that no one understood what I was asking." But that just seemed plain rude, so I went with "the answer was no". From darcy at vex.net Wed Jan 4 22:00:09 2017 From: darcy at vex.net (D'Arcy Cain) Date: Wed, 4 Jan 2017 22:00:09 -0500 Subject: Clickable hyperlinks In-Reply-To: <002401d266de$18bc4450$27b23dae@sambora> References: <002401d266de$18bc4450$27b23dae@sambora> Message-ID: On 2017-01-04 05:58 PM, Deborah Swanson wrote: >> the user to go and authenticate, you can simply >> webbrowser.open("http://......./") and it'll DTRT. > > Thank you, thank you! Finally, at least one person on this list knows > about something (anything) in the python world that is internet aware. Lots of things in Python are Internet aware. That's not the question you asked though. > It's also occurred to me that Beautifulsoup downloads data from a url, > so that code must have access to some kind of an internet engine too. Nope. You have to feed it HTML. You either need to generate that or capture it from somewhere. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From rosuav at gmail.com Wed Jan 4 22:08:04 2017 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 05 Jan 2017 15:08:04 +1200 Subject: Clickable hyperlinks Message-ID: <1835463384@f38.n261.z1.binkp.net> On Thu, Jan 5, 2017 at 2:24 PM, D'Arcy Cain wrote: > Think of it this way. You drop a ring down a drain. You can ask two > questions, "How do I remove a drain trap?" or "How do I recover a ring that > I dropped down the drain?" If you ask the first question you will get lots > of advice on tools and buckets, etc. People will assume that the drain is > blocked. Ask the second question and someone might mention a magnet and a > piece of string. ... and then you follow up with "it's a gold ring, magnet won't touch it", and we'll go off on a tangent about whether the ring is sufficiently plain that it might be the One Ring, and shouldn't you destroy it instead of retrieving it.... because that's what we do here :D ChrisA From rustompmody at gmail.com Wed Jan 4 22:22:29 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Wed, 4 Jan 2017 19:22:29 -0800 (PST) Subject: Clickable hyperlinks In-Reply-To: References: <002901d266e7$a8583fc0$27b23dae@sambora> Message-ID: <40b7d676-6e5c-4855-89ba-f19c8bbf479e@googlegroups.com> This thread does lead to the question: Is the Url type in python less first-class than it could be? In scheme I could point to something like this https://docs.racket-lang.org/net/url.html Is there something equivalent in python? From darcy at vex.net Wed Jan 4 22:24:35 2017 From: darcy at vex.net (D'Arcy Cain) Date: Wed, 4 Jan 2017 22:24:35 -0500 Subject: Clickable hyperlinks In-Reply-To: <002901d266e7$a8583fc0$27b23dae@sambora> References: <002901d266e7$a8583fc0$27b23dae@sambora> Message-ID: <08aab9ab-eff2-86de-8b68-8b8514aedf0a@vex.net> On 2017-01-04 07:07 PM, Deborah Swanson wrote: > D'Arcy Cain wrote, on Wednesday, January 04, 2017 5:03 AM >> In all the messages in this thread I still don't understand what this >> "teensy advantage" is supposed to be. Do you want to be able >> to do this: >> >> make_web_link(http://...) >> >> instead of: >> >> make_web_link("http://...") [...] > Is make_web_link("http://...") valid python code? That's exactly the It's just a made up name. My point was that the first was syntactically incorrect and the second, while not a real method, was at least parseable. I think I saw someone else mention this but it bears repeating. When you ask a question make sure you are presenting the problem and not your solution to a secret problem. Always tell us the actual problem you are trying to solve. You will get much better answers. Think of it this way. You drop a ring down a drain. You can ask two questions, "How do I remove a drain trap?" or "How do I recover a ring that I dropped down the drain?" If you ask the first question you will get lots of advice on tools and buckets, etc. People will assume that the drain is blocked. Ask the second question and someone might mention a magnet and a piece of string. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From rosuav at gmail.com Wed Jan 4 22:27:18 2017 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 05 Jan 2017 15:27:18 +1200 Subject: Clickable hyperlinks Message-ID: <2710021487@f38.n261.z1.binkp.net> On Thu, Jan 5, 2017 at 3:19 PM, Deborah Swanson wrote: > I downloaded the code from the Package Index, but there really wasn't > much in it. This is the entire .py file: Ehh, wrong file. Try the one in the standard library: https://github.com/python/cpython/blob/master/Lib/antigravity.py https://github.com/python/cpython/blob/master/Lib/webbrowser.py Or you can look in your installed Python - "import webbrowser; print(webbrowser.__file__)" will tell you where. ChrisA From flebber.crue at gmail.com Wed Jan 4 22:32:22 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Wed, 4 Jan 2017 19:32:22 -0800 (PST) Subject: Is there a good process or library for validating changes to XML format? Message-ID: <1604a9b9-08ee-40fb-9014-abc2d5d989f1@googlegroups.com> Afternoon Is there a good library or way I could use to check that the author of the XML doc I am using doesn't make small changes to structure over releases? Not fully over this with XML but thought that XSD may be what I need, if I search "python XSD" I get a main result for PyXB and generateDS (https://pythonhosted.org/generateDS/). Both seem to be libraries for generating bindings to structures for parsing so maybe I am searching the wrong thing. What is the right thing to search? Cheers Sayth From rosuav at gmail.com Wed Jan 4 23:08:04 2017 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 5 Jan 2017 15:08:04 +1100 Subject: Clickable hyperlinks In-Reply-To: <08aab9ab-eff2-86de-8b68-8b8514aedf0a@vex.net> References: <002901d266e7$a8583fc0$27b23dae@sambora> <08aab9ab-eff2-86de-8b68-8b8514aedf0a@vex.net> Message-ID: On Thu, Jan 5, 2017 at 2:24 PM, D'Arcy Cain wrote: > Think of it this way. You drop a ring down a drain. You can ask two > questions, "How do I remove a drain trap?" or "How do I recover a ring that > I dropped down the drain?" If you ask the first question you will get lots > of advice on tools and buckets, etc. People will assume that the drain is > blocked. Ask the second question and someone might mention a magnet and a > piece of string. ... and then you follow up with "it's a gold ring, magnet won't touch it", and we'll go off on a tangent about whether the ring is sufficiently plain that it might be the One Ring, and shouldn't you destroy it instead of retrieving it.... because that's what we do here :D ChrisA From python at deborahswanson.net Wed Jan 4 23:19:33 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 20:19:33 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <005701d2670a$eb5d9b80$27b23dae@sambora> Chris Angelico wrote, on January 04, 2017 4:16 AM > > Yeah, there's no simple answer; however, you'll find that > Python on many platforms is entirely capable of popping a URL > up in the user's default browser. Check this out: > > >>> import antigravity I downloaded the code from the Package Index, but there really wasn't much in it. This is the entire .py file: STRIP_URL = "http://xkcd.com/353/" def start(): return STRIP_URL And setup.py is equally disappointing: from distutils.core import setup setup( name='antigravity', version='0.1', description='A really simple module that allow everyone to do "import antigravity"', author='Fabien Schwob', author_email='antigravity at x-phuture.com', url='http://fabien.schwob.org/antigravity/', packages=['antigravity'], ) > This uses the 'webbrowser' module, which knows about a number > of different ways to open a browser, and will attempt them > all. So if you can figure out the UI part of things, actually > making the link pop up in a browser isn't too hard; for > instance, if you're doing OAuth at the command line and need > the user to go and authenticate, you can simply > webbrowser.open("http://......./") and it'll DTRT. > > ChrisA All the action of antigravity must be done by the import statement. When import opens a module that immediately returns a url, it must have a mechanism to open it in a browser. It would be very easy to do the same thing with my own .py and import it into another .py. Or, take a look at import's code and figure out how it opens a url in a browser. I imagine it's the 'webbrowser' module you mention. If it tries several methods, just pick one that will work for you. Or, take a look at this Index of Packages Matching 'webbrowser' (~50 packages) https://pypi.python.org/pypi?%3Aaction=search&term=webbrowser&submit=sea rch D'Arcy was right, there's lots in python that's internet aware, though that wasn't the question I knew to ask. From rosuav at gmail.com Wed Jan 4 23:27:19 2017 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 5 Jan 2017 15:27:19 +1100 Subject: Clickable hyperlinks In-Reply-To: <005701d2670a$eb5d9b80$27b23dae@sambora> References: <005701d2670a$eb5d9b80$27b23dae@sambora> Message-ID: On Thu, Jan 5, 2017 at 3:19 PM, Deborah Swanson wrote: > I downloaded the code from the Package Index, but there really wasn't > much in it. This is the entire .py file: Ehh, wrong file. Try the one in the standard library: https://github.com/python/cpython/blob/master/Lib/antigravity.py https://github.com/python/cpython/blob/master/Lib/webbrowser.py Or you can look in your installed Python - "import webbrowser; print(webbrowser.__file__)" will tell you where. ChrisA From torriem at gmail.com Wed Jan 4 23:41:20 2017 From: torriem at gmail.com (Michael Torrie) Date: Wed, 4 Jan 2017 21:41:20 -0700 Subject: Clickable hyperlinks In-Reply-To: <005701d2670a$eb5d9b80$27b23dae@sambora> References: <005701d2670a$eb5d9b80$27b23dae@sambora> Message-ID: <2cf52e7b-147e-9f51-23a5-0847ee298865@gmail.com> On 01/04/2017 09:19 PM, Deborah Swanson wrote: > Or, take a look at import's code and figure out how it opens a url in a > browser. I imagine it's the 'webbrowser' module you mention. If it tries > several methods, just pick one that will work for you. webbrowser is part of the python standard library: https://docs.python.org/3/library/webbrowser.html It uses whatever method is appropriate for the platform for opening a url in the user's default browser. > Or, take a look at this Index of Packages Matching 'webbrowser' (~50 > packages) > https://pypi.python.org/pypi?%3Aaction=search&term=webbrowser&submit=sea > rch There may be modules in there that will be useful, but the first port of call should always be the python standard library. > D'Arcy was right, there's lots in python that's internet aware, though > that wasn't the question I knew to ask. That's why it's always good to state your end goal when asking about things. From python at deborahswanson.net Wed Jan 4 23:52:28 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 20:52:28 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <005801d2670f$84ab39b0$27b23dae@sambora> Chris Angelico wrote, on January 04, 2017 3:49 PM > > On Thu, Jan 5, 2017 at 9:58 AM, Deborah Swanson > wrote: > > > > Thank you, thank you! Finally, at least one person on this list knows > > about something (anything) in the python world that is internet aware. > > We've been all talking at cross purposes a bit in this > thread. Most of us thought you were talking about the *user > interface* of a clickable link, but if you're talking about > the *mechanics* of HTTP downloads, Python has excellent > facilities. I'd recommend checking out the third-party > 'requests' module on PyPI. My original question was in fact whether there was a way to make clickable hyperlinks in a console. I was persuaded after about 10 replies that the answer was no, and I tried with little success to change the question to one of directly opening a url in a browser from Python. (In hindsight, maybe I should have started a new thread.) Turns out this version of the question does have good answers, but the revised question got totally drowned out in the scramble to show that clickable links in a console are a no-go. I'm not surprised that you didn't see the shift in what I was looking for. > > Hehe, yeah. It's a big joke that started because XKCD > mentioned the language. But actually, the source code for > antigravity.py itself isn't significant; all it does is call > on the webbrowser module: > https://docs.python.org/3/library/webbrowser.html I saw how insignificant antigravity's code is when I downloaded it from the Package Index, and I copied the current version into another message of yours that I replied to before this one. The original antigravity may have used webbrowser, but now it just returns a url and hands it off to import to open it. Maybe the author didn't know import had that capability until after antigravity had been in the wild awhile. Regardless, antigravity does point the way to pythonic web access. > Yes, I'd gotten as far as figuring out that you don't need a clickable > link. Code that opens a url in a browser would do the job just fine. Or > the webbrowser.open("http://......./") in a Linux terminal you > suggest. (I just have to get my Linux machine up and running again to > try it.) > > All in all, given that clickable urls in a console is a non-starter, > this hits the nail on the head. Many thanks again! Cool! Glad I could help out a bit. There are a few different things you can consider: 1) Open up a browser tab and let the user see the result 2) Make the request programmatically and access the text of the page for further processing 3) Invoke a hidden web browser, browse to a URL, submit form data, etc, as a means of testing a web server. All three are possible. Take your pick! ChrisA I have a fourth option. I use Firefox with profiles and I could make one specifically for this task, and have the code open the page in that profile. This would give it it's own window, history, bookmarks, and sqlite database, which could be handy if a particular url continues to be a problem, or I want to track it for some reason. In this project I usually would want to look at the photos, so a text download wouldn't be that helpful. But it could be in some other project, and I expect to be doing quite a bit of web work down the road. From python at deborahswanson.net Wed Jan 4 23:55:56 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 20:55:56 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <005901d26710$006c4940$27b23dae@sambora> Chris Angelico wrote, on January 04, 2017 8:27 PM > > On Thu, Jan 5, 2017 at 3:19 PM, Deborah Swanson > wrote: > > I downloaded the code from the Package Index, but there really wasn't > > much in it. This is the entire .py file: > > Ehh, wrong file. Try the one in the standard library: > https://github.com/python/cpython/blob/master/Lib/antigravity.py https://github.com/python/cpython/blob/master/Lib/webbrowser.py >Or you can look in your installed Python - "import webbrowser; print>(webbrowser.__file__)" will tell you where. > >ChrisA Great! I will check it out. Thanks again! From tjreedy at udel.edu Thu Jan 5 00:04:17 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 5 Jan 2017 00:04:17 -0500 Subject: Hey, I'm new to python so don't judge. In-Reply-To: <586db4c9$0$2921$c3e8da3$76491128@news.astraweb.com> References: <40adacf3-b4de-4243-8cb5-6ea5fb74cf18@googlegroups.com> <586db4c9$0$2921$c3e8da3$76491128@news.astraweb.com> Message-ID: On 1/4/2017 9:51 PM, Steven D'Aprano wrote: > On Thursday 05 January 2017 10:21, Terry Reedy wrote: >> IDLE does this when one runs code from the editor, because it >> cannot/should not inject error messages into the editor buffer... >> AND it replaces the ^ with red highlighting of the code pointed to. No >> information is lost. Apparently, some beginners do not see the >> connection between the SyntaxError box and the red highlighting. > > Some people may not even be able to distinguish red from other colours. Those > with red-green colourblindness will probably see the red as a sort of muddy > brown that hardly stands out as different from usual black text. > > http://wearecolorblind.com/ > > One should never use colour alone as the only indicator of status. > > http://stackoverflow.com/questions/1498669/gui-design-for-color-blindness > > http://davemeeker.com/color-blind-considerations-for-ui-design/ I have described what I inherited. After I wrote this, it occurred to me that perhaps the error color should be flashing, a bell rung, and maybe even the line and column numbers included in the box. >> I >> think I should add something to the box. Maybe 'The error was detected >> at the point of the red highlighting.' > > > I just tested the REPL in idle for 2.7.4, and I get this: > >>>> print Hello World > SyntaxError: invalid syntax > > > where "print" is green text on a white background, and "World" is black text on > a red background. That may be unfriendly to the colourblind, and makes coping > and pasting the error less helpful. I suggest: > > - check the colours in a colourblindness simulator, and pay attention to the > contrast; is the text still clear? User can configure most of the syntax colors. I am not sure ir the error 'red' is one of them or not. #Suggestion 1 > - include the ^ caret as the primary status indicator, delegating colour to > secondary; this makes errors in IDLE a little more like the same experience in > the standard REPL. Indeed, why not both in the Shell? IDLE must currently be deleting the caret line. > If I open a Python file containing: > > print Hello World > > and choose "Check Module", IDLE highlights the word "World" in red and displays > a dialog showing: > > There's an error in your program: > invalid syntax #Suggestion 2 > Change the dialog box to display a read-only text field showing the traceback: > > File "idletest.py", line 1 > print Hello World > ^ > SyntaxError: invalid syntax #Suggestion 3 > and add a button "Copy traceback" to allow the user to copy the text of the > traceback and paste it into an email. > > (Strictly speaking, the Copy button is redundant if the user can select the > text in the field, but beginners may not realise the text can be selected.) Thank you for the suggestions. I will keep a copy of this. -- Terry Jan Reedy From python at deborahswanson.net Thu Jan 5 00:11:28 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 4 Jan 2017 21:11:28 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <005e01d26712$2c75b6f0$27b23dae@sambora> Terry Reedy wrote, on January 04, 2017 3:58 PM > > On 1/4/2017 4:32 AM, Deborah Swanson wrote: > > > My original question was whether python had anything to provide this > > functionality, and the answer appears to be a resounding NO!!! > > I would say 'Yes, but with user effort'. > > To have a string interpreted as a clickable link, you send the string to > software capable of creating a clickable link, plus the information > 'this is a clickable link'*. There are two ways to tag a string as a > link. One is to use markup around the url in the string itself. > '' and html are example. Python provides multiple to make this > easy. The other is to tag the string with a separate argument. Python > provides tkinter, which wraps tk Text widgets, which have a powerful tag > system. One can define a Link tag that will a) cause text to be > displayed, for instance, blue and underlined and b) cause clicks on the > text to generate a web request. One could then use > mytext.insert('insert', 'http://www.example.com', Link) > Browser must do something similar when they encounter when they > encounter html link tags. I've actually moved on from my original question to one of opening a url in a browser with python, which seems to be a much more easily achieved goal. But someone else mentioned tkinter, and I looked at it while ago but haven't used it for anything. That really could be the way to go if you want to make clickable links, although you still need some kind of internet engine to open the url in a browser. PyCharm has clickable local links in its console output, but they're not internet enabled, they just jump to the relevant line of code. You say, "There are two ways to tag a string as a link. One is to use markup around the url in the string itself. '' and html are examples. Python provides multiple ways to make this easy." Can you tell me where I'd begin to look for these? Are they in the core language, or in packages? > * If the software directly recognizes a bare url such as > 'http://www.example.com' as a link, without further > indication, then it > should have a way to disable conversion to a clickable link. One would think so. Thanks for all your info! > > -- > Terry Jan Reedy From steve+comp.lang.python at pearwood.info Thu Jan 5 00:30:42 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Thu, 05 Jan 2017 16:30:42 +1100 Subject: Clickable hyperlinks References: <002901d266e7$a8583fc0$27b23dae@sambora> <40b7d676-6e5c-4855-89ba-f19c8bbf479e@googlegroups.com> Message-ID: <586dda09$0$11124$c3e8da3@news.astraweb.com> On Thursday 05 January 2017 14:22, Rustom Mody wrote: > This thread does lead to the question: > Is the Url type in python less first-class than it could be? > > In scheme I could point to something like this > https://docs.racket-lang.org/net/url.html Those docs say: "To access the text of a document from the web, first obtain its URL AS A STRING..." [emphasis added] which means that URLs are not a first-class data type in Racket at all. URLs in Racket are just strings, exactly the same as in Python. There is a url struct: https://docs.racket-lang.org/net/url.html#%28def._%28%28lib._net%2Furl- structs..rkt%29._url%29%29 but there no first-class syntactic support for them, as ints and lists have in Python: 123 # rather than int("123") [1, 2] # rather than list(1, 2) > Is there something equivalent in python? Just like Racket, URLs in Python are not first-class. They start as a string, and then you parse them into a tuple: https://docs.python.org/3/library/urllib.parse.html https://docs.python.org/2/library/urlparse.html -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From torriem at gmail.com Thu Jan 5 00:41:48 2017 From: torriem at gmail.com (Michael Torrie) Date: Thu, 05 Jan 2017 17:41:48 +1200 Subject: Python for WEB-page !? Message-ID: <2895939025@f38.n261.z1.binkp.net> On 01/05/2017 04:53 PM, Victor Porton wrote: > Ionut Predoiu wrote: > >> I am a beginner in programming language. >> I want to know what version of Python I must to learn to use, beside of >> basic language, because I want to integrate in my site 1 page in which >> users to can made calculus based on my formulas already write behind (the >> users will only complete some field, and after push "Calculate" button >> will see the results in form of: table, graphic, and so on ...). Please >> take into account that behind will be more mathematical >> equations/formulas, so the speed I think must be take into account. > > Consider PyPi. I never used it, but they say, it is faster than usual > CPython interpreter. With respect, I don't think it's appropriate to direct a python beginner to PyPi. Far better to direct him to the relevant resources (like Django) and focus him on the standard Python interpreter, hopefully version 3. Besides that, there's the old expression. Premature optimization is the root of all evil. Until Python is shown to be too slow for a given task, it's premature to think about speedups like Cython or even PyPi. From torriem at gmail.com Thu Jan 5 00:48:30 2017 From: torriem at gmail.com (Michael Torrie) Date: Thu, 05 Jan 2017 17:48:30 +1200 Subject: Python for WEB-page !? Message-ID: <2317493603@f38.n261.z1.binkp.net> On 01/05/2017 05:57 AM, Ionut Predoiu wrote: > Good afternoon, > > I am a beginner in programming language. I want to know what version > of Python I must to learn to use, beside of basic language, because I > want to integrate in my site 1 page in which users to can made > calculus based on my formulas already write behind (the users will > only complete some field, and after push "Calculate" button will see > the results in form of: table, graphic, and so on ...). Please take > into account that behind will be more mathematical > equations/formulas, so the speed I think must be take into account. While Python can do that, using a web framework to process HTTP requests and generate HTML to display in the browser, I don't believe Python is the appropriate language for the task at hand. Most web sites that do interactive formula calculations like you describe do it all in the browser using Javascript. No need to have a web server do all that heavy lifting at all. A simple html file would contain everything you need. Even if you want to use Python to generate the web page and process events, you'll still have to master Javascript at some point to make the webpages more interactive. From tjreedy at udel.edu Thu Jan 5 01:17:37 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 5 Jan 2017 01:17:37 -0500 Subject: Clickable hyperlinks In-Reply-To: <005e01d26712$2c75b6f0$27b23dae@sambora> References: <005e01d26712$2c75b6f0$27b23dae@sambora> Message-ID: On 1/5/2017 12:11 AM, Deborah Swanson wrote: > Terry Reedy wrote, on January 04, 2017 3:58 PM >> To have a string interpreted as a clickable link, you send the string > to >> software capable of creating a clickable link, plus the information >> 'this is a clickable link'*. There are two ways to tag a string as a >> link. One is to use markup around the url in the string itself. >> '' and html are example. Python provides multiple to make this >> easy. The other is to tag the string with a separate argument. Python > >> provides tkinter, which wraps tk Text widgets, which have a powerful > tag >> system. One can define a Link tag that will a) cause text to be >> displayed, for instance, blue and underlined and b) cause clicks on > the >> text to generate a web request. One could then use >> mytext.insert('insert', 'http://www.example.com', Link) >> Browser must do something similar when they encounter when they >> encounter html link tags. > > I've actually moved on from my original question to one of opening a url > in a browser with python, which seems to be a much more easily achieved > goal. > But someone else mentioned tkinter, and I looked at it while ago but > haven't used it for anything. That really could be the way to go if you > want to make clickable links, although you still need some kind of > internet engine to open the url in a browser. IDLE allows a user to add help menu entries that, when clicked on, open either a local file or an internet url. For instance, adding the pair 'Pillow' and "https://pillow.readthedocs.io/en/latest/" in the Settings dialog adda "Pillow" to the help menu (after the standard stuff). Clicking on Help => Pillow opens "https://pillow.readthedocs.io/en/latest/" in the default browswer. IDLE just used the webbrowser module to do this. No use re-inventing the wheel. If instead "Pillow" were a link in text, the click handler should do something similar. > You say, "There are two ways to tag a string as a link. One is to use > markup around the url in the string itself. '' and html are > examples. Python provides multiple ways to make this easy." > > Can you tell me where I'd begin to look for these? Are they in the core > language, or in packages? I was referring to using either % or .format string formatting. Both are in the core and described somewhere in the Library manual. '%' should be in the Symbols page of the Index and 'format' on the 'F' page. -- Terry Jan Reedy From hetchkay at gmail.com Thu Jan 5 02:09:11 2017 From: hetchkay at gmail.com (H Krishnan) Date: Thu, 5 Jan 2017 12:39:11 +0530 Subject: Forcing prompt to be on newline when embedding Python with stdin/out redirection Message-ID: Hi, I am working on embedding Python in my application. I have redirected sys.stdin and sys.stdout to call methods from a Qt TextEdit widget. Everything works fine except that the Python prompt does not always come in a new line: >>> dir() ['__builtins__', '__doc__', '__name__', '__package__']>>> Why doesn't the prompt appear in a new line as with the default stdout? Thanks, Krishnan From domini.lex at gmail.com Thu Jan 5 02:31:08 2017 From: domini.lex at gmail.com (domini.lex at gmail.com) Date: Wed, 4 Jan 2017 23:31:08 -0800 (PST) Subject: how to create IPictureDisp objects with python? In-Reply-To: References: Message-ID: ???????, 25 ??????? 2001 ?., 12:29:26 UTC+3 ???????????? Felix ???????: > i try to host an activex control in my program . the control has a picture > property which only accepts "IPictureDisp" interfaces. does anybody know how > to create IPictureDisp objects? thanks. Hi, today in 2017 i have the same problem with IPictureDisp. I saw your posts on some sources without an answer, did you find a solution? From rus.cahimb at gmail.com Thu Jan 5 03:23:09 2017 From: rus.cahimb at gmail.com (Ramanathan Muthaiah) Date: Thu, 5 Jan 2017 00:23:09 -0800 (PST) Subject: Is there a good process or library for validating changes to XML format? In-Reply-To: <1604a9b9-08ee-40fb-9014-abc2d5d989f1@googlegroups.com> References: <1604a9b9-08ee-40fb-9014-abc2d5d989f1@googlegroups.com> Message-ID: <7c3fadf7-53a4-434c-b04b-0800c4de597a@googlegroups.com> On Thursday, January 5, 2017 at 9:02:38 AM UTC+5:30, Sayth Renshaw wrote: > Afternoon > > Is there a good library or way I could use to check that the author of the XML doc I am using doesn't make small changes to structure over releases? > > Not fully over this with XML but thought that XSD may be what I need, if I search "python XSD" I get a main result for PyXB and generateDS (https://pythonhosted.org/generateDS/). > > Both seem to be libraries for generating bindings to structures for parsing so maybe I am searching the wrong thing. What is the right thing to search? [RM]: Have you tried 'xmllint' (though it's not Python library) ? /Ram From nntp.fpp at spamgourmet.com Thu Jan 5 03:32:00 2017 From: nntp.fpp at spamgourmet.com (fpp) Date: Thu, 05 Jan 2017 20:32:00 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do Message-ID: <928897436@f38.n261.z1.binkp.net> > On Thu, Jan 5, 2017 at 12:12 PM, Chris Clark > wrote: >> I want an IDE that I can use at work and home, linux and dare I say >> windows. >> Sublime, had to remove it from my work PC as it is not licensed. >> Atom, loved it until it slowed down. >> VIM, ok the best if you know vi inside out. >> Any JAVA based IDE, just slows up on work PC's due to all the >> background stuff that corporates insist they run. >> Why can not someone more clever than I fork DrPython and bring it up >> to date. >> Its is fast, looks great and just does the job ? I'm suprised no one in this rich thread has even mentioned SciTE : http://www.scintilla.org/ Admittedly it's closer to an excellent code editor than a full-blown IDE. But it's very lightweight and fast, cross-platform, has superb syntax coloring and UTF8 handling, and is highly configurable through its configuration file(s) and embedded LUA scripting. It's also well maintained : version 1.0 came out in 1999, and the latest (3.7.2) is just a week old... Its IDE side consists mostly of hotkeys to run the interpreter or compiler for the language you're editing, with the file in the current tab. A side pane shows the output (prints, exceptions, errors etc.) of the running script. A nice touch is that it understands these error messages and makes them clickable, taking you to the tab/module/line where the error occurred. Also, it can save its current tabs (and their state) to a "session" file for later reloading, which is close to the idea of a "project" in most IDEs. Oh, and it had multi-selection and multi-editing before most of the new IDEs out there :-) Personally that's about all I need for my Python activities, but it can be customized much further than I have done : there are "hooks" for other external programs than compilers/interpreters, so you can also run a linter, debugger or cvs from the editor. One word of warning: unlike most newer IDEs which tend to be shiny-shiny and ful of bells and whistles at first sight, out of the box SciTE is *extremely* plain looking (you could even say drab, or ugly :-). It is up to you to decide how it should look and what it should do or not, through the configuration file. Fortunately the documentation is very thorough, and there are a lot of examples lying around to be copy/pasted (like a dark theme, LUA scripts etc.). Did I mention it's lightweight ? The archive is about 1.5 MB and it just needs unzipping, no installation. May be worth a look if you haven't tried it yet... fp From flebber.crue at gmail.com Thu Jan 5 06:00:12 2017 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Thu, 5 Jan 2017 03:00:12 -0800 (PST) Subject: Is there a good process or library for validating changes to XML format? In-Reply-To: <7c3fadf7-53a4-434c-b04b-0800c4de597a@googlegroups.com> References: <1604a9b9-08ee-40fb-9014-abc2d5d989f1@googlegroups.com> <7c3fadf7-53a4-434c-b04b-0800c4de597a@googlegroups.com> Message-ID: It definitely has more features than i knew http://xmlsoft.org/xmllint.html Essentially thigh it appears to be aimed at checking validity and compliance of xml. I why to check the structure of 1 xml file against the previous known structure to ensure there are no changes. Cheers Sayth From cae.mefeagroup at gmail.com Thu Jan 5 06:07:28 2017 From: cae.mefeagroup at gmail.com (Ionut Predoiu) Date: Thu, 05 Jan 2017 23:07:28 +1200 Subject: Python for WEB-page !? References: <604212044@f38.n261.z1.binkp.net> Message-ID: <3995227808@f38.n261.z1.binkp.net> Good morning, Thanks to all for feedback and advice. Because I am a beginner I will read more about versions of Python recommended by you. On the other side I am interested to know if exist some sites which have develop platform where can be use for free Python from browsers, without have it installed on PC/laptop. As beginner I want to practice from everywhere. I waiting with higher interest your feedback. Thanks to all members of community for support and advice. Keep in touch. Kind regards. On Thursday, January 5, 2017 at 2:57:23 PM UTC+2, Ionut Predoiu wrote: > Good afternoon, > > I am a beginner in programming language. > I want to know what version of Python I must to learn to use, beside of basic language, because I want to integrate in my site 1 page in which users to can made calculus based on my formulas already write behind (the users will only complete some field, and after push "Calculate" button will see the results in form of: table, graphic, and so on ...). > Please take into account that behind will be more mathematical equations/formulas, so the speed I think must be take into account. > > I waiting with higher interest your feedback. > > Thanks to all members of community for support and advice. > Keep in touch. > Kind regards. From irmen.NOSPAM at xs4all.nl Thu Jan 5 06:49:34 2017 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Thu, 05 Jan 2017 23:49:34 +1200 Subject: Work between multiple processes References: <2510477911@f38.n261.z1.binkp.net> Message-ID: <2985338719@f38.n261.z1.binkp.net> On 4-1-2017 23:14, zxpatric at gmail.com wrote: > Hi everyone, > > I ran into a case that I need to create a work process of an application (Jython so has to call using java.exe) which will collect the data based on what main process indicates. > > (1) I tried multiprocessing package, no luck. Java.exe can't be called from Process class? > > (2) I tried subprocess. subprocess.communicate function will wait for the work process to terminate so to return. > > > either (1) or (2) doesn't work out well. Please suggest. Global system queue? > > Thanks, > Patrick. > Is it a requirement that the workdf process is also Jython? If not: you could spawn a Python subprocess that hosts a Pyro4 daemon. Utilizing the Pyrolite java client library you can call methods in it from the java/jython side. (Unfortunately it is not yet possible (due to jython incompatibilities) to use the full Pyro4 library on the Jython side as well). Not sure if it meets your set of requirements but have a look at http://pythonhosted.org/Pyro4/ http://pythonhosted.org/Pyro4/pyrolite.html Irmen From rhodri at kynesim.co.uk Thu Jan 5 06:53:15 2017 From: rhodri at kynesim.co.uk (Rhodri James) Date: Thu, 5 Jan 2017 11:53:15 +0000 Subject: Clickable hyperlinks In-Reply-To: <005801d2670f$84ab39b0$27b23dae@sambora> References: <005801d2670f$84ab39b0$27b23dae@sambora> Message-ID: <763d0613-7c39-d540-5b1d-c2e925638c57@kynesim.co.uk> On 05/01/17 04:52, Deborah Swanson wrote: > My original question was in fact whether there was a way to make > clickable hyperlinks in a console. I was persuaded after about 10 > replies that the answer was no, Then you were persuaded wrong; the actual answer was "this isn't a meaningful question since it's based on incorrect assumptions." Translating that to "No" is just as much a mistake as translating it to "Yes." -- Rhodri James *-* Kynesim Ltd From eryksun at gmail.com Thu Jan 5 07:00:30 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 06 Jan 2017 00:00:30 +1200 Subject: Forcing prompt to be on newline when embedding Python with Message-ID: <2421804724@f38.n261.z1.binkp.net> On Thu, Jan 5, 2017 at 7:09 AM, H Krishnan wrote: > > I am working on embedding Python in my application. You forgot to tell us the version of Python that you're embedding. > I have redirected sys.stdin and sys.stdout to call methods from a Qt TextEdit > widget. Everything works fine except that the Python prompt does not always > come in a new line: > >>>> dir() > ['__builtins__', '__doc__', '__name__', '__package__']>>> > > Why doesn't the prompt appear in a new line as with the default stdout? Are you using code.InteractiveConsole / code.interact? If not, in what mode do you compile, Py_file_input ("exec") or Py_single_input ("single")? The latter executes PRINT_EXPR: >>> dis.dis(compile('1', '', 'single')) 1 0 LOAD_CONST 0 (1) 3 PRINT_EXPR 4 LOAD_CONST 1 (None) 7 RETURN_VALUE PRINT_EXPR in turn calls sys.displayhook on the value it pops from the stack. The default hook writes the repr of the value and a newline to sys.stdout, and it also references the value as "_" in the builtins module (2.x __builtin__). From tonycamgar at gmail.com Thu Jan 5 07:13:24 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Fri, 06 Jan 2017 00:13:24 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I References: <1078639094@f38.n261.z1.binkp.net> Message-ID: <532913255@f38.n261.z1.binkp.net> On Thursday, January 5, 2017 at 9:51:17 AM UTC-8, ArnoB wrote: > On 02-01-17 12:38, Antonio Caminero Garcia wrote: > > Hello, I am having a hard time deciding what IDE or IDE-like code editor should I use. This can be overwhelming. > > > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, IntelliJ with Python plugin. > > > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. My screen should be mostly ? ?made of code? ? as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool and python oriented. > > > > The problem with Vim is the learning curve, so I know the very basic stuff, but obviously not enough for coding and I do not have time to learn it, it is a pity because there are awesome plugins that turns Vim into a lightweight powerful IDE-like. So now it is not an option but I will reconsider it in the future, learning little by little. Also, I am not very fan GUI guy if the task can be accomplished through the terminal. However, I don? ?t understand why people underrate GUIs, that said I normally use shortcuts for the most frequent tasks and when I have to do something that is not that frequent then I do it with the mouse, for the latter case in vim you would need to look for that specific command every time. > > > > Sublime is my current and preferred code editor. I installed Anaconda, Git integration and a couple of additional plugins that make sublime very powerful. Also, what I like about sublime compared to the full featured IDEs, besides the minimalism, is how you can perform code navigation back and forth so fast, I mean this is something that you can also do with the others but for some subjective reason I specifically love how sublime does it. The code completion in sublime I do not find it very intelligence, the SublimeCodeIntel is better than the one that Anaconda uses but the completions are not as verbose as in the IDEs. > > > > Now, I am thinking about giving a try to Visual Studio Code Edition (take a look, it sounds good https://marketplace.visualstudio.com/items?itemName=donjay amanne.python). I need an editor for professional software development. What would you recommend to me? > > Hi Antonio, > > Just an extra one in case you'll ever want to create > a nice GUI, then there's also QT Creator: > https://wiki.qt.io/QtCreator_and_PySide > > A very simple but powerful interface a la XCode... > > It integrates nicely with PySide: > https://wiki.qt.io/QtCreator_and_PySide > > gr > Arno Thanks! From tonycamgar at gmail.com Thu Jan 5 07:40:16 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Fri, 06 Jan 2017 00:40:16 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I References: <2715841046@f38.n261.z1.binkp.net> Message-ID: <424559356@f38.n261.z1.binkp.net> On Wednesday, January 4, 2017 at 1:10:04 PM UTC-8, Dietmar Schwertberger wrote: > On 04.01.2017 07:54, Antonio Caminero Garcia wrote: > > Unfortunately most of the time I am still using print and input functions. I know that sucks, I did not use the pdb module, I guess that IDE debuggers leverage such module. > pdb is actually quite useful. On my Windows PCs I can invoke python on > any .py file with the -i command line switch by right clicking in the > Explorer and selecting "Debug". Now when the script crashes, I can > inspect variables without launching a full-scale IDE or starting the > script from the command line. For such quick fixes I have also a context > menu entry "Edit" for editing with Pythonwin, which is still quite OK as > editor and has no licensing restrictions or installation requirements. > This is a nice option when you deploy your installation to many PCs over > the network. I am on MacOS but interesting way of debugging, I will take the idea. > > For the print functions vs. debugger: > The most useful application for a debugger like Wing is not for > bug-fixing, but to set a break point and then interactively develop on > the debugger console and with the IDE editor's autocompletion using > introspection on the live objects. This is very helpful for hardware > interfacing, network protocols or GUI programs. It really boosted my > productivity in a way I could not believe before. This is something most > people forget when they evaluate programming languages. It's not the > language or syntax that counts, but the overall environment. Probably > the only other really interactive language and environment is Forth. > This is exactly part of the capabilities that I am looking for. I loved you brought that up. When I think of an ideal IDE (besides the desirable features that I already mentioned previously) as a coworker who is telling me the values,types and ids that the objects are getting as you are setting breakpoints. So why not use the debugger interactively to develop applications. As long as one sets the breakpoints in a meaningful way so you can trace your code in a very productive way. Is that what you mean by interactive environment? > > If it happens to be Arduino I normally use a sublime plugin called Stino > > https://github.com/Robot-Will/Stino > > (1337 people starred that cool number :D) > Well, it is CodeWarrior which was quite famous at the time of the 68k Macs. > The company was bought by Motorola and the IDE is still around for > Freescale/NXP/Qualcomm microcontrollers like the HCS08 8 bit series. > Around ten years ago the original CodeWarrior IDE was migrated to > something Eclipse based. > When I last evaluated HCS08 vs. Arduino, the HCS08 won due to the better > debug interface and native USB support. HCS08 is still quite cool, but > when it comes to documentation, learning curve, tools etc. the Arduinos > win.... > > > Regards, > > Dietmar From tonycamgar at gmail.com Thu Jan 5 07:46:26 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Fri, 06 Jan 2017 00:46:26 +1200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I Message-ID: <3454157350@f38.n261.z1.binkp.net> On Thursday, January 5, 2017 at 12:32:19 PM UTC-8, fpp wrote: > > On Thu, Jan 5, 2017 at 12:12 PM, Chris Clark > > wrote: > >> I want an IDE that I can use at work and home, linux and dare I say > >> windows. > >> Sublime, had to remove it from my work PC as it is not licensed. > >> Atom, loved it until it slowed down. > >> VIM, ok the best if you know vi inside out. > >> Any JAVA based IDE, just slows up on work PC's due to all the > >> background stuff that corporates insist they run. > >> Why can not someone more clever than I fork DrPython and bring it up > >> to date. > >> Its is fast, looks great and just does the job ? > > I'm suprised no one in this rich thread has even mentioned SciTE : > http://www.scintilla.org/ > > Admittedly it's closer to an excellent code editor than a full-blown IDE. > But it's very lightweight and fast, cross-platform, has superb syntax > coloring and UTF8 handling, and is highly configurable through its > configuration file(s) and embedded LUA scripting. > It's also well maintained : version 1.0 came out in 1999, and the latest > (3.7.2) is just a week old... > > Its IDE side consists mostly of hotkeys to run the interpreter or > compiler for the language you're editing, with the file in the current > tab. > A side pane shows the output (prints, exceptions, errors etc.) of the > running script. > A nice touch is that it understands these error messages and makes them > clickable, taking you to the tab/module/line where the error occurred. > Also, it can save its current tabs (and their state) to a "session" file > for later reloading, which is close to the idea of a "project" in most > IDEs. > Oh, and it had multi-selection and multi-editing before most of the new > IDEs out there :-) > > Personally that's about all I need for my Python activities, but it can > be customized much further than I have done : there are "hooks" for other > external programs than compilers/interpreters, so you can also run a > linter, debugger or cvs from the editor. > > One word of warning: unlike most newer IDEs which tend to be shiny-shiny > and ful of bells and whistles at first sight, out of the box SciTE is > *extremely* plain looking (you could even say drab, or ugly :-). > It is up to you to decide how it should look and what it should do or > not, through the configuration file. > Fortunately the documentation is very thorough, and there are a lot of > examples lying around to be copy/pasted (like a dark theme, LUA scripts > etc.). > > Did I mention it's lightweight ? The archive is about 1.5 MB and it just > needs unzipping, no installation. May be worth a look if you haven't > tried it yet... > fp Interesting thanks for the link. There are a huge diversity when it comes to IDEs/editors. Now I have more than enough options. From cae.mefeagroup at gmail.com Thu Jan 5 07:57:12 2017 From: cae.mefeagroup at gmail.com (Ionut Predoiu) Date: Thu, 5 Jan 2017 04:57:12 -0800 (PST) Subject: Python for WEB-page !? Message-ID: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Good afternoon, I am a beginner in programming language. I want to know what version of Python I must to learn to use, beside of basic language, because I want to integrate in my site 1 page in which users to can made calculus based on my formulas already write behind (the users will only complete some field, and after push "Calculate" button will see the results in form of: table, graphic, and so on ...). Please take into account that behind will be more mathematical equations/formulas, so the speed I think must be take into account. I waiting with higher interest your feedback. Thanks to all members of community for support and advice. Keep in touch. Kind regards. From rustompmody at gmail.com Thu Jan 5 08:43:07 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Thu, 5 Jan 2017 05:43:07 -0800 (PST) Subject: Is there a good process or library for validating changes to XML format? In-Reply-To: <1604a9b9-08ee-40fb-9014-abc2d5d989f1@googlegroups.com> References: <1604a9b9-08ee-40fb-9014-abc2d5d989f1@googlegroups.com> Message-ID: <6364315d-77ea-4136-9072-23cb534b7654@googlegroups.com> On Thursday, January 5, 2017 at 9:02:38 AM UTC+5:30, Sayth Renshaw wrote: > Afternoon > > Is there a good library or way I could use to check that the author of the XML doc I am using doesn't make small changes to structure over releases? > > Not fully over this with XML but thought that XSD may be what I need, if I search "python XSD" I get a main result for PyXB and generateDS (https://pythonhosted.org/generateDS/). > > Both seem to be libraries for generating bindings to structures for parsing so maybe I am searching the wrong thing. What is the right thing to search? > Know v little about this. Some links that you can chase: http://diffxml.sourceforge.net/ http://stackoverflow.com/questions/1871076/are-there-any-free-xml-diff-merge-tools-available And in python https://pypi.python.org/pypi/xmldiff From porton at narod.ru Thu Jan 5 08:53:36 2017 From: porton at narod.ru (Victor Porton) Date: Fri, 06 Jan 2017 01:53:36 +1200 Subject: Python for WEB-page !? References: <604212044@f38.n261.z1.binkp.net> Message-ID: <1795172493@f38.n261.z1.binkp.net> Ionut Predoiu wrote: > I am a beginner in programming language. > I want to know what version of Python I must to learn to use, beside of > basic language, because I want to integrate in my site 1 page in which > users to can made calculus based on my formulas already write behind (the > users will only complete some field, and after push "Calculate" button > will see the results in form of: table, graphic, and so on ...). Please > take into account that behind will be more mathematical > equations/formulas, so the speed I think must be take into account. Consider PyPi. I never used it, but they say, it is faster than usual CPython interpreter. > I waiting with higher interest your feedback. > > Thanks to all members of community for support and advice. > Keep in touch. > Kind regards. -- Victor Porton - http://portonvictor.org From rosuav at gmail.com Thu Jan 5 10:18:12 2017 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 6 Jan 2017 02:18:12 +1100 Subject: Python for WEB-page !? In-Reply-To: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: On Thu, Jan 5, 2017 at 11:57 PM, Ionut Predoiu wrote: > I am a beginner in programming language. > I want to know what version of Python I must to learn to use, beside of basic language, because I want to integrate in my site 1 page in which users to can made calculus based on my formulas already write behind (the users will only complete some field, and after push "Calculate" button will see the results in form of: table, graphic, and so on ...). > Please take into account that behind will be more mathematical equations/formulas, so the speed I think must be take into account. > Building a web site can be done with Flask, Django, aiohttp, or a number of other frameworks. Generating graphs can be done with matplotlib. Speed won't be a problem - a computer can do a LOT of mathematical calculations in the time it takes to request something over the internet. Have fun exploring! ChrisA From python at deborahswanson.net Thu Jan 5 10:20:26 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 06 Jan 2017 03:20:26 +1200 Subject: Python for WEB-page !? Message-ID: <3581984281@f38.n261.z1.binkp.net> Ionut Predoiu wrote, on January 05, 2017 11:07 PM > > Good morning, > > Thanks to all for feedback and advice. > Because I am a beginner I will read more about versions of > Python recommended by you. > > On the other side I am interested to know if exist some sites > which have develop platform where can be use for free Python > from browsers, without have it installed on PC/laptop. As > beginner I want to practice from everywhere. There's a website called Python Tutor where you can write Python programs and it will show you the structures built in memory as it executes. It's very useful for seeing how recursive functions work, or any function or class you call. It will also work for simple programs and it has an output console you can print to. (Well, it's more like a print window, but it works.) Very good for beginners, I used it all the time when I was first learning, and I still use it for recursive functions, since PyCharm doesn't step through recursion in a clear way. http://pythontutor.com/ > I waiting with higher interest your feedback. > > Thanks to all members of community for support and advice. > Keep in touch. > Kind regards. > > > > On Thursday, January 5, 2017 at 2:57:23 PM UTC+2, Ionut Predoiu wrote: > > Good afternoon, > > > > I am a beginner in programming language. > > I want to know what version of Python I must to learn to > use, beside of basic language, because I want to integrate in > my site 1 page in which users to can made calculus based on > my formulas already write behind (the users will only > complete some field, and after push "Calculate" button will > see the results in form of: table, graphic, and so on ...). > > Please take into account that behind will be more > mathematical equations/formulas, so the speed I think must be > take into account. > > > > I waiting with higher interest your feedback. > > > > Thanks to all members of community for support and advice. Keep in > > touch. Kind regards. > > -- > https://mail.python.org/mailman/listinfo/python-list > From uri at speedy.net Thu Jan 5 10:25:48 2017 From: uri at speedy.net (Uri Even-Chen) Date: Thu, 5 Jan 2017 17:25:48 +0200 Subject: Python for WEB-page !? In-Reply-To: References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: I recommend starting with Python 3, I also use it (3.5.2) for my Django projects - Speedy Net and Speedy Match. Uri. *Uri Even-Chen* [image: photo] Phone: +972-54-3995700 Email: uri at speedy.net Website: http://www.speedysoftware.com/uri/en/ On Thu, Jan 5, 2017 at 5:18 PM, Chris Angelico wrote: > On Thu, Jan 5, 2017 at 11:57 PM, Ionut Predoiu > wrote: > > I am a beginner in programming language. > > I want to know what version of Python I must to learn to use, beside of > basic language, because I want to integrate in my site 1 page in which > users to can made calculus based on my formulas already write behind (the > users will only complete some field, and after push "Calculate" button will > see the results in form of: table, graphic, and so on ...). > > Please take into account that behind will be more mathematical > equations/formulas, so the speed I think must be take into account. > > > > Building a web site can be done with Flask, Django, aiohttp, or a > number of other frameworks. Generating graphs can be done with > matplotlib. Speed won't be a problem - a computer can do a LOT of > mathematical calculations in the time it takes to request something > over the internet. > > Have fun exploring! > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > From python at deborahswanson.net Thu Jan 5 10:28:12 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 06 Jan 2017 03:28:12 +1200 Subject: Receiving a lot of double messages. Message-ID: <4289917042@f38.n261.z1.binkp.net> Antoon Pardon wrote, on January 06, 2017 2:11 AM > > Is there something going on with the mailinglist? Because I > have receive a lot of double messages. One copy is fairly > normal and is part of the discussion thread, the other is > completely seperated. -- Antoon Pardon. Looks to me like the mail server got backed up or jammed and they're flushing the mail queue. I haven't seen anything older than Jan. 2 and just started seeing some current ones, so I think it's almost over with. From eryksun at gmail.com Thu Jan 5 10:54:42 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 06 Jan 2017 03:54:42 +1200 Subject: Forcing prompt to be on newline when embedding Python with Message-ID: <3051026109@f38.n261.z1.binkp.net> On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan wrote: > I tried replacing sys.displayhook with a function that does not print > newline but the newline still got inserted. So, I am not sure where the > newline is coming from. In any case, I could override sys.displayhook to add > a newline at the end and that seems to resolve my problem. In Python 2 the newline is written depending on the value of sys.stdout.softspace. sys.displayhook initially calls Py_FlushLine, which resets the file's softspace to 0 via PyFile_SoftSpace and writes a newline if the previous value was non-zero. Next displayhook writes the repr, sets the softspace to 1 and calls Py_FlushLine again. The result you're seeing could occur if your filelike object doesn't have a dict or a property to allow setting the "softspace" attribute, as the following toy example demonstrates: import sys class File(object): def __init__(self, file): self._file = file self._sp_enabled = True self.softspace = 0 def write(self, string): return self._file.write(string) def __getattribute__(self, name): value = object.__getattribute__(self, name) if name == 'softspace': if not self._sp_enabled: raise AttributeError self._file.write('[get softspace <- %d]\n' % value) return value def __setattr__(self, name, value): if name == 'softspace': if not self._sp_enabled: raise AttributeError self._file.write('[set softspace -> %d]\n' % value) object.__setattr__(self, name, value) softspace enabled: >>> sys.stdout = File(sys.stdout) [set softspace -> 0] [get softspace <- 0] [set softspace -> 0] >>> 42 [get softspace <- 0] [set softspace -> 0] 42[get softspace <- 0] [set softspace -> 1] [get softspace <- 1] [set softspace -> 0] [get softspace <- 0] [set softspace -> 0] softspace disabled: >>> sys.stdout._sp_enabled = False >>> 42 42>>> 42 42>>> From python at rgbaz.eu Thu Jan 5 12:32:33 2017 From: python at rgbaz.eu (ArnoB) Date: Thu, 5 Jan 2017 18:32:33 +0100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> Message-ID: <3dfabd5d-3a45-9fff-4053-fb40e8617154@rgbaz.eu> On 02-01-17 12:38, Antonio Caminero Garcia wrote: > Hello, I am having a hard time deciding what IDE or IDE-like code editor should I use. This can be overwhelming. > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, IntelliJ with Python plugin. > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. My screen should be mostly ?made of code? as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool and python oriented. > > The problem with Vim is the learning curve, so I know the very basic stuff, but obviously not enough for coding and I do not have time to learn it, it is a pity because there are awesome plugins that turns Vim into a lightweight powerful IDE-like. So now it is not an option but I will reconsider it in the future, learning little by little. Also, I am not very fan GUI guy if the task can be accomplished through the terminal. However, I don?t understand why people underrate GUIs, that said I normally use shortcuts for the most frequent tasks and when I have to do something that is not that frequent then I do it with the mouse, for the latter case in vim you would need to look for that specific command every time. > > Sublime is my current and preferred code editor. I installed Anaconda, Git integration and a couple of additional plugins that make sublime very powerful. Also, what I like about sublime compared to the full featured IDEs, besides the minimalism, is how you can perform code navigation back and forth so fast, I mean this is something that you can also do with the others but for some subjective reason I specifically love how sublime does it. The code completion in sublime I do not find it very intelligence, the SublimeCodeIntel is better than the one that Anaconda uses but the completions are not as verbose as in the IDEs. > > Now, I am thinking about giving a try to Visual Studio Code Edition (take a look, it sounds good https://marketplace.visualstudio.com/items?itemName=donjayamanne.python). I need an editor for professional software development. What would you recommend to me? Hi Antonio, Just an extra one in case you'll ever want to create a nice GUI, then there's also QT Creator: https://wiki.qt.io/QtCreator_and_PySide A very simple but powerful interface a la XCode... It integrates nicely with PySide: https://wiki.qt.io/QtCreator_and_PySide gr Arno From chris.p.clark at ba.com Thu Jan 5 13:12:14 2017 From: chris.p.clark at ba.com (Chris Clark) Date: Thu, 5 Jan 2017 18:12:14 +0000 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: <3dfabd5d-3a45-9fff-4053-fb40e8617154@rgbaz.eu> References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com>, <3dfabd5d-3a45-9fff-4053-fb40e8617154@rgbaz.eu> Message-ID: I want an IDE that I can use at work and home, linux and dare I say windows. Sublime, had to remove it from my work PC as it is not licensed. Atom, loved it until it slowed down. VIM, ok the best if you know vi inside out. Any JAVA based IDE, just slows up on work PC's due to all the background stuff that corporates insist they run. Why can not someone more clever than I fork DrPython and bring it up to date. Its is fast, looks great and just does the job ? Its wx, no idea if that is good or bad but it just works. ________________________________ From: Python-list on behalf of ArnoB Sent: 05 January 2017 17:32:33 To: python-list at python.org Subject: Re: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. On 02-01-17 12:38, Antonio Caminero Garcia wrote: > Hello, I am having a hard time deciding what IDE or IDE-like code editor should I use. This can be overwhelming. > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, IntelliJ with Python plugin. > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. My screen should be mostly ?made of code? as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool and python oriented. > > The problem with Vim is the learning curve, so I know the very basic stuff, but obviously not enough for coding and I do not have time to learn it, it is a pity because there are awesome plugins that turns Vim into a lightweight powerful IDE-like. So now it is not an option but I will reconsider it in the future, learning little by little. Also, I am not very fan GUI guy if the task can be accomplished through the terminal. However, I don?t understand why people underrate GUIs, that said I normally use shortcuts for the most frequent tasks and when I have to do something that is not that frequent then I do it with the mouse, for the latter case in vim you would need to look for that specific command every time. > > Sublime is my current and preferred code editor. I installed Anaconda, Git integration and a couple of additional plugins that make sublime very powerful. Also, what I like about sublime compared to the full featured IDEs, besides the minimalism, is how you can perform code navigation back and forth so fast, I mean this is something that you can also do with the others but for some subjective reason I specifically love how sublime does it. The code completion in sublime I do not find it very intelligence, the SublimeCodeIntel is better than the one that Anaconda uses but the completions are not as verbose as in the IDEs. > > Now, I am thinking about giving a try to Visual Studio Code Edition (take a look, it sounds good https://marketplace.visualstudio.com/items?itemName=donjayamanne.python). I need an editor for professional software development. What would you recommend to me? Hi Antonio, Just an extra one in case you'll ever want to create a nice GUI, then there's also QT Creator: https://wiki.qt.io/QtCreator_and_PySide A very simple but powerful interface a la XCode... It integrates nicely with PySide: https://wiki.qt.io/QtCreator_and_PySide gr Arno -- https://mail.python.org/mailman/listinfo/python-list This message is private and confidential and may also be legally privileged. If you have received this message in error, please email it back to the sender and immediately permanently delete it from your computer system. Please do not read, print, re-transmit, store or act in reliance on it or any attachments. British Airways may monitor email traffic data and also the content of emails, where permitted by law, for the purposes of security and staff training and in order to prevent or detect unauthorised use of the British Airways email system. Virus checking of emails (including attachments) is the responsibility of the recipient. British Airways Plc is a public limited company registered in England and Wales. Registered number: 1777777. Registered office: Waterside, PO Box 365, Harmondsworth, West Drayton, Middlesex, England, UB7 0GB. Additional terms and conditions are available on our website: www.ba.com From jonas at wielicki.name Thu Jan 5 13:17:57 2017 From: jonas at wielicki.name (Jonas Wielicki) Date: Thu, 05 Jan 2017 19:17:57 +0100 Subject: [ANN] aioxmpp 0.7.2 released Message-ID: <4553913.7XCvY1p1dk@sinistra> Dear subscribers, I am pleased to announce the release of aioxmpp 0.7.1 (footnote 1). The current release can be obtained from GitHub [1] (check out the v0.6.0 tag or the master branch) or PyPI [2]. The HTML documentation can be found at [3]. Examples can be found in the GitHub repository, in the examples subdirectory. aioxmpp is a Python library based on asyncio. It implements the client side of the XMPP protocol (RFC 6120 and others). For a more detailed description of the package, please review the README of the package on GitHub [1] or on PyPI [2]. For more information on XMPP, please see [8]. It is licensed under the terms of the GNU Lesser General Public License Version 3.0 or later. Version 0.7.2 is a bugfix release. The most severe issue which has been fixed is that message stanzas without from attribute caused the stream to crash. Other bugfixes included are listed in the changelog [9]. Bugs, feature requests, patches and questions can be directed to either the aioxmpp mailing list [4], the GitHub issue tracker [5] or the XMPP Multi-User chat [6], whatever floats your boat. Please direct security-relevant issue reports directly to me (jonas at wielicki.name), preferrably encrypted using my GPG public key [7]. best regards and happy-asyncio-ing, Jonas Wielicki footnote 1: 0.7.0 and 0.7.1 are functionally identical. I messed up the 0.7.0 release by forgetting to change the license in the setup.py to LGPLv3+, which made PyPI show the incorrect GPLv3 license. [1]: https://github.com/horazont/aioxmpp [2]: https://pypi.python.org/pypi/aioxmpp [3]: https://docs.zombofant.net/aioxmpp/0.7/ [4]: https://lists.zombofant.net/mailman/listinfo/aioxmpp-devel [5]: https://github.com/horazont/aioxmpp/issues [6]: aioxmpp at conference.zombofant.net [7]: https://sks-keyservers.net/pks/lookup?op=get&search=0xE5EDE5AC679E300F AA5A 78FF 508D 8CF4 F355 F682 E5ED E5AC 679E 300F [8]: https://xmpp.org/ [9]: https://docs.zombofant.net/aioxmpp/0.7/api/ changelog.html#version-0-7-2 From hetchkay at gmail.com Thu Jan 5 13:36:48 2017 From: hetchkay at gmail.com (H Krishnan) Date: Fri, 06 Jan 2017 06:36:48 +1200 Subject: Forcing prompt to be on newline when embedding Python with Message-ID: <595980780@f38.n261.z1.binkp.net> Thanks for your help. > > > > > I am working on embedding Python in my application. > > You forgot to tell us the version of Python that you're embedding. > > I am using Python2.7. > > I have redirected sys.stdin and sys.stdout to call methods from a Qt > TextEdit > > widget. Everything works fine except that the Python prompt does not > always > > come in a new line: > > > >>>> dir() > > ['__builtins__', '__doc__', '__name__', '__package__']>>> > > > > Why doesn't the prompt appear in a new line as with the default stdout? > > Are you using code.InteractiveConsole / code.interact? > > I am using code.InteractiveConsole().interact(). > If not, in what mode do you compile, Py_file_input ("exec") or > Py_single_input ("single")? The latter executes PRINT_EXPR: > > >>> dis.dis(compile('1', '', 'single')) > 1 0 LOAD_CONST 0 (1) > 3 PRINT_EXPR > 4 LOAD_CONST 1 (None) > 7 RETURN_VALUE > > PRINT_EXPR in turn calls sys.displayhook on the value it pops from the > stack. The default hook writes the repr of the value and a newline to > sys.stdout, and it also references the value as "_" in the builtins > module (2.x __builtin__). > I tried replacing sys.displayhook with a function that does not print newline but the newline still got inserted. So, I am not sure where the newline is coming from. In any case, I could override sys.displayhook to add a newline at the end and that seems to resolve my problem. > -- > https://mail.python.org/mailman/listinfo/python-list > Thanks, Krishnan From marko at pacujo.net Thu Jan 5 13:46:54 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Thu, 05 Jan 2017 20:46:54 +0200 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <3dfabd5d-3a45-9fff-4053-fb40e8617154@rgbaz.eu> Message-ID: <87fukxtkdd.fsf@elektro.pacujo.net> Chris Clark : > I want an IDE that I can use at work and home, linux and dare I say > windows. I use emacs for all of my typing, including Python programming (and making this post). Marko From nathan.ernst at gmail.com Thu Jan 5 13:49:09 2017 From: nathan.ernst at gmail.com (Nathan Ernst) Date: Thu, 5 Jan 2017 12:49:09 -0600 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <3dfabd5d-3a45-9fff-4053-fb40e8617154@rgbaz.eu> Message-ID: Have you looked into Visual Studio Code (https://code.visualstudio.com/)? I've not used it extensively, and only on Windows, but it's an open source IDE originated by MS that purportedly works on Windows, Linux & OS X. It does have pretty decent Python support (haven't tried debugging, but syntax highlighting works well). There's also a really good vim extension available. I tend to prefer vim (or vim extensions) when I can because even though I probably know less than 10% of vim's capabilities, I'm more productive using it. I also use the VsVim extension in full proper Visual Studio. Regards, Nate On Thu, Jan 5, 2017 at 12:12 PM, Chris Clark wrote: > I want an IDE that I can use at work and home, linux and dare I say > windows. > > Sublime, had to remove it from my work PC as it is not licensed. > > Atom, loved it until it slowed down. > > VIM, ok the best if you know vi inside out. > > Any JAVA based IDE, just slows up on work PC's due to all the background > stuff that corporates insist they run. > > Why can not someone more clever than I fork DrPython and bring it up to > date. > > Its is fast, looks great and just does the job ? > > Its wx, no idea if that is good or bad but it just works. > > > ________________________________ > From: Python-list > on behalf of ArnoB > Sent: 05 January 2017 17:32:33 > To: python-list at python.org > Subject: Re: Choosing a Python IDE. what is your Pythonish recommendation? > I do not know what to choose. > > > > On 02-01-17 12:38, Antonio Caminero Garcia wrote: > > Hello, I am having a hard time deciding what IDE or IDE-like code editor > should I use. This can be overwhelming. > > > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, > IntelliJ with Python plugin. > > > > The thing with the from-the-scratch full featured IDEs (Eclipse, > IntelliJ, Pycharm) is that they look like a space craft dashboard and that > unwarranted resources consumption and the unnecessary icons. I want my IDE > to be minimalistic but powerful. My screen should be mostly ?made of code? > as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool > and python oriented. > > > > The problem with Vim is the learning curve, so I know the very basic > stuff, but obviously not enough for coding and I do not have time to learn > it, it is a pity because there are awesome plugins that turns Vim into a > lightweight powerful IDE-like. So now it is not an option but I will > reconsider it in the future, learning little by little. Also, I am not very > fan GUI guy if the task can be accomplished through the terminal. However, > I don?t understand why people underrate GUIs, that said I normally use > shortcuts for the most frequent tasks and when I have to do something that > is not that frequent then I do it with the mouse, for the latter case in > vim you would need to look for that specific command every time. > > > > Sublime is my current and preferred code editor. I installed Anaconda, > Git integration and a couple of additional plugins that make sublime very > powerful. Also, what I like about sublime compared to the full featured > IDEs, besides the minimalism, is how you can perform code navigation back > and forth so fast, I mean this is something that you can also do with the > others but for some subjective reason I specifically love how sublime does > it. The code completion in sublime I do not find it very intelligence, the > SublimeCodeIntel is better than the one that Anaconda uses but the > completions are not as verbose as in the IDEs. > > > > Now, I am thinking about giving a try to Visual Studio Code Edition > (take a look, it sounds good https://marketplace.visualstudio.com/items? > itemName=donjayamanne.python). I need an editor for professional software > development. What would you recommend to me? > > Hi Antonio, > > Just an extra one in case you'll ever want to create > a nice GUI, then there's also QT Creator: > https://wiki.qt.io/QtCreator_and_PySide > > A very simple but powerful interface a la XCode... > > It integrates nicely with PySide: > https://wiki.qt.io/QtCreator_and_PySide > > gr > Arno > > -- > https://mail.python.org/mailman/listinfo/python-list > This message is private and confidential and may also be legally > privileged. If you have received this message in error, please email it > back to the sender and immediately permanently delete it from your computer > system. Please do not read, print, re-transmit, store or act in reliance on > it or any attachments. British Airways may monitor email traffic data and > also the content of emails, where permitted by law, for the purposes of > security and staff training and in order to prevent or detect unauthorised > use of the British Airways email system. Virus checking of emails > (including attachments) is the responsibility of the recipient. British > Airways Plc is a public limited company registered in England and Wales. > Registered number: 1777777. Registered office: Waterside, PO Box 365, > Harmondsworth, West Drayton, Middlesex, England, UB7 0GB. Additional terms > and conditions are available on our website: www.ba.com > -- > https://mail.python.org/mailman/listinfo/python-list > From nntp.fpp at spamgourmet.com Thu Jan 5 15:32:00 2017 From: nntp.fpp at spamgourmet.com (fpp) Date: 05 Jan 2017 20:32:00 GMT Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <3dfabd5d-3a45-9fff-4053-fb40e8617154@rgbaz.eu> Message-ID: > On Thu, Jan 5, 2017 at 12:12 PM, Chris Clark > wrote: >> I want an IDE that I can use at work and home, linux and dare I say >> windows. >> Sublime, had to remove it from my work PC as it is not licensed. >> Atom, loved it until it slowed down. >> VIM, ok the best if you know vi inside out. >> Any JAVA based IDE, just slows up on work PC's due to all the >> background stuff that corporates insist they run. >> Why can not someone more clever than I fork DrPython and bring it up >> to date. >> Its is fast, looks great and just does the job ? I'm suprised no one in this rich thread has even mentioned SciTE : http://www.scintilla.org/ Admittedly it's closer to an excellent code editor than a full-blown IDE. But it's very lightweight and fast, cross-platform, has superb syntax coloring and UTF8 handling, and is highly configurable through its configuration file(s) and embedded LUA scripting. It's also well maintained : version 1.0 came out in 1999, and the latest (3.7.2) is just a week old... Its IDE side consists mostly of hotkeys to run the interpreter or compiler for the language you're editing, with the file in the current tab. A side pane shows the output (prints, exceptions, errors etc.) of the running script. A nice touch is that it understands these error messages and makes them clickable, taking you to the tab/module/line where the error occurred. Also, it can save its current tabs (and their state) to a "session" file for later reloading, which is close to the idea of a "project" in most IDEs. Oh, and it had multi-selection and multi-editing before most of the new IDEs out there :-) Personally that's about all I need for my Python activities, but it can be customized much further than I have done : there are "hooks" for other external programs than compilers/interpreters, so you can also run a linter, debugger or cvs from the editor. One word of warning: unlike most newer IDEs which tend to be shiny-shiny and ful of bells and whistles at first sight, out of the box SciTE is *extremely* plain looking (you could even say drab, or ugly :-). It is up to you to decide how it should look and what it should do or not, through the configuration file. Fortunately the documentation is very thorough, and there are a lot of examples lying around to be copy/pasted (like a dark theme, LUA scripts etc.). Did I mention it's lightweight ? The archive is about 1.5 MB and it just needs unzipping, no installation. May be worth a look if you haven't tried it yet... fp From python at deborahswanson.net Thu Jan 5 17:29:00 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Thu, 5 Jan 2017 14:29:00 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <002301d267a3$1d2e27e0$27b23dae@sambora> Terry Reedy wrote, on January 04, 2017 10:18 PM > > On 1/5/2017 12:11 AM, Deborah Swanson wrote: > > Terry Reedy wrote, on January 04, 2017 3:58 PM > > >> To have a string interpreted as a clickable link, you send the string to > >> software capable of creating a clickable link, plus the information > >> 'this is a clickable link'*. There are two ways to tag a string as a > >> link. One is to use markup around the url in the string itself. > >> '' and html are example. Python provides multiple to make this > >> easy. The other is to tag the string with a separate argument. > >> Python provides tkinter, which wraps tk Text widgets, which have a > >> powerful tag system. One can define a Link tag that will a) cause text > >> to be displayed, for instance, blue and underlined and b) cause clicks on > >> the text to generate a web request. One could then use > >> mytext.insert('insert', 'http://www.example.com', Link) Browser > >> must do something similar when they encounter when they encounter > >> html link tags. > > > > I've actually moved on from my original question to one of opening a > > url in a browser with python, which seems to be a much more easily > > achieved goal. > > > But someone else mentioned tkinter, and I looked at it awhile ago but > > haven't used it for anything. That really could be the way to go if > > you want to make clickable links, although you still need some kind of > > internet engine to open the url in a browser. > > IDLE allows a user to add help menu entries that, when clicked on, open > either a local file or an internet url. For instance, adding the pair > 'Pillow' and "https://pillow.readthedocs.io/en/latest/" in > the Settings > dialog adda "Pillow" to the help menu (after the standard stuff). > Clicking on Help => Pillow opens > "https://pillow.readthedocs.io/en/latest/" in the default browswer. > IDLE just used the webbrowser module to do this. No use re-inventing > the wheel. If instead "Pillow" were a link in text, the click handler > should do something similar. Yes, unless someone suggests something better, the webbrowser module looks like the way to go for opening urls in a browser. > > > You say, "There are two ways to tag a string as a link. One is to use > > markup around the url in the string itself. '' and html are > > examples. Python provides multiple ways to make this easy." > > > > Can you tell me where I'd begin to look for these? Are they in the > > core language, or in packages? > > I was referring to using either % or .format string formatting. Both > are in the core and described somewhere in the Library manual. '%' > should be in the Symbols page of the Index and 'format' on > the 'F' page. > > -- > Terry Jan Reedy I looked up % in the Symbols page, but I didn't see any specifier related to urls. It would be nice if there was something like a %u for url format, but it isn't in there. I also tried print("http//python.org") ^ but got 'SyntaxError: invalid syntax', with the red arrow pointing at the first angle bracket. I also tried print("http//python.org") ^ and got the same syntax error, but I'm not sure if that's how you meant html should be used. I also tried to look up 'format', but there's no such entry in the Index. There are a lot of entries that begin with 'format', but none of them mention urls or anything link related. 'format_field() (string.Formatter method)' looked like a possibility, but again, I didn't see anything to format a link with. Maybe I didn't look hard enough, or didn't see something that _would_ work. As I've said, at this point I've moved on to directly opening a url in a browser with the webbrowser module. All I originally wanted to do was be able to open a url without leaving my IDE while I was debugging data that has urls in it. Clickable links are really just eye candy that I don't need if I can get the same result programmatically. But I was curious whether there are any ways to tag a string as a link in a print statement. If there are, I didn't find any, but thanks for your suggestions! Deborah From rosuav at gmail.com Thu Jan 5 17:46:16 2017 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 06 Jan 2017 10:46:16 +1200 Subject: MySQL schema sync or diff Message-ID: <251176773@f38.n261.z1.binkp.net> On Thu, Jan 5, 2017 at 11:02 AM, Charles Heizer wrote: > I have a MySQL database that is not managed (yet) and I would like to get an output or diff against my new model file. I'm using flask-sqlalchemy. > > Are there any modules that would help me discover the differences so that I can script a migration to begin using flask-migrate? I'm not specifically aware of any such tool per se, but what you may want to consider is a tool for generating models from existing tables. Then you could diff the generated models against your hand-made ones, and build your migrations from that. Expect a ton of noise, though. ChrisA From irmen.NOSPAM at xs4all.nl Thu Jan 5 17:49:35 2017 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Thu, 5 Jan 2017 23:49:35 +0100 Subject: Work between multiple processes In-Reply-To: <892336f7-0adb-431b-aed0-b871d14ac16c@googlegroups.com> References: <892336f7-0adb-431b-aed0-b871d14ac16c@googlegroups.com> Message-ID: <586ecd7d$0$21507$e4fe514c@news.xs4all.nl> On 4-1-2017 23:14, zxpatric at gmail.com wrote: > Hi everyone, > > I ran into a case that I need to create a work process of an application (Jython so has to call using java.exe) which will collect the data based on what main process indicates. > > (1) I tried multiprocessing package, no luck. Java.exe can't be called from Process class? > > (2) I tried subprocess. subprocess.communicate function will wait for the work process to terminate so to return. > > > either (1) or (2) doesn't work out well. Please suggest. Global system queue? > > Thanks, > Patrick. > Is it a requirement that the workdf process is also Jython? If not: you could spawn a Python subprocess that hosts a Pyro4 daemon. Utilizing the Pyrolite java client library you can call methods in it from the java/jython side. (Unfortunately it is not yet possible (due to jython incompatibilities) to use the full Pyro4 library on the Jython side as well). Not sure if it meets your set of requirements but have a look at http://pythonhosted.org/Pyro4/ http://pythonhosted.org/Pyro4/pyrolite.html Irmen From python at deborahswanson.net Thu Jan 5 17:53:02 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Thu, 5 Jan 2017 14:53:02 -0800 Subject: Clickable hyperlinks In-Reply-To: <763d0613-7c39-d540-5b1d-c2e925638c57@kynesim.co.uk> Message-ID: <002801d267a6$78ade4e0$27b23dae@sambora> Rhodri James wrote, on January 05, 2017 3:53 AM > > On 05/01/17 04:52, Deborah Swanson wrote: > > My original question was in fact whether there was a way to make > > clickable hyperlinks in a console. I was persuaded after about 10 > > replies that the answer was no, > > Then you were persuaded wrong; the actual answer was "this isn't a > meaningful question since it's based on incorrect assumptions." > Translating that to "No" is just as much a mistake as > translating it to > "Yes." > > -- > Rhodri James *-* Kynesim Ltd Actually, your statement "this isn't a meaningful question since it's based on incorrect assumptions" is false. PyCharm outputs clickable links to the console, but they aren't web links, they simply link to lines of code. I'd seen that PyCharm can make links in the console that aren't web enabled, so it seemed, and in fact it is, reasonable to assume that it could be done for urls. I just wanted to know if anyone knew how to do it. Granted, the suggestion to use tkinter to enable the links came up much later than in the first 10 or so replies, and since tkinter makes clickable links possible, that's another reason my question wasn't based on false assumptions. It simply appears that the early responders to my question went off on a tangent of what is or is not technologically possible, and all of the approaches under consideration were in fact dead ends. But clickable links turns out to be just eye candy, and the real result I wanted, which is opening urls in a browser from my IDE, is much more quickly and easily done programmatically. Although I didn't see this before I asked my question, and only saw it after reading quite a few replies. Perhaps though, I should have said "I was persuaded after about 10 replies that that no one understood what I was asking." But that just seemed plain rude, so I went with "the answer was no". From antoon.pardon at rece.vub.ac.be Thu Jan 5 18:11:06 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 06 Jan 2017 11:11:06 +1200 Subject: Receiving a lot of double messages. Message-ID: <3639766662@f38.n261.z1.binkp.net> Is there something going on with the mailinglist? Because I have receive a lot of double messages. One copy is fairly normal and is part of the discussion thread, the other is completely seperated. -- Antoon Pardon. From rosuav at gmail.com Thu Jan 5 18:46:17 2017 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 6 Jan 2017 10:46:17 +1100 Subject: MySQL schema sync or diff In-Reply-To: <68944b94-64d5-41a2-8d85-9a6471f5caab@googlegroups.com> References: <68944b94-64d5-41a2-8d85-9a6471f5caab@googlegroups.com> Message-ID: On Thu, Jan 5, 2017 at 11:02 AM, Charles Heizer wrote: > I have a MySQL database that is not managed (yet) and I would like to get an output or diff against my new model file. I'm using flask-sqlalchemy. > > Are there any modules that would help me discover the differences so that I can script a migration to begin using flask-migrate? I'm not specifically aware of any such tool per se, but what you may want to consider is a tool for generating models from existing tables. Then you could diff the generated models against your hand-made ones, and build your migrations from that. Expect a ton of noise, though. ChrisA From porton at narod.ru Thu Jan 5 18:53:37 2017 From: porton at narod.ru (Victor Porton) Date: Fri, 06 Jan 2017 01:53:37 +0200 Subject: Python for WEB-page !? References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: Ionut Predoiu wrote: > I am a beginner in programming language. > I want to know what version of Python I must to learn to use, beside of > basic language, because I want to integrate in my site 1 page in which > users to can made calculus based on my formulas already write behind (the > users will only complete some field, and after push "Calculate" button > will see the results in form of: table, graphic, and so on ...). Please > take into account that behind will be more mathematical > equations/formulas, so the speed I think must be take into account. Consider PyPi. I never used it, but they say, it is faster than usual CPython interpreter. > I waiting with higher interest your feedback. > > Thanks to all members of community for support and advice. > Keep in touch. > Kind regards. -- Victor Porton - http://portonvictor.org From eryksun at gmail.com Thu Jan 5 19:00:31 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 6 Jan 2017 00:00:31 +0000 Subject: Forcing prompt to be on newline when embedding Python with stdin/out redirection In-Reply-To: References: Message-ID: On Thu, Jan 5, 2017 at 7:09 AM, H Krishnan wrote: > > I am working on embedding Python in my application. You forgot to tell us the version of Python that you're embedding. > I have redirected sys.stdin and sys.stdout to call methods from a Qt TextEdit > widget. Everything works fine except that the Python prompt does not always > come in a new line: > >>>> dir() > ['__builtins__', '__doc__', '__name__', '__package__']>>> > > Why doesn't the prompt appear in a new line as with the default stdout? Are you using code.InteractiveConsole / code.interact? If not, in what mode do you compile, Py_file_input ("exec") or Py_single_input ("single")? The latter executes PRINT_EXPR: >>> dis.dis(compile('1', '', 'single')) 1 0 LOAD_CONST 0 (1) 3 PRINT_EXPR 4 LOAD_CONST 1 (None) 7 RETURN_VALUE PRINT_EXPR in turn calls sys.displayhook on the value it pops from the stack. The default hook writes the repr of the value and a newline to sys.stdout, and it also references the value as "_" in the builtins module (2.x __builtin__). From iranna.gani28 at gmail.com Thu Jan 5 19:00:32 2017 From: iranna.gani28 at gmail.com (Iranna Mathapati) Date: Fri, 06 Jan 2017 12:00:32 +1200 Subject: Pexpect Message-ID: <1324722496@f38.n261.z1.binkp.net> Hi Team, How to match latter(caps and small) ,numbers and # symbol in python pexpect. Thanks, Iranna M From breamoreboy at gmail.com Thu Jan 5 19:19:49 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Thu, 5 Jan 2017 16:19:49 -0800 (PST) Subject: Python for WEB-page !? In-Reply-To: References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: <59bd081f-1d98-4d78-83e1-2e0f8df0b553@googlegroups.com> On Thursday, January 5, 2017 at 11:53:51 PM UTC, Victor Porton wrote: > Ionut Predoiu wrote: > > > I am a beginner in programming language. > > I want to know what version of Python I must to learn to use, beside of > > basic language, because I want to integrate in my site 1 page in which > > users to can made calculus based on my formulas already write behind (the > > users will only complete some field, and after push "Calculate" button > > will see the results in form of: table, graphic, and so on ...). Please > > take into account that behind will be more mathematical > > equations/formulas, so the speed I think must be take into account. > > Consider PyPi. I never used it, but they say, it is faster than usual > CPython interpreter. > Really? I'd strongly contrast the Python Package Index https://pypi.python.org/pypi with the Python implementation pypy at http://pypy.org/ :) > -- > Victor Porton - http://portonvictor.org From torriem at gmail.com Thu Jan 5 19:41:49 2017 From: torriem at gmail.com (Michael Torrie) Date: Thu, 5 Jan 2017 17:41:49 -0700 Subject: Python for WEB-page !? In-Reply-To: References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: On 01/05/2017 04:53 PM, Victor Porton wrote: > Ionut Predoiu wrote: > >> I am a beginner in programming language. >> I want to know what version of Python I must to learn to use, beside of >> basic language, because I want to integrate in my site 1 page in which >> users to can made calculus based on my formulas already write behind (the >> users will only complete some field, and after push "Calculate" button >> will see the results in form of: table, graphic, and so on ...). Please >> take into account that behind will be more mathematical >> equations/formulas, so the speed I think must be take into account. > > Consider PyPi. I never used it, but they say, it is faster than usual > CPython interpreter. With respect, I don't think it's appropriate to direct a python beginner to PyPi. Far better to direct him to the relevant resources (like Django) and focus him on the standard Python interpreter, hopefully version 3. Besides that, there's the old expression. Premature optimization is the root of all evil. Until Python is shown to be too slow for a given task, it's premature to think about speedups like Cython or even PyPi. From torriem at gmail.com Thu Jan 5 19:48:30 2017 From: torriem at gmail.com (Michael Torrie) Date: Thu, 5 Jan 2017 17:48:30 -0700 Subject: Python for WEB-page !? In-Reply-To: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: On 01/05/2017 05:57 AM, Ionut Predoiu wrote: > Good afternoon, > > I am a beginner in programming language. I want to know what version > of Python I must to learn to use, beside of basic language, because I > want to integrate in my site 1 page in which users to can made > calculus based on my formulas already write behind (the users will > only complete some field, and after push "Calculate" button will see > the results in form of: table, graphic, and so on ...). Please take > into account that behind will be more mathematical > equations/formulas, so the speed I think must be take into account. While Python can do that, using a web framework to process HTTP requests and generate HTML to display in the browser, I don't believe Python is the appropriate language for the task at hand. Most web sites that do interactive formula calculations like you describe do it all in the browser using Javascript. No need to have a web server do all that heavy lifting at all. A simple html file would contain everything you need. Even if you want to use Python to generate the web page and process events, you'll still have to master Javascript at some point to make the webpages more interactive. From hetchkay at gmail.com Thu Jan 5 20:06:48 2017 From: hetchkay at gmail.com (H Krishnan) Date: Fri, 6 Jan 2017 06:36:48 +0530 Subject: Forcing prompt to be on newline when embedding Python with stdin/out redirection In-Reply-To: References: Message-ID: Thanks for your help. > > > > > I am working on embedding Python in my application. > > You forgot to tell us the version of Python that you're embedding. > > I am using Python2.7. > > I have redirected sys.stdin and sys.stdout to call methods from a Qt > TextEdit > > widget. Everything works fine except that the Python prompt does not > always > > come in a new line: > > > >>>> dir() > > ['__builtins__', '__doc__', '__name__', '__package__']>>> > > > > Why doesn't the prompt appear in a new line as with the default stdout? > > Are you using code.InteractiveConsole / code.interact? > > I am using code.InteractiveConsole().interact(). > If not, in what mode do you compile, Py_file_input ("exec") or > Py_single_input ("single")? The latter executes PRINT_EXPR: > > >>> dis.dis(compile('1', '', 'single')) > 1 0 LOAD_CONST 0 (1) > 3 PRINT_EXPR > 4 LOAD_CONST 1 (None) > 7 RETURN_VALUE > > PRINT_EXPR in turn calls sys.displayhook on the value it pops from the > stack. The default hook writes the repr of the value and a newline to > sys.stdout, and it also references the value as "_" in the builtins > module (2.x __builtin__). > I tried replacing sys.displayhook with a function that does not print newline but the newline still got inserted. So, I am not sure where the newline is coming from. In any case, I could override sys.displayhook to add a newline at the end and that seems to resolve my problem. > -- > https://mail.python.org/mailman/listinfo/python-list > Thanks, Krishnan From hetchkay at gmail.com Thu Jan 5 22:10:18 2017 From: hetchkay at gmail.com (H Krishnan) Date: Fri, 06 Jan 2017 15:10:18 +1200 Subject: Forcing prompt to be on newline when embedding Python with Message-ID: <3419216577@f38.n261.z1.binkp.net> Hello Mr.Eryk, Thanks for the detailed explanation. After I added attribute support to my extension class for stdio, the problem was resolved. Regards, Krishnan On Fri, Jan 6, 2017 at 9:24 AM, eryk sun wrote: > On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan wrote: > > I tried replacing sys.displayhook with a function that does not print > > newline but the newline still got inserted. So, I am not sure where the > > newline is coming from. In any case, I could override sys.displayhook to > add > > a newline at the end and that seems to resolve my problem. > > In Python 2 the newline is written depending on the value of > sys.stdout.softspace. sys.displayhook initially calls Py_FlushLine, > which resets the file's softspace to 0 via PyFile_SoftSpace and writes > a newline if the previous value was non-zero. Next displayhook writes > the repr, sets the softspace to 1 and calls Py_FlushLine again. > > The result you're seeing could occur if your filelike object doesn't > have a dict or a property to allow setting the "softspace" attribute, > as the following toy example demonstrates: > > import sys > > class File(object): > def __init__(self, file): > self._file = file > self._sp_enabled = True > self.softspace = 0 > > def write(self, string): > return self._file.write(string) > > def __getattribute__(self, name): > value = object.__getattribute__(self, name) > if name == 'softspace': > if not self._sp_enabled: > raise AttributeError > self._file.write('[get softspace <- %d]\n' % value) > return value > > def __setattr__(self, name, value): > if name == 'softspace': > if not self._sp_enabled: > raise AttributeError > self._file.write('[set softspace -> %d]\n' % value) > object.__setattr__(self, name, value) > > softspace enabled: > > >>> sys.stdout = File(sys.stdout) > [set softspace -> 0] > [get softspace <- 0] > [set softspace -> 0] > >>> 42 > [get softspace <- 0] > [set softspace -> 0] > 42[get softspace <- 0] > [set softspace -> 1] > [get softspace <- 1] > [set softspace -> 0] > > [get softspace <- 0] > [set softspace -> 0] > > softspace disabled: > > >>> sys.stdout._sp_enabled = False > >>> 42 > 42>>> 42 > 42>>> > -- > https://mail.python.org/mailman/listinfo/python-list > From eryksun at gmail.com Thu Jan 5 22:54:42 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 6 Jan 2017 03:54:42 +0000 Subject: Forcing prompt to be on newline when embedding Python with stdin/out redirection In-Reply-To: References: Message-ID: On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan wrote: > I tried replacing sys.displayhook with a function that does not print > newline but the newline still got inserted. So, I am not sure where the > newline is coming from. In any case, I could override sys.displayhook to add > a newline at the end and that seems to resolve my problem. In Python 2 the newline is written depending on the value of sys.stdout.softspace. sys.displayhook initially calls Py_FlushLine, which resets the file's softspace to 0 via PyFile_SoftSpace and writes a newline if the previous value was non-zero. Next displayhook writes the repr, sets the softspace to 1 and calls Py_FlushLine again. The result you're seeing could occur if your filelike object doesn't have a dict or a property to allow setting the "softspace" attribute, as the following toy example demonstrates: import sys class File(object): def __init__(self, file): self._file = file self._sp_enabled = True self.softspace = 0 def write(self, string): return self._file.write(string) def __getattribute__(self, name): value = object.__getattribute__(self, name) if name == 'softspace': if not self._sp_enabled: raise AttributeError self._file.write('[get softspace <- %d]\n' % value) return value def __setattr__(self, name, value): if name == 'softspace': if not self._sp_enabled: raise AttributeError self._file.write('[set softspace -> %d]\n' % value) object.__setattr__(self, name, value) softspace enabled: >>> sys.stdout = File(sys.stdout) [set softspace -> 0] [get softspace <- 0] [set softspace -> 0] >>> 42 [get softspace <- 0] [set softspace -> 0] 42[get softspace <- 0] [set softspace -> 1] [get softspace <- 1] [set softspace -> 0] [get softspace <- 0] [set softspace -> 0] softspace disabled: >>> sys.stdout._sp_enabled = False >>> 42 42>>> 42 42>>> From iranna.gani28 at gmail.com Fri Jan 6 01:30:33 2017 From: iranna.gani28 at gmail.com (Iranna Mathapati) Date: Fri, 6 Jan 2017 12:00:33 +0530 Subject: Pexpect Message-ID: Hi Team, How to match latter(caps and small) ,numbers and # symbol in python pexpect. Thanks, Iranna M From cae.mefeagroup at gmail.com Fri Jan 6 02:07:29 2017 From: cae.mefeagroup at gmail.com (Ionut Predoiu) Date: Thu, 5 Jan 2017 23:07:29 -0800 (PST) Subject: Python for WEB-page !? In-Reply-To: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: <8e976271-c56b-4ac8-bf59-1fd4dd449ca5@googlegroups.com> Good morning, Thanks to all for feedback and advice. Because I am a beginner I will read more about versions of Python recommended by you. On the other side I am interested to know if exist some sites which have develop platform where can be use for free Python from browsers, without have it installed on PC/laptop. As beginner I want to practice from everywhere. I waiting with higher interest your feedback. Thanks to all members of community for support and advice. Keep in touch. Kind regards. On Thursday, January 5, 2017 at 2:57:23 PM UTC+2, Ionut Predoiu wrote: > Good afternoon, > > I am a beginner in programming language. > I want to know what version of Python I must to learn to use, beside of basic language, because I want to integrate in my site 1 page in which users to can made calculus based on my formulas already write behind (the users will only complete some field, and after push "Calculate" button will see the results in form of: table, graphic, and so on ...). > Please take into account that behind will be more mathematical equations/formulas, so the speed I think must be take into account. > > I waiting with higher interest your feedback. > > Thanks to all members of community for support and advice. > Keep in touch. > Kind regards. From tonycamgar at gmail.com Fri Jan 6 03:13:25 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Fri, 6 Jan 2017 00:13:25 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <3dfabd5d-3a45-9fff-4053-fb40e8617154@rgbaz.eu> Message-ID: On Thursday, January 5, 2017 at 9:51:17 AM UTC-8, ArnoB wrote: > On 02-01-17 12:38, Antonio Caminero Garcia wrote: > > Hello, I am having a hard time deciding what IDE or IDE-like code editor should I use. This can be overwhelming. > > > > So far, I have used Vim, Sublime, Atom, Eclipse with PyDev, Pycharm, IntelliJ with Python plugin. > > > > The thing with the from-the-scratch full featured IDEs (Eclipse, IntelliJ, Pycharm) is that they look like a space craft dashboard and that unwarranted resources consumption and the unnecessary icons. I want my IDE to be minimalistic but powerful. My screen should be mostly ?made of code? as usually happens in Vim, Sublime or Atom. However, Pycharm is really cool and python oriented. > > > > The problem with Vim is the learning curve, so I know the very basic stuff, but obviously not enough for coding and I do not have time to learn it, it is a pity because there are awesome plugins that turns Vim into a lightweight powerful IDE-like. So now it is not an option but I will reconsider it in the future, learning little by little. Also, I am not very fan GUI guy if the task can be accomplished through the terminal. However, I don?t understand why people underrate GUIs, that said I normally use shortcuts for the most frequent tasks and when I have to do something that is not that frequent then I do it with the mouse, for the latter case in vim you would need to look for that specific command every time. > > > > Sublime is my current and preferred code editor. I installed Anaconda, Git integration and a couple of additional plugins that make sublime very powerful. Also, what I like about sublime compared to the full featured IDEs, besides the minimalism, is how you can perform code navigation back and forth so fast, I mean this is something that you can also do with the others but for some subjective reason I specifically love how sublime does it. The code completion in sublime I do not find it very intelligence, the SublimeCodeIntel is better than the one that Anaconda uses but the completions are not as verbose as in the IDEs. > > > > Now, I am thinking about giving a try to Visual Studio Code Edition (take a look, it sounds good https://marketplace.visualstudio.com/items?itemName=donjayamanne.python). I need an editor for professional software development. What would you recommend to me? > > Hi Antonio, > > Just an extra one in case you'll ever want to create > a nice GUI, then there's also QT Creator: > https://wiki.qt.io/QtCreator_and_PySide > > A very simple but powerful interface a la XCode... > > It integrates nicely with PySide: > https://wiki.qt.io/QtCreator_and_PySide > > gr > Arno Thanks! From tonycamgar at gmail.com Fri Jan 6 03:40:16 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Fri, 6 Jan 2017 00:40:16 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> Message-ID: On Wednesday, January 4, 2017 at 1:10:04 PM UTC-8, Dietmar Schwertberger wrote: > On 04.01.2017 07:54, Antonio Caminero Garcia wrote: > > Unfortunately most of the time I am still using print and input functions. I know that sucks, I did not use the pdb module, I guess that IDE debuggers leverage such module. > pdb is actually quite useful. On my Windows PCs I can invoke python on > any .py file with the -i command line switch by right clicking in the > Explorer and selecting "Debug". Now when the script crashes, I can > inspect variables without launching a full-scale IDE or starting the > script from the command line. For such quick fixes I have also a context > menu entry "Edit" for editing with Pythonwin, which is still quite OK as > editor and has no licensing restrictions or installation requirements. > This is a nice option when you deploy your installation to many PCs over > the network. I am on MacOS but interesting way of debugging, I will take the idea. > > For the print functions vs. debugger: > The most useful application for a debugger like Wing is not for > bug-fixing, but to set a break point and then interactively develop on > the debugger console and with the IDE editor's autocompletion using > introspection on the live objects. This is very helpful for hardware > interfacing, network protocols or GUI programs. It really boosted my > productivity in a way I could not believe before. This is something most > people forget when they evaluate programming languages. It's not the > language or syntax that counts, but the overall environment. Probably > the only other really interactive language and environment is Forth. > This is exactly part of the capabilities that I am looking for. I loved you brought that up. When I think of an ideal IDE (besides the desirable features that I already mentioned previously) as a coworker who is telling me the values,types and ids that the objects are getting as you are setting breakpoints. So why not use the debugger interactively to develop applications. As long as one sets the breakpoints in a meaningful way so you can trace your code in a very productive way. Is that what you mean by interactive environment? > > If it happens to be Arduino I normally use a sublime plugin called Stino > > https://github.com/Robot-Will/Stino > > (1337 people starred that cool number :D) > Well, it is CodeWarrior which was quite famous at the time of the 68k Macs. > The company was bought by Motorola and the IDE is still around for > Freescale/NXP/Qualcomm microcontrollers like the HCS08 8 bit series. > Around ten years ago the original CodeWarrior IDE was migrated to > something Eclipse based. > When I last evaluated HCS08 vs. Arduino, the HCS08 won due to the better > debug interface and native USB support. HCS08 is still quite cool, but > when it comes to documentation, learning curve, tools etc. the Arduinos > win.... > > > Regards, > > Dietmar From tonycamgar at gmail.com Fri Jan 6 03:46:27 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Fri, 6 Jan 2017 00:46:27 -0800 (PST) Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <3dfabd5d-3a45-9fff-4053-fb40e8617154@rgbaz.eu> Message-ID: On Thursday, January 5, 2017 at 12:32:19 PM UTC-8, fpp wrote: > > On Thu, Jan 5, 2017 at 12:12 PM, Chris Clark > > wrote: > >> I want an IDE that I can use at work and home, linux and dare I say > >> windows. > >> Sublime, had to remove it from my work PC as it is not licensed. > >> Atom, loved it until it slowed down. > >> VIM, ok the best if you know vi inside out. > >> Any JAVA based IDE, just slows up on work PC's due to all the > >> background stuff that corporates insist they run. > >> Why can not someone more clever than I fork DrPython and bring it up > >> to date. > >> Its is fast, looks great and just does the job ? > > I'm suprised no one in this rich thread has even mentioned SciTE : > http://www.scintilla.org/ > > Admittedly it's closer to an excellent code editor than a full-blown IDE. > But it's very lightweight and fast, cross-platform, has superb syntax > coloring and UTF8 handling, and is highly configurable through its > configuration file(s) and embedded LUA scripting. > It's also well maintained : version 1.0 came out in 1999, and the latest > (3.7.2) is just a week old... > > Its IDE side consists mostly of hotkeys to run the interpreter or > compiler for the language you're editing, with the file in the current > tab. > A side pane shows the output (prints, exceptions, errors etc.) of the > running script. > A nice touch is that it understands these error messages and makes them > clickable, taking you to the tab/module/line where the error occurred. > Also, it can save its current tabs (and their state) to a "session" file > for later reloading, which is close to the idea of a "project" in most > IDEs. > Oh, and it had multi-selection and multi-editing before most of the new > IDEs out there :-) > > Personally that's about all I need for my Python activities, but it can > be customized much further than I have done : there are "hooks" for other > external programs than compilers/interpreters, so you can also run a > linter, debugger or cvs from the editor. > > One word of warning: unlike most newer IDEs which tend to be shiny-shiny > and ful of bells and whistles at first sight, out of the box SciTE is > *extremely* plain looking (you could even say drab, or ugly :-). > It is up to you to decide how it should look and what it should do or > not, through the configuration file. > Fortunately the documentation is very thorough, and there are a lot of > examples lying around to be copy/pasted (like a dark theme, LUA scripts > etc.). > > Did I mention it's lightweight ? The archive is about 1.5 MB and it just > needs unzipping, no installation. May be worth a look if you haven't > tried it yet... > fp Interesting thanks for the link. There are a huge diversity when it comes to IDEs/editors. Now I have more than enough options. From hetchkay at gmail.com Fri Jan 6 04:40:18 2017 From: hetchkay at gmail.com (H Krishnan) Date: Fri, 6 Jan 2017 15:10:18 +0530 Subject: Forcing prompt to be on newline when embedding Python with stdin/out redirection In-Reply-To: References: Message-ID: Hello Mr.Eryk, Thanks for the detailed explanation. After I added attribute support to my extension class for stdio, the problem was resolved. Regards, Krishnan On Fri, Jan 6, 2017 at 9:24 AM, eryk sun wrote: > On Fri, Jan 6, 2017 at 1:06 AM, H Krishnan wrote: > > I tried replacing sys.displayhook with a function that does not print > > newline but the newline still got inserted. So, I am not sure where the > > newline is coming from. In any case, I could override sys.displayhook to > add > > a newline at the end and that seems to resolve my problem. > > In Python 2 the newline is written depending on the value of > sys.stdout.softspace. sys.displayhook initially calls Py_FlushLine, > which resets the file's softspace to 0 via PyFile_SoftSpace and writes > a newline if the previous value was non-zero. Next displayhook writes > the repr, sets the softspace to 1 and calls Py_FlushLine again. > > The result you're seeing could occur if your filelike object doesn't > have a dict or a property to allow setting the "softspace" attribute, > as the following toy example demonstrates: > > import sys > > class File(object): > def __init__(self, file): > self._file = file > self._sp_enabled = True > self.softspace = 0 > > def write(self, string): > return self._file.write(string) > > def __getattribute__(self, name): > value = object.__getattribute__(self, name) > if name == 'softspace': > if not self._sp_enabled: > raise AttributeError > self._file.write('[get softspace <- %d]\n' % value) > return value > > def __setattr__(self, name, value): > if name == 'softspace': > if not self._sp_enabled: > raise AttributeError > self._file.write('[set softspace -> %d]\n' % value) > object.__setattr__(self, name, value) > > softspace enabled: > > >>> sys.stdout = File(sys.stdout) > [set softspace -> 0] > [get softspace <- 0] > [set softspace -> 0] > >>> 42 > [get softspace <- 0] > [set softspace -> 0] > 42[get softspace <- 0] > [set softspace -> 1] > [get softspace <- 1] > [set softspace -> 0] > > [get softspace <- 0] > [set softspace -> 0] > > softspace disabled: > > >>> sys.stdout._sp_enabled = False > >>> 42 > 42>>> 42 > 42>>> > -- > https://mail.python.org/mailman/listinfo/python-list > From antoon.pardon at rece.vub.ac.be Fri Jan 6 05:11:07 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 6 Jan 2017 11:11:07 +0100 Subject: Receiving a lot of double messages. Message-ID: <1974b8a3-7898-1b0a-a9da-5f0b96719df6@rece.vub.ac.be> Is there something going on with the mailinglist? Because I have receive a lot of double messages. One copy is fairly normal and is part of the discussion thread, the other is completely seperated. -- Antoon Pardon. From python at deborahswanson.net Fri Jan 6 06:20:26 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 6 Jan 2017 03:20:26 -0800 Subject: Python for WEB-page !? In-Reply-To: <8e976271-c56b-4ac8-bf59-1fd4dd449ca5@googlegroups.com> Message-ID: <006e01d2680e$e1dc55b0$27b23dae@sambora> Ionut Predoiu wrote, on January 05, 2017 11:07 PM > > Good morning, > > Thanks to all for feedback and advice. > Because I am a beginner I will read more about versions of > Python recommended by you. > > On the other side I am interested to know if exist some sites > which have develop platform where can be use for free Python > from browsers, without have it installed on PC/laptop. As > beginner I want to practice from everywhere. There's a website called Python Tutor where you can write Python programs and it will show you the structures built in memory as it executes. It's very useful for seeing how recursive functions work, or any function or class you call. It will also work for simple programs and it has an output console you can print to. (Well, it's more like a print window, but it works.) Very good for beginners, I used it all the time when I was first learning, and I still use it for recursive functions, since PyCharm doesn't step through recursion in a clear way. http://pythontutor.com/ > I waiting with higher interest your feedback. > > Thanks to all members of community for support and advice. > Keep in touch. > Kind regards. > > > > On Thursday, January 5, 2017 at 2:57:23 PM UTC+2, Ionut Predoiu wrote: > > Good afternoon, > > > > I am a beginner in programming language. > > I want to know what version of Python I must to learn to > use, beside of basic language, because I want to integrate in > my site 1 page in which users to can made calculus based on > my formulas already write behind (the users will only > complete some field, and after push "Calculate" button will > see the results in form of: table, graphic, and so on ...). > > Please take into account that behind will be more > mathematical equations/formulas, so the speed I think must be > take into account. > > > > I waiting with higher interest your feedback. > > > > Thanks to all members of community for support and advice. Keep in > > touch. Kind regards. > > -- > https://mail.python.org/mailman/listinfo/python-list > From python at deborahswanson.net Fri Jan 6 06:28:13 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 6 Jan 2017 03:28:13 -0800 Subject: Receiving a lot of double messages. In-Reply-To: <1974b8a3-7898-1b0a-a9da-5f0b96719df6@rece.vub.ac.be> Message-ID: <006f01d2680f$f804d4b0$27b23dae@sambora> Antoon Pardon wrote, on January 06, 2017 2:11 AM > > Is there something going on with the mailinglist? Because I > have receive a lot of double messages. One copy is fairly > normal and is part of the discussion thread, the other is > completely seperated. -- Antoon Pardon. Looks to me like the mail server got backed up or jammed and they're flushing the mail queue. I haven't seen anything older than Jan. 2 and just started seeing some current ones, so I think it's almost over with. From cae.mefeagroup at gmail.com Fri Jan 6 06:31:18 2017 From: cae.mefeagroup at gmail.com (Ionut Predoiu) Date: Fri, 6 Jan 2017 03:31:18 -0800 (PST) Subject: Python for WEB-page !? In-Reply-To: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: <31394809-e0da-445e-aa9a-30c47ada342e@googlegroups.com> Good afternoon, Thank you for advice and promptitude in answer. Keep in touch for further questions. Kind regards. On Thursday, January 5, 2017 at 2:57:23 PM UTC+2, Ionut Predoiu wrote: > Good afternoon, > > I am a beginner in programming language. > I want to know what version of Python I must to learn to use, beside of basic language, because I want to integrate in my site 1 page in which users to can made calculus based on my formulas already write behind (the users will only complete some field, and after push "Calculate" button will see the results in form of: table, graphic, and so on ...). > Please take into account that behind will be more mathematical equations/formulas, so the speed I think must be take into account. > > I waiting with higher interest your feedback. > > Thanks to all members of community for support and advice. > Keep in touch. > Kind regards. From rosuav at gmail.com Fri Jan 6 06:34:58 2017 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 6 Jan 2017 22:34:58 +1100 Subject: Receiving a lot of double messages. In-Reply-To: <1974b8a3-7898-1b0a-a9da-5f0b96719df6@rece.vub.ac.be> References: <1974b8a3-7898-1b0a-a9da-5f0b96719df6@rece.vub.ac.be> Message-ID: On Fri, Jan 6, 2017 at 9:11 PM, Antoon Pardon wrote: > Is there something going on with the mailinglist? Because I have receive > a lot of double messages. One copy is fairly normal and is part of the > discussion thread, the other is completely seperated. -- Antoon Pardon. Yeah, I'm seeing the same. Is the newsgroup gateway feeding back? ChrisA From Joaquin.Alzola at lebara.com Fri Jan 6 06:37:24 2017 From: Joaquin.Alzola at lebara.com (Joaquin Alzola) Date: Fri, 6 Jan 2017 11:37:24 +0000 Subject: Pexpect In-Reply-To: References: Message-ID: > How to match latter(caps and small) ,numbers and # symbol in python pexpect. With a .* child = pexpect.spawnu("ssh cbpapp@%s"% CBP[cust_cbp_server]) child.setecho(False) child.logfile = open("/opt/webapi/logs/delete_accountID.log", "w") child.expect(".*assword:") child.sendline("XXXX\r") child.expect(".*/onip/app/cbpapp>") child.sendline("mdsql\r") child.expect("SQL>") child.sendline("select * from table; \r") child.expect("SQL>") child.expect("SQL>") ----- 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 maillist at schwertberger.de Fri Jan 6 07:14:52 2017 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Fri, 6 Jan 2017 13:14:52 +0100 Subject: Choosing a Python IDE. what is your Pythonish recommendation? I do not know what to choose. In-Reply-To: References: <73af2d2d-71d5-450d-a8f8-ef69c016ac8f@googlegroups.com> <64837604-9f3a-806c-d631-fcc352738952@schwertberger.de> <04dc51b5-d546-427f-8ad2-d69284682bdf@googlegroups.com> Message-ID: On 06.01.2017 09:40, Antonio Caminero Garcia wrote: > So why not use the debugger interactively to develop > applications. As long as one sets the breakpoints in a meaningful way so you can trace your code in a very productive way. Is that what you mean by interactive environment? Well, not exactly. Maybe have a look at the video "Interactive Debug Probe" at https://wingware.com/wingide/debugger Regards, Dietmar From steve+python at pearwood.info Fri Jan 6 08:03:37 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 07 Jan 2017 00:03:37 +1100 Subject: The hardest problem in computer science... Message-ID: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> The second hardest problem in computer science is cache invalidation. The *hardest* problem is naming things. In a hierarchical tree view widget that displays items like this: Fiction ?? Fantasy ? ?? Terry Pratchett ? ? ?? Discworld ? ? ? ?? Wyrd Sisters ? ? ? ?? Carpe Jugulum ? ? ?? Dodger ? ?? JK Rowling ? ?? Harry Potter And The Philosopher's Stone ?? Science Fiction ? ?? Connie Willis ? ? ?? To Say Nothing Of The Dog ? ?? Neal Stephenson ? ?? Snow Crash ?? Horror ?? Stephen King ?? Salem's Lot ?? It what do we call the vertical and horizontal line elements? I want to make them configurable, which means the user has to be able to pass an argument that specifies them. I have names for the individual components: XXX = namedtuple("XXX", "vline tee corner") default_YYY = XXX("? ", "?? ", "?? ") bold_YYY = XXX("? ", "?? ", "?? ") ascii_YYY = XXX("| ", "|- ", "+- ") def draw_tree(tree, YYY=default_YYY): ... but what do I call XXX and YYY? Seriously-considering-just-hard-coding-them-as-magic-constants-ly y'rs, -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From muhammadasad254 at gmail.com Fri Jan 6 08:13:21 2017 From: muhammadasad254 at gmail.com (muhammadasad254 at gmail.com) Date: Fri, 6 Jan 2017 05:13:21 -0800 (PST) Subject: "paperpk" "paper pk" "paperpk.com" "express newspaper" "newspaper classified ads" "dawn jobs ads" "jang newspaper" "dawn jobs" "jang jobs ads" "nawaiwaqt" "classified ads" on www.npjobs.blogspot.com In-Reply-To: <2d475602-e3b6-4d31-9a3b-55429df58984@y32g2000prd.googlegroups.com> References: <2d475602-e3b6-4d31-9a3b-55429df58984@y32g2000prd.googlegroups.com> Message-ID: <5ec651f9-6be5-45cd-92fe-170d30a2e441@googlegroups.com> this a all of facek you are veiw the latest post today newspaperp on view paperpkads. From dan at tombstonezero.net Fri Jan 6 08:44:28 2017 From: dan at tombstonezero.net (Dan Sommers) Date: Fri, 6 Jan 2017 13:44:28 -0000 (UTC) Subject: The hardest problem in computer science... References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, 07 Jan 2017 00:03:37 +1100, Steve D'Aprano wrote: > The *hardest* problem is naming things. > > Fiction > ?? Fantasy > ? ?? Terry Pratchett > ? ? ?? Discworld > ? ? ? ?? Wyrd Sisters > ? ? ? ?? Carpe Jugulum [...] > what do we call the vertical and horizontal line elements? I want to make > them configurable, which means the user has to be able to pass an argument > that specifies them ... pstree(1) (https://en.wikipedia.org/wiki/Pstree, http://man7.org/linux/man-pages/man1/pstree.1.html) calls them "line drawing characters," and took a slightly different approach (I think) to the command line argument(s): The default is ASCII, or you can specify -G to specify the VT100 line drawing characters (G for Graphic, I assume), or you can specify -U for the UTF-8 line drawing characters. Dan From python.list at tim.thechases.com Fri Jan 6 09:01:28 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 6 Jan 2017 08:01:28 -0600 Subject: The hardest problem in computer science... In-Reply-To: References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20170106080128.6e45f019@bigbox.christie.dr> On 2017-01-06 13:44, Dan Sommers wrote: > On Sat, 07 Jan 2017 00:03:37 +1100, Steve D'Aprano wrote: > > what do we call the vertical and horizontal line elements? I want > > to make them configurable, which means the user has to be able to > > pass an argument that specifies them ... > > pstree(1) calls them "line drawing characters," I second the use of "drawing characters" as I've seen them described as "line-drawing characters", "box drawing characters", or "tree drawing characters" depending on the context. Wikipedia seems to favor "box drawing characters" https://en.wikipedia.org/wiki/Box-drawing_character Takes me back to my ascii-art days on BBSes. :-) -tkc From __peter__ at web.de Fri Jan 6 09:04:02 2017 From: __peter__ at web.de (Peter Otten) Date: Fri, 06 Jan 2017 15:04:02 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found Message-ID: Example: you are looking for the minimum absolute value in a series of integers. As soon as you encounter the first 0 it's unnecessary extra work to check the remaining values, but the builtin min() will continue. The solution is a minimum function that allows the user to specify a stop value: >>> from itertools import count, chain >>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) 0 How would you implement stopmin()? Currently I raise an exception in the key function: class Stop(Exception): pass def stopmin(items, key, stop): """ >>> def g(): ... for i in reversed(range(10)): ... print(10*i) ... yield str(i) >>> stopmin(g(), key=int, stop=5) 90 80 70 60 50 '5' """ def key2(value): result = key(value) if result <= stop: raise Stop(value) return result try: return min(items, key=key2) except Stop as stop: return stop.args[0] From alain at universite-de-strasbourg.fr.invalid Fri Jan 6 09:08:07 2017 From: alain at universite-de-strasbourg.fr.invalid (Alain Ketterlin) Date: Fri, 06 Jan 2017 15:08:07 +0100 Subject: The hardest problem in computer science... References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87y3yo703c.fsf@universite-de-strasbourg.fr.invalid> Steve D'Aprano writes: [...] > Fiction > ?? Fantasy > ? ?? Terry Pratchett > ? ? ?? Discworld > ? ? ? ?? Wyrd Sisters > ? ? ? ?? Carpe Jugulum > ? ? ?? Dodger > ? ?? JK Rowling [...] > what do we call the vertical and horizontal line elements? Box-drawing characters. At least that's how Unicode calls them. Even if you don't draw boxes... https://en.wikipedia.org/wiki/Box-drawing_character -- Alain. From skip.montanaro at gmail.com Fri Jan 6 09:12:10 2017 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Fri, 6 Jan 2017 08:12:10 -0600 Subject: The hardest problem in computer science... In-Reply-To: <20170106080128.6e45f019@bigbox.christie.dr> References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> <20170106080128.6e45f019@bigbox.christie.dr> Message-ID: "VT52 special graphics characters", anyone? Credit where credit is due. Who hasn't borked their output and wound up with their VT(52|100) in graphics mode? :-) https://en.wikipedia.org/wiki/VT52 Skip From ethan at stoneleaf.us Fri Jan 6 10:37:40 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 06 Jan 2017 07:37:40 -0800 Subject: The hardest problem in computer science... In-Reply-To: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> Message-ID: <586FB9C4.6000505@stoneleaf.us> On 01/06/2017 05:03 AM, Steve D'Aprano wrote: > what do we call the vertical and horizontal line elements? I want to make > them configurable, which means the user has to be able to pass an argument > that specifies them. I have names for the individual components: > > XXX = namedtuple("XXX", "vline tee corner") > > default_YYY = XXX("? ", "?? ", "?? ") > bold_YYY = XXX("? ", "?? ", "?? ") > ascii_YYY = XXX("| ", "|- ", "+- ") > > def draw_tree(tree, YYY=default_YYY): > ... > > but what do I call XXX and YYY? Looks like horizontal, vertical, and corner are as groups -- so I would call YYY "style" and XXX "default", "bold", and "ascii". -- ~Ethan~ From mal at python.org Fri Jan 6 11:01:00 2017 From: mal at python.org (M.-A. Lemburg) Date: Fri, 6 Jan 2017 17:01:00 +0100 Subject: ANN: Python Events Calendar - Please submit your 2017 events Message-ID: <855935bd-f516-0778-b711-8b35530825c3@python.org> [Please help spread the word by forwarding to other relevant mailing lists, user groups, etc. world-wide; thanks :-)] ________________________________________________________________________ ANNOUNCING Python Events Calendars - Please submit your 2017 events maintained by the Python Software Foundation (PSF) and a group of volunteers ________________________________________________________________________ INTRODUCTION As some of you may know, the PSF has a team of volunteers who are maintaining a set of central Python event calendars. We currently have two calendars in place: * Python Events Calendar - meant for conferences and larger gatherings focusing on Python or a related technology (in whole or in part) * Python User Group Calendar - meant for user group events and other smaller local events The calendars are displayed on https://www.python.org/events/ and http://pycon.org/. There's also a map mashup to show events near you or get an overview of what currently going in the Python community: http://lmorillas.github.io/python_events/ You can subscribe to the calendars using iCal and RSS feeds and also embed the calendar widgets on your sites. We have also added a Twitter feed @PythonEvents to get immediate updates whenever a new event is added. Please see our wiki page for details: https://wiki.python.org/moin/PythonEventsCalendar The calendars are open to the world-wide Python community, so you can have local user group events, as well as regional and international conference events added to the calendars. ________________________________________________________________________ NEWS Looking back, the calendars have proven to be a great tool for the Python community to connect, with more than 250 conferences and more than a hundred of user group events listed since 2012. We would therefore like to encourage everyone to submit their 2017 events, so that the Python community can get a better overview over what's happening in Python land. ________________________________________________________________________ ADDING EVENTS Please see the instructions at https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event for details on how to submit an event. We've made it really easy for you: just need to send an email to our team address using the email template we provide for this. Thanks. ________________________________________________________________________ MORE INFORMATION More information on the calendars, the URLs, feed links, IDs, embedding, etc. is available on the wiki: https://wiki.python.org/moin/PythonEventsCalendar Enjoy, -- Marc-Andre Lemburg Python Software Foundation http://www.python.org/psf/ http://www.malemburg.com/ From zxpatric at gmail.com Fri Jan 6 14:03:13 2017 From: zxpatric at gmail.com (Patrick Zhou) Date: Fri, 6 Jan 2017 11:03:13 -0800 (PST) Subject: Work between multiple processes In-Reply-To: <586ecd7d$0$21507$e4fe514c@news.xs4all.nl> References: <892336f7-0adb-431b-aed0-b871d14ac16c@googlegroups.com> <586ecd7d$0$21507$e4fe514c@news.xs4all.nl> Message-ID: On Thursday, January 5, 2017 at 5:49:46 PM UTC-5, Irmen de Jong wrote: > On 4-1-2017 23:14, zxpatric at gmail.com wrote: > > Hi everyone, > > > > I ran into a case that I need to create a work process of an application (Jython so has to call using java.exe) which will collect the data based on what main process indicates. > > > > (1) I tried multiprocessing package, no luck. Java.exe can't be called from Process class? > > > > (2) I tried subprocess. subprocess.communicate function will wait for the work process to terminate so to return. > > > > > > either (1) or (2) doesn't work out well. Please suggest. Global system queue? > > > > Thanks, > > Patrick. > > > > > Is it a requirement that the workdf process is also Jython? > > If not: you could spawn a Python subprocess that hosts a Pyro4 daemon. > Utilizing the Pyrolite java client library you can call methods in it from the > java/jython side. (Unfortunately it is not yet possible (due to jython > incompatibilities) to use the full Pyro4 library on the Jython side as well). > Not sure if it meets your set of requirements but have a look at > http://pythonhosted.org/Pyro4/ > http://pythonhosted.org/Pyro4/pyrolite.html > > > Irmen Thanks Irmen. I think that could be the solution I am looking for. The main process will be native python so it can be used to host a pyro daemon even with lock. The jython will be a subprocess that takes the daemon's uri and use the pyrolite library to make the communication. The only trouble is that I will have to involve two more java libraries but I don't think it is a big of deal. Will try it and post the source code if possible. From jussi.piitulainen at helsinki.fi Fri Jan 6 14:21:32 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Fri, 06 Jan 2017 21:21:32 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: Message-ID: Peter Otten writes: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum function that allows the user to specify a stop > value: > >>>> from itertools import count, chain >>>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) > 0 > > How would you implement stopmin()? Only let min see the data up to, but including, the stop value: from itertools import groupby def takeuntil(data, pred): '''Take values from data until and including the first that satisfies pred (until data is exhausted if none does).''' for kind, group in groupby(data, pred): if kind: yield next(group) break else: yield from group def stopmin(data, key, stop): return min(takeuntil(data, lambda o : key(o) == stop), key = key) data = '31415926' for stop in range(5): print(stop, '=>', repr(''.join(takeuntil(data, lambda o : int(o) == stop))), '=>', repr(stopmin(data, int, stop))) # 0 => '31415926' => '1' # 1 => '31' => '1' # 2 => '3141592' => '1' # 3 => '3' => '3' # 4 => '314' => '1' from itertools import count, chain print(stopmin(chain(reversed(range(10)), count()), key=abs, stop=0)) print(stopmin(chain(reversed(range(10)), count()), key=abs, stop=3)) # 0 # 3 From wolfgang.maier at biologie.uni-freiburg.de Fri Jan 6 15:06:58 2017 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Fri, 6 Jan 2017 21:06:58 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found In-Reply-To: References: Message-ID: <072dfa54-0e8f-ee1e-b822-ad96300a1e6b@biologie.uni-freiburg.de> On 1/6/2017 15:04, Peter Otten wrote: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum function that allows the user to specify a stop > value: > >>>> from itertools import count, chain >>>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) > 0 > > How would you implement stopmin()? > How about: def stopmin (iterable, key, stop): def take_until (): for e in iterable: yield e if key(e) <= stop: break return min(take_until(), key=key) ? From wolfgang.maier at biologie.uni-freiburg.de Fri Jan 6 15:06:58 2017 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Fri, 6 Jan 2017 21:06:58 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found In-Reply-To: References: Message-ID: <072dfa54-0e8f-ee1e-b822-ad96300a1e6b@biologie.uni-freiburg.de> On 1/6/2017 15:04, Peter Otten wrote: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum function that allows the user to specify a stop > value: > >>>> from itertools import count, chain >>>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) > 0 > > How would you implement stopmin()? > How about: def stopmin (iterable, key, stop): def take_until (): for e in iterable: yield e if key(e) <= stop: break return min(take_until(), key=key) ? From jf_byrnes at comcast.net Fri Jan 6 16:44:53 2017 From: jf_byrnes at comcast.net (jim) Date: Fri, 6 Jan 2017 15:44:53 -0600 Subject: Using sudo with pip3? Message-ID: Setting up a new computer to run Ubuntu 16.04. Started using pip3 to install all the python stuff I had on the old machine and got this message: jfb at jims-1604:~$ sudo pip3 install matplotlib [sudo] password for jfb: The directory '/home/jfb/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. I (jfb) own the directory in question. I used sudo because I recall needing to use it on the old machine to get something to install. So is it necessary or even desirable to use sudo with pip3? Thanks, Jim From grant.b.edwards at gmail.com Fri Jan 6 16:55:52 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Fri, 6 Jan 2017 21:55:52 +0000 (UTC) Subject: Receiving a lot of double messages. References: <3639766662@f38.n261.z1.binkp.net> Message-ID: On 2017-01-05, Antoon Pardon wrote: > Is there something going on with the mailinglist? Because I have receive a lot > of double messages. One copy is fairly normal and is part of the discussion > thread, the other is completely seperated. -- Antoon Pardon. Yep, there are a _lot_ of duplicate messages showing up on gmane's NNTP server. I would guess around 50-100 of them in the past day. Entire chunks of long threads comprising 20-30 posts seem to be duplicated in some cases. In other cases, they're just individual posts that seem to be detached from their original threads. Something is seriously broken somewhere... -- Grant Edwards grant.b.edwards Yow! I want a VEGETARIAN at BURRITO to go ... with gmail.com EXTRA MSG!! From contact at clintmoyer.com Fri Jan 6 17:14:57 2017 From: contact at clintmoyer.com (Clint Moyer) Date: Fri, 06 Jan 2017 22:14:57 +0000 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: >From Ubuntu, why not try: sudo apt-get install python-matplotlib -Clint On Fri, Jan 6, 2017 at 3:09 PM jim wrote: > Setting up a new computer to run Ubuntu 16.04. Started using pip3 to > > install all the python stuff I had on the old machine and got this message: > > > > jfb at jims-1604:~$ sudo pip3 install matplotlib > > [sudo] password for jfb: > > The directory '/home/jfb/.cache/pip/http' or its parent directory is not > > owned by the current user and the cache has been disabled. Please check > > the permissions and owner of that directory. If executing pip with sudo, > > you may want sudo's -H flag. > > > > I (jfb) own the directory in question. > > > > I used sudo because I recall needing to use it on the old machine to get > > something to install. So is it necessary or even desirable to use sudo > > with pip3? > > > > Thanks, Jim > > > > -- > > https://mail.python.org/mailman/listinfo/python-list > > From cs at zip.com.au Fri Jan 6 17:36:44 2017 From: cs at zip.com.au (cs at zip.com.au) Date: Sat, 7 Jan 2017 09:36:44 +1100 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: <20170106223644.GA71783@cskk.homeip.net> On 06Jan2017 15:44, jim wrote: >Setting up a new computer to run Ubuntu 16.04. Started using pip3 to >install all the python stuff I had on the old machine and got this >message: > >jfb at jims-1604:~$ sudo pip3 install matplotlib >[sudo] password for jfb: >The directory '/home/jfb/.cache/pip/http' or its parent directory is >not owned by the current user and the cache has been disabled. Please >check the permissions and owner of that directory. If executing pip >with sudo, you may want sudo's -H flag. > >I (jfb) own the directory in question. > >I used sudo because I recall needing to use it on the old machine to >get something to install. So is it necessary or even desirable to use sudo >with pip3? I would not, unless I were specificly trying to install into the system's python3 libraries. That will inherently fight with any vendor (Unbuntu) supplied packages that come through apt-get. Instead I would make myself a virtualenv _based off the system python3_ and use the venv's pip to install extra packages. Not using sudo. They will land in your virtualenv directory's lib area, be entirely owned and controlled by you, and not have any complications that come with sudo. Then just symlink the virtualenv's "python3" into your own $HOME/bin and whenever you invoke "python3" it will run the virtualenv one, getting all the package goodness you have added. An important sysadmin rule of thumb: use apt (or yum etc, depending on distro) as root to install vendor supplied packages. And install your owon packages _as you_ in another area, _not_ in the system managed area. Virtualenv makes this very easy to do for Python. Cheers, Cameron Simpson From cs at zip.com.au Fri Jan 6 18:00:13 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 7 Jan 2017 10:00:13 +1100 Subject: Pexpect In-Reply-To: References: Message-ID: <20170106230012.GA7100@cskk.homeip.net> On 06Jan2017 11:37, Joaquin Alzola wrote: >Iranna Mathapati asked: >> How to match latter(caps and small) ,numbers and # symbol in python pexpect. > >With a .* Ugh. Please not. Expect() accepts a nongreedy regular expression. ".*" is the lazy "match absolutely anything" pattern. Generally overused and imprecise. Iranna Mathapati knows what to look for. See the documentation for the re module, specificly the regular expression syntax: https://docs.python.org/3/library/re.html#regular-expression-syntax To match letters, digits and "#", try: [0-9a-zA-Z#]* Cheers, Cameron Simpson From contact at clintmoyer.com Fri Jan 6 18:03:05 2017 From: contact at clintmoyer.com (Clint Moyer) Date: Fri, 06 Jan 2017 23:03:05 +0000 Subject: Using sudo with pip3? In-Reply-To: <20170106223644.GA71783@cskk.homeip.net> References: <20170106223644.GA71783@cskk.homeip.net> Message-ID: Packages supplied by your distribution can be trusted more than packages from PyPi. Just my two cents. Most distros offer nearly all the useful Python modules directly from the repo. Virtual environments are great, but if you want to add libraries to your system interpreter I'd recommend a simple sync through your repo. - Clint On Fri, Jan 6, 2017 at 3:38 PM wrote: > On 06Jan2017 15:44, jim wrote: > > >Setting up a new computer to run Ubuntu 16.04. Started using pip3 to > > >install all the python stuff I had on the old machine and got this > > >message: > > > > > >jfb at jims-1604:~$ sudo pip3 install matplotlib > > >[sudo] password for jfb: > > >The directory '/home/jfb/.cache/pip/http' or its parent directory is > > >not owned by the current user and the cache has been disabled. Please > > >check the permissions and owner of that directory. If executing pip > > >with sudo, you may want sudo's -H flag. > > > > > >I (jfb) own the directory in question. > > > > > >I used sudo because I recall needing to use it on the old machine to > > >get something to install. So is it necessary or even desirable to use sudo > > >with pip3? > > > > I would not, unless I were specificly trying to install into the system's > > python3 libraries. That will inherently fight with any vendor (Unbuntu) > > supplied packages that come through apt-get. > > > > Instead I would make myself a virtualenv _based off the system python3_ > and use > > the venv's pip to install extra packages. Not using sudo. They will land in > > your virtualenv directory's lib area, be entirely owned and controlled by > you, > > and not have any complications that come with sudo. > > > > Then just symlink the virtualenv's "python3" into your own $HOME/bin and > > whenever you invoke "python3" it will run the virtualenv one, getting all > the > > package goodness you have added. > > > > An important sysadmin rule of thumb: use apt (or yum etc, depending on > distro) > > as root to install vendor supplied packages. And install your owon > packages _as > > you_ in another area, _not_ in the system managed area. Virtualenv makes > this > > very easy to do for Python. > > > > Cheers, > > Cameron Simpson > > -- > > https://mail.python.org/mailman/listinfo/python-list > > From python at deborahswanson.net Fri Jan 6 18:03:45 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 6 Jan 2017 15:03:45 -0800 Subject: Receiving a lot of double messages. In-Reply-To: Message-ID: <002701d26871$2222b9e0$27b23dae@sambora> Grant Edwards wrote, on January 06, 2017 1:56 PM > > On 2017-01-05, Antoon Pardon wrote: > > > Is there something going on with the mailinglist? Because I have > > receive a lot > > of double messages. One copy is fairly normal and is part > of the discussion > > thread, the other is completely seperated. -- Antoon Pardon. > > Yep, there are a _lot_ of duplicate messages showing up on > gmane's NNTP server. I would guess around 50-100 of them in > the past day. Entire chunks of long threads comprising 20-30 > posts seem to be duplicated in some cases. In other cases, > they're just individual posts that seem to be detached from > their original threads. > > Something is seriously broken somewhere... > > -- > Grant Edwards grant.b.edwards Yow! I > want a VEGETARIAN > at BURRITO to > go ... with > gmail.com EXTRA MSG!! 162 duplicate messages, as of 2:04 PM PST today, to be precise. This same thing happened when I was on the Exchange team at Microsoft, but that was on a pre-release dogfood mail server, not even a beta for a pre-release. Good thing it was only in-house Microsofties who had to field the thousands of garbage messages when that happened. The problem then was that they had to flush the mail queue that had become clogged up with duplicate messages because some bit of code had looped back on itself. Granted, the list mail server probably isn't an enterprise version, with mail replicating between hundreds of servers, but as Chris mentioned, it does take a feed from the newsgroup, and there's plenty of room for that to break. Quite likely the fix for any mail server whose mail queue has been corrupted is to flush it. From valkrem at yahoo.com Fri Jan 6 18:26:32 2017 From: valkrem at yahoo.com (Val Krem) Date: Fri, 6 Jan 2017 23:26:32 +0000 (UTC) Subject: crosstab output References: <665870666.2223003.1483745192910.ref@mail.yahoo.com> Message-ID: <665870666.2223003.1483745192910@mail.yahoo.com> Hi all, How do I access the rows and columns of a data frame crosstab output? Here is code using a sample data and output. a= pd.read_csv("cross.dat", skipinitialspace=True) xc=pd.crosstab(a['nam'],a['x1'],margins=True) print(xc) x1 0 1 nam A1 3 2 A2 1 4 I want to create a variable by adding 2/(3+2) for the first row(A1) and 4/(1+4) for the second row (A2) Final data frame would be A1 3 2 0.4 A2 1 4 0.8 Thank you in advance From no.email at nospam.invalid Fri Jan 6 20:34:09 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Fri, 06 Jan 2017 17:34:09 -0800 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: Message-ID: <87eg0f3b72.fsf@nightsong.com> Peter Otten <__peter__ at web.de> writes: > How would you implement stopmin()? Use itertools.takewhile From nimbiotics at gmail.com Fri Jan 6 20:45:18 2017 From: nimbiotics at gmail.com (Mario R. Osorio) Date: Fri, 6 Jan 2017 17:45:18 -0800 (PST) Subject: The hardest problem in computer science... In-Reply-To: References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> <586FB9C4.6000505@stoneleaf.us> Message-ID: <45df7e06-ca04-4aa3-90a0-32fa3f54d042@googlegroups.com> On Friday, January 6, 2017 at 10:37:40 AM UTC-5, Ethan Furman wrote: > On 01/06/2017 05:03 AM, Steve D'Aprano wrote: > > > what do we call the vertical and horizontal line elements? I want to make > > them configurable, which means the user has to be able to pass an argument > > that specifies them. I have names for the individual components: > > > > XXX = namedtuple("XXX", "vline tee corner") > > > > default_YYY = XXX("? ", "?? ", "?? ") > > bold_YYY = XXX("? ", "?? ", "?? ") > > ascii_YYY = XXX("| ", "|- ", "+- ") > > > > def draw_tree(tree, YYY=default_YYY): > > ... > > > > but what do I call XXX and YYY? > > Looks like horizontal, vertical, and corner are as groups -- so I would call YYY "style" and XXX "default", "bold", and "ascii". > > -- > ~Ethan~ back in the days of CPM this group was referred to as "box characters", and each of them were called as follows: "?": vertical_line, v_line "??": left_intersection, l_intersection, left_tee, l_tee "??": bottom_left_corner, bl_corner [...and son on...] (the names also apply to the combination of lower ascii characters) From nimbiotics at gmail.com Fri Jan 6 20:49:12 2017 From: nimbiotics at gmail.com (Mario R. Osorio) Date: Fri, 6 Jan 2017 17:49:12 -0800 (PST) Subject: The hardest problem in computer science... In-Reply-To: <45df7e06-ca04-4aa3-90a0-32fa3f54d042@googlegroups.com> References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> <586FB9C4.6000505@stoneleaf.us> <45df7e06-ca04-4aa3-90a0-32fa3f54d042@googlegroups.com> Message-ID: On Friday, January 6, 2017 at 8:45:41 PM UTC-5, Mario R. Osorio wrote: > On Friday, January 6, 2017 at 10:37:40 AM UTC-5, Ethan Furman wrote: > > On 01/06/2017 05:03 AM, Steve D'Aprano wrote: > > > > > what do we call the vertical and horizontal line elements? I want to make > > > them configurable, which means the user has to be able to pass an argument > > > that specifies them. I have names for the individual components: > > > > > > XXX = namedtuple("XXX", "vline tee corner") > > > > > > default_YYY = XXX("? ", "?? ", "?? ") > > > bold_YYY = XXX("? ", "?? ", "?? ") > > > ascii_YYY = XXX("| ", "|- ", "+- ") > > > > > > def draw_tree(tree, YYY=default_YYY): > > > ... > > > > > > but what do I call XXX and YYY? > > > > Looks like horizontal, vertical, and corner are as groups -- so I would call YYY "style" and XXX "default", "bold", and "ascii". > > > > -- > > ~Ethan~ > > back in the days of CPM this group was referred to as "box characters", and each of them were called as follows: > > "?": vertical_line, v_line > > "??": left_intersection, l_intersection, left_tee, l_tee > > "??": bottom_left_corner, bl_corner > > [...and son on...] > > (the names also apply to the combination of lower ascii characters) NOW ... in particular this case I'd call them: "?": vertical_line, v_line "??": node "??": last_node, l_node From cs at zip.com.au Fri Jan 6 21:24:24 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 7 Jan 2017 13:24:24 +1100 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: <20170107022424.GA68905@cskk.homeip.net> On 06Jan2017 23:03, Clint Moyer wrote: >Packages supplied by your distribution can be trusted more than packages >from PyPi. Just my two cents. >Most distros offer nearly all the useful Python modules directly from the >repo. I would agree with this on the whole. And also that it is generally better to add modules to your system python via the distro's repo because that bring benefit to every user on the system, not just yourself. >Virtual environments are great, but if you want to add libraries to your >system interpreter I'd recommend a simple sync through your repo. I'm directly advocating _not_ adding PyPI packages to the system interpreter. If nothing else, they may differ in behaviour and potentially actually break system behaviour. Having your on virtualenv is good for: adding packages no provided by your vendor, adding packages deliberately different from those from your vendor (eg newer versions with specific bugfixes or extra features), having an isolated environment for packages (you can make more than one virtual environment). And of course it avoids interfering with your system python install. Cheers, Cameron Simpson From orgnut at yahoo.com Fri Jan 6 22:12:52 2017 From: orgnut at yahoo.com (Larry Hudson) Date: Fri, 6 Jan 2017 19:12:52 -0800 Subject: The hardest problem in computer science... In-Reply-To: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> Message-ID: <45udnZ3EUtsowe3FnZ2dnUU7-VnNnZ2d@giganews.com> On 01/06/2017 05:03 AM, Steve D'Aprano wrote: > The second hardest problem in computer science is cache invalidation. > > The *hardest* problem is naming things. > > In a hierarchical tree view widget that displays items like this: > > > Fiction > ?? Fantasy > ? ?? Terry Pratchett > ? ? ?? Discworld [snip] > but what do I call XXX and YYY? > > > Seriously-considering-just-hard-coding-them-as-magic-constants-ly y'rs, I don't know if this is helpful or even relevant, but here is a class for using these box-drawing characters that I wrote for my own use. (I'm just a hobby programmer...) It's not very elegant and not particularly convenient to use, but it is usable. And I hesitate to post it because it is so long (approx 90 line docstring to explain its use, and ending with some test code). In any case, here it is... # boxch.py """ BoxChr class to handle box-drawing characters. The character is selected by a two-character string: 'tl', 'tm', 'tr' -- Top Left, Top Mid, Top Right: ? ? ? 'ml', 'mm', 'mr' -- Mid Left, Mid Mid, Mid Right: ? ? ? 'bl', 'bm', 'br' -- Bot Left, Bot Mid, Bot Right: ? ? ? 'hz', 'vt' -- Horizontal and Vertical lines: ? ? Case is ignored. Invalid selection string returns a dot: '?' NOTE: It is currently disabled, but the code is still available to handle small and large dots with 'dt' and 'dd'. These both ignore the style. The style is selected by a two-character string: The characters are 's', 'd' and 'b' for single, double and bold. The first character defines the horizontal components, the second defines the vertical components. The valid styles are 'ss', 'sd', 'sb', 'ds', 'dd', 'bs', 'bb'. The potential styles 'db' and 'bd' are invalid. The class defaults to 'ss' -- single horizontal, single vertical. Case is ignored. Invalid style string raises ValueError. NOTE: The following examples assume bc has been set to a BoxChr class. bc = BoxChr() # Style defaults to 'ss' or bc = BoxChr('sd') # Style set to single/double, or whatever desired. (Examples assume 'ss' style.) Attributes: style: set or return the style-code string. Case is ignored. Invalid style raises ValueError. Examples: bc.style = 'ds' -- sets style to double/single. st = bc.style -- sets variable st to current style code. Methods: [] (__getitem__): Returns selected box character. Case is ignored. Invalid selector returns a dot: '?' Example: bc['tm'] returns '?' bxstr(): Returns a string created from a sequence of box character codes and 'normal' characters. (in this description 'selector' refers to the two-character codes as defined above.) Each element of the given sequence (list or tuple) must be a string, either a series of selector codes, or a 'normal' string (one without any box selectors). The box character selector string must start with 'BX' followed by the selector codes, separated by spaces. The 'BX' must be upper case, but the case of the selector codes is ignored. A space between the 'BX' prefix and the first selector code is optional. This selector string can only contain selector codes. 'Normal' characters are simply given as normal strings, interspersed with the selector strings in the given sequence. If the selection is only box characters, it can opionally be passed as a single string rather than enclosing it in a list. Example: seq = ['BX ml hz', ' TEXT ', 'BX hz mr'] bc.bxstr(seq) returns '?? TEXT ??' Note: If you need a string beginning with 'BX' it has to be given as a single-character string 'B' followed by a string containing the remaining text. Example 'BX etc' must be given as: ['B', 'X etc']. boxtext(): Create boxed text, returned as a single string with embedded newlines. Parameters: txt The text to use tsize Expand tabs to specified number of spaces, Default is 4 just '<', '^' or '>' as left/center/right justification. Default is '<' ? left-justified. tall If True, inserts a blank line above and below the text. If False, no extra blank lines are used. Default is True. Text can be either a single string with embedded newlines (ie. a triple-quoted string) or list of strings. Trailing blank lines are removed. Center and right justification will strip whitespace from both ends of the lines. Left justification (the default) only strips trailing whitespace. Width is based on the length of the longest line, plus two spaces before and after. """ class BoxChr: def __init__(self, style='ss'): self._styles = ('ss', 'sd', 'sb', 'ds', 'dd', 'bs', 'bb') self._keys = ('tl', 'tm', 'tr', 'ml', 'mm', 'mr', 'bl', 'bm', 'br', 'hz', 'vt') # 'dt', 'dd') self._boxchrs = { 'tl' : ('?', '?', '?', '?', '?', '?', '?'), 'tm' : ('?', '?', '?', '?', '?', '?', '?'), 'tr' : ('?', '?', '?', '?', '?', '?', '?'), 'ml' : ('?', '?', '?', '?', '?', '?', '?'), 'mm' : ('?', '?', '?', '?', '?', '?', '?'), 'mr' : ('?', '?', '?', '?', '?', '?', '?'), 'bl' : ('?', '?', '?', '?', '?', '?', '?'), 'bm' : ('?', '?', '?', '?', '?', '?', '?'), 'br' : ('?', '?', '?', '?', '?', '?', '?'), 'hz' : ('?', '?', '?', '?', '?', '?', '?'), 'vt' : ('?', '?', '?', '?', '?', '?', '?') # 'dt' : ('?', '?', '?', '?', '?', '?', '?'), # 'dd' : ('?', '?', '?', '?', '?', '?', '?') } self._setStyle(style) def _setStyle(self, style): """Set the class style""" try: self._style = self._styles.index(style.lower()) except IndexError: raise ValueError def _getStyle(self): """Return the class style code""" return self._styles[self._style] style = property(_getStyle, _setStyle) def __getitem__(self, key): """Select and return the box character""" try: return self._boxchrs[key.lower()][self._style] except KeyError: return '?' def bxstr(self, slst): """Create a string from a given sequence of strings""" def cnvst(s): """Convert selector string to box character string""" bs = [] for sc in s: # For each selector code bs.append(self.__getitem__(sc)) # Get box char return ''.join(bs) out = [] if isinstance(slst, str): # Input is string? slst = [slst] # Make it a list for ss in slst: # For each string in list if ss.startswith('BX'): # Check if selector or normal string out.append(cnvst(ss[2:].split())) # Selector else: out.append(ss) # Normal string return ''.join(out) def boxtext(self, txt, tsize=4, just='<', tall=True): """Create boxed text""" if just not in '<^>': # Check for valid justification code just = '<' if isinstance(txt, str): # Check for string input txt = txt.split('\n') # Convert to list while txt[-1].rstrip() == '': # Delete trailing blank lines txt = txt[:-1] if just == '<': # Left just, only strip on right txt = [line.rstrip() for line in txt] else: # Otherwise strip both ends txt = [line.strip() for line in txt] txt = [line.expandtabs(tsize) for line in txt] # Cnvt tabs to spaces maxlen = max([len(line) for line in txt]) + 4 # Find longest line # Create the boxed text out = [] out.append(self.bxstr('BXtl ' + 'hz ' * maxlen + 'tr')) if tall: out.append(self.bxstr(['BXvt', ' ' * maxlen, 'BXvt'])) for line in txt: out.append(self.bxstr(['BXvt', ' {:{}{}} '. format(line, just, maxlen-4), 'BXvt'])) if tall: out.append(self.bxstr(['BXvt', ' ' * maxlen, 'BXvt'])) out.append(self.bxstr('BXbl ' + 'hz ' * maxlen + 'br')) return '\n'.join(out) #====================================== if __name__ == '__main__': def grid(bc): """Print a box""" print(bc.bxstr(['BXtl hz hz hz TM hz Hz hZ tr'])) print(bc.bxstr(['BXvt', ' ', 'BXvt', ' ', 'BXvt'])) print(bc.bxstr('BX ml hz hz hz mm hz hz hz mr')) print(bc.bxstr(['BX vt', ' ', 'BXvt', ' ', 'BXvt'])) print(bc.bxstr('BXbl hz hz hz bm hz hz hz br')) bc = BoxChr() # Check all chars in all styles for style in bc._styles: bc.style = style grid(bc) # Verify error test try: bc.style = 'xx' except ValueError: print('Invaled style') # Test boxtext() method jab = """'Twas brillig, and the slithy toves \tDid gyre and gimbol in the wabe. All mimsy were the borogoves, \tAnd the momraths outgrabe. """ bc.style='ds' print(bc.boxtext(jab)) print(bc.boxtext(jab, tsize=8)) print(bc.boxtext(jab, just='^')) print(bc.boxtext(jab, just='>')) print(bc.boxtext(jab, tall=False)) -- -=- Larry -=- From python at deborahswanson.net Fri Jan 6 22:46:50 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 6 Jan 2017 19:46:50 -0800 Subject: Namedtuples: TypeError: 'str' object is not callable Message-ID: <005301d26898$ae2bcef0$27b23dae@sambora> I'm not sure what Python is complaining about here, or why. Here's the example from the Python docs: https://docs.python.org/3/library/collections.html#collections.namedtupl e EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') import csv for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))): print(emp.name, emp.title) And here's my code: listings = namedtuple('listings', ['CLDesc', 'url', 'desc', 'Description', 'Location', 'STco', 'miles', 'Kind', 'Rent', 'Date', 'br', 'Notes', 'yesno', 'mark', 'arc']) for lst in map(listings._make, csv.reader(open('Moving 2017.csv', "r"))): . . (['x', 'y'] is a valid format for the field names, and there are no errors when the definition for listings executes.) And here's the Traceback in PyCharm: File "E:/Coding projects/Pycharm/Moving/moving_numberedtuples.py", line 139, in moving() for lst in map(listings._make, csv.reader(open('E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv',"r"))): TypeError: 'str' object is not callable What str object is not callable, and how do I fix it? I see an iteration variable (lst), a namedtuples method call(listings._make, which makes a new instance of listings) and a csv.reader call, a variable and 2 method calls, none of which are bare strings, except possibly 'listings._make', but there's no str it might be trying to call that I can see. My 'for' statement looks like a good copy of the example from the docs to me, and the example in the docs uses 'EmployeeRecord._make' with no parentheses. Is the example from the docs possibly in error? I don't see any other possibility, but maybe I'm not looking in the right place. I don't know enough about classes in Python yet to know if this is ok or not. And when I tried again with 'listings._make()', I got: TypeError: _make() missing 1 required positional argument: 'iterable'. This suggests that maybe 'listings._make' is wrong, but if it should be 'listings._make()', what should the iterable be? 'listings'? 'listings._make(listings)' looks awfully redundant for Python, so '_make(listings)' would make more sense (pun not intended), and seems to be what the error thinks it is. But when I change it to '_make(listings)', I get: NameError: name '_make' is not defined 'listings._make(listings)' gets "TypeError: 'type' object is not iterable", which answers the question of whether a namedtuple instance (if that's what 'listings' is) is an iterable. It isn't. There might be a version problem here. I'm using Python 3.4.3, and the url for this page simply indicates version 3. As most of us know, there's a lot of variance between dot versions of 3, and quite possibly not all of the changes are documented correctly. In 3.4.3 the usage might be slightly different, or the code broken. Any help with this would be hugely appreciated. From rosuav at gmail.com Fri Jan 6 23:04:42 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 7 Jan 2017 15:04:42 +1100 Subject: Namedtuples: TypeError: 'str' object is not callable In-Reply-To: <005301d26898$ae2bcef0$27b23dae@sambora> References: <005301d26898$ae2bcef0$27b23dae@sambora> Message-ID: On Sat, Jan 7, 2017 at 2:46 PM, Deborah Swanson wrote: > And here's the Traceback in PyCharm: > File "E:/Coding projects/Pycharm/Moving/moving_numberedtuples.py", > line 139, in moving() > for lst in map(listings._make, csv.reader(open('E:\\Coding > projects\\Pycharm\\Moving\\Moving 2017 in.csv',"r"))): > TypeError: 'str' object is not callable > > What str object is not callable, and how do I fix it? Toss in a 'print' above that. You're calling map and open (and csv.reader, but I doubt you've shadowed that). My money would be on 'map' having been assigned something. ChrisA From python at deborahswanson.net Sat Jan 7 00:07:13 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 6 Jan 2017 21:07:13 -0800 Subject: Namedtuples: TypeError: 'str' object is not callable In-Reply-To: Message-ID: <005e01d268a3$e8e73470$27b23dae@sambora> Chris Angelico wrote, on January 06, 2017 8:05 PM > To: python-list at python.org > Subject: Re: Namedtuples: TypeError: 'str' object is not callable > > > On Sat, Jan 7, 2017 at 2:46 PM, Deborah Swanson > wrote: > > And here's the Traceback in PyCharm: > > File "E:/Coding projects/Pycharm/Moving/moving_numberedtuples.py", > > line 139, in moving() > > for lst in map(listings._make, csv.reader(open('E:\\Coding > > projects\\Pycharm\\Moving\\Moving 2017 in.csv',"r"))): > > TypeError: 'str' object is not callable > > > > What str object is not callable, and how do I fix it? > > Toss in a 'print' above that. You're calling map and open > (and csv.reader, but I doubt you've shadowed that). My money > would be on 'map' having been assigned something. > > ChrisA And you would be precisely correct. I have a variable named 'map', and I intended to delete it and the code that used it, but totally forgot about it. It's still in there somewhere, but a simple search will find it. I really don't know how long it would've taken me to think of that, so thank you! Deborah From rosuav at gmail.com Sat Jan 7 00:14:27 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 7 Jan 2017 16:14:27 +1100 Subject: Namedtuples: TypeError: 'str' object is not callable In-Reply-To: <005e01d268a3$e8e73470$27b23dae@sambora> References: <005e01d268a3$e8e73470$27b23dae@sambora> Message-ID: On Sat, Jan 7, 2017 at 4:07 PM, Deborah Swanson wrote: > And you would be precisely correct. I have a variable named 'map', and I > intended to delete it and the code that used it, but totally forgot > about it. It's still in there somewhere, but a simple search will find > it. > > I really don't know how long it would've taken me to think of that, so > thank you! I have a well-trained crystal ball :) ChrisA From python at deborahswanson.net Sat Jan 7 01:11:26 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 6 Jan 2017 22:11:26 -0800 Subject: Namedtuples: TypeError: 'str' object is not callable In-Reply-To: Message-ID: <000001d268ac$e14b0760$27b23dae@sambora> Chris Angelico wrote, on January 06, 2017 9:14 PM > > On Sat, Jan 7, 2017 at 4:07 PM, Deborah Swanson > wrote: > > > > I really don't know how long it would've taken me to think of that, so > > thank you! > > I have a well-trained crystal ball :) > > ChrisA More like a very experienced eye. I keep hoping I'll get one of those, but it takes time. From jussi.piitulainen at helsinki.fi Sat Jan 7 01:55:54 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sat, 07 Jan 2017 08:55:54 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> Message-ID: Paul Rubin writes: > Peter Otten writes: >> How would you implement stopmin()? > > Use itertools.takewhile How? It consumes the crucial stop element: it = iter('what?') list(takewhile(str.isalpha, it)) # ==> ['w', 'h', 'a', 't'] next(it, 42) # ==> 42 From rustompmody at gmail.com Sat Jan 7 02:10:11 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Fri, 6 Jan 2017 23:10:11 -0800 (PST) Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found In-Reply-To: References: <87eg0f3b72.fsf@nightsong.com> Message-ID: <029aea48-3f1f-4a99-8a3b-6e8a906cbdfd@googlegroups.com> On Saturday, January 7, 2017 at 12:26:04 PM UTC+5:30, Jussi Piitulainen wrote: > Paul Rubin writes: > > > Peter Otten writes: > >> How would you implement stopmin()? > > > > Use itertools.takewhile > > How? It consumes the crucial stop element: > > it = iter('what?') > list(takewhile(str.isalpha, it)) # ==> ['w', 'h', 'a', 't'] > next(it, 42) # ==> 42 I was also wondering how? In a lazy language (eg haskell) with non-strict foldr (reduce but rightwards) supplied non-strict operator this is trivial. ie in python idiom with reduce being right_reduce reduce(operator.mul, [1,2,0,4,...], 1) the reduction would stop at the 0 Not sure how to simulate this in a strict language like python Making fold(r) non-strict by using generators is ok How to pass a non-strict operator? From steve+python at pearwood.info Sat Jan 7 02:34:16 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 07 Jan 2017 18:34:16 +1100 Subject: The hardest problem in computer science... References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> Message-ID: <587099fa$0$1610$c3e8da3$5496439d@news.astraweb.com> On Sat, 7 Jan 2017 12:03 am, Steve D'Aprano wrote: > The second hardest problem in computer science is cache invalidation. > > The *hardest* problem is naming things. Thanks everyone who answered, but I think some of you misunderstood my question. I know that the individual characters themselves are called some variation of "line drawing characters", or "box drawing characters". But the important part of the question was given by the Python code: > XXX = namedtuple("XXX", "vline tee corner") > > default_YYY = XXX("? ", "?? ", "?? ") > bold_YYY = XXX("? ", "?? ", "?? ") > ascii_YYY = XXX("| ", "|- ", "+- ") > > def draw_tree(tree, YYY=default_YYY): > ... > > > but what do I call XXX and YYY? After puzzling over this for three days, it suddenly hit me: Theme = namedtuple("Theme", "vline tee corner") DEFAULT = Theme("? ", "?? ", "?? ") BOLD = Theme("? ", "?? ", "?? ") ASCII = Theme("| ", "|- ", "+- ") def draw_tree(tree, theme=DEFAULT): ... Ethan's idea of "style" is also good. Thanks for all your ideas! -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Sat Jan 7 03:01:27 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 07 Jan 2017 19:01:27 +1100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: Message-ID: <5870a057$0$1585$c3e8da3$5496439d@news.astraweb.com> On Sat, 7 Jan 2017 01:04 am, Peter Otten wrote: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum function that allows the user to specify a stop > value: > >>>> from itertools import count, chain >>>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) > 0 > > How would you implement stopmin()? That depends on whether you want to stop when you find a value *equal* to the stop value, or *less than* the stop value, or both. It depends on whether you want to check for equality or check for any arbitrary condition. It depends on whether you want the stop value to be included or not. (It looks like you do want it included.) But I'd start with this: # not tested def stopmin(iterable, key=None, stop=None): it = iter(iterable) try: smallest = next(it) except StopIteration: raise ValueError('empty iterable has no minimum') else: if key is not None: keyed_smallest = key(smallest) if key is None: for x in iterable: if x < smallest: smallest = x if x == stop: break else: for x in iterable: y = key(x) if y < keyed_smallest: keyed_smallest = y smallest = x if y == stop: break return smallest Another possibility is to create a variant of itertools takewhile: def takeuntil(predicate, iterable): # takeuntil(lambda x: x<5, [1,4,6,4,1]) --> 1 4 6 for x in iterable: yield x if predicate(x): break min(takeuntil(lambda x: x == 0, iterable), key=abs) py> from itertools import count, chain py> iterable = chain(reversed(range(10)), count()) py> min(takeuntil(lambda x: x == 0, iterable), key=abs) 0 py> iterable = chain(range(-9, 10), count()) py> min(takeuntil(lambda x: x == 0, iterable), key=abs) 0 -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From jussi.piitulainen at helsinki.fi Sat Jan 7 03:12:38 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sat, 07 Jan 2017 10:12:38 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <029aea48-3f1f-4a99-8a3b-6e8a906cbdfd@googlegroups.com> Message-ID: Rustom Mody writes: > On Saturday, Jussi Piitulainen wrote: >> Paul Rubin writes: >> >> > Peter Otten writes: >> >> How would you implement stopmin()? >> > >> > Use itertools.takewhile >> >> How? It consumes the crucial stop element: >> >> it = iter('what?') >> list(takewhile(str.isalpha, it)) # ==> ['w', 'h', 'a', 't'] >> next(it, 42) # ==> 42 > > I was also wondering how? > In a lazy language (eg haskell) with non-strict foldr (reduce but > rightwards) supplied non-strict operator this is trivial. > ie in python idiom with reduce being right_reduce > reduce(operator.mul, [1,2,0,4,...], 1) > the reduction would stop at the 0 > Not sure how to simulate this in a strict language like python > Making fold(r) non-strict by using generators is ok > How to pass a non-strict operator? I think it would have to be some really awkward pseudo-operator that throws an exception when it encounters its zero, and then reduce (or something outside reduce) would catch that exception. Something like that could be done but it would still be awkward. Don't wanna :) You switched to a simpler operator. Would Haskell notice that def minabs(x, y): return min(x, y, key = abs) has a meaningful zero? Surely it has its limits somewhere and then the programmer needs to supply the information. From rosuav at gmail.com Sat Jan 7 03:19:03 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 7 Jan 2017 19:19:03 +1100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found In-Reply-To: References: <87eg0f3b72.fsf@nightsong.com> <029aea48-3f1f-4a99-8a3b-6e8a906cbdfd@googlegroups.com> Message-ID: On Sat, Jan 7, 2017 at 7:12 PM, Jussi Piitulainen wrote: > You switched to a simpler operator. Would Haskell notice that > > def minabs(x, y): return min(x, y, key = abs) > > has a meaningful zero? Surely it has its limits somewhere and then the > programmer needs to supply the information. If the return value of abs is int(0..) then yeah, it could. (Or whatever the notation is. That's Pike's limited-range-int type syntax.) ChrisA From jussi.piitulainen at helsinki.fi Sat Jan 7 03:54:35 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sat, 07 Jan 2017 10:54:35 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <029aea48-3f1f-4a99-8a3b-6e8a906cbdfd@googlegroups.com> Message-ID: Chris Angelico writes: > On Sat, Jan 7, 2017 at 7:12 PM, Jussi Piitulainen wrote: >> You switched to a simpler operator. Would Haskell notice that >> >> def minabs(x, y): return min(x, y, key = abs) >> >> has a meaningful zero? Surely it has its limits somewhere and then >> the programmer needs to supply the information. > > If the return value of abs is int(0..) then yeah, it could. (Or > whatever the notation is. That's Pike's limited-range-int type > syntax.) Maybe so. If Haskell abs has such types. (For integers, rationals, whatever numeric types Haskell has, which I've quite forgotten, or it may have even changed since I knew some Haskell. It's been a while.) I rewrite the question so that the answer cannot be deduced from just the types of the functions: def minabs(x, y): return min(x, y, key = lambda w: max(w, -w)) Surely max of two ints is an int. Maybe the Haskell compiler could specialize the type, but my question is, is it _guaranteed_ to do so, and how should the programmer know to rely on that? From greg.ewing at canterbury.ac.nz Sat Jan 7 04:12:49 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sat, 07 Jan 2017 22:12:49 +1300 Subject: Namedtuples: TypeError: 'str' object is not callable In-Reply-To: References: <005301d26898$ae2bcef0$27b23dae@sambora> Message-ID: Deborah Swanson wrote: > File "E:/Coding projects/Pycharm/Moving/moving_numberedtuples.py", > line 139, in moving() > for lst in map(listings._make, csv.reader(open('E:\\Coding > projects\\Pycharm\\Moving\\Moving 2017 in.csv',"r"))): > TypeError: 'str' object is not callable I know you've found the answer to this one, but as a general tip, if you're getting an exception from a complicated line like that, it can pay to break it down into simpler steps, then you can see more clearly at what stage of the process the exception is occurring. E.g. file = open('E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv',"r") reader = csv.reader(file) rows = map(listings._make, reader) for lst in rows: ... In this case the traceback would have pointed you to the third line, telling you that it was something to do with the map() call. -- Greg From motoom at xs4all.nl Sat Jan 7 04:32:44 2017 From: motoom at xs4all.nl (Michiel Overtoom) Date: Sat, 7 Jan 2017 10:32:44 +0100 Subject: Using sudo with pip3? In-Reply-To: <20170107022424.GA68905@cskk.homeip.net> References: <20170107022424.GA68905@cskk.homeip.net> Message-ID: <2BFDB1B8-E168-4D10-BEF8-845D3FC9BA69@xs4all.nl> > On 2017-01-07, at 03:24, Cameron Simpson wrote: > > Having your on virtualenv is good for: [...] having an isolated environment for packages (you can make more than one virtual environment). Yes, indeed. For example, if you have to maintain two different codebases, one using Django 1.8 and the other Django 1.10 and a slew of other packages, for example an older and newer version of allauth, you'd have to create two virtual environments or otherwise you'll end up with a broken situation. Greetings, From python at deborahswanson.net Sat Jan 7 04:41:56 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sat, 7 Jan 2017 01:41:56 -0800 Subject: Namedtuples: TypeError: 'str' object is not callable In-Reply-To: Message-ID: <001301d268ca$497d8110$27b23dae@sambora> Gregory Ewing wrote, on January 07, 2017 1:13 AM > > Deborah Swanson wrote: > > File "E:/Coding projects/Pycharm/Moving/moving_numberedtuples.py", > > line 139, in moving() > > for lst in map(listings._make, csv.reader(open('E:\\Coding > > projects\\Pycharm\\Moving\\Moving 2017 in.csv',"r"))): > > TypeError: 'str' object is not callable > > I know you've found the answer to this one, but as a general > tip, if you're getting an exception from a complicated line > like that, it can pay to break it down into simpler steps, > then you can see more clearly at what stage of the process > the exception is occurring. E.g. > > file = open('E:\\Coding projects\\Pycharm\\Moving\\Moving > 2017 in.csv',"r") > reader = csv.reader(file) > rows = map(listings._make, reader) > for lst in rows: > ... > > In this case the traceback would have pointed you to the > third line, telling you that it was something to do with the > map() call. > > -- > Greg Thanks Greg. I'm really kicking myself for seeing everything in that statement except the 'map('! Not really sure why it was invisible to me, but the mind is very devious (also a Terrible Thing to Taste, according to Ministry). But your method gives a systematic way to break things down and look at them one by one. It can be really dismaying to have a complex statement like this one, that you copied from an example, and have it spit out error after error no matter what you try. Having a systematic method of approach is quite probably guaranteed to succeed. (I'll bet you can guess why I'm taking on numberedtuples. Hope to have the rewritten code using them soon, and the more I look at it the surer I am that numberedtuples will clean up a lot of fuzziness and wobbly code. Thanks for the suggestion.) Deborah From steve+python at pearwood.info Sat Jan 7 05:56:18 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 07 Jan 2017 21:56:18 +1100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <072dfa54-0e8f-ee1e-b822-ad96300a1e6b@biologie.uni-freiburg.de> Message-ID: <5870c953$0$1614$c3e8da3$5496439d@news.astraweb.com> On Sat, 7 Jan 2017 07:06 am, Wolfgang Maier wrote: > On 1/6/2017 15:04, Peter Otten wrote: [...] >> How would you implement stopmin()? >> > > How about: > > def stopmin (iterable, key, stop): > def take_until (): > for e in iterable: > yield e > if key(e) <= stop: > break > return min(take_until(), key=key) Heh, great minds think alike :-) -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From __peter__ at web.de Sat Jan 7 06:42:58 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 07 Jan 2017 12:42:58 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: Message-ID: Jussi Piitulainen wrote: > Peter Otten writes: > >> Example: you are looking for the minimum absolute value in a series of >> integers. As soon as you encounter the first 0 it's unnecessary extra >> work to check the remaining values, but the builtin min() will continue. >> >> The solution is a minimum function that allows the user to specify a stop >> value: >> >>>>> from itertools import count, chain >>>>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) >> 0 >> >> How would you implement stopmin()? > > Only let min see the data up to, but including, the stop value: I should have mentioned that my actual use case has a costly key() function. > from itertools import groupby > > def takeuntil(data, pred): > '''Take values from data until and including the first that > satisfies pred (until data is exhausted if none does).''' > for kind, group in groupby(data, pred): > if kind: > yield next(group) > break > else: > yield from group A clever takeuntil() implementation ;) I may steal it for my current favourite def stopmin_du(items, *, key, stop): # decorate, process, undecorate pairs = ((key(item), item) for item in items) pairs = takeuntil(pairs, lambda pair: pair[0] <= stop) return min(pairs, key=firstitem)[1] > def stopmin(data, key, stop): > return min(takeuntil(data, lambda o : key(o) == stop), > key = key) > > data = '31415926' > for stop in range(5): > print(stop, > '=>', repr(''.join(takeuntil(data, lambda o : int(o) == stop))), > '=>', repr(stopmin(data, int, stop))) > > # 0 => '31415926' => '1' > # 1 => '31' => '1' > # 2 => '3141592' => '1' > # 3 => '3' => '3' > # 4 => '314' => '1' > > from itertools import count, chain > print(stopmin(chain(reversed(range(10)), count()), key=abs, stop=0)) > print(stopmin(chain(reversed(range(10)), count()), key=abs, stop=3)) > > # 0 > # 3 From __peter__ at web.de Sat Jan 7 06:44:05 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 07 Jan 2017 12:44:05 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <072dfa54-0e8f-ee1e-b822-ad96300a1e6b@biologie.uni-freiburg.de> Message-ID: Wolfgang Maier wrote: > On 1/6/2017 15:04, Peter Otten wrote: >> Example: you are looking for the minimum absolute value in a series of >> integers. As soon as you encounter the first 0 it's unnecessary extra >> work to check the remaining values, but the builtin min() will continue. >> >> The solution is a minimum function that allows the user to specify a stop >> value: >> >>>>> from itertools import count, chain >>>>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) >> 0 >> >> How would you implement stopmin()? >> > > How about: > > def stopmin (iterable, key, stop): > def take_until (): > for e in iterable: > yield e > if key(e) <= stop: > break > return min(take_until(), key=key) > > ? Clean and simple -- if it could be tweaked to invoke key() just once per item it would be perfect. From __peter__ at web.de Sat Jan 7 06:45:24 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 07 Jan 2017 12:45:24 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> Message-ID: Paul Rubin wrote: > Peter Otten <__peter__ at web.de> writes: >> How would you implement stopmin()? > > Use itertools.takewhile I should have mentioned that I had already run into that -- let's call it -- off-by-one bug. From __peter__ at web.de Sat Jan 7 06:55:12 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 07 Jan 2017 12:55:12 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <5870a057$0$1585$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steve D'Aprano wrote: > On Sat, 7 Jan 2017 01:04 am, Peter Otten wrote: > >> Example: you are looking for the minimum absolute value in a series of >> integers. As soon as you encounter the first 0 it's unnecessary extra >> work to check the remaining values, but the builtin min() will continue. >> >> The solution is a minimum function that allows the user to specify a stop >> value: >> >>>>> from itertools import count, chain >>>>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) >> 0 >> >> How would you implement stopmin()? > > That depends on whether you want to stop when you find a value *equal* to > the stop value, or *less than* the stop value, or both. > > It depends on whether you want to check for equality or check for any > arbitrary condition. > > It depends on whether you want the stop value to be included or not. (It > looks like you do want it included.) > > But I'd start with this: > > > # not tested > def stopmin(iterable, key=None, stop=None): > it = iter(iterable) > try: > smallest = next(it) > except StopIteration: > raise ValueError('empty iterable has no minimum') > else: > if key is not None: > keyed_smallest = key(smallest) Must test for stop here. > if key is None: > for x in iterable: Replace `iterable` with `it` in the for loops. > if x < smallest: > smallest = x > if x == stop: > break > else: > for x in iterable: > y = key(x) > if y < keyed_smallest: > keyed_smallest = y > smallest = x > if y == stop: > break > return smallest The whole distraction started when I wanted to avoid the straight-forward thing ;) As it turns out there is not as much legwork as I expected, particularly as I'm not interested in the no-key branch. > Another possibility is to create a variant of itertools takewhile: > > def takeuntil(predicate, iterable): > # takeuntil(lambda x: x<5, [1,4,6,4,1]) --> 1 4 6 > for x in iterable: > yield x > if predicate(x): > break > > min(takeuntil(lambda x: x == 0, iterable), key=abs) > > > py> from itertools import count, chain > py> iterable = chain(reversed(range(10)), count()) > py> min(takeuntil(lambda x: x == 0, iterable), key=abs) > 0 > py> iterable = chain(range(-9, 10), count()) > py> min(takeuntil(lambda x: x == 0, iterable), key=abs) > 0 From __peter__ at web.de Sat Jan 7 07:15:22 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 07 Jan 2017 13:15:22 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: Message-ID: Peter Otten wrote: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum function that allows the user to specify a stop > value: Thanks everyone who contributed to this thread! For those interested here's my collection of stopmin() implementations so far. Remember, there should be one ... *obvious* way to do it (emphasis mine). $ cat stopmin_simple.py import functools import operator import re from itertools import groupby from functools import lru_cache def steal_doc(f): def set_doc(g): sub = re.compile(r"\b{}\b".format(f.__name__)).sub g.__doc__ = sub(g.__name__, f.__doc__) return g return set_doc class Stop(Exception): pass def stopmin(items, *, key, stop): """ >>> def g(): ... for i in reversed(range(10)): ... print(10*i) ... yield str(i) >>> stopmin(g(), key=int, stop=5) 90 80 70 60 50 '5' >>> stopmin(g(), key=int, stop=8.5) 90 80 '8' >>> stopmin(g(), key=int, stop=9) 90 '9' >>> stopmin([10, 9, -11, 7, -12], key=abs, stop=0) 7 >>> try: stopmin([], key=abs, stop=0) ... except ValueError: print('OK') OK >>> stopmin("a", key=lambda x: print(x) or "c", stop="b") a 'a' """ def key2(value): result = key(value) if result <= stop: raise Stop(value) return result try: return min(items, key=key2) except Stop as stop: return stop.args[0] def takeuntil(data, pred): '''Take values from data until and including the first that satisfies pred (until data is exhausted if none does).''' for kind, group in groupby(data, pred): if kind: yield next(group) break else: yield from group @steal_doc(stopmin) def stopmin_jussi(data, *, key, stop): key = lru_cache(maxsize=1)(key) return min( takeuntil(data, lambda o: key(o) <= stop), key=key ) @steal_doc(stopmin) def stopmin_wolfgang(iterable, *, key, stop): key = lru_cache(maxsize=1)(key) def take_until(): for e in iterable: yield e if key(e) <= stop: break return min(take_until(), key=key) firstitem = operator.itemgetter(0) @steal_doc(stopmin) def stopmin_du(items, *, key, stop): # decorate, process, undecorate pairs = ((key(item), item) for item in items) pairs = takeuntil(pairs, lambda pair: pair[0] <= stop) return min(pairs, key=firstitem)[1] @functools.total_ordering class Pair: __slots__ = ["key", "value"] def __init__(self, key, value): self.key = key self.value = value def __lt__(self, other): return self.key < other.key @steal_doc(stopmin) def stopmin_du2(items, *, key, stop): pairs = (Pair(key(item), item) for item in items) pairs = takeuntil(pairs, lambda p: p.key <= stop) return min(pairs).value @steal_doc(stopmin) def stopmin_steve(iterable, *, key, stop): it = iter(iterable) try: smallest = next(it) except StopIteration: raise ValueError('empty iterable has no minimum') keyed_smallest = key(smallest) if keyed_smallest <= stop: return smallest for x in it: y = key(x) if y < keyed_smallest: keyed_smallest = y smallest = x if y <= stop: break return smallest @steal_doc(stopmin) def stopmin2(items, key, stop): pairs = ((key(item), item) for item in items) try: minkey, minval = next(pairs) except StopIteration: raise ValueError("stopmin2() arg is an empty sequence") if minkey <= stop: return minval for k, v in pairs: if k < minkey: if k <= stop: return v minkey = k minval = v return minval $ pep8 stopmin_simple.py $ python3 -m doctest stopmin_simple.py From rustompmody at gmail.com Sat Jan 7 10:14:58 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Sat, 7 Jan 2017 07:14:58 -0800 (PST) Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found In-Reply-To: References: <87eg0f3b72.fsf@nightsong.com> <029aea48-3f1f-4a99-8a3b-6e8a906cbdfd@googlegroups.com> Message-ID: On Saturday, January 7, 2017 at 1:42:47 PM UTC+5:30, Jussi Piitulainen wrote: > Rustom Mody writes: > > > On Saturday, Jussi Piitulainen wrote: > >> Paul Rubin writes: > >> > >> > Peter Otten writes: > >> >> How would you implement stopmin()? > >> > > >> > Use itertools.takewhile > >> > >> How? It consumes the crucial stop element: > >> > >> it = iter('what?') > >> list(takewhile(str.isalpha, it)) # ==> ['w', 'h', 'a', 't'] > >> next(it, 42) # ==> 42 > > > > I was also wondering how? > > In a lazy language (eg haskell) with non-strict foldr (reduce but > > rightwards) supplied non-strict operator this is trivial. > > ie in python idiom with reduce being right_reduce > > reduce(operator.mul, [1,2,0,4,...], 1) > > the reduction would stop at the 0 > > Not sure how to simulate this in a strict language like python > > Making fold(r) non-strict by using generators is ok > > How to pass a non-strict operator? > > I think it would have to be some really awkward pseudo-operator that > throws an exception when it encounters its zero, and then reduce (or > something outside reduce) would catch that exception. Something like > that could be done but it would still be awkward. Don't wanna :) > > You switched to a simpler operator. Would Haskell notice that > > def minabs(x, y): return min(x, y, key = abs) > > has a meaningful zero? Surely it has its limits somewhere and then the > programmer needs to supply the information. Over ? multiply has 1 identity and 0 absorbent min has ? as identity and 0 as absorbent If you allow for ? they are quite the same Below I am pretending that 100 = ? Here are two lazy functions: mul.0.y = 0 -- Lazy in y ie y not evaluated mul.x.y = x*y minm.0.y = 0 -- likewise lazy in y minm.x.y = min.x.y Now at the interpreter: ? foldr.minm . 100.[1,2,3,4] 1 : Int ? foldr.minm . 100.[1,2,3,4,0] 0 : Int ? foldr.minm . 100.([1,2,3,4,0]++[1...]) 0 : Int The last expression appended [1,2,3,4,0] to the infinite list of numbers. More succinctly: ? foldr.minm . 100.([1,2,3,4,0]++undefined) 0 : Int Both these are extremal examples of what Peter is asking for ? avoiding an expensive computation From alister.ware at ntlworld.com Sat Jan 7 10:33:22 2017 From: alister.ware at ntlworld.com (alister) Date: Sat, 07 Jan 2017 15:33:22 GMT Subject: Pexpect References: <1324722496@f38.n261.z1.binkp.net> Message-ID: <6T7cA.702399$7m2.516222@fx37.am4> On Fri, 06 Jan 2017 12:00:32 +1200, Iranna Mathapati wrote: > Hi Team, > > How to match latter(caps and small) ,numbers and # symbol in python > pexpect. > > > Thanks, > Iranna M Simple just write a small program -- New screensaver released: Curtains for Windows. From jussi.piitulainen at helsinki.fi Sat Jan 7 11:38:16 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sat, 07 Jan 2017 18:38:16 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <029aea48-3f1f-4a99-8a3b-6e8a906cbdfd@googlegroups.com> Message-ID: Rustom Mody writes: > On a Saturday, Jussi Piitulainen wrote: [snip] >> You switched to a simpler operator. Would Haskell notice that >> >> def minabs(x, y): return min(x, y, key = abs) >> >> has a meaningful zero? Surely it has its limits somewhere and then >> the programmer needs to supply the information. > > Over ? multiply has 1 identity and 0 absorbent > min has ? as identity and 0 as absorbent > If you allow for ? they are quite the same There is nothing like ? in Python ints. Floats would have one, but we can leave empty minimum undefined instead. No worries. > Below I am pretending that 100 = ? Quite silly but fortunately not really relevant. > Here are two lazy functions: > mul.0.y = 0 -- Lazy in y ie y not evaluated > mul.x.y = x*y > > minm.0.y = 0 -- likewise lazy in y > minm.x.y = min.x.y Now I don't see any reason to avoid the actual function that's been the example in this thread: minabs.0.y = 0 minabs.x.y = x if abs.x <= abs.y else y And now I see where the desired behaviour comes from in Haskell. The absorbing clause is redundant, apart from providing the specific stopping condition explicitly. > Now at the interpreter: > ? foldr.minm . 100.[1,2,3,4] > 1 : Int > ? foldr.minm . 100.[1,2,3,4,0] > 0 : Int > ? foldr.minm . 100.([1,2,3,4,0]++[1...]) > 0 : Int > > The last expression appended [1,2,3,4,0] to the infinite list of numbers. > > More succinctly: > ? foldr.minm . 100.([1,2,3,4,0]++undefined) > 0 : Int > > Both these are extremal examples of what Peter is asking for ? avoiding an > expensive computation Ok. Thanks. From jf_byrnes at comcast.net Sat Jan 7 13:22:45 2017 From: jf_byrnes at comcast.net (jim) Date: Sat, 7 Jan 2017 12:22:45 -0600 Subject: Using sudo with pip3? In-Reply-To: <20170107022424.GA68905@cskk.homeip.net> References: <20170107022424.GA68905@cskk.homeip.net> Message-ID: On 01/06/2017 08:24 PM, Cameron Simpson wrote: > On 06Jan2017 23:03, Clint Moyer wrote: Thanks everyone for the advice. Please note in my following comments I am not arguing with anyone, merely trying to understand. I am not a professional programmer, just someone who enjoys programing for my own use/enjoyment and I am the only user of this system. >> Packages supplied by your distribution can be trusted more than packages >> from PyPi. Just my two cents. >> Most distros offer nearly all the useful Python modules directly from the >> repo. > > I would agree with this on the whole. And also that it is generally > better to add modules to your system python via the distro's repo > because that bring benefit to every user on the system, not just yourself. What is "system python"? If I do $ which python I get /usr/bin/python which points to python 2.7xx. So if everything I added was for python 3 either using pip3 or apt-get would I be adding to "system python"? I see that most of the "major" python3 modules I had installed, with the exception of scripy, are in the repository. If I upgraded one of the repository modules using pip3 would I lose the benefits of installing from the repository? I know it seems to be possible as I installed pip3 from the repository. The first time I used it to install a module it informed me there was a more current version available and showed me the command to update. I updated and it seems to be fine. >> Virtual environments are great, but if you want to add libraries to your >> system interpreter I'd recommend a simple sync through your repo. > > I'm directly advocating _not_ adding PyPI packages to the system > interpreter. If nothing else, they may differ in behaviour and > potentially actually break system behaviour. > > Having your on virtualenv is good for: adding packages no provided by > your vendor, adding packages deliberately different from those from your > vendor (eg newer versions with specific bugfixes or extra features), > having an isolated environment for packages (you can make more than one > virtual environment). > > And of course it avoids interfering with your system python install. A number of years ago I had virtualenv installed. At the time I remember it took me a while to get it installed and working. Right now I am working on some scripts to download some financial date using Selenium and paste it into Libreoffice Calc spreadsheets. Will using virtualenv have any effect on me running those scripts? Thanks, Jim > > Cheers, > Cameron Simpson From contact at clintmoyer.com Sat Jan 7 13:39:40 2017 From: contact at clintmoyer.com (Clint Moyer) Date: Sat, 7 Jan 2017 11:39:40 -0700 Subject: Using sudo with pip3? In-Reply-To: References: <20170107022424.GA68905@cskk.homeip.net> Message-ID: All Linux operating systems come with Python installed, with more recent systems such as Arch defaulting /usr/bin/python to Python3, since Python2 discontinued some 7-9 years ago. I believe Ubuntu still defaults that to Python2, however. So when you run pip3 you are attempting to download and install a package from the internet, specifically the PyPi repository. You are deliberately avoiding your Package Repository from your OS in this scenario. That's fine, but when the package requires root privilege to install arbitrary executables and bindings in the root filesystem, you are taking on too much risk to be acceptable. Which is why nearly all current Linux OS systems have these common Python packages ready for instant download, through the respective repo. I find the benefit of adding libraries directly to your system's Python, outweighs the extra effort of setting up a VirtualEnv. When it comes to individual projects, that's when I'll set up the env. Being able to run those commands directly: $ ansible-playbook run.yml Is quite useful. Working in the field, sadly the most common roadblocks I see are difficulties installing through Pip, or even a VirtualEnv. Much better off going through your system, and learning what other dependencies may be needed. -- Clint On Sat, Jan 7, 2017 at 11:22 AM, jim wrote: > On 01/06/2017 08:24 PM, Cameron Simpson wrote: >> >> On 06Jan2017 23:03, Clint Moyer wrote: > > > Thanks everyone for the advice. Please note in my following comments I am > not arguing with anyone, merely trying to understand. I am not a > professional programmer, just someone who enjoys programing for my own > use/enjoyment and I am the only user of this system. > >>> Packages supplied by your distribution can be trusted more than packages >>> from PyPi. Just my two cents. >>> Most distros offer nearly all the useful Python modules directly from the >>> repo. >> >> >> I would agree with this on the whole. And also that it is generally >> better to add modules to your system python via the distro's repo >> because that bring benefit to every user on the system, not just yourself. > > > What is "system python"? If I do $ which python I get /usr/bin/python which > points to python 2.7xx. So if everything I added was for python 3 either > using pip3 or apt-get would I be adding to "system python"? > > I see that most of the "major" python3 modules I had installed, with the > exception of scripy, are in the repository. If I upgraded one of the > repository modules using pip3 would I lose the benefits of installing from > the repository? I know it seems to be possible as I installed pip3 from the > repository. The first time I used it to install a module it informed me > there was a more current version available and showed me the command to > update. I updated and it seems to be fine. > >>> Virtual environments are great, but if you want to add libraries to your >>> system interpreter I'd recommend a simple sync through your repo. >> >> >> I'm directly advocating _not_ adding PyPI packages to the system >> interpreter. If nothing else, they may differ in behaviour and >> potentially actually break system behaviour. >> >> Having your on virtualenv is good for: adding packages no provided by >> your vendor, adding packages deliberately different from those from your >> vendor (eg newer versions with specific bugfixes or extra features), >> having an isolated environment for packages (you can make more than one >> virtual environment). >> >> And of course it avoids interfering with your system python install. > > > A number of years ago I had virtualenv installed. At the time I remember it > took me a while to get it installed and working. Right now I am working on > some scripts to download some financial date using Selenium and paste it > into Libreoffice Calc spreadsheets. Will using virtualenv have any effect > on me running those scripts? > > Thanks, Jim > > >> >> Cheers, >> Cameron Simpson > > > > -- > https://mail.python.org/mailman/listinfo/python-list From ethan at stoneleaf.us Sat Jan 7 14:05:28 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Sat, 07 Jan 2017 11:05:28 -0800 Subject: The hardest problem in computer science... In-Reply-To: <587099fa$0$1610$c3e8da3$5496439d@news.astraweb.com> References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> <587099fa$0$1610$c3e8da3$5496439d@news.astraweb.com> Message-ID: <58713BF8.7080803@stoneleaf.us> On 01/06/2017 11:34 PM, Steve D'Aprano wrote: > On Sat, 7 Jan 2017 12:03 am, Steve D'Aprano wrote: > >> The second hardest problem in computer science is cache invalidation. >> >> The *hardest* problem is naming things. > > After puzzling over this for three days, it suddenly hit me: > > Theme = namedtuple("Theme", "vline tee corner") > > DEFAULT = Theme("? ", "?? ", "?? ") > BOLD = Theme("? ", "?? ", "?? ") > ASCII = Theme("| ", "|- ", "+- ") > > def draw_tree(tree, theme=DEFAULT): > ... Ya know, that looks an /awful/ lot like a collection! Maybe even an Enum? ;) -- 8< ------------------------------------------------------- from aenum import Enum # note the 'a' before the 'enum' :) class Theme(Enum, init='v vr llc'): DEFAULT = "? ", "?? ", "?? " BOLD = "? ", "?? ", "?? " ASCII = "| ", "|- ", "+- " def draw_tree(tree, theme=Theme.DEFAULT): print(theme.v) print(theme.vr) print(theme.v) print(theme.llc) draw_tree(None) -- 8< ------------------------------------------------------- -- ~Ethan~ From cmpython at gmail.com Sat Jan 7 16:40:42 2017 From: cmpython at gmail.com (CM) Date: Sat, 7 Jan 2017 13:40:42 -0800 (PST) Subject: Can't match str/unicode Message-ID: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> This is probably very simple but I get confused when it comes to encoding and am generally rusty. (What follows is in Python 2.7; I know.). I'm scraping a Word docx using win32com and am just trying to do some matching rules to find certain paragraphs that, for testing purposes, equal the word 'match', which I know exists as its own "paragraph" in the target document. First, this is at the top of the file: #!/usr/bin/env python # -*- coding: utf-8 -*- Then this is the relevant code: candidate_text = Paragraph.Range.Text.encode('utf-8') print 'This is candidate_text:', candidate_text print type(candidate_text) print type('match') print candidate_text == 'match' if candidate_text == 'match': # do something... And that section produces this: This is candidate_text: match False and, of course, doesn't enter that "do something" loop since apparently candidate_text != 'match'...even though it seems like it does. So what's going on here? Why isn't a string with the content 'match' equal to another string with the content 'match'? I've also tried it with removing that .encode part and the encoding part at the very top, but then the candidate_text is a unicode object that I also can't get to match to anything. What am I doing wrong? How should I approach this? Thanks. From ethan at stoneleaf.us Sat Jan 7 16:48:53 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Sat, 07 Jan 2017 13:48:53 -0800 Subject: python 3 dict: .keys(), .values(), and .item() Message-ID: <58716245.3010102@stoneleaf.us> In Python 2 we have: dict().keys() \ dict().items() --> separate list() of the results dict().values() / and dict().iter_keys() \ dict().iter_items() --> integrated iter() of the results dict().iter_values() / By "separate list" I mean a snapshot of the dict at the time, and by "integrated iter()" I mean changes to the dict during iteration are seen by the iter. In Python 3 the iter_* methods replaced the list() type methods, which makes sense from the point-of-view of moving to a more iterator based language; however, as a result of that change the typical "iterate over a dict" operation now has a built-in gotcha: modifying the dict during the iteration can now cause exceptions. The solution, of course, is simple: surround the iterator call with list(): list(dict.keys()) list(dict.items()) list(dict.values()) for k, v in list(flag._value2member_map_.items()): ... The solution, however, feels a lot more boilerplate-ish. Either the programmer takes a lot more care to remember the current state of the dict (since it's no longer a snapshot), or "list()" is sprinkled around every iterator access. In other words, what used to be a completely safe operation now is not. Thoughts? -- ~Ethan~ From torriem at gmail.com Sat Jan 7 17:34:51 2017 From: torriem at gmail.com (Michael Torrie) Date: Sat, 7 Jan 2017 15:34:51 -0700 Subject: Using sudo with pip3? In-Reply-To: References: <20170107022424.GA68905@cskk.homeip.net> Message-ID: <4578c74c-c174-aabb-44cf-bf4cfd95ec98@gmail.com> On 01/07/2017 11:39 AM, Clint Moyer wrote: > All Linux operating systems come with Python installed, with more > recent systems such as Arch defaulting /usr/bin/python to Python3, > since Python2 discontinued some 7-9 years ago. Poor choice of words, in my opinion. Python 2 has not received new features for 7-9 years now but it certainly hasn't been "discontinued" and won't be for some years yet, though new programming (and distros) should be with Python 3 now. From __peter__ at web.de Sat Jan 7 18:04:26 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 08 Jan 2017 00:04:26 +0100 Subject: python 3 dict: .keys(), .values(), and .item() References: <58716245.3010102@stoneleaf.us> Message-ID: Ethan Furman wrote: > In Python 2 we have: > > dict().keys() \ > dict().items() --> separate list() of the results > dict().values() / > > and > > dict().iter_keys() \ > dict().iter_items() --> integrated iter() of the results > dict().iter_values() / I guess you didn't use these as often as I did ;) By the way, there are also the virtually unused/unknown dict.viewXXX() methods that are the exact? equivalent to the dict.XXX() methods in Python 3. > By "separate list" I mean a snapshot of the dict at the time, and by > "integrated iter()" I mean changes to the dict during iteration are > seen by the iter. > > In Python 3 the iter_* methods replaced the list() type methods, which > makes sense from the point-of-view of moving to a more iterator based > language; however, as a result of that change the typical "iterate over > a dict" operation now has a built-in gotcha: modifying the dict during > the iteration can now cause exceptions. > > The solution, of course, is simple: surround the iterator call with > list(): > > list(dict.keys()) > list(dict.items()) > list(dict.values()) > > for k, v in list(flag._value2member_map_.items()): > ... > > The solution, however, feels a lot more boilerplate-ish. Either the > programmer takes a lot more care to remember the current state of the dict > (since it's no longer a snapshot), or "list()" is sprinkled around every > iterator access. > > In other words, what used to be a completely safe operation now is not. > > Thoughts? Is code that was written for Python 3 riddled with list(...) calls? That's not my impression. Do you see RuntimeError: dictionary changed size during iteration regularly? I don't, and while I wouldn't have changed the dict interface I think Python 3's items() and values() -- who uses keys()? -- are clearly the better defaults. From ethan at stoneleaf.us Sat Jan 7 18:34:16 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Sat, 07 Jan 2017 15:34:16 -0800 Subject: python 3 dict: .keys(), .values(), and .item() In-Reply-To: References: <58716245.3010102@stoneleaf.us> Message-ID: <58717AF8.4040901@stoneleaf.us> On 01/07/2017 03:04 PM, Peter Otten wrote: > Ethan Furman wrote: > >> In Python 2 we have: >> >> dict().keys() \ >> dict().items() --> separate list() of the results >> dict().values() / >> >> and >> >> dict().iter_keys() \ >> dict().iter_items() --> integrated iter() of the results >> dict().iter_values() / > > I guess you didn't use these as often as I did ;) > > By the way, there are also the virtually unused/unknown dict.viewXXX() > methods that are the exact? equivalent to the dict.XXX() methods in Python > 3. Ah, right -- in fact, those are the ones that got transplanted, I think. >> In other words, what used to be a completely safe operation now is not. >> >> Thoughts? > > Is code that was written for Python 3 riddled with list(...) calls? Generally, or just around dicts? I know I've placed list() around a few dict calls (when I am going to modify the dict in the loop) or I make a separate list to save the modifications and the bulk change in a separate loop. > Do you see > > RuntimeError: dictionary changed size during iteration > > regularly? No, thank goodness. Still embarrassing when it happens, though. :( Thinking on it some more, I'm pretty sure I would have ended up with another exception of a different flavor, so overall no extra harm done. ;) -- ~Ethan~ From rosuav at gmail.com Sat Jan 7 18:39:58 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 8 Jan 2017 10:39:58 +1100 Subject: Using sudo with pip3? In-Reply-To: <4578c74c-c174-aabb-44cf-bf4cfd95ec98@gmail.com> References: <20170107022424.GA68905@cskk.homeip.net> <4578c74c-c174-aabb-44cf-bf4cfd95ec98@gmail.com> Message-ID: On Sun, Jan 8, 2017 at 9:34 AM, Michael Torrie wrote: > On 01/07/2017 11:39 AM, Clint Moyer wrote: >> All Linux operating systems come with Python installed, with more >> recent systems such as Arch defaulting /usr/bin/python to Python3, >> since Python2 discontinued some 7-9 years ago. > > Poor choice of words, in my opinion. Python 2 has not received new > features for 7-9 years now but it certainly hasn't been "discontinued" > and won't be for some years yet, though new programming (and distros) > should be with Python 3 now. Also, /usr/bin/python shouldn't be Python 3. https://www.python.org/dev/peps/pep-0394/ But various distros are moving towards "don't have Python 2 installed by default", which consequently means "no system scripts depend on Python 2". ChrisA From rosuav at gmail.com Sat Jan 7 18:42:06 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 8 Jan 2017 10:42:06 +1100 Subject: Can't match str/unicode In-Reply-To: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> References: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> Message-ID: On Sun, Jan 8, 2017 at 8:40 AM, CM wrote: > > This is candidate_text: match > > > False > > and, of course, doesn't enter that "do something" loop since apparently candidate_text != 'match'...even though it seems like it does. > > So what's going on here? Why isn't a string with the content 'match' equal to another string with the content 'match'? What happens if you print the repr of each string? Or, if one of them truly is a literal, just print the repr of the one you got from win32com. ChrisA From cs at zip.com.au Sat Jan 7 18:51:39 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 8 Jan 2017 10:51:39 +1100 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: <20170107235139.GA45969@cskk.homeip.net> On 07Jan2017 12:22, jim wrote: >On 01/06/2017 08:24 PM, Cameron Simpson wrote: >>On 06Jan2017 23:03, Clint Moyer wrote: >>>Packages supplied by your distribution can be trusted more than packages >>>from PyPi. Just my two cents. >>>Most distros offer nearly all the useful Python modules directly from the >>>repo. >> >>I would agree with this on the whole. And also that it is generally >>better to add modules to your system python via the distro's repo >>because that bring benefit to every user on the system, not just yourself. > >What is "system python"? If I do $ which python I get /usr/bin/python >which points to python 2.7xx. So if everything I added was for python >3 either using pip3 or apt-get would I be adding to "system python"? Whatever python(s) came from the vendor. If they're in /bin or /usr/bin, then they will almost certainly be the vendor python. The thing is, many distros will have an aim to move to python 3 as their python for tooling (python 2 is essentially maintenance only - see PEP 404), and there will be a mix of python 2 and python 3 programs on your system, both core vendor-made tools (eg yum on redhat/centos) or third party but vendor supplied stuff (== anything else that apt-get will give you). Basicly, for Ubuntu the "system" python is anything supplied by apt-get and therefore _managed_ by apt. This is why you should avoid "become root and run pip3 install" on the whole - you may well tread in the space owned by the vendor because Ubuntu supply many python packages. Normally a vendor will not install its packages in /opt or /usr/local or in /home/wherever; that leaves such spaces as safe for third party stuff installs. So, instead, make a python3 virtualenv as yourself and use _its_ pip3 for installs; they will land in the virtualenv, away from the vendor files. When you make a virtualenv you base it on an existing python; if you tell it to use /usr/bin/python3 that will be the underlying python executable, but the "python3" from inside the virutalenv will be configured to use the virtualenv's packages. I generally configure my virtualenvs to leverag the system's packages, as this lets me benefit if more vendor supplied packages are added. (Some virtualenvs are not, in order to isolate them from changes.) Here's an example: $ mkdir ~/var ~/var/venv # where I keep my virtualenvs $ virtualenv -p /usr/bin/python3 --system-site-packages ~/var/venv/3 $ ~/var/venv/3/bin/python3 # invokes the virtualenv python3 $ ~/var/venv/3/bin/pip3 # invokes the virtualenv pip3 $ ln -s ~/var/venv/3/bin/python3 ~/bin/. # make these my default $ ln -s ~/var/venv/3/bin/pip3 ~/bin/. From this point on "pip3" should execute your own $HOME/bin/pip3, which comes from the virtualenv and installs in ~/var/venv/3/lib/... In this way: - you get whatever vendor supplied packages there are, including any updates - you can install freely with pip3 safe in the knowledge that it will be in ~/var/venv/3, away from the system packages; pip3 install packages will override any vendor supplied one for the venv python3 >A number of years ago I had virtualenv installed. At the time I remember it >took me a while to get it installed and working. It is almost a one liner these days, see example above. >Right now I am working on some scripts to download some financial date using >Selenium and paste it into Libreoffice Calc spreadsheets. Will using >virtualenv have any effect on me running those scripts? I wouldn't think so. Especially if they already work. Cheers, Cameron Simpson From contact at clintmoyer.com Sat Jan 7 18:58:45 2017 From: contact at clintmoyer.com (Clint Moyer) Date: Sat, 7 Jan 2017 16:58:45 -0700 Subject: Using sudo with pip3? Message-ID: Not sure how you guys got this thread so far off topic, but I think it is valuable to present the current situation in the context of Jim's sudo question. Staying on topic, the emphasis should be on taking the path of least resistance with your current OS. The only thing to be gleaned from PEP394 is that users should not put faith or expectations in what their /usr/bin/python symlink points to. Most systems point to Python2, but it is not guaranteed. So to minimize your issues with installing Python packages, take the path of least resistance and install through your system repo. And use Python2 or Python3 explicitly to avoid conflicts. -- Clint On Sat, Jan 7, 2017 at 4:39 PM, Chris Angelico wrote: > On Sun, Jan 8, 2017 at 9:34 AM, Michael Torrie wrote: >> On 01/07/2017 11:39 AM, Clint Moyer wrote: >>> All Linux operating systems come with Python installed, with more >>> recent systems such as Arch defaulting /usr/bin/python to Python3, >>> since Python2 discontinued some 7-9 years ago. >> >> Poor choice of words, in my opinion. Python 2 has not received new >> features for 7-9 years now but it certainly hasn't been "discontinued" >> and won't be for some years yet, though new programming (and distros) >> should be with Python 3 now. > > Also, /usr/bin/python shouldn't be Python 3. > > https://www.python.org/dev/peps/pep-0394/ > > But various distros are moving towards "don't have Python 2 installed > by default", which consequently means "no system scripts depend on > Python 2". > > ChrisA From jackrharvey at hotmail.com Sat Jan 7 19:15:30 2017 From: jackrharvey at hotmail.com (Jack Harvey) Date: Sun, 8 Jan 2017 00:15:30 +0000 Subject: Error with math.sqrt Message-ID: I'm starting out with Python 3.5. My current frustration is with: >>> math.sqrt(25) Traceback (most recent call last): File "", line 1, in math.sqrt(25) NameError: name 'math' is not defined >>> Advice? Jack From rosuav at gmail.com Sat Jan 7 19:22:02 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 8 Jan 2017 11:22:02 +1100 Subject: Using sudo with pip3? In-Reply-To: <20170107235139.GA45969@cskk.homeip.net> References: <20170107235139.GA45969@cskk.homeip.net> Message-ID: On Sun, Jan 8, 2017 at 10:51 AM, Cameron Simpson wrote: > Here's an example: > > $ mkdir ~/var ~/var/venv # where I keep my virtualenvs > $ virtualenv -p /usr/bin/python3 --system-site-packages ~/var/venv/3 > $ ~/var/venv/3/bin/python3 # invokes the virtualenv python3 > $ ~/var/venv/3/bin/pip3 # invokes the virtualenv pip3 > $ ln -s ~/var/venv/3/bin/python3 ~/bin/. # make these my default > $ ln -s ~/var/venv/3/bin/pip3 ~/bin/. > > From this point on "pip3" should execute your own $HOME/bin/pip3, which > comes from the virtualenv and installs in ~/var/venv/3/lib/... Can you do the same with the standard library 'venv' module? I generally recommend and prefer that over the third-party 'virtualenv', which achieves what it does via a series of hacks. ChrisA From rosuav at gmail.com Sat Jan 7 19:23:29 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 8 Jan 2017 11:23:29 +1100 Subject: Error with math.sqrt In-Reply-To: References: Message-ID: On Sun, Jan 8, 2017 at 11:15 AM, Jack Harvey wrote: > I'm starting out with Python 3.5. My current frustration is with: > > >>>> math.sqrt(25) > Traceback (most recent call last): > File "", line 1, in > math.sqrt(25) > NameError: name 'math' is not defined >>>> > > > Advice? A lot of Python's features are in what's called the "standard library" - a set of modules that you can call up with the 'import' statement. In this case, all you need is to start with this line: import math Once you do that, everything in the math module (including math.sqrt) should be available. Enjoy! ChrisA From cs at zip.com.au Sat Jan 7 19:48:51 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 8 Jan 2017 11:48:51 +1100 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: <20170108004851.GA32483@cskk.homeip.net> On 08Jan2017 11:22, Chris Angelico wrote: >On Sun, Jan 8, 2017 at 10:51 AM, Cameron Simpson wrote: >> Here's an example: >> >> $ mkdir ~/var ~/var/venv # where I keep my virtualenvs >> $ virtualenv -p /usr/bin/python3 --system-site-packages ~/var/venv/3 >> $ ~/var/venv/3/bin/python3 # invokes the virtualenv python3 >> $ ~/var/venv/3/bin/pip3 # invokes the virtualenv pip3 >> $ ln -s ~/var/venv/3/bin/python3 ~/bin/. # make these my default >> $ ln -s ~/var/venv/3/bin/pip3 ~/bin/. >> >> From this point on "pip3" should execute your own $HOME/bin/pip3, which >> comes from the virtualenv and installs in ~/var/venv/3/lib/... > >Can you do the same with the standard library 'venv' module? I >generally recommend and prefer that over the third-party 'virtualenv', >which achieves what it does via a series of hacks. Probably? Haven't tried. I thought the venv module was essentially virtualenv's innards presented as a module, but have never tested it. Do I need to retire my "venv" shell script (convenience wrapper for the virtualenv command)? Cheers, Cameron Simpson From steve+python at pearwood.info Sat Jan 7 19:48:56 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 08 Jan 2017 11:48:56 +1100 Subject: python 3 dict: .keys(), .values(), and .item() References: <58716245.3010102@stoneleaf.us> Message-ID: <58718c7a$0$1587$c3e8da3$5496439d@news.astraweb.com> On Sun, 8 Jan 2017 08:48 am, Ethan Furman wrote: > In Python 2 we have: > > dict().keys() \ > dict().items() --> separate list() of the results > dict().values() / > > and > > dict().iter_keys() \ > dict().iter_items() --> integrated iter() of the results > dict().iter_values() / You missed another group of methods: viewkeys() viewitems() viewvalues() which are integrated, iterable, set-like views of the keys/items/values. > By "separate list" I mean a snapshot of the dict at the time, and by > "integrated iter()" I mean changes to the dict during iteration are > seen by the iter. > > In Python 3 the iter_* methods replaced the list() type methods, Its actually the view* methods. > which > makes sense from the point-of-view of moving to a more iterator based > language; however, as a result of that change the typical "iterate over > a dict" operation now has a built-in gotcha: modifying the dict during > the iteration can now cause exceptions. Even in Python 2, modifying the dict during iteration can cause exceptions: for key in adict: # iterate over the dict directly ... As usual, the standard rule applies: don't add or remove elements of a collection while you are iterating over it. This doesn't work either: for i, x in enumerate(alist): if condition(x): del alist(i) and has a similar solution: for i, x in enumerate(list(alist)): ... except that's more commonly written using slicing: for i, x in enumerate(alist[:]): ... For both dicts and lists, it is safe to modify existing elements: adict[key] = new_value # provided key already exists alist[index] = new_value are both safe, but insertions and deletions are not. > The solution, of course, is simple: surround the iterator call with > list(): There's nothing magical about list. You could use tuple, or (sometimes) set or frozenset. But list is good. > list(dict.keys()) > list(dict.items()) > list(dict.values()) > > for k, v in list(flag._value2member_map_.items()): > ... > > The solution, however, feels a lot more boilerplate-ish. Either the > programmer takes a lot more care to remember the current state of the dict > (since it's no longer a snapshot), or "list()" is sprinkled around every > iterator access. Not *every* view access. Only the ones where you insert/delete elements. > In other words, what used to be a completely safe operation now is not. > > Thoughts? The old Python 2 keys/values/items methods used to make a copy of the elements, whether you needed a copy or not. Now making a copy is your responsibility. That feels more Pythonic to me. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Sat Jan 7 19:58:51 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 08 Jan 2017 11:58:51 +1100 Subject: Can't match str/unicode References: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> Message-ID: <58718ecd$0$1611$c3e8da3$5496439d@news.astraweb.com> On Sun, 8 Jan 2017 08:40 am, CM wrote: > So what's going on here? Why isn't a string with the content 'match' equal > to another string with the content 'match'? You don't know that the content is 'match'. All you know is that when printed, it *looks like* 'match'. Hint: s = 'match ' print 'match', s # they look the same print 'match' == s # but they aren't the same Start by printing repr(candidate_text) and see what you really have. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From jf_byrnes at comcast.net Sat Jan 7 20:02:14 2017 From: jf_byrnes at comcast.net (jim) Date: Sat, 7 Jan 2017 19:02:14 -0600 Subject: Using sudo with pip3? In-Reply-To: References: <20170107022424.GA68905@cskk.homeip.net> Message-ID: On 01/07/2017 03:17 PM, Dennis Lee Bieber wrote: > On Sat, 7 Jan 2017 12:22:45 -0600, jim declaimed > the following: > >> >> What is "system python"? If I do $ which python I get /usr/bin/python >> which points to python 2.7xx. So if everything I added was for python 3 >> either using pip3 or apt-get would I be adding to "system python"? >> > > What does > > which python3 > > return? jfb at jims-1604:~$ which python3 /usr/bin/python3 Regards, Jim > wulfraed at Jessie:~$ which python > /usr/bin/python > wulfraed at Jessie:~$ which python3 > /usr/bin/python3 > wulfraed at Jessie:~$ python > Python 2.7.9 (default, Mar 1 2015, 12:57:24) > [GCC 4.9.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> > wulfraed at Jessie:~$ python3 > Python 3.4.2 (default, Oct 8 2014, 10:45:20) > [GCC 4.9.1] on linux > Type "help", "copyright", "credits" or "license" for more information. >>>> > wulfraed at Jessie:~$ > > I consider both of those to be "system" as I don't recall explicitly > asking for either (Debian running in VirtualBox) > From jf_byrnes at comcast.net Sat Jan 7 20:45:17 2017 From: jf_byrnes at comcast.net (jim) Date: Sat, 7 Jan 2017 19:45:17 -0600 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: On 01/07/2017 05:58 PM, Clint Moyer wrote: > Not sure how you guys got this thread so far off topic, but I think it > is valuable to present the current situation in the context of Jim's > sudo question. Staying on topic, the emphasis should be on taking the > path of least resistance with your current OS. The only thing to be > gleaned from PEP394 is that users should not put faith or expectations > in what their /usr/bin/python symlink points to. Most systems point to > Python2, but it is not guaranteed. > > So to minimize your issues with installing Python packages, take the > path of least resistance and install through your system repo. And use > Python2 or Python3 explicitly to avoid conflicts. > > -- > Clint > As I mentioned in another post, most of the more popular modules I had installed on my old system using pip are available in the repository and I will use the repository to install them on the new system. I now understand that using sudo is a bad idea. One question from the earlier post that did not get answered concerned upgrading a repository installed module with pip. To get started on the new system I installed pip3 from the repository. The first time I used it to install a module it said a newer version was available and gave the command to update it. What are the consequences of using pip to upgrade repository installed modules? I ask because 16.04 is LTS and won't be getting version upgrades unless they are security related. Also pandas is in the repositories but the module pandas-datareader, which I may need to use, is not. Regards, Jim > > On Sat, Jan 7, 2017 at 4:39 PM, Chris Angelico wrote: >> On Sun, Jan 8, 2017 at 9:34 AM, Michael Torrie wrote: >>> On 01/07/2017 11:39 AM, Clint Moyer wrote: >>>> All Linux operating systems come with Python installed, with more >>>> recent systems such as Arch defaulting /usr/bin/python to Python3, >>>> since Python2 discontinued some 7-9 years ago. >>> >>> Poor choice of words, in my opinion. Python 2 has not received new >>> features for 7-9 years now but it certainly hasn't been "discontinued" >>> and won't be for some years yet, though new programming (and distros) >>> should be with Python 3 now. >> >> Also, /usr/bin/python shouldn't be Python 3. >> >> https://www.python.org/dev/peps/pep-0394/ >> >> But various distros are moving towards "don't have Python 2 installed >> by default", which consequently means "no system scripts depend on >> Python 2". >> >> ChrisA From plucena24 at gmail.com Sat Jan 7 20:56:30 2017 From: plucena24 at gmail.com (Pablo Lucena) Date: Sat, 7 Jan 2017 17:56:30 -0800 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found In-Reply-To: References: <87eg0f3b72.fsf@nightsong.com> <029aea48-3f1f-4a99-8a3b-6e8a906cbdfd@googlegroups.com> Message-ID: How about using the second usage of builtin iter()? In [92]: iter? Docstring: iter(iterable) -> iterator iter(callable, sentinel) -> iterator Get an iterator from an object. In the first form, the argument must supply its own iterator, or be a sequence. *In the second form, the callable is called until it returns the sentinel. <<<-----this one <<<-------* Type: builtin_function_or_method In [88]: numbers Out[88]: [1, 9, 8, 11, 22, 4, 0, 3, 5, 6] # create iterator over the numbers to make callable simple # you may pre-sort or do w/e as needed of course In [89]: numbers_it = iter(numbers) # callable passed into iter - you may customize this # using functools.partial if need to add function arguments In [90]: def grab_until(): ...: return next(numbers_it) ...: # here 0 is the 'sentinel' ('int()' would work as well as you have # the iterator produced by iter() here stops as soon as sentinel value # is encountered In [91]: list(iter(grab_until, 0)) Out[91]: [1, 9, 8, 11, 22, 4] Hope this helps Pablo On Sat, Jan 7, 2017 at 8:38 AM, Jussi Piitulainen < jussi.piitulainen at helsinki.fi> wrote: > Rustom Mody writes: > > On a Saturday, Jussi Piitulainen wrote: > > [snip] > > >> You switched to a simpler operator. Would Haskell notice that > >> > >> def minabs(x, y): return min(x, y, key = abs) > >> > >> has a meaningful zero? Surely it has its limits somewhere and then > >> the programmer needs to supply the information. > > > > Over ? multiply has 1 identity and 0 absorbent > > min has ? as identity and 0 as absorbent > > If you allow for ? they are quite the same > > There is nothing like ? in Python ints. Floats would have one, but we > can leave empty minimum undefined instead. No worries. > > > Below I am pretending that 100 = ? > > Quite silly but fortunately not really relevant. > > > Here are two lazy functions: > > mul.0.y = 0 -- Lazy in y ie y not evaluated > > mul.x.y = x*y > > > > minm.0.y = 0 -- likewise lazy in y > > minm.x.y = min.x.y > > Now I don't see any reason to avoid the actual function that's been the > example in this thread: > > minabs.0.y = 0 > minabs.x.y = x if abs.x <= abs.y else y > > And now I see where the desired behaviour comes from in Haskell. The > absorbing clause is redundant, apart from providing the specific > stopping condition explicitly. > > > Now at the interpreter: > > ? foldr.minm . 100.[1,2,3,4] > > 1 : Int > > ? foldr.minm . 100.[1,2,3,4,0] > > 0 : Int > > ? foldr.minm . 100.([1,2,3,4,0]++[1...]) > > 0 : Int > > > > The last expression appended [1,2,3,4,0] to the infinite list of numbers. > > > > More succinctly: > > ? foldr.minm . 100.([1,2,3,4,0]++undefined) > > 0 : Int > > > > Both these are extremal examples of what Peter is asking for ? avoiding > an > > expensive computation > > Ok. Thanks. > -- > https://mail.python.org/mailman/listinfo/python-list > -- *Pablo Lucena* From contact at clintmoyer.com Sat Jan 7 21:07:55 2017 From: contact at clintmoyer.com (Clint Moyer) Date: Sat, 7 Jan 2017 19:07:55 -0700 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: I would lightly advise against, assuming both Pip and your package manager are trying to accomplish nearly the same thing. Stick with updating through the repo. If you find that the version your OS provides is out-of-date compared to what's on PyPi or Github, then you might want to remove from your OS and re-install through Pip, for those discrete cases. That's the platform agnostic route. -- Clint On Sat, Jan 7, 2017 at 6:45 PM, jim wrote: > On 01/07/2017 05:58 PM, Clint Moyer wrote: >> >> Not sure how you guys got this thread so far off topic, but I think it >> is valuable to present the current situation in the context of Jim's >> sudo question. Staying on topic, the emphasis should be on taking the >> path of least resistance with your current OS. The only thing to be >> gleaned from PEP394 is that users should not put faith or expectations >> in what their /usr/bin/python symlink points to. Most systems point to >> Python2, but it is not guaranteed. >> >> So to minimize your issues with installing Python packages, take the >> path of least resistance and install through your system repo. And use >> Python2 or Python3 explicitly to avoid conflicts. >> >> -- >> Clint >> > > As I mentioned in another post, most of the more popular modules I had > installed on my old system using pip are available in the repository and I > will use the repository to install them on the new system. I now understand > that using sudo is a bad idea. > > One question from the earlier post that did not get answered concerned > upgrading a repository installed module with pip. To get started on the new > system I installed pip3 from the repository. The first time I used it to > install a module it said a newer version was available and gave the command > to update it. What are the consequences of using pip to upgrade repository > installed modules? > > I ask because 16.04 is LTS and won't be getting version upgrades unless they > are security related. Also pandas is in the repositories but the module > pandas-datareader, which I may need to use, is not. > > Regards, Jim > > >> >> On Sat, Jan 7, 2017 at 4:39 PM, Chris Angelico wrote: >>> >>> On Sun, Jan 8, 2017 at 9:34 AM, Michael Torrie wrote: >>>> >>>> On 01/07/2017 11:39 AM, Clint Moyer wrote: >>>>> >>>>> All Linux operating systems come with Python installed, with more >>>>> recent systems such as Arch defaulting /usr/bin/python to Python3, >>>>> since Python2 discontinued some 7-9 years ago. >>>> >>>> >>>> Poor choice of words, in my opinion. Python 2 has not received new >>>> features for 7-9 years now but it certainly hasn't been "discontinued" >>>> and won't be for some years yet, though new programming (and distros) >>>> should be with Python 3 now. >>> >>> >>> Also, /usr/bin/python shouldn't be Python 3. >>> >>> https://www.python.org/dev/peps/pep-0394/ >>> >>> But various distros are moving towards "don't have Python 2 installed >>> by default", which consequently means "no system scripts depend on >>> Python 2". >>> >>> ChrisA > > > > -- > https://mail.python.org/mailman/listinfo/python-list From cs at zip.com.au Sat Jan 7 21:31:01 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 8 Jan 2017 13:31:01 +1100 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: <20170108023101.GA70579@cskk.homeip.net> On 07Jan2017 19:02, jim wrote: >On 01/07/2017 03:17 PM, Dennis Lee Bieber wrote: >>On Sat, 7 Jan 2017 12:22:45 -0600, jim declaimed >>the following: >>>What is "system python"? If I do $ which python I get /usr/bin/python >>>which points to python 2.7xx. So if everything I added was for python 3 >>>either using pip3 or apt-get would I be adding to "system python"? >> >> What does >> which python3 >>return? > >jfb at jims-1604:~$ which python3 >/usr/bin/python3 That is the system (Ubuntu supplied) python3. /usr/bin belongs to the vendor. Cheers, Cameron Simpson From cs at zip.com.au Sat Jan 7 21:42:14 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 8 Jan 2017 13:42:14 +1100 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: <20170108024214.GA78154@cskk.homeip.net> On 07Jan2017 19:45, jim wrote: >On 01/07/2017 05:58 PM, Clint Moyer wrote: >>So to minimize your issues with installing Python packages, take the >>path of least resistance and install through your system repo. And use >>Python2 or Python3 explicitly to avoid conflicts. > >As I mentioned in another post, most of the more popular modules I had >installed on my old system using pip are available in the repository >and I will use the repository to install them on the new system. I now >understand that using sudo is a bad idea. Cool. >One question from the earlier post that did not get answered concerned >upgrading a repository installed module with pip. I would recommend not. As soon as you get there: - if the vendor updates it, use apt-get (I know this doesn't fit your situation with Ubuntu 16.04 LTS) - if you want a more modern version, now is the time to use virtualenv The thing about LTS is that the vendor guarentees its stability. If you upgrade the vendor installed package using /usr/bin/pip (or pip3) as root, you're replacing the vendor supplied module with a newer one, which may break vendor supplied packages which use that module expected the _old_ behaviour. So endeavour to leave the vendor suppplied stuff entirely alone except for apt-get style updates. Instead, make a virtualenv an upgrade it with a newer package. >To get started on the new system I installed pip3 from the repository. The >first time I used it to install a module it said a newer version was available >and gave the command to update it. What are the consequences of using pip to >upgrade repository installed modules? In theory, if the newer version of the module installs a breaking change it can break things in the system which might use that module and expect its old behaviour. Also, _sometimes_, vendors supply patches versions of packages, possibly including python modules. If they're modified/patched, there is probably a reason. You'd be undoing that patch. >I ask because 16.04 is LTS and won't be getting version upgrades >unless they are security related. Also pandas is in the repositories but the >module pandas-datareader, which I may need to use, is not. Again: make a virtualenv as yourself. Its pip can be run as yourself, therefore cannot accidentally modify system module. It's pip can freely upgrade a older module. If you install a module _not_ supplied by the vendor, the virtualenv pip can freely do that, and _also_ upgrade a required module should that be required (PyPI package definitions let a module specify required revisions of other modules it may use). Cheers, Cameron Simpson From no.email at nospam.invalid Sat Jan 7 22:46:10 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 07 Jan 2017 19:46:10 -0800 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> Message-ID: <87a8b22ozh.fsf@nightsong.com> Jussi Piitulainen writes: >> Use itertools.takewhile > How? It consumes the crucial stop element: Oh yucch, you're right, it takes it from both sides. How about this: from itertools import takewhile, islice def minabs(xs): a = iter(xs) m = min(map(abs,takewhile(lambda x: x!=0, a))) z = list(islice(a,1)) if z: return 0 return m From jf_byrnes at comcast.net Sat Jan 7 23:26:47 2017 From: jf_byrnes at comcast.net (jim) Date: Sat, 7 Jan 2017 22:26:47 -0600 Subject: Using sudo with pip3? In-Reply-To: <20170108024214.GA78154@cskk.homeip.net> References: <20170108024214.GA78154@cskk.homeip.net> Message-ID: On 01/07/2017 08:42 PM, Cameron Simpson wrote: > On 07Jan2017 19:45, jim wrote: >> On 01/07/2017 05:58 PM, Clint Moyer wrote: >>> So to minimize your issues with installing Python packages, take the >>> path of least resistance and install through your system repo. And use >>> Python2 or Python3 explicitly to avoid conflicts. >> >> As I mentioned in another post, most of the more popular modules I had >> installed on my old system using pip are available in the repository >> and I will use the repository to install them on the new system. I now >> understand that using sudo is a bad idea. > > Cool. > >> One question from the earlier post that did not get answered concerned >> upgrading a repository installed module with pip. > > I would recommend not. As soon as you get there: > > - if the vendor updates it, use apt-get (I know this doesn't fit your > situation with Ubuntu 16.04 LTS) > > - if you want a more modern version, now is the time to use virtualenv > > The thing about LTS is that the vendor guarentees its stability. If you > upgrade the vendor installed package using /usr/bin/pip (or pip3) as > root, you're replacing the vendor supplied module with a newer one, > which may break vendor supplied packages which use that module expected > the _old_ behaviour. > > So endeavour to leave the vendor suppplied stuff entirely alone except > for apt-get style updates. > > Instead, make a virtualenv an upgrade it with a newer package. > >> To get started on the new system I installed pip3 from the repository. >> The first time I used it to install a module it said a newer version >> was available and gave the command to update it. What are the >> consequences of using pip to upgrade repository installed modules? > > In theory, if the newer version of the module installs a breaking change > it can break things in the system which might use that module and expect > its old behaviour. Also, _sometimes_, vendors supply patches versions of > packages, possibly including python modules. If they're > modified/patched, there is probably a reason. You'd be undoing that patch. > >> I ask because 16.04 is LTS and won't be getting version upgrades >> unless they are security related. Also pandas is in the repositories >> but the module pandas-datareader, which I may need to use, is not. > > Again: make a virtualenv as yourself. > > Its pip can be run as yourself, therefore cannot accidentally modify > system module. It's pip can freely upgrade a older module. If you > install a module _not_ supplied by the vendor, the virtualenv pip can > freely do that, and _also_ upgrade a required module should that be > required (PyPI package definitions let a module specify required > revisions of other modules it may use). > > Cheers, > Cameron Simpson You've convinced me. I will have to buckle down and figure out to use virtualenv/venv. I see Chris recommends venv. I read through PEP405 and this link https://docs.python.org/3.5/library/venv.html. I don't pretend to fully understand it all but it does seem to make a good case for using venv over virtualenv. thanks, Jim From cmpython at gmail.com Sat Jan 7 23:31:25 2017 From: cmpython at gmail.com (CM) Date: Sat, 7 Jan 2017 20:31:25 -0800 (PST) Subject: Can't match str/unicode In-Reply-To: References: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> Message-ID: <0ce4fcd4-d183-4b50-84cd-83857d1d8f12@googlegroups.com> On Saturday, January 7, 2017 at 6:42:25 PM UTC-5, Chris Angelico wrote: > What happens if you print the repr of each string? Or, if one of them > truly is a literal, just print the repr of the one you got from > win32com. > > ChrisA Yes, that did it. The repr of that one was, in fact: u'match /r' Thanks! From cmpython at gmail.com Sat Jan 7 23:33:15 2017 From: cmpython at gmail.com (CM) Date: Sat, 7 Jan 2017 20:33:15 -0800 (PST) Subject: Can't match str/unicode In-Reply-To: <58718ecd$0$1611$c3e8da3$5496439d@news.astraweb.com> References: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> <58718ecd$0$1611$c3e8da3$5496439d@news.astraweb.com> Message-ID: <44e508f2-6c8d-41f8-bc2f-eaeda2a35e3a@googlegroups.com> On Saturday, January 7, 2017 at 7:59:01 PM UTC-5, Steve D'Aprano wrote: > On Sun, 8 Jan 2017 08:40 am, CM wrote: > > > So what's going on here? Why isn't a string with the content 'match' equal > > to another string with the content 'match'? > > You don't know that the content is 'match'. All you know is that when > printed, it *looks like* 'match'. > > Hint: > > s = 'match ' > print 'match', s # they look the same > print 'match' == s # but they aren't the same > > > Start by printing repr(candidate_text) and see what you really have. Yes, that did it. The repr of that one was, in fact: u'match /r' Thanks, that did it. Do you happen to know why that /r was appended to the unicode object in this case? From cmpython at gmail.com Sat Jan 7 23:38:30 2017 From: cmpython at gmail.com (CM) Date: Sat, 7 Jan 2017 20:38:30 -0800 (PST) Subject: What is PyOleMissing object? (win32com) Message-ID: <45ed4da7-96ad-45b4-9535-5d73c87e9c1f@googlegroups.com> Trying to manipulate z-order for MSOffice with win32com and wasn't sure what argument was needed. Using help on that ZOrder method gives: >>> Help on method ZOrder in module win32com.client.dynamic: ZOrder(self, ZOrderCmd=) method of win32com.client.CDispatch instance So, what does " mean in this case? From rosuav at gmail.com Sat Jan 7 23:46:48 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 8 Jan 2017 15:46:48 +1100 Subject: Can't match str/unicode In-Reply-To: <0ce4fcd4-d183-4b50-84cd-83857d1d8f12@googlegroups.com> References: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> <0ce4fcd4-d183-4b50-84cd-83857d1d8f12@googlegroups.com> Message-ID: On Sun, Jan 8, 2017 at 3:31 PM, CM wrote: > On Saturday, January 7, 2017 at 6:42:25 PM UTC-5, Chris Angelico wrote: > >> What happens if you print the repr of each string? Or, if one of them >> truly is a literal, just print the repr of the one you got from >> win32com. >> >> ChrisA > > Yes, that did it. The repr of that one was, in fact: > > u'match /r' > > Thanks! Are you sure that's what it was? Copy and paste for accuracy. ChrisA From python at deborahswanson.net Sun Jan 8 00:39:47 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sat, 7 Jan 2017 21:39:47 -0800 Subject: Using namedtuples field names for column indices in a list of lists Message-ID: <004101d26971$a0395ae0$27b23dae@sambora> What I've done so far: with open('E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv', 'r') as infile: ls = list(csv.reader(infile)) lst = namedtuple('lst', ls[0]) where 'ls[0]' is the header row of the csv, and it works perfectly well. 'lst' is a namedtuple instance with each of the column titles as field names. And retrieving values also works: if len(lst.Location.fget(l)) == 0: . . Using the .fget method for each field name returns the value in the row and column, here row 'l' and column 'Location'. So far so good. 'lst.Location.fget(l)' is clunkier than l[lo], but it's recognizable and it works. But I haven't found a way to assign new values to a list element. using namedtuple.fieldname. I think a basic problem is that namedtuples have the properties of tuples, and you can't assign to an existing tuple because they're immutable. Also there's the matter of namedtuple.fieldname being a property and not an index integer, so it doesn't work if you try to use them as indices. If I try: l[lst.Location] = 'xyz' ('l' is a list) I get: TypeError: list indices must be integers, not property I'm a little hopeful that namedtuples has a built-in solution. In addition to the method fget, each field name also has a property fset: fset = {NoneType}None But I can't find anything in the documentation that tells what fset is and how to use it. Another possibility would be to map index integers to the field names in the namedtuple, in a way that could be accessed using the namedtuple's field names. I've looked into using map(), but the documentation is so sketchy I couldn't see a way to do this from what is given. And google's no help. If anyone else has tried to do this, google can't find it. They don't have a string of words that I could find preloaded into their "AI" code that will turn up anything close. Most search attempts just ignored all my search terms except 'namedtuples' and returned general stuff on namedtuples. I looked through a few of them, but nothing I looked at gave any useful solution to this problem. Anybody know how to use .fset? Or a way to assign to list locations by their namedtuples fieldnames and list rows? From jussi.piitulainen at helsinki.fi Sun Jan 8 01:06:52 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sun, 08 Jan 2017 08:06:52 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> Message-ID: Paul Rubin writes: > Jussi Piitulainen writes: >>> Use itertools.takewhile >> How? It consumes the crucial stop element: > > Oh yucch, you're right, it takes it from both sides. How about this: > > from itertools import takewhile, islice > def minabs(xs): > a = iter(xs) > m = min(map(abs,takewhile(lambda x: x!=0, a))) > z = list(islice(a,1)) > if z: return 0 > return m That would return 0 even when there is no 0 in xs at all. (It would also return the absolute value, not a value whose absolute value is minimal, but that is easy to fix.) From steve+comp.lang.python at pearwood.info Sun Jan 8 01:17:41 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Sun, 08 Jan 2017 17:17:41 +1100 Subject: Can't match str/unicode References: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> <58718ecd$0$1611$c3e8da3$5496439d@news.astraweb.com> <44e508f2-6c8d-41f8-bc2f-eaeda2a35e3a@googlegroups.com> Message-ID: <5871d987$0$11114$c3e8da3@news.astraweb.com> On Sunday 08 January 2017 15:33, CM wrote: > On Saturday, January 7, 2017 at 7:59:01 PM UTC-5, Steve D'Aprano wrote: [...] >> Start by printing repr(candidate_text) and see what you really have. > > Yes, that did it. The repr of that one was, in fact: > > u'match /r' Are you sure it is a forward-slash /r rather than backslash \r? > Thanks, that did it. Do you happen to know why that /r was appended to the > unicode object in this case? *shrug* You're the only one that can answer that, because only you know where the text came from. The code you showed: candidate_text = Paragraph.Range.Text.encode('utf-8') is a mystery, because we don't know what Paragraph is or where it comes from, or what .Range.Text does. You mention "scraping a Word docx", but we have no idea how you're scraping it. If I had to guess, I'd guess: - you actually mean \r rather than /r; - paragraphs in Word docs always end with a carriage return \r; - and whoever typed the paragraph accidentally hit the spacebar after typing the word "match". But its just a guess. For all I know, the software you are using to scrape the docx file inserts space/r after everything. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From cs at zip.com.au Sun Jan 8 01:30:29 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 8 Jan 2017 17:30:29 +1100 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: <20170108063029.GA21525@cskk.homeip.net> On 07Jan2017 22:26, jim wrote: >You've convinced me. I will have to buckle down and figure out to use >virtualenv/venv. I see Chris recommends venv. I read through PEP405 and this >link https://docs.python.org/3.5/library/venv.html. I don't pretend to fully >understand it all but it does seem to make a good case for using venv over >virtualenv. Note that it recommends: python3 -m venv ...options... path/to/env_dir which makes sure you're getting a venv based on a specific python executable; in the case above whatever "python3" runs -- you could invoke a different python 3 to use that instead. It looks like aside from saying "python3 -m venv" instead of "virtualenv" my previous example recipe should pretty much work for you. Good luck. Cheers, Cameron Simpson From steve+comp.lang.python at pearwood.info Sun Jan 8 01:42:49 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Sun, 08 Jan 2017 17:42:49 +1100 Subject: Using namedtuples field names for column indices in a list of lists References: <004101d26971$a0395ae0$27b23dae@sambora> Message-ID: <5871df6c$0$1536$c3e8da3$5496439d@news.astraweb.com> On Sunday 08 January 2017 16:39, Deborah Swanson wrote: > What I've done so far: > > with open('E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv', > 'r') as infile: > ls = list(csv.reader(infile)) > lst = namedtuple('lst', ls[0]) > > where 'ls[0]' is the header row of the csv, and it works perfectly well. > 'lst' is a namedtuple instance with each of the column titles as field > names. Are you sure? namedtuple() returns a class, not a list: py> from collections import namedtuple py> names = ['A', 'B', 'C'] py> namedtuple('lst', names) The way namedtuple() is intended to be used is like this: py> from collections import namedtuple py> names = ['A', 'B', 'C'] py> Record = namedtuple('Record', names) py> instance = Record(10, 20, 30) py> print(instance) Record(A=10, B=20, C=30) There is no need to call fget directly to access the individual fields: py> instance.A 10 py> instance.B 20 py> instance[1] # indexing works too 20 which is *much* simpler than: py> Record.A.fget(instance) 10 I think you should be doing something like this: pathname = 'E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv' with open(pathname, 'r') as infile: rows = list(csv.reader(infile)) Record = namedtuple("Record", rows[0]) for row in rows[1:]: # skip the first row, the header row = Record(row) # process this row... if row.location == 0: ... [...] > But I haven't found a way to assign new values to a list element. using > namedtuple.fieldname. I think a basic problem is that namedtuples have > the properties of tuples, and you can't assign to an existing tuple > because they're immutable. Indeed. Being tuples, you have to create a new one. You can do it with slicing, like ordinary tuples, but that's rather clunky: py> print(instance) Record(A=10, B=20, C=30) py> Record(999, *instance[1:]) Record(A=999, B=20, C=30) The recommended way is with the _replace method: py> instance._replace(A=999) Record(A=999, B=20, C=30) py> instance._replace(A=999, C=888) Record(A=999, B=20, C=888) Note that despite the leading underscore, _replace is *not* a private method of the class. It is intentionally documented as public. The leading underscore is so that it won't clash with any field names. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From jussi.piitulainen at helsinki.fi Sun Jan 8 02:01:08 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sun, 08 Jan 2017 09:01:08 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <029aea48-3f1f-4a99-8a3b-6e8a906cbdfd@googlegroups.com> Message-ID: Pablo Lucena writes: > How about using the second usage of builtin iter()? > > In [92]: iter? > Docstring: > iter(iterable) -> iterator > iter(callable, sentinel) -> iterator Nice to learn about that. But it has the same problem as itertools.takewhile: > In [88]: numbers > Out[88]: [1, 9, 8, 11, 22, 4, 0, 3, 5, 6] > > # create iterator over the numbers to make callable simple > # you may pre-sort or do w/e as needed of course > In [89]: numbers_it = iter(numbers) > > # callable passed into iter - you may customize this > # using functools.partial if need to add function arguments > In [90]: def grab_until(): > ...: return next(numbers_it) > ...: > > # here 0 is the 'sentinel' ('int()' would work as well as you have > # the iterator produced by iter() here stops as soon as sentinel value > # is encountered > In [91]: list(iter(grab_until, 0)) > Out[91]: [1, 9, 8, 11, 22, 4] You get the same with numbers = [1, 9, 8, 11, 22, 4], where 0 does not occur. How do you then tell which it was? I think both itertools.takewhile(-, -) and iter(-, -) should have an option to include the sentinel in their output. Then they could be used in situations like this. [The discussion with Rustom about Haskell's lazy evaluation is not related to this, as far as I can see, so I just snipped it from here.] From steve+comp.lang.python at pearwood.info Sun Jan 8 02:37:06 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Sun, 08 Jan 2017 18:37:06 +1100 Subject: Grumpy: Python to Go compiler Message-ID: <5871ec24$0$11119$c3e8da3@news.astraweb.com> Grumpy, an experimental project from Google, transpiles Python code into Go, allowing Python programs to be compiled and run as static binaries using the Go toolchain. http://www.infoworld.com/article/3154624/application-development/google-boosts-python-by-turning-it-into-go.html -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From cmpython at gmail.com Sun Jan 8 02:44:12 2017 From: cmpython at gmail.com (CM) Date: Sat, 7 Jan 2017 23:44:12 -0800 (PST) Subject: Can't match str/unicode In-Reply-To: <5871d987$0$11114$c3e8da3@news.astraweb.com> References: <821e460c-1bf1-4062-9984-03ec1e110fe1@googlegroups.com> <58718ecd$0$1611$c3e8da3$5496439d@news.astraweb.com> <44e508f2-6c8d-41f8-bc2f-eaeda2a35e3a@googlegroups.com> <5871d987$0$11114$c3e8da3@news.astraweb.com> Message-ID: <247a4ce0-9cf3-4620-9a57-bf7e2e17a1e6@googlegroups.com> On Sunday, January 8, 2017 at 1:17:56 AM UTC-5, Steven D'Aprano wrote: > On Sunday 08 January 2017 15:33, CM wrote: > > > On Saturday, January 7, 2017 at 7:59:01 PM UTC-5, Steve D'Aprano wrote: > [...] > >> Start by printing repr(candidate_text) and see what you really have. > > > > Yes, that did it. The repr of that one was, in fact: > > > > u'match /r' > > Are you sure it is a forward-slash /r rather than backslash \r? No, you're right, it was a backslash \r. Should have pasted it. Good catch. From no.email at nospam.invalid Sun Jan 8 04:49:39 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Sun, 08 Jan 2017 01:49:39 -0800 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> Message-ID: <8760lp3mq4.fsf@nightsong.com> Jussi Piitulainen writes: > That would return 0 even when there is no 0 in xs at all. Doesn't look that way to me: >>> minabs([5,3,1,2,4]) 1 From python at deborahswanson.net Sun Jan 8 04:53:00 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 01:53:00 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <5871df6c$0$1536$c3e8da3$5496439d@news.astraweb.com> Message-ID: <000401d26994$ffd151b0$27b23dae@sambora> Steven D'Aprano wrote, on January 07, 2017 10:43 PM > > On Sunday 08 January 2017 16:39, Deborah Swanson wrote: > > > What I've done so far: > > > > with open('E:\\Coding projects\\Pycharm\\Moving\\Moving > 2017 in.csv', > > 'r') as infile: > > ls = list(csv.reader(infile)) > > lst = namedtuple('lst', ls[0]) > > > > where 'ls[0]' is the header row of the csv, and it works perfectly > > well. 'lst' is a namedtuple instance with each of the > column titles as > > field names. > > Are you sure? namedtuple() returns a class, not a list: Yes. 'ls' is defined as 'list(csv.reader(infile))', so ls[0] is the first row from the csv, the header row. 'lst' is the namedtuple. Perhaps what's puzzling you is that the way I've written it, the list of data and the namedtuple are disjoint, and that's the problem. > py> from collections import namedtuple > py> names = ['A', 'B', 'C'] > py> namedtuple('lst', names) > > > The way namedtuple() is intended to be used is like this: > > > py> from collections import namedtuple > py> names = ['A', 'B', 'C'] > py> Record = namedtuple('Record', names) > py> instance = Record(10, 20, 30) > py> print(instance) > Record(A=10, B=20, C=30) > > > There is no need to call fget directly to access the > individual fields: > > py> instance.A > 10 > py> instance.B > 20 > py> instance[1] # indexing works too > 20 > > > which is *much* simpler than: > > py> Record.A.fget(instance) > 10 I don't disagree with anything you've said and shown here. But I want to use the 'instance.A' as a subscript for the list 'ls', and the only way to do that is with .fget(). Believe me, I tried every possible way to use instance.A or instance[1] and no way could I get ls[instance.A]. The problem I'm having here is one of linkage between the named tuple for the column titles and the list that holds the data in the columns. > I think you should be doing something like this: > > pathname = 'E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 > in.csv' with open(pathname, 'r') as infile: > rows = list(csv.reader(infile)) > Record = namedtuple("Record", rows[0]) > for row in rows[1:]: # skip the first row, the header > row = Record(row) > # process this row... > if row.location == 0: > ... Now here you have something I didn't think of: 'row = Record(row)' in a loop through the rows. > [...] > > But I haven't found a way to assign new values to a list element. > > using namedtuple.fieldname. I think a basic problem is that > > namedtuples have the properties of tuples, and you can't assign to an > > existing tuple because they're immutable. > > Indeed. Being tuples, you have to create a new one. You can > do it with slicing, > like ordinary tuples, but that's rather clunky: > > py> print(instance) > Record(A=10, B=20, C=30) > py> Record(999, *instance[1:]) > Record(A=999, B=20, C=30) Very clunky. I don't like modifying standard tuples with slicing, and this is even worse. > The recommended way is with the _replace method: > > py> instance._replace(A=999) > Record(A=999, B=20, C=30) > py> instance._replace(A=999, C=888) > Record(A=999, B=20, C=888) > > > Note that despite the leading underscore, _replace is *not* a > private method of > the class. It is intentionally documented as public. The > leading underscore is > so that it won't clash with any field names. > > > > > -- > Steven > "Ever since I learned about confirmation bias, I've been seeing > it everywhere." - Jon Ronson I will have to work with this. It's entirely possible it will do what I want it to do. The key problem I was having was getting a linkage between the namedtuple and the list of data from the csv. I want to implement a suggestion I got to use a namedtuple made from the header row as subscripts for elements in the list of data, and the example given in the docs: EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') import csv for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))): print(emp.name, emp.title) assumes the field names will be hardcoded. Reading the csv into a list and then trying to use the namedtuple made from the header row as subscripts is how I ended up resorting to 'Record.A.fget(instance)' to read values, and wasn't able to assign them. But assigning the rows of data into namedtuple instances with: Record = namedtuple("Record", rows[0]) for row in rows[1:]: row = Record(row) does look like the linkage I need and wasn't finding the way I was doing it. If 'Record(row)' is the list data and the columns are the same as defined in 'namedtuple("Record", rows[0])', it really should work. And I didn't get it that _replace could be used to assign new values to namedtuples (duh. Pretty clear now that I reread it, and all the row data is in namedtuple instances.) The big question is whether the namedtuple instances can be used as something recognizable as field name subscripts, but that's something I'll just have to try and see what it looks like. The goal is that they'll look like row.Description, row.Location, etc., and I think they will. Thanks Steven. I know stuff that I've already learned and used enough that I'm familiar with it pretty well, but I'm a new enough python coder that I still have to thrash around with something new before I understand it. I hope I get to a point where I'll be more systematic in learning complex new things without a professor to tell me how to do it, but that hasn't quite happened yet. Deborah From no.email at nospam.invalid Sun Jan 8 05:02:02 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Sun, 08 Jan 2017 02:02:02 -0800 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> <8760lp3mq4.fsf@nightsong.com> Message-ID: <871swd3m5h.fsf@nightsong.com> Paul Rubin writes: > Doesn't look that way to me: > >>> minabs([5,3,1,2,4]) > 1 There's a different problem though: >>> minabs([1,2,3,0]) 1 I think Python's version of iterators is actually buggy and at least the first element of the rest of the sequence should be preserved. There are ways to fake it but they're too messy for something like this. It should be the default and might have been a good change for Python 3. def minabs2(xs): def z(): for x in xs: yield x if x==0: break return min((abs(x),x) for x in z())[1] seems to work, but is ugly. Maybe there's something better. From no.email at nospam.invalid Sun Jan 8 05:04:30 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Sun, 08 Jan 2017 02:04:30 -0800 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> <8760lp3mq4.fsf@nightsong.com> <871swd3m5h.fsf@nightsong.com> Message-ID: <87wpe527gx.fsf@nightsong.com> Paul Rubin writes: > seems to work, but is ugly. Maybe there's something better. def minabs2(xs): def z(): for x in xs: yield abs(x), x if x==0: break return min(z())[1] is the same thing but a little bit nicer. From __peter__ at web.de Sun Jan 8 06:01:23 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 08 Jan 2017 12:01:23 +0100 Subject: Using namedtuples field names for column indices in a list of lists References: <5871df6c$0$1536$c3e8da3$5496439d@news.astraweb.com> <000401d26994$ffd151b0$27b23dae@sambora> Message-ID: Deborah Swanson wrote: > to do that is with .fget(). Believe me, I tried every possible way to > use instance.A or instance[1] and no way could I get ls[instance.A]. Sorry, no. To get a list of namedtuple instances use: rows = csv.reader(infile) Record = namedtuple("Record", next(rows)) records = [Record._make(row) for row in rows] If you want a column from a list of records you need to extract it manually: columnA = [record.A for record in records] From __peter__ at web.de Sun Jan 8 06:24:04 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 08 Jan 2017 12:24:04 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> <8760lp3mq4.fsf@nightsong.com> <871swd3m5h.fsf@nightsong.com> <87wpe527gx.fsf@nightsong.com> Message-ID: Paul Rubin wrote: > Paul Rubin writes: >> seems to work, but is ugly. Maybe there's something better. > > def minabs2(xs): > def z(): > for x in xs: > yield abs(x), x > if x==0: break > return min(z())[1] > > is the same thing but a little bit nicer. Yes, that's another variant of the decorate/undecorate approach combined with Wolfgang's local take_until(). The generalized form that cannot rely on the sequence items being orderable would be firstitem = operator.itemgetter(0) def stopmin_paul(items, *, key, stop): def take_until(): for item in items: k = key(item) yield k, item if k <= stop: break return min(take_until(), key=firstitem)[1] From python at deborahswanson.net Sun Jan 8 06:54:36 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 03:54:36 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: Message-ID: <000c01d269a5$fc654020$27b23dae@sambora> Peter Otten wrote, on January 08, 2017 3:01 AM > > Deborah Swanson wrote: > > > to do that is with .fget(). Believe me, I tried every > possible way to > > use instance.A or instance[1] and no way could I get ls[instance.A]. > > Sorry, no. I quite agree, I was describing the dead end I was in from peeling the list of data and the namedtuple from the header row off the csv separately. That was quite obviously the wrong path to take, but I didn't know what a good way would be. > To get a list of namedtuple instances use: > > rows = csv.reader(infile) > Record = namedtuple("Record", next(rows)) > records = [Record._make(row) for row in rows] This is slightly different from Steven's suggestion, and it makes a block of records that I think would be iterable. At any rate all the data from the csv would belong to a single data structure, and that seems inherently a good thing. a = records[i].A , for example And I think that this would produce recognizable field names in my code (which was the original goal) if the following works: records[0] is the header row == ('Description', 'Location', etc.) If I can use records[i].Location for the Location column data in row 'i', then I've got my recognizable-field-name variables. > If you want a column from a list of records you need to > extract it manually: > > columnA = [record.A for record in records] This is very neat. Something like a list comprehension for named tuples? Thanks Peter, I'll try it all tomorrow and see how it goes. PS. I haven't forgotten your defaultdict suggestion, I'm just taking the suggestions I got in the "Cleaning up Conditionals" thread one at a time, and I will get to defaultdict. Then I'll look at all of them and see what final version of the code will work best with all the factors to consider. From jussi.piitulainen at helsinki.fi Sun Jan 8 08:14:03 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sun, 08 Jan 2017 15:14:03 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> <8760lp3mq4.fsf@nightsong.com> Message-ID: Paul Rubin writes: > Jussi Piitulainen writes: >> That would return 0 even when there is no 0 in xs at all. > > Doesn't look that way to me: > > >>> minabs([5,3,1,2,4]) > 1 Sorry about that. I honestly meant to say it would return 1 even when there was a single 0 at the very end. Somehow I got seriously confused. You noticed the actual problem yourself. From jussi.piitulainen at helsinki.fi Sun Jan 8 08:16:16 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sun, 08 Jan 2017 15:16:16 +0200 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> <8760lp3mq4.fsf@nightsong.com> <871swd3m5h.fsf@nightsong.com> Message-ID: Paul Rubin writes: > I think Python's version of iterators is actually buggy and at least > the first element of the rest of the sequence should be preserved. > There are ways to fake it but they're too messy for something like > this. It should be the default and might have been a good change for > Python 3. It could still be added as an option, to both takewhile and iter(_, _). From __peter__ at web.de Sun Jan 8 08:20:51 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 08 Jan 2017 14:20:51 +0100 Subject: Using namedtuples field names for column indices in a list of lists References: <000c01d269a5$fc654020$27b23dae@sambora> Message-ID: Deborah Swanson wrote: > Peter Otten wrote, on January 08, 2017 3:01 AM >> >> Deborah Swanson wrote: >> >> > to do that is with .fget(). Believe me, I tried every > possible way > to >> > use instance.A or instance[1] and no way could I get ls[instance.A]. >> >> Sorry, no. > > I quite agree, I was describing the dead end I was in from peeling the > list of data and the namedtuple from the header row off the csv > separately. That was quite obviously the wrong path to take, but I > didn't know what a good way would be. > >> To get a list of namedtuple instances use: >> >> rows = csv.reader(infile) >> Record = namedtuple("Record", next(rows)) >> records = [Record._make(row) for row in rows] > > This is slightly different from Steven's suggestion, and it makes a > block of records that I think would be iterable. At any rate all the > data from the csv would belong to a single data structure, and that > seems inherently a good thing. > > a = records[i].A , for example > > And I think that this would produce recognizable field names in my code > (which was the original goal) if the following works: > > records[0] is the header row == ('Description', 'Location', etc.) Personally I would recommend against mixing data (an actual location) and metadata (the column name,"Location"), but if you wish my code can be adapted as follows: infile = open("dictreader_demo.csv") rows = csv.reader(infile) fieldnames = next(rows) Record = namedtuple("Record", fieldnames) records = [Record._make(fieldnames)] records.extend(Record._make(row) for row in rows) If you want a lot of flexibility without doing the legwork yourself you might also have a look at pandas. Example session: $ cat places.csv Location,Description,Size here,something,17 there,something else,10 $ python3 Python 3.4.3 (default, Nov 17 2016, 01:08:31) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pandas >>> places = pandas.read_csv("places.csv") >>> places Location Description Size 0 here something 17 1 there something else 10 [2 rows x 3 columns] >>> places.Location 0 here 1 there Name: Location, dtype: object >>> places.sort(columns="Size") Location Description Size 1 there something else 10 0 here something 17 [2 rows x 3 columns] >>> places.Size.mean() 13.5 Be aware that there is a learning curve... > If I can use records[i].Location for the Location column data in row > 'i', then I've got my recognizable-field-name variables. > >> If you want a column from a list of records you need to >> extract it manually: >> >> columnA = [record.A for record in records] > > This is very neat. Something like a list comprehension for named tuples? > > Thanks Peter, I'll try it all tomorrow and see how it goes. > > PS. I haven't forgotten your defaultdict suggestion, I'm just taking the > suggestions I got in the "Cleaning up Conditionals" thread one at a > time, and I will get to defaultdict. Then I'll look at all of them and > see what final version of the code will work best with all the factors > to consider. From python at deborahswanson.net Sun Jan 8 08:53:15 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 05:53:15 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: Message-ID: <002401d269b6$8fb73a80$27b23dae@sambora> Peter Otten wrote, on January 08, 2017 5:21 AM > > Deborah Swanson wrote: > > > Peter Otten wrote, on January 08, 2017 3:01 AM > >> > >> Deborah Swanson wrote: > >> > >> > to do that is with .fget(). Believe me, I tried every > possible > >> > way > > to > >> > use instance.A or instance[1] and no way could I get > >> > ls[instance.A]. > >> > >> Sorry, no. > > > > I quite agree, I was describing the dead end I was in from > peeling the > > list of data and the namedtuple from the header row off the csv > > separately. That was quite obviously the wrong path to take, but I > > didn't know what a good way would be. > > > >> To get a list of namedtuple instances use: > >> > >> rows = csv.reader(infile) > >> Record = namedtuple("Record", next(rows)) > >> records = [Record._make(row) for row in rows] > > > > This is slightly different from Steven's suggestion, and it makes a > > block of records that I think would be iterable. At any > rate all the > > data from the csv would belong to a single data structure, and that > > seems inherently a good thing. > > > > a = records[i].A , for example > > > > And I think that this would produce recognizable field names in my > > code (which was the original goal) if the following works: > > > > records[0] is the header row == ('Description', 'Location', etc.) > > Personally I would recommend against mixing data (an actual > location) and > metadata (the column name,"Location"), but if you wish my code can be > adapted as follows: > > infile = open("dictreader_demo.csv") > rows = csv.reader(infile) > fieldnames = next(rows) > Record = namedtuple("Record", fieldnames) > records = [Record._make(fieldnames)] > records.extend(Record._make(row) for row in rows) Peter, this looks really good, and yes, I didn't feel so good about records[i].Location either, but it was the only way I could see to get the recognizable variable names I want. By extending records from a namedtuple of field names, I think it can be done cleanly. I'll try it and see. > If you want a lot of flexibility without doing the legwork > yourself you > might also have a look at pandas. Example session: > > $ cat places.csv > Location,Description,Size > here,something,17 > there,something else,10 > $ python3 > Python 3.4.3 (default, Nov 17 2016, 01:08:31) > [GCC 4.8.4] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import pandas > >>> places = pandas.read_csv("places.csv") > >>> places > Location Description Size > 0 here something 17 > 1 there something else 10 > > [2 rows x 3 columns] > >>> places.Location > 0 here > 1 there > Name: Location, dtype: object > >>> places.sort(columns="Size") > Location Description Size > 1 there something else 10 > 0 here something 17 > > [2 rows x 3 columns] > >>> places.Size.mean() > 13.5 > > Be aware that there is a learning curve... Yes, and I'm sure the learning curve is steep. I watched a webinar on pandas about a year ago, not to actually learn it, but just to take in the big picture and see something people were really accomplishing with python. I won't take this on any time right away, but I'll definitely keep it and work with it sometime. Maybe as just an intro to pandas, using my data from the real estate project. > > If I can use records[i].Location for the Location column > data in row > > 'i', then I've got my recognizable-field-name variables. > > > >> If you want a column from a list of records you need to extract it > >> manually: > >> > >> columnA = [record.A for record in records] > > > > This is very neat. Something like a list comprehension for named > > tuples? > > > > Thanks Peter, I'll try it all tomorrow and see how it goes. > > > > PS. I haven't forgotten your defaultdict suggestion, I'm > just taking > > the suggestions I got in the "Cleaning up Conditionals" > thread one at > > a time, and I will get to defaultdict. Then I'll look at > all of them > > and see what final version of the code will work best with all the > > factors to consider. From paul.nospam at rudin.co.uk Sun Jan 8 09:48:41 2017 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Sun, 08 Jan 2017 14:48:41 +0000 Subject: Using namedtuples field names for column indices in a list of lists References: <000c01d269a5$fc654020$27b23dae@sambora> Message-ID: <87d1fx4ng6.fsf@rudin.co.uk> "Deborah Swanson" writes: > Peter Otten wrote, on January 08, 2017 3:01 AM >> >> columnA = [record.A for record in records] > > This is very neat. Something like a list comprehension for named tuples? Not something like - this *is* a list comprehension - it creates a list of named tuples. The thing you iterate over within the comprehension can be any iterator. (Of course you're going to run into problems if you try to construct a list from an infinite iterator.) From no.email at nospam.invalid Sun Jan 8 14:08:43 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Sun, 08 Jan 2017 11:08:43 -0800 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> <8760lp3mq4.fsf@nightsong.com> <871swd3m5h.fsf@nightsong.com> <87wpe527gx.fsf@nightsong.com> Message-ID: <87lgul1i9w.fsf@nightsong.com> Peter Otten <__peter__ at web.de> writes: > return min(take_until(), key=firstitem)[1] Actually, key=abs should work. I realized that after posting. From no.email at nospam.invalid Sun Jan 8 14:10:23 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Sun, 08 Jan 2017 11:10:23 -0800 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <87eg0f3b72.fsf@nightsong.com> <87a8b22ozh.fsf@nightsong.com> <8760lp3mq4.fsf@nightsong.com> <871swd3m5h.fsf@nightsong.com> Message-ID: <87h9591i74.fsf@nightsong.com> Jussi Piitulainen writes: > It could still be added as an option, to both takewhile and iter(_, _). That's too messy, it really should be pervasive in iterators. From python at deborahswanson.net Sun Jan 8 15:22:52 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 12:22:52 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <87d1fx4ng6.fsf@rudin.co.uk> Message-ID: <005d01d269ec$fdb52070$27b23dae@sambora> Paul Rudin wrote, on January 08, 2017 6:49 AM > > "Deborah Swanson" writes: > > > Peter Otten wrote, on January 08, 2017 3:01 AM > >> > >> columnA = [record.A for record in records] > > > > This is very neat. Something like a list comprehension for named > > tuples? > > Not something like - this *is* a list comprehension - it > creates a list of named tuples. > > The thing you iterate over within the comprehension can be > any iterator. (Of course you're going to run into problems if > you try to construct a list from an infinite iterator.) Thanks Paul. I've been meaning to spend some time getting to thoroughly know list comprehensions for awhile now, but I keep running into so many new things I just haven't gotten to it. I thought it looked like one, but I hedged my wording because I wasn't sure. Infinite iterators definitely sound like something to remember! From hpj at urpla.net Sun Jan 8 18:18:38 2017 From: hpj at urpla.net (Hans-Peter Jansen) Date: Mon, 09 Jan 2017 00:18:38 +0100 Subject: Using sudo with pip3? In-Reply-To: References: Message-ID: <8295323.khLXTiFXdt@xrated> On Samstag, 7. Januar 2017 19:07:55 Clint Moyer wrote: > I would lightly advise against, assuming both Pip and your package > manager are trying to accomplish nearly the same thing. Stick with > updating through the repo. > > If you find that the version your OS provides is out-of-date compared > to what's on PyPi or Github, then you might want to remove from your > OS and re-install through Pip, for those discrete cases. That's the > platform agnostic route. Or take the ninja way, I do: Build all packages, you want (updated) for your distribution: https://build.opensuse.org/project/monitor/home:frispete:python https://build.opensuse.org/project/monitor/home:frispete:python3 The red labels are reminders... SCR, Pete From python at deborahswanson.net Sun Jan 8 19:18:28 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 16:18:28 -0800 Subject: Grumpy: Python to Go compiler In-Reply-To: <5871ec24$0$11119$c3e8da3@news.astraweb.com> Message-ID: <000e01d26a0d$e75d3490$27b23dae@sambora> Steven D'Aprano wrote, on January 07, 2017 11:37 PM > > Grumpy, an experimental project from Google, transpiles > Python code into Go, allowing Python programs to be compiled > and run as static binaries using the Go toolchain. > > > http://www.infoworld.com/article/3154624/application-development/google- boosts-python-by-turning-it-into-go.html > > >-- >Steven >"Ever since I learned about confirmation bias, I've been seeing >it everywhere." - Jon Ronson Interesting online resource, if you can stomach contact with Google. I'll spare you the diatribe of all my beefs with Google (haha, unless you ask). But this is a potentially useful tool to know about. From tonycamgar at gmail.com Sun Jan 8 19:45:29 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Sun, 8 Jan 2017 16:45:29 -0800 (PST) Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found In-Reply-To: References: Message-ID: <725de1f8-42f4-4cef-9ca3-58a2c7477576@googlegroups.com> On Friday, January 6, 2017 at 6:04:33 AM UTC-8, Peter Otten wrote: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum function that allows the user to specify a stop > value: > > >>> from itertools import count, chain > >>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) > 0 > > How would you implement stopmin()? > > Currently I raise an exception in the key function: > > class Stop(Exception): > pass > > def stopmin(items, key, stop): > """ > >>> def g(): > ... for i in reversed(range(10)): > ... print(10*i) > ... yield str(i) > >>> stopmin(g(), key=int, stop=5) > 90 > 80 > 70 > 60 > 50 > '5' > """ > def key2(value): > result = key(value) > if result <= stop: > raise Stop(value) > return result > try: > return min(items, key=key2) > except Stop as stop: > return stop.args[0] This is the simplest version I could come up with. I also like the classic 100% imperative, but it seems that is not trendy between the solutions given :D. you can test it here https://repl.it/FD5A/0 source code: from itertools import accumulate # stopmin calculates the greatest lower bound (infimum). # https://upload.wikimedia.org/wikipedia/commons/0/0a/Infimum_illustration.svg def takeuntil(pred, seq): for item in seq: yield item if not pred(item): break def stopmin(seq, stop=0): drop_ltstop = (item for item in seq if item >= stop) min_gen = (min_ for min_ in accumulate(drop_ltstop, func=min)) return list(takeuntil(lambda x: x!= stop, min_gen))[-1] seq = [1, 4, 7, -8, 0, 7, -8, 9] # 0 just until zero is generated seq = [1, 4, 7, -8, 7, -8, 9] # 1 the entire sequence is generated print(stopmin(seq, stop=0)) From info at tundraware.com Sun Jan 8 19:49:10 2017 From: info at tundraware.com (Tim Daneliuk) Date: Sun, 8 Jan 2017 18:49:10 -0600 Subject: Grumpy: Python to Go compiler In-Reply-To: References: <5871ec24$0$11119$c3e8da3@news.astraweb.com> <000e01d26a0d$e75d3490$27b23dae@sambora> Message-ID: On 01/08/2017 06:18 PM, Deborah Swanson wrote: > (haha, unless > you ask) C'mon, go for it ... there hasn't been a good rant here in 4 or 5 minutes ... From steve+comp.lang.python at pearwood.info Sun Jan 8 22:29:51 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Mon, 09 Jan 2017 14:29:51 +1100 Subject: Using namedtuples field names for column indices in a list of lists References: <5871df6c$0$1536$c3e8da3$5496439d@news.astraweb.com> <000401d26994$ffd151b0$27b23dae@sambora> Message-ID: <587303b0$0$11102$c3e8da3@news.astraweb.com> On Sunday 08 January 2017 20:53, Deborah Swanson wrote: > Steven D'Aprano wrote, on January 07, 2017 10:43 PM >> >> On Sunday 08 January 2017 16:39, Deborah Swanson wrote: >> >> > What I've done so far: >> > >> > with open('E:\\Coding projects\\Pycharm\\Moving\\Moving >> 2017 in.csv', >> > 'r') as infile: >> > ls = list(csv.reader(infile)) >> > lst = namedtuple('lst', ls[0]) >> > >> > where 'ls[0]' is the header row of the csv, and it works perfectly >> > well. 'lst' is a namedtuple instance with each of the >> column titles as >> > field names. >> >> Are you sure? namedtuple() returns a class, not a list: > > Yes. 'ls' is defined as 'list(csv.reader(infile))', so ls[0] is the > first row from the csv, the header row. 'lst' is the namedtuple. > > Perhaps what's puzzling you is that the way I've written it, the list of > data and the namedtuple are disjoint, and that's the problem. No, I'm pretty sure that's not the case. I don't have access to your CSV file, but I can simulate it: ls = [['Location', 'Date', 'Price'], ['here', '1/1/17', '1234'], ['there', '1/1/17', '5678'], ['everywhere', '1/1/17', '9821'] ] from collections import namedtuple lst = namedtuple('lst', ls[0]) print(type(lst)) print(lst) If you run that code, you should see: which contradicts your statement: 'lst' is a namedtuple instance with each of the column titles as field names. and explains why you had to access the individual property method `fget`: you were accessing the *class object* rather than an actual named tuple instance. The code you gave was: lst.Location.fget(l) where l was not given, but I can guess it was a row of the CSV file, i.e. an individual record. So: - lst was the named tuple class, a subclass of tuple - lst.Location returns a property object - lst.Location.fget is the internal fget method of the property object. I think Peter Otten has the right idea: create a list of records with something like this: Record = namedtuple('Record', ls[0]) data = [Record(*row) for row in ls[1:]) or if you prefer Peter's version: data = [Record._make(row) for row in ls[1:]) Half the battle is coming up with the right data structures :-) -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at deborahswanson.net Sun Jan 8 22:33:45 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 19:33:45 -0800 Subject: Grumpy: Python to Go compiler In-Reply-To: Message-ID: <001f01d26a29$2ee552f0$27b23dae@sambora> Tim Daneliuk wrote, on January 08, 2017 4:49 PM > > On 01/08/2017 06:18 PM, Deborah Swanson wrote: > > (haha, unless > > you ask) > > C'mon, go for it ... there hasn't been a good rant here in > 4 or 5 minutes ... Oh hell. (How do I tell him I was up til 8am this morning, only got a few hours sleep, and I'm just too pookie now to deliver a decent rant on all the ways Google is truly evil. Don't have to, just did.) Besides, I'm rewriting code this afternoon and really want to see how much better it is when I'm done. From steve+comp.lang.python at pearwood.info Sun Jan 8 22:53:26 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Mon, 09 Jan 2017 14:53:26 +1100 Subject: Temporary variables in list comprehensions Message-ID: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Suppose you have an expensive calculation that gets used two or more times in a loop. The obvious way to avoid calculating it twice in an ordinary loop is with a temporary variable: result = [] for x in data: tmp = expensive_calculation(x) result.append((tmp, tmp+1)) But what if you are using a list comprehension? Alas, list comps don't let you have temporary variables, so you have to write this: [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data] Or do you? ... no, you don't! [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] I can't decide whether that's an awesome trick or a horrible hack... -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From rosuav at gmail.com Sun Jan 8 23:09:33 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 9 Jan 2017 15:09:33 +1100 Subject: Temporary variables in list comprehensions In-Reply-To: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jan 9, 2017 at 2:53 PM, Steven D'Aprano wrote: > [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > > > I can't decide whether that's an awesome trick or a horrible hack... A horrible hack on par with abusing a recursive function's arguments for private variables. Much better would be to refactor the append part: def this_and_one(value): return value, value + 1 [this_and_one(expensive_calculation(x)) for x in data] ChrisA From steve+comp.lang.python at pearwood.info Sun Jan 8 23:49:10 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Mon, 09 Jan 2017 15:49:10 +1100 Subject: Temporary variables in list comprehensions References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: <58731647$0$11108$c3e8da3@news.astraweb.com> On Monday 09 January 2017 15:09, Chris Angelico wrote: > On Mon, Jan 9, 2017 at 2:53 PM, Steven D'Aprano > wrote: >> [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] >> >> >> I can't decide whether that's an awesome trick or a horrible hack... > > A horrible hack on par with abusing a recursive function's arguments > for private variables. What's wrong with that? That's a perfectly legitimate technique. I prefer to hide it behind a private function rather than expose it in a public interface: # instead of this def recursive(arg, _internal=None): """Recursive function. Don't supply the _internal argument.""" ... return recursive(arg-1, spam) # I usually prefer this def recursive(arg): """Recursive function.""" return _recursive(arg, spam) def _recursive(arg, internal): ... but that's just polishing the code. > Much better would be to refactor the append > part: > > def this_and_one(value): > return value, value + 1 I wouldn't call that "much better". Requiring an extra function defeats the purpose of using a list comp, and it doesn't scale well for multiple list comps each of which needs a different helper function: def this_and_one(value): return value, value + 1 def this_less_one_and_this_plus_one_and_this(value): return value - 1, value + 1, value def this_and_that_or_something(value): return value, value.that() or something def extract_value(value): return spam[value] or ham[value] or eggs[value] and so on. Helper functions are good. Helper functions that are only used *once* are a code smell. *LOTS* of helper functions that are only used once are a sign that something is horrible, and it might just be your language... -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From rosuav at gmail.com Sun Jan 8 23:54:31 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 9 Jan 2017 15:54:31 +1100 Subject: Temporary variables in list comprehensions In-Reply-To: <58731647$0$11108$c3e8da3@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <58731647$0$11108$c3e8da3@news.astraweb.com> Message-ID: On Mon, Jan 9, 2017 at 3:49 PM, Steven D'Aprano wrote: > Helper functions are good. Helper functions that are only used > *once* are a code smell. *LOTS* of helper functions that are only used once are > a sign that something is horrible, and it might just be your language... Agreed, but with a toy example like you posted, it's impossible to say which is happening :) ChrisA From rustompmody at gmail.com Mon Jan 9 00:51:03 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Sun, 8 Jan 2017 21:51:03 -0800 (PST) Subject: Temporary variables in list comprehensions In-Reply-To: <58731647$0$11108$c3e8da3@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <58731647$0$11108$c3e8da3@news.astraweb.com> Message-ID: On Monday, January 9, 2017 at 10:19:31 AM UTC+5:30, Steven D'Aprano wrote: > On Monday 09 January 2017 15:09, Chris Angelico wrote: > > > On Mon, Jan 9, 2017 at 2:53 PM, Steven D'Aprano wrote: > >> [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > >> > >> > >> I can't decide whether that's an awesome trick or a horrible hack... > > > > A horrible hack on par with abusing a recursive function's arguments > > for private variables. > > What's wrong with that? That's a perfectly legitimate technique. I prefer to > hide it behind a private function rather than expose it in a public interface: > > # instead of this > def recursive(arg, _internal=None): > """Recursive function. Don't supply the _internal argument.""" > ... > return recursive(arg-1, spam) > > > # I usually prefer this > def recursive(arg): > """Recursive function.""" > return _recursive(arg, spam) > > def _recursive(arg, internal): > ... > > > but that's just polishing the code. > > > > > Much better would be to refactor the append > > part: > > > > def this_and_one(value): > > return value, value + 1 > > I wouldn't call that "much better". Requiring an extra function defeats the > purpose of using a list comp, and it doesn't scale well for multiple list comps +1 Id call it (your original) neither an awesome trick nor a horrible hack ? just a neat workaround for an obvious lacuna As for Chris' solution there is a simple test to see whether it's worth it: Does the function name make sense? If yes then the subexpression refactored- into-a-function is probably fine. If not the kludgyness shows [Im assuming the real usage is something else and your question is a highly sscce-ed version] From esj at harvee.org Mon Jan 9 00:57:24 2017 From: esj at harvee.org (Eric S. Johansson) Date: Mon, 9 Jan 2017 00:57:24 -0500 Subject: Python for WEB-page !? In-Reply-To: References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> Message-ID: <0b621aaa-03d0-0e72-f12d-030189e6a3f1@harvee.org> On 1/5/2017 7:48 PM, Michael Torrie wrote: > While Python can do that, using a web framework to process HTTP requests > and generate HTML to display in the browser, I don't believe Python is > the appropriate language for the task at hand. Most web sites that do > interactive formula calculations like you describe do it all in the > browser using Javascript. No need to have a web server do all that > heavy lifting at all. A simple html file would contain everything you need. > > Even if you want to use Python to generate the web page and process > events, you'll still have to master Javascript at some point to make the > webpages more interactive. There are a few implementations of Python that generate code that can run the browser. I'm currently using brython and its significantly easier than struggling with JavaScript. It's not perfect by any means, it has its own quirks and bugs but the developer is very responsive, the product is getting better every month and I like to see the Python community give it a bit more love From python at deborahswanson.net Mon Jan 9 01:18:35 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 22:18:35 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <5871df6c$0$1536$c3e8da3$5496439d@news.astraweb.com> Message-ID: <002a01d26a40$35ea89a0$27b23dae@sambora> Steven D'Aprano wrote, on January 07, 2017 10:43 PM > > On Sunday 08 January 2017 16:39, Deborah Swanson wrote: > > The recommended way is with the _replace method: > > py> instance._replace(A=999) > Record(A=999, B=20, C=30) > py> instance._replace(A=999, C=888) > Record(A=999, B=20, C=888) > > -- > Steven > "Ever since I learned about confirmation bias, I've been seeing > it everywhere." - Jon Ronson instance._replace(A=999) works perfectly, and editting my existing assignment statements was really easy. Thanks - a lot. From python at deborahswanson.net Mon Jan 9 01:58:34 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 22:58:34 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: Message-ID: <003301d26a45$cc04a5b0$27b23dae@sambora> Peter Otten wrote, on January 08, 2017 5:21 AM > > Deborah Swanson wrote: > > > Peter Otten wrote, on January 08, 2017 3:01 AM > > Personally I would recommend against mixing data (an actual location) and > metadata (the column name,"Location"), but if you wish my code can be > adapted as follows: > > infile = open("dictreader_demo.csv") > rows = csv.reader(infile) > fieldnames = next(rows) > Record = namedtuple("Record", fieldnames) > records = [Record._make(fieldnames)] > records.extend(Record._make(row) for row in rows) Works like a charm. I stumbled a bit changing all my subscripted variables to namedtuples and rewriting the inevitable places my code that didn't work the same. But actually it was fun, especially deleting all the sections and variables I no longer needed. And it executes correctly now too - with recognizable fieldnames instead of my quirky 2-letter code subscripts. All in all a huge win! I do have two more questions. 1) I have a section that loops through the sorted data, compares two adjacent rows at a time, and marks one of them for deletion if the rows are identical. I'm using for i in range(len(records)-1): r1 = records[i] r2 = records[i+1] if r1.xx = r2.xx: . . and my question is whether there's a way to work with two adjacent rows without using subscripts? Even better, to get hold of all the records with the same Description as the current row, compare them all, mark all but the different ones for deletion, and then resume processing the records after the last one? 2) I'm using mergesort. (I didn't see any way to sort a namedtuple in the docs.) In the list version of my code I copied and inserted the 2 columns I wanted to sort by into the beginning of the list, and then deleted them after the list was sorted. But just looking at records, I'm not so sure that can easily be done. I remember your code to work with columns of the data: columnA = [record.A for record in records] and I can see how that would get me columnA and columnB, but then is there any better way to insert and delete columns in an existing namedtuple than slicing? And I don't think you can insert or delete a whole column while slicing. Or maybe my entire approach is not the best. I know it's possible to do keyed sorts, but I haven't actually written or used any. So I just pulled a mergesort off the shelf and got what I wanted by inserting copies of those 2 columns at the front, and then deleting them when the sort was complete. Not exactly elegant, but it works. Any suggestions would be most welcome. From python at deborahswanson.net Mon Jan 9 02:11:58 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 8 Jan 2017 23:11:58 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <587303b0$0$11102$c3e8da3@news.astraweb.com> Message-ID: <003901d26a47$ab209ff0$27b23dae@sambora> Steven D'Aprano wrote, on January 08, 2017 7:30 PM > > On Sunday 08 January 2017 20:53, Deborah Swanson wrote: > > > Steven D'Aprano wrote, on January 07, 2017 10:43 PM > > No, I'm pretty sure that's not the case. I don't have access > to your CSV file, > but I can simulate it: > > ls = [['Location', 'Date', 'Price'], > ['here', '1/1/17', '1234'], > ['there', '1/1/17', '5678'], > ['everywhere', '1/1/17', '9821'] > ] > > from collections import namedtuple > lst = namedtuple('lst', ls[0]) > > print(type(lst)) > print(lst) > > > > If you run that code, you should see: > > > > > > which contradicts your statement: > > 'lst' is a namedtuple instance with each of the column > titles as field names. Yes, yes. In a careless moment I called a class an instance. > and explains why you had to access the individual property > method `fget`: you > were accessing the *class object* rather than an actual named > tuple instance. That code is deleted and long gone now, so I can't look at it in the debugger, but yes, I'm pretty sure 'fget' is a class member. > The code you gave was: > > lst.Location.fget(l) > > where l was not given, but I can guess it was a row of the > CSV file, i.e. an > individual record. So: > > - lst was the named tuple class, a subclass of tuple > > - lst.Location returns a property object > > - lst.Location.fget is the internal fget method of the > property object. And your point is? Perhaps I didn't express myself in a way that you could recognize, but I understood all of that before I wrote to you, and attempted to convey that understanding to you. Obviously I failed, if you now think I need a lesson in what's going on here. > I think Peter Otten has the right idea: create a list of > records with something > like this: > > > Record = namedtuple('Record', ls[0]) > data = [Record(*row) for row in ls[1:]) > > > or if you prefer Peter's version: > > data = [Record._make(row) for row in ls[1:]) > > > Half the battle is coming up with the right data structures :-) Can't and wouldn't disagree with any part of that! > -- > Steven > "Ever since I learned about confirmation bias, I've been seeing > it everywhere." - Jon Ronson From stephane at wirtel.be Mon Jan 9 04:54:49 2017 From: stephane at wirtel.be (Stephane Wirtel) Date: Mon, 9 Jan 2017 10:54:49 +0100 Subject: Python Events in 2017, Need your help. Message-ID: <20170109095449.v4ual5czl5mc7xxs@sg1> Dear Community, For the PythonFOSDEM [1] on 4th and 5th February in Belgium, I would like to present some slides with the Python events around the World. Based on https://python.org/events, I have noted that there are missing events, for example: * PyCon Otto: Italy * PyCon UK: United Kingdom * PyCon CA: Canada * PyCon Ireland: Ireland * PyCon France: France Some of these events are not yet announced and I understand they are in the second semester, and thus, they don't know the location and the dates, excepted for PyCon Otto (April). In fact, I have noted that we know some big events in the Python community (for example: PyCon US and EuroPython) but do you know the others events, maybe the local event, PyCon IE, PyCon UK or PyCon IT. I like to know where there is a PyCon or a Django Conf or a PyData Event. In fact, I think we can help the Python Community if we submit all the events in https://python.org/events. This page has been created by the PSF and is maintained by some volunteers. I know this list of events: * PyCon Cameroon : 20-23 Jav, Cameroon * PythonFOSDEM : 4-5 Feb, Belgium * PyCon Colombia : 10-12 Feb, Colombia * PyCon Pune : 16-20 Feb, India * Swiss Python Summit : 17-18 Feb, Switzerland * IrPyCon : 17-18 Feb, Iran * PyCon SK : 10-13 Mar, Slovakia * Django Europe : 3-8 Apr, Italy * PyCon Otto : 6-9 Apr, Italy * Python Sudeste : 5-7 Mai, Brazil * GeoPython : 8-11 May, Switzerland * PyCon US : 17-26 May, USA * EuroPython : July, Italy * PyCon AU : 3-9 Aug, Australia * PyCon UK : September, United Kingdom * PyCon CA : November, Canada * PyCon Ireland : October, Ireland * PyCon FR : October/November, France And you ? Please, could you check on https://www.python.org/events/ , if you are an organizer, please add your event. If you think there is a missing event, please, send me the info via [email](mailto:stephane at wirtel.be) or via my [twitter account](https://twitter.com/matrixise) and I will add it on my slides. I would like to present your event. Thank you so much for your help. Stephane Wirtel [1] https://www.python-fosdem.org -- St?phane Wirtel - http://wirtel.be - @matrixise From antoon.pardon at rece.vub.ac.be Mon Jan 9 05:14:19 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 9 Jan 2017 11:14:19 +0100 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <003301d26a45$cc04a5b0$27b23dae@sambora> References: <003301d26a45$cc04a5b0$27b23dae@sambora> Message-ID: <65e73143-fa67-c160-b38c-2d1577872c45@rece.vub.ac.be> Op 09-01-17 om 07:58 schreef Deborah Swanson: > Peter Otten wrote, on January 08, 2017 5:21 AM >> Deborah Swanson wrote: >> >>> Peter Otten wrote, on January 08, 2017 3:01 AM >> >> Personally I would recommend against mixing data (an actual location) > and >> metadata (the column name,"Location"), but if you wish my code can be >> adapted as follows: >> >> infile = open("dictreader_demo.csv") >> rows = csv.reader(infile) >> fieldnames = next(rows) >> Record = namedtuple("Record", fieldnames) >> records = [Record._make(fieldnames)] >> records.extend(Record._make(row) for row in rows) > Works like a charm. I stumbled a bit changing all my subscripted > variables to namedtuples and rewriting the inevitable places my code > that didn't work the same. But actually it was fun, especially deleting > all the sections and variables I no longer needed. And it executes > correctly now too - with recognizable fieldnames instead of my quirky > 2-letter code subscripts. All in all a huge win! > > I do have two more questions. > > 1) I have a section that loops through the sorted data, compares two > adjacent rows at a time, and marks one of them for deletion if the rows > are identical. > > I'm using > > for i in range(len(records)-1): > r1 = records[i] > r2 = records[i+1] > if r1.xx = r2.xx: > . > . > and my question is whether there's a way to work with two adjacent rows > without using subscripts? for r1, r2 in zip(records[i], records[i+1]): if r1.xx == r2.xx . . From antoon.pardon at rece.vub.ac.be Mon Jan 9 05:35:18 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 9 Jan 2017 11:35:18 +0100 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <003301d26a45$cc04a5b0$27b23dae@sambora> References: <003301d26a45$cc04a5b0$27b23dae@sambora> Message-ID: <133f4cc5-b56a-fba6-cde9-bb98eb080fac@rece.vub.ac.be> Op 09-01-17 om 07:58 schreef Deborah Swanson: > Peter Otten wrote, on January 08, 2017 5:21 AM >> Deborah Swanson wrote: >> >>> Peter Otten wrote, on January 08, 2017 3:01 AM >> >> Personally I would recommend against mixing data (an actual location) > and >> metadata (the column name,"Location"), but if you wish my code can be >> adapted as follows: >> >> infile = open("dictreader_demo.csv") >> rows = csv.reader(infile) >> fieldnames = next(rows) >> Record = namedtuple("Record", fieldnames) >> records = [Record._make(fieldnames)] >> records.extend(Record._make(row) for row in rows) > Works like a charm. I stumbled a bit changing all my subscripted > variables to namedtuples and rewriting the inevitable places my code > that didn't work the same. But actually it was fun, especially deleting > all the sections and variables I no longer needed. And it executes > correctly now too - with recognizable fieldnames instead of my quirky > 2-letter code subscripts. All in all a huge win! > > I do have two more questions. > > 1) I have a section that loops through the sorted data, compares two > adjacent rows at a time, and marks one of them for deletion if the rows > are identical. > > I'm using > > for i in range(len(records)-1): > r1 = records[i] > r2 = records[i+1] > if r1.xx = r2.xx: > . > . > and my question is whether there's a way to work with two adjacent rows > without using subscripts? > > Even better, to get hold of all the records with the same Description as > the current row, compare them all, mark all but the different ones for > deletion, and then resume processing the records after the last one? If I understand correctly you want something like: records.sort(key = lamda rec: rec.xx) AKA from operator import attrgetter records.sort(key = attrgetter('xx')) or maybe: records.sort(key = lambda rec: (rec.xx,) + tuple(rec)) -- Antoon Pardon From storchaka at gmail.com Mon Jan 9 05:41:44 2017 From: storchaka at gmail.com (Serhiy Storchaka) Date: Mon, 9 Jan 2017 12:41:44 +0200 Subject: Temporary variables in list comprehensions In-Reply-To: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 09.01.17 05:53, Steven D'Aprano wrote: > Suppose you have an expensive calculation that gets used two or more times in a > loop. The obvious way to avoid calculating it twice in an ordinary loop is with > a temporary variable: > > result = [] > for x in data: > tmp = expensive_calculation(x) > result.append((tmp, tmp+1)) > > > But what if you are using a list comprehension? result = [(tmp, tmp + 1) for tmp in (expensive_calculation(x) for x in data)] You could also assign an internal generator expression to temporal variable for readability if it is long. gen = (expensive_calculation(x) for x in data) result = [(tmp, tmp + 1) for tmp in gen] From no.email at nospam.invalid Mon Jan 9 05:44:12 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Mon, 09 Jan 2017 02:44:12 -0800 Subject: Temporary variables in list comprehensions References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8760lo1pj7.fsf@nightsong.com> Steven D'Aprano writes: > [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data] def memoize(f): cache = {} def m(x): if x in cache: return cache[x] a = f(x) cache[x] = a return a return m ec = memoize(expensive_calculation) ... [(ec(x), ec(x) + 1) for x in data] Or can write: @memoize def expensive_calculation(x): .... Note the Haskell version of your listcomp would be: [(e, e+1) | x <- data_, let e = expensive_calculation x] Maybe Python could get some version of that. I've wanted it more than once. (I used "data_" because data is a Haskell keyword). From python at deborahswanson.net Mon Jan 9 05:45:54 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 02:45:54 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <65e73143-fa67-c160-b38c-2d1577872c45@rece.vub.ac.be> Message-ID: <005601d26a65$8e01f5e0$27b23dae@sambora> Antoon Pardon wrote, on January 09, 2017 2:14 AM > > 1) I have a section that loops through the sorted data, compares two > > adjacent rows at a time, and marks one of them for deletion if the > > rows are identical. > > > > I'm using > > > > for i in range(len(records)-1): > > r1 = records[i] > > r2 = records[i+1] > > if r1.xx = r2.xx: > > . > > . > > and my question is whether there's a way to work with two adjacent > > rows without using subscripts? > > for r1, r2 in zip(records[i], records[i+1]): > if r1.xx == r2.xx > . > . Ok, I've seen the zip function before and it might do the job, but here I think you're suggesting: for i in range(len(records)-1): for r1, r2 in zip(records[i], records[i+1]): if r1.xx == r2.xx . . The hope was to do the loop without subscripts, and this may or may not have gotchas because records is a namedtuple: for r in records(1:): . . Deborah From no.email at nospam.invalid Mon Jan 9 05:46:44 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Mon, 09 Jan 2017 02:46:44 -0800 Subject: Temporary variables in list comprehensions References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: <871swc1pez.fsf@nightsong.com> Serhiy Storchaka writes: > gen = (expensive_calculation(x) for x in data) > result = [(tmp, tmp + 1) for tmp in gen] result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] From python at deborahswanson.net Mon Jan 9 05:57:48 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 02:57:48 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <133f4cc5-b56a-fba6-cde9-bb98eb080fac@rece.vub.ac.be> Message-ID: <005701d26a67$37b99290$27b23dae@sambora> Antoon Pardon wrote, on January 09, 2017 2:35 AM > If I understand correctly you want something like: > > records.sort(key = lamda rec: rec.xx) > > AKA > > from operator import attrgetter > records.sort(key = attrgetter('xx')) > > or maybe: > > records.sort(key = lambda rec: (rec.xx,) + tuple(rec)) > > -- > Antoon Pardon I think you are replying to my question about sorting a namedtuple, in this case it's called 'records'. I think your suggestion works for lists and tuples, and probably dictionaries. But namedtuples doesn't have a sort function. >>> from collections import namedtuple >>> dir(namedtuple) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] so nothing with records.sort will work. :( Deborah From steve+python at pearwood.info Mon Jan 9 06:39:45 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Mon, 09 Jan 2017 22:39:45 +1100 Subject: Using namedtuples field names for column indices in a list of lists References: <133f4cc5-b56a-fba6-cde9-bb98eb080fac@rece.vub.ac.be> <005701d26a67$37b99290$27b23dae@sambora> Message-ID: <58737683$0$1606$c3e8da3$5496439d@news.astraweb.com> On Mon, 9 Jan 2017 09:57 pm, Deborah Swanson wrote: [...] > I think you are replying to my question about sorting a namedtuple, in > this case it's called 'records'. > > I think your suggestion works for lists and tuples, and probably > dictionaries. But namedtuples doesn't have a sort function. Tuples in general (whether named or not) represent structs or records, where the position of the item is significant. It doesn't usually make sense to sort individual elements of a record or tuple: Before sorting: Record(name='George', spouse='', position='Accountant') After sorting: Record(name='', spouse='Accountant', position='George') I think what you want to do is sort a list of records, not each record itself. Or possibly you want to reorder the columns, in which case the easiest way to do that is by editing the CSV file in LibreOffice or Excel or another spreadsheet application. If you have a list of records, call .sort() on the list, not the individual records. But if I am wrong, and you absolutely must sort the fields of each record, call the sorted() function, which will copy the fields into a list and sort the list. That is: alist = sorted(record) -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From ghasselbring at hotmail.com Mon Jan 9 06:48:00 2017 From: ghasselbring at hotmail.com (Gretchen Hasselbring) Date: Mon, 9 Jan 2017 11:48:00 +0000 Subject: Error message IDLE Message-ID: Hello Trying to learn python on a laptop. Was successful for awhile then... Had a 'subprocess startup error' 'IDLE's subprocess didn't make connection. Either IDLE can't start subprocess or personal firewall software is blocking the connection ' Doesn't appear to be firewall and I uninstalled and reinstalled python 3.6.032 bit??? Any advice? Thanks Sent from my iPad From python.list at tim.thechases.com Mon Jan 9 07:15:31 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 9 Jan 2017 06:15:31 -0600 Subject: Temporary variables in list comprehensions In-Reply-To: <871swc1pez.fsf@nightsong.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <871swc1pez.fsf@nightsong.com> Message-ID: <20170109061531.71e3c795@bigbox.christie.dr> On 2017-01-09 02:46, Paul Rubin wrote: > > gen = (expensive_calculation(x) for x in data) > > result = [(tmp, tmp + 1) for tmp in gen] > > result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] As charmingly expressive as map() is, the wildly different behavior in py3 (it's a generator that evaluates lazily) vs py2 (it consumes the entire iterable in one go) leads me to avoid it in general, especially when Python gives me list-comprehensions and generators that can do what I intend explicitly. E.g., passing in itertools.count() as an iterable to map() will kill py2 but is perfectly fine in py3 (a horrible example): import itertools as i for x in i.takewhile( lambda n: n < 100, map(lambda g: g**2, i.count()) ): print(x) But yes, both ChrisA's pass-it-to-a-function and Serhiy's nested generate-the-tmp-values-then-operate-on-those are good solutions depending on how they feel in any particular context. If I need to use an "if" clause in the outer generator that tests the resulting temp values, I use Serhiy's solution: [(tmp, tmp + 1) for tmp in ( expensive_calculation(x) for x in data ) if tmp > 42 ] Otherwise, I like the cleanliness of ChrisA's function: def fn(x) tmp = expensive_calculation(x) return x, x + 1 [fn(x) for x in data] -tkc From python at deborahswanson.net Mon Jan 9 07:25:27 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 04:25:27 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <58737683$0$1606$c3e8da3$5496439d@news.astraweb.com> Message-ID: <007101d26a73$7601d2e0$27b23dae@sambora> Steve D'Aprano wrote, on January 09, 2017 3:40 AM > > On Mon, 9 Jan 2017 09:57 pm, Deborah Swanson wrote: > > [...] > > I think you are replying to my question about sorting a > namedtuple, in > > this case it's called 'records'. > > > > I think your suggestion works for lists and tuples, and probably > > dictionaries. But namedtuples doesn't have a sort function. > > Tuples in general (whether named or not) represent structs or > records, where the position of the item is significant. It > doesn't usually make sense to sort individual elements of a > record or tuple: > > Before sorting: Record(name='George', spouse='', > position='Accountant') After sorting: Record(name='', > spouse='Accountant', position='George') > > > I think what you want to do is sort a list of records, not > each record itself. Or possibly you want to reorder the > columns, in which case the easiest way to do that is by > editing the CSV file in LibreOffice or Excel or another > spreadsheet application. > If you have a list of records, call .sort() on the list, not > the individual records. > I want to sort a nametuple of records. I could convert it to a list easy enough, with: recs = list(records) and then use the column copying and deleting method I described in my previous post and use mergesort. This would give me exactly what I had in my original code, and it would be ok. There's only one step after the mergesort, and I could do it without the namedtuple, although I'd have to count column indices for that section, and rewrite them whenever the columns changed, which was what my original 2-letter codes and the conversion to namedtuples were both meant to avoid. So all in all, the best thing would be if there's a way to sort records as a namedtuple. > But if I am wrong, and you absolutely must sort the fields of > each record, call the sorted() function, which will copy the > fields into a list and sort the list. That is: > > alist = sorted(record) > > -- > Steve > "Cheer up," they said, "things could be worse." So I cheered > up, and sure enough, things got worse. I'm not sure what you mean by sorting the fields of each record. I want all the rows in records sorted by the Description and Date in each record. alist = sorted(record) looks like it sorts one record, by what? An alphanumeric ordering of all the fields in record? That would be beyond useless for my purposes. It's possible sorted() will work on namedtuples. Stackoverflow has an example: from operator import attrgetter from collections import namedtuple Person = namedtuple('Person', 'name age score') seq = [Person(name='nick', age=23, score=100), Person(name='bob', age=25, score=200)] # Sort list by name sorted(seq, key=attrgetter('name')) # Sort list by age sorted(seq, key=attrgetter('age')) So apparently it's done, and it's a keyed sort too. Although what they're sorting is a list of namedtuples, which may or may not work on a single namedtuple made of row (record) namedtuples. I'll definitely try it tomorrow. Know any way to convert a list back into a namedtuple? I suppose I could go through all the steps used at the beginning to make records, but that seems a waste if there's any way at all to sort the namedtuple without converting it into a list. Thanks Steven! Maybe sorted() is my friend here. (haha, and maybe not.) Deborah From rhodri at kynesim.co.uk Mon Jan 9 07:27:38 2017 From: rhodri at kynesim.co.uk (Rhodri James) Date: Mon, 9 Jan 2017 12:27:38 +0000 Subject: Clickable hyperlinks In-Reply-To: <4018729974@f38.n261.z1.binkp.net> References: <4018729974@f38.n261.z1.binkp.net> Message-ID: <6cafdb17-0a68-1000-abc0-fd8634fd473d@kynesim.co.uk> On 05/01/17 02:53, Deborah Swanson (Deborah Swanson) wrote: > Rhodri James wrote, on January 05, 2017 3:53 AM >> >> On 05/01/17 04:52, Deborah Swanson wrote: >>> My original question was in fact whether there was a way to make >>> clickable hyperlinks in a console. I was persuaded after about 10 >>> replies that the answer was no, >> >> Then you were persuaded wrong; the actual answer was "this isn't a >> meaningful question since it's based on incorrect assumptions." >> Translating that to "No" is just as much a mistake as >> translating it to >> "Yes." > > Actually, your statement "this isn't a meaningful question since it's based on > incorrect assumptions" is false. PyCharm outputs clickable links to the > console, but they aren't web links, they simply link to lines of code. Nope. PyCharm outputs text to the console that the console chooses to interpret as a link and makes clickable. As Stephen pointed out right back at the beginning of this thread, printing the textual string that is a URL could do exactly the same thing *if* the console you print to chooses to interpret it as such. The choice is with the console, not your program; that there is the incorrect assumption. -- Rhodri James *-* Kynesim Ltd From rustompmody at gmail.com Mon Jan 9 07:59:56 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Mon, 9 Jan 2017 04:59:56 -0800 (PST) Subject: Temporary variables in list comprehensions In-Reply-To: References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <871swc1pez.fsf@nightsong.com> <20170109061531.71e3c795@bigbox.christie.dr> Message-ID: <135a6b02-def4-4b59-b230-ee5a5a733bfd@googlegroups.com> On Monday, January 9, 2017 at 5:54:15 PM UTC+5:30, Tim Chase wrote: > On 2017-01-09 02:46, Paul Rubin wrote: > > > gen = (expensive_calculation(x) for x in data) > > > result = [(tmp, tmp + 1) for tmp in gen] > > > > result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] > > As charmingly expressive as map() is, the wildly different behavior in > py3 (it's a generator that evaluates lazily) vs py2 (it consumes the > entire iterable in one go) leads me to avoid it in general, > especially when Python gives me list-comprehensions and generators > that can do what I intend explicitly. E.g., passing in > itertools.count() as an iterable to map() will kill py2 but is > perfectly fine in py3 (a horrible example): > > import itertools as i > for x in i.takewhile( > lambda n: n < 100, > map(lambda g: g**2, i.count()) > ): > print(x) > > But yes, both ChrisA's pass-it-to-a-function and Serhiy's nested > generate-the-tmp-values-then-operate-on-those are good solutions > depending on how they feel in any particular context. If I need to > use an "if" clause in the outer generator that tests the resulting > temp values, I use Serhiy's solution: > > [(tmp, tmp + 1) > for tmp in ( > expensive_calculation(x) > for x in data > ) > if tmp > 42 > ] What happens when the expensive is on an inner generator? Something like: [expensive?(y) for x in data for y in foo(x)] [The ? representing the 2 or more occurrences in Steven's eg] From python at deborahswanson.net Mon Jan 9 08:00:16 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 05:00:16 -0800 Subject: Clickable hyperlinks In-Reply-To: <6cafdb17-0a68-1000-abc0-fd8634fd473d@kynesim.co.uk> Message-ID: <007401d26a78$530df890$27b23dae@sambora> Rhodri James wrote, on January 09, 2017 4:28 AM > > Nope. PyCharm outputs text to the console that the console > chooses to > interpret as a link and makes clickable. As Stephen pointed > out right > back at the beginning of this thread, printing the textual > string that > is a URL could do exactly the same thing *if* the console you > print to > chooses to interpret it as such. The choice is with the console, not > your program; that there is the incorrect assumption. > > -- > Rhodri James *-* Kynesim Ltd Sorry, Rhodri. I don't agree with your logic. You can use tkinter (code in a program) to make clickable links in the console, and the webbrowser module to web-enable them so urls open in a browser when you click on them. I have no idea how this crowd got off on the mantra "The choice is with the console". Code does in fact have the power to control what happens in the console. How do you think Linux does it on their terminals with clickable links? Granted, the code may have to specify parameters for a particular console, but I certainly wasn't asking for universal code that would work with any console. That was something made up by the responders on the thread, so they could revile me for such an outrageously impossible demand. My original question was if Python had anything equivalent to the hyperlink formula in Excel, which is a program (code) feature. Nothing about doing it on any console in the universe. The developers who wrote PyCharm coded it for the console they were using. The console is a dead thing, it has no mind or soul to choose anything. Surely an educated person would know that. From josemsuarezsierra at gmail.com Mon Jan 9 08:08:51 2017 From: josemsuarezsierra at gmail.com (=?UTF-8?Q?Jos=C3=A9_Manuel_Su=C3=A1rez_Sierra?=) Date: Mon, 9 Jan 2017 05:08:51 -0800 (PST) Subject: Help with this code Message-ID: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> Hello, I am trying to make a code wich compares between 2 or several sequences (lists). It compares every element in a list with another list elements. For example, if we have a list_a=["a","b","c","d"] and list_b=["a","b"] I want to obtain a new list_c containing elements that match between these lists (a and b here), but, if for instance list_b were ["a","c"] the program must not store this data because they are not in same order. Said this, I wrote this code but it doesnt work: if __name__ == "__main__": def compare(a, b): i = 0 j = 0 c1 = [] while a[i] == b[j]: c1.append(a[i]) j = j+1 i=i+1 return c1 cadena_1=raw_input("Introduce list 1 \n") cadena_2 = raw_input("Introduce list 2 \n") transf1=list(cad_1) transf2 = list(cad_2) print compare(transf1,transf2) Thank you From antoon.pardon at rece.vub.ac.be Mon Jan 9 08:12:24 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 9 Jan 2017 14:12:24 +0100 Subject: Temporary variables in list comprehensions In-Reply-To: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: <718d3926-e673-3d7b-e55c-8df5db719957@rece.vub.ac.be> Op 09-01-17 om 04:53 schreef Steven D'Aprano: > Suppose you have an expensive calculation that gets used two or more times in a > loop. The obvious way to avoid calculating it twice in an ordinary loop is with > a temporary variable: > > result = [] > for x in data: > tmp = expensive_calculation(x) > result.append((tmp, tmp+1)) > > > But what if you are using a list comprehension? Alas, list comps don't let you > have temporary variables, so you have to write this: > > > [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data] > > > Or do you? ... no, you don't! > > > [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > > > I can't decide whether that's an awesome trick or a horrible hack... Maybe this in an occasion to use your recipe. http://code.activestate.com/recipes/580625-collection-pipeline-in-python/ result = data | Map(expensive_calculation) | Map(lambda tmp: (tmp, tmp + 1)) | List -- Antoon. From python at deborahswanson.net Mon Jan 9 08:39:33 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 05:39:33 -0800 Subject: Help with this code In-Reply-To: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> Message-ID: <007a01d26a7d$d034e770$27b23dae@sambora> Jos? Manuel Su?rez Sierra wrote, on January 09, 2017 5:09 AM > > Hello, I am trying to make a code wich compares between 2 or > several sequences (lists). It compares every element in a > list with another list elements. For example, if we have a > list_a=["a","b","c","d"] and list_b=["a","b"] I want to > obtain a new list_c containing elements that match between > these lists (a and b here), but, if for instance list_b were > ["a","c"] the program must not store this data because they > are not in same order. > > Said this, I wrote this code but it doesnt work: > > if __name__ == "__main__": > > > def compare(a, b): > > i = 0 > j = 0 > c1 = [] > > while a[i] == b[j]: > c1.append(a[i]) > j = j+1 > i=i+1 > > return c1 > > > > > cadena_1=raw_input("Introduce list 1 \n") > cadena_2 = raw_input("Introduce list 2 \n") > > > transf1=list(cad_1) > transf2 = list(cad_2) > > > print compare(transf1,transf2) > > > > > Thank you I think your code assumes that a and b are the same length, and that all the elements in the intersection (the ones in both lists) are in order at the beginning of the list, like: a = [1, 2, 5, 8, 22, 11, 14] b = [1, 2, 5, 19, 12, 31, 42] You only want to append the current list element if you test that it's in both lists, and the answer is True. We do that with an 'if' statement. You need something like this, and a for loop would be the better choice here. cl = [] # look at every list element in 'a' for c in a: # is this element in 'b'? if e in b: # if True, then append e to the result list, or continue if False cl.append(e) return cl There's fancier ways to do it all in one line of code, but you need to understand the fundamentals before you move on to tricks. Deborah From josemsuarezsierra at gmail.com Mon Jan 9 08:46:44 2017 From: josemsuarezsierra at gmail.com (=?UTF-8?Q?Jos=C3=A9_Manuel_Su=C3=A1rez_Sierra?=) Date: Mon, 9 Jan 2017 05:46:44 -0800 (PST) Subject: Help with this code In-Reply-To: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> References: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> Message-ID: <625c76bf-2548-4261-adc9-274cc77a5051@googlegroups.com> El lunes, 9 de enero de 2017, 14:09:09 (UTC+1), Jos? Manuel Su?rez Sierra escribi?: > Hello, I am trying to make a code wich compares between 2 or several sequences (lists). It compares every element in a list with another list elements. For example, if we have a list_a=["a","b","c","d"] and list_b=["a","b"] I want to obtain a new list_c containing elements that match between these lists (a and b here), but, if for instance list_b were ["a","c"] the program must not store this data because they are not in same order. > > Said this, I wrote this code but it doesnt work: > > if __name__ == "__main__": > > > def compare(a, b): > > i = 0 > j = 0 > c1 = [] > > while a[i] == b[j]: > c1.append(a[i]) > j = j+1 > i=i+1 > > return c1 > > > > > cadena_1=raw_input("Introduce list 1 \n") > cadena_2 = raw_input("Introduce list 2 \n") > > > transf1=list(cad_1) > transf2 = list(cad_2) > > > print compare(transf1,transf2) > > > > > Thank you Thanks for your reply, I wrote this code: def comparar(cad1, cad2): i = 0 j = 0 cad3 = [] k = True for cad1[i] in cad1: k = True for cad2[j] in cad2: while cad1[i] == cad2[j] and i<= len(cad1) and j <= len(cad2): cad3.append(cad1[i]) i = i + 1 j = j + 1 if cad1[i] != cad2[j]: k = False i = i + 1 return cad3 This is a function, another important thing is the order of elements, it must be ordered as it appears in one of the lists, in your example, my function must return a list with elements 1,2,5 in that order. I cant find my mistake in these function I wrote you above. Thank you very much From __peter__ at web.de Mon Jan 9 08:46:56 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 09 Jan 2017 14:46:56 +0100 Subject: Help with this code References: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> Message-ID: Jos? Manuel Su?rez Sierra wrote: > Hello, Welcome! > I am trying to make a code wich compares between 2 or several > sequences (lists). It compares every element in a list with another list > elements. For example, if we have a list_a=["a","b","c","d"] and > list_b=["a","b"] I want to obtain a new list_c containing elements that > match between these lists (a and b here), but, if for instance list_b were > ["a","c"] the program must not store this data because they are not in > same order. > > Said this, I wrote this code but it doesnt work: Get into the habit of describing what "doesn't work" as precisely as you can. That is the first and most important step to fixing a bug. Python often helps you with that task by producing a "traceback", in your case something like $ python tmp3.py File "tmp3.py", line 12 j = j+1 ^ IndentationError: unexpected indent probably with another filename and line number. This is the compiler trying to tell you that line 12 has more leading whitespace than the preceding one, something that should only occur e. g. for the body of a while-loop, not at random places like the j = j+1 that follows the ordinary statement c1.append(a[i]) Note that once you have fixed your code in this place you will run into another error. Read the traceback carefully and try to figure out what's going on. If you need help come back here or, better, ask on the tutor mailing list which is dedicated to newbies making their first steps with Python. Good luck! > if __name__ == "__main__": > > > def compare(a, b): > > i = 0 > j = 0 > c1 = [] > > while a[i] == b[j]: > c1.append(a[i]) > j = j+1 > i=i+1 > > return c1 > > > > > cadena_1=raw_input("Introduce list 1 \n") > cadena_2 = raw_input("Introduce list 2 \n") > > > transf1=list(cad_1) > transf2 = list(cad_2) > > > print compare(transf1,transf2) > > > > > Thank you From python.list at tim.thechases.com Mon Jan 9 08:53:13 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 9 Jan 2017 07:53:13 -0600 Subject: Clickable hyperlinks In-Reply-To: <007401d26a78$530df890$27b23dae@sambora> References: <6cafdb17-0a68-1000-abc0-fd8634fd473d@kynesim.co.uk> <007401d26a78$530df890$27b23dae@sambora> Message-ID: <20170109075313.583d49e2@bigbox.christie.dr> On 2017-01-09 05:00, Deborah Swanson wrote: > Code does in fact have the power to control what happens > in the console. How do you think Linux does it on their terminals > with clickable links? Granted, the code may have to specify > parameters for a particular console, but I certainly wasn't asking > for universal code that would work with any console. Only to a degree. Some consoles sniff the text sent to them for particular URL-like patterns and linkify them for you. All you have to do is print a URL that its pattern-matching identifies: print("http://example.com") However, as Rhodri details, *this is a feature of the terminal emulator*. I've got a smattering of terminal emulators at my disposal and many don't auto-linkify (xterm, rxvt, the base non-X terminal) while others do (Gnome terminal). And in the ones where it doesn't work, it's because it's the *terminal* that doesn't support linkifying, so no amount of work on the application's part will make it linkify. > The console is a dead thing, it has no mind or soul to choose > anything. Surely an educated person would know that. Pretty much every quality system administrator I know uses the terminal. Just about all of the best devs I know use the terminal. Microsoft added Powershell because of demand. They added Ubuntu/bash support because of demand. It allows for powerful automation that would otherwise require writing full-fledged scripts. There is nothing dead about it. I'm not sure where you get your baseless claim that "an educated person would know that", since someone who had taken the time to study the state of terminal use would see that is far from dead. -tkc From python.list at tim.thechases.com Mon Jan 9 09:21:43 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 9 Jan 2017 08:21:43 -0600 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <003301d26a45$cc04a5b0$27b23dae@sambora> References: <003301d26a45$cc04a5b0$27b23dae@sambora> Message-ID: <20170109082143.6b0a019f@bigbox.christie.dr> On 2017-01-08 22:58, Deborah Swanson wrote: > 1) I have a section that loops through the sorted data, compares two > adjacent rows at a time, and marks one of them for deletion if the > rows are identical. > > I'm using > > for i in range(len(records)-1): > r1 = records[i] > r2 = records[i+1] > if r1.xx = r2.xx: > . > . > and my question is whether there's a way to work with two adjacent > rows without using subscripts? I usually wrap the iterable in something like def pairwise(it): prev = next(it) for thing in it: yield prev, thing prev = thing for prev, cur in pairwise(records): compare(prev, cur) which I find makes it more readable. -tkc From josemsuarezsierra at gmail.com Mon Jan 9 09:47:47 2017 From: josemsuarezsierra at gmail.com (=?UTF-8?Q?Jos=C3=A9_Manuel_Su=C3=A1rez_Sierra?=) Date: Mon, 9 Jan 2017 06:47:47 -0800 (PST) Subject: Help with this code In-Reply-To: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> References: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> Message-ID: <36919c0e-ebc3-456f-889a-121081f28a37@googlegroups.com> El lunes, 9 de enero de 2017, 14:09:09 (UTC+1), Jos? Manuel Su?rez Sierra escribi?: > Hello, I am trying to make a code wich compares between 2 or several sequences (lists). It compares every element in a list with another list elements. For example, if we have a list_a=["a","b","c","d"] and list_b=["a","b"] I want to obtain a new list_c containing elements that match between these lists (a and b here), but, if for instance list_b were ["a","c"] the program must not store this data because they are not in same order. > > Said this, I wrote this code but it doesnt work: > > if __name__ == "__main__": > > > def compare(a, b): > > i = 0 > j = 0 > c1 = [] > > while a[i] == b[j]: > c1.append(a[i]) > j = j+1 > i=i+1 > > return c1 > > > > > cadena_1=raw_input("Introduce list 1 \n") > cadena_2 = raw_input("Introduce list 2 \n") > > > transf1=list(cad_1) > transf2 = list(cad_2) > > > print compare(transf1,transf2) > > > > > Thank you Thank you! Here is my code, I dont understand why it doesnt work very well: if __name__ == "__main__": cadena_1 = raw_input("Introduce la cadena 1 \n") cadena_2 = raw_input("Introduce la cadena 2 \n") # n=input("De cuantas letras quieres hacer la comparacion?\n") transf1 = list(cadena_1) transf2 = list(cadena_2) l1=len(transf1) l2=len(transf2) c3=[] i=0 j=0 for transf1[i] in transf1: for transf2[j] in transf2: if transf1[i]==transf2[j]: c3.append(transf1[i]) i=i+1 j=j+1 else: j=j+1 print c3 This is the traceback: line 18, in for transf2[j] in transf2: IndexError: list assignment index out of range If I have initialized j=0 (such as i) why does it not work? I want the script to read sequence 1 and compares every element inside it with elements in sequence 2 no mattering where it matches. Thank you! From __peter__ at web.de Mon Jan 9 09:51:14 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 09 Jan 2017 15:51:14 +0100 Subject: Using namedtuples field names for column indices in a list of lists References: <003301d26a45$cc04a5b0$27b23dae@sambora> Message-ID: Deborah Swanson wrote: > Even better, to get hold of all the records with the same Description as > the current row, compare them all, mark all but the different ones for > deletion, and then resume processing the records after the last one? When you look at all fields for deduplication anyway there's no need to treat one field (Description) specially. Just records = set(records) should be fine. As the initial order is lost* you probably want to sort afterwards. The code then becomes records = sorted( set(records), key=operator.attrgetter("Description") ) Now if you want to fill in missing values, you should probably do this before deduplication -- and the complete() function introduced in https://mail.python.org/pipermail/python-list/2016-December/717847.html can be adapted to work with namedtuples instead of dicts. (*) If you want to preserve the initial order you can use a collections.OrderedDict instead of the set. From rhodri at kynesim.co.uk Mon Jan 9 10:15:04 2017 From: rhodri at kynesim.co.uk (Rhodri James) Date: Mon, 9 Jan 2017 15:15:04 +0000 Subject: Clickable hyperlinks In-Reply-To: <20170109075313.583d49e2@bigbox.christie.dr> References: <6cafdb17-0a68-1000-abc0-fd8634fd473d@kynesim.co.uk> <007401d26a78$530df890$27b23dae@sambora> <20170109075313.583d49e2@bigbox.christie.dr> Message-ID: On 09/01/17 13:53, Tim Chase wrote: > On 2017-01-09 05:00, Deborah Swanson wrote: >> The console is a dead thing, it has no mind or soul to choose >> anything. Surely an educated person would know that. > > Pretty much every quality system administrator I know uses the > terminal. Just about all of the best devs I know use the terminal. > Microsoft added Powershell because of demand. They added Ubuntu/bash > support because of demand. It allows for powerful automation that > would otherwise require writing full-fledged scripts. There is > nothing dead about it. I think you're falling foul of Deborah's inability to communicate again. I think she meant to say that the console is dumb, not dead. In a very strict sense that's almost true, but since we've been using "console" interchangeably with "terminal emulator" throughout this discussion, it's hilariously wrong. -- Rhodri James *-* Kynesim Ltd From ghasselbring at hotmail.com Mon Jan 9 10:54:15 2017 From: ghasselbring at hotmail.com (Gretchen Hasselbring) Date: Mon, 9 Jan 2017 15:54:15 +0000 Subject: Error message IDLE In-Reply-To: References: Message-ID: FYI Turns out I was saving my practice exercises as files (.py) under the python directory which conflicted with the running of Idle. All cleared up now. Sent from my iPad > On 9 Jan 2017, at 12:16, Gretchen Hasselbring wrote: > > Hello > > Trying to learn python on a laptop. Was successful for awhile then... > > Had a 'subprocess startup error' > > 'IDLE's subprocess didn't make connection. Either IDLE can't start subprocess or personal firewall software is blocking the connection ' > > > Doesn't appear to be firewall and I uninstalled and reinstalled python 3.6.032 bit??? > > Any advice? > Thanks > > > Sent from my iPad > -- > https://mail.python.org/mailman/listinfo/python-list From tjreedy at udel.edu Mon Jan 9 10:58:52 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 9 Jan 2017 10:58:52 -0500 Subject: Error message IDLE In-Reply-To: References: Message-ID: On 1/9/2017 6:48 AM, Gretchen Hasselbring wrote: > Hello > > Trying to learn python on a laptop. Was successful for awhile then... > > Had a 'subprocess startup error' > > 'IDLE's subprocess didn't make connection. Either IDLE can't start subprocess or personal firewall software is blocking the connection ' > > > Doesn't appear to be firewall and I uninstalled and reinstalled python 3.6.032 bit??? > > Any advice? When IDLE works consistently and then consistently does not work, something changed on your system. Since reinstalling did not fix the issue, the change was not in Python and IDLE. The first thing to do is to run IDLE from your system's console with '/python -m idlelib' and see if an error message appears. There are at least 5 other possible causes, including 1. You created a file with the same name as a stdlib mdoule. (Rename it.) 2. There is a problem with a config file in $HOME/.idlerc/. (Easier is to delete.) 3. Some network setting change cause socket connection to fail. -- Terry Jan Reedy From __peter__ at web.de Mon Jan 9 10:59:17 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 09 Jan 2017 16:59:17 +0100 Subject: Help with this code References: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> <36919c0e-ebc3-456f-889a-121081f28a37@googlegroups.com> Message-ID: Jos? Manuel Su?rez Sierra wrote: > This is the traceback: > line 18, in > for transf2[j] in transf2: > IndexError: list assignment index out of range > > If I have initialized j=0 (such as i) why does it not work? A for loop for x in y: ... sequentually assigns every value in y to x. So with y = "ab" it executes x = "a" ... x = "b" ... This is the case even when there is something more complex like transf2[j]: for transf2[j] in y: ... translates to transf2[j] = "a" ... # both transf2 and j may be rebound here and thus affect transf2[j] = "b" # what this actually does ... and thus can fail with an IndexError when j is bound to a value greater than the length of the transf2 list. But most likely you want to avoid this feature of Python -- even the experts never use it. > I want the script to read sequence 1 and compares every element inside it > with elements in sequence 2 no mattering where it matches. That may be sufficient to explain the objective to a human who already has a rough idea of your goal, but to come up with an actual algorithm you need to think about -- and communicate -- a lot more detail. There is a third party module that operates on strings, not lists which finds the edit operations >>> Levenshtein.editops("abcdefgj", "axcdyezfg") [('replace', 1, 1), ('insert', 4, 4), ('insert', 5, 6), ('delete', 7, 9)] What should your function return given the equivalent lists ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'j'] ['a', 'x', 'c', 'd', 'y', 'e', 'z', 'f', 'g'] ? Try to make your plain-english description as precise as possible before you start coding. From __peter__ at web.de Mon Jan 9 11:05:01 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 09 Jan 2017 17:05:01 +0100 Subject: Error message IDLE References: Message-ID: Gretchen Hasselbring wrote: > FYI > > Turns out I was saving my practice exercises as files (.py) under the > python directory which conflicted with the running of Idle. All cleared > up now. Thanks for the update. Can you tell which file you shadowed to cause the error? Perhaps even if the underlying problem turns out to be hard to fix at least the error message could be improved. From alemos at plux.info Mon Jan 9 11:17:41 2017 From: alemos at plux.info (=?UTF-8?B?QW5kcsOpIExlbW9z?=) Date: Mon, 9 Jan 2017 16:17:41 +0000 Subject: enable-framework Vs. Naught Message-ID: Hi, I have a C++ module that I am compiling to use inside of my Python installation under Mac OS. If I compile & link it against a Framework enabled Python installation, it works fine, but if I compile & link it against a *non* enabled Framework installation that we use for distribution, I simply get a non inspiring: Fatal Python error: PyThreadState_Get: no current thread I am using python-config to get my flags on both the examples, but I simply cannot get it to run (although it compiles fine) on a *non* enabled Framework installation. Thoughts/Help? -- Andr? Lemos From breamoreboy at gmail.com Mon Jan 9 11:31:55 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Mon, 9 Jan 2017 08:31:55 -0800 (PST) Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: References: <003301d26a45$cc04a5b0$27b23dae@sambora> <20170109082143.6b0a019f@bigbox.christie.dr> Message-ID: <51c2cf13-b516-4ebf-8ca9-a915520adcab@googlegroups.com> On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: > On 2017-01-08 22:58, Deborah Swanson wrote: > > 1) I have a section that loops through the sorted data, compares two > > adjacent rows at a time, and marks one of them for deletion if the > > rows are identical. > > > > I'm using > > > > for i in range(len(records)-1): > > r1 = records[i] > > r2 = records[i+1] > > if r1.xx = r2.xx: > > . > > . > > and my question is whether there's a way to work with two adjacent > > rows without using subscripts? > > I usually wrap the iterable in something like > > def pairwise(it): > prev = next(it) > for thing in it: > yield prev, thing > prev = thing > > for prev, cur in pairwise(records): > compare(prev, cur) > > which I find makes it more readable. > > -tkc Or from https://docs.python.org/3/library/itertools.html#itertools-recipes:- def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b) This and many other recipes are available in the more-itertools module which is on pypi. From ghasselbring at hotmail.com Mon Jan 9 12:02:02 2017 From: ghasselbring at hotmail.com (Gretchen Hasselbring) Date: Mon, 9 Jan 2017 17:02:02 +0000 Subject: Error message IDLE In-Reply-To: References: , Message-ID: Thank you for your responses. The file I think is responsible was called types.py Sent from my iPad > On 9 Jan 2017, at 16:11, Peter Otten <__peter__ at web.de> wrote: > > Gretchen Hasselbring wrote: > >> FYI >> >> Turns out I was saving my practice exercises as files (.py) under the >> python directory which conflicted with the running of Idle. All cleared >> up now. > > Thanks for the update. Can you tell which file you shadowed to cause the > error? Perhaps even if the underlying problem turns out to be hard to fix at > least the error message could be improved. > > > -- > https://mail.python.org/mailman/listinfo/python-list From python.list at tim.thechases.com Mon Jan 9 12:08:30 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 9 Jan 2017 11:08:30 -0600 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <51c2cf13-b516-4ebf-8ca9-a915520adcab@googlegroups.com> References: <003301d26a45$cc04a5b0$27b23dae@sambora> <20170109082143.6b0a019f@bigbox.christie.dr> <51c2cf13-b516-4ebf-8ca9-a915520adcab@googlegroups.com> Message-ID: <20170109110830.42cb734e@bigbox.christie.dr> On 2017-01-09 08:31, breamoreboy at gmail.com wrote: > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: > > I usually wrap the iterable in something like > > > > def pairwise(it): > > prev = next(it) > > for thing in it: > > yield prev, thing > > prev = thing > > Or from > https://docs.python.org/3/library/itertools.html#itertools-recipes:- > > def pairwise(iterable): > "s -> (s0,s1), (s1,s2), (s2, s3), ..." > a, b = tee(iterable) > next(b, None) > return zip(a, b) > > This and many other recipes are available in the more-itertools > module which is on pypi. Ah, helpful to not have to do it from scratch each time. Also, I see several others that I've coded up from scratch (particularly the partition() and first_true() functions). I usually want to make sure it's tailored for my use cases. The above pairwise() is my most common use case, but I occasionally want N-wise pairing s -> (s0,s1,?sN), (s1,s2,?S{N+1}), (s2,s3,?s{N+2}), ? or to pad them out so either the leader/follower gets *all* of the values, with subsequent values being a padding value: # lst = [s0, s1, s2] (s0,s1), (s1, s2), (s2, PADDING) # or (PADDING, s0), (s0, s1), (s1, s2) but it's good to have my common cases already coded & tested. -tkc From python.list at tim.thechases.com Mon Jan 9 12:11:29 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 9 Jan 2017 11:11:29 -0600 Subject: Temporary variables in list comprehensions In-Reply-To: <135a6b02-def4-4b59-b230-ee5a5a733bfd@googlegroups.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <871swc1pez.fsf@nightsong.com> <20170109061531.71e3c795@bigbox.christie.dr> <135a6b02-def4-4b59-b230-ee5a5a733bfd@googlegroups.com> Message-ID: <20170109111129.0170a709@bigbox.christie.dr> On 2017-01-09 04:59, Rustom Mody wrote: > What happens when the expensive is on an inner generator? > Something like: > > [expensive?(y) for x in data for y in foo(x)] > > [The ? representing the 2 or more occurrences in Steven's eg] Well, if I understand your question correctly, the goal would be to execute each expensive operation only once, regardless of how many expensive functions you execute: [(x, a, b, x+1, a+1, b+1) for x, a, b in ( x, expensive_a(x), expensive_b(x), for x in data ) # if x > 42 # optionally test against values ] -tkc From torriem at gmail.com Mon Jan 9 12:27:41 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 9 Jan 2017 10:27:41 -0700 Subject: Clickable hyperlinks In-Reply-To: <007401d26a78$530df890$27b23dae@sambora> References: <007401d26a78$530df890$27b23dae@sambora> Message-ID: On 01/09/2017 06:00 AM, Deborah Swanson wrote: > Rhodri James wrote, on January 09, 2017 4:28 AM >> >> Nope. PyCharm outputs text to the console that the console >> chooses to >> interpret as a link and makes clickable. As Stephen pointed >> out right >> back at the beginning of this thread, printing the textual >> string that >> is a URL could do exactly the same thing *if* the console you >> print to >> chooses to interpret it as such. The choice is with the console, not >> your program; that there is the incorrect assumption. > Sorry, Rhodri. I don't agree with your logic. You may, but that doesn't change the fact that Rhodri's post is 100% accurate. > You can use tkinter (code > in a program) to make clickable links in the console, Unless you're talking about an implementation of a console or terminal emulator in tkinter, this is incorrect. Tkinter does not do anything with standard out. > and the webbrowser > module to web-enable them so urls open in a browser when you click on > them. > > I have no idea how this crowd got off on the mantra "The choice is with > the console". I guess things keep going in circles because people are just trying to help you understand what these various parts of the system are and how they fit together, particularly with regards to so-called "console applications." (Text-mode, terminal-based, etc.) > Code does in fact have the power to control what happens > in the console. Other than a pre-arranged protocol (win32 console api, ANSI, VT100, or similar) between the displayer of the bytes and the python program, code does not in fact have power to control what happens in the "console." > How do you think Linux does it on their terminals with > clickable links? This is just the terminal emulator trying to be smart and assuming meaning in the bytes given to it, which may be a valid assumption on its part, or may not. This is no way implies that all terminal emulators should do this, nor do most terminal emulators. > Granted, the code may have to specify parameters for a > particular console, but I certainly wasn't asking for universal code > that would work with any console. Your initial query back in the beginning did not state this. Indeed you never stated which console you wanted to work with. We addressed several common ones including the Win32 console window (which cmd.exe runs in) and various terminal emulators. > That was something made up by the > responders on the thread, so they could revile me for such an > outrageously impossible demand. My original question was if Python had > anything equivalent to the hyperlink formula in Excel, which is a > program (code) feature. Nothing about doing it on any console in the > universe. I read frustration in several posts, but never reviling! > The developers who wrote PyCharm coded it for the console they were > using. The PyCharm developers built their own window to display "console" output (standard-out bytes) from the app, and they could have their own window interpret bytes and messages however they wanted to, and added their own PyCharm-specific feature including hot links to error locations (I think that comes from standard-err, though, not standard-out). So if you want the PyCharm output window to create a clickable hot-link for you you have to modify the PyCharm code to do that. > The console is a dead thing, it has no mind or soul to choose > anything. Surely an educated person would know that. Not sure what you mean by that? From __peter__ at web.de Mon Jan 9 12:30:30 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 09 Jan 2017 18:30:30 +0100 Subject: Search a sequence for its minimum and stop as soon as the lowest possible value is found References: <725de1f8-42f4-4cef-9ca3-58a2c7477576@googlegroups.com> Message-ID: Antonio Caminero Garcia wrote: > On Friday, January 6, 2017 at 6:04:33 AM UTC-8, Peter Otten wrote: >> Example: you are looking for the minimum absolute value in a series of >> integers. As soon as you encounter the first 0 it's unnecessary extra >> work to check the remaining values, but the builtin min() will continue. >> >> The solution is a minimum function that allows the user to specify a stop >> value: >> >> >>> from itertools import count, chain >> >>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0) >> 0 >> >> How would you implement stopmin()? >> >> Currently I raise an exception in the key function: >> >> class Stop(Exception): >> pass >> >> def stopmin(items, key, stop): >> """ >> >>> def g(): >> ... for i in reversed(range(10)): >> ... print(10*i) >> ... yield str(i) >> >>> stopmin(g(), key=int, stop=5) >> 90 >> 80 >> 70 >> 60 >> 50 >> '5' >> """ >> def key2(value): >> result = key(value) >> if result <= stop: >> raise Stop(value) >> return result >> try: >> return min(items, key=key2) >> except Stop as stop: >> return stop.args[0] > > This is the simplest version I could come up with. I also like the classic > 100% imperative, but it seems that is not trendy between the solutions > given :D. > > you can test it here https://repl.it/FD5A/0 > source code: > > from itertools import accumulate > > # stopmin calculates the greatest lower bound (infimum). > # https://upload.wikimedia.org/wikipedia/commons/0/0a/Infimum_illustration.svg > > def takeuntil(pred, seq): > for item in seq: > yield item > if not pred(item): The "not": one of those decisions that can drive programmers into madness ;) > break > > def stopmin(seq, stop=0): > drop_ltstop = (item for item in seq if item >= stop) > min_gen = (min_ for min_ in accumulate(drop_ltstop, func=min)) You don't need the genexp here; just accumulate() will do. Using accumulate() is a nice suggestion, by the way. > return list(takeuntil(lambda x: x!= stop, min_gen))[-1] > seq = [1, 4, 7, -8, 0, 7, -8, 9] # 0 just until zero is generated > seq = [1, 4, 7, -8, 7, -8, 9] # 1 the entire sequence is generated > > print(stopmin(seq, stop=0)) Once it passes my doctests your code isn't quite as simple, but still readable: from itertools import accumulate from operator import itemgetter from collections import deque firstitem = itemgetter(0) def takeuntil(pred, items): for item in items: yield item if pred(item): break def last(items): tail = deque(items, maxlen=1) if tail: return tail.pop() else: raise ValueError def minkey(a, b): return min(a, b, key=firstitem) def stopmin(items, *, key, stop): """ >>> def g(): ... for i in reversed(range(10)): ... print(10*i) ... yield str(i) >>> stopmin(g(), key=int, stop=5) 90 80 70 60 50 '5' >>> stopmin(g(), key=int, stop=8.5) 90 80 '8' >>> stopmin(g(), key=int, stop=9) 90 '9' >>> stopmin([10, 9, -11, 7, -12], key=abs, stop=0) 7 >>> try: stopmin([], key=abs, stop=0) ... except ValueError: print('OK') OK >>> stopmin("a", key=lambda x: print(x) or "c", stop="b") a 'a' >>> class A: ... def __init__(self, key): ... self.key = key >>> stopmin([A(2), A(2), A(1), A(0)], key=lambda a: a.key, stop=1.1).key 1 """ pairs = ((key(item), item) for item in items) descending_pairs = accumulate(pairs, func=minkey) return last(takeuntil(lambda p: p[0] <= stop, descending_pairs))[1] From exarkun at twistedmatrix.com Mon Jan 9 12:58:24 2017 From: exarkun at twistedmatrix.com (Jean-Paul Calderone) Date: Mon, 9 Jan 2017 12:58:24 -0500 Subject: Announcing txAWS 0.2.3.1 Message-ID: I've just release txAWS 0.2.3.1. txAWS is a library for interacting with Amazon Web Services (AWS) using Twisted. AWSServiceEndpoint's ssl_hostname_verification's parameter now defaults to True instead of False. This affects all txAWS APIs which issue requests to AWS endpoints. For any application which uses the default AWSServiceEndpoints, the server's TLS certificate will now be verified. This resolves a security issue in which txAWS applications were vulnerable to man-in-the-middle attacks which could either steal sensitive information or, possibly, alter the AWS operation requested. The new release is available on PyPI in source and wheel forms. You can also find txAWS at its new home on github, . Special thanks to Least Authority Enterprises () for sponsoring the work to find and fix this issue and to publish this new release. Jean-Paul From Joaquin.Alzola at lebara.com Mon Jan 9 13:14:45 2017 From: Joaquin.Alzola at lebara.com (Joaquin Alzola) Date: Mon, 9 Jan 2017 18:14:45 +0000 Subject: Help with this code In-Reply-To: References: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> Message-ID: >> elements. For example, if we have a list_a=["a","b","c","d"] and >> list_b=["a","b"] I want to obtain a new list_c containing elements that >> match between these lists (a and b here), >Perhaps this might work: >>>> list(set(list_a).intersection(set(list_b))) >['a', 'b'] >>> list_a={"a","b","c","d"} >>> list_b={"a","b"} > sorted(list_a & list_b) ['a', 'b'] ------ 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 joel.goldstick at gmail.com Mon Jan 9 13:43:35 2017 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 9 Jan 2017 13:43:35 -0500 Subject: Help with this code In-Reply-To: References: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> Message-ID: On Mon, Jan 9, 2017 at 1:02 PM, Gilmeh Serda wrote: > On Mon, 09 Jan 2017 05:08:51 -0800, Jos? Manuel Su?rez Sierra wrote: > >> elements. For example, if we have a list_a=["a","b","c","d"] and >> list_b=["a","b"] I want to obtain a new list_c containing elements that >> match between these lists (a and b here), > > Perhaps this might work: > >>>> list(set(list_a).intersection(set(list_b))) > ['a', 'b'] > > HTH > -- > Gilmeh > -- > https://mail.python.org/mailman/listinfo/python-list I was confused whether the OP wanted to retain the list order. The spec seemed unclear. If not, this is a nice approach. Of course this also removes duplicate values. -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From torriem at gmail.com Mon Jan 9 14:00:13 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 9 Jan 2017 12:00:13 -0700 Subject: Clickable hyperlinks In-Reply-To: References: <007401d26a78$530df890$27b23dae@sambora> Message-ID: <968c2174-f32d-d225-9b81-549c3c998c67@gmail.com> On 01/09/2017 10:27 AM, Michael Torrie wrote: >> You can use tkinter (code >> in a program) to make clickable links in the console, > > Unless you're talking about an implementation of a console or terminal > emulator in tkinter, this is incorrect. Tkinter does not do anything > with standard out. To be clearer, what I mean is that using Tkinter or some other toolkit you can indeed place links on a GUI form or window. But this has nothing to do with a terminal emulator or console (meaning the display of standard-out bytes). Using your understanding of the word, console, Tkinter could be used to create a console of your own that displays the output from other processes and implements clickable hyperlinks, but Tkinter itself does not display using standard out, and thus cannot cause a terminal emulator or Windows console to display hyperlinks in a clickable fashion. From tonycamgar at gmail.com Mon Jan 9 14:15:59 2017 From: tonycamgar at gmail.com (Antonio Caminero Garcia) Date: Mon, 9 Jan 2017 11:15:59 -0800 (PST) Subject: Temporary variables in list comprehensions In-Reply-To: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: <11bfb5cd-c8c3-408a-bedb-ca9eafb78460@googlegroups.com> On Sunday, January 8, 2017 at 7:53:37 PM UTC-8, Steven D'Aprano wrote: > Suppose you have an expensive calculation that gets used two or more times in a > loop. The obvious way to avoid calculating it twice in an ordinary loop is with > a temporary variable: > > result = [] > for x in data: > tmp = expensive_calculation(x) > result.append((tmp, tmp+1)) > > > But what if you are using a list comprehension? Alas, list comps don't let you > have temporary variables, so you have to write this: > > > [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data] > > > Or do you? ... no, you don't! > > > [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > > > I can't decide whether that's an awesome trick or a horrible hack... > > > -- > Steven > "Ever since I learned about confirmation bias, I've been seeing > it everywhere." - Jon Ronson Hello I saw some memoizing functions, in that sense you can use the functools.lru_cache decorator, that is even better if you have repeated elements in data. @functools.lru_cache def expensive_calculation(x): # very NP-hard calculation pass Hope that helps :) From auriocus at gmx.de Mon Jan 9 14:43:01 2017 From: auriocus at gmx.de (Christian Gollwitzer) Date: Mon, 9 Jan 2017 20:43:01 +0100 Subject: Temporary variables in list comprehensions In-Reply-To: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 09.01.17 um 04:53 schrieb Steven D'Aprano: > Or do you? ... no, you don't! > > [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > > I can't decide whether that's an awesome trick or a horrible hack... I think this is quite clear, and a useful feature, only that Python makes it unnecessarily hard. In Haskell, there is a "let" clause, which would allow to write it as: [let tmp = expensive_calc(x), (tmp, tmp+1) for x in data] or better readable using "with" or "where" as in [(tmp, tmp + 1) with tmp = expensive_calc(x) for x in data] or similar. So maybe that's a PEP to extend the list comprehension syntax? Christian From danny.adair at unfold.co.nz Mon Jan 9 16:01:05 2017 From: danny.adair at unfold.co.nz (Danny Adair) Date: Tue, 10 Jan 2017 10:01:05 +1300 Subject: [PSF-Community] Python Events in 2017, Need your help. In-Reply-To: <20170109095449.v4ual5czl5mc7xxs@sg1> References: <20170109095449.v4ual5czl5mc7xxs@sg1> Message-ID: Thanks Stephane, Kiwi PyCon 2017 will be in Auckland, New Zealand in September - exact dates and location not yet determined. I'll submit it when they are. Cheers, Danny On Mon, Jan 9, 2017 at 10:54 PM, Stephane Wirtel via PSF-Community wrote: > Dear Community, > > For the PythonFOSDEM [1] on 4th and 5th February in Belgium, I would like to > present some slides with the Python events around the World. Based on > https://python.org/events, I have noted that there are missing events, for > example: > > * PyCon Otto: Italy > * PyCon UK: United Kingdom > * PyCon CA: Canada > * PyCon Ireland: Ireland > * PyCon France: France > > Some of these events are not yet announced and I understand they are in the > second semester, and thus, they don't know the location and the dates, > excepted for PyCon Otto (April). > > In fact, I have noted that we know some big events in the Python community > (for example: PyCon US and EuroPython) but do you know the others events, > maybe the local event, PyCon IE, PyCon UK or PyCon IT. > > I like to know where there is a PyCon or a Django Conf or a PyData Event. > > In fact, I think we can help the Python Community if we submit all the > events in https://python.org/events. > > This page has been created by the PSF and is maintained by some volunteers. > > I know this list of events: > * PyCon Cameroon : 20-23 Jav, Cameroon > * PythonFOSDEM : 4-5 Feb, Belgium > * PyCon Colombia : 10-12 Feb, Colombia > * PyCon Pune : 16-20 Feb, India > * Swiss Python Summit : 17-18 Feb, Switzerland > * IrPyCon : 17-18 Feb, Iran > * PyCon SK : 10-13 Mar, Slovakia > * Django Europe : 3-8 Apr, Italy > * PyCon Otto : 6-9 Apr, Italy > * Python Sudeste : 5-7 Mai, Brazil > * GeoPython : 8-11 May, Switzerland > * PyCon US : 17-26 May, USA > * EuroPython : July, Italy > * PyCon AU : 3-9 Aug, Australia > * PyCon UK : September, United Kingdom > * PyCon CA : November, Canada > * PyCon Ireland : October, Ireland > * PyCon FR : October/November, France > > And you ? > Please, could you check on https://www.python.org/events/ , if you are an > organizer, please add your event. > > If you think there is a missing event, please, send me the info via > [email](mailto:stephane at wirtel.be) or via my [twitter > account](https://twitter.com/matrixise) and I will add it on my slides. > > I would like to present your event. > > Thank you so much for your help. > > Stephane Wirtel > > [1] https://www.python-fosdem.org > > -- > St?phane Wirtel - http://wirtel.be - @matrixise > _______________________________________________ > PSF-Community mailing list > PSF-Community at python.org > https://mail.python.org/mailman/listinfo/psf-community -- Kind regards, Danny W. Adair From no.email at nospam.invalid Mon Jan 9 16:16:25 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Mon, 09 Jan 2017 13:16:25 -0800 Subject: Temporary variables in list comprehensions References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <871swc1pez.fsf@nightsong.com> <20170109061531.71e3c795@bigbox.christie.dr> Message-ID: <87tw98ylw6.fsf@nightsong.com> Tim Chase writes: >> result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] > > As charmingly expressive as map() is, the wildly different behavior in > py3 (it's a generator that evaluates lazily) vs py2 (it consumes the > entire iterable in one go) leads me to avoid it in general, Well, there's itertools.imap which maps lazily in py2. From python at deborahswanson.net Mon Jan 9 16:40:47 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 13:40:47 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: Message-ID: <003c01d26ac1$0a358e00$27b23dae@sambora> Peter Otten wrote, on January 09, 2017 6:51 AM > > Deborah Swanson wrote: > > > Even better, to get hold of all the records with the same Description > > as the current row, compare them all, mark all but the different ones > > for deletion, and then resume processing the records after the last > > one? > > When you look at all fields for deduplication anyway there's no need to > treat one field (Description) specially. Just > > records = set(records) I haven't worked with sets before, so this would be a good time to start. > should be fine. As the initial order is lost* you probably want to sort > afterwards. The code then becomes > > records = sorted( > set(records), > key=operator.attrgetter("Description") > ) Good, this is confirmation that 'sorted()' is the way to go. I want a 2 key sort, Description and Date, but I think I can figure out how to do that. > Now if you want to fill in missing values, you should probably do this > before deduplication That's how my original code was written, to fill in missing values as the very last thing before saving to csv. > -- and the complete() function introduced in >https://mail.python.org/pipermail/python-list/2016-December/717847.html > can be adapted to work with namedtuples instead of dicts. Ah, your defaultdict suggestion. Since my original comprows() function to fill in missing values is now broken after the rest of the code was rewritten for namedtuples (I just commented it out to test the namedtuples version), this would be a good time to look at defaultdict. > (*) If you want to preserve the initial order you can use a > collections.OrderedDict instead of the set. OrderedDict is another thing I haven't used, but would love to, so I think I'll try both the set and the OrderedDict, and see which one is best here. Thanks again Peter, all your help is very much appreciated. Deborah From python at deborahswanson.net Mon Jan 9 16:52:01 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 13:52:01 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <51c2cf13-b516-4ebf-8ca9-a915520adcab@googlegroups.com> Message-ID: <003d01d26ac2$9c17f0a0$27b23dae@sambora> breamoreboy at gmail.com wrote, on January 09, 2017 8:32 AM > > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: > > On 2017-01-08 22:58, Deborah Swanson wrote: > > > 1) I have a section that loops through the sorted data, compares two > > > adjacent rows at a time, and marks one of them for deletion if the > > > rows are identical. > > > and my question is whether there's a way to work with two adjacent > > > rows without using subscripts? > > > > I usually wrap the iterable in something like > > > > def pairwise(it): > > prev = next(it) > > for thing in it: > > yield prev, thing > > prev = thing > > > > for prev, cur in pairwise(records): > > compare(prev, cur) > > > > which I find makes it more readable. > > > > -tkc > > Or from > https://docs.python.org/3/library/itertools.ht> ml#itertools-recipes:- > > def pairwise(iterable): > "s -> (s0,s1), (s1,s2), (s2, s3), ..." > a, b = tee(iterable) > next(b, None) > return zip(a, b) > > This and many other recipes are available in the > more-itertools module which is on pypi. Thanks, I'll keep this since I seem to do pairwise comparisons a lot. I'm going to try using set or OrderedDict for the current problem, per Peter's suggestion, but if I can't make that work, this surely will. From python at deborahswanson.net Mon Jan 9 16:56:04 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 13:56:04 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <20170109082143.6b0a019f@bigbox.christie.dr> Message-ID: <003e01d26ac3$2d1036d0$27b23dae@sambora> Tim Chase wrote, on January 09, 2017 6:22 AM > > On 2017-01-08 22:58, Deborah Swanson wrote: > > 1) I have a section that loops through the sorted data, compares two > > adjacent rows at a time, and marks one of them for deletion if the > > rows are identical. > > and my question is whether there's a way to work with two adjacent > > rows without using subscripts? > > I usually wrap the iterable in something like > > def pairwise(it): > prev = next(it) > for thing in it: > yield prev, thing > prev = thing > > for prev, cur in pairwise(records): > compare(prev, cur) > > which I find makes it more readable. > > -tkc This looks very useful, and comparing two adjacent rows is something I do often. Thanks Tim! Deborah From breamoreboy at gmail.com Mon Jan 9 17:16:41 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Mon, 9 Jan 2017 14:16:41 -0800 (PST) Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: References: <003301d26a45$cc04a5b0$27b23dae@sambora> <20170109082143.6b0a019f@bigbox.christie.dr> <51c2cf13-b516-4ebf-8ca9-a915520adcab@googlegroups.com> <20170109110830.42cb734e@bigbox.christie.dr> Message-ID: <2ba0c533-b586-4865-a524-2ee0aaff14ad@googlegroups.com> On Monday, January 9, 2017 at 5:34:12 PM UTC, Tim Chase wrote: > On 2017-01-09 08:31, breamoreboy wrote: > > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: > > > I usually wrap the iterable in something like > > > > > > def pairwise(it): > > > prev = next(it) > > > for thing in it: > > > yield prev, thing > > > prev = thing > > > > Or from > > https://docs.python.org/3/library/itertools.html#itertools-recipes:- > > > > def pairwise(iterable): > > "s -> (s0,s1), (s1,s2), (s2, s3), ..." > > a, b = tee(iterable) > > next(b, None) > > return zip(a, b) > > > > This and many other recipes are available in the more-itertools > > module which is on pypi. > > Ah, helpful to not have to do it from scratch each time. Also, I see > several others that I've coded up from scratch (particularly the > partition() and first_true() functions). > > I usually want to make sure it's tailored for my use cases. The above > pairwise() is my most common use case, but I occasionally want N-wise > pairing def ntuplewise(iterable, n=2): args = tee(iterable, n) loops = n - 1 while loops: for _ in range(loops): next(args[loops], None) loops -= 1 return zip(*args) > > s -> (s0,s1,?sN), (s1,s2,?S{N+1}), (s2,s3,?s{N+2}), ? > > or to pad them out so either the leader/follower gets *all* of the > values, with subsequent values being a padding value: > > # lst = [s0, s1, s2] > (s0,s1), (s1, s2), (s2, PADDING) Use zip_longest instead of zip in the example code above. > # or > (PADDING, s0), (s0, s1), (s1, s2) Haven't a clue off of the top of my head and I'm too darn tired to think about it :) > > but it's good to have my common cases already coded & tested. > > -tkc Kindest regards. Mark Lawrence. From rhodri at kynesim.co.uk Mon Jan 9 17:28:00 2017 From: rhodri at kynesim.co.uk (Rhodri James) Date: Mon, 9 Jan 2017 22:28:00 +0000 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <003c01d26ac1$0a358e00$27b23dae@sambora> References: <003c01d26ac1$0a358e00$27b23dae@sambora> Message-ID: <43063cff-c970-3221-96bc-11b6d8638e9e@kynesim.co.uk> On 09/01/17 21:40, Deborah Swanson wrote: > Peter Otten wrote, on January 09, 2017 6:51 AM >> >> records = sorted( >> set(records), >> key=operator.attrgetter("Description") >> ) > > Good, this is confirmation that 'sorted()' is the way to go. I want a 2 > key sort, Description and Date, but I think I can figure out how to do > that. There's a handy trick that you can use because the sorting algorithm is stable. First, sort on your secondary key. This will leave the data in the wrong order, but objects with the same primary key will be in the right order by secondary key relative to each other. Then sort on your primary key. Because the sorting algorithm is stable, it won't disturb the relative order of objects with the same primary key, giving you the sort that you want! So assuming you want your data sorted by date, and then by description within the same date, it's just: records = sorted( sorted( set(records), key=operator.attrgetter("Description") ), key=operator.attrgetter("Date") ) -- Rhodri James *-* Kynesim Ltd From python at deborahswanson.net Mon Jan 9 17:33:12 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 14:33:12 -0800 Subject: Clickable hyperlinks In-Reply-To: <20170109075313.583d49e2@bigbox.christie.dr> Message-ID: <004601d26ac8$5cdbfc50$27b23dae@sambora> Tim Chase wrote, on January 09, 2017 5:53 AM > > On 2017-01-09 05:00, Deborah Swanson wrote: > > Code does in fact have the power to control what happens > > in the console. How do you think Linux does it on their terminals with > > clickable links? Granted, the code may have to specify parameters for > > a particular console, but I certainly wasn't asking for universal code > > that would work with any console. > > Only to a degree. Some consoles sniff the text sent to them > for particular URL-like patterns and linkify them for you. > All you have to do is print a URL that its pattern-matching > identifies: > > print("http://example.com") > > However, as Rhodri details, *this is a feature of the > terminal emulator*. I've got a smattering of terminal > emulators at my disposal and many don't auto-linkify (xterm, > rxvt, the base non-X terminal) while others do (Gnome > terminal). And in the ones where it doesn't work, it's > because it's the *terminal* that doesn't support linkifying, > so no amount of work on the application's part will make it linkify. Ok, here is the crux of this thread's communication problem. I didn't ask, or particularly care for all these lectures on the technology of terminal emulators. I asked how to code Python to make clickable links. Since all of you are quite obviously passionate about terminal emulators, I would suggest that you start your own thread. Except that it would have nothing to do with Python, and would be OT for this list. What you're doing instead is thread jacking, hijacking a thread to talk about a different topic. And in every list I've ever been on, thread jacking is considered to be very rude. And it's also confusing if the two topics are similar, which is exactly what happened here. > > The console is a dead thing, it has no mind or soul to choose > > anything. Surely an educated person would know that. > > Pretty much every quality system administrator I know uses > the terminal. Just about all of the best devs I know use the > terminal. Microsoft added Powershell because of demand. They > added Ubuntu/bash support because of demand. It allows for > powerful automation that would otherwise require writing > full-fledged scripts. There is nothing dead about it. > > I'm not sure where you get your baseless claim that "an > educated person would know that", since someone who had taken > the time to study the state of terminal use would see that is > far from dead. > > -tkc You are an ardently passionate and loyal bunch, aren't you. I wasn't using "dead" in the figurative sense of no longer in use, I was using it literally, as I clarified by saying "it has no mind or soul to choose anything". A console (or terminal) is not a living thing with a brain and consciousness, and its only capacity to choose anything is dictated by the conditionals programmed into it. I've never heard of a console (or terminal) that could choose to respond to some clicks and not to others, have you? No, consoles have no minds of their own to choose anything. This is what "an educated person would know" means in the sentence I wrote. It's not a good practice to twist other people's words to mean what you want them to mean, while ignoring what they meant by them. Though it is a convenient way to invent an argument where there is none, and that's exactly what's happened here. From ben.usenet at bsb.me.uk Mon Jan 9 18:11:18 2017 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Mon, 09 Jan 2017 23:11:18 +0000 Subject: Temporary variables in list comprehensions References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87vatnvnft.fsf@bsb.me.uk> Steven D'Aprano writes: > Suppose you have an expensive calculation that gets used two or more times in a > loop. The obvious way to avoid calculating it twice in an ordinary loop is with > a temporary variable: > > result = [] > for x in data: > tmp = expensive_calculation(x) > result.append((tmp, tmp+1)) > > > But what if you are using a list comprehension? Alas, list comps don't let you > have temporary variables, so you have to write this: > > > [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data] > > > Or do you? ... no, you don't! > > > [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > > > I can't decide whether that's an awesome trick or a horrible hack... Nor I (but then I'm not really a Pythonista). However I would use a lambda as the natural way to create a local binding: [(lambda tmp: (tmp, tmp+1))(expensive_calculation(x)) for x in data] -- Ben. From __peter__ at web.de Mon Jan 9 18:27:20 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 10 Jan 2017 00:27:20 +0100 Subject: Using namedtuples field names for column indices in a list of lists References: <003c01d26ac1$0a358e00$27b23dae@sambora> <43063cff-c970-3221-96bc-11b6d8638e9e@kynesim.co.uk> Message-ID: Rhodri James wrote: > On 09/01/17 21:40, Deborah Swanson wrote: >> Peter Otten wrote, on January 09, 2017 6:51 AM >>> >>> records = sorted( >>> set(records), >>> key=operator.attrgetter("Description") >>> ) >> >> Good, this is confirmation that 'sorted()' is the way to go. I want a 2 >> key sort, Description and Date, but I think I can figure out how to do >> that. > > There's a handy trick that you can use because the sorting algorithm is > stable. First, sort on your secondary key. This will leave the data in > the wrong order, but objects with the same primary key will be in the > right order by secondary key relative to each other. > > Then sort on your primary key. Because the sorting algorithm is stable, > it won't disturb the relative order of objects with the same primary > key, giving you the sort that you want! > > So assuming you want your data sorted by date, and then by description > within the same date, it's just: > > records = sorted( > sorted( > set(records), > key=operator.attrgetter("Description") > ), > key=operator.attrgetter("Date") > ) While stable sort is nice in this case you can just say key=operator.attrgetter("Description", "Date") Personally I'd only use sorted() once and then switch to the sort() method. From __peter__ at web.de Mon Jan 9 19:09:46 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 10 Jan 2017 01:09:46 +0100 Subject: Using namedtuples field names for column indices in a list of lists References: <003301d26a45$cc04a5b0$27b23dae@sambora> <20170109082143.6b0a019f@bigbox.christie.dr> <51c2cf13-b516-4ebf-8ca9-a915520adcab@googlegroups.com> <20170109110830.42cb734e@bigbox.christie.dr> <2ba0c533-b586-4865-a524-2ee0aaff14ad@googlegroups.com> Message-ID: breamoreboy at gmail.com wrote: > On Monday, January 9, 2017 at 5:34:12 PM UTC, Tim Chase wrote: >> On 2017-01-09 08:31, breamoreboy wrote: >> > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: >> > > I usually wrap the iterable in something like >> > > >> > > def pairwise(it): >> > > prev = next(it) >> > > for thing in it: >> > > yield prev, thing >> > > prev = thing >> > >> > Or from >> > https://docs.python.org/3/library/itertools.html#itertools-recipes:->> > >> > def pairwise(iterable): >> > "s -> (s0,s1), (s1,s2), (s2, s3), ..." >> > a, b = tee(iterable) >> > next(b, None) >> > return zip(a, b) >> > >> > This and many other recipes are available in the more-itertools >> > module which is on pypi. >> >> Ah, helpful to not have to do it from scratch each time. Also, I see >> several others that I've coded up from scratch (particularly the >> partition() and first_true() functions). >> >> I usually want to make sure it's tailored for my use cases. The above >> pairwise() is my most common use case, but I occasionally want N-wise >> pairing > > def ntuplewise(iterable, n=2): > args = tee(iterable, n) > loops = n - 1 > while loops: > for _ in range(loops): > next(args[loops], None) > loops -= 1 > return zip(*args) > >> >> s -> (s0,s1,?sN), (s1,s2,?S{N+1}), (s2,s3,?s{N+2}), ? >> >> or to pad them out so either the leader/follower gets *all* of the >> values, with subsequent values being a padding value: >> >> # lst = [s0, s1, s2] >> (s0,s1), (s1, s2), (s2, PADDING) > > Use zip_longest instead of zip in the example code above. > >> # or >> (PADDING, s0), (s0, s1), (s1, s2) > > Haven't a clue off of the top of my head and I'm too darn tired to think > about it :) In both cases modify the iterable before feeding it to ntuplewise(): >>> PADDING = None >>> N = 3 >>> items = range(5) >>> list(ntuplewise(chain(repeat(PADDING, N-1), items), N)) [(None, None, 0), (None, 0, 1), (0, 1, 2), (1, 2, 3), (2, 3, 4)] >>> list(ntuplewise(chain(items, repeat(PADDING, N-1)), N)) [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, None), (4, None, None)] From larry.martell at gmail.com Mon Jan 9 19:10:59 2017 From: larry.martell at gmail.com (Larry Martell) Date: Mon, 9 Jan 2017 19:10:59 -0500 Subject: Clickable hyperlinks In-Reply-To: <004601d26ac8$5cdbfc50$27b23dae@sambora> References: <20170109075313.583d49e2@bigbox.christie.dr> <004601d26ac8$5cdbfc50$27b23dae@sambora> Message-ID: On Mon, Jan 9, 2017 at 5:33 PM, Deborah Swanson wrote: > Ok, here is the crux of this thread's communication problem. I didn't > ask, or particularly care for all these lectures on the technology of > terminal emulators. I asked how to code Python to make clickable links. > > Since all of you are quite obviously passionate about terminal > emulators, I would suggest that you start your own thread. Except that > it would have nothing to do with Python, and would be OT for this list. > What you're doing instead is thread jacking, hijacking a thread to talk > about a different topic. And in every list I've ever been on, thread > jacking is considered to be very rude. And it's also confusing if the > two topics are similar, which is exactly what happened here. Once you start a thread and put it out in the ether, it takes on a life of its own, and cannot be controlled by anyone. That is true more on this list then any other of the 100's I've been on. And it's why I love it here. From python at deborahswanson.net Mon Jan 9 19:54:06 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 16:54:06 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: Message-ID: <006901d26adc$0bc07f80$27b23dae@sambora> Peter Otten wrote, on January 09, 2017 3:27 PM > > While stable sort is nice in this case you can just say > > key=operator.attrgetter("Description", "Date") > > Personally I'd only use sorted() once and then switch to the > sort() method. This works perfectly, thank you. As I read the docs, the main (only?) difference between sorted() and .sort() is that .sort() sorts the list in place. Since I won't change the order of the records again after the sort, I'm using records.sort(key=operator.attrgetter("Description", "Date")) once, which also works perfectly. So both sorted() and sort() can be used to sort namedtuples. Good to know! From python at deborahswanson.net Mon Jan 9 20:02:57 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 17:02:57 -0800 Subject: Clickable hyperlinks In-Reply-To: Message-ID: <007101d26add$48752880$27b23dae@sambora> Larry Martell wrote, on January 09, 2017 4:11 PM > > On Mon, Jan 9, 2017 at 5:33 PM, Deborah Swanson > wrote: > > Ok, here is the crux of this thread's communication problem. I didn't > > ask, or particularly care for all these lectures on the technology of > > terminal emulators. I asked how to code Python to make clickable links. > > > > Since all of you are quite obviously passionate about terminal > > emulators, I would suggest that you start your own thread. Except that > > it would have nothing to do with Python, and would be OT for this > > list. What you're doing instead is thread jacking, hijacking a thread > > to talk about a different topic. And in every list I've ever been on, > > thread jacking is considered to be very rude. And it's also confusing > > if the two topics are similar, which is exactly what happened here. > > Once you start a thread and put it out in the ether, it takes > on a life of its own, and cannot be controlled by anyone. > That is true more on this list then any other of the 100's > I've been on. And it's why I love it here. Fair enough. I only suggested that they could have started their own thread, but mainly just to point out that they would have been off-topic if they did. I didn't demand that they do so, I just wanted them to think about it. I also prefer a loosely controlled list, and a freewheeling list is ideal, but it only works well if people exercise basic politeness and common sense. From torriem at gmail.com Mon Jan 9 20:33:28 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 9 Jan 2017 18:33:28 -0700 Subject: Clickable hyperlinks In-Reply-To: <007101d26add$48752880$27b23dae@sambora> References: <007101d26add$48752880$27b23dae@sambora> Message-ID: <16bf1e18-1ab8-5f5f-1942-727949476a70@gmail.com> On 01/09/2017 06:02 PM, Deborah Swanson wrote: > Fair enough. I only suggested that they could have started their own > thread, but mainly just to point out that they would have been off-topic > if they did. I didn't demand that they do so, I just wanted them to > think about it. I don't see how it would be any more off-topic than your original query. Which is to say, talking about Python's use of the console (beit a Win32 Console API window, a terminal emulator, or a PyCharm window) is on-topic. > I also prefer a loosely controlled list, and a freewheeling list is > ideal, but it only works well if people exercise basic politeness and > common sense. I agree. Be aware that your own posts in this thread have bordered on inflammatory on occasion. Certainly your remark about "any educated person would know that" could be taken in a very negative way. I can't remember everything I read in this thread, but I'm hard-pressed to find examples where people were not polite to you, even though you pressed on with certain lines of comment, especially when it was pointed out that your question came from the wrong premise and idea of what the "console" is. I don't believe that someone pointing out flaws in your understanding is inherently rude, though I think you took it as such. From joel.goldstick at gmail.com Mon Jan 9 20:42:39 2017 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 9 Jan 2017 20:42:39 -0500 Subject: Clickable hyperlinks In-Reply-To: <16bf1e18-1ab8-5f5f-1942-727949476a70@gmail.com> References: <007101d26add$48752880$27b23dae@sambora> <16bf1e18-1ab8-5f5f-1942-727949476a70@gmail.com> Message-ID: On Mon, Jan 9, 2017 at 8:33 PM, Michael Torrie wrote: > On 01/09/2017 06:02 PM, Deborah Swanson wrote: >> Fair enough. I only suggested that they could have started their own >> thread, but mainly just to point out that they would have been off-topic >> if they did. I didn't demand that they do so, I just wanted them to >> think about it. > > I don't see how it would be any more off-topic than your original query. > Which is to say, talking about Python's use of the console (beit a Win32 > Console API window, a terminal emulator, or a PyCharm window) is on-topic. > >> I also prefer a loosely controlled list, and a freewheeling list is >> ideal, but it only works well if people exercise basic politeness and >> common sense. > > I agree. Be aware that your own posts in this thread have bordered on > inflammatory on occasion. Certainly your remark about "any educated > person would know that" could be taken in a very negative way. I can't > remember everything I read in this thread, but I'm hard-pressed to find > examples where people were not polite to you, even though you pressed on > with certain lines of comment, especially when it was pointed out that > your question came from the wrong premise and idea of what the "console" > is. I don't believe that someone pointing out flaws in your > understanding is inherently rude, though I think you took it as such. > > > -- > https://mail.python.org/mailman/listinfo/python-list Well put Michael -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From python at lucidity.plus.com Mon Jan 9 20:47:00 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 10 Jan 2017 01:47:00 +0000 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <006901d26adc$0bc07f80$27b23dae@sambora> References: <006901d26adc$0bc07f80$27b23dae@sambora> Message-ID: On 10/01/17 00:54, Deborah Swanson wrote: > Since I won't change the order of the records again after the sort, I'm > using > > records.sort(key=operator.attrgetter("Description", "Date")) > > once, which also works perfectly. > > So both sorted() and sort() can be used to sort namedtuples. Good to > know! As people keep saying, the object you have called 'records' is a *list* of namedtuple objects. It is not a namedtuple. IIRC, you create it using a list comprehension which creates the records. A list comprehension always creates a list. The sorted() function and the list.sort() method can be used to sort a list containing any objects - it's just a case of telling them how to obtain the key values to compare (which, in the case of simple attribute access which the namedtuple objects allow, "operator.attrgetter()" will do that). This is why sorting the list works for you. You could sort objects of different types - but you might need to supply a function instead of operator.attrgetter() which looks at the type of each object and returns something that's obtained differently for each type (but which the sort function can compare). When you say 'Foo = namedtuple("Foo", "spam ham")', you are creating a "factory" which is able to generate "Foo" objects for you. When you say "x = Foo(1, 2)" you are using the factory to create an object for you which has its "spam" and "ham" attributes set to the values 1 and 2 respectively. When you say "records = [Foo(x, y) for x, y in some_iterable()]", you are creating a list of such objects. This is the thing you are then sorting. Does that make sense? Regards, E. From python.list at tim.thechases.com Mon Jan 9 21:37:15 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 9 Jan 2017 20:37:15 -0600 Subject: Temporary variables in list comprehensions In-Reply-To: <87tw98ylw6.fsf@nightsong.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <871swc1pez.fsf@nightsong.com> <20170109061531.71e3c795@bigbox.christie.dr> <87tw98ylw6.fsf@nightsong.com> Message-ID: <20170109203715.1fa875a5@bigbox.christie.dr> On 2017-01-09 13:16, Paul Rubin wrote: > Tim Chase writes: > >> result = [(tmp, tmp+1) for tmp in map(expensive_calculation, > >> data)] > > > > As charmingly expressive as map() is, the wildly different > > behavior in py3 (it's a generator that evaluates lazily) vs py2 > > (it consumes the entire iterable in one go) leads me to avoid it > > in general, > > Well, there's itertools.imap which maps lazily in py2. Yes, but it's one of those things that I have to remember to distinguish which one I use based on whether I'm working in py2 or py3. Meanwhile, using a generator expression is readable (my #1 reason for using Python is its readability) and works across 2 and 3 without changes or thinking about it further. So that's what I tend to use. -tkc From no.email at nospam.invalid Mon Jan 9 21:57:21 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Mon, 09 Jan 2017 18:57:21 -0800 Subject: Temporary variables in list comprehensions References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <87vatnvnft.fsf@bsb.me.uk> Message-ID: <87d1fvzkoe.fsf@nightsong.com> Ben Bacarisse writes: > [(lambda tmp: (tmp, tmp+1))(expensive_calculation(x)) for x in data] Nice. The Haskell "let" expression is implemented as syntax sugar for that, I believe. From python at deborahswanson.net Mon Jan 9 22:02:59 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 19:02:59 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: Message-ID: <007c01d26aee$0d76b5d0$27b23dae@sambora> Erik wrote, on January 09, 2017 5:47 PM > As people keep saying, the object you have called 'records' > is a *list* > of namedtuple objects. It is not a namedtuple. > > IIRC, you create it using a list comprehension which creates the > records. A list comprehension always creates a list. Well no. The list is created with: records.extend(Record._make(row) for row in rows) I'm new to both namedtuples and list comprehensions, so I'm not exactly sure if this statement is a list comprehension. It looks like it could be. In any case I recreated records in IDLE and got >>> type(records) So it's a class, derived from list? (Not sure what the 'list' means.) 'records' is in fact a class, it has an fget method and data members that I've used. And it behaves like a list sometimes, but many times not. The only reason I've hedged away from advice to treat records as a list for sorting until I tried it for myself, was because of an awful lot of strange behavior I've seen, while trying to do the same things with namedtuples as I routinely do with scalars and lists. This is all new, and until now, unexplored territory for me. And I generally avoid saying I'm sure or outright agreeing with something unless I really do know it. > The sorted() function and the list.sort() method can be used > to sort a > list containing any objects - it's just a case of telling them how to > obtain the key values to compare (which, in the case of > simple attribute > access which the namedtuple objects allow, > "operator.attrgetter()" will > do that). This is why sorting the list works for you. > > You could sort objects of different types - but you might > need to supply > a function instead of operator.attrgetter() which looks at > the type of > each object and returns something that's obtained differently > for each > type (but which the sort function can compare). > > > > > When you say 'Foo = namedtuple("Foo", "spam ham")', you are > creating a > "factory" which is able to generate "Foo" objects for you. > > When you say "x = Foo(1, 2)" you are using the factory to create an > object for you which has its "spam" and "ham" attributes set to the > values 1 and 2 respectively. > > When you say "records = [Foo(x, y) for x, y in some_iterable()]", you > are creating a list of such objects. This is the thing you > are then sorting. > > > > Does that make sense? > > Regards, E. Perfect sense. And now that I've confirmed in code that both sorted() and .sort() behave as hoped for with namedtuples, I couldn't be happier. ;) The only thing I don't think you have 100% correct is your assertion that records is a list. And I'm really not sure now that records.extend(Record._make(row) for row in rows) is a list comprehension. That's the last statement in the creation of 'records', and immediately after that statement executes, the type function says the resulting 'records' is a class, probably derived from list, but it's not a straight up list. 'records' is enough different that you can't assume across the board that namedtuples created this way are equivalent to a list. You do run into problems if you assume it behaves like a list, or even like standard tuples, because it doesn't always. Believe me, when I first started working with namedtuples, I got plenty snarled up debugging code that was written assuming list behavior to know that a namedtuple of namedtuples is not exactly a list. Or even exactly like a list. But that's just a quibble. The important thing in this context is that both .sort() and sorted() treat it like a list and DTRT. And that's very nice. ;) Deborah From python.list at tim.thechases.com Mon Jan 9 22:10:40 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 9 Jan 2017 21:10:40 -0600 Subject: Clickable hyperlinks In-Reply-To: <004601d26ac8$5cdbfc50$27b23dae@sambora> References: <20170109075313.583d49e2@bigbox.christie.dr> <004601d26ac8$5cdbfc50$27b23dae@sambora> Message-ID: <20170109211040.3b09940e@bigbox.christie.dr> On 2017-01-09 14:33, Deborah Swanson wrote: > Ok, here is the crux of this thread's communication problem. I > didn't ask, or particularly care for all these lectures on the > technology of terminal emulators. I asked how to code Python to > make clickable links. The crux of the problem is that words mean things. Specific words have *specific* meanings. When you say "make a clickable hyperlink in a console", those words each have meanings. Part of helping people is coming to a common understanding. In this case, the common meaning is "I want to send some magical sequence to a terminal emulator to make it treat the item as a clickable link." To which anybody with experience in the matter answers exactly like happened on the list: that depends on your terminal emulator but is entirely the domain of that emulator, and only within the context of knowing which terminal emulator you're talking about, can any sort of meaningful solution be given (if any). You used words that carry a particular technical meaning, but didn't use them in a way that made much sense without taking the effort to clarify. To round it out, you brought up your lengthy experience in the computer field: > I've only been online since 1992 (offline computing since 1972) This usually suggests that you have been around enough to know that these terms have precise meanings, and that you are wielding them in the way others in the field use them. So the roundabout discussion finally sussed out that you meant "I want to send information to the PyCharm output area which I'm calling a console (but is not necessarily an actual terminal emulator as the technical word 'console' would usually be understood) and that output area happens to know how to interpret some sequences as things that are clickable. How can I do that?" Which then became "well, I don't really even need to make clickable links, I just want to open some target in a web browser." And until folks on the list could understand your *intent* despite the words you were using, it's a frustrating experience for all involved. So because of imprecise language, even if you didn't ask or care about the terminology, *it matters if you want a viable answer*. -tkc From python at mrabarnett.plus.com Mon Jan 9 22:37:01 2017 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 10 Jan 2017 03:37:01 +0000 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <007c01d26aee$0d76b5d0$27b23dae@sambora> References: <007c01d26aee$0d76b5d0$27b23dae@sambora> Message-ID: On 2017-01-10 03:02, Deborah Swanson wrote: > Erik wrote, on January 09, 2017 5:47 PM >> As people keep saying, the object you have called 'records' >> is a *list* >> of namedtuple objects. It is not a namedtuple. >> >> IIRC, you create it using a list comprehension which creates the >> records. A list comprehension always creates a list. > > Well no. The list is created with: > > records.extend(Record._make(row) for row in rows) > > I'm new to both namedtuples and list comprehensions, so I'm not exactly > sure if this statement is a list comprehension. It looks like it could > be. > This is a list comprehension: [Record._make(row) for row in rows] and this is a generator expression: (Record._make(row) for row in rows) It needs the outer parentheses. The .extend method will accept any iterable, including list comprehensions: records.extend([Record._make(row) for row in rows]) and generator expressions: records.extend((Record._make(row) for row in rows)) In the latter case, the generator expression is the only argument of the .extend method, and Python lets us drop the pair of parentheses: records.extend(Record._make(row) for row in rows) If there were another argument, it would be ambiguous and Python would complain. > In any case I recreated records in IDLE and got > >>>> type(records) > > > So it's a class, derived from list? (Not sure what the 'list' means.) > 'records' is in fact a class, it has an fget method and data members > that I've used. And it behaves like a list sometimes, but many times > not. > Its type is 'list', so it's an instance of a list, i.e. it's a list! > The only reason I've hedged away from advice to treat records as a list > for sorting until I tried it for myself, was because of an awful lot of > strange behavior I've seen, while trying to do the same things with > namedtuples as I routinely do with scalars and lists. This is all new, > and until now, unexplored territory for me. And I generally avoid saying > I'm sure or outright agreeing with something unless I really do know it. > >> The sorted() function and the list.sort() method can be used >> to sort a >> list containing any objects - it's just a case of telling them how to >> obtain the key values to compare (which, in the case of >> simple attribute >> access which the namedtuple objects allow, >> "operator.attrgetter()" will >> do that). This is why sorting the list works for you. >> >> You could sort objects of different types - but you might >> need to supply >> a function instead of operator.attrgetter() which looks at >> the type of >> each object and returns something that's obtained differently >> for each >> type (but which the sort function can compare). >> >> >> >> >> When you say 'Foo = namedtuple("Foo", "spam ham")', you are >> creating a >> "factory" which is able to generate "Foo" objects for you. >> >> When you say "x = Foo(1, 2)" you are using the factory to create an >> object for you which has its "spam" and "ham" attributes set to the >> values 1 and 2 respectively. >> >> When you say "records = [Foo(x, y) for x, y in some_iterable()]", you >> are creating a list of such objects. This is the thing you >> are then sorting. >> >> >> >> Does that make sense? >> >> Regards, E. > > Perfect sense. And now that I've confirmed in code that both sorted() > and > .sort() behave as hoped for with namedtuples, I couldn't be happier. ;) > > The only thing I don't think you have 100% correct is your assertion > that records is a list. And I'm really not sure now that > > records.extend(Record._make(row) for row in rows) > > is a list comprehension. > > That's the last statement in the creation of 'records', and immediately > after that statement executes, the type function says the resulting > 'records' is a class, probably derived from list, but it's not a > straight up list. > > 'records' is enough different that you can't assume across the board > that namedtuples created this way are equivalent to a list. You do run > into problems if you assume it behaves like a list, or even like > standard tuples, because it doesn't always. Believe me, when I first > started working with namedtuples, I got plenty snarled up debugging code > that was written assuming list behavior to know that a namedtuple of > namedtuples is not exactly a list. Or even exactly like a list. > > But that's just a quibble. The important thing in this context is that > both .sort() and sorted() treat it like a list and DTRT. And that's > very nice. ;) > The list class has the .sort method, which sorts in-place. The 'sorted' function is a simple function that takes an iterable, iterates over it to build a list, sorts that list in-place, and then returns the list. The oft-stated rule is that not every 2- or 3-line function needs to be a built-in, but 'sorted' is one of those cases where it's just nice to have it, a case of "practicality beats purity". From ethan at stoneleaf.us Mon Jan 9 23:00:47 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 09 Jan 2017 20:00:47 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <007c01d26aee$0d76b5d0$27b23dae@sambora> References: <007c01d26aee$0d76b5d0$27b23dae@sambora> Message-ID: <58745C6F.3020403@stoneleaf.us> On 01/09/2017 07:02 PM, Deborah Swanson wrote: > Erik wrote, on January 09, 2017 5:47 PM >> As people keep saying, the object you have called 'records' >> is a *list* >> of namedtuple objects. It is not a namedtuple. >> >> IIRC, you create it using a list comprehension which creates the >> records. A list comprehension always creates a list. > > Well no. The list is created with: > > records.extend(Record._make(row) for row in rows) > > I'm new to both namedtuples and list comprehensions, so I'm not exactly > sure if this statement is a list comprehension. It looks like it could > be. In any case I recreated records in IDLE and got > >--> type(records) > > > So it's a class, derived from list? (Not sure what the 'list' means.) On the one hand, Deborah, I applaud your perseverance. On the other, it seems as if you trying to run before you can walk. I know tutorials can be boring, but you really should go through one so you have a basic understanding of the fundamentals. Working in the REPL (the python console), we can see: Python 3.4.0 (default, Apr 11 2014, 13:05:18) ... --> type(list) --> --> type(list()) --> type([1, 2, 3]) So the `list` type is 'type', and the type of list instances is 'class list'. Your records variable is an instance of a list filled with instances of a namedtuple, 'Record'. One cannot sort a namedtuple, but one can sort a list of namedtuples -- which is what you are doing. As I said earlier, I admire your persistence -- but take some time and learn the basic vocabulary as that will make it much easier for you to ask questions, and for us to give you meaningful answers. -- ~Ethan~ From python at lucidity.plus.com Mon Jan 9 23:05:42 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 10 Jan 2017 04:05:42 +0000 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <007c01d26aee$0d76b5d0$27b23dae@sambora> References: <007c01d26aee$0d76b5d0$27b23dae@sambora> Message-ID: <21ae7283-b017-9181-7370-eb9b4678fe57@lucidity.plus.com> On 10/01/17 03:02, Deborah Swanson wrote: > Erik wrote, on January 09, 2017 5:47 PM >> IIRC, you create it using a list comprehension which creates the >> records. A list comprehension always creates a list. > > Well no. The list is created with: > > records.extend(Record._make(row) for row in rows) No, the list is _extended_ by that code. The list is _created_ with a line that will say something like "records = []" or "records = list()" (or "records = "). It's nice to see you agree that it's a list though. Oh, hold on ... ;) > I'm not exactly > sure if this statement is a list comprehension. No, it's not. I was remembering an old message where someone suggested using the _make() method and that was expressed as a list comprehension. What you have there is a call to list.extend() passing a _generator_ comprehension as its only parameter (which in this case you can consider to be equivalent to a list comprehension as all of the data are generated greedily). You see that I said "list.extend()". That's because 'records' is a list. >>>> type(records) > Yes, it's an instance of the list class. A list object. A list. >>> type(list()) >>> type([]) >>> class foo: pass ... >>> type(foo()) >>> ... type() will tell you what class your object is an instance of. "" tells you that your object is a list. > And it behaves like a list sometimes, but many times > not. I think that's impossible. I'm 100% sure it's a list. Please give an example of when 'records' does not behave like a list. > The only thing I don't think you have 100% correct is your assertion > that records is a list. It's a list. > But that's just a quibble. The important thing in this context is that > both .sort() and sorted() treat it like a list and DTRT. That's because it's a list :) E. From python at deborahswanson.net Mon Jan 9 23:27:38 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 20:27:38 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: Message-ID: <008801d26af9$e0968ed0$27b23dae@sambora> MRAB wrote, on January 09, 2017 7:37 PM > > On 2017-01-10 03:02, Deborah Swanson wrote: > > Erik wrote, on January 09, 2017 5:47 PM > >> As people keep saying, the object you have called 'records' is a > >> *list* of namedtuple objects. It is not a namedtuple. > >> > >> IIRC, you create it using a list comprehension which creates the > >> records. A list comprehension always creates a list. > > > > Well no. The list is created with: > > > > records.extend(Record._make(row) for row in rows) > > > > I'm new to both namedtuples and list comprehensions, so I'm not > > exactly sure if this statement is a list comprehension. It > looks like > > it could be. > > > This is a list comprehension: > > [Record._make(row) for row in rows] > > and this is a generator expression: > > (Record._make(row) for row in rows) > > It needs the outer parentheses. > > The .extend method will accept any iterable, including list > comprehensions: > > records.extend([Record._make(row) for row in rows]) > > and generator expressions: > > records.extend((Record._make(row) for row in rows)) > > In the latter case, the generator expression is the only > argument of the > .extend method, and Python lets us drop the pair of parentheses: > > records.extend(Record._make(row) for row in rows) > > If there were another argument, it would be ambiguous and > Python would > complain. Appreciate your explanation of why this statement looks like a list comprehension, but it isn't one. > > In any case I recreated records in IDLE and got > > > >>>> type(records) > > > > > > So it's a class, derived from list? (Not sure what the > 'list' means.) >>> [1,2,3] [1, 2, 3] >>> type(_) So it is a list, despite not being made by a list comprehension and despite its non-listlike behaviors. Guess I've never looked at the type of a list before, probably because lists are so obvious by looking at them. > > 'records' is in fact a class, it has an fget method and data members > > that I've used. And it behaves like a list sometimes, but many times not. > > > Its type is 'list', so it's an instance of a list, i.e. it's a list! As testified by IDLE above! ;) A list of namedtuples may be an instance of a list, but it doesn't always behave like a list of lists. For example, if you want to modify an element of a record in records, you can't just say 'record.Location = Tulsa' like you can say 'record[Location] = Tulsa' because each record is very much like a tuple, and tuples are immutable. You have to use the _replace function: record._replace(Location) = Tulsa This is very unlike a list of lists. Only the outer data structure is a list, and inside it's all namedtuples. So it's not a list of lists, it's a list of namedtuples. But .sort and sorted() DTRT, and that's valuable. > > The only reason I've hedged away from advice to treat records as a > > list for sorting until I tried it for myself, was because of an awful > > lot of strange behavior I've seen, while trying to do the same things > > with namedtuples as I routinely do with scalars and lists. This is all > > new, and until now, unexplored territory for me. And I generally avoid > > saying I'm sure or outright agreeing with something unless I really do > > know it. > > > >> The sorted() function and the list.sort() method can be used to sort > >> a list containing any objects - it's just a case of telling them how > >> to obtain the key values to compare (which, in the case of > >> simple attribute access which the namedtuple objects allow, > >> "operator.attrgetter()" will > >> do that). This is why sorting the list works for you. > >> > >> You could sort objects of different types - but you might need to > >> supply a function instead of operator.attrgetter() which looks at > >> the type of > >> each object and returns something that's obtained differently > >> for each > >> type (but which the sort function can compare). > >> > >> > >> > >> > >> When you say 'Foo = namedtuple("Foo", "spam ham")', you > are creating > >> a "factory" which is able to generate "Foo" objects for you. > >> > >> When you say "x = Foo(1, 2)" you are using the factory to > create an > >> object for you which has its "spam" and "ham" attributes > set to the > >> values 1 and 2 respectively. > >> > >> When you say "records = [Foo(x, y) for x, y in > some_iterable()]", you > >> are creating a list of such objects. This is the thing you > are then > >> sorting. > >> > >> > >> > >> Does that make sense? > >> > >> Regards, E. > > > > Perfect sense. And now that I've confirmed in code that both sorted() and > > .sort() behave as hoped for with namedtuples, I couldn't be happier. > > ;) > > > > The only thing I don't think you have 100% correct is your assertion > > that records is a list. And I'm really not sure now that > > > > records.extend(Record._make(row) for row in rows) > > > > is a list comprehension. > > > > That's the last statement in the creation of 'records', and > > immediately after that statement executes, the type function says the > > resulting 'records' is a class, probably derived from list, but it's > > not a straight up list. > > > > 'records' is enough different that you can't assume across the board > > that namedtuples created this way are equivalent to a list. You do run > > into problems if you assume it behaves like a list, or even like > > standard tuples, because it doesn't always. Believe me, when I first > > started working with namedtuples, I got plenty snarled up debugging > > code that was written assuming list behavior to know that a namedtuple > > of namedtuples is not exactly a list. Or even exactly like a list. > > > > But that's just a quibble. The important thing in this context is that > > both .sort() and sorted() treat it like a list and DTRT. > And that's very nice. ;) > > > The list class has the .sort method, which sorts in-place. The 'sorted' > function is a simple function that takes an iterable, iterates over it > to build a list, sorts that list in-place, and then returns the list. > > The oft-stated rule is that not every 2- or 3-line function needs to be > a built-in, but 'sorted' is one of those cases where it's just nice to > have it, a case of "practicality beats purity". > > -- > https://mail.python.org/mailman/listinfo/python-list > From steve+comp.lang.python at pearwood.info Mon Jan 9 23:43:27 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 10 Jan 2017 15:43:27 +1100 Subject: Enum with only a single member Message-ID: <58746671$0$11123$c3e8da3@news.astraweb.com> Is it silly to create an enumeration with only a single member? That is, a singleton enum? from enum import Enum class Unique(Enum): FOO = auto() The reason I ask is that I have two functions that take an enum argument. The first takes one of three enums: class MarxBros(Enum): GROUCHO = 1 CHICO = 2 HARPO = 3 and the second takes *either* one of those three, or a fourth distinct value. So in my two functions, I have something like this: def spam(arg): if isinstance(arg, MarxBros): ... def ham(arg): if isinstance(arg, MarxBros) or arg is Unique.FOO: ... Good, bad or indifferent? -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at deborahswanson.net Mon Jan 9 23:51:44 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 20:51:44 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <58745C6F.3020403@stoneleaf.us> Message-ID: <008901d26afd$3e632200$27b23dae@sambora> Ethan Furman wrote, on January 09, 2017 8:01 PM > > On 01/09/2017 07:02 PM, Deborah Swanson wrote: > > Erik wrote, on January 09, 2017 5:47 PM > > >> As people keep saying, the object you have called 'records' is a > >> *list* of namedtuple objects. It is not a namedtuple. > >> > >> IIRC, you create it using a list comprehension which creates the > >> records. A list comprehension always creates a list. > > > > Well no. The list is created with: > > > > records.extend(Record._make(row) for row in rows) > > > > I'm new to both namedtuples and list comprehensions, so I'm not > > exactly sure if this statement is a list comprehension. It > looks like > > it could be. In any case I recreated records in IDLE and got > > > >--> type(records) > > > > > > So it's a class, derived from list? (Not sure what the > 'list' means.) > > On the one hand, Deborah, I applaud your perseverance. On > the other, it seems as if you trying to run before you can > walk. I know tutorials can be boring, but you really should > go through one so you have a basic understanding of the fundamentals. I actually have had a solid foundation of study in 2 terms of MIT's introductory Python courses. But they can't cover everything in that short a time. > Working in the REPL (the python console), we can see: > > Python 3.4.0 (default, Apr 11 2014, 13:05:18) > ... > --> type(list) > > --> > --> type(list()) > > --> type([1, 2, 3]) > > > So the `list` type is 'type', and the type of list instances > is 'class list'. I just saw that while replying to MRAB. 'records' has type list, but it's only the outer data structure that's a list. Inside, all the records are namedtuples, and I think that accounts for behaviors that are unlike a list of lists. (And the reason I was reluctant to accept that it could be sorted until I tried it for myself.) The method calls I was able to use were from the namedtuples, not the list of namedtuples. > Your records variable is an instance of a list filled with > instances of a namedtuple, 'Record'. One cannot sort a > namedtuple, but one can sort a list of namedtuples -- which > is what you are doing. Yes, I think we've got that straight now. > As I said earlier, I admire your persistence -- but take some > time and learn the basic vocabulary as that will make it much > easier for you to ask questions, and for us to give you > meaningful answers. > > -- > ~Ethan~ As I mentioned, I have completed MIT's 2 introductory Python courses with final grades of 98% and 97%. What tutorials do you think would significantly add to that introduction? It's true that I didn't spend much time in the forums while I was taking those courses, so this is the first time I've talked with people about Python this intensively. But I'm a good learner and I'm picking up a lot of it pretty quickly. People on the list also talk and comprehend differently than people in the MIT courses did, so I have to become accustomed to this as well. And the only place to learn that is right here. From ethan at stoneleaf.us Tue Jan 10 00:01:25 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 09 Jan 2017 21:01:25 -0800 Subject: Enum with only a single member In-Reply-To: <58746671$0$11123$c3e8da3@news.astraweb.com> References: <58746671$0$11123$c3e8da3@news.astraweb.com> Message-ID: <58746AA5.7000107@stoneleaf.us> On 01/09/2017 08:43 PM, Steven D'Aprano wrote: > Is it silly to create an enumeration with only a single member? That is, a > singleton enum? > > from enum import Enum > > class Unique(Enum): > FOO = auto() > > > The reason I ask is that I have two functions that take an enum argument. The > first takes one of three enums: > > class MarxBros(Enum): > GROUCHO = 1 > CHICO = 2 > HARPO = 3 > > and the second takes *either* one of those three, or a fourth distinct value. > So in my two functions, I have something like this: > > > def spam(arg): > if isinstance(arg, MarxBros): > ... > > > def ham(arg): > if isinstance(arg, MarxBros) or arg is Unique.FOO: > ... > > > Good, bad or indifferent? Well, the basic purpose of Enum is give meaningful names to otherwise magic constants, so while it certainly looks a bit unusual to have only one item in the enumeration, if that group naturally has only one item then sure, go for it! -- ~Ethan~ From python at deborahswanson.net Tue Jan 10 00:03:04 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 21:03:04 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <21ae7283-b017-9181-7370-eb9b4678fe57@lucidity.plus.com> Message-ID: <008a01d26afe$d3fa7650$27b23dae@sambora> Erik wrote, on January 09, 2017 8:06 PM > > On 10/01/17 03:02, Deborah Swanson wrote: > > Erik wrote, on January 09, 2017 5:47 PM > >> IIRC, you create it using a list comprehension which creates the > >> records. A list comprehension always creates a list. > > > > Well no. The list is created with: > > > > records.extend(Record._make(row) for row in rows) > > No, the list is _extended_ by that code. The list is _created_ with a > line that will say something like "records = []" or "records > = list()" > (or "records = "). The list was created with this code: infile = open("E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv") rows = csv.reader(infile) fieldnames = next(rows) Record = namedtuple("Record", fieldnames) records = [Record._make(fieldnames)] records.extend(Record._make(row) for row in rows) I just pulled out the .extend statement to show you because that's what looks like a list comprehension, but turns out not to be one. We still get a list though, on that we agree. ;) > It's nice to see you agree that it's a list though. Oh, hold on ... ;) > > > I'm not exactly > > sure if this statement is a list comprehension. > > No, it's not. I was remembering an old message where someone > suggested > using the _make() method and that was expressed as a list > comprehension. > > What you have there is a call to list.extend() passing a _generator_ > comprehension as its only parameter (which in this case you > can consider > to be equivalent to a list comprehension as all of the data are > generated greedily). You see that I said "list.extend()". > That's because > 'records' is a list. > > >>>> type(records) > > > > Yes, it's an instance of the list class. A list object. A list. > > >>> type(list()) > > >>> type([]) > > >>> class foo: pass > ... > >>> type(foo()) > > >>> > > ... type() will tell you what class your object is an instance of. > "" tells you that your object is a list. > > > And it behaves like a list sometimes, but many times > > not. > > I think that's impossible. I'm 100% sure it's a list. Please give an > example of when 'records' does not behave like a list. I gave an example in one of my last two replies to other people. The thing is that it's a list, but it's not a list of lists. It's a list of namedtuples, and the non-listlike behaviors appear when I'm directly working with the namedtuples. > > The only thing I don't think you have 100% correct is your assertion > > that records is a list. > > It's a list. I agree, now. > > But that's just a quibble. The important thing in this context is that > > both .sort() and sorted() treat it like a list and DTRT. > > That's because it's a list :) > > E. It is! A list of namedtuples that is, not a list of lists. sorted() and .sort work because they only interact with the outer data structure, which is a list. From steve+comp.lang.python at pearwood.info Tue Jan 10 00:18:09 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 10 Jan 2017 16:18:09 +1100 Subject: Is enum iteration order guaranteed? Message-ID: <58746e93$0$1587$c3e8da3$5496439d@news.astraweb.com> The docs say that enums can be iterated over, but it isn't clear to me whether they are iterated over in definition order or value order. If I have: class MarxBros(Enum): GROUCHO = 999 CHICO = 5 HARPO = 11 ZEPPO = auto() GUMMO = -1 GROUCHO, CHICO, HARPO, ZEPPO, GUMMO = list(MarxBros) is that guaranteed to do the right thing? i.e. GROUCHO is MarxBros.GROUCHO, and similarly for the other members. On that related note, how would people feel about a method that injects enums into the given namespace? MarxBros._inject_(globals()) although maybe from MarxBros import * would be more familiar syntax :-) -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From jussi.piitulainen at helsinki.fi Tue Jan 10 00:41:22 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Tue, 10 Jan 2017 07:41:22 +0200 Subject: Enum with only a single member References: <58746671$0$11123$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano writes: > Is it silly to create an enumeration with only a single member? That > is, a singleton enum? > > from enum import Enum > > class Unique(Enum): > FOO = auto() > > > The reason I ask is that I have two functions that take an enum > argument. The first takes one of three enums: > > class MarxBros(Enum): > GROUCHO = 1 > CHICO = 2 > HARPO = 3 > > and the second takes *either* one of those three, or a fourth distinct > value. So in my two functions, I have something like this: > > > def spam(arg): > if isinstance(arg, MarxBros): > ... > > > def ham(arg): > if isinstance(arg, MarxBros) or arg is Unique.FOO: > ... > > > Good, bad or indifferent? With a class of its own, the single value can be identified by its class uniformly with the other relevant values. def ham(arg): if insinstance(arg, (MarxBros, Unique)): ... Seems good to me. From ethan at stoneleaf.us Tue Jan 10 00:55:10 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 09 Jan 2017 21:55:10 -0800 Subject: Is enum iteration order guaranteed? In-Reply-To: <58746e93$0$1587$c3e8da3$5496439d@news.astraweb.com> References: <58746e93$0$1587$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5874773E.5040407@stoneleaf.us> On 01/09/2017 09:18 PM, Steven D'Aprano wrote: > The docs say that enums can be iterated over, but it isn't clear to me whether > they are iterated over in definition order or value order. > > If I have: > > class MarxBros(Enum): > GROUCHO = 999 > CHICO = 5 > HARPO = 11 > ZEPPO = auto() > GUMMO = -1 > > GROUCHO, CHICO, HARPO, ZEPPO, GUMMO = list(MarxBros) In Python 3 it is always definition order. In Python 2 (using the enum34 [1] backport), the order is either by value if possible, or alphabetical if not -- unless you use the _order_ attribute to set it: class MarkBros(Enum): _order_ = 'GROUCHO CHICO HARGPO ZEPPO GUMMO' GROUCHO = 999 ... > On that related note, how would people feel about a method that injects enums > into the given namespace? > > MarxBros._inject_(globals()) > > although maybe > > from MarxBros import * > > would be more familiar syntax :-) Or you can do globals().update(MarxBros.__members__). -- ~Ethan~ From ethan at stoneleaf.us Tue Jan 10 01:05:52 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 09 Jan 2017 22:05:52 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <008901d26afd$3e632200$27b23dae@sambora> References: <008901d26afd$3e632200$27b23dae@sambora> Message-ID: <587479C0.7050401@stoneleaf.us> On 01/09/2017 08:51 PM, Deborah Swanson wrote: > Ethan Furman wrote, on January 09, 2017 8:01 PM >> As I said earlier, I admire your persistence -- but take some >> time and learn the basic vocabulary as that will make it much >> easier for you to ask questions, and for us to give you >> meaningful answers. > > As I mentioned, I have completed MIT's 2 introductory Python courses > with final grades of 98% and 97%. What tutorials do you think would > significantly add to that introduction? The Python version of "Think like a computer scientist" is good. Otherwise, ask the list for recommendations. I'm not suggesting more advanced topics, but rather basic topics such as how the REPL works, how to tell what objects you have, how to find the methods those objects have, etc. > It's true that I didn't spend much time in the forums while I was taking > those courses, so this is the first time I've talked with people about > Python this intensively. But I'm a good learner and I'm picking up a lot > of it pretty quickly. People on the list also talk and comprehend > differently than people in the MIT courses did, so I have to become > accustomed to this as well. And the only place to learn that is right > here. Indeed. The issue I (and others) see, though, is more along the lines of basic understanding: you seemed to think that a list of lists should act the same as a list of tuples, even though lists and tuples are not the same thing. It's like expecting a basket of oranges to behave like a basket of avocados. ;) As you say, you're making good progress. -- ~Ethan~ From steve+comp.lang.python at pearwood.info Tue Jan 10 01:22:34 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 10 Jan 2017 17:22:34 +1100 Subject: Is enum iteration order guaranteed? References: <58746e93$0$1587$c3e8da3$5496439d@news.astraweb.com> <5874773E.5040407@stoneleaf.us> Message-ID: <58747dac$0$22142$c3e8da3$5496439d@news.astraweb.com> On Tuesday 10 January 2017 16:55, Ethan Furman wrote: > On 01/09/2017 09:18 PM, Steven D'Aprano wrote: > >> The docs say that enums can be iterated over, but it isn't clear to me >> whether they are iterated over in definition order or value order. >> >> If I have: >> >> class MarxBros(Enum): >> GROUCHO = 999 >> CHICO = 5 >> HARPO = 11 >> ZEPPO = auto() >> GUMMO = -1 >> >> GROUCHO, CHICO, HARPO, ZEPPO, GUMMO = list(MarxBros) > > In Python 3 it is always definition order. I only care about Python 3 for this. Did I miss something in the docs, or should this be added? [...] >> On that related note, how would people feel about a method that injects >> enums into the given namespace? [...] > Or you can do globals().update(MarxBros.__members__). Sweet! -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From python at deborahswanson.net Tue Jan 10 02:14:16 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Mon, 9 Jan 2017 23:14:16 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <587479C0.7050401@stoneleaf.us> Message-ID: <009501d26b11$281a3d30$27b23dae@sambora> Ethan Furman wrote, on January 09, 2017 10:06 PM > > On 01/09/2017 08:51 PM, Deborah Swanson wrote: > > Ethan Furman wrote, on January 09, 2017 8:01 PM > > >> As I said earlier, I admire your persistence -- but take some time > >> and learn the basic vocabulary as that will make it much easier for > >> you to ask questions, and for us to give you meaningful answers. > > > > As I mentioned, I have completed MIT's 2 introductory Python courses > > with final grades of 98% and 97%. What tutorials do you think would > > significantly add to that introduction? > > The Python version of "Think like a computer scientist" is > good. Otherwise, ask the list for recommendations. I'm not > suggesting more advanced topics, but rather basic topics such > as how the REPL works, how to tell what objects you have, how > to find the methods those objects have, etc. I'm working on all of that, and I've been taking physics, chem, computer science and theoretical mathematics (straight 4.0s my last 2 years, graduated summa cum laude), but it's been a couple of decades since my last brick building university course. It's coming back fast, but there's a lot I'm still pulling out of cobwebs. I basically know the tools to use for the things you mention, but in the online coursework I've done in the past year, we didn't need to use them because they told us everything. So that's another thing I'm doing catch up on. Really shouldn't take too long, it's not that complicated or difficult. > > It's true that I didn't spend much time in the forums while I was > > taking those courses, so this is the first time I've talked with > > people about Python this intensively. But I'm a good learner and I'm > > picking up a lot of it pretty quickly. People on the list also talk > > and comprehend differently than people in the MIT courses did, so I > > have to become accustomed to this as well. And the only place to learn > > that is right here. > > Indeed. > > The issue I (and others) see, though, is more along the lines > of basic understanding: you seemed to think that a list of > lists should act the same as a list of tuples, even though > lists and tuples are not the same thing. It's like expecting > a basket of oranges to behave like a basket of avocados. ;) > > As you say, you're making good progress. > > -- > ~Ethan~ I'm sorry, I didn't think a list of namedtuples would be like a list of lists when I wrote to Erik just a bit ago today, but I sort of did when I first started trying to use them a couple days ago. And to the extent I was pretty sure they weren't the same, I still didn't know in what ways they were different. So when people ask me questions about why I did things the way I did, I try to explain that I didn't know certain things then, but I know them now. I'm guessing that you (and those who see things like you do) might not be used to working with quick learners who make mistakes at first but catch up with them real fast, or you're very judgemental about people who make mistakes, period. I certainly don't care if you want to judge me, you're entitled to your opinion. One of my MIT professors was always screwing up little things, even things he knew inside out. Some people are like that, and I think I might be one of them, although I'm really not as bad as he was. So I guess you should just do your thing and I'll do mine. I don't promise that I'll ever get to the point where I never set a foot wrong. I know some people can do that, and I hope someday I will. But then I remember the MIT professor, who I really learned a lot from, despite all his flub-ups, and that I might be a little bit like him. Takes all kinds, and I think in the end what will count is the quality of my finished work (which has always been excellent), and not the messy process to get there. It shocked me the first time someone on this list jumped down my throat for a momentary lapse on my part, as if I was a total idiot and knew nothing, but I'm sort of getting used to that too. It must be nice to be so perfect, I guess. Deborah From storchaka at gmail.com Tue Jan 10 02:19:14 2017 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 10 Jan 2017 09:19:14 +0200 Subject: Temporary variables in list comprehensions In-Reply-To: <871swc1pez.fsf@nightsong.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <871swc1pez.fsf@nightsong.com> Message-ID: On 09.01.17 12:46, Paul Rubin wrote: > Serhiy Storchaka writes: >> gen = (expensive_calculation(x) for x in data) >> result = [(tmp, tmp + 1) for tmp in gen] > > result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] Yes, of course, but only in the case of one-argument function expensive_calculation(). If this is just an expensive expression or need to pass multiple arguments to an expensive function, a generator expression could look simpler than a lambda or a local function. From no.email at nospam.invalid Tue Jan 10 03:33:32 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 10 Jan 2017 00:33:32 -0800 Subject: Enum with only a single member References: <58746671$0$11123$c3e8da3@news.astraweb.com> Message-ID: <878tqjz543.fsf@nightsong.com> Steven D'Aprano writes: > Is it silly to create an enumeration with only a single member? No. > That is, a singleton enum? Why stop there? You can make empty ones too. (Zerotons?) > The reason I ask is that I have two functions that take an enum > argument. Sounds like a good reason. > def spam(arg): > if isinstance(arg, MarxBros): > def ham(arg): > if isinstance(arg, MarxBros) or arg is Unique.FOO: > Good, bad or indifferent? Ummmm, that's ugly, but the alternatives are worse unless there's more branches, and maybe even then. From ethan at stoneleaf.us Tue Jan 10 03:35:25 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 10 Jan 2017 00:35:25 -0800 Subject: Is enum iteration order guaranteed? In-Reply-To: <58747dac$0$22142$c3e8da3$5496439d@news.astraweb.com> References: <58746e93$0$1587$c3e8da3$5496439d@news.astraweb.com> <5874773E.5040407@stoneleaf.us> <58747dac$0$22142$c3e8da3$5496439d@news.astraweb.com> Message-ID: <58749CCD.6080606@stoneleaf.us> On 01/09/2017 10:22 PM, Steven D'Aprano wrote: > On Tuesday 10 January 2017 16:55, Ethan Furman wrote: >> On 01/09/2017 09:18 PM, Steven D'Aprano wrote: >> >>> The docs say that enums can be iterated over, but it isn't clear to me >>> whether they are iterated over in definition order or value order. >>> >>> If I have: >>> >>> class MarxBros(Enum): >>> GROUCHO = 999 >>> CHICO = 5 >>> HARPO = 11 >>> ZEPPO = auto() >>> GUMMO = -1 >>> >>> GROUCHO, CHICO, HARPO, ZEPPO, GUMMO = list(MarxBros) >> >> In Python 3 it is always definition order. > > I only care about Python 3 for this. > > Did I miss something in the docs, or should this be added? In https://docs.python.org/3/library/enum.html#creating-an-enum, fifth paragraph down: Enumerations support iteration, in definition order ... -- ~Ethan~ From rosuav at gmail.com Tue Jan 10 03:37:08 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 10 Jan 2017 19:37:08 +1100 Subject: Enum with only a single member In-Reply-To: <878tqjz543.fsf@nightsong.com> References: <58746671$0$11123$c3e8da3@news.astraweb.com> <878tqjz543.fsf@nightsong.com> Message-ID: On Tue, Jan 10, 2017 at 7:33 PM, Paul Rubin wrote: >> That is, a singleton enum? > > Why stop there? You can make empty ones too. (Zerotons?) Sure you *can*, but I can't think of any time they'd be useful. Can you give an example? ChrisA From ethan at stoneleaf.us Tue Jan 10 03:49:36 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 10 Jan 2017 00:49:36 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <009501d26b11$281a3d30$27b23dae@sambora> References: <009501d26b11$281a3d30$27b23dae@sambora> Message-ID: <5874A020.7070001@stoneleaf.us> On 01/09/2017 11:14 PM, Deborah Swanson wrote: > So I guess you should just do your thing and I'll do mine. As you say. > Takes all kinds, and I think in the end what will count is the quality of my > finished work (which has always been excellent), and not the messy > process to get there. Agreed. -- ~Ethan~ From ethan at stoneleaf.us Tue Jan 10 03:51:45 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 10 Jan 2017 00:51:45 -0800 Subject: Enum with only a single member In-Reply-To: References: <58746671$0$11123$c3e8da3@news.astraweb.com> <878tqjz543.fsf@nightsong.com> Message-ID: <5874A0A1.3010707@stoneleaf.us> On 01/10/2017 12:37 AM, Chris Angelico wrote: > On Tue, Jan 10, 2017 at 7:33 PM, Paul Rubin wrote: >>> That is, a singleton enum? >> >> Why stop there? You can make empty ones too. (Zerotons?) > > Sure you *can*, but I can't think of any time they'd be useful. Can > you give an example? Sure. Any time you have some base behaviour you want shared amongst multiple Enum classes. ;) -- ~Ethan~ From jeanmichel at sequans.com Tue Jan 10 04:47:01 2017 From: jeanmichel at sequans.com (jmp) Date: Tue, 10 Jan 2017 10:47:01 +0100 Subject: Enum with only a single member In-Reply-To: <58746671$0$11123$c3e8da3@news.astraweb.com> References: <58746671$0$11123$c3e8da3@news.astraweb.com> Message-ID: On 01/10/2017 05:43 AM, Steven D'Aprano wrote: > Is it silly to create an enumeration with only a single member? That is, a > singleton enum? Don't think so, for the same reason that lists with one element make sense. > def ham(arg): > if isinstance(arg, MarxBros) or arg is Unique.FOO: > ... > > > Good, bad or indifferent? > Though I'd write def ham(arg): if arg in MarxBros or arg in Unique: ... JM From __peter__ at web.de Tue Jan 10 04:52:26 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 10 Jan 2017 10:52:26 +0100 Subject: Enum with only a single member References: <58746671$0$11123$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > Is it silly to create an enumeration with only a single member? That is, a > singleton enum? > > from enum import Enum > > class Unique(Enum): > FOO = auto() > > > The reason I ask is that I have two functions that take an enum argument. > The first takes one of three enums: > > class MarxBros(Enum): > GROUCHO = 1 > CHICO = 2 > HARPO = 3 > > and the second takes *either* one of those three, or a fourth distinct > value. So in my two functions, I have something like this: > > > def spam(arg): > if isinstance(arg, MarxBros): > ... > > > def ham(arg): > if isinstance(arg, MarxBros) or arg is Unique.FOO: > ... > > > Good, bad or indifferent? I don't see a problem. As it looks like you cannot subclass Enum subclasses alternative spellings might be isintance(args, (MarxBros, Unique)) or BOTH = set(MarxBros) | set(Unique) arg in MarxBros arg in BOTH From jeanmichel at sequans.com Tue Jan 10 05:35:31 2017 From: jeanmichel at sequans.com (jmp) Date: Tue, 10 Jan 2017 11:35:31 +0100 Subject: Temporary variables in list comprehensions In-Reply-To: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 01/09/2017 04:53 AM, Steven D'Aprano wrote: > Suppose you have an expensive calculation that gets used two or more times in a > loop. The obvious way to avoid calculating it twice in an ordinary loop is with > a temporary variable: > > result = [] > for x in data: > tmp = expensive_calculation(x) > result.append((tmp, tmp+1)) > > > But what if you are using a list comprehension? Alas, list comps don't let you > have temporary variables, so you have to write this: > > > [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data] > > > Or do you? ... no, you don't! > > > [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > > > I can't decide whether that's an awesome trick or a horrible hack... In any situation, the double list comprehension (also used to flatten lists) is very difficult to read. What about for x in (f(d) for d in data): result.append(x, x+1) There's a double for loop in the same line but the generator parenthesis help a lot. No lame tmp variable involved. JM From p.f.moore at gmail.com Tue Jan 10 09:32:31 2017 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 10 Jan 2017 06:32:31 -0800 (PST) Subject: Temporary variables in list comprehensions In-Reply-To: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> Message-ID: <520ce11d-5549-4b87-911d-15fa91851218@googlegroups.com> On Monday, 9 January 2017 03:53:37 UTC, Steven D'Aprano wrote: > Suppose you have an expensive calculation that gets used two or more times > in a loop. [...] > [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > > I can't decide whether that's an awesome trick or a horrible hack... My immediate reaction is that it depends on the constraints that made it important for you to use a comprehension rather than an explicit loop in the first place. For a toy example like this it's obviously a horrible hack, and you should simply make an explicit loop: result = [] for x in data: # Don't recalculate this value, as it's expensive! val = expensive_calculation(x) result.append((val, val+1)) In a real world case, maybe there's a good reason why you'd prefer to stick with a comprehension. In that case, you look at trade-offs like def intermediate_tuple(val): return val, val+1 [intermediate_tuple(x) for x in data] or [(lambda val: (val, val+1))(x) for x in data] or your version. All have their own unique uglinesses, as does the explicit loop. Personally my preferences would be the explicit loop, then the intermediate_tuple function, in that order. Paul From phd at phdru.name Tue Jan 10 09:59:45 2017 From: phd at phdru.name (Oleg Broytman) Date: Tue, 10 Jan 2017 15:59:45 +0100 Subject: Announcement: TLSv1.2 will become mandatory in the future for Python.org Sites In-Reply-To: <1B1B29CD-AA4E-47C0-89A8-2748D5A14FC3@python.org> References: <1B1B29CD-AA4E-47C0-89A8-2748D5A14FC3@python.org> Message-ID: <20170110145945.GA2925@phdru.name> On Tue, Jan 10, 2017 at 08:27:21AM -0500, Donald Stufft wrote: > python3 -c "import urllib.request,json; print(json.loads(urllib.request.urlopen('https://www.howsmyssl.com/a/check').read())['tls_version'])" Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.4/json/__init__.py", line 312, in loads s.__class__.__name__)) TypeError: the JSON object must be str, not 'bytes' Fix: $ python3 -c "import urllib.request,json; print(json.loads(urllib.request.urlopen('https://www.howsmyssl.com/a/check').read().decode('ascii'))['tls_version'])" Oleg. -- Oleg Broytman http://phdru.name/ phd at phdru.name Programmers don't die, they just GOSUB without RETURN. From donald at python.org Tue Jan 10 10:01:22 2017 From: donald at python.org (Donald Stufft) Date: Tue, 10 Jan 2017 10:01:22 -0500 Subject: Announcement: TLSv1.2 will become mandatory in the future for Python.org Sites In-Reply-To: <20170110145945.GA2925@phdru.name> References: <1B1B29CD-AA4E-47C0-89A8-2748D5A14FC3@python.org> <20170110145945.GA2925@phdru.name> Message-ID: <04A82852-13DF-422E-8A29-03C8E8261DCB@python.org> > On Jan 10, 2017, at 9:59 AM, Oleg Broytman wrote: > > On Tue, Jan 10, 2017 at 08:27:21AM -0500, Donald Stufft wrote: >> python3 -c "import urllib.request,json; print(json.loads(urllib.request.urlopen('https://www.howsmyssl.com/a/check').read())['tls_version'])" > > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python3.4/json/__init__.py", line 312, in loads > s.__class__.__name__)) > TypeError: the JSON object must be str, not ?bytes' > Huh, just tested, my original snippet works on Python 3.6 but fails on Python 3.5. From p.f.moore at gmail.com Tue Jan 10 10:47:00 2017 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 10 Jan 2017 07:47:00 -0800 (PST) Subject: The hardest problem in computer science... In-Reply-To: References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> <587099fa$0$1610$c3e8da3$5496439d@news.astraweb.com> <58713BF8.7080803@stoneleaf.us> Message-ID: On Saturday, 7 January 2017 19:14:43 UTC, Ethan Furman wrote: > Ya know, that looks an /awful/ lot like a collection! Maybe even an Enum? ;) > > -- 8< ------------------------------------------------------- > from aenum import Enum # note the 'a' before the 'enum' :) > > class Theme(Enum, init='v vr llc'): > DEFAULT = "? ", "?? ", "?? " > BOLD = "? ", "?? ", "?? " > ASCII = "| ", "|- ", "+- " > > def draw_tree(tree, theme=Theme.DEFAULT): > print(theme.v) > print(theme.vr) > print(theme.v) > print(theme.llc) > > draw_tree(None) I noted the "a" before enum :-) Is the implication that this form (a sort of combined namedtuple/enum) *isn't* possible with the stdlib enum? But rather that it's specific to your aenum module? I don't see any documentation for the "init" parameter in either version, so I'm a little puzzled. The capability seems neat, although (as is probably obvious) the way you declare it seems a little confusing to me. Paul From p.f.moore at gmail.com Tue Jan 10 11:06:35 2017 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 10 Jan 2017 08:06:35 -0800 (PST) Subject: The hardest problem in computer science... In-Reply-To: References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> <587099fa$0$1610$c3e8da3$5496439d@news.astraweb.com> <58713BF8.7080803@stoneleaf.us> Message-ID: <6cde77f0-255e-414f-9d53-8e016a884074@googlegroups.com> On Tuesday, 10 January 2017 15:47:20 UTC, Paul Moore wrote: > On Saturday, 7 January 2017 19:14:43 UTC, Ethan Furman wrote: > > Ya know, that looks an /awful/ lot like a collection! Maybe even an Enum? ;) > > > > -- 8< ------------------------------------------------------- > > from aenum import Enum # note the 'a' before the 'enum' :) > > > > class Theme(Enum, init='v vr llc'): > > DEFAULT = "? ", "?? ", "?? " > > BOLD = "? ", "?? ", "?? " > > ASCII = "| ", "|- ", "+- " > > > > def draw_tree(tree, theme=Theme.DEFAULT): > > print(theme.v) > > print(theme.vr) > > print(theme.v) > > print(theme.llc) > > > > draw_tree(None) > > I noted the "a" before enum :-) > > Is the implication that this form (a sort of combined namedtuple/enum) *isn't* possible with the stdlib enum? But rather that it's specific to your aenum module? I don't see any documentation for the "init" parameter in either version, so I'm a little puzzled. > > The capability seems neat, although (as is probably obvious) the way you declare it seems a little confusing to me. > > Paul After a bit more digging I found https://docs.python.org/3.6/library/enum.html#planet which looks like a stdlib-supported way of doing the same sort of thing. I assume init is an aenum convenience argument to do the same? Anyway, it's a neat feature - I'd not really looked beyond the surface of the new enum module, looks like I missed some good stuff :-) Paul From irmen.NOSPAM at xs4all.nl Tue Jan 10 12:24:10 2017 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Tue, 10 Jan 2017 18:24:10 +0100 Subject: Announcement: TLSv1.2 will become mandatory in the future for Python.org Sites In-Reply-To: References: <1B1B29CD-AA4E-47C0-89A8-2748D5A14FC3@python.org> <20170110145945.GA2925@phdru.name> <04A82852-13DF-422E-8A29-03C8E8261DCB@python.org> Message-ID: <587518b7$0$21416$e4fe514c@news.xs4all.nl> On 10-1-2017 16:01, Donald Stufft wrote: >> TypeError: the JSON object must be str, not ?bytes' > Huh, just tested, my original snippet works on Python 3.6 but fails on Python 3.5. My guess is that is due to an improvement in 3.6 mentioned here: https://docs.python.org/3/whatsnew/3.6.html#json Irmen From jladasky at itu.edu Tue Jan 10 15:10:19 2017 From: jladasky at itu.edu (jladasky at itu.edu) Date: Tue, 10 Jan 2017 12:10:19 -0800 (PST) Subject: Help with this code In-Reply-To: References: <84a38553-c6bb-4c6d-9247-eb22a69fe897@googlegroups.com> Message-ID: On Monday, January 9, 2017 at 12:50:11 PM UTC-8, Joaquin Alzola wrote: > >> elements. For example, if we have a list_a=["a","b","c","d"] and > >> list_b=["a","b"] I want to obtain a new list_c containing elements that > >> match between these lists (a and b here), > > >Perhaps this might work: > > >>>> list(set(list_a).intersection(set(list_b))) > >['a', 'b'] > > >>> list_a={"a","b","c","d"} > >>> list_b={"a","b"} > > sorted(list_a & list_b) > ['a', 'b'] You might want to use some other names for the objects you defined: "list_a" and "list_b" are not lists, they are sets. Of course it doesn't matter if it's two throw-away lines typed into the interpreter, but in code that you share with someone else (especially a beginner like the OP), it could be confusing. From zxpatric at gmail.com Tue Jan 10 17:57:51 2017 From: zxpatric at gmail.com (Patrick Zhou) Date: Tue, 10 Jan 2017 14:57:51 -0800 (PST) Subject: Work between multiple processes In-Reply-To: <2985338719@f38.n261.z1.binkp.net> References: <2510477911@f38.n261.z1.binkp.net> <2985338719@f38.n261.z1.binkp.net> Message-ID: Hi Irmen, I have successfully got it to work with both side as python but so far having trouble with pyrolite.jar which is downloaded from https://mvnrepository.com/artifact/net.razorvine/pyrolite/4.4 Having simple codes as: public static void main(String[] args) { // System.out.println("Hello world"); try { String uri = "PYRO:obj_12a65b09f95f4ee9bec5958f819ced45 at localhost:64404"; PyroURI pyURI = new PyroURI(uri); PyroProxy pyroP = new PyroProxy(pyURI); Object result = pyroP.call("getDst"); String message = (String) result; System.out.println("result message=" + message); pyroP.close(); } catch (IOException e1) { System.out.println(e1.getMessage()); } catch (PickleException e2) { System.out.println(e2.getMessage()); } catch (PyroException e3) { System.out.println(e3.getMessage()); } } which "getDst" works on Java but hangs on handshake() in the call function. Any thought? Thanks. -P From steve+comp.lang.python at pearwood.info Tue Jan 10 20:10:13 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 11 Jan 2017 12:10:13 +1100 Subject: Temporary variables in list comprehensions References: <58730938$0$1597$c3e8da3$5496439d@news.astraweb.com> <718d3926-e673-3d7b-e55c-8df5db719957@rece.vub.ac.be> Message-ID: <587585f7$0$1621$c3e8da3$5496439d@news.astraweb.com> On Tuesday 10 January 2017 00:12, Antoon Pardon wrote: > Op 09-01-17 om 04:53 schreef Steven D'Aprano: >> Suppose you have an expensive calculation that gets used two or more times >> in a loop. The obvious way to avoid calculating it twice in an ordinary loop >> is with a temporary variable: [...] >> I can't decide whether that's an awesome trick or a horrible hack... > > Maybe this in an occasion to use your recipe. > > http://code.activestate.com/recipes/580625-collection-pipeline-in-python/ > > result = data | Map(expensive_calculation) | Map(lambda tmp: (tmp, tmp + 1)) > | List > Indeed :-) -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From gerald.britton at gmail.com Tue Jan 10 20:26:38 2017 From: gerald.britton at gmail.com (Gerald Britton) Date: Tue, 10 Jan 2017 20:26:38 -0500 Subject: Question about abstract base classes and abstract properties -- Python 2.7 In-Reply-To: References: Message-ID: I was rereading the 2.7 docs about abstract base classes the other day. I found this line in the usage section of the abc.abstractproperty function: "This defines a read-only property; you can also define a read-write abstract property using the ?long? form of property declaration:" along with an example. so I copied the example and put in a little surrounding code: from abc import ABCMeta, abstractproperty class C(object): __metaclass__ = ABCMeta def getx(self): pass def setx(self, value): pass x = abstractproperty(getx, setx) class D(C): @property def x(self):self._x d = D() print(d) When I ran this, I expected an exception, since I defined a read/write abstract property but only implemented the read operation. However, the example runs fine. That is the class D can be instantiated without error. Of course I cannot set the property since I didn't implement that part. Now, If I don't implement the property at all, I can't instantiate the class. I get: "TypeError: Can't instantiate abstract class D with abstract methods x" which is what I would expect. What I don't understand is why I don't get a similar error when I implement the read operation for the property but not the write operation. If this actually doesn't work (catching the non-implementation at instantiation time), then would it be appropriate to amend the documentation? To me at least the doc implies that it *will* raise on the missing write property implementation (otherwise, what's the point of the example?) Or, am I just not grokking it properly? From ethan at stoneleaf.us Tue Jan 10 21:07:34 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 10 Jan 2017 18:07:34 -0800 Subject: The hardest problem in computer science... In-Reply-To: <6cde77f0-255e-414f-9d53-8e016a884074@googlegroups.com> References: <586f95ac$0$1589$c3e8da3$5496439d@news.astraweb.com> <587099fa$0$1610$c3e8da3$5496439d@news.astraweb.com> <58713BF8.7080803@stoneleaf.us> <6cde77f0-255e-414f-9d53-8e016a884074@googlegroups.com> Message-ID: <58759366.5050207@stoneleaf.us> On 01/10/2017 08:06 AM, Paul Moore wrote: > On Tuesday, 10 January 2017 15:47:20 UTC, Paul Moore wrote: >> On Saturday, 7 January 2017 19:14:43 UTC, Ethan Furman wrote: >>> Ya know, that looks an /awful/ lot like a collection! Maybe even an Enum? ;) >>> >>> -- 8< ------------------------------------------------------- >>> from aenum import Enum # note the 'a' before the 'enum' :) >>> >>> class Theme(Enum, init='v vr llc'): >>> DEFAULT = "? ", "?? ", "?? " >>> BOLD = "? ", "?? ", "?? " >>> ASCII = "| ", "|- ", "+- " >>> >>> def draw_tree(tree, theme=Theme.DEFAULT): >>> print(theme.v) >>> print(theme.vr) >>> print(theme.v) >>> print(theme.llc) >>> >>> draw_tree(None) >> >> I noted the "a" before enum :-) >> >> Is the implication that this form (a sort of combined namedtuple/enum) >> *isn't* possible with the stdlib enum? But rather that it's specific >> to your aenum module? It's possible with the stdlib version, and a built-in convenience with aenum. >> I don't see any documentation for the "init" >> parameter in either version, so I'm a little puzzled. It's in the docs for aenum, which you will get with the source download. I need to figure out how to get them included in the wheels. :( >> The capability seems neat, although (as is probably obvious) the way you >> declare it seems a little confusing to me. Yes, it is possible to pass keyword arguments in a class header, although (as far as I know) very few make use of this. > Anyway, it's a neat feature - I'd not really looked beyond the surface of > the new enum module, looks like I missed some good stuff :-) Lot's of good stuff in the stdlib, but who has time to learn it all? :( -- ~Ethan~ From steve+comp.lang.python at pearwood.info Tue Jan 10 21:16:43 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 11 Jan 2017 13:16:43 +1100 Subject: Question about abstract base classes and abstract properties -- Python 2.7 References: Message-ID: <5875958c$0$1595$c3e8da3$5496439d@news.astraweb.com> On Wednesday 11 January 2017 12:26, Gerald Britton wrote: > I was rereading the 2.7 docs about abstract base classes the other day. I > found this line in the usage section of the abc.abstractproperty function: > > "This defines a read-only property; you can also define a read-write > abstract property using the ?long? form of property declaration:" > > along with an example. so I copied the example and put in a little > surrounding code: > > > from abc import ABCMeta, abstractproperty > > class C(object): > __metaclass__ = ABCMeta > def getx(self): pass > def setx(self, value): pass > x = abstractproperty(getx, setx) > > class D(C): > @property > def x(self):self._x > > d = D() > print(d) > > When I ran this, I expected an exception, since I defined a read/write > abstract property but only implemented the read operation. I expected the same thing. [...] > If this actually doesn't work (catching the non-implementation at > instantiation time), then would it be appropriate to amend the > documentation? To me at least the doc implies that it *will* raise on the > missing write property implementation (otherwise, what's the point of the > example?) Personally, I consider this a design bug: as designed, abstractproperty is broken. But I couldn't call it a functional bug: it looks like it is working as documented, as useless as that it. Note that in Python 3.3 and better, abstractproperty is deprecated: the ABC metaclass is smart enough to work with the regular property decorator: https://docs.python.org/3.6/library/abc.html#abc.abstractproperty > Or, am I just not grokking it properly? No, I agree with you. I think that a bug request to improve the documentation is appropriate. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From steve+comp.lang.python at pearwood.info Tue Jan 10 21:18:49 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 11 Jan 2017 13:18:49 +1100 Subject: Using namedtuples field names for column indices in a list of lists References: <587479C0.7050401@stoneleaf.us> <009501d26b11$281a3d30$27b23dae@sambora> Message-ID: <58759609$0$1595$c3e8da3$5496439d@news.astraweb.com> On Tuesday 10 January 2017 18:14, Deborah Swanson wrote: > I'm guessing that you (and those who > see things like you do) might not be used to working with quick learners > who make mistakes at first but catch up with them real fast, or you're > very judgemental about people who make mistakes, period. I certainly > don't care if you want to judge me, you're entitled to your opinion. Be fair. We're not mind-readers, and we're trying to help you, not attack you. It is true that we're often extremely pedantic. Sometimes annoyingly so, but on the other hand precision is important, especially in technical fields. There's a *huge* difference between a MTA and a MUA, even though they differ only by one letter and both are related to email. One of the techs I work with has the same habit of correcting me, and when I'm invariably forced to agree that he is technical correct, he replies "technical correctness is the best kind of correctness". Annoying as it is to be on the receiving end, he is right: at least half the time I learn something from his pedantry. (The other half of the time, its a difference that makes no difference.) What are we supposed to do when somebody asks a question based on an obvious mistake? Assume that they're a quick learner who has probably already worked out their mistake and doesn't need an answer? That would certainly make our life easier: we can just ignore everybody's questions. Sometimes people make errors of terminology that doesn't affect the semantics of what they're asking: "I have an array [1, 2, 3, 4] and I want to ..." It's very likely that they have a list and they're merely using a term they're familiar with from another language, rather than the array module. But what are we supposed to make of mistakes that *do* affect the semantics of the question: "I have a dict of ints {1, 2, 3, 4} and want to sort the values by key so that I get [4, 2, 3, 1], how do I do that?" How can we answer that without correcting their misuse of terminology and asking for clarification of what data structure they *actually* have? We get questions like this very frequently. How would you answer it? Or: "I have a list l = namedtuple('l', 'field1 field2') and can't extract fields by index, l[0] doesn't work..." Of course it doesn't work. How would you respond to that if you were in our place? - ignore the question because the poster is ever so smart and will have worked it out by now? - point out that l is not a list, or even a tuple, and of course l[0] doesn't work because l is actually a class? Its easy to criticise us for answering the questions you ask instead of the questions you intended, but we're not mind-readers. We don't know what you're thinking, we only know what you communicate to us. Telling us off for answering your questions is hardly likely to encourage us to answer them in the future. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From bc at freeuk.com Wed Jan 11 08:27:38 2017 From: bc at freeuk.com (BartC) Date: Wed, 11 Jan 2017 13:27:38 +0000 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <58759609$0$1595$c3e8da3$5496439d@news.astraweb.com> References: <587479C0.7050401@stoneleaf.us> <009501d26b11$281a3d30$27b23dae@sambora> <58759609$0$1595$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11/01/2017 02:18, Steven D'Aprano wrote: > On Tuesday 10 January 2017 18:14, Deborah Swanson wrote: > >> I'm guessing that you (and those who >> see things like you do) might not be used to working with quick learners >> who make mistakes at first but catch up with them real fast, or you're >> very judgemental about people who make mistakes, period. I certainly >> don't care if you want to judge me, you're entitled to your opinion. > > Be fair. We're not mind-readers, and we're trying to help you, not attack you. > > It is true that we're often extremely pedantic. Sometimes annoyingly so, but on > the other hand precision is important, especially in technical fields. There's > a *huge* difference between a MTA and a MUA, even though they differ only by > one letter and both are related to email. There's a bigger difference between USB and USA! -- Bartc From pengyu.ut at gmail.com Wed Jan 11 10:51:19 2017 From: pengyu.ut at gmail.com (Peng Yu) Date: Wed, 11 Jan 2017 09:51:19 -0600 Subject: Is there an peekable similar to peekable but in additional allowing one to put some data to it? Message-ID: Hi, peekable from more-itertools only allow peeking an iterator. But sometimes, one may want to take a look at an element, manipulate it, then put it back to the iterator. Is there a class in python that can help do this? https://pypi.python.org/pypi/more-itertools/ -- Regards, Peng From rustompmody at gmail.com Wed Jan 11 11:50:33 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Wed, 11 Jan 2017 08:50:33 -0800 (PST) Subject: reactiveX vs Async Message-ID: There is the recent flurry around the new async additions to python Also, addressing the same problem area, there is the slightly older reactiveX system that seems to have bindings for at least 15 mainstream languages? including python: http://reactivex.io/ Has someone been able to put these two in context? To relate one to the other? From rustompmody at gmail.com Wed Jan 11 11:53:40 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Wed, 11 Jan 2017 08:53:40 -0800 (PST) Subject: reactiveX vs Async In-Reply-To: References: Message-ID: On Wednesday, January 11, 2017 at 10:21:02 PM UTC+5:30, Rustom Mody wrote: > There is the recent flurry around the new async additions to python I meant to add: ?? which I dont pretend to understand?? From ian.g.kelly at gmail.com Wed Jan 11 12:07:35 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 11 Jan 2017 10:07:35 -0700 Subject: Is there an peekable similar to peekable but in additional allowing one to put some data to it? In-Reply-To: References: Message-ID: On Wed, Jan 11, 2017 at 8:51 AM, Peng Yu wrote: > Hi, peekable from more-itertools only allow peeking an iterator. But > sometimes, one may want to take a look at an element, manipulate it, > then put it back to the iterator. Is there a class in python that can > help do this? Not that I'm aware of, but maybe this recipe will help. from collections import deque class PushIterator: def __init__(self, iterable, push=()): self._iter = iter(iterable) self._pushed = deque(push) self._stopped = False def push(self, item): if self._stopped: raise RuntimeError('Cannot push onto exhausted iterator') self._pushed.append(item) def __iter__(self): return self def __next__(self): if self._pushed: return self._pushed.pop() try: return next(self._iter) except StopIteration: self._stopped = True raise From ian.g.kelly at gmail.com Wed Jan 11 12:15:39 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 11 Jan 2017 10:15:39 -0700 Subject: Is there an peekable similar to peekable but in additional allowing one to put some data to it? In-Reply-To: References: Message-ID: On Wed, Jan 11, 2017 at 10:07 AM, Ian Kelly wrote: > On Wed, Jan 11, 2017 at 8:51 AM, Peng Yu wrote: >> Hi, peekable from more-itertools only allow peeking an iterator. But >> sometimes, one may want to take a look at an element, manipulate it, >> then put it back to the iterator. Is there a class in python that can >> help do this? > > Not that I'm aware of, but maybe this recipe will help. > > > from collections import deque > > class PushIterator: > > def __init__(self, iterable, push=()): > self._iter = iter(iterable) > self._pushed = deque(push) > self._stopped = False > > def push(self, item): > if self._stopped: > raise RuntimeError('Cannot push onto exhausted iterator') > self._pushed.append(item) > > def __iter__(self): > return self > > def __next__(self): > if self._pushed: > return self._pushed.pop() > try: > return next(self._iter) > except StopIteration: > self._stopped = True > raise And in case it's not obvious, adding a peek method to this is pretty simple: from collections import deque class PushIterator: def __init__(self, iterable, push=()): self._iter = iter(iterable) self._pushed = deque(push) self._stopped = False def peek(self): item = next(self) self.push(item) return item def push(self, item): if self._stopped: raise RuntimeError('Cannot push onto exhausted iterator') self._pushed.append(item) def __iter__(self): return self def __next__(self): if self._pushed: return self._pushed.pop() try: return next(self._iter) except StopIteration: self._stopped = True raise From falter_donald at bah.com Wed Jan 11 12:31:42 2017 From: falter_donald at bah.com (Falter, Donald [USA]) Date: Wed, 11 Jan 2017 17:31:42 +0000 Subject: Cannot import GDAL Message-ID: <93285AC0FA20BA47BE43FA54FCA5B321EDD9AE3B@ASHBDAG2M3.resource.ds.bah.com> I am new to Python and I am trying to utilize GDAL. I installed Python 3.6. The version I get when I activate IDLE is: MSC v.1900 32 bit (Intel)] on win32. In downloading the GDAL bindings the latest version I can find is release-1800-gdal-2-1-2-mapserver-7-0-2 so I installed the following: gdal-201-1800-core.msi and GDAL-2.1.2.win32-py3.4.msi. I added C:\Program Files\GDAL to my path and added the system variables GDAL_DATA = C:\Program Files\GDAL\gdal-data and\ GDAL_DRIVER_PATH = C:\Program Files\GDAL\gdalplugins When I open IDLE and type the following: from osgeo import gdal I get the following error >>> from osgeo import gdal Traceback (most recent call last): File "", line 1, in from osgeo import gdal ModuleNotFoundError: No module named 'osgeo' >>> I have been all over the internet looking for answer but have found none that work. Some real help here would be appreciated. Thanks, Don From irmen.NOSPAM at xs4all.nl Wed Jan 11 15:07:34 2017 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Wed, 11 Jan 2017 21:07:34 +0100 Subject: Work between multiple processes In-Reply-To: References: <2510477911@f38.n261.z1.binkp.net> <2985338719@f38.n261.z1.binkp.net> Message-ID: <58769083$0$21443$e4fe514c@news.xs4all.nl> On 10-1-2017 23:57, Patrick Zhou wrote: > Hi Irmen, > > I have successfully got it to work with both side as python but so far having trouble with pyrolite.jar which is downloaded from https://mvnrepository.com/artifact/net.razorvine/pyrolite/4.4 > > [...] > > which "getDst" works on Java but hangs on handshake() in the call function. > > Any thought? Thanks. -P > Yeah, don't use the oldest version of the library available, use the newest ;-) https://mvnrepository.com/artifact/net.razorvine/pyrolite/4.15 Irmen From irmen.NOSPAM at xs4all.nl Wed Jan 11 16:35:50 2017 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Wed, 11 Jan 2017 22:35:50 +0100 Subject: Cannot import GDAL In-Reply-To: References: <93285AC0FA20BA47BE43FA54FCA5B321EDD9AE3B@ASHBDAG2M3.resource.ds.bah.com> Message-ID: <5876a533$0$21466$e4fe514c@news.xs4all.nl> On 11-1-2017 18:31, Falter, Donald [USA] wrote: > I am new to Python and I am trying to utilize GDAL. I installed Python 3.6. > I installed the following: gdal-201-1800-core.msi and GDAL-2.1.2.win32-py3.4.msi. Those don't match. You'll have to use a MSI built for py3.6, one thinks. Either install and use Python 3.4 with this (rather than 3.6) or find the approprite installer for python 3.6. It seems that you can get one here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal Irmen From fabien.maussion at gmail.com Wed Jan 11 16:59:27 2017 From: fabien.maussion at gmail.com (Fabien) Date: Wed, 11 Jan 2017 22:59:27 +0100 Subject: Cannot import GDAL References: <93285AC0FA20BA47BE43FA54FCA5B321EDD9AE3B@ASHBDAG2M3.resource.ds.bah.com> Message-ID: On 01/11/2017 06:31 PM, Falter, Donald [USA] wrote: > I am new to Python and I am trying to utilize GDAL GDAL is notoriously difficult to install :-( I would strongly recommend to use conda (http://conda.pydata.org/miniconda.html) for managing your packages and environments, and then use the conda-forge channel (https://conda-forge.github.io/) to install gdal: conda install -c conda-forge gdal Good luck! Fabien From fabien.maussion at gmail.com Wed Jan 11 17:06:00 2017 From: fabien.maussion at gmail.com (Fabien) Date: Wed, 11 Jan 2017 23:06:00 +0100 Subject: Cannot import GDAL References: <93285AC0FA20BA47BE43FA54FCA5B321EDD9AE3B@ASHBDAG2M3.resource.ds.bah.com> Message-ID: On 01/11/2017 10:59 PM, Fabien wrote: > I would strongly recommend to use conda > (http://conda.pydata.org/miniconda.html) for managing your packages and > environments, and then use the conda-forge channel > (https://conda-forge.github.io/) to install gdal: > > conda install -c conda-forge gdal I forgot to mention that since py3.6 is quite recent, most packages still don't have binaries ready for it. Staying with py3.5 is a better idea for now. From python at deborahswanson.net Wed Jan 11 17:57:24 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Wed, 11 Jan 2017 14:57:24 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <58759609$0$1595$c3e8da3$5496439d@news.astraweb.com> Message-ID: <001f01d26c5e$13762080$27b23dae@sambora> Steven D'Aprano wrote, on January 10, 2017 6:19 PM > > On Tuesday 10 January 2017 18:14, Deborah Swanson wrote: > > > I'm guessing that you (and those who see things like you do) might > > not be used to working with quick learners who make mistakes at > > first but catch up with them real fast, or you're very judgemental > > about people who make mistakes, period. I certainly don't care if > > you want to judge me, you're entitled to your opinion. > > Be fair. We're not mind-readers, and we're trying to help you, not > attack you. You aren't, and I've never seen you as attacking me, but there's a few others here who have attacked me, and they weren't trying to help me either. But it's only a very few people who've harrassed me, and the person I was writing to above wasn't one of them. > It is true that we're often extremely pedantic. Sometimes annoyingly > so, but on the other hand precision is important, especially in > technical fields. > There's a *huge* difference between a MTA and a > MUA, even though they differ only by one letter and both are related > to email. > > One of the techs I work with has the same habit of correcting me, and > when I'm invariably forced to agree that he is technical correct, he > replies "technical correctness is the best kind of correctness". > Annoying as it is to be on the receiving end, he is right: at least > half the time I learn something from his pedantry. (The other half of > the time, its a difference that makes no difference.) I'm sorry you have to work with someone like that, he sounds perfectly awful. (But that doesn't give you license to do the same. You're better than that.) > What are we supposed to do when somebody asks a question based on an > obvious mistake? Assume that they're a quick learner who has probably > already worked out their mistake and doesn't need an answer? That > would certainly make our life easier: we can just ignore everybody's > questions. No, of course not. My advice to people who want to help is to not assume that you know what the question asker does and doesn't know, and just answer the questions without obsessing about what they know. If that's impossible because they have something so wrong that you don't know what they're asking, that would be a good time to point it out and give them a chance to correct it. > Sometimes people make errors of terminology that doesn't affect the > semantics of what they're asking: > > "I have an array [1, 2, 3, 4] and I want to ..." > > It's very likely that they have a list and they're merely using a term > they're familiar with from another language, rather than the array > module. > > But what are we supposed to make of mistakes that *do* affect the > semantics of the question: > > "I have a dict of ints {1, 2, 3, 4} and want to sort the values by > key so that I get [4, 2, 3, 1], how do I do that?" > > How can we answer that without correcting their misuse of terminology > and asking for clarification of what data structure they *actually* > have? > > We get questions like this very frequently. How would you answer it? Well, I'd tell them how to reverse sort a dictionary, and point out that what they've given isn't a dictionary because it doesn't have any keys, and on this occasion I'd just give them an example of what a dictionary looks like (as part of showing them how to reverse sort it) with its keys, including the curly braces, and see what they come back with. They pretty clearly mean a dictionary and not a list, since they said "dict", used curly braces, and said they want to sort the values by key" in the first clause of their sentence. So they're just a little confused and maybe absent-mindedly slipping back into the more familiar list notation and concepts, or they don't exactly know what a dictionary is. I wouldn't belabor the point at this time, unless they keep coming back with the same issues. That would be the time to belabor it, in my opinion. When some people are learning it's hard to keep all the new things firmly in mind and not confuse them with more familiar things they already know. But if they get it all straight in reasonably good time, that should be ok. > Or: > > "I have a list l = namedtuple('l', 'field1 field2') and can't > extract fields by index, l[0] doesn't work..." > > Of course it doesn't work. How would you respond to that if you were > in our place? > > - ignore the question because the poster is ever so smart and will have > worked it out by now? > > - point out that l is not a list, or even a tuple, and of course l[0] > doesn't work because l is actually a class? I'd go with some variant of option 2, depending on how well I knew the person asking. If the asker had just dropped in out of the blue and I knew nothing about them I'd say something like "You can't because 'l' isn't a list." Then I'd try to gauge how useful it would be to them to know exactly what 'l' is, but most likely I would be asking them one of several questions next (depending on the context), because it's not clear what they're trying to do. You can't proceed if you don't have enough information to work with. (Well, you can, but it frequently doesn't work out too well.) > Its easy to criticise us for answering the questions you ask instead > of the questions you intended, but we're not mind-readers. We don't > know what you're thinking, we only know what you communicate to us. > Telling us off for answering your questions is hardly likely to > encourage us to answer them in the future. > -- > Steven > "Ever since I learned about confirmation bias, I've been seeing it > everywhere." - Jon Ronson I imagine that people answer questions because they're interested in the question, or they want to help. But really, it's up to them whether they want to answer it or not. From jcasale at activenetwerx.com Wed Jan 11 20:11:32 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 12 Jan 2017 01:11:32 +0000 Subject: reactiveX vs Async In-Reply-To: References: Message-ID: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> >> There is the recent flurry around the new async additions to python > > I meant to add: ?? which I dont pretend to understand?? Try these links on for size: https://msdn.microsoft.com/en-us/library/hh242982(v=vs.103).aspx which links to https://msdn.microsoft.com/en-us/library/hh242983(v=vs.103).aspx near the end. That summarizes the conceptual difference pretty well imho. It's also summarized on the .NET versions GitHub page: https://github.com/Reactive-Extensions/Rx.NET#a-brief-intro I think you can summarize RX as a framework whereas async/await can be used without significant buy-in (Overlook the "async top down or none at all" best practice sound bite). That is, you can artificially convert a synchronous method into a threaded operation and have it executely asynchronously while you do something else and wait on it for completion at somne other time. However, I have only used the .NET implementations. jlc From jcasale at activenetwerx.com Wed Jan 11 20:15:30 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 12 Jan 2017 01:15:30 +0000 Subject: reactiveX vs Async In-Reply-To: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> References: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> Message-ID: > Try these links on for size: > > https://msdn.microsoft.com/en-us/library/hh242982(v=vs.103).aspx which links to > https://msdn.microsoft.com/en-us/library/hh242983(v=vs.103).aspx near the end. These two SO threads have a variation of pretty good explanations: http://stackoverflow.com/questions/2550763/good-example-of-reactive-extensions-use http://stackoverflow.com/questions/1596158/good-introduction-to-the-net-reactive-framework From rustompmody at gmail.com Wed Jan 11 20:47:43 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Wed, 11 Jan 2017 17:47:43 -0800 (PST) Subject: reactiveX vs Async In-Reply-To: References: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> Message-ID: On Thursday, January 12, 2017 at 6:50:09 AM UTC+5:30, Joseph L. Casale wrote: > >> There is the recent flurry around the new async additions to python > > > > I meant to add: ?? which I dont pretend to understand?? > > Try these links on for size: > > https://msdn.microsoft.com/en-us/library/hh242982(v=vs.103).aspx which links to > https://msdn.microsoft.com/en-us/library/hh242983(v=vs.103).aspx near the end. > > That summarizes the conceptual difference pretty well imho. It's also summarized on > the .NET versions GitHub page: https://github.com/Reactive-Extensions/Rx.NET#a-brief-intro > > I think you can summarize RX as a framework whereas async/await can be used without > significant buy-in (Overlook the "async top down or none at all" best practice sound bite). > That is, you can artificially convert a synchronous method into a threaded operation and > have it executely asynchronously while you do something else and wait on it for completion > at somne other time. > > However, I have only used the .NET implementations. > > jlc Thanks Joseph Trouble is there is stew of technologies/languages? (meta)-stewed with more abstract concepts, eg push vs pull, Enumerable-Observable duality, continuous vs discrete time The last causing its own share of confusion with ?functional reactive programming? (FRP) meaning sometimes the one and sometimes the other: http://lambda-the-ultimate.org/node/4982 As for 'Overlook the ?async/await can be used without significant buy-in? ' I believe Ive seen people with considerable experience/understanding of the area take the opposite view, usually along the lines: ?Once you start going non-blocking, you have to non-block all the way? From rustompmody at gmail.com Wed Jan 11 20:56:07 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Wed, 11 Jan 2017 17:56:07 -0800 (PST) Subject: reactiveX vs Async In-Reply-To: References: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> Message-ID: <93f66f14-c9ee-446e-9996-3971e96509f7@googlegroups.com> On Thursday, January 12, 2017 at 6:50:09 AM UTC+5:30, Joseph L. Casale wrote: > However, I have only used the .NET implementations. One more question: Do you know if (and how much) of these things would work in Linux/C# (ie mono)? From tjreedy at udel.edu Wed Jan 11 20:59:30 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 11 Jan 2017 20:59:30 -0500 Subject: Cannot import GDAL In-Reply-To: References: <93285AC0FA20BA47BE43FA54FCA5B321EDD9AE3B@ASHBDAG2M3.resource.ds.bah.com> Message-ID: On 1/11/2017 5:06 PM, Fabien wrote: > On 01/11/2017 10:59 PM, Fabien wrote: >> I would strongly recommend to use conda >> (http://conda.pydata.org/miniconda.html) for managing your packages and >> environments, and then use the conda-forge channel >> (https://conda-forge.github.io/) to install gdal: >> >> conda install -c conda-forge gdal > > > I forgot to mention that since py3.6 is quite recent, most packages > still don't have binaries ready for it. Staying with py3.5 is a better > idea for now. http://www.lfd.uci.edu/~gohlke/pythonlibs/ has 32 and 64 bit 3.6 binaries for over 200 packages, including GDAL 2.1.2. -- Terry Jan Reedy From tjreedy at udel.edu Wed Jan 11 21:06:15 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 11 Jan 2017 21:06:15 -0500 Subject: Cannot import GDAL In-Reply-To: <93285AC0FA20BA47BE43FA54FCA5B321EDD9AE3B@ASHBDAG2M3.resource.ds.bah.com> References: <93285AC0FA20BA47BE43FA54FCA5B321EDD9AE3B@ASHBDAG2M3.resource.ds.bah.com> Message-ID: On 1/11/2017 12:31 PM, Falter, Donald [USA] wrote: > I am new to Python and I am trying to utilize GDAL. I installed Python 3.6. The version I get when I activate IDLE is: MSC v.1900 32 bit (Intel)] on win32. In downloading the GDAL bindings the latest version I can find is release-1800-gdal-2-1-2-mapserver-7-0-2 so I installed the following: gdal-201-1800-core.msi and GDAL-2.1.2.win32-py3.4.msi. > > I added C:\Program Files\GDAL to my path and added the system variables GDAL_DATA = C:\Program Files\GDAL\gdal-data and\ > GDAL_DRIVER_PATH = C:\Program Files\GDAL\gdalplugins > > When I open IDLE and type the following: from osgeo import gdal I get the following error IDLE does not normally cause imports to fail (though it often get blamed), but one can check by running, in this case, 3.6, from the Windows console (ie, Command Prompt) and trying the same import statement. >>>> from osgeo import gdal > Traceback (most recent call last): > File "", line 1, in > from osgeo import gdal > ModuleNotFoundError: No module named 'osgeo' Does C:\Program Files\GDAL contain a package 'osgeo' containing a module 'gdal'? I am not sure if the version mismatch pointed out by others would cause that particular error. If replacing the 3.4 package with a 3.6 package fixes your problem completely, please let us know. -- Terry Jan Reedy From jcasale at activenetwerx.com Wed Jan 11 21:12:29 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 12 Jan 2017 02:12:29 +0000 Subject: reactiveX vs Async In-Reply-To: References: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> Message-ID: > Thanks Joseph > Trouble is there is stew of technologies/languages? > (meta)-stewed with more abstract concepts, eg push vs pull, Enumerable-Observable > duality, continuous vs discrete time > The last causing its own share of confusion with ?functional reactive programming? (FRP) meaning sometimes the one and sometimes the other: > http://lambda-the-ultimate.org/node/4982 Heh, Yeah I don't claim to be an expert, I have only just started using the syntax in my own projects whereas I almost always used the classical thread model with sync primitives and message passing. > As for 'Overlook the ?async/await can be used without significant buy-in? ' > I believe Ive seen people with considerable experience/understanding of the area > take the opposite view, usually along the lines: ?Once you start going non-blocking, > you have to non-block all the way? Right, as I mentioned overlook the pedantic argument just for the concept. While you *can* do as I mentioned rather easily, it's not a useful or scalable approach. Their suggestion is correct in reality but I only mentioned the minimal buy-in it to illustrate that existing code *could* adopt the pattern trivially whereas Rx is not simply two keywords, it's an entire framework. Ultimately they accomplish the same thing, that is asynchronous execution but use two different patterns. I'll paraphrase another source: "Rx adds a convenient way to add callbacks and manage execution." The async/await pattern is closer to the metal, it's not dealing with a sequence of observable objects, it is however manipulating execution of the explicit block of code where its applied. jlc From jcasale at activenetwerx.com Wed Jan 11 21:17:04 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 12 Jan 2017 02:17:04 +0000 Subject: reactiveX vs Async In-Reply-To: <93f66f14-c9ee-446e-9996-3971e96509f7@googlegroups.com> References: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> <93f66f14-c9ee-446e-9996-3971e96509f7@googlegroups.com> Message-ID: > One more question: Do you know if (and how much) of these things would work > in Linux/C# (ie mono)? Mono, I forgot what that is when .net core debuted:) Looks like the .net Rx guys have a port, https://github.com/Reactive-Extensions/Rx.NET/issues/148 A package for .net core is up on nuget. jlc From rustompmody at gmail.com Thu Jan 12 00:00:38 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Wed, 11 Jan 2017 21:00:38 -0800 (PST) Subject: reactiveX vs Async In-Reply-To: References: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> <93f66f14-c9ee-446e-9996-3971e96509f7@googlegroups.com> Message-ID: <0a41fd4a-e51a-4f29-baf8-312cbb85f30f@googlegroups.com> On Thursday, January 12, 2017 at 7:47:21 AM UTC+5:30, Joseph L. Casale wrote: > > One more question: Do you know if (and how much) of these things would work > > in Linux/C# (ie mono)? > > Mono, I forgot what that is when .net core debuted:) A brief elaboration on that please?? ? ? > > Looks like the .net Rx guys have a port, https://github.com/Reactive-Extensions/Rx.NET/issues/148 All this makes little sense to me :-( C# is a sweet language, in many small ways better than java But Windows/.NET?? Hoo boy! [Just an ol-nixer getting lost... No judgement really!] > > A package for .net core is up on nuget. So you are saying that nuget-ing .Net core would be a workable pre-requisite for Rx on mono? From jcasale at activenetwerx.com Thu Jan 12 00:20:07 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 12 Jan 2017 05:20:07 +0000 Subject: reactiveX vs Async In-Reply-To: <0a41fd4a-e51a-4f29-baf8-312cbb85f30f@googlegroups.com> References: <4062a277404f4823bf0baea1ae76dd8f@activenetwerx.com> <93f66f14-c9ee-446e-9996-3971e96509f7@googlegroups.com> <0a41fd4a-e51a-4f29-baf8-312cbb85f30f@googlegroups.com> Message-ID: > So you are saying that nuget-ing .Net core would be a workable pre-requisite for > Rx on mono? Microsoft open sourced .net a while ago. With that came the movement to bring .net to other platforms. https://en.wikipedia.org/wiki/.NET_Framework#.NET_Core As its currently being heavily developed, it only provides a subset of the full .net base class library but its covering more ground constantly. A good intro into what is covered is https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/ So you can install the sdk and runtime on many supported Linux versions, a mac or Windows. Get it at https://www.microsoft.com/net/download/core Then use your text editor of choice, or something like visual studio code (another free offering that supports multiple platforms), add the nuget package and hack away on your platform of choice;). jlc From metal.suomi at gmail.com Thu Jan 12 00:36:54 2017 From: metal.suomi at gmail.com (metal.suomi at gmail.com) Date: Wed, 11 Jan 2017 21:36:54 -0800 (PST) Subject: just started In-Reply-To: References: <7e1190f3-e5be-4a39-ad68-35abb9832653@googlegroups.com> Message-ID: along the above discussion, resuming my python after the Xmas break (hope you had a nice one!) I want to install a number of scientific libraries, but I'm confused which packages it contains what. 1) I see on scipy.org that scipy contains scipy library (fine, it makes sense), along with other ones (numpy, matplotlib ....). So, I would guess that the following command pip install --user scipy install all the libraries I need, but instead it seems that I need to specify them too? I.e. to install them I need to name them pip install --user scipy numpy matplotlib etc... etc... (of course, I guess I can equivalently use the command python3.5 -m pip install --user scipy numpy matplotlib .... right?) 2) I have already install matplotlib time ago. So, in the above commands I can avoid to add matplotlib, or I can included and a new installation will be overwritten, correct? Thanks! From antoon.pardon at rece.vub.ac.be Thu Jan 12 03:49:18 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Thu, 12 Jan 2017 09:49:18 +0100 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <001f01d26c5e$13762080$27b23dae@sambora> References: <001f01d26c5e$13762080$27b23dae@sambora> Message-ID: <3e3e7166-dc5f-2df2-b8d8-3aca4b1c3cf7@rece.vub.ac.be> Op 11-01-17 om 23:57 schreef Deborah Swanson: > >> What are we supposed to do when somebody asks a question based on an >> obvious mistake? Assume that they're a quick learner who has probably >> already worked out their mistake and doesn't need an answer? That >> would certainly make our life easier: we can just ignore everybody's >> questions. > No, of course not. My advice to people who want to help is to not assume > that you know what the question asker does and doesn't know, and just > answer the questions without obsessing about what they know. With all respect, such an answer betrays not much experience on this list. Half the time answering in this way would mean making very little progress in actually helping the person. There is an important difference in trying to help someone and just answering his questions. And your advice may be the best way to help someone like you but not everyone is like you. A lot of people have been helped by a remark that didn't answer the question. > If that's > impossible because they have something so wrong that you don't know what > they're asking, that would be a good time to point it out and give them > a chance to correct it. It is rarely a question of impossibility. It often enough is a sense that the person asking the question is approaching the problem from the wrong side. Often enough that sense is correct, often enough that sense is wrong. All the participants can do is take a clue from the question and then guess what respons would help this person best. Nobody can expect that this list will treat their questions in a way that suits their personal style. -- Antoon Pardon From python at deborahswanson.net Thu Jan 12 04:26:38 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Thu, 12 Jan 2017 01:26:38 -0800 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <3e3e7166-dc5f-2df2-b8d8-3aca4b1c3cf7@rece.vub.ac.be> Message-ID: <001301d26cb5$fa51c930$27b23dae@sambora> Antoon Pardon wrote, on January 12, 2017 12:49 AM > > Op 11-01-17 om 23:57 schreef Deborah Swanson: > > > >> What are we supposed to do when somebody asks a question based on an > >> obvious mistake? Assume that they're a quick learner who has probably > >> already worked out their mistake and doesn't need an answer? That > >> would certainly make our life easier: we can just ignore everybody's > >> questions. > > No, of course not. My advice to people who want to help is to not > > assume that you know what the question asker does and doesn't know, > > and just answer the questions without obsessing about what > they know. > > With all respect, such an answer betrays not much experience > on this list. Half the time answering in this way would mean > making very little progress in actually helping the person. > There is an important difference in trying to help someone > and just answering his questions. And your advice may be the > best way to help someone like you but not everyone is like > you. A lot of people have been helped by a remark that didn't > answer the question. It's true, I've only been on this list a few weeks, although I've seen and been on the receiving end of the kind of "help" that feels more like being sneered at than help. Not on this list, but on Linux and similar lists. There does seem to be a "tough love" approach to "helping" people, and I haven't seen that it really helped that much, in other places that I've seen it in action over a period of time. I'm willing though to just see how it works on this list. Since I've been here, I haven't seen people come back who get that kind of approach, but a few weeks is too short a time to draw conclusions. Still, when people who need help don't come right back, that should be a first clue that they didn't get it. > > If that's > > impossible because they have something so wrong that you don't know > > what they're asking, that would be a good time to point it out and > > give them a chance to correct it. > > It is rarely a question of impossibility. It often enough is > a sense that the person asking the question is approaching > the problem from the wrong side. Often enough that sense is > correct, often enough that sense is wrong. All the > participants can do is take a clue from the question and then > guess what respons would help this person best. This sounds right to me. Any list or forum that fields questions has the problem of understanding the questioner who's writing in. Any strategy that briges that gap seems like a good one to me. > Nobody can expect that this list will treat their questions > in a way that suits their personal style. > > -- > Antoon Pardon > > Oh, I'm sure that's true, though I do think more direct question asking and answering is always helpful. Communication in lists and forums is somewhat in the dark, because there's so little context in many of the conversations. Questions (and waiting for the answers before responding) are an excellent way to fill in some of the dark spaces. From ansu.igit at gmail.com Thu Jan 12 05:26:30 2017 From: ansu.igit at gmail.com (Ansuman Suryaprakash) Date: Thu, 12 Jan 2017 02:26:30 -0800 (PST) Subject: need help getting error when trying selenium with python Message-ID: <1c47379c-cf9b-40e0-a288-f5385317d118@googlegroups.com> pyhton version 3.5 selenium version 3.0.2 firefox version 50.1.0 pyhton code: from selenium import webdriver browser = webdriver.Firefox() browser.get('http://127.0.0.1:8000') assert 'Django' in browser.title Error: D:\django_learning_TDD>py functional_tests.py Traceback (most recent call last): File "functional_tests.py", line 3, in browser = webdriver.Firefox() File "C:\Users\Subhasish\AppData\Local\Programs\Python\Python35-32\lib\site-pa ckages\selenium\webdriver\firefox\webdriver.py", line 140, in __init__ self.service.start() File "C:\Users\Subhasish\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 102, in start raise WebDriverException("Can not connect to the Service %s" % self.path) selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver Can someonr help me with getting rid of the above error From marko at pacujo.net Thu Jan 12 05:27:34 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Thu, 12 Jan 2017 12:27:34 +0200 Subject: Using namedtuples field names for column indices in a list of lists References: <3e3e7166-dc5f-2df2-b8d8-3aca4b1c3cf7@rece.vub.ac.be> <001301d26cb5$fa51c930$27b23dae@sambora> Message-ID: <87o9zck1yh.fsf@elektro.pacujo.net> "Deborah Swanson" : > I've only been on this list a few weeks, although I've seen and been > on the receiving end of the kind of "help" that feels more like being > sneered at than help. Not on this list, but on Linux and similar > lists. There does seem to be a "tough love" approach to "helping" > people, An instructive anecdote: somebody I know told me he once needed the definitive list of some websites. He posted a question on the relevant online forum, but it fell on deaf ears. After some days, he replied to his own posting saying he had found the list and included the list in his reply. He knew the list was probably not current, but it served its purpose: right away he got an angry response pointing out his list was completely wrong with a link to the up-to-date list. Marko From skip.montanaro at gmail.com Thu Jan 12 07:12:00 2017 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Thu, 12 Jan 2017 06:12:00 -0600 Subject: timers in threaded application Message-ID: I know signals and threads are a bad mix, but I only discovered I was playing with fire a few minutes ago. I've been using the websocket-client library (https://pypi.python.org/pypi/websocket-client/) to interact with another system at work. It implements its own event loop, but doesn't seem to play nice with others. I used gtk/gobject for years. It implemented timeouts for you, you could integrate it with other event loops, etc. As far as I could tell, websocket-client implements none of that. I needed some timer functionality, so not thinking about it too carefully, I implemented it using signal.alarm. This worked pretty well, but I was experiencing mysterious hangs. This morning, looking through the websocket-client code to see if I'd missed something, I saw that it uses threads. So, that might be my problem. I still need timers, and for the moment I'm stuck with this package's event loop. What options do I have? I'm certainly willing to switch to another websocket client library if that will make all my problems go away. This is my first ws experience though, and I inherited some code which used websocket-client, so just went with what I had. Thx, Skip From rosuav at gmail.com Thu Jan 12 08:31:01 2017 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 13 Jan 2017 00:31:01 +1100 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <87o9zck1yh.fsf@elektro.pacujo.net> References: <3e3e7166-dc5f-2df2-b8d8-3aca4b1c3cf7@rece.vub.ac.be> <001301d26cb5$fa51c930$27b23dae@sambora> <87o9zck1yh.fsf@elektro.pacujo.net> Message-ID: On Thu, Jan 12, 2017 at 9:27 PM, Marko Rauhamaa wrote: > An instructive anecdote: somebody I know told me he once needed the > definitive list of some websites. He posted a question on the relevant > online forum, but it fell on deaf ears. After some days, he replied to > his own posting saying he had found the list and included the list in > his reply. He knew the list was probably not current, but it served its > purpose: right away he got an angry response pointing out his list was > completely wrong with a link to the up-to-date list. There's a reason for that. Inaccurate information is worse than none at all, because it's indistinguishable from accurate information without deep analysis. Also, often someone won't know the correct answer, but will recognize a wrong answer. In correcting the record, you come a bit closer to the truth. Sometimes all it takes is one wrong answer, and the discussion kicks off. ChrisA From info at wingware.com Thu Jan 12 08:49:58 2017 From: info at wingware.com (Wingware) Date: Thu, 12 Jan 2017 08:49:58 -0500 Subject: ANN: Wing IDE 6.0.1 released Message-ID: <58778986.8060509@wingware.com> Hi, We've just released Wing IDE 6.0.1 which improves remote host configuration, adds remote development support for 32-bit Linux and older 64-bit Linux systems, fixes stability problems affecting some users, and makes many other improvements. For details on this release, see the change log at http://wingware.com/pub/wingide/6.0.1/CHANGELOG.txt Wing IDE 6 is the latest major release of Wingware's Python IDE that adds many new features, introduces a new annual license option, and makes some changes to the product line. New Features * Improved Multiple Selections: Quickly add selections and edit them all at once * Easy Remote Development: Work seamlessly on remote Linux, OS X, and Raspberry Pi systems * Debugging in the Python Shell: Reach breakpoints and exceptions in (and from) the Python Shell * Recursive Debugging: Debug code invoked in the context of stack frames that are already being debugged * PEP 484 and PEP 526 Type Hinting: Inform Wing's static analysis engine of types it cannot infer * Support for Python 3.6 and Stackless 3.4: Use async and other new language features * Optimized debugger: Run faster, particularly in multi-process and multi-threaded code * Support for OS X full screen mode: Zoom to a virtual screen, with auto-hiding menu bar * Added a new One Dark color palette: Enjoy the best dark display style yet * Updated French and German localizations: Thanks to Jean Sanchez, Laurent Fasnacht, and Christoph Heitkamp For a much more detailed overview of new features see the release notice at http://wingware.com/news/2017-01-10 Annual Use License Option Wing 6 adds the option of purchasing a lower-cost expiring annual license for Wing IDE Pro. An annual license includes access to all available Wing IDE versions while it is valid, and then ceases to function if it is now renewed. Pricing for annual licenses is US$ 179/user for Commercial Use and US$ 69/user for Non-Commercial Use. Perpetual licenses for Wing IDE will continue to be available at the same pricing. The cost of extending Support+Upgrades subscriptions on Non-Commercial Use perpetual licenses for Wing IDE Pro has also been dropped from US$ 89 to US$ 39 per user. For details, see https://wingware.com/store/purchase Wing Personal is Free Wing IDE Personal is now free and no longer requires a license to run. It now also includes the Source Browser, PyLint, and OS Commands tools, and supports the scripting API and Perspectives. However, Wing Personal does not include Wing Pro's advanced editing, debugging, testing and code management features, such as remote host access, refactoring, find uses, version control, unit testing, interactive debug probe, multi-process and child process debugging, move program counter, conditional breakpoints, debug watch, framework-specific support (for matplotlib, Django, and others), find symbol in project, and other features. Links Release notice: http://wingware.com/news/2017-01-10 Free trial: http://wingware.com/wingide/trial Downloads: http://wingware.com/downloads Feature list: http://wingware.com/wingide/features Buy: http://wingware.com/store/purchase Upgrade: https://wingware.com/store/upgrade Questions? Don't hesitate to email us at support at wingware.com. Thanks, -- Stephan Deibel Wingware | Python IDE The Intelligent Development Environment for Python Programmers wingware.com From iranna.gani28 at gmail.com Thu Jan 12 08:58:02 2017 From: iranna.gani28 at gmail.com (Iranna Mathapati) Date: Thu, 12 Jan 2017 19:28:02 +0530 Subject: Find and append file path Message-ID: Hi Team, How to append file path and find the latest folder according to the latest date: CLI input :"/root/user/branches/xyz" i want to find latest file within CLI input path and append to given CLI path Example: CLI Input: /root/user/branches/xyz find latest file and append to CLI path: "/root/user/branches/xyz/Apple" From rhodri at kynesim.co.uk Thu Jan 12 09:44:17 2017 From: rhodri at kynesim.co.uk (Rhodri James) Date: Thu, 12 Jan 2017 14:44:17 +0000 Subject: Find and append file path In-Reply-To: References: Message-ID: <9938851c-9a95-016f-1cc0-14859845d78c@kynesim.co.uk> On 12/01/17 13:58, Iranna Mathapati wrote: > Hi Team, > > How to append file path and find the latest folder according to the latest > date: > > CLI input :"/root/user/branches/xyz" > > i want to find latest file within CLI input path and append to given CLI > path > > Example: > CLI Input: /root/user/branches/xyz > > find latest file and append to CLI path: > "/root/user/branches/xyz/Apple" > Use os.scandir() to list whatever is in the directory, DirEntry.is_dir() will tell you if an entry is itself a directory, and DirEntry.stat() will let you get at the directory's various time stats. Which attribute you want depends on exactly what you mean by "the latest folder according to the latest date". Then just use your favourite means to join the directory name you selected to the original path. -- Rhodri James *-* Kynesim Ltd From python.list at tim.thechases.com Thu Jan 12 10:39:34 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 12 Jan 2017 09:39:34 -0600 Subject: Find and append file path In-Reply-To: References: Message-ID: <20170112093934.5689207a@bigbox.christie.dr> On 2017-01-12 19:28, Iranna Mathapati wrote: > Example: > CLI Input: /root/user/branches/xyz > > find latest file and append to CLI path: > "/root/user/branches/xyz/Apple" I've used something like the below to find the most recent file in a directory tree (it also works for pulling other attributes like the file-size from os.stat() results). -tkc import os from operator import attrgetter def dir_iter(root): for fullpath in ( os.path.join(path, dir_) for path, dirs, files in os.walk(root) for dir_ in dirs ): if os.path.isdir(fullpath): yield fullpath def find_latest(root, which_time=attrgetter("st_mtime") ): return max(dir_iter(root), key=lambda fullpath: which_time(os.stat(fullpath)) ) if __name__ == "__main__": from sys import argv if len(argv) > 1: root = argv[1] else: root = '.' print(find_latest(root)) print(find_latest(root, which_time=attrgetter("st_ctime"))) From josemsuarezsierra at gmail.com Thu Jan 12 11:07:31 2017 From: josemsuarezsierra at gmail.com (=?UTF-8?Q?Jos=C3=A9_Manuel_Su=C3=A1rez_Sierra?=) Date: Thu, 12 Jan 2017 08:07:31 -0800 (PST) Subject: Doubt with matrix Message-ID: Hello, I want to go over matrix indexs with this code: def comparador2(a, b): c3 = ["0"] # variables x = -1 # contador de letras aniadidas a c3 i = 0 # contador bucle secuencia a j = 0 # contador bucle secuencia b l1 = len(a) l2 = len(b) cont = [] # contador de ciclos poner primer termino a 0 k = -1 # contador de 0 y 1 aniadidos a cont if l1 > l2: # metodo de la burbuja que elige la secuencia mas larga REVISAR puede ser alreves aux = a a = b b = aux for a[i] in a: # en la secuencia1 recorro los elementos for b[j] in b : if a[i] == b[j] and i <= l1 and j <= l2: # Si el elemento i de la seq1 es igual que el elemento j de la seq2, y el numero de elementos en i y j es menor o igual que la longitud de las secuencias 1 y 2 c3.append(a[i]) # se aniade el elemento comun a la lista c3 x = x + 1 k = k + 1 j = j + 1 # se pasa el elemento siguiente de la seq2 i = i + 1 # se pasa el elemento siguiente de la seq1 cont.append(1) elif a[i] != b[ j] and i <= l1 and j <= l2: # si no coinciden estos elementos se pasa al siguiente elemento de la lista 2 j = j + 1 k = k + 1 cont.append(0) if cont[k] == 0 and cont[k - 1] == 1 and cont[k - 2] == 0 and k >= 2: i = i - 1 j = j - 1 else: k = k + 1 cont.append(0) if i == l2: i = i + 1 j = 0 return c3 and this issue is prompted: IndexError: list assignment index out of range How could I fix it? Thank you for your assistance From bgailer at gmail.com Thu Jan 12 11:33:43 2017 From: bgailer at gmail.com (Bob Gailer) Date: Thu, 12 Jan 2017 11:33:43 -0500 Subject: Doubt with matrix In-Reply-To: References: Message-ID: Always Post the entire traceback. That will show us the line of code that raised the error, as well as the sequence of function calls involved. On Jan 12, 2017 11:10 AM, "Jos? Manuel Su?rez Sierra" < josemsuarezsierra at gmail.com> wrote: > Hello, I want to go over matrix indexs with this code: > def comparador2(a, b): > c3 = ["0"] # variables > x = -1 # contador de letras aniadidas a c3 > i = 0 # contador bucle secuencia a > j = 0 # contador bucle secuencia b > l1 = len(a) > l2 = len(b) > cont = [] # contador de ciclos poner primer termino a 0 > k = -1 # contador de 0 y 1 aniadidos a cont > > if l1 > l2: # metodo de la burbuja que elige la secuencia mas larga > REVISAR puede ser alreves > aux = a > a = b > b = aux > > for a[i] in a: # en la secuencia1 recorro los elementos > > for b[j] in b : > > if a[i] == b[j] and i <= l1 and j <= l2: # Si el elemento > i de la seq1 es igual que el elemento j de la seq2, y el numero de > elementos en i y j es menor o igual que la longitud de las secuencias 1 y 2 > c3.append(a[i]) # se aniade el elemento comun a la > lista c3 > x = x + 1 > k = k + 1 > j = j + 1 # se pasa el elemento siguiente de la seq2 > i = i + 1 # se pasa el elemento siguiente de la seq1 > cont.append(1) > > > > elif a[i] != b[ > j] and i <= l1 and j <= l2: # si no coinciden estos > elementos se pasa al siguiente elemento de la lista 2 > j = j + 1 > k = k + 1 > cont.append(0) > if cont[k] == 0 and cont[k - 1] == 1 and cont[k - 2] > == 0 and k >= 2: > i = i - 1 > j = j - 1 > else: > k = k + 1 > cont.append(0) > if i == l2: > i = i + 1 > j = 0 > > > return c3 > > and this issue is prompted: > IndexError: list assignment index out of range > > How could I fix it? > > Thank you for your assistance > -- > https://mail.python.org/mailman/listinfo/python-list > From __peter__ at web.de Thu Jan 12 11:59:31 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 12 Jan 2017 17:59:31 +0100 Subject: Doubt with matrix References: Message-ID: Jos? Manuel Su?rez Sierra wrote: > Hello, I want to go over matrix indexs with this code: > def comparador2(a, b): > c3 = ["0"] # variables > x = -1 # contador de letras aniadidas a c3 > i = 0 # contador bucle secuencia a > j = 0 # contador bucle secuencia b > l1 = len(a) > l2 = len(b) > cont = [] # contador de ciclos poner primer termino a 0 > k = -1 # contador de 0 y 1 aniadidos a cont > > if l1 > l2: # metodo de la burbuja que elige la secuencia mas larga > REVISAR puede ser alreves In Python there's a cool way to swap values without an explicit helper variable: a, b = b, a > aux = a > a = b > b = aux OK, now you have swapped a and b, but not their respective lengths l1 and l2... > for a[i] in a: # en la secuencia1 recorro los elementos Do you really want to modify a[i] on every iteration? Most certainly not. Try to use the normal way with just a name, e. g. for entry_a in a: ... Then use entry_a instead of a[i] for your comparison in the body of the for- loop. With that approach it's impossible to get an IndexError. If you still need the index to alter an entry in the list -- enumerate() is your friend: for index_a, entry_a in enumerate(a): ... Again it's unlikely that you will get an IndexError. > for b[j] in b : > > if a[i] == b[j] and i <= l1 and j <= l2: # Si el elemento > i de la seq1 es igual que el elemento j de la seq2, y el > numero de elementos en i y j es menor o igual que la > longitud de las secuencias 1 y 2 > c3.append(a[i]) # se aniade el elemento comun a la > lista c3 x = x + 1 > k = k + 1 > j = j + 1 # se pasa el elemento siguiente de la seq2 > i = i + 1 # se pasa el elemento siguiente de la seq1 > cont.append(1) > > > > elif a[i] != b[ > j] and i <= l1 and j <= l2: # si no coinciden estos > elementos se pasa al siguiente elemento de la lista 2 > j = j + 1 k = k + 1 > cont.append(0) > if cont[k] == 0 and cont[k - 1] == 1 and cont[k - 2] > == 0 and k >= 2: > i = i - 1 > j = j - 1 > else: > k = k + 1 > cont.append(0) > if i == l2: > i = i + 1 > j = 0 > > > return c3 > > and this issue is prompted: > IndexError: list assignment index out of range > > How could I fix it? > > Thank you for your assistance From __peter__ at web.de Thu Jan 12 12:32:02 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 12 Jan 2017 18:32:02 +0100 Subject: Find and append file path References: <20170112093934.5689207a@bigbox.christie.dr> Message-ID: Tim Chase wrote: > def dir_iter(root): > for fullpath in ( > os.path.join(path, dir_) > for path, dirs, files in os.walk(root) > for dir_ in dirs > ): > if os.path.isdir(fullpath): > yield fullpath Why did you add an explicit isdir() check? My understanding is that the names in dirs are known to be directories. From python.list at tim.thechases.com Thu Jan 12 12:50:39 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 12 Jan 2017 11:50:39 -0600 Subject: Find and append file path In-Reply-To: References: <20170112093934.5689207a@bigbox.christie.dr> Message-ID: <20170112115039.3d335097@bigbox.christie.dr> On 2017-01-12 18:32, Peter Otten wrote: > Why did you add an explicit isdir() check? My understanding is that > the names in dirs are known to be directories. Ah, right. The code I had laying around that did something similar but processes both the dirs and the files, so when the OP asked for just dirs, I ended up with both belt-and-suspenders changes to it. So that isdir() check could readily be removed. -tkc From kulakov.ilya at gmail.com Thu Jan 12 20:20:49 2017 From: kulakov.ilya at gmail.com (Ilya Kulakov) Date: Thu, 12 Jan 2017 17:20:49 -0800 (PST) Subject: Automatically restore virtualenv in Terminal.app on macOS (zsh, bash) Message-ID: <727d9c09-be19-44cc-ae47-75561b4dccb4@googlegroups.com> Today I discovered that one can customize session restoration in Terminal.app by defining the shell_session_save_user_state function. Here is a script that restores active virtualenv, e.g. after you reboot your machine: https://gist.github.com/Kentzo/185e4eab382bbcf1f0a9738a28128dce It's written for ZSH, but it's fairly simple to port it to bash: you don't even need plugin, since session management is supported out of the box. Here is a plugin for oh-my-zsh: https://github.com/Kentzo/oh-my-zsh/tree/master/plugins/session From torriem at gmail.com Thu Jan 12 20:40:57 2017 From: torriem at gmail.com (Michael Torrie) Date: Thu, 12 Jan 2017 18:40:57 -0700 Subject: Using namedtuples field names for column indices in a list of lists In-Reply-To: <001301d26cb5$fa51c930$27b23dae@sambora> References: <001301d26cb5$fa51c930$27b23dae@sambora> Message-ID: On 01/12/2017 02:26 AM, Deborah Swanson wrote: > It's true, I've only been on this list a few weeks, although I've seen > and been on the receiving end of the kind of "help" that feels more like > being sneered at than help. Not on this list, but on Linux and similar > lists. There does seem to be a "tough love" approach to "helping" > people, and I haven't seen that it really helped that much, in other > places that I've seen it in action over a period of time. If you go down a wrong path, people are going to try to warn you. For example, you were told several times, no that object really is a list, yet you argued with them on that point for several posts. Tough love or sneering? No, absolutely not. Communication difficulties? Yes! But not even close to wholly the fault of those who were trying to assist you. If you haven't been helped, it's not for lack of their trying. > I'm willing > though to just see how it works on this list. Since I've been here, I > haven't seen people come back who get that kind of approach, but a few > weeks is too short a time to draw conclusions. Still, when people who > need help don't come right back, that should be a first clue that they > didn't get it. Fortunately this list is pretty friendly and open to newbies. And long-time posters have no problem telling other long-time posters when they do cross the line into bullying or trolling territory. Fortunately I've seen nothing but people wanting to help you with your ventures in Python programming responding to your queries, which I'm gratified to see. And many many newbies have been helped in their explorations of Python land. From dieter at handshake.de Fri Jan 13 04:23:51 2017 From: dieter at handshake.de (dieter) Date: Fri, 13 Jan 2017 10:23:51 +0100 Subject: timers in threaded application References: Message-ID: <87lguf9uu0.fsf@handshake.de> Skip Montanaro writes: > ... > I still need timers, and for the moment I'm stuck with this package's event > loop. What options do I have? If the activities are really run in separate threads (such that an event processing can take arbitrary time without much affecting the processing of other events) and "select.select" is available on your platform (e.g. some *nix* platform), you might be able to use its "timeout" parameter. This is particularly useful when what you want to timeout are I/O operations. If what you want to timeout are not I/O operations, you can have a look at "threading.Timer". It uses a separate thread, lets it wait a specified time and then starts a specified function, unless "cancel"ed. In this case, you must find some way to abort a pending event processing due to a timeout and let the timer function use it for abortion. From haraldnordgren at gmail.com Fri Jan 13 04:27:10 2017 From: haraldnordgren at gmail.com (haraldnordgren at gmail.com) Date: Fri, 13 Jan 2017 01:27:10 -0800 (PST) Subject: A detailed description on virtualenv's packaging dependecies? (pip, easy_install, wheel, setuptools, distlib, etc.) Message-ID: <05080bd7-5903-4621-9cc5-e5c9be04e80d@googlegroups.com> I was working on a bugfix for Virtualenv, regarding very long shebang lines that are breaking things. In the process, I realized that if I want really fix to my particular issue it likely needs to be done on the lower level of Python package management. I started with pip, moved to setuptools and now set my sight on distlib. Can someone describe the specific dependencies of all the *packaging* libraries that `virtualenv` uses? And the dependencies between them? I believe that virtualenv directly imports `pip`, `easy_install` and `wheel`. Those in turn import `setuptools` and `distlib`. Am I so lucky as to assume that distlib in the lowest-level library used by all the rest in virtualenv for packages? From haraldnordgren at gmail.com Fri Jan 13 04:27:46 2017 From: haraldnordgren at gmail.com (haraldnordgren at gmail.com) Date: Fri, 13 Jan 2017 01:27:46 -0800 (PST) Subject: Running Virtualenv with a custom distlib? Message-ID: <2bc1125c-eb70-49bc-bd90-ebeaa9741258@googlegroups.com> I want to do some development on `distlib`, and in the process run the code via `virtualenv` which has distlib as a dependency. That is, not run the process inside a virtualenv, but run virtualenv's code using a custom dependency. What are the steps I need to go through to achieve this? It seems to me that normal package management (`pip`) is not possible here. From steve+python at pearwood.info Fri Jan 13 05:46:34 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Fri, 13 Jan 2017 21:46:34 +1100 Subject: Byte-run: a Python interpreter written in Python Message-ID: <5878b00c$0$1588$c3e8da3$5496439d@news.astraweb.com> http://aosabook.org/en/500L/a-python-interpreter-written-in-python.html -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From adnan.connexus at gmail.com Fri Jan 13 06:38:33 2017 From: adnan.connexus at gmail.com (adnan.connexus at gmail.com) Date: Fri, 13 Jan 2017 03:38:33 -0800 (PST) Subject: matrix from matrix Message-ID: Hi all, I have just started python coding and have ran into my first brick wall. Can some one please assist me with the following query ? I have two Matrices: 1 ? masterMatrix ? A 2d matrix containing items in the rows and time as the columns column headers (time beginning from 00:00 to 23:45 at every 15 minutes interval (96 columns e.g. 00:00 | 00:15 . . . .23:45). this matrix provides a value of each item in the list a every 14 minutes. 2 ? subMatrix ? A matrix which has to be populate based on selected items and times from masterMatrix What would be the fastest way to first initialise the subMatrix and populate the workingTable based on the matching values in masterTable. best regards, Shei From __peter__ at web.de Fri Jan 13 10:20:23 2017 From: __peter__ at web.de (Peter Otten) Date: Fri, 13 Jan 2017 16:20:23 +0100 Subject: matrix from matrix References: Message-ID: adnan.connexus at gmail.com wrote: > Hi all, > > I have just started python coding and have ran into my first brick wall. > Can some one please assist me with the following query ? > > I have two Matrices: > 1 ? masterMatrix ? A 2d matrix containing items in the rows and time as > the columns column headers (time beginning from 00:00 to 23:45 at every 15 > minutes interval (96 columns e.g. 00:00 | 00:15 . . . .23:45). this matrix > provides a value of each item in the list a every 14 minutes. > > 2 ? subMatrix ? A matrix which has to be populate based on selected items > and times from masterMatrix > > What would be the fastest way to first initialise the subMatrix and Forget about "fastest" as long as you have no way. > populate the workingTable based on the matching values in masterTable. Give us a bit of code that initialises your master matrix with some toy data (3 to 5 rows and columns, say) and that shows what you have tried to build the sub-matrix. Then we can identify your "road block" and help you overcome it. From steve+python at pearwood.info Fri Jan 13 10:23:47 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 14 Jan 2017 02:23:47 +1100 Subject: matrix from matrix References: Message-ID: <5878f104$0$1587$c3e8da3$5496439d@news.astraweb.com> On Fri, 13 Jan 2017 10:38 pm, adnan.connexus at gmail.com wrote: > Hi all, > > I have just started python coding and have ran into my first brick wall. > Can some one please assist me with the following query ? Not really, because you haven't given us enough information to answer your questions. > I have two Matrices: What is a matrix? Do you mean a list? A numpy array? The best thing is to show us the code you already have. We don't need to see ALL your code, just the relevant part that creates the two matrices. > 1 ? masterMatrix ? A 2d matrix containing items in the rows and time as > the columns column headers (time beginning from 00:00 to 23:45 at every 15 > minutes interval (96 columns e.g. 00:00 | 00:15 . . . .23:45). this matrix > provides a value of each item in the list a every 14 minutes. > > 2 ? subMatrix ? A matrix which has to be populate based on selected items > and times from masterMatrix How do you select the items that you want? > What would be the fastest way to first initialise the subMatrix and > populate the workingTable based on the matching values in masterTable. That's like asking: "What's the shortest piece of string to tie two things together?" Um, it depends on which two things, it depends on how secure you need the tie to be... we need more detail. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From skip.montanaro at gmail.com Fri Jan 13 10:41:38 2017 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Fri, 13 Jan 2017 09:41:38 -0600 Subject: timers in threaded application In-Reply-To: <87lguf9uu0.fsf@handshake.de> References: <87lguf9uu0.fsf@handshake.de> Message-ID: On Fri, Jan 13, 2017 at 3:23 AM, dieter wrote: > If what you want to timeout are not I/O operations, you can have a > look at "threading.Timer". It uses a separate thread, lets it wait > a specified time and then starts a specified function, unless > "cancel"ed. > Ooh, hadn't considered that. That would seem to do the trick. I assume the function to be executed will be run in the timer's thread, so will have to suitably lock any shared data. Thx, Skip From ian.g.kelly at gmail.com Fri Jan 13 12:08:57 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 13 Jan 2017 10:08:57 -0700 Subject: Byte-run: a Python interpreter written in Python In-Reply-To: <5878b00c$0$1588$c3e8da3$5496439d@news.astraweb.com> References: <5878b00c$0$1588$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Jan 13, 2017 at 3:46 AM, Steve D'Aprano wrote: > > http://aosabook.org/en/500L/a-python-interpreter-written-in-python.html Neat. But not really surprising IMO that it can fit into 500 lines, since it doesn't handle compiling Python into bytecode (which is the hard part) and doesn't include libraries. There doesn't seem to be much purpose to this other than it being a toy project written for a book. From michael.s011235 at gmail.com Fri Jan 13 13:00:05 2017 From: michael.s011235 at gmail.com (Michael S) Date: Fri, 13 Jan 2017 19:00:05 +0100 Subject: Problem while building Python 3.6 from source. Message-ID: Hello, I am new to this mailing-list and I really don't know whether this mail should belong to python-dev. Please tell me, if so. Unfortunately, I have got the following problem: I wanted to build and install Python 3.6 from source but did not succeed. To clarify my situation, I got as an operating system Debian jessie 8.6 and I used the xz compressed source tarball from https://www.python.org/downloads/release/python-360/. Concerning the build dependencies: I just executed: $ sudo apt-get build-dep python3.4 (since 3.6 and 3.5 did not work). Then I executed ./configure --enable-optimizations and make -j4 (I got 4 cores). The output of make ended like: 'make: *** [profile-opt] Error 2'. I had redirected the output and error of the configure and make commands via $ make -j4 &> /home/username/make_output.txt. Nevertheless I got an error to the console: '*** Error in ./python'" free(): invalid next size (normal): 0x00000000015bdf90 ***'. Due to these error messages (this one and the one at the end of make) I think the build was not successful. How to solve this problem? Of course I could send you the output and error files. I'd be glad at any help. -MichaelS From brettsalyer at gmail.com Fri Jan 13 13:07:29 2017 From: brettsalyer at gmail.com (brettsalyer at gmail.com) Date: Fri, 13 Jan 2017 10:07:29 -0800 (PST) Subject: Software to Control RGB Strips Message-ID: <739c31a6-bf54-41c9-b5c8-050bbe174ac9@googlegroups.com> Hey all, I was debating on getting some RGB light strips for my room. I noticed, from this tutorial: http://popoklopsi.github.io/RaspberryPi-LedStrip/#!/ws2812 there is a WS2812x library to run commands that control the RGB strip. Well, I wanted to, instead of running commands through a terminal, create a GUI using python, then exploit the library I mentioned above to create a piece of software that controls my lighting from my Raspberry Pi. I was going to use Tkinter to create the GUI. Now, I'm a pretty beginner programmer, having only taken a C++ course and the Java course I'm in now. Do I just need to download the WS2812x library, and, then, access that library as I would, say, a header file in C++? In Pythons syntax, of course. Anything to get me pointed in the right direction would be greatly appreciated! Thanks! From bc at freeuk.com Fri Jan 13 13:07:33 2017 From: bc at freeuk.com (BartC) Date: Fri, 13 Jan 2017 18:07:33 +0000 Subject: Byte-run: a Python interpreter written in Python In-Reply-To: References: <5878b00c$0$1588$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13/01/2017 17:08, Ian Kelly wrote: > On Fri, Jan 13, 2017 at 3:46 AM, Steve D'Aprano > wrote: >> >> http://aosabook.org/en/500L/a-python-interpreter-written-in-python.html > > Neat. But not really surprising IMO that it can fit into 500 lines, If there are still 120 or so byte-codes, then that's just 4 lines on average to implement each byte-code, which is pretty good going. Even when it turns out that the actual code on github is 1000 lines rather than 500! Maybe it grew a bit since the 500 lines was quoted. But it's still 8 lines per byte-code. > since it doesn't handle compiling Python into bytecode (which is the > hard part) I disagree that that's the hardest part. But it was probably not very interesting for this project. Implementing the runtime environment, the object system, type-dispatchers, error-handling, symbol tables, garbage collection and all the rest would be more challenging. > and doesn't include libraries. There doesn't seem to be > much purpose to this other than it being a toy project written for a > book. Being educational would be enough of a point. -- Bartc From tomuxiong at gmx.com Fri Jan 13 13:12:25 2017 From: tomuxiong at gmx.com (Thomas Nyberg) Date: Fri, 13 Jan 2017 10:12:25 -0800 Subject: Problem while building Python 3.6 from source. In-Reply-To: References: Message-ID: <2efba555-859e-717a-42fb-8daee5497e4e@gmx.com> On 01/13/2017 10:00 AM, Michael S wrote: > '*** Error in ./python'" free(): invalid next size (normal): > 0x00000000015bdf90 ***'. Are you possibly running out of memory due to the extra concurrent compilations? Cheers, Thomas From rosuav at gmail.com Fri Jan 13 13:19:10 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 14 Jan 2017 05:19:10 +1100 Subject: Problem while building Python 3.6 from source. In-Reply-To: References: Message-ID: On Sat, Jan 14, 2017 at 5:00 AM, Michael S wrote: > Hello, > I am new to this mailing-list and I really don't know whether this > mail should belong to python-dev. Please tell me, if so. Hi and welcome! This kind of thing is best on this list initially. > Unfortunately, I have got the following problem: I wanted to build and > install Python 3.6 from source but did not succeed. > To clarify my situation, I got as an operating system Debian jessie > 8.6 and I used the xz compressed source tarball from > https://www.python.org/downloads/release/python-360/. > Concerning the build dependencies: I just executed: > $ sudo apt-get build-dep python3.4 (since 3.6 and 3.5 did not work). That should be fine; the build dependencies of Python don't tend to change frequently. Jessie shipped with Python 3.4 but nothing newer, so there won't be packages for python3.5 or python3.6. > Then I executed ./configure --enable-optimizations and make -j4 (I got 4 cores). > The output of make ended like: > 'make: *** [profile-opt] Error 2'. That just means that something went wrong. You'd have to scroll up to find the actual cause of the error. > I had redirected the output and error of the configure and make commands via > $ make -j4 &> /home/username/make_output.txt. > Nevertheless I got an error to the console: > '*** Error in ./python'" free(): invalid next size (normal): > 0x00000000015bdf90 ***'. > Due to these error messages (this one and the one at the end of make) > I think the build was not successful. > > How to solve this problem? > > Of course I could send you the output and error files. The first thing I'd do would be to try a non-optimized build. Set your current build tree aside and re-extract into a new directory (that way, when you go back to playing with optimized builds, you don't have to redo the work), and run configure with no arguments. I'd also be inclined to run make with no arguments; there've been issues with parallel builds in enough projects that I've gotten into the habit of "problem? do it the slow way". If that build also fails, scroll up a bit and find where stuff failed. Are you familiar with building programs from source? If not, the best solution might be to post the entire log, but ideally, you should be able to skim through the last part of the log and report the actual problem that's cropping up. ChrisA From rosuav at gmail.com Fri Jan 13 13:24:41 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 14 Jan 2017 05:24:41 +1100 Subject: Software to Control RGB Strips In-Reply-To: <739c31a6-bf54-41c9-b5c8-050bbe174ac9@googlegroups.com> References: <739c31a6-bf54-41c9-b5c8-050bbe174ac9@googlegroups.com> Message-ID: On Sat, Jan 14, 2017 at 5:07 AM, wrote: > Hey all, > > I was debating on getting some RGB light strips for my room. I noticed, from this tutorial: > > http://popoklopsi.github.io/RaspberryPi-LedStrip/#!/ws2812 > > > there is a WS2812x library to run commands that control the RGB strip. > > Do I just need to download the WS2812x library, and, then, access that library as I would, say, a header file in C++? In Pythons syntax, of course. > > Anything to get me pointed in the right direction would be greatly appreciated! Thanks! Pretty much, yeah. The Python syntax to do that is probably: import ws2812 although a glance at the examples suggests that the package might be called "neopixel" instead, which is a bit surprising. You may be able to skip the download and compilation steps and just run this: python3 -m pip install rpi_ws281x I don't know anything about the library itself, but that's the normal way to install more Python packages. You can find a full list of packages you can install that way here: https://pypi.python.org/pypi There's a lot of them :) Enjoy! ChrisA From ian.g.kelly at gmail.com Fri Jan 13 13:47:31 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 13 Jan 2017 11:47:31 -0700 Subject: Byte-run: a Python interpreter written in Python In-Reply-To: References: <5878b00c$0$1588$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Jan 13, 2017 at 11:07 AM, BartC wrote: > Even when it turns out that the actual code on github is 1000 lines rather > than 500! Maybe it grew a bit since the 500 lines was quoted. I assume they're excluding blank lines, comments and docstrings. And I don't know whether the 500 lines is a hard limit or if there is a bit of leeway there. From bc at freeuk.com Fri Jan 13 14:25:00 2017 From: bc at freeuk.com (BartC) Date: Fri, 13 Jan 2017 19:25:00 +0000 Subject: Byte-run: a Python interpreter written in Python In-Reply-To: References: <5878b00c$0$1588$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13/01/2017 18:47, Ian Kelly wrote: > On Fri, Jan 13, 2017 at 11:07 AM, BartC wrote: >> Even when it turns out that the actual code on github is 1000 lines rather >> than 500! Maybe it grew a bit since the 500 lines was quoted. > > I assume they're excluding blank lines, comments and docstrings. And I > don't know whether the 500 lines is a hard limit or if there is a bit > of leeway there. > https://github.com/nedbat/byterun/blob/master/byterun/pyvm2.py github reports 869 sloc from a total of 1044 lines. But 1000 lines is still small compared with the 220,000 lines of CPython (and that was an old version) which is only the C code and nothing else. If someone wants to experiment with the behaviour of a byte-code, they might be able to do with this project (I haven't tried), more easily than with the real thing. -- Bartc From ned at nedbatchelder.com Fri Jan 13 14:57:32 2017 From: ned at nedbatchelder.com (Ned Batchelder) Date: Fri, 13 Jan 2017 11:57:32 -0800 (PST) Subject: Byte-run: a Python interpreter written in Python In-Reply-To: References: <5878b00c$0$1588$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Friday, January 13, 2017 at 12:09:52 PM UTC-5, Ian wrote: > On Fri, Jan 13, 2017 at 3:46 AM, Steve D'Aprano > wrote: > > > > http://aosabook.org/en/500L/a-python-interpreter-written-in-python.html > > Neat. But not really surprising IMO that it can fit into 500 lines, > since it doesn't handle compiling Python into bytecode (which is the > hard part) and doesn't include libraries. There doesn't seem to be > much purpose to this other than it being a toy project written for a > book. I can tell you what its purpose was: it was to verify my understanding of the execution semantics of Python bytecode. At the time, coverage.py analyzed code for possible branches by reading the bytecode. There are some very tricky bytecodes, and I wasn't sure that I understood what they did. I figured that if I could implement a Python VM, then it would prove that I understood it, or would shine a light on my misconceptions. I thought perhaps someone had done it already. I found Paul Swartz's vm2 code, which was a very similar idea. I refactored it, polished it up, and extended it, and the result was byterun. It served its purpose: although it didn't execute everything properly, there were some twisty bits made clearer by the exercise. Post-Script: coverage.py no longer uses bytecode analysis, it uses AST analysis, which works much better. Since we're talking about possible purposes: Paul Swartz's original goal was to sandbox Python execution. I'm not sure that was a realistic goal for code like this, but that was his goal. --Ned. From darcy at VybeNetworks.com Fri Jan 13 17:24:27 2017 From: darcy at VybeNetworks.com (D'Arcy Cain) Date: Fri, 13 Jan 2017 17:24:27 -0500 Subject: Extended ASCII Message-ID: <20aa3bc3-b26d-aa3d-2265-8e3c8d7bdf65@VybeNetworks.com> I thought I was done with this crap once I moved to 3.x but some Winblows machines are still sending what some circles call "Extended ASCII". I have a file that I am trying to read and it is barfing on some characters. For example: due to the Qu\xe9bec government Obviously should be "due to the Qu?bec government". I can't figure out what that encoding is or if it is anything that can even be understood outside of M$. I have tried ascii, cp437, cp858, cp1140, cp1250, latin-1, utf8 and others. None of them recognize that character. Can someone tell me what encoding includes that character please. Here is the failing code: with open(sys.argv[1], encoding="latin-1") as fp: for ln in fp: print(ln) Traceback (most recent call last): File "./load_iff", line 11, in print(ln) UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 132: ordinal not in range(128) I don't understand why the error says "ascii" when I told it to use "latin-1". -- D'Arcy J.M. Cain Vybe Networks Inc. http://www.VybeNetworks.com/ IM:darcy at Vex.Net VoIP: sip:darcy at VybeNetworks.com From daviddschool at gmail.com Fri Jan 13 17:26:54 2017 From: daviddschool at gmail.com (daviddschool at gmail.com) Date: Fri, 13 Jan 2017 14:26:54 -0800 (PST) Subject: How can I make a sentinel value NOT be initialized in a class/method - OOP? Message-ID: I am testing out some basic Object Oriented Programming in Python. The basics: -User enters a name -While loop with a sentinel value of "quit" will continue entering names until the sentinel value is reached -The object is created with the inputted value (NAME and until a sentinel value is entered) -the object is then printed out using the constructor __str__ In my solution ( I am saying this because someone might have a work around that doesn't include the following below) -I want to use __init__ -I want to use __str__ -I want to use a while loop with a sentinel value EXPLANATION: I want to have the user enter in their NAME using a while loop, with "quit" being the sentinel value that breaks the loop. Once the name is entered, an object is created and it is passed into a constructor def __ini__ (self, name) and then I have them return a string value using __str__ for the name entered. The issue I am having is that when i enter the sentinel value of QUIT, it gets initialized as the name and printed out. How can I get around this? I hope this makes sense. From random832 at fastmail.com Fri Jan 13 17:36:11 2017 From: random832 at fastmail.com (Random832) Date: Fri, 13 Jan 2017 17:36:11 -0500 Subject: Extended ASCII In-Reply-To: <20aa3bc3-b26d-aa3d-2265-8e3c8d7bdf65@VybeNetworks.com> References: <20aa3bc3-b26d-aa3d-2265-8e3c8d7bdf65@VybeNetworks.com> Message-ID: <1484346971.3140184.847206424.09B816FC@webmail.messagingengine.com> On Fri, Jan 13, 2017, at 17:24, D'Arcy Cain wrote: > I thought I was done with this crap once I moved to 3.x but some > Winblows machines are still sending what some circles call "Extended > ASCII". I have a file that I am trying to read and it is barfing on > some characters. For example: > > due to the Qu\xe9bec government > > Obviously should be "due to the Qu?bec government". I can't figure out > what that encoding is or if it is anything that can even be understood > outside of M$. I have tried ascii, cp437, cp858, cp1140, cp1250, > latin-1, utf8 and others. None of them recognize that character. Can > someone tell me what encoding includes that character please. It's latin-1 (or possibly cp1252 or something else depending on other characters), your problem is elsewhere. > Here is the failing code: > > with open(sys.argv[1], encoding="latin-1") as fp: > for ln in fp: > print(ln) > > Traceback (most recent call last): > File "./load_iff", line 11, in > print(ln) > UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in > position 132: ordinal not in range(128) Note that this is an encode error - it's converting *from* unicode *to* bytes, for the print statement. > I don't understand why the error says "ascii" when I told it to use > "latin-1". You set the encoding for the file, not the output. The problem is in your print statement, and the fact that you probably have your locale set to "C" or not set up at all instead of e.g. "en_CA.UTF-8". From jon+usenet at unequivocal.eu Fri Jan 13 17:43:24 2017 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Fri, 13 Jan 2017 22:43:24 -0000 (UTC) Subject: Extended ASCII References: <20aa3bc3-b26d-aa3d-2265-8e3c8d7bdf65@VybeNetworks.com> Message-ID: On 2017-01-13, D'Arcy Cain wrote: > I thought I was done with this crap once I moved to 3.x but some > Winblows machines are still sending what some circles call "Extended > ASCII". I have a file that I am trying to read and it is barfing on > some characters. For example: > > due to the Qu\xe9bec government > > Obviously should be "due to the Qu?bec government". I can't figure out > what that encoding is or if it is anything that can even be understood > outside of M$. $ cat decode.py #!/usr/bin/env python3 CODECS = ( "ascii", "big5", "big5hkscs", "cp037", "cp273", "cp424", "cp437", "cp500", "cp720", "cp737", "cp775", "cp850", "cp852", "cp855", "cp856", "cp857", "cp858", "cp860", "cp861", "cp862", "cp863", "cp864", "cp865", "cp866", "cp869", "cp874", "cp875", "cp932", "cp949", "cp950", "cp1006", "cp1026", "cp1125", "cp1140", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258", "cp65001", "euc_jp", "euc_jis_2004", "euc_jisx0213", "euc_kr", "gb2312", "gbk", "gb18030", "hz", "iso2022_jp", "iso2022_jp_1", "iso2022_jp_2", "iso2022_jp_2004", "iso2022_jp_3", "iso2022_jp_ext", "iso2022_kr", "latin_1", "iso8859_2", "iso8859_3", "iso8859_4", "iso8859_5", "iso8859_6", "iso8859_7", "iso8859_8", "iso8859_9", "iso8859_10", "iso8859_11", "iso8859_13", "iso8859_14", "iso8859_15", "iso8859_16", "johab", "koi8_r", "koi8_t", "koi8_u", "kz1048", "mac_cyrillic", "mac_greek", "mac_iceland", "mac_latin2", "mac_roman", "mac_turkish", "ptcp154", "shift_jis", "shift_jis_2004", "shift_jisx0213", "utf_32", "utf_32_be", "utf_32_le", "utf_16", "utf_16_be", "utf_16_le", "utf_7", "utf_8", "utf_8_sig", ) for encoding in CODECS: try: if b"Qu\xe9bec".decode(encoding) == "Qu?bec": print(encoding) except (UnicodeError, LookupError): pass $ ./decode.py cp1250 cp1252 cp1254 cp1256 cp1257 cp1258 latin_1 iso8859_2 iso8859_3 iso8859_4 iso8859_9 iso8859_10 iso8859_13 iso8859_14 iso8859_15 iso8859_16 From grant.b.edwards at gmail.com Fri Jan 13 17:44:09 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Fri, 13 Jan 2017 22:44:09 +0000 (UTC) Subject: Extended ASCII References: <20aa3bc3-b26d-aa3d-2265-8e3c8d7bdf65@VybeNetworks.com> Message-ID: On 2017-01-13, D'Arcy Cain wrote: > Here is the failing code: > > with open(sys.argv[1], encoding="latin-1") as fp: > for ln in fp: > print(ln) > > Traceback (most recent call last): > File "./load_iff", line 11, in > print(ln) > UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in > position 132: ordinal not in range(128) > > I don't understand why the error says "ascii" when I told it to use > "latin-1". That can't be the failing code, since it's failing at line 11, and that's only 5 lines. It helps if we can tell which line generated the error. ;) I'm _guessing_ that line 11 is the print(), and it's barfing because stdout is using ascii encoding, and there's no way to encode that character in ascii so that it can be printed to an ascii output stream. -- Grant Edwards grant.b.edwards Yow! Everybody gets free at BORSCHT! gmail.com From python at lucidity.plus.com Fri Jan 13 18:32:51 2017 From: python at lucidity.plus.com (Erik) Date: Fri, 13 Jan 2017 23:32:51 +0000 Subject: How can I make a sentinel value NOT be initialized in a class/method - OOP? In-Reply-To: References: Message-ID: <401cc9ce-824f-6cc5-c92e-c397430d11d4@lucidity.plus.com> Hi, On 13/01/17 22:26, daviddschool at gmail.com wrote: > The issue I am having is that when i enter the sentinel value of QUIT, it gets initialized as the name and printed out. How can I get around this? If I understand the question correctly (which looks like it's just a re-worded homework question (*)), you need to look at the 'if' statement: https://docs.python.org/3/tutorial/controlflow.html#if-statements E. (*) If it is a homework question, you'd look better on the list to state it as such, and post some code that at least tries to answer the question before you're likely to get a useful response. However, as you have indicated a specific problem, I'll assume you have actually written some code. From oliver.schoenborn at gmail.com Fri Jan 13 18:38:28 2017 From: oliver.schoenborn at gmail.com (oliver) Date: Fri, 13 Jan 2017 23:38:28 +0000 Subject: Announcement: TLSv1.2 will become mandatory in the future for Python.org Sites In-Reply-To: References: <1B1B29CD-AA4E-47C0-89A8-2748D5A14FC3@python.org> Message-ID: When I run this per email from my work laptop, python3 -c "import urllib.request,json; print(json.loads(urllib.request.urlopen(' https://www.howsmyssl.com/a/check').read())['tls_version'])" I get the following traceback: C:\...>python -c "import urllib.request,json; print(json.loads(urllib.request.url w.howsmyssl.com/a/check').read())['tls_version'])" Traceback (most recent call last): File "c:\Python35\lib\urllib\request.py", line 1254, in do_open h.request(req.get_method(), req.selector, req.data, headers) File "c:\Python35\lib\http\client.py", line 1106, in request self._send_request(method, url, body, headers) File "c:\Python35\lib\http\client.py", line 1151, in _send_request self.endheaders(body) File "c:\Python35\lib\http\client.py", line 1102, in endheaders self._send_output(message_body) File "c:\Python35\lib\http\client.py", line 934, in _send_output self.send(msg) File "c:\Python35\lib\http\client.py", line 877, in send self.connect() File "c:\Python35\lib\http\client.py", line 1260, in connect server_hostname=server_hostname) File "c:\Python35\lib\ssl.py", line 377, in wrap_socket _context=self) File "c:\Python35\lib\ssl.py", line 752, in __init__ self.do_handshake() File "c:\Python35\lib\ssl.py", line 988, in do_handshake self._sslobj.do_handshake() File "c:\Python35\lib\ssl.py", line 633, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "c:\Python35\lib\urllib\request.py", line 163, in urlopen return opener.open(url, data, timeout) File "c:\Python35\lib\urllib\request.py", line 466, in open response = self._open(req, data) File "c:\Python35\lib\urllib\request.py", line 484, in _open '_open', req) File "c:\Python35\lib\urllib\request.py", line 444, in _call_chain result = func(*args) File "c:\Python35\lib\urllib\request.py", line 1297, in https_open context=self._context, check_hostname=self._check_hostname) File "c:\Python35\lib\urllib\request.py", line 1256, in do_open raise URLError(err) urllib.error.URLError: Anyone know how to deal with that? When using pip, I get same error, unless I add "--trusted-host pypi.python.org": C:\...>pip install nose Collecting nose Could not fetch URL https://pypi.python.org/simple/nose/: There was a problem confirming the ssl certificate: [SSL: CERTIF LED] certificate verify failed (_ssl.c:645) - skipping Could not find a version that satisfies the requirement nose (from versions: ) No matching distribution found for nose C:\...>pip install nose --trusted-host pypi.python.org Collecting nose Downloading nose-1.3.7-py3-none-any.whl (154kB) 100% |################################| 163kB 386kB/s Installing collected packages: nose Successfully installed nose-1.3.7 -- Oliver -- Oliver My StackOverflow contributions My CodeProject articles My Github projects My SourceForget.net projects From ian.g.kelly at gmail.com Fri Jan 13 18:52:55 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 13 Jan 2017 16:52:55 -0700 Subject: How can I make a sentinel value NOT be initialized in a class/method - OOP? In-Reply-To: References: Message-ID: On Jan 13, 2017 3:33 PM, wrote: The issue I am having is that when i enter the sentinel value of QUIT, it gets initialized as the name and printed out. How can I get around this? I hope this makes sense. Hard to say for certain without seeing your code, but the most likely cause of this is that the input string contains a trailing newline that causes it to differ from the sentinel value you're comparing it to. Consider using the rstrip string method to remove any trailing whitespace. From hba008 at aol.com Fri Jan 13 19:02:53 2017 From: hba008 at aol.com (=?utf-8?B?aGJhMDA4QGFvbC5jb20=?=) Date: Sat, 14 Jan 2017 00:02:53 GMT Subject: Can not run the Python software Message-ID: <000f4242.04d2cee60ab85b00@aol.com> I have been added to the mailing list per your instructions. Please, have someone address the problem below....Thanks Sent from my Sprint Phone. ------ Original message------From: Date: Thu, Jan 5, 2017 10:13 PMTo: python-list at python.org;Subject:Can not run the Python software Hi,?Just downloaded Python 3.6.0 2016-12-23 and PyCharm. Tried to run the "Hello World" program and got the following message:"Process finished with exit code 1073741515 (0xC0000135)" I am using Windows 8.1 on an HP ENVY Touchsmart Notebook (64-bit OS, x64-based processor).Help Needed....Thanks,Bernard From darcy at vex.net Fri Jan 13 19:30:58 2017 From: darcy at vex.net (D'Arcy Cain) Date: Fri, 13 Jan 2017 19:30:58 -0500 Subject: Extended ASCII [solved] In-Reply-To: References: <20aa3bc3-b26d-aa3d-2265-8e3c8d7bdf65@VybeNetworks.com> Message-ID: On 2017-01-13 05:44 PM, Grant Edwards wrote: > On 2017-01-13, D'Arcy Cain wrote: > >> Here is the failing code: >> >> with open(sys.argv[1], encoding="latin-1") as fp: >> for ln in fp: >> print(ln) >> >> Traceback (most recent call last): >> File "./load_iff", line 11, in >> print(ln) >> UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in >> position 132: ordinal not in range(128) >> >> I don't understand why the error says "ascii" when I told it to use >> "latin-1". > > That can't be the failing code, since it's failing at line 11, and > that's only 5 lines. It helps if we can tell which line generated the > error. ;) I didn't think that the part leading up to it was relevant. Here it is. #! /usr/bin/python import sys, os if len(sys.argv) < 2: print("No file named", file=sys.stderr) sys.exit(1) > I'm _guessing_ that line 11 is the print(), and it's barfing because Of course. That's why the error is listed right below it. > stdout is using ascii encoding, and there's no way to encode that > character in ascii so that it can be printed to an ascii output > stream. Thank you! I know all about that but for some reason I did not have PYTHONIOENCODING=utf8 set on that particular machine. I have it set on every other machine I work on an never thought to check. My Unicode universe is all right again. Cheers. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From sohcahtoa82 at gmail.com Fri Jan 13 20:02:30 2017 From: sohcahtoa82 at gmail.com (sohcahtoa82 at gmail.com) Date: Fri, 13 Jan 2017 17:02:30 -0800 (PST) Subject: How can I make a sentinel value NOT be initialized in a class/method - OOP? In-Reply-To: References: Message-ID: <9e40483f-59e4-4e7a-a038-552f941e6927@googlegroups.com> On Friday, January 13, 2017 at 2:27:04 PM UTC-8, David D wrote: > I am testing out some basic Object Oriented Programming in Python. The basics: > > -User enters a name > -While loop with a sentinel value of "quit" will continue entering names until the sentinel value is reached > -The object is created with the inputted value (NAME and until a sentinel value is entered) > -the object is then printed out using the constructor __str__ > > In my solution ( I am saying this because someone might have a work around that doesn't include the following below) > -I want to use __init__ > -I want to use __str__ > -I want to use a while loop with a sentinel value > > EXPLANATION: > I want to have the user enter in their NAME using a while loop, with "quit" being the sentinel value that breaks the loop. Once the name is entered, an object is created and it is passed into a constructor def __ini__ (self, name) and then I have them return a string value using __str__ for the name entered. > The issue I am having is that when i enter the sentinel value of QUIT, it gets initialized as the name and printed out. How can I get around this? I hope this makes sense. It would help if you posted your code to see what you're doing. Also, is the sentinel value supposed to be "quit" or "QUIT"? If you're only checking for "quit", but you type "QUIT", it isn't going to work. Programs execute exactly as you write them, and "quit" is not the same as "QUIT". From hba008 at aol.com Fri Jan 13 20:34:23 2017 From: hba008 at aol.com (hba008 at aol.com) Date: Fri, 13 Jan 2017 20:34:23 -0500 Subject: Can not run the Python software In-Reply-To: <000f4242.04d2cee60ab85b00@aol.com> References: <000f4242.04d2cee60ab85b00@aol.com> Message-ID: <1599a9c7b49-1232-d4f@webstg-m06.mail.aol.com> -----Original Message----- From: hba008 To: hba008 ; python-list Sent: Fri, Jan 13, 2017 7:02 pm Subject: Re: Can not run the Python software I have been added to the mailing list per your instructions. Please, have someone address the problem below....Thanks Sent from my Sprint Phone. ------ Original message------ From: Date: Thu, Jan 5, 2017 10:13 PM To: python-list at python.org; Subject:Can not run the Python software Hi, Just downloaded Python 3.6.0 2016-12-23 and PyCharm. Tried to run the "Hello World" program and got the following message: "Process finished with exit code 1073741515 (0xC0000135)" I am using Windows 8.1 on an HP ENVY Touchsmart Notebook (64-bit OS, x64-based processor). Help Needed.... Thanks, Bernard From torriem at gmail.com Fri Jan 13 22:01:21 2017 From: torriem at gmail.com (Michael Torrie) Date: Fri, 13 Jan 2017 20:01:21 -0700 Subject: Can not run the Python software In-Reply-To: <1599a9c7b49-1232-d4f@webstg-m06.mail.aol.com> References: <000f4242.04d2cee60ab85b00@aol.com> <1599a9c7b49-1232-d4f@webstg-m06.mail.aol.com> Message-ID: <0a2d8b37-0c5e-b349-6d6c-0bcf05dcdfe6@gmail.com> On 01/13/2017 06:34 PM, Bernard via Python-list wrote: > Hi, > > Just downloaded Python 3.6.0 2016-12-23 and PyCharm. Tried to run the "Hello World" program and got the following message: > "Process finished with exit code 1073741515 (0xC0000135)" > I am using Windows 8.1 on an HP ENVY Touchsmart Notebook (64-bit OS, x64-based processor). > Help Needed.... Unfortunately there's not a lot of information there to go on, which is why you haven't got any replies yet. What was the python program you tried to run? How did you run it? The error message you quote does not appear to be a Python error message, so I'm not sure where it is coming from. It's possible the python interpreter itself crashed, which is unusual, especially for a simple program. Can you post the entire python program you were trying to run? Also can you try a simple program of your own such as this as a sanity check? print ("Hello, World.") From brettsalyer at gmail.com Fri Jan 13 22:16:07 2017 From: brettsalyer at gmail.com (Brett Salyer) Date: Fri, 13 Jan 2017 19:16:07 -0800 (PST) Subject: Software to Control RGB Strips In-Reply-To: <739c31a6-bf54-41c9-b5c8-050bbe174ac9@googlegroups.com> References: <739c31a6-bf54-41c9-b5c8-050bbe174ac9@googlegroups.com> Message-ID: <1eae2e6a-3f84-4820-b648-58d23ca39125@googlegroups.com> On Friday, January 13, 2017 at 1:07:40 PM UTC-5, Brett Salyer wrote: > Hey all, > > I was debating on getting some RGB light strips for my room. I noticed, from this tutorial: > > http://popoklopsi.github.io/RaspberryPi-LedStrip/#!/ws2812 > > > there is a WS2812x library to run commands that control the RGB strip. Well, I wanted to, instead of running commands through a terminal, create a GUI using python, then exploit the library I mentioned above to create a piece of software that controls my lighting from my Raspberry Pi. > > I was going to use Tkinter to create the GUI. > > Now, I'm a pretty beginner programmer, having only taken a C++ course and the Java course I'm in now. > > Do I just need to download the WS2812x library, and, then, access that library as I would, say, a header file in C++? In Pythons syntax, of course. > > Anything to get me pointed in the right direction would be greatly appreciated! Thanks! Thanks a lot, man! From jcasale at activenetwerx.com Fri Jan 13 22:32:26 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Sat, 14 Jan 2017 03:32:26 +0000 Subject: Can not run the Python software In-Reply-To: <1599a9c7b49-1232-d4f@webstg-m06.mail.aol.com> References: <000f4242.04d2cee60ab85b00@aol.com> <1599a9c7b49-1232-d4f@webstg-m06.mail.aol.com> Message-ID: > Just downloaded Python 3.6.0 2016-12-23 and PyCharm. Tried to run the "Hello World" program and got the following message: > "Process finished with exit code 1073741515 (0xC0000135)" > I am using Windows 8.1 on an HP ENVY Touchsmart Notebook (64-bit OS, x64-based processor). If you track the error, it indicates a file was not found, bets are you don't have the needed runtime installed for Python to run. Nothing else PyCharm can do here, it tries to start Python and fails. If memory serves me, you need the Visual C++ Redistributable for Visual Studio 2015... jlc From torriem at gmail.com Fri Jan 13 23:08:04 2017 From: torriem at gmail.com (Michael Torrie) Date: Fri, 13 Jan 2017 21:08:04 -0700 Subject: Can not run the Python software In-Reply-To: References: <000f4242.04d2cee60ab85b00@aol.com> <1599a9c7b49-1232-d4f@webstg-m06.mail.aol.com> Message-ID: <4a5c7ae0-b09a-9063-f521-3318c71d769c@gmail.com> On 01/13/2017 08:32 PM, Joseph L. Casale wrote: >> Just downloaded Python 3.6.0 2016-12-23 and PyCharm. Tried to run the "Hello World" program and got the following message: >> "Process finished with exit code 1073741515 (0xC0000135)" >> I am using Windows 8.1 on an HP ENVY Touchsmart Notebook (64-bit OS, x64-based processor). > > If you track the error, it indicates a file was not found, bets are > you don't have the needed runtime installed for Python to run. > > Nothing else PyCharm can do here, it tries to start Python and > fails. > > If memory serves me, you need the Visual C++ Redistributable > for Visual Studio 2015... And this is coming up a lot. This is something that should already be on all supported versions of Windows if Windows updates are done, right? It's probably in the FAQ on python.org, and I know this is really a user problem (not installing updates), but maybe it's time that the Python installer bundles the redistributable installer and installs it if necessary during Python's install. We get queries on the list almost weekly from Windows users. From hba008 at aol.com Fri Jan 13 23:20:30 2017 From: hba008 at aol.com (hba008 at aol.com) Date: Fri, 13 Jan 2017 23:20:30 -0500 Subject: Can not run the Python software In-Reply-To: <4a5c7ae0-b09a-9063-f521-3318c71d769c@gmail.com> Message-ID: <1599b34906a-29af-209a@webstg-m09.mail.aol.com> Thanks for the info.. -----Original Message----- From: Michael Torrie To: python-list Sent: Fri, Jan 13, 2017 11:08 pm Subject: Re: Can not run the Python software On 01/13/2017 08:32 PM, Joseph L. Casale wrote: >> Just downloaded Python 3.6.0 2016-12-23 and PyCharm. Tried to run the "Hello World" program and got the following message: >> "Process finished with exit code 1073741515 (0xC0000135)" >> I am using Windows 8.1 on an HP ENVY Touchsmart Notebook (64-bit OS, x64-based processor). > > If you track the error, it indicates a file was not found, bets are > you don't have the needed runtime installed for Python to run. > > Nothing else PyCharm can do here, it tries to start Python and > fails. > > If memory serves me, you need the Visual C++ Redistributable > for Visual Studio 2015... And this is coming up a lot. This is something that should already be on all supported versions of Windows if Windows updates are done, right? It's probably in the FAQ on python.org, and I know this is really a user problem (not installing updates), but maybe it's time that the Python installer bundles the redistributable installer and installs it if necessary during Python's install. We get queries on the list almost weekly from Windows users. -- https://mail.python.org/mailman/listinfo/python-list From jcasale at activenetwerx.com Fri Jan 13 23:22:43 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Sat, 14 Jan 2017 04:22:43 +0000 Subject: Can not run the Python software In-Reply-To: <4a5c7ae0-b09a-9063-f521-3318c71d769c@gmail.com> References: <000f4242.04d2cee60ab85b00@aol.com> <1599a9c7b49-1232-d4f@webstg-m06.mail.aol.com> <4a5c7ae0-b09a-9063-f521-3318c71d769c@gmail.com> Message-ID: <97dbd7d47c2445a381ecb3a857fb1a95@activenetwerx.com> > And this is coming up a lot. This is something that should already be > on all supported versions of Windows if Windows updates are done, right? No, it's not an update. You install the runtime *if* you need it. > but maybe it's time that the > Python installer bundles the redistributable installer and installs it > if necessary during Python's install. That's exactly how most other software does work and wix for example even has support for it... http://wixtoolset.org/documentation/manual/v3/howtos/redistributables_and_install_checks/ From shahsn11 at gmail.com Fri Jan 13 23:42:47 2017 From: shahsn11 at gmail.com (shahsn11 at gmail.com) Date: Fri, 13 Jan 2017 20:42:47 -0800 (PST) Subject: Python Web Scrapping : Within href readonly those value that have href in it Message-ID: I am trying to scrape a webpage just for learning. In that webpage there are multiple "a" tags. consider the below code Something Something Now i want to read only those href in which there is http. My Current code is for link in soup.find_all("a"): print link.get("href") i would like to change it to read only http links. From dieter at handshake.de Sat Jan 14 03:38:09 2017 From: dieter at handshake.de (dieter) Date: Sat, 14 Jan 2017 09:38:09 +0100 Subject: timers in threaded application References: <87lguf9uu0.fsf@handshake.de> Message-ID: <87o9zahw9a.fsf@handshake.de> Skip Montanaro writes: > On Fri, Jan 13, 2017 at 3:23 AM, dieter wrote: > >> If what you want to timeout are not I/O operations, you can have a >> look at "threading.Timer". It uses a separate thread, lets it wait >> a specified time and then starts a specified function, unless >> "cancel"ed. >> > > Ooh, hadn't considered that. That would seem to do the trick. I assume the > function to be executed will be run in the timer's thread, so will have to > suitably lock any shared data. Yes. From dieter at handshake.de Sat Jan 14 03:43:45 2017 From: dieter at handshake.de (dieter) Date: Sat, 14 Jan 2017 09:43:45 +0100 Subject: Running Virtualenv with a custom distlib? References: <2bc1125c-eb70-49bc-bd90-ebeaa9741258@googlegroups.com> Message-ID: <87k29yhvzy.fsf@handshake.de> haraldnordgren at gmail.com writes: > I want to do some development on `distlib`, and in the process run the code via `virtualenv` which has distlib as a dependency. > > That is, not run the process inside a virtualenv, but run virtualenv's code using a custom dependency. What are the steps I need to go through to achieve this? I would try to run "virtualenv" in a virtual environment with a "distlib" seemingly to fulfill the "virtualenv" requirements but in fact be under your control (e.g. set up via "python setup.py develop"). I do not know whether the virtual environment mentioned above can be set up via "virtualenv" itself (that would be the easiest thing). If not, I would install "python" from source and tweak its "site-packages" as necessary. From __peter__ at web.de Sat Jan 14 03:44:33 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 14 Jan 2017 09:44:33 +0100 Subject: Python Web Scrapping : Within href readonly those value that have href in it References: Message-ID: shahsn11 at gmail.com wrote: > I am trying to scrape a webpage just for learning. In that webpage there > are multiple "a" tags. consider the below code > > Something > > Something These are probaly all forward slashes. > Now i want to read only those href in which there is http. My Current code > is > > for link in soup.find_all("a"): > print link.get("href") > > i would like to change it to read only http links. You mean href values that start with "http://"? While you can do that with a callback def check_scheme(href): return href is not None and href.startswith("http://") for a in soup.find_all("a", href=check_scheme): print(a["href"]) or a regular expression import re for a in soup.find_all("a", href=re.compile("^http://")): print(a["href"]) why not keep things simple and check before printing? Like for a in soup.find_all("a"): href = a.get("href", "") # empty string if href is missing if href.startswith("http://"): print(href) From dieter at handshake.de Sat Jan 14 04:03:26 2017 From: dieter at handshake.de (dieter) Date: Sat, 14 Jan 2017 10:03:26 +0100 Subject: Announcement: TLSv1.2 will become mandatory in the future for Python.org Sites References: <1B1B29CD-AA4E-47C0-89A8-2748D5A14FC3@python.org> Message-ID: <87fukmhv35.fsf@handshake.de> oliver writes: > When I run this per email from my work laptop, > > python3 -c "import urllib.request,json; > print(json.loads(urllib.request.urlopen(' > https://www.howsmyssl.com/a/check').read())['tls_version'])" > > I get the following traceback: > ... > File "c:\Python35\lib\ssl.py", line 633, in do_handshake > self._sslobj.do_handshake() > ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed > (_ssl.c:645) I guess (!) that somehow the well known trusted CA (= "Certificate authority") certificates are incomplete on your machine. Certificate verification works as follows: a certificate is always signed by a certificate authority ("CA"); for a certificate to be trusted, the signing CA must be trusted. There may be several trust steps but finally, there must be some "CA" that you are trusting "without further proof". The certificates of those "CA"s are somewhere stored on your machine. Apparently, the "https" servers you have problems with are using a CA which is not declared trusted on your machine (by installing the appropriate certificate at the correct place). From saxri89 at gmail.com Sat Jan 14 06:18:33 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 03:18:33 -0800 (PST) Subject: geopandas bug ? Message-ID: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> i want to create a simple spatial joing using geopandas but i thing so geopandas has bug ? geopandas code : from geopandas import gpd import geopandas points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc polys = geopandas.GeoDataFrame.from_file('polygons.shp') pointInPoly = gpd.sjoin(points, polys, how='left',op='within') error : Traceback (most recent call last): File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", line 7, in pointInPoly = gpd.sjoin(points, polys, how='left',op='within') File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", line 57, in sjoin r_idx = np.concatenate(idxmatch.values) ValueError: need at least one array to concatenate and if i change the imports with the some code : import geopandas import pandas as pd import geopandas as gpd from geopandas import GeoDataFrame, read_file from geopandas.tools import sjoin from shapely.geometry import Point, mapping,shape import pandas as gpd i take that error : pointInPoly = gpd.sjoin(points, polys, how='left',op='within') AttributeError: 'module' object has no attribute 'sjoin' any idea why ? From saxri89 at gmail.com Sat Jan 14 06:20:24 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 03:20:24 -0800 (PST) Subject: geopandas bug ? Message-ID: i want to create a simple spatial joing using geopandas but i thing so geopandas has bug ? geopandas code : from geopandas import gpd import geopandas points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc polys = geopandas.GeoDataFrame.from_file('polygons.shp') pointInPoly = gpd.sjoin(points, polys, how='left',op='within') error : Traceback (most recent call last): File "/home/username/testshapely/sumpointsinsidepolygon/testgeo.py", line 7, in pointInPoly = gpd.sjoin(points, polys, how='left',op='within') File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", line 57, in sjoin r_idx = np.concatenate(idxmatch.values) ValueError: need at least one array to concatenate and if i change the imports with the some code : import geopandas import pandas as pd import geopandas as gpd from geopandas import GeoDataFrame, read_file from geopandas.tools import sjoin from shapely.geometry import Point, mapping,shape import pandas as gpd i take that error : pointInPoly = gpd.sjoin(points, polys, how='left',op='within') AttributeError: 'module' object has no attribute 'sjoin' any idea why ? From __peter__ at web.de Sat Jan 14 08:42:23 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 14 Jan 2017 14:42:23 +0100 Subject: geopandas bug ? References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: Xristos Xristoou wrote: > i want to create a simple spatial joing using geopandas but i thing so > geopandas has bug ? Have you tried the examples on ? Do they work? If yes, inspect your data, does it have the same format? > geopandas code : > > from geopandas import gpd > import geopandas > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc > polys = geopandas.GeoDataFrame.from_file('polygons.shp') > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > > error : > > Traceback (most recent call last): > File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", > line 7, in > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", > line 57, in sjoin > r_idx = np.concatenate(idxmatch.values) > ValueError: need at least one array to concatenate > > and if i change the imports with the some code : > > import geopandas > import pandas as pd > import geopandas as gpd > from geopandas import GeoDataFrame, read_file > from geopandas.tools import sjoin > from shapely.geometry import Point, mapping,shape > import pandas as gpd > > i take that error : > > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > AttributeError: 'module' object has no attribute 'sjoin' > > > any idea why ? My crystal ball says that either points or polys is empty ;) From saxri89 at gmail.com Sat Jan 14 09:06:15 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 06:06:15 -0800 (PST) Subject: geopandas bug ? In-Reply-To: References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: ?? ???????, 14 ?????????? 2017 - 3:43:10 ?.?. UTC+2, ? ??????? Peter Otten ??????: > Xristos Xristoou wrote: > > > i want to create a simple spatial joing using geopandas but i thing so > > geopandas has bug ? > > Have you tried the examples on ? > Do they work? If yes, inspect your data, does it have the same format? > > > geopandas code : > > > > from geopandas import gpd > > import geopandas > > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc > > polys = geopandas.GeoDataFrame.from_file('polygons.shp') > > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > > > > error : > > > > Traceback (most recent call last): > > File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", > > line 7, in > > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > > File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", > > line 57, in sjoin > > r_idx = np.concatenate(idxmatch.values) > > ValueError: need at least one array to concatenate > > > > and if i change the imports with the some code : > > > > import geopandas > > import pandas as pd > > import geopandas as gpd > > from geopandas import GeoDataFrame, read_file > > from geopandas.tools import sjoin > > from shapely.geometry import Point, mapping,shape > > import pandas as gpd > > > > i take that error : > > > > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > > AttributeError: 'module' object has no attribute 'sjoin' > > > > > > any idea why ? > > My crystal ball says that either points or polys is empty ;) is not empty and yes i have two shapefiles from qgis.what is the error ? From __peter__ at web.de Sat Jan 14 09:30:05 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 14 Jan 2017 15:30:05 +0100 Subject: geopandas bug ? References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: Xristos Xristoou wrote: > ?? ???????, 14 ?????????? 2017 - 3:43:10 ?.?. UTC+2, ? ??????? Peter Otten > ??????: >> Xristos Xristoou wrote: >> >> > i want to create a simple spatial joing using geopandas but i thing so >> > geopandas has bug ? >> >> Have you tried the examples on ? >> Do they work? If yes, inspect your data, does it have the same format? Looks like you chose to ignore the hard part. >> > geopandas code : >> > >> > from geopandas import gpd >> > import geopandas >> > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson >> > etc polys = geopandas.GeoDataFrame.from_file('polygons.shp') >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') >> > >> > error : >> > >> > Traceback (most recent call last): >> > File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", >> > line 7, in >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') >> > File >> > "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", >> > line 57, in sjoin >> > r_idx = np.concatenate(idxmatch.values) >> > ValueError: need at least one array to concatenate >> > any idea why ? >> >> My crystal ball says that either points or polys is empty ;) > > is not empty and yes i have two shapefiles from qgis. Can I download those files somewhere? > what is the error ? No idea. Without those files I cannot run your code. From saxri89 at gmail.com Sat Jan 14 09:37:08 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 06:37:08 -0800 (PST) Subject: geopandas bug ? In-Reply-To: References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: <9a293ac7-d306-4c8b-9f7e-f5a0de377169@googlegroups.com> ?? ???????, 14 ?????????? 2017 - 4:30:48 ?.?. UTC+2, ? ??????? Peter Otten ??????: > Xristos Xristoou wrote: > > > ?? ???????, 14 ?????????? 2017 - 3:43:10 ?.?. UTC+2, ? ??????? Peter Otten > > ??????: > >> Xristos Xristoou wrote: > >> > >> > i want to create a simple spatial joing using geopandas but i thing so > >> > geopandas has bug ? > >> > >> Have you tried the examples on ? > >> Do they work? If yes, inspect your data, does it have the same format? > > Looks like you chose to ignore the hard part. > > >> > geopandas code : > >> > > >> > from geopandas import gpd > >> > import geopandas > >> > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson > >> > etc polys = geopandas.GeoDataFrame.from_file('polygons.shp') > >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >> > > >> > error : > >> > > >> > Traceback (most recent call last): > >> > File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", > >> > line 7, in > >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >> > File > >> > "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", > >> > line 57, in sjoin > >> > r_idx = np.concatenate(idxmatch.values) > >> > ValueError: need at least one array to concatenate > > >> > any idea why ? > >> > >> My crystal ball says that either points or polys is empty ;) > > > > is not empty and yes i have two shapefiles from qgis. > > Can I download those files somewhere? > > > what is the error ? > > No idea. Without those files I cannot run your code. yes i sent you From duncan at invalid.invalid Sat Jan 14 09:38:29 2017 From: duncan at invalid.invalid (duncan smith) Date: Sat, 14 Jan 2017 14:38:29 +0000 Subject: geopandas bug ? In-Reply-To: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: On 14/01/17 11:18, Xristos Xristoou wrote: > i want to create a simple spatial joing using geopandas but i thing so geopandas has bug ? > > > > geopandas code : > > from geopandas import gpd > import geopandas > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc > polys = geopandas.GeoDataFrame.from_file('polygons.shp') > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > > error : > > Traceback (most recent call last): > File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", line 7, in > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", line 57, in sjoin > r_idx = np.concatenate(idxmatch.values) > ValueError: need at least one array to concatenate > > and if i change the imports with the some code : > > import geopandas > import pandas as pd > import geopandas as gpd > from geopandas import GeoDataFrame, read_file > from geopandas.tools import sjoin > from shapely.geometry import Point, mapping,shape > import pandas as gpd > > i take that error : > > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > AttributeError: 'module' object has no attribute 'sjoin' > > > any idea why ? > import geopandas as gpd import pandas as gpd Does pandas have an attribute 'sjoin'? Duncan From saxri89 at gmail.com Sat Jan 14 09:58:59 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 06:58:59 -0800 (PST) Subject: geopandas bug ? In-Reply-To: References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: ?? ???????, 14 ?????????? 2017 - 4:30:48 ?.?. UTC+2, ? ??????? Peter Otten ??????: > Xristos Xristoou wrote: > > > ?? ???????, 14 ?????????? 2017 - 3:43:10 ?.?. UTC+2, ? ??????? Peter Otten > > ??????: > >> Xristos Xristoou wrote: > >> > >> > i want to create a simple spatial joing using geopandas but i thing so > >> > geopandas has bug ? > >> > >> Have you tried the examples on ? > >> Do they work? If yes, inspect your data, does it have the same format? > > Looks like you chose to ignore the hard part. > > >> > geopandas code : > >> > > >> > from geopandas import gpd > >> > import geopandas > >> > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson > >> > etc polys = geopandas.GeoDataFrame.from_file('polygons.shp') > >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >> > > >> > error : > >> > > >> > Traceback (most recent call last): > >> > File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", > >> > line 7, in > >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >> > File > >> > "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", > >> > line 57, in sjoin > >> > r_idx = np.concatenate(idxmatch.values) > >> > ValueError: need at least one array to concatenate > > >> > any idea why ? > >> > >> My crystal ball says that either points or polys is empty ;) > > > > is not empty and yes i have two shapefiles from qgis. > > Can I download those files somewhere? > > > what is the error ? > > No idea. Without those files I cannot run your code. https://www.dropbox.com/s/2693nfi248z0y9q/files.zip?dl=0 with the shapefiles From saxri89 at gmail.com Sat Jan 14 09:59:55 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 06:59:55 -0800 (PST) Subject: geopandas bug ? In-Reply-To: References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: ?? ???????, 14 ?????????? 2017 - 4:38:39 ?.?. UTC+2, ? ??????? duncan smith ??????: > On 14/01/17 11:18, Xristos Xristoou wrote: > > i want to create a simple spatial joing using geopandas but i thing so geopandas has bug ? > > > > > > > > geopandas code : > > > > from geopandas import gpd > > import geopandas > > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc > > polys = geopandas.GeoDataFrame.from_file('polygons.shp') > > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > > > > error : > > > > Traceback (most recent call last): > > File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", line 7, in > > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > > File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", line 57, in sjoin > > r_idx = np.concatenate(idxmatch.values) > > ValueError: need at least one array to concatenate > > > > and if i change the imports with the some code : > > > > import geopandas > > import pandas as pd > > import geopandas as gpd > > from geopandas import GeoDataFrame, read_file > > from geopandas.tools import sjoin > > from shapely.geometry import Point, mapping,shape > > import pandas as gpd > > > > i take that error : > > > > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > > AttributeError: 'module' object has no attribute 'sjoin' > > > > > > any idea why ? > > > > > import geopandas as gpd > import pandas as gpd > > Does pandas have an attribute 'sjoin'? > > Duncan i dont know i follow detais i am newbie From __peter__ at web.de Sat Jan 14 11:00:41 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 14 Jan 2017 17:00:41 +0100 Subject: geopandas bug ? References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: Xristos Xristoou wrote: > ?? ???????, 14 ?????????? 2017 - 4:30:48 ?.?. UTC+2, ? ??????? Peter Otten > ??????: >> Xristos Xristoou wrote: >> >> > ?? ???????, 14 ?????????? 2017 - 3:43:10 ?.?. UTC+2, ? ??????? Peter >> > Otten ??????: >> >> Xristos Xristoou wrote: >> >> >> >> > i want to create a simple spatial joing using geopandas but i thing >> >> > so geopandas has bug ? >> >> >> >> Have you tried the examples on >> >> ? Do they work? If yes, inspect >> >> your data, does it have the same format? >> >> Looks like you chose to ignore the hard part. >> >> >> > geopandas code : >> >> > >> >> > from geopandas import gpd >> >> > import geopandas >> >> > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson >> >> > etc polys = geopandas.GeoDataFrame.from_file('polygons.shp') >> >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') >> >> > >> >> > error : >> >> > >> >> > Traceback (most recent call last): >> >> > File >> >> > "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", >> >> > line 7, in >> >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') >> >> > File >> >> > "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", >> >> > line 57, in sjoin >> >> > r_idx = np.concatenate(idxmatch.values) >> >> > ValueError: need at least one array to concatenate >> >> >> > any idea why ? >> >> >> >> My crystal ball says that either points or polys is empty ;) >> > >> > is not empty and yes i have two shapefiles from qgis. >> >> Can I download those files somewhere? >> >> > what is the error ? >> >> No idea. Without those files I cannot run your code. > > https://www.dropbox.com/s/2693nfi248z0y9q/files.zip?dl=0 with the > shapefiles It looks like there are no intersections in your data. With the proviso that I've learned about the library only today I think you should get an empty result set rather than the ValueError. Here's a way to reproduce the error (?) with the data provided in the project: import geopandas from geopandas import gpd world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities')) countries = world[['geometry', 'name']] def find(items, name): # didn't find the idomatic way quickly, so for index, n in enumerate(items["name"]): if n == name: return items[index:index+1] raise ValueError berlin = find(cities, "Berlin") paris = find(cities, "Paris") germany = find(countries, "Germany") print(gpd.sjoin(berlin, germany)) print(gpd.sjoin(paris, germany)) # ValueError $ python demo.py geometry name_left index_right \ 175 POINT (13.39960276470055 52.52376452225116) Berlin 41 name_right 175 Germany [1 rows x 4 columns] Traceback (most recent call last): File "demo.py", line 20, in print(gpd.sjoin(paris, germany)) # ValueError File "/home/peter/virt/geopandas/lib/python3.4/site- packages/geopandas/tools/sjoin.py", line 57, in sjoin r_idx = np.concatenate(idxmatch.values) ValueError: need at least one array to concatenate $ I suggest that you file a bug report. From saxri89 at gmail.com Sat Jan 14 11:11:17 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 08:11:17 -0800 (PST) Subject: geopandas bug ? In-Reply-To: References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: <4fe4140d-b424-42dd-a132-f7d76fce44bb@googlegroups.com> ?? ???????, 14 ?????????? 2017 - 6:01:39 ?.?. UTC+2, ? ??????? Peter Otten ??????: > Xristos Xristoou wrote: > > > ?? ???????, 14 ?????????? 2017 - 4:30:48 ?.?. UTC+2, ? ??????? Peter Otten > > ??????: > >> Xristos Xristoou wrote: > >> > >> > ?? ???????, 14 ?????????? 2017 - 3:43:10 ?.?. UTC+2, ? ??????? Peter > >> > Otten ??????: > >> >> Xristos Xristoou wrote: > >> >> > >> >> > i want to create a simple spatial joing using geopandas but i thing > >> >> > so geopandas has bug ? > >> >> > >> >> Have you tried the examples on > >> >> ? Do they work? If yes, inspect > >> >> your data, does it have the same format? > >> > >> Looks like you chose to ignore the hard part. > >> > >> >> > geopandas code : > >> >> > > >> >> > from geopandas import gpd > >> >> > import geopandas > >> >> > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson > >> >> > etc polys = geopandas.GeoDataFrame.from_file('polygons.shp') > >> >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >> >> > > >> >> > error : > >> >> > > >> >> > Traceback (most recent call last): > >> >> > File > >> >> > "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", > >> >> > line 7, in > >> >> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >> >> > File > >> >> > "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", > >> >> > line 57, in sjoin > >> >> > r_idx = np.concatenate(idxmatch.values) > >> >> > ValueError: need at least one array to concatenate > >> > >> >> > any idea why ? > >> >> > >> >> My crystal ball says that either points or polys is empty ;) > >> > > >> > is not empty and yes i have two shapefiles from qgis. > >> > >> Can I download those files somewhere? > >> > >> > what is the error ? > >> > >> No idea. Without those files I cannot run your code. > > > > https://www.dropbox.com/s/2693nfi248z0y9q/files.zip?dl=0 with the > > shapefiles > > It looks like there are no intersections in your data. > With the proviso that I've learned about the library only today I think you > should get an empty result set rather than the ValueError. > Here's a way to reproduce the error (?) with the data provided in the > project: > > import geopandas > from geopandas import gpd > > world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) > cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities')) > countries = world[['geometry', 'name']] > > def find(items, name): > # didn't find the idomatic way quickly, so > for index, n in enumerate(items["name"]): > if n == name: > return items[index:index+1] > raise ValueError > > berlin = find(cities, "Berlin") > paris = find(cities, "Paris") > germany = find(countries, "Germany") > > print(gpd.sjoin(berlin, germany)) > print(gpd.sjoin(paris, germany)) # ValueError > > $ python demo.py > geometry name_left index_right \ > 175 POINT (13.39960276470055 52.52376452225116) Berlin 41 > > name_right > 175 Germany > > [1 rows x 4 columns] > Traceback (most recent call last): > File "demo.py", line 20, in > print(gpd.sjoin(paris, germany)) # ValueError > File "/home/peter/virt/geopandas/lib/python3.4/site- > packages/geopandas/tools/sjoin.py", line 57, in sjoin > r_idx = np.concatenate(idxmatch.values) > ValueError: need at least one array to concatenate > $ > > I suggest that you file a bug report. Mr.Peter Otten do you see my shapefiles ?have instersection 100 to 100 i use instersection on QGIS ad work fine From __peter__ at web.de Sat Jan 14 11:33:01 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 14 Jan 2017 17:33:01 +0100 Subject: geopandas bug ? References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> <4fe4140d-b424-42dd-a132-f7d76fce44bb@googlegroups.com> Message-ID: Xristos Xristoou wrote: >> I suggest that you file a bug report. > > Mr.Peter Otten do you see my shapefiles ?have instersection 100 to 100 i > use instersection on QGIS ad work fine Yes, I downloaded the zipfile at > https://www.dropbox.com/s/2693nfi248z0y9q/files.zip?dl=0 with the and when I ran your code I got the very error that you saw. There are many NaN values in your data, so if it works elsewhere perhaps the data is corrupted in some way. I'm sorry I cannot help you any further. Good luck! From saxri89 at gmail.com Sat Jan 14 11:41:31 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 08:41:31 -0800 (PST) Subject: geopandas bug ? In-Reply-To: References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> <4fe4140d-b424-42dd-a132-f7d76fce44bb@googlegroups.com> Message-ID: ?? ???????, 14 ?????????? 2017 - 6:33:54 ?.?. UTC+2, ? ??????? Peter Otten ??????: > Xristos Xristoou wrote: > > >> I suggest that you file a bug report. > > > > Mr.Peter Otten do you see my shapefiles ?have instersection 100 to 100 i > > use instersection on QGIS ad work fine > > Yes, I downloaded the zipfile at > > > https://www.dropbox.com/s/2693nfi248z0y9q/files.zip?dl=0 with the > > and when I ran your code I got the very error that you saw. There are many > NaN values in your data, so if it works elsewhere perhaps the data is > corrupted in some way. I'm sorry I cannot help you any further. > > Good luck! one more question,i have a idea what is wrong,but if my code work how to export spatial join "pointInPoly" to new shapefile ? From __peter__ at web.de Sat Jan 14 12:30:23 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 14 Jan 2017 18:30:23 +0100 Subject: geopandas bug ? References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> <4fe4140d-b424-42dd-a132-f7d76fce44bb@googlegroups.com> Message-ID: Xristos Xristoou wrote: > ?? ???????, 14 ?????????? 2017 - 6:33:54 ?.?. UTC+2, ? ??????? Peter Otten > ??????: >> Xristos Xristoou wrote: >> >> >> I suggest that you file a bug report. >> > >> > Mr.Peter Otten do you see my shapefiles ?have instersection 100 to 100 >> > i use instersection on QGIS ad work fine >> >> Yes, I downloaded the zipfile at >> >> > https://www.dropbox.com/s/2693nfi248z0y9q/files.zip?dl=0 with the >> >> and when I ran your code I got the very error that you saw. There are >> many NaN values in your data, so if it works elsewhere perhaps the data >> is corrupted in some way. I'm sorry I cannot help you any further. >> >> Good luck! > > one more question,i have a idea what is wrong,but if my code work how to > export spatial join "pointInPoly" to new shapefile ? You can find an object's methods in the interactive interpreter with dir() >>> dir(pointInPoly) ['T', '_AXIS_ALIASES', '_AXIS_IALIASES', '_AXIS_LEN', '_AXIS_NAMES', '_AXIS_NUMBERS', '_AXIS_ORDERS', '_AXIS_REVERSED', '_AXIS_SLICEMAP', 'rmod', 'rmul', 'rotate', 'rpow', 'rsub', 'rtruediv', 'save', 'scale', 'select', 'set_geometry', 'set_index', 'set_value', 'shape', 'shift', 'simplify', 'sindex', 'skew', 'sort', 'sort_index', 'sortlevel', 'squeeze', 'stack', 'std', 'sub', 'subtract', 'sum', 'swapaxes', 'swaplevel', 'symmetric_difference', 'tail', 'take', 'to_clipboard', 'to_crs', 'to_csv', 'to_dense', 'to_dict', 'to_excel', 'to_file', 'to_gbq', 'to_hdf', 'to_html', 'to_json', 'to_latex', 'to_msgpack', 'to_panel', 'to_period', 'to_pickle', 'to_records', 'to_sparse', 'to_sql', 'to_stata', 'to_string', 'to_timestamp', 'to_wide', 'total_bounds', 'touches', 'translate', 'transpose', 'truediv', 'truncate', 'tshift', 'type', 'tz_convert', 'tz_localize', 'unary_union', 'union', 'unstack', 'update', 'values', 'var', 'where', 'within', 'xs'] OK, that's quite a lot, but to_file() seems to be a good candidate. Let's see what it does: >>> help(pointInPoly.to_file) Help on method to_file in module geopandas.geodataframe: to_file(filename, driver='ESRI Shapefile', schema=None, **kwargs) metod of geopandas.geodataframe.GeoDataFrame instance Write this GeoDataFrame to an OGR data source A dictionary of supported OGR providers is available via: >>> import fiona >>> fiona.supported_drivers Parameters ---------- filename : string File path or file handle to write to. driver : string, default 'ESRI Shapefile' The OGR format driver used to write the vector file. schema : dict, default None If specified, the schema dictionary is passed to Fiona to better control how the file is written. The *kwargs* are passed to fiona.open and can be used to write to multi-layer data, store data within archives (zip files), etc. Looks good, run it: >>> pointInPoly.to_file("point_in_poly") No error. Does it round-trip? >>> pointInPoly == gpd.GeoDataFrame.from_file("point_in_poly") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3/dist-packages/pandas/core/ops.py", line 875, in f return self._compare_frame(other, func, str_rep) File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 2860, in _compare_frame raise ValueError('Can only compare identically-labeled ' ValueError: Can only compare identically-labeled DataFrame objects Ouch, unfortunately not. Upon further inspection: >>> pip.columns Index(['geometry', 'index_righ', 'name_left', 'name_right'], dtype='object') >>> pointInPoly.columns Index(['geometry', 'name_left', 'index_right', 'name_right'], dtype='object') Looks like column names are either corrupted or limited to 10 characters by default. Again I don't know how to overcome this, but as a special service here's the first hit for 'shp file column name limit' on a popular search engine: http://gis.stackexchange.com/questions/15784/how-to-bypass-10-character-limit-of-field-name-in-shapefiles I'm out of this now. From duncan at invalid.invalid Sat Jan 14 13:09:42 2017 From: duncan at invalid.invalid (duncan smith) Date: Sat, 14 Jan 2017 18:09:42 +0000 Subject: geopandas bug ? In-Reply-To: References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: On 14/01/17 14:59, Xristos Xristoou wrote: > ?? ???????, 14 ?????????? 2017 - 4:38:39 ?.?. UTC+2, ? ??????? duncan smith ??????: >> On 14/01/17 11:18, Xristos Xristoou wrote: >>> i want to create a simple spatial joing using geopandas but i thing so geopandas has bug ? >>> >>> >>> >>> geopandas code : >>> >>> from geopandas import gpd >>> import geopandas >>> points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc >>> polys = geopandas.GeoDataFrame.from_file('polygons.shp') >>> pointInPoly = gpd.sjoin(points, polys, how='left',op='within') >>> >>> error : >>> >>> Traceback (most recent call last): >>> File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", line 7, in >>> pointInPoly = gpd.sjoin(points, polys, how='left',op='within') >>> File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", line 57, in sjoin >>> r_idx = np.concatenate(idxmatch.values) >>> ValueError: need at least one array to concatenate >>> >>> and if i change the imports with the some code : >>> >>> import geopandas >>> import pandas as pd >>> import geopandas as gpd >>> from geopandas import GeoDataFrame, read_file >>> from geopandas.tools import sjoin >>> from shapely.geometry import Point, mapping,shape >>> import pandas as gpd >>> >>> i take that error : >>> >>> pointInPoly = gpd.sjoin(points, polys, how='left',op='within') >>> AttributeError: 'module' object has no attribute 'sjoin' >>> >>> >>> any idea why ? >>> >> >> >> import geopandas as gpd >> import pandas as gpd >> >> Does pandas have an attribute 'sjoin'? >> >> Duncan > > i dont know i follow detais i am newbie > You import geopandas as gpd, then import pandas as gpd. So maybe you think gpd refers to geopandas while it actually refers to pandas. I don't know geopandas or pandas, but you should check your imports. Duncan From saxri89 at gmail.com Sat Jan 14 13:29:45 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sat, 14 Jan 2017 10:29:45 -0800 (PST) Subject: geopandas bug ? In-Reply-To: References: <2bffcdb3-066a-4f91-b00b-e390dd03c103@googlegroups.com> Message-ID: <65c4340e-3edb-4703-8159-6927d975a8a2@googlegroups.com> ?? ???????, 14 ?????????? 2017 - 8:09:53 ?.?. UTC+2, ? ??????? duncan smith ??????: > On 14/01/17 14:59, Xristos Xristoou wrote: > > ?? ???????, 14 ?????????? 2017 - 4:38:39 ?.?. UTC+2, ? ??????? duncan smith ??????: > >> On 14/01/17 11:18, Xristos Xristoou wrote: > >>> i want to create a simple spatial joing using geopandas but i thing so geopandas has bug ? > >>> > >>> > >>> > >>> geopandas code : > >>> > >>> from geopandas import gpd > >>> import geopandas > >>> points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc > >>> polys = geopandas.GeoDataFrame.from_file('polygons.shp') > >>> pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >>> > >>> error : > >>> > >>> Traceback (most recent call last): > >>> File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", line 7, in > >>> pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >>> File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", line 57, in sjoin > >>> r_idx = np.concatenate(idxmatch.values) > >>> ValueError: need at least one array to concatenate > >>> > >>> and if i change the imports with the some code : > >>> > >>> import geopandas > >>> import pandas as pd > >>> import geopandas as gpd > >>> from geopandas import GeoDataFrame, read_file > >>> from geopandas.tools import sjoin > >>> from shapely.geometry import Point, mapping,shape > >>> import pandas as gpd > >>> > >>> i take that error : > >>> > >>> pointInPoly = gpd.sjoin(points, polys, how='left',op='within') > >>> AttributeError: 'module' object has no attribute 'sjoin' > >>> > >>> > >>> any idea why ? > >>> > >> > >> > >> import geopandas as gpd > >> import pandas as gpd > >> > >> Does pandas have an attribute 'sjoin'? > >> > >> Duncan > > > > i dont know i follow detais i am newbie > > > > You import geopandas as gpd, then import pandas as gpd. So maybe you > think gpd refers to geopandas while it actually refers to pandas. I > don't know geopandas or pandas, but you should check your imports. > > Duncan @Duncan i see that and i remove line with pandas ans no change From rodrick.brown at gmail.com Sat Jan 14 13:46:19 2017 From: rodrick.brown at gmail.com (Rodrick Brown) Date: Sat, 14 Jan 2017 13:46:19 -0500 Subject: multiprocessing.Process call blocks other processes from running Message-ID: I'm trying to implement a script that tracks how much space certain applications are using and send a metric to a statsd service for realtime analysis however when running this code it nots forking multiple processes its still running sequential at some point the forking was working but I can't seem to figure out where I went wrong please advise I'm just trying to keep track of the growth rate of certain dirs these dirs have hundreds of thousands of files and take sometime to run I use du as it runs much faster than os.walk() and it also returns the correct compressed size on the file system pythons getsize() does not. Thanks. from datadog import initialize from datadog import api from datadog import statsd import os import subprocess from import Process, Queue from datadog import ThreadStats import time import datetime from hashlib import md5 options = { 'api_key': 'xx', 'app_key': 'xx' } def getWhispererLogsDirSize(clientcfg, queue): clientName, logPath = clientcfg.items()[0] totalSize = 0 clientResult = {} for item in os.listdir(logPath): logDir = logPath + "/" + item try: totalSize = totalSize + int(subprocess.check_output(["du","-s",logDir]).split('\t')[0]) except subprocess.CalledProcessError: print("Error processing {0} skipping.....".format(logDir)) continue clientResult[clientName] = [os.path.basename(logPath),totalSize] queue.put(clientResult) return if __name__ == '__main__': title = 'Whisperer client marketdata usage' text = 'This simple utility sends whisperer logs into datadog based on client usage on disk' tags = ['version:1'] initialize(**options) #api.Event.create(title=title, text=text, tags=tags) #stats = ThreadStats() #stats.start() queue = Queue() jobs = [] clients = [ {'xx1':'/mnt/auto/glusterfs/app/NYC01-xx-PROD-01'}, {'xx2':'/mnt/auto/glusterfs/app/NYC01-xx-PROD-01'}, {'xx3':'/mnt/auto/glusterfs/app/NYC01-xx-PROD-01'}, {'xx4':'/mnt/auto/glusterfs/app/NYC01-xx-PROD-01'} ] tags = [] while True: for client in clients: stats = ThreadStats() stats.start() p = Process(target=getWhispererLogsDirSize, args=(client,queue,)) jobs.append(p) p.start() p.join() clientinfo = queue.get() clientName = clientinfo.values()[0][0] clientPath = clientinfo.keys()[0] clientLogSize = clientinfo.values()[0][1] tags = [clientName,clientPath] aggregation_key = md5(clientName).hexdigest() print(clientName, clientPath, clientLogSize) with open('/tmp/dogstatd_out.log', 'a+') as fp: fp.write("{0} {1} {2} {3}\n".format(str(datetime.datetime.now()),clientName, clientPath, clientLogSize)) stats.gauge('whisperer.marketdata.clientlogsize',int(clientLogSize),tags=tags) From jcasale at activenetwerx.com Sat Jan 14 14:05:48 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Sat, 14 Jan 2017 19:05:48 +0000 Subject: multiprocessing.Process call blocks other processes from running In-Reply-To: References: Message-ID: > while True: > for client in clients: > stats = ThreadStats() > stats.start() > p = Process(target=getWhispererLogsDirSize, args=(client,queue,)) > jobs.append(p) > p.start() > p.join() You start one client then join before starting the next... Start them all and push the pointer the resulting process object into a collection. Then use whatever semantics you desire to wait them all... jlc From python at mrabarnett.plus.com Sat Jan 14 15:10:02 2017 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 14 Jan 2017 20:10:02 +0000 Subject: multiprocessing.Process call blocks other processes from running In-Reply-To: References: Message-ID: On 2017-01-14 19:05, Joseph L. Casale wrote: >> while True: >> for client in clients: >> stats = ThreadStats() >> stats.start() >> p = Process(target=getWhispererLogsDirSize, args=(client,queue,)) >> jobs.append(p) >> p.start() >> p.join() > > You start one client then join before starting the next... > > Start them all and push the pointer the resulting process object into a collection. > Then use whatever semantics you desire to wait them all... > To me it also looks slightly odd that you're creating a ThreadStats instance each time around the loop, and the only other reference to it is to the last one created, and that line's indentation puts it outside the """if __name__ == '__main__':""" block. From python at lucidity.plus.com Sat Jan 14 16:13:26 2017 From: python at lucidity.plus.com (Erik) Date: Sat, 14 Jan 2017 21:13:26 +0000 Subject: How can I make a sentinel value NOT be initialized in a class/method - OOP? In-Reply-To: References: Message-ID: <64df2ad9-3944-ff95-74e1-9d83ea8ad1a5@lucidity.plus.com> [Replying to direct email. David - please respond to the list so that you can receive other people's suggestions also] Hi David, On 14/01/17 15:30, David D wrote: > 1) No this is not homework, I am doing this all on my own. In that case I apologise - I gave you the benefit of the doubt though as I was only half-suspicious. FWIW, listing specific constructs that must be used is often a thing that a tutor would write in an assignment (because the purpose of the assignment is to get you to learn about those particular constructs). That's what raised the flag for me. Anyway ... > 2) I am changing the class to cars. > > 3) A second question arose that I thought would work, but I am getting > this error :*can't assign to a function call* > > What I am trying to do is this pseudo code : Firstly, there's little point in posting pseudo code if you are reporting a specific compiler or run-time error. The errors you get are very specific to your code and in some cases are caused by *subtle* things in your code. We generally need to see the actual code (or a cut-down example) that allows the subtleties to be described to you. > count= 0 > > class car > initialize all of the values and put self so they are available to the > entire class > def __init__(self, model...) > self.mode=model etc > > > while loop > input model =what is the model > input serial = what is the serial > input doors = how many doors > count = count + 1 > #create the instance/object > mycar (count) = car(model, serial, doors) > > input do you want another car in the database? > if yes > continue > if no > break > > The issue is that when creating an object/instance, python won't let me > use this syntax of having -- car(count) which would give me multiple > objects (car1, car2, car3 etc) with the count variable. I am not sure why. The syntax "foo(spam)" will *call* the function "foo" and pass it the value "spam". It doesn't make any sense to _assign a value_ to a function call operation (you're effectively trying to assign a value - the thing after the '=' - to another value - the return value of the function call). So, what is "mycar"? How do you create it? In Python, the built-in structure for a group of objects which can be dynamically extended with new entries in the way you want is a list. Create an empty one with: mycar = [] or mycar = list() Lists have an 'append()' method which will add the parameter it is called with to the end of the list object it is called on: mycar.append(car(model, serial, doors)) You don't have to worry about keeping track of 'count'. Lists will grow dynamically as you add things. Use "len(mycar)" to see how many items it holds and "for vehicle in mycar:" to step through them all or "mycar[n]" to address as specific entry. From the Python prompt, type "help(list)" Hope that helps. E. From steve+python at pearwood.info Sat Jan 14 18:32:07 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 15 Jan 2017 10:32:07 +1100 Subject: multiprocessing.Process call blocks other processes from running References: Message-ID: <587ab4f9$0$1588$c3e8da3$5496439d@news.astraweb.com> On Sun, 15 Jan 2017 05:46 am, Rodrick Brown wrote: > at some point the forking was working Then whatever you changed, you should change back to the way it was. That's the most important lesson here: never make two or more unrelated changes to a program unless you have a backup of the working file. An excellent way to manage this process is by using a revision control system like Mercurial (hg) or equivalent. But at the very least, whenever you change a working program, you should confirm it is still working before making the next change. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From hawat.thufir at gmail.com Sun Jan 15 12:19:34 2017 From: hawat.thufir at gmail.com (Thufir Hawat) Date: Sun, 15 Jan 2017 09:19:34 -0800 (PST) Subject: nfldb API Message-ID: I'm looking at nfldb: pip install --user nfldb which is on github: https://github.com/BurntSushi/nfldb/wiki/More-examples Coming at this from Java, how would I hook into nfldb with Java? Or, perhaps, run the code in Jython? I'm sure there's a general solution or approach, would appreciate any pointers. Perhaps simply using JNI to leverage the work that's gone into this library already. thanks, Thufir From daviddschool at gmail.com Sun Jan 15 14:58:16 2017 From: daviddschool at gmail.com (David D) Date: Sun, 15 Jan 2017 11:58:16 -0800 (PST) Subject: working with classes, inheritance, _str_ returns and a list Message-ID: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> I am creating a parent class and a child class. I am inheriting from the parent with an additional attribute in the child class. I am using __str__ to return the information. When I run the code, it does exactly what I want, it returns the __str__ information. This all works great. BUT 1) I want what is returned to be appended to a list (the list will be my database) 2) I append the information to the list that I created 3) Whenever I print the list, I get a memory location So how do I take the information that is coming out of the child class (as a __str__ string), and keep it as a string so I can append it to the list? pseudo code allcars=[] parent class() def init (self, model, wheels, doors) self.model= model etc child class (parent) def init(self, model, wheels, doors, convertible) super(child, self).__init__(model, wheels, doors) self.convertible = convertible def __str__(self): return "model: " + self.model + etc car1= child(model, wheels, doors, convertible) print car1 Here is where it goes wrong for me allcars.append(car1) I am sure I am making a silly mistake in here somewhere... From rosuav at gmail.com Sun Jan 15 15:08:04 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 16 Jan 2017 07:08:04 +1100 Subject: working with classes, inheritance, _str_ returns and a list In-Reply-To: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> References: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> Message-ID: On Mon, Jan 16, 2017 at 6:58 AM, David D wrote: > I am creating a parent class and a child class. I am inheriting from the parent with an additional attribute in the child class. I am using __str__ to return the information. When I run the code, it does exactly what I want, it returns the __str__ information. This all works great. > > BUT > > 1) I want what is returned to be appended to a list (the list will be my database) > 2) I append the information to the list that I created > 3) Whenever I print the list, I get a memory location Technically it's an identity, not a memory location, but yes, I know what you mean here. What's happening is that printing a list actually prints the *repr* of an object, rather than its str. The easiest change is to rename your __str__ function to __repr__; if you don't need them to be different, define __repr__ and it'll be used for printing as well. By the way, you'll find that Python 3 makes some things with class definitions a bit easier. There are less traps to potentially fall into. I strongly suggest using the latest Python (currently 3.6) or something close to it (3.5 is in a lot of Linux distributions). All the best! ChrisA From michael.s011235 at gmail.com Sun Jan 15 15:55:14 2017 From: michael.s011235 at gmail.com (Michael S) Date: Sun, 15 Jan 2017 21:55:14 +0100 Subject: Problem while building Python 3.6 from source. In-Reply-To: References: Message-ID: Thanks Thomas and ChrisA! @Thomas: I don't know.. Unfortunately I am not as skilled as to be able to find out.. @ChrisA: I tried your advice and think it worked. So, I just used $ ./configure and then $ make. Then I was not sure, whether it had worked so I used $ make test and got a fairly long result. At the end there was a line like "test successful". Nevertheless not all of the tests were successful. So, now I got some questions: 1) Why did not all of the tests in $ make test succeed? But the end line was "test successful". That confuses me. 2) This is more general. In order to get the build-dependencies I used # apt-get build-dep python3.4. I also googled for the build dependencies but did not find anything. So, how could I actually figure out the build dependencies for Python3.6? On Fri, Jan 13, 2017 at 7:19 PM, Chris Angelico wrote: > On Sat, Jan 14, 2017 at 5:00 AM, Michael S wrote: >> Hello, >> I am new to this mailing-list and I really don't know whether this >> mail should belong to python-dev. Please tell me, if so. > > Hi and welcome! This kind of thing is best on this list initially. > >> Unfortunately, I have got the following problem: I wanted to build and >> install Python 3.6 from source but did not succeed. >> To clarify my situation, I got as an operating system Debian jessie >> 8.6 and I used the xz compressed source tarball from >> https://www.python.org/downloads/release/python-360/. >> Concerning the build dependencies: I just executed: >> $ sudo apt-get build-dep python3.4 (since 3.6 and 3.5 did not work). > > That should be fine; the build dependencies of Python don't tend to > change frequently. Jessie shipped with Python 3.4 but nothing newer, > so there won't be packages for python3.5 or python3.6. > >> Then I executed ./configure --enable-optimizations and make -j4 (I got 4 cores). >> The output of make ended like: >> 'make: *** [profile-opt] Error 2'. > > That just means that something went wrong. You'd have to scroll up to > find the actual cause of the error. > >> I had redirected the output and error of the configure and make commands via >> $ make -j4 &> /home/username/make_output.txt. >> Nevertheless I got an error to the console: >> '*** Error in ./python'" free(): invalid next size (normal): >> 0x00000000015bdf90 ***'. >> Due to these error messages (this one and the one at the end of make) >> I think the build was not successful. >> >> How to solve this problem? >> >> Of course I could send you the output and error files. > > The first thing I'd do would be to try a non-optimized build. Set your > current build tree aside and re-extract into a new directory (that > way, when you go back to playing with optimized builds, you don't have > to redo the work), and run configure with no arguments. I'd also be > inclined to run make with no arguments; there've been issues with > parallel builds in enough projects that I've gotten into the habit of > "problem? do it the slow way". If that build also fails, scroll up a > bit and find where stuff failed. > > Are you familiar with building programs from source? If not, the best > solution might be to post the entire log, but ideally, you should be > able to skim through the last part of the log and report the actual > problem that's cropping up. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Sun Jan 15 16:09:17 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 16 Jan 2017 08:09:17 +1100 Subject: Problem while building Python 3.6 from source. In-Reply-To: References: Message-ID: On Mon, Jan 16, 2017 at 7:55 AM, Michael S wrote: > @ChrisA: I tried your advice and think it worked. So, I just used $ > ./configure and then $ make. Then I was not sure, whether it had > worked so I used $ make test and got a fairly long result. At the end > there was a line like "test successful". Nevertheless not all of the > tests were successful. Can you run: $ ./python That should fire up the newly-built interpreter. > So, now I got some questions: > > 1) Why did not all of the tests in $ make test succeed? But the end > line was "test successful". That confuses me. Not sure - I'd have to see what the actual output was. Most likely there were some tests skipped; not all tests apply on all platforms. For instance, there are a number of Windows-specific tests, which won't be running on your system. Also, Python includes a number of optional modules, and if you don't have their dependencies, you get a Python that's fully functional with the exception of that/those module(s). > 2) This is more general. In order to get the build-dependencies I used > # apt-get build-dep python3.4. I also googled for the build > dependencies but did not find anything. So, how could I actually > figure out the build dependencies for Python3.6? They don't often change between versions. Certainly between 3.4 and 3.6 you should have no problems. It's only an issue if you're porting to a brand new platform or something, and then you have to do the usual dance of "compile, read the error, grab a new library, rinse and repeat". ChrisA From python at lucidity.plus.com Sun Jan 15 16:27:58 2017 From: python at lucidity.plus.com (Erik) Date: Sun, 15 Jan 2017 21:27:58 +0000 Subject: working with classes, inheritance, _str_ returns and a list In-Reply-To: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> References: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> Message-ID: <6f235a6f-cb53-a613-a4c1-da3de72990a9@lucidity.plus.com> Hi, On 15/01/17 19:58, David D wrote: > I am creating a parent class and a child class. I am inheriting from > the parent with an additional attribute in the child class. I am > using __str__ to return the information. When I run the code, it > does exactly what I want, it returns the __str__ information. This > all works great. > > BUT > > 1) I want what is returned to be appended to a list (the list will be > my database) 2) I append the information to the list that I created > 3) Whenever I print the list, I get a memory location > > So how do I take the information that is coming out of the child > class (as a __str__ string), and keep it as a string so I can append > it to the list? [snip] > Here is where it goes wrong for me > > allcars.append(car1) This adds the object (of the child or parent class) to the list. If you _really_ want the *string* returned by __str__() to be appended to the list then you would do: allcars.append(str(car1)) (The str() function returns what the object's __str__() method returns if it has one - otherwise it will return SOME sort of string representation of your object, but you can't rely on the format of that). I'm a bit confused though as to why you would want to create that object only store its __str__() value and discard the object itself. E. From frank at chagford.com Mon Jan 16 00:25:59 2017 From: frank at chagford.com (Frank Millman) Date: Mon, 16 Jan 2017 07:25:59 +0200 Subject: working with classes, inheritance, _str_ returns and a list In-Reply-To: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> References: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> Message-ID: "David D" wrote in message news:4f0680eb-2678-4ea2-b622-a6cd5a19e0a0 at googlegroups.com... > > I am creating a parent class and a child class. I am inheriting from the > parent with an additional attribute in the child class. I am using > __str__ to return the information. When I run > the code, it does exactly > what I want, it returns the __str__ information. This all works great. > > BUT > > 1) I want what is returned to be appended to a list (the list will be my > database) > 2) I append the information to the list that I created > 3) Whenever I print the list, I get a memory location > You have been given an explanation, and a couple of workarounds. Here is another possible workaround, which may help depending on how you actually print the list - If you are saying - for item in list: print(item) you can say instead - for item in list: print(str(item)) HTH Frank Millman From girishrkhasnis at gmail.com Mon Jan 16 00:32:25 2017 From: girishrkhasnis at gmail.com (Girish Khasnis) Date: Mon, 16 Jan 2017 11:02:25 +0530 Subject: Python Error Message-ID: Hi, I am unable to install Python on my system. After installing Python I get the below error when I try to open Python. [image: Inline image 1] Regards Girish From jessealama at fastmail.fm Mon Jan 16 00:39:34 2017 From: jessealama at fastmail.fm (Jesse Alama) Date: Mon, 16 Jan 2017 06:39:34 +0100 Subject: Python Web Scrapping : Within href readonly those value that have href in it In-Reply-To: References: Message-ID: <1484545174.2046826.848802760.2F41A44F@webmail.messagingengine.com> To complement what Peter wrote: I'd approach this problem using XPath. XPath is a query language for XML/HTML documents; it's a great tool to have in your web scraping toolbox (among other tasks). With Python's excellent lxml library you can do some XPath processing. Here's how I might tackle this problem: == [ scrape.py ] ====================================================== from lxml import etree # ...somehow get HTML/XML into the variable xml root = etree.HTML(xml) hrefs = root.xpath("//a[@href and starts-with(@href, 'http://')]/@href") # magic =========> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ print(hrefs) # if you want to see what this looks like == [ end scrape.py ] ================================================== The argument to the xpath method here is an XPath expression. The overall form is: //a[.....]/@href The '//a' at the beginning means: starting at the root node of the document, find all a (anchor) elements that match the condition specified by ".....". The '/@href' at the end means: give me the href attribute of the nodes (if any) that remain. Looking inside the square brackets (what's known as the predicate in the XPath world), we find @href and starts-with(@href, 'http://') The 'and' bit should be clear (there are two conditions that need to be checked). The first part says: the a element should have an href attribute. The second part says that the value of the href element had better start with 'http://'. In fact, we could simplify the predicate to starts-with(@href, 'http://') If an element does not even have an href attribute, its value does not start with 'http://'. It's not an error, and no exception will be thrown, when the XPath evaluator applies the starts-with function to an a element that does not have an href attribute. Hope this helps. Best regards, Jesse -- Jesse Alama http://xml.sh From pkpearson at nowhere.invalid Mon Jan 16 00:47:58 2017 From: pkpearson at nowhere.invalid (Peter Pearson) Date: 16 Jan 2017 05:47:58 GMT Subject: Sockets: IPPROTO_IP not supported Message-ID: Trying to sniff Ethernet packets, I do this: s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) but it results in this: $ sudo python3 sniff_survey.py Traceback (most recent call last): File "sniff_survey.py", line 118, in s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) File "/usr/lib/python3.2/socket.py", line 94, in __init__ _socket.socket.__init__(self, family, type, proto, fileno) socket.error: [Errno 93] Protocol not supported Anybody know what I'm doing wrong? (Python 3.2.3 under Debian 3.2.84-1.) -- To email me, substitute nowhere->runbox, invalid->com. From frank at chagford.com Mon Jan 16 00:57:16 2017 From: frank at chagford.com (Frank Millman) Date: Mon, 16 Jan 2017 07:57:16 +0200 Subject: working with classes, inheritance, _str_ returns and a list In-Reply-To: References: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> Message-ID: "Frank Millman" wrote in message news:o5hlh4$1sb$1 at blaine.gmane.org... > > If you are saying - > for item in list: > print(item) > > you can say instead - > for item in list: > print(str(item)) > This is not correct, sorry. print(item) will automatically print the string representation of item, so it makes no difference. The principle is still correct, though. If you want to convert what you call the memory address of the item to the string representation, just wrap it in str(...) Frank From frank at chagford.com Mon Jan 16 01:03:19 2017 From: frank at chagford.com (Frank Millman) Date: Mon, 16 Jan 2017 08:03:19 +0200 Subject: working with classes, inheritance, _str_ returns and a list In-Reply-To: References: <4f0680eb-2678-4ea2-b622-a6cd5a19e0a0@googlegroups.com> Message-ID: "Frank Millman" wrote in message news:o5hnbq$q36$1 at blaine.gmane.org... > > "Frank Millman" wrote in message news:o5hlh4$1sb$1 at blaine.gmane.org... > > > > If you are saying - > > for item in list: > > print(item) > > > > you can say instead - > > for item in list: > > print(str(item)) > > > > This is not correct, sorry. > > print(item) will automatically print the string representation of item, so > it makes no difference. > > The principle is still correct, though. > > If you want to convert what you call the memory address of the item to the > string representation, just wrap it in str(...) > I keep thinking of something else just after I have posted - sorry about that. When you say you print the list, maybe you are literally doing the following - print(list) In that case, the solution is to turn it into a list comprehension, and apply str() to each item - print([str(item) for item in list]) Frank From adnan.connexus at gmail.com Mon Jan 16 02:55:02 2017 From: adnan.connexus at gmail.com (Adnan Sheikh) Date: Sun, 15 Jan 2017 23:55:02 -0800 (PST) Subject: matrix from matrix In-Reply-To: References: Message-ID: <9f06e031-fba6-4863-bbab-fde485d1c655@googlegroups.com> My apologies for not providing enough information. I'll share my code attempt shortly. Cheers for your help :) Shei From tjreedy at udel.edu Mon Jan 16 04:24:08 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 16 Jan 2017 04:24:08 -0500 Subject: Python Error In-Reply-To: References: Message-ID: On 1/16/2017 12:32 AM, Girish Khasnis wrote: > Hi, > > > I am unable to install Python on my system. After installing Python I get > the below error when I try to open Python. > > [image: Inline image 1] Copy and paste the error message. This is text only list. -- Terry Jan Reedy From jcasale at activenetwerx.com Mon Jan 16 05:17:06 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 16 Jan 2017 10:17:06 +0000 Subject: Sockets: IPPROTO_IP not supported In-Reply-To: References: Message-ID: <32fb9ad66f854486889cb7af607e6fff@activenetwerx.com> > Trying to sniff Ethernet packets, I do this: > > s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) > > but it results in this: > > $ sudo python3 sniff_survey.py > Traceback (most recent call last): > File "sniff_survey.py", line 118, in > s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) > File "/usr/lib/python3.2/socket.py", line 94, in __init__ > _socket.socket.__init__(self, family, type, proto, fileno) > socket.error: [Errno 93] Protocol not supported > > Anybody know what I'm doing wrong? (Python 3.2.3 under Debian 3.2.84-1.) Have a look at the bottom of this SO question: http://stackoverflow.com/questions/5385312/ipproto-ip-vs-ipproto-tcp-ipproto-udp From info at egenix.com Mon Jan 16 06:35:15 2017 From: info at egenix.com (eGenix Team: M.-A. Lemburg) Date: Mon, 16 Jan 2017 12:35:15 +0100 Subject: =?UTF-8?Q?ANN:_Python_Meeting_D=c3=bcsseldorf_-_18.01.2017?= Message-ID: <6ac5851e-cc56-c128-cb08-267b711973be@egenix.com> [This announcement is in German since it targets a local user group meeting in D?sseldorf, Germany] ________________________________________________________________________ ANK?NDIGUNG Python Meeting D?sseldorf http://pyddf.de/ Ein Treffen von Python Enthusiasten und Interessierten in ungezwungener Atmosph?re. Mittwoch, 18.01.2017, 18:00 Uhr Raum 1, 2.OG im B?rgerhaus Stadtteilzentrum Bilk D?sseldorfer Arcaden, Bachstr. 145, 40217 D?sseldorf Diese Nachricht ist auch online verf?gbar: http://www.egenix.com/company/news/Python-Meeting-Duesseldorf-2017-01-18 ________________________________________________________________________ NEUIGKEITEN * Bereits angemeldete Vortr?ge: Charlie Clark "Kurze Einf?hrung in openpyxl und Pandas" Jochen Wersd?rfer "CookieCutter" Marc-Andre Lemburg "Optimierung in Python mit PuLP" Weitere Vortr?ge k?nnen gerne noch angemeldet werden: info at pyddf.de * Startzeit und Ort: Wir treffen uns um 18:00 Uhr im B?rgerhaus in den D?sseldorfer Arcaden. Das B?rgerhaus teilt sich den Eingang mit dem Schwimmbad und befindet sich an der Seite der Tiefgarageneinfahrt der D?sseldorfer Arcaden. ?ber dem Eingang steht ein gro?es "Schwimm' in Bilk" Logo. Hinter der T?r direkt links zu den zwei Aufz?gen, dann in den 2. Stock hochfahren. Der Eingang zum Raum 1 liegt direkt links, wenn man aus dem Aufzug kommt. Google Street View: http://bit.ly/11sCfiw ________________________________________________________________________ EINLEITUNG Das Python Meeting D?sseldorf ist eine regelm??ige Veranstaltung in D?sseldorf, die sich an Python Begeisterte aus der Region wendet: * http://pyddf.de/ Einen guten ?berblick ?ber die Vortr?ge bietet unser YouTube-Kanal, auf dem wir die Vortr?ge nach den Meetings ver?ffentlichen: * http://www.youtube.com/pyddf/ Veranstaltet wird das Meeting von der eGenix.com GmbH, Langenfeld, in Zusammenarbeit mit Clark Consulting & Research, D?sseldorf: * http://www.egenix.com/ * http://www.clark-consulting.eu/ ________________________________________________________________________ PROGRAMM Das Python Meeting D?sseldorf nutzt eine Mischung aus (Lightning) Talks und offener Diskussion. Vortr?ge k?nnen vorher angemeldet werden, oder auch spontan w?hrend des Treffens eingebracht werden. Ein Beamer mit XGA Aufl?sung steht zur Verf?gung. (Lightning) Talk Anmeldung bitte formlos per EMail an info at pyddf.de ________________________________________________________________________ KOSTENBETEILIGUNG Das Python Meeting D?sseldorf wird von Python Nutzern f?r Python Nutzer veranstaltet. Um die Kosten zumindest teilweise zu refinanzieren, bitten wir die Teilnehmer um einen Beitrag in H?he von EUR 10,00 inkl. 19% Mwst, Sch?ler und Studenten zahlen EUR 5,00 inkl. 19% Mwst. Wir m?chten alle Teilnehmer bitten, den Betrag in bar mitzubringen. ________________________________________________________________________ ANMELDUNG Da wir nur f?r ca. 20 Personen Sitzpl?tze haben, m?chten wir bitten, sich per EMail anzumelden. Damit wird keine Verpflichtung eingegangen. Es erleichtert uns allerdings die Planung. Meeting Anmeldung bitte formlos per EMail an info at pyddf.de ________________________________________________________________________ WEITERE INFORMATIONEN Weitere Informationen finden Sie auf der Webseite des Meetings: http://pyddf.de/ Mit freundlichen Gr??en, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Jan 16 2017) >>> Python Projects, Coaching and Consulting ... http://www.egenix.com/ >>> Python Database Interfaces ... http://products.egenix.com/ >>> Plone/Zope Database Interfaces ... http://zope.egenix.com/ ________________________________________________________________________ ::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ http://www.malemburg.com/ From p.f.moore at gmail.com Mon Jan 16 07:09:06 2017 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 16 Jan 2017 04:09:06 -0800 (PST) Subject: A detailed description on virtualenv's packaging dependecies? (pip, easy_install, wheel, setuptools, distlib, etc.) In-Reply-To: <05080bd7-5903-4621-9cc5-e5c9be04e80d@googlegroups.com> References: <05080bd7-5903-4621-9cc5-e5c9be04e80d@googlegroups.com> Message-ID: <7a1a87bc-5cdd-4b20-b305-86a1f303de19@googlegroups.com> On Friday, 13 January 2017 09:27:21 UTC, haraldn... at gmail.com wrote: > I was working on a bugfix for Virtualenv, regarding very long shebang lines that are breaking things. In the process, I realized that if I want really fix to my particular issue it likely needs to be done on the lower level of Python package management. I started with pip, moved to setuptools and now set my sight on distlib. > > Can someone describe the specific dependencies of all the *packaging* libraries that `virtualenv` uses? And the dependencies between them? > > I believe that virtualenv directly imports `pip`, `easy_install` and `wheel`. Those in turn import `setuptools` and `distlib`. Am I so lucky as to assume that distlib in the lowest-level library used by all the rest in virtualenv for packages? I believe distlib is the lowest level. But I think you're looking at the problem wrongly. From the issue you reported, the problem you're hitting is that your OS limits the length of paths you can use in a "shebang" line, and the pathname for your virtualenv exceeds that limit. And as a result, script wrappers for Python modules installed in the virtualenv fail because the shebang line is too long. So it's not virtualenv creating the environment that needs to be fixed, but *anything* that installs wrappers that point to the environment Python with a shebang line (wherever they are installed to). By patching distlib, you'll catch the case of installing packages into the virtualenv using pip and distil (once they update distlib). But I don't think you will catch easy_install (which uses setuptools, which uses its own wrapper script) or setup.py install (which again most likely uses setuptools). And nothing will catch 3rd party tools that do their own thing. So if you want to try to minimise the impact of the OS limitation, you can patch both distlib and setuptools and probably get most things (actually, there's probably not much that doesn't use pip, so just patching distlib will probably get the majority of cases). You'll have to manually apply the workaround to any scripts you write yourself that you want to run with the virtualenv's Python. Or you can just accept the limitation and use shorter pathnames. If you're willing to accept the limitation, and it's possible to somehow determine from the OS what it allows, then getting virtualenv to warn if you use a "too long" pathname might be an alternative solution. Paul From valkrem at yahoo.com Mon Jan 16 07:45:45 2017 From: valkrem at yahoo.com (Val Krem) Date: Mon, 16 Jan 2017 12:45:45 -0000 Subject: data frame In-Reply-To: References: <235400868.1750803.1482525599916.ref@mail.yahoo.com> <235400868.1750803.1482525599916@mail.yahoo.com> Message-ID: <1242451632.1794626.1482530144997@mail.yahoo.com> Here is the first few lines of the data s1.csv size,w1,h1 512,214,26 123,250,34 234,124,25 334,213,43 and the script a=pd.read_csv("s1.csv", skipinitialspace=True).keys() print(a) i see the following Index(['size', 'w1', 'h1'], dtype='object') when I wanted to add the two columns; then I get the following message. a=pd.read_csv("s1.csv", skipinitialspace=True).keys() a['test']=a['w1'] + a['h1'] print(a) data/apps/Intel/intelpython35/lib/python3.5/site-packages/pandas/indexes/base.py:1393: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future return getitem(key) Traceback (most recent call last): File "tt.py", line 12, in a['test']=a['w1'] + a['h1'] File "/data/apps/Intel/intelpython35/lib/python3.5/site-packages/pandas/indexes/base.py", line 1393, in __getitem__ return getitem(key) IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices On Friday, December 23, 2016 3:09 PM, Peter Otten <__peter__ at web.de> wrote: Val Krem via Python-list wrote: > Hi all, > > #!/usr/bin/env python > import sys > import csv > import numpy as np > import pandas as pd > > a= pd.read_csv("s1.csv") > print(a) > > size w1 h1 > 0 512 214 26 > 1 123 250 34 > 2 234 124 25 > 3 334 213 43 > 4 a45 223 32 > 5 a12 214 26 > > I wanted to create a new column by adding the two column values > as follows > > a['test'] = a['w1'] + a['h1'] > > Traceback (most recent call last): > File > "/data/apps/Intel/intelpython35/lib/python3.5/site- packages/pandas/indexes/base.py", > line 2104, in get_loc return self._engine.get_loc(key) File > "pandas/index.pyx", line 139, in pandas.index.IndexEngine.get_loc > (pandas/index.c:4152) File "pandas/index.pyx", line 161, in > pandas.index.IndexEngine.get_loc (pandas/index.c:4016) File > "pandas/src/hashtable_class_helper.pxi", line 732, in > pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13153) > File "pandas/src/hashtable_class_helper.pxi", line 740, in > pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13107) > KeyError: 'w1' > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "tt.py", line 16, in > a['test']=a['w1'] + a['h1'] > > File "pandas/src/hashtable_class_helper.pxi", line 740, in > pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13107) > KeyError: 'w1' > > Can someone help me what the problem is? > > Thank you in advance Have a look at a.keys(). I suspect that the column name has extra space: >>> pd.read_csv("s1.csv").keys() Index([u'size', u' w1', u' h1'], dtype='object') I that's what you see you can fix it by reading the csv with skipinitialspace=True: >>> pd.read_csv("s1.csv", skipinitialspace=True).keys() Index([u'size', u'w1', u'h1'], dtype='object') -- https://mail.python.org/mailman/listinfo/python-list /data/apps/Intel/intelpython35/lib/python3.5/site-packages/pandas/indexes/base.py:1393: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future return getitem(key) Traceback (most recent call last): File "tt.py", line 12, in a['test']=a['w1'] + a['h1'] File "/data/apps/Intel/intelpython35/lib/python3.5/site-packages/pandas/indexes/base.py", line 1393, in __getitem__ return getitem(key) IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices On Friday, December 23, 2016 3:09 PM, Peter Otten <__peter__ at web.de> wrote: Val Krem via Python-list wrote: > Hi all, > > #!/usr/bin/env python > import sys > import csv > import numpy as np > import pandas as pd > > a= pd.read_csv("s1.csv") > print(a) > > size w1 h1 > 0 512 214 26 > 1 123 250 34 > 2 234 124 25 > 3 334 213 43 > 4 a45 223 32 > 5 a12 214 26 > > I wanted to create a new column by adding the two column values > as follows > > a['test'] = a['w1'] + a['h1'] > > Traceback (most recent call last): > File > "/data/apps/Intel/intelpython35/lib/python3.5/site- packages/pandas/indexes/base.py", > line 2104, in get_loc return self._engine.get_loc(key) File > "pandas/index.pyx", line 139, in pandas.index.IndexEngine.get_loc > (pandas/index.c:4152) File "pandas/index.pyx", line 161, in > pandas.index.IndexEngine.get_loc (pandas/index.c:4016) File > "pandas/src/hashtable_class_helper.pxi", line 732, in > pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13153) > File "pandas/src/hashtable_class_helper.pxi", line 740, in > pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13107) > KeyError: 'w1' > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "tt.py", line 16, in > a['test']=a['w1'] + a['h1'] > > File "pandas/src/hashtable_class_helper.pxi", line 740, in > pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13107) > KeyError: 'w1' > > Can someone help me what the problem is? > > Thank you in advance Have a look at a.keys(). I suspect that the column name has extra space: >>> pd.read_csv("s1.csv").keys() Index([u'size', u' w1', u' h1'], dtype='object') I that's what you see you can fix it by reading the csv with skipinitialspace=True: >>> pd.read_csv("s1.csv", skipinitialspace=True).keys() Index([u'size', u'w1', u'h1'], dtype='object') -- https://mail.python.org/mailman/listinfo/python-list From p.f.moore at gmail.com Mon Jan 16 11:44:37 2017 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 16 Jan 2017 08:44:37 -0800 (PST) Subject: Running Virtualenv with a custom distlib? In-Reply-To: <2bc1125c-eb70-49bc-bd90-ebeaa9741258@googlegroups.com> References: <2bc1125c-eb70-49bc-bd90-ebeaa9741258@googlegroups.com> Message-ID: <985384eb-0336-4836-b24c-9350807969db@googlegroups.com> On Friday, 13 January 2017 09:27:59 UTC, haraldn... at gmail.com wrote: > I want to do some development on `distlib`, and in the process run the code via `virtualenv` which has distlib as a dependency. > > That is, not run the process inside a virtualenv, but run virtualenv's code using a custom dependency. What are the steps I need to go through to achieve this? > > It seems to me that normal package management (`pip`) is not possible here. virtualenv bundles a local copy of pip. And that pip in turn vendors distlib. So you'd need to build a new pip wheel with your customised version of distlib vendored in it, and then build virtualenv with that version of pip embedded. (You can avoid the last step by putting the new pip wheel in a virtualenv_embedded directory next to virtualenv.py, but you'd have to check the docs and maybe sources to ensure I remembered that right. Also your pip wheel would need to be a higher version than the embedded one, or it would get ignored). Paul From israel at ravnalaska.net Mon Jan 16 13:06:02 2017 From: israel at ravnalaska.net (Israel Brewster) Date: Mon, 16 Jan 2017 09:06:02 -0900 Subject: Error handling in context managers Message-ID: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> I generally use context managers for my SQL database connections, so I can just write code like: with psql_cursor() as cursor: And the context manager takes care of making a connection (or getting a connection from a pool, more likely), and cleaning up after the fact (such as putting the connection back in the pool), even if something goes wrong. Simple, elegant, and works well. The problem is that, from time to time, I can't get a connection, the result being that cursor is None, and attempting to use it results in an AttributeError. So my instinctive reaction is to wrap the potentially offending code in a try block, such that if I get that AttributeError I can decide how I want to handle the "no connection" case. This, of course, results in code like: try: with psql_cursor() as cursor: except AttributeError as e: I could also wrap the code within the context manager in an if block checking for if cursor is not None, but while perhaps a bit clearer as to the purpose, now I've got an extra check that will not be needed most of the time (albeit a quite inexpensive check). The difficulty I have with either of these solutions, however, is that they feel ugly to me - and wrapping the context manager in a try block almost seems to defeat the purpose of the context manager in the first place - If I'm going to be catching errors anyway, why not just do the cleanup there rather than hiding it in the context manager? Now don't get me wrong - neither of these issues is terribly significant to me. I'll happily wrap all the context manager calls in a try block and move on with life if that it in fact the best option. It's just my gut says "there should be a better way", so I figured I'd ask: *is* there a better way? Perhaps some way I could handle the error internally to the context manager, such that it just dumps me back out? Of course, that might not work, given that I may need to do something different *after* the context manager, depending on if I was able to get a connection, but it's a thought. Options? ----------------------------------------------- Israel Brewster Systems Analyst II Ravn Alaska 5245 Airport Industrial Rd Fairbanks, AK 99709 (907) 450-7293 ----------------------------------------------- From rosuav at gmail.com Mon Jan 16 13:25:54 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 17 Jan 2017 05:25:54 +1100 Subject: Error handling in context managers In-Reply-To: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: On Tue, Jan 17, 2017 at 5:06 AM, Israel Brewster wrote: > I generally use context managers for my SQL database connections, so I can just write code like: > > with psql_cursor() as cursor: > > > And the context manager takes care of making a connection (or getting a connection from a pool, more likely), and cleaning up after the fact (such as putting the connection back in the pool), even if something goes wrong. Simple, elegant, and works well. > > The problem is that, from time to time, I can't get a connection, the result being that cursor is None, and attempting to use it results in an AttributeError. My question is: If you can't get a connection, why do you get back None? Wouldn't it be better for psql_cursor() to raise an exception? ChrisA From __peter__ at web.de Mon Jan 16 13:52:59 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 16 Jan 2017 19:52:59 +0100 Subject: Error handling in context managers References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: Israel Brewster wrote: > I generally use context managers for my SQL database connections, so I can > just write code like: > > with psql_cursor() as cursor: > > > And the context manager takes care of making a connection (or getting a > connection from a pool, more likely), and cleaning up after the fact (such > as putting the connection back in the pool), even if something goes wrong. > Simple, elegant, and works well. > > The problem is that, from time to time, I can't get a connection, the > result being that cursor is None, and attempting to use it results in an > AttributeError. So my instinctive reaction is to wrap the potentially > offending code in a try block, such that if I get that AttributeError I > can decide how I want to handle the "no connection" case. This, of course, > results in code like: > > try: > with psql_cursor() as cursor: > > except AttributeError as e: > > > I could also wrap the code within the context manager in an if block > checking for if cursor is not None, but while perhaps a bit clearer as to > the purpose, now I've got an extra check that will not be needed most of > the time (albeit a quite inexpensive check). That seems to be the cleanest approach. > The difficulty I have with either of these solutions, however, is that > they feel ugly to me - and wrapping the context manager in a try block > almost seems to defeat the purpose of the context manager in the first > place - If I'm going to be catching errors anyway, why not just do the > cleanup there rather than hiding it in the context manager? > > Now don't get me wrong - neither of these issues is terribly significant > to me. I'll happily wrap all the context manager calls in a try block and > move on with life if that it in fact the best option. It's just my gut > says "there should be a better way", so I figured I'd ask: *is* there a > better way? Perhaps some way I could handle the error internally to the > context manager, such that it just dumps me back out? Of course, that > might not work, given that I may need to do something different *after* > the context manager, depending on if I was able to get a connection, but > it's a thought. Options? The problem is that there is no way to skip the with-block. It would be nice if you could raise a special exception in the __enter__() method that would achieve that. For the time being you could (1) abuse a for loop def psql_cursor(): try: yield make_cursor() except NoConnection: pass for cursor in psql_cursor(): ... or you could (2) return something that hopefully raises an exception soon, e. g. $ cat conditional_context.py import sys from contextlib import contextmanager class Cursor: def execute(self, sql): print("EXECUTING", sql) class Exit(Exception): pass class FailingCursor: def __getattr__(self, name): raise Exit @contextmanager def cursor(): try: yield FailingCursor() if "--fail" in sys.argv else Cursor() except Exit: print("no connection") with cursor() as cs: cs.execute("insert into...") $ python3 conditional_context.py EXECUTING insert into... $ python3 conditional_context.py --fail no connection Option (1) works, but looks wrong while option (2) looks better, but does not work reliably in the general case, e. g. when you perform some costly calculation before the first attribute access. From steve+python at pearwood.info Mon Jan 16 14:07:55 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Tue, 17 Jan 2017 06:07:55 +1100 Subject: Error handling in context managers References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: <587d1a0d$0$1606$c3e8da3$5496439d@news.astraweb.com> On Tue, 17 Jan 2017 05:06 am, Israel Brewster wrote: > I generally use context managers for my SQL database connections, so I can > just write code like: > > with psql_cursor() as cursor: > > > And the context manager takes care of making a connection (or getting a > connection from a pool, more likely), and cleaning up after the fact (such > as putting the connection back in the pool), even if something goes wrong. > Simple, elegant, and works well. > > The problem is that, from time to time, I can't get a connection, the > result being that cursor is None, Seriously? psql_cursor().__enter__ returns None instead of failing? That sounds like a poor design to me. Where is this psql_cursor coming from? > and attempting to use it results in an > AttributeError. So my instinctive reaction is to wrap the potentially > offending code in a try block, such that if I get that AttributeError I > can decide how I want to handle the "no connection" case. This, of course, > results in code like: > > try: > with psql_cursor() as cursor: > > except AttributeError as e: > Except that isn't necessarily the no-connection case. It could be *any* AttributeError anywhere in the entire with block. > I could also wrap the code within the context manager in an if block > checking for if cursor is not None, but while perhaps a bit clearer as to > the purpose, now I've got an extra check that will not be needed most of > the time (albeit a quite inexpensive check). It's cheap, it's only needed once (at the start of the block), it isn't subject to capturing the wrong exception... I would definitely write: with psql_cursor() as cursor: if cursor is not None: > The difficulty I have with either of these solutions, however, is that > they feel ugly to me - and wrapping the context manager in a try block > almost seems to defeat the purpose of the context manager in the first > place - If I'm going to be catching errors anyway, why not just do the > cleanup there rather than hiding it in the context manager? Context managers don't necessarily swallow exceptions (although they can). That's not what they're for. Context managers are intended to avoid: try: ... finally: ... *not* try...except blocks. If you need a try...except, then you could avoid using the context manager and re-invent the wheel: try: ... except: ... finally: # do whatever cleanup the context manager already defines # but now you have to do it yourself or you can let the context manager do what it does, and write your own code to do what you do: try: with ...: ... except: ... [...] > says "there should be a better way", so I figured I'd ask: *is* there a > better way? Perhaps some way I could handle the error internally to the > context manager, such that it just dumps me back out? That's what's supposed to happen: py> with open('foobarbaz') as f: ... pass ... Traceback (most recent call last): File "", line 1, in FileNotFoundError: [Errno 2] No such file or directory: 'foobarbaz' Notice that the context manager simply raises an exception on failure, which I can catch or not as I so choose, rather than returning None. I really think that the problem here is the design of psql_cursor(). > Of course, that > might not work, given that I may need to do something different *after* > the context manager, depending on if I was able to get a connection, but > it's a thought. Options? -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From marko at pacujo.net Mon Jan 16 14:29:42 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Mon, 16 Jan 2017 21:29:42 +0200 Subject: Error handling in context managers References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> <587d1a0d$0$1606$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8760leiz15.fsf@elektro.pacujo.net> Steve D'Aprano : > or you can let the context manager do what it does, and write your own code > to do what you do: > > try: > with ...: > ... > except: > ... Even better: try: a = open(...) except ...: ... with a: ... You want to catch exceptions immediately after (potentially) raising them. Marko From pkpearson at nowhere.invalid Mon Jan 16 17:24:46 2017 From: pkpearson at nowhere.invalid (Peter Pearson) Date: 16 Jan 2017 22:24:46 GMT Subject: Sockets: IPPROTO_IP not supported References: <32fb9ad66f854486889cb7af607e6fff@activenetwerx.com> Message-ID: On Mon, 16 Jan 2017 10:17:06 +0000, Joseph L. Casale wrote: >> Trying to sniff Ethernet packets, I do this: >> >> s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) >> >> but it results in this: >> >> $ sudo python3 sniff_survey.py >> Traceback (most recent call last): >> File "sniff_survey.py", line 118, in >> s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) >> File "/usr/lib/python3.2/socket.py", line 94, in __init__ >> _socket.socket.__init__(self, family, type, proto, fileno) >> socket.error: [Errno 93] Protocol not supported >> >> Anybody know what I'm doing wrong? (Python 3.2.3 under Debian 3.2.84-1.) > > Have a look at the bottom of this SO question: > http://stackoverflow.com/questions/5385312/ipproto-ip-vs-ipproto-tcp-ipproto-udp That discussion was helpful. Thanks. Still, I'm not out of the woods. From bmargulies's answer at that link, "IPPROTO_IP is for raw IP packets", which sounds like what I want. But when I use AF_INET, SOCK_RAW, and IPPROTO_IP, I get the "Protocol not supported" error as shown above. >From the link you provided and "man socket", I was inspired to flail about, finding this: domain type protocol result -------- ------- ---------- ----------------- AF_INET SOCK_RAW IPPROTO_IP "Protocol not supported" AF_INET SOCK_RAW IPPROTO_IPIP Sees nothing AF_INET SOCK_RAW IPPROTO_TCP Sees TCP traffic, no "dig", no "ping" AF_INET SOCK_RAW IPPROTO_UDP Sees DNS ("dig", but not "ping") AF_INET SOCK_RAW IPPROTO_ICMP Sees "ping", but not "dig" AF_INET SOCK_RAW IPPROTO_RAW Sees nothing AF_INET SOCK_RAW 0 "Protocol not supported" AF_INET SOCK_STREAM 0 "Transport endpoint is not connected" AF_INET SOCK_DGRAM 0 Sees nothing AF_INET SOCK_RDM 0 "Socket type not supported" AF_IPX SOCK_RAW IPPROTO_RAW "Socket type not supported" AF_PACKET SOCK_RAW IPPROTO_RAW Sees nothing AF_PACKET SOCK_RAW IPPROTO_TCP Sees nothing So I can receive TCP traffic through one socket, and UDP traffic through another socket, and ICMP traffic through a third; but I would like to see all IP packets, regardless of higher-level protocol, and would prefer to get them through a single pipe. (Perhaps it's unreasonable for me to ask something as high-level as a socket to give me something as low-level as a raw packet.) My starting point, by the way, was sample code for "a very simple network sniffer", presented at docs.python.org/3/library/socket.html, which opened the socket with s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) which is the line that results in the "Protocol not supported" error on my system. (That sample code is labelled as being "for Windows", so the document is not in error.) -- To email me, substitute nowhere->runbox, invalid->com. From tjreedy at udel.edu Mon Jan 16 17:27:56 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 16 Jan 2017 17:27:56 -0500 Subject: Error handling in context managers In-Reply-To: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: On 1/16/2017 1:06 PM, Israel Brewster wrote: > I generally use context managers for my SQL database connections, so I can just write code like: > > with psql_cursor() as cursor: > > > And the context manager takes care of making a connection (or getting a connection from a pool, more likely), and cleaning up after the fact (such as putting the connection back in the pool), even if something goes wrong. Simple, elegant, and works well. > > The problem is that, from time to time, I can't get a connection, the result being that cursor is None, This would be like open('bad file') returning None instead of raising FileNotFoundError. > and attempting to use it results in an AttributeError. Just as None.read would. Actually, I have to wonder about your claim. The with statement would look for cursor.__enter__ and then cursor.__exit__, and None does not have those methods. In other words, the expression following 'with' must evaluate to a context manager and None is not a context manager. >>> with None: pass Traceback (most recent call last): File "", line 1, in with None: pass AttributeError: __enter__ Is psql_cursor() returning a fake None object with __enter__ and __exit__ methods? -- Terry Jan Reedy From sg552 at hotmail.co.uk Mon Jan 16 17:42:43 2017 From: sg552 at hotmail.co.uk (Rotwang) Date: Mon, 16 Jan 2017 14:42:43 -0800 (PST) Subject: tokenize.untokenize adding line continuation characters Message-ID: Here's something odd I've found with the tokenize module: tokenizing 'if x:\n y' and then untokenizing the result adds '\\\n' to the end. Attempting to tokenize the result again fails because of the backslash continuation with nothing other than a newline after it. On the other hand, if the original string ends with a newline then it works fine. Can anyone explain why this happens? I'm using Python 3.4.3 on Windows 8. Copypasted from iPython: import tokenize, io tuple(tokenize.tokenize(io.BytesIO('if x:\n y'.encode()).readline)) Out[2]: (TokenInfo(type=56 (ENCODING), string='utf-8', start=(0, 0), end=(0, 0), line=''), TokenInfo(type=1 (NAME), string='if', start=(1, 0), end=(1, 2), line='if x:\n'), TokenInfo(type=1 (NAME), string='x', start=(1, 3), end=(1, 4), line='if x:\n'), TokenInfo(type=52 (OP), string=':', start=(1, 4), end=(1, 5), line='if x:\n'), TokenInfo(type=4 (NEWLINE), string='\n', start=(1, 5), end=(1, 6), line='if x:\n'), TokenInfo(type=5 (INDENT), string=' ', start=(2, 0), end=(2, 4), line=' y'), TokenInfo(type=1 (NAME), string='y', start=(2, 4), end=(2, 5), line=' y'), TokenInfo(type=6 (DEDENT), string='', start=(3, 0), end=(3, 0), line=''), TokenInfo(type=0 (ENDMARKER), string='', start=(3, 0), end=(3, 0), line='')) tokenize.untokenize(_).decode() Out[3]: 'if x:\n y\\\n' tuple(tokenize.tokenize(io.BytesIO(_.encode()).readline)) --------------------------------------------------------------------------- TokenError Traceback (most recent call last) in () ----> 1 tuple(tokenize.tokenize(io.BytesIO(_.encode()).readline)) C:\Program Files\Python34\lib\tokenize.py in _tokenize(readline, encoding) 558 else: # continued statement 559 if not line: --> 560 raise TokenError("EOF in multi-line statement", (lnum, 0)) 561 continued = 0 562 TokenError: ('EOF in multi-line statement', (3, 0)) From steve+comp.lang.python at pearwood.info Mon Jan 16 21:46:53 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 17 Jan 2017 13:46:53 +1100 Subject: tokenize.untokenize adding line continuation characters References: Message-ID: <587d859e$0$2804$c3e8da3$76491128@news.astraweb.com> On Tuesday 17 January 2017 09:42, Rotwang wrote: > Here's something odd I've found with the tokenize module: [...] > Copypasted from iPython: It's not impossible that iPython is doing something funny with the tokenize module. Before reporting it as a bug, I recommend that you confirm that it also occurs in the standard Python interpreter and isn't iPython specific. Just run your same code but directly from python, not the interactive iPython console. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From greg.ewing at canterbury.ac.nz Tue Jan 17 00:01:03 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 17 Jan 2017 18:01:03 +1300 Subject: Error handling in context managers In-Reply-To: References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: Israel Brewster wrote: > The problem is that, from time to time, I can't get a connection, the result > being that cursor is None, That's your problem right there -- you want a better-behaved version of psql_cursor(). def get_psql_cursor(): c = psql_cursor() if c is None: raise CantGetAConnectionError() return c with get_psql_cursor() as c: ... -- Greg From greg.ewing at canterbury.ac.nz Tue Jan 17 00:01:54 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 17 Jan 2017 18:01:54 +1300 Subject: Error handling in context managers In-Reply-To: References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: Terry Reedy wrote: > Traceback (most recent call last): > File "", line 1, in > with None: pass > AttributeError: __enter__ Like he said, you get an AttributeError! -- Greg From steve+comp.lang.python at pearwood.info Tue Jan 17 02:05:35 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 17 Jan 2017 18:05:35 +1100 Subject: Emulating Final classes in Python Message-ID: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> I wish to emulate a "final" class using Python, similar to bool: py> class MyBool(bool): ... pass ... Traceback (most recent call last): File "", line 1, in TypeError: type 'bool' is not an acceptable base type It doesn't have to be absolutely bulletproof, but anyone wanting to subclass my class should need to work for it, which hopefully will tell them that they're doing something unsupported. Any hints? -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From ethan at stoneleaf.us Tue Jan 17 02:25:58 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 16 Jan 2017 23:25:58 -0800 Subject: Emulating Final classes in Python In-Reply-To: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> Message-ID: <587DC706.4090902@stoneleaf.us> On 01/16/2017 11:05 PM, Steven D'Aprano wrote: > I wish to emulate a "final" class using Python, similar to bool: > > py> class MyBool(bool): > ... pass > ... > Traceback (most recent call last): > File "", line 1, in > TypeError: type 'bool' is not an acceptable base type > > > It doesn't have to be absolutely bulletproof, but anyone wanting to subclass my > class should need to work for it, which hopefully will tell them that they're > doing something unsupported. > > Any hints? Use a metaclass. Have the metaclass create the first one, and refuse to make any others. -- ~Ethan~ From steve+comp.lang.python at pearwood.info Tue Jan 17 02:32:53 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 17 Jan 2017 18:32:53 +1100 Subject: Emulating Final classes in Python References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> Message-ID: <587dc8a8$0$1589$c3e8da3$5496439d@news.astraweb.com> On Tuesday 17 January 2017 18:05, Steven D'Aprano wrote: > I wish to emulate a "final" class using Python, similar to bool: [...] > Any hints? I may have a solution: here's a singleton (so more like None than bools) where instantiating the class returns the singleton, and subclassing the class fails: class DoneMeta(type): _final = None def __new__(meta, name, bases, ns): if meta._final is None: meta._final = cls = super().__new__(meta, name, bases, ns) return cls elif meta._final in bases: # Not sure this check is needed. raise TypeError('base class is final and cannot be subclassed') class DoneType(metaclass=DoneMeta): __slots__ = () _instance = None def __new__(cls): if cls._instance is None: cls._instance = inst = super().__new__(cls) return inst return cls._instance def __repr__(self): return '' DONE = DoneType() del DoneType, DoneMeta assert type(DONE)() is DONE Thoughts? Any improvements or criticism? (Apart from "don't do it" :-) Of course this is Python, and so we can actually dig under the hood and make a new instance if we really try: py> x = object.__new__(type(DONE)) py> x py> x is DONE False and similarly there are probably ways to bypass the anti-subclassing code. But the way I see it, if you're doing that, you either know what you're doing in which case good on you, or you don't, in which case you deserve whatever happens to you :-) -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From __peter__ at web.de Tue Jan 17 03:34:13 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 17 Jan 2017 09:34:13 +0100 Subject: Error handling in context managers References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: Gregory Ewing wrote: > Israel Brewster wrote: >> The problem is that, from time to time, I can't get a connection, the >> result being that cursor is None, > > That's your problem right there -- you want a better-behaved > version of psql_cursor(). > > def get_psql_cursor(): > c = psql_cursor() > if c is None: > raise CantGetAConnectionError() > return c > > with get_psql_cursor() as c: > ... You still need to catch the error -- which leads to option (3) in my zoo, the only one that is actually usable. If one contextmanager cannot achieve what you want, use two: $ cat conditional_context_raise.py import sys from contextlib import contextmanager class NoConnection(Exception): pass class Cursor: def execute(self, sql): print("EXECUTING", sql) @contextmanager def cursor(): if "--fail" in sys.argv: raise NoConnection yield Cursor() @contextmanager def no_connection(): try: yield except NoConnection: print("no connection") with no_connection(), cursor() as cs: cs.execute("insert into...") $ python3 conditional_context_raise.py EXECUTING insert into... $ python3 conditional_context_raise.py --fail no connection If you want to ignore the no-connection case use contextlib.suppress(NoConnection) instead of the custom no_connection() manager. From larry at hastings.org Tue Jan 17 03:40:39 2017 From: larry at hastings.org (Larry Hastings) Date: Tue, 17 Jan 2017 00:40:39 -0800 Subject: [RELEASED] Python 3.4.6 and Python 3.5.3 are now available Message-ID: On behalf of the Python development community and the Python 3.4 and Python 3.5 release teams, I'm delighted to announce the availability of Python 3.4.6 and Python 3.5.3. Python 3.4 is now in "security fixes only" mode. This is the final stage of support for Python 3.4. Python 3.4 now only receives security fixes, not bug fixes, and Python 3.4 releases are source code only--no more official binary installers will be produced. Python 3.5 is still in active "bug fix" mode. Python 3.5.3 contains many incremental improvements over Python 3.5.2. There were literally no code changes between rc1 and final for either release. The only change--apart from the necessary updates from "rc1" to final--was a single copyright notice update for one of the OS X ".plist" property list files in 3.5.3 final. You can find Python 3.5.3 here: https://www.python.org/downloads/release/python-353/ And you can find Python 3.4.6 here: https://www.python.org/downloads/release/python-346/ Best wishes, //arry/ From antoon.pardon at rece.vub.ac.be Tue Jan 17 04:37:04 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Tue, 17 Jan 2017 10:37:04 +0100 Subject: Emulating Final classes in Python In-Reply-To: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> Message-ID: <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> Op 17-01-17 om 08:05 schreef Steven D'Aprano: > I wish to emulate a "final" class using Python, similar to bool: > > py> class MyBool(bool): > ... pass > ... > Traceback (most recent call last): > File "", line 1, in > TypeError: type 'bool' is not an acceptable base type > > > It doesn't have to be absolutely bulletproof, but anyone wanting to subclass my > class should need to work for it, which hopefully will tell them that they're > doing something unsupported. > > Any hints? I find those kind of classes annoying as hell and nobody has ever given me a good reason for them. What good was it to change Lock from a factory function to a class if you can't subclass the result anyway. The result will probably be that users that would prefer to subclass your class will monkey-patch it. Something like: class MyLock: def __init__(self): self.lock = Lock() def __getattr__(self, attr): return getattr(self.lock, attr) So I wonder what reasons do you have prefering that your users monkey-patch your class instead of subclassing it? -- Antoon Pardon From python at lucidity.plus.com Tue Jan 17 04:54:21 2017 From: python at lucidity.plus.com (Erik) Date: Tue, 17 Jan 2017 09:54:21 +0000 Subject: Emulating Final classes in Python In-Reply-To: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> Message-ID: Hi Steven, On 17/01/17 07:05, Steven D'Aprano wrote: > I wish to emulate a "final" class using Python, similar to bool: [snip] > It doesn't have to be absolutely bulletproof, but anyone wanting to subclass my > class should need to work for it, which hopefully will tell them that they're > doing something unsupported. When someone brings up something like this you usually quote the "consenting adults" argument, so I'll throw it back at you - why not just add it to your documentation? E. From sg552 at hotmail.co.uk Tue Jan 17 05:30:34 2017 From: sg552 at hotmail.co.uk (sg552 at hotmail.co.uk) Date: Tue, 17 Jan 2017 02:30:34 -0800 (PST) Subject: tokenize.untokenize adding line continuation characters In-Reply-To: <587d859e$0$2804$c3e8da3$76491128@news.astraweb.com> References: <587d859e$0$2804$c3e8da3$76491128@news.astraweb.com> Message-ID: <883349aa-467c-450d-87e6-487099cfe5ab@googlegroups.com> On Tuesday, January 17, 2017 at 2:47:03 AM UTC, Steven D'Aprano wrote: > On Tuesday 17 January 2017 09:42, Rotwang wrote: > > > Here's something odd I've found with the tokenize module: > [...] > > Copypasted from iPython: > > It's not impossible that iPython is doing something funny with the tokenize > module. It happens outside iPython: 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 io, tokenize >>> tokenize.untokenize(tokenize.tokenize(io.BytesIO('if x:\n y'.encode()).readline)).decode() 'if x:\n y\\\n' and also in Python 2: Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import io, tokenize >>> tokenize.untokenize(tokenize.generate_tokens(io.BytesIO('if x:\n y').readline)) 'if x:\n y\\\n' > Before reporting it as a bug, I recommend that you confirm that it also > occurs in the standard Python interpreter and isn't iPython specific. Is this behaviour actually a bug, as opposed to a feature I don't understand? From __peter__ at web.de Tue Jan 17 06:10:37 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 17 Jan 2017 12:10:37 +0100 Subject: tokenize.untokenize adding line continuation characters References: Message-ID: Rotwang wrote: > Here's something odd I've found with the tokenize module: tokenizing 'if > x:\n y' and then untokenizing the result adds '\\\n' to the end. > Attempting to tokenize the result again fails because of the backslash > continuation with nothing other than a newline after it. On the other > hand, if the original string ends with a newline then it works fine. Can > anyone explain why this happens? > I'm using Python 3.4.3 on Windows 8. Copypasted from iPython: This looks like a bug... $ python3.4 -c 'import tokenize as t, io; print(t.untokenize(t.tokenize(io.BytesIO(b"if x:\n y").readline)))' b'if x:\n y\\\n' ...that is fixed now: $ python3.6 -c 'import tokenize as t, io; print(t.untokenize(t.tokenize(io.BytesIO(b"if x:\n y").readline)))' b'if x:\n y' A quick search brought up multiple bug reports, among them http://bugs.python.org/issue12691 From sg552 at hotmail.co.uk Tue Jan 17 06:20:40 2017 From: sg552 at hotmail.co.uk (Rotwang) Date: Tue, 17 Jan 2017 03:20:40 -0800 (PST) Subject: tokenize.untokenize adding line continuation characters In-Reply-To: References: Message-ID: <320abf24-1062-42c7-b2e2-93ced2ab12c3@googlegroups.com> On Tuesday, January 17, 2017 at 11:11:27 AM UTC, Peter Otten wrote: > Rotwang wrote: > > > Here's something odd I've found with the tokenize module: tokenizing 'if > > x:\n y' and then untokenizing the result adds '\\\n' to the end. > > Attempting to tokenize the result again fails because of the backslash > > continuation with nothing other than a newline after it. On the other > > hand, if the original string ends with a newline then it works fine. Can > > anyone explain why this happens? > > > I'm using Python 3.4.3 on Windows 8. Copypasted from iPython: > > This looks like a bug... > > $ python3.4 -c 'import tokenize as t, io; > print(t.untokenize(t.tokenize(io.BytesIO(b"if x:\n y").readline)))' > b'if x:\n y\\\n' > > ...that is fixed now: > > $ python3.6 -c 'import tokenize as t, io; > print(t.untokenize(t.tokenize(io.BytesIO(b"if x:\n y").readline)))' > b'if x:\n y' > > A quick search brought up multiple bug reports, among them > > http://bugs.python.org/issue12691 Ah, thanks. I did search for bug reports before I started this thread but didn't find anything. From rosuav at gmail.com Tue Jan 17 11:57:51 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 18 Jan 2017 03:57:51 +1100 Subject: Python, asyncio, and systemd Message-ID: If I write a web server using asyncio (and the aiohttp package), I can spin up the server with: await loop.create_server(app.make_handler(), "0.0.0.0", 8080) This works fine for a high port, but if I want to bind to port 80, I need to either start as root and then drop privileges, or get given the socket by someone else. With systemd, the latter is an option; you configure a service unit and a socket unit, and when the service gets started, it's given a bound listening socket as FD 3. Most of the work is pretty straight-forward, but I ran into one problem. The event loop's create_server() method calls a private _start_serving method, which means I can't (or rather, I shouldn't) just replicate create_server. This works, but it's naughty: sock = socket.socket(fileno=3) sock.setblocking(False) loop._start_serving(app.make_handler(), sock) What's the official way to say to asyncio "here's a listening socket, start managing it for me"? ChrisA From ian.g.kelly at gmail.com Tue Jan 17 12:06:53 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 17 Jan 2017 10:06:53 -0700 Subject: Python, asyncio, and systemd In-Reply-To: References: Message-ID: On Tue, Jan 17, 2017 at 9:57 AM, Chris Angelico wrote: > If I write a web server using asyncio (and the aiohttp package), I can > spin up the server with: > > await loop.create_server(app.make_handler(), "0.0.0.0", 8080) > > This works fine for a high port, but if I want to bind to port 80, I > need to either start as root and then drop privileges, or get given > the socket by someone else. With systemd, the latter is an option; you > configure a service unit and a socket unit, and when the service gets > started, it's given a bound listening socket as FD 3. > > Most of the work is pretty straight-forward, but I ran into one > problem. The event loop's create_server() method calls a private > _start_serving method, which means I can't (or rather, I shouldn't) > just replicate create_server. This works, but it's naughty: > > sock = socket.socket(fileno=3) > > sock.setblocking(False) > loop._start_serving(app.make_handler(), sock) > > What's the official way to say to asyncio "here's a listening socket, > start managing it for me"? I haven't tried this but create_server takes a "sock" keyword-argument. https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.create_server From christian at python.org Tue Jan 17 12:19:05 2017 From: christian at python.org (Christian Heimes) Date: Tue, 17 Jan 2017 18:19:05 +0100 Subject: Python, asyncio, and systemd In-Reply-To: References: Message-ID: On 2017-01-17 17:57, Chris Angelico wrote: > If I write a web server using asyncio (and the aiohttp package), I can > spin up the server with: > > await loop.create_server(app.make_handler(), "0.0.0.0", 8080) > > This works fine for a high port, but if I want to bind to port 80, I > need to either start as root and then drop privileges, or get given > the socket by someone else. With systemd, the latter is an option; you > configure a service unit and a socket unit, and when the service gets > started, it's given a bound listening socket as FD 3. > > Most of the work is pretty straight-forward, but I ran into one > problem. The event loop's create_server() method calls a private > _start_serving method, which means I can't (or rather, I shouldn't) > just replicate create_server. This works, but it's naughty: > > sock = socket.socket(fileno=3) > > sock.setblocking(False) > loop._start_serving(app.make_handler(), sock) > > What's the official way to say to asyncio "here's a listening socket, > start managing it for me"? Hi Chris, you might be interested in ticket [1], my module socketfromfd [2] and some code I wrote for a HTTP server with socket activation [3]. The latter does not use asyncio but shows how to use socket activation with systemd. The blog posting [4] has some example code in case you don't want to use systemd's Python module. [1] https://bugs.python.org/issue28134 [2] https://github.com/tiran/socketfromfd [3] https://github.com/latchset/custodia/blob/master/custodia/httpd/server.py#L491 [4] http://0pointer.de/blog/projects/socket-activation.html Christian From rosuav at gmail.com Tue Jan 17 13:03:04 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 18 Jan 2017 05:03:04 +1100 Subject: Python, asyncio, and systemd In-Reply-To: References: Message-ID: On Wed, Jan 18, 2017 at 4:06 AM, Ian Kelly wrote: >> What's the official way to say to asyncio "here's a listening socket, >> start managing it for me"? > > I haven't tried this but create_server takes a "sock" keyword-argument. > > https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.create_server Oh. Wow. I am blind, or an idiot. That is exactly what I'm looking for. My code now looks like this: if sock: srv = await loop.create_server(app.make_handler(), sock=sock) else: srv = await loop.create_server(app.make_handler(), "0.0.0.0", port) sock = srv.sockets[0] print("Listening on %s:%s" % sock.getsockname(), file=sys.stderr) According to the docs, host and port must be unset (None) if sock is given. It'd be nice if they could simply be ignored, in which case I could just pass in all three parameters. (Yes, I'm forcing this to IPv4 for this app, rather than using None or "::" to accept IPv6 as well. If you're copying this code into your app, you should probably accept IPv6, unless you know otherwise.) But wow, I just completely didn't see that. I am incredible. Thank you Ian! ChrisA From EIzydore at zoll.com Tue Jan 17 13:23:58 2017 From: EIzydore at zoll.com (Earl Izydore) Date: Tue, 17 Jan 2017 18:23:58 +0000 Subject: Python 3.6 Installation Message-ID: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> I having problems installing Python 3.6. I was using Python 2.7 successfully. Today, I installed python-3.6.0.exe. At the end of the installation I got a message saying the installation was successful. When attempt to start Python, I get the following Application Error: The application was unable to start correctly (0xc000007b). Click OK to close the application. I am using Windows 7 Professional, 64 bit. Can you help? Thanks Earl Izydore 412-968-3333 ext. 14663 eizydore at zoll.com [http://www.zoll.com/uploadedImages/images/emailsignaturelogo9_09.jpg] An Asahi Kasei Group Company From israel at ravnalaska.net Tue Jan 17 13:54:08 2017 From: israel at ravnalaska.net (Israel Brewster) Date: Tue, 17 Jan 2017 09:54:08 -0900 Subject: Error handling in context managers In-Reply-To: References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: On Jan 16, 2017, at 1:27 PM, Terry Reedy wrote: > > On 1/16/2017 1:06 PM, Israel Brewster wrote: >> I generally use context managers for my SQL database connections, so I can just write code like: >> >> with psql_cursor() as cursor: >> >> >> And the context manager takes care of making a connection (or getting a connection from a pool, more likely), and cleaning up after the fact (such as putting the connection back in the pool), even if something goes wrong. Simple, elegant, and works well. >> >> The problem is that, from time to time, I can't get a connection, the result being that cursor is None, > > This would be like open('bad file') returning None instead of raising FileNotFoundError. > >> and attempting to use it results in an AttributeError. > > Just as None.read would. > > Actually, I have to wonder about your claim. The with statement would look for cursor.__enter__ and then cursor.__exit__, and None does not have those methods. In other words, the expression following 'with' must evaluate to a context manager and None is not a context manager. > > >>> with None: pass > > Traceback (most recent call last): > File "", line 1, in > with None: pass > AttributeError: __enter__ > > Is psql_cursor() returning a fake None object with __enter__ and __exit__ methods? No, the *context manager*, which I call in the with *does* have __enter__ and __exit__ methods. It's just that the __enter__ method returns None when it can't get a connection. So the expression following with *does* evaluate to a context manager, but the expression following as evaluates to None. ----------------------------------------------- Israel Brewster Systems Analyst II Ravn Alaska 5245 Airport Industrial Rd Fairbanks, AK 99709 (907) 450-7293 ----------------------------------------------- > > -- > Terry Jan Reedy > > -- > https://mail.python.org/mailman/listinfo/python-list From israel at ravnalaska.net Tue Jan 17 13:55:35 2017 From: israel at ravnalaska.net (Israel Brewster) Date: Tue, 17 Jan 2017 09:55:35 -0900 Subject: Error handling in context managers In-Reply-To: References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: <76F831C9-20B9-4B4A-8557-0AB1C10C2EF6@ravnalaska.net> On Jan 16, 2017, at 8:01 PM, Gregory Ewing wrote: > > Israel Brewster wrote: >> The problem is that, from time to time, I can't get a connection, the result >> being that cursor is None, > > That's your problem right there -- you want a better-behaved > version of psql_cursor(). > > def get_psql_cursor(): > c = psql_cursor() > if c is None: > raise CantGetAConnectionError() > return c > > with get_psql_cursor() as c: > ... > Ok, fair enough. So I get a better exception, raised at the proper time. This is, in fact, better - but doesn't actually change how I would *handle* the exception :-) ----------------------------------------------- Israel Brewster Systems Analyst II Ravn Alaska 5245 Airport Industrial Rd Fairbanks, AK 99709 (907) 450-7293 ----------------------------------------------- > -- > Greg > -- > https://mail.python.org/mailman/listinfo/python-list From israel at ravnalaska.net Tue Jan 17 13:59:16 2017 From: israel at ravnalaska.net (Israel Brewster) Date: Tue, 17 Jan 2017 09:59:16 -0900 Subject: Error handling in context managers In-Reply-To: References: <8932DADB-D790-4A62-849A-4CB48D76E65A@ravnalaska.net> Message-ID: <4A29C931-8241-4175-ACE0-B472910DD701@ravnalaska.net> On Jan 16, 2017, at 11:34 PM, Peter Otten <__peter__ at web.de> wrote: > > Gregory Ewing wrote: > >> Israel Brewster wrote: >>> The problem is that, from time to time, I can't get a connection, the >>> result being that cursor is None, >> >> That's your problem right there -- you want a better-behaved >> version of psql_cursor(). >> >> def get_psql_cursor(): >> c = psql_cursor() >> if c is None: >> raise CantGetAConnectionError() >> return c >> >> with get_psql_cursor() as c: >> ... > > You still need to catch the error -- which leads to option (3) in my zoo, > the only one that is actually usable. If one contextmanager cannot achieve > what you want, use two: > > $ cat conditional_context_raise.py > import sys > from contextlib import contextmanager > > class NoConnection(Exception): > pass > > class Cursor: > def execute(self, sql): > print("EXECUTING", sql) > > @contextmanager > def cursor(): > if "--fail" in sys.argv: > raise NoConnection > yield Cursor() > > @contextmanager > def no_connection(): > try: > yield > except NoConnection: > print("no connection") > > with no_connection(), cursor() as cs: > cs.execute("insert into...") > $ python3 conditional_context_raise.py > EXECUTING insert into... > $ python3 conditional_context_raise.py --fail > no connection > > If you want to ignore the no-connection case use > contextlib.suppress(NoConnection) instead of the custom no_connection() > manager. Fun :-) I'll have to play around with that. Thanks! :-) ----------------------------------------------- Israel Brewster Systems Analyst II Ravn Alaska 5245 Airport Industrial Rd Fairbanks, AK 99709 (907) 450-7293 ----------------------------------------------- > > -- > https://mail.python.org/mailman/listinfo/python-list From ethan at stoneleaf.us Tue Jan 17 14:14:13 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 17 Jan 2017 11:14:13 -0800 Subject: Emulating Final classes in Python In-Reply-To: <587dc8a8$0$1589$c3e8da3$5496439d@news.astraweb.com> References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <587dc8a8$0$1589$c3e8da3$5496439d@news.astraweb.com> Message-ID: <587E6D05.8080603@stoneleaf.us> On 01/16/2017 11:32 PM, Steven D'Aprano wrote: > On Tuesday 17 January 2017 18:05, Steven D'Aprano wrote: > >> I wish to emulate a "final" class using Python, similar to bool: > > I may have a solution: here's a singleton (so more like None than bools) where > instantiating the class returns the singleton, and subclassing the class fails: > > class DoneMeta(type): > _final = None > def __new__(meta, name, bases, ns): > if meta._final is None: > meta._final = cls = super().__new__(meta, name, bases, ns) > return cls > elif meta._final in bases: # Not sure this check is needed. > raise TypeError('base class is final and cannot be subclassed') This will make DoneMeta a one-shot, meaning you'll have to make more DoneMeta's if you need more than one unsubclassable class. > class DoneType(metaclass=DoneMeta): > __slots__ = () > _instance = None > def __new__(cls): > if cls._instance is None: > cls._instance = inst = super().__new__(cls) > return inst > return cls._instance > def __repr__(self): > return '' And this has to do with single instances, which is not what you asked about. Here's some sample code that creates a Final class; any class that subclasses from it cannot be further subclassed: -- 8< ------------------------------------------------------------ Final = None class FinalMeta(type): def __new__(metacls, cls, bases, clsdict): print('-' * 50) print('class: ', cls) print('bases: ', bases) if Final is not None: for base in bases: if base is not Final and issubclass(base, Final): print('should raise') print('-' * 50) return type.__new__(metacls, cls, bases, clsdict) class Final(metaclass=FinalMeta): pass class One(Final): pass class Two(One): pass class Three(Two): pass class Ten(Final): pass -- 8< ------------------------------------------------------------ Change the "should raise" to a raise, remove the other print()s, and away you go. Should work in any Python 3. -- ~Ethan~ From tjreedy at udel.edu Tue Jan 17 17:31:15 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 17 Jan 2017 17:31:15 -0500 Subject: Python 3.6 Installation In-Reply-To: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> References: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> Message-ID: On 1/17/2017 1:23 PM, Earl Izydore wrote: > I having problems installing Python 3.6. I was using Python 2.7 > successfully. > > Today, I installed python-3.6.0.exe. Which binary? from where? > At the end of the installation I got a message saying the > installation was successful. > > When attempt to start Python, I get the following Application Error: > The application was unable to start correctly (0xc000007b). Click OK > to close the application. Searching the web for 0xc000007b gets many hits and suggestions. For python, searching stackoverflow.con is probably better. https://stackoverflow.com/questions/20650596/cannot-open-python-error-0xc000007b is only one of the hits. > I am using Windows 7 Professional, 64 bit. -- Terry Jan Reedy From steve+python at pearwood.info Tue Jan 17 18:25:36 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 18 Jan 2017 10:25:36 +1100 Subject: Emulating Final classes in Python References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> Message-ID: <587ea7f2$0$22141$c3e8da3$5496439d@news.astraweb.com> On Tue, 17 Jan 2017 08:54 pm, Erik wrote: > Hi Steven, > > On 17/01/17 07:05, Steven D'Aprano wrote: >> I wish to emulate a "final" class using Python, similar to bool: > > [snip] > >> It doesn't have to be absolutely bulletproof, but anyone wanting to >> subclass my class should need to work for it, which hopefully will tell >> them that they're doing something unsupported. > > When someone brings up something like this you usually quote the > "consenting adults" argument, so I'll throw it back at you - why not > just add it to your documentation? A very good question :-) Why am I doing this? Part of the answer is simply because I can. I want to see how far I can go to lock down the class in pure Python code. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Tue Jan 17 18:51:03 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 18 Jan 2017 10:51:03 +1100 Subject: Emulating Final classes in Python References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <587dc8a8$0$1589$c3e8da3$5496439d@news.astraweb.com> <587E6D05.8080603@stoneleaf.us> Message-ID: <587eade9$0$1617$c3e8da3$5496439d@news.astraweb.com> On Wed, 18 Jan 2017 06:14 am, Ethan Furman wrote: > On 01/16/2017 11:32 PM, Steven D'Aprano wrote: >> On Tuesday 17 January 2017 18:05, Steven D'Aprano wrote: >> >>> I wish to emulate a "final" class using Python, similar to bool: >> >> I may have a solution: here's a singleton (so more like None than bools) >> where instantiating the class returns the singleton, and subclassing the >> class fails: >> >> class DoneMeta(type): >> _final = None >> def __new__(meta, name, bases, ns): >> if meta._final is None: >> meta._final = cls = super().__new__(meta, name, bases, ns) >> return cls >> elif meta._final in bases: # Not sure this check is needed. >> raise TypeError('base class is final and cannot be >> subclassed') > > This will make DoneMeta a one-shot, meaning you'll have to make more > DoneMeta's if you need more than one unsubclassable class. For my purposes, that's enough -- I only have one object that I actually need, something which is like None or Ellipsis or NotImplemented, just a unique and featureless point-particle with no state or behaviour (apart from a neat repr). Yes yes, it's that damn singleton design (anti-)pattern again :-) It's a bit ... something ... that to make a single stateless, behaviourless instance I need a class and a metaclass, but that's Python for you. As an alternative, I may use a single Enum, and delete the class object after I've extracted the enumeration: from enum import Enum class DoneType(Enum): DONE = 'Done' DONE = DoneType(DONE) del DoneType Although enumerations have more behaviour than I need, maybe this is close enough that I don't care. >> class DoneType(metaclass=DoneMeta): >> __slots__ = () >> _instance = None >> def __new__(cls): >> if cls._instance is None: >> cls._instance = inst = super().__new__(cls) >> return inst >> return cls._instance >> def __repr__(self): >> return '' > > And this has to do with single instances, which is not what you asked > about. Very observant of you :-) > Here's some sample code that creates a Final class; any class that > subclasses from it cannot be further subclassed: Thanks. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From torriem at gmail.com Tue Jan 17 20:30:36 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 17 Jan 2017 18:30:36 -0700 Subject: Python 3.6 Installation In-Reply-To: References: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> Message-ID: <4ef0f5b2-01f0-72ae-b6e7-add88d5bacc0@gmail.com> On 01/17/2017 03:31 PM, Terry Reedy wrote: > On 1/17/2017 1:23 PM, Earl Izydore wrote: >> I having problems installing Python 3.6. I was using Python 2.7 >> successfully. >> >> Today, I installed python-3.6.0.exe. > > Which binary? from where? > >> At the end of the installation I got a message saying the >> installation was successful. >> >> When attempt to start Python, I get the following Application Error: >> The application was unable to start correctly (0xc000007b). Click OK >> to close the application. > > Searching the web for 0xc000007b gets many hits and suggestions. For > python, searching stackoverflow.con is probably better. > > https://stackoverflow.com/questions/20650596/cannot-open-python-error-0xc000007b > > is only one of the hits. > >> I am using Windows 7 Professional, 64 bit. Yes googling error messages is a good idea. However the SO link seems to describe this problem as a missing DLL, probably the VS 2015 runtime redistributable library. If this is the problem, why isn't Python's installer bundling the runtime for optional install? Many other installers do this. Judging by the frequency of posts like this one to this list, it's a problem that affects more than a few users. Anyway, it seems like this kind of runtime error is cropping up more and more for folks. From steve+comp.lang.python at pearwood.info Tue Jan 17 21:10:41 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 18 Jan 2017 13:10:41 +1100 Subject: Emulating Final classes in Python References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> Message-ID: <587ecea3$0$1522$c3e8da3$5496439d@news.astraweb.com> On Tuesday 17 January 2017 20:37, Antoon Pardon wrote: > Op 17-01-17 om 08:05 schreef Steven D'Aprano: >> I wish to emulate a "final" class using Python, similar to bool: >> >> py> class MyBool(bool): >> ... pass >> ... >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: type 'bool' is not an acceptable base type [...] > I find those kind of classes annoying as hell and nobody has ever given me a > good reason for them. What good was it to change Lock from a factory function > to a class if you can't subclass the result anyway. I'm not sure which Lock class you're referring to. > The result will probably be that users that would prefer to subclass your > class will monkey-patch it. Something like: > > class MyLock: > def __init__(self): > self.lock = Lock() > > def __getattr__(self, attr): > return getattr(self.lock, attr) That technique is called "delegation", or sometimes "composition". > So I wonder what reasons do you have prefering that your users monkey-patch > your class instead of subclassing it? Since my class provides no useful behaviour, I don't think anyone will seriously have any reason to subclass it. Python has at least three singleton instances which are used purely as abstract symbols: they have no state, and very little behaviour besides a nice repr. They are None, NotImplemented and Ellipsis. I'm effectively trying to make my own abstract symbol. But it's mostly a learning exercise. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From steve+comp.lang.python at pearwood.info Tue Jan 17 21:12:22 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 18 Jan 2017 13:12:22 +1100 Subject: Python 3.6 Installation References: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> <4ef0f5b2-01f0-72ae-b6e7-add88d5bacc0@gmail.com> Message-ID: <587ecf06$0$1522$c3e8da3$5496439d@news.astraweb.com> On Wednesday 18 January 2017 12:30, Michael Torrie wrote: > Yes googling error messages is a good idea. However the SO link seems to > describe this problem as a missing DLL, probably the VS 2015 runtime > redistributable library. If this is the problem, why isn't Python's > installer bundling the runtime for optional install? Many other > installers do this. Judging by the frequency of posts like this one to > this list, it's a problem that affects more than a few users. > > Anyway, it seems like this kind of runtime error is cropping up more and > more for folks. Perhaps some Python-on-Windows user who cares about this issue should raise it on the bug tracker as a feature request. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From torriem at gmail.com Tue Jan 17 23:32:46 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 17 Jan 2017 21:32:46 -0700 Subject: Python 3.6 Installation In-Reply-To: <587ecf06$0$1522$c3e8da3$5496439d@news.astraweb.com> References: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> <4ef0f5b2-01f0-72ae-b6e7-add88d5bacc0@gmail.com> <587ecf06$0$1522$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5dd005ca-1022-0521-d70c-b8f007477663@gmail.com> On 01/17/2017 07:12 PM, Steven D'Aprano wrote: > On Wednesday 18 January 2017 12:30, Michael Torrie wrote: > >> Yes googling error messages is a good idea. However the SO link seems to >> describe this problem as a missing DLL, probably the VS 2015 runtime >> redistributable library. If this is the problem, why isn't Python's >> installer bundling the runtime for optional install? Many other >> installers do this. Judging by the frequency of posts like this one to >> this list, it's a problem that affects more than a few users. >> >> Anyway, it seems like this kind of runtime error is cropping up more and >> more for folks. > > Perhaps some Python-on-Windows user who cares about this issue should raise it > on the bug tracker as a feature request. I did a quick search of the issue tracker and now I'm confused. Apparently the installer does bundle the runtime with Python. And if Python is compiled against this new MS universal runtime that is supposed to be in all supported versions of windows (from windows update) why is Visual Studio 2015 redistributable required, as per the SO link Terry linked to? http://bugs.python.org/issue28592 -- Erik mentions that the runtime is bundled with the installer, but probably shouldn't be since it's a windows update thing now... From ethan at stoneleaf.us Tue Jan 17 23:42:28 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 17 Jan 2017 20:42:28 -0800 Subject: Emulating Final classes in Python In-Reply-To: <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> Message-ID: <587EF234.90502@stoneleaf.us> On 01/17/2017 01:37 AM, Antoon Pardon wrote: > Op 17-01-17 om 08:05 schreef Steven D'Aprano: >> I wish to emulate a "final" class using Python, similar to bool: >> >> py> class MyBool(bool): >> ... pass >> ... >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: type 'bool' is not an acceptable base type > > I find those kind of classes annoying as hell and nobody has ever given me a good > reason for them. Subclassing an Enum that has members can be confusing -- subclassed members pass isinstance checks but fail to show up in the parent class' iteration. Subclassing a new data type, such as one with Yes, No, and Maybe, with lots of code dealing explicitly with those three singletons, would be confusing if not outright broken. Both those cases are good candidates for disallowing subclassing. -- ~Ethan~ From tjreedy at udel.edu Wed Jan 18 01:23:34 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 18 Jan 2017 01:23:34 -0500 Subject: Python 3.6 Installation In-Reply-To: <5dd005ca-1022-0521-d70c-b8f007477663@gmail.com> References: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> <4ef0f5b2-01f0-72ae-b6e7-add88d5bacc0@gmail.com> <587ecf06$0$1522$c3e8da3$5496439d@news.astraweb.com> <5dd005ca-1022-0521-d70c-b8f007477663@gmail.com> Message-ID: On 1/17/2017 11:32 PM, Michael Torrie wrote: > On 01/17/2017 07:12 PM, Steven D'Aprano wrote: >> On Wednesday 18 January 2017 12:30, Michael Torrie wrote: >> >>> Yes googling error messages is a good idea. However the SO link seems to >>> describe this problem as a missing DLL, probably the VS 2015 runtime >>> redistributable library. If this is the problem, why isn't Python's >>> installer bundling the runtime for optional install? Many other >>> installers do this. Judging by the frequency of posts like this one to >>> this list, it's a problem that affects more than a few users. >>> >>> Anyway, it seems like this kind of runtime error is cropping up more and >>> more for folks. >> >> Perhaps some Python-on-Windows user who cares about this issue should raise it >> on the bug tracker as a feature request. > > I did a quick search of the issue tracker and now I'm confused. > Apparently the installer does bundle the runtime with Python. And if > Python is compiled against this new MS universal runtime that is > supposed to be in all supported versions of windows (from windows > update) Not everyone has run Windows update since the current runtime was released. why is Visual Studio 2015 redistributable required, as per the > SO link Terry linked to? > > http://bugs.python.org/issue28592 -- Erik mentions that the runtime is > bundled with the installer, but probably shouldn't be since it's a > windows update thing now... > -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Jan 18 02:05:33 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Wed, 18 Jan 2017 18:05:33 +1100 Subject: Emulating Final classes in Python References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> <587EF234.90502@stoneleaf.us> Message-ID: <587f13c0$0$1590$c3e8da3$5496439d@news.astraweb.com> On Wednesday 18 January 2017 15:42, Ethan Furman wrote: [...] > Both those cases are good candidates for disallowing subclassing. I've given a metaclass that disallows subclassing: class MyClass(MyParent, metaclass=FinalMeta): ... Ethan took that one step further by giving a class you inherit from to disallow subclassing: class MyClass(MyParent, Final): ... Could we solve this problem with a decorator? @final class MyClass(MyParent): ... Without needing to add any special magic to MyParent or MyClass, apart from the decorator, can we make MyClass final? That would (in principle) allow us to make a subclass, and *then* set the class final so that no more subclasses could be made. I thought that the decorator could simply set the class' metaclass: def final(cls): if cls.__class__ is type: cls.__class__ = Meta return cls raise TypeErrror('Possible metaclass conflict') but that's disallowed. Any ideas? -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From cs at zip.com.au Wed Jan 18 05:49:31 2017 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 18 Jan 2017 21:49:31 +1100 Subject: trouble with cmd.Cmd and prompting In-Reply-To: <20170104012018.GA72609@cskk.homeip.net> References: <20170104012018.GA72609@cskk.homeip.net> Message-ID: <20170118104931.GA98212@cskk.homeip.net> On 04Jan2017 12:20, Cameron Simpson wrote: >I will try to get a minimal example others can run. Well I've made some time for this, and it seems to be an interaction with my python3, the "input" builtin, and readline. Cmd.cmdloop behaves fine if I turn off the .raw_input attribute, and I can reproduce the issue without the cmd module. Test code: import readline line = input("F> ") print("gap") line = input("G> ") Results on a terminal: [~]fleet*2> python3 foo.py F> a F> gap n G> % [~]fleet*> That "%" is an artifact of no final newline after the "G> ". If I comment out the import of readline it looks like this: G> % [~]fleet*> python3 foo.py F> a gap G> b [~]fleet*> as one would expect. Ignoring my shell prompts, the contents of a typescript show: python3 foo.py^M F> a^M F> gap^M b^M G> Those "^M"s are carriage returns. I kind of suspect the second "F> " is readline doing some kind of redraw. I am baffled by the lack of initial "G> " prompt; that "G> " above appears to be the secondary issue, after I've pressed return for the "b" line. This is on a Mac, so I've no strace command to hand. I've also got all manner of potential special stuff (readline library from MacPorts, etc). I'll keep stripping stuff back... Cheers, Cameron Simpson From __peter__ at web.de Wed Jan 18 07:47:47 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 18 Jan 2017 13:47:47 +0100 Subject: trouble with cmd.Cmd and prompting References: <20170104012018.GA72609@cskk.homeip.net> <20170118104931.GA98212@cskk.homeip.net> Message-ID: Cameron Simpson wrote: > On 04Jan2017 12:20, Cameron Simpson wrote: >>I will try to get a minimal example others can run. > > Well I've made some time for this, and it seems to be an interaction with > my python3, the "input" builtin, and readline. Cmd.cmdloop behaves fine if > I turn off the .raw_input attribute, and I can reproduce the issue without > the cmd module. > > Test code: > > import readline > line = input("F> ") > print("gap") > line = input("G> ") If readline is not used input() writes its prompt to stderr. I don't have a Mac, but it might be worthwhile to investigate what goes where. For comparison here's what happens on my Linux system: $ cat tmp.py import sys if "-r" in sys.argv: import readline print( "uses readline" if "readline" in sys.modules else "no readline" ) line = input("F> ") print("gap") line = input("G> ") $ python3 tmp.py no readline F> alpha gap G> beta $ python3 tmp.py 2>/dev/null no readline alpha gap beta $ python3 tmp.py -r 2>/dev/null uses readline F> alpha gap G> beta From alister.ware at ntlworld.com Wed Jan 18 08:43:10 2017 From: alister.ware at ntlworld.com (alister) Date: Wed, 18 Jan 2017 13:43:10 GMT Subject: Emulating Final classes in Python References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> <587ecea3$0$1522$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, 18 Jan 2017 13:10:41 +1100, Steven D'Aprano wrote: > On Tuesday 17 January 2017 20:37, Antoon Pardon wrote: > >> Op 17-01-17 om 08:05 schreef Steven D'Aprano: >>> I wish to emulate a "final" class using Python, similar to bool: >>> >>> py> class MyBool(bool): >>> ... pass ... >>> Traceback (most recent call last): >>> File "", line 1, in >>> TypeError: type 'bool' is not an acceptable base type > [...] >> I find those kind of classes annoying as hell and nobody has ever given >> me a good reason for them. What good was it to change Lock from a >> factory function to a class if you can't subclass the result anyway. > > I'm not sure which Lock class you're referring to. > > >> The result will probably be that users that would prefer to subclass >> your class will monkey-patch it. Something like: >> >> class MyLock: >> def __init__(self): >> self.lock = Lock() >> >> def __getattr__(self, attr): >> return getattr(self.lock, attr) > > That technique is called "delegation", or sometimes "composition". > > >> So I wonder what reasons do you have prefering that your users >> monkey-patch your class instead of subclassing it? > > Since my class provides no useful behaviour, I don't think anyone will > seriously have any reason to subclass it. if it provides "No Usefull behaviour" why does it even exist what useless behaviour does it provide? :-) (no mater how pointless there is always someone how will find a use, I even know someone who successfully sold a box of paper shredding on ebay!) > > Python has at least three singleton instances which are used purely as > abstract symbols: they have no state, and very little behaviour besides > a nice repr. They are None, NotImplemented and Ellipsis. I'm effectively > trying to make my own abstract symbol. > > But it's mostly a learning exercise. -- Good programmers treat Microsoft products as damage and route around them. -- From a Slashdot.org post From ethan at stoneleaf.us Wed Jan 18 11:24:26 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 18 Jan 2017 08:24:26 -0800 Subject: Emulating Final classes in Python In-Reply-To: <587f13c0$0$1590$c3e8da3$5496439d@news.astraweb.com> References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> <587EF234.90502@stoneleaf.us> <587f13c0$0$1590$c3e8da3$5496439d@news.astraweb.com> Message-ID: <587F96BA.8070707@stoneleaf.us> On 01/17/2017 11:05 PM, Steven D'Aprano wrote: > I've given a metaclass that disallows subclassing: > > class MyClass(MyParent, metaclass=FinalMeta): > ... > > > Ethan took that one step further by giving a class you inherit from to disallow > subclassing: > > class MyClass(MyParent, Final): > ... > > > Could we solve this problem with a decorator? > > > @final > class MyClass(MyParent): > ... > > > Without needing to add any special magic to MyParent or MyClass, apart from the > decorator, can we make MyClass final? That would (in principle) allow us to > make a subclass, and *then* set the class final so that no more subclasses > could be made. > > > I thought that the decorator could simply set the class' metaclass: > > def final(cls): > if cls.__class__ is type: > cls.__class__ = Meta > return cls > raise TypeErrror('Possible metaclass conflict') > > > but that's disallowed. Any ideas? You still need to have the FinalMeta type and Final class available, but to use a decorator you'll need to scavenge the bits from the old class to make a new class of the same name and return that: def final(cls): new_cls = Meta(cls.__name__, (Final, )+cls.__bases__, dict(cls.__dict__)) return new_cls Not sure if more is needed to handle __slots__, but this should get us started. -- ~Ethan~ From eryksun at gmail.com Wed Jan 18 12:59:05 2017 From: eryksun at gmail.com (eryk sun) Date: Wed, 18 Jan 2017 17:59:05 +0000 Subject: Python 3.6 Installation In-Reply-To: References: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> <4ef0f5b2-01f0-72ae-b6e7-add88d5bacc0@gmail.com> <587ecf06$0$1522$c3e8da3$5496439d@news.astraweb.com> <5dd005ca-1022-0521-d70c-b8f007477663@gmail.com> Message-ID: On Wed, Jan 18, 2017 at 6:23 AM, Terry Reedy wrote: > Not everyone has run Windows update since the current runtime was released. Python's installer tries (and sometimes fails) to install the KB2999226 update, which installs an old version of the Universal CRT. Some installers instead bundle the VC++ redistributable, which includes KB2999226, but that would be overkill for CPython. I thought we could phase this out in 3.6 to instead require an up-to-date OS. If people need offline updates, as opposed to using Windows update, it's better to get the most recent version directly from Microsoft, KB3118401 [1]. Steve Dower disagreed, and that's the end of it. As to the loader returning STATUS_INVALID_IMAGE_FORMAT (0xC000007B), I would run the executable/script under a debugger such as WinDbg or cdb, with loader snaps enabled via gflags. Find the troublesome DLL(s), and manually resolve the problem. [1]: https://support.microsoft.com/en-us/kb/3118401 From smith at smith.it Wed Jan 18 14:51:05 2017 From: smith at smith.it (Smith) Date: Wed, 18 Jan 2017 20:51:05 +0100 Subject: sorting list python Message-ID: Hi all, could you do better? Thank you in advance link code: https://repl.it/FMin/8 From __peter__ at web.de Wed Jan 18 15:20:17 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 18 Jan 2017 21:20:17 +0100 Subject: sorting list python References: Message-ID: Smith wrote: > Hi all, > could you do better? > Thank you in advance > > link code: > https://repl.it/FMin/8 Don't make it unnecessarily hard to find your code -- as long as it's small you can include it into your mail Given > with open('partite.txt', 'r') as f: > splitted = [(int(line.split("'")[0]), line) for line in f] > splitted.sort() > lsorted = [line for (m, line) in splitted] > for z in lsorted: > print(z) > with partite.txt looking like this > 74' Kessie' > 90' + 4' D'alessandro > 51' Mchedlidze > 54' Banega > 56' Icardi > 65' Icardi > 14' Sau Assuming you want to perform a numerical sort on the numbers before the ' you can just apply sorted with open(...) as f: print("".join(sorted(f)) as long as all number strings have the same length. If that's not the case python's sorted() has decorate-sort-undecorate capabilities built in -- no need to do it manually: with open("partite.txt") as f: by_number = sorted(f, key=lambda line: int(line.partition("'")[0])) print("".join(by_number)) From grant.b.edwards at gmail.com Wed Jan 18 15:21:53 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Wed, 18 Jan 2017 20:21:53 +0000 (UTC) Subject: sorting list python References: Message-ID: On 2017-01-18, Smith wrote: > could you do better? Yes. > link code: > https://repl.it/FMin/8 I would have done better by posting the actual code instead of a blind link that may point to Dog-knows-what... -- Grant Edwards grant.b.edwards Yow! I had pancake makeup at for brunch! gmail.com From python at mrabarnett.plus.com Wed Jan 18 15:34:25 2017 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 18 Jan 2017 20:34:25 +0000 Subject: sorting list python In-Reply-To: References: Message-ID: <74d49140-9405-f367-1223-90f8f24faccd@mrabarnett.plus.com> On 2017-01-18 19:51, Smith wrote: > Hi all, > could you do better? > Thank you in advance > > link code: > https://repl.it/FMin/8 > If you're wondering about the blank lines, it's because the lines end with '\n', which starts a new line, and the print function also starts a new line after printing the string. Try stripping the '\n' off the end of the line read in with the .rstrip method. From smith at smith.it Wed Jan 18 15:35:21 2017 From: smith at smith.it (Smith) Date: Wed, 18 Jan 2017 21:35:21 +0100 Subject: sorting list python References: Message-ID: <7ad5b0d4-5142-a39a-816b-16c90e53fc10@smith.it> On 18/01/2017 21:20, Peter Otten wrote: > with open("partite.txt") as f: > by_number = sorted(f, key=lambda line: int(line.partition("'")[0])) > print("".join(by_number)) Great!!! :-) From smith at smith.it Wed Jan 18 15:35:54 2017 From: smith at smith.it (Smith) Date: Wed, 18 Jan 2017 21:35:54 +0100 Subject: sorting list python References: Message-ID: <3d5af145-cfa5-7399-2f13-86849cf3893a@smith.it> On 18/01/2017 21:21, Grant Edwards wrote: > I would have done better by posting the actual code instead of a blind > link that may point to Dog-knows-what... sorry :-( From smith at smith.it Wed Jan 18 15:38:31 2017 From: smith at smith.it (Smith) Date: Wed, 18 Jan 2017 21:38:31 +0100 Subject: sorting list python References: <74d49140-9405-f367-1223-90f8f24faccd@mrabarnett.plus.com> Message-ID: On 18/01/2017 21:34, MRAB wrote: > If you're wondering about the blank lines, it's because the lines end > with '\n', which starts a new line, and the print function also starts a > new line after printing the string. > > Try stripping the '\n' off the end of the line read in with the .rstrip > method. Thank you for your kind cooperation From german.fajardo at gmail.com Wed Jan 18 16:23:37 2017 From: german.fajardo at gmail.com (=?UTF-8?Q?Germ=C3=A1n_Fajardo?=) Date: Wed, 18 Jan 2017 16:23:37 -0500 Subject: sorting list python In-Reply-To: References: <74d49140-9405-f367-1223-90f8f24faccd@mrabarnett.plus.com> Message-ID: with open('partite.txt', 'r') as r: for line in sorted(r): print(line, end='') 2017-01-18 15:38 GMT-05:00 Smith : > On 18/01/2017 21:34, MRAB wrote: > >> If you're wondering about the blank lines, it's because the lines end >> with '\n', which starts a new line, and the print function also starts a >> new line after printing the string. >> >> Try stripping the '\n' off the end of the line read in with the .rstrip >> method. >> > > Thank you for your kind cooperation > -- > https://mail.python.org/mailman/listinfo/python-list > -- Atentamente Germ?n A. Fajardo G. From martin.schoon at gmail.com Wed Jan 18 16:41:34 2017 From: martin.schoon at gmail.com (Martin =?UTF-8?Q?Sch=C3=B6=C3=B6n?=) Date: 18 Jan 2017 21:41:34 GMT Subject: Adding colormaps? Message-ID: Is there a way to add colormaps to Matplotlib? What I would like to do is to add the perceptually uniform sequential colormaps introduced in version 1.5.something. I would like to do this without breaking my Debian system in which Matplotlib version 1.4.2 is the newest version available in the repo. /Martin From torriem at gmail.com Wed Jan 18 16:49:11 2017 From: torriem at gmail.com (Michael Torrie) Date: Wed, 18 Jan 2017 14:49:11 -0700 Subject: Python 3.6 Installation In-Reply-To: References: <061F1A8C6BEA684BBC8CC0972537D3D024DBA9BB@AUSP07DAG0106.hostedmail.local> <4ef0f5b2-01f0-72ae-b6e7-add88d5bacc0@gmail.com> <587ecf06$0$1522$c3e8da3$5496439d@news.astraweb.com> <5dd005ca-1022-0521-d70c-b8f007477663@gmail.com> Message-ID: <7166beae-0e95-f98a-32d2-5cdf9e0f55cb@gmail.com> On 01/18/2017 10:59 AM, eryk sun wrote: > On Wed, Jan 18, 2017 at 6:23 AM, Terry Reedy wrote: >> Not everyone has run Windows update since the current runtime was released. > > Python's installer tries (and sometimes fails) to install the > KB2999226 update, which installs an old version of the Universal CRT. > Some installers instead bundle the VC++ redistributable, which > includes KB2999226, but that would be overkill for CPython. I thought > we could phase this out in 3.6 to instead require an up-to-date OS. If > people need offline updates, as opposed to using Windows update, it's > better to get the most recent version directly from Microsoft, > KB3118401 [1]. Steve Dower disagreed, and that's the end of it. > > As to the loader returning STATUS_INVALID_IMAGE_FORMAT (0xC000007B), I > would run the executable/script under a debugger such as WinDbg or > cdb, with loader snaps enabled via gflags. Find the troublesome > DLL(s), and manually resolve the problem. > > [1]: https://support.microsoft.com/en-us/kb/3118401 > Thanks for the clarifications, Erik. Appreciate it. From ethan at stoneleaf.us Wed Jan 18 17:02:47 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 18 Jan 2017 14:02:47 -0800 Subject: Emulating Final classes in Python In-Reply-To: <587F96BA.8070707@stoneleaf.us> References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> <587EF234.90502@stoneleaf.us> <587f13c0$0$1590$c3e8da3$5496439d@news.astraweb.com> <587F96BA.8070707@stoneleaf.us> Message-ID: <587FE607.7040002@stoneleaf.us> On 01/18/2017 08:24 AM, Ethan Furman wrote: > On 01/17/2017 11:05 PM, Steven D'Aprano wrote: >> I've given a metaclass that disallows subclassing: >> >> class MyClass(MyParent, metaclass=FinalMeta): >> ... >> >> >> Ethan took that one step further by giving a class you inherit from to disallow >> subclassing: >> >> class MyClass(MyParent, Final): >> ... >> >> >> Could we solve this problem with a decorator? >> >> >> @final >> class MyClass(MyParent): >> ... >> >> >> Without needing to add any special magic to MyParent or MyClass, apart from the >> decorator, can we make MyClass final? That would (in principle) allow us to >> make a subclass, and *then* set the class final so that no more subclasses >> could be made. >> >> >> I thought that the decorator could simply set the class' metaclass: >> >> def final(cls): >> if cls.__class__ is type: >> cls.__class__ = Meta >> return cls >> raise TypeErrror('Possible metaclass conflict') >> >> >> but that's disallowed. Any ideas? > > You still need to have the FinalMeta type and Final class available, but to use a > decorator you'll need to scavenge the bits from the old class to make a new class > of the same name and return that: > > def final(cls): > new_cls = Meta(cls.__name__, (Final, )+cls.__bases__, dict(cls.__dict__)) > return new_cls > > Not sure if more is needed to handle __slots__, but this should get us started. One problem with the above is existing instances won't be modified to inherit from the updated class. I am unsure if that is solvable before 3.6, but in 3.6 one can use the new __init_subclass__ to avoid a Final base class, a FinalMeta type, and just update the existing class: def final(cls): def init_subclass(cls, **kwargs): raise Exception('Final class cannot be subclassed') cls.__init_subclass__ = classmethod(init_subclass) return cls This can be used as a decorator at class creation time, or at any later date to lock down a class. The downside is it's less obvious that the class is final... meaning there are no clues in the MRO. -- ~Ethan~ From steve+python at pearwood.info Wed Jan 18 20:36:51 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Thu, 19 Jan 2017 12:36:51 +1100 Subject: Emulating Final classes in Python References: <587dc242$0$1601$c3e8da3$5496439d@news.astraweb.com> <19450be4-a788-0e38-d70d-33d3edebc19e@rece.vub.ac.be> <587EF234.90502@stoneleaf.us> <587f13c0$0$1590$c3e8da3$5496439d@news.astraweb.com> <587F96BA.8070707@stoneleaf.us> <587FE607.7040002@stoneleaf.us> Message-ID: <58801837$0$1606$c3e8da3$5496439d@news.astraweb.com> On Thu, 19 Jan 2017 09:02 am, Ethan Furman wrote: [...] > One problem with the above is existing instances won't be modified to > inherit from the updated class. I am unsure if that is solvable before > 3.6, but in 3.6 one can use the new __init_subclass__ to avoid a Final > base class, a FinalMeta type, and just update the existing class: > > def final(cls): > def init_subclass(cls, **kwargs): > raise Exception('Final class cannot be subclassed') > cls.__init_subclass__ = classmethod(init_subclass) > return cls > > This can be used as a decorator at class creation time, or at any later > date to lock down a class. The downside is it's less obvious that the > class is final... meaning there are no clues in the MRO. Ah nice! I didn't know about __init__subclass__. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From jobmattcon at gmail.com Thu Jan 19 03:04:30 2017 From: jobmattcon at gmail.com (Ho Yeung Lee) Date: Thu, 19 Jan 2017 00:04:30 -0800 (PST) Subject: Must target be only one bit one such as 0001,0010,0100,1000 In supervised neural learning f(w*p+b) with perceptron rule w = w + e for linear case? Message-ID: Must target be only one bit one such as 0001,0010,0100,1000 In supervised neural learning f(w*p+b) with perceptron rule w = w + e for linear case? with neural network design does it means that can not use two or more one as target such as 0011,0110,1100,1010, 0111,1110,1101, etc when training weight? From johann.spies at gmail.com Thu Jan 19 05:16:19 2017 From: johann.spies at gmail.com (Johann Spies) Date: Thu, 19 Jan 2017 12:16:19 +0200 Subject: Python for WEB-page !? In-Reply-To: <0b621aaa-03d0-0e72-f12d-030189e6a3f1@harvee.org> References: <4df49556-ce91-4ca5-8d9e-f505d7f1a1e9@googlegroups.com> <0b621aaa-03d0-0e72-f12d-030189e6a3f1@harvee.org> Message-ID: It might be worth while to look at web2py (http://web2py.com). Here is a good tutorial: http://killer-web-development.com/ Regards Johann -- Because experiencing your loyal love is better than life itself, my lips will praise you. (Psalm 63:3) From davidbenny2000 at gmail.com Thu Jan 19 05:54:03 2017 From: davidbenny2000 at gmail.com (Ho Yeung Lee) Date: Thu, 19 Jan 2017 02:54:03 -0800 (PST) Subject: Is "two different input map to one unique target called nonlinear case " or "one unique input map to two different target called nonlinear case"? Which learning method can train these two cases or no method to train one of case? Message-ID: Is "two different input map to one unique target called nonlinear case " or "one unique input map to two different target called nonlinear case"? Which learning method can train these two cases or no method to train one of case? From Cecil at decebal.nl Thu Jan 19 10:06:40 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Thu, 19 Jan 2017 16:06:40 +0100 Subject: ipython2 does not work anymore Message-ID: <87bmv3xf5r.fsf@Equus.decebal.nl> I did not work with ipython2 for a long time. Most of my work is done with python3. I just tried to start ipython2 and got: Traceback (most recent call last): File "/usr/bin/ipython2", line 7, in from IPython import start_ipython File "/usr/lib/python2.7/site-packages/IPython/__init__.py", line 49, in from .terminal.embed import embed File "/usr/lib/python2.7/site-packages/IPython/terminal/embed.py", line 16, in from IPython.core.interactiveshell import DummyMod, InteractiveShell File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 31, in from pickleshare import PickleShareDB File "/usr/lib/python2.7/site-packages/pickleshare.py", line 40, in from path import path as Path ImportError: No module named path What could be the problem here? Python2 does work: Python 2.7.12 (default, Jul 01 2016, 15:36:53) [GCC] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> Otherwise I would have a problem. Some scripts still use it. I am working with openSUSE 13.2 -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From rosuav at gmail.com Thu Jan 19 13:21:44 2017 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 20 Jan 2017 05:21:44 +1100 Subject: Odd message while building Python from tip Message-ID: I don't think I've ever seen this message before. This is just after 'hg pull -u' to get to the latest CPython (my previous build was about a month ago). 'make' succeeded, but concluded with this message: running build_ext The following modules found by detect_modules() in setup.py, have been built by the Makefile instead, as configured by the Setup files: atexit pwd time And then proceeded to run build_scripts as normal. It doesn't look like an error, and those three modules can happily be imported by "./python" and by the freshly-installed "python3.7". It's curious though. Any explanation? ChrisA From Cecil at decebal.nl Thu Jan 19 14:08:32 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Thu, 19 Jan 2017 20:08:32 +0100 Subject: Using python to start programs after logging in Message-ID: <878tq6amvj.fsf@Equus.decebal.nl> I am writing a python program to start the programs that need to be started after logging in. I have the following imports: from subprocess import check_call, Popen, STDOUT from time import sleep, strftime And use the following code: check_call(tuple('wmctrl -s 10'.split())) log_file = open('Logging/firefox_%T.log'.replace('%T', strftime('%F_%R')), 'w') Popen(tuple('firefox'.split()), stdout = log_file, stderr = STDOUT) The first statement is to go to the correct desktop. Is this a good way to do things, or could I do it in a better way? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From gordon at panix.com Thu Jan 19 14:29:05 2017 From: gordon at panix.com (John Gordon) Date: Thu, 19 Jan 2017 19:29:05 +0000 (UTC) Subject: Using python to start programs after logging in References: <878tq6amvj.fsf@Equus.decebal.nl> Message-ID: In <878tq6amvj.fsf at Equus.decebal.nl> Cecil Westerhof writes: > I am writing a python program to start the programs that need to be > started after logging in. > Is this a good way to do things, or could I do it in a better way? I think using your window manager's built-in facilities for starting programs would be better. Why are you using Python instead? -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From python at mrabarnett.plus.com Thu Jan 19 15:08:34 2017 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 19 Jan 2017 20:08:34 +0000 Subject: ipython2 does not work anymore In-Reply-To: <87bmv3xf5r.fsf@Equus.decebal.nl> References: <87bmv3xf5r.fsf@Equus.decebal.nl> Message-ID: <35dadf92-ada7-02ce-3449-750cb914c426@mrabarnett.plus.com> On 2017-01-19 15:06, Cecil Westerhof wrote: > I did not work with ipython2 for a long time. Most of my work is done > with python3. I just tried to start ipython2 and got: > Traceback (most recent call last): > File "/usr/bin/ipython2", line 7, in > from IPython import start_ipython > File "/usr/lib/python2.7/site-packages/IPython/__init__.py", line 49, in > from .terminal.embed import embed > File "/usr/lib/python2.7/site-packages/IPython/terminal/embed.py", line 16, in > from IPython.core.interactiveshell import DummyMod, InteractiveShell > File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 31, in > from pickleshare import PickleShareDB > File "/usr/lib/python2.7/site-packages/pickleshare.py", line 40, in > from path import path as Path > ImportError: No module named path > > > What could be the problem here? > > > Python2 does work: > Python 2.7.12 (default, Jul 01 2016, 15:36:53) [GCC] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> > > Otherwise I would have a problem. Some scripts still use it. > > I am working with openSUSE 13.2 > There's no module called 'path' in Python 3. Do you mean 'pathlib'? If you do mean 'pathlib', it was introduced in Python 3.4. From python at mrabarnett.plus.com Thu Jan 19 15:12:30 2017 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 19 Jan 2017 20:12:30 +0000 Subject: Using python to start programs after logging in In-Reply-To: <878tq6amvj.fsf@Equus.decebal.nl> References: <878tq6amvj.fsf@Equus.decebal.nl> Message-ID: On 2017-01-19 19:08, Cecil Westerhof wrote: > I am writing a python program to start the programs that need to be > started after logging in. > > I have the following imports: > from subprocess import check_call, Popen, STDOUT > from time import sleep, strftime > > And use the following code: > check_call(tuple('wmctrl -s 10'.split())) > log_file = open('Logging/firefox_%T.log'.replace('%T', strftime('%F_%R')), 'w') > Popen(tuple('firefox'.split()), stdout = log_file, stderr = STDOUT) > > The first statement is to go to the correct desktop. > > Is this a good way to do things, or could I do it in a better way? > Apart from the other answer, your use of .replace is odd. This would be better: log_file = open('Logging/firefox_%s.log' % strftime('%F_%R'), 'w') Even better would be to use the 'with' statement as well. From Cecil at decebal.nl Thu Jan 19 16:11:31 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Thu, 19 Jan 2017 22:11:31 +0100 Subject: Using python to start programs after logging in References: <878tq6amvj.fsf@Equus.decebal.nl> Message-ID: <878tq6hi0s.fsf@Equus.decebal.nl> On Thursday 19 Jan 2017 20:29 CET, John Gordon wrote: > In <878tq6amvj.fsf at Equus.decebal.nl> Cecil Westerhof > writes: > >> I am writing a python program to start the programs that need to be >> started after logging in. > >> Is this a good way to do things, or could I do it in a better way? > > I think using your window manager's built-in facilities for starting > programs would be better. Why are you using Python instead? Because when you use the window managers builtin facilities then all programs will be started on the same virtual desktop and I want to start them on different ones. Second reason is that I want to fetch the programs to start from a database so I can easily change the programs to be started. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From Cecil at decebal.nl Thu Jan 19 16:12:43 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Thu, 19 Jan 2017 22:12:43 +0100 Subject: ipython2 does not work anymore References: <87bmv3xf5r.fsf@Equus.decebal.nl> <35dadf92-ada7-02ce-3449-750cb914c426@mrabarnett.plus.com> Message-ID: <874m0uhhys.fsf@Equus.decebal.nl> On Thursday 19 Jan 2017 21:08 CET, MRAB wrote: > On 2017-01-19 15:06, Cecil Westerhof wrote: >> I did not work with ipython2 for a long time. Most of my work is >> done with python3. I just tried to start ipython2 and got: >> Traceback (most recent call last): File "/usr/bin/ipython2", line >> 7, in from IPython import start_ipython File >> "/usr/lib/python2.7/site-packages/IPython/__init__.py", line 49, in >> from .terminal.embed import embed File >> "/usr/lib/python2.7/site-packages/IPython/terminal/embed.py", line >> 16, in from IPython.core.interactiveshell import DummyMod, >> InteractiveShell File >> "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", >> line 31, in from pickleshare import PickleShareDB File >> "/usr/lib/python2.7/site-packages/pickleshare.py", line 40, in >> from path import path as Path ImportError: No module named >> path >> >> >> What could be the problem here? >> >> >> Python2 does work: Python 2.7.12 (default, Jul 01 2016, 15:36:53) >> [GCC] on linux2 Type "help", "copyright", "credits" or "license" >> for more information. >>>>> >> >> Otherwise I would have a problem. Some scripts still use it. >> >> I am working with openSUSE 13.2 >> > There's no module called 'path' in Python 3. Do you mean 'pathlib'? I copied this from the output I got. > If you do mean 'pathlib', it was introduced in Python 3.4. It is about python2. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From Cecil at decebal.nl Thu Jan 19 16:21:59 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Thu, 19 Jan 2017 22:21:59 +0100 Subject: Using python to start programs after logging in References: <878tq6amvj.fsf@Equus.decebal.nl> Message-ID: <87ziimg2yw.fsf@Equus.decebal.nl> On Thursday 19 Jan 2017 21:12 CET, MRAB wrote: > On 2017-01-19 19:08, Cecil Westerhof wrote: >> I am writing a python program to start the programs that need to be >> started after logging in. >> >> I have the following imports: >> from subprocess import check_call, Popen, STDOUT >> from time import sleep, strftime >> >> And use the following code: check_call(tuple('wmctrl -s >> 10'.split())) log_file = >> open('Logging/firefox_%T.log'.replace('%T', strftime('%F_%R')), >> 'w') Popen(tuple('firefox'.split()), stdout = log_file, stderr = >> STDOUT) >> >> The first statement is to go to the correct desktop. >> >> Is this a good way to do things, or could I do it in a better way? >> > Apart from the other answer, your use of .replace is odd. This would > be better: > > log_file = open('Logging/firefox_%s.log' % strftime('%F_%R'), 'w') The reason is that it is ?generic? code. In the future instead of string it will be a variable and only when the variable contains %T it should be replaced. > Even better would be to use the 'with' statement as well. The same here. Not always has the output to be logged and then I would use NONE for log_file. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From songofacandy at gmail.com Thu Jan 19 18:09:37 2017 From: songofacandy at gmail.com (INADA Naoki) Date: Fri, 20 Jan 2017 08:09:37 +0900 Subject: Odd message while building Python from tip In-Reply-To: References: Message-ID: Hi, Chris. They are "builtin" module. python executable contains the modules already. But I don't know it's safe to remove them from setup.py. There are so many platforms and special build of Python. $ ./python Python 3.7.0a0 (default:9f7d16266928, Jan 18 2017, 23:59:22) [GCC 6.2.0 20161005] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.builtin_module_names ('_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'xxsubtype', 'zipimport') On Fri, Jan 20, 2017 at 3:21 AM, Chris Angelico wrote: > I don't think I've ever seen this message before. This is just after > 'hg pull -u' to get to the latest CPython (my previous build was about > a month ago). 'make' succeeded, but concluded with this message: > > running build_ext > The following modules found by detect_modules() in setup.py, have been > built by the Makefile instead, as configured by the Setup files: > atexit pwd time > > And then proceeded to run build_scripts as normal. > > It doesn't look like an error, and those three modules can happily be > imported by "./python" and by the freshly-installed "python3.7". It's > curious though. Any explanation? > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list From Cecil at decebal.nl Thu Jan 19 18:36:41 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Fri, 20 Jan 2017 00:36:41 +0100 Subject: Using python to start programs after logging in References: <878tq6amvj.fsf@Equus.decebal.nl> <87ziimg2yw.fsf@Equus.decebal.nl> Message-ID: <87shoefwqe.fsf@Equus.decebal.nl> On Thursday 19 Jan 2017 22:21 CET, Cecil Westerhof wrote: > On Thursday 19 Jan 2017 21:12 CET, MRAB wrote: > >> On 2017-01-19 19:08, Cecil Westerhof wrote: >>> I am writing a python program to start the programs that need to >>> be started after logging in. >>> >>> I have the following imports: >>> from subprocess import check_call, Popen, STDOUT >>> from time import sleep, strftime >>> >>> And use the following code: check_call(tuple('wmctrl -s >>> 10'.split())) log_file = >>> open('Logging/firefox_%T.log'.replace('%T', strftime('%F_%R')), >>> 'w') Popen(tuple('firefox'.split()), stdout = log_file, stderr = >>> STDOUT) >>> >>> The first statement is to go to the correct desktop. >>> >>> Is this a good way to do things, or could I do it in a better way? >>> >> Apart from the other answer, your use of .replace is odd. This >> would be better: >> >> log_file = open('Logging/firefox_%s.log' % strftime('%F_%R'), 'w') > > The reason is that it is ?generic? code. In the future instead of > string it will be a variable and only when the variable contains %T > it should be replaced. > > >> Even better would be to use the 'with' statement as well. > > The same here. Not always has the output to be logged and then I > would use NONE for log_file. I wrote a function for switching to the correct virtual desktop and starting all the commands. I am also using with now: def do_desktop(desktop, commands, seconds_to_wait = 10): desktop_command = ('wmctrl -s ' + desktop).split() check_call(tuple(desktop_command)) for command_arr in commands: command = command_arr[0].split() log_directory = command_arr[1] directory = command_arr[2] if (directory != ''): chdir(directory) if (log_directory == 'NONE'): Popen(tuple(command.split())) else: log_file_name = log_directory.replace('%T', strftime('%F_%R')) with open(log_file_name, 'w') as log_file: Popen(tuple(command), stdout = log_file, stderr = STDOUT) if (directory != ''): set_default_dir() sleep(seconds_to_wait) The function set_default_dir: def set_default_dir(): chdir(expanduser('~')) The example with firefox is then done with: commands = [ ['firefox', 'Logging/firefox_%T.log', ''], ] do_desktop('10', commands, 30) Sometimes a command needs to be executed in a different directory, that is done like: commands = [ ['lein run', 'NONE', 'Clojure/Quotes'], ['xfce4-terminal --maximize --execute screen -S Clojure -c ~/.screenrcClojure', 'NONE', ''], ] do_desktop('6', commands, 30) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From rosuav at gmail.com Thu Jan 19 18:49:15 2017 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 20 Jan 2017 10:49:15 +1100 Subject: Odd message while building Python from tip In-Reply-To: References: Message-ID: On Fri, Jan 20, 2017 at 10:09 AM, INADA Naoki wrote: > Hi, Chris. > > They are "builtin" module. > python executable contains the modules already. > > But I don't know it's safe to remove them from setup.py. > There are so many platforms and special build of Python. I haven't changed the code in any way, btw. This is a vanilla CPython (at the moment - there have been times I've had patches sitting around), and this message is, I believe, new. ChrisA From steve+python at pearwood.info Thu Jan 19 19:01:09 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Fri, 20 Jan 2017 11:01:09 +1100 Subject: ipython2 does not work anymore References: <87bmv3xf5r.fsf@Equus.decebal.nl> Message-ID: <58815347$0$1620$c3e8da3$5496439d@news.astraweb.com> On Fri, 20 Jan 2017 02:06 am, Cecil Westerhof wrote: > I did not work with ipython2 for a long time. Most of my work is done > with python3. I just tried to start ipython2 and got: > Traceback (most recent call last): [...] > ImportError: No module named path > > > What could be the problem here? Something has changed between the last time you ran IPython and now. You have deleted something, changed something, installed something... or you have file system corruption and files are disappearing... or your PYTHONPATH environment variable is not set, or is set differently to the way it used to be... Do you want to investigate why the error occurred, or just fix it in the most convenient and easy way? If the second, then you can probably fix it by running: python2 -m pip install path assuming that you trust that path is a legitimate module. Looking at the traceback, I see the final call which fails is in a module called pickleshare.py: File "/usr/lib/python2.7/site-packages/pickleshare.py", line 40, in from path import path as Path You could try removing pickleshare and re-installing, or upgrading to the latest version, and see whether it brings in the "path" dependency. You could try running "locate path.py | grep python2.7" in the shell and see whether path is installed. If it is installed, it sounds like the PYTHONPATH is messed up. Run these and tell us what they say: locate path.py | grep python2.7 python2.7 -c "import sys; print sys.path" echo $PYTHONPATH Googling for your error gives me these two relevant looking hits: https://duckduckgo.com/html/?q=%22ImportError:%20No%20module%20named%20path%22+ipython https://github.com/jupyter/notebook/issues/270 https://github.com/jupyter/notebook/issues/525 -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From saxri89 at gmail.com Thu Jan 19 19:03:33 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Thu, 19 Jan 2017 16:03:33 -0800 (PST) Subject: python corrupted double-linked list error Message-ID: i am a python 2.7 and ubuntu 16.04 user and i have unexpected error when try to excute some python scripts . i use pycharm and i have the some error when i try to run python script from the ubuntu terminal. the error : *** Error in `/usr/bin/python2.7': corrupted double-linked list: 0x0b83c880 *** the error message : ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb764b377] /lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb76512f7] /lib/i386-linux-gnu/libc.so.6(+0x6e2f1)[0xb76522f1] /usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x18)[0xb6ba9d88] /usr/lib/libgdal.so.1(_ZN15GDALMajorObjectD0Ev+0x22)[0xb075a582] /usr/lib/libgdal.so.1(GDALClose+0x77)[0xb074d747] /usr/lib/qgis/plugins/libgdalprovider.so(+0xa930)[0xa4888930] /usr/lib/qgis/plugins/libgdalprovider.so(+0xaafa)[0xa4888afa] /usr/lib/libqgis_core.so.2.18.3(_ZN13QgsRasterPipeD1Ev+0x75)[0xb3d6a7d5] /usr/lib/libqgis_core.so.2.18.3(_ZN14QgsRasterLayerD1Ev+0x2f)[0xb3d5cd0f] /usr/lib/python2.7/dist-packages/qgis/_core.i386-linux-gnu.so(_ZN17sipQgsRasterLayerD1Ev+0x3b)[0xb489dd2b] /usr/lib/python2.7/dist-packages/qgis/_core.i386-linux-gnu.so(_ZN17sipQgsRasterLayerD0Ev+0x1a)[0xb489dd5a] /usr/lib/python2.7/dist-packages/qgis/_core.i386-linux-gnu.so(+0x43df45)[0xb4883f45] /usr/lib/python2.7/dist-packages/qgis/_core.i386-linux-gnu.so(+0x43df8a)[0xb4883f8a] /usr/lib/python2.7/dist-packages/sip.i386-linux-gnu.so(+0x5d49)[0xb724dd49] /usr/lib/python2.7/dist-packages/sip.i386-linux-gnu.so(+0xc19b)[0xb725419b] /usr/bin/python2.7[0x8144aad] /usr/bin/python2.7[0x80fd127] /usr/bin/python2.7(PyDict_SetItem+0x478)[0x80e9268] /usr/bin/python2.7(_PyModule_Clear+0xba)[0x8149a1a] /usr/bin/python2.7(PyImport_Cleanup+0x37a)[0x81495ca] /usr/bin/python2.7(Py_Finalize+0x99)[0x8147399] /usr/bin/python2.7(Py_Main+0x4bd)[0x80e639d] /usr/bin/python2.7(main+0x26)[0x80e5ec6] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb75fc637] /usr/bin/python2.7[0x80e5dc8] ======= Memory map: ======== 08048000-0838f000 r-xp 00000000 08:01 1583892 /usr/bin/python2.7 08390000-08391000 r--p 00347000 08:01 1583892 /usr/bin/python2.7 08391000-083f1000 rw-p 00348000 08:01 1583892 /usr/bin/python2.7 083f1000-08406000 rw-p 00000000 00:00 0 0a0f7000-0ba69000 rw-p 00000000 00:00 0 [heap] a03e0000-a03eb000 r-xp 00000000 08:01 1049930 /lib/i386-linux-gnu/libnss_files-2.23.so a03eb000-a03ec000 r--p 0000a000 08:01 1049930 /lib/i386-linux-gnu/libnss_files-2.23.so a03ec000-a03ed000 rw-p 0000b000 08:01 1049930 /lib/i386-linux-gnu/libnss_files-2.23.so a03ed000-a03f3000 rw-p 00000000 00:00 0 a03f3000-a03fe000 r-xp 00000000 08:01 1048664 /lib/i386-linux-gnu/libnss_nis-2.23.so a03fe000-a03ff000 r--p 0000a000 08:01 1048664 /lib/i386-linux-gnu/libnss_nis-2.23.so a03ff000-a0400000 rw-p 0000b000 08:01 1048664 /lib/i386-linux-gnu/libnss_nis-2.23.so a0400000-a0421000 rw-p 00000000 00:00 0 a0421000-a0500000 ---p 00000000 00:00 0 a0509000-a0520000 r-xp 00000000 08:01 1048674 /lib/i386-linux-gnu/libnsl-2.23.so a0520000-a0521000 r--p 00016000 08:01 1048674 /lib/i386-linux-gnu/libnsl-2.23.so a0521000-a0522000 rw-p 00017000 08:01 1048674 /lib/i386-linux-gnu/libnsl-2.23.so a0522000-a0524000 rw-p 00000000 00:00 0 a0524000-a052c000 r-xp 00000000 08:01 1048675 /lib/i386-linux-gnu/libnss_compat-2.23.so a052c000-a052d000 r--p 00007000 08:01 1048675 /lib/i386-linux-gnu/libnss_compat-2.23.so a052d000-a052e000 rw-p 00008000 08:01 1048675 /lib/i386-linux-gnu/libnss_compat-2.23.so a054a000-a054b000 ---p 00000000 00:00 0 a054b000-a10cb000 rw-p 00000000 00:00 0 a10cb000-a10d4000 r-xp 00000000 08:01 150141 /usr/lib/i386-linux-gnu/qt4/plugins/iconengines/libqsvgicon.so a10d4000-a10d5000 r--p 00008000 08:01 150141 /usr/lib/i386-linux-gnu/qt4/plugins/iconengines/libqsvgicon.so a10d5000-a10d6000 rw-p 00009000 08:01 150141 /usr/lib/i386-linux-gnu/qt4/plugins/iconengines/libqsvgicon.so a10d6000-a1296000 rw-p 00000000 00:00 0 a1296000-a12a5000 r-xp 00000000 08:01 283552 /usr/lib/python2.7/dist-packages/scipy/stats/mvn.i386-linux-gnu.so a12a5000-a12a6000 ---p 0000f000 08:01 283552 /usr/lib/python2.7/dist-packages/scipy/stats/mvn.i386-linux-gnu.so a12a6000-a12a7000 r--p 0000f000 08:01 283552 /usr/lib/python2.7/dist-packages/scipy/stats/mvn.i386-linux-gnu.so a12a7000-a12a8000 rw-p 00010000 08:01 283552 /usr/lib/python2.7/dist-packages/scipy/stats/mvn.i386-linux-gnu.so a12a8000-a139f000 rw-p 00000000 00:00 0 a139f000-a13a8000 r-xp 00000000 08:01 283553 /usr/lib/python2.7/dist-packages/scipy/stats/statlib.i386-linux-gnu.so a13a8000-a13a9000 ---p 00009000 08:01 283553 /usr/lib/python2.7/dist-packages/scipy/stats/statlib.i386-linux-gnu.so a13a9000-a13aa000 r--p 00009000 08:01 283553 /usr/lib/python2.7/dist-packages/scipy/stats/statlib.i386-linux-gnu.so a13aa000-a13ab000 rw-p 0000a000 08:01 283553 /usr/lib/python2.7/dist-packages/scipy/stats/statlib.i386-linux-gnu.so a13ab000-a13eb000 rw-p 00000000 00:00 0 a13eb000-a13f8000 r-xp 00000000 08:01 283548 /usr/lib/python2.7/dist-packages/scipy/stats/_rank.i386-linux-gnu.so a13f8000-a13f9000 r--p 0000c000 08:01 283548 /usr/lib/python2.7/dist-packages/scipy/stats/_rank.i386-linux-gnu.so a13f9000-a13fb000 rw-p 0000d000 08:01 283548 /usr/lib/python2.7/dist-packages/scipy/stats/_rank.i386-linux-gnu.so a13fb000-a147b000 rw-p 00000000 00:00 0 a147b000-a148e000 r-xp 00000000 08:01 283509 /usr/lib/python2.7/dist-packages/scipy/stats/vonmises_cython.i386-linux-gnu.so a148e000-a148f000 r--p 00012000 08:01 283509 /usr/lib/python2.7/dist-packages/scipy/stats/vonmises_cython.i386-linux-gnu.so a148f000-a1490000 rw-p 00013000 08:01 283509 /usr/lib/python2.7/dist-packages/scipy/stats/vonmises_cython.i386-linux-gnu.so a1490000-a1510000 rw-p 00000000 00:00 0 a1510000-a1519000 r-xp 00000000 08:01 534502 /usr/lib/python2.7/dist-packages/scipy/optimize/_nnls.i386-linux-gnu.so a1519000-a151a000 r--p 00008000 08:01 534502 /usr/lib/python2.7/dist-packages/scipy/optimize/_nnls.i386-linux-gnu.so a151a000-a151b000 rw-p 00009000 08:01 534502 /usr/lib/python2.7/dist-packages/scipy/optimize/_nnls.i386-linux-gnu.so a151b000-a1539000 r-xp 00000000 08:01 534537 /usr/lib/python2.7/dist-packages/scipy/optimize/_lsq/givens_elimination.i386-linux-gnu.so a1539000-a153a000 r--p 0001d000 08:01 534537 /usr/lib/python2.7/dist-packages/scipy/optimize/_lsq/givens_elimination.i386-linux-gnu.so a153a000-a153c000 rw-p 0001e000 08:01 534537 /usr/lib/python2.7/dist-packages/scipy/optimize/_lsq/givens_elimination.i386-linux-gnu.so a153c000-a1562000 r-xp 00000000 08:01 534554 /usr/lib/python2.7/dist-packages/scipy/optimize/_group_columns.i386-linux-gnu.so a1562000-a1563000 r--p 00025000 08:01 534554 /usr/lib/python2.7/dist-packages/scipy/optimize/_group_columns.i386-linux-gnu.so a1563000-a1565000 rw-p 00026000 08:01 534554 /usr/lib/python2.7/dist-packages/scipy/optimize/_group_columns.i386-linux-gnu.so a1565000-a157b000 r-xp 00000000 08:01 534540 /usr/lib/python2.7/dist-packages/scipy/optimize/_minpack.i386-linux-gnu.so a157b000-a157c000 r--p 00015000 08:01 534540 /usr/lib/python2.7/dist-packages/scipy/optimize/_minpack.i386-linux-gnu.so a157c000-a157d000 rw-p 00016000 08:01 534540 /usr/lib/python2.7/dist-packages/scipy/optimize/_minpack.i386-linux-gnu.so a157d000-a1592000 r-xp 00000000 08:01 534543 /usr/lib/python2.7/dist-packages/scipy/optimize/_slsqp.i386-linux-gnu.so a1592000-a1593000 r--p 00014000 08:01 534543 /usr/lib/python2.7/dist-packages/scipy/optimize/_slsqp.i386-linux-gnu.so a1593000-a1594000 rw-p 00015000 08:01 534543 /usr/lib/python2.7/dist-packages/scipy/optimize/_slsqp.i386-linux-gnu.so a1594000-a15ad000 r-xp 00000000 08:01 534495 /usr/lib/python2.7/dist-packages/scipy/optimize/_cobyla.i386-linux-gnu.so a15ad000-a15ae000 r--p 00018000 08:01 534495 /usr/lib/python2.7/dist-packages/scipy/optimize/_cobyla.i386-linux-gnu.so a15ae000-a15af000 rw-p 00019000 08:01 534495 /usr/lib/python2.7/dist-packages/scipy/optimize/_cobyla.i386-linux-gnu.so a15af000-a15b7000 r-xp 00000000 08:01 534499 /usr/lib/python2.7/dist-packages/scipy/optimize/moduleTNC.i386-linux-gnu.so a15b7000-a15b8000 r--p 00007000 08:01 534499 /usr/lib/python2.7/dist-packages/scipy/optimize/moduleTNC.i386-linux-gnu.so a15b8000-a15b9000 rw-p 00008000 08:01 534499 /usr/lib/python2.7/dist-packages/scipy/optimize/moduleTNC.i386-linux-gnu.so a15b9000-a15f9000 rw-p 00000000 00:00 0 a15f9000-a1689000 r-xp 00000000 08:01 412052 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/eigen/arpack/_arpack.i386-linux-gnu.so a1689000-a168a000 r--p 0008f000 08:01 412052 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/eigen/arpack/_arpack.i386-linux-gnu.so a168a000-a1692000 rw-p 00090000 08:01 412052 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/eigen/arpack/_arpack.i386-linux-gnu.so a1692000-a16e9000 r-xp 00000000 08:01 412020 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/dsolve/_superlu.i386-linux-gnu.so a16e9000-a16ea000 r--p 00056000 08:01 412020 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/dsolve/_superlu.i386-linux-gnu.so a16ea000-a16eb000 rw-p 00057000 08:01 412020 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/dsolve/_superlu.i386-linux-gnu.so a16eb000-a171f000 r-xp 00000000 08:01 412028 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/_iterative.i386-linux-gnu.so a171f000-a1720000 r--p 00033000 08:01 412028 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/_iterative.i386-linux-gnu.so a1720000-a1726000 rw-p 00034000 08:01 412028 /usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/_iterative.i386-linux-gnu.so a1726000-a1727000 rw-p 00000000 00:00 0 a1727000-a1766000 r-xp 00000000 08:01 412005 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_reordering.i386-linux-gnu.so a1766000-a1767000 ---p 0003f000 08:01 412005 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_reordering.i386-linux-gnu.so a1767000-a1768000 r--p 0003f000 08:01 412005 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_reordering.i386-linux-gnu.so a1768000-a176c000 rw-p 00040000 08:01 412005 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_reordering.i386-linux-gnu.so a176c000-a1790000 r-xp 00000000 08:01 412007 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_min_spanning_tree.i386-linux-gnu.so a1790000-a1791000 r--p 00023000 08:01 412007 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_min_spanning_tree.i386-linux-gnu.so a1791000-a1794000 rw-p 00024000 08:01 412007 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_min_spanning_tree.i386-linux-gnu.so a1794000-a1795000 rw-p 00000000 00:00 0 a1795000-a17b4000 r-xp 00000000 08:01 411990 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_traversal.i386-linux-gnu.so a17b4000-a17b5000 r--p 0001e000 08:01 411990 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_traversal.i386-linux-gnu.so a17b5000-a17ba000 rw-p 0001f000 08:01 411990 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_traversal.i386-linux-gnu.so a17ba000-a17d9000 r-xp 00000000 08:01 411992 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_tools.i386-linux-gnu.so a17d9000-a17da000 r--p 0001e000 08:01 411992 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_tools.i386-linux-gnu.so a17da000-a17de000 rw-p 0001f000 08:01 411992 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_tools.i386-linux-gnu.so a17de000-a1812000 r-xp 00000000 08:01 412004 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_shortest_path.i386-linux-gnu.so a1812000-a1813000 r--p 00033000 08:01 412004 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_shortest_path.i386-linux-gnu.so a1813000-a1818000 rw-p 00034000 08:01 412004 /usr/lib/python2.7/dist-packages/scipy/sparse/csgraph/_shortest_path.i386-linux-gnu.so a1818000-a1858000 rw-p 00000000 00:00 0 a1858000-a18b3000 r-xp 00000000 08:01 283597 /usr/lib/python2.7/dist-packages/scipy/sparse/_csparsetools.i386-linux-gnu.so a18b3000-a18b4000 r--p 0005a000 08:01 283597 /usr/lib/python2.7/dist-packages/scipy/sparse/_csparsetools.i386-linux-gnu.so a18b4000-a18b8000 rw-p 0005b000 08:01 283597 /usr/lib/python2.7/dist-packages/scipy/sparse/_csparsetools.i386-linux-gnu.so a18b8000-a1bcd000 r-xp 00000000 08:01 283577 /usr/lib/python2.7/dist-packages/scipy/sparse/_sparsetools.i386-linux-gnu.so a1bcd000-a1bce000 r--p 00314000 08:01 283577 /usr/lib/python2.7/dist-packages/scipy/sparse/_sparsetools.i386-linux-gnu.so a1bce000-a1bcf000 rw-p 00315000 08:01 283577 /usr/lib/python2.7/dist-packages/scipy/sparse/_sparsetools.i386-linux-gnu.so a1bcf000-a1beb000 r-xp 00000000 08:01 534539 /usr/lib/python2.7/dist-packages/scipy/optimize/_lbfgsb.i386-linux-gnu.so a1beb000-a1bec000 r--p 0001b000 08:01 534539 /usr/lib/python2.7/dist-packages/scipy/optimize/_lbfgsb.i386-linux-gnu.so a1bec000-a1bed000 rw-p 0001c000 08:01 534539 /usr/lib/python2.7/dist-packages/scipy/optimize/_lbfgsb.i386-linux-gnu.so a1bed000-a1c2d000 rw-p 00000000 00:00 0 a1c2d000-a1c36000 r-xp 00000000 08:01 534548 /usr/lib/python2.7/dist-packages/scipy/optimize/minpack2.i386-linux-gnu.so a1c36000-a1c37000 r--p 00008000 08:01 534548 /usr/lib/python2.7/dist-packages/scipy/optimize/minpack2.i386-linux-gnu.so a1c37000-a1c38000 rw-p 00009000 08:01 534548 /usr/lib/python2.7/dist-packages/scipy/optimize/minpack2.i386-linux-gnu.so a1c38000-a1c5d000 r-xp 00000000 08:01 534392 /usr/lib/python2.7/dist-packages/scipy/integrate/lsoda.i386-linux-gnu.so a1c5d000-a1c5e000 r--p 00024000 08:01 534392 /usr/lib/python2.7/dist-packages/scipy/integrate/lsoda.i386-linux-gnu.so a1c5e000-a1c5f000 rw-p 00025000 08:01 534392 /usr/lib/python2.7/dist-packages/scipy/integrate/lsoda.i386-linux-gnu.so a1c5f000-a1c60000 rw-p 00000000 00:00 0 a1c60000-a1c75000 r-xp 00000000 08:01 534374 /usr/lib/python2.7/dist-packages/scipy/integrate/_dop.i386-linux-gnu.so a1c75000-a1c76000 r--p 00014000 08:01 534374 /usr/lib/python2.7/dist-packages/scipy/integrate/_dop.i386-linux-gnu.so a1c76000-a1c78000 rw-p 00015000 08:01 534374 /usr/lib/python2.7/dist-packages/scipy/integrate/_dop.i386-linux-gnu.so a1c78000-a1ca3000 r-xp 00000000 08:01 534376 /usr/lib/python2.7/dist-packages/scipy/integrate/vode.i386-linux-gnu.so a1ca3000-a1ca4000 r--p 0002a000 08:01 534376 /usr/lib/python2.7/dist-packages/scipy/integrate/vode.i386-linux-gnu.so a1ca4000-a1ca5000 rw-p 0002b000 08:01 534376 /usr/lib/python2.7/dist-packages/scipy/integrate/vode.i386-linux-gnu.so a1ca5000-a1ce6000 rw-p 00000000 00:00 0 a1ce6000-a1d00000 r-xp 00000000 08:01 534375 /usr/lib/python2.7/dist-packages/scipy/integrate/_quadpack.i386-linux-gnu.so a1d00000-a1d01000 r--p 00019000 08:01 534375 /usr/lib/python2.7/dist-packages/scipy/integrate/_quadpack.i386-linux-gnu.so a1d01000-a1d02000 rw-p 0001a000 08:01 534375 /usr/lib/python2.7/dist-packages/scipy/integrate/_quadpack.i386-linux-gnu.so a1d02000-a1d23000 r-xp 00000000 08:01 534393 /usr/lib/python2.7/dist-packages/scipy/integrate/_odepack.i386-linux-gnu.so a1d23000-a1d24000 r--p 00020000 08:01 534393 /usr/lib/python2.7/dist-packages/scipy/integrate/_odepack.i386-linux-gnu.so a1d24000-a1d25000 rw-p 00021000 08:01 534393 /usr/lib/python2.7/dist-packages/scipy/integrate/_odepack.i386-linux-gnu.so a1d25000-a1d36000 r-xp 00000000 08:01 701893 /usr/lib/python2.7/dist-packages/scipy/special/_ellip_harm_2.i386-linux-gnu.so a1d36000-a1d37000 r--p 00010000 08:01 701893 /usr/lib/python2.7/dist-packages/scipy/special/_ellip_harm_2.i386-linux-gnu.so a1d37000-a1d38000 rw-p 00011000 08:01 701893 /usr/lib/python2.7/dist-packages/scipy/special/_ellip_harm_2.i386-linux-gnu.so a1d38000-a1dd4000 r-xp 00000000 08:01 701842 /usr/lib/python2.7/dist-packages/scipy/linalg/cython_lapack.i386-linux-gnu.so a1dd4000-a1dd5000 r--p 0009b000 08:01 701842 /usr/lib/python2.7/dist-packages/scipy/linalg/cython_lapack.i386-linux-gnu.so a1dd5000-a1dda000 rw-p 0009c000 08:01 701842 /usr/lib/python2.7/dist-packages/scipy/linalg/cython_lapack.i386-linux-gnu.so a1dda000-a1e0f000 r-xp 00000000 08:01 701852 /usr/lib/python2.7/dist-packages/scipy/linalg/cython_blas.i386-linux-gnu.so a1e0f000-a1e10000 r--p 00034000 08:01 701852 /usr/lib/python2.7/dist-packages/scipy/linalg/cython_blas.i386-linux-gnu.so a1e10000-a1e13000 rw-p 00035000 08:01 701852 /usr/lib/python2.7/dist-packages/scipy/linalg/cython_blas.i386-linux-gnu.so a1e13000-a1e5d000 r-xp 00000000 08:01 701797 /usr/lib/python2.7/dist-packages/scipy/linalg/_decomp_update.i386-linux-gnu.so a1e5d000-a1e5e000 r--p 00049000 08:01 701797 /usr/lib/python2.7/dist-packages/scipy/linalg/_decomp_update.i386-linux-gnu.so a1e5e000-a1e67000 rw-p 0004a000 08:01 701797 /usr/lib/python2.7/dist-packages/scipy/linalg/_decomp_update.i386-linux-gnu.so a1e67000-a1e97000 r-xp 00000000 08:01 701803 /usr/lib/python2.7/dist-packages/scipy/linalg/_solve_toeplitz.i386-linux-gnu.so a1e97000-a1e98000 r--p 0002f000 08:01 701803 /usr/lib/python2.7/dist-packages/scipy/linalg/_solve_toeplitz.i386-linux-gnu.so a1e98000-a1e9b000 rw-p 00030000 08:01 701803 /usr/lib/python2.7/dist-packages/scipy/linalg/_solve_toeplitz.i386-linux-gnu.so a1e9b000-a1edb000 rw-p 00000000 00:00 0 a26a1000-a26a2000 ---p 00011000 08:01 147230 /usr/lib/python2.7/dist- bf8e6000-bf907000 rw-p 00000000 00:00 0 [stack] From python at mrabarnett.plus.com Thu Jan 19 19:24:49 2017 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 20 Jan 2017 00:24:49 +0000 Subject: Using python to start programs after logging in In-Reply-To: <87shoefwqe.fsf@Equus.decebal.nl> References: <878tq6amvj.fsf@Equus.decebal.nl> <87ziimg2yw.fsf@Equus.decebal.nl> <87shoefwqe.fsf@Equus.decebal.nl> Message-ID: On 2017-01-19 23:36, Cecil Westerhof wrote: > On Thursday 19 Jan 2017 22:21 CET, Cecil Westerhof wrote: > >> On Thursday 19 Jan 2017 21:12 CET, MRAB wrote: >> >>> On 2017-01-19 19:08, Cecil Westerhof wrote: >>>> I am writing a python program to start the programs that need to >>>> be started after logging in. >>>> >>>> I have the following imports: >>>> from subprocess import check_call, Popen, STDOUT >>>> from time import sleep, strftime >>>> >>>> And use the following code: check_call(tuple('wmctrl -s >>>> 10'.split())) log_file = >>>> open('Logging/firefox_%T.log'.replace('%T', strftime('%F_%R')), >>>> 'w') Popen(tuple('firefox'.split()), stdout = log_file, stderr = >>>> STDOUT) >>>> >>>> The first statement is to go to the correct desktop. >>>> >>>> Is this a good way to do things, or could I do it in a better way? >>>> >>> Apart from the other answer, your use of .replace is odd. This >>> would be better: >>> >>> log_file = open('Logging/firefox_%s.log' % strftime('%F_%R'), 'w') >> >> The reason is that it is ?generic? code. In the future instead of >> string it will be a variable and only when the variable contains %T >> it should be replaced. >> >> >>> Even better would be to use the 'with' statement as well. >> >> The same here. Not always has the output to be logged and then I >> would use NONE for log_file. > > I wrote a function for switching to the correct virtual desktop and > starting all the commands. I am also using with now: > def do_desktop(desktop, commands, seconds_to_wait = 10): > desktop_command = ('wmctrl -s ' + desktop).split() > check_call(tuple(desktop_command)) You don't have to pass a tuple to check_call; it'll also accept a list. > for command_arr in commands: > command = command_arr[0].split() > log_directory = command_arr[1] > directory = command_arr[2] > if (directory != ''): > chdir(directory) There's no need for the parentheses around the 'if' condition. > if (log_directory == 'NONE'): > Popen(tuple(command.split())) > else: > log_file_name = log_directory.replace('%T', strftime('%F_%R')) > with open(log_file_name, 'w') as log_file: > Popen(tuple(command), stdout = log_file, stderr = STDOUT) > if (directory != ''): > set_default_dir() Using 'chdir' is generally a bad idea. 'Popen' will start the process and then continue, so what you're doing is changing the directory, starting a process, and then changing the directory again, possibly before the process has truly started (i.e. you've told the OS to start it, but there might be a small delay before that happens). Popen accepts a 'cwd' argument, which sets the 'current working directory' for that process. > sleep(seconds_to_wait) > > > The function set_default_dir: > def set_default_dir(): > chdir(expanduser('~')) > > > The example with firefox is then done with: > commands = [ > ['firefox', 'Logging/firefox_%T.log', ''], > ] > do_desktop('10', commands, 30) > > > Sometimes a command needs to be executed in a different directory, > that is done like: > commands = [ > ['lein run', 'NONE', 'Clojure/Quotes'], > ['xfce4-terminal --maximize --execute screen -S Clojure -c ~/.screenrcClojure', 'NONE', ''], > ] > do_desktop('6', commands, 30) > Why 'NONE'? Why not None? From python at mrabarnett.plus.com Thu Jan 19 19:35:28 2017 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 20 Jan 2017 00:35:28 +0000 Subject: python corrupted double-linked list error In-Reply-To: References: Message-ID: <755b72f0-e388-7fa3-a29c-1660b3594298@mrabarnett.plus.com> On 2017-01-20 00:03, Xristos Xristoou wrote: > i am a python 2.7 and ubuntu 16.04 user and i have unexpected error when try to excute some python scripts . i use pycharm and i have the some error when i try to run python script from the ubuntu terminal. the error : > > *** Error in `/usr/bin/python2.7': corrupted double-linked list: 0x0b83c880 *** > > the error message : > > ======= Backtrace: ========= > /lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb764b377] > /lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb76512f7] > /lib/i386-linux-gnu/libc.so.6(+0x6e2f1)[0xb76522f1] > /usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x18)[0xb6ba9d88] > /usr/lib/libgdal.so.1(_ZN15GDALMajorObjectD0Ev+0x22)[0xb075a582] > /usr/lib/libgdal.so.1(GDALClose+0x77)[0xb074d747] > /usr/lib/qgis/plugins/libgdalprovider.so(+0xa930)[0xa4888930] > /usr/lib/qgis/plugins/libgdalprovider.so(+0xaafa)[0xa4888afa] > /usr/lib/libqgis_core.so.2.18.3(_ZN13QgsRasterPipeD1Ev+0x75)[0xb3d6a7d5] > /usr/lib/libqgis_core.so.2.18.3(_ZN14QgsRasterLayerD1Ev+0x2f)[0xb3d5cd0f] > /usr/lib/python2.7/dist-packages/qgis/_core.i386-linux-gnu.so(_ZN17sipQgsRasterLayerD1Ev+0x3b)[0xb489dd2b] > /usr/lib/python2.7/dist-packages/qgis/_core.i386-linux-gnu.so(_ZN17sipQgsRasterLayerD0Ev+0x1a)[0xb489dd5a] > /usr/lib/python2.7/dist-packages/qgis/_core.i386-linux-gnu.so(+0x43df45)[0xb4883f45] > /usr/lib/python2.7/dist-packages/qgis/_core.i386-linux-gnu.so(+0x43df8a)[0xb4883f8a] > /usr/lib/python2.7/dist-packages/sip.i386-linux-gnu.so(+0x5d49)[0xb724dd49] > /usr/lib/python2.7/dist-packages/sip.i386-linux-gnu.so(+0xc19b)[0xb725419b] > /usr/bin/python2.7[0x8144aad] > /usr/bin/python2.7[0x80fd127] > /usr/bin/python2.7(PyDict_SetItem+0x478)[0x80e9268] > /usr/bin/python2.7(_PyModule_Clear+0xba)[0x8149a1a] > /usr/bin/python2.7(PyImport_Cleanup+0x37a)[0x81495ca] > /usr/bin/python2.7(Py_Finalize+0x99)[0x8147399] > /usr/bin/python2.7(Py_Main+0x4bd)[0x80e639d] > /usr/bin/python2.7(main+0x26)[0x80e5ec6] > /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb75fc637] > /usr/bin/python2.7[0x80e5dc8] [snip] It looks like a bug in an extension, possibly in libgdal, qgis or libqgis. It has written to a part of the heap that it shouldn't, perhaps beyond the end of an allocated block, corrupting the heap in the process. From saxri89 at gmail.com Thu Jan 19 19:53:37 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Thu, 19 Jan 2017 16:53:37 -0800 (PST) Subject: python corrupted double-linked list error In-Reply-To: References: <755b72f0-e388-7fa3-a29c-1660b3594298@mrabarnett.plus.com> Message-ID: how to understand that ?solution ? From torriem at gmail.com Thu Jan 19 20:22:08 2017 From: torriem at gmail.com (Michael Torrie) Date: Thu, 19 Jan 2017 18:22:08 -0700 Subject: python corrupted double-linked list error In-Reply-To: References: <755b72f0-e388-7fa3-a29c-1660b3594298@mrabarnett.plus.com> Message-ID: On 01/19/2017 05:53 PM, Xristos Xristoou wrote: > > > how to understand that ?solution ? Well the problem is likely in the gdal or/and qgis modules. You'll probably want to talk to the qgis folks about this problem. It's not a bug in Python itself. If you can reproduce the problem with a minimal script, you can post it here and maybe someone can help here. From Cecil at decebal.nl Thu Jan 19 23:26:20 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Fri, 20 Jan 2017 05:26:20 +0100 Subject: Using python to start programs after logging in References: <878tq6amvj.fsf@Equus.decebal.nl> <87ziimg2yw.fsf@Equus.decebal.nl> <87shoefwqe.fsf@Equus.decebal.nl> Message-ID: <878tq65pcj.fsf@Equus.decebal.nl> On Friday 20 Jan 2017 01:24 CET, MRAB wrote: >> I wrote a function for switching to the correct virtual desktop and >> starting all the commands. I am also using with now: >> def do_desktop(desktop, commands, seconds_to_wait = 10): >> desktop_command = ('wmctrl -s ' + desktop).split() >> check_call(tuple(desktop_command)) > > You don't have to pass a tuple to check_call; it'll also accept a > list. I did not know that. The same for Popen. >> for command_arr in commands: >> command = command_arr[0].split() >> log_directory = command_arr[1] >> directory = command_arr[2] >> if (directory != ''): >> chdir(directory) > > There's no need for the parentheses around the 'if' condition. Thanks, I always thought these were necessary. >> if (log_directory == 'NONE'): >> Popen(tuple(command.split())) >> else: >> log_file_name = log_directory.replace('%T', strftime('%F_%R')) >> with open(log_file_name, 'w') as log_file: >> Popen(tuple(command), stdout = log_file, stderr = STDOUT) >> if (directory != ''): >> set_default_dir() > > Using 'chdir' is generally a bad idea. > > 'Popen' will start the process and then continue, so what you're > doing is changing the directory, starting a process, and then > changing the directory again, possibly before the process has truly > started (i.e. you've told the OS to start it, but there might be a > small delay before that happens). Popen accepts a 'cwd' argument, > which sets the 'current working directory' for that process. I am using cwd now. >> Sometimes a command needs to be executed in a different directory, >> that is done like: commands = [ ['lein run', 'NONE', >> 'Clojure/Quotes'], ['xfce4-terminal --maximize --execute screen -S >> Clojure -c ~/.screenrcClojure', 'NONE', ''], >> ] >> do_desktop('6', commands, 30) >> > Why 'NONE'? Why not None? I am using None now. Thanks. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From Cecil at decebal.nl Thu Jan 19 23:34:45 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Fri, 20 Jan 2017 05:34:45 +0100 Subject: Using python to start programs after logging in References: <878tq6amvj.fsf@Equus.decebal.nl> <87ziimg2yw.fsf@Equus.decebal.nl> <87shoefwqe.fsf@Equus.decebal.nl> Message-ID: <874m0u5oyi.fsf@Equus.decebal.nl> On Friday 20 Jan 2017 00:36 CET, Cecil Westerhof wrote: > I wrote a function for switching to the correct virtual desktop and > starting all the commands. I am also using with now: > def do_desktop(desktop, commands, seconds_to_wait = 10): > desktop_command = ('wmctrl -s ' + desktop).split() > check_call(tuple(desktop_command)) > for command_arr in commands: > command = command_arr[0].split() > log_directory = command_arr[1] > directory = command_arr[2] > if (directory != ''): > chdir(directory) > if (log_directory == 'NONE'): > Popen(tuple(command.split())) > else: > log_file_name = log_directory.replace('%T', strftime('%F_%R')) > with open(log_file_name, 'w') as log_file: > Popen(tuple(command), stdout = log_file, stderr = STDOUT) > if (directory != ''): > set_default_dir() > sleep(seconds_to_wait) I wrote a little better version: def do_desktop(desktop, commands, seconds_to_wait): desktop_command = (switch_desktop + desktop).split() if seconds_to_wait == 0: seconds_to_wait = 10 check_call(desktop_command) for command_arr in commands: command = command_arr[0].split() log_directory = command_arr[1] directory = command_arr[2] if not log_directory: log_directory = '/dev/null' log_file_name = log_directory.replace('%T', strftime('%F_%R')) with open(log_file_name, 'w') as log_file: Popen(command, stdout = log_file, cwd = directory, stderr = STDOUT) sleep(seconds_to_wait) When there is no log_directory I set it to '/dev/null': that makes the code a little bit cleaner. And everything is now fetched from the database: for desktop in cursor.execute(select_desktops).fetchall(): desktop_name = desktop[0] desktop_value = desktop[1] desktop_wait = desktop[2] commands = cursor.execute(select_commands, [desktop_name]).fetchall() do_desktop(desktop_value, commands, desktop_wait) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From python at deborahswanson.net Fri Jan 20 04:01:57 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Fri, 20 Jan 2017 01:01:57 -0800 Subject: Must target be only one bit one such as 0001, 0010, 0100, 1000 In supervised neural learning f(w*p+b) with perceptron rule w = w + e for linear case? In-Reply-To: Message-ID: <000e01d272fb$db42edc0$27b23dae@sambora> Ho Yeung Lee wrote, on January 19, 2017 12:05 AM > > Must target be only one bit one such as 0001,0010,0100,1000 > In supervised neural learning f(w*p+b) with perceptron rule w > = w + e for linear case? > > with neural network design > > does it means that can not use two or more one as target such > as 0011,0110,1100,1010, 0111,1110,1101, etc when training weight? With all respect, this sounds like a question you should be asking your professor, or one of the teaching assistants (TAs) for the course, or maybe your local computer science tutoring volunteers, if none of the above are available. This list is for questions and discussion about python. Maybe you're trying to code this problem in python, or maybe you're planning to at some stage, but you need to understand the fundamentals of the problem first. It's possible someone on this list would recognize the problem you're having, but it's unlikely. Come back with a clearly stated python problem, and I'm sure lots of people would help you with it. Deborah From dieter at handshake.de Fri Jan 20 04:46:14 2017 From: dieter at handshake.de (dieter) Date: Fri, 20 Jan 2017 10:46:14 +0100 Subject: ipython2 does not work anymore References: <87bmv3xf5r.fsf@Equus.decebal.nl> <35dadf92-ada7-02ce-3449-750cb914c426@mrabarnett.plus.com> <874m0uhhys.fsf@Equus.decebal.nl> Message-ID: <8737geukrd.fsf@handshake.de> Cecil Westerhof writes: > ... >> If you do mean 'pathlib', it was introduced in Python 3.4. > > It is about python2. I can remember to have seen announcements for enhanced "path" modules in this list. Your previously posted traceback shows that the problem comes from the package "pickleshare" which expects a "path" module (likely one of those enhanced modules) and cannot find one. I would try to find out what "path" module "pickleshare" requires and then install it. From dieter at handshake.de Fri Jan 20 04:52:27 2017 From: dieter at handshake.de (dieter) Date: Fri, 20 Jan 2017 10:52:27 +0100 Subject: python corrupted double-linked list error References: Message-ID: <87y3y6t5wk.fsf@handshake.de> Xristos Xristoou writes: > i am a python 2.7 and ubuntu 16.04 user. While analysing a problem upgrading to Ubuntu 16.04 (unrelated to Python) I found messages reporting about a problem with Python an Ubuntu 16.04[.0] (leading to memory corruption - something you are seeing). The proposed solution has been to upgrade to Ubuntu 16.04.1. From gordon at panix.com Fri Jan 20 12:46:00 2017 From: gordon at panix.com (John Gordon) Date: Fri, 20 Jan 2017 17:46:00 +0000 (UTC) Subject: Using python to start programs after logging in References: <878tq6amvj.fsf@Equus.decebal.nl> <878tq6hi0s.fsf@Equus.decebal.nl> Message-ID: In <878tq6hi0s.fsf at Equus.decebal.nl> Cecil Westerhof writes: > > I think using your window manager's built-in facilities for starting > > programs would be better. Why are you using Python instead? > Because when you use the window managers builtin facilities then all > programs will be started on the same virtual desktop and I want to > start them on different ones. The window manager doesn't allow you to specify a target desktop? That seems like a pretty heinous feature omission. -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From petef4+usenet at gmail.com Fri Jan 20 17:35:02 2017 From: petef4+usenet at gmail.com (Pete Forman) Date: Fri, 20 Jan 2017 22:35:02 +0000 Subject: PEP 393 vs UTF-8 Everywhere Message-ID: Can anyone point me at a rationale for PEP 393 being incorporated in Python 3.3 over using UTF-8 as an internal string representation? I've found good articles by Nick Coghlan, Armin Ronacher and others on the matter. What I have not found is discussion of pros and cons of alternatives to the old narrow or wide implementation of Unicode strings. ISTM that most operations on strings are via iterators and thus agnostic to variable or fixed width encodings. How important is it to be able to get to part of a string with a simple index? Just because old skool strings could be treated as a sequence of characters, is that a reason to shoehorn the subtleties of Unicode into that model? -- Pete Forman From ckaynor at zindagigames.com Fri Jan 20 18:06:24 2017 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Fri, 20 Jan 2017 15:06:24 -0800 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: Message-ID: On Fri, Jan 20, 2017 at 2:35 PM, Pete Forman wrote: > Can anyone point me at a rationale for PEP 393 being incorporated in > Python 3.3 over using UTF-8 as an internal string representation? I've > found good articles by Nick Coghlan, Armin Ronacher and others on the > matter. What I have not found is discussion of pros and cons of > alternatives to the old narrow or wide implementation of Unicode > strings. The PEP itself has the rational for the problems with the narrow/wide idea, the quote from https://www.python.org/dev/peps/pep-0393/: There are two classes of complaints about the current implementation of the unicode type:on systems only supporting UTF-16, users complain that non-BMP characters are not properly supported. On systems using UCS-4 internally (and also sometimes on systems using UCS-2), there is a complaint that Unicode strings take up too much memory - especially compared to Python 2.x, where the same code would often use ASCII strings (i.e. ASCII-encoded byte strings). With the proposed approach, ASCII-only Unicode strings will again use only one byte per character; while still allowing efficient indexing of strings containing non-BMP characters (as strings containing them will use 4 bytes per character). Basically, narrow builds had very odd behavior with non-BMP characters, namely that indexing into the string could easily produce mojibake. Wide builds used quite a bit more memory, which generally translates to reduced performance. > ISTM that most operations on strings are via iterators and thus agnostic > to variable or fixed width encodings. How important is it to be able to > get to part of a string with a simple index? Just because old skool > strings could be treated as a sequence of characters, is that a reason > to shoehorn the subtleties of Unicode into that model? I think you are underestimating the indexing usages of strings. Every operation on a string using UTF8 that contains larger characters must be completed by starting at index 0 - you can never start anywhere else safely. rfind/rsplit/rindex/rstrip and the other related reverse functions would require walking the string from start to end, rather than short-circuiting by reading from right to left. With indexing becoming linear time, many simple algorithms need to be written with that in mind, to avoid n*n time. Such performance regressions can often go unnoticed by developers, who are likely to be testing with small data, and thus may cause (accidental) DOS attacks when used on real data. The exact same problems occur with the old narrow builds (UTF16; note that this was NOT implemented in those builds, however, which caused the mojibake problems) as well - only a UTF32 or PEP393 implementation can avoid those problems. Note that from a user (including most developers, if not almost all), PEP393 strings can be treated as if they were UTF32, but with many of the benefits of UTF8. As far as I'm aware, it is only developers writing extension modules that need to care - and only then if they need maximum performance, and thus cannot convert every string they access to UTF32 or UTF8. -- Chris Kaynor From tomuxiong at gmx.com Fri Jan 20 18:15:50 2017 From: tomuxiong at gmx.com (Thomas Nyberg) Date: Fri, 20 Jan 2017 15:15:50 -0800 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: Message-ID: <9d3809e3-51e8-5697-a791-dc7f7e7d8bb6@gmx.com> On 01/20/2017 03:06 PM, Chris Kaynor wrote: > > [...snip...] > > -- > Chris Kaynor > I was able to delete my response which was a wholly contained subset of this one. :) But I have one extra question. Is string indexing guaranteed to be constant-time for python? I thought so, but I couldn't find it documented anywhere. (Not that I think it practically matters, since it couldn't really change if it weren't for all the reasons you mentioned.) I found this which at details (if not explicitly "guarantees") the complexity properties of other datatypes: https://wiki.python.org/moin/TimeComplexity Cheers, Thomas From rosuav at gmail.com Fri Jan 20 18:36:03 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 21 Jan 2017 10:36:03 +1100 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: <9d3809e3-51e8-5697-a791-dc7f7e7d8bb6@gmx.com> References: <9d3809e3-51e8-5697-a791-dc7f7e7d8bb6@gmx.com> Message-ID: On Sat, Jan 21, 2017 at 10:15 AM, Thomas Nyberg wrote: > But I have one extra question. Is string indexing guaranteed to be > constant-time for python? I thought so, but I couldn't find it documented > anywhere. (Not that I think it practically matters, since it couldn't really > change if it weren't for all the reasons you mentioned.) I found this which > at details (if not explicitly "guarantees") the complexity properties of > other datatypes: > No, it isn't; this question came up in the context of MicroPython, which chose to go UTF-8 internally instead of PEP 393. But the considerations for uPy are different - it's not designed to handle gobs of data, so constant-time vs linear isn't going to have as much impact. But in normal work, it's important enough to have predictable string performance. You can't afford to deploy a web application, test it, and then have someone send a large amount of data at it, causing massive O(n^2) blowouts. ChrisA From ckaynor at zindagigames.com Fri Jan 20 18:37:44 2017 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Fri, 20 Jan 2017 15:37:44 -0800 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: <9d3809e3-51e8-5697-a791-dc7f7e7d8bb6@gmx.com> References: <9d3809e3-51e8-5697-a791-dc7f7e7d8bb6@gmx.com> Message-ID: . On Fri, Jan 20, 2017 at 3:15 PM, Thomas Nyberg wrote: > On 01/20/2017 03:06 PM, Chris Kaynor wrote: >> >> >> [...snip...] >> >> -- >> Chris Kaynor >> > > I was able to delete my response which was a wholly contained subset of this > one. :) > > > But I have one extra question. Is string indexing guaranteed to be > constant-time for python? I thought so, but I couldn't find it documented > anywhere. (Not that I think it practically matters, since it couldn't really > change if it weren't for all the reasons you mentioned.) I found this which > at details (if not explicitly "guarantees") the complexity properties of > other datatypes: > > https://wiki.python.org/moin/TimeComplexity As far as I'm aware, the language does not guarantee it. In fact, I believe it was decided that MicroPython could use UTF8 strings with linear indexing while still calling itself Python. This was very useful for MicroPython due to the platforms it supports (embedded), and needing to keep the memory footprint very small. I believe Guido (on Python-ideas) has stated that constant-time string indexing is a guarantee of CPython, however. The only reference I found in my (very quick) search is the Python-Dev thread at https://groups.google.com/forum/#!msg/dev-python/3lfXwljNLj8/XxO2s0TGYrYJ From python at mrabarnett.plus.com Fri Jan 20 19:18:51 2017 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 21 Jan 2017 00:18:51 +0000 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: Message-ID: On 2017-01-20 23:06, Chris Kaynor wrote: > On Fri, Jan 20, 2017 at 2:35 PM, Pete Forman wrote: >> Can anyone point me at a rationale for PEP 393 being incorporated in >> Python 3.3 over using UTF-8 as an internal string representation? I've >> found good articles by Nick Coghlan, Armin Ronacher and others on the >> matter. What I have not found is discussion of pros and cons of >> alternatives to the old narrow or wide implementation of Unicode >> strings. > > The PEP itself has the rational for the problems with the narrow/wide > idea, the quote from https://www.python.org/dev/peps/pep-0393/: > There are two classes of complaints about the current implementation > of the unicode type:on systems only supporting UTF-16, users complain > that non-BMP characters are not properly supported. On systems using > UCS-4 internally (and also sometimes on systems using UCS-2), there is > a complaint that Unicode strings take up too much memory - especially > compared to Python 2.x, where the same code would often use ASCII > strings (i.e. ASCII-encoded byte strings). With the proposed approach, > ASCII-only Unicode strings will again use only one byte per character; > while still allowing efficient indexing of strings containing non-BMP > characters (as strings containing them will use 4 bytes per > character). > > Basically, narrow builds had very odd behavior with non-BMP > characters, namely that indexing into the string could easily produce > mojibake. Wide builds used quite a bit more memory, which generally > translates to reduced performance. > >> ISTM that most operations on strings are via iterators and thus agnostic >> to variable or fixed width encodings. How important is it to be able to >> get to part of a string with a simple index? Just because old skool >> strings could be treated as a sequence of characters, is that a reason >> to shoehorn the subtleties of Unicode into that model? > > I think you are underestimating the indexing usages of strings. Every > operation on a string using UTF8 that contains larger characters must > be completed by starting at index 0 - you can never start anywhere > else safely. rfind/rsplit/rindex/rstrip and the other related reverse > functions would require walking the string from start to end, rather > than short-circuiting by reading from right to left. With indexing > becoming linear time, many simple algorithms need to be written with > that in mind, to avoid n*n time. Such performance regressions can > often go unnoticed by developers, who are likely to be testing with > small data, and thus may cause (accidental) DOS attacks when used on > real data. The exact same problems occur with the old narrow builds > (UTF16; note that this was NOT implemented in those builds, however, > which caused the mojibake problems) as well - only a UTF32 or PEP393 > implementation can avoid those problems. > You could implement rsplit and rstrip easily enough, but rfind and rindex return the index, so you'd need to scan the string to return that. > Note that from a user (including most developers, if not almost all), > PEP393 strings can be treated as if they were UTF32, but with many of > the benefits of UTF8. As far as I'm aware, it is only developers > writing extension modules that need to care - and only then if they > need maximum performance, and thus cannot convert every string they > access to UTF32 or UTF8. > As someone who has written an extension, I can tell you that I much prefer dealing with a fixed number of bytes per codepoint than a variable number of bytes per codepoint, especially as I'm also supporting earlier versions of Python where that was the case. From petef4+usenet at gmail.com Fri Jan 20 19:30:16 2017 From: petef4+usenet at gmail.com (Pete Forman) Date: Sat, 21 Jan 2017 00:30:16 +0000 Subject: PEP 393 vs UTF-8 Everywhere References: Message-ID: Chris Kaynor writes: > On Fri, Jan 20, 2017 at 2:35 PM, Pete Forman wrote: >> Can anyone point me at a rationale for PEP 393 being incorporated in >> Python 3.3 over using UTF-8 as an internal string representation? >> I've found good articles by Nick Coghlan, Armin Ronacher and others >> on the matter. What I have not found is discussion of pros and cons >> of alternatives to the old narrow or wide implementation of Unicode >> strings. > > The PEP itself has the rational for the problems with the narrow/wide > idea, the quote from https://www.python.org/dev/peps/pep-0393/: There > are two classes of complaints about the current implementation of the > unicode type:on systems only supporting UTF-16, users complain that > non-BMP characters are not properly supported. On systems using UCS-4 > internally (and also sometimes on systems using UCS-2), there is a > complaint that Unicode strings take up too much memory - especially > compared to Python 2.x, where the same code would often use ASCII > strings (i.e. ASCII-encoded byte strings). With the proposed approach, > ASCII-only Unicode strings will again use only one byte per character; > while still allowing efficient indexing of strings containing non-BMP > characters (as strings containing them will use 4 bytes per > character). > > Basically, narrow builds had very odd behavior with non-BMP > characters, namely that indexing into the string could easily produce > mojibake. Wide builds used quite a bit more memory, which generally > translates to reduced performance. I'm taking as a given that the old way was often sub-optimal in many scenarios. My questions were about the alternatives, and why PEP 393 was chosen over other approaches. >> ISTM that most operations on strings are via iterators and thus >> agnostic to variable or fixed width encodings. How important is it to >> be able to get to part of a string with a simple index? Just because >> old skool strings could be treated as a sequence of characters, is >> that a reason to shoehorn the subtleties of Unicode into that model? > > I think you are underestimating the indexing usages of strings. Every > operation on a string using UTF8 that contains larger characters must > be completed by starting at index 0 - you can never start anywhere > else safely. rfind/rsplit/rindex/rstrip and the other related reverse > functions would require walking the string from start to end, rather > than short-circuiting by reading from right to left. With indexing > becoming linear time, many simple algorithms need to be written with > that in mind, to avoid n*n time. Such performance regressions can > often go unnoticed by developers, who are likely to be testing with > small data, and thus may cause (accidental) DOS attacks when used on > real data. The exact same problems occur with the old narrow builds > (UTF16; note that this was NOT implemented in those builds, however, > which caused the mojibake problems) as well - only a UTF32 or PEP393 > implementation can avoid those problems. I was asserting that most useful operations on strings start from index 0. The r* operations would not be slowed down that much as UTF-8 has the useful property that attempting to interpret from a byte that is not at the start of a sequence (in the sense of a code point rather than Python) is invalid and so quick to move over while working backwards from the end. The only significant use of an index dereference that I could come up with was the result of a find() or index(). I put out this public question so that I could be enclued as to other uses. My personal experience is that in most cases where I might consider find() that I end up using re and use the return from match groups which has copies of the (sub)strings that I want. > Note that from a user (including most developers, if not almost all), > PEP393 strings can be treated as if they were UTF32, but with many of > the benefits of UTF8. As far as I'm aware, it is only developers > writing extension modules that need to care - and only then if they > need maximum performance, and thus cannot convert every string they > access to UTF32 or UTF8. PEP 393 already says that "the specification chooses UTF-8 as the recommended way of exposing strings to C code". -- Pete Forman From petef4+usenet at gmail.com Fri Jan 20 19:51:53 2017 From: petef4+usenet at gmail.com (Pete Forman) Date: Sat, 21 Jan 2017 00:51:53 +0000 Subject: PEP 393 vs UTF-8 Everywhere References: Message-ID: MRAB writes: > As someone who has written an extension, I can tell you that I much > prefer dealing with a fixed number of bytes per codepoint than a > variable number of bytes per codepoint, especially as I'm also > supporting earlier versions of Python where that was the case. At the risk of sounding harsh, if supporting variable bytes per codepoint is a pain you should roll with it for the greater good of supporting users. PEP 393 / Python 3.3 required extension writers to revisit their access to strings. My explicit question was about why PEP 393 was adopted to replace the deficient old implementations rather than another approach. The implicit question is whether a UTF-8 internal representation should replace that of PEP 393. -- Pete Forman From rosuav at gmail.com Fri Jan 20 19:58:01 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 21 Jan 2017 11:58:01 +1100 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: Message-ID: On Sat, Jan 21, 2017 at 11:30 AM, Pete Forman wrote: > I was asserting that most useful operations on strings start from index > 0. The r* operations would not be slowed down that much as UTF-8 has the > useful property that attempting to interpret from a byte that is not at > the start of a sequence (in the sense of a code point rather than > Python) is invalid and so quick to move over while working backwards > from the end. Let's take one very common example: decoding JSON. A ton of web servers out there will call json.loads() on user-supplied data. The bulk of the work is in the scanner, which steps through the string and does the actual parsing. That function is implemented in Python, so it's a good example. (There is a C accelerator, but we can ignore that and look at the pure Python one.) So, how could you implement this function? The current implementation maintains an index - an integer position through the string. It repeatedly requests the next character as string[idx], and can also slice the string (to check for keywords like "true") or use a regex (to check for numbers). Everything's clean, but it's lots of indexing. Alternatively, it could remove and discard characters as they're consumed. It would maintain a string that consists of all the unparsed characters. All indexing would be at or near zero, but after every tiny piece of parsing, the string would get sliced. With immutable UTF-8 strings, both of these would be O(n^2). Either indexing is linear, so parsing the tail of the string means scanning repeatedly; or slicing is linear, so parsing the head of the string means slicing all the rest away. The only way for it to be fast enough would be to have some sort of retainable string iterator, which means exposing an opaque "position marker" that serves no purpose other than parsing. Every string parse operation would have to be reimplemented this way, lest it perform abysmally on large strings. It'd mean some sort of magic "thing" that probably has a reference to the original string, so you don't get the progressive RAM refunds that slicing gives, and you'd still have to deal with lots of the other consequences. It's probably doable, but it would be a lot of pain. ChrisA From rosuav at gmail.com Fri Jan 20 20:00:58 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 21 Jan 2017 12:00:58 +1100 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: Message-ID: On Sat, Jan 21, 2017 at 11:51 AM, Pete Forman wrote: > MRAB writes: > >> As someone who has written an extension, I can tell you that I much >> prefer dealing with a fixed number of bytes per codepoint than a >> variable number of bytes per codepoint, especially as I'm also >> supporting earlier versions of Python where that was the case. > > At the risk of sounding harsh, if supporting variable bytes per > codepoint is a pain you should roll with it for the greater good of > supporting users. That hasn't been demonstrated, though. There's plenty of evidence regarding cache usage that shows that direct indexing is incredibly beneficial on large strings. What are the benefits of variable-sized encodings? AFAIK, the only real benefit is that you can use less memory for strings that contain predominantly ASCII but a small number of astral characters (plus *maybe* a faster encode-to-UTF-8; you wouldn't get a faster decode-from-UTF-8, because you still need to check that the byte sequence is valid). Can you show a use-case that would be materially improved by UTF-8? ChrisA From python at mrabarnett.plus.com Fri Jan 20 21:10:15 2017 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 21 Jan 2017 02:10:15 +0000 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: Message-ID: <22ab560b-febf-429e-297e-1e41e232da68@mrabarnett.plus.com> On 2017-01-21 00:51, Pete Forman wrote: > MRAB writes: > >> As someone who has written an extension, I can tell you that I much >> prefer dealing with a fixed number of bytes per codepoint than a >> variable number of bytes per codepoint, especially as I'm also >> supporting earlier versions of Python where that was the case. > > At the risk of sounding harsh, if supporting variable bytes per > codepoint is a pain you should roll with it for the greater good of > supporting users. > Or I could decide not bother and leave it to someone else to continue the project. After all, it's not like I'm not getting paid for the work, it's purely voluntary. > PEP 393 / Python 3.3 required extension writers to revisit their access > to strings. My explicit question was about why PEP 393 was adopted to > replace the deficient old implementations rather than another approach. > The implicit question is whether a UTF-8 internal representation should > replace that of PEP 393. > I already had to handle 1-byte bytestrings and 2/4-byte (narrow/wide) Unicode strings, so switching to 1/2/4 strings wasn't too bad. Switching to a completely different, variable-width system would've been a lot more work. From no.email at nospam.invalid Sat Jan 21 01:01:09 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Fri, 20 Jan 2017 22:01:09 -0800 Subject: PEP 393 vs UTF-8 Everywhere References: Message-ID: <87inp9rly2.fsf@jester.hsd1.ca.comcast.net> Chris Angelico writes: > decoding JSON... the scanner, which steps through the string and > does the actual parsing. ... > The only way for it to be fast enough would be to have some sort of > retainable string iterator, which means exposing an opaque "position > marker" that serves no purpose other than parsing. Python already has that type of iterator: x = "foo" for c in x: .... > It'd mean some sort of magic "thing" that probably has a reference to > the original string It's a regular old string iterator unless I'm missing something. Of course a json parser should use it, though who uses the non-C json parser anyway these days? [Chris Kaynor writes:] > rfind/rsplit/rindex/rstrip and the other related reverse > functions would require walking the string from start to end, rather > than short-circuiting by reading from right to left. UTF-8 can be read from right to left because you can recognize when a codepoint begins by looking at the top 2 bits of each byte as you scan backwards. Any combination except for 11 is a leading byte, and 11 is always a continuation byte. This "prefix property" of UTF8 is a design feature and not a trick someone noticed after the fact. Also if you really want O(1) random access, you could put an auxiliary table into long strings, giving the byte offset of every 256th codepoint or something like that. Then you'd go to the nearest table entry and scan from there. This would usually be in-cache scanning so quite fast. Or use the related representation of "ropes" which are also very easy to concatenate if they can be nested. Erlang does something like that with what it calls "binaries". From rosuav at gmail.com Sat Jan 21 01:23:02 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 21 Jan 2017 17:23:02 +1100 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: <87inp9rly2.fsf@jester.hsd1.ca.comcast.net> References: <87inp9rly2.fsf@jester.hsd1.ca.comcast.net> Message-ID: On Sat, Jan 21, 2017 at 5:01 PM, Paul Rubin wrote: > Chris Angelico writes: >> decoding JSON... the scanner, which steps through the string and >> does the actual parsing. ... >> The only way for it to be fast enough would be to have some sort of >> retainable string iterator, which means exposing an opaque "position >> marker" that serves no purpose other than parsing. > > Python already has that type of iterator: > x = "foo" > for c in x: .... > >> It'd mean some sort of magic "thing" that probably has a reference to >> the original string > > It's a regular old string iterator unless I'm missing something. Of > course a json parser should use it, though who uses the non-C json > parser anyway these days? You can't do a look-ahead with a vanilla string iterator. That's necessary for a lot of parsers. > Also if you really want O(1) random access, you could put an auxiliary > table into long strings, giving the byte offset of every 256th codepoint > or something like that. Then you'd go to the nearest table entry and > scan from there. This would usually be in-cache scanning so quite fast. > Or use the related representation of "ropes" which are also very easy to > concatenate if they can be nested. Erlang does something like that > with what it calls "binaries". Yes, which gives a two-level indexing (first find the strand, then the character), and that's going to play pretty badly with CPU caches. I'd be curious to know how an alternate Python with that implementation would actually perform. ChrisA From jussi.piitulainen at helsinki.fi Sat Jan 21 01:38:33 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sat, 21 Jan 2017 08:38:33 +0200 Subject: PEP 393 vs UTF-8 Everywhere References: Message-ID: Chris Angelico writes: > On Sat, Jan 21, 2017 at 11:30 AM, Pete Forman wrote: >> I was asserting that most useful operations on strings start from >> index 0. The r* operations would not be slowed down that much as >> UTF-8 has the useful property that attempting to interpret from a >> byte that is not at the start of a sequence (in the sense of a code >> point rather than Python) is invalid and so quick to move over while >> working backwards from the end. > > Let's take one very common example: decoding JSON. A ton of web > servers out there will call json.loads() on user-supplied data. The > bulk of the work is in the scanner, which steps through the string and > does the actual parsing. That function is implemented in Python, so > it's a good example. (There is a C accelerator, but we can ignore that > and look at the pure Python one.) > > So, how could you implement this function? The current implementation > maintains an index - an integer position through the string. It > repeatedly requests the next character as string[idx], and can also > slice the string (to check for keywords like "true") or use a regex > (to check for numbers). Everything's clean, but it's lots of indexing. > Alternatively, it could remove and discard characters as they're > consumed. It would maintain a string that consists of all the unparsed > characters. All indexing would be at or near zero, but after every > tiny piece of parsing, the string would get sliced. > > With immutable UTF-8 strings, both of these would be O(n^2). Either > indexing is linear, so parsing the tail of the string means scanning > repeatedly; or slicing is linear, so parsing the head of the string > means slicing all the rest away. > > The only way for it to be fast enough would be to have some sort of > retainable string iterator, which means exposing an opaque "position > marker" that serves no purpose other than parsing. Every string parse > operation would have to be reimplemented this way, lest it perform > abysmally on large strings. It'd mean some sort of magic "thing" that > probably has a reference to the original string, so you don't get the > progressive RAM refunds that slicing gives, and you'd still have to > deal with lots of the other consequences. It's probably doable, but it > would be a lot of pain. Julia does this. It has immutable UTF-8 strings, and there is a JSON parser. The "opaque position marker" is just the byte index. An attempt to use an invalid index throws an error. A substring type points to an underlying string. An iterator, called graphemes, even returns substrings that correspond to what people might consider a character. I offer Julia as evidence. My impression is that Julia's UTF-8-based system works and is not a pain. I wrote a toy function once to access the last line of a large memory-mapped text file, so I have just this little bit of personal experience of it, so far. Incidentally, can Python memory-map a UTF-8 file as a string? http://docs.julialang.org/en/stable/manual/strings/ https://github.com/JuliaIO/JSON.jl From no.email at nospam.invalid Sat Jan 21 04:14:20 2017 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 21 Jan 2017 01:14:20 -0800 Subject: PEP 393 vs UTF-8 Everywhere References: <87inp9rly2.fsf@jester.hsd1.ca.comcast.net> Message-ID: <87efzwsrkj.fsf@jester.hsd1.ca.comcast.net> Chris Angelico writes: > You can't do a look-ahead with a vanilla string iterator. That's > necessary for a lot of parsers. For JSON? For other parsers you usually have a tokenizer that reads characters with maybe 1 char of lookahead. > Yes, which gives a two-level indexing (first find the strand, then the > character), and that's going to play pretty badly with CPU caches. If you're jumping around at random all over the string, you probably really want a bytearray rather than a unicode string. If you're scanning sequentually you won't have to look at the outer table very often. From Cecil at decebal.nl Sat Jan 21 06:30:50 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Sat, 21 Jan 2017 12:30:50 +0100 Subject: Let ipython3 use the latest python3 Message-ID: <87efzwzm39.fsf@Equus.decebal.nl> I built python3.6, but ipython3 is still using the old one (3.4.5). How can I make ipython3 use 3.6? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From kwpolska at gmail.com Sat Jan 21 06:49:38 2017 From: kwpolska at gmail.com (Chris Warrick) Date: Sat, 21 Jan 2017 12:49:38 +0100 Subject: Let ipython3 use the latest python3 In-Reply-To: <87efzwzm39.fsf@Equus.decebal.nl> References: <87efzwzm39.fsf@Equus.decebal.nl> Message-ID: On 21 January 2017 at 12:30, Cecil Westerhof wrote: > I built python3.6, but ipython3 is still using the old one (3.4.5). > How can I make ipython3 use 3.6? All packages you have installed are tied to a specific Python version. If you want to use IPython with Python 3.6, you need to install it for that version (most likely, with pip) and make sure there is an ipython3 executable in your $PATH pointing at 3.6. You don?t need to remove IPython for 3.4 (but you can if you want to get rid of it). -- Chris Warrick PGP: 5EAAEA16 From Cecil at decebal.nl Sat Jan 21 06:56:28 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Sat, 21 Jan 2017 12:56:28 +0100 Subject: Problems with python3.6 on one system, but OK on another Message-ID: <87a8akzkwj.fsf@Equus.decebal.nl> I build python3.6 on two systems. On one system everything is OK: Python 3.6.0 (default, Jan 21 2017, 11:19:56) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. But on another I get: Could not find platform dependent libraries Consider setting $PYTHONHOME to [:] Python 3.6.0 (default, Jan 21 2017, 12:20:38) [GCC 4.8.5] on linux Type "help", "copyright", "credits" or "license" for more information. Probably not a big problem, but just wondering what is happening here. On both systems PYTHONHOME is not set and with the old version (3.4.5) I did/do not get this message. Another is that I used PYTHONSTARTUP to point to the following script: # startup script for python to enable saving of interpreter history and # enabling name completion # import needed modules import atexit import os import readline import rlcompleter # where is history saved historyPath = os.path.expanduser("~/.pyhistory") # handler for saving history def save_history(historyPath=historyPath): import readline try: readline.write_history_file(historyPath) except: pass # read history, if it exists if os.path.exists(historyPath): readline.set_history_length(10000) readline.read_history_file(historyPath) # register saving handler atexit.register(save_history) # enable completion readline.parse_and_bind('tab: complete') # cleanup del os, atexit, readline, rlcompleter, save_history, historyPath This works with 3.4.5, but with 3.6 it gives: Traceback (most recent call last): File "/etc/pythonstart", line 7, in import readline ModuleNotFoundError: No module named 'readline' Probably not a big problem because I will mostly work with ipython3 at the moment I get it working with 3.6, but just wondering. By the way all other import (including rlcompleter) do work in 3.6. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From python.list at tim.thechases.com Sat Jan 21 07:45:46 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 21 Jan 2017 06:45:46 -0600 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: Message-ID: <20170121064546.0eba2e4c@bigbox.christie.dr> On 2017-01-21 11:58, Chris Angelico wrote: > So, how could you implement this function? The current > implementation maintains an index - an integer position through the > string. It repeatedly requests the next character as string[idx], > and can also slice the string (to check for keywords like "true") > or use a regex (to check for numbers). Everything's clean, but it's > lots of indexing. But in these parsing cases, the indexes all originate from stepping through the string from the beginning and processing it codepointwise. Even this is a bit of an oddity, especially once you start taking combining characters into consideration and need to process them with the preceding character(s). So while you may be doing indexing, those indexes usually stem from having walked to that point, not arbitrarily picking some offset. You allude to it in your: > The only way for it to be fast enough would be to have some sort of > retainable string iterator, which means exposing an opaque "position > marker" that serves no purpose other than parsing. Every string > parse operation would have to be reimplemented this way, lest it > perform abysmally on large strings. It'd mean some sort of magic > "thing" that probably has a reference to the original string, so > you don't get the progressive RAM refunds that slicing gives, and > you'd still have to deal with lots of the other consequences. It's > probably doable, but it would be a lot of pain. but I'm hard-pressed to come up with any use case where direct indexing into a (non-byte)string makes sense unless you've already processed/searched up to that point and can use a recorded index from that processing/search. Can you provide real-world examples of "I need character 2832 from this string of unicode text, but I never had to scan to that point linearly from the beginning/end of the string"? -tkc From steve+python at pearwood.info Sat Jan 21 08:56:03 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 22 Jan 2017 00:56:03 +1100 Subject: PEP 393 vs UTF-8 Everywhere References: Message-ID: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> On Sat, 21 Jan 2017 09:35 am, Pete Forman wrote: > Can anyone point me at a rationale for PEP 393 being incorporated in > Python 3.3 over using UTF-8 as an internal string representation? I've read over the PEP, and the email discussion, and there is very little mention of UTF-8, and as far as I can see no counter-proposal for using UTF-8. However, there are a few mentions of UTF-8 that suggest that the participants were aware of it as an alternative, and simply didn't think it was worth considering. I don't know why. You can read the PEP and the mailing list discussion here: The PEP: https://www.python.org/dev/peps/pep-0393/ Mailing list discussion starts here: https://mail.python.org/pipermail/python-dev/2011-January/107641.html Stefan Behnel (author of Cython) states that UTF-8 is much harder to use: https://mail.python.org/pipermail/python-dev/2011-January/107739.html I see nobody challenging that claim, so perhaps there was simply enough broad agreement that UTF-8 would have been more work and so nobody wanted to propose it. I'm just guessing though. Perhaps it would have been too big a change to adapt the CPython internals to variable-width UTF-8 from the existing fixed-width UTF-16 and UTF-32 implementations? (I know that UTF-16 is actually variable-width, but Python prior to PEP 393 treated it as if it were fixed.) There was a much earlier discussion about the internal implementation of Unicode strings: https://mail.python.org/pipermail/python-3000/2006-September/003795.html including some discussion of UTF-8: https://mail.python.org/pipermail/python-3000/2006-September/003816.html It too proposed using a three-way internal implementation, and made it clear that O(1) indexing was an requirement. Here's a comment explicitly pointing out that constant-time indexing is wanted, and that using UTF-8 with a two-level table destroys any space advantage UTF-8 might have: https://mail.python.org/pipermail/python-3000/2006-September/003822.html Ironically, Martin v. L?wis, the author of PEP 393 originally started off opposing an three-way internal representation, calling it "terrible": https://mail.python.org/pipermail/python-3000/2006-September/003891.html Another factor which I didn't see discussed anywhere is that Python strings treat surrogates as normal code points. I believe that would be troublesome for a UTF-8 implementation: py> '\uDC37'.encode('utf-8') Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'utf-8' codec can't encode character '\udc37' in position 0: surrogates not allowed but of course with a UCS-2 or UTF-32 implementation it is trivial: you just treat the surrogate as another code point like any other. [...] > ISTM that most operations on strings are via iterators and thus agnostic > to variable or fixed width encodings. Slicing is not. start = text.find(":") end = text.rfind("!") assert end > start chunk = text[start:end] But even with iteration, we still would expect that indexes be consecutive: for i, c in enumerate(text): assert c == text[i] The complexity of those functions will be greatly increased with UTF-8. Of course you can make it work, and you can even hide the fact that UTF-8 has variable-width code points. But you can't have all three of: - simplicity; - memory efficiency; - O(1) operations with UTF-8. But of course, I'd be happy for a competing Python implementation to use UTF-8 and prove me wrong! -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Sat Jan 21 09:44:30 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 22 Jan 2017 01:44:30 +1100 Subject: PEP 393 vs UTF-8 Everywhere References: <20170121064546.0eba2e4c@bigbox.christie.dr> Message-ID: <588373cf$0$1602$c3e8da3$5496439d@news.astraweb.com> On Sat, 21 Jan 2017 11:45 pm, Tim Chase wrote: > but I'm hard-pressed to come up with any use case where direct > indexing into a (non-byte)string makes sense unless you've already > processed/searched up to that point and can use a recorded index > from that processing/search. Let's take a simple example: you do a find to get an offset, and then slice from that offset. py> text = "??????xx" py> offset = text.find("?") py> stuff = text[offset:] py> assert stuff == "?xx" That works fine whether indexing refers to code points or bytes. py> "??????xx".find("?") 5 py> "??????xx".encode('utf-8').find("?".encode('utf-8')) 10 Either way, you get the expected result. However: py> stuff = text[offset + 1:] py> assert stuff == "xx" That requires indexes to point to the beginning of *code points*, not bytes: taking byte 11 of "??????xx".encode('utf-8') drops you into the middle of the ? representation: py> "??????xx".encode('utf-8')[11:] b'\x84xx' and it isn't a valid UTF-8 substring. Slicing would generate an exception unless you happened to slice right at the start of a code point. It's like seek() and tell() on text files: you cannot seek to arbitrary positions, but only to the opaque positions returned by tell. That's unacceptable for strings. You could avoid that error by increasing the offset by the right amount: stuff = text[offset + len("?".encode('utf-8'):] which is awful. I believe that's what Go and Julia expect you to do. Another solution would be to have the string slicing method automatically scan forward to the start of the next valid UTF-8 code point. That would be the "Do What I Mean" solution. The problem with the DWIM solution is that not only is it adding complexity, but it's frankly *weird*. It would mean: - if the character at position `offset` fits in 2 bytes: text[offset+1:] == text[offset+2:] - if it fits in 3 bytes: text[offset+1:] == text[offset+2:] == text[offset+3:] - and if it fits in 4 bytes: text[offset+1:] == text[offset+2:] == text[offset+3:] == text[offset+4:] Having the string slicing method Do The Right Thing would actually be The Wrong Thing. It would make it awful to reason about slicing. You can avoid this by having the interpreter treat the Python-level indexes as opaque "code point offsets", and converting them to and from "byte offsets" as needed. That's not even very hard. But it either turns every indexing into O(N) (since you have to walk the string to count which byte represents the nth code point), or you have to keep an auxiliary table with every string, letting you convert from byte indexes to code point indexes quickly, but that will significantly increase the memory size of every string, blowing out the advantage of using UTF-8 in the first place. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From petef4+usenet at gmail.com Sat Jan 21 10:50:40 2017 From: petef4+usenet at gmail.com (Pete Forman) Date: Sat, 21 Jan 2017 15:50:40 +0000 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steve D'Aprano writes: > [...] > Another factor which I didn't see discussed anywhere is that Python > strings treat surrogates as normal code points. I believe that would > be troublesome for a UTF-8 implementation: > > py> '\uDC37'.encode('utf-8') > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'utf-8' codec can't encode character '\udc37' in > position 0: surrogates not allowed > > but of course with a UCS-2 or UTF-32 implementation it is trivial: you > just treat the surrogate as another code point like any other. Thanks for a very thorough reply, most useful. I'm going to pick you up on the above, though. Surrogates only exist in UTF-16. They are expressly forbidden in UTF-8 and UTF-32. The rules for UTF-8 were tightened up in Unicode 4 and RFC 3629 (2003). There is CESU-8 if you really need a naive encoding of UTF-16 to UTF-8-alike. py> low = '\uDC37' is only meaningful on narrow builds pre Python 3.3 where the user must do extra to correctly handle characters outside the BMP. -- Pete Forman https://payg-petef.rhcloud.com From jussi.piitulainen at helsinki.fi Sat Jan 21 10:56:35 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sat, 21 Jan 2017 17:56:35 +0200 Subject: PEP 393 vs UTF-8 Everywhere References: <20170121064546.0eba2e4c@bigbox.christie.dr> <588373cf$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steve D'Aprano writes: [snip] > You could avoid that error by increasing the offset by the right > amount: > > stuff = text[offset + len("?".encode('utf-8'):] > > which is awful. I believe that's what Go and Julia expect you to do. Julia provides a method to get the next index. let text = "??? ?????? ??????", offset = 1 while offset <= endof(text) print(text[offset], ".") offset = nextind(text, offset) end println() end # prints: ?.?.?. .?.?.?.?.?.?. .?.?.?.?.?.?. From rosuav at gmail.com Sat Jan 21 11:18:03 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 22 Jan 2017 03:18:03 +1100 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: <20170121064546.0eba2e4c@bigbox.christie.dr> <588373cf$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Jan 22, 2017 at 2:56 AM, Jussi Piitulainen wrote: > Steve D'Aprano writes: > > [snip] > >> You could avoid that error by increasing the offset by the right >> amount: >> >> stuff = text[offset + len("?".encode('utf-8'):] >> >> which is awful. I believe that's what Go and Julia expect you to do. > > Julia provides a method to get the next index. > > let text = "??? ?????? ??????", offset = 1 > while offset <= endof(text) > print(text[offset], ".") > offset = nextind(text, offset) > end > println() > end # prints: ?.?.?. .?.?.?.?.?.?. .?.?.?.?.?.?. This implies that regular iteration isn't good enough, though. Here's a function that creates a numbered list: def print_list(items): width = len(str(len(items))) for idx, item in enumerate(items, 1): print("%*d: %s" % (width, idx, item)) In Python, this will happily accept anything that is iterable and has a known length. Could be a list or tuple, obviously, but can also just as easily be a dict view (keys or items), a range object, or.... a string. It's perfectly acceptable to enumerate the characters of a string. And enumerate() itself is implemented entirely generically. If you have to call nextind() to get the next character, you've made it impossible to do any kind of generic operation on the text. You can't do a windowed view by slicing while iterating, you can't have a "lag" or "lead" value, you can't do any of those kinds of simple and obvious index-based operations. Oh, and Python 3.3 wasn't the first programming language to use this flexible string representation. Pike introduced an extremely similar string representation back in 1998: https://github.com/pikelang/Pike/commit/db4a4 So yes, UTF-8 has its advantages. But it also has its costs, and for a text processing language like Pike or Python, they significantly outweigh the benefits. ChrisA From jussi.piitulainen at helsinki.fi Sat Jan 21 13:54:02 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Sat, 21 Jan 2017 20:54:02 +0200 Subject: PEP 393 vs UTF-8 Everywhere References: <20170121064546.0eba2e4c@bigbox.christie.dr> <588373cf$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: Chris Angelico writes: > On Sun, Jan 22, 2017 at 2:56 AM, Jussi Piitulainen wrote: >> Steve D'Aprano writes: >> >> [snip] >> >>> You could avoid that error by increasing the offset by the right >>> amount: >>> >>> stuff = text[offset + len("?".encode('utf-8'):] >>> >>> which is awful. I believe that's what Go and Julia expect you to do. >> >> Julia provides a method to get the next index. >> >> let text = "??? ?????? ??????", offset = 1 >> while offset <= endof(text) >> print(text[offset], ".") >> offset = nextind(text, offset) >> end >> println() >> end # prints: ?.?.?. .?.?.?.?.?.?. .?.?.?.?.?.?. > > This implies that regular iteration isn't good enough, though. It doesn't. Here's the straightforward iteration over the whole string: let text = "??? ?????? ??????" for c in text print(c, ".") end println() end # prints: ?.?.?. .?.?.?.?.?.?. .?.?.?.?.?.?. One can also join any iterable whose elements can be converted to strings, and characters can: let text = "??? ?????? ??????" println(join(text, "."), ".") end # prints: ?.?.?. .?.?.?.?.?.?. .?.?.?.?.?.?. And strings, trivially, can: let text = "??? ?????? ??????" println(join(split(text), "."), ".") end # prints: ???.??????.??????. > Here's a function that creates a numbered list: > > def print_list(items): > width = len(str(len(items))) > for idx, item in enumerate(items, 1): > print("%*d: %s" % (width, idx, item)) > > In Python, this will happily accept anything that is iterable and has > a known length. Could be a list or tuple, obviously, but can also just > as easily be a dict view (keys or items), a range object, or.... a > string. It's perfectly acceptable to enumerate the characters of a > string. And enumerate() itself is implemented entirely generically. I'll skip the formatting - I don't know off-hand how to do it - but keep the width calculation, and I cut the character iterator short at 10 items to save some space. There, it's much the same in Julia: let text = "??? ?????? ??????" function print_list(items) width = endof(string(length(items))) println("width = ", width) for (idx, item) in enumerate(items) println(idx, '\t', item) end end print_list(take(text, 10)) print_list([text, text, text]) print_list(split(text)) end That prints this: width = 2 1 ? 2 ? 3 ? 4 5 ? 6 ? 7 ? 8 ? 9 ? 10 ? width = 1 1 ??? ?????? ?????? 2 ??? ?????? ?????? 3 ??? ?????? ?????? width = 1 1 ??? 2 ?????? 3 ?????? > If you have to call nextind() to get the next character, you've made > it impossible to do any kind of generic operation on the text. You > can't do a windowed view by slicing while iterating, you can't have a > "lag" or "lead" value, you can't do any of those kinds of simple and > obvious index-based operations. Yet Julia does with ease many things that you seem to think it cannot possibly do at all. The iteration system works on types that have methods for certain generic functions. For strings, the default is to iterate over something like its characters; I think another iterator over valid indexes is available, or wouldn't be hard to write; it could be forward or backward, and in Julia many of these things are often peekable by default (because the iteration protocol itself does not have state - see below at "more magic"). The usual things work fine: let text = "??? ?????? ??????" foreach(print, enumerate(zip(text, split(text)))) end # prints: (1,('?',"???"))(2,('?',"??????"))(3,('?',"??????")) How is that bad? More magic: let text = "??? ?????? ??????" let ever = cycle(split(text)) println(first(ever)) println(first(ever)) for n in 2:6 println(join(take(ever, n), " ")) end end end This prints the following. The cycle iterator, ever, produces an endless repetition of the three words, but it doesn't have state like Python iterators do, so it's possible to look at the first word twice (and then five more times). ??? ??? ??? ?????? ??? ?????? ?????? ??? ?????? ?????? ??? ??? ?????? ?????? ??? ?????? ??? ?????? ?????? ??? ?????? ?????? > Oh, and Python 3.3 wasn't the first programming language to use this > flexible string representation. Pike introduced an extremely similar > string representation back in 1998: > > https://github.com/pikelang/Pike/commit/db4a4 Ok. Is GitHub that old? > So yes, UTF-8 has its advantages. But it also has its costs, and for a > text processing language like Pike or Python, they significantly > outweigh the benefits. I process text in my work but I really don't use character indexes much at all. Rather split, join, startswith, endswith, that kind of thing, and whether a string contains some character or substring anywhere. From marko at pacujo.net Sat Jan 21 14:52:42 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sat, 21 Jan 2017 21:52:42 +0200 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87o9z0fawl.fsf@elektro.pacujo.net> Pete Forman : > Surrogates only exist in UTF-16. They are expressly forbidden in UTF-8 > and UTF-32. Also, they don't exist as Unicode code points. Python shouldn't allow surrogate characters in strings. Thus the range of code points that are available for use as characters is U+0000?U+D7FF and U+E000?U+10FFFF (1,112,064 code points). The Unicode Character Database is basically a table of characters indexed using integers called ?code points?. Valid code points are in the ranges 0 to #xD7FF inclusive or #xE000 to #x10FFFF inclusive, which is about 1.1 million code points. Guile does the right thing: scheme@(guile-user)> #\xd7ff $1 = #\153777 scheme@(guile-user)> #\xe000 $2 = #\160000 scheme@(guile-user)> #\xd812 While reading expression: ERROR: In procedure scm_lreadr: #:5:8: out-of-range hex c haracter escape: xd812 > py> low = '\uDC37' That should raise a SyntaxError exception. Marko From python.list at tim.thechases.com Sat Jan 21 14:58:46 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 21 Jan 2017 13:58:46 -0600 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: <588373cf$0$1602$c3e8da3$5496439d@news.astraweb.com> References: <20170121064546.0eba2e4c@bigbox.christie.dr> <588373cf$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20170121135846.7b54297b@bigbox.christie.dr> On 2017-01-22 01:44, Steve D'Aprano wrote: > On Sat, 21 Jan 2017 11:45 pm, Tim Chase wrote: > > > but I'm hard-pressed to come up with any use case where direct > > indexing into a (non-byte)string makes sense unless you've already > > processed/searched up to that point and can use a recorded index > > from that processing/search. > > > Let's take a simple example: you do a find to get an offset, and > then slice from that offset. > > py> text = "??????xx" > py> offset = text.find("?") Right, so here, you've done a (likely linear, but however you get here) search, which then makes sense to use this opaque "offset" token for slicing purposes: > py> stuff = text[offset:] > py> assert stuff == "?xx" > That works fine whether indexing refers to code points or bytes. > > py> "??????xx".find("?") > 5 > py> "??????xx".encode('utf-8').find("?".encode('utf-8')) > 10 > > Either way, you get the expected result. However: > > py> stuff = text[offset + 1:] > py> assert stuff == "xx" > > That requires indexes to point to the beginning of *code points*, > not bytes: taking byte 11 of "??????xx".encode('utf-8') drops you > into the middle of the ? representation: > > py> "??????xx".encode('utf-8')[11:] > b'\x84xx' > > and it isn't a valid UTF-8 substring. Slicing would generate an > exception unless you happened to slice right at the start of a code > point. Right. It gets even weirder (edge-case'ier) when dealing with combining characters: >>> s = "man\N{COMBINING TILDE}ana" >>> for i, c in enumerate(s): print("%i: %s" % (i, c)) ... 0: m 1: a 2: n 3:? 4: a 5: n 6: a >>> ''.join(reversed(s)) 'an?nam' Offsetting s[3:] produces a (sub)string that begins with a combining character that doesn't have anything preceding it to combine with. > It's like seek() and tell() on text files: you cannot seek to > arbitrary positions, but only to the opaque positions returned by > tell. That's unacceptable for strings. I'm still unclear on *why* this would be considered unacceptable for strings. It makes sense when dealing with byte-strings, since they contain binary data that may need to get sliced at arbitrary offsets. But for strings, slicing only makes sense (for every use-case I've been able to come up with) in the context of known offsets like you describe with tell(). The cost of not using opaque tell()like offsets is, as you describe, slicing in the middle of characters. > You could avoid that error by increasing the offset by the right > amount: > > stuff = text[offset + len("?".encode('utf-8'):] > > which is awful. I believe that's what Go and Julia expect you to do. It may be awful, but only because it hasn't been pythonified. If the result from calling .find() on a string returns a "StringOffset" object, then it would make sense that its __add__/__radd__ methods would accept an integer and to such translation for you. > You can avoid this by having the interpreter treat the Python-level > indexes as opaque "code point offsets", and converting them to and > from "byte offsets" as needed. That's not even very hard. But it > either turns every indexing into O(N) (since you have to walk the > string to count which byte represents the nth code point) The O(N) cost has to be paid at some point, but I'd put forth that other operations like .find() already pay that O(N) cost and can return an opaque "offset token" that can be subsequently used for O(1) indexing (multiple times if needed). -tkc From petef4+usenet at gmail.com Sat Jan 21 15:21:21 2017 From: petef4+usenet at gmail.com (Pete Forman) Date: Sat, 21 Jan 2017 20:21:21 +0000 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> Message-ID: Marko Rauhamaa writes: >> py> low = '\uDC37' > > That should raise a SyntaxError exception. Quite. My point was that with older Python on a narrow build (Windows and Mac) you need to understand that you are using UTF-16 rather than Unicode. On a wide build or Python 3.3+ then all is rosy. (At this point I'm tempted to put in a winky emoji but that might push the internal representation into UCS-4.) -- Pete Forman From mruffalo at cs.cmu.edu Sat Jan 21 15:23:26 2017 From: mruffalo at cs.cmu.edu (Matt Ruffalo) Date: Sat, 21 Jan 2017 15:23:26 -0500 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> Message-ID: <04a3ca3b-28f5-d2bd-a2ce-fcd1236b3174@cs.cmu.edu> On 2017-01-21 10:50, Pete Forman wrote: > Thanks for a very thorough reply, most useful. I'm going to pick you up > on the above, though. > > Surrogates only exist in UTF-16. They are expressly forbidden in UTF-8 > and UTF-32. The rules for UTF-8 were tightened up in Unicode 4 and RFC > 3629 (2003). There is CESU-8 if you really need a naive encoding of > UTF-16 to UTF-8-alike. > > py> low = '\uDC37' > > is only meaningful on narrow builds pre Python 3.3 where the user must > do extra to correctly handle characters outside the BMP. Hi Pete- Lone surrogate characters have a standardized use in Python, not just in narrow builds of Python <= 3.2. Unpaired high surrogate characters are used to store any bytes that couldn't be decoded with a given character encoding scheme, for use in OS/filesystem interfaces that use arbitrary byte strings: """ Python 3.6.0 (default, Dec 23 2016, 08:25:24) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> s = 'h?llo' >>> b = s.encode('latin-1') >>> b b'h\xe9llo' >>> from os import fsdecode, fsencode >>> decoded = fsdecode(b) >>> decoded 'h\udce9llo' >>> fsencode(decoded) b'h\xe9llo' """ This provides a mechanism for lossless round-trip decoding and encoding of arbitrary byte strings which aren't valid under the user's locale. This is absolutely necessary in POSIX systems in which filenames can contain any sequence of bytes despite the user's locale, and is even necessary in Windows, where filenames are stored as opaque not-quite-UCS2 strings: """ Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> from pathlib import Path >>> import os >>> os.chdir(Path('~/Desktop').expanduser()) >>> filename = '\udcf9' >>> with open(filename, 'w'): pass >>> os.listdir('.') ['desktop.ini', '\udcf9'] """ MMR... From martin.schoon at gmail.com Sat Jan 21 15:42:29 2017 From: martin.schoon at gmail.com (Martin =?UTF-8?Q?Sch=C3=B6=C3=B6n?=) Date: 21 Jan 2017 20:42:29 GMT Subject: Adding colormaps? References: Message-ID: Den 2017-01-21 skrev Gilmeh Serda : > On Wed, 18 Jan 2017 21:41:34 +0000, Martin Sch??n wrote: > >> What I would like to do is to add the perceptually uniform sequential >> colormaps introduced in version 1.5.something. I would like to do this >> without breaking my Debian system in which Matplotlib version 1.4.2 is >> the newest version available in the repo. > > Haven't checked, but I assume you can get the source. Compile it but > don't install it and then use the result in virtualenv, maybe? > I am hoping for directions to a config file to download and place somewhere... /Martin From eryksun at gmail.com Sat Jan 21 15:49:26 2017 From: eryksun at gmail.com (eryk sun) Date: Sat, 21 Jan 2017 20:49:26 +0000 Subject: PEP 393 vs UTF-8 Everywhere In-Reply-To: References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> Message-ID: On Sat, Jan 21, 2017 at 8:21 PM, Pete Forman wrote: > Marko Rauhamaa writes: > >>> py> low = '\uDC37' >> >> That should raise a SyntaxError exception. > > Quite. My point was that with older Python on a narrow build (Windows > and Mac) you need to understand that you are using UTF-16 rather than > Unicode. On a wide build or Python 3.3+ then all is rosy. (At this point > I'm tempted to put in a winky emoji but that might push the internal > representation into UCS-4.) CPython allows surrogate codes for use with the "surrogateescape" and "surrogatepass" error handlers, which are used for POSIX and Windows file-system encoding, respectively. Maybe MicroPython goes about the file-system round-trip problem differently, or maybe it just require using bytes for file-system and environment-variable names on POSIX and doesn't care about Windows. "surrogateescape" allows 'decoding' arbitrary bytes: >>> b'\x81'.decode('ascii', 'surrogateescape') '\udc81' >>> '\udc81'.encode('ascii', 'surrogateescape') b'\x81' This error handler is required by CPython on POSIX to handle arbitrary bytes in file-system paths. For example, when running with LANG=C: >>> sys.getfilesystemencoding() 'ascii' >>> os.listdir(b'.') [b'\x81'] >>> os.listdir('.') ['\udc81'] "surrogatepass" allows encoding surrogates: >>> '\udc81'.encode('utf-8', 'surrogatepass') b'\xed\xb2\x81' >>> b'\xed\xb2\x81'.decode('utf-8', 'surrogatepass') '\udc81' This error handler is used by CPython 3.6+ to encode Windows UCS-2 file-system paths as WTF-8 (Wobbly). For example: >>> os.listdir('.') ['\udc81'] >>> os.listdir(b'.') [b'\xed\xb2\x81'] From grant.b.edwards at gmail.com Sat Jan 21 17:28:42 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sat, 21 Jan 2017 22:28:42 +0000 (UTC) Subject: How to create a socket.socket() object from a socket fd? Message-ID: Given a Unix file discriptor for an open TCP socket, I can't figure out how to create a python 2.7 socket object like those returned by socket.socket() Based on the docs, one might think that socket.fromfd() would do that (since the docs say that's what it does): Quoting https://docs.python.org/2/library/socket.html the socket() function returns a socket object [...] socket.socket([family[, type[, proto]]]) Create a new socket using the given address family[...] [...] socket.fromfd(fd, family, type[, proto]) Duplicate the file descriptor fd (an integer as returned by a file object?s fileno() method) and build a socket object from the result. But it doesn't work as described -- the docs apprently use the term "socket object" to refer to two different things. IOW, a "socket object" is a different type than a "socket object". Here's what a "socket object" returned by socket.socket() looks like: repr(osock): dir(osock): ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_sock', 'accept', 'bind', 'close', 'connect', 'connect_ex', 'dup', 'family', 'fileno', 'getpeername', 'getsockname', 'getsockopt', 'gettimeout', 'listen', 'makefile', 'proto', 'recv', 'recv_into', 'recvfrom', 'recvfrom_into', 'send', 'sendall', 'sendto', 'setblocking', 'setsockopt', 'settimeout', 'shutdown', 'type'] [If asked, I would call that a "socket._socketobject object".] Here's what a "socket object" returned from socket.fromfd() object looks like: repr(sock): dir(sock): ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'accept', 'bind', 'close', 'connect', 'connect_ex', 'dup', 'family', 'fileno', 'getpeername', 'getsockname', 'getsockopt', 'gettimeout', 'listen', 'makefile', 'proto', 'recv', 'recv_into', 'recvfrom', 'recvfrom_into', 'send', 'sendall', 'sendto', 'setblocking', 'setsockopt', 'settimeout', 'shutdown', 'timeout', 'type'] They're different types. [That one I sould all a "socket object".] Note that the socket.socket() object has a '_sock' attribute, which appears to contain an object like that returned by socket.fromfd(): repr(osock._sock): That prompts a question: given a "socket object" as returned by socket.fromfd(), how does one create a "socket._socketobject object" as returned by socket.socket()? It also makes one wonder why doesn't socket.fromfd() return the same type of object as socke.socket()? In what context would you want the type of object returned from socket.fromrd() instead of that returned by socket.socket()? [An ssl context will wrap the latter, but not the former, in case you're wondering why I care about the difference.] Also, I passed socket.fromfd fd==5, and the resulting "socket object" is using fd==6. Why does socket.fromfd() duplicate the fd? If I wanted the file descriptor duplicated, I would have do it myself! [Yes, I know the documentation _says_ it will duplicate the fd, I just don't understand why, and I think it a bad idea.] -- Grant From rosuav at gmail.com Sat Jan 21 17:39:41 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 22 Jan 2017 09:39:41 +1100 Subject: How to create a socket.socket() object from a socket fd? In-Reply-To: References: Message-ID: On Sun, Jan 22, 2017 at 9:28 AM, Grant Edwards wrote: > Given a Unix file discriptor for an open TCP socket, I can't figure > out how to create a python 2.7 socket object like those returned by > socket.socket() I suspect you can't easily do it. In more recent Pythons, you can socket.socket(fileno=N), but that doesn't exist in 2.7. But maybe..... > Here's what a "socket object" returned by socket.socket() looks like: > > repr(osock): > > > Here's what a "socket object" returned from socket.fromfd() object looks like: > > repr(sock): > > > Note that the socket.socket() object has a '_sock' attribute, which > appears to contain an object like that returned by socket.fromfd(): > > repr(osock._sock): > ... maybe you could construct a socket.socket(), then monkeypatch its _sock?? > [An ssl context will wrap the latter, but not the former, in case > you're wondering why I care about the difference.] > > Also, I passed socket.fromfd fd==5, and the resulting "socket object" > is using fd==6. Why does socket.fromfd() duplicate the fd? If I > wanted the file descriptor duplicated, I would have do it myself! > [Yes, I know the documentation _says_ it will duplicate the fd, I just > don't understand why, and I think it a bad idea.] Agreed. It's much cleaner in Py3, but I don't know of a backport. ChrisA From grant.b.edwards at gmail.com Sat Jan 21 17:41:31 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sat, 21 Jan 2017 22:41:31 +0000 (UTC) Subject: How to create a socket.socket() object from a socket fd? References: Message-ID: On 2017-01-21, Grant Edwards wrote: > Given a Unix file discriptor for an open TCP socket, I can't figure > out how to create a python 2.7 socket object like those returned by > socket.socket() > > Based on the docs, one might think that socket.fromfd() would do that > (since the docs say that's what it does): [...] > That prompts a question: given a "socket object" as returned by > socket.fromfd(), how does one create a "socket._socketobject object" > as returned by socket.socket()? Of course I figured it out immediately after spending 15 minutes whinging about it. help(socket.socket) gives you a hint: class _socketobject(__builtin__.object) | socket([family[, type[, proto]]]) -> socket object | [...] | | Methods defined here: | | __init__(self, family=2, type=1, proto=0, _sock=None) | Ah! There's a keyword argument that doesn't appear in the docs, so let's try that... context = ssl.create_default_context() [...] sock = socket.fromfd(fd2,socket.AF_INET,socket.SOCK_STREAM) nsock = socket.socket(socket.AF_INET,socket.SOCK_STREAM,_sock=sock) conn = context.wrap_socket(nsock, server_hostname="whatever.invalid") That works. -- Grant From rosuav at gmail.com Sat Jan 21 17:52:13 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 22 Jan 2017 09:52:13 +1100 Subject: How to create a socket.socket() object from a socket fd? In-Reply-To: References: Message-ID: On Sun, Jan 22, 2017 at 9:41 AM, Grant Edwards wrote: > | __init__(self, family=2, type=1, proto=0, _sock=None) > | > > Ah! There's a keyword argument that doesn't appear in the docs, so > let's try that... That's marginally better than my monkeypatch-after-creation suggestion, but still broadly the same. Your code may well break in other Python implementations, but within CPython 2.7, you should be pretty safe. ChrisA From christian at python.org Sat Jan 21 18:28:48 2017 From: christian at python.org (Christian Heimes) Date: Sun, 22 Jan 2017 00:28:48 +0100 Subject: How to create a socket.socket() object from a socket fd? In-Reply-To: References: Message-ID: On 2017-01-21 23:41, Grant Edwards wrote: > On 2017-01-21, Grant Edwards wrote: > >> Given a Unix file discriptor for an open TCP socket, I can't figure >> out how to create a python 2.7 socket object like those returned by >> socket.socket() >> >> Based on the docs, one might think that socket.fromfd() would do that >> (since the docs say that's what it does): > [...] >> That prompts a question: given a "socket object" as returned by >> socket.fromfd(), how does one create a "socket._socketobject object" >> as returned by socket.socket()? > > Of course I figured it out immediately after spending 15 minutes > whinging about it. > > help(socket.socket) gives you a hint: > > class _socketobject(__builtin__.object) > | socket([family[, type[, proto]]]) -> socket object > | > [...] > | > | Methods defined here: > | > | __init__(self, family=2, type=1, proto=0, _sock=None) > | > > Ah! There's a keyword argument that doesn't appear in the docs, so > let's try that... > > context = ssl.create_default_context() > [...] > sock = socket.fromfd(fd2,socket.AF_INET,socket.SOCK_STREAM) > nsock = socket.socket(socket.AF_INET,socket.SOCK_STREAM,_sock=sock) > conn = context.wrap_socket(nsock, server_hostname="whatever.invalid") > > That works. You might be interested in my small module https://pypi.python.org/pypi/socketfromfd/ . I just releases a new version with a fix for Python 2. Thanks for the hint! :) The module correctly detects address family, socket type and proto from a fd. It works correctly with e.g. IPv6 or Unix sockets. Ticket https://bugs.python.org/issue28134 has additional background information on the matter. Christian From __peter__ at web.de Sat Jan 21 18:31:54 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 22 Jan 2017 00:31:54 +0100 Subject: How to create a socket.socket() object from a socket fd? References: Message-ID: Grant Edwards wrote: > Given a Unix file discriptor for an open TCP socket, I can't figure > out how to create a python 2.7 socket object like those returned by > socket.socket() > > Based on the docs, one might think that socket.fromfd() would do that > (since the docs say that's what it does): [...] > Note that the socket.socket() object has a '_sock' attribute, which > appears to contain an object like that returned by socket.fromfd(): > > repr(osock._sock): > > > That prompts a question: given a "socket object" as returned by > socket.fromfd(), how does one create a "socket._socketobject object" > as returned by socket.socket()? The socket.py source reveals that you can do it like this: mysocket = socket.socket(_sock=socket.fromfd(...)) The code has from _socket import socket # actually a star import ... _realsocket = socket ... socket = _socketobject which looks very much like an ad-hoc hack gone permanent. From grant.b.edwards at gmail.com Sat Jan 21 18:35:41 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sat, 21 Jan 2017 23:35:41 +0000 (UTC) Subject: How to create a socket.socket() object from a socket fd? References: Message-ID: On 2017-01-21, Chris Angelico wrote: > On Sun, Jan 22, 2017 at 9:41 AM, Grant Edwards > wrote: >> | __init__(self, family=2, type=1, proto=0, _sock=None) >> | >> >> Ah! There's a keyword argument that doesn't appear in the docs, so >> let's try that... > > That's marginally better than my monkeypatch-after-creation > suggestion, but still broadly the same. Your code may well break in > other Python implementations, but within CPython 2.7, you should be > pretty safe. For those of you still paying attention... It looks like CPython 3.4 socket.fromfd() does return the same type object as socket.socket(). And, using the fileno= argument to socket.socket() avoids duplicating the descriptor. Looks like somebody with a time machine read my original post... -- Grant From grant.b.edwards at gmail.com Sat Jan 21 19:03:37 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 22 Jan 2017 00:03:37 +0000 (UTC) Subject: How to create a socket.socket() object from a socket fd? References: Message-ID: On 2017-01-21, Christian Heimes wrote: > You might be interested in my small module > https://pypi.python.org/pypi/socketfromfd/ . I just releases a new > version with a fix for Python 2. Thanks for the hint! :) > > The module correctly detects address family, socket type and proto from > a fd. It works correctly with e.g. IPv6 or Unix sockets. Ticket > https://bugs.python.org/issue28134 has additional background information > on the matter. Yes, thanks! Just a few minutes ago I stumbled across that issue. For Python3, I was using: sock = socket.socket(fileno=fd) But as you point out in that issue, the Python3 docs are wrong: when using socket.socket(fileno=fd) you _do_ have to specify the correct family and type parameters that correspond to the socket file descriptor. So, I starting looking for os.getsockopt(), which doesn't exist. I see you use ctypes to call gestsockopt (that was going to be my next step). I suspect the code I'm working will end up being re-written in C for the real product (so that it can run in-process in a thread rather than as an external helper process). If not, I'll have to use your module (or something like it) so that the solution will work on both IPv4 and IPv6 TCP sockets (I'd also like it to work with Unix domain sockets, but the piece at the other end of the socket connection currently only supports TCP). -- Grant From grant.b.edwards at gmail.com Sat Jan 21 19:52:46 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 22 Jan 2017 00:52:46 +0000 (UTC) Subject: How to create a socket.socket() object from a socket fd? References: Message-ID: Newsgroups: gmane.comp.python.general From: Grant Edwards Subject: Re: How to create a socket.socket() object from a socket fd? References: Followup-To: I'm still baffled why the standard library fromfd() code dup()s the descriptor. According to the comment in the CPython sources, the author of fromfd() is guessing that the user wants to be able to close the descriptor separately from the socket. If the user wanted the socket object to use a duplicate descriptor for some reason, the caller should call os.dup() -- it's only _eight_ keystrokes. Eight keystrokes that makes it obvious to anybody reading the code that there are now two descriptors and you have to close both the original descriptor and the socket. When you create a Python file object from a file descriptor using os.fdopen(), does it dup the descriptor? No. Would a reasonable person expect socket.fromfd() to duplicate the descriptor? No. Should it? No. I know... that particular mistake is set in stone now, and it's not going to change. But I feel better. :) $ python Python 2.7.12 (default, Dec 6 2016, 23:41:51) [GCC 4.9.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. **** Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. **** Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. **** In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! From steve+python at pearwood.info Sat Jan 21 21:36:47 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 22 Jan 2017 13:36:47 +1100 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> Message-ID: <58841ac0$0$1609$c3e8da3$5496439d@news.astraweb.com> On Sun, 22 Jan 2017 06:52 am, Marko Rauhamaa wrote: > Pete Forman : > >> Surrogates only exist in UTF-16. They are expressly forbidden in UTF-8 >> and UTF-32. > > Also, they don't exist as Unicode code points. Python shouldn't allow > surrogate characters in strings. Not quite. This is where it gets a bit messy and confusing. The bottom line is: surrogates *are* code points, but they aren't *characters*. Strings which contain surrogates are strictly speaking illegal, although some programming languages (including Python) allow them. The Unicode standard defines surrogates as follows: http://www.unicode.org/glossary/ - Surrogate Character. A misnomer. It would be an encoded character having a surrogate code point, which is impossible. Do not use this term. - Surrogate Code Point. A Unicode code point in the range U+D800..U+DFFF. Reserved for use by UTF-16, where a pair of surrogate code units (a high surrogate followed by a low surrogate) ?stand in? for a supplementary code point. - Surrogate Pair. A representation for a single abstract character that consists of a sequence of two 16-bit code units, where the first value of the pair is a high-surrogate code unit, and the second is a low-surrogate code unit. (See definition D75 in Section 3.8, Surrogates.) http://www.unicode.org/versions/Unicode9.0.0/ch03.pdf#G2630 So far so good, this is clear: surrogates are not characters. Surrogate pairs are only used by UTF-16 (since that's the only UTF which uses 16-bit code units). Suppose you read two code units (four bytes) in UTF-16 (big endian): b'\xd8\x02\xdd\x00' That could be ambiguous, as it could mean: - a single code point, U+10900 PHOENICIAN LETTER ALF, encoded as the surrogate pair, U+D802 U+DD00; - two surrogate code points, U+D802 followed by U+DD00. UTF-16 definitely rejects the second alternative and categorically makes only the first valid. To ensure that is the only valid interpretation, the second is explicitly disallowed: conforming Unicode strings are not allowed to include surrogate code points, regardless of whether you are intending to encode them in UTF-32 (where there is no ambiguity) or UTF-16 (where there is). Only UTF-16 encoded bytes are allowed to include surrogate code *units*, and only in pairs. However, Python (and other languages?) take a less strict approach. In Python 3.3 and better, the code point U+10900 (Phoenician Alf) is encoded using a single four-byte code unit: 0x00010900', so there's no ambiguity. That allows Python to encode surrogates as double-byte code units with no ambiguity: the interpreter can distinguish a single 32 byte number 0x00010900 from a pair of 16 bit numbers 0x0001 0x0900 and treat the first as Alf and the second as two surrogates. By the letter of the Unicode standard, it should not do this, but nevertheless it does and it appears to do no real harm and have some benefit. The glossary also says: - High-Surrogate Code Point. A Unicode code point in the range U+D800 to U+DBFF. (See definition D71 in Section 3.8, Surrogates.) - High-Surrogate Code Unit. A 16-bit code unit in the range D800_16 to DBFF_16, used in UTF-16 as the leading code unit of a surrogate pair. Also known as a leading surrogate. (See definition D72 in Section 3.8, Surrogates.) - Low-Surrogate Code Point. A Unicode code point in the range U+DC00 to U+DFFF. (See definition D73 in Section 3.8, Surrogates.) - Low-Surrogate Code Unit. A 16-bit code unit in the range DC00_16 to DFFF_16, used in UTF-16 as the trailing code unit of a surrogate pair. Also known as a trailing surrogate. (See definition D74 in Section 3.8, Surrogates.) So we can certainly talk about surrogates being code points: the code points U+D800 through U+DFFF inclusive are surrogate code points, but not characters. They're not "non-characters" either. Unicode includes exactly 66 code points formally defined as "non-characters": - Noncharacter. A code point that is permanently reserved for internal use. Noncharacters consist of the values U+nFFFE and U+nFFFF (where n is from 0 to 1016), and the values U+FDD0..U+FDEF. See the FAQ on Private-Use Characters, Noncharacters and Sentinels. http://www.unicode.org/faq/private_use.html#noncharacters So even though noncharacters (with or without the hyphen) are code points reserved for internal use, and surrogates are code points reserved for the internal use of the UTF-16 encoder, and surrogates are not characters, surrogates are not noncharacters. Naming things is hard. > Thus the range of code points that are available for use as > characters is U+0000?U+D7FF and U+E000?U+10FFFF (1,112,064 code > points). > > That is correct for a strictly conforming Unicode implementation. >> py> low = '\uDC37' > > That should raise a SyntaxError exception. If Python was strictly conforming, that is correct, but it turns out there are some useful things you can do with strings if you allow surrogates. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Sat Jan 21 21:42:57 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 22 Jan 2017 13:42:57 +1100 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> Message-ID: <58841c32$0$1612$c3e8da3$5496439d@news.astraweb.com> On Sun, 22 Jan 2017 07:21 am, Pete Forman wrote: > Marko Rauhamaa writes: > >>> py> low = '\uDC37' >> >> That should raise a SyntaxError exception. > > Quite. My point was that with older Python on a narrow build (Windows > and Mac) you need to understand that you are using UTF-16 rather than > Unicode. But you're *not* using UTF-16, at least not proper UTF-16, in older narrow builds. If you were, then Unicode strings u'...' containing surrogate pairs would be treated as supplementary single code points, but they aren't. unichr() doesn't support supplementary code points in narrow builds: [steve at ando ~]$ python2.7 -c "print len(unichr(0x10900))" Traceback (most recent call last): File "", line 1, in ValueError: unichr() arg not in range(0x10000) (narrow Python build) and even if you sneak a supplementary code point in, it is treated wrongly: [steve at ando ~]$ python2.7 -c "print len(u'\U00010900')" 2 So Python narrow builds are more like a bastard hybrid of UCS-2 and UTF-16. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+comp.lang.python at pearwood.info Sun Jan 22 01:47:35 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Sun, 22 Jan 2017 17:47:35 +1100 Subject: PEP 393 vs UTF-8 Everywhere References: <20170121064546.0eba2e4c@bigbox.christie.dr> <588373cf$0$1602$c3e8da3$5496439d@news.astraweb.com> <20170121135846.7b54297b@bigbox.christie.dr> Message-ID: <58845588$0$2736$c3e8da3$76491128@news.astraweb.com> On Sunday 22 January 2017 06:58, Tim Chase wrote: > Right. It gets even weirder (edge-case'ier) when dealing with > combining characters: > > >>>> s = "man\N{COMBINING TILDE}ana" >>>> for i, c in enumerate(s): print("%i: %s" % (i, c)) > ... > 0: m > 1: a > 2: n > 3:? > 4: a > 5: n > 6: a >>>> ''.join(reversed(s)) > 'an?nam' > > Offsetting s[3:] produces a (sub)string that begins with a combining > character that doesn't have anything preceding it to combine with. That doesn't matter. Unicode is a universal character set, not a universal *grapheme* set. But even speaking about characters is misleading: Unicode's "characters" (note the scare quotes) are abstract code points which can represent at least: - letters of alphabets - digits - punctuation marks - ideographs - line drawing symbols - emoji - noncharacters Since it doesn't promise to only provide graphemes (I can write "$\N{COMBINING TILDE}" which is not a valid grapheme in any human language) it doesn't matter if you end up with lone combining characters. Or rather, it does matter, but fixing that is not Unicode's responsibility. That should become a layer built on top of Unicode. >> It's like seek() and tell() on text files: you cannot seek to >> arbitrary positions, but only to the opaque positions returned by >> tell. That's unacceptable for strings. > > I'm still unclear on *why* this would be considered unacceptable for > strings. Sometimes you want to slice at a particular index which is *not* an opaque position returned by find(). text[offset + 1:] Of for that matter: middle_character = text[len(text)//2] Forbidding those sorts of operations are simply too big a break with previous versions. > It makes sense when dealing with byte-strings, since they > contain binary data that may need to get sliced at arbitrary > offsets. But for strings, slicing only makes sense (for every > use-case I've been able to come up with) in the context of known > offsets like you describe with tell(). I'm sorry, I find it hard to believe that you've never needed to add or subtract 1 from a given offset returned by find() or equivalent. > The cost of not using opaque > tell()like offsets is, as you describe, slicing in the middle of > characters. >> You could avoid that error by increasing the offset by the right >> amount: >> >> stuff = text[offset + len("?".encode('utf-8'):] >> >> which is awful. I believe that's what Go and Julia expect you to do. > > It may be awful, but only because it hasn't been pythonified. No, it's awful no matter what. It makes it painful to reason about which code points will be picked up by a slice. What's the length of...? text[offset:offset+5] In current Python, that's got to be five code points (excluding the edge cases of slicing past the end of the string). But with opaque indexes, that could be anything from 1 to 5 code points. > If the > result from calling .find() on a string returns a "StringOffset" > object, then it would make sense that its __add__/__radd__ methods > would accept an integer and to such translation for you. At cost of predictability. >> You can avoid this by having the interpreter treat the Python-level >> indexes as opaque "code point offsets", and converting them to and >> from "byte offsets" as needed. That's not even very hard. But it >> either turns every indexing into O(N) (since you have to walk the >> string to count which byte represents the nth code point) > > The O(N) cost has to be paid at some point, but I'd put forth that > other operations like .find() already pay that O(N) cost and can > return an opaque "offset token" that can be subsequently used for O(1) > indexing (multiple times if needed). Sure -- but only at the cost of blowing out the complexity and memory requirements of the string, which completely negates the point in using UTF-8 in the first place. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From marko at pacujo.net Sun Jan 22 03:13:10 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sun, 22 Jan 2017 10:13:10 +0200 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> Message-ID: <87fukbtsvd.fsf@elektro.pacujo.net> eryk sun : > On Sat, Jan 21, 2017 at 8:21 PM, Pete Forman wrote: >> Marko Rauhamaa writes: >> >>>> py> low = '\uDC37' >>> >>> That should raise a SyntaxError exception. >> >> Quite. [...] > > CPython allows surrogate codes for use with the "surrogateescape" and > "surrogatepass" error handlers, which are used for POSIX and Windows > file-system encoding, respectively. Yes, but at the cost of violating Unicode, leading to unprintable strings etc. In my opinion, Python should have "stayed pure" instead of playing cheap tricks with surrogates. (Of course, Unicode itself is a mess, but that's another story.) Marko From marko at pacujo.net Sun Jan 22 03:34:07 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sun, 22 Jan 2017 10:34:07 +0200 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> <58841ac0$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87bmuztrwg.fsf@elektro.pacujo.net> Steve D'Aprano : > On Sun, 22 Jan 2017 06:52 am, Marko Rauhamaa wrote: >> Also, [surrogates] don't exist as Unicode code points. Python >> shouldn't allow surrogate characters in strings. > > Not quite. This is where it gets a bit messy and confusing. The bottom > line is: surrogates *are* code points, but they aren't *characters*. All animals are equal, but some animals are more equal than others. > Strings which contain surrogates are strictly speaking illegal, > although some programming languages (including Python) allow them. Python shouldn't allow them. > The Unicode standard defines surrogates as follows: > [...] > > - Surrogate Code Point. A Unicode code point in the range > U+D800..U+DFFF. Reserved for use by UTF-16, The writer of the standard is playing word games, maybe to offer a fig leaf to Windows, Java et al. > By the letter of the Unicode standard, [Python] should not do this, > but nevertheless it does and it appears to do no real harm and have > some benefit. I'm afraid Python's choice may lead to exploitable security holes in Python programs. >>> py> low = '\uDC37' >> >> That should raise a SyntaxError exception. > > If Python was strictly conforming, that is correct, but it turns out > there are some useful things you can do with strings if you allow > surrogates. Conceptual confusion is a high price to pay for such tricks. Marko From christian at python.org Sun Jan 22 06:48:31 2017 From: christian at python.org (Christian Heimes) Date: Sun, 22 Jan 2017 12:48:31 +0100 Subject: How to create a socket.socket() object from a socket fd? In-Reply-To: References: Message-ID: On 2017-01-22 01:03, Grant Edwards wrote: > On 2017-01-21, Christian Heimes wrote: > >> You might be interested in my small module >> https://pypi.python.org/pypi/socketfromfd/ . I just releases a new >> version with a fix for Python 2. Thanks for the hint! :) >> >> The module correctly detects address family, socket type and proto from >> a fd. It works correctly with e.g. IPv6 or Unix sockets. Ticket >> https://bugs.python.org/issue28134 has additional background information >> on the matter. > > Yes, thanks! > > Just a few minutes ago I stumbled across that issue. For Python3, I > was using: > > sock = socket.socket(fileno=fd) > > But as you point out in that issue, the Python3 docs are wrong: when > using socket.socket(fileno=fd) you _do_ have to specify the correct > family and type parameters that correspond to the socket file > descriptor. So, I starting looking for os.getsockopt(), which doesn't > exist. > > I see you use ctypes to call gestsockopt (that was going to be my next > step). > > I suspect the code I'm working will end up being re-written in C for > the real product (so that it can run in-process in a thread rather > than as an external helper process). If not, I'll have to use your > module (or something like it) so that the solution will work on both > IPv4 and IPv6 TCP sockets (I'd also like it to work with Unix domain > sockets, but the piece at the other end of the socket connection > currently only supports TCP). I wanted to fix the function before 3.6.0 came out but I faced some resistance. My approach was deemed too magic and not fully functional on some platforms. Other core devs are underestimating the severity of the issue. Even in simple examples with IPv4 and IPv6 it breaks getpeername(). I'd appreciate if you could jump in a leave a comment on the ticket to show your support. :) By the way I just pushed another commit with a new feature just for you. But see for yourself: https://github.com/tiran/socketfromfd/commit/0a2fd6dae86267cedea5ae8b956e2876e6057c74 Christian From steve+python at pearwood.info Sun Jan 22 09:01:32 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Mon, 23 Jan 2017 01:01:32 +1100 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> <58841ac0$0$1609$c3e8da3$5496439d@news.astraweb.com> <87bmuztrwg.fsf@elektro.pacujo.net> Message-ID: <5884bb3d$0$1619$c3e8da3$5496439d@news.astraweb.com> On Sun, 22 Jan 2017 07:34 pm, Marko Rauhamaa wrote: > Steve D'Aprano : > >> On Sun, 22 Jan 2017 06:52 am, Marko Rauhamaa wrote: >>> Also, [surrogates] don't exist as Unicode code points. Python >>> shouldn't allow surrogate characters in strings. >> >> Not quite. This is where it gets a bit messy and confusing. The bottom >> line is: surrogates *are* code points, but they aren't *characters*. > > All animals are equal, but some animals are more equal than others. Huh? >> Strings which contain surrogates are strictly speaking illegal, >> although some programming languages (including Python) allow them. > > Python shouldn't allow them. That's one opinion. >> The Unicode standard defines surrogates as follows: >> [...] >> >> - Surrogate Code Point. A Unicode code point in the range >> U+D800..U+DFFF. Reserved for use by UTF-16, > > The writer of the standard is playing word games, maybe to offer a fig > leaf to Windows, Java et al. Seriously? >> By the letter of the Unicode standard, [Python] should not do this, >> but nevertheless it does and it appears to do no real harm and have >> some benefit. > > I'm afraid Python's choice may lead to exploitable security holes in > Python programs. Feel free to back up that with an actual demonstration of an exploit, rather than just FUD. >>>> py> low = '\uDC37' >>> >>> That should raise a SyntaxError exception. >> >> If Python was strictly conforming, that is correct, but it turns out >> there are some useful things you can do with strings if you allow >> surrogates. > > Conceptual confusion is a high price to pay for such tricks. There's a lot to comprehend about Unicode. I don't see that Python's non-strict implementation is harder to understand than the strict version. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From marko at pacujo.net Sun Jan 22 10:19:34 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sun, 22 Jan 2017 17:19:34 +0200 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> <58841ac0$0$1609$c3e8da3$5496439d@news.astraweb.com> <87bmuztrwg.fsf@elektro.pacujo.net> <5884bb3d$0$1619$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87ziijqfzt.fsf@elektro.pacujo.net> Steve D'Aprano : > On Sun, 22 Jan 2017 07:34 pm, Marko Rauhamaa wrote: > >> Steve D'Aprano : >> >>> On Sun, 22 Jan 2017 06:52 am, Marko Rauhamaa wrote: >>>> Also, [surrogates] don't exist as Unicode code points. Python >>>> shouldn't allow surrogate characters in strings. >>> >>> Not quite. This is where it gets a bit messy and confusing. The >>> bottom line is: surrogates *are* code points, but they aren't >>> *characters*. >> >> All animals are equal, but some animals are more equal than others. > > Huh? There is no difference between 0xD800 and 0xD8000000. They are both numbers that don't--and won't--represent anything in Unicode. It's pointless to call one a "code point" and not the other one. A code point that isn't code for anything can barely be called a code point. I'm guessing 0xD800 is called a code point because it was always called that. It was dropped out when UTF-16 was invented but they didn't want to "demote" the number retroactively, especially since Windows and Java already were allowing them in strings. >>> By the letter of the Unicode standard, [Python] should not do this, >>> but nevertheless it does and it appears to do no real harm and have >>> some benefit. >> >> I'm afraid Python's choice may lead to exploitable security holes in >> Python programs. > > Feel free to back up that with an actual demonstration of an exploit, > rather than just FUD. It might come as a surprise to programmers that pathnames cannot be UTF-encoded or displayed. Also, those situations might not show up during testing but only with appropriately crafted input. Marko From grant.b.edwards at gmail.com Sun Jan 22 15:18:49 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 22 Jan 2017 20:18:49 +0000 (UTC) Subject: Is Python SSL API thread-safe? Message-ID: Is the Python SSL API thread-safe with respect to recv() and send()? IOW, can I have one thread doing blocking recv() calls on an SSL connection object while "simultaneously" a second thread is calling send() on that same connection object? I assumed that was allowed, but I can't find anything in the documentation that actually says it is. -- Grant From jon+usenet at unequivocal.eu Sun Jan 22 15:55:16 2017 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Sun, 22 Jan 2017 20:55:16 -0000 (UTC) Subject: Is Python SSL API thread-safe? References: Message-ID: On 2017-01-22, Grant Edwards wrote: > Is the Python SSL API thread-safe with respect to recv() and send()? > > IOW, can I have one thread doing blocking recv() calls on an SSL > connection object while "simultaneously" a second thread is calling > send() on that same connection object? I think this question is equivalent to asking "is OpenSSL thread-safe", the answer to which would appear to be "yes": https://www.openssl.org/docs/man1.0.2/crypto/threads.html (the necessary functions mentioned on that page, threadid_func and locking_function are indeed set by Python). From christian at python.org Sun Jan 22 16:34:38 2017 From: christian at python.org (Christian Heimes) Date: Sun, 22 Jan 2017 22:34:38 +0100 Subject: Is Python SSL API thread-safe? In-Reply-To: References: Message-ID: <4f923fe7-fa75-698e-37b9-3e4423c7e59b@python.org> On 2017-01-22 21:18, Grant Edwards wrote: > Is the Python SSL API thread-safe with respect to recv() and send()? > > IOW, can I have one thread doing blocking recv() calls on an SSL > connection object while "simultaneously" a second thread is calling > send() on that same connection object? > > I assumed that was allowed, but I can't find anything in the > documentation that actually says it is. OpenSSL and Python's ssl module are thread-safe. However IO is not safe concerning reentrancy. You cannot safely share a SSLSocket between threads without a mutex. Certain aspects of the TLS protocol can cause interesting side effects. A recv() call can send data across a wire and a send() call can receive data from the wire, e.g. during re-keying. In order to archive reentrancy, you have to do all IO yourself by operating the SSL connection in non-blocking mode or with a Memorio-BIO https://docs.python.org/3/library/ssl.html#ssl-nonblocking From jf_byrnes at comcast.net Sun Jan 22 19:10:18 2017 From: jf_byrnes at comcast.net (Jim) Date: Sun, 22 Jan 2017 18:10:18 -0600 Subject: pyuno in Libreoffice 5.1.4.2 Message-ID: Does anyone know if the changes outlined here [1] have been implemented? Supposedly changes have been made to pyuno to make it more pythonic. Item 2 Cellranges says that: cell = sheet.getCellByPosition(cellCol + col, cellRow + row) Can be written as: cell = sheet.cellrange[cellRow + row, cellCol + col] But when I try that I get: Traceback (most recent call last): File "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", line 68, in keyPressed move_selected_cell(1, 0) File "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", line 112, in move_selected_cell cell = sheet.cellrange[cellRow + row, cellCol + col] AttributeError: cellrange Or maybe I am misunderstanding how to use it. Regards, Jim [1] https://cgit.freedesktop.org/libreoffice/core/commit/?id=af8143bc40cf2cfbc12e77c9bb7de01b655f7b30 From grant.b.edwards at gmail.com Sun Jan 22 20:01:59 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Mon, 23 Jan 2017 01:01:59 +0000 (UTC) Subject: Is Python SSL API thread-safe? References: <4f923fe7-fa75-698e-37b9-3e4423c7e59b@python.org> Message-ID: On 2017-01-22, Christian Heimes wrote: > On 2017-01-22 21:18, Grant Edwards wrote: >> Is the Python SSL API thread-safe with respect to recv() and send()? >> >> IOW, can I have one thread doing blocking recv() calls on an SSL >> connection object while "simultaneously" a second thread is calling >> send() on that same connection object? >> >> I assumed that was allowed, but I can't find anything in the >> documentation that actually says it is. > > OpenSSL and Python's ssl module are thread-safe. However IO is not safe > concerning reentrancy. You cannot safely share a SSLSocket between > threads without a mutex. Certain aspects of the TLS protocol can cause > interesting side effects. A recv() call can send data across a wire and > a send() call can receive data from the wire, e.g. during re-keying. > > In order to archive reentrancy, you have to do all IO yourself by > operating the SSL connection in non-blocking mode or with a Memorio-BIO > https://docs.python.org/3/library/ssl.html#ssl-nonblocking IOW, what I'm doing is not safe. Rats. -- Grant From python at mrabarnett.plus.com Sun Jan 22 20:02:03 2017 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 23 Jan 2017 01:02:03 +0000 Subject: pyuno in Libreoffice 5.1.4.2 In-Reply-To: References: Message-ID: On 2017-01-23 00:10, Jim wrote: > Does anyone know if the changes outlined here [1] have been implemented? > > Supposedly changes have been made to pyuno to make it more pythonic. > > Item 2 Cellranges says that: > cell = sheet.getCellByPosition(cellCol + col, cellRow + row) > Can be written as: > cell = sheet.cellrange[cellRow + row, cellCol + col] > > But when I try that I get: > > Traceback (most recent call last): > File > "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", > line 68, in keyPressed > move_selected_cell(1, 0) > File > "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", > line 112, in move_selected_cell > cell = sheet.cellrange[cellRow + row, cellCol + col] > AttributeError: cellrange > > Or maybe I am misunderstanding how to use it. > I think it might be direct indexing of the sheet: cell = sheet[cellRow + row, cellCol + col] > Regards, Jim > > [1] > https://cgit.freedesktop.org/libreoffice/core/commit/?id=af8143bc40cf2cfbc12e77c9bb7de01b655f7b30 > > From jf_byrnes at comcast.net Sun Jan 22 20:30:08 2017 From: jf_byrnes at comcast.net (Jim) Date: Sun, 22 Jan 2017 19:30:08 -0600 Subject: pyuno in Libreoffice 5.1.4.2 In-Reply-To: References: Message-ID: On 01/22/2017 07:02 PM, MRAB wrote: > On 2017-01-23 00:10, Jim wrote: >> Does anyone know if the changes outlined here [1] have been implemented? >> >> Supposedly changes have been made to pyuno to make it more pythonic. >> >> Item 2 Cellranges says that: >> cell = sheet.getCellByPosition(cellCol + col, cellRow + row) >> Can be written as: >> cell = sheet.cellrange[cellRow + row, cellCol + col] >> >> But when I try that I get: >> >> Traceback (most recent call last): >> File >> "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", >> >> line 68, in keyPressed >> move_selected_cell(1, 0) >> File >> "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", >> >> line 112, in move_selected_cell >> cell = sheet.cellrange[cellRow + row, cellCol + col] >> AttributeError: cellrange >> >> Or maybe I am misunderstanding how to use it. >> > I think it might be direct indexing of the sheet: > > cell = sheet[cellRow + row, cellCol + col] > >> Regards, Jim >> >> [1] >> https://cgit.freedesktop.org/libreoffice/core/commit/?id=af8143bc40cf2cfbc12e77c9bb7de01b655f7b30 >> You are correct, that worked. Thank you very much. Regards, Jim From steve+python at pearwood.info Sun Jan 22 21:14:20 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Mon, 23 Jan 2017 13:14:20 +1100 Subject: PEP 393 vs UTF-8 Everywhere References: <58836875$0$1600$c3e8da3$5496439d@news.astraweb.com> <87o9z0fawl.fsf@elektro.pacujo.net> <58841ac0$0$1609$c3e8da3$5496439d@news.astraweb.com> <87bmuztrwg.fsf@elektro.pacujo.net> <5884bb3d$0$1619$c3e8da3$5496439d@news.astraweb.com> <87ziijqfzt.fsf@elektro.pacujo.net> Message-ID: <588566fd$0$1619$c3e8da3$5496439d@news.astraweb.com> On Mon, 23 Jan 2017 02:19 am, Marko Rauhamaa wrote: > Steve D'Aprano : > >> On Sun, 22 Jan 2017 07:34 pm, Marko Rauhamaa wrote: >> >>> Steve D'Aprano : >>> >>>> On Sun, 22 Jan 2017 06:52 am, Marko Rauhamaa wrote: >>>>> Also, [surrogates] don't exist as Unicode code points. Python >>>>> shouldn't allow surrogate characters in strings. >>>> >>>> Not quite. This is where it gets a bit messy and confusing. The >>>> bottom line is: surrogates *are* code points, but they aren't >>>> *characters*. >>> >>> All animals are equal, but some animals are more equal than others. >> >> Huh? > > There is no difference between 0xD800 and 0xD8000000. Arithmetic disagrees: py> 0xD800 == 0xD8000000 False > They are both > numbers that don't--and won't--represent anything in Unicode. Your use of hex notation 0x... indicates that you're talking about code units rather than U+... code points. The first one 0xD800 could be: - a Little Endian double-byte code unit for '?' in either UCS-2 or UTF-16; - a Big Endian double-byte code unit that has no special meaning in UCS-2; - one half of a surrogate pair (two double-byte code units) in Big Endian UTF-16, encoding some unknown supplementary code point. The second one 0xD8000000 could be: - a C long (four-byte int) 3623878656, which is out of range for Big Endian UCS-4 or UTF-32; - the Little Endian four-byte code unit for '?' in either UCS-4 or UTF-32. > It's pointless to call one a "code point" and not the other one. Neither of them are code points. You're confusing the concrete representation with the abstract character. Perhaps you meant to compare the code point U+D800 to, well, there's no comparison to be made, because "U+D8000000" is not valid and is completely out of range. The largest code point is U+10FFFF. > A code point > that isn't code for anything can barely be called a code point. It does have a purpose. Or even more than one. - It ensures that there is a one-to-one mapping between code points and code units in any specific encoding and byte-order. - By reserving those code points, it ensures that they cannot be accidentally used by the standard for something else. - It makes it easier to talk about the entities: "U+D800 is a surrogate code point reserved for UTF-16 surrogates", as opposed to "U+D800 isn't anything, but if it was something, it would be a code point reserved for UTF-16 surrogates". - Or worse, forcing us to talk in terms of code units (implementation) instead of abstract characters, which is painfully verbose: "0xD800 in Big Endian UTF-16, or 0x00D8 in Little Endian UTF-16, or 0x0000D800 in Big Endian UTF-32, or 0x00D80000 in Little Endian UTF-16, doesn't map to any code point but is reserved for UTF-16 surrogate pairs." And, an entirely unforeseen purpose: - It allows languages like Python to (ab)use surrogate code points for round-tripping file names which aren't valid Unicode. [...] >>> I'm afraid Python's choice may lead to exploitable security holes in >>> Python programs. >> >> Feel free to back up that with an actual demonstration of an exploit, >> rather than just FUD. > > It might come as a surprise to programmers that pathnames cannot be > UTF-encoded or displayed. Many things come as surprises to programmers, and many pathnames cannot be UTF-encoded. To be precise, Mac OS requires pathnames to be both valid and normalised UTF-8, and it would be nice if that practice spread. But Windows only requires pathnames to consist of UCS-2 code points, and Linux pathnames are arbitrary bytes that may include characters which are illegal on Windows. So you don't need to involve surrogates to have undecodable pathnames. > Also, those situations might not show up > during testing but only with appropriately crafted input. I'm not seeing a security exploit here. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From dieter at handshake.de Mon Jan 23 03:42:12 2017 From: dieter at handshake.de (dieter) Date: Mon, 23 Jan 2017 09:42:12 +0100 Subject: Problems with python3.6 on one system, but OK on another References: <87a8akzkwj.fsf@Equus.decebal.nl> Message-ID: <87inp6jhgb.fsf@handshake.de> Cecil Westerhof writes: > I build python3.6 on two systems. On one system everything is OK: > Python 3.6.0 (default, Jan 21 2017, 11:19:56) > [GCC 4.9.2] on linux > Type "help", "copyright", "credits" or "license" for more information. > > > But on another I get: > Could not find platform dependent libraries > Consider setting $PYTHONHOME to [:] > Python 3.6.0 (default, Jan 21 2017, 12:20:38) > [GCC 4.8.5] on linux > Type "help", "copyright", "credits" or "license" for more information. > > Probably not a big problem, but just wondering what is happening here. > On both systems PYTHONHOME is not set and with the old version (3.4.5) > I did/do not get this message. A Python installation consists of two parts: the Python binary (i.e. the Python interpreter) and the Python runtime library (everything not built into the interpreter itself). When you start Python, you effectively start the binary. This then tries to locate the parts forming its runtime library. Essentially, there are two parts: platform independent (i.e. Python modules/packages) and platform dependent (usually shared objects implemented in "C" or "C++"). The binary is using heuristics to locate those parts, involving the (usually internal) variables "prefix" and "exec_prefix" for the platform independent and dependent parts, respectively. On some platforms, the operating system allows a binary to determine the full path it has been started from. On those platforms, the above mentioned heuristics may use this path to locate the runtime library parts. On other platforms, default values for "prefix" and "exec_prefix" are determined during the Python generation. As a consequnece, something must have gone wrong with the generation or installation of your badly behaving Python. To start the investigation, I would look at the binaries' notion of "prefix" and "exec_prefix". You can find them via >>> import sys >>> sys.prefix, sys.exec_prefix On a "*nix" like platform, you should find the platform dependent parts of the runtime library unter "/lib/Python/lib-dynload". Check whether this directory exists and is not empty. From catalinfest at gmail.com Mon Jan 23 05:38:02 2017 From: catalinfest at gmail.com (blue) Date: Mon, 23 Jan 2017 02:38:02 -0800 (PST) Subject: Adding colormaps? In-Reply-To: References: Message-ID: you have here a full example , for another version not significant changes: http://matplotlib.org/examples/color/colormaps_reference.html From Cecil at decebal.nl Mon Jan 23 06:53:51 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Mon, 23 Jan 2017 12:53:51 +0100 Subject: Using python to start programs after logging in References: <878tq6amvj.fsf@Equus.decebal.nl> Message-ID: <874m0q6lgw.fsf@Equus.decebal.nl> On Thursday 19 Jan 2017 20:08 CET, Cecil Westerhof wrote: > I am writing a python program to start the programs that need to be > started after logging in. I published what I have until now at: https://github.com/CecilWesterhof/PythonScripts/blob/master/startPrograms.py I do not mind some feedback. ;-) Next step is to write a GUI program to maintain the database. That is new territory for me. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From mal at egenix.com Mon Jan 23 09:11:38 2017 From: mal at egenix.com (M.-A. Lemburg) Date: Mon, 23 Jan 2017 15:11:38 +0100 Subject: [Python-ideas] "Immutable Builder" Pattern and Operator In-Reply-To: References: <2127feaa-0a5b-8246-978f-6346dea554a2@gmail.com> <0654a27e-7200-c468-d4eb-17bef13b61d2@egenix.com> Message-ID: <5032a807-75d5-d58f-5332-97ec22b0340a@egenix.com> On 23.01.2017 14:28, Soni L. wrote: > > > On 23/01/17 11:18 AM, M.-A. Lemburg wrote: >> On 23.01.2017 14:05, Soni L. wrote: >>> Yeah but the dotequals operator has many other benefits: >>> >>> long_name .= __call__ # cast to callable >>> long_name .= wrapped # unwrap >>> etc >>> >>> And it also looks neat. >> I don't see this an being a particular intuitive way of writing >> such rather uncommon constructs. >> >> The syntax is not clear (what if you have an expression on the RHS) >> and it doesn't save you much in writing (if long_name is too long >> simply rebind it under a shorter name for the purpose of the code >> block). > > It's literally sugar for repeating the name and moving the dot to the > right. I think it's clearer than most other compound operators in that > it doesn't affect precedence rules. > > `x += y`, for any code `y`, is equivalent to `x = x + (y)`, not `x = x + > y`. > > `x .= y`, for any code `y`, is equivalent to `x = x . y`, not `x = x . > (y)`. Well, then please consider these: x .= y + z x .= y * z x .= y.z x .= y.z() >> Also note that rebinding different objects to the same name >> in the same block is often poor style and can easily lead to >> hard to track bugs. >> > > Rebinding different objects to the same name in rapid succession > is fine. Not in my book :-) It may be fine if the object type stays the same or in those few cases, where you want to accept multiple different types for a parameter and then coerce these to a type that you use in the rest of the code block. But even in those cases, debugging becomes easier if you keep the original binding in place (since you then know where the new values originated). This is not good style... x = 'abc' x = len(x) x = [x, 1] x = ''.join(str(a) for a in x) print (x) -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Jan 23 2017) >>> Python Projects, Coaching and Consulting ... http://www.egenix.com/ >>> Python Database Interfaces ... http://products.egenix.com/ >>> Plone/Zope Database Interfaces ... http://zope.egenix.com/ ________________________________________________________________________ ::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ http://www.malemburg.com/ From antoon.pardon at rece.vub.ac.be Mon Jan 23 11:02:22 2017 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 23 Jan 2017 17:02:22 +0100 Subject: How to create a socket.socket() object from a socket fd? In-Reply-To: References: Message-ID: Op 22-01-17 om 01:52 schreef Grant Edwards: > Newsgroups: gmane.comp.python.general > From: Grant Edwards > Subject: Re: How to create a socket.socket() object from a socket fd? > References: > Followup-To: > > > > I'm still baffled why the standard library fromfd() code dup()s the > descriptor. > > According to the comment in the CPython sources, the author of > fromfd() is guessing that the user wants to be able to close the > descriptor separately from the socket. > > If the user wanted the socket object to use a duplicate descriptor for > some reason, the caller should call os.dup() -- it's only _eight_ > keystrokes. Eight keystrokes that makes it obvious to anybody reading > the code that there are now two descriptors and you have to close both > the original descriptor and the socket. > > When you create a Python file object from a file descriptor using > os.fdopen(), does it dup the descriptor? No. Would a reasonable > person expect socket.fromfd() to duplicate the descriptor? No. > > Should it? > > No. The standard response to issues like this is: A foolish consistency is the hobgoblin of little minds -- Antoon Pardon From breamoreboy at gmail.com Mon Jan 23 11:24:27 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Mon, 23 Jan 2017 08:24:27 -0800 (PST) Subject: [Python-ideas] "Immutable Builder" Pattern and Operator In-Reply-To: References: <2127feaa-0a5b-8246-978f-6346dea554a2@gmail.com> <0654a27e-7200-c468-d4eb-17bef13b61d2@egenix.com> <5032a807-75d5-d58f-5332-97ec22b0340a@egenix.com> Message-ID: <9d943553-2133-4b30-81ca-75a649900520@googlegroups.com> On Monday, January 23, 2017 at 2:11:53 PM UTC, M.-A. Lemburg wrote: > On 23.01.2017 14:28, Soni L. wrote: > > > > > > On 23/01/17 11:18 AM, M.-A. Lemburg wrote: > >> On 23.01.2017 14:05, Soni L. wrote: > >>> Yeah but the dotequals operator has many other benefits: > >>> > >>> long_name .= __call__ # cast to callable > >>> long_name .= wrapped # unwrap > >>> etc > >>> > >>> And it also looks neat. > >> I don't see this an being a particular intuitive way of writing > >> such rather uncommon constructs. > >> > >> The syntax is not clear (what if you have an expression on the RHS) > >> and it doesn't save you much in writing (if long_name is too long > >> simply rebind it under a shorter name for the purpose of the code > >> block). > > > > It's literally sugar for repeating the name and moving the dot to the > > right. I think it's clearer than most other compound operators in that > > it doesn't affect precedence rules. > > > > `x += y`, for any code `y`, is equivalent to `x = x + (y)`, not `x = x + > > y`. > > > > `x .= y`, for any code `y`, is equivalent to `x = x . y`, not `x = x . > > (y)`. > > Well, then please consider these: > > x .= y + z > x .= y * z > x .= y.z > x .= y.z() > > >> Also note that rebinding different objects to the same name > >> in the same block is often poor style and can easily lead to > >> hard to track bugs. > >> > > > > Rebinding different objects to the same name in rapid succession > > is fine. > > Not in my book :-) > > It may be fine if the object type stays the same or > in those few cases, where you want to accept multiple > different types for a parameter and then coerce these > to a type that you use in the rest of the code block. > > But even in those cases, debugging becomes easier if > you keep the original binding in place (since you then > know where the new values originated). > > This is not good style... > > x = 'abc' > x = len(x) > x = [x, 1] > x = ''.join(str(a) for a in x) > print (x) > > -- > Marc-Andre Lemburg > eGenix.com > The wrong list methinks :) Kindest regards. Mark Lawrence. From grant.b.edwards at gmail.com Mon Jan 23 12:00:49 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Mon, 23 Jan 2017 17:00:49 +0000 (UTC) Subject: How to create a socket.socket() object from a socket fd? References: Message-ID: On 2017-01-23, Antoon Pardon wrote: > Op 22-01-17 om 01:52 schreef Grant Edwards: >> Newsgroups: gmane.comp.python.general >> From: Grant Edwards >> Subject: Re: How to create a socket.socket() object from a socket fd? >> References: >> Followup-To: >> >> >> >> I'm still baffled why the standard library fromfd() code dup()s the >> descriptor. >> >> According to the comment in the CPython sources, the author of >> fromfd() is guessing that the user wants to be able to close the >> descriptor separately from the socket. >> >> If the user wanted the socket object to use a duplicate descriptor for >> some reason, the caller should call os.dup() -- it's only _eight_ >> keystrokes. Eight keystrokes that makes it obvious to anybody reading >> the code that there are now two descriptors and you have to close both >> the original descriptor and the socket. >> >> When you create a Python file object from a file descriptor using >> os.fdopen(), does it dup the descriptor? No. Would a reasonable >> person expect socket.fromfd() to duplicate the descriptor? No. >> >> Should it? >> >> No. > > The standard response to issues like this is: > > A foolish consistency is the hobgoblin of little minds And wise consistency is the foundation of a good language design. -- Grant Edwards grant.b.edwards Yow! I'm wet! I'm wild! at gmail.com From breamoreboy at gmail.com Mon Jan 23 12:24:45 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Mon, 23 Jan 2017 09:24:45 -0800 (PST) Subject: How coding in Python is bad for you Message-ID: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> The article is here http://lenkaspace.net/index.php/blog/show/111 Kindest regards. Mark Lawrence. From rosuav at gmail.com Mon Jan 23 12:34:19 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 04:34:19 +1100 Subject: How coding in Python is bad for you In-Reply-To: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On Tue, Jan 24, 2017 at 4:24 AM, wrote: > The article is here http://lenkaspace.net/index.php/blog/show/111 I would respond point-by-point if I thought the author had a clue. ChrisA From kalalsunnyy at gmail.com Mon Jan 23 12:49:48 2017 From: kalalsunnyy at gmail.com (Sourabh Kalal) Date: Mon, 23 Jan 2017 23:19:48 +0530 Subject: Is it possible to get the Physical memory address of a variable in python? Message-ID: how we can access the value from using id.. like x=10 id(x) 3235346364 how i can read value 10 using id 3235346364 From ethan at stoneleaf.us Mon Jan 23 13:09:35 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 23 Jan 2017 10:09:35 -0800 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <588646DF.9050905@stoneleaf.us> On 01/23/2017 09:34 AM, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 4:24 AM, wrote: >> The article is here http://lenkaspace.net/index.php/blog/show/111 > > I would respond point-by-point if I thought the author had a clue. Yeah, arguing with that person will be a waste of time. -- ~Ethan~ From rosuav at gmail.com Mon Jan 23 13:49:06 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 05:49:06 +1100 Subject: Is it possible to get the Physical memory address of a variable in python? In-Reply-To: References: Message-ID: On Tue, Jan 24, 2017 at 4:49 AM, Sourabh Kalal wrote: > how we can access the value from using id.. > like x=10 > id(x) > 3235346364 > > how i can read value 10 using id 3235346364 No, you can't. That isn't a memory address - it's just a unique identifier. Python doesn't have memory addresses. ChrisA From tjreedy at udel.edu Mon Jan 23 13:59:52 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 23 Jan 2017 13:59:52 -0500 Subject: Is it possible to get the Physical memory address of a variable in python? In-Reply-To: References: Message-ID: On 1/23/2017 12:49 PM, Sourabh Kalal wrote: > how we can access the value from using id.. > like x=10 > id(x) > 3235346364 > > how i can read value 10 using id 3235346364 *In Python*, you cannot. Ids are mainly for internal use of implementations. Implementors also use them to test their implementation. *CPython* comes with a module (ctypes) that can be used to muck around with interpreter internals, but it is definitely not for beginners. I don't know about other implementations. -- Terry Jan Reedy From skip.montanaro at gmail.com Mon Jan 23 14:05:58 2017 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Mon, 23 Jan 2017 13:05:58 -0600 Subject: Is it possible to get the Physical memory address of a variable in python? In-Reply-To: References: Message-ID: On Mon, Jan 23, 2017 at 12:49 PM, Chris Angelico wrote: > > On Tue, Jan 24, 2017 at 4:49 AM, Sourabh Kalal wrote: > > how we can access the value from using id.. > > like x=10 > > id(x) > > 3235346364 > > > > how i can read value 10 using id 3235346364 > > No, you can't. That isn't a memory address - it's just a unique > identifier. Python doesn't have memory addresses. Well, it might be, but that would be very implementation-dependent. Any attempt to treat an id as a memory address and interpret the object at that address as the beginning of a PyObject of any kind will be fragile in the extreme. Even if you have two different implementations at hand which both use memory addresses as convenient ids, there's no guarantee that the address represents that start of a CPython PyObject. >>> hex(3235346364) '0xc0d777bc' There. *Now* you have an address. Hack to your heart's content. Skip From __peter__ at web.de Mon Jan 23 14:34:06 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 23 Jan 2017 20:34:06 +0100 Subject: Is it possible to get the Physical memory address of a variable in python? References: Message-ID: Sourabh Kalal wrote: > how we can access the value from using id.. > like x=10 > id(x) > 3235346364 > > how i can read value 10 using id 3235346364 Use ctypes: $ python3 Python 3.4.3 (default, Nov 17 2016, 01:08:31) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes >>> ctypes.c_ubyte.from_address(id(10) + 24).value = 11 >>> 10 == 11 True :) From martin.schoon at gmail.com Mon Jan 23 14:57:50 2017 From: martin.schoon at gmail.com (Martin =?UTF-8?Q?Sch=C3=B6=C3=B6n?=) Date: 23 Jan 2017 19:57:50 GMT Subject: Adding colormaps? References: Message-ID: Den 2017-01-23 skrev blue : > you have here a full example , for another version not significant changes: > http://matplotlib.org/examples/color/colormaps_reference.html Thanks but this only shows how to use it once you have it. When I run this code I get an error message telling it can't find any of those new colormaps. /Martin From grant.b.edwards at gmail.com Mon Jan 23 14:59:58 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Mon, 23 Jan 2017 19:59:58 +0000 (UTC) Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On 2017-01-23, breamoreboy at gmail.com wrote: > The article is here http://lenkaspace.net/index.php/blog/show/111 I don't really think any of his points are valid, but one way that programming in Python is bad for you: * It reduces your tolerance for progamming in PHP zero. If you end up assigned to a PHP project, you end up miserable and unpleasant to work with or just plain unemployed. -- Grant Edwards grant.b.edwards Yow! MERYL STREEP is my at obstetrician! gmail.com From torriem at gmail.com Mon Jan 23 15:15:30 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 23 Jan 2017 13:15:30 -0700 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <665f8eaf-6fbe-958d-a0b0-2bfc1f263327@gmail.com> On 01/23/2017 10:34 AM, Chris Angelico wrote: > I would respond point-by-point if I thought the author had a clue. Yeah a pretty bizarre, flame-bait blog post. Glad I use an ad-blocker as a matter of course. I'm uncertain as to why Mark chose to post that particular little gem to the list. It's neither thought-provoking nor terribly insightful. From rosuav at gmail.com Mon Jan 23 15:17:32 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 07:17:32 +1100 Subject: Is it possible to get the Physical memory address of a variable in python? In-Reply-To: References: Message-ID: On Tue, Jan 24, 2017 at 6:05 AM, Skip Montanaro wrote: > On Mon, Jan 23, 2017 at 12:49 PM, Chris Angelico wrote: >> >> On Tue, Jan 24, 2017 at 4:49 AM, Sourabh Kalal wrote: >> > how we can access the value from using id.. >> > like x=10 >> > id(x) >> > 3235346364 >> > >> > how i can read value 10 using id 3235346364 >> >> No, you can't. That isn't a memory address - it's just a unique >> identifier. Python doesn't have memory addresses. > > Well, it might be, but that would be very implementation-dependent. > Any attempt to treat an id as a memory address and interpret the > object at that address as the beginning of a PyObject of any kind will > be fragile in the extreme. Even if you have two different > implementations at hand which both use memory addresses as convenient > ids, there's no guarantee that the address represents that start of a > CPython PyObject. > >>>> hex(3235346364) > '0xc0d777bc' > > There. *Now* you have an address. Hack to your heart's content. No, you now have a hexadecimal representation of an integer. Memory addresses are integers; Python object identifiers are also integers. The fact that they may happen to correspond *in CPython* is basically a coincidence. In a language like C, where pointers and memory addresses are real things, you can work with an integer and dereference it to get the data at that address. In BASIC, where I first learned programming, you have specific instructions to mess with memory (I have fond memories of messing around with segment zero to change the status of Caps Lock etc, and of peeking and poking in video memory, and stuff). But in Python, that exists only in ctypes, and that only because ctypes is basically C. rosuav at sikorsky:~$ python3 Python 3.7.0a0 (default:cebc9c7ad195, Jan 24 2017, 06:55:19) [GCC 6.2.0 20161027] on linux Type "help", "copyright", "credits" or "license" for more information. >>> objs = [object() for _ in range(10)] >>> [id(x) for x in objs] [140652901773504, 140652901773520, 140652901773536, 140652901773552, 140652901773568, 140652901773584, 140652901773600, 140652901773616, 140652901773632, 140652901773648] These could well be memory addresses, but... >>> objs = [object() for _ in range(10)] >>> [id(x) for x in objs] [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] ... these almost certainly aren't. They are, however, unique integer values (the 'objs' list guarantees that all objects are simultaneously alive, ergo the IDs must be unique). Other Python implementations are free to do what they like. Quite a few do seem to use something like a memory address (uPy, PyPy, PyPyJS), but not all: Brython 3.3.0 on Netscape 5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 >>> objs = [object() for _ in range(10)] >>> [id(x) for x in objs] [54, 55, 56, 57, 58, 59, 60, 61, 62, 63] There's basically no way to go from an ID to the object. If you know what data type it is (eg an integer, as in the OP's example), you can reach in and grab the contents, but there's no way to write this function: def deref(id): ... # ???? assert deref(id(x)) is x The best you could do, in terms of parlour tricks, is to test it only on small integers, where ctypes can get you the value of the integer, and thanks to small-int-caching, it will actually be the same object. But it won't work for larger integers. It's not a pointer. ChrisA From rosuav at gmail.com Mon Jan 23 15:19:42 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 07:19:42 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On Tue, Jan 24, 2017 at 6:59 AM, Grant Edwards wrote: > On 2017-01-23, breamoreboy at gmail.com wrote: > >> The article is here http://lenkaspace.net/index.php/blog/show/111 > > I don't really think any of his points are valid, but one way that > programming in Python is bad for you: > > * It reduces your tolerance for progamming in PHP zero. If you end > up assigned to a PHP project, you end up miserable and unpleasant > to work with or just plain unemployed. I believe that's "bad for you" in the sense that chocolate is bad for you. It isn't. ChrisA From torriem at gmail.com Mon Jan 23 15:24:35 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 23 Jan 2017 13:24:35 -0700 Subject: Is it possible to get the Physical memory address of a variable in python? In-Reply-To: References: Message-ID: <8d84a398-aa19-3819-5408-0738555a5455@gmail.com> On 01/23/2017 10:49 AM, Sourabh Kalal wrote: > how we can access the value from using id.. > like x=10 > id(x) > 3235346364 > > how i can read value 10 using id 3235346364 Many objects in python such as numbers like 10 or strings are immutable; they can never be altered once called into existance. When you assign to a variable, you are binding the variable name to an object. So if you later took your x variable and re-assigned it to value 11, the id of x would change to reflect this new object 11. If you could find the memory address in your example where 10 is stored, and then modified it, you could mess up a lot of things because the interpreter is free to optimize and to use the same 10 object every places where 10 is assigned to a variable. From skip.montanaro at gmail.com Mon Jan 23 15:26:31 2017 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Mon, 23 Jan 2017 14:26:31 -0600 Subject: Is it possible to get the Physical memory address of a variable in python? In-Reply-To: References: Message-ID: On Mon, Jan 23, 2017 at 2:17 PM, Chris Angelico wrote: >> There. *Now* you have an address. Hack to your heart's content. > > No, you now have a hexadecimal representation of an integer. You missed my attempt at levity, I think. S From rosuav at gmail.com Mon Jan 23 15:32:29 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 07:32:29 +1100 Subject: Is it possible to get the Physical memory address of a variable in python? In-Reply-To: References: Message-ID: On Tue, Jan 24, 2017 at 7:26 AM, Skip Montanaro wrote: > On Mon, Jan 23, 2017 at 2:17 PM, Chris Angelico wrote: >>> There. *Now* you have an address. Hack to your heart's content. >> >> No, you now have a hexadecimal representation of an integer. > > You missed my attempt at levity, I think. Ahh, I guess I did. Sadly, it is all too true that there is no statement so ridiculous that it's unquestionably a joke. An ill-informed person could easily come out with exactly the same comment. :( ChrisA From alister.ware at ntlworld.com Mon Jan 23 15:32:49 2017 From: alister.ware at ntlworld.com (alister) Date: Mon, 23 Jan 2017 20:32:49 GMT Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 6:59 AM, Grant Edwards > wrote: >> On 2017-01-23, breamoreboy at gmail.com wrote: >> >>> The article is here http://lenkaspace.net/index.php/blog/show/111 >> >> I don't really think any of his points are valid, but one way that >> programming in Python is bad for you: >> >> * It reduces your tolerance for progamming in PHP zero. If you end >> up assigned to a PHP project, you end up miserable and unpleasant to >> work with or just plain unemployed. > > I believe that's "bad for you" in the sense that chocolate is bad for > you. > > It isn't. > > ChrisA chocolate is a poison (lethal dose for a human approx 22lb) -- Make headway at work. Continue to let things deteriorate at home. From sjeik_appie at hotmail.com Mon Jan 23 15:35:52 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Mon, 23 Jan 2017 20:35:52 +0000 Subject: Adding colormaps? In-Reply-To: References: , Message-ID: (sorry for top-posting) I does not appear to be possible in matplolibrc (1). But you can use matplotlib.cm.register_cmap to register new cmaps (2) such as these (3). (Note: I did not try this) (1)http://matplotlib.org/1.4.0/users/customizing.html (2)http://matplotlib.org/api/cm_api.html (3)https://github.com/BIDS/colormap/blob/master/colormaps.py ________________________________ From: Python-list on behalf of Martin Sch??n Sent: Saturday, January 21, 2017 8:42:29 PM To: python-list at python.org Subject: Re: Adding colormaps? Den 2017-01-21 skrev Gilmeh Serda : > On Wed, 18 Jan 2017 21:41:34 +0000, Martin Sch??n wrote: > >> What I would like to do is to add the perceptually uniform sequential >> colormaps introduced in version 1.5.something. I would like to do this >> without breaking my Debian system in which Matplotlib version 1.4.2 is >> the newest version available in the repo. > > Haven't checked, but I assume you can get the source. Compile it but > don't install it and then use the result in virtualenv, maybe? > I am hoping for directions to a config file to download and place somewhere... /Martin -- https://mail.python.org/mailman/listinfo/python-list From jon+usenet at unequivocal.eu Mon Jan 23 15:39:26 2017 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Mon, 23 Jan 2017 20:39:26 -0000 (UTC) Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On 2017-01-23, alister wrote: > On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: >> I believe that's "bad for you" in the sense that chocolate is bad for >> you. >> >> It isn't. > > chocolate is a poison (lethal dose for a human approx 22lb) That's a meaningless statement. *Everything* is a poison in sufficient quantities. From sjeik_appie at hotmail.com Mon Jan 23 15:44:29 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Mon, 23 Jan 2017 20:44:29 +0000 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> , Message-ID: sola dosis facit venenum ~ Paracelsus (1493-1541) ________________________________ From: Python-list on behalf of alister Sent: Monday, January 23, 2017 8:32:49 PM To: python-list at python.org Subject: Re: How coding in Python is bad for you On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 6:59 AM, Grant Edwards > wrote: >> On 2017-01-23, breamoreboy at gmail.com wrote: >> >>> The article is here http://lenkaspace.net/index.php/blog/show/111 >> >> I don't really think any of his points are valid, but one way that >> programming in Python is bad for you: >> >> * It reduces your tolerance for progamming in PHP zero. If you end >> up assigned to a PHP project, you end up miserable and unpleasant to >> work with or just plain unemployed. > > I believe that's "bad for you" in the sense that chocolate is bad for > you. > > It isn't. > > ChrisA chocolate is a poison (lethal dose for a human approx 22lb) -- Make headway at work. Continue to let things deteriorate at home. -- https://mail.python.org/mailman/listinfo/python-list From amorawski at magna-power.com Mon Jan 23 16:04:01 2017 From: amorawski at magna-power.com (Adam M) Date: Mon, 23 Jan 2017 13:04:01 -0800 (PST) Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <19df8402-c187-4250-b7f8-66d7e02475c0@googlegroups.com> On Monday, January 23, 2017 at 3:41:17 PM UTC-5, Jon Ribbens wrote: > On 2017-01-23, alister wrote: > > On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: > >> I believe that's "bad for you" in the sense that chocolate is bad for > >> you. > >> > >> It isn't. > > > > chocolate is a poison (lethal dose for a human approx 22lb) > > That's a meaningless statement. *Everything* is a poison > in sufficient quantities. I think you need to calibrate your sarcasm filter ;-). By the way coffee is also dangerous - especially in 50lbs bags (when it hits you). From rosuav at gmail.com Mon Jan 23 16:11:02 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 08:11:02 +1100 Subject: How coding in Python is bad for you In-Reply-To: <19df8402-c187-4250-b7f8-66d7e02475c0@googlegroups.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <19df8402-c187-4250-b7f8-66d7e02475c0@googlegroups.com> Message-ID: On Tue, Jan 24, 2017 at 8:04 AM, Adam M wrote: > On Monday, January 23, 2017 at 3:41:17 PM UTC-5, Jon Ribbens wrote: >> On 2017-01-23, alister wrote: >> > On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: >> >> I believe that's "bad for you" in the sense that chocolate is bad for >> >> you. >> >> >> >> It isn't. >> > >> > chocolate is a poison (lethal dose for a human approx 22lb) >> >> That's a meaningless statement. *Everything* is a poison >> in sufficient quantities. > > I think you need to calibrate your sarcasm filter ;-). By the way coffee is also dangerous - especially in 50lbs bags (when it hits you). Or with sufficient velocity. The LD50 of lead is about 450mg/kg [1], which means that a typical 70kg human could ingest about 31 grams of lead and have a fifty-fifty chance of survival. But just a few grams of lead at the speed of sound will probably kill you. [2] ChrisA [1] http://whs.rocklinusd.org/documents/Science/Lethal_Dose_Table.pdf [2] https://xkcd.com/444/ From breamoreboy at gmail.com Mon Jan 23 16:21:00 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Mon, 23 Jan 2017 13:21:00 -0800 (PST) Subject: Is it possible to get the Physical memory address of a variable in python? In-Reply-To: References: Message-ID: <2517f772-2a0f-45f6-8ab1-ceb3548cc876@googlegroups.com> On Monday, January 23, 2017 at 5:59:42 PM UTC, Sourabh Kalal wrote: > how we can access the value from using id.. > like x=10 > id(x) > 3235346364 > > how i can read value 10 using id 3235346364 What are you trying to achieve here? If you'd explain that rather than how you're trying to achieve it you're likely to get better answers. Kindest regards. Mark Lawrence. From subhabangalore at gmail.com Mon Jan 23 16:23:38 2017 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Mon, 23 Jan 2017 13:23:38 -0800 (PST) Subject: String Replacement Message-ID: I have a string like "Trump is $ the president of USA % Obama was $ the president of USA % Putin is $ the premier of Russia%" Here, I want to extract the portions from $...%, which would be "the president of USA", "the president of USA", "the premier of Russia" and would work some post extraction jobs, like I may split them or annotate them and may replace them back to its own position with the edited string. In the end it may look like "Trump is the/DET president/NN of/PREP USA/NN Obama was the/DET president/NN of/PREP USA/NN Putin is the/DET premier/NN of/PREP Russia/NN" I am working around replace and re.sub If any one may kindly suggest. I am using Python2.7.12 on MS-Windows 7 Thanking in Advance From rosuav at gmail.com Mon Jan 23 16:30:45 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 08:30:45 +1100 Subject: String Replacement In-Reply-To: References: Message-ID: On Tue, Jan 24, 2017 at 8:23 AM, wrote: > I have a string like > > "Trump is $ the president of USA % Obama was $ the president of USA % Putin is $ the premier of Russia%" > > Here, I want to extract the portions from $...%, which would be > > "the president of USA", > "the president of USA", > "the premier of Russia" > > and would work some post extraction jobs, like I may split them or annotate them and may replace them > back to its own position with the edited string. > The simplest solution would be a regex. Have a shot at it and see how you go; if you get stuck, post code. > I am using Python2.7.12 on MS-Windows 7 Any particular reason you're not using Python 3? ChrisA From best_lay at yahoo.com Mon Jan 23 16:39:58 2017 From: best_lay at yahoo.com (Wildman) Date: Mon, 23 Jan 2017 15:39:58 -0600 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <7YSdnSVIpN2z5RvFnZ2dnUU7-WmdnZ2d@giganews.com> On Mon, 23 Jan 2017 20:39:26 +0000, Jon Ribbens wrote: > On 2017-01-23, alister wrote: >> On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: >>> I believe that's "bad for you" in the sense that chocolate is bad for >>> you. >>> >>> It isn't. >> >> chocolate is a poison (lethal dose for a human approx 22lb) > > That's a meaningless statement. *Everything* is a poison > in sufficient quantities. Yes, even water. -- GNU/Linux user #557453 The cow died so I don't need your bull! From bc at freeuk.com Mon Jan 23 16:55:01 2017 From: bc at freeuk.com (BartC) Date: Mon, 23 Jan 2017 21:55:01 +0000 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On 23/01/2017 17:34, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 4:24 AM, wrote: >> The article is here http://lenkaspace.net/index.php/blog/show/111 > > I would respond point-by-point if I thought the author had a clue. I thought points 1 to 4 were valid, in that the assertions were true. Point 5 (memory leaks) I haven't experienced. Point 6 (libraries depending on libraries...) is probably true in other languages too. Point 7 (Python claiming to be general purpose) I don't have an opinion about. -- Bartc From rosuav at gmail.com Mon Jan 23 17:09:51 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 09:09:51 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On Tue, Jan 24, 2017 at 8:55 AM, BartC wrote: > On 23/01/2017 17:34, Chris Angelico wrote: >> >> On Tue, Jan 24, 2017 at 4:24 AM, wrote: >>> >>> The article is here http://lenkaspace.net/index.php/blog/show/111 >> >> >> I would respond point-by-point if I thought the author had a clue. > > > I thought points 1 to 4 were valid, in that the assertions were true. 1 is wrong - there is structure, same as in every language. Or if it's true, it's true in every language. 2 is also trivially true - you can ALWAYS define variables wrongly. Yes, okay, so you don't have data type checking, but that's only going to catch a specific subset of errors. 3, well, okay. But if you get your indentation wrong in C++, Java, etc, it should fail code review. 4 is flat out wrong. > Point 5 (memory leaks) I haven't experienced. Me neither - Python doesn't leak memory, and in fact CPython has leak testing as part of its test suite. You are *far* more likely to experience memory leaks in a C program than a Python one. > Point 6 (libraries depending on libraries...) is probably true in other > languages too. And far more so. He says "(because, let's face it, Python by itself doesn't support much)" - that's flat wrong. Python's standard library is _excellent_ compared to many other languages'. > Point 7 (Python claiming to be general purpose) I don't have an opinion > about. He doesn't even have a decent point in there. We're so insular because we think Python is general? Huh? > But for a big project, Python code can become a big mess very quickly, > unless there is an experienced programmer to guide the development process. So... can you name a language where a bunch of novices can build a huge project with no scaling troubles? Honestly? Get yourself an expert, at least in an advisory role. Not one of his points is truly valid, other than the ones that are trivially valid and true of every language. ChrisA From ethan at stoneleaf.us Mon Jan 23 17:18:26 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 23 Jan 2017 14:18:26 -0800 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <58868132.8070500@stoneleaf.us> On 01/23/2017 01:55 PM, BartC wrote: > On 23/01/2017 17:34, Chris Angelico wrote: >> On Tue, Jan 24, 2017 at 4:24 AM, breamoreboy wrote: >>> The article is here http://lenkaspace.net/index.php/blog/show/111 >> >> I would respond point-by-point if I thought the author had a clue. > > I thought points 1 to 4 were valid, in that the assertions were true. Seriously? 1. Structure - having a predefined structure does not keep your program from becoming a mess. 2. Lying - any language can have poorly named parameters. 3. Meaning based on indentation - flat-out wrong: x += 1 does exactly what it says, whether it's indented once, twice, or seven times. 4. No compiler & no feedback - Python code is compiled, and feedback is available via linters, mypy, etc. The only thing proven by that essay is that Lenka is a lousy Python programmer. He (she?) didn't even tag the article with Python, instead using the tags Java, C++, and ActionScript. -- ~Ethan~ From ethan at stoneleaf.us Mon Jan 23 17:50:35 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 23 Jan 2017 14:50:35 -0800 Subject: How to create a socket.socket() object from a socket fd? In-Reply-To: References: Message-ID: <588688BB.3040900@stoneleaf.us> On 01/23/2017 09:00 AM, Grant Edwards wrote: > On 2017-01-23, Antoon Pardon wrote: >> The standard response to issues like this is: >> >> A foolish consistency is the hobgoblin of little minds > > And wise consistency is the foundation of a good language design. Otherwise known as: if there's not a /very/ good reason to make them different, make them the same. -- ~Ethan~ From none at invalid.com Mon Jan 23 17:55:19 2017 From: none at invalid.com (mm0fmf) Date: Mon, 23 Jan 2017 22:55:19 +0000 Subject: How coding in Python is bad for you In-Reply-To: <19df8402-c187-4250-b7f8-66d7e02475c0@googlegroups.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <19df8402-c187-4250-b7f8-66d7e02475c0@googlegroups.com> Message-ID: On 23/01/2017 21:04, Adam M wrote: > On Monday, January 23, 2017 at 3:41:17 PM UTC-5, Jon Ribbens wrote: >> On 2017-01-23, alister wrote: >>> On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: >>>> I believe that's "bad for you" in the sense that chocolate is bad for >>>> you. >>>> >>>> It isn't. >>> >>> chocolate is a poison (lethal dose for a human approx 22lb) >> >> That's a meaningless statement. *Everything* is a poison >> in sufficient quantities. > > I think you need to calibrate your sarcasm filter ;-). By the way coffee is also dangerous - especially in 50lbs bags (when it hits you). > What a way to go... 50lbs of coffee beans made into espresso and 22lbs of chocolate to eat with all those tiny cups. Yes, the article is clickbait which is why I delight in reading them with ad and script blockers turned up to maximum. From info at tundraware.com Mon Jan 23 18:15:20 2017 From: info at tundraware.com (Tim Daneliuk) Date: Mon, 23 Jan 2017 17:15:20 -0600 Subject: How coding in Python is bad for you In-Reply-To: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <8dfild-vgj.ln1@oceanview.tundraware.com> On 01/23/2017 11:24 AM, breamoreboy at gmail.com wrote: > The article is here http://lenkaspace.net/index.php/blog/show/111 > > Kindest regards. > > Mark Lawrence. > Beyond silly. Languages - like all tools - can be used properly or badly. From info at tundraware.com Mon Jan 23 18:16:28 2017 From: info at tundraware.com (Tim Daneliuk) Date: Mon, 23 Jan 2017 17:16:28 -0600 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On 01/23/2017 02:19 PM, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 6:59 AM, Grant Edwards > wrote: >> On 2017-01-23, breamoreboy at gmail.com wrote: >> >>> The article is here http://lenkaspace.net/index.php/blog/show/111 >> >> I don't really think any of his points are valid, but one way that >> programming in Python is bad for you: >> >> * It reduces your tolerance for progamming in PHP zero. If you end >> up assigned to a PHP project, you end up miserable and unpleasant >> to work with or just plain unemployed. > > I believe that's "bad for you" in the sense that chocolate is bad for you. > > It isn't. > > ChrisA > It's bad for you in the same sense tha chocolate is bad for you - if you melt it and pour it into your ears... From bc at freeuk.com Mon Jan 23 19:44:14 2017 From: bc at freeuk.com (BartC) Date: Tue, 24 Jan 2017 00:44:14 +0000 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On 23/01/2017 22:09, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 8:55 AM, BartC wrote: >> On 23/01/2017 17:34, Chris Angelico wrote: >>> >>> On Tue, Jan 24, 2017 at 4:24 AM, wrote: >>>> >>>> The article is here http://lenkaspace.net/index.php/blog/show/111 >>> >>> >>> I would respond point-by-point if I thought the author had a clue. >> >> >> I thought points 1 to 4 were valid, in that the assertions were true. > > 1 is wrong - there is structure, same as in every language. Or if it's > true, it's true in every language. Python (I think in common with other scripting languages) allows you to place statement outside functions, either before, after or in-between functions. While functions themselves, as well as imports, can be conditionally defined. 2 is also trivially true - you can > ALWAYS define variables wrongly. Yes, okay, so you don't have data > type checking, but that's only going to catch a specific subset of > errors. With Python, you can take function names that you've defined, imported modules, or math.pi, and set them all to "edward g robinson". (The name just came into my head..) > 3, well, okay. But if you get your indentation wrong in C++, > Java, etc, it should fail code review. With C++ or Java, it's possible to tell the indentation is wrong (because of the extra redundancy of having the indentation /and/ block delimiters). That's a bit harder in Python making source more fragile. > 4 is flat out wrong. (About having no compiler.) Yes (C)Python does have a byte-code compiler but there are a vast range of errors that it cannot trap at compile-time because of its dynamic nature. Maybe you can run tools to do extra analyses, but as it's normally used, many errors are not detected until execution. Spelling errors in names for example. (Actually when I tested my assertion above I assigned to math.p rather than math.pi; no error not even at runtime, just the wrong result. Or technically the right one, as math.pi still had the usual value!) I'm not criticising it, just stating facts which may well have been the experience of the guy who wrote the article, giving the impression of a more chaotic, free-for-all language than they may have been used to, or expected. -- bartc From rosuav at gmail.com Mon Jan 23 19:56:01 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 11:56:01 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On Tue, Jan 24, 2017 at 11:44 AM, BartC wrote: > On 23/01/2017 22:09, Chris Angelico wrote: >> 1 is wrong - there is structure, same as in every language. Or if it's >> true, it's true in every language. > > > Python (I think in common with other scripting languages) allows you to > place statement outside functions, either before, after or in-between > functions. While functions themselves, as well as imports, can be > conditionally defined. In other words, Python doesn't have arbitrary, useless structure by forcing you to put everything inside a class definition and then inside a method, as in Java. How is that a reason that Python is "bad for you"? REXX has even less structure than Python - it doesn't even have functions, just labels, so you can actually have two functions that share a common tail. And yes, you can abuse that horrendously to create unreadable code. Is REXX a bad language because of that? No. You can use structure badly, and you can use freedom badly. > 2 is also trivially true - you can >> >> ALWAYS define variables wrongly. Yes, okay, so you don't have data >> type checking, but that's only going to catch a specific subset of >> errors. > > > With Python, you can take function names that you've defined, imported > modules, or math.pi, and set them all to "edward g robinson". (The name just > came into my head..) Yes, and? You can do that in other languages too. So? Again, how is this proof that Python is "bad for you"? >> 3, well, okay. But if you get your indentation wrong in C++, >> Java, etc, it should fail code review. > > > With C++ or Java, it's possible to tell the indentation is wrong (because of > the extra redundancy of having the indentation /and/ block delimiters). > That's a bit harder in Python making source more fragile. With C++ or Java, it's possible to tell that the braces are misplaced (because of the extra redundancy). When that happens, the compiler won't tell you; it'll just silently do the wrong thing. In Python, that can't happen. Python is therefore more robust. What's to say which is correct? >> 4 is flat out wrong. > > > (About having no compiler.) Yes (C)Python does have a byte-code compiler but > there are a vast range of errors that it cannot trap at compile-time because > of its dynamic nature. > > Maybe you can run tools to do extra analyses, but as it's normally used, > many errors are not detected until execution. Spelling errors in names for > example. (Actually when I tested my assertion above I assigned to math.p > rather than math.pi; no error not even at runtime, just the wrong result. Or > technically the right one, as math.pi still had the usual value!) Plenty of editors can catch those even before you run the code. And there are languages with far more structure that still don't protect you against very common classes of bug. if (i = 1) { printf("i is 1.... now."); } Some compilers will give you a warning for this code, if you ask for all warnings to be enabled. Others won't. The C language doesn't protect you. Should we abolish C? > I'm not criticising it, just stating facts which may well have been the > experience of the guy who wrote the article, giving the impression of a more > chaotic, free-for-all language than they may have been used to, or expected. And because it's not what he's used to, it is a fundamentally bad language. That's the sign of a badly uninformed post. ChrisA From rosuav at gmail.com Mon Jan 23 19:58:38 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 11:58:38 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <19df8402-c187-4250-b7f8-66d7e02475c0@googlegroups.com> Message-ID: On Tue, Jan 24, 2017 at 11:55 AM, Dennis Lee Bieber wrote: > On Mon, 23 Jan 2017 22:55:19 +0000, mm0fmf declaimed the > following: > > >>50lbs of coffee beans made into espresso and 22lbs of chocolate to eat >>with all those tiny cups. >> > How about just 75lbs of chocolate covered espresso beans Can you fire them out of a machine gun? ChrisA From bc at freeuk.com Mon Jan 23 20:47:07 2017 From: bc at freeuk.com (BartC) Date: Tue, 24 Jan 2017 01:47:07 +0000 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On 24/01/2017 00:56, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 11:44 AM, BartC wrote: >> With C++ or Java, it's possible to tell the indentation is wrong (because of >> the extra redundancy of having the indentation /and/ block delimiters). >> That's a bit harder in Python making source more fragile. > > With C++ or Java, it's possible to tell that the braces are misplaced > (because of the extra redundancy). When that happens, the compiler > won't tell you; it'll just silently do the wrong thing. In Python, > that can't happen. Python is therefore more robust. > > What's to say which is correct? I don't get you. Start with this code if 0: print ("one") print ("two") print ("three") It prints "three". Now the second indent accidentally gets deleted: if 0: print ("one") print ("two") print ("three") It still runs, but now prints "two", "three". Take the same code with block delimiters, and take out that same indent: if 0 then print ("one") print ("two") endif print ("three") It still compiles, it still runs, and still shows the correct "three" as output. And the discrepancy can be caught by extra tools (that you say ought to be used), or it is obvious visually anyway that the "two" line belongs with the "one" line not the "three" line. Yes, delimiters can be placed wrongly, but that is a harder error to make just by pressing one key (deleting a tab, or pressing Tab). (Someone is now going to tell me that it is possible to declare: endif = 0 in Python, and use 'endif' at the end of a block just like my example. Now any adjacent line one tab out of place will be equally obvious. However, no one does that! The language needs to require it and enforce it.) >> but as it's normally used, >> many errors are not detected until execution. > Plenty of editors can catch those even before you run the code. Extra tools... (But how would it catch assigning to a wrongly spelled attribute?) -- Bartc From steve+python at pearwood.info Mon Jan 23 20:55:50 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Tue, 24 Jan 2017 12:55:50 +1100 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <5886b429$0$1605$c3e8da3$5496439d@news.astraweb.com> On Tue, 24 Jan 2017 08:55 am, BartC wrote: > On 23/01/2017 17:34, Chris Angelico wrote: >> On Tue, Jan 24, 2017 at 4:24 AM, wrote: >>> The article is here http://lenkaspace.net/index.php/blog/show/111 >> >> I would respond point-by-point if I thought the author had a clue. > > I thought points 1 to 4 were valid, in that the assertions were true. Assertions can be true and still nonsense criticisms. "Oh my gawd, this car goes forward AND backwards!!! What a pile of guano!" What he's describing as flaws in language are seen by many as strengths of the language. Not only can Python go forward and backward, it can turn around corners as well! The author seems to want a B&D language which will force him to do The Right Thing? whether you want to or not. He seems to care about a very narrow definition of correctness, and doesn't mind how many weeks or months it takes him to write the code, or for that matter, how many years it takes him to learn how to write the code in one of his preferred languages: "...it takes years to build up a sense of what is good and what is bad coding practise" The reason why Python has become the #1 language for scientific computing is that for most such programmings, the extra level of safety you (allegedly) get from heavyweight C++ and Java style coding frameworks is unjustified. You're not building a nuclear reactor. With Python, you can get reliable *enough* code quickly, rather than bulletproof code that takes you forever. Or I should say, *allegedly* bulletproof. There's no actual reliable, objective evidence that coding in Java or C++ leads to actually more reliable code, rather than just the false sense of security that because it was *really really hard* to get the compiler to accept your code, it must have been protecting you from all sorts of bad things. Maybe it was, maybe it wasn't. > Point 5 (memory leaks) I haven't experienced. I suspect he's using memory leak in the sense, if you hold onto data forever, the memory used by that data will never be available for re-use. Reading his post, I get the impression he's the sort of programmer who believes in never, ever, ever, ever re-using a variable, so he might do something like: raw_data = collect_raw_data() # generates 10GB of data data_after_step_one = massage(raw_data) data_after_step_two = process(data_after_step_one) ... sorted_data = sorted(data_after_step_ten) and then wonders why he's running out of memory. "That's a memory leak!" But perhaps not. Maybe he's just using a lot of badly written C extensions that do leak memory. Or maybe he's one of the unlucky few who have stumbled across genuine memory leaks in the interpreter, but hasn't bothered to report it as a bug or upgrade to a version where it is fixed. Or maybe he's just mistaken. Or lying. > Point 6 (libraries depending on libraries...) is probably true in other > languages too. "I must have installed at least 30 libraries" Really? 30? As many as that? You poor thing. > Point 7 (Python claiming to be general purpose) I don't have an opinion > about. Python is an object-oriented language which encourages multi-paradigm coding styles: OOP, functional and even procedural. The underlying data model is based on objects, but that doesn't imply that you must or even should write all your code in an OO style. I suspect this guy is just a Java or C++ bigot. I have nothing against those who like to program in Java or C++, that's cool. I have my opinion on the languages themselves, but that's often a matter of personal taste. But when somebody says: "For example, encapsulation, one of the basic principles in OO is non-existant in Python." as he does, that's a form of the "no true Scotsman" fallacy. He is asserting that *his* (probably taken from Java?) definition of encapsulation is the only one. And that's why I call him a bigot. In a figurative sense -- the author might be the sweetest, most open and unprejudiced guy in the world when it comes to skin colour or religion for all I know. https://en.wikipedia.org/wiki/Object-oriented_programming#Encapsulation -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From rosuav at gmail.com Mon Jan 23 21:38:46 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 13:38:46 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On Tue, Jan 24, 2017 at 12:47 PM, BartC wrote: > On 24/01/2017 00:56, Chris Angelico wrote: >> >> On Tue, Jan 24, 2017 at 11:44 AM, BartC wrote: > > >>> With C++ or Java, it's possible to tell the indentation is wrong (because >>> of >>> the extra redundancy of having the indentation /and/ block delimiters). >>> That's a bit harder in Python making source more fragile. >> >> >> With C++ or Java, it's possible to tell that the braces are misplaced >> (because of the extra redundancy). When that happens, the compiler >> won't tell you; it'll just silently do the wrong thing. In Python, >> that can't happen. Python is therefore more robust. >> >> What's to say which is correct? > > Take the same code with block > delimiters, and take out that same indent: > > if 0 then > print ("one") > print ("two") > endif > print ("three") > > It still compiles, it still runs, and still shows the correct "three" as > output. My point is that you *assume* that showing just "three" is the correct behaviour. Why? Why do you automatically assume that the indentation is wrong and the endif is correct? All you have is that the two disagree. That's the sort of (presumably unintentional) bias that comes from your particular history of programming languages. Some languages teach you that "foo" and "Foo" are the same name. Others teach you that the integer 1 and the string "1" aren't materially different. Still others leave you believing that bytes and characters are perfectly equivalent. All those assumptions are proven wrong when you switch to a language that isn't like that, and indentation is the same. So what's to say which is more important? Surely it's nothing more than your preconceived ideas, and could easily be different if you had different experiences? ChrisA From rosuav at gmail.com Mon Jan 23 21:41:39 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 13:41:39 +1100 Subject: How coding in Python is bad for you In-Reply-To: <5886b429$0$1605$c3e8da3$5496439d@news.astraweb.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886b429$0$1605$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jan 24, 2017 at 12:55 PM, Steve D'Aprano wrote: > Reading his post, I get the impression he's the sort of programmer who > believes in never, ever, ever, ever re-using a variable, so he might do > something like: > > raw_data = collect_raw_data() # generates 10GB of data > data_after_step_one = massage(raw_data) > data_after_step_two = process(data_after_step_one) > ... > sorted_data = sorted(data_after_step_ten) > > and then wonders why he's running out of memory. "That's a memory leak!" How is that any different in any other language? I don't understand how even this is a criticism of Python. If you were working in Java, say, you'd have basically the same result - keep all the stuff, keep all the memory usage. ChrisA From steve+comp.lang.python at pearwood.info Mon Jan 23 23:22:11 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 24 Jan 2017 15:22:11 +1100 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> On Tuesday 24 January 2017 13:38, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 12:47 PM, BartC wrote: >> Take the same code with block >> delimiters, and take out that same indent: >> >> if 0 then >> print ("one") >> print ("two") >> endif >> print ("three") >> >> It still compiles, it still runs, and still shows the correct "three" as >> output. > > My point is that you *assume* that showing just "three" is the correct > behaviour. Why? Why do you automatically assume that the indentation > is wrong and the endif is correct? All you have is that the two > disagree. It's Bart's special language, so the correct behaviour is whatever he says it is :-) But more seriously, it's easy to typo an extra indent. It's harder to typo "endif" when you actually meant to type, oh, "ending = 1 if condition else 3", say. So faced with ambiguity, and the insistence that the right way to break ambiguity is to guess ("Do What I Mean, dammit!!!") the most likely guess is that the indentation is wrong. But not guaranteed. That's the thing about being ambiguous -- there is a chance that the indentation is correct. This *especially* applies to languages like C, when open/close delimiters optional if the block is only a single statement, and where the delimiters are only a single character. And sure enough, C is prone to indent/brace mismatch errors. When I design my killer language, it won't fail when there's a mismatch between indentation and open/close delimiters. Nor will it just give priority to one or the other. Instead, it will learn from your errors and typos in order to determine which you are statistically more likely to want, and silently compile the code that way. But without changing the source code, of course. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From steve+comp.lang.python at pearwood.info Mon Jan 23 23:27:02 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 24 Jan 2017 15:27:02 +1100 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886b429$0$1605$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5886d797$0$1501$c3e8da3$5496439d@news.astraweb.com> On Tuesday 24 January 2017 13:41, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 12:55 PM, Steve D'Aprano > wrote: >> Reading his post, I get the impression he's the sort of programmer who >> believes in never, ever, ever, ever re-using a variable, so he might do >> something like: >> >> raw_data = collect_raw_data() # generates 10GB of data >> data_after_step_one = massage(raw_data) >> data_after_step_two = process(data_after_step_one) >> ... >> sorted_data = sorted(data_after_step_ten) >> >> and then wonders why he's running out of memory. "That's a memory leak!" > > How is that any different in any other language? I don't understand > how even this is a criticism of Python. If you were working in Java, > say, you'd have basically the same result - keep all the stuff, keep > all the memory usage. Keep in mind that I'm only *guessing* from the impression that the author gives. I don't *know* that is what he's doing. He makes all these claims about Python, but doesn't show his code, so the best we can do is try to predict what could plausibly explain the observation. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From greg.ewing at canterbury.ac.nz Mon Jan 23 23:31:57 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 24 Jan 2017 17:31:57 +1300 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <19df8402-c187-4250-b7f8-66d7e02475c0@googlegroups.com> Message-ID: Chris Angelico wrote: > On Tue, Jan 24, 2017 at 11:55 AM, Dennis Lee Bieber > wrote: > >>On Mon, 23 Jan 2017 22:55:19 +0000, mm0fmf declaimed the >>following: >> >>>50lbs of coffee beans made into espresso and 22lbs of chocolate to eat >>>with all those tiny cups. >> >> How about just 75lbs of chocolate covered espresso beans > > Can you fire them out of a machine gun? And if you can, should you use a Bren or a Spandau? (Sorry, I've been watching too many lindybeige videos about machine guns recently. Just throw a pommel at me.) -- Greg From rosuav at gmail.com Mon Jan 23 23:41:30 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 15:41:30 +1100 Subject: How coding in Python is bad for you In-Reply-To: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jan 24, 2017 at 3:22 PM, Steven D'Aprano wrote: > But more seriously, it's easy to typo an extra indent. It's harder to typo > "endif" when you actually meant to type, oh, "ending = 1 if condition else 3", > say. So faced with ambiguity, and the insistence that the right way to break > ambiguity is to guess ("Do What I Mean, dammit!!!") the most likely guess is > that the indentation is wrong. > > But not guaranteed. That's the thing about being ambiguous -- there is a chance > that the indentation is correct. Indeed. I teach JavaScript as well as Python, and I've seen some pretty horrendous indentation flaws (examples available if people ask privately, but I will anonymize them because I'm not here to shame students) - but there have been nearly as many cases where the indentation's fine and the bracket nesting isn't. I probably sound like a broken record [1] with the number of times I've said "check your indentation", as a hint to where the missed-out close brace or parenthesis is. True, with "endif" it's a bit harder to typo, but it's still just as easy to make a copy-and-paste error and end up with code like this: if condition: statement endif statement endif What's this code meant to do? Can't know. Remember: If you have only one clock, it might be right and it might be wrong, but it's consistent. If you have two clocks and they disagree, you have no clue what the time is. ChrisA [1] Wonder how many of today's generation of programmers have actually heard a record skip... From steve+comp.lang.python at pearwood.info Tue Jan 24 00:00:30 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Tue, 24 Jan 2017 16:00:30 +1100 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5886df70$0$11127$c3e8da3@news.astraweb.com> On Tuesday 24 January 2017 15:41, Chris Angelico wrote: > Remember: If you have only one clock, it might be right and it might > be wrong, but it's consistent. If you have two clocks and they > disagree, you have no clue what the time is. During the golden age of sail, there was a saying, never go to sea with two chronometers. Take one, or three, but never two. Obviously the lesson here is that programming languages should have *three* ways of setting out the structure: if condition: { BEGIN code goes here } END > [1] Wonder how many of today's generation of programmers have actually > heard a record skip... Haven't you heard? Vinyl is making a comeback. Seriously. -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From rosuav at gmail.com Tue Jan 24 00:16:43 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 16:16:43 +1100 Subject: How coding in Python is bad for you In-Reply-To: <5886df70$0$11127$c3e8da3@news.astraweb.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <5886df70$0$11127$c3e8da3@news.astraweb.com> Message-ID: On Tue, Jan 24, 2017 at 4:00 PM, Steven D'Aprano wrote: > On Tuesday 24 January 2017 15:41, Chris Angelico wrote: > >> Remember: If you have only one clock, it might be right and it might >> be wrong, but it's consistent. If you have two clocks and they >> disagree, you have no clue what the time is. > > During the golden age of sail, there was a saying, never go to sea with two > chronometers. Take one, or three, but never two. > > Obviously the lesson here is that programming languages should have *three* > ways of setting out the structure: > > if condition: > { > BEGIN > code goes here > } > END > No no no. You have two orthogonal styles (indentation and tokens), but then you added another of the same style (another pair of tokens). You need a third orthogonal style. I suggest that each nesting level be heralded by an increase in indentation, an open brace, and a new text colour. ChrisA From bob.martin at excite.com Tue Jan 24 02:00:06 2017 From: bob.martin at excite.com (Bob Martin) Date: Tue, 24 Jan 2017 07:00:06 GMT Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: in 770207 20170124 005601 Chris Angelico wrote: >REXX has even less structure than Python - it doesn't even have >functions, just labels, so you can actually have two functions that >share a common tail. And yes, you can abuse that horrendously to >create unreadable code. Is REXX a bad language because of that? No. >You can use structure badly, and you can use freedom badly. Of course Rexx has functions! From rosuav at gmail.com Tue Jan 24 02:08:53 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 18:08:53 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: On Tue, Jan 24, 2017 at 6:00 PM, Bob Martin wrote: > in 770207 20170124 005601 Chris Angelico wrote: > >>REXX has even less structure than Python - it doesn't even have >>functions, just labels, so you can actually have two functions that >>share a common tail. And yes, you can abuse that horrendously to >>create unreadable code. Is REXX a bad language because of that? No. >>You can use structure badly, and you can use freedom badly. > > Of course Rexx has functions! Built-ins, yes, but when you define your own, it's by giving a label. It isn't a function until it's called as a function. ChrisA From rryan.asu at gmail.com Tue Jan 24 02:09:09 2017 From: rryan.asu at gmail.com (rryan.asu at gmail.com) Date: Mon, 23 Jan 2017 23:09:09 -0800 (PST) Subject: PhotoImage.paste Message-ID: I'm trying to build a tkinter GUI with python 3.5, and would like to interactively adjust the color palette for an image by moving the mouse in the canvas using PIL. In pseudo-code, I have something like palette=color_map(x,y) # x,y are scalars indicating the position of the mouse in the Canvas image.putpalette(palette) photo.paste(image) This works just fine, but is very slow. The photo.paste(image) step takes about 0.15 s (using an image that is 4k x 4k). I read the manual on effbot, and they caution that the paste method would be slow if the image is display --- and I'm guessing that's what I'm doing. So my question is, what alternatives do I have to speed that step up. Thank you for any thoughts you might have... -Russell From auriocus at gmx.de Tue Jan 24 02:46:06 2017 From: auriocus at gmx.de (Christian Gollwitzer) Date: Tue, 24 Jan 2017 08:46:06 +0100 Subject: How coding in Python is bad for you In-Reply-To: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: Am 23.01.17 um 18:24 schrieb breamoreboy at gmail.com: > The article is here http://lenkaspace.net/index.php/blog/show/111 > I don't agree with it (unsurprisingly), even though Python is not my favourite language in itself, but a viable compromise for many circumstances. Below is a point-by-point reply. (BTW, Lenka Pitonakova is a lady of Slova origin, so we should refer to her as "she") 1. Having the code in shoehorned into a given structure is no guarantee for clean code. Look at messy Java inheritance diagrams with hundreds of Factory objects all creating themselves around the code that solves the real problem 2. It is true that the prototype of a function can be more descriptive, if it includes type information (such as "list", "dict" etc.). See https://www.python.org/dev/peps/pep-0484/ The documentation is needed in any case to understand, what the function does. Mandatory types do not prevent bad naming of the arguments. 3. Significant indentation has been discussed over and over, with no clear result. Humans need indentation to identify structure in a large chunk of code. Brace language programmers use auto-indenters to do that, Python uses the layout to deduce the code blocks. Both can fail; most notably, missing braces in if clauses lead to several security bugs in the past, while white space can be messed up more easily. So that's a standoff. 4. It's true that a strict compiler can find errors earlier. Most notably, Haskell detects many errors during compile time. Linters exist for that purpose in interpreted languages. Still no compiler can actually catch semantic errors, which means that automated tests are needed anyway. 5. This is wrong. Python does have a garbage collector, but it uses reference counting (CPython) - so in simple cases you know exactly how to release the memory held by big objects. Unless you use a flawed library. 6. There are even more bad libraries for C and Java. Writing interfaces to libraries that do not fit together is actually a much greater pain in these languages. 7. This is actually the same argument as 6. Bad programmers wrote some code, which you want to use. That is independent of the language, maybe in a more strict language the author wouldn't bother to give you the code at all! Scientific software was always horrible, previously people used impenetrable FORTRAN 77, thankfully they use Python now. Lack of encapsulation is a featurenot a bug. Christian From dieter at handshake.de Tue Jan 24 04:59:20 2017 From: dieter at handshake.de (dieter) Date: Tue, 24 Jan 2017 10:59:20 +0100 Subject: Is it possible to get the Physical memory address of a variable in python? References: Message-ID: <877f5ksrrb.fsf@handshake.de> Sourabh Kalal writes: > how we can access the value from using id.. > like x=10 > id(x) > 3235346364 > > how i can read value 10 using id 3235346364 You should not do this (read the value instead by looking at "x") -- unless you are debugging at "C" level. For "C" level debugging, there is a set of "gdb" macros allowing you to display Python objects giving their "C" level address. From bc at freeuk.com Tue Jan 24 06:52:51 2017 From: bc at freeuk.com (BartC) Date: Tue, 24 Jan 2017 11:52:51 +0000 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 24/01/2017 04:41, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 3:22 PM, Steven D'Aprano > wrote: >> But more seriously, it's easy to typo an extra indent. It's harder to typo >> "endif" when you actually meant to type, oh, "ending = 1 if condition else 3", >> say. So faced with ambiguity, and the insistence that the right way to break >> ambiguity is to guess ("Do What I Mean, dammit!!!") the most likely guess is >> that the indentation is wrong. >> >> But not guaranteed. That's the thing about being ambiguous -- there is a chance >> that the indentation is correct. > > Indeed. I teach JavaScript as well as Python, and I've seen some > pretty horrendous indentation flaws (examples available if people ask > privately, but I will anonymize them because I'm not here to shame > students) - but there have been nearly as many cases where the > indentation's fine and the bracket nesting isn't. I probably sound > like a broken record [1] with the number of times I've said "check > your indentation", as a hint to where the missed-out close brace or > parenthesis is. True, with "endif" it's a bit harder to typo, but it's > still just as easy to make a copy-and-paste error and end up with code > like this: > > if condition: > statement > endif > statement > endif > > What's this code meant to do? Can't know. But whatever it does, a language that enforces 'endif' would report an error, so requiring further investigation. Without the 'endifs', it would look like this: if condition: statement statement Legal python. Presumably you don't like parentheses in function calls for the same reasons; you'd prefer 'f a,b,c' rather than 'f(a,b,c)' in case, with copy&paste, someone might end up with: 'f(a,b,c),d)'. Having 'f a,b,c,d' is better; much less chance of those pesky syntax errors! > Remember: If you have only one clock, it might be right and it might > be wrong, but it's consistent. If you have two clocks and they > disagree, you have no clue what the time is. I've actually got three wall clocks. Usually only one will disagree with the other two, meaning it needs a new battery. But don't they use such systems in avionics? -- Bartc From rosuav at gmail.com Tue Jan 24 06:58:55 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 24 Jan 2017 22:58:55 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jan 24, 2017 at 10:52 PM, BartC wrote: >> Remember: If you have only one clock, it might be right and it might >> be wrong, but it's consistent. If you have two clocks and they >> disagree, you have no clue what the time is. > > > I've actually got three wall clocks. Usually only one will disagree with the > other two, meaning it needs a new battery. But don't they use such systems > in avionics? See what Steve said about 1 or 3 but never 2. Triple redundancy gives you a chance to figure out that two of them agree. ChrisA From bc at freeuk.com Tue Jan 24 07:12:05 2017 From: bc at freeuk.com (BartC) Date: Tue, 24 Jan 2017 12:12:05 +0000 Subject: How coding in Python is bad for you In-Reply-To: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 24/01/2017 04:22, Steven D'Aprano wrote: > On Tuesday 24 January 2017 13:38, Chris Angelico wrote: >> On Tue, Jan 24, 2017 at 12:47 PM, BartC wrote: >>> if 0 then >>> print ("one") >>> print ("two") >>> endif >> My point is that you *assume* that showing just "three" is the correct >> behaviour. Why? Why do you automatically assume that the indentation >> is wrong and the endif is correct? All you have is that the two >> disagree. > > It's Bart's special language, so the correct behaviour is whatever he says it > is :-) I chose 'if...endif' above because it's used in a number of well-known languages. (Including PHP I think, while others use 'end if' or just 'end'. In my own language I prefer 'if...fi') > This *especially* applies to languages like C, when open/close delimiters > optional if the block is only a single statement, and where the delimiters are > only a single character. > > And sure enough, C is prone to indent/brace mismatch errors. I didn't use brace syntax in the example for that reason. C has plenty of its own problems. Although funnily enough, its preprocessor language uses '#if...#endif'; much more sensible! C suffers also from the 'dangling else' problem, which a 'if...endif' syntax doesn't. Nor does Python's scheme, as the 'else' /has/ to be aligned with the corresponding 'if' or 'elif'. However... take this code: if a: b if c: d You want to import an 'else' block from elsewhere to add to that nested if: if a: b if c: d else: e The trouble is, that other code was not nested; it will need extra indentation here. Until that happens however, the above is still legal. -- Bartc From bc at freeuk.com Tue Jan 24 07:19:16 2017 From: bc at freeuk.com (BartC) Date: Tue, 24 Jan 2017 12:19:16 +0000 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 24/01/2017 11:58, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 10:52 PM, BartC wrote: >>> Remember: If you have only one clock, it might be right and it might >>> be wrong, but it's consistent. If you have two clocks and they >>> disagree, you have no clue what the time is. >> >> >> I've actually got three wall clocks. Usually only one will disagree with the >> other two, meaning it needs a new battery. But don't they use such systems >> in avionics? > > See what Steve said about 1 or 3 but never 2. Triple redundancy gives > you a chance to figure out that two of them agree. But even with two clocks you will know something is amiss if they don't agree, and will seek confirmation from a third. With one clock you can't be sure, and could end up missing your flight or whatever. (Of course, when the clocks go forward in the spring, all the (cheap, non-automatic) clocks in my house will show the same incorrect time!) -- Bartc From skip.montanaro at gmail.com Tue Jan 24 07:20:43 2017 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Tue, 24 Jan 2017 06:20:43 -0600 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: I have nothing to add to the discussion other than too note that Gmail marks many of the messages as spam. :-) Skip From python at mrabarnett.plus.com Tue Jan 24 08:03:24 2017 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 24 Jan 2017 13:03:24 +0000 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: <36cce4ec-46bf-6816-0f16-02b28309f69f@mrabarnett.plus.com> On 2017-01-24 12:12, BartC wrote: > On 24/01/2017 04:22, Steven D'Aprano wrote: >> On Tuesday 24 January 2017 13:38, Chris Angelico wrote: >>> On Tue, Jan 24, 2017 at 12:47 PM, BartC wrote: > >>>> if 0 then >>>> print ("one") >>>> print ("two") >>>> endif > >>> My point is that you *assume* that showing just "three" is the correct >>> behaviour. Why? Why do you automatically assume that the indentation >>> is wrong and the endif is correct? All you have is that the two >>> disagree. >> >> It's Bart's special language, so the correct behaviour is whatever he says it >> is :-) > > I chose 'if...endif' above because it's used in a number of well-known > languages. (Including PHP I think, while others use 'end if' or just > 'end'. In my own language I prefer 'if...fi') > [snip] PHP lets you use if...endif or braces. From python at mrabarnett.plus.com Tue Jan 24 08:08:34 2017 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 24 Jan 2017 13:08:34 +0000 Subject: PhotoImage.paste In-Reply-To: References: Message-ID: <2c6196b4-89ac-6111-cb13-5062bf538297@mrabarnett.plus.com> On 2017-01-24 07:09, rryan.asu at gmail.com wrote: > I'm trying to build a tkinter GUI with python 3.5, and would like to interactively adjust the color palette for an image by moving the mouse in the canvas using PIL. In pseudo-code, I have something like > > palette=color_map(x,y) # x,y are scalars indicating the position of the mouse in the Canvas > image.putpalette(palette) > photo.paste(image) > > This works just fine, but is very slow. The photo.paste(image) step takes about 0.15 s (using an image that is 4k x 4k). I read the manual on effbot, and they caution that the paste method would be slow if the image is display --- and I'm guessing that's what I'm doing. So my question is, what alternatives do I have to speed that step up. > > Thank you for any thoughts you might have... > -Russell > How often are you pasting? Are you pasting whenever the mouse moves, even a little? If that's the case, try using the .after method to paste only occasionally, and then only if the mouse has moved since the last time. From alister.ware at ntlworld.com Tue Jan 24 08:47:50 2017 From: alister.ware at ntlworld.com (alister) Date: Tue, 24 Jan 2017 13:47:50 GMT Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <19df8402-c187-4250-b7f8-66d7e02475c0@googlegroups.com> Message-ID: On Tue, 24 Jan 2017 08:11:02 +1100, Chris Angelico wrote: > On Tue, Jan 24, 2017 at 8:04 AM, Adam M > wrote: >> On Monday, January 23, 2017 at 3:41:17 PM UTC-5, Jon Ribbens wrote: >>> On 2017-01-23, alister wrote: >>> > On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: >>> >> I believe that's "bad for you" in the sense that chocolate is bad >>> >> for you. >>> >> >>> >> It isn't. >>> > >>> > chocolate is a poison (lethal dose for a human approx 22lb) >>> >>> That's a meaningless statement. *Everything* is a poison in sufficient >>> quantities. >> >> I think you need to calibrate your sarcasm filter ;-). By the way >> coffee is also dangerous - especially in 50lbs bags (when it hits >> you). > > Or with sufficient velocity. The LD50 of lead is about 450mg/kg [1], > which means that a typical 70kg human could ingest about 31 grams of > lead and have a fifty-fifty chance of survival. But just a few grams of > lead at the speed of sound will probably kill you. [2] if you were to take the statistics of WWI or WWII (And probably any other major war & compare the number of rounds fired to the number of people killed by gunshot then you could say it probably wouldn't ;-) (No I am not willing to put this to the test) > > ChrisA > > [1] http://whs.rocklinusd.org/documents/Science/Lethal_Dose_Table.pdf > [2] https://xkcd.com/444/ -- Given a choice between grief and nothing, I'd choose grief. -- William Faulkner From alister.ware at ntlworld.com Tue Jan 24 09:26:03 2017 From: alister.ware at ntlworld.com (alister) Date: Tue, 24 Jan 2017 14:26:03 GMT Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <%tJhA.371507$1y4.144483@fx20.am4> On Mon, 23 Jan 2017 20:39:26 +0000, Jon Ribbens wrote: > On 2017-01-23, alister wrote: >> On Tue, 24 Jan 2017 07:19:42 +1100, Chris Angelico wrote: >>> I believe that's "bad for you" in the sense that chocolate is bad for >>> you. >>> >>> It isn't. >> >> chocolate is a poison (lethal dose for a human approx 22lb) > > That's a meaningless statement. *Everything* is a poison in sufficient > quantities. indees when I here someone saying "I won't have any xyz because they have heard that too much is bad for them" I invariably inform them that too much oxygen is poisonous & challenge then to cut that out completely :-) . -- Peace is much more precious than a piece of land... let there be no more wars. -- Mohammed Anwar Sadat, 1918-1981 From jon+usenet at unequivocal.eu Tue Jan 24 09:28:55 2017 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Tue, 24 Jan 2017 14:28:55 -0000 (UTC) Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <%tJhA.371507$1y4.144483@fx20.am4> Message-ID: On 2017-01-24, alister wrote: > On Mon, 23 Jan 2017 20:39:26 +0000, Jon Ribbens wrote: >> That's a meaningless statement. *Everything* is a poison in sufficient >> quantities. > > indees when I here someone saying "I won't have any xyz because they have > heard that too much is bad for them" I invariably inform them that too > much oxygen is poisonous & challenge then to cut that out completely :-) Just wait until they find out that raw fruit and vegetables contain hundreds of chemicals! From rryan.asu at gmail.com Tue Jan 24 10:15:22 2017 From: rryan.asu at gmail.com (rryan.asu at gmail.com) Date: Tue, 24 Jan 2017 07:15:22 -0800 (PST) Subject: PhotoImage.paste In-Reply-To: References: <2c6196b4-89ac-6111-cb13-5062bf538297@mrabarnett.plus.com> Message-ID: Hi MRAB Yes, I am pasting every time the mouse moves (or every time the tk event handler gets called). I thought about the after method, and I guess i can try to implement that. It seems like that would help in the "jerkiness" of the GUI's response, but it leaves me kinda disappointed. I obviously do not understand the inner workings of PIL or tk, but it seems kinda broken that it must take 0.2 sec to adjust a palette. This GUI is a rewrite of a code I had in another language, and this palette modifying was much faster. I'll try the after method, hoping it'll at least make the GUI a bit more responsive. Thanks again! Russell On Tuesday, January 24, 2017 at 8:08:53 AM UTC-5, MRAB wrote: > On 2017-01-24 07:09, at gmail.com wrote: > > I'm trying to build a tkinter GUI with python 3.5, and would like to interactively adjust the color palette for an image by moving the mouse in the canvas using PIL. In pseudo-code, I have something like > > > > palette=color_map(x,y) # x,y are scalars indicating the position of the mouse in the Canvas > > image.putpalette(palette) > > photo.paste(image) > > > > This works just fine, but is very slow. The photo.paste(image) step takes about 0.15 s (using an image that is 4k x 4k). I read the manual on effbot, and they caution that the paste method would be slow if the image is display --- and I'm guessing that's what I'm doing. So my question is, what alternatives do I have to speed that step up. > > > > Thank you for any thoughts you might have... > > -Russell > > > How often are you pasting? > > Are you pasting whenever the mouse moves, even a little? > > If that's the case, try using the .after method to paste only > occasionally, and then only if the mouse has moved since the last time. From ben.usenet at bsb.me.uk Tue Jan 24 10:51:57 2017 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Tue, 24 Jan 2017 15:51:57 +0000 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: <874m0otq02.fsf@bsb.me.uk> Chris Angelico writes: > ... I teach JavaScript as well as Python, and I've seen some > pretty horrendous indentation flaws (examples available if people ask > privately, but I will anonymize them because I'm not here to shame > students) - but there have been nearly as many cases where the > indentation's fine and the bracket nesting isn't. Can I ask what editor(s) your students have available? I ask because I've not given a moment's thought to indentation or what bracket matches what for decades due to having a helpful editor. -- Ben. From rosuav at gmail.com Tue Jan 24 10:58:14 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 25 Jan 2017 02:58:14 +1100 Subject: How coding in Python is bad for you In-Reply-To: <874m0otq02.fsf@bsb.me.uk> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> Message-ID: On Wed, Jan 25, 2017 at 2:51 AM, Ben Bacarisse wrote: > Chris Angelico writes: > >> ... I teach JavaScript as well as Python, and I've seen some >> pretty horrendous indentation flaws (examples available if people ask >> privately, but I will anonymize them because I'm not here to shame >> students) - but there have been nearly as many cases where the >> indentation's fine and the bracket nesting isn't. > > Can I ask what editor(s) your students have available? I ask because > I've not given a moment's thought to indentation or what bracket matches > what for decades due to having a helpful editor. > Quite a few, depending on which platform they're using. I'd say Sublime is the most common choice. ChrisA From alister.ware at ntlworld.com Tue Jan 24 11:03:45 2017 From: alister.ware at ntlworld.com (alister) Date: Tue, 24 Jan 2017 16:03:45 GMT Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <%tJhA.371507$1y4.144483@fx20.am4> Message-ID: On Tue, 24 Jan 2017 14:28:55 +0000, Jon Ribbens wrote: > On 2017-01-24, alister wrote: >> On Mon, 23 Jan 2017 20:39:26 +0000, Jon Ribbens wrote: >>> That's a meaningless statement. *Everything* is a poison in sufficient >>> quantities. >> >> indees when I here someone saying "I won't have any xyz because they >> have heard that too much is bad for them" I invariably inform them that >> too much oxygen is poisonous & challenge then to cut that out >> completely :-) > > Just wait until they find out that raw fruit and vegetables contain > hundreds of chemicals! then send them of to one of the sitre relating to Di-Hydrogen Monoxide... I know of one organic food producer that has even tried to deny the use the stuff ;-) -- AP/STT. Helsinki, Dec 5th, 6:22 AM. For immediate release. In order to allay fears about the continuity of the Linux project, Linus Torvalds together with his manager Tove Monni have released "Linus v2.0", affectionately known as "Kernel Hacker - The Next Generation". Linux stock prices on Wall Street rose sharply after the announcement; as one well-known analyst who wishes to remain anonymous says - "It shows a long-term commitment, and while we expect a short-term decrease in productivity, we feel that this solidifies the development in the long run". Other analysts downplay the importance of the event, and claim that just about anybody could have done it. "I'm glad somebody finally told them about the birds and the bees" one sceptic comments cryptically. But even the skeptics agree that it is an interesting turn of events. Others bring up other issues with the new version - "I'm especially intrigued by the fact that the new version is female, and look forward to seeing what the impact of that will be on future development. Will "Red Hat Linux" change to "Pink Hat Linux", for example?" -- Linus Torvalds announcing that he became father of a girl From ben.usenet at bsb.me.uk Tue Jan 24 11:21:37 2017 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Tue, 24 Jan 2017 16:21:37 +0000 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> Message-ID: <87sho8sa26.fsf@bsb.me.uk> Chris Angelico writes: > On Wed, Jan 25, 2017 at 2:51 AM, Ben Bacarisse wrote: >> Chris Angelico writes: >> >>> ... I teach JavaScript as well as Python, and I've seen some >>> pretty horrendous indentation flaws (examples available if people ask >>> privately, but I will anonymize them because I'm not here to shame >>> students) - but there have been nearly as many cases where the >>> indentation's fine and the bracket nesting isn't. >> >> Can I ask what editor(s) your students have available? I ask because >> I've not given a moment's thought to indentation or what bracket matches >> what for decades due to having a helpful editor. >> > > Quite a few, depending on which platform they're using. I'd say > Sublime is the most common choice. So what's going in your opinion? The errors are in the group who don't use the right tools or what? -- Ben. From steve+python at pearwood.info Tue Jan 24 11:35:56 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 25 Jan 2017 03:35:56 +1100 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> <87sho8sa26.fsf@bsb.me.uk> Message-ID: <5887826d$0$1621$c3e8da3$5496439d@news.astraweb.com> On Wed, 25 Jan 2017 03:21 am, Ben Bacarisse wrote: > Chris Angelico writes: > >> On Wed, Jan 25, 2017 at 2:51 AM, Ben Bacarisse >> wrote: [...] >>> Can I ask what editor(s) your students have available? I ask because >>> I've not given a moment's thought to indentation or what bracket matches >>> what for decades due to having a helpful editor. >>> >> >> Quite a few, depending on which platform they're using. I'd say >> Sublime is the most common choice. > > So what's going in your opinion? The errors are in the group who don't > use the right tools or what? If braces can be inserted automatically by the compiler, then they are useless and should be left out of the language. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From bc at freeuk.com Tue Jan 24 11:37:52 2017 From: bc at freeuk.com (BartC) Date: Tue, 24 Jan 2017 16:37:52 +0000 Subject: How coding in Python is bad for you In-Reply-To: <874m0otq02.fsf@bsb.me.uk> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> Message-ID: On 24/01/2017 15:51, Ben Bacarisse wrote: > Chris Angelico writes: > >> ... I teach JavaScript as well as Python, and I've seen some >> pretty horrendous indentation flaws (examples available if people ask >> privately, but I will anonymize them because I'm not here to shame >> students) - but there have been nearly as many cases where the >> indentation's fine and the bracket nesting isn't. > > Can I ask what editor(s) your students have available? I ask because > I've not given a moment's thought to indentation or what bracket matches > what for decades due to having a helpful editor. How would your editor detect the error in the example I gave earlier? (Where a tab has been inadvertently removed - or added - but the result is still valid Python code.) -- Bartc From grant.b.edwards at gmail.com Tue Jan 24 11:53:50 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 24 Jan 2017 16:53:50 +0000 (UTC) Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <5886df70$0$11127$c3e8da3@news.astraweb.com> Message-ID: On 2017-01-24, Chris Angelico wrote: > No no no. You have two orthogonal styles (indentation and tokens), but > then you added another of the same style (another pair of tokens). You > need a third orthogonal style. I suggest that each nesting level be > heralded by an increase in indentation, an open brace, and a new text > colour. Bravo! Actually, some news/mail readers pretty much do that with quote levels, and it works pretty well. Assuming you're not color blind, don't care about hardcopy on monochrome printers, don't care about screenreaders, don't use monochrome monitors, etc. -- Grant Edwards grant.b.edwards Yow! I want the presidency at so bad I can already taste gmail.com the hors d'oeuvres. From __peter__ at web.de Tue Jan 24 11:59:05 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 24 Jan 2017 17:59:05 +0100 Subject: PhotoImage.paste References: Message-ID: rryan.asu at gmail.com wrote: > I'm trying to build a tkinter GUI with python 3.5, and would like to > interactively adjust the color palette for an image by moving the mouse in > the canvas using PIL. In pseudo-code, I have something like > > palette=color_map(x,y) # x,y are scalars indicating the position of the > mouse in the Canvas image.putpalette(palette) > photo.paste(image) > > This works just fine, but is very slow. The photo.paste(image) step takes > about 0.15 s (using an image that is 4k x 4k). I read the manual on > effbot, and they caution that the paste method would be slow if the image > is display --- and I'm guessing that's what I'm doing. So you have read """ Note: Pasting into an RGBA image that is displayed is a very slow operation. It?s usually better to create a new image object. """ on . > So my question is, what alternatives do I have to speed that step up. Did you try to create a new image, as the note suggests? > Thank you for any thoughts you might have... > -Russell From steve+python at pearwood.info Tue Jan 24 12:07:57 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 25 Jan 2017 04:07:57 +1100 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> Message-ID: <588789ef$0$1583$c3e8da3$5496439d@news.astraweb.com> On Tue, 24 Jan 2017 10:52 pm, BartC wrote: >> if condition: >> statement >> endif >> statement >> endif >> >> What's this code meant to do? Can't know. > > But whatever it does, a language that enforces 'endif' would report an > error, so requiring further investigation. Without the 'endifs', it > would look like this: > > if condition: > statement > statement > > Legal python. Indeed. And 99.99% of the time it is correct Python as well. I suspect that the use of redundant braces or end delimiters has something to do with attitudes to risk. Or even, dare I say it, with confidence in your own programming skill. If (generic) you are risk adverse, or have low confidence in your ability, then you might want all the help you can get from the compiler and insist on having end delimiter for blocks. Think about how rare it is that it will really matter: - you're editing an if block; - and you do something that changes the block structure; - but you do it wrong; - and the failure is something which is still syntactically legal; - but you don't notice; - and the failure isn't instantly picked up by your tests; - or you don't have tests which exercise that part of the code. That's fairly unusually. How often does it happen? If you're a beginner, or programming while high/drunk/sick/distracted, it might happen a lot. (I've seen programmers who are seemingly incapable of writing `y = x + 1` without six syntax errors and two logic errors.) But, I think, the average half-decent coder should find that this isn't really a problem more than, oh, once or twice a year. Or less. By "a problem" I mean either a bug sneaks through into production, or they find the bug but it takes more than an hour's debugging. So if you're risk adverse, you're thinking about that wasted hour, or the possibility of a bug sneaking into production code. (As if they don't already, B&D compilers or not.) But if you're more tolerant of risk (at least when it comes to coding), or have more confidence in your ability, you might think: - these bugs are rare; - an hour isn't a long time to spend debugging; - besides it probably won't be an hour; - it's more likely to be five minutes with a debugger; - and what's the worst that can happen? this isn't a nuclear power plant, we'll fix the bug in the next release. It's not that we don't see the benefit of those braces or end delimiters. But we just don't think they're important enough to carry them around all the time. Most of my `if` blocks are short, say, two or three lines. Adding an "endif" after the block increases them by 33% or 50%. Multiply by the hundreds, thousands, perhaps tens of thousands of block statements I'll ever write or read, and that's a huge overhead for very little benefit. And the next time I see a piece of code that ends: end end end end end the author is going to get a kickin'. So for some of us, endif or however you want to spell it, is just noise, eating up valuable vertical real estate. We recognise that it is *theoretically* useful, but the *actual* usefulness in practice is so rare that it really doesn't matter enough to make it worth while. There's no right or wrong answer here. Once you get past the actual bug magnets like C with its optional braces problem, and dangling elses, or equivalent, the choice becomes one of taste. There's a million brace-style languages out there for those who want them, and only a handful like Python. Why can't you folks leave Python the way we want it, instead of insisting it's wrong? > Presumably you don't like parentheses in function calls for the same > reasons; you'd prefer 'f a,b,c' rather than 'f(a,b,c)' in case, with > copy&paste, someone might end up with: 'f(a,b,c),d)'. No, because f a, b, c is ambigious. spam(a, b, eggs(c, d), e) would become in your system: spam a, b, eggs c, d, e and there's no way to tell which arguments are passed to eggs(). (Come on Bart, you've been designing languages for decades. You know this.) -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From rosuav at gmail.com Tue Jan 24 12:30:11 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 25 Jan 2017 04:30:11 +1100 Subject: How coding in Python is bad for you In-Reply-To: <87sho8sa26.fsf@bsb.me.uk> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> <87sho8sa26.fsf@bsb.me.uk> Message-ID: On Wed, Jan 25, 2017 at 3:21 AM, Ben Bacarisse wrote: > Chris Angelico writes: > >> On Wed, Jan 25, 2017 at 2:51 AM, Ben Bacarisse wrote: >>> Chris Angelico writes: >>> >>>> ... I teach JavaScript as well as Python, and I've seen some >>>> pretty horrendous indentation flaws (examples available if people ask >>>> privately, but I will anonymize them because I'm not here to shame >>>> students) - but there have been nearly as many cases where the >>>> indentation's fine and the bracket nesting isn't. >>> >>> Can I ask what editor(s) your students have available? I ask because >>> I've not given a moment's thought to indentation or what bracket matches >>> what for decades due to having a helpful editor. >>> >> >> Quite a few, depending on which platform they're using. I'd say >> Sublime is the most common choice. > > So what's going in your opinion? The errors are in the group who don't > use the right tools or what? In my opinion, what's going on is that exploring code is a messy process, and that the more little finicky things you have to get right, the more likely you'll get something wrong. Which one you get wrong is pretty much arbitrary. Getting indentation wrong in JS won't affect the interpreter, but it majorly affects the human. Which one would you rather confuse? ChrisA From bc at freeuk.com Tue Jan 24 12:50:56 2017 From: bc at freeuk.com (BartC) Date: Tue, 24 Jan 2017 17:50:56 +0000 Subject: How coding in Python is bad for you In-Reply-To: <588789ef$0$1583$c3e8da3$5496439d@news.astraweb.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <588789ef$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 24/01/2017 17:07, Steve D'Aprano wrote: > On Tue, 24 Jan 2017 10:52 pm, BartC wrote: > >>> if condition: >>> statement >>> endif >>> statement >>> endif >>> >>> What's this code meant to do? Can't know. >> >> But whatever it does, a language that enforces 'endif' would report an >> error, so requiring further investigation. Without the 'endifs', it >> would look like this: >> >> if condition: >> statement >> statement >> >> Legal python. > > Indeed. > And 99.99% of the time it is correct Python as well. You don't know that. If this has been pasted from elsewhere, you need to match up the indentation level with the current code. > I suspect that the use of redundant braces or end delimiters has something > to do with attitudes to risk. Or even, dare I say it, with confidence in > your own programming skill. (I'm in the early stages of implementing a C compiler at the moment. The test input is a single 750Kloc source file (900Kloc when 400 include files are taken account of). If I've accidentally lost a space or tab while messing about with it, and it's significant, I would rather the compiler reported it! As I'm not going to spot it by perusing the 15,000 pages of code.) > It's not that we don't see the benefit of those braces or end delimiters. > But we just don't think they're important enough to carry them around all > the time. I don't use Python enough for it to be a problem, yet it /has/ been a problem. I paste code from elsewhere into my editor; usually the indentation is a mess of spaces, and usually the whole thing is indented if the imported from a post here. But if you now try to manually fix the indentation, invariably there comes a point where you don't know if a particular line has the new indentation, or the old. The original structure is temporarily lost. Possibly permanently; you may have to refer to the original. Or, worse, you don't realise there is a mistake in the new indentation and the structure has been subtly altered. This means that a straightforward tweak to a bit a source code is so error-prone that you are obliged to use automatic tools to do the job. Most of my `if` blocks are short, say, two or three lines. Adding > an "endif" after the block increases them by 33% or 50%. Multiply by the > hundreds, thousands, perhaps tens of thousands of block statements I'll > ever write or read, and that's a huge overhead for very little benefit. > > And the next time I see a piece of code that ends: > > end > end > end > end > end > > the author is going to get a kickin'. Yes, but for using too much nesting! Sometimes all these 'ends' are a nuisance, but IMO they are valuable, especially if they appear like this: end if end for as they give extra information. They can also inject extra vertical space which can be useful (although your example gives too much). But they also TELL YOU that you are at the end of a set of blocks! In Python, the above wouldn't appear at all; if you're lucky, you'll get one blank line. Whether that signals the end of all five blocks, or three, or none, depends on what comes next: you have to scroll further and infer from that which of the blocks have terminated. (I've mentioned in another thread the movie Monty Python and the Holy Grail, which doesn't have normal closing credits, just a black screen being projected. Eventually the audience realise there is nothing more to see... Just having 'The End' appear wouldn't be as quirky but it would save some time!) > There's a million brace-style languages out there for those who want them, > and only a handful like Python. Why can't you folks leave Python the way we > want it, instead of insisting it's wrong? Nothing's going to happen to Python and its unique block style. I'm just pointing out some of the flaws. -- Bartc From pkpearson at nowhere.invalid Tue Jan 24 13:02:30 2017 From: pkpearson at nowhere.invalid (Peter Pearson) Date: 24 Jan 2017 18:02:30 GMT Subject: String Replacement References: Message-ID: On Mon, 23 Jan 2017 13:23:38 -0800 (PST), subhabangalore at gmail.com wrote: > I have a string like > > "Trump is $ the president of USA % Obama was $ the president > of USA % Putin is $ the premier of Russia%" > > Here, I want to extract the portions from $...%, which would be > > "the president of USA", > "the president of USA", > "the premier of Russia" [snip] This might get you started: $ cat temp.py import re x = ("Trump is $ the president of USA % Obama was $ the pres" "ident of USA % Putin is $ the premier of Russia%") for y in re.finditer(r"(?P[^\$]*)\$(?P[^%]*)%", x): print("'{0}' -- '{1}'".format(y.group("first"), y.group("second"))) $ python3 temp.py 'Trump is ' -- ' the president of USA ' ' Obama was ' -- ' the president of USA ' ' Putin is ' -- ' the premier of Russia' -- To email me, substitute nowhere->runbox, invalid->com. From ben.usenet at bsb.me.uk Tue Jan 24 14:21:08 2017 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Tue, 24 Jan 2017 19:21:08 +0000 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> <87sho8sa26.fsf@bsb.me.uk> <5887826d$0$1621$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87mvegs1qz.fsf@bsb.me.uk> Steve D'Aprano writes: > On Wed, 25 Jan 2017 03:21 am, Ben Bacarisse wrote: > >> Chris Angelico writes: >> >>> On Wed, Jan 25, 2017 at 2:51 AM, Ben Bacarisse >>> wrote: > [...] >>>> Can I ask what editor(s) your students have available? I ask because >>>> I've not given a moment's thought to indentation or what bracket matches >>>> what for decades due to having a helpful editor. >>>> >>> >>> Quite a few, depending on which platform they're using. I'd say >>> Sublime is the most common choice. >> >> So what's going in your opinion? The errors are in the group who don't >> use the right tools or what? > > If braces can be inserted automatically by the compiler, then they are > useless and should be left out of the language. I'm, not sure I agree but that would be a whole new topic. I'm talking about tools (editors specifically) that help the author to write what they intend to write. -- Ben. From ben.usenet at bsb.me.uk Tue Jan 24 14:31:18 2017 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Tue, 24 Jan 2017 19:31:18 +0000 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> Message-ID: <87h94os1a1.fsf@bsb.me.uk> BartC writes: > On 24/01/2017 15:51, Ben Bacarisse wrote: >> Chris Angelico writes: >> >>> ... I teach JavaScript as well as Python, and I've seen some >>> pretty horrendous indentation flaws (examples available if people ask >>> privately, but I will anonymize them because I'm not here to shame >>> students) - but there have been nearly as many cases where the >>> indentation's fine and the bracket nesting isn't. >> >> Can I ask what editor(s) your students have available? I ask because >> I've not given a moment's thought to indentation or what bracket matches >> what for decades due to having a helpful editor. > > How would your editor detect the error in the example I gave earlier? I'm not talking about detecting errors -- that's for the programmer -- but the editor can help the programmer to be sure they wrote what they meant by doing things like matching brackets and auto-indenting code in {}s. (I'm replying to a post about JavaScript which has {}s.) The trouble is that I've been programming for so long that I can't remember what it's like to make block and/or indent errors. Obviously I make typos but they don't survive more than a few seconds. I hoped that Chris's students would provide an insight into how the tools do or don't help. > (Where a tab has been inadvertently removed - or added - but the >result is still valid Python code.) In Python the editor could, for example, highlight the block you are typing in, so as soon as you leave the body of the 'if' it would stop being marked and the containing code would be highlighted. Just moving the cursor up and down would show you what block everything is in. I don't know if any editors help like this -- that's part of my reason to ask. -- Ben. From rryan.asu at gmail.com Tue Jan 24 14:40:14 2017 From: rryan.asu at gmail.com (rryan.asu at gmail.com) Date: Tue, 24 Jan 2017 11:40:14 -0800 (PST) Subject: PhotoImage.paste In-Reply-To: References: Message-ID: <609e413c-a2aa-4c63-8f60-125bb8c589ab@googlegroups.com> Hi Peter, Yes, that was the first thing I did, even before using the paste method. I read the warning on effbot and other sites, and simply coded it up as new_image.putpalette(palette) photo=ImageTk.PhotoImage(image=new_image) canvas_object=canvas.create_iamge(x,y,image=photo) And this was a small bit faster, but still quite slow (say 0.1 s). So just to understand the warning on effbot, I tried using paste, where'd I just do something like image.putpalette(palette) photo.paste(image) and that took about 0.15 s. But my canvas has a few other things displayed on it, so each time that you create a new image/canvas object, I still have to run the lift method to order the items appropriately. So all in all, the new image method wasn't that much better --- and ultimately still too slow for my interest. It seems that for some reason PIL just has this a limitation, which is really sad. I think I can skirt the issue by when you start the mouse action, grab the bounding box from the view and only apply the new palette math to that subregion. Then when the mouse action is finished, load the palette to the entire image with paste, and accept the time is 0.15 sec --- but this is not done in "real time". This is will be a substantial amount of coding and quite ugly. I'm just surprised that this is so clunky. Thanks for the suggestion. Russell On Tuesday, January 24, 2017 at 11:59:49 AM UTC-5, Peter Otten wrote: >@gmail.com wrote: > > > I'm trying to build a tkinter GUI with python 3.5, and would like to > > interactively adjust the color palette for an image by moving the mouse in > > the canvas using PIL. In pseudo-code, I have something like > > > > palette=color_map(x,y) # x,y are scalars indicating the position of the > > mouse in the Canvas image.putpalette(palette) > > photo.paste(image) > > > > This works just fine, but is very slow. The photo.paste(image) step takes > > about 0.15 s (using an image that is 4k x 4k). I read the manual on > > effbot, and they caution that the paste method would be slow if the image > > is display --- and I'm guessing that's what I'm doing. > > So you have read > > """ > Note: Pasting into an RGBA image that is displayed is a very slow operation. > It?s usually better to create a new image object. > """ > > on . > > > So my question is, what alternatives do I have to speed that step up. > > Did you try to create a new image, as the note suggests? > > > Thank you for any thoughts you might have... > > -Russell From ben.usenet at bsb.me.uk Tue Jan 24 14:50:20 2017 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Tue, 24 Jan 2017 19:50:20 +0000 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> <87sho8sa26.fsf@bsb.me.uk> Message-ID: <87bmuws0eb.fsf@bsb.me.uk> Chris Angelico writes: > On Wed, Jan 25, 2017 at 3:21 AM, Ben Bacarisse wrote: >> Chris Angelico writes: >> >>> On Wed, Jan 25, 2017 at 2:51 AM, Ben Bacarisse wrote: >>>> Chris Angelico writes: >>>> >>>>> ... I teach JavaScript as well as Python, and I've seen some >>>>> pretty horrendous indentation flaws (examples available if people ask >>>>> privately, but I will anonymize them because I'm not here to shame >>>>> students) - but there have been nearly as many cases where the >>>>> indentation's fine and the bracket nesting isn't. >>>> >>>> Can I ask what editor(s) your students have available? I ask because >>>> I've not given a moment's thought to indentation or what bracket matches >>>> what for decades due to having a helpful editor. >>>> >>> >>> Quite a few, depending on which platform they're using. I'd say >>> Sublime is the most common choice. >> >> So what's going in your opinion? The errors are in the group who don't >> use the right tools or what? > > In my opinion, what's going on is that exploring code is a messy > process, and that the more little finicky things you have to get > right, the more likely you'll get something wrong. Which one you get > wrong is pretty much arbitrary. Do you think the tools (the editor in particular) could be more helpful? As I said elsewhere I've forgotten how these sorts of errors happen so I have very little insight into how to prevent them. With token-marked blocks I feel (though I don't know) that matching and auto-indenting help me to know I've written what I intended to write. I was just surprised that these errors happened enough for you to comment on them. > Getting indentation wrong in JS won't affect the interpreter, but it > majorly affects the human. Which one would you rather confuse? I'm guessing that's a rhetorical question! (It obviously depends on the situation.) I suspect that part of the reason these errors occur is precisely because they don't matter to the interpreter and students are doing a lot of self-easement based on "does it work?" tests. This is why Python is a win in such situations -- you can't write confusingly indented code, only wrong code. -- Ben. From rosuav at gmail.com Tue Jan 24 16:19:20 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 25 Jan 2017 08:19:20 +1100 Subject: How an editor can help with block nesting (was Re: How coding in Python is bad for you) Message-ID: On Wed, Jan 25, 2017 at 6:31 AM, Ben Bacarisse wrote: > I'm not talking about detecting errors -- that's for the programmer -- > but the editor can help the programmer to be sure they wrote what they > meant by doing things like matching brackets and auto-indenting code in > {}s. (I'm replying to a post about JavaScript which has {}s.) > > The trouble is that I've been programming for so long that I can't > remember what it's like to make block and/or indent errors. Obviously I > make typos but they don't survive more than a few seconds. I hoped that > Chris's students would provide an insight into how the tools do or > don't help. > > In Python the editor could, for example, highlight the block you are > typing in, so as soon as you leave the body of the 'if' it would stop > being marked and the containing code would be highlighted. Just moving > the cursor up and down would show you what block everything is in. I > don't know if any editors help like this -- that's part of my reason to > ask. This is a huge topic, and worthy of its own thread. I've toyed with a lot of different editors myself, and in the end, I actually found that features like this are less valuable than having an editor that's light on its toes and responds snappily even on lower-end hardware (and I like using the same editor with the same configs on all my systems). so I'm using SciTE. The most common editor among my students is probably Sublime, followed by Atom, and they have fairly similar feature sets. Ultimately, programming is a messy business. You start out with an imperfect idea of what you want, and progressively refine from there. So anything that lets you "sketch" your code more easily is a good thing. I think we can all agree on that part, right? It's just a question of what _kind_ of help the editor gives. The main two forms of help that I get or see from editors are: 1) Actually doing stuff for you, where it's 95% certain what you want 2) Visually showing you what your code is doing. The first category includes stuff like auto-indentation (add an indentation level when you have an open brace, or when there's a colon ending the previous line), and the second category includes stuff that highlights language keywords, or tells you about local vs global names, or shows you where the matching bracket is for the one your cursor's on. I kinda like the idea of showing what the innermost active block heading is for any given line of code. That would be fairly straight-forward: scroll up till you find a non-blank line with less indentation than the one you're on, and put a marker there. The nearest equivalent I have is a feature that one of my cow-orkers asked for once: a Python version of "jump to nearest brace". I bound it to Ctrl-E, and played with the feature, but never really found it useful - it was of mild value but I could generally eyeball it just as easily. Would be curious to see how it works with a colour highlight rather than an active "go to the top of this block" keystroke. (Bikeshed: should _every_ active block heading be highlighted, or just the innermost one? If you're inside a function, a loop, and a condition, should the 'def', 'for', and 'if' lines all be highlighted?) With my JavaScript students, the greatest help is probably a keystroke beautifier. You edit your code with sloppy indentation, and then bam, it reindents for you. The trouble is that they can end up with code where the indentation matches the braces, but *both are now wrong*. I'd be very curious to see an "auto-bracket-closer" that adjusts the brackets to match the indentation. Here's an example piece of JavaScript - imagine this has been through a few rounds of editing: function reducer(state=initial_state, action={}) { switch (action.type) { case 'FROBNOSTICATE': let items = info_store.filter((item) => { console.log("item:", item); return action.frob == item.frobbability; )} console.log("Keeping:", items) return {...state, frobbables: items} } default: break; } return state; } Now, I've deliberately made the indentation follow my intention, and then messed up the brackets. An editor should be able to look at this and pinpoint the places where the indentation changes unexpectedly, and either automatically correct it, or raise an error (in the case of the omitted open brace in the above example). I'd be curious to see how well this works for people, particularly novices. But it'd have to be implemented first :) Python removes some of this by not using braces for block delimiters, but you can still get the same phenomenon with nested list/dict displays, function calls, etc. So the code would be identical, except that a colon at the end of a line indicates indentation too. Anyone want to code this into an editor and see how it performs? ChrisA From this.wiederkehr at gmail.com Tue Jan 24 16:31:44 2017 From: this.wiederkehr at gmail.com (This Wiederkehr) Date: Tue, 24 Jan 2017 22:31:44 +0100 Subject: With class as contextmanager Message-ID: Hellou having a class definition: class Test(): @classmethod def __enter__(cls): pass @classmethod def __exit__(cls, exception_type, execption_value, callback): pass now using this as a contextmanager does not work, even though Test is an object and has the two required methods __enter__ and __exit__. it fails with: #Attribute Error: __enter__ This is not working because behind the scene it does something like: type(Test).__enter__(Test) But isn't this supposed to be working? I am asking because I'd like to implement the clean up behaviour for multiple instances directly into the class: with Test: testinstance1 = Test() testinstance2 = Test() # on context exit Test.__exit__ should take care on cleaning up testinstance1 and testinstance2. Regards This From ian.g.kelly at gmail.com Tue Jan 24 17:31:31 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 24 Jan 2017 15:31:31 -0700 Subject: With class as contextmanager In-Reply-To: References: Message-ID: On Tue, Jan 24, 2017 at 2:31 PM, This Wiederkehr wrote: > Hellou > > having a class definition: > > class Test(): > > @classmethod > def __enter__(cls): > pass > > @classmethod > def __exit__(cls, exception_type, execption_value, callback): > pass > > now using this as a contextmanager does not work, even though Test is an > object and has the two required methods __enter__ and __exit__. > > it fails with: > #Attribute Error: __enter__ > > > This is not working because behind the scene it does something like: > type(Test).__enter__(Test) > > But isn't this supposed to be working? > > I am asking because I'd like to implement the clean up behaviour for > multiple instances directly into the class: > > with Test: > testinstance1 = Test() > testinstance2 = Test() > # on context exit Test.__exit__ should take care on cleaning up > testinstance1 and testinstance2. Special dunder methods are generally only looked up on the object's class, not on the object itself. If you wish to use a class object as a context manager then you need to define __enter__ and __exit__ on the class's class, i.e. its metaclass. Alternatively you could separate the context manager from the testing class and make Test be just an object that defines __enter__, __exit__ and __call__, with __call__ creating and returning a new instance of the actual testing class. From ethan at stoneleaf.us Tue Jan 24 17:35:08 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 24 Jan 2017 14:35:08 -0800 Subject: With class as contextmanager In-Reply-To: References: Message-ID: <5887D69C.9040909@stoneleaf.us> On 01/24/2017 01:31 PM, This Wiederkehr wrote: > having a class definition: > > class Test(): > > @classmethod > def __enter__(cls): > pass > > @classmethod > def __exit__(cls, exception_type, execption_value, callback): > pass > > now using this as a contextmanager does not work, even though Test is an > object and has the two required methods __enter__ and __exit__. It is not working because you are trying to use the class itself, and not it's instances, as a context manager (which also means you don't need classmethod): Wrong: with Test: Correct: with Test(): > I am asking because I'd like to implement the clean up behaviour for > multiple instances directly into the class: > > with Test: > testinstance1 = Test() > testinstance2 = Test() > # on context exit Test.__exit__ should take care on cleaning up > testinstance1 and testinstance2. You might be able to make this work with a custom type (aka using a custom metaclass) -- but that could be a bunch of work. -- ~Ethan~ From hpj at urpla.net Tue Jan 24 18:23:06 2017 From: hpj at urpla.net (Hans-Peter Jansen) Date: Wed, 25 Jan 2017 00:23:06 +0100 Subject: Referencing section name by interpolation in ConfigParser Message-ID: <4337515.1dgV1QdrqS@xrated> Hi, I would like to use a interpolated section name, e.g.: [Section] secref: %{section}s/whatever should result in: >>> config['Section']['secref'] 'Section/whatever' Any idea anybody, how to archive this with minimum fuzz? Thanks, Pete From ethan at stoneleaf.us Tue Jan 24 18:25:25 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 24 Jan 2017 15:25:25 -0800 Subject: With class as contextmanager In-Reply-To: <5887D69C.9040909@stoneleaf.us> References: <5887D69C.9040909@stoneleaf.us> Message-ID: <5887E265.4000700@stoneleaf.us> On 01/24/2017 02:35 PM, Ethan Furman wrote: > On 01/24/2017 01:31 PM, This Wiederkehr wrote: >> having a class definition: >> >> class Test(): >> >> @classmethod >> def __enter__(cls): >> pass >> >> @classmethod >> def __exit__(cls, exception_type, execption_value, callback): >> pass >> >> now using this as a contextmanager does not work, even though Test is an >> object and has the two required methods __enter__ and __exit__. > > It is not working because you are trying to use the class itself, and not it's instances, as a context manager (which also means you don't need classmethod): > > Wrong: > > with Test: > > Correct: > > with Test(): > >> I am asking because I'd like to implement the clean up behaviour for >> multiple instances directly into the class: >> >> with Test: >> testinstance1 = Test() >> testinstance2 = Test() >> # on context exit Test.__exit__ should take care on cleaning up >> testinstance1 and testinstance2. > > You might be able to make this work with a custom type (aka using a custom metaclass) -- but that could be a bunch of work. Okay, here's the easy part: --- 8< ---------------------------- class TestType(type): def __enter__(cls): pass def __exit__(cls, *args): pass class Test(metaclass=TestType): pass with Test: print('worked!') --- 8< ---------------------------- Let us know how it turns out. -- ~Ethan~ From steve+python at pearwood.info Tue Jan 24 20:31:11 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Wed, 25 Jan 2017 12:31:11 +1100 Subject: How an editor can help with block nesting (was Re: How coding in Python is bad for you) References: Message-ID: <5887ffe2$0$1603$c3e8da3$5496439d@news.astraweb.com> On Wed, 25 Jan 2017 08:19 am, Chris Angelico wrote: > I kinda like the idea of showing what the innermost active block > heading is for any given line of code. That would be fairly > straight-forward: scroll up till you find a non-blank line with less > indentation than the one you're on, and put a marker there. The > nearest equivalent I have is a feature that one of my cow-orkers asked > for once: a Python version of "jump to nearest brace". I bound it to > Ctrl-E, and played with the feature, but never really found it useful > - it was of mild value but I could generally eyeball it just as > easily. Indeed. Good programming practice suggests that your block should ideally be under half an dozen lines and certainly less than a full screenful. If you need a command to jump to the top or bottom of the block, your blocks are too big and need refactoring. > Would be curious to see how it works with a colour highlight > rather than an active "go to the top of this block" keystroke. > (Bikeshed: should _every_ active block heading be highlighted, or just > the innermost one? If you're inside a function, a loop, and a > condition, should the 'def', 'for', and 'if' lines all be > highlighted?) I actually don't see the point. For an experienced coder, you already have all the clue you need to see the block structure: the indentation level. (If you are indenting by a single space, or even two spaces, you're probably hamstringing yourself and hurting your ability to recognise structure at a glance.) Adding an extra colour cue to duplicate the information given by indentation just adds to the noise level of code. But if you were to do the experiment, I'd *only* colour the currently edited block, by marking the margins with a subtle colour. You know how many GUI editors highlight the current line with a faint colour? Something similar. You could make the colour different for each block, I guess, if you wanted to be fancy. > With my JavaScript students, the greatest help is probably a keystroke > beautifier. You edit your code with sloppy indentation, and then bam, > it reindents for you. Really? I wouldn't want that, or find it helpful to type badly formatted code. I would want one of two things: (1) Indentation is up to you. You either get it syntactically right, or the code won't run, like Python. That teaches you to get the indentation right through negative reinforcement. Of course merely getting the indentation syntactically correct doesn't mean it is *semantically* correct, but the editor or compiler doesn't know what you mean, only what you say. If you cannot get the syntax right, you can't hope to communicate to the compiler. So learning to get the syntax right is a necessary first step to learning to program. (2) Or the editor won't let you enter sloppy indentation in the first place. This teaches you good indentation by example. Every time you hit return, the editor automatically indents as needed. If you mistakenly change the indentation (or fail to change the indentation when needed), then when you hit return on that line (or any other editing command that moves to another line), the editor either fixes the indentation for you, if it can, or flags the line as a syntax error if it cannot determine the right thing to do. For example, suppose I already have this, where | is the current text insertion point: def func(a, b): if condition: spam() | I hit ENTER, and the editor automatically indents to the current block level. There's nothing new about that, most editors already do this: def func(a, b): if condition: spam() | But now I type something which cannot possibly be indented there: def func(a, b): if condition: spam() elif something: | and hit ENTER again. There's nothing ambiguous about this, and the editor could (and should?) re-indent the elif line, giving this: def func(a, b): if condition: spam() elif something: | In other words, with the second scenario, you can never have more than one mis-indented line at a time during the normal course of editing. (At least not without really working to defeat the editor.) -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From dan at tombstonezero.net Tue Jan 24 20:44:20 2017 From: dan at tombstonezero.net (Dan Sommers) Date: Wed, 25 Jan 2017 01:44:20 -0000 (UTC) Subject: How an editor can help with block nesting (was Re: How coding in Python is bad for you) References: <5887ffe2$0$1603$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, 25 Jan 2017 12:31:11 +1100, Steve D'Aprano wrote: > But now I type something which cannot possibly be indented there: > > def func(a, b): > if condition: > spam() > elif something: | > > and hit ENTER again. There's nothing ambiguous about this, and the > editor could (and should?) re-indent the elif line, giving this: > > def func(a, b): > if condition: > spam() > elif something: > | > > In other words, with the second scenario, you can never have more than > one mis-indented line at a time during the normal course of > editing. (At least not without really working to defeat the editor.) And then you get to this one: def f(x): if condition: spam() if g(x): eggs() elif something_else: | Now, everyone, say it with me: In the face of ambiguity, refuse the temptation to guess. In languages with braces, re-indenting lines as I press ENTER is possible (I happen not to like that behavior, but that's a personal preference, and editors that won't get out of the way and let me edit my own code get deleted). In Python, I prefer to get the indent right myself before I press ENTER. Dan From rosuav at gmail.com Tue Jan 24 20:47:28 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 25 Jan 2017 12:47:28 +1100 Subject: How an editor can help with block nesting (was Re: How coding in Python is bad for you) In-Reply-To: <5887ffe2$0$1603$c3e8da3$5496439d@news.astraweb.com> References: <5887ffe2$0$1603$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Jan 25, 2017 at 12:31 PM, Steve D'Aprano wrote: >> With my JavaScript students, the greatest help is probably a keystroke >> beautifier. You edit your code with sloppy indentation, and then bam, >> it reindents for you. > > Really? I wouldn't want that, or find it helpful to type badly formatted > code. This isn't something I use (when I want a beautifier, it's because I'm de-minifying some code to figure out what it does, so I just paste it into http://jsbeautifier.org/ or something), but I've seen my students move braces around and then hit the beautifier, where I would highlight the text and hit Tab or Shift-Tab to adjust the indentation level. > I would want one of two things: > > (1) Indentation is up to you. You either get it syntactically right, or the > code won't run, like Python. That teaches you to get the indentation right > through negative reinforcement. > > (2) Or the editor won't let you enter sloppy indentation in the first place. > This teaches you good indentation by example. > > [chomp details of entering code for the first time] Tell me, how often do you enter code and it works the first time? :) Much more common - and this is especially true of student programmers, but we're none of us immune to it - the code gets tinkered with and edited and manipulated many times before it's finally working. Stuff gets reordered, conditionals get added or removed, etc, etc, etc. So there are a few possibilities: 1) Get the symbols right, then adjust the indentation to match 2) Get the indentation right, then adjust the symbols to match 3) Check before permitting code to be committed, and reject if they don't match 4) Check before permitting code even to be run 5) Ignore the indentation because it "doesn't matter", and then waste your instructor's time when you call him in to help you figure out where your bug is. Python makes the second option easier (by having no symbols for the most common indentation), but doesn't eliminate the problem entirely. The fifth option is what most often happens. The first is the "hit the beautifier key" option, and is fine as long as you absolutely definitely got the symbols right. I'm thinking the third or fourth might be the best :) ChrisA From bc at freeuk.com Tue Jan 24 20:50:43 2017 From: bc at freeuk.com (BartC) Date: Wed, 25 Jan 2017 01:50:43 +0000 Subject: How coding in Python is bad for you In-Reply-To: References: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <588789ef$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 25/01/2017 01:04, Dennis Lee Bieber wrote: > On Tue, 24 Jan 2017 17:50:56 +0000, BartC declaimed the > following: >> If I've accidentally lost a space or tab while messing about with it, >> and it's significant, I would rather the compiler reported it! As I'm >> not going to spot it by perusing the 15,000 pages of code.) >> > None of the C/Java/Pascal/Ada family will report a missing space or > tab. The point is, it usually doesn't matter as in most situations it's not significant. But if it was significant it would be useful for the language to pick it up. (Usually it would be in such languages, but not always.) And if C code is posted on usenet, and leading whitespace is lost, as often happens, then the results are still valid code. Losing leading white space with Python code would cause serious problems as it contains program structure that is not backed up by explicit block markers (which could have been used to reconstruct the indentation). -- Bartc From python.list at tim.thechases.com Tue Jan 24 20:54:30 2017 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 24 Jan 2017 19:54:30 -0600 Subject: How coding in Python is bad for you In-Reply-To: References: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <588789ef$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20170124195430.6c7dcafc@bigbox.christie.dr> On 2017-01-24 20:04, Dennis Lee Bieber wrote: >>You don't know that. If this has been pasted from elsewhere, you >>need to match up the indentation level with the current code. > > So? The editor(s) I tend to use have the ability to shift > indent in/out for selected blocks. Do the paste, highlight the > pasted content, and shift the indent as needed. The editor I use (vim) doesn't even require me to re-select the range to shift it. Just using >'] or <'] will indent/dedent from the cursor to the line where your paste ended. Hurray for laziness :-) -tkc From tjreedy at udel.edu Tue Jan 24 21:21:02 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 24 Jan 2017 21:21:02 -0500 Subject: With class as contextmanager In-Reply-To: References: Message-ID: On 1/24/2017 4:31 PM, This Wiederkehr wrote: > having a class definition: > > class Test(): > > @classmethod > def __enter__(cls): > pass > > @classmethod > def __exit__(cls, exception_type, execption_value, callback): > pass > > now using this as a contextmanager does not work, even though Test is an > object and has the two required methods __enter__ and __exit__. > > it fails with: > #Attribute Error: __enter__ > > > This is not working because behind the scene it does something like: > type(Test).__enter__(Test) > > But isn't this supposed to be working? No. Unqualified 'method' means instance method, not class method. One can simulate instance methods like so: >>> import types >>> def f(self): print('f called') >>> class C: pass >>> c = C() >>> c.m = types.MethodType(f, c) >>> c.m() f called However, this will not work for dunder methods where the lookup for the method starts with the class and not the object itself. >>> def h(self): return 1 >>> c.__hash__ = types.MethodType(h, c) >>> hash(c) # calls type(c).__hash__, not c.__hash__ -9223371924496369383 -- Terry Jan Reedy From greg.ewing at canterbury.ac.nz Tue Jan 24 23:44:51 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 25 Jan 2017 17:44:51 +1300 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <%tJhA.371507$1y4.144483@fx20.am4> <4iuf8clgqekpr1jkf9danh99h4eedd5ptb@4ax.com> Message-ID: Dennis Lee Bieber wrote: > While I'm trying to figure out what significance Centrum vitamins have > with being "non-GMO"... Possibly they're just complying with some legal requirement or other to declare whether the product has any GMO components. If so, bit of a silly regulation, I'll agree. What we really want to know is whether they use any dihydrogen monoxide in the manufacturing process! -- Greg From greg.ewing at canterbury.ac.nz Tue Jan 24 23:51:24 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 25 Jan 2017 17:51:24 +1300 Subject: PhotoImage.paste In-Reply-To: References: <2c6196b4-89ac-6111-cb13-5062bf538297@mrabarnett.plus.com> <61vf8c580fkmpm4jr7mmf928tgfmu7bmb4@4ax.com> Message-ID: Dennis Lee Bieber wrote: > But practically everything these days uses true/high color, in which > each pixel encodes the exact color to be displayed. This means that > changing all matching pixels from one given color to another given color > requires rewriting those pixels color data. Yes, and a 4k x 4k RGBA image is about 64MB of data. I'm not surprised it takes a noticeable amount of time to change all of that. -- Greg From cmalliopoulos at gmail.com Wed Jan 25 01:38:08 2017 From: cmalliopoulos at gmail.com (Christos Malliopoulos) Date: Wed, 25 Jan 2017 06:38:08 +0000 Subject: configparser bug In-Reply-To: References: Message-ID: Hi, I run Ubuntu 16.04 LTS in a VM using VMWare Workstation on a Windows 10 host. apt show python-configparser shows 3.3.0r2-2 On python 2.7.12 I use the following code: import configparser as cfg root = u'/'.join(os.path.split(os.path.abspath('cfg.py'))[0].split('/')[:-2]) cp = cfg.ConfigParser(interpolation = cfg.ExtendedInterpolation()) cp.read(os.path.abspath(os.path.join(root, u'config/sampling.cfg'))) cp.items('Sampling') sampling.cfg contains the following lines: [Sampling] nobs = 10 nzin = 4 nzout = 3 ndrops = 1 inh = day,loc,z0,value outh = day,loc,sku,value invalran = 1,1e3 outvalran = 1,1000 cp.items(u'Sampling') prints the following: [(u'nobs', u'10'), (u'nzin', u'4'), (u'nzout', u'3\nndrops = 1'), (u'inh', u'day,loc,z0,value'), (u'outh', u'day,loc,sku,value'), (u'invalran', u'1,1e3'), (u'outvalran', u'1,1000')] ndrops = 1 is not parsed correctly From tonytonytony0724 at gmail.com Wed Jan 25 02:22:21 2017 From: tonytonytony0724 at gmail.com (Tony Chen) Date: Tue, 24 Jan 2017 23:22:21 -0800 (PST) Subject: any one used moviepy please come in!!! I need help, thanks! Message-ID: <5ec6d36f-5200-4882-98ce-c317deab35b3@googlegroups.com> I have to use moviepy in one of my project. So I downloaded it, and tried the example code on the website. It gives me this error. Anyone can give me some help? Thank you very much! it gives this error: [MoviePy] This command returned an error !Traceback (most recent call last): File "tst.py", line 5, in txt_clip = TextClip("My Holidays 2013", fontsize = 70, color = 'white') File "/usr/local/lib/python2.7/dist-packages/moviepy/video/VideoClip.py", line 1145, in __init__ raise IOError(error) IOError: MoviePy Error: creation of None failed because of the following error: convert: not authorized `@/tmp/tmpjistPm.txt' @ error/property.c/InterpretImageProperties/3405. convert: no images defined `PNG32:/tmp/tmpJM0NVq.png' @ error/convert.c/ConvertImageCommand/3210. . .This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or.that the path you specified is incorrect From rosuav at gmail.com Wed Jan 25 02:33:46 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 25 Jan 2017 18:33:46 +1100 Subject: any one used moviepy please come in!!! I need help, thanks! In-Reply-To: <5ec6d36f-5200-4882-98ce-c317deab35b3@googlegroups.com> References: <5ec6d36f-5200-4882-98ce-c317deab35b3@googlegroups.com> Message-ID: On Wed, Jan 25, 2017 at 6:22 PM, Tony Chen wrote: > This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or.that the path you specified is incorrect So... is ImageMagick installed? ChrisA From hmmeeranrizvi18 at gmail.com Wed Jan 25 02:45:01 2017 From: hmmeeranrizvi18 at gmail.com (hmmeeranrizvi18 at gmail.com) Date: Tue, 24 Jan 2017 23:45:01 -0800 (PST) Subject: Hide text in entry box when i click on it.(GUI using Tkinter in python) Message-ID: <01419236-7cb8-498e-b3bb-9f209b1db53e@googlegroups.com> Hello Guys, Here i am creating a entry box with some text,i need to hide the text when i click on it. Here is my code from Tkinter import * obj = Tk() b = Entry(obj,width=100) b.insert(0,"Enter the value to search") b.pack() mainloop() From bob.martin at excite.com Wed Jan 25 02:53:57 2017 From: bob.martin at excite.com (Bob Martin) Date: Wed, 25 Jan 2017 07:53:57 GMT Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: in 770220 20170124 070853 Chris Angelico wrote: >On Tue, Jan 24, 2017 at 6:00 PM, Bob Martin wrote: >> in 770207 20170124 005601 Chris Angelico wrote: >> >>>REXX has even less structure than Python - it doesn't even have >>>functions, just labels, so you can actually have two functions that >>>share a common tail. And yes, you can abuse that horrendously to >>>create unreadable code. Is REXX a bad language because of that? No. >>>You can use structure badly, and you can use freedom badly. >> >> Of course Rexx has functions! > >Built-ins, yes, but when you define your own, it's by giving a label. >It isn't a function until it's called as a function. Just because the word "function" isn't in the header doesn't change matters. The word "procedure" can appear in the header, but it can be called as a procedure or a function. From __peter__ at web.de Wed Jan 25 04:01:56 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 25 Jan 2017 10:01:56 +0100 Subject: Referencing section name by interpolation in ConfigParser References: <4337515.1dgV1QdrqS@xrated> Message-ID: Hans-Peter Jansen wrote: > I would like to use a interpolated section name, e.g.: > > [Section] > secref: %{section}s/whatever > > should result in: > >>>> config['Section']['secref'] > 'Section/whatever' > > Any idea anybody, how to archive this with minimum fuzz? If you can live with the existing "basic interpolation", i. e. %(...)s, not %{...}s: $ cat config.ini [Foo] secref: %(section)s/whatever [Bar] secref: %(section)s/whatever $ cat demo.py import configparser class Interpolation(configparser.BasicInterpolation): def before_get(self, parser, section, option, value, defaults): defaults = defaults.copy() defaults["section"] = section return super().before_get(parser, section, option, value, defaults) p = configparser.ConfigParser(interpolation=Interpolation()) p.read("config.ini") for section in "Foo", "Bar": print(section, "-->", p[section]["secref"]) $ python3 demo.py Foo --> Foo/whatever Bar --> Bar/whatever $ From __peter__ at web.de Wed Jan 25 04:18:41 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 25 Jan 2017 10:18:41 +0100 Subject: Hide text in entry box when i click on it.(GUI using Tkinter in python) References: <01419236-7cb8-498e-b3bb-9f209b1db53e@googlegroups.com> Message-ID: hmmeeranrizvi18 at gmail.com wrote: > Hello Guys, > Here i am creating a entry box with some text,i need to hide the text when > i click on it. Here is my code > > from Tkinter import * > obj = Tk() > b = Entry(obj,width=100) > b.insert(0,"Enter the value to search") > b.pack() > mainloop() You need to bind a callback function to a mouse event: import Tkinter as tk def clear_search(event): search.delete(0, tk.END) root = tk.Tk() search = tk.Entry(root, width=100) search.insert(0, "Enter the value to search") search.pack() search.bind("", clear_search) root.mainloop() This will always clear the Entry; you probably want to check if it contains the initial message first to avoid that the user accidentally loses input. From hmmeeranrizvi18 at gmail.com Wed Jan 25 04:53:03 2017 From: hmmeeranrizvi18 at gmail.com (hmmeeranrizvi18 at gmail.com) Date: Wed, 25 Jan 2017 01:53:03 -0800 (PST) Subject: Hide text in entry box when i click on it.(GUI using Tkinter in python) In-Reply-To: <01419236-7cb8-498e-b3bb-9f209b1db53e@googlegroups.com> References: <01419236-7cb8-498e-b3bb-9f209b1db53e@googlegroups.com> Message-ID: On Wednesday, January 25, 2017 at 1:15:11 PM UTC+5:30, hmmeera... at gmail.com wrote: > Hello Guys, > Here i am creating a entry box with some text,i need to hide the text when i click on it. > Here is my code > > from Tkinter import * > obj = Tk() > b = Entry(obj,width=100) > b.insert(0,"Enter the value to search") > b.pack() > mainloop() Thanks Peter From __peter__ at web.de Wed Jan 25 06:30:56 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 25 Jan 2017 12:30:56 +0100 Subject: configparser bug References: Message-ID: Christos Malliopoulos wrote: > Hi, > > I run Ubuntu 16.04 LTS in a VM using VMWare Workstation on a Windows 10 > host. > apt show python-configparser shows 3.3.0r2-2 > On python 2.7.12 I use the following code: > > import configparser as cfg > root = > u'/'.join(os.path.split(os.path.abspath('cfg.py'))[0].split('/')[:-2]) > cp = cfg.ConfigParser(interpolation = cfg.ExtendedInterpolation()) > cp.read(os.path.abspath(os.path.join(root, u'config/sampling.cfg'))) This looks confusing. Are you sure you are reading the right sampling.cfg? > cp.items('Sampling') > > sampling.cfg contains the following lines: > [Sampling] > nobs = 10 > nzin = 4 > nzout = 3 > ndrops = 1 > > inh = day,loc,z0,value > outh = day,loc,sku,value > > invalran = 1,1e3 > outvalran = 1,1000 > > cp.items(u'Sampling') prints the following: > [(u'nobs', u'10'), > (u'nzin', u'4'), > (u'nzout', u'3\nndrops = 1'), > (u'inh', u'day,loc,z0,value'), > (u'outh', u'day,loc,sku,value'), > (u'invalran', u'1,1e3'), > (u'outvalran', u'1,1000')] > > ndrops = 1 is not parsed correctly It looks like the parser does not expand tabs, and your problem may be mixing tabs and spaces: $ cat demo.py import configparser data = """\ [Sampling] \talpha = 1 \tbeta = 2 \tgamma = 3 \tdelta = 4 """ print "what you see:" print data.expandtabs(4) print "what the parser sees:" print data.replace("\t", " ") with open("sampling.cfg", "w") as f: f.write(data) cp = configparser.ConfigParser( interpolation=configparser.ExtendedInterpolation() ) cp.read("sampling.cfg") for item in cp.items('Sampling'): print item $ python demo.py what you see: [Sampling] alpha = 1 beta = 2 gamma = 3 delta = 4 what the parser sees: [Sampling] alpha = 1 beta = 2 gamma = 3 delta = 4 (u'alpha', u'1') (u'beta', u'2\ngamma = 3') (u'delta', u'4') $ From alister.ware at ntlworld.com Wed Jan 25 07:00:25 2017 From: alister.ware at ntlworld.com (alister) Date: Wed, 25 Jan 2017 12:00:25 GMT Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <%tJhA.371507$1y4.144483@fx20.am4> <4iuf8clgqekpr1jkf9danh99h4eedd5ptb@4ax.com> Message-ID: On Tue, 24 Jan 2017 20:09:45 -0500, Dennis Lee Bieber wrote: > On Tue, 24 Jan 2017 16:03:45 GMT, alister > declaimed the following: > >>On Tue, 24 Jan 2017 14:28:55 +0000, Jon Ribbens wrote: >> >>> On 2017-01-24, alister wrote: >>>> On Mon, 23 Jan 2017 20:39:26 +0000, Jon Ribbens wrote: >>>>> That's a meaningless statement. *Everything* is a poison in >>>>> sufficient quantities. >>>> >>>> indees when I here someone saying "I won't have any xyz because they >>>> have heard that too much is bad for them" I invariably inform them >>>> that too much oxygen is poisonous & challenge then to cut that out >>>> completely :-) >>> >>> Just wait until they find out that raw fruit and vegetables contain >>> hundreds of chemicals! >> >>then send them of to one of the sitre relating to Di-Hydrogen >>Monoxide... > > Or: http://vintage.failed-dam.org/tomato.htm >> >>I know of one organic food producer that has even tried to deny the use >>the stuff ;-) > > While I'm trying to figure out what significance Centrum vitamins have > with being "non-GMO"... Unless they are implying that other companies > are using modified bacteria as chemical factories... How many vitamin > tablets can you think of that are compounded from plant extracts these > days? what has centrum vitamins & GMO got to do with di-hydrogen-monoxide? (although 1000-1 says they use it in their processes somewhere) -- A sequel is an admission that you've been reduced to imitating yourself. -- Don Marquis From this.wiederkehr at gmail.com Wed Jan 25 07:36:34 2017 From: this.wiederkehr at gmail.com (This Wiederkehr) Date: Wed, 25 Jan 2017 13:36:34 +0100 Subject: With class as contextmanager In-Reply-To: References: Message-ID: Thank you for your answers. Very appreciated. I ended up doing it as follows: class MetaLock(type): def __init__(cls, *args): super().__init__(*args) cls.lock = Lock() cls.instances = [] def register(cls, instance): cls.instances.append(instance) def __enter__(cls): cls.lock.acquire() def __exit__(cls, exc_type, exc_val, exc_tb): for instance in cls.instances: instance.cleanup() cls.instances = [] cls.lock.release() return False class Klass(metaclass=MetaLock): def __init__(self): if not self.__class__.lock.locked(): raise Exception("You have to use the context manager on the Class Object!") self.__class__.register(self) def cleaup(self): pass with Klass: inst1 = Klass() inst2 = Klass() # on leaving the context the cleanup of each instance of Klass is called. Regards This 2017-01-25 3:21 GMT+01:00 Terry Reedy : > On 1/24/2017 4:31 PM, This Wiederkehr wrote: > > having a class definition: >> >> class Test(): >> >> @classmethod >> def __enter__(cls): >> pass >> >> @classmethod >> def __exit__(cls, exception_type, execption_value, callback): >> pass >> >> now using this as a contextmanager does not work, even though Test is an >> object and has the two required methods __enter__ and __exit__. >> >> it fails with: >> #Attribute Error: __enter__ >> >> >> This is not working because behind the scene it does something like: >> type(Test).__enter__(Test) >> >> But isn't this supposed to be working? >> > > No. Unqualified 'method' means instance method, not class method. > > One can simulate instance methods like so: > > >>> import types > >>> def f(self): print('f called') > > >>> class C: pass > > >>> c = C() > >>> c.m = types.MethodType(f, c) > >>> c.m() > f called > > However, this will not work for dunder methods where the lookup for the > method starts with the class and not the object itself. > > >>> def h(self): return 1 > > >>> c.__hash__ = types.MethodType(h, c) > >>> hash(c) # calls type(c).__hash__, not c.__hash__ > -9223371924496369383 > > -- > Terry Jan Reedy > > -- > https://mail.python.org/mailman/listinfo/python-list > -- This Wiederkehr Vorgasse 21 5722 Gr?nichen 079 785 70 75 From murphyclara565 at gmail.com Wed Jan 25 09:15:04 2017 From: murphyclara565 at gmail.com (murphyclara565 at gmail.com) Date: Wed, 25 Jan 2017 06:15:04 -0800 (PST) Subject: Python Message-ID: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> Need help From breamoreboy at gmail.com Wed Jan 25 09:36:05 2017 From: breamoreboy at gmail.com (breamoreboy at gmail.com) Date: Wed, 25 Jan 2017 06:36:05 -0800 (PST) Subject: Python In-Reply-To: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> References: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> Message-ID: On Wednesday, January 25, 2017 at 2:15:16 PM UTC, murphyc... at gmail.com wrote: > Need help Please read this http://www.catb.org/~esr/faqs/smart-questions.html and then this http://sscce.org/ before trying again. Kindest regards. Mark Lawrence From Joaquin.Alzola at lebara.com Wed Jan 25 09:46:46 2017 From: Joaquin.Alzola at lebara.com (Joaquin Alzola) Date: Wed, 25 Jan 2017 14:46:46 +0000 Subject: Python In-Reply-To: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> References: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> Message-ID: > Need help I need a beer Put your question in the mailing list I think you can get the help needed. 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 stephane at wirtel.be Wed Jan 25 09:51:46 2017 From: stephane at wirtel.be (Stephane Wirtel) Date: Wed, 25 Jan 2017 15:51:46 +0100 Subject: Python In-Reply-To: References: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> Message-ID: <7B46CC65-0EE9-48D0-A303-A1DB0F9C296C@wirtel.be> Come to PythonFosdem https://www.python-fosdem.org we will offer some beers on 4 & 5 feb in Brussels > On 25 Jan 2017, at 15:46, Joaquin Alzola wrote: > > > >> Need help > I need a beer > > Put your question in the mailing list I think you can get the help needed. > 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. > -- > https://mail.python.org/mailman/listinfo/python-list From cannedham284 at hotmail.com Wed Jan 25 10:13:15 2017 From: cannedham284 at hotmail.com (Ben Iannitelli) Date: Wed, 25 Jan 2017 15:13:15 +0000 Subject: How an editor can help with block nesting (was Re: How coding in Python is bad for you) In-Reply-To: References: <5887ffe2$0$1603$c3e8da3$5496439d@news.astraweb.com>, Message-ID: On Wed, Jan 25, 2017 at 6:31 AM, Ben Bacarisse wrote: In Python the editor could, for example, highlight the block you are typing in, so as soon as you leave the body of the 'if' it would stop being marked and the containing code would be highlighted. Just moving the cursor up and down would show you what block everything is in. I don't know if any editors help like this -- that's part of my reason to ask. @Ben B.: Have you had a look at Notepad++? When it detects that you're editing a Python file, a ruler on the left-hand margin of the editing window employs a system of boxes and lines to show which blocks of code belong to which conditional clause, with those pertaining to the current block in red. Below are attempts to sketch it out without the benefit of screenshots or color. First attempt: [box with a dash inside of it ]if __name__ == "__main__": [U+2514] main() Second attempt (imagine that your cursor is somewhere in the same line as the "print" statement) : [box with a dash inside of it, in gray ]def main(): [box with a dash inside of it, in gray ] with open("some_spreadsheet.csv",newline='') as sheet: [U+2502, in gray] data = csv.DictReader(sheet) [box with a dash inside of it, in RED ] for n,row in enumerate(data): [U+2514, in RED] print(n,row) I didn't do it justice, but that's honestly the best I can do. If you can find the time to install it and then play with it, you can actually see for yourself whether that feature is loyal to the idea you described. HTH -Ben I. From bkline at rksystems.com Wed Jan 25 10:16:24 2017 From: bkline at rksystems.com (Bob Kline) Date: Wed, 25 Jan 2017 07:16:24 -0800 (PST) Subject: The argparse docs don't say who's responsible for closing FileType objects Message-ID: The subject line pretty much says it all. Should the programmer close the file? If the programmer does that, and the user has asked that the file object be hooked up to standard in (or standard out) what will happen? If the programmer doesn't close it, does it get closed cleanly in the face of an exception? Thanks! From alister.ware at ntlworld.com Wed Jan 25 10:33:03 2017 From: alister.ware at ntlworld.com (alister) Date: Wed, 25 Jan 2017 15:33:03 GMT Subject: The argparse docs don't say who's responsible for closing FileType objects References: Message-ID: On Wed, 25 Jan 2017 07:16:24 -0800, Bob Kline wrote: > The subject line pretty much says it all. Should the programmer close > the file? If the programmer does that, and the user has asked that the > file object be hooked up to standard in (or standard out) what will > happen? If the programmer doesn't close it, does it get closed cleanly > in the face of an exception? > > Thanks! i would think the principle is always the same if you open a file close it behind you using "with" should ensure this always happens -- It is bad luck to be superstitious. -- Andrew W. Mathis From auriocus at gmx.de Wed Jan 25 14:12:15 2017 From: auriocus at gmx.de (Christian Gollwitzer) Date: Wed, 25 Jan 2017 20:12:15 +0100 Subject: Hide text in entry box when i click on it.(GUI using Tkinter in python) In-Reply-To: References: <01419236-7cb8-498e-b3bb-9f209b1db53e@googlegroups.com> Message-ID: Am 25.01.17 um 10:18 schrieb Peter Otten: > hmmeeranrizvi18 at gmail.com wrote: > >> Hello Guys, >> Here i am creating a entry box with some text,i need to hide the text when >> i click on it. > search.bind("", clear_search) > This is the correct answer for a mouse click. The typical use case (disappearing placeholder) should also trigger, when somebody uses the tab key to set the focus to the window. This can be achieved by binding to the event instead: search.bind("", clear_search) In addition, repeated clicking does not clear the text then. Christian From rryan.asu at gmail.com Wed Jan 25 15:37:07 2017 From: rryan.asu at gmail.com (rryan.asu at gmail.com) Date: Wed, 25 Jan 2017 12:37:07 -0800 (PST) Subject: PhotoImage.paste In-Reply-To: References: <2c6196b4-89ac-6111-cb13-5062bf538297@mrabarnett.plus.com> <61vf8c580fkmpm4jr7mmf928tgfmu7bmb4@4ax.com> Message-ID: <8da721cb-9813-48f0-9f8c-1380db25ed68@googlegroups.com> Hi Greg, Yeah, I thought of that too... So I was trying to store it as only a grayscale image and use the "L" format. I wonder if maybe that didn't work? I'll play around with that and ensure I did what I think I did. But that's a good reminder. Thanks! Russell On Tuesday, January 24, 2017 at 11:51:37 PM UTC-5, Gregory Ewing wrote: > Dennis Lee Bieber wrote: > > But practically everything these days uses true/high color, in which > > each pixel encodes the exact color to be displayed. This means that > > changing all matching pixels from one given color to another given color > > requires rewriting those pixels color data. > > Yes, and a 4k x 4k RGBA image is about 64MB of data. > I'm not surprised it takes a noticeable amount of > time to change all of that. > > -- > Greg From paul.nospam at rudin.co.uk Wed Jan 25 17:14:29 2017 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 25 Jan 2017 22:14:29 +0000 Subject: Python References: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> Message-ID: <87k29iu6re.fsf@rudin.co.uk> Joaquin Alzola writes: > 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. Probably best not to send it to a publicly accessible mailing list then :/ From none at invalid.com Wed Jan 25 17:48:42 2017 From: none at invalid.com (mm0fmf) Date: Wed, 25 Jan 2017 22:48:42 +0000 Subject: Python In-Reply-To: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> References: <277618c7-269d-4c73-bb64-1c83e932f9fd@googlegroups.com> Message-ID: On 25/01/2017 14:15, murphyclara565 at gmail.com wrote: > Need help > Can help, need money. From sandeep.nagar at gmail.com Wed Jan 25 21:25:44 2017 From: sandeep.nagar at gmail.com (Sandeep Nagar) Date: Wed, 25 Jan 2017 18:25:44 -0800 (PST) Subject: Need reviews for my book on introductory python Message-ID: Hi, A few month ago I wrote a book on introductory python based on my experinces while teaching python to Bachelor students of engineering. It is now available in e-book and paperback format at Amazon. https://www.amazon.com/dp/1520153686 The book is written for beginners of python programming language and written in learn-by-doing manner. If some group members can review the same, it will be useful for myself to come up with an improved version. Other similar books of mine are based on Octave and Scilab with following links: https://www.amazon.com/dp/152015111X (Scilab) https://www.amazon.com/dp/1520158106 (Octave) If you are interested in open source computing, please have a look. Also please do share the link for print books with your colleagues at other universities and recommend them for libraries and researchers, if you feel that they can be helpful to them. Regards Sandeep From tonytonytony0724 at gmail.com Wed Jan 25 21:41:59 2017 From: tonytonytony0724 at gmail.com (Tony Chen) Date: Wed, 25 Jan 2017 18:41:59 -0800 (PST) Subject: any one used moviepy please come in!!! I need help, thanks! In-Reply-To: References: <5ec6d36f-5200-4882-98ce-c317deab35b3@googlegroups.com> Message-ID: <12117181-ec3b-4dd6-9f58-7b0c81f0ae20@googlegroups.com> On Wednesday, January 25, 2017 at 8:34:01 PM UTC+13, Chris Angelico wrote: > On Wed, Jan 25, 2017 at 6:22 PM, Tony Chen wrote: > > This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or.that the path you specified is incorrect > > So... is ImageMagick installed? > > ChrisA Okay... Yes it's installed. This problem has been solved haha. Thank you for replying. From cmalliopoulos at gmail.com Thu Jan 26 07:13:30 2017 From: cmalliopoulos at gmail.com (Christos Malliopoulos) Date: Thu, 26 Jan 2017 12:13:30 +0000 Subject: configparser bug In-Reply-To: References: Message-ID: Thanx Peter for your prompt response. I replaced tabs with spaces and had no problem reading the file. I think however it is sth to correct in next versions BR Chris On Wed, Jan 25, 2017 at 1:32 PM Peter Otten <__peter__ at web.de> wrote: > Christos Malliopoulos wrote: > > > Hi, > > > > I run Ubuntu 16.04 LTS in a VM using VMWare Workstation on a Windows 10 > > host. > > apt show python-configparser shows 3.3.0r2-2 > > On python 2.7.12 I use the following code: > > > > import configparser as cfg > > root = > > u'/'.join(os.path.split(os.path.abspath('cfg.py'))[0].split('/')[:-2]) > > cp = cfg.ConfigParser(interpolation = cfg.ExtendedInterpolation()) > > cp.read(os.path.abspath(os.path.join(root, u'config/sampling.cfg'))) > > This looks confusing. Are you sure you are reading the right sampling.cfg? > > > cp.items('Sampling') > > > > sampling.cfg contains the following lines: > > [Sampling] > > nobs = 10 > > nzin = 4 > > nzout = 3 > > ndrops = 1 > > > > inh = day,loc,z0,value > > outh = day,loc,sku,value > > > > invalran = 1,1e3 > > outvalran = 1,1000 > > > > cp.items(u'Sampling') prints the following: > > [(u'nobs', u'10'), > > (u'nzin', u'4'), > > (u'nzout', u'3\nndrops = 1'), > > (u'inh', u'day,loc,z0,value'), > > (u'outh', u'day,loc,sku,value'), > > (u'invalran', u'1,1e3'), > > (u'outvalran', u'1,1000')] > > > > ndrops = 1 is not parsed correctly > > It looks like the parser does not expand tabs, and your problem may be > mixing tabs and spaces: > > $ cat demo.py > import configparser > > data = """\ > [Sampling] > \talpha = 1 > \tbeta = 2 > \tgamma = 3 > \tdelta = 4 > """ > print "what you see:" > print data.expandtabs(4) > > print "what the parser sees:" > print data.replace("\t", " ") > > with open("sampling.cfg", "w") as f: > f.write(data) > > cp = configparser.ConfigParser( > interpolation=configparser.ExtendedInterpolation() > ) > cp.read("sampling.cfg") > for item in cp.items('Sampling'): > print item > $ python demo.py > what you see: > [Sampling] > alpha = 1 > beta = 2 > gamma = 3 > delta = 4 > > what the parser sees: > [Sampling] > alpha = 1 > beta = 2 > gamma = 3 > delta = 4 > > (u'alpha', u'1') > (u'beta', u'2\ngamma = 3') > (u'delta', u'4') > $ > > > -- > https://mail.python.org/mailman/listinfo/python-list > From tdldev at gmail.com Thu Jan 26 07:32:17 2017 From: tdldev at gmail.com (Verde Denim) Date: Thu, 26 Jan 2017 07:32:17 -0500 Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: You would like us to volunteer to "review" your book that you'd like us to buy from you and that has already been published and used as a curriculum guide... If you'd like it reviewed by the Py community, how about making it available to us and allow us to edit it or suggest changes as needed? This, of course, would be for no-charge as we are using our time and resources to review your publication at no cost to you. Thanks. On 1/25/2017 9:25 PM, Sandeep Nagar wrote: > Hi, > > A few month ago I wrote a book on introductory python based on my experinces while teaching python to Bachelor students of engineering. It is now available in e-book and paperback format at Amazon. > > https://www.amazon.com/dp/1520153686 > > The book is written for beginners of python programming language and written in learn-by-doing manner. If some group members can review the same, it will be useful for myself to come up with an improved version. > > Other similar books of mine are based on Octave and Scilab with following links: > > https://www.amazon.com/dp/152015111X (Scilab) > > https://www.amazon.com/dp/1520158106 (Octave) > > If you are interested in open source computing, please have a look. > > Also please do share the link for print books with your colleagues at other universities and recommend them for libraries and researchers, if you feel that they can be helpful to them. > > Regards > > Sandeep --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus From bc at freeuk.com Thu Jan 26 08:32:41 2017 From: bc at freeuk.com (BartC) Date: Thu, 26 Jan 2017 13:32:41 +0000 Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: On 26/01/2017 12:32, Verde Denim wrote: > You would like us to volunteer to "review" your book that you'd like us > to buy from you and that has already been published and used as a > curriculum guide... If you'd like it reviewed by the Py community, how > about making it available to us and allow us to edit it or suggest > changes as needed? The amazon link lets you look at the first few pages free of charge. That might be enough to form an opinion. (The first pages do include a Hello World program; unfortunately it's in C not Python!) -- Bartc From bgailer at gmail.com Thu Jan 26 18:32:30 2017 From: bgailer at gmail.com (bob gailer) Date: Thu, 26 Jan 2017 18:32:30 -0500 Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: <8115024f-71c7-d526-37d4-65cfae9be207@gmail.com> On 1/25/2017 9:25 PM, Sandeep Nagar wrote: > Hi, > > A few month ago I wrote a book on introductory python based on my experinces while teaching python to Bachelor students of engineering. It is now available in e-book and paperback format at Amazon. > > https://www.amazon.com/dp/1520153686 > > The book is written for beginners of python programming language and written in learn-by-doing manner. If some group members can review the same, it will be useful for myself to come up with an improved version. I'd like to review it, but am reluctant to put out money for a copy! How about offering a review copy at no charge? > > Other similar books of mine are based on Octave and Scilab with following links: > > https://www.amazon.com/dp/152015111X (Scilab) > > https://www.amazon.com/dp/1520158106 (Octave) > > If you are interested in open source computing, please have a look. > > Also please do share the link for print books with your colleagues at other universities and recommend them for libraries and researchers, if you feel that they can be helpful to them. > > Regards > > Sandeep -- Image and video hosting by TinyPic From bamccaig at gmail.com Thu Jan 26 23:07:39 2017 From: bamccaig at gmail.com (Brandon McCaig) Date: Thu, 26 Jan 2017 23:07:39 -0500 Subject: How coding in Python is bad for you In-Reply-To: <87h94os1a1.fsf@bsb.me.uk> References: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> <87h94os1a1.fsf@bsb.me.uk> Message-ID: <20170127040739.GE1362@test-chamber-1.castopulence.org> On Tue, Jan 24, 2017 at 07:31:18PM +0000, Ben Bacarisse wrote: > The trouble is that I've been programming for so long that I > can't remember what it's like to make block and/or indent > errors. Obviously I make typos but they don't survive more > than a few seconds. Agreed. In very rare circumstances (likely very tired) you might make such a mistake without catching it, but typically you'll catch it during testing, and code review is another chance to catch it. Source control FTW. I think that a lot of these kinds of problems happen to beginners and the beginners make the most noise about how it will be a problem. I initially was against Python's significant white-space, but in hindsight I can see how it saves a bit of typing and is not necessarily any worse off. Hell, considering the code that I have seen in the wild it might even catch some extra errors that become syntax errors! It's not at all rare for indentation to not match in languages that don't require it to at least fit a pattern. I think that an apples to apples comparison of an erroneous indentation level would be comparing a misplaced brace: foo { bar; } baz; wapz. Was that supposed to be "foo { bar; baz; }" or "foo { bar; } baz;" ? That's effectively the same problem that you might have with Python code. The answer? Hopefully it's obvious when looking at the code! If it's not, hopefully source control can tell you. And if you can't be certain, *talk* to somebody. There's no substitute for communication. > In Python the editor could, for example, highlight the block > you are typing in, so as soon as you leave the body of the 'if' > it would stop being marked and the containing code would be > highlighted. Just moving the cursor up and down would show you > what block everything is in. I don't know if any editors help > like this -- that's part of my reason to ask. That's actually a pretty neat idea. I don't think I've ever encoutered an editor that does (or if I have, it has either been too long or so subtle that it doesn't stand out). I think that it is a pretty good idea though. I certainly find it useful to highlight matching braces in editors, but it can be a pain still. I think that highlighting the entire block could be useful. Though I suppose it might be too noisy and distract from what matters? Alternatively, you'd need different levels of indentation to capture nested indentation. That shouldn't be a problem if people are limiting their indentation levels though... Regards, -- Brandon McCaig Castopulence Software Blog perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: Digital signature URL: From bamccaig at gmail.com Thu Jan 26 23:15:12 2017 From: bamccaig at gmail.com (Brandon McCaig) Date: Thu, 26 Jan 2017 23:15:12 -0500 Subject: How coding in Python is bad for you In-Reply-To: <87bmuws0eb.fsf@bsb.me.uk> References: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> <87sho8sa26.fsf@bsb.me.uk> <87bmuws0eb.fsf@bsb.me.uk> Message-ID: <20170127041512.GF1362@test-chamber-1.castopulence.org> On Tue, Jan 24, 2017 at 07:50:20PM +0000, Ben Bacarisse wrote: > I suspect that part of the reason these errors occur is > precisely because they don't matter to the interpreter and > students are doing a lot of self-easement based on "does it > work?" tests. I cringe when I hear "it works"! In particular, because it's often followed by "but I don't know how". I can't even count the number of times I have reviewed code, spotted something questionable, approached the author about it, and heard "but it works!" Well if my analysis is correct it shouldn't so one of us is obviously wrong. Unfortunately, in my experience, they usually expect the conversation to be over after "but it works"... Regards, -- Brandon McCaig Castopulence Software Blog perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: Digital signature URL: From bamccaig at gmail.com Thu Jan 26 23:33:51 2017 From: bamccaig at gmail.com (Brandon McCaig) Date: Thu, 26 Jan 2017 23:33:51 -0500 Subject: How coding in Python is bad for you In-Reply-To: <20170124195430.6c7dcafc@bigbox.christie.dr> References: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <588789ef$0$1583$c3e8da3$5496439d@news.astraweb.com> <20170124195430.6c7dcafc@bigbox.christie.dr> Message-ID: <20170127043351.GG1362@test-chamber-1.castopulence.org> On Tue, Jan 24, 2017 at 07:54:30PM -0600, Tim Chase wrote: > The editor I use (vim) doesn't even require me to re-select the range > to shift it. Just using > > >'] > > or > > <'] > > will indent/dedent from the cursor to the line where your paste ended. (O_O) You learn something every day. Thank you. I have been using Vim and vi for probably close to a decade by now, but I still have a lot to learn... But nevertheless hate to go without for even a few seconds. Regards, -- Brandon McCaig Castopulence Software Blog perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: Digital signature URL: From Cecil at decebal.nl Fri Jan 27 05:54:19 2017 From: Cecil at decebal.nl (Cecil Westerhof) Date: Fri, 27 Jan 2017 11:54:19 +0100 Subject: How coding in Python is bad for you References: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> <87h94os1a1.fsf@bsb.me.uk> <20170127040739.GE1362@test-chamber-1.castopulence.org> Message-ID: <87efzo6aec.fsf@Equus.decebal.nl> On Friday 27 Jan 2017 05:07 CET, Brandon McCaig wrote: > Hell, considering the code that I have seen in the wild it might > even catch some extra errors that become syntax errors! It's not > at all rare for indentation to not match in languages that don't > require it to at least fit a pattern. I remember that when I had to amend the code of a certain programmer I always needed half a day to reformat his code. Because he was very good in copy/paste, but I could not made head or tail of his code because of the zig-zag of his code. That is why I immediately like the indent rule of Python very much. My only ?problem? was that I used to put debug statement at the beginning of a line to see immediately what the debug statements where. But that was a small price to pay. ;-) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From hpj at urpla.net Fri Jan 27 09:23:53 2017 From: hpj at urpla.net (Hans-Peter Jansen) Date: Fri, 27 Jan 2017 15:23:53 +0100 Subject: Referencing section name by interpolation in ConfigParser In-Reply-To: References: <4337515.1dgV1QdrqS@xrated> Message-ID: <1979405.f8UazQS3Ae@xrated> On Mittwoch, 25. Januar 2017 10:01:56 Peter Otten wrote: > Hans-Peter Jansen wrote: > > I would like to use a interpolated section name, e.g.: > > > > [Section] > > secref: %{section}s/whatever > > > > should result in: > >>>> config['Section']['secref'] > > > > 'Section/whatever' > > > > Any idea anybody, how to archive this with minimum fuzz? > > If you can live with the existing "basic interpolation", i. e. %(...)s, not > %{...}s: Yes, of course.. Sorry for the typo. > $ cat config.ini > [Foo] > secref: %(section)s/whatever > [Bar] > secref: %(section)s/whatever > $ cat demo.py > import configparser > > > class Interpolation(configparser.BasicInterpolation): > def before_get(self, parser, section, option, value, defaults): > defaults = defaults.copy() > defaults["section"] = section > return super().before_get(parser, section, option, value, defaults) > > > p = configparser.ConfigParser(interpolation=Interpolation()) > p.read("config.ini") > for section in "Foo", "Bar": > print(section, "-->", p[section]["secref"]) > $ python3 demo.py > Foo --> Foo/whatever > Bar --> Bar/whatever > $ Brilliant as usual, thank you, Peter. This is exactly, what I was after. Have a nice weekend, Pete From nathan.ernst at gmail.com Fri Jan 27 12:30:03 2017 From: nathan.ernst at gmail.com (Nathan Ernst) Date: Fri, 27 Jan 2017 11:30:03 -0600 Subject: How coding in Python is bad for you In-Reply-To: <87efzo6aec.fsf@Equus.decebal.nl> References: <5886d675$0$1501$c3e8da3$5496439d@news.astraweb.com> <874m0otq02.fsf@bsb.me.uk> <87h94os1a1.fsf@bsb.me.uk> <20170127040739.GE1362@test-chamber-1.castopulence.org> <87efzo6aec.fsf@Equus.decebal.nl> Message-ID: I used to manually reformat unfamiliar C++ by hand, if for no other reason in that it forced me to read the code and somewhat comprehend what was going on. Now, I've lost my patience and use clang-format, a great & highly configurable tool. I also use vim for Python & C++ coding, so I also rely upon the command "gg=G" to reformat the current file (this doesn't do much in Python, however). One of the things I like about python is the indentation. It almost forces (or rather strongly encourages you) to keep functions short & sweet, to do a single thing in a function, and avoid too deeply nested structures. I've seen horrible abominations in C++ over the years. In separate projects, I've seen functions that were, in one case, over 45 printed pages, another over 65 pages. The nesting level was way too deep. I think in the worst case there were 8 levels of nested loops. This was problematic for many reasons. Multiple developers working on this function at the same time and attempting to merge was a big problem - the diff tool used by Clearcase (I know, a WTF, in of itself) would get confused and occassionally omit closing braces. Good luck resolving a compilation error by a missing brace in a 5000 line function. As a result of the pain I've experienced, I try and keep functions small enough to fit on a half-vertical, half-horizontal screen, with no line-wraps on a 1080p monitor. (I'm usually working in a terminal half-screen wide and use vertical splits in vim to see different parts of a file, or different files). On Fri, Jan 27, 2017 at 4:54 AM, Cecil Westerhof wrote: > On Friday 27 Jan 2017 05:07 CET, Brandon McCaig wrote: > > > Hell, considering the code that I have seen in the wild it might > > even catch some extra errors that become syntax errors! It's not > > at all rare for indentation to not match in languages that don't > > require it to at least fit a pattern. > > I remember that when I had to amend the code of a certain programmer I > always needed half a day to reformat his code. Because he was very > good in copy/paste, but I could not made head or tail of his code > because of the zig-zag of his code. > > That is why I immediately like the indent rule of Python very much. > > My only ?problem? was that I used to put debug statement at the > beginning of a line to see immediately what the debug statements > where. But that was a small price to pay. ;-) > > -- > Cecil Westerhof > Senior Software Engineer > LinkedIn: http://www.linkedin.com/in/cecilwesterhof > -- > https://mail.python.org/mailman/listinfo/python-list > From bgailer at gmail.com Fri Jan 27 15:09:48 2017 From: bgailer at gmail.com (bob gailer) Date: Fri, 27 Jan 2017 15:09:48 -0500 Subject: Need reviews for my book on introductory python In-Reply-To: References: <8115024f-71c7-d526-37d4-65cfae9be207@gmail.com> Message-ID: On 1/26/2017 8:05 PM, Sandeep Nagar wrote: > Hi > > As I mentioned, a scaled down version is available for free at > bookmuft.Com which can be used to judge in this case. Maybe I am blind, but I don't see any mention of bookmuft.Co. > > Regards > > On Jan 27, 2017 05:02, "bob gailer" > wrote: > > On 1/25/2017 9:25 PM, Sandeep Nagar wrote: > > Hi, > > A few month ago I wrote a book on introductory python based on > my experinces while teaching python to Bachelor students of > engineering. It is now available in e-book and paperback > format at Amazon. > > https://www.amazon.com/dp/1520153686 > > > The book is written for beginners of python programming > language and written in learn-by-doing manner. If some group > members can review the same, it will be useful for myself to > come up with an improved version. > > I'd like to review it, but am reluctant to put out money for a > copy! How about offering a review copy at no charge? > > > Other similar books of mine are based on Octave and Scilab > with following links: > > https://www.amazon.com/dp/152015111X > (Scilab) > > https://www.amazon.com/dp/1520158106 > (Octave) > > If you are interested in open source computing, please have a > look. > > Also please do share the link for print books with your > colleagues at other universities and recommend them for > libraries and researchers, if you feel that they can be > helpful to them. > > Regards > > Sandeep > > > > -- > Image and video hosting by TinyPic > -- Image and video hosting by TinyPic From bgailer at gmail.com Fri Jan 27 15:17:14 2017 From: bgailer at gmail.com (bob gailer) Date: Fri, 27 Jan 2017 15:17:14 -0500 Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: On 1/25/2017 9:25 PM, Sandeep Nagar wrote: > Hi, > > A few month ago I wrote a book on introductory python based on my experinces while teaching python to Bachelor students of engineering. It is now available in e-book and paperback format at Amazon. > > https://www.amazon.com/dp/1520153686 > > The book is written for beginners of python programming language and written in learn-by-doing manner. If some group members can review the same, it will be useful for myself to come up with an improved version. Who is the publisher of this book? I just took a look at the pages that can be viewed on Amazon. Many reactions. it is hard for me to write this, as it seems it would sound harsh, judgemental, unappreciative. But you did ask for a review, and this is my honest reaction. I find it hard to read a book written by a non-native speaker of English. I an constantly having to overlook what to me are spelling, grammatical and vocabulary errors. I HIGHLY recommend you find an editor who can fix these errors. When I pick up an "Introduction to ....." book I expect to get right into the language. History, interpreted vs compiled, examples in C, if included at all should go in an appendix. I think you should be teaching Python 3 rather than 2. I disagree with your reasons for sticking with ver 2. I, as a developer using Python for over 10 years, welcomed ver 3 and now use it exclusively. Last sentence of 2.2 is confusing. Section 2.3 you need to tell reader how to obtain a python prompt. The example is also confusing, since to most python users it looks like: >>> 2+4 6 The result of 2+4. does not appear. sudo apt-get won't work on Windows. Tell the reader that this is how to do it in Unix, and show the Windows equivalent. I would avoid showing from xxx import * as it is likely to cause confusion when a module so imported overwrites a built-in. Bottom of p 23 (sys.sizeof(). How would a python newbie know to import sys? I will stop here - there are many other issues. I am, by degree, an engineer. If this were my introduction to python I probably would walk away from python, discouraged by how hard it is to learn. From darcy at vex.net Fri Jan 27 15:48:06 2017 From: darcy at vex.net (D'Arcy Cain) Date: Fri, 27 Jan 2017 15:48:06 -0500 Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: <551ac389-a89d-fac3-6b8f-e505fe1789ed@vex.net> On 2017-01-27 03:17 PM, bob gailer wrote: > sudo apt-get won't work on Windows. Tell the reader that this is how to > do it in Unix, and show the Windows equivalent. Actually it doesn't work on Unix either. It only works on Linux. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From jon+usenet at unequivocal.eu Fri Jan 27 16:06:50 2017 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Fri, 27 Jan 2017 21:06:50 -0000 (UTC) Subject: Need reviews for my book on introductory python References: <551ac389-a89d-fac3-6b8f-e505fe1789ed@vex.net> Message-ID: On 2017-01-27, D'Arcy Cain wrote: > On 2017-01-27 03:17 PM, bob gailer wrote: >> sudo apt-get won't work on Windows. Tell the reader that this is how to >> do it in Unix, and show the Windows equivalent. > > Actually it doesn't work on Unix either. It only works on Linux. Actually it doesn't work on Linux either. It only works on Debian-derived systems. From none at invalid.com Fri Jan 27 16:18:15 2017 From: none at invalid.com (mm0fmf) Date: Fri, 27 Jan 2017 21:18:15 +0000 Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: On 27/01/2017 20:17, bob gailer wrote: > On 1/25/2017 9:25 PM, Sandeep Nagar wrote: >> Hi, >> >> A few month ago I wrote a book on introductory python based on my >> experinces while teaching python to Bachelor students of engineering. >> It is now available in e-book and paperback format at Amazon. >> >> https://www.amazon.com/dp/1520153686 >> >> The book is written for beginners of python programming language and >> written in learn-by-doing manner. If some group members can review the >> same, it will be useful for myself to come up with an improved version. > Who is the publisher of this book? > > I just took a look at the pages that can be viewed on Amazon. Many > reactions. it is hard for me to write this, as it seems it would sound > harsh, judgemental, unappreciative. But you did ask for a review, and > this is my honest reaction. > > I find it hard to read a book written by a non-native speaker of > English. I an constantly having to overlook what to me are spelling, > grammatical and vocabulary errors. I HIGHLY recommend you find an editor > who can fix these errors. > Snap. I found it impossible to read and never got to the Python parts. It is not written in English. Most of the pronouns and conjunctions are missing. It looks like it has not been proof-read as words loose capitalisation, many are mis-spelt and grammar rules regarding plural cases and agreement are just ignored. From python at mrabarnett.plus.com Fri Jan 27 16:36:45 2017 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 27 Jan 2017 21:36:45 +0000 Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: <7e57adfc-60f5-1d6c-fb4a-0613e06ab396@mrabarnett.plus.com> On 2017-01-27 21:18, mm0fmf wrote: > On 27/01/2017 20:17, bob gailer wrote: >> On 1/25/2017 9:25 PM, Sandeep Nagar wrote: >>> Hi, >>> >>> A few month ago I wrote a book on introductory python based on my >>> experinces while teaching python to Bachelor students of engineering. >>> It is now available in e-book and paperback format at Amazon. >>> >>> https://www.amazon.com/dp/1520153686 >>> >>> The book is written for beginners of python programming language and >>> written in learn-by-doing manner. If some group members can review the >>> same, it will be useful for myself to come up with an improved version. >> Who is the publisher of this book? >> >> I just took a look at the pages that can be viewed on Amazon. Many >> reactions. it is hard for me to write this, as it seems it would sound >> harsh, judgemental, unappreciative. But you did ask for a review, and >> this is my honest reaction. >> >> I find it hard to read a book written by a non-native speaker of >> English. I an constantly having to overlook what to me are spelling, >> grammatical and vocabulary errors. I HIGHLY recommend you find an editor >> who can fix these errors. >> > Snap. I found it impossible to read and never got to the Python parts. > It is not written in English. Most of the pronouns and conjunctions are > missing. It looks like it has not been proof-read as words loose > capitalisation, many are mis-spelt and grammar rules regarding plural > cases and agreement are just ignored. > "loose"? Don't you mean "lose"? (Or possible "lack"?) From rgacote at appropriatesolutions.com Fri Jan 27 17:09:54 2017 From: rgacote at appropriatesolutions.com (Ray Cote) Date: Fri, 27 Jan 2017 17:09:54 -0500 Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: On Fri, Jan 27, 2017 at 4:18 PM, mm0fmf wrote: > On 27/01/2017 20:17, bob gailer wrote: > >> On 1/25/2017 9:25 PM, Sandeep Nagar wrote: >> >>> Hi, >>> >>> A few month ago I wrote a book on introductory python based on my >>> experinces while teaching python to Bachelor students of engineering. >>> It is now available in e-book and paperback format at Amazon. >>> >>> https://www.amazon.com/dp/1520153686 >>> >>> The book is written for beginners of python programming language and >>> written in learn-by-doing manner. If some group members can review the >>> same, it will be useful for myself to come up with an improved version. >>> >> Who is the publisher of this book? >> >> I just took a look at the pages that can be viewed on Amazon. Many >> reactions. it is hard for me to write this, as it seems it would sound >> harsh, judgemental, unappreciative. But you did ask for a review, and >> this is my honest reaction. >> >> I find it hard to read a book written by a non-native speaker of >> English. I an constantly having to overlook what to me are spelling, >> grammatical and vocabulary errors. I HIGHLY recommend you find an editor >> who can fix these errors. >> >> Snap. I found it impossible to read and never got to the Python parts. It > is not written in English. Most of the pronouns and conjunctions are > missing. It looks like it has not been proof-read as words loose > capitalisation, many are mis-spelt and grammar rules regarding plural cases > and agreement are just ignored. > > In addition to what other?s have said: - congrats on taking the time to write a book and put it out for others. - Introduction to Python section 2.1 starts with C example. Seems out of place. - Search inside the book has 0 results for iterator -- how do you do Python without iterators? - Two references to generator, but they are random number generator references. - Was confused by the switching in and out of numpy usage. - And yes, Python3 is the way to go. Been writing Python since 1.5 and about 90% of my new code is Python 3.5. From python at lucidity.plus.com Fri Jan 27 18:11:15 2017 From: python at lucidity.plus.com (Erik) Date: Fri, 27 Jan 2017 23:11:15 +0000 Subject: Need reviews for my book on introductory python In-Reply-To: <7e57adfc-60f5-1d6c-fb4a-0613e06ab396@mrabarnett.plus.com> References: <7e57adfc-60f5-1d6c-fb4a-0613e06ab396@mrabarnett.plus.com> Message-ID: On 27/01/17 21:36, MRAB wrote: > "loose"? Don't you mean "lose"? (Or possible "lack"?) Don't you mean 'Or possibly "lack"'? https://en.wikipedia.org/wiki/Muphry%27s_law ;) E. From ian.g.kelly at gmail.com Fri Jan 27 18:12:14 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 27 Jan 2017 16:12:14 -0700 Subject: Need reviews for my book on introductory python In-Reply-To: References: <8115024f-71c7-d526-37d4-65cfae9be207@gmail.com> Message-ID: On Jan 27, 2017 2:13 PM, "bob gailer" wrote: On 1/26/2017 8:05 PM, Sandeep Nagar wrote: > Hi > > As I mentioned, a scaled down version is available for free at > bookmuft.Com which can be used to judge in this case. > Maybe I am blind, but I don't see any mention of bookmuft.Co. I pulled up the PDF from bookmuft.com (which I also do not see mentioned in the original post). 2.1 page 15: "For example, suppose line 5 of a python program has syntax error, in this case the program will executes all commands till line 4 and will then show an error." This is factually incorrect. A program with a syntax error will not be executed at all. The description of the Python interpreter is also pretty far from the truth. The interpreter does not read Python source code one line at a time as described. The interpreter only reads Python bytecode. At the point that the interpreter starts reading bytecode, the Python compiler has already compiled the entire source file into bytecode. This description only applies to CPython. Other Python implementations may work differently. Page 17: "When the programs compose of hundreds and thousands of lines, a compilation process will yield a faster result because the object code needs to be only compiled once and then run directly on microprocessor. Whereas an interpreted code will check for interpretations each time it needs to be processed." Again, it's only Python byte code that is interpreted. The actual Python source is compiled and does not need to be reprocessed each time a line of code is executed. 3.2, page 21: "logical: This type of data stores boolean values True or False boolean values and can be operated by boolean operators like AND, OR etc." The type is named "bool", not "logical". "Most programming languages use the values 1 or 0 for boolean values but python differs in this approach." It's not really important, but I disagree with that assessment of "most programming languages", at least in regard to modern or high-level ones. 3.3, page 22: "There are four types of numeric data types" (listing int, long, float and complex) You left out Decimal and Fraction. 3.3.1: "Python has arbitrary precision for float, long, complex, hence the limit to length of these numbers is subject to availability of memory" Uh, no, that's only true for long. A Python float only has the same precision as a C double. Same for complex but there are two components, each with the precision of a C double. 3.3.2, page 24: "The issue with floating point number based arithmetic is that the answer is an approximation of real number since real numbers are defined for 10 as their base whereas computer works with numbers where 2 is used as the base." This is also incorrect. The definition of real numbers has nothing whatsoever to do with base 10, and there are plenty of real numbers that still could not be represented exactly even if float used decimal digits instead of binary digits, and even if it did have arbitrary precision. For example, 1/3. For another example, pi. "Above calculation shows that 0.123_2 = 0.135_10." Say what? First of all, 0.123 isn't even a valid number in binary. The only digits available in base 2 are 0 and 1. Secondly, even if we ignore that, the result of 1/2 + 2/2^2 + 3/2^3 = 1.375, not 0.135. Page 25: "One can use the decimal module which has a function Decimal() that returns the number as stored by the computer." That is not even close to an accurate description. First, Decimal is a type, not a function. It differs from float in two important respects: one, it uses a base-ten representation instead of base two; two, it actually does have arbitrary precision. Also, it does not return the number as stored by the computer. It's a completely separate implementation of real numbers. "As seen above, Decimal(0.123) occupies 72 bits and hence is more accurate approximation as compared to 0.123 which occupies 24 bits" Bytes, not bits. But also, Decimal(0.123) is no more accurate an approximation than 0.123 because you constructed it from 0.123 which is already an approximation. It's actually the exact same approximation. Garbage in, garbage out. Decimal("0.123") on the other hand (note the use of a string rather than a float as the constructor argument) denotes exactly the real number 0.123. 3.4, page 26: All of the information in this section about characters and encodings is specific to strings and does not pertain to sequences generally. "Python also deal with characters using data type string, list, tuple." List and tuple have nothing to do with characters. 3.6, page 28: "Mapping is a scheme of defining data where each element is identified with a key called ?hash tag?." I think you mean "hash value". A hash tag is something one uses when posting on Twitter. Page 29: "In example above, we created a dictionary containing two characters a and b identified by two keys 1 and 10." In the example given, 'a' and 'b' are the keys and 1 and 10 are the values. That's about all I have the energy for. These are the most egregious issues that I've spotted, but there are loads of others that really need the services of a professional editor. As it stands I wouldn't recommend this book to anybody. From ethan at stoneleaf.us Fri Jan 27 18:26:38 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 27 Jan 2017 15:26:38 -0800 Subject: Need reviews for my book on introductory python In-Reply-To: References: <7e57adfc-60f5-1d6c-fb4a-0613e06ab396@mrabarnett.plus.com> Message-ID: <588BD72E.7030701@stoneleaf.us> On 01/27/2017 03:11 PM, Erik wrote: > On 27/01/17 21:36, MRAB wrote: >> "loose"? Don't you mean "lose"? (Or possible "lack"?) > > Don't you mean 'Or possibly "lack"'? And this is why books should be reviewed and edited /before publishing/. ;) -- ~Ethan~ From tjreedy at udel.edu Fri Jan 27 23:01:59 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 27 Jan 2017 23:01:59 -0500 Subject: Need reviews for my book on introductory python In-Reply-To: References: <8115024f-71c7-d526-37d4-65cfae9be207@gmail.com> Message-ID: On 1/27/2017 6:12 PM, Ian Kelly wrote: > On Jan 27, 2017 2:13 PM, "bob gailer" wrote: > > On 1/26/2017 8:05 PM, Sandeep Nagar wrote: > >> Hi >> >> As I mentioned, a scaled down version is available for free at >> bookmuft.Com which can be used to judge in this case. >> > Maybe I am blind, but I don't see any mention of bookmuft.Co. > > > I pulled up the PDF from bookmuft.com (which I also do not see mentioned in > the original post). > > 2.1 page 15: > > "For example, suppose line 5 of a python program has syntax error, in this > case the program will executes all commands till line 4 and will then show > an error." This is a run-on sentence, which is to say, two sentences. Change 'suntax,' to 'syntax.' and 'in' to ' In'. > This is factually incorrect. A program with a syntax error will not be > executed at all. > > The description of the Python interpreter is also pretty far from the > truth. The interpreter does not read Python source code one line at a time > as described. The interpreter only reads Python bytecode. At the point that > the interpreter starts reading bytecode, the Python compiler has already > compiled the entire source file into bytecode. > > This description only applies to CPython. Other Python implementations may > work differently. > > Page 17: > > "When the programs compose of hundreds and thousands of lines, a > compilation process will yield a faster result because the object code > needs to be only compiled once and then run directly on microprocessor. > Whereas an interpreted code will check for interpretations each time it > needs to be processed." "When programs are composed of ..." > Again, it's only Python byte code that is interpreted. The actual Python > source is compiled and does not need to be reprocessed each time a line of > code is executed. > > 3.2, page 21: > > "logical: This type of data stores boolean values True or False boolean > values and can be operated by boolean operators like AND, OR etc." > > The type is named "bool", not "logical". > > "Most programming languages use the values 1 or 0 for boolean values but > python differs in this approach." > > It's not really important, but I disagree with that assessment of "most > programming languages", at least in regard to modern or high-level ones. > > 3.3, page 22: > > "There are four types of numeric data types" (listing int, long, float and > complex) In 3.x, 'int' was delected and 'long' was renamed 'int'. I strongly suggest that the next version be written for 3.x with 2.x-only info either deleted or relegated to footnotes (or parenthetical notes). -- Terry Jan Reedy From darcy at vex.net Fri Jan 27 23:12:59 2017 From: darcy at vex.net (D'Arcy Cain) Date: Fri, 27 Jan 2017 23:12:59 -0500 Subject: Need reviews for my book on introductory python In-Reply-To: References: <8115024f-71c7-d526-37d4-65cfae9be207@gmail.com> Message-ID: <05b91269-e562-b9a4-147b-f1b9b100f5ab@vex.net> On 2017-01-27 11:01 PM, Terry Reedy wrote: > This is a run-on sentence, which is to say, two sentences. Change > 'suntax,' to 'syntax.' and 'in' to ' In'. ^^^^^^ I can't believe how many typos and grammar errors there are in this thread by people correcting typos and grammar. And please don't say "suntax". The government hasn't thought of that one yet. Don't give them ideas. -- D'Arcy J.M. Cain System Administrator, Vex.Net http://www.Vex.Net/ IM:darcy at Vex.Net VoIP: sip:darcy at Vex.Net From rustompmody at gmail.com Sat Jan 28 00:47:50 2017 From: rustompmody at gmail.com (Rustom Mody) Date: Fri, 27 Jan 2017 21:47:50 -0800 (PST) Subject: Need reviews for my book on introductory python In-Reply-To: References: Message-ID: <40a0350b-be77-4826-82c8-174e43c3c067@googlegroups.com> On Thursday, January 26, 2017 at 7:55:57 AM UTC+5:30, Sandeep Nagar wrote: > Hi, > > A few month ago I wrote a book on introductory python based on my experinces while teaching python to Bachelor students of engineering. It is now available in e-book and paperback format at Amazon. > > https://www.amazon.com/dp/1520153686 > > The book is written for beginners of python programming language and written in learn-by-doing manner. If some group members can review the same, it will be useful for myself to come up with an improved version. > > Other similar books of mine are based on Octave and Scilab with following links: > > https://www.amazon.com/dp/152015111X (Scilab) > > https://www.amazon.com/dp/1520158106 (Octave) > > If you are interested in open source computing, please have a look. Friendly advice: Write a blog instead of, or at least before writing a book Sure there are disadvantages... which are IMHO minor/unrealistic: eg YOur intended audience has no access to internet. About as unrealistic as them not having access to electricity Advantages - Very low bar on entry - Much finer granularity ? one post vs one book - Much tighter write?feedback?correct cycle: Minor corrections can be just edited in/out. Major additions get a new post - More current medium ?Medium is the message (or massage)!? - Saves trees [Personal note: I started blogging on programming related topics a few years ago: http://blog.languager.org/ Which crossed 50K hits a few months ago. More gratifying, Ive received comments and thanks from people like Doug McIllroy, co-creator of Unix] From steve+python at pearwood.info Sat Jan 28 02:35:01 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 28 Jan 2017 18:35:01 +1100 Subject: Need reviews for my book on introductory python References: <8115024f-71c7-d526-37d4-65cfae9be207@gmail.com> <05b91269-e562-b9a4-147b-f1b9b100f5ab@vex.net> Message-ID: <588c49a7$0$1618$c3e8da3$5496439d@news.astraweb.com> On Sat, 28 Jan 2017 03:12 pm, D'Arcy Cain wrote: > I can't believe how many typos and grammar errors there are in this > thread by people correcting typos and grammar. That's practically a law of physics: the Iron Law of Nitpicking, better known as Muphry's Law. https://en.wikipedia.org/wiki/Muphry's_law -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From none at invalid.com Sat Jan 28 02:57:49 2017 From: none at invalid.com (mm0fmf) Date: Sat, 28 Jan 2017 07:57:49 +0000 Subject: Need reviews for my book on introductory python In-Reply-To: References: <7e57adfc-60f5-1d6c-fb4a-0613e06ab396@mrabarnett.plus.com> Message-ID: On 27/01/2017 21:36, MRAB wrote: > On 2017-01-27 21:18, mm0fmf wrote: >> On 27/01/2017 20:17, bob gailer wrote: >>> On 1/25/2017 9:25 PM, Sandeep Nagar wrote: >>>> Hi, >>>> >>>> A few month ago I wrote a book on introductory python based on my >>>> experinces while teaching python to Bachelor students of engineering. >>>> It is now available in e-book and paperback format at Amazon. >>>> >>>> https://www.amazon.com/dp/1520153686 >>>> >>>> The book is written for beginners of python programming language and >>>> written in learn-by-doing manner. If some group members can review the >>>> same, it will be useful for myself to come up with an improved version. >>> Who is the publisher of this book? >>> >>> I just took a look at the pages that can be viewed on Amazon. Many >>> reactions. it is hard for me to write this, as it seems it would sound >>> harsh, judgemental, unappreciative. But you did ask for a review, and >>> this is my honest reaction. >>> >>> I find it hard to read a book written by a non-native speaker of >>> English. I an constantly having to overlook what to me are spelling, >>> grammatical and vocabulary errors. I HIGHLY recommend you find an editor >>> who can fix these errors. >>> >> Snap. I found it impossible to read and never got to the Python parts. >> It is not written in English. Most of the pronouns and conjunctions are >> missing. It looks like it has not been proof-read as words loose >> capitalisation, many are mis-spelt and grammar rules regarding plural >> cases and agreement are just ignored. >> > "loose"? Don't you mean "lose"? (Or possible "lack"?) > My spelling is not brilliant especially when typing live and so you have proved why books need proof-reading. In particular "lose" is what I meant. Python, the name of the language, is a proper noun and should be captialised. In the few pages I read, Python appears as "python" and "Python", randomly losing the capital letter. Lacking capitalisation would suggest to me that some or all words are consistently written without the capital letter such as always writing "united states of america". From steve+python at pearwood.info Sat Jan 28 03:03:42 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 28 Jan 2017 19:03:42 +1100 Subject: Is shutil.get_terminal_size useless? Message-ID: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> shutil.get_terminal_size returns the wrong values when you pipe your output to another process, even it you do so in a terminal. Consider this script: import os import shutil print('shutil:', shutil.get_terminal_size(fallback=(999, 999))) print('os:', os.get_terminal_size(0)) That uses two different methods to get the terminal size. If I run that in a terminal in the normal way, it works fine, returning the correct size for my terminal: [steve at ando ~]$ python3.5 test_gts.py shutil: os.terminal_size(columns=116, lines=29) os: os.terminal_size(columns=116, lines=29) But if I pipe the output to something else, the shutil version fails to determine the correct terminal size, and falls back on the default: [steve at ando ~]$ python3.5 test_gts.py | cat shutil: os.terminal_size(columns=999, lines=999) os: os.terminal_size(columns=116, lines=29) while the os version gives the correct result. Is shutil.get_terminal_size useless? When, if ever, should I use it in preference to the os version? If the shutil version is broken, can it be fixed? Thanks to Bernardas Ali?auskas: http://granitosaurus.rocks/getting-terminal-size.html -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From rosuav at gmail.com Sat Jan 28 03:27:20 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 28 Jan 2017 19:27:20 +1100 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Jan 28, 2017 at 7:03 PM, Steve D'Aprano wrote: > But if I pipe the output to something else, the shutil version fails to > determine the correct terminal size, and falls back on the default: > > > [steve at ando ~]$ python3.5 test_gts.py | cat > shutil: os.terminal_size(columns=999, lines=999) > os: os.terminal_size(columns=116, lines=29) > > > while the os version gives the correct result. I believe the problem here is your definition of "correct". When you inquire of the os module, you're asking, at a fairly low level, what the terminal window is sized to. But when you ask shutil, you're asking what a shell utility should do. Quite a few programs change in behaviour when piped into something else (for instance, 'ls' will often apply colour and columnate its text, but if you pipe it into grep, you don't want any of that), and shutil is acknowledging that different effect. Both are correct answers - to different questions. ChrisA From __peter__ at web.de Sat Jan 28 03:39:57 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 28 Jan 2017 09:39:57 +0100 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steve D'Aprano wrote: > shutil.get_terminal_size returns the wrong values when you pipe your > output to another process, even it you do so in a terminal. Consider this > script: > > > import os > import shutil > print('shutil:', shutil.get_terminal_size(fallback=(999, 999))) > print('os:', os.get_terminal_size(0)) > > > That uses two different methods to get the terminal size. If I run that in > a terminal in the normal way, it works fine, returning the correct size > for my terminal: > > > [steve at ando ~]$ python3.5 test_gts.py > shutil: os.terminal_size(columns=116, lines=29) > os: os.terminal_size(columns=116, lines=29) > > > But if I pipe the output to something else, the shutil version fails to > determine the correct terminal size, and falls back on the default: > > > [steve at ando ~]$ python3.5 test_gts.py | cat > shutil: os.terminal_size(columns=999, lines=999) > os: os.terminal_size(columns=116, lines=29) > > > while the os version gives the correct result. > > Is shutil.get_terminal_size useless? When, if ever, should I use it in > preference to the os version? If the shutil version is broken, can it be > fixed? One potential advantage of shutil.get_terminal_size() is that you can affect it with an environment variable: $ python3 test_gts.py | cat shutil: os.terminal_size(columns=999, lines=999) os: os.terminal_size(columns=72, lines=48) $ COLUMNS=123 python3 test_gts.py | cat shutil: os.terminal_size(columns=123, lines=999) os: os.terminal_size(columns=72, lines=48) I have the line export LINES COLUMNS in my .bashrc, so by default I see the physical size: $ export COLUMNS LINES $ python3 test_gts.py | cat shutil: os.terminal_size(columns=72, lines=48) os: os.terminal_size(columns=72, lines=48) > Thanks to Bernardas Ali?auskas: > > http://granitosaurus.rocks/getting-terminal-size.html From hany.amin.mishriky at gmail.com Sat Jan 28 04:42:38 2017 From: hany.amin.mishriky at gmail.com (hany.amin.mishriky at gmail.com) Date: Sat, 28 Jan 2017 01:42:38 -0800 (PST) Subject: GUI Message-ID: hay , i am new in the coding world,i would like to understand how a python program is communicating with GUI, for example, if i have a code that require the user to enter a value ,then this code will do some calculations and return a value to the user, how to do that? From steve+python at pearwood.info Sat Jan 28 05:49:10 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 28 Jan 2017 21:49:10 +1100 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> On Sat, 28 Jan 2017 07:27 pm, Chris Angelico wrote: > On Sat, Jan 28, 2017 at 7:03 PM, Steve D'Aprano > wrote: >> But if I pipe the output to something else, the shutil version fails to >> determine the correct terminal size, and falls back on the default: >> >> >> [steve at ando ~]$ python3.5 test_gts.py | cat >> shutil: os.terminal_size(columns=999, lines=999) >> os: os.terminal_size(columns=116, lines=29) >> >> >> while the os version gives the correct result. > > I believe the problem here is your definition of "correct". That would be the dictionary definition: Correct - conformable to truth; not faulty; free from error :-P > When you > inquire of the os module, you're asking, at a fairly low level, what > the terminal window is sized to. Right: I want to know what the terminal window is sized to. > But when you ask shutil, you're > asking what a shell utility should do. No, I'm asking what the terminal window is sized to. That's why the function is called get_terminal_size(), not what_should_a_shell_utility_do(). What should a shell utility do, under what circumstances? How can any function answer that? > Quite a few programs change in > behaviour when piped into something else (for instance, 'ls' will > often apply colour and columnate its text, but if you pipe it into > grep, you don't want any of that), Sure, and if I wanted to know if standard output was being piped to something else, I'd expect to call a function called something like where_is_std_out_being_piped_to(). The terminal size doesn't change just because I'm piping output to another process. Using the terminal size as a proxy for "being piped" is sheer insanity. That would be like calling len(some_string) and expecting it to return None if some_string was all uppercase. > and shutil is acknowledging that different effect. Before there can be a *different* effect, there needs to be an *original* effect, and I have no idea what effects you are referring to. Be precise, please. I don't dispute that there are times where a process may wish to change behaviour when being piped to something else. But that's a red herring: I'm asking what the terminal size is. I expect to get the terminal size, or perhaps an exception if there is no terminal, with an *optional* default, not a mandatory one. So far it sounds like shutil.get_terminal_size() is broken by design. Can somebody convince me it isn't? > Both are correct answers - to different questions. But I'm only asking one question: what's the terminal size? It sounds like your answer is just a long, round-about way of saying "Yes, it's useless. You cannot trust it to return the terminal size, even when there is a terminal." I haven't even asked what happens if it is called when there is no associated terminal... but now I'm afraid that if I do, it will return the number of CPUs in my computer or something... -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Sat Jan 28 06:00:57 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sat, 28 Jan 2017 22:00:57 +1100 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: <588c79eb$0$1603$c3e8da3$5496439d@news.astraweb.com> On Sat, 28 Jan 2017 07:39 pm, Peter Otten wrote: > One potential advantage of shutil.get_terminal_size() is that you can > affect it with an environment variable: > > $ python3 test_gts.py | cat > shutil: os.terminal_size(columns=999, lines=999) > os: os.terminal_size(columns=72, lines=48) > > $ COLUMNS=123 python3 test_gts.py | cat > shutil: os.terminal_size(columns=123, lines=999) > os: os.terminal_size(columns=72, lines=48) Unless your terminal *actually is* 123 columns wide, I don't see that as an advantage. I see that as "Hey look, we can fool shutil into returning absolute garbage instead of the terminal size!" I can already read environment variables using os.getenv() and os.environ, so this gives me no new functionality. > I have the line > > export LINES COLUMNS > > in my .bashrc, so by default I see the physical size: > > $ export COLUMNS LINES > $ python3 test_gts.py | cat > shutil: os.terminal_size(columns=72, lines=48) > os: os.terminal_size(columns=72, lines=48) But what happens if you resize the terminal while the process is running? Again, the os.get_terminal_size() correctly returns the updated size, while shutil.get_terminal_size() doesn't. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From rosuav at gmail.com Sat Jan 28 06:50:07 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 28 Jan 2017 22:50:07 +1100 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Jan 28, 2017 at 9:49 PM, Steve D'Aprano wrote: > The terminal size doesn't change just because I'm piping output to another > process. Using the terminal size as a proxy for "being piped" is sheer > insanity. In a sense, there _is no_ terminal size when you're being piped to another process. Likewise if you're running in some detached (non-terminal) context, or if you're being run over some remote link that hasn't transmitted terminal size info, etc, etc, etc, etc. It's not a proxy for "being piped" - it's that when your output isn't going to a terminal, asking "what is my terminal size" isn't particularly productive. Would you expect that a process started from systemd is told about the size of the terminal in which you ran "systemctl start servicename"? I doubt it. Would you expect a cronjob to use the terminal size when you most recently edited crontab? No. So why should a program that's being piped into something else automatically assume the size of the other program's terminal? You might well be a completely background process. You can still ask the concrete question about terminal size, and that's in the 'os' module. The 'shutil' module also looks at environment variables, and probably would be the place to respond to TELNET NAWS if that has any sort of primary-level support. ChrisA From eryksun at gmail.com Sat Jan 28 07:09:53 2017 From: eryksun at gmail.com (eryk sun) Date: Sat, 28 Jan 2017 12:09:53 +0000 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Jan 28, 2017 at 8:03 AM, Steve D'Aprano wrote: > print('shutil:', shutil.get_terminal_size(fallback=(999, 999))) > print('os:', os.get_terminal_size(0)) [snip] > But if I pipe the output to something else, the shutil version fails to > determine the correct terminal size, and falls back on the default: The high-level shutil wrapper uses sys.__stdout__, without falling back on sys.__stdin__ or sys.__stderr__, so it has to rely on the environment variables and/or fallback value when stdout isn't a terminal. OTOH, in this case you're calling os.get_terminal_size(0) on the STDIN file descriptor, which obviously works -- at least on Unix -- because it's a tty. On Windows that generally won't work because getting the screen size requires a handle for a screen buffer, not an input buffer. In this case, os.get_terminal_size(2) would generally work on Windows because stderr hasn't been redirected. I just wrote an extended version of shutil.get_terminal_size that tries six (minus two) ways to Sunday to get the terminal size, starting with /dev/tty on Unix and CONOUT$ on Windows. It's needlessly complicated by the Windows implementation of os.get_terminal_size, which is strangely hard coded. os.get_terminal_size calls GetStdHandle with a fake mapping of 0, 1, and 2 to the standard handles. It should just call get_osfhandle on the given file descriptor. That way someone could use something like the following: with open('CONOUT$', 'r+') as conout: size = os.get_terminal_size(conout.fileno()) which would be guaranteed to work if the process is attached to a console. The current implementation forces one to temporarily modify a standard handle, which has to be gated by a lock for thread safety. From PointedEars at web.de Sat Jan 28 07:49:52 2017 From: PointedEars at web.de (Thomas 'PointedEars' Lahn) Date: Sat, 28 Jan 2017 13:49:52 +0100 Subject: Reviews of book on introductory Python (was: Need reviews for my book on introductory python) References: <7e57adfc-60f5-1d6c-fb4a-0613e06ab396@mrabarnett.plus.com> Message-ID: <2259315.Lt9SDvczpP@PointedEars.de> mm0fmf wrote: > [?] Python, the name of the language, is a proper noun and should be > captialised. In the few pages I read, Python appears as "python" and > "Python", randomly losing the capital letter. Lacking capitalisation > would suggest to me that some or all words are consistently written > without the capital letter such as always writing "united states of > america". I have not read the book, but it should be noted that lack of capitalisation can be intentional. For example, ?Python? would refer to the programming language, while ?python? would refer to the command/program with which the language compiler/interpreter is executed. The latter can appear in prose as well, and should, by contrast to the former, be set in a monospace type. If it is truly capitalised randomly, though, as indicated by the OP?s Subject header field value (until before now repeated by everyone else), then it is of course badly written. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail. From __peter__ at web.de Sat Jan 28 07:53:55 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 28 Jan 2017 13:53:55 +0100 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c79eb$0$1603$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steve D'Aprano wrote: >> One potential advantage of shutil.get_terminal_size() is that you can >> affect it with an environment variable: >> >> $ python3 test_gts.py | cat >> shutil: os.terminal_size(columns=999, lines=999) >> os: os.terminal_size(columns=72, lines=48) >> >> $ COLUMNS=123 python3 test_gts.py | cat >> shutil: os.terminal_size(columns=123, lines=999) >> os: os.terminal_size(columns=72, lines=48) > > Unless your terminal *actually is* 123 columns wide, I don't see that as > an advantage. But the script does not write to the terminal and has no way (?) of knowing whether the pipe ends in the terminal or in a file. Assuming it does end in the terminal seems like a good default to me, and setting COLUMNS provides a uniform way to override that default. I prefer that to what e. g. aptitude does which uses the full width of the terminal window but falls back to 80 $ aptitude search foo p bygfoot - Fu?ball-Managerspiel ... $ aptitude search foo | grep python p python-foolscap - RPC-System auf Basis der Objektf?higkeiten p python-rfoo - Fast RPC package for Python (and a To get back well-behaved output you need both the envvar and a commandline option: $ aptitude search -w $COLUMNS foo | grep python p python-foolscap - RPC-System auf Basis der Objektf?higk > I see that as "Hey look, we can fool shutil into returning > absolute garbage instead of the terminal size!" There are valid reasons for temporarily altering the number of columns, like writing to a file or preparing a code sample. > I can already read environment variables using os.getenv() and os.environ, > so this gives me no new functionality. shutil.get_terminal_size() combines access to shell variables and the os, and IMHO that is a useful approach. From __peter__ at web.de Sat Jan 28 08:58:54 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 28 Jan 2017 14:58:54 +0100 Subject: GUI References: Message-ID: hany.amin.mishriky at gmail.com wrote: > hay , i am new in the coding world,i would like to understand how a python > program is communicating with GUI, for example, if i have a code that > require the user to enter a value ,then this code will do some > calculations and return a value to the user, how to do that? A command line script adding two numbers will look like this: x = input_int("Enter x:") y = input_int("Enter y:") print("x + y =", x + y) This script will not continue to ask for y until the user has entered the value for x In a GUI you have to provide widgets that allow the user to interact with the GUI . Let's assume make_widget() creates an entry widget and shows it in a window. Then we can create the part of the GUI where the user enters the x and y value with: entry_x = make_widget() entry_y = make_widget() Now we need one more widget to show the result: entry_sum = make_widget() We also need a function that recalculates the contents of entry_sum whenever the user changes x or y: def recalculate_sum(): x = int(entry_x.get_value()) y = int(entry_y.get_value()) entry_sum.set_value(sigma) But when should this function run? We have to connect it to the widgets on whose changes it should react. Such a function is called "callback": # assume that every time the contents of entry_x/y change it calls # self.on_change() entry_x.on_change = recalculate_sum entry_y.on_change = recalculate_sum Finally you enter an infinite loop that waits for and handles GUI events, the most interesting being user activities like typing a digit into one of your widgets. run_event_loop() There a many GUIs, but they all tend to work in a way similar to the one sketched above. Here's a translation of the above pseudo-code to tkinter: import tkinter as tk def make_entry(caption, row): """Create a Label and an Entry. Return the StringVar associated with the Entry. """ # the text displayed to the left of the Entry label = tk.Label(root, text=caption) label.grid(row=row, column=0) var = tk.StringVar() # holds the text entered into the Entry entry = tk.Entry(root, textvariable=var) entry.grid(row=row, column=1, sticky=tk.EW) return var def calculate_sum(*args): """Callback invoked on changes of x or y The GUI passes some arguments (*args) that we are not interested in. """ try: x = int(var_x.get()) y = int(var_y.get()) sigma = x + y except ValueError as err: # if x or y aren't valid integers # show an error message instead of the sum sigma = err var_sum.set(sigma) # Create the main window root = tk.Tk() # Have the second column take all extra space root.columnconfigure(1, weight=1) var_x = make_entry("x", 0) var_y = make_entry("y", 1) var_sum = make_entry("x + y =", 2) # Tell the GUI to invoke calculate_sum() after every change of x or y var_x.trace("w", calculate_sum) var_y.trace("w", calculate_sum) root.mainloop() From laurent.pointal at free.fr Sat Jan 28 10:32:05 2017 From: laurent.pointal at free.fr (Laurent Pointal) Date: 28 Jan 2017 15:32:05 GMT Subject: Update to Python 3 Cheat Sheet Message-ID: <588cb975$0$4274$426a74cc@news.free.fr> Hi, I updated the cheat sheet on the aesthetic side. Parts bloc and their title are now more easily identified with colors (but its nice with B&W printing too). French and german versions have also been updated. See https://perso.limsi.fr/pointal/python:memento A+ L.Pointal. From grant.b.edwards at gmail.com Sat Jan 28 11:01:03 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sat, 28 Jan 2017 16:01:03 +0000 (UTC) Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2017-01-28, Steve D'Aprano wrote: > Right: I want to know what the terminal window is sized to. What do you mean by "the terminal"? Do you mean the device to which the program's output is connected? Since output is what you have control over, and what's width you might want to change, that's what makes sense to me. Or do you mean the device to which the program's input is connected? Or do you mean the controlling tty for the process group in which the program is running? > The terminal size doesn't change just because I'm piping output to > another process. Using the terminal size as a proxy for "being > piped" is sheer insanity. Why do you want to know what the terminal width is if it isn't to control the format of the output? That's why _I_ would want to know the terminal width. And in that case, the width of there stdout is going is what makes the most sense to me. > But I'm only asking one question: what's the terminal size? The library call you're making defines "the terminal" as the device to which stdout is connected. You apparently which to define "the terminal" differently. In which case you need to use a different library call. -- Grant From torriem at gmail.com Sat Jan 28 11:03:22 2017 From: torriem at gmail.com (Michael Torrie) Date: Sat, 28 Jan 2017 09:03:22 -0700 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588c79eb$0$1603$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c79eb$0$1603$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8a850620-86b9-55c3-6516-624b520f81a3@gmail.com> On 01/28/2017 04:00 AM, Steve D'Aprano wrote: >> $ COLUMNS=123 python3 test_gts.py | cat >> shutil: os.terminal_size(columns=123, lines=999) >> os: os.terminal_size(columns=72, lines=48) Interesting. On my machine with Python 3.4, calling os.get_terminal_size() and piping the output results in the exception OSError: [Errno 25] Inappropriate ioctl for device Which is what I'd expect, seeing as there's no tty and no terminal emulator. I'm not sure why you're not seeing that error also. > Unless your terminal *actually is* 123 columns wide, I don't see that as an > advantage. I see that as "Hey look, we can fool shutil into returning > absolute garbage instead of the terminal size!" Influencing the column width used by a program for output is very useful and is often used in shell scripting and piping data. This allows programs to just have to use one mechanism to determine line length, regardless of whether it's running on a tty or connected to a pipe. > I can already read environment variables using os.getenv() and os.environ, > so this gives me no new functionality. Sure but working through shutils provides a uniform way of getting the maximum column width. Furthermore using shutils is probably more portable. I'm not sure if you can override LINES or not, but even if you can't that's pointless. > But what happens if you resize the terminal while the process is running? > Again, the os.get_terminal_size() correctly returns the updated size, while > shutil.get_terminal_size() doesn't. For a program that's communicating with a tty, you just have to call shutil.get_terminal_size() again to get the updated size. For your piped process, the question doesn't make sense. You can't resize a pipe. The only process that can respond to terminal changes is the final process in your chain that's actually writing to the tty. If your process is talking to a pipe, then the only thing you can do is make an arbitrary decision about the line length (columns==999 means unlimited, otherwise limit to columns). Nothing else makes sense. From steve+python at pearwood.info Sat Jan 28 11:15:25 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Sun, 29 Jan 2017 03:15:25 +1100 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> Message-ID: <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> On Sat, 28 Jan 2017 10:50 pm, Chris Angelico wrote: > On Sat, Jan 28, 2017 at 9:49 PM, Steve D'Aprano > wrote: >> The terminal size doesn't change just because I'm piping output to >> another process. Using the terminal size as a proxy for "being piped" is >> sheer insanity. > > In a sense, there _is no_ terminal size when you're being piped to > another process. In which sense, and why do you think it is relevant? There clearly is a terminal, because that's where I'm running the code. Regardless of whether I pipe it to grep or cat or something else, the output from *that* process still ends up in the same terminal that I typed the command in. I acknowledge that there are cases where there is no terminal. As you say: > Likewise if you're running in some detached > (non-terminal) context, Then get_terminal_size() should raise, unless you explicitly ask for a default size. Likewise: > or if you're being run over some remote link > that hasn't transmitted terminal size info, I think that the possibility of having such a remote link is a failure of the remote protocol being used. (ssh? telnet?) But it is what it is. Perhaps there genuinely are circumstances where the terminal size exists but is unknowable -- but the example code isn't one of them. > etc, etc, etc, etc. It's > not a proxy for "being piped" - it's that when your output isn't going > to a terminal, asking "what is my terminal size" isn't particularly > productive. Then explain why os.get_terminal_size() returns the correct answer. The output might not be going to a terminal (not directly at least) but the question isn't "what's the size of the terminal that output is going to". The question is "what's the size of the terminal that this process is running in", and that has an answer regardless of where output is piped. > Would you expect that a process started from systemd is told about the > size of the terminal in which you ran "systemctl start servicename"? I > doubt it. I wouldn't make any assumptions at all about what systemd does. For all I know, it hard codes a size of (300, 100) into every single process because that's the size of terminals on Lennart's laptop. > Would you expect a cronjob to use the terminal size when you > most recently edited crontab? No. Of course not -- the terminal where you edited crontab is not where the process is running. Why would it be the least bit relevant? > So why should a program that's being > piped into something else automatically assume the size of the other > program's terminal? Because the other program is running in the same terminal as the first process. grep foo * | wc -l Both grep and wc are running in the same terminal. (Assuming there is any terminal at all.) > You might well be a completely background process. And if that background process is running in a terminal? What's your point? I'm not disputing that there are processes where no terminal exists at all. That's a red herring. os.get_terminal_size() returns the correct result. Why doesn't shutil? > You can still ask the concrete question about terminal size, You mean like a function called get_terminal_size()? > and that's in the 'os' module. The 'shutil' module also looks at > environment variables, Why? We already have ways to look at environment variables. The only complication here is that, perhaps, there may be more than one set of environment variables to compare, and some order that they should be preferred. It is often the Unix way that there might be two, or twenty-two *wink* different environment variables to communicate this information. But in this case, I'm not able to find anything other than the two standard variables, COLUMNS and LINES. Aside: technically these are shell variables, and they may not exist in arbitrary shells. They also need to be explicitly exported as environment variables to become visible to Python. See this StackOverflow question for more information: http://unix.stackexchange.com/questions/215584/whats-the-name-of-the-environment-variable-with-current-terminal-width -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From torriem at gmail.com Sat Jan 28 11:22:24 2017 From: torriem at gmail.com (Michael Torrie) Date: Sat, 28 Jan 2017 09:22:24 -0700 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <8a850620-86b9-55c3-6516-624b520f81a3@gmail.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c79eb$0$1603$c3e8da3$5496439d@news.astraweb.com> <8a850620-86b9-55c3-6516-624b520f81a3@gmail.com> Message-ID: <8a02131b-fc7d-2039-3e34-aaaeb24210cc@gmail.com> On 01/28/2017 09:03 AM, Michael Torrie wrote: > On 01/28/2017 04:00 AM, Steve D'Aprano wrote: >>> $ COLUMNS=123 python3 test_gts.py | cat >>> shutil: os.terminal_size(columns=123, lines=999) >>> os: os.terminal_size(columns=72, lines=48) > > Interesting. On my machine with Python 3.4, calling > os.get_terminal_size() and piping the output results in the exception > OSError: [Errno 25] Inappropriate ioctl for device Oh I see you are calling get_terminal_size() on file handle 0 which is standard in. Generally, I can't think of very many use cases for caring about the terminal size on the *input* side. Most times I only care about the output side, which if it's a pipe doesn't make a lot of sense to care about, other than maximum line length. From torriem at gmail.com Sat Jan 28 11:29:42 2017 From: torriem at gmail.com (Michael Torrie) Date: Sat, 28 Jan 2017 09:29:42 -0700 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4363a1c3-1944-da2c-7993-58c41c66f778@gmail.com> On 01/28/2017 09:15 AM, Steve D'Aprano wrote: > Then get_terminal_size() should raise, unless you explicitly ask for a > default size. Which it does if you call it on the standard out file handle, which is the default, and for most applications, the most useful. From torriem at gmail.com Sat Jan 28 11:44:54 2017 From: torriem at gmail.com (Michael Torrie) Date: Sat, 28 Jan 2017 09:44:54 -0700 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52029266-5e1d-be47-56b6-240567bf0524@gmail.com> On 01/28/2017 09:15 AM, Steve D'Aprano wrote: > Then explain why os.get_terminal_size() returns the correct answer. Basically you were asking two different questions there. shutil.get_terminal_size always asks the question of size of the terminal that the standard output file handle is connected to. Whereas, if I read this correctly, you asked os.get_terminal_size to query the size of the terminal attached to the *standard input* file handle. These are very different things. If you want to know the size of the terminal on the input side, use os.get_terminal_size(0), not shutil. By the way my comment about raising an exception a moment ago was about os.get_terminal_size(1). Apparently shutil is trying to be a convenience function and lets you specify a default size if there is no size to determine. > The output might not be going to a terminal (not directly at least) but the > question isn't "what's the size of the terminal that output is going to". > The question is "what's the size of the terminal that this process is > running in", and that has an answer regardless of where output is piped. In your specific case, the answer is to get the terminal size by querying os.get_terminal_size on standard in, since your standard out is not a tty. In most other situations the question doesn't make a lot of sense because there's no correlation between the terminal that the process is running in and the terminal the process's output is going to be piped to when a tty is not involved. For example: command1 | python3 test_gts.py | command2 In that case your process has no terminal on either end. Only command1 and command 2 do. From grant.b.edwards at gmail.com Sat Jan 28 12:53:11 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sat, 28 Jan 2017 17:53:11 +0000 (UTC) Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2017-01-28, Steve D'Aprano wrote: > On Sat, 28 Jan 2017 10:50 pm, Chris Angelico wrote: > >> On Sat, Jan 28, 2017 at 9:49 PM, Steve D'Aprano >> wrote: >>> The terminal size doesn't change just because I'm piping output to >>> another process. Using the terminal size as a proxy for "being piped" is >>> sheer insanity. >> >> In a sense, there _is no_ terminal size when you're being piped to >> another process. > > In which sense, and why do you think it is relevant? > > There clearly is a terminal, because that's where I'm running the > code. Regardless of whether I pipe it to grep or cat or something > else, the output from *that* process still ends up in the same > terminal that I typed the command in. I'm sorry, it's not at all obvious to me (or, apparently to the author of shutil.get_terminal_size) that the output is going to end up in the terminal where you typed the command. The output might just as easily end up in a file or in a completely different terminal (possibly on a different machine). > I acknowledge that there are cases where there is no terminal. And cases where there is a different terminal entirely. To me, the current behavior of shutil.get_terminal_size seems to be the most useful. -- Grant From rosuav at gmail.com Sat Jan 28 12:58:36 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 29 Jan 2017 04:58:36 +1100 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Jan 29, 2017 at 3:15 AM, Steve D'Aprano wrote: > On Sat, 28 Jan 2017 10:50 pm, Chris Angelico wrote: > >> On Sat, Jan 28, 2017 at 9:49 PM, Steve D'Aprano >> wrote: >>> The terminal size doesn't change just because I'm piping output to >>> another process. Using the terminal size as a proxy for "being piped" is >>> sheer insanity. >> >> In a sense, there _is no_ terminal size when you're being piped to >> another process. > > In which sense, and why do you think it is relevant? > > There clearly is a terminal, because that's where I'm running the code. > Regardless of whether I pipe it to grep or cat or something else, the > output from *that* process still ends up in the same terminal that I typed > the command in. No, not automatically. I've written plenty of programs that accept input via a pipe and don't display any of it. Just because someone types "command1 | command2", you can't assume that the terminal command2 is outputting to is the same size as the one command1 should be outputting to. >> etc, etc, etc, etc. It's >> not a proxy for "being piped" - it's that when your output isn't going >> to a terminal, asking "what is my terminal size" isn't particularly >> productive. > > Then explain why os.get_terminal_size() returns the correct answer. > > The output might not be going to a terminal (not directly at least) but the > question isn't "what's the size of the terminal that output is going to". > The question is "what's the size of the terminal that this process is > running in", and that has an answer regardless of where output is piped. Processes aren't always running "in" terminals, though. That's my point. A terminal is not a fundamental feature of a process. >> Would you expect a cronjob to use the terminal size when you >> most recently edited crontab? No. > > Of course not -- the terminal where you edited crontab is not where the > process is running. Why would it be the least bit relevant? So where *is* that process running? What terminal is it in? Don't you see how similar this is to the pipe situation - sure, there might be a terminal that the program was invoked from, but it's utterly irrelevant to how the program actually runs. >> You might well be a completely background process. > > And if that background process is running in a terminal? What's your point? Background processes don't have terminal access. Whether it's a daemon or something started as "commandname >/dev/null 2>/dev/null <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87r33nnfrx.fsf@elektro.pacujo.net> Chris Angelico : > Background processes don't have terminal access. Whether it's a daemon > or something started as "commandname >/dev/null 2>/dev/null &" from bash, it doesn't have access to a terminal. A nitpick: a process running in the background or a process with no open terminal file descriptor still does have a controlling terminal under Unix/Linux. Marko From grant.b.edwards at gmail.com Sat Jan 28 15:02:48 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sat, 28 Jan 2017 20:02:48 +0000 (UTC) Subject: Is Python SSL API thread-safe? References: <4f923fe7-fa75-698e-37b9-3e4423c7e59b@python.org> Message-ID: On 2017-01-22, Christian Heimes wrote: > OpenSSL and Python's ssl module are thread-safe. However IO is not > safe concerning reentrancy. You cannot safely share a SSLSocket > between threads without a mutex. Certain aspects of the TLS protocol > can cause interesting side effects. A recv() call can send data > across a wire and a send() call can receive data from the wire, > e.g. during re-keying. And it looks to me like the Python SSL module does all of that. It provides mutexes and thread ID and locking callbacks as described in the page below: https://www.openssl.org/docs/man1.0.2/crypto/threads.html According to that page above it's safe to share the socket between threads: OpenSSL can safely be used in multi-threaded applications provided that at least two callback functions are set, locking_function and threadid_func. They python ssl module code does that, so python ssl sockets should be thread safe. Can you explain why you disagree? Can you provide example code that demonstrates a failure? > In order to archive reentrancy, you have to do all IO yourself by > operating the SSL connection in non-blocking mode or with a > Memorio-BIO https://docs.python.org/3/library/ssl.html#ssl-nonblocking That section is about how to work with non-blocking sockets. I'm not using non-blocking sockets. -- Grant Edwards grant.b.edwards Yow! Now I'm concentrating at on a specific tank battle gmail.com toward the end of World War II! From eryksun at gmail.com Sat Jan 28 15:04:45 2017 From: eryksun at gmail.com (eryk sun) Date: Sat, 28 Jan 2017 20:04:45 +0000 Subject: Is shutil.get_terminal_size useless? In-Reply-To: References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Jan 28, 2017 at 5:58 PM, Chris Angelico wrote: > Processes in the middle of pipelines *do not have* terminals. No, in the following case stderr is a terminal: $ echo spam | > python3 -c 'import os > print(os.get_terminal_size(2))' | > cat os.terminal_size(columns=132, lines=43) Let's also redirect stderr to the pipe: $ echo spam | > 2>&1 python3 -c 'import os > print(os.get_terminal_size(2))' | > cat Traceback (most recent call last): File "", line 2, in OSError: [Errno 25] Inappropriate ioctl for device Now let's open and use the controlling terminal instead: $ echo spam | > 2>&1 python3 -c 'import os > fd = os.open("/dev/tty", os.O_RDONLY) > print(os.get_terminal_size(fd))' | > cat os.terminal_size(columns=132, lines=43) Now let's get rid of the terminal via setsid: $ echo spam | > 2>&1 setsid python3 -c 'import os > fd = os.open("/dev/tty", os.O_RDONLY) > print(os.get_terminal_size(fd))' | > cat Traceback (most recent call last): File "", line 2, in OSError: [Errno 6] No such device or address: '/dev/tty' From rosuav at gmail.com Sat Jan 28 15:36:25 2017 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 29 Jan 2017 07:36:25 +1100 Subject: Is shutil.get_terminal_size useless? In-Reply-To: References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Jan 29, 2017 at 7:04 AM, eryk sun wrote: > Now let's get rid of the terminal via setsid: > > $ echo spam | > > 2>&1 setsid python3 -c 'import os > > fd = os.open("/dev/tty", os.O_RDONLY) > > print(os.get_terminal_size(fd))' | > > cat > Traceback (most recent call last): > File "", line 2, in > OSError: [Errno 6] No such device or address: '/dev/tty' And you could have achieved the same result in any number of other ways, too, like setting yourself up with the local boot system (true of systemd, upstart, sysvinit, and probably of all others too), or cron, or inetd, etc, etc, etc. You can definitely lose access to your "controlling terminal". ChrisA From marko at pacujo.net Sat Jan 28 16:26:33 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sat, 28 Jan 2017 23:26:33 +0200 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87mveaooza.fsf@elektro.pacujo.net> Chris Angelico : > On Sun, Jan 29, 2017 at 7:04 AM, eryk sun wrote: >> Now let's get rid of the terminal via setsid: >> >> $ echo spam | >> > 2>&1 setsid python3 -c 'import os >> > fd = os.open("/dev/tty", os.O_RDONLY) >> > print(os.get_terminal_size(fd))' | >> > cat >> Traceback (most recent call last): >> File "", line 2, in >> OSError: [Errno 6] No such device or address: '/dev/tty' > > And you could have achieved the same result in any number of other > ways, too, like setting yourself up with the local boot system (true > of systemd, upstart, sysvinit, and probably of all others too), or > cron, or inetd, etc, etc, etc. You can definitely lose access to your > "controlling terminal". Eryk has a real, deeper point worth getting acquainted with. (Although if I were to design an operating system, I don't know if I would bother with controlling terminals, job control or chirping modems.) Marko From pavlovevidence at gmail.com Sat Jan 28 20:35:17 2017 From: pavlovevidence at gmail.com (pavlovevidence at gmail.com) Date: Sat, 28 Jan 2017 17:35:17 -0800 (PST) Subject: How coding in Python is bad for you In-Reply-To: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> Message-ID: <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> On Monday, January 23, 2017 at 9:24:56 AM UTC-8, bream... at gmail.com wrote: > The article is here http://lenkaspace.net/index.php/blog/show/111 > > Kindest regards. > > Mark Lawrence. I remember the old days of Python when it was just Perl's little brother. Sometimes I feel moments of amazement whenever someone makes this much of an effort to badmouth it (and this blog is definitely badmouthing it, very little of criticism is reasonable). Carl Banks From grant.b.edwards at gmail.com Sat Jan 28 21:53:03 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 29 Jan 2017 02:53:03 +0000 (UTC) Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> Message-ID: On 2017-01-28, Marko Rauhamaa wrote: > (Although if I were to design an operating system, I don't know if I > would bother with controlling terminals, job control or chirping > modems.) I've been using serial ports on Unix for 35 years, and maintaining serial drivers for Linux for almost 20. Many days, it seems like the tty/serial API in Unix is the rug under which all the ugly dirt got swept... Then I think about X. and I guess the networking stuff gets ugly pretty fast when you look under the hood. It's really pretty impressive how orthogonal they did manage to make things. -- Grant From juan0christian at gmail.com Sat Jan 28 22:06:31 2017 From: juan0christian at gmail.com (Juan C.) Date: Sun, 29 Jan 2017 01:06:31 -0200 Subject: What are your opinions on .NET Core vs Python? Message-ID: As you guys might know, .NET Core is up and running, promising a "cross-platform, unified, fast, lightweight, modern and open source experience" (source: .NET Core official site). What do you guys think about it? Do you think it will be able to compete with and overcome Python in the opensource medium? From python at deborahswanson.net Sun Jan 29 04:11:28 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 29 Jan 2017 01:11:28 -0800 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: Message-ID: <007801d27a0f$ad381e80$27b23dae@sambora> Juan C. wrote, on Saturday, January 28, 2017 7:07 PM > > As you guys might know, .NET Core is up and running, > promising a "cross-platform, unified, fast, lightweight, > modern and open source experience" (source: .NET Core > official site). What do you guys think about it? Do you think > it will be able to compete with and overcome Python in the > opensource medium? I don't really know what .NET does these days, but looking at the list of languages it supports: https://en.wikipedia.org/wiki/List_of_CLI_languages#Current_Languages I don't see any I'd likely want to use instead of Python, with the possible exception of IronPython. A few people on this list have mentioned it, but I don't really see any good reason to paste any version of Python onto some other kind of system. Unless maybe you're required to use that system for some other reason. Most of the languages on this "List_of_CLI_languages" supported by .NET have already been shown to my satisfaction to have been overtaken and subsumed by Python. It's a good thing that .NET's framework has shed some baggage, which was sorely needed, but what really matters is what you can do with it. And given the choices of languages available in .NET, in most cases I think you'd be better off working in straight Python. You can't put lipstick on a pig. It will still be a pig, even if you give it flashy sunglasses and put it on jetskis. From marko at pacujo.net Sun Jan 29 04:23:49 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sun, 29 Jan 2017 11:23:49 +0200 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> Message-ID: <87fuk2dxsq.fsf@elektro.pacujo.net> Grant Edwards : > On 2017-01-28, Marko Rauhamaa wrote: > >> (Although if I were to design an operating system, I don't know if I >> would bother with controlling terminals, job control or chirping >> modems.) > > I've been using serial ports on Unix for 35 years, and maintaining > serial drivers for Linux for almost 20. Many days, it seems like the > tty/serial API in Unix is the rug under which all the ugly dirt got > swept... > > [...] > > It's really pretty impressive how orthogonal they did manage to make > things. I *have* been longing for a serial console in linux distros. That would make it possible to set up virtual machines from ISO images automatically as virtualization environments can emulate a serial interface with an SSH connection. As it stands, such automation requires ugly, brittle VNC tricks. Marko From gerald.britton at gmail.com Sun Jan 29 07:51:48 2017 From: gerald.britton at gmail.com (Gerald Britton) Date: Sun, 29 Jan 2017 07:51:48 -0500 Subject: Fwd: What are your opinions on .NET Core vs Python? In-Reply-To: References: Message-ID: > > As you guys might know, .NET Core is up and running, promising a > "cross-platform, unified, fast, lightweight, modern and open source > experience" (source: .NET Core official site). What do you guys think about > it? Do you think it will be able to compete with and overcome Python in the > opensource medium? It's an apples/oranges comparison. .NET is a library that can be used from many languages, including Python. (Not just IronPython, but also Python for .NET (pythonnet.sourceforge*.*net *))* Python is a language that can use many libraries, including .NET The set of libraries that can be used from all the languages that can also use .NET (out of the box, that is) is smaller. From bc at freeuk.com Sun Jan 29 08:05:48 2017 From: bc at freeuk.com (BartC) Date: Sun, 29 Jan 2017 13:05:48 +0000 Subject: How coding in Python is bad for you In-Reply-To: <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> Message-ID: On 29/01/2017 01:35, pavlovevidence at gmail.com wrote: > On Monday, January 23, 2017 at 9:24:56 AM UTC-8, bream... at gmail.com wrote: >> The article is here http://lenkaspace.net/index.php/blog/show/111 >> >> Kindest regards. >> >> Mark Lawrence. > > I remember the old days of Python when it was just Perl's little brother. Sometimes I feel moments of amazement whenever someone makes this much of an effort to badmouth it (and this blog is definitely badmouthing it, very little of criticism is reasonable). I think it's completely reasonable for anyone to criticise Python if they want (or any other computer language for that matter). (And it didn't cover that much; I could do a far more in-depth critique if I wanted.) What might be unreasonable is to criticise it in a /Python/ group full of language aficionados who are going to view every feature and quirk of the language in a good light; nothing is ever a problem! But the author of piece didn't post it here. -- bartc From rosuav at gmail.com Sun Jan 29 08:17:43 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 00:17:43 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> Message-ID: On Mon, Jan 30, 2017 at 12:05 AM, BartC wrote: > On 29/01/2017 01:35, pavlovevidence at gmail.com wrote: >> >> On Monday, January 23, 2017 at 9:24:56 AM UTC-8, bream... at gmail.com wrote: >>> >>> The article is here http://lenkaspace.net/index.php/blog/show/111 >>> >>> Kindest regards. >>> >>> Mark Lawrence. >> >> >> I remember the old days of Python when it was just Perl's little brother. >> Sometimes I feel moments of amazement whenever someone makes this much of an >> effort to badmouth it (and this blog is definitely badmouthing it, very >> little of criticism is reasonable). > > > I think it's completely reasonable for anyone to criticise Python if they > want (or any other computer language for that matter). > > (And it didn't cover that much; I could do a far more in-depth critique if I > wanted.) > > What might be unreasonable is to criticise it in a /Python/ group full of > language aficionados who are going to view every feature and quirk of the > language in a good light; nothing is ever a problem! > > But the author of piece didn't post it here. It's completely reasonable for someone to critique Python. It's not reasonable to post a bunch of baseless FUD, regardless of your forum. There's plenty in Python that you can legitimately criticise. Sometimes you'll get a response of "actually this is good, because X, Y, Z"; sometimes you get "well, what you suggest is marginally better, but not enough to justify breaking backward compatibility"; and sometimes you get "good point, maybe we can change that in the next version - want to write a patch?". But when all your criticisms are either (a) true of virtually every programming language, yet you claim they're Python's faults; or (b) actually false, you just make yourself look like a troll. ChrisA From steve+python at pearwood.info Sun Jan 29 09:42:15 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Mon, 30 Jan 2017 01:42:15 +1100 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> Message-ID: <588dff48$0$22142$c3e8da3$5496439d@news.astraweb.com> On Mon, 30 Jan 2017 12:05 am, BartC wrote: > What might be unreasonable is to criticise it in a Python group full > of language aficionados who are going to view every feature and quirk of > the language in a good light; It's all about the trade-offs we choose to make. Its not that we don't understand that feature X has a downside; its that we value that feature enough to make up for it. > nothing is ever a problem! 1. for...else is misspelled, and should be for...then; 2. Same for while...else; 3. shutil.get_terminal_size() is broken by design; 4. Decimal is not integrated with the numeric tower; 5. The statistics module is too slow (and if I ever meet the author, I'll give him a kick in the head); 6. duck-typing with numbers often fails, requiring you to resort to polymorphic code with type checking; 7. which is too slow; 8. fractions could easily support infinities (?1/0) and NAN (0/0) but don't; 9. the state of GUI toolkits in 2017 is nowhere near as easy to use as Hypercard in 1986 (although that doesn't apply just to Python); 10. packaging is still too complex and hard to get right; 11. the last couple of years has seen an emphasis on asynchronous programming that I don't understand and scares me; 12. there's no try...except expression; 13. or constants; 14. writing bullet-proof file handling code (e.g. atomic file save that either is guaranteed to write to the disk or guaranteed to leave the previous file contents in place) is too hard; 15. str.centre() is misspelled; 16. despite what the Zen says, there aren't enough namespaces; 17. unicodedata is missing a lot of functionality; 18. CPython makes object IDs look like memory addresses; 19. there's no way to tell the compiler to do certain calculations at compile-time, instead of run-time; 20. the utf-8 encoder is strict, but the utf-16 and utf-32 encoders are not, which makes them technically non-compliant; 21. the CPython compiler isn't smart enough, and so has to forgo the opportunity for optimizations which apply 99.9% of the time, because of the 0.1% of the time that they don't apply (e.g. in-lining functions); 22. there's no syntax for Design By Contract; 23. no support for Snobol/Icon like string patterns (BNF context-free grammars, more powerful than regular expressions); 24. lack of support for Prolog-like logic programming paradigm; 25. no way to bail out of generator expressions early, e.g. (expr for x in seq while condition); 26. the glob module doesn't support escaping, or {a,b,c} alternatives; 27. in hindsight, all() and any() shouldn't coerce their result to True or False, but return a truthy or falsey value from their inputs. That's just off the top of my head. Is that enough for you? -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From grant.b.edwards at gmail.com Sun Jan 29 11:07:26 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 29 Jan 2017 16:07:26 +0000 (UTC) Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> Message-ID: On 2017-01-29, Marko Rauhamaa wrote: > Grant Edwards : > >> On 2017-01-28, Marko Rauhamaa wrote: >> >>> (Although if I were to design an operating system, I don't know if I >>> would bother with controlling terminals, job control or chirping >>> modems.) >> >> I've been using serial ports on Unix for 35 years, and maintaining >> serial drivers for Linux for almost 20. Many days, it seems like the >> tty/serial API in Unix is the rug under which all the ugly dirt got >> swept... >> >> [...] >> >> It's really pretty impressive how orthogonal they did manage to make >> things. > > I *have* been longing for a serial console in linux distros. Well, all it takes is a tweak to the bootloader to add a kernel "command-line" parameter... > That would make it possible to set up virtual machines from ISO > images automatically as virtualization environments can emulate a > serial interface with an SSH connection. As it stands, such > automation requires ugly, brittle VNC tricks. -- Grant From marko at pacujo.net Sun Jan 29 11:28:08 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sun, 29 Jan 2017 18:28:08 +0200 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> Message-ID: <87bmupespz.fsf@elektro.pacujo.net> Grant Edwards : > On 2017-01-29, Marko Rauhamaa wrote: >> I *have* been longing for a serial console in linux distros. > > Well, all it takes is a tweak to the bootloader to add a kernel > "command-line" parameter... Can you give me a short Python script that effects the tweak to an ISO image? I need to run the script as a regular user. Marko From rosuav at gmail.com Sun Jan 29 11:32:41 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 03:32:41 +1100 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <87bmupespz.fsf@elektro.pacujo.net> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> <87bmupespz.fsf@elektro.pacujo.net> Message-ID: On Mon, Jan 30, 2017 at 3:28 AM, Marko Rauhamaa wrote: > Grant Edwards : > >> On 2017-01-29, Marko Rauhamaa wrote: >>> I *have* been longing for a serial console in linux distros. >> >> Well, all it takes is a tweak to the bootloader to add a kernel >> "command-line" parameter... > > Can you give me a short Python script that effects the tweak to an ISO > image? I need to run the script as a regular user. It'd start by mounting it somewhere, and finish by building a new ISO with mkisofs, so a shell script would be more appropriate :) ChrisA From marko at pacujo.net Sun Jan 29 11:44:55 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sun, 29 Jan 2017 18:44:55 +0200 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> <87bmupespz.fsf@elektro.pacujo.net> Message-ID: <877f5dery0.fsf@elektro.pacujo.net> Chris Angelico : > On Mon, Jan 30, 2017 at 3:28 AM, Marko Rauhamaa wrote: >> Grant Edwards : >> >>> On 2017-01-29, Marko Rauhamaa wrote: >>>> I *have* been longing for a serial console in linux distros. >>> >>> Well, all it takes is a tweak to the bootloader to add a kernel >>> "command-line" parameter... >> >> Can you give me a short Python script that effects the tweak to an ISO >> image? I need to run the script as a regular user. > > It'd start by mounting it somewhere, and finish by building a new ISO > with mkisofs, so a shell script would be more appropriate :) Mount? As a regular user? Marko From rosuav at gmail.com Sun Jan 29 11:53:43 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 03:53:43 +1100 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <877f5dery0.fsf@elektro.pacujo.net> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> <87bmupespz.fsf@elektro.pacujo.net> <877f5dery0.fsf@elektro.pacujo.net> Message-ID: On Mon, Jan 30, 2017 at 3:44 AM, Marko Rauhamaa wrote: > Chris Angelico : > >> On Mon, Jan 30, 2017 at 3:28 AM, Marko Rauhamaa wrote: >>> Grant Edwards : >>> >>>> On 2017-01-29, Marko Rauhamaa wrote: >>>>> I *have* been longing for a serial console in linux distros. >>>> >>>> Well, all it takes is a tweak to the bootloader to add a kernel >>>> "command-line" parameter... >>> >>> Can you give me a short Python script that effects the tweak to an ISO >>> image? I need to run the script as a regular user. >> >> It'd start by mounting it somewhere, and finish by building a new ISO >> with mkisofs, so a shell script would be more appropriate :) > > Mount? As a regular user? Let me see, how much effort are you prepared to go to in order to do this as a "regular user"... because at some point, "sudo" becomes only one of many options, including "install another Unix system somewhere that you are root of". But if you genuinely have to mount as a non-root user, there's fusermount, which is often capable of loading up an ISO. But honestly, "ssh you at somebox" can be run by anyone, so there's always a way around the whole not-having-root thing. ChrisA From grant.b.edwards at gmail.com Sun Jan 29 11:56:28 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 29 Jan 2017 16:56:28 +0000 (UTC) Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> <87bmupespz.fsf@elektro.pacujo.net> <877f5dery0.fsf@elektro.pacujo.net> Message-ID: On 2017-01-29, Marko Rauhamaa wrote: > Chris Angelico : > >> On Mon, Jan 30, 2017 at 3:28 AM, Marko Rauhamaa wrote: >>> Grant Edwards : >>> >>>> On 2017-01-29, Marko Rauhamaa wrote: >>>>> I *have* been longing for a serial console in linux distros. >>>> >>>> Well, all it takes is a tweak to the bootloader to add a kernel >>>> "command-line" parameter... >>> >>> Can you give me a short Python script that effects the tweak to an ISO >>> image? I need to run the script as a regular user. >> >> It'd start by mounting it somewhere, and finish by building a new ISO >> with mkisofs, so a shell script would be more appropriate :) > > Mount? As a regular user? https://linux.die.net/man/8/isoinfo -- Grant From grant.b.edwards at gmail.com Sun Jan 29 12:08:23 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 29 Jan 2017 17:08:23 +0000 (UTC) Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> <87bmupespz.fsf@elektro.pacujo.net> <877f5dery0.fsf@elektro.pacujo.net> Message-ID: On 2017-01-29, Marko Rauhamaa wrote: > Mount? As a regular user? Yes, using a "fuse" use-space filesystem, you can mount things as a normal user. There are a couple ISO9660 fuse implemenations. But, you can't modify a mounted ISO9660 filesystem. I have read about how to use a union mount to simulate a writable ISO9660 filesystem, but that's going to require root (I've never tried it). As long as you've got the disk space available, the simplest option is to unpack the .iso into a directory, modify the files, and then use mkisofs to create the new .iso image. I've written scripts like that to build cusotmized bootable ISO images (usually using systemrescuecd as the starting point). It's not particulary difficult, but it does burn up a lot of disk space. It's also rather slow, so when you get into the tweak-build-burn-test cycle you don't get in a lot of guesses-per-hour. -- Grant From rosuav at gmail.com Sun Jan 29 12:13:48 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 04:13:48 +1100 Subject: Is shutil.get_terminal_size useless? In-Reply-To: References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588c7728$0$1602$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> <87bmupespz.fsf@elektro.pacujo.net> <877f5dery0.fsf@elektro.pacujo.net> Message-ID: On Mon, Jan 30, 2017 at 4:08 AM, Grant Edwards wrote: > As long as you've got the disk space available, the simplest option is > to unpack the .iso into a directory, modify the files, and then use > mkisofs to create the new .iso image. > > I've written scripts like that to build cusotmized bootable ISO images > (usually using systemrescuecd as the starting point). It's not > particulary difficult, but it does burn up a lot of disk space. It's > also rather slow, so when you get into the tweak-build-burn-test cycle > you don't get in a lot of guesses-per-hour. These days, CD images are a tiny proportion of typical hard disk capacities. Every terabyte of disk can hold over a thousand ISOs, and that's assuming they're using the whole disc (~760MB). Several thousand, if they're only part-full images. But the time factor is considerable. Especially if you then have to boot up a VM or something to test it. ChrisA From cl at isbd.net Sun Jan 29 12:18:38 2017 From: cl at isbd.net (Chris Green) Date: Sun, 29 Jan 2017 17:18:38 +0000 Subject: Looking for a basic address book/contacts program Message-ID: I'm looking for a simple Address Book program written in Python, I've looked at addressbook 1.0.1 from PyPi but it doesn't quite do what I want. I may have to start with this and modify it but maybe there's something better out there. In particular I want some sort of import/export ability and I'd really like some more configurability. Any ideas anyone? -- Chris Green ? From marko at pacujo.net Sun Jan 29 12:22:49 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Sun, 29 Jan 2017 19:22:49 +0200 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> <87bmupespz.fsf@elektro.pacujo.net> <877f5dery0.fsf@elektro.pacujo.net> Message-ID: <871svleq6u.fsf@elektro.pacujo.net> Chris Angelico : > On Mon, Jan 30, 2017 at 3:44 AM, Marko Rauhamaa wrote: >> Mount? As a regular user? > > Let me see, how much effort are you prepared to go to in order to do > this as a "regular user"... because at some point, "sudo" becomes only > one of many options, including "install another Unix system somewhere > that you are root of". But if you genuinely have to mount as a > non-root user, there's fusermount, which is often capable of loading > up an ISO. > > But honestly, "ssh you at somebox" can be run by anyone, so there's > always a way around the whole not-having-root thing. At the moment, we have found it easiest to create our base OS snapshots with some manual steps. Ideally, we could get rid of the manual steps and just have a python script take the place of the human user. That would be trivial if the distros continued the grand tradition of offering serial consoles out of the box. As it stands, all methods of working serial-interface support in the ISO image would still be more trouble than the manual steps. We also use "Packer" to build virtual machines automatically. That works albeit in a somewhat kludgy manner. As far as I understand, Packer delivers keystrokes to the boot command over VNC but can't interpret the graphical responses but simply trusts that the commands do the right thing with a proper timing. Marko From grant.b.edwards at gmail.com Sun Jan 29 12:27:07 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sun, 29 Jan 2017 17:27:07 +0000 (UTC) Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588cc39f$0$1609$c3e8da3$5496439d@news.astraweb.com> <87mveaooza.fsf@elektro.pacujo.net> <87fuk2dxsq.fsf@elektro.pacujo.net> <87bmupespz.fsf@elektro.pacujo.net> <877f5dery0.fsf@elektro.pacujo.net> Message-ID: On 2017-01-29, Grant Edwards wrote: > On 2017-01-29, Marko Rauhamaa wrote: > >> Mount? As a regular user? > > Yes, using a "fuse" use-space filesystem, you can mount things as a > normal user. There are a couple ISO9660 fuse implemenations. But, you > can't modify a mounted ISO9660 filesystem. I have read about how to > use a union mount to simulate a writable ISO9660 filesystem, but > that's going to require root (I've never tried it). > > As long as you've got the disk space available, the simplest option is > to unpack the .iso into a directory, modify the files, and then use > mkisofs to create the new .iso image. OK, just one more level of pointless digression... _If_ your ISO image is using syslinux as the bootloader (it probably isn't, it's almost certainly using isolinux -- usually in hybrid mode), then the bootloader stuff is actually an image of a bootable floppy disk. It is sort of "outside" the normal ISO9660 filesystem with a fixed size in a "well known" location. I'm pretty sure you could extract that chunk from the ISO using dd, tweak it, and then put it back into the ISO image without having to mount/extract everything and then recreate a new ISO9660 image. But, it's very probably using isolinux, so the bootloader configuration stuff is actually inside the ISO9660 filesystem tree, and you'll have to extract the whole tree, modify the files, and create a new ISO9660 image. -- Grant From vinay_sajip at yahoo.co.uk Sun Jan 29 13:53:44 2017 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Sun, 29 Jan 2017 18:53:44 +0000 (UTC) Subject: ANN: A new version (0.4.0) of python-gnupg has been released. References: <1452661764.26048248.1485716024551.ref@mail.yahoo.com> Message-ID: <1452661764.26048248.1485716024551@mail.yahoo.com> A new version of the Python module which wraps GnuPG has been released. What Changed? ============= This is an enhancement and bug-fix release, and all users are encouraged to upgrade. See the project website [1] for more information. Brief summary: * Added support for ``KEY_CONSIDERED`` in more places - encryption / decryption, signing, key generation and key import. * Partial fix for #32 (GPG 2.1 compatibility). Unfortunately, better support cannot be provided at this point, unless there are certain changes (relating to pinentry popups) in how GPG 2.1 works. * Fixed #60: An IndexError was being thrown by ``scan_keys()``. * Ensured that utf-8 encoding is used when the ``--with-column`` mode is used. Thanks to Yann Leboulanger for the patch. * ``list_keys()`` now uses ``--fixed-list-mode``. Thanks to Werner Koch for the pointer. This release [2] has been signed with my code signing key: Vinay Sajip (CODE SIGNING KEY) Fingerprint: CA74 9061 914E AC13 8E66 EADB 9147 B477 339A 9B86 What Does It Do? ================ The gnupg module allows Python programs to make use of the functionality provided by the Gnu Privacy Guard (abbreviated GPG or GnuPG). Using this module, Python programs can encrypt and decrypt data, digitally sign documents and verify digital signatures, manage (generate, list and delete) encryption keys, using proven Public Key Infrastructure (PKI) encryption technology based on OpenPGP. This module is expected to be used with Python versions >= 2.4, as it makes use of the subprocess module which appeared in that version of Python. This module is a newer version derived from earlier work by Andrew Kuchling, Richard Jones and Steve Traugott. A test suite using unittest is included with the source distribution. Simple usage: >>> import gnupg >>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory') >>> gpg.list_keys() [{ ... 'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2', 'keyid': '197D5DAC68F1AAB2', 'length': '1024', 'type': 'pub', 'uids': ['', 'Gary Gross (A test user) ']}, { ... 'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A', 'keyid': '0C5FEFA7A921FC4A', 'length': '1024', ... 'uids': ['', 'Danny Davis (A test user) ']}] >>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A']) >>> str(encrypted) '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n \nhQIOA/6NHMDTXUwcEAf . -----END PGP MESSAGE-----\n' >>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret') >>> str(decrypted) 'Hello, world!' >>> signed = gpg.sign("Goodbye, world!", passphrase='secret') >>> verified = gpg.verify(str(signed)) >>> print "Verified" if verified else "Not verified" 'Verified' As always, your feedback is most welcome (especially bug reports [3], patches and suggestions for improvement, or any other points via the mailing list/discussion group [4]). Enjoy! Cheers Vinay Sajip Red Dove Consultants Ltd. [1] https://bitbucket.org/vinay.sajip/python-gnupg [2] https://pypi.python.org/pypi/python-gnupg/0.4.0 [3] https://bitbucket.org/vinay.sajip/python-gnupg/issues [4] https://groups.google.com/forum/#!forum/python-gnupg From saxri89 at gmail.com Sun Jan 29 14:42:47 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Sun, 29 Jan 2017 11:42:47 -0800 (PST) Subject: count points where is within the polygons using shapely and fiona Message-ID: i tried to count points from the point shapefile where is within in the polygon shapefile but i fail.Maybe my code is complete wrong but i tried. any idea how to fix my code ? i want fast method because i have big data features from shapely.geometry import shape import fiona filepath1 = "point.shp" file1features = [] intersectfeatures_file1 = [] intersectfeatures_file2 = [] count =0 count=count+1 outschema = None with fiona.collection(filepath1, "r") as input1: outschema = input1.schema.copy() for p1 in input1: file1features.append(p1) filepath2 = "polygon.shp" with fiona.collection(filepath2, "r") as input2: for p2 in input2: for p1 in file1features: [count for i in [shape(p1['geometry']).within(shape(p2['geometry']))]] if p1 not in intersectfeatures_file1: intersectfeatures_file1.append(p1) if p2 not in intersectfeatures_file2: intersectfeatures_file2.append(p2) print count ''' if intersectfeatures_file1: outfile = "outputfile1.shp" with fiona.collection(outfile, "w", "ESRI Shapefile", outschema) as output: for outfeature in intersectfeatures: output.write(outfeature) if intersectfeatures_file2: outfile = "outputfile2.shp" with fiona.collection(outfile, "w", "ESRI Shapefile", outschema) as output: for outfeature in intersectfeatures: output.write(outfeature) ''' on the print count i take 20 times the number 1. after for this count i want to export to new shapefile the polygons where have specific number of points. thnx From storchaka at gmail.com Sun Jan 29 16:12:23 2017 From: storchaka at gmail.com (Serhiy Storchaka) Date: Sun, 29 Jan 2017 23:12:23 +0200 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 28.01.17 10:03, Steve D'Aprano wrote: > Is shutil.get_terminal_size useless? When, if ever, should I use it in > preference to the os version? If the shutil version is broken, can it be > fixed? Read the history of shutil.get_terminal_size(). All this was discussed. From jcasale at activenetwerx.com Sun Jan 29 16:13:32 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Sun, 29 Jan 2017 21:13:32 +0000 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: Message-ID: > .NET is a library that can be used from many languages, including Python. No. .NET Core (what the OP asked about which is not .NET) is a cross-platform framework. Obviously Python and .NET differ in runtime semantics with respect to the original source code, however they are now roughly equivalent in that both specifications are cross platform. I personally like .NET Core a great deal and as the framework matures and it starts to present all of the .NET APIs, it will become a viable choice on non Windows platforms. jlc From greg.ewing at canterbury.ac.nz Sun Jan 29 16:54:38 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 30 Jan 2017 10:54:38 +1300 Subject: How coding in Python is bad for you In-Reply-To: <588dff48$0$22142$c3e8da3$5496439d@news.astraweb.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> <588dff48$0$22142$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steve D'Aprano wrote: > 5. The statistics module is too slow (and if I ever meet the author, I'll > give him a kick in the head); Wow... ad-hominem take to a physical level! -- Greg From greg.ewing at canterbury.ac.nz Sun Jan 29 17:00:14 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 30 Jan 2017 11:00:14 +1300 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: Message-ID: Joseph L. Casale wrote: >>.NET is a library that can be used from many languages, including Python. > > No. Yes: http://pythonnet.sourceforge.net/ "Python for .NET is a package that gives Python programmers nearly seamless integration with the .NET Common Language Runtime (CLR) and provides a powerful application scripting tool for .NET developers. Using this package you can script .NET applications or build entire applications in Python, using .NET services and components written in any language that targets the CLR (Managed C++, C#, VB, JScript)." -- Greg From rosuav at gmail.com Sun Jan 29 17:02:31 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 09:02:31 +1100 Subject: How coding in Python is bad for you In-Reply-To: References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> <588dff48$0$22142$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jan 30, 2017 at 8:54 AM, Gregory Ewing wrote: > Steve D'Aprano wrote: > >> 5. The statistics module is too slow (and if I ever meet the author, I'll >> give him a kick in the head); > > > Wow... ad-hominem take to a physical level! If Steve ever comes face to face with the author of the statistics module, he should avoid excessive violence, lest he get seven years of bad luck... for breaking a mirror. :) ChrisA From python at deborahswanson.net Sun Jan 29 17:09:41 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 29 Jan 2017 14:09:41 -0800 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: Message-ID: <001801d27a7c$6452b140$27b23dae@sambora> Joseph L. Casale wrote, on January 29, 2017 1:14 PM > > > .NET is a library that can be used from many languages, including > > Python. > > No. > > .NET Core (what the OP asked about which is not .NET) is a > cross-platform framework. Obviously Python and .NET differ in > runtime semantics with respect to the original source code, > however they are now roughly equivalent in that both > specifications are cross platform. > > I personally like .NET Core a great deal and as the framework > matures and it starts to present all of the .NET APIs, it > will become a viable choice on non Windows platforms. > > jlc What .NET APIs are anticipated to be released that aren't on the official CLI list now: https://en.wikipedia.org/wiki/List_of_CLI_languages#Current_Languages, and/or, are .NET supported languages expected to expand beyond the CLI list? From python at lucidity.plus.com Sun Jan 29 18:52:33 2017 From: python at lucidity.plus.com (Erik) Date: Sun, 29 Jan 2017 23:52:33 +0000 Subject: How coding in Python is bad for you In-Reply-To: <588dff48$0$22142$c3e8da3$5496439d@news.astraweb.com> References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> <588dff48$0$22142$c3e8da3$5496439d@news.astraweb.com> Message-ID: <00e3f05e-86b4-ea26-7211-39bebc6b4afe@lucidity.plus.com> On 29/01/17 14:42, Steve D'Aprano wrote: > 1. for...else is misspelled, and should be for...then; > > 2. Same for while...else; I don't think I'll ever agree with you on this one. "then", to me, implies the code following it is always executed. "else" implies it's conditional. In those constructs it's conditional and therefore, to me, "else" is a better reminder of that. It would be even better if it was "else if not break:" to make the meaning clearer. I would agree that it would be even better than that if it was "then if not break:" (apart from needing the new keyword ;)), as then the conditional aspect is explicit. E. From pkpearson at nowhere.invalid Sun Jan 29 20:06:24 2017 From: pkpearson at nowhere.invalid (Peter Pearson) Date: 30 Jan 2017 01:06:24 GMT Subject: count points where is within the polygons using shapely and fiona References: Message-ID: On Sun, 29 Jan 2017 11:42:47 -0800 (PST), Xristos Xristoou wrote: > i tried to count points from the point shapefile where is within in > the polygon shapefile but i fail.Maybe my code is complete wrong but i > tried. > any idea how to fix my code ? > i want fast method because i have big data features > from shapely.geometry import shape > import fiona > > filepath1 = "point.shp" > file1features = [] > intersectfeatures_file1 = [] > intersectfeatures_file2 = [] > count =0 > count=count+1 This is an illogical place to increment count. > outschema = None > with fiona.collection(filepath1, "r") as input1: > outschema = input1.schema.copy() > for p1 in input1: > file1features.append(p1) I believe you could also say file1features = list(input1). > filepath2 = "polygon.shp" > with fiona.collection(filepath2, "r") as input2: > for p2 in input2: > for p1 in file1features: > [count for i in [shape(p1['geometry']).within(shape(p2['geometry']))]] The above line produces a list that is discarded because it is not assigned any name. > if p1 not in intersectfeatures_file1: > intersectfeatures_file1.append(p1) > if p2 not in intersectfeatures_file2: > intersectfeatures_file2.append(p2) > > print count > ''' > if intersectfeatures_file1: > outfile = "outputfile1.shp" > with fiona.collection(outfile, "w", "ESRI Shapefile", outschema) as output: > for outfeature in intersectfeatures: > output.write(outfeature) > if intersectfeatures_file2: > outfile = "outputfile2.shp" > with fiona.collection(outfile, "w", "ESRI Shapefile", outschema) as output: > for outfeature in intersectfeatures: > output.write(outfeature) > ''' The above lines between triple quotes should have been omitted from this post. > on the print count i take 20 times the number 1. after for this count i want to export to new shapefile the polygons where have specific number of points. thnx -- To email me, substitute nowhere->runbox, invalid->com. From steve+python at pearwood.info Sun Jan 29 21:14:30 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Mon, 30 Jan 2017 13:14:30 +1100 Subject: How coding in Python is bad for you References: <355c105c-d51a-4c66-be66-dde41cba20c8@googlegroups.com> <87ab542f-0473-40c1-8a56-b41b397fb8b2@googlegroups.com> <588dff48$0$22142$c3e8da3$5496439d@news.astraweb.com> <00e3f05e-86b4-ea26-7211-39bebc6b4afe@lucidity.plus.com> Message-ID: <588ea186$0$22140$c3e8da3$5496439d@news.astraweb.com> On Mon, 30 Jan 2017 10:52 am, Erik wrote: > On 29/01/17 14:42, Steve D'Aprano wrote: >> 1. for...else is misspelled, and should be for...then; >> >> 2. Same for while...else; > > I don't think I'll ever agree with you on this one. > > "then", to me, implies the code following it is always executed. > "else" implies it's conditional. > > In those constructs it's conditional and therefore, to me, "else" is a > better reminder of that. But it isn't conditional. When the code reaches the end of the for/while block, the else block is ALWAYS executed. Unconditionally. The only way to skip the else block is to avoid reaching the end of the for/while block: raise, break or return out of the body of the block. > It would be even better if it was "else if not break:" to make the > meaning clearer. break is not the only way to exit the for loop. > I would agree that it would be even better than that if > it was "then if not break:" (apart from needing the new keyword ;)), as > then the conditional aspect is explicit. But it isn't conditional. Your syntax implies that the interpreter keeps some sort of flag did_we_reach_the_end_of_the_loop_without_break or something, and then it checks the state of that flag. There is no such flag. If I remember correctly, that last time this came up here was because somebody asked how they could read the value of that flag, instead of setting their own: for x in seq: ... if not did_we_reach_the_end_of_the_loop_without_break: print("break") They hoped to use the same flag the for-loop used. But if you use the dis module to decompile the CPython byte-code you will see that there is no such flag. Code like this: for i in seq: break else: foo bar compiles to something like this (results may vary according to the version of Python): py> import dis py> code = compile(""" ... for i in seq: ... break ... else: ... foo ... bar ... """, "", "exec") py> dis.dis(code) 2 0 SETUP_LOOP 19 (to 22) 3 LOAD_NAME 0 (seq) 6 GET_ITER >> 7 FOR_ITER 7 (to 17) 10 STORE_NAME 1 (i) 3 13 BREAK_LOOP 14 JUMP_ABSOLUTE 7 >> 17 POP_BLOCK 5 18 LOAD_NAME 2 (foo) 21 POP_TOP 6 >> 22 LOAD_NAME 3 (bar) 25 POP_TOP 26 LOAD_CONST 0 (None) 29 RETURN_VALUE Not a single condition to be seen, anywhere. Its all unconditional jumps. To anticipate a possible objection: it is possible that the FOR_ITER bytecode is implemented with a conditional test, but even so, all that tests for is whether to enter the main body of the for-loop (10 STORE_NAME ...) or jump to (18 LOAD_NAME ...). If you remove the break completely, or replace it with raise or (in a function) return, and you'll see the same structure to the code: for loops run the main body of the loop, then unconditionally run the else block, then unconditionally exit the loop and continue on with the rest of the program. To avoid the else block, you have to jump out of the entire for-loop. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Sun Jan 29 21:16:38 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Mon, 30 Jan 2017 13:16:38 +1100 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: <588ea206$0$22140$c3e8da3$5496439d@news.astraweb.com> On Mon, 30 Jan 2017 08:12 am, Serhiy Storchaka wrote: > On 28.01.17 10:03, Steve D'Aprano wrote: >> Is shutil.get_terminal_size useless? When, if ever, should I use it in >> preference to the os version? If the shutil version is broken, can it be >> fixed? > > Read the history of shutil.get_terminal_size(). All this was discussed. Where? -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From steve+python at pearwood.info Sun Jan 29 21:49:43 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Mon, 30 Jan 2017 13:49:43 +1100 Subject: Rename file without overwriting existing files Message-ID: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> This code contains a Time Of Check to Time Of Use bug: if os.path.exists(destination) raise ValueError('destination already exists') os.rename(oldname, destination) In the microsecond between checking for the existence of the destination and actually doing the rename, it is possible that another process may create the destination, resulting in data loss. Apart from keeping my fingers crossed, how should I fix this TOCTOU bug? -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From eryksun at gmail.com Sun Jan 29 21:51:14 2017 From: eryksun at gmail.com (eryk sun) Date: Mon, 30 Jan 2017 02:51:14 +0000 Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588ea206$0$22140$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588ea206$0$22140$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jan 30, 2017 at 2:16 AM, Steve D'Aprano wrote: > On Mon, 30 Jan 2017 08:12 am, Serhiy Storchaka wrote: > >> On 28.01.17 10:03, Steve D'Aprano wrote: >>> Is shutil.get_terminal_size useless? When, if ever, should I use it in >>> preference to the os version? If the shutil version is broken, can it be >>> fixed? >> >> Read the history of shutil.get_terminal_size(). All this was discussed. > > Where? See issue 13609: http://bugs.python.org/issue13609 From rosuav at gmail.com Sun Jan 29 22:27:08 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 14:27:08 +1100 Subject: Rename file without overwriting existing files In-Reply-To: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Jan 30, 2017 at 1:49 PM, Steve D'Aprano wrote: > This code contains a Time Of Check to Time Of Use bug: > > if os.path.exists(destination) > raise ValueError('destination already exists') > os.rename(oldname, destination) > > > In the microsecond between checking for the existence of the destination and > actually doing the rename, it is possible that another process may create > the destination, resulting in data loss. > > Apart from keeping my fingers crossed, how should I fix this TOCTOU bug? The Linux kernel (sorry, I don't know about others) provides a renameat2() system call that has the option of failing if the destination exists. However, I can't currently see any way to call that from CPython. Seems like an excellent feature request - another keyword-only argument for os.rename(), like the directory file descriptors. ChrisA From python at mrabarnett.plus.com Sun Jan 29 22:45:20 2017 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 30 Jan 2017 03:45:20 +0000 Subject: Rename file without overwriting existing files In-Reply-To: References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> Message-ID: <42cccb97-f771-9e65-3b8d-4fc01cab3cd1@mrabarnett.plus.com> On 2017-01-30 03:27, Chris Angelico wrote: > On Mon, Jan 30, 2017 at 1:49 PM, Steve D'Aprano > wrote: >> This code contains a Time Of Check to Time Of Use bug: >> >> if os.path.exists(destination) >> raise ValueError('destination already exists') >> os.rename(oldname, destination) >> >> >> In the microsecond between checking for the existence of the destination and >> actually doing the rename, it is possible that another process may create >> the destination, resulting in data loss. >> >> Apart from keeping my fingers crossed, how should I fix this TOCTOU bug? > > The Linux kernel (sorry, I don't know about others) provides a > renameat2() system call that has the option of failing if the > destination exists. However, I can't currently see any way to call > that from CPython. Seems like an excellent feature request - another > keyword-only argument for os.rename(), like the directory file > descriptors. > On Windows it raises FileExistsError if the destination already exists. shutil.move, on the other hand, replaces if the destination already exists. From cs at zip.com.au Sun Jan 29 23:33:03 2017 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 30 Jan 2017 15:33:03 +1100 Subject: Rename file without overwriting existing files In-Reply-To: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20170130043303.GA78047@cskk.homeip.net> On 30Jan2017 13:49, Steve D'Aprano wrote: >This code contains a Time Of Check to Time Of Use bug: > > if os.path.exists(destination) > raise ValueError('destination already exists') > os.rename(oldname, destination) > > >In the microsecond between checking for the existence of the destination and >actually doing the rename, it is possible that another process may create >the destination, resulting in data loss. > >Apart from keeping my fingers crossed, how should I fix this TOCTOU bug? For files this is a problem at the Python level. At the UNIX level you can play neat games with open(2) and the various O_* modes. however, with directories things are more cut and dry. Do you have much freedom here? What's the wider context of the question? Cheers, Cameron Simpson From Irv at furrypants.com Mon Jan 30 00:03:33 2017 From: Irv at furrypants.com (Irv Kalb) Date: Sun, 29 Jan 2017 21:03:33 -0800 Subject: Overriding True and False ? Message-ID: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> I teach intro to programming using Python. In my first assignment, students are asked to assign variables of different types and print out the values. One student (who really did not understand Booleans) turned in the following for his/her interpretation of Booleans (Python 2.7): True = 'shadow' False = 'light' print "If the sun is behind a cloud, there is", True print "If it is a clear day, there is", False And it printed: If the sun is behind a cloud, there is shadow If it is a clear day, there is light It seems very odd that Python allows you to override the values of True and False. In the code, True and False were clearly recognized as keywords as they were colored purple. But there was no error message. You cannot assign new values to other keywords. Simple tests of things like: for = 5 while = 2 not = 3 As expected, all result in SyntaxError: invalid syntax. Why would Python allow you to override the values of True and False? I wonder if this is some sort of historical thing as these are the only keywords besides None that are uppercased. This line: None = 5 Even gives a special SyntaxError: cannot assign to None Just curious, Irv From inyeol.lee at gmail.com Mon Jan 30 00:24:07 2017 From: inyeol.lee at gmail.com (inyeol.lee at gmail.com) Date: Sun, 29 Jan 2017 21:24:07 -0800 (PST) Subject: GeneratorExit masks StopIteration? Message-ID: <56c5f0c3-cfc9-4555-8fd6-a23f4bc5ff32@googlegroups.com> Does generator.close() prevent raising StopIteration? I'm trying to get the return value from coroutine after terminating it. Here is simple test code: $ python3 Python 3.6.0 (default, Dec 23 2016, 12:50:55) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def cor(): ... try: ... item = yield ... except GeneratorExit: ... return 1 ... >>> c = cor() >>> next(c) >>> c.close() >>> I was expecting StopIteration from c.close() call, but Python 3.6 doesn't raise any. Is this behavior expected? I couldn't find any reference regarding GeneratorExit and StopIteration interaction. From steve+comp.lang.python at pearwood.info Mon Jan 30 00:37:21 2017 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Mon, 30 Jan 2017 16:37:21 +1100 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: <588ed113$0$1519$c3e8da3$5496439d@news.astraweb.com> On Monday 30 January 2017 08:12, Serhiy Storchaka wrote: > On 28.01.17 10:03, Steve D'Aprano wrote: >> Is shutil.get_terminal_size useless? When, if ever, should I use it in >> preference to the os version? If the shutil version is broken, can it be >> fixed? > > Read the history of shutil.get_terminal_size(). All this was discussed. Yes, it was discussed, but not resolved: Antoine Pitrou just closed the task and declared it done, without resolving the failures I am talking about here. http://bugs.python.org/issue13609 (Thanks Eryk Sun for the link.) -- Steven "Ever since I learned about confirmation bias, I've been seeing it everywhere." - Jon Ronson From rosuav at gmail.com Mon Jan 30 00:54:31 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 16:54:31 +1100 Subject: GeneratorExit masks StopIteration? In-Reply-To: <56c5f0c3-cfc9-4555-8fd6-a23f4bc5ff32@googlegroups.com> References: <56c5f0c3-cfc9-4555-8fd6-a23f4bc5ff32@googlegroups.com> Message-ID: On Mon, Jan 30, 2017 at 4:24 PM, wrote: > I was expecting StopIteration from c.close() call, but Python 3.6 doesn't raise any. > Is this behavior expected? I couldn't find any reference regarding GeneratorExit and StopIteration interaction. When you close() a generator, it raises GeneratorExit into it, and then silences any StopIteration or GeneratorExit that comes out of it. If you need different behaviour, what you could do is explicitly throw() into it: >>> c.throw(GeneratorExit) Traceback (most recent call last): File "", line 1, in StopIteration: 1 which you'd obviously want to wrap in try/except, but at that point, you have the result. Maybe this could be a feature request - that generator.close() returns the return value of the generator (or None if the GeneratorExit comes out)? ChrisA From best_lay at yahoo.com Mon Jan 30 00:59:19 2017 From: best_lay at yahoo.com (Wildman) Date: Sun, 29 Jan 2017 23:59:19 -0600 Subject: Is shutil.get_terminal_size useless? References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, 28 Jan 2017 19:03:42 +1100, Steve D'Aprano wrote: > shutil.get_terminal_size returns the wrong values when you pipe your output > to another process, even it you do so in a terminal. Consider this script: > > > import os > import shutil > print('shutil:', shutil.get_terminal_size(fallback=(999, 999))) > print('os:', os.get_terminal_size(0)) > > > That uses two different methods to get the terminal size. If I run that in a > terminal in the normal way, it works fine, returning the correct size for > my terminal: > > > [steve at ando ~]$ python3.5 test_gts.py > shutil: os.terminal_size(columns=116, lines=29) > os: os.terminal_size(columns=116, lines=29) > > > But if I pipe the output to something else, the shutil version fails to > determine the correct terminal size, and falls back on the default: > > > [steve at ando ~]$ python3.5 test_gts.py | cat > shutil: os.terminal_size(columns=999, lines=999) > os: os.terminal_size(columns=116, lines=29) > > > while the os version gives the correct result. > > Is shutil.get_terminal_size useless? When, if ever, should I use it in > preference to the os version? If the shutil version is broken, can it be > fixed? > > > Thanks to Bernardas Ali?auskas: > > http://granitosaurus.rocks/getting-terminal-size.html I can suggest a third approach to try but I have not tried piping it. YMMV. import fcntl import os import struct import termios tty = os.open(os.ctermid(), os.O_RDONLY) ts = struct.unpack("hh", fcntl.ioctl(tty, termios.TIOCGWINSZ, "1234")) os.close(tty) columns = ts[1] rows = ts[0] print(str(columns) + "x" + str(rows)) -- GNU/Linux user #557453 The cow died so I don't need your bull! From python at deborahswanson.net Mon Jan 30 01:09:07 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 29 Jan 2017 22:09:07 -0800 Subject: GeneratorExit masks StopIteration? In-Reply-To: <56c5f0c3-cfc9-4555-8fd6-a23f4bc5ff32@googlegroups.com> Message-ID: <002a01d27abf$5e8a2660$27b23dae@sambora> inyeol.lee at gmail.com wrote, on January 29, 2017 9:24 PM > > Does generator.close() prevent raising StopIteration? > > I'm trying to get the return value from coroutine after > terminating it. Here is simple test code: > > $ python3 > Python 3.6.0 (default, Dec 23 2016, 12:50:55) > [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on > darwin Type "help", "copyright", "credits" or "license" for > more information. > >>> def cor(): > ... try: > ... item = yield > ... except GeneratorExit: > ... return 1 > ... > >>> c = cor() > >>> next(c) > >>> c.close() > >>> > > I was expecting StopIteration from c.close() call, but Python > 3.6 doesn't raise any. Is this behavior expected? I couldn't > find any reference regarding GeneratorExit and StopIteration > interaction. Use except StopIteration: (Not sure what generator.close() should do, or if GeneratorExit is any kind of equivalent, but StopIteration works. I'm less familiar with Python 2.) Python 3.4.3 |Anaconda 2.3.0 (32-bit)| (default, Mar 6 2015, 12:08:17) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> help(generator.close) Traceback (most recent call last): File "", line 1, in help(generator.close) NameError: name 'generator' is not defined So at the very least, generator needs to be defined, if it's possible to define or import it in Python 3. Probably not. In Python 2, GeneratorExit is raised when the generator's close() method is called. But in my quick search, I couldn't find GeneratorExit in Python 3, or any note that it had been dropped, but it's a good guess that it was dropped. It's redundant with StopIteration. I always use StopIteration. It works in all Python versions. From hmmeeranrizvi18 at gmail.com Mon Jan 30 01:32:31 2017 From: hmmeeranrizvi18 at gmail.com (hmmeeranrizvi18 at gmail.com) Date: Sun, 29 Jan 2017 22:32:31 -0800 (PST) Subject: Display a label while pressing the button in my GUI Message-ID: Hello Guys, Here i am creating a GUI which will act as a search engine that will find the results from the browser and save the results as a xls file. When i typed something in my search box and click the (GO)button.It should display search in progress.when the file is saved it should display done. How to do that? My button gets hung for a seconds.We should give any timeout for that? Here is my script from Tkinter import * import mechanize def clear_search(event): search.delete(0,END) obj = Tk() Label = Label(obj,text="Top Findings:",font="-weight bold") search = Entry(obj,width=100) search.insert(0, "Enter the value to search") search.bind("", clear_search) def fun(): new = search.get() url = "http://duckduckgo.com/html" br = mechanize.Browser() br.set_handle_robots(False) br.open(url) br.select_form(name="x") br["q"] = str(new) res = br.submit() content = res.read() with open("result1.xls", "w") as f: f.write(content) fun() Go = Button(obj,text="GO",width=5,command=fun) Label.pack() search.pack() Go.pack() mainloop() From inyeol.lee at gmail.com Mon Jan 30 01:38:26 2017 From: inyeol.lee at gmail.com (inyeol.lee at gmail.com) Date: Sun, 29 Jan 2017 22:38:26 -0800 (PST) Subject: GeneratorExit masks StopIteration? In-Reply-To: References: <56c5f0c3-cfc9-4555-8fd6-a23f4bc5ff32@googlegroups.com> Message-ID: On Sunday, January 29, 2017 at 9:54:44 PM UTC-8, Chris Angelico wrote: > ... > When you close() a generator, it raises GeneratorExit into it, and > then silences any StopIteration or GeneratorExit that comes out of it. Chris, Thanks for the info. Is this (GenExit silencing StopIteration) documented somewhere? I was suspecting this but couldn't find any reference. From rosuav at gmail.com Mon Jan 30 01:46:55 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 17:46:55 +1100 Subject: GeneratorExit masks StopIteration? In-Reply-To: References: <56c5f0c3-cfc9-4555-8fd6-a23f4bc5ff32@googlegroups.com> Message-ID: On Mon, Jan 30, 2017 at 5:38 PM, wrote: > On Sunday, January 29, 2017 at 9:54:44 PM UTC-8, Chris Angelico wrote: >> ... >> When you close() a generator, it raises GeneratorExit into it, and >> then silences any StopIteration or GeneratorExit that comes out of it. > > Chris, > Thanks for the info. Is this (GenExit silencing StopIteration) documented somewhere? > I was suspecting this but couldn't find any reference. Actually..... think this might be incorrect. I didn't look in the docs, I looked in the source code, so my information is guaranteed accurate; this is where I would expect the information to be: https://docs.python.org/3/reference/expressions.html#generator.close As a general rule, a generator shouldn't be leaking StopIteration. In a future version of Python, this will trigger RuntimeError. (You can get that behaviour in 3.6 with a future import, too.) So what this really means is that close() will suppress any GeneratorExit, or the generator returning, both of which are normal occurrences. The only significant thing is that the generator's return value could usefully be propagated out of close(). ChrisA From inyeol.lee at gmail.com Mon Jan 30 02:17:09 2017 From: inyeol.lee at gmail.com (inyeol.lee at gmail.com) Date: Sun, 29 Jan 2017 23:17:09 -0800 (PST) Subject: GeneratorExit masks StopIteration? In-Reply-To: References: <56c5f0c3-cfc9-4555-8fd6-a23f4bc5ff32@googlegroups.com> Message-ID: <8683ea83-1c3b-44d0-a7d4-764743c7e33a@googlegroups.com> On Sunday, January 29, 2017 at 10:47:09 PM UTC-8, Chris Angelico wrote: > On Mon, Jan 30, 2017 at 5:38 PM, wrote: > > On Sunday, January 29, 2017 at 9:54:44 PM UTC-8, Chris Angelico wrote: > >> ... > >> When you close() a generator, it raises GeneratorExit into it, and > >> then silences any StopIteration or GeneratorExit that comes out of it. > > > > Chris, > > Thanks for the info. Is this (GenExit silencing StopIteration) documented somewhere? > > I was suspecting this but couldn't find any reference. > > Actually..... think this might be incorrect. I didn't look in the > docs, I looked in the source code, so my information is guaranteed > accurate I found PEP-342 describes this behavior - silencing other GenExit or StopIteration. BTW, the reason why I was checking this was to find a solution on how to get return value from coroutine without relying on some sentinel value, something like (not tested): def accumulator(): sum = 0 try: while True: sum += yield except GeneratorExit: return sum Any alternatives? Explicitly throwing GenExit looks like a hack. From jcasale at activenetwerx.com Mon Jan 30 02:25:05 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 30 Jan 2017 07:25:05 +0000 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: <001801d27a7c$6452b140$27b23dae@sambora> References: <001801d27a7c$6452b140$27b23dae@sambora> Message-ID: > What .NET APIs are anticipated to be released that aren't on the > official CLI list now: > https://en.wikipedia.org/wiki/List_of_CLI_languages#Current_Languages, > and/or, are .NET supported languages expected to expand beyond the CLI > list? I think this (and the last point) misinterprets the semantics of the OPs post. .NET Core is not .NET, it's a **multi-platform** framework for a language syntax specification (c#). It's not an implementation of some API that you cannleverage in another language specification. Writing a ctype accessor for a Windows API does not equate to cpp code that is now magically Python. It means no more than what it is, "a foreign function library for Python" (https://docs.python.org/3/library/ctypes.html). .Net Core is fundamentally different and much like Python in the way that a compiler and runtime for a common language syntax specification has been written for multiple platforms. So in general, the same Python script could run on both platforms, and most certainly the same .NET Core source could compile and run on its supported platforms. So back to the question, .NET Core is a wip which where applicable will soon present all the existing .NET APIs, excluding those which are Windows specific and don't make sense to present on a non-Windows platform like Windows Forms. jlc From songofacandy at gmail.com Mon Jan 30 02:38:31 2017 From: songofacandy at gmail.com (INADA Naoki) Date: Mon, 30 Jan 2017 16:38:31 +0900 Subject: Overriding True and False ? In-Reply-To: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> Message-ID: It's fixed already in Python 3. Please use Python 3 when teaching to students. $ python3 Python 3.6.0 (default, Dec 24 2016, 00:01:50) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> True = "foo" File "", line 1 SyntaxError: can't assign to keyword From denis.akhiyarov at gmail.com Mon Jan 30 02:49:19 2017 From: denis.akhiyarov at gmail.com (denis.akhiyarov at gmail.com) Date: Sun, 29 Jan 2017 23:49:19 -0800 (PST) Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: Message-ID: <7f5e4c86-181b-457a-ab6c-1cea0989aed9@googlegroups.com> On Sunday, January 29, 2017 at 4:00:27 PM UTC-6, Gregory Ewing wrote: > Joseph L. Casale wrote: > >>.NET is a library that can be used from many languages, including Python. > > > > No. > > Yes: > > http://pythonnet.sourceforge.net/ > > "Python for .NET is a package that gives Python programmers nearly seamless > integration with the .NET Common Language Runtime (CLR) and provides a powerful > application scripting tool for .NET developers. Using this package you can > script .NET applications or build entire applications in Python, using .NET > services and components written in any language that targets the CLR (Managed > C++, C#, VB, JScript)." > > -- > Greg This is outdated location. pythonnet (python for .NET) is on GitHub since 2014. https://github.com/pythonnet/pythonnet We just released v2.2.2 with Python 3.6 support and transition to MIT license. Download from PYPI using pip or from Anaconda using conda: https://pypi.python.org/pypi/pythonnet/2.2.2 https://anaconda.org/pythonnet/pythonnet From python at deborahswanson.net Mon Jan 30 02:50:21 2017 From: python at deborahswanson.net (Deborah Swanson) Date: Sun, 29 Jan 2017 23:50:21 -0800 Subject: Overriding True and False ? In-Reply-To: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> Message-ID: <000a01d27acd$82b3c6f0$27b23dae@sambora> Irv Kalb wrote, on Sunday, January 29, 2017 9:04 PM > > I teach intro to programming using Python. In my first > assignment, students are asked to assign variables of > different types and print out the values. > > One student (who really did not understand Booleans) turned > in the following for his/her interpretation of Booleans (Python 2.7): > > True = 'shadow' > False = 'light' > print "If the sun is behind a cloud, there is", True > print "If it is a clear day, there is", False > > And it printed: > > If the sun is behind a cloud, there is shadow > If it is a clear day, there is light > > > It seems very odd that Python allows you to override the > values of True and False. In the code, True and False were > clearly recognized as keywords as they were colored purple. > But there was no error message. > > You cannot assign new values to other keywords. Simple tests > of things like: > > for = 5 > > while = 2 > > not = 3 > > As expected, all result in SyntaxError: invalid syntax. Why > would Python allow you to override the values of True and > False? I wonder if this is some sort of historical thing as > these are the only keywords besides None that are uppercased. > This line: > > None = 5 > > Even gives a special SyntaxError: cannot assign to None > > Just curious, > > Irv Just guessing, but in the examples you give in Python 2.7, substitute strings are syntactically correct in print statements, but: 5 in list('abc'): 2 True: if a 3 b: would all be syntactical errors. As is 'None = 5'. Looks like the moral of the story is that in Python 2.7 you can redefine keywords, so long as you don't get any syntax errors after (or during) redefinition. From ben+python at benfinney.id.au Mon Jan 30 02:50:56 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 30 Jan 2017 18:50:56 +1100 Subject: Overriding True and False ? References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> Message-ID: <85zii9t28v.fsf@benfinney.id.au> Irv Kalb writes: > I teach intro to programming using Python. [?] Thank you for teaching Python to beginners! > It seems very odd that Python allows you to override the values of > True and False. Yes, it is. That's why Python 3 forbids it:: >>> True = "shadow" File "", line 1 SyntaxError: can't assign to keyword >>> False = "light" File "", line 1 SyntaxError: can't assign to keyword When teaching Python, please do not teach Python 2. Your students should learn Python 3 first, primarily, and for most of the course. Python 2 is a legacy that will never gain new features, and will only slip further behind the current supported Python version. That makes Python 2 a poor choice for teaching to beginners. -- \ ?The entertainment industry calls DRM "security" software, | `\ because it makes them secure from their customers.? ?Cory | _o__) Doctorow, 2014-02-05 | Ben Finney From rosuav at gmail.com Mon Jan 30 02:54:28 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 18:54:28 +1100 Subject: Overriding True and False ? In-Reply-To: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> Message-ID: On Mon, Jan 30, 2017 at 4:03 PM, Irv Kalb wrote: > It seems very odd that Python allows you to override the values of True and False. In the code, True and False were clearly recognized as keywords as they were colored purple. But there was no error message. > > You cannot assign new values to other keywords. Simple tests of things like: > > for = 5 > > while = 2 > > not = 3 > > As expected, all result in SyntaxError: invalid syntax. Why would Python allow you to override the values of True and False? I wonder if this is some sort of historical thing as these are the only keywords besides None that are uppercased. This line: > > None = 5 > > Even gives a special SyntaxError: cannot assign to None There are slightly different things going on here. Trying to assign to a piece of syntax like "while" makes absolutely no sense, but trying to assign to "None" is structurally sane, yet disallowed. IIRC there's only one non-assignable name in Python 2 (None), but as mentioned, Python 3 adds True and False to that. (Interestingly, Ellipsis is not included in that.) > I teach intro to programming using Python. May I please request that you consider teaching Python 3? Python 2 isn't going anywhere (for better or for worse), and Py3 is a superior language in many ways, not least of which is that it keeps text and bytes separate, giving text the full power that it should have. For a beginning programmer, this is very helpful; there's nothing to un-learn when going international. (There will be new nuances to be learned, such as RTL text, but nothing to unlearn.) Python 3 also fixes a number of other problems that Python 2 inherited from C, making it altogether a better language for teaching with. ChrisA From rosuav at gmail.com Mon Jan 30 02:57:11 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 18:57:11 +1100 Subject: Overriding True and False ? In-Reply-To: <000a01d27acd$82b3c6f0$27b23dae@sambora> References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> <000a01d27acd$82b3c6f0$27b23dae@sambora> Message-ID: On Mon, Jan 30, 2017 at 6:50 PM, Deborah Swanson wrote: > Looks like the moral of the story is that in Python 2.7 you can redefine > keywords, so long as you don't get any syntax errors after (or during) > redefinition. The moral is actually that "True" and "False" aren't keywords in Py2. They're just built-ins. Compare: rosuav at sikorsky:~$ python3 Python 3.7.0a0 (default:cebc9c7ad195, Jan 24 2017, 06:55:19) [GCC 6.2.0 20161027] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import dis >>> dis.dis(lambda: True) 1 0 LOAD_CONST 1 (True) 2 RETURN_VALUE >>> rosuav at sikorsky:~$ python Python 2.7.12+ (default, Sep 1 2016, 20:27:38) [GCC 6.2.0 20160927] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import dis >>> dis.dis(lambda: True) 1 0 LOAD_GLOBAL 0 (True) 3 RETURN_VALUE >>> In Python 2, returning "True" involves a name lookup; in Python 3, it's simply a constant - a literal. ChrisA From rosuav at gmail.com Mon Jan 30 03:00:25 2017 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Jan 2017 19:00:25 +1100 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <001801d27a7c$6452b140$27b23dae@sambora> Message-ID: On Mon, Jan 30, 2017 at 6:25 PM, Joseph L. Casale wrote: > .Net Core is fundamentally different and much like Python in the way that > a compiler and runtime for a common language syntax specification has been > written for multiple platforms. So in general, the same Python script could > run on both platforms, and most certainly the same .NET Core source could > compile and run on its supported platforms. What do you mean by "both platforms"? Python scripts already run on three major operating systems (Win/Lin/Mac) and a good number of less-popular OSes; a well-written Python script will run in four major Pythons (CPython, PyPy, Jython, IronPython) and a number of others; and all manner of CPU architectures and so on. ChrisA From __peter__ at web.de Mon Jan 30 04:31:01 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 10:31:01 +0100 Subject: Display a label while pressing the button in my GUI References: Message-ID: hmmeeranrizvi18 at gmail.com wrote: > Hello Guys, > Here i am creating a GUI which will act as a search engine that will find > the results from the browser and save the results as a xls file. When i > typed something in my search box and click the (GO)button.It should > display search in progress.when the file is saved it should display done. > How to do that? My button gets hung for a seconds.We should give any > timeout for that? Move the actual processing into another thread and have it communicate with your GUI through a Queue. Here's a sketch: import Queue import threading from Tkinter import * import mechanize def clear_search(event): search.delete(0, END) def start_search(): thread = threading.Thread( target=fun, args=(search.get(),) ) thread.setDaemon(True) thread.start() root.after(100, check_state) def fun(new): queue.put("searching") url = "http://duckduckgo.com/html" br = mechanize.Browser() br.set_handle_robots(False) br.open(url) br.select_form(name="x") br["q"] = str(new) res = br.submit() queue.put("writing result") content = res.read() with open("result1.xls", "w") as f: f.write(content) queue.put("done") def check_state(): while True: try: message = queue.get_nowait() except Queue.Empty: break state_info["text"] = message if message == "done": return root.after(100, check_state) queue = Queue.Queue() root = Tk() top_findings = Label(root, text="Top Findings:", font="-weight bold") top_findings.pack() search = Entry(root, width=100) search.insert(0, "Enter the value to search") # Did you see Christian Gollwitzer's post? search.bind("", clear_search) search.pack() go_button = Button(root, text="GO", width=5, command=start_search) go_button.pack() state_info = Label(root) state_info.pack() root.mainloop() From steve+python at pearwood.info Mon Jan 30 05:16:29 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Mon, 30 Jan 2017 21:16:29 +1100 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> Message-ID: <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote: > On 30Jan2017 13:49, Steve D'Aprano wrote: >>This code contains a Time Of Check to Time Of Use bug: >> >> if os.path.exists(destination) >> raise ValueError('destination already exists') >> os.rename(oldname, destination) >> >> >>In the microsecond between checking for the existence of the destination >>and actually doing the rename, it is possible that another process may >>create the destination, resulting in data loss. >> >>Apart from keeping my fingers crossed, how should I fix this TOCTOU bug? > > For files this is a problem at the Python level. At the UNIX level you can > play neat games with open(2) and the various O_* modes. > > however, with directories things are more cut and dry. Do you have much > freedom here? What's the wider context of the question? The wider context is that I'm taking from 1 to path names to existing files as arguments, and for each path name I transfer the file name part (but not the directory part) and then rename the file. For example: foo/bar/baz/spam.txt may be renamed to: foo/bar/baz/ham.txt but only provided ham.txt doesn't already exist. -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From p.f.moore at gmail.com Mon Jan 30 05:31:59 2017 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 30 Jan 2017 02:31:59 -0800 (PST) Subject: Is shutil.get_terminal_size useless? In-Reply-To: <588ed113$0$1519$c3e8da3$5496439d@news.astraweb.com> References: <588c505f$0$1583$c3e8da3$5496439d@news.astraweb.com> <588ed113$0$1519$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Monday, 30 January 2017 05:37:32 UTC, Steven D'Aprano wrote: > On Monday 30 January 2017 08:12, Serhiy Storchaka wrote: > > > On 28.01.17 10:03, Steve D'Aprano wrote: > >> Is shutil.get_terminal_size useless? When, if ever, should I use it in > >> preference to the os version? If the shutil version is broken, can it be > >> fixed? > > > > Read the history of shutil.get_terminal_size(). All this was discussed. > > > Yes, it was discussed, but not resolved: Antoine Pitrou just closed the task > and declared it done, without resolving the failures I am talking about here. > > http://bugs.python.org/issue13609 > > (Thanks Eryk Sun for the link.) Looks like it was closed with the comment "If you want further features, please open a new issue". It sounds to me like os.get_terminal_size() does what you want, and others find the shutil version useful. But if you have a use case for the shutil version that needs a change in its behaviour, I guess you should open an issue for that. The docs on shutil.get_terminal_size seem pretty clear on what it does (whether you agree with the behaviour or not), so I don't think there's much benefit to proposing changes to the docs alone. Paul From __peter__ at web.de Mon Jan 30 05:39:49 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 11:39:49 +0100 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steve D'Aprano wrote: > On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote: > >> On 30Jan2017 13:49, Steve D'Aprano wrote: >>>This code contains a Time Of Check to Time Of Use bug: >>> >>> if os.path.exists(destination) >>> raise ValueError('destination already exists') >>> os.rename(oldname, destination) >>> >>> >>>In the microsecond between checking for the existence of the destination >>>and actually doing the rename, it is possible that another process may >>>create the destination, resulting in data loss. >>> >>>Apart from keeping my fingers crossed, how should I fix this TOCTOU bug? >> >> For files this is a problem at the Python level. At the UNIX level you >> can play neat games with open(2) and the various O_* modes. >> >> however, with directories things are more cut and dry. Do you have much >> freedom here? What's the wider context of the question? > > The wider context is that I'm taking from 1 to > path names to existing files as arguments, and for each path name I > transfer the file name part (but not the directory part) and then rename > the file. For example: > > foo/bar/baz/spam.txt > > may be renamed to: > > foo/bar/baz/ham.txt > > but only provided ham.txt doesn't already exist. Google finds http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions and from a quick test it appears to work on Linux: $ echo foo > foo $ echo bar > bar $ python3 Python 3.4.3 (default, Nov 17 2016, 01:08:31) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> def rename(source, dest): ... os.link(source, dest) ... os.unlink(source) ... >>> rename("foo", "baz") >>> os.listdir() ['bar', 'baz'] >>> rename("bar", "baz") Traceback (most recent call last): File "", line 1, in File "", line 2, in rename FileExistsError: [Errno 17] File exists: 'bar' -> 'baz' From jussi.piitulainen at helsinki.fi Mon Jan 30 05:55:45 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Mon, 30 Jan 2017 12:55:45 +0200 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: Peter Otten writes: > Steve D'Aprano wrote: > >> On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote: >> >>> On 30Jan2017 13:49, Steve D'Aprano wrote: >>>>This code contains a Time Of Check to Time Of Use bug: >>>> >>>> if os.path.exists(destination) >>>> raise ValueError('destination already exists') >>>> os.rename(oldname, destination) >>>> >>>> >>>>In the microsecond between checking for the existence of the destination >>>>and actually doing the rename, it is possible that another process may >>>>create the destination, resulting in data loss. >>>> >>>>Apart from keeping my fingers crossed, how should I fix this TOCTOU bug? >>> >>> For files this is a problem at the Python level. At the UNIX level you >>> can play neat games with open(2) and the various O_* modes. >>> >>> however, with directories things are more cut and dry. Do you have much >>> freedom here? What's the wider context of the question? >> >> The wider context is that I'm taking from 1 to >> path names to existing files as arguments, and for each path name I >> transfer the file name part (but not the directory part) and then rename >> the file. For example: >> >> foo/bar/baz/spam.txt >> >> may be renamed to: >> >> foo/bar/baz/ham.txt >> >> but only provided ham.txt doesn't already exist. > > Google finds > > http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions > > and from a quick test it appears to work on Linux: It doesn't seem to be documented. I looked at help(os.link) on Python 3.4 and the corresponding current library documentation on the web. I saw no mention of what happens when dst exists already. Also, creating a hard link doesn't seem to work between different file systems, which may well be relevant to Steve's case. I get: OSError: [Errno 18] Invalid cross-device link: [snip] And that also is not mentioned in the docs. From wolfgang.maier at biologie.uni-freiburg.de Mon Jan 30 06:24:58 2017 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Mon, 30 Jan 2017 12:24:58 +0100 Subject: Rename file without overwriting existing files In-Reply-To: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 01/30/2017 03:49 AM, Steve D'Aprano wrote: > This code contains a Time Of Check to Time Of Use bug: > > if os.path.exists(destination) > raise ValueError('destination already exists') > os.rename(oldname, destination) > > > In the microsecond between checking for the existence of the destination and > actually doing the rename, it is possible that another process may create > the destination, resulting in data loss. > > Apart from keeping my fingers crossed, how should I fix this TOCTOU bug? > There is a rather extensive discussion of this problem (with no good cross-platform solution if I remember correctly): https://mail.python.org/pipermail/python-ideas/2011-August/011131.html which is related to http://bugs.python.org/issue12741 Wolfgang From hmmeeranrizvi18 at gmail.com Mon Jan 30 07:11:01 2017 From: hmmeeranrizvi18 at gmail.com (Meeran Rizvi) Date: Mon, 30 Jan 2017 04:11:01 -0800 (PST) Subject: Display a label while pressing the button in my GUI In-Reply-To: References: Message-ID: <975c607d-ae84-43a4-a0e6-fa18e9f2b5d3@googlegroups.com> On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote: > Hello Guys, > Here i am creating a GUI which will act as a search engine that will find the results from the browser and save the results as a xls file. > When i typed something in my search box and click the (GO)button.It should display search in progress.when the file is saved it should display done. > How to do that? > My button gets hung for a seconds.We should give any timeout for that? > > Here is my script > > from Tkinter import * > import mechanize > def clear_search(event): > search.delete(0,END) > obj = Tk() > Label = Label(obj,text="Top Findings:",font="-weight bold") > search = Entry(obj,width=100) > search.insert(0, "Enter the value to search") > search.bind("", clear_search) > def fun(): > new = search.get() > url = "http://duckduckgo.com/html" > br = mechanize.Browser() > br.set_handle_robots(False) > br.open(url) > br.select_form(name="x") > br["q"] = str(new) > res = br.submit() > content = res.read() > with open("result1.xls", "w") as f: > f.write(content) > fun() > Go = Button(obj,text="GO",width=5,command=fun) > Label.pack() > search.pack() > Go.pack() > mainloop() > Hi Peter, can u explain about these functions def start_search(): thread = threading.Thread( target=fun, args=(search.get(),) ) thread.setDaemon(True) thread.start() root.after(100, check_state) def check_state(): while True: try: message = queue.get_nowait() except Queue.Empty: break state_info["text"] = message if message == "done": return root.after(100, check_state) From __peter__ at web.de Mon Jan 30 08:26:16 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 14:26:16 +0100 Subject: Display a label while pressing the button in my GUI References: <975c607d-ae84-43a4-a0e6-fa18e9f2b5d3@googlegroups.com> Message-ID: Meeran Rizvi wrote: > On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote: >> Hello Guys, >> Here i am creating a GUI which will act as a search engine that will find >> the results from the browser and save the results as a xls file. When i >> typed something in my search box and click the (GO)button.It should >> display search in progress.when the file is saved it should display done. >> How to do that? My button gets hung for a seconds.We should give any >> timeout for that? >> >> Here is my script >> >> from Tkinter import * >> import mechanize >> def clear_search(event): >> search.delete(0,END) >> obj = Tk() >> Label = Label(obj,text="Top Findings:",font="-weight bold") >> search = Entry(obj,width=100) >> search.insert(0, "Enter the value to search") >> search.bind("", clear_search) >> def fun(): >> new = search.get() >> url = "http://duckduckgo.com/html" >> br = mechanize.Browser() >> br.set_handle_robots(False) >> br.open(url) >> br.select_form(name="x") >> br["q"] = str(new) >> res = br.submit() >> content = res.read() >> with open("result1.xls", "w") as f: >> f.write(content) >> fun() >> Go = Button(obj,text="GO",width=5,command=fun) >> Label.pack() >> search.pack() >> Go.pack() >> mainloop() >> > > Hi Peter, > can u explain about these functions > def start_search(): This creates a new thread; target and args specify that in that thread fun will be invoked with the current contents of the search Entry. > thread = threading.Thread( > target=fun, > args=(search.get(),) > ) > thread.setDaemon(True) > thread.start() This tells Tkinter to run the check_state function 100 milliseconds later. > root.after(100, check_state) As the -- aptly named ;) -- start_search() only starts the search without having to wait for the result it will only block the GUI for a moment that should normally be too short for the user to note. You can think of the resulting script as two mini-programs that -- at least conceptually -- run indepently. If they access each other's data you can get an inconsistent state. The Queue class is written with precautions to avoid such problems. check_state() and Tkinter run in the main thread and only receive data from the thread executing fun() through the queue. > def check_state(): > while True: Look into the queue if there is a message from fun(). If there is return it, otherwise instead of waiting for the next message to arrive raise an exception immediately. Remember, if we stay here too long the GUI will lag. > try: > message = queue.get_nowait() > except Queue.Empty: Case (1) No pending messages, break out of the loop. > break Case (2) We have a message. Update the text of the state_info Label accordingly, > state_info["text"] = message then check if it's the last message we expect. > if message == "done": Yes it's the last message; leave check_state() without rescheduling it. > return We got here from the break statement. There are currently no messages, but there may be later, so we ask Tkinter to run check_state again in 100 milliseconds. > root.after(100, check_state) PS: You can verify that you understood this by answering the question: what happens if the user hits the go-button while the second thread is still running. Hint: as written the script does not handle this correctly. From jon+usenet at unequivocal.eu Mon Jan 30 08:36:13 2017 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Mon, 30 Jan 2017 13:36:13 -0000 (UTC) Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2017-01-30, Jussi Piitulainen wrote: > It doesn't seem to be documented. I looked at help(os.link) on Python > 3.4 and the corresponding current library documentation on the web. I > saw no mention of what happens when dst exists already. > > Also, creating a hard link doesn't seem to work between different file > systems, which may well be relevant to Steve's case. I get: > > OSError: [Errno 18] Invalid cross-device link: [snip] > > And that also is not mentioned in the docs. Nor *should* either of those things be mentioned in the Python docs. A lot of the functions of the 'os' module do nothing but call the underlying OS system call with the same name. It would not only be redundant to copy the OS documentation into the Python documentation, it would be misleading and wrong, because of course the behaviour may vary slightly from OS to OS. From __peter__ at web.de Mon Jan 30 08:46:39 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 14:46:39 +0100 Subject: Display a label while pressing the button in my GUI References: <975c607d-ae84-43a4-a0e6-fa18e9f2b5d3@googlegroups.com> Message-ID: Peter Otten wrote: > PS: You can verify that you understood this by answering the question: > what happens if the user hits the go-button while the second thread is > still running. Hint: as written the script does not handle this correctly. On second thought it's probably not as bad as I thought when I wrote the above. I assumed that the messages after the first "done" would remain in the queue forever, but there is another independent check_state() invocation that should read the rest. I hope ;) From __peter__ at web.de Mon Jan 30 08:51:31 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 14:51:31 +0100 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: Jon Ribbens wrote: > On 2017-01-30, Jussi Piitulainen wrote: >> It doesn't seem to be documented. I looked at help(os.link) on Python >> 3.4 and the corresponding current library documentation on the web. I >> saw no mention of what happens when dst exists already. >> >> Also, creating a hard link doesn't seem to work between different file >> systems, which may well be relevant to Steve's case. I get: >> >> OSError: [Errno 18] Invalid cross-device link: [snip] >> >> And that also is not mentioned in the docs. > > Nor *should* either of those things be mentioned in the Python docs. > > A lot of the functions of the 'os' module do nothing but call the > underlying OS system call with the same name. It would not only be > redundant to copy the OS documentation into the Python documentation, > it would be misleading and wrong, because of course the behaviour may > vary slightly from OS to OS. However, the current Python version of link() is sufficiently different from , say, to warrant its own documentation. From __peter__ at web.de Mon Jan 30 08:58:10 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 14:58:10 +0100 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: Jussi Piitulainen wrote: > Peter Otten writes: > >> Steve D'Aprano wrote: >>> The wider context is that I'm taking from 1 to >>> path names to existing files as arguments, and for each path name I >>> transfer the file name part (but not the directory part) and then rename >>> the file. For example: >>> >>> foo/bar/baz/spam.txt >>> >>> may be renamed to: >>> >>> foo/bar/baz/ham.txt >>> >>> but only provided ham.txt doesn't already exist. >> >> Google finds >> >> http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions >> >> and from a quick test it appears to work on Linux: > > It doesn't seem to be documented. For functions with a C equivalent a look into the man page is usually helpful. > I looked at help(os.link) on Python > 3.4 and the corresponding current library documentation on the web. I > saw no mention of what happens when dst exists already. > > Also, creating a hard link doesn't seem to work between different file > systems, which may well be relevant to Steve's case. In his example above he operates inside a single directory. Can one directory spread across multiple file systems? > I get: > > OSError: [Errno 18] Invalid cross-device link: [snip] > > And that also is not mentioned in the docs. From jon+usenet at unequivocal.eu Mon Jan 30 09:10:58 2017 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Mon, 30 Jan 2017 14:10:58 -0000 (UTC) Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2017-01-30, Peter Otten <__peter__ at web.de> wrote: > Jon Ribbens wrote: >> A lot of the functions of the 'os' module do nothing but call the >> underlying OS system call with the same name. It would not only be >> redundant to copy the OS documentation into the Python documentation, >> it would be misleading and wrong, because of course the behaviour may >> vary slightly from OS to OS. > > However, the current Python version of link() is sufficiently different from >, say, to warrant its own documentation. What are you referring to here? As far as I can see, the current Python implementation of link() just calls the underlying OS call directly. From hmmeeranrizvi18 at gmail.com Mon Jan 30 09:13:21 2017 From: hmmeeranrizvi18 at gmail.com (Meeran Rizvi) Date: Mon, 30 Jan 2017 06:13:21 -0800 (PST) Subject: Display a label while pressing the button in my GUI In-Reply-To: References: Message-ID: <05235018-464b-422d-9b38-686c021abf1a@googlegroups.com> On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote: > Hello Guys, > Here i am creating a GUI which will act as a search engine that will find the results from the browser and save the results as a xls file. > When i typed something in my search box and click the (GO)button.It should display search in progress.when the file is saved it should display done. > How to do that? > My button gets hung for a seconds.We should give any timeout for that? > > Here is my script > > from Tkinter import * > import mechanize > def clear_search(event): > search.delete(0,END) > obj = Tk() > Label = Label(obj,text="Top Findings:",font="-weight bold") > search = Entry(obj,width=100) > search.insert(0, "Enter the value to search") > search.bind("", clear_search) > def fun(): > new = search.get() > url = "http://duckduckgo.com/html" > br = mechanize.Browser() > br.set_handle_robots(False) > br.open(url) > br.select_form(name="x") > br["q"] = str(new) > res = br.submit() > content = res.read() > with open("result1.xls", "w") as f: > f.write(content) > fun() > Go = Button(obj,text="GO",width=5,command=fun) > Label.pack() > search.pack() > Go.pack() > mainloop() > Thanks for the explanation Peter From hmmeeranrizvi18 at gmail.com Mon Jan 30 09:14:47 2017 From: hmmeeranrizvi18 at gmail.com (Meeran Rizvi) Date: Mon, 30 Jan 2017 06:14:47 -0800 (PST) Subject: Display a label while pressing the button in my GUI In-Reply-To: References: Message-ID: <35e6a5ba-f128-471b-8a28-ed0bc4ad6c18@googlegroups.com> On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote: > Hello Guys, > Here i am creating a GUI which will act as a search engine that will find the results from the browser and save the results as a xls file. > When i typed something in my search box and click the (GO)button.It should display search in progress.when the file is saved it should display done. > How to do that? > My button gets hung for a seconds.We should give any timeout for that? > > Here is my script > > from Tkinter import * > import mechanize > def clear_search(event): > search.delete(0,END) > obj = Tk() > Label = Label(obj,text="Top Findings:",font="-weight bold") > search = Entry(obj,width=100) > search.insert(0, "Enter the value to search") > search.bind("", clear_search) > def fun(): > new = search.get() > url = "http://duckduckgo.com/html" > br = mechanize.Browser() > br.set_handle_robots(False) > br.open(url) > br.select_form(name="x") > br["q"] = str(new) > res = br.submit() > content = res.read() > with open("result1.xls", "w") as f: > f.write(content) > fun() > Go = Button(obj,text="GO",width=5,command=fun) > Label.pack() > search.pack() > Go.pack() > mainloop() > It is possible to display the output in my GUI itself? From rosuav at gmail.com Mon Jan 30 09:18:39 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 31 Jan 2017 01:18:39 +1100 Subject: Rename file without overwriting existing files In-Reply-To: References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Jan 31, 2017 at 12:58 AM, Peter Otten <__peter__ at web.de> wrote: >> I looked at help(os.link) on Python >> 3.4 and the corresponding current library documentation on the web. I >> saw no mention of what happens when dst exists already. >> >> Also, creating a hard link doesn't seem to work between different file >> systems, which may well be relevant to Steve's case. > > In his example above he operates inside a single directory. Can one > directory spread across multiple file systems? Yep. Try unionfs. ... make ourselves some scratch space ... $ mkdir space modifier $ dd if=/dev/zero of=space.img bs=4096 count=65536 $ mkfs space.img $ sudo mount space.img space $ dd if=/dev/zero of=modifier.img bs=4096 count=1024\ $ mkfs modifier.img $ sudo mount modifier.img modifier ... put some content into the base directory ... $ sudo -e space/demo.txt ... and now the magic: $ unionfs modifier=RW:space=RO joiner/ $ cd joiner At this point, you're in a directory that is the union of the two directories. One of them is read-only, the other is read/write. It is thus possible to view a file that you can't hard-link to a new name, because the new name would have to be created in the 'modifier' file system, but the old file exists on the 'space' one. ChrisA From __peter__ at web.de Mon Jan 30 09:38:12 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 15:38:12 +0100 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: Jon Ribbens wrote: > On 2017-01-30, Peter Otten <__peter__ at web.de> wrote: >> Jon Ribbens wrote: >>> A lot of the functions of the 'os' module do nothing but call the >>> underlying OS system call with the same name. It would not only be >>> redundant to copy the OS documentation into the Python documentation, >>> it would be misleading and wrong, because of course the behaviour may >>> vary slightly from OS to OS. >> >> However, the current Python version of link() is sufficiently different >> from >>, say, to warrant its own documentation. > > What are you referring to here? As far as I can see, the current > Python implementation of link() just calls the underlying OS call > directly. The current signature differs from that of link() os.link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True) but it looks like you are right in so far as link() is still called by default: if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) result = linkat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow, follow_symlinks ? AT_SYMLINK_FOLLOW : 0); else #endif /* HAVE_LINKAT */ result = link(src->narrow, dst->narrow); From jussi.piitulainen at helsinki.fi Mon Jan 30 09:40:15 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Mon, 30 Jan 2017 16:40:15 +0200 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: Peter Otten writes: > Jussi Piitulainen wrote: > >> Peter Otten writes: >> >>> Steve D'Aprano wrote: > >>>> The wider context is that I'm taking from 1 to >>>> path names to existing files as arguments, and for each path name I >>>> transfer the file name part (but not the directory part) and then rename >>>> the file. For example: >>>> >>>> foo/bar/baz/spam.txt >>>> >>>> may be renamed to: >>>> >>>> foo/bar/baz/ham.txt >>>> >>>> but only provided ham.txt doesn't already exist. >>> >>> Google finds >>> >>> http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions >>> >>> and from a quick test it appears to work on Linux: >> >> It doesn't seem to be documented. > > For functions with a C equivalent a look into the man page is usually > helpful. Followed by a few test cases to see what Python actually does, at least in those particular test cases, I suppose. Yes. But is it a bug in Python if a Python function *doesn't* do what the relevant man page in the user's operating system says? Or whatever the user's documentation entry is called. For me, yes, it's a man page. >> I looked at help(os.link) on Python >> 3.4 and the corresponding current library documentation on the web. I >> saw no mention of what happens when dst exists already. >> >> Also, creating a hard link doesn't seem to work between different file >> systems, which may well be relevant to Steve's case. > > In his example above he operates inside a single directory. Can one > directory spread across multiple file systems? Hm, you are right, he does say he's working in a single directory. But *I'm* currently working on processes where results from a batch system are eventually moved to another directory, and I have no control over the file systems. So while it was interesting to learn about os.link, I cannot use os.link here; on the other hand, I can use shutil.move, and in my present case it will only accidentally overwrite a file if I've made a programming mistake myself, or if the underlying platform is not working as advertised, so I'm in a different situation. [- -] From __peter__ at web.de Mon Jan 30 09:49:22 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 15:49:22 +0100 Subject: Display a label while pressing the button in my GUI References: <35e6a5ba-f128-471b-8a28-ed0bc4ad6c18@googlegroups.com> Message-ID: Meeran Rizvi wrote: > On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote: >> Hello Guys, >> Here i am creating a GUI which will act as a search engine that will find >> the results from the browser and save the results as a xls file. When i >> typed something in my search box and click the (GO)button.It should >> display search in progress.when the file is saved it should display done. >> How to do that? My button gets hung for a seconds.We should give any >> timeout for that? >> >> Here is my script >> >> from Tkinter import * >> import mechanize >> def clear_search(event): >> search.delete(0,END) >> obj = Tk() >> Label = Label(obj,text="Top Findings:",font="-weight bold") >> search = Entry(obj,width=100) >> search.insert(0, "Enter the value to search") >> search.bind("", clear_search) >> def fun(): >> new = search.get() >> url = "http://duckduckgo.com/html" >> br = mechanize.Browser() >> br.set_handle_robots(False) >> br.open(url) >> br.select_form(name="x") >> br["q"] = str(new) >> res = br.submit() >> content = res.read() >> with open("result1.xls", "w") as f: >> f.write(content) >> fun() >> Go = Button(obj,text="GO",width=5,command=fun) >> Label.pack() >> search.pack() >> Go.pack() >> mainloop() >> > > It is possible to display the output in my GUI itself? Instead of the strings "searching", "writing result", and "done" you could put search results into the queue and display them in a Text widget. To signal when you're done you can use an out-of-band value, typically None. See also . From hmmeeranrizvi18 at gmail.com Mon Jan 30 09:55:56 2017 From: hmmeeranrizvi18 at gmail.com (Meeran Rizvi) Date: Mon, 30 Jan 2017 06:55:56 -0800 (PST) Subject: Display a label while pressing the button in my GUI In-Reply-To: References: <35e6a5ba-f128-471b-8a28-ed0bc4ad6c18@googlegroups.com> Message-ID: On Monday, January 30, 2017 at 8:19:47 PM UTC+5:30, Peter Otten wrote: > Meeran Rizvi wrote: > > > On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote: > >> Hello Guys, > >> Here i am creating a GUI which will act as a search engine that will find > >> the results from the browser and save the results as a xls file. When i > >> typed something in my search box and click the (GO)button.It should > >> display search in progress.when the file is saved it should display done. > >> How to do that? My button gets hung for a seconds.We should give any > >> timeout for that? > >> > >> Here is my script > >> > >> from Tkinter import * > >> import mechanize > >> def clear_search(event): > >> search.delete(0,END) > >> obj = Tk() > >> Label = Label(obj,text="Top Findings:",font="-weight bold") > >> search = Entry(obj,width=100) > >> search.insert(0, "Enter the value to search") > >> search.bind("", clear_search) > >> def fun(): > >> new = search.get() > >> url = "http://duckduckgo.com/html" > >> br = mechanize.Browser() > >> br.set_handle_robots(False) > >> br.open(url) > >> br.select_form(name="x") > >> br["q"] = str(new) > >> res = br.submit() > >> content = res.read() > >> with open("result1.xls", "w") as f: > >> f.write(content) > >> fun() > >> Go = Button(obj,text="GO",width=5,command=fun) > >> Label.pack() > >> search.pack() > >> Go.pack() > >> mainloop() > >> > > > > It is possible to display the output in my GUI itself? > > Instead of the strings "searching", "writing result", and "done" you could > put search results into the queue and display them in a Text widget. To > signal when you're done you can use an out-of-band value, typically None. > > See also . I just want to save it as a xls file and also from xls file i need to get the data and display in a text box. From hmmeeranrizvi18 at gmail.com Mon Jan 30 09:58:54 2017 From: hmmeeranrizvi18 at gmail.com (Meeran Rizvi) Date: Mon, 30 Jan 2017 06:58:54 -0800 (PST) Subject: Display a label while pressing the button in my GUI In-Reply-To: References: <35e6a5ba-f128-471b-8a28-ed0bc4ad6c18@googlegroups.com> Message-ID: <80d57db0-ac99-4d24-85be-46d8af502bd4@googlegroups.com> On Monday, January 30, 2017 at 8:26:07 PM UTC+5:30, Meeran Rizvi wrote: > On Monday, January 30, 2017 at 8:19:47 PM UTC+5:30, Peter Otten wrote: > > Meeran Rizvi wrote: > > > > > On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote: > > >> Hello Guys, > > >> Here i am creating a GUI which will act as a search engine that will find > > >> the results from the browser and save the results as a xls file. When i > > >> typed something in my search box and click the (GO)button.It should > > >> display search in progress.when the file is saved it should display done. > > >> How to do that? My button gets hung for a seconds.We should give any > > >> timeout for that? > > >> > > >> Here is my script > > >> > > >> from Tkinter import * > > >> import mechanize > > >> def clear_search(event): > > >> search.delete(0,END) > > >> obj = Tk() > > >> Label = Label(obj,text="Top Findings:",font="-weight bold") > > >> search = Entry(obj,width=100) > > >> search.insert(0, "Enter the value to search") > > >> search.bind("", clear_search) > > >> def fun(): > > >> new = search.get() > > >> url = "http://duckduckgo.com/html" > > >> br = mechanize.Browser() > > >> br.set_handle_robots(False) > > >> br.open(url) > > >> br.select_form(name="x") > > >> br["q"] = str(new) > > >> res = br.submit() > > >> content = res.read() > > >> with open("result1.xls", "w") as f: > > >> f.write(content) > > >> fun() > > >> Go = Button(obj,text="GO",width=5,command=fun) > > >> Label.pack() > > >> search.pack() > > >> Go.pack() > > >> mainloop() > > >> > > > > > > It is possible to display the output in my GUI itself? > > > > Instead of the strings "searching", "writing result", and "done" you could > > put search results into the queue and display them in a Text widget. To > > signal when you're done you can use an out-of-band value, typically None. > > > > See also . > > I just want to save it as a xls file and also from xls file i need to get the data and display in a text box. Or either i just display the output in my text widget and also save it as an xls file From tjreedy at udel.edu Mon Jan 30 10:00:09 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 30 Jan 2017 10:00:09 -0500 Subject: Rename file without overwriting existing files In-Reply-To: References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 1/30/2017 8:58 AM, Peter Otten wrote: > Jussi Piitulainen wrote: >> It doesn't seem to be documented. > > For functions with a C equivalent a look into the man page is usually > helpful. Man pages do not exist on Windows. I suspect that there are more individual Python programs on Windows than *nix. I am more sure of this as applied to beginners, most of whom have no idea what a 'man' page is (sexist docs? ;-). -- Terry Jan Reedy From jcasale at activenetwerx.com Mon Jan 30 10:05:27 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 30 Jan 2017 15:05:27 +0000 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <001801d27a7c$6452b140$27b23dae@sambora> Message-ID: > What do you mean by "both platforms"? Python scripts already run on > three major operating systems (Win/Lin/Mac) and a good number of > less-popular OSes; a well-written Python script will run in four major > Pythons (CPython, PyPy, Jython, IronPython) and a number of others; > and all manner of CPU architectures and so on. I meant exactly what you did, in hindsight it was less than perfect wording on my part... Thanks, jlc From __peter__ at web.de Mon Jan 30 10:14:16 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 16:14:16 +0100 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: Chris Angelico wrote: > On Tue, Jan 31, 2017 at 12:58 AM, Peter Otten <__peter__ at web.de> wrote: >>> I looked at help(os.link) on Python >>> 3.4 and the corresponding current library documentation on the web. I >>> saw no mention of what happens when dst exists already. >>> >>> Also, creating a hard link doesn't seem to work between different file >>> systems, which may well be relevant to Steve's case. >> >> In his example above he operates inside a single directory. Can one >> directory spread across multiple file systems? > > Yep. Try unionfs. > > ... make ourselves some scratch space ... > $ mkdir space modifier > $ dd if=/dev/zero of=space.img bs=4096 count=65536 > $ mkfs space.img > $ sudo mount space.img space > $ dd if=/dev/zero of=modifier.img bs=4096 count=1024\ > $ mkfs modifier.img > $ sudo mount modifier.img modifier > > ... put some content into the base directory ... > $ sudo -e space/demo.txt > > ... and now the magic: > $ unionfs modifier=RW:space=RO joiner/ > $ cd joiner > > At this point, you're in a directory that is the union of the two > directories. One of them is read-only, the other is read/write. It is > thus possible to view a file that you can't hard-link to a new name, > because the new name would have to be created in the 'modifier' file > system, but the old file exists on the 'space' one. Interesting example, thanks! From __peter__ at web.de Mon Jan 30 10:25:11 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Jan 2017 16:25:11 +0100 Subject: Display a label while pressing the button in my GUI References: <35e6a5ba-f128-471b-8a28-ed0bc4ad6c18@googlegroups.com> <80d57db0-ac99-4d24-85be-46d8af502bd4@googlegroups.com> Message-ID: Meeran Rizvi wrote: > I just want to save it as a xls file and also from xls file i need to get > the data and display in a text box. > > Or either i just display the output in my text widget and also save it as > an xls file What exactly you want to do is up to you; just try your hands at it and come back here when you run into problems. Keep in mind that whatever you do in the GUI thread you should only spend a small amount of time on it at once. If the task is big break it into parts, e. g. instead of reading a complete web page or file break it into smaller blocks that you insert into the widget one after another. As to the XLS file, I recommend that you first try the conversion from HTML to XLS in a small separate script that reads HTML from a file on your computer. Once it works is should be easy to integrate a working conversion function into a working Search GUI. From jon+usenet at unequivocal.eu Mon Jan 30 10:40:05 2017 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Mon, 30 Jan 2017 15:40:05 -0000 (UTC) Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2017-01-30, Peter Otten <__peter__ at web.de> wrote: > Jon Ribbens wrote: >> On 2017-01-30, Peter Otten <__peter__ at web.de> wrote: >>> However, the current Python version of link() is sufficiently different >>> from >>>, say, to warrant its own documentation. >> >> What are you referring to here? As far as I can see, the current >> Python implementation of link() just calls the underlying OS call >> directly. > > The current signature differs from that of link() > > os.link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True) > > but it looks like you are right in so far as link() is still called by > default: Yeah, it's been extended to call linkat() if the extra parameters are provided, but it still calls link() if you call it like link(). So basically it's been extended analogously to how Linux has been, and the OS manpage is still the right place to look to understand what it does. (linkat() is documented as part of the same manpage as link() anyway.) From grant.b.edwards at gmail.com Mon Jan 30 10:48:16 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Mon, 30 Jan 2017 15:48:16 +0000 (UTC) Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2017-01-30, Jussi Piitulainen wrote: > It doesn't seem to be documented. I looked at help(os.link) on Python > 3.4 and the corresponding current library documentation on the web. I > saw no mention of what happens when dst exists already. The functions in the os module are thin-as-possible wrappers around the OS's libc functions. The authors of the os module don't really have any way of knowing the details of what your os/libc combination is going to do. If you're calling os.foo(), you're sort of expected to know what foo() does on your OS. > Also, creating a hard link doesn't seem to work between different file > systems, which may well be relevant to Steve's case. I get: > > OSError: [Errno 18] Invalid cross-device link: [snip] > > And that also is not mentioned in the docs. Again, that's a detail that depends on your particular OS/libc/filesystem implementation. It's not determined by nor knowable by the authors of the os module. -- Grant Edwards grant.b.edwards Yow! I'm ANN LANDERS!! at I can SHOPLIFT!! gmail.com From saxri89 at gmail.com Mon Jan 30 10:55:39 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Mon, 30 Jan 2017 07:55:39 -0800 (PST) Subject: count points where is within the polygons using shapely and fiona In-Reply-To: References: Message-ID: tell your proposal to count From grant.b.edwards at gmail.com Mon Jan 30 10:56:01 2017 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Mon, 30 Jan 2017 15:56:01 +0000 (UTC) Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2017-01-30, Terry Reedy wrote: > On 1/30/2017 8:58 AM, Peter Otten wrote: >> Jussi Piitulainen wrote: > >>> It doesn't seem to be documented. >> >> For functions with a C equivalent a look into the man page is usually >> helpful. > > Man pages do not exist on Windows. I suspect that there are more > individual Python programs on Windows than *nix. I am more sure of this > as applied to beginners, most of whom have no idea what a 'man' page is > (sexist docs? ;-). IMO, beginners shouldn't be using the os module. This is implied by the first parapgraph of the doc: If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module. When you're using the OS module, you're just writing C code in Python. [Which, BTW, is a _very_ useful thing to be able to do, but it's probably not what beginners are trying to do.] I always found the first sentence to be a bit funny: This module provides a portable way of using operating system dependent functionality. I understand whay they're tying to say, but I always found it amusing to say you're going to provide a portable way to do something non-portable. -- Grant Edwards grant.b.edwards Yow! Somewhere in DOWNTOWN at BURBANK a prostitute is gmail.com OVERCOOKING a LAMB CHOP!! From ian.g.kelly at gmail.com Mon Jan 30 11:05:37 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 30 Jan 2017 09:05:37 -0700 Subject: Overriding True and False ? In-Reply-To: References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> Message-ID: On Jan 30, 2017 2:00 AM, "Chris Angelico" wrote: (Interestingly, Ellipsis is not included in that.) Perhaps because it's rather unusual for a program to depend upon the value of Ellipsis. From marko at pacujo.net Mon Jan 30 11:11:50 2017 From: marko at pacujo.net (Marko Rauhamaa) Date: Mon, 30 Jan 2017 18:11:50 +0200 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87sho08r3t.fsf@elektro.pacujo.net> Grant Edwards : > IMO, beginners shouldn't be using the os module. Hard to know. Depends on what the beginner wants to accomplish. > I always found the first sentence to be a bit funny: > > This module provides a portable way of using operating system > dependent functionality. > > I understand whay they're tying to say, but I always found it amusing > to say you're going to provide a portable way to do something > non-portable. One of the best things in Python is that it has exposed the operating system to the application programmer. I don't think a programming language should abstract the operating system away. Marko From ian.g.kelly at gmail.com Mon Jan 30 11:12:50 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 30 Jan 2017 09:12:50 -0700 Subject: Overriding True and False ? In-Reply-To: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> Message-ID: On Jan 30, 2017 1:32 AM, "Irv Kalb" wrote: I teach intro to programming using Python. In my first assignment, students are asked to assign variables of different types and print out the values. One student (who really did not understand Booleans) turned in the following for his/her interpretation of Booleans (Python 2.7): True = 'shadow' False = 'light' print "If the sun is behind a cloud, there is", True print "If it is a clear day, there is", False And it printed: If the sun is behind a cloud, there is shadow If it is a clear day, there is light It seems very odd that Python allows you to override the values of True and False. In the code, True and False were clearly recognized as keywords as they were colored purple. But there was no error message. Plenty of people have remarked that this is fixed in Python 3, but nobody that I see has explained yet the reason for this behavior in the first place. True and False are not keywords in Python 2 for backward compatibility. Many versions ago, Python did not define True and False at all. It was common in that era to lead scripts with: False = 0 True = 1 To define useful boolean constants. When the built-in constants were added, the developers did not want to break programs that followed this pattern. This is also the reason why bool is a subclass of int -- so that False == 0 and True == 1 remain true for programs that depend on this. From cl at isbd.net Mon Jan 30 12:58:30 2017 From: cl at isbd.net (Chris Green) Date: Mon, 30 Jan 2017 17:58:30 +0000 Subject: What library/package to use for parsing XML? Message-ID: <6fb4md-kns.ln1@esprimo.zbmc.eu> I want to parse some XML data, it's the address book data from the linux program osmo. The file I want to parse is like this:- None Peter and Denise Smith 0 0 Some address AAA BBB Oxfordshire I basically want to be able to extract the data and output in other formats - e.g. write to a Sqlite3 database and/or CSV. So what do I need to import (and probably install first) to do this? -- Chris Green ? From juan0christian at gmail.com Mon Jan 30 13:44:15 2017 From: juan0christian at gmail.com (Juan C.) Date: Mon, 30 Jan 2017 16:44:15 -0200 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: Message-ID: On Sun, Jan 29, 2017 at 1:06 AM, Juan C. wrote: > > As you guys might know, .NET Core is up and running, promising a "cross-platform, unified, fast, lightweight, modern and open source experience" (source: .NET Core official site). What do you guys think about it? Do you think it will be able to compete with and overcome Python in the opensource medium? Oh, I forgot to say, I was indeed talking about .NET Core in general, the framework, but I also had C#/ASP .NET Core in mind. The real comparison here would be C#/ASP .NET Core vs Python/Django,Flask. I personally had to work with C# a few times, it's a great language, but my main problem was that it was Windows only and most of my personal programs run on Linux. Yes, we have Mono, but it really didn't feel "right" to me, it seemed like a workaround, I wanted something really "native". Python still has my heart, but .NET Core tempts me. One great thing of coding in C# would be no GIL. From jcasale at activenetwerx.com Mon Jan 30 15:33:42 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 30 Jan 2017 20:33:42 +0000 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: Message-ID: <5eed732b8ec64871a33e7b8fea23c002@activenetwerx.com> > Python still has my heart, but .NET Core tempts me. One great thing of > coding in C# would be no GIL. Seriously, check out the benchmarks at https://github.com/aspnet/benchmarks. I think aside from the obvious, you'll find the Razor engine and the overall library to be a pleasure to work with. The framework is truly a representation of what a high quality, brilliantly engineered piece of software looks like. From irmen.NOSPAM at xs4all.nl Mon Jan 30 16:12:18 2017 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Mon, 30 Jan 2017 22:12:18 +0100 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <5eed732b8ec64871a33e7b8fea23c002@activenetwerx.com> Message-ID: <588fac31$0$786$e4fe514c@news.xs4all.nl> On 30-1-2017 21:33, Joseph L. Casale wrote: >> Python still has my heart, but .NET Core tempts me. One great thing of >> coding in C# would be no GIL. > > Seriously, check out the benchmarks at https://github.com/aspnet/benchmarks. Results vary quite a bit there. For instance take the json serialization benchmark: https://www.techempower.com/benchmarks/#section=data-r13&hw=ph&test=json There's 6 python frameworks performing better than aspcore in this chart... Irmen From irmen.NOSPAM at xs4all.nl Mon Jan 30 16:20:08 2017 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Mon, 30 Jan 2017 22:20:08 +0100 Subject: What library/package to use for parsing XML? In-Reply-To: <6fb4md-kns.ln1@esprimo.zbmc.eu> References: <6fb4md-kns.ln1@esprimo.zbmc.eu> Message-ID: <588fae07$0$786$e4fe514c@news.xs4all.nl> On 30-1-2017 18:58, Chris Green wrote: > I want to parse some XML data, it's the address book data from the > linux program osmo. The file I want to parse is like this:- > [snip] > > I basically want to be able to extract the data and output in other > formats - e.g. write to a Sqlite3 database and/or CSV. > > So what do I need to import (and probably install first) to do this? No need to install anything. Python has batteries included. Parse your XML with xml.etree.ElementTree [https://docs.python.org/3/library/xml.etree.elementtree.html] Read/write CSV with csv [https://docs.python.org/3/library/csv.html] Put stuff in sqlite3 database and query it using sqlite3 [https://docs.python.org/3/library/sqlite3.html] -irmen From juan0christian at gmail.com Mon Jan 30 17:14:54 2017 From: juan0christian at gmail.com (Juan C.) Date: Mon, 30 Jan 2017 20:14:54 -0200 Subject: What library/package to use for parsing XML? In-Reply-To: <6fb4md-kns.ln1@esprimo.zbmc.eu> References: <6fb4md-kns.ln1@esprimo.zbmc.eu> Message-ID: On Mon, Jan 30, 2017 at 3:58 PM, Chris Green wrote: > I want to parse some XML data, it's the address book data from the > linux program osmo. The file I want to parse is like this:- Just like Irmen said, use the default xml.etree.ElementTree, it's amazing and very simple to use. From rgaddi at highlandtechnology.invalid Mon Jan 30 18:05:16 2017 From: rgaddi at highlandtechnology.invalid (Rob Gaddi) Date: Mon, 30 Jan 2017 15:05:16 -0800 Subject: What library/package to use for parsing XML? In-Reply-To: References: <6fb4md-kns.ln1@esprimo.zbmc.eu> Message-ID: On 01/30/2017 02:14 PM, Juan C. wrote: > On Mon, Jan 30, 2017 at 3:58 PM, Chris Green wrote: >> I want to parse some XML data, it's the address book data from the >> linux program osmo. The file I want to parse is like this:- > > Just like Irmen said, use the default xml.etree.ElementTree, it's > amazing and very simple to use. > And if you get halfway into your project and find ElementTree was insufficient, you can switch to lxml without changing practically anything you've already written. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. From jf_byrnes at comcast.net Mon Jan 30 18:16:24 2017 From: jf_byrnes at comcast.net (Jim) Date: Mon, 30 Jan 2017 17:16:24 -0600 Subject: How to write libreoffice python macros in venv? Message-ID: I have all the necessary libreoffice modules installed in my Mint 18.1 system to allow me to write libreoffice calc macros in python. I now have venv installed. If I try to import uno for a calc macro in it I get an error that there is no uno module. How can I get my venv to find uno and the other necessary modules so I can write calc macros in it? Thanks, Jim From steve+python at pearwood.info Mon Jan 30 19:02:06 2017 From: steve+python at pearwood.info (Steve D'Aprano) Date: Tue, 31 Jan 2017 11:02:06 +1100 Subject: Overriding True and False ? References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> Message-ID: <588fd400$0$1583$c3e8da3$5496439d@news.astraweb.com> On Tue, 31 Jan 2017 03:12 am, Ian Kelly wrote: > On Jan 30, 2017 1:32 AM, "Irv Kalb" wrote: > > I teach intro to programming using Python. In my first assignment, > students are asked to assign variables of different types and print out > the values. [...] Hey Ian, Your news reader or mail client has stopped quoting the text you are quoting, so it appears as if you have written it. See: https://mail.python.org/pipermail/python-list/2017-January/719015.html -- Steve ?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure enough, things got worse. From ben+python at benfinney.id.au Mon Jan 30 19:17:56 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 31 Jan 2017 11:17:56 +1100 Subject: Rename file without overwriting existing files References: <588ea9c8$0$1605$c3e8da3$5496439d@news.astraweb.com> <20170130043303.GA78047@cskk.homeip.net> <588f127e$0$1586$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8537g0t74b.fsf@benfinney.id.au> Peter Otten <__peter__ at web.de> writes: > http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions > > and from a quick test it appears to work on Linux: By ?works on Linux?, I assume you mean ?works on filesystems that use inodes and hard links?. That is not true for all filesystems, even on Linux. -- \ ?[Entrenched media corporations will] maintain the status quo, | `\ or die trying. Either is better than actually WORKING for a | _o__) living.? ?ringsnake.livejournal.com, 2007-11-12 | Ben Finney From torriem at gmail.com Mon Jan 30 19:35:05 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 30 Jan 2017 17:35:05 -0700 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: Message-ID: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> On 01/30/2017 11:44 AM, Juan C. wrote: > On Sun, Jan 29, 2017 at 1:06 AM, Juan C. wrote: >> >> As you guys might know, .NET Core is up and running, promising a "cross-platform, unified, fast, lightweight, modern and open source experience" (source: .NET Core official site). What do you guys think about it? Do you think it will be able to compete with and overcome Python in the opensource medium? > > Oh, I forgot to say, I was indeed talking about .NET Core in general, > the framework, but I also had C#/ASP .NET Core in mind. The real > comparison here would be C#/ASP .NET Core vs Python/Django,Flask. I > personally had to work with C# a few times, it's a great language, but > my main problem was that it was Windows only and most of my personal > programs run on Linux. Yes, we have Mono, but it really didn't feel > "right" to me, it seemed like a workaround, I wanted something really > "native". C# under Mono is as native as C# on the DotNet runtime on Windows or DotNet core on Linux. However now that DotNet Core is open source and supported on Linux, Mono is likely to fade away since all of the Xamarin developers work for MS now. > Python still has my heart, but .NET Core tempts me. One great thing of > coding in C# would be no GIL. C# hardly seems any better than Java to me as far as a language goes. Being forced into working with classes even when they are not appropriate is jarring. Because everything is forced into a class, one often ends up with very long classes in C#, spanning more than one file! Makes the code much harder to follow from a human point of view. After working in C# I very much appreciate Python's explicit self requirement for accessing local instance variables. From gerald.britton at gmail.com Mon Jan 30 20:06:41 2017 From: gerald.britton at gmail.com (Gerald Britton) Date: Mon, 30 Jan 2017 20:06:41 -0500 Subject: What are your opinions on .NET Core vs Python? Message-ID: On Sun, Jan 29, 2017 at 1:06 AM, Juan C. > wrote: > > > > >* As you guys might know, .NET Core is up and running, promising a "cross-platform, unified, fast, lightweight, modern and open source experience" (source: .NET Core official site). What do you guys think about it? Do you think it will be able to compete with and overcome Python in the opensource medium? > * Oh, I forgot to say, I was indeed talking about .NET Core in general, > the framework, but I also had C#/ASP .NET Core in mind. The real > comparison here would be C#/ASP .NET Core vs Python/Django,Flask. I > personally had to work with C# a few times, it's a great language, but > my main problem was that it was Windows only and most of my personal > programs run on Linux. Yes, we have Mono, but it really didn't feel > "right" to me, it seemed like a workaround, I wanted something really > "native". Python still has my heart, but .NET Core tempts me. One great thing of > coding in C# would be no GIL. For what it's worth, IronPython, which is implemented in .NET, has no GIL and doesn't need it since ir runs on the CLR. That means that, for some things, IronPython can be more performant. No word yet if the IronPython project intends to port to .NET core or enable to run it on OS's other than Windows. Also, it's worth noting that the PythonNet project has a branch working on .NET core support -- Gerald Britton, MCSE-DP, MVP LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton From jcasale at activenetwerx.com Mon Jan 30 20:18:14 2017 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Tue, 31 Jan 2017 01:18:14 +0000 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> Message-ID: <21c7475509634730811331fe2804cc0f@activenetwerx.com> > C# hardly seems any better than Java to me as far as a language goes. Which sounds pretty good to me, they are both high performance, mature and rich languages. > Being forced into working with classes even when they are not > appropriate is jarring. And 100% irrelevant, it doesn't prevent you from doing anything you otherwise could without. > Because everything is forced into a class, one > often ends up with very long classes in C#, spanning more than one file! Sorry, sounds like you need to learn SOLID, none of my classes have ever taken this form. > Makes the code much harder to follow from a human point of view. After > working in C# I very much appreciate Python's explicit self requirement > for accessing local instance variables. So, prefix them with "this." and they will look the same? From nathan.ernst at gmail.com Mon Jan 30 20:52:06 2017 From: nathan.ernst at gmail.com (Nathan Ernst) Date: Mon, 30 Jan 2017 19:52:06 -0600 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: <21c7475509634730811331fe2804cc0f@activenetwerx.com> References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: I mostly agree with this On Mon, Jan 30, 2017 at 7:18 PM, Joseph L. Casale wrote: > > C# hardly seems any better than Java to me as far as a language goes. > > Which sounds pretty good to me, they are both high performance, mature > and rich languages. > > > Being forced into working with classes even when they are not > > appropriate is jarring. > > And 100% irrelevant, it doesn't prevent you from doing anything you > otherwise could without. > > > Because everything is forced into a class, one > > often ends up with very long classes in C#, spanning more than one file! > > Sorry, sounds like you need to learn SOLID, none of my classes > have ever taken this form. > There is no reason you cannot introduce a static class with pure static members (i.e. the Math class in System). A static class effectively becomes another namespace in C++ parlance. I'll admit the syntax is a bit odd, and enforces you, at a minimum to use the outer name a as a qualifier, but it's effectively the same effect. > > > Makes the code much harder to follow from a human point of view. After > > working in C# I very much appreciate Python's explicit self requirement > > for accessing local instance variables. > > So, prefix them with "this." and they will look the same? > -- > https://mail.python.org/mailman/listinfo/python-list > self vs this, and you might start a language holy war. Of course "self" is the preferred to refer to an instance, just by python convention. Whether I agree with or not, I've seen "this" used to refer to instances where it is ambiguous in Python parlance between a "self" instance or a class instance - those instances are a bit weird, but there are rare occasions you do want to override class variables where this makes sense (they should be rare - because it's generally very hard to follow). From torriem at gmail.com Mon Jan 30 20:57:18 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 30 Jan 2017 18:57:18 -0700 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: <21c7475509634730811331fe2804cc0f@activenetwerx.com> References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: On 01/30/2017 06:18 PM, Joseph L. Casale wrote: > Which sounds pretty good to me, they are both high performance, mature > and rich languages. Sure it's a matter of personal preference and need. I happen to find the expressivity and flexibility of Python (warts and all) to be rather liberating compared to Java. I find Java rather stifling particularly because of the forced OOP structure. Not really sure what you mean by "rich language." Java has a rich runtime library and a rich ecosystem of utility libraries to draw on. So does C#. And so does Python. > Sorry, sounds like you need to learn SOLID, none of my classes > have ever taken this form. Never said they were my classes, or even my programs. What is this "SOLID" thing? In any case, partial classes seems like a misfeature of C# to me but apparently they are used when making Windows Forms GUI-based apps. Apparently VS autogenerates the class that backs the form, and then the developer creates a partial class that extends that to flesh out the logic and handle the events. I'm surprised you haven't encountered this. >> Makes the code much harder to follow from a human point of view. After >> working in C# I very much appreciate Python's explicit self requirement >> for accessing local instance variables. > > So, prefix them with "this." and they will look the same? Probably. But C# developers never do that (nor is it normal to do that in C++ either). Not sure what your point is there. I was merely saying that I now appreciate the logic of the Python explicit self. From rosuav at gmail.com Mon Jan 30 21:02:12 2017 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 31 Jan 2017 13:02:12 +1100 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: On Tue, Jan 31, 2017 at 12:57 PM, Michael Torrie wrote: >> Sorry, sounds like you need to learn SOLID, none of my classes >> have ever taken this form. > > Never said they were my classes, or even my programs. What is this > "SOLID" thing? https://en.wikipedia.org/wiki/SOLID_(object-oriented_design) Five well-respected principles that every class designer should be aware of. You won't necessarily follow them all slavishly, but know what the rules are and why you're breaking them. ChrisA From torriem at gmail.com Mon Jan 30 21:04:23 2017 From: torriem at gmail.com (Michael Torrie) Date: Mon, 30 Jan 2017 19:04:23 -0700 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: On 01/30/2017 06:52 PM, Nathan Ernst wrote: > self vs this, and you might start a language holy war. Actually no, you misread his point. He was speaking of C#, not Python. In C#, the only word you can use is "this." He was saying that you can use the explicit self paradigm in C#. Simply prefix each member variable with "this." From ian.g.kelly at gmail.com Mon Jan 30 22:27:41 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 30 Jan 2017 20:27:41 -0700 Subject: Overriding True and False ? In-Reply-To: <588fd400$0$1583$c3e8da3$5496439d@news.astraweb.com> References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> <588fd400$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Jan 30, 2017 6:07 PM, "Steve D'Aprano" wrote: > Hey Ian, > > Your news reader or mail client has stopped quoting the text you are > quoting, so it appears as if you have written it. > > See: > > https://mail.python.org/pipermail/python-list/2017-January/719015.html Well, nuts. It looks fine in my client (the Gmail Android app) so I guess this must be an issue of HTML versus text content. Unfortunately the mobile client doesn't have any option for text-only that I can find, so my options appear to be to laboriously replace the quoting as I've done here, or just stop using the mobile client. From ben+python at benfinney.id.au Mon Jan 30 23:57:04 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 31 Jan 2017 15:57:04 +1100 Subject: Overriding True and False ? References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> <588fd400$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: <85poj3su73.fsf@benfinney.id.au> Ian Kelly writes: > Well, nuts. It looks fine in my client (the Gmail Android app) so I > guess this must be an issue of HTML versus text content. Unfortunately > the mobile client doesn't have any option for text-only that I can > find, so my options appear to be to laboriously replace the quoting as > I've done here, or just stop using the mobile client. For composing discussion posts, yes, I recommend people stop using mobile clients. It can wait until you're at a real keyboard and a decent authoring environment. -- \ ?Very few things happen at the right time, and the rest do not | `\ happen at all. The conscientious historian will correct these | _o__) defects.? ?Mark Twain, _A Horse's Tale_ | Ben Finney From greg.ewing at canterbury.ac.nz Tue Jan 31 00:26:45 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 31 Jan 2017 18:26:45 +1300 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: Nathan Ernst wrote: > There is no reason you cannot introduce a static class with pure static > members (i.e. the Math class in System). A static class effectively becomes > another namespace in C++ parlance. I'll admit the syntax is a bit odd, and > enforces you, at a minimum to use the outer name a as a qualifier, That's the thing I find most frustrating about both C# and Java, the lack of anything like Python's "from ... import ...". You get a choice of either effectively doing "import *" on the contents of a whole class and polluting your namespace, or Fully.Qualifying. Every.Little.Thing. -- Greg From greg.ewing at canterbury.ac.nz Tue Jan 31 00:31:24 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 31 Jan 2017 18:31:24 +1300 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: Michael Torrie wrote: > He was saying that you can > use the explicit self paradigm in C#. Simply prefix each member variable > with "this." One can do that in one's own code, but it doesn't help you to read the code of someone else who hasn't done that. Since it's not part of the C# culture, the vast majority of other C# code you encounter won't be using it. -- Greg From auriocus at gmx.de Tue Jan 31 02:21:01 2017 From: auriocus at gmx.de (Christian Gollwitzer) Date: Tue, 31 Jan 2017 08:21:01 +0100 Subject: Overriding True and False ? In-Reply-To: References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> <588fd400$0$1583$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 31.01.17 um 04:27 schrieb Ian Kelly: > On Jan 30, 2017 6:07 PM, "Steve D'Aprano" > wrote: >> Hey Ian, >> >> Your news reader or mail client has stopped quoting the text you are >> quoting, so it appears as if you have written it. >> >> See: >> >> https://mail.python.org/pipermail/python-list/2017-January/719015.html > > Well, nuts. It looks fine in my client (the Gmail Android app) so I guess > this must be an issue of HTML versus text content. Unfortunately the mobile > client doesn't have any option for text-only that I can find, so my options > appear to be to laboriously replace the quoting as I've done here, or just > stop using the mobile client. This one looks fine, FYI. Christian From bob.martin at excite.com Tue Jan 31 02:30:49 2017 From: bob.martin at excite.com (Bob Martin) Date: Tue, 31 Jan 2017 07:30:49 GMT Subject: What are your opinions on .NET Core vs Python? References: Message-ID: in 770457 20170131 011814 "Joseph L. Casale" wrote: >> C# hardly seems any better than Java to me as far as a language goes. > >Which sounds pretty good to me, they are both high performance, mature >and rich languages. > >> Being forced into working with classes even when they are not >> appropriate is jarring. > >And 100% irrelevant, it doesn't prevent you from doing anything you >otherwise could without. > >> Because everything is forced into a class, one >> often ends up with very long classes in C#, spanning more than one file! > >Sorry, sounds like you need to learn SOLID, none of my classes >have ever taken this form. > >> Makes the code much harder to follow from a human point of view. After >> working in C# I very much appreciate Python's explicit self requirement >> for accessing local instance variables. > >So, prefix them with "this." and they will look the same? Please include the identity of people you are quoting. From cl at isbd.net Tue Jan 31 03:52:22 2017 From: cl at isbd.net (Chris Green) Date: Tue, 31 Jan 2017 08:52:22 +0000 Subject: What library/package to use for parsing XML? References: <6fb4md-kns.ln1@esprimo.zbmc.eu> <588fae07$0$786$e4fe514c@news.xs4all.nl> Message-ID: <6rv5md-nba.ln1@esprimo.zbmc.eu> Irmen de Jong wrote: > On 30-1-2017 18:58, Chris Green wrote: > > I want to parse some XML data, it's the address book data from the > > linux program osmo. The file I want to parse is like this:- > > > > [snip] > > > > > I basically want to be able to extract the data and output in other > > formats - e.g. write to a Sqlite3 database and/or CSV. > > > > So what do I need to import (and probably install first) to do this? > > No need to install anything. Python has batteries included. > > Parse your XML with xml.etree.ElementTree > [https://docs.python.org/3/library/xml.etree.elementtree.html] > Read/write CSV with csv > [https://docs.python.org/3/library/csv.html] > Put stuff in sqlite3 database and query it using sqlite3 > [https://docs.python.org/3/library/sqlite3.html] > Thanks, it was the ElementTree bit I didn't know. Searching for 'Python XML' produces so many hits it's difficult to see what's necessary/best. -- Chris Green ? From paulwolf333 at gmail.com Tue Jan 31 05:33:31 2017 From: paulwolf333 at gmail.com (Paul Wolf) Date: Tue, 31 Jan 2017 02:33:31 -0800 (PST) Subject: command line micro wiki written in Python Message-ID: I've created a command line utility for managing text files. It's written in Python: https://github.com/paul-wolf/yewdoc-client It makes heavy use of the fantastic Click module by Armin Ronacher: http://click.pocoo.org/5/ This can be thought of in different ways: * A micro-wiki * A note-taking application * A file manager The key aspects are * Entirely command-line driven * Text documents only with a slight preference for Markdown * Make it easy to operate on documents without having to remember where they are or the exact names * Editor agnostic (vim, emacs, Sublime, Atom, etc.) Here's how to create a document: yd edit "shopping list" After editing, saving, closing, you can find again: ? yd ls -l shop 15981278 md 61 2017-01-20 12:15:43 shopping list While some people might gasp at the idea of a command line wiki, I find using the command line with a text editor like emacs the best workflow. I also like not having to worry about where a file of this kind is located. You can also use it to track configuration files: yd take ~/.emacs --symlink Now, I can edit this. Because it's a link to the actual file - having used the `--symlink` option, the configuration file will be updated: ? yd ls -l emacs 1c608cd7 md 113 2016-12-16 10:53:24 emacs ln 183a5b80 txt 5608 2017-01-15 12:59:39 /Users/paul/.emacs Using the cloud sync options lets me get my current config file wherever I am, completely up-to-date. For a more wiki-like experience, I can load all the documents with a common tag so: yd browse wolf This converts all the documents tagged with 'wolf' and loads them in a browser with a simple navigation sidebar. Convert a Markdown document called "Python3" to .odt: ? yd convert Python3 odt python3.odt There is an optional cloud syncronisation feature that connects to a specific endpoint, https://doc.yew.io: ? yd sync This requires registration at https://doc.yew.io, which can also be done via command line. But that is entirely optional. I'd be interested in feedback if there is any interest in using this kind of utility. I'll expose a python API so bulk operations can be done easily on the command line or via scripts. From daiyueweng at gmail.com Tue Jan 31 06:28:45 2017 From: daiyueweng at gmail.com (Daiyue Weng) Date: Tue, 31 Jan 2017 11:28:45 +0000 Subject: update a list element using an element in another list Message-ID: Hi, I am trying to update a list of dictionaries using another list of dictionaries correspondingly. In that, for example, # the list of dicts that need to be updated dicts_1 = [{'dict_1': '1'}, {'dict_2': '2'}, {'dict_3': '3'}] # dict used to update dicts_1 update_dicts = [{'dict_1': '1_1'}, {'dict_2': '1_2'}, {'dict_3': '1_3'}] so that after updating, dicts_1 = [{'dict_1': '1_1'}, {'dict_2': '1_2'}, {'dict_3': '1_3'}] what's the best way to the updates? This is actually coming from when I tried to create a list of entities (dictionaries), then updating the entities using another list dictionaries using google.cloud.datastore. entities = [Entity(self.client.key(kind, entity_id)) for entity_id in entity_ids] # update entities using update_dicts for j in range(len(entities)): for i in range(len(update_dicts)): if j == i: entities[j].update(update_dicts[i]) I am wondering is there a brief way to do this. cheers From rhodri at kynesim.co.uk Tue Jan 31 06:49:20 2017 From: rhodri at kynesim.co.uk (Rhodri James) Date: Tue, 31 Jan 2017 11:49:20 +0000 Subject: update a list element using an element in another list In-Reply-To: References: Message-ID: <5f0872b9-41a2-14b3-ef7a-cd37d0877c26@kynesim.co.uk> On 31/01/17 11:28, Daiyue Weng wrote: > Hi, I am trying to update a list of dictionaries using another list of > dictionaries correspondingly. In that, for example, > > # the list of dicts that need to be updated > dicts_1 = [{'dict_1': '1'}, {'dict_2': '2'}, {'dict_3': '3'}] > > # dict used to update dicts_1 > update_dicts = [{'dict_1': '1_1'}, {'dict_2': '1_2'}, {'dict_3': '1_3'}] > > so that after updating, > > dicts_1 = [{'dict_1': '1_1'}, {'dict_2': '1_2'}, {'dict_3': '1_3'}] > > what's the best way to the updates? > > This is actually coming from when I tried to create a list of entities > (dictionaries), then updating the entities using another list dictionaries > using google.cloud.datastore. > > entities = [Entity(self.client.key(kind, entity_id)) for entity_id in > entity_ids] > > # update entities using update_dicts > for j in range(len(entities)): > for i in range(len(update_dicts)): > if j == i: > entities[j].update(update_dicts[i]) > > I am wondering is there a brief way to do this. This all relies on the lists being in the same order and the same length, which is probably an unwise assumption, but it's what your code does: for entity, update_dict in zip(entities, update_dicts): entity.update(update_dict) range(len(something)) is usually a warning sign (code smell, if you prefer) that you aren't thinking in Python. If you really need the list index for some nefarious purpose, enumerate(something) is probably still a better bet. -- Rhodri James *-* Kynesim Ltd From jussi.piitulainen at helsinki.fi Tue Jan 31 06:53:13 2017 From: jussi.piitulainen at helsinki.fi (Jussi Piitulainen) Date: Tue, 31 Jan 2017 13:53:13 +0200 Subject: update a list element using an element in another list References: Message-ID: Daiyue Weng writes: > Hi, I am trying to update a list of dictionaries using another list of > dictionaries correspondingly. In that, for example, > > # the list of dicts that need to be updated > dicts_1 = [{'dict_1': '1'}, {'dict_2': '2'}, {'dict_3': '3'}] > > # dict used to update dicts_1 > update_dicts = [{'dict_1': '1_1'}, {'dict_2': '1_2'}, {'dict_3': '1_3'}] > > so that after updating, > > dicts_1 = [{'dict_1': '1_1'}, {'dict_2': '1_2'}, {'dict_3': '1_3'}] > > what's the best way to the updates? > > This is actually coming from when I tried to create a list of entities > (dictionaries), then updating the entities using another list > dictionaries using google.cloud.datastore. > > entities = [Entity(self.client.key(kind, entity_id)) for entity_id in > entity_ids] > > # update entities using update_dicts > for j in range(len(entities)): > for i in range(len(update_dicts)): > if j == i: > entities[j].update(update_dicts[i]) [I restored the indentation.] > I am wondering is there a brief way to do this. A straightforward algorithmic improvement: for j in range(len(entities)): entities[j].update(update_dicts[j]) The real thing: for e, u in zip(entities, update_dicts): e.update(u) A thing between those: for j, e in enumerate(entities): e.update(update_dicts[j]) (By symmetry, you could enumerate update_dicts instead.) It pays to learn zip and enumerate. From ethan at stoneleaf.us Tue Jan 31 11:15:58 2017 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 31 Jan 2017 08:15:58 -0800 Subject: command line micro wiki written in Python In-Reply-To: References: Message-ID: <5890B83E.602@stoneleaf.us> On 01/31/2017 02:33 AM, Paul Wolf wrote: > I've created a command line utility for managing text files. It's written in Python... That sounds really cool! -- ~Ethan~ From torriem at gmail.com Tue Jan 31 11:18:21 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 31 Jan 2017 09:18:21 -0700 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: <37e5755b-3dcb-b0cf-2435-a39e02a0b5de@gmail.com> On 01/30/2017 10:31 PM, Gregory Ewing wrote: > Michael Torrie wrote: >> He was saying that you can >> use the explicit self paradigm in C#. Simply prefix each member variable >> with "this." > > One can do that in one's own code, but it doesn't help > you to read the code of someone else who hasn't done > that. Since it's not part of the C# culture, the vast > majority of other C# code you encounter won't be using > it. Agreed, and that's what I said in response to his suggestion about doing that. From fabiofz at gmail.com Tue Jan 31 12:17:42 2017 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Tue, 31 Jan 2017 15:17:42 -0200 Subject: PyDev 5.5.0 Released Message-ID: PyDev 5.5.0 Release Highlights ------------------------------- * **Important** PyDev now requires Java 8 and Eclipse 4.6 (Neon) onwards. * PyDev 5.2.0 is the last release supporting Eclipse 4.5 (Mars). * If you enjoy PyDev, you can help in keeping it supported through its Patreon crowdfunding: https://www.patreon.com/fabioz. * **Refactoring** * Fixed refactoring error when dealing with imports which have a continuation char inside the module name part. **#PyDev-712** * When extracting a method, decorators are properly considered for the new method position. **#PyDev-321** * **Code completion** * When accessing enums, 'value' and 'name' are properly found. **#PyDev-591** * Code completion improved on method chaining. **#PyDev-636** and **#PyDev-583** * It's now possible to choose whether when a code-completion which adds a local import should add the import to the beginning of the function or the line above where it was requested. * It may be configured in the preferences (Preferences > PyDev > Editor > Code Completion > Put local imports on top of method?). * Default was changed to add it to the top of the method. * **New actions** * **Ctrl+Shift+Alt+O** can be used to open the last hyperlink in the console that's currently open (it's now possible to jump directly to the error in some exception). **#PyDev-755** * **Ctrl+2,sw** switches the target and value in assign statements (may not work properly if more than one '=' is found in the line). * **Debugger** * Fixed error when hovering over variable when debugging. **#PyDev-580** * **Others** * Fixed issue in grammar parsing on nested async calls. **#PyDev-753** * Fixed issue grouping imports when an import has a continuation char inside the module part. **#PyDev 712** What is PyDev? --------------------------- PyDev is an open-source Python IDE on top of Eclipse for Python, Jython and IronPython development. It comes with goodies such as code completion, syntax highlighting, syntax analysis, code analysis, refactor, debug, interactive console, etc. Details on PyDev: http://pydev.org Details on its development: http://pydev.blogspot.com What is LiClipse? --------------------------- LiClipse is a PyDev standalone with goodies such as support for Multiple cursors, theming, TextMate bundles and a number of other languages such as Django Templates, Jinja2, Kivy Language, Mako Templates, Html, Javascript, etc. It's also a commercial counterpart which helps supporting the development of PyDev. Details on LiClipse: http://www.liclipse.com/ Cheers, -- Fabio Zadrozny ------------------------------------------------------ Software Developer LiClipse http://www.liclipse.com PyDev - Python Development Environment for Eclipse http://pydev.org http://pydev.blogspot.com PyVmMonitor - Python Profiler http://www.pyvmmonitor.com/ From ian.g.kelly at gmail.com Tue Jan 31 12:45:42 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 31 Jan 2017 10:45:42 -0700 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: On Jan 30, 2017 11:32 PM, "Gregory Ewing" wrote: > That's the thing I find most frustrating about both C# and Java, > the lack of anything like Python's "from ... import ...". You get > a choice of either effectively doing "import *" on the contents > of a whole class and polluting your namespace, or Fully.Qualifying. > Every.Little.Thing. I'm not sure about C#, but this is certainly not true in Java. Importing a class adds exactly one name to your namespace, just as in Python, which is the unqualified name of the class. Java also has "static import" which lets you individually import specific static methods or fields from a class. From ian.g.kelly at gmail.com Tue Jan 31 12:55:34 2017 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 31 Jan 2017 10:55:34 -0700 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: On Jan 30, 2017 8:00 PM, "Michael Torrie" wrote: > In any case, partial classes seems like a misfeature of C# to me but > apparently they are used when making Windows Forms GUI-based apps. > Apparently VS autogenerates the class that backs the form, and then the > developer creates a partial class that extends that to flesh out the > logic and handle the events. I'm not sure that partial classes were ever really meant as anything other than a useful feature for code-generating IDEs. But if you think that's weird, read up on C# extension methods, which are how users of your classes can add their own methods to them. True, this is also possible in Python but requires monkey-patching which is rightfully frowned upon. From george.trojan at noaa.gov Tue Jan 31 14:18:58 2017 From: george.trojan at noaa.gov (George Trojan - NOAA Federal) Date: Tue, 31 Jan 2017 19:18:58 +0000 Subject: Python3.6 tkinter bug? Message-ID: The following program behaves differently under Python 3.6: ''' checkbutton test ''' import tkinter class GUI(tkinter.Tk): def __init__(self): tkinter.Tk.__init__(self) frame = tkinter.Frame(self) for tag in ('A', 'B'): w = tkinter.Checkbutton(frame, text=tag) w.pack(side='top', padx=10, pady=10) print(w) frame.pack(side='top') frame = tkinter.Frame(self) for tag in ('C', 'D'): w = tkinter.Checkbutton(frame, text=tag) w.pack(side='top', padx=10, pady=10) print(w) frame.pack(side='top') gui = GUI() gui.mainloop() Selection of button 'A' also selects button 'C'. Same goes for 'B' and 'D'. I noticed that widget names have changed, which likely leads to the cause: > /usr/local/Python-3.5.1/bin/python3 foo.py .140182648425776.140182647743208 .140182648425776.140182647841848 .140182648424152.140182648282080 .140182648424152.140182648282136 > /usr/local/Python-3.6.0/bin/python3 foo.py .!frame.!checkbutton .!frame.!checkbutton2 .!frame2.!checkbutton .!frame2.!checkbutton2 Is this a known issue? George From larry.martell at gmail.com Tue Jan 31 15:26:05 2017 From: larry.martell at gmail.com (Larry Martell) Date: Tue, 31 Jan 2017 15:26:05 -0500 Subject: sorting a list of dicts by a computed field Message-ID: I have a list of dicts and one item of the dict is a date in m/d/Y format. I want to sort by that. I tried this: sorted(data['trends'], key=lambda k: datetime.strptime(k['date_time'],'%m/%d/%Y')) But that fails with: Exception Type: AttributeError at /report/CDSEM/WaferAlignment/ajax/waChart.json Exception Value: 'module' object has no attribute 'strptime' How can I do this sort? From rosuav at gmail.com Tue Jan 31 15:30:52 2017 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 1 Feb 2017 07:30:52 +1100 Subject: sorting a list of dicts by a computed field In-Reply-To: References: Message-ID: On Wed, Feb 1, 2017 at 7:26 AM, Larry Martell wrote: > I have a list of dicts and one item of the dict is a date in m/d/Y > format. I want to sort by that. I tried this: > > sorted(data['trends'], key=lambda k: > datetime.strptime(k['date_time'],'%m/%d/%Y')) > > But that fails with: > > Exception Type: AttributeError at /report/CDSEM/WaferAlignment/ajax/waChart.json > Exception Value: 'module' object has no attribute 'strptime' > > How can I do this sort? I think your problem here may be with how you're trying to parse that datetime. There are two common ways to get access to a thing called "datetime": # Option 1: import datetime # Option 2: from datetime import datetime The first one gives you a module; the second gives you just one class out of that module (one that happens to have the same name as the module itself). My guess is that you're using the first form, but you picked up some example code from somewhere that assumes the second. Try using "datetime.datetime.strptime" in your key function, and see if that helps. ChrisA From kevin.p.dwyer at gmail.com Tue Jan 31 15:33:08 2017 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Tue, 31 Jan 2017 20:33:08 +0000 Subject: sorting a list of dicts by a computed field References: Message-ID: Larry Martell wrote: > I have a list of dicts and one item of the dict is a date in m/d/Y > format. I want to sort by that. I tried this: > > sorted(data['trends'], key=lambda k: > datetime.strptime(k['date_time'],'%m/%d/%Y')) > > But that fails with: > > Exception Type: AttributeError at > /report/CDSEM/WaferAlignment/ajax/waChart.json Exception Value: 'module' > object has no attribute 'strptime' > > How can I do this sort? datetime.datetime.strptime? >>> import datetime >>> datetime.strptime Traceback (most recent call last): File "", line 1, in AttributeError: module 'datetime' has no attribute 'strptime' >>> datetime.datetime.strptime From python at mrabarnett.plus.com Tue Jan 31 15:34:14 2017 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 31 Jan 2017 20:34:14 +0000 Subject: sorting a list of dicts by a computed field In-Reply-To: References: Message-ID: On 2017-01-31 20:26, Larry Martell wrote: > I have a list of dicts and one item of the dict is a date in m/d/Y > format. I want to sort by that. I tried this: > > sorted(data['trends'], key=lambda k: > datetime.strptime(k['date_time'],'%m/%d/%Y')) > > But that fails with: > > Exception Type: AttributeError at /report/CDSEM/WaferAlignment/ajax/waChart.json > Exception Value: 'module' object has no attribute 'strptime' > > How can I do this sort? > The module 'datetime' contains a class called 'datetime'. Judging by your exception, 'datetime' is the module. Try "datetime.datetime.strptime" instead. From torriem at gmail.com Tue Jan 31 15:36:40 2017 From: torriem at gmail.com (Michael Torrie) Date: Tue, 31 Jan 2017 13:36:40 -0700 Subject: sorting a list of dicts by a computed field In-Reply-To: References: Message-ID: <7a563f6f-10c2-20e6-4379-26f219007d81@gmail.com> On 01/31/2017 01:26 PM, Larry Martell wrote: > I have a list of dicts and one item of the dict is a date in m/d/Y > format. I want to sort by that. I tried this: > > sorted(data['trends'], key=lambda k: > datetime.strptime(k['date_time'],'%m/%d/%Y')) > > But that fails with: > > Exception Type: AttributeError at /report/CDSEM/WaferAlignment/ajax/waChart.json > Exception Value: 'module' object has no attribute 'strptime' > > How can I do this sort? You're not showing us all your code, so I can only make an assumption that the "datetime" you are referencing is a module import. Did you try your sort function before you stuck it in a lambda? Here's a attempt: >>> import datetime >>> datetime.strptime Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'strptime' Looks like the same problem you encountered. So it has nothing to do with sorting. Quite literally, your datetime symbol does not contain the attribute strptime like you thought it did. I think you meant to use datetime.datetime.strptime: >>> import datetime >>> datetime.datetime.strptime("01/02/2003","%m/%d/%Y") datetime.datetime(2003, 1, 2, 0, 0) Use the interactive interpreter to test out little things like this. It will save you a lot of hassle later on. From larry.martell at gmail.com Tue Jan 31 15:39:40 2017 From: larry.martell at gmail.com (Larry Martell) Date: Tue, 31 Jan 2017 15:39:40 -0500 Subject: sorting a list of dicts by a computed field In-Reply-To: References: Message-ID: On Tue, Jan 31, 2017 at 3:30 PM, Chris Angelico wrote: > On Wed, Feb 1, 2017 at 7:26 AM, Larry Martell wrote: >> I have a list of dicts and one item of the dict is a date in m/d/Y >> format. I want to sort by that. I tried this: >> >> sorted(data['trends'], key=lambda k: >> datetime.strptime(k['date_time'],'%m/%d/%Y')) >> >> But that fails with: >> >> Exception Type: AttributeError at /report/CDSEM/WaferAlignment/ajax/waChart.json >> Exception Value: 'module' object has no attribute 'strptime' >> >> How can I do this sort? > > I think your problem here may be with how you're trying to parse that > datetime. There are two common ways to get access to a thing called > "datetime": > > # Option 1: > import datetime > # Option 2: > from datetime import datetime > > The first one gives you a module; the second gives you just one class > out of that module (one that happens to have the same name as the > module itself). My guess is that you're using the first form, but you > picked up some example code from somewhere that assumes the second. > > Try using "datetime.datetime.strptime" in your key function, and see > if that helps. Thanks. I knew that ;-) I've had a very long day, and I don't use lambda much so I thought it was an issue with that. From python at mrabarnett.plus.com Tue Jan 31 15:43:14 2017 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 31 Jan 2017 20:43:14 +0000 Subject: Python3.6 tkinter bug? In-Reply-To: References: Message-ID: On 2017-01-31 19:18, George Trojan - NOAA Federal wrote: > The following program behaves differently under Python 3.6: > > ''' > checkbutton test > ''' > > import tkinter > > class GUI(tkinter.Tk): > def __init__(self): > tkinter.Tk.__init__(self) > frame = tkinter.Frame(self) > for tag in ('A', 'B'): > w = tkinter.Checkbutton(frame, text=tag) > w.pack(side='top', padx=10, pady=10) > print(w) > frame.pack(side='top') > frame = tkinter.Frame(self) > for tag in ('C', 'D'): > w = tkinter.Checkbutton(frame, text=tag) > w.pack(side='top', padx=10, pady=10) > print(w) > frame.pack(side='top') > > gui = GUI() > gui.mainloop() > > Selection of button 'A' also selects button 'C'. Same goes for 'B' and 'D'. > I noticed that widget names have changed, which likely leads to the cause: > >> /usr/local/Python-3.5.1/bin/python3 foo.py > .140182648425776.140182647743208 > .140182648425776.140182647841848 > .140182648424152.140182648282080 > .140182648424152.140182648282136 > >> /usr/local/Python-3.6.0/bin/python3 foo.py > .!frame.!checkbutton > .!frame.!checkbutton2 > .!frame2.!checkbutton > .!frame2.!checkbutton2 > > Is this a known issue? > I don't believe so; at least, I couldn't see it in the bug tracker. Presumably you'll want to check the values of the checkboxes, so you'll bind them to tkinter variables (IntVar, etc). When you do that, it works correctly. From tjreedy at udel.edu Tue Jan 31 16:11:48 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 31 Jan 2017 16:11:48 -0500 Subject: Python3.6 tkinter bug? In-Reply-To: References: Message-ID: On 1/31/2017 3:43 PM, MRAB wrote: > On 2017-01-31 19:18, George Trojan - NOAA Federal wrote: >> The following program behaves differently under Python 3.6: >> >> ''' >> checkbutton test >> ''' >> >> import tkinter >> >> class GUI(tkinter.Tk): >> def __init__(self): >> tkinter.Tk.__init__(self) >> frame = tkinter.Frame(self) >> for tag in ('A', 'B'): >> w = tkinter.Checkbutton(frame, text=tag) >> w.pack(side='top', padx=10, pady=10) >> print(w) >> frame.pack(side='top') >> frame = tkinter.Frame(self) >> for tag in ('C', 'D'): >> w = tkinter.Checkbutton(frame, text=tag) >> w.pack(side='top', padx=10, pady=10) >> print(w) >> frame.pack(side='top') >> >> gui = GUI() >> gui.mainloop() >> >> Selection of button 'A' also selects button 'C'. Same goes for 'B' and >> 'D'. >> I noticed that widget names have changed, which likely leads to the >> cause: >> >>> /usr/local/Python-3.5.1/bin/python3 foo.py >> .140182648425776.140182647743208 >> .140182648425776.140182647841848 >> .140182648424152.140182648282080 >> .140182648424152.140182648282136 >> >>> /usr/local/Python-3.6.0/bin/python3 foo.py >> .!frame.!checkbutton >> .!frame.!checkbutton2 >> .!frame2.!checkbutton >> .!frame2.!checkbutton2 >> >> Is this a known issue? >> > I don't believe so; at least, I couldn't see it in the bug tracker. Me neither. I will open an issue on the tracker. > Presumably you'll want to check the values of the checkboxes, so you'll > bind them to tkinter variables (IntVar, etc). When you do that, it works > correctly. > -- Terry Jan Reedy From jladasky at itu.edu Tue Jan 31 16:23:43 2017 From: jladasky at itu.edu (jladasky at itu.edu) Date: Tue, 31 Jan 2017 13:23:43 -0800 (PST) Subject: sorting a list of dicts by a computed field In-Reply-To: References: Message-ID: <47e5beda-d536-4c7a-8c13-c61c18477058@googlegroups.com> On Tuesday, January 31, 2017 at 12:34:38 PM UTC-8, MRAB wrote: > On 2017-01-31 20:26, Larry Martell wrote: > The module 'datetime' contains a class called 'datetime'. Judging by > your exception, 'datetime' is the module. > > Try "datetime.datetime.strptime" instead. This re-use of the name "datetime" has tripped me up more than once. It's one of my few grumbles with the Python standard library. From saxri89 at gmail.com Tue Jan 31 16:29:43 2017 From: saxri89 at gmail.com (Xristos Xristoou) Date: Tue, 31 Jan 2017 13:29:43 -0800 (PST) Subject: DJANGO IMAGES QUESTION Message-ID: <594dba52-5a04-4bb5-8cfd-0d87cf921b90@googlegroups.com> who is the better method to take images from uses(visitors) on my web site and to return the new images(processing) back to users using DJANGO? i am new i dont know how to connect my python script with the html templetes to take the images and how to return? for example my script take images paths to processing new images with new paths. From jladasky at itu.edu Tue Jan 31 16:34:32 2017 From: jladasky at itu.edu (jladasky at itu.edu) Date: Tue, 31 Jan 2017 13:34:32 -0800 (PST) Subject: Overriding True and False ? In-Reply-To: References: <3F8981B9-4D4B-41B0-A864-24388A35F5FD@furrypants.com> Message-ID: On Sunday, January 29, 2017 at 11:29:29 PM UTC-8, Irv Kalb wrote: > It seems very odd that Python allows you to override the values of > True and False. Hi Irv, Let me join the chorus of people who are encouraging you to switch to Python3, which corrects this problem. Overriding builtins has been judged harmful, and now it is a lot harder to do. There are sometimes good reasons for shadowing builtins, but more than a few newcomers to Python (once upon a time, myself included) wrote buggy code by accident because of this surprising flexibility. That being said, I can't resist re-posting a story from 14 years ago: https://groups.google.com/forum/#!original/comp.lang.python/xEtUYsxLnFE/6yYgvhvkggMJ ============== From: lad... at my-deja.com (John Ladasky) Newsgroups: comp.lang.python Subject: Re: Python 2.3 True = False Date: 24 Mar 2003 12:29:04 -0800 an... at calbay.com (Anand) wrote in message news:... > >>> True = False > >>> True > False Shhh! Anand! You're not supposed to show anyone that! Now every politician on Earth will want to program in Python! ============== From tjreedy at udel.edu Tue Jan 31 16:38:09 2017 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 31 Jan 2017 16:38:09 -0500 Subject: Python3.6 tkinter bug? In-Reply-To: References: Message-ID: On 1/31/2017 4:11 PM, Terry Reedy wrote: > On 1/31/2017 3:43 PM, MRAB wrote: >> On 2017-01-31 19:18, George Trojan - NOAA Federal wrote: >>> The following program behaves differently under Python 3.6: >>> >>> ''' >>> checkbutton test >>> ''' >>> >>> import tkinter >>> >>> class GUI(tkinter.Tk): >>> def __init__(self): >>> tkinter.Tk.__init__(self) >>> frame = tkinter.Frame(self) >>> for tag in ('A', 'B'): >>> w = tkinter.Checkbutton(frame, text=tag) >>> w.pack(side='top', padx=10, pady=10) >>> print(w) >>> frame.pack(side='top') >>> frame = tkinter.Frame(self) >>> for tag in ('C', 'D'): >>> w = tkinter.Checkbutton(frame, text=tag) >>> w.pack(side='top', padx=10, pady=10) >>> print(w) >>> frame.pack(side='top') >>> >>> gui = GUI() >>> gui.mainloop() >>> >>> Selection of button 'A' also selects button 'C'. Same goes for 'B' and >>> 'D'. >>> I noticed that widget names have changed, which likely leads to the >>> cause: >>> >>>> /usr/local/Python-3.5.1/bin/python3 foo.py >>> .140182648425776.140182647743208 >>> .140182648425776.140182647841848 >>> .140182648424152.140182648282080 >>> .140182648424152.140182648282136 >>> >>>> /usr/local/Python-3.6.0/bin/python3 foo.py >>> .!frame.!checkbutton >>> .!frame.!checkbutton2 >>> .!frame2.!checkbutton >>> .!frame2.!checkbutton2 >>> >>> Is this a known issue? >>> >> I don't believe so; at least, I couldn't see it in the bug tracker. > > Me neither. I will open an issue on the tracker. https://bugs.python.org/issue29402 >> Presumably you'll want to check the values of the checkboxes, so you'll >> bind them to tkinter variables (IntVar, etc). When you do that, it works >> correctly. This still seems like a bug to me. Similar issue https://bugs.python.org/issue25684 has the same workaround for ttk Option Menu radiobuttons. -- Terry Jan Reedy From greg.ewing at canterbury.ac.nz Tue Jan 31 17:02:13 2017 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 01 Feb 2017 11:02:13 +1300 Subject: What are your opinions on .NET Core vs Python? In-Reply-To: References: <6ade49f7-e0fa-90e0-7b47-ad5e14c391fe@gmail.com> <21c7475509634730811331fe2804cc0f@activenetwerx.com> Message-ID: Ian Kelly wrote: > Java also has "static import" which lets > you individually import specific static methods or fields from a class. Yes, but it's nowhere near as convenient as Python's import. To import individual names you have to qualify all of them with the whole package name, and there is no way to give them different names locally. -- Greg From ceh329 at gmail.com Tue Jan 31 17:11:10 2017 From: ceh329 at gmail.com (Charles Heizer) Date: Tue, 31 Jan 2017 14:11:10 -0800 (PST) Subject: Embedding python, run a function and get it's result Message-ID: <1d151468-9d4f-4be7-8f36-c53131717b3e@googlegroups.com> Hello, I'm messing around with the embedded python and I can get parts to work. What I'm having a hard time is getting my head around calling a function in the python string and getting it's result. Question, how do I load the python script and call runMe() and get it's value? Thanks! Example Works Obj-C Code: Py_Initialize(); PyObject *py_main, *py_dict; py_main = PyImport_AddModule("__main__"); py_dict = PyModule_GetDict(py_main); // Run the python source code PyRun_String([auditScript UTF8String], Py_file_input, py_dict, py_dict); PyObject *pythonString = PyDict_GetItemString(py_dict, "x") ; if (!pythonString) { NSLog(@"pythonString returned NULL"); return "NO"; } // Convert the unpickled Python string to a C string char *cString = PyString_AsString(pythonString); Py_DECREF(pythonString); f (!cString) { NSLog(@"Failed converting pythonString string to C string"); return 1; } NSLog(@"answer: %@",[NSString cString]); Python Code: x = "NA" def runMe(): return "YES" x = runMe() From auriocus at gmx.de Tue Jan 31 17:34:22 2017 From: auriocus at gmx.de (Christian Gollwitzer) Date: Tue, 31 Jan 2017 23:34:22 +0100 Subject: Python3.6 tkinter bug? In-Reply-To: References: Message-ID: Am 31.01.17 um 20:18 schrieb George Trojan - NOAA Federal: > Selection of button 'A' also selects button 'C'. Same goes for 'B' and 'D'. > I noticed that widget names have changed, which likely leads to the cause: > >> /usr/local/Python-3.5.1/bin/python3 foo.py > .140182648425776.140182647743208 > .140182648425776.140182647841848 > .140182648424152.140182648282080 > .140182648424152.140182648282136 > >> /usr/local/Python-3.6.0/bin/python3 foo.py > .!frame.!checkbutton > .!frame.!checkbutton2 > .!frame2.!checkbutton > .!frame2.!checkbutton2 The widget names look fine to, and the 3.6 naming is way better, because it can be used for debugging more easily. The behaviour you describe can have two reasons, a) the same widget can be packed twice b) the widgets use the same variable. In Tk, a widget does not need an associate variable - which can be done by setting the variable to an empty string, or by leaving this option off. Presumably Python 3.6 passes anything else than an empty string to Tk as the -variable option, maybe a mistranslated None? Can't test it myself, but in your example you could, for instance, check the output of self.call(w, 'configure'), which lists you the Tk side configuration, or self.call(w, 'configure', '-variable') to get specifically the bound variable. Christian From kapitikusha at gmail.com Tue Jan 31 17:34:58 2017 From: kapitikusha at gmail.com (Marvin Sinclair) Date: Tue, 31 Jan 2017 14:34:58 -0800 (PST) Subject: What's the best python projects book Message-ID: <01584806-b34f-4261-a466-128269fd2f78@googlegroups.com> Am a newbie to python, just to completed reading and practice a python book, but I wanted to know if there is a good python book that is composed of different projects on different topics that I can code along, read what is happening so i can gain more experience, please suggest me any, or if you have it please email it to me Kapitikusha at gmail.com i will so much appreciate From python at mrabarnett.plus.com Tue Jan 31 18:02:41 2017 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 31 Jan 2017 23:02:41 +0000 Subject: Python3.6 tkinter bug? In-Reply-To: References: Message-ID: On 2017-01-31 22:34, Christian Gollwitzer wrote: > Am 31.01.17 um 20:18 schrieb George Trojan - NOAA Federal: >> Selection of button 'A' also selects button 'C'. Same goes for 'B' and 'D'. >> I noticed that widget names have changed, which likely leads to the cause: >> >>> /usr/local/Python-3.5.1/bin/python3 foo.py >> .140182648425776.140182647743208 >> .140182648425776.140182647841848 >> .140182648424152.140182648282080 >> .140182648424152.140182648282136 >> >>> /usr/local/Python-3.6.0/bin/python3 foo.py >> .!frame.!checkbutton >> .!frame.!checkbutton2 >> .!frame2.!checkbutton >> .!frame2.!checkbutton2 > > The widget names look fine to, and the 3.6 naming is way better, because > it can be used for debugging more easily. The behaviour you describe can > have two reasons, a) the same widget can be packed twice b) the widgets > use the same variable. In Tk, a widget does not need an associate > variable - which can be done by setting the variable to an empty string, > or by leaving this option off. > > Presumably Python 3.6 passes anything else than an empty string to Tk as > the -variable option, maybe a mistranslated None? Can't test it myself, > but in your example you could, for instance, check the output of > self.call(w, 'configure'), which lists you the Tk side configuration, or > self.call(w, 'configure', '-variable') to get specifically the bound > variable. > Perhaps someone who knows Tcl and tk can tell me, but I notice that in the first example, the second part of the widget names are unique, whereas in the second example, the second part of the widget names are the reused (both "!checkbutton" and "!checkbutton2" occur twice). Is that the cause? Do the names need to be: .!frame.!checkbutton .!frame.!checkbutton2 .!frame2.!checkbutton3 .!frame2.!checkbutton4 ? From ben+python at benfinney.id.au Tue Jan 31 18:39:21 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 01 Feb 2017 10:39:21 +1100 Subject: command line micro wiki written in Python References: Message-ID: <8560kusst2.fsf@benfinney.id.au> Paul Wolf writes: > I've created a command line utility for managing text files. It's > written in Python: [?] Thank you. > The key aspects are > > * Entirely command-line driven > > * Text documents only with a slight preference for Markdown The Python community has a stronger (?) preference for reStructuredText format. Can that be the default? That is, I want my text files to be named ?foo? (no suffix) or ?foo.txt? (because they're primarily text), and have the default be to parse them as reStructuredText. > I'd be interested in feedback if there is any interest in using this > kind of utility. I'll expose a python API so bulk operations can be > done easily on the command line or via scripts. This is a great way to start! -- \ ?Science shows that belief in God is not only obsolete. It is | `\ also incoherent.? ?Victor J. Stenger, 2001 | _o__) | Ben Finney From george.trojan at noaa.gov Tue Jan 31 18:48:38 2017 From: george.trojan at noaa.gov (George Trojan - NOAA Federal) Date: Tue, 31 Jan 2017 23:48:38 +0000 Subject: Python3.6 tkinter bug Message-ID: On 2017-01-31 18:02, MRAB wrote: On 2017-01-31 22:34, Christian Gollwitzer wrote: > >* Am 31.01.17 um 20:18 schrieb George Trojan - NOAA Federal: > *>>* Selection of button 'A' also selects button 'C'. Same goes for 'B' and 'D'. > *>>* I noticed that widget names have changed, which likely leads to the cause: > *>> > >>>* /usr/local/Python-3.5.1/bin/python3 foo.py > *>>* .140182648425776.140182647743208 > *>>* .140182648425776.140182647841848 > *>>* .140182648424152.140182648282080 > *>>* .140182648424152.140182648282136 > *>> > >>>* /usr/local/Python-3.6.0/bin/python3 foo.py > *>>* .!frame.!checkbutton > *>>* .!frame.!checkbutton2 > *>>* .!frame2.!checkbutton > *>>* .!frame2.!checkbutton2 > *> > >* The widget names look fine to, and the 3.6 naming is way better, because > *>* it can be used for debugging more easily. The behaviour you describe can > *>* have two reasons, a) the same widget can be packed twice b) the widgets > *>* use the same variable. In Tk, a widget does not need an associate > *>* variable - which can be done by setting the variable to an empty string, > *>* or by leaving this option off. > *> > >* Presumably Python 3.6 passes anything else than an empty string to Tk as > *>* the -variable option, maybe a mistranslated None? Can't test it myself, > *>* but in your example you could, for instance, check the output of > *>* self.call(w, 'configure'), which lists you the Tk side configuration, or > *>* self.call(w, 'configure', '-variable') to get specifically the bound > *>* variable. > *> > Perhaps someone who knows Tcl and tk can tell me, but I notice that in > the first example, the second part of the widget names are unique, > whereas in the second example, the second part of the widget names are > the reused (both "!checkbutton" and "!checkbutton2" occur twice). Is > that the cause? > Do the names need to be: > .!frame.!checkbutton > .!frame.!checkbutton2 > .!frame2.!checkbutton3 > .!frame2.!checkbutton4 > ? Adding dummy variable solves the issue. Following Christian's suggestion I added code to print Tk variable: from functools import partial import tkinter class GUI(tkinter.Tk): def __init__(self): tkinter.Tk.__init__(self) frame = tkinter.Frame(self) for tag in ('A', 'B'): w = tkinter.Checkbutton(frame, text=tag, # variable=tkinter.IntVar(), command=partial(print, tag)) w.pack(side='top', padx=10, pady=10) print(tag, self.call(w, 'configure', '-variable')) print(w) frame.pack(side='top') frame = tkinter.Frame(self) for tag in ('C', 'D'): w = tkinter.Checkbutton(frame, text=tag, # variable=tkinter.IntVar(), command=partial(print, tag)) w.pack(side='top', padx=10, pady=10) print(tag, self.call(w, 'configure', '-variable')) print(w) frame.pack(side='top') gui = GUI() gui.mainloop() The output is: (venv-3.6.0) dilbert at gtrojan> python foo.py A ('-variable', 'variable', 'Variable', '', ) .!frame.!checkbutton B ('-variable', 'variable', 'Variable', '', ) .!frame.!checkbutton2 C ('-variable', 'variable', 'Variable', '', ) .!frame2.!checkbutton D ('-variable', 'variable', 'Variable', '', ) .!frame2.!checkbutton2 It looks like the default name is the last component of widget tree. When the # sign is removed, the names are unique and the problem disappears: (venv-3.6.0) dilbert at gtrojan> python foo.py A ('-variable', 'variable', 'Variable', '', ) .!frame.!checkbutton B ('-variable', 'variable', 'Variable', '', ) .!frame.!checkbutton2 C ('-variable', 'variable', 'Variable', '', ) .!frame2.!checkbutton D ('-variable', 'variable', 'Variable', '', ) .!frame2.!checkbutton2 From jaydesai834 at hotmail.com Tue Jan 31 19:45:58 2017 From: jaydesai834 at hotmail.com (Jay Desai) Date: Wed, 1 Feb 2017 00:45:58 +0000 Subject: SQL CE Database (.sdf) file connection with python Message-ID: Hello, I came across a post from Nikhil Joshi at https://mail.python.org/pipermail/python-list/2014-November/681568.html. *.sdf database access - mail.python.org mail.python.org On Saturday, April 21, 2012 6:55:55 AM UTC-4, Alex Willmer wrote: > On Apr 19, 9:18 pm, Page3D wrote: > > Hi, I am trying to connect and access ... I am facing the following problem. Can anyone please provide some guideline? Problem Statement: Extract data stored in the .sdf file to python. System config: Win 10 Pro 64-bit, python 3.5.2-64 bit, adodbapi library, SQL CE 3.5 I am fairly new to programming and I have picked up Python as my first language to learn. Currently, I have hit a wall in process of connecting a SQL CE 3.5 .sdf file. I have used the adodbapi library. I have searched the web extensively over the past week to find a solution to this problem and to make sure that my connection string is correct. I have tried multiple options/solutions provided on stack overflow and https://www.connectionstrings.com/microsoft-sqlserver-ce-oledb-3-5/. Code: import adodbapi cons_str = "Provoider=Microsoft.SQLSERVER.MOBILE.OLEDB.3.5;" \ "Data Source=D:\Work\Programming\Python\SQL_DataTransfer\LF.sdf;"\ "Persist Security Info=False;" \ "SSCE:Max Database Size=4091" connection = adodbapi.connect(cons_str) print(connection) Error Message: Traceback (most recent call last): File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 93, in make_COM_connecter c = Dispatch('ADODB.Connection') #connect after CoIninialize v2.1.1 adamvan NameError: name 'Dispatch' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 112, in connect co.connect(kwargs) File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 269, in connect self.connector = connection_maker() File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 95, in make_COM_connecter raise api.InterfaceError ("Windows COM Error: Dispatch('ADODB.Connection') failed.") adodbapi.apibase.InterfaceError: Windows COM Error: Dispatch('ADODB.Connection') failed. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:/Work/Programming/Python/SQL_DataTransfer/SQL_CE_reportDB.py", line 8, in connection = adodbapi.connect(cons_str) File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 116, in connect raise api.OperationalError(e, message) adodbapi.apibase.OperationalError: (InterfaceError("Windows COM Error: Dispatch('ADODB.Connection') failed.",), 'Error opening connection to "Provoider=Microsoft.SQLSERVER.MOBILE.OLEDB.3.5;Data Source=D:\Work\Programming\Python\SQL_DataTransfer\LF.sdf;Persist Security Info=False;SSCE:Max Database Size=4091"') At this point any help is much appreciated. Thank you, Sincerely, JD. From littledddna at gmail.com Tue Jan 31 20:41:30 2017 From: littledddna at gmail.com (Xiao Zhang) Date: Tue, 31 Jan 2017 15:41:30 -1000 Subject: Qtcore error problem Message-ID: Dear python users, I am new to python and now I met a problem. When I try to run a py file, I got the following error: from vacumm.misc.grid.regridding import regrid2d File "/home/xzhang3/usr/local/uvcdat/2.4.1/lib/python2.7/site-packages/vacumm/misc/__init__.py", line 41, in import color File "/home/xzhang3/usr/local/uvcdat/2.4.1/lib/python2.7/site-packages/vacumm/misc/color.py", line 50, in import pylab as P ImportError: /home/xzhang3/usr/local/uvcdat/2.4.1/lib/python2.7/site-packages/PyQt4/QtCore.so: undefined symbol: _ZN7QLocale14scriptToStringENS_6ScriptE I searched the web and it looks like the problem has something to do with different versions. Any idea how to fix the problem ? I am using python 2.7 now. Really appreciate your ideas and best regards, Xiao From python at mrabarnett.plus.com Tue Jan 31 22:12:08 2017 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 1 Feb 2017 03:12:08 +0000 Subject: SQL CE Database (.sdf) file connection with python In-Reply-To: References: Message-ID: <4459f40e-1838-3f81-7bd9-0a928342b7b7@mrabarnett.plus.com> On 2017-02-01 00:45, Jay Desai wrote: > Hello, > > > I came across a post from Nikhil Joshi at https://mail.python.org/pipermail/python-list/2014-November/681568.html. > > *.sdf database access - mail.python.org > mail.python.org > On Saturday, April 21, 2012 6:55:55 AM UTC-4, Alex Willmer wrote: > On Apr 19, 9:18 pm, Page3D wrote: > > Hi, I am trying to connect and access ... > > > > > I am facing the following problem. Can anyone please provide some guideline? > > Problem Statement: Extract data stored in the .sdf file to python. System config: Win 10 Pro 64-bit, python 3.5.2-64 bit, adodbapi library, SQL CE 3.5 > > I am fairly new to programming and I have picked up Python as my first language to learn. Currently, I have hit a wall in process of connecting a SQL CE 3.5 .sdf file. > > I have used the adodbapi library. I have searched the web extensively over the past week to find a solution to this problem and to make sure that my connection string is correct. I have tried multiple options/solutions provided on stack overflow and https://www.connectionstrings.com/microsoft-sqlserver-ce-oledb-3-5/. > > Code: > > import adodbapi > > cons_str = "Provoider=Microsoft.SQLSERVER.MOBILE.OLEDB.3.5;" \ > "Data Source=D:\Work\Programming\Python\SQL_DataTransfer\LF.sdf;"\ > "Persist Security Info=False;" \ > "SSCE:Max Database Size=4091" > I can a problem here. Your string contains "Provoider", which is a misspelling. It should be "Provider". It would also be a good idea to make those 'raw' strings (prefix them with 'r') where they contain backslashes. > connection = adodbapi.connect(cons_str) > print(connection) > > Error Message: Traceback (most recent call last): File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 93, in make_COM_connecter c = Dispatch('ADODB.Connection') #connect after CoIninialize v2.1.1 adamvan NameError: name 'Dispatch' is not defined > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 112, in connect co.connect(kwargs) File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 269, in connect self.connector = connection_maker() File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 95, in make_COM_connecter raise api.InterfaceError ("Windows COM Error: Dispatch('ADODB.Connection') failed.") adodbapi.apibase.InterfaceError: Windows COM Error: Dispatch('ADODB.Connection') failed. > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): File "D:/Work/Programming/Python/SQL_DataTransfer/SQL_CE_reportDB.py", line 8, in connection = adodbapi.connect(cons_str) File "D:\Work\Programs\Python35.virtualenvs\sql_output\lib\site-packages\adodbapi\adodbapi.py", line 116, in connect raise api.OperationalError(e, message) adodbapi.apibase.OperationalError: (InterfaceError("Windows COM Error: Dispatch('ADODB.Connection') failed.",), 'Error opening connection to "Provoider=Microsoft.SQLSERVER.MOBILE.OLEDB.3.5;Data Source=D:\Work\Programming\Python\SQL_DataTransfer\LF.sdf;Persist Security Info=False;SSCE:Max Database Size=4091"') > > At this point any help is much appreciated. > I've had a quick look on StackOverflow. I think you have to install pywin32.