From jorvis at gmail.com Fri May 3 14:54:26 2013 From: jorvis at gmail.com (jorvis at gmail.com) Date: Fri, 3 May 2013 11:54:26 -0700 (PDT) Subject: pycache directories In-Reply-To: <841fcc01-df05-4a8b-8b4f-1217c49aac44@googlegroups.com> References: <841fcc01-df05-4a8b-8b4f-1217c49aac44@googlegroups.com> Message-ID: I agree, this would be a good feature. I redirect my emacs backups to a single directory too so that they're not distributed all over the file system. While I don't know how to do that with the things python creates, I've read a few ways you can disable their creation altogether (assuming you don't care or need the benefit of their use): You can run python with -B or set the environmental variable PYTHONDONTWRITEBYTECODE=1 For fun, lots of developer discussion about its original creation here: http://www.gossamer-threads.com/lists/python/dev/815510 From nikos.gr33k at gmail.com Sun May 26 16:48:47 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 13:48:47 -0700 (PDT) Subject: This mail never gets delivered. Any ideas why? In-Reply-To: <531b6ec4-2782-4886-bbfe-a395752c026f@googlegroups.com> References: <10f30df1-e0f8-4834-ae5f-b6e0d0aac079@googlegroups.com> <513c209d$0$6512$c3e8da3$5496439d@news.astraweb.com> <3daebceb-88e2-4d0b-bc32-802e6cece93e@googlegroups.com> <513c624c$0$6512$c3e8da3$5496439d@news.astraweb.com> <08433d4e-6ffb-4929-a708-f8ba1d670449@googlegroups.com> <531b6ec4-2782-4886-bbfe-a395752c026f@googlegroups.com> Message-ID: <6a94ee23-f0f6-470c-a927-0d6bb35a3114@googlegroups.com> I'am receiving this now after some tries: A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. /home/nikos/public_html/cgi-bin/metrites.py in () 139 else: 140 sp = subprocess.Popen(['mail', '-f', FROM, '-s', 'Mail from Guest', 'support at superhost.gr'], stdin=subprocess.PIPE) => 141 sp.communicate( MESSAGE ) 142 status = sp.wait() 143 if status: sp = , sp.communicate = >, MESSAGE = 'kdsjfksdjkfjksdjfs\r\n\t' /opt/python3/lib/python3.3/subprocess.py in communicate(self=, input='kdsjfksdjkfjksdjfs\r\n\t', timeout=None) 901 if input: 902 try: => 903 self.stdin.write(input) 904 except IOError as e: 905 if e.errno != errno.EPIPE and e.errno != errno.EINVAL: self = , self.stdin = <_io.BufferedWriter name=5>, self.stdin.write = , input = 'kdsjfksdjkfjksdjfs\r\n\t' TypeError: 'str' does not support the buffer interface args = ("'str' does not support the buffer interface",) with_traceback = From cs at zip.com.au Sun May 26 20:22:29 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 27 May 2013 10:22:29 +1000 Subject: This mail never gets delivered. Any ideas why? In-Reply-To: <6a94ee23-f0f6-470c-a927-0d6bb35a3114@googlegroups.com> References: <6a94ee23-f0f6-470c-a927-0d6bb35a3114@googlegroups.com> Message-ID: <20130527002229.GA12740@cskk.homeip.net> On 26May2013 13:48, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= wrote: | I'am receiving this now after some tries: | | A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. | | /home/nikos/public_html/cgi-bin/metrites.py in () | 139 else: | 140 sp = subprocess.Popen(['mail', '-f', FROM, '-s', 'Mail from Guest', 'support at superhost.gr'], stdin=subprocess.PIPE) | => 141 sp.communicate( MESSAGE ) | 142 status = sp.wait() | 143 if status: | sp = , sp.communicate = >, MESSAGE = 'kdsjfksdjkfjksdjfs\r\n\t' | /opt/python3/lib/python3.3/subprocess.py in communicate(self=, input='kdsjfksdjkfjksdjfs\r\n\t', timeout=None) | 901 if input: | 902 try: | => 903 self.stdin.write(input) Well, you should probably be writing to sp.stdin, not sys.stdin. | 904 except IOError as e: | 905 if e.errno != errno.EPIPE and e.errno != errno.EINVAL: | self = , self.stdin = <_io.BufferedWriter name=5>, self.stdin.write = , input = 'kdsjfksdjkfjksdjfs\r\n\t' | TypeError: 'str' does not support the buffer interface | args = ("'str' does not support the buffer interface",) | with_traceback = This is symptomatic of sys.stdin (well, whatever you're writing to) being open in binary mode instead of text mode. And you're passing a str. Try passing std.encode(). Cheers, -- Cameron Simpson Yes, [congress is] petty and venal and selfish. That's why they're called _representatives_. - Will Durst From cs at zip.com.au Sun May 26 20:28:09 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 27 May 2013 10:28:09 +1000 Subject: This mail never gets delivered. Any ideas why? In-Reply-To: <20130527002229.GA12740@cskk.homeip.net> References: <20130527002229.GA12740@cskk.homeip.net> Message-ID: <20130527002809.GA28797@cskk.homeip.net> On 27May2013 10:22, I wrote: | | => 903 self.stdin.write(input) [...] | | self = , self.stdin = <_io.BufferedWriter name=5>, self.stdin.write = , input = 'kdsjfksdjkfjksdjfs\r\n\t' | | TypeError: 'str' does not support the buffer interface | | args = ("'str' does not support the buffer interface",) | | with_traceback = | | This is symptomatic of sys.stdin (well, whatever you're writing to) | being open in binary mode instead of text mode. And you're passing | a str. Try passing std.encode(). Sorry, that should be "input.encode()". -- Cameron Simpson PCs are like a submarine, it will work fine till you open Windows. - zollie101 From nikos.gr33k at gmail.com Mon May 27 01:00:15 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 22:00:15 -0700 (PDT) Subject: This mail never gets delivered. Any ideas why? In-Reply-To: References: <20130527002229.GA12740@cskk.homeip.net> Message-ID: <6f870aa1-8a94-4510-a466-ca48e0605d9f@googlegroups.com> else: sp = subprocess.Popen(['mail', '-f', FROM, '-s', 'Mail from Guest', 'support at superhost.gr'], sp.stdin=subprocess.PIPE) sp.communicate( input.encode(MESSAGE, 'utf-8') ) status = sp.wait() this gives me an internal server error Cameron, but i guess iam writing it wrong. Thsi though doesnt provide an error: else: sp = subprocess.Popen(['mail', '-f', FROM, '-s', 'Mail from Guest', 'support at superhost.gr'], stdin=subprocess.PIPE) sp.communicate( bytes( MESSAGE, 'utf-8' ) ) status = sp.wait() but neither gets the mail across. From nikos.gr33k at gmail.com Mon May 27 05:15:12 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Mon, 27 May 2013 02:15:12 -0700 (PDT) Subject: This mail never gets delivered. Any ideas why? In-Reply-To: <6f870aa1-8a94-4510-a466-ca48e0605d9f@googlegroups.com> References: <20130527002229.GA12740@cskk.homeip.net> <6f870aa1-8a94-4510-a466-ca48e0605d9f@googlegroups.com> Message-ID: <2f789ce5-b493-4ee4-95c4-0e8885978259@googlegroups.com> Please, do you see an error in this? As i said the 2nd solution doesnt provide an error but also doesn't get the mail send too. From breamoreboy at yahoo.co.uk Mon May 27 07:44:21 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 27 May 2013 12:44:21 +0100 Subject: This mail never gets delivered. Any ideas why? In-Reply-To: <2f789ce5-b493-4ee4-95c4-0e8885978259@googlegroups.com> References: <20130527002229.GA12740@cskk.homeip.net> <6f870aa1-8a94-4510-a466-ca48e0605d9f@googlegroups.com> <2f789ce5-b493-4ee4-95c4-0e8885978259@googlegroups.com> Message-ID: On 27/05/2013 10:15, ????? ???33? wrote: > Please, do you see an error in this? > As i said the 2nd solution doesnt provide an error but also doesn't get the mail send too. > At least you're improving. Yesterday you were chasing after two hours, it's now up to four hours 15 minutes. Keep doubling the time like this and you'll soon be up to the 24 hours that's considered polite. As previously stated the time can be shortened substantially by getting your cheque book out. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From dreamingforward at gmail.com Wed May 8 22:35:58 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Wed, 8 May 2013 19:35:58 -0700 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Fri, Apr 12, 2013 at 11:28 PM, Mark Janssen wrote: >> Mark, this proposal is out of place on a Python list, because it proposes an >> object methodology radically different from any that is implemented in >> Python now, or is even remotely likely to be implemented in Python in the >> future. > > Wow, you guys are a bunch of ninnies. I'm going to find some > theoretical folks.... Okay, to anyone who might be listening, I found the core of the problem. This issue is/was much deeper than OOP (which would be roughly a 20 year refactoring) -- that was my mistake. The issue goes right to the core to models of computation and the historical factions within theoretical CS itself (a 50+ year refactoring). The field needs re-invented and re-centered. Mark my words. There has been a half-century of confusion between two entirely separate domains and they've been using the same lexicon. Long story short: the lambda calculus folks have to split from the Turing machine folks. These models of computation should not use the same language. Their computation models are too radically different. Lisp will remain a pinnacle of the lambda calculus, but should be remanded to philosophy. The logic of the binary/boolean arithmetic is simply not compatible, but forms the basis of any sensible computer science here in the West. Here pronouncith the.... whatever -- MarkJ Tacoma, Washington From dreamingforward at gmail.com Wed May 8 22:39:03 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Wed, 8 May 2013 19:39:03 -0700 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: > ...The field needs re-invented and re-centered.[...] For anyone who want to be involved. See the wikiwikiweb -- a tool that every programmer should know and use -- and these pages: ComputerScienceVersionTwo and ObjectOrientedRefactored. Cheers! -- MarkJ Tacoma, Washington From ned at nedbatchelder.com Sun May 12 13:18:58 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Sun, 12 May 2013 13:18:58 -0400 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: <518FCF02.9090307@nedbatchelder.com> On 5/8/2013 10:39 PM, Mark Janssen wrote: >> ...The field needs re-invented and re-centered.[...] > For anyone who want to be involved. See the wikiwikiweb -- a tool > that every programmer should know and use -- and these pages: > ComputerScienceVersionTwo and ObjectOrientedRefactored. I've never understood why people use that site: the pages end up being unintelligible cocktail-party noise-scapes with no hope of understanding who is saying what, or in response to whom. --Ned. > Cheers! From fabiosantosart at gmail.com Sun May 12 13:52:58 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sun, 12 May 2013 18:52:58 +0100 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: <518FCF02.9090307@nedbatchelder.com> References: <5167F327.3030803@nedbatchelder.com> <518FCF02.9090307@nedbatchelder.com> Message-ID: On 12 May 2013 18:23, "Ned Batchelder" wrote: > > I've never understood why people use that site: the pages end up being unintelligible cocktail-party noise-scapes with no hope of understanding who is saying what, or in response to whom. > > --Ned. There's not so much noise there, but indeed the communication gets confusing after a few paragraphs. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Sun May 12 16:26:48 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 12 May 2013 16:26:48 -0400 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: <518FCF02.9090307@nedbatchelder.com> References: <5167F327.3030803@nedbatchelder.com> <518FCF02.9090307@nedbatchelder.com> Message-ID: On 5/12/2013 1:18 PM, Ned Batchelder wrote: > On 5/8/2013 10:39 PM, Mark Janssen wrote: >>> ...The field needs re-invented and re-centered.[...] >> For anyone who want to be involved. See the wikiwikiweb -- a tool >> that every programmer should know and use -- and these pages: >> ComputerScienceVersionTwo and ObjectOrientedRefactored. > > I've never understood why people use that site: the pages end up being > unintelligible cocktail-party noise-scapes with no hope of understanding > who is saying what, or in response to whom. I certainly found it confusing that something responder and OP comments were in normal text and italic respectively, and sometimes the opposite, and maybe sometimes both in normal types. From dreamingforward at gmail.com Mon May 13 00:48:45 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Sun, 12 May 2013 21:48:45 -0700 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: <518FCF02.9090307@nedbatchelder.com> References: <5167F327.3030803@nedbatchelder.com> <518FCF02.9090307@nedbatchelder.com> Message-ID: On Sun, May 12, 2013 at 10:18 AM, Ned Batchelder wrote: > I've never understood why people use that site: the pages end up being > unintelligible cocktail-party noise-scapes with no hope of understanding who > is saying what, or in response to whom. You're very right. But that is what has made it sort of a test-bed for internet collaboration. The project I'm working on is aimed to solve that problem and take the Wiki philosophy to its next or even ultimate level. By adding a "natural" per-revision voting and user-ranking it can clear up all the noise and scale to the whole internet itself. ....But no one around here seem to think its possible. -- MarkJ Tacoma, Washington From fabiosantosart at gmail.com Mon May 13 04:29:19 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 13 May 2013 09:29:19 +0100 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> <518FCF02.9090307@nedbatchelder.com> Message-ID: Sounds a lot like reddit threads. On 13 May 2013 08:17, "Mark Janssen" wrote: > On Sun, May 12, 2013 at 10:18 AM, Ned Batchelder > wrote: > > I've never understood why people use that site: the pages end up being > > unintelligible cocktail-party noise-scapes with no hope of understanding > who > > is saying what, or in response to whom. > > You're very right. But that is what has made it sort of a test-bed > for internet collaboration. The project I'm working on is aimed to > solve that problem and take the Wiki philosophy to its next or even > ultimate level. By adding a "natural" per-revision voting and > user-ranking it can clear up all the noise and scale to the whole > internet itself. ....But no one around here seem to think its > possible. > -- > MarkJ > Tacoma, Washington > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dreamingforward at gmail.com Tue May 14 15:11:24 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Tue, 14 May 2013 12:11:24 -0700 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> <518FCF02.9090307@nedbatchelder.com> Message-ID: > Sounds a lot like reddit threads. It's similar, but it goes a lot further. Where every other site without centralized editors, the thread mess on a simple flat page doesn't scale after about a 100 interactions. To sort out the mess, it takes another dimension. The project I'm working on uses a 3 dimensional visualization model that can scale and order millions of nodes. -- MarkJ Tacoma, Washington From fabiosantosart at gmail.com Tue May 14 16:11:29 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 14 May 2013 21:11:29 +0100 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> <518FCF02.9090307@nedbatchelder.com> Message-ID: Impressive, I'd say. On Tue, May 14, 2013 at 8:11 PM, Mark Janssen wrote: >> Sounds a lot like reddit threads. > > It's similar, but it goes a lot further. Where every other site > without centralized editors, the thread mess on a simple flat page > doesn't scale after about a 100 interactions. To sort out the mess, > it takes another dimension. The project I'm working on uses a 3 > dimensional visualization model that can scale and order millions of > nodes. > > -- > MarkJ > Tacoma, Washington -- F?bio Santos From ian.g.kelly at gmail.com Tue May 14 15:46:56 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 14 May 2013 13:46:56 -0600 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> <518FCF02.9090307@nedbatchelder.com> Message-ID: On Sun, May 12, 2013 at 10:48 PM, Mark Janssen wrote: > You're very right. But that is what has made it sort of a test-bed > for internet collaboration. The project I'm working on is aimed to > solve that problem and take the Wiki philosophy to its next or even > ultimate level. By adding a "natural" per-revision voting and > user-ranking it can clear up all the noise and scale to the whole > internet itself. ....But no one around here seem to think its > possible. I may have missed it in the noise, but I don't recall that you ever proposed such a project "around here". That said, your brief description does sound rather like Cloud Cuckoo Land to me. From ian.g.kelly at gmail.com Thu May 9 13:33:45 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 9 May 2013 11:33:45 -0600 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Wed, May 8, 2013 at 8:35 PM, Mark Janssen wrote: > Okay, to anyone who might be listening, I found the core of the problem. What "problem" are you referring to? You've been posting on this topic for going on two months now, and I still have no idea of what the point of it all is. I recall something about not being happy with the OOP paradigm because apparently none of the dozens of existing languages handle message passing in the particular way that you think it should work; but if that's all it is, then why don't you just build a new language that does it the way that you think is right? If it's good, spread it around and it will gain traction. If not, then it will die the quiet death it deserves. All this irrelevant nonsense about Turing machines and lambda calculus that you've injected into the conversation though just reminds me of the "Einstein was wrong" cranks. From rosuav at gmail.com Thu May 9 13:47:26 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 03:47:26 +1000 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Fri, May 10, 2013 at 3:33 AM, Ian Kelly wrote: > All this irrelevant nonsense > about Turing machines and lambda calculus that you've injected into > the conversation though just reminds me of the "Einstein was wrong" > cranks. http://xkcd.com/1206/ ChrisA From darcy at druid.net Thu May 9 13:51:12 2013 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Thu, 9 May 2013 13:51:12 -0400 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: <20130509135112.3796431d@imp> On Thu, 9 May 2013 11:33:45 -0600 Ian Kelly wrote: > about Turing machines and lambda calculus that you've injected into > the conversation though just reminds me of the "Einstein was wrong" > cranks. But Einstein *was* wrong. http://www.xkcd.com/1206/ -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 788 2246 (DoD#0082) (eNTP) | what's for dinner. IM: darcy at Vex.Net, VOIP: sip:darcy at Vex.Net From dreamingforward at gmail.com Thu May 9 17:51:28 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Thu, 9 May 2013 14:51:28 -0700 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Thu, May 9, 2013 at 10:33 AM, Ian Kelly wrote: > On Wed, May 8, 2013 at 8:35 PM, Mark Janssen wrote: >> Okay, to anyone who might be listening, I found the core of the problem. > > What "problem" are you referring to? You've been posting on this > topic for going on two months now, and I still have no idea of what > the point of it all is. You see Ian, while you and the other millions of coding practitioners have (mal)adapted to a suboptimal coding environment where "hey there's a language for everyone" and terms are thrown around, misused, this is not how it needs or should be. Instead of the thriving Open Source culture on the web we could have, the community stays fractured. Languages can reach for an optimal design (within a constant margin of leeway). Language "expressivity" can be measured. --Tron From ian.g.kelly at gmail.com Thu May 9 18:30:47 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 9 May 2013 16:30:47 -0600 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Thu, May 9, 2013 at 3:51 PM, Mark Janssen wrote: > On Thu, May 9, 2013 at 10:33 AM, Ian Kelly wrote: >> On Wed, May 8, 2013 at 8:35 PM, Mark Janssen wrote: >>> Okay, to anyone who might be listening, I found the core of the problem. >> >> What "problem" are you referring to? You've been posting on this >> topic for going on two months now, and I still have no idea of what >> the point of it all is. > > You see Ian, while you and the other millions of coding practitioners > have (mal)adapted to a suboptimal coding environment where "hey > there's a language for everyone" and terms are thrown around, > misused, this is not how it needs or should be. Instead of the > thriving Open Source culture on the web we could have, Non sequitur. Open source software has nothing to do with coding environment or choice of language or OOP paradigm. Or Turing machines or lambda calculus, for that matter. > the community stays fractured. The open source community seems pretty healthy to me. What is the basis of your claim that it is "fractured"? > Languages can reach for an optimal design (within a constant margin of leeway). There is no "optimal design". The *reason* that "there's a language for everyone" is because different people think about software in different ways and find different approaches better suited to them. Furthermore, some programming styles are naturally more conducive to accomplishing certain tasks, and worse at others. Take video game programming for an example. If I'm working on the graphics engine for a game, I would probably want to use a low-level imperative language for efficiency reasons. If I'm working on the AI, I will more likely prefer a functional or declarative language for clarity, flexibility and static analysis. In either case, OOP is probably a bad choice. > Language "expressivity" can be measured. And the measurements can be endlessly debated. Expressivity is not the sole measure of a programming language, though. From rosuav at gmail.com Thu May 9 19:05:15 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 09:05:15 +1000 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Fri, May 10, 2013 at 8:30 AM, Ian Kelly wrote: > On Thu, May 9, 2013 at 3:51 PM, Mark Janssen wrote: >> the community stays fractured. > > The open source community seems pretty healthy to me. What is the > basis of your claim that it is "fractured"? The carpentry community is fractured. There are people who use screwdrivers, and there are people who use hammers! Screws and nails are such completely different things that we shouldn't try to use the same language to discuss them. >> Language "expressivity" can be measured. > > And the measurements can be endlessly debated. Expressivity is not > the sole measure of a programming language, though. Every programming language can encode every program. It's fairly straightforward to prove that a Python interpreter can be written in Ruby, or a C interpreter in Lua; so there is no program that can be expressed in one language and not in another (there will be apparent exceptions, eg web-browser JavaScript cannot call on arbitrary C-level functionality, but if the entirety of program code were written in C, then it could all be interpreted by one C interpreter). Larry Wall of Perl put it this way, in a State of the Onion address: http://www.perl.com/pub/2007/12/06/soto-11.html """... Human languages are Turing complete, as it were. Human languages therefore differ not so much in what you can say but in what you must say. In English, you are forced to differentiate singular from plural. In Japanese, you don't have to distinguish singular from plural, but you do have to pick a specific level of politeness, taking into account not only your degree of respect for the person you're talking to, but also your degree of respect for the person or thing you're talking about. So languages differ in what you're forced to say. Obviously, if your language forces you to say something, you can't be concise in that particular dimension using your language. Which brings us back to scripting. How many ways are there for different scripting languages to be concise? """ In C, for example, you are forced to write explicit notation representing {blocks; of; code;} and explicit characters separating; statements;. In Python, on the other hand, you have to write out your indentation. In Java, you state what exceptions you might throw. REXX mandates that you annotate procedures with their list of exposed names (effectively, non-local and global variables). So the expressivity of a language can't be defined in terms of how many programs can be written in it, but in how concisely they can be written - and that's something that depends on specific design choices and how they align with the code you're trying to write. Compare these two snippets: #!/bin/sh pg_dumpall | gzip | ssh user at host 'gzip -d|psql' #!/usr/bin/env python words=input("Enter words, blank delimited: ") lengths=[len(x) for x in words.split(" ")] print("Average word length: %d"%int(sum(lengths)/len(lengths))) Could you write each in the other's language? Sure! But it won't be as concise. (The Python version of the shell script could cheat and just call on the shell to do the work, but even that would add the expressive overhead of "os.system".) This is why there are so many languages: because each is good at something different. ChrisA From wuwei23 at gmail.com Thu May 9 19:58:15 2013 From: wuwei23 at gmail.com (alex23) Date: Thu, 9 May 2013 16:58:15 -0700 (PDT) Subject: Message passing syntax for objects | OOPv2 References: <5167F327.3030803@nedbatchelder.com> Message-ID: On 10 May, 07:51, Mark Janssen wrote: > You see Ian, while you and the other millions of coding practitioners > have (mal)adapted to a suboptimal coding environment where "hey > there's a language for everyone" ?and terms are thrown around, > misused, this is not how it needs or should be. Please cite your industry experience so we know this is a pragmatic exercise for you and not a display of public onanism. > Instead of the > thriving Open Source culture on the web we could have, the community > stays fractured. What fractures communities is telling "millions of [maladapted] practitioners" that they're wrong, and that your unsubstantiated intuition somehow trumps their billions of hours of combined experience. > Languages can reach for an optimal design (within a > constant margin of leeway). ? Language "expressivity" can be measured. I'm sure that's great. I, however, have a major project going live in a few weeks and would rather just get something done. From rosuav at gmail.com Thu May 9 23:07:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 13:07:55 +1000 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Fri, May 10, 2013 at 9:58 AM, alex23 wrote: > On 10 May, 07:51, Mark Janssen wrote: >> Languages can reach for an optimal design (within a >> constant margin of leeway). Language "expressivity" can be measured. > > I'm sure that's great. I, however, have a major project going live in > a few weeks and would rather just get something done. Hmm, not really a fair argument there. A well-designed language lets you "just get something done" far more efficiently than a poorly-designed one. Being confident that similar objects behave correspondingly when invoked the same way lets you write your code without fiddling with minutiae, for instance. ("Hmm, I'll just switch that from being a tuple to being a list, so I can modify this one element." - code that indexes or iterates won't be affected.) Now, whether or not it's worth _debating_ the expressiveness of a language... well, that's another point entirely. But for your major project, I think you'll do better working in Python than in machine code. ChrisA From dreamingforward at gmail.com Thu May 9 23:08:28 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Thu, 9 May 2013 20:08:28 -0700 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Thu, May 9, 2013 at 4:58 PM, alex23 wrote: > On 10 May, 07:51, Mark Janssen wrote: >> You see Ian, while you and the other millions of coding practitioners >> have (mal)adapted to a suboptimal coding environment where "hey >> there's a language for everyone" and terms are thrown around, >> misused, this is not how it needs or should be. > > Please cite your industry experience so we know this is a pragmatic > exercise for you and not a display of public onanism. "Industry experience...." Do you know all the world's [industrial] leaders are endorsing an impossible path of endless, exponential growth on a finite planet? Is that who you answer to? --m From rosuav at gmail.com Fri May 10 00:33:52 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 14:33:52 +1000 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Fri, May 10, 2013 at 1:08 PM, Mark Janssen wrote: > On Thu, May 9, 2013 at 4:58 PM, alex23 wrote: >> On 10 May, 07:51, Mark Janssen wrote: >>> You see Ian, while you and the other millions of coding practitioners >>> have (mal)adapted to a suboptimal coding environment where "hey >>> there's a language for everyone" and terms are thrown around, >>> misused, this is not how it needs or should be. >> >> Please cite your industry experience so we know this is a pragmatic >> exercise for you and not a display of public onanism. > > "Industry experience...." > > Do you know all the world's [industrial] leaders are endorsing an > impossible path of endless, exponential growth on a finite planet? > > Is that who you answer to? I don't answer to them. I also believe in a path of endless exponential growth. Challenge: Create more information than can be stored in one teaspoon of matter. Go ahead. Try! The first hard disk I ever worked with stored 20MB in the space of a 5.25" slot (plus its associated ISA controller card). Later on we got 3.5" form factor drives, and I remember installing this *gigantic* FOUR GIGABYTE drive into our disk server. Wow! We'll NEVER use all that space! (Well, okay. Even then we knew that space consumption kept going up. But we did figure on that 4GB lasting us a good while, which it did.) Today, I can pop into Budget PC or MSY (or you folks in the US could check out newegg) and pick up a terabyte of storage in the same amount of physical space, or you can go 2.5" form factor and take up roughly a fifth of the physical space and still get half a terabyte fairly cheaply. So our exponential growth is being supported by exponential increases in data per cubic meter. Between that and the vast size of this planet, I don't think we really need to worry too much about finite limits to IT growth. ChrisA From rosuav at gmail.com Sat May 11 18:41:00 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 May 2013 08:41:00 +1000 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: Message-ID: On Sun, May 12, 2013 at 5:32 AM, Dennis Lee Bieber wrote: > On Fri, 10 May 2013 14:33:52 +1000, Chris Angelico > declaimed the following in gmane.comp.python.general: > >> >> I don't answer to them. I also believe in a path of endless >> exponential growth. Challenge: Create more information than can be >> stored in one teaspoon of matter. Go ahead. Try! >> > The coordinates of each particle storing the information in that > teaspoon of matter. Which is probably more data than any of us will keyboard in a lifetime. Hence my point. ChrisA From greg.ewing at canterbury.ac.nz Sat May 11 21:02:09 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sun, 12 May 2013 13:02:09 +1200 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: Message-ID: Chris Angelico wrote: > On Sun, May 12, 2013 at 5:32 AM, Dennis Lee Bieber > wrote: >> The coordinates of each particle storing the information in that >>teaspoon of matter. > > Which is probably more data than any of us will keyboard in a > lifetime. Hence my point. My 1TB hard disk *already* contains more information than I could keyboard in my lifetime. The fact that it all got there is due to two things: (1) I didn't have to enter it all myself, and (2) most of it was auto-generated from other information, using compilers and other such tools. Our disk capacities are increasing exponentially, but so is the rate at which we have the ability to create information. I wouldn't be surprised if, at some point before the human race becomes extinct, we build computers whose operating system requires more than a teaspoonful of atoms to store. Especially if Microsoft still exists by then. :-) -- Greg From rosuav at gmail.com Sat May 11 21:19:11 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 May 2013 11:19:11 +1000 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: Message-ID: On Sun, May 12, 2013 at 11:02 AM, Gregory Ewing wrote: > Chris Angelico wrote: >> >> On Sun, May 12, 2013 at 5:32 AM, Dennis Lee Bieber >> wrote: > > >>> The coordinates of each particle storing the information in that >>> teaspoon of matter. >> >> >> Which is probably more data than any of us will keyboard in a >> lifetime. Hence my point. > > > My 1TB hard disk *already* contains more information than > I could keyboard in my lifetime. > > The fact that it all got there is due to two things: (1) > I didn't have to enter it all myself, and (2) most of it > was auto-generated from other information, using compilers > and other such tools. I would like to differentiate between information and data, here. Point 1 is correct, but point 2 is not; auto-generated data is not more information, and basic data compression can improve that. (Simple form of compression there: `rm *.o` - you've lost nothing.) > Our disk capacities are increasing exponentially, but > so is the rate at which we have the ability to create > information. I wouldn't be surprised if, at some point > before the human race becomes extinct, we build > computers whose operating system requires more than > a teaspoonful of atoms to store. Especially if > Microsoft still exists by then. :-) That's possible. But that would be data bloat, not true information. It's certainly possible to conceive more data than can be stored. Microsoft, as you cite, are experts at this :) ChrisA From greg.ewing at canterbury.ac.nz Sat May 11 20:50:38 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sun, 12 May 2013 12:50:38 +1200 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: Message-ID: Dennis Lee Bieber wrote: >>I also believe in a path of endless >>exponential growth. Challenge: Create more information than can be >>stored in one teaspoon of matter. Go ahead. Try! If that's your argument, then you don't really believe in *endless* exponential growth. You only believe in "exponential growth for long enough that I won't be around to suffer the consequences when it runs out". -- Greg From rosuav at gmail.com Sat May 11 21:10:36 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 May 2013 11:10:36 +1000 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: Message-ID: On Sun, May 12, 2013 at 10:50 AM, Gregory Ewing wrote: > Dennis Lee Bieber wrote: >>> >>> I also believe in a path of endless >>> exponential growth. Challenge: Create more information than can be >>> stored in one teaspoon of matter. Go ahead. Try! > > > If that's your argument, then you don't really believe > in *endless* exponential growth. You only believe in > "exponential growth for long enough that I won't be > around to suffer the consequences when it runs out". Technically, according to the laws of thermodynamics, there cannot be any actually endless growth, yes. (Anything beyond that is the realm of religion, not science.) But in that case, the term "endless" is like "infinity" - a concept only. Like the Infinite Monkey Protocol Suite description in RFC 2795, there will be many numbers that come up that are plenty huge but fall pitifully short of infinity (Graham's Number, for instance, is pretty small in those terms). So long as storage capacities keep on increasing, we can keep increasing the world's information at the same rate. So long as the number of computers connected to the internet increases, we can keep increasing the internet's information at the same rate. Put both together - and neither shows any sign of ceasing any time soon - we can continue with the corresponding growth. How long before that runs out? A *looooong* time. We're not talking here of the Year 2000, a couple of decades after the software was written. We're not talking about the 2038 issues, roughly half a century after the software was written. We are talking timeframes that make the Y10K problem look like a serious lack of foresight. ChrisA From roy at panix.com Fri May 10 00:55:29 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 00:55:29 -0400 Subject: Message passing syntax for objects | OOPv2 References: <5167F327.3030803@nedbatchelder.com> Message-ID: In article , Chris Angelico wrote: > The first hard disk I ever worked with stored 20MB in the space of a > 5.25" slot (plus its associated ISA controller card). Heh. The first hard disk I ever worked with stored 2.4 MB in 6U of rack space (plus 4 Unibus cards worth of controller). That's not actually the first hard disk I ever used. Just the first one I ever got to touch with my own hands. Did I mention that the air filters had to be changed a few times a year? Uphill both ways, in the snow, while beating off the dinosaurs with sticks. From rosuav at gmail.com Fri May 10 03:37:30 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 17:37:30 +1000 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On Fri, May 10, 2013 at 2:55 PM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> The first hard disk I ever worked with stored 20MB in the space of a >> 5.25" slot (plus its associated ISA controller card). > > Heh. The first hard disk I ever worked with stored 2.4 MB in 6U of rack > space (plus 4 Unibus cards worth of controller). That's not actually > the first hard disk I ever used. Just the first one I ever got to touch > with my own hands. > > Did I mention that the air filters had to be changed a few times a year? > > Uphill both ways, in the snow, while beating off the dinosaurs with > sticks. Yeah, I'm pretty young. First computer I ever broke (the same one that had the aforementioned 20MB drive) addressed a whole megabyte of memory, 640KB of which was OK (we're left wondering whether anyone would notice if ROM developed a fault), and I got to play around with 64KB of it in DEBUG.EXE. And yeah, I wrote code straight in DEBUG and saved it and crashed the system. (Tip: If you want to write a device driver, make sure you start with your dad happy with you.) Was good fun. I heartily recommend the exercise, but... uhh... do consider setting up a virtual machine first :) ChrisA From wrw at mac.com Fri May 10 07:49:11 2013 From: wrw at mac.com (William Ray Wing) Date: Fri, 10 May 2013 07:49:11 -0400 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: On May 10, 2013, at 12:55 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> The first hard disk I ever worked with stored 20MB in the space of a >> 5.25" slot (plus its associated ISA controller card). > > Heh. The first hard disk I ever worked with stored 2.4 MB in 6U of rack > space (plus 4 Unibus cards worth of controller). That's not actually > the first hard disk I ever used. Just the first one I ever got to touch > with my own hands. > Sounds suspiciously like an RK05. We used a lot of those on DEC PDP-8e's. I can remember how startled I was when I first saw a DEC engineer pull the top off one and then, with it open, spin it up. The platter inside was warped (a ceiling light reflected off the platter wiggled and then blurred) but the head mechanism actually just followed it up and down!!! Bill > Did I mention that the air filters had to be changed a few times a year? > > Uphill both ways, in the snow, while beating off the dinosaurs with > sticks. > -- > http://mail.python.org/mailman/listinfo/python-list From roy at panix.com Fri May 10 08:58:58 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 08:58:58 -0400 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: References: <5167F327.3030803@nedbatchelder.com> Message-ID: <3140EE72-3CE2-446F-B77B-066F7460BF25@panix.com> On May 10, 2013, at 7:49 AM, William Ray Wing wrote: > On May 10, 2013, at 12:55 AM, Roy Smith wrote: > >> In article , >> Chris Angelico wrote: >> >>> The first hard disk I ever worked with stored 20MB in the space of a >>> 5.25" slot (plus its associated ISA controller card). >> >> Heh. The first hard disk I ever worked with stored 2.4 MB in 6U of rack >> space (plus 4 Unibus cards worth of controller). That's not actually >> the first hard disk I ever used. Just the first one I ever got to touch >> with my own hands. >> > > Sounds suspiciously like an RK05. Yup. -- Roy Smith roy at panix.com From wuwei23 at gmail.com Sat May 11 18:23:17 2013 From: wuwei23 at gmail.com (alex23) Date: Sat, 11 May 2013 15:23:17 -0700 (PDT) Subject: Message passing syntax for objects | OOPv2 References: <5167F327.3030803@nedbatchelder.com> Message-ID: <965b6137-9a4f-4bd5-930b-8c242305dbd9@fq2g2000pbb.googlegroups.com> On 10 May, 13:07, Chris Angelico wrote: > Now, whether or not it's worth _debating_ the expressiveness of a > language... well, that's another point entirely. But for your major > project, I think you'll do better working in Python than in machine > code. I wasn't disagreeing with the concept of linguistic expressiveness, my ire was over the "I'm RIGHT and EVERYONE else is WRONG so STOP WHAT YOU'RE DOING so I can REBUILD COMPUTER SCIENCE" aspect of these posts. Thought without experience or experiment is about as useful to my work requirements as bowel gas. From wuwei23 at gmail.com Thu May 9 19:53:56 2013 From: wuwei23 at gmail.com (alex23) Date: Thu, 9 May 2013 16:53:56 -0700 (PDT) Subject: Message passing syntax for objects | OOPv2 References: <5167F327.3030803@nedbatchelder.com> Message-ID: On 10 May, 03:33, Ian Kelly wrote: > You've been posting on this > topic for going on two months now, and I still have no idea of what > the point of it all is. As Charlie Brooker put it: "almost every monologue consists of nothing but the words PLEASE AUTHENTICATE MY EXISTENCE, repeated over and over again, in disguise". From rustompmody at gmail.com Wed May 8 23:26:49 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 8 May 2013 20:26:49 -0700 (PDT) Subject: Message passing syntax for objects | OOPv2 References: <5167F327.3030803@nedbatchelder.com> Message-ID: <4558a365-4c27-4656-9cc9-9efbdec31e6a@pl9g2000pbb.googlegroups.com> On May 9, 7:35?am, Mark Janssen wrote: > On Fri, Apr 12, 2013 at 11:28 PM, Mark Janssen > > wrote: > >> Mark, this proposal is out of place on a Python list, because it proposes an > >> object methodology radically different from any that is implemented in > >> Python now, or is even remotely likely to be implemented in Python in the > >> future. > > > Wow, you guys are a bunch of ninnies. ?I'm going to find some > > theoretical folks.... > > Okay, to anyone who might be listening, I found the core of the problem. > > This issue is/was much deeper than OOP (which would be roughly a 20 > year refactoring) -- that was my mistake. ?The issue goes right to the > core to models of computation and the historical factions within > theoretical CS itself (a 50+ year refactoring). > > The field needs re-invented and re-centered. ?Mark my words. ?There > has been a half-century of confusion between two entirely separate > domains and they've been using the same lexicon. ?Long story short: > the lambda calculus folks have to split from the Turing machine folks. > ?These models of computation should not use the same language. ?Their > computation models are too radically different. ?Lisp will remain a > pinnacle of the lambda calculus, but should be remanded to philosophy. > ?The logic of the binary/boolean arithmetic is simply not compatible, > but forms the basis of any sensible computer science here in the West. > > Here pronouncith the.... whatever > > -- > MarkJ > Tacoma, Washington "Lisp will remain the pinnacle of lambda calculus" ??? : Surreal feeling of falling into a 25-year time-warp Read this http://www.cs.kent.ac.uk/people/staff/dat/miranda/wadler87.pdf Just for historical context: When this was written in the 80s: - The FP languages of the time -- KRC, SASL, Miranda, Orwell -- were elegant and academic - Lisp was quasi-industrial-strength but as Wadler argues above, was not doing good service to functional programming From dreamingforward at gmail.com Wed May 8 23:53:42 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Wed, 8 May 2013 20:53:42 -0700 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: <4558a365-4c27-4656-9cc9-9efbdec31e6a@pl9g2000pbb.googlegroups.com> References: <5167F327.3030803@nedbatchelder.com> <4558a365-4c27-4656-9cc9-9efbdec31e6a@pl9g2000pbb.googlegroups.com> Message-ID: > "Lisp will remain the pinnacle of lambda calculus" ??? : Surreal > feeling of falling into a 25-year time-warp > > Read this http://www.cs.kent.ac.uk/people/staff/dat/miranda/wadler87.pdf > > Just for historical context: > When this was written in the 80s: > - The FP languages of the time -- KRC, SASL, Miranda, Orwell -- were > elegant and academic > - Lisp was quasi-industrial-strength but as Wadler argues above, was > not doing good service to functional programming Fascinating. -- MarkJ Tacoma, Washington From steve+comp.lang.python at pearwood.info Thu May 9 01:39:37 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 05:39:37 GMT Subject: Message passing syntax for objects | OOPv2 References: <5167F327.3030803@nedbatchelder.com> Message-ID: <518b3699$0$11120$c3e8da3@news.astraweb.com> On Wed, 08 May 2013 19:35:58 -0700, Mark Janssen wrote: > Long story short: the lambda > calculus folks have to split from the Turing machine folks. > These models of computation should not use the same language. Their > computation models are too radically different. Their computation models are exactly equivalent. This is like saying that Cartesian coordinates and polar coordinates are so radically different that they cannot possibly both describe the same space. -- Steven From rustompmody at gmail.com Thu May 9 07:56:44 2013 From: rustompmody at gmail.com (rusi) Date: Thu, 9 May 2013 04:56:44 -0700 (PDT) Subject: Alternate computational models can be harmonious (was Message passing syntax for objects | OOPv2) References: <5167F327.3030803@nedbatchelder.com> <518b3699$0$11120$c3e8da3@news.astraweb.com> Message-ID: <294f7dda-3ead-47b5-a191-29556213d859@g5g2000pbp.googlegroups.com> On May 9, 10:39?am, Steven D'Aprano wrote: > On Wed, 08 May 2013 19:35:58 -0700, Mark Janssen wrote: > > Long story short: the lambda > > calculus folks have to split from the Turing machine folks. > > ?These models of computation should not use the same language. ?Their > > computation models are too radically different. > > Their computation models are exactly equivalent. > > This is like saying that Cartesian coordinates and polar coordinates are > so radically different that they cannot possibly both describe the same > space. Spot on Steven -- thanks. And further we do know that from a pragmatic POV the two can be quite different. For example cartesian are easier for add/subtract, whereas polar are easier for multiply/divide. And so on occasion the best way of doing an operation is to -- if necessary -- convert to the more appropriate format. I feel that the case of alternate computation models is analogous -- for some purposes one model works well and sometimes another. Python embeds the functional model almost as natively as it does the imperative/OO model. This is an aspect of python that is powerful but can also make it hard for some people. In short, python's multi- paradigm possibilities could do with some good publicity. My own attempts at bringing functional thinking to classical imperative languages and Python in particular, will be up at: https://moocfellowship.org/submissions/the-dance-of-functional-programming-languaging-with-haskell-and-python It is also an attempt at bringing the lightness and freedom of Python to the Haskell community and answer divisive judgements of computational models/paradigms such as the OP's. More details at http://blog.languager.org/2013/05/dance-of-functional-programming.html From dreamingforward at gmail.com Thu May 9 17:41:26 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Thu, 9 May 2013 14:41:26 -0700 Subject: Message passing syntax for objects | OOPv2 In-Reply-To: <518b3699$0$11120$c3e8da3@news.astraweb.com> References: <5167F327.3030803@nedbatchelder.com> <518b3699$0$11120$c3e8da3@news.astraweb.com> Message-ID: >> These models of computation should not use the same language. Their >> computation models are too radically different. > > Their computation models are exactly equivalent. No they are not. While one can find levels of indirection to translate between one and the other, that doesn't mean they're equivalent. It's like saying that because I can make an equivalence between the complex plane and the real, that they should be treated as equivalent. But they shouldn't -- else you run into a domain error. -- MarkJ Tacoma, Washington From cjw at ncf.ca Fri May 3 19:49:27 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Fri, 03 May 2013 19:49:27 -0400 Subject: [ANNC] pynguin-0.14 python turtle graphics application In-Reply-To: References: Message-ID: <51844D07.9000803@ncf.ca> On 13/04/2013 8:10 PM, Miki Tebeka wrote: >> Pynguin is a python-based turtle graphics application. > I wonder why Pynguin does not get more traction in the teaching sector. Looks ideal for teaching kids. Mili, I suggest that it's because Pynguin is not yet fully operational. I don't know where to report problems. The problems I had were installing for Python 3.3 and 2.7, using win32 In the latter case, it seem as though the code is tied with 3.3. On the face of it, the package appear to have potential. Colin W. From cjw at ncf.ca Fri May 3 19:49:27 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Fri, 03 May 2013 19:49:27 -0400 Subject: [ANNC] pynguin-0.14 python turtle graphics application In-Reply-To: References: Message-ID: <51844D07.9000803@ncf.ca> On 13/04/2013 8:10 PM, Miki Tebeka wrote: >> Pynguin is a python-based turtle graphics application. > I wonder why Pynguin does not get more traction in the teaching sector. Looks ideal for teaching kids. Mili, I suggest that it's because Pynguin is not yet fully operational. I don't know where to report problems. The problems I had were installing for Python 3.3 and 2.7, using win32 In the latter case, it seem as though the code is tied with 3.3. On the face of it, the package appear to have potential. Colin W. From jurgis.pralgauskis at gmail.com Fri May 3 12:01:17 2013 From: jurgis.pralgauskis at gmail.com (Jurgis Pralgauskis) Date: Fri, 3 May 2013 19:01:17 +0300 Subject: [Edu-sig] [ANNC] pynguin-0.14 python turtle graphics application In-Reply-To: References: Message-ID: Hi, do you plan to make step/debug functionality as it is made in RurPLE? I generally like RurPLE-NG http://dev.lshift.net/paul/rurple/ I improoved it fore easier learning http://grokbase.com/t/python/edu-sig/129r2hkchm/rurple-ng-is-nice#20121113xy5wryeowevmgumpyo6o3t6jka but moving around just rows and columns is a bit boring -- Would it be hard to somehow merge code tracking with Pynguin? On Sun, Apr 14, 2013 at 2:24 AM, Lee Harr wrote: > Pynguin is a python-based turtle graphics application. > It combines an editor, interactive interpreter, and > graphics display area. > > It is meant to be an easy environment for introducing > some programming concepts to beginning programmers. > > > http://pynguin.googlecode.com/ > > > This release adds several user-requested features, including > circular arcs, SVG export, and undo. > > > Pynguin is tested with Python 3.2.3 and PyQt 4.9.3 and > will use Pygments syntax highlighting if available. > > Pynguin is released under GPLv3. > > > Changes in pynguin-0.14: > Important fixes > > Other fixes > > Pynguin API > - Added font() to change font for use with write() > - Added move parameter for write() > - Added align and valign parameters for write() > - Added arc() for circular arcs > > Canvas > - Added SVG Export > > UI > - Added ability to use translations > - Added (basic) undo capability (ctrl-z) > > Integrated Editor > - Added shortcut (ctrl-w) for removing doc pages > - Stop asking for confirmation to remove empty doc page > - Stop autosave from modifying empty doc page > > Integrated Console > > Examples > - Added arc examples > _______________________________________________ > Edu-sig mailing list > Edu-sig at python.org > http://mail.python.org/mailman/listinfo/edu-sig > -- Jurgis Pralgauskis tel: 8-616 77613; Don't worry, be happy and make things better ;) http://galvosukykla.lt -------------- next part -------------- An HTML attachment was scrubbed... URL: From cjw at ncf.ca Fri May 3 19:53:28 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Fri, 03 May 2013 19:53:28 -0400 Subject: [Edu-sig] [ANNC] pynguin-0.14 python turtle graphics application In-Reply-To: References: Message-ID: <51844DF8.8010206@ncf.ca> On 03/05/2013 12:01 PM, Jurgis Pralgauskis wrote: > Hi, > > do you plan to make step/debug functionality > as it is made in RurPLE? > > I generally like RurPLE-NG > http://dev.lshift.net/paul/rurple/ > > I improoved it fore easier learning > http://grokbase.com/t/python/edu-sig/129r2hkchm/rurple-ng-is-nice#20121113xy5wryeowevmgumpyo6o3t6jka > > but moving around just rows and columns is a bit boring -- > Would it be hard to somehow merge code tracking with Pynguin? > > I tried to run it using PySCripter, but I didn't get far enough to check it out Colin W. [snip] From cjw at ncf.ca Fri May 3 19:53:28 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Fri, 03 May 2013 19:53:28 -0400 Subject: [Edu-sig] [ANNC] pynguin-0.14 python turtle graphics application In-Reply-To: References: Message-ID: <51844DF8.8010206@ncf.ca> On 03/05/2013 12:01 PM, Jurgis Pralgauskis wrote: > Hi, > > do you plan to make step/debug functionality > as it is made in RurPLE? > > I generally like RurPLE-NG > http://dev.lshift.net/paul/rurple/ > > I improoved it fore easier learning > http://grokbase.com/t/python/edu-sig/129r2hkchm/rurple-ng-is-nice#20121113xy5wryeowevmgumpyo6o3t6jka > > but moving around just rows and columns is a bit boring -- > Would it be hard to somehow merge code tracking with Pynguin? > > I tried to run it using PySCripter, but I didn't get far enough to check it out Colin W. [snip] From missive at hotmail.com Sat May 4 01:20:28 2013 From: missive at hotmail.com (Lee Harr) Date: Sat, 4 May 2013 09:50:28 +0430 Subject: [ANNC] pynguin-0.14 python turtle graphics application Message-ID: >>> Pynguin is a python-based turtle graphics application. >>> http://pynguin.googlecode.com/ >> I wonder why Pynguin does not get more traction in the teaching sector. >> Looks ideal for teaching kids. > I suggest that it's because Pynguin is not yet fully operational. > I don't know where to report problems. Go to the home page and click on "Issues" You can also email me directly. I appreciate any feedback. > The problems I had were installing for Python 3.3 and 2.7, using win32 > it seem as though the code is tied with 3.3 >>> ?Pynguin is tested with Python 3.2.3 and PyQt 4.9.3 For anyone struggling with installing on Windows, please see: https://code.google.com/p/pynguin/wiki/InstallingPynguinOnWindows I realize it is not simple to install on Windows, especially for the target audience, beginners. I have an open issue "Difficult to start the program on Windows" which could use more attention. The simplest way forward is to use Portable Python 2 and use the 0.12 version of Pynguin (the last one to support Python 2) It is also possible to run the latest version (0.14) on Portable Python 3 but it takes some experience to get PyQt installed in the right place. From dreamingforward at gmail.com Wed May 1 16:32:39 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Wed, 1 May 2013 13:32:39 -0700 Subject: The type/object distinction and possible synthesis of OOP and imperative programming languages In-Reply-To: <5170A77F.7040002@nedbatchelder.com> References: <516bd241$0$29872$c3e8da3$5496439d@news.astraweb.com> <516C3C44.6010706@rece.vub.ac.be> <51709740$0$29977$c3e8da3$5496439d@news.astraweb.com> <5170A77F.7040002@nedbatchelder.com> Message-ID: >> Here's a simple rule to resolve the ambiguity. Whoever publishes >> first, gets to claim origin of a word and its usage, kind of like a >> BDFL. The rest can adapt around that, make up their own word, or be >> corrected as the community requires. > > You seem to want to squeeze all of computer science and programming into a > tidy hierarchy. It won't work, it's not tidy. I strongly suggest you read > more about computer science before forming more opinions. You have a lot to > learn ahead of you. Done: see the wikiwikiweb: http://c2.com/cgi/wiki?ComputerScienceVersionTwo -- MarkJ Tacoma, Washington From wuwei23 at gmail.com Wed May 1 21:13:33 2013 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 May 2013 18:13:33 -0700 (PDT) Subject: The type/object distinction and possible synthesis of OOP and imperative programming languages References: <516bd241$0$29872$c3e8da3$5496439d@news.astraweb.com> <516C3C44.6010706@rece.vub.ac.be> <51709740$0$29977$c3e8da3$5496439d@news.astraweb.com> <5170A77F.7040002@nedbatchelder.com> Message-ID: <9fb4fe47-368f-4587-a30b-7b7336e913a4@mq5g2000pbb.googlegroups.com> On May 2, 6:32?am, Mark Janssen wrote: > > You seem to want to squeeze all of computer science and programming into a > > tidy hierarchy. ?It won't work, it's not tidy. I strongly suggest you read > > more about computer science before forming more opinions. ?You have a lot to > > learn ahead of you. > > Done: ?see the wikiwikiweb: ?http://c2.com/cgi/wiki?ComputerScienceVersionTwo "There are two camps: Us, who are right and good. Them, who are wrong and evil. Us should be supported, whilst Them should be condemned." Wow. Just....wow. Because the world really needs a manichean model of computation. From dieter at handshake.de Wed May 1 04:33:16 2013 From: dieter at handshake.de (dieter) Date: Wed, 01 May 2013 10:33:16 +0200 Subject: Finding referents with Gdb References: Message-ID: <87hain14mr.fsf@handshake.de> Dave Butler writes: > with gdb, can you find referents of an object given an object id? Usually no. "gdb" is a C level debugger. If debugging information is available (and the type of an object know), "gdb" can show you the fields of an object. If you know the correct type, you can cast an object to this type and get the field information. Thus, "gdb" gives you a possibility to examine the objects, a given object refers to. However, in general, there is no data structure (in a C program) that provides access to the objects that refer to a given object. In such cases, "gdb" cannot help you to find those objects. From stefan_ml at behnel.de Wed May 1 04:46:51 2013 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 01 May 2013 10:46:51 +0200 Subject: Finding referents with Gdb In-Reply-To: References: Message-ID: Dave Butler, 23.04.2013 16:52: > with gdb, can you find referents of an object given an object id? Have you looked at the gc module? And, could you explain why you want to work with the object's ID instead of the object reference itself? Stefan From skip at pobox.com Wed May 1 06:53:22 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 1 May 2013 05:53:22 -0500 Subject: Finding referents with Gdb In-Reply-To: References: Message-ID: On Tue, Apr 23, 2013 at 9:52 AM, Dave Butler wrote: > with gdb, can you find referents of an object given an object id? Look at the C code for gc.get_referents and set things up to call it from GDB. Skip From mikefromgreenburghnewyork at gmail.com Sat May 4 23:37:17 2013 From: mikefromgreenburghnewyork at gmail.com (Michael Marshall) Date: Sat, 4 May 2013 20:37:17 -0700 (PDT) Subject: Libroffice PMT equivalent in python In-Reply-To: References: Message-ID: On Thursday, April 25, 2013 6:46:01 AM UTC-4, ??????? wrote: > Hi, > > > > Are there equivalent in any Python libraries that could match function > > like PMT in libreoffice? > > > > Refer: https://help.libreoffice.org/Calc/Financial_Functions_Part_Two#PMT > > > > -- > > > > Amachu The PMT function in spreadsheet programs (Excel, OpenOffice Calc, LibreOffice Calc, Symphony Calc, Gnumeric) and in pocket financial calculators (Texas Instruments BA II plus, HP 10B II, Casio Fc 100, 200) will provide an answer for the initial (periodic) payment. Whereas depending on the value of number of periods NPER there may be 5 different payments as explained below 0) the periodic payment This is the payment that is in constant amount and is paid or collected over the life of investment assuming the number of periods are in whole and do not have a fractional part If the number of periods have a fractional part such as 10.5 years of 119.25 months then besides the initial periodic payment, there may be 4 different last payments to choose from 1) Balloon payment When the NPER has a fraction, then the last payment may be made or collected at the last full time period for example if NPER is 10.5 then the balloon payment is made at the end of year 10 where you pay the periodic payment plus the remaining principal and the interest due 2) Actual last payment When the NPER has a fraction, you may opt to pay or collect the last payment at the actual time period. For example if NPER is 10.5 then you will make or collect the last payment at the end of 10 years and 6 months. This last payment will be the principal remaining after making 10 payments and will also include the interest on remaining principal 3) Drop payment When the NPER has a fraction, you may opt to pay or collect the last payment at the end of the next full year. For example if NPER is 10.5 then you will make or collect the last payment at the end of 11 years. This last payment will be the principal remainining plus the interest on it 4) Partial period payment But then if the NPER has a fraction, you may still opt to pay or collect the payment at the actual time period but the amount you pay is the fraction of time period times the remaining principal. Thus if the NPER is 10.5 you make the last payment at the end of 10 years and 6 months in amount of 0.5 * remaining_principal Now the present value of the periodic payments plus the last payments from case 1, 2, 3 comes out to be the same as the loan amount. But this is not the case if we use payment from case number 4, in this instance the lender collects a few extra bucks from the borrower. Excel financial functions were copied from those found in Lotus 123 and the financial functions in OpenOffice Calc, LibreOffice Calc, Symphony Calc are copies from Excel. This Excel PMT function http://tadxl.com/excel_pmt_function.html offers the options of finding any of the 5 different payments as explained in the previous paragraphs. And it even goes further and finds all periodic payments when payments are growing, shrinking, increasing or decreasing. In this last case there may be as many different payments as there are number of periods. All this is part of tadXL add-in http://tadxl.com/ From mikefromgreenburghnewyork at gmail.com Sat May 4 23:55:07 2013 From: mikefromgreenburghnewyork at gmail.com (Michael Marshall) Date: Sat, 4 May 2013 20:55:07 -0700 (PDT) Subject: Libroffice PMT equivalent in python In-Reply-To: References: Message-ID: On Saturday, May 4, 2013 11:37:17 PM UTC-4, Michael Marshall wrote: > On Thursday, April 25, 2013 6:46:01 AM UTC-4, ??????? wrote: > If the number of periods have a fractional part such as 10.5 years of 119.25 months then besides the initial periodic payment, there may be 4 different last payments to choose from > EDIT There was a typo in my text I meant to write If the number of periods have a fractional part such as 10.5 years OR 119.25 months From mikefromgreenburghnewyork at gmail.com Sun May 5 00:05:16 2013 From: mikefromgreenburghnewyork at gmail.com (Michael Marshall) Date: Sat, 4 May 2013 21:05:16 -0700 (PDT) Subject: Libroffice PMT equivalent in python In-Reply-To: References: Message-ID: On Saturday, May 4, 2013 11:37:17 PM UTC-4, Michael Marshall wrote: > 4) Partial period payment > > But then if the NPER has a fraction, you may still opt to pay or collect the payment at the actual time period but the amount you pay is the fraction of time period times the remaining principal. Thus if the NPER is 10.5 you make the last payment at the end of 10 years and 6 months in amount of 0.5 * remaining_principal > EDIT Meant to write But then if the NPER has a fraction, you may still opt to pay or collect the payment at the actual time period but the amount you pay is the fraction of time period times the periodic payment. Thus if the NPER is 10.5 you make the last payment at the end of 10 years and 6 months in amount of 0.5 * periodic_payment From maitreymishra23 at gmail.com Thu May 2 10:13:48 2013 From: maitreymishra23 at gmail.com (Maitrey Mishra) Date: Thu, 2 May 2013 16:13:48 +0200 Subject: Efficient way of looging in python In-Reply-To: <229947553.5215691.1366982379397.JavaMail.root@sequans.com> References: <229947553.5215691.1366982379397.JavaMail.root@sequans.com> Message-ID: I wrote some tests for logging module. I use python 2.6 and use the example as in cookbook file : Test_Task.py from threading import Thread import Logging.logQueueHandler as logq class TestTask(threading.Thread): def __init__(self): pass def run(self): t = Thread(target=logq.log, args=("ERROR", "Hi")) t.start() q = Thread(target=logq.log, args=("ERROR", "Hello")) q.start() p = Thread(target=logq.log, args=("ERROR", "How are you?")) p.start() l = Thread(target=logq.log, args=("ERROR", "I am fine, Thank You!")) l.start() call_test.py from Test_Task import TestTask import unittest task = TestTask() task.run() However, In the log messages I see a number of messages missing. With the tests, my aim is to see how logging works with multiple threads. On Fri, Apr 26, 2013 at 3:19 PM, Jean-Michel Pichavant < jeanmichel at sequans.com> wrote: > Yep that's the one I used. > > ------------------------------ > > Ah, > http://plumberjack.blogspot.de/2010/09/using-logging-with-multiprocessing.html > I use this now, it seems to work! > Thanks! > > > On Fri, Apr 26, 2013 at 2:16 PM, Maitrey Mishra > wrote: > >> Hi All, >> Thanks for the inputs. >> I use python2.6. It seems to be easier with python3.2 and QueueHandler >> class. >> I pasted the class in my code however I still get the >> error: AttributeError: 'module' object has no attribute 'QueueHandler' >> What changes did you do for adapting to python2.5? >> >> Thanks! >> Br, >> Maitrey >> >> >> >> On Thu, Apr 25, 2013 at 4:32 PM, Jean-Michel Pichavant < >> jeanmichel at sequans.com> wrote: >> >>> ----- Original Message ----- >>> > Hi, >>> > >>> > I need an efficient way of logging using python. >>> > My problem statemnt: >>> > 1. I have multiple processes using the same logging file. >>> > I need solutions to the following: >>> > a) If multiple processes are trying to write to the same file, I need >>> > to prevent that. Otherwise, the logging messages will be puzzling to >>> > the user as he would see logging messages from one module than may >>> > be some other following it. >>> > >>> > Please let me know if there is an efficient way to do it. >>> > >>> > Thanks! >>> > >>> > Br, >>> > Maitrey >>> > -- >>> > http://mail.python.org/mailman/listinfo/python-list >>> >>> Everything you need should be explained here >>> >>> http://docs.python.org/release/3.2/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes >>> >>> Most of the concepts are applicable to any version of python I've done >>> it with python 2.5. I used some Vinay's code posted on the net to implement >>> a logging server, all my processes are logging to that server which is >>> responsible for writing the file. >>> >>> JM >>> >>> >>> -- IMPORTANT NOTICE: >>> >>> The contents of this email and any attachments are confidential and may >>> also be privileged. If you are not the intended recipient, please notify >>> the sender immediately and do not disclose the contents to any other >>> person, use it for any purpose, or store or copy the information in any >>> medium. Thank you. >>> >> >> > > > > -- IMPORTANT NOTICE: > > The contents of this email and any attachments are confidential and may > also be privileged. If you are not the intended recipient, please notify > the sender immediately and do not disclose the contents to any other > person, use it for any purpose, or store or copy the information in any > medium. Thank you. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From maitreymishra23 at gmail.com Thu May 2 10:33:56 2013 From: maitreymishra23 at gmail.com (Maitrey Mishra) Date: Thu, 2 May 2013 16:33:56 +0200 Subject: Efficient way of looging in python In-Reply-To: References: <229947553.5215691.1366982379397.JavaMail.root@sequans.com> Message-ID: Sometimes, I notice the below errors: python call_Test.py Traceback (most recent call last): File "call_Test.py", line 1, in from Test_Task import TestTask File "/cygdrive/d/smoketest/targetflasher/flasher_server/Source/Test_Task.py", line 5, in import Logging.logQueueHandler as logq File "/cygdrive/d/smoketest/targetflasher/flasher_server/Source/Logging/logQueueHandler.py", line 72, in queue = multiprocessing.Queue(-1) File "/usr/lib/python2.6/multiprocessing/__init__.py", line 213, in Queue return Queue(maxsize) File "/usr/lib/python2.6/multiprocessing/queues.py", line 37, in __init__ self._rlock = Lock() File "/usr/lib/python2.6/multiprocessing/synchronize.py", line 117, in __init__ SemLock.__init__(self, SEMAPHORE, 1, 1) File "/usr/lib/python2.6/multiprocessing/synchronize.py", line 49, in __init__ sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue) OSError: [Errno 17] File exists On Thu, May 2, 2013 at 4:13 PM, Maitrey Mishra wrote: > I wrote some tests for logging module. I use python 2.6 and use the > example as in cookbook > file : Test_Task.py > from threading import Thread > import Logging.logQueueHandler as logq > > class TestTask(threading.Thread): > def __init__(self): > pass > > def run(self): > t = Thread(target=logq.log, args=("ERROR", "Hi")) > t.start() > q = Thread(target=logq.log, args=("ERROR", "Hello")) > q.start() > p = Thread(target=logq.log, args=("ERROR", "How are you?")) > p.start() > l = Thread(target=logq.log, args=("ERROR", "I am fine, Thank You!")) > l.start() > call_test.py > from Test_Task import TestTask > import unittest > > task = TestTask() > task.run() > However, In the log messages I see a number of messages missing. > With the tests, my aim is to see how logging works with multiple threads. > > > On Fri, Apr 26, 2013 at 3:19 PM, Jean-Michel Pichavant < > jeanmichel at sequans.com> wrote: > >> Yep that's the one I used. >> >> ------------------------------ >> >> Ah, >> http://plumberjack.blogspot.de/2010/09/using-logging-with-multiprocessing.html >> I use this now, it seems to work! >> Thanks! >> >> >> On Fri, Apr 26, 2013 at 2:16 PM, Maitrey Mishra < >> maitreymishra23 at gmail.com> wrote: >> >>> Hi All, >>> Thanks for the inputs. >>> I use python2.6. It seems to be easier with python3.2 and QueueHandler >>> class. >>> I pasted the class in my code however I still get the >>> error: AttributeError: 'module' object has no attribute 'QueueHandler' >>> What changes did you do for adapting to python2.5? >>> >>> Thanks! >>> Br, >>> Maitrey >>> >>> >>> >>> On Thu, Apr 25, 2013 at 4:32 PM, Jean-Michel Pichavant < >>> jeanmichel at sequans.com> wrote: >>> >>>> ----- Original Message ----- >>>> > Hi, >>>> > >>>> > I need an efficient way of logging using python. >>>> > My problem statemnt: >>>> > 1. I have multiple processes using the same logging file. >>>> > I need solutions to the following: >>>> > a) If multiple processes are trying to write to the same file, I need >>>> > to prevent that. Otherwise, the logging messages will be puzzling to >>>> > the user as he would see logging messages from one module than may >>>> > be some other following it. >>>> > >>>> > Please let me know if there is an efficient way to do it. >>>> > >>>> > Thanks! >>>> > >>>> > Br, >>>> > Maitrey >>>> > -- >>>> > http://mail.python.org/mailman/listinfo/python-list >>>> >>>> Everything you need should be explained here >>>> >>>> http://docs.python.org/release/3.2/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes >>>> >>>> Most of the concepts are applicable to any version of python I've done >>>> it with python 2.5. I used some Vinay's code posted on the net to implement >>>> a logging server, all my processes are logging to that server which is >>>> responsible for writing the file. >>>> >>>> JM >>>> >>>> >>>> -- IMPORTANT NOTICE: >>>> >>>> The contents of this email and any attachments are confidential and may >>>> also be privileged. If you are not the intended recipient, please notify >>>> the sender immediately and do not disclose the contents to any other >>>> person, use it for any purpose, or store or copy the information in any >>>> medium. Thank you. >>>> >>> >>> >> >> >> >> -- IMPORTANT NOTICE: >> >> The contents of this email and any attachments are confidential and may >> also be privileged. If you are not the intended recipient, please notify >> the sender immediately and do not disclose the contents to any other >> person, use it for any purpose, or store or copy the information in any >> medium. Thank you. >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiosantosart at gmail.com Wed May 1 17:00:40 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 1 May 2013 22:00:40 +0100 Subject: "python.exe has stopped working" when os.execl() runs on Windows 7 In-Reply-To: <0d9d14dc-877b-4b68-aefe-834b530dee2c@googlegroups.com> References: <2e11de8b-f7e1-407f-a2fd-a08d5ce55d0b@googlegroups.com> <9ac24332-ead8-4889-815a-a12052f43742@googlegroups.com> <0d9d14dc-877b-4b68-aefe-834b530dee2c@googlegroups.com> Message-ID: Reproduced in Windows 7 Ultimate: >>> import os >>> os.execl('ping.exe', '') At this point the REPL freezes, and windows prompts me to close Python since it stopped responding. -- F?bio Santos From neilc at norwich.edu Thu May 2 08:50:09 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 2 May 2013 12:50:09 GMT Subject: "python.exe has stopped working" when os.execl() runs on Windows 7 References: <2e11de8b-f7e1-407f-a2fd-a08d5ce55d0b@googlegroups.com> <9ac24332-ead8-4889-815a-a12052f43742@googlegroups.com> <0d9d14dc-877b-4b68-aefe-834b530dee2c@googlegroups.com> Message-ID: On 2013-05-01, F?bio Santos wrote: > Reproduced in Windows 7 Ultimate: > >>>> import os >>>> os.execl('ping.exe', '') > > At this point the REPL freezes, and windows prompts me to close Python > since it stopped responding. To repeat others and the documentation: In either case, the arguments to the child process should start with the name of the command being run, but this is not enforced. The argument list must contain the executable as its first argument. Python indeed crashes if I don't include the first argument as directed. In any case, on Windows I would normally need to do something like this instead: os.execlp('cmd.exe', 'cmd.exe', '/K', 'ping.exe') -- Neil Cerutti From ned at nedbatchelder.com Wed May 1 18:01:39 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Wed, 01 May 2013 18:01:39 -0400 Subject: How do I encode and decode this data to write to a file? In-Reply-To: References: <27s15a-943.ln1@chris.zbmc.eu> Message-ID: <518190C3.8080708@nedbatchelder.com> On 5/1/2013 5:20 PM, Tony the Tiger wrote: > On Mon, 29 Apr 2013 10:47:46 +0100, cl wrote: > >> raw = os.path.join(directory, self.getNameNoExtension()) + >> ".html" >> file = open(raw, "w") >> file.write("".join(html).encode('utf-8')) >> file.close() > This works for me: > > Python 2.7.3 (default, Aug 1 2012, 05:16:07) > [GCC 4.6.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> html='Blah????' >>>> f=open('test.html', 'w') >>>> f.write(''.join(html.decode('utf-8').encode('utf-8'))) >>>> f.close() > Perhaps there are better ways to do it. Your .write() line is exactly equivalent to: f.write(html) Because: if X is a UTF-8 bytestring, then: X.decode('utf-8').encode('utf-8') == X And if X is a bytestring, then: ''.join(X) == X --Ned. > > /Grrr From ned at nedbatchelder.com Wed May 1 19:36:19 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Wed, 01 May 2013 19:36:19 -0400 Subject: How do I encode and decode this data to write to a file? In-Reply-To: <27s15a-943.ln1@chris.zbmc.eu> References: <27s15a-943.ln1@chris.zbmc.eu> Message-ID: <5181A6F3.3050800@nedbatchelder.com> On 4/29/2013 5:47 AM, cl at isbd.net wrote: > If I understand correctly the encode() is saying that it can't > understand the data in the html because there's a character 0xc3 in it. > I *think* this means that the ? is encoded in UTF-8 already in the > incoming data stream (should be as my system is wholly UTF-8 as far as I > know and I created the directory name). > > So how do I change the code so I don't get the error? Do I just > decode() the data first and then encode() it? > BTW, I did a presentation at PyCon 2012 that many people have found helpful: Pragmatic Unicode, or, How Do I Stop the Pain: http://nedbatchelder.com/text/unipain.html . It explains the principles at work here. --Ned. From Robert.Flintham at uhb.nhs.uk Wed May 1 04:06:41 2013 From: Robert.Flintham at uhb.nhs.uk (Robert Flintham) Date: Wed, 1 May 2013 09:06:41 +0100 Subject: Drag and drop in Windows In-Reply-To: <20130430231705.EB0BC44A84B@nhs-pd1e-esg008.ad1.nhs.net> References: <20130430231705.EB0BC44A84B@nhs-pd1e-esg008.ad1.nhs.net> Message-ID: <20130501080655.CEC2C4486FA@nhs-pd1e-esg102.ad1.nhs.net> Thanks Kevin, that looks great. It's having trouble finding TkDND though - is there a certain place in the "Python27" directory that it's most likely to look? It's currently under Lib/site-packages, but I'm suspicious that Tk/Tkinter has its own library somewhere. Christian - you were right. The TkDND DLL looks to be for x64. Is there a way around this? All the best, Rob -----Original Message----- From: Python-list [mailto:python-list-bounces+robert.flintham=uhb.nhs.uk at python.org] On Behalf Of Kevin Walzer Sent: 01 May 2013 00:10 To: python-list at python.org Subject: Re: Drag and drop in Windows Official link: http://tkinterdnd.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list DISCLAIMER: This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful. From auriocus at gmx.de Wed May 1 04:25:34 2013 From: auriocus at gmx.de (Christian Gollwitzer) Date: Wed, 01 May 2013 10:25:34 +0200 Subject: Drag and drop in Windows In-Reply-To: References: <20130430231705.EB0BC44A84B@nhs-pd1e-esg008.ad1.nhs.net> Message-ID: Hi Robert, Am 01.05.13 10:06, schrieb Robert Flintham: > Thanks Kevin, that looks great. It's having trouble finding TkDND > though - is there a certain place in the "Python27" directory that > it's most likely to look? It's currently under Lib/site-packages, > but I'm suspicious that Tk/Tkinter has its own library somewhere. Does it do a "package require?" In that case, check your auto path tk.eval('set auto_path') Tcl looks for the libs in the folders listed there. > Christian - you were right. The TkDND DLL looks to be for x64. Is > there a way around this? > Just download the right version: http://sourceforge.net/projects/tkdnd/files/Windows%20Binaries/TkDND%202.6/ I haven't checked the content, but expect the "*ix86.tar.gz" file to be 32 bit, whereas the "*x86_64.tar.gz" is 64 bit (despite the "win32" in the name). You can extract tar.gz using winzip for example. I think it is a mistake to package Windows binaries using tar.gz instead of ZIP. Christian From jimmie.he at gmail.com Sat May 4 06:26:48 2013 From: jimmie.he at gmail.com (Jimmie He) Date: Sat, 4 May 2013 03:26:48 -0700 (PDT) Subject: Can read in the BMP data correctly ,but the size is not right? In-Reply-To: References: Message-ID: <0b3223e6-2414-45a3-9493-3df54ee643d6@googlegroups.com> Totally right!Thanks again! From rosuav at gmail.com Wed May 1 00:37:34 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 1 May 2013 14:37:34 +1000 Subject: in need of some help... In-Reply-To: <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: On Wed, May 1, 2013 at 9:20 AM, Alex Norton wrote: > the teacher actually cant teach anything, he as the knowledge of Vb but his teaching methods are abysmal and severely lacking, but he said we can use any language we feel more comfortable in. some are using VB others PHP and some in C ++. Quit the course and go study someplace else... I wouldn't want to be in any course where people use VB and PHP to build GUIs! ChrisA From ayjayn1101 at gmail.com Wed May 1 06:27:50 2013 From: ayjayn1101 at gmail.com (Alex Norton) Date: Wed, 1 May 2013 03:27:50 -0700 (PDT) Subject: in need of some help... In-Reply-To: References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: On Wednesday, 1 May 2013 05:37:34 UTC+1, Chris Angelico wrote: > On Wed, May 1, 2013 at 9:20 AM, Alex Norton wrote: > > > the teacher actually cant teach anything, he as the knowledge of Vb but his teaching methods are abysmal and severely lacking, but he said we can use any language we feel more comfortable in. some are using VB others PHP and some in C ++. > > > > Quit the course and go study someplace else... I wouldn't want to be > > in any course where people use VB and PHP to build GUIs! > > > > ChrisA its a college course (Uk college btw) and its the last unit... im just gonna stick with it python is quite easy ot understand (reading wise) that the teacher can attempt to read it with my comments From jt at toerring.de Wed May 1 08:15:28 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 1 May 2013 12:15:28 GMT Subject: in need of some help... References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: Alex Norton wrote: > thanks... ill take a look at the Qt event handling It's rather simple: instead of the program running through a sequence of steps, the program normally is basically doing nothing. It just reacts to events that normally come from the user, i.e. the user clicks on some icon or widget, or (s)he enters something via the keyboard. You etermine which of all the possible events to which widget are relevant to you, write handler functions for them and tell the widget to call some function when an event happens. The simplest case is a button: you want to react to it, so you write a function for what's to be done when it's clicked on and then tell Qt to call that function once it gets clicked (there are different events even for a simple push button, it can be clicked, just pushed, released etc.). And if you have set up everything for that you tell Qt to start waiting for events. So the steps are: 1. Tell Qt that this is a program using it app = QtGui.QApplication( sys.argv ) 2. Create your graphical interface (what you seem to have done more or less) 3. Connect desired events (what's called "signals" in Qt lingo) for a certain widget to the function to be called with something like your_widget.clicked.connect( your_function ) (replace 'clicked' with e.g. 'pushed' or 'released' when interested in a push or release signal instead) 4. Start the event loop (i.e. have Qt wait for the user to do something and call one of your functions if the user did something you're interested in) with app.exec_( ) When this returns the game is over. So you don't wait for keyboard input with input() like in your original program but instead tell Qt to do the waiting for you and call the appropriate function you defined when something interesting happens. What you probably will have to change about the graphical interface is that instead of using QLabel widgets for 'Air', 'Earth', 'Fire', 'Water' to use e.g. QPushButtons since QLabels are rather static objects - they don't receive any "click" events and it's rather likely some kind of event like this is what you're going to want to react to. And for that QPushButtons seem to be the simplest choice to start with. So have an 'Air' button (let's call it 'bAir' and then do bAir.clicked.connect( air_clicked ) after defining a function air_clicked() in which you deal with that case. that might be as simple as def air_clicked( ) : # Randomly pick one of 'air', 'fire', 'water' or 'earth' z = [ 'air', 'fire', 'water', earth' ][ random.randrange( 4 ) ] if z == 'air' : print( 'Stalemate' ) elif z == 'water' : print( 'Air removes Water, you win!' ) ... Now, when during the game the 'Air' button is clicked this function will get called. Of course, it might be nicer to have a "result" label some- where in the graphical interface which you set to the text instead of printing it out to the console. And you also will probably add some "Quit" button to end the game. Regards, Jens -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From ayjayn1101 at gmail.com Wed May 1 19:24:16 2013 From: ayjayn1101 at gmail.com (Alex Norton) Date: Wed, 1 May 2013 16:24:16 -0700 (PDT) Subject: in need of some help... In-Reply-To: References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: <7f7c31dc-94cf-45d7-8556-a56d09293559@googlegroups.com> Thank you very much for the specific detail. I have already done the signal for the finish button so that the app closes when clicked From ayjayn1101 at gmail.com Fri May 10 04:23:40 2013 From: ayjayn1101 at gmail.com (Alex Norton) Date: Fri, 10 May 2013 01:23:40 -0700 (PDT) Subject: in need of some help... In-Reply-To: References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: On Wednesday, 1 May 2013 13:15:28 UTC+1, Jens Thoms Toerring wrote: > Alex Norton wrote: > > > thanks... ill take a look at the Qt event handling > > > > It's rather simple: instead of the program running through a > > sequence of steps, the program normally is basically doing > > nothing. It just reacts to events that normally come from > > the user, i.e. the user clicks on some icon or widget, or > > (s)he enters something via the keyboard. You etermine which > > of all the possible events to which widget are relevant to > > you, write handler functions for them and tell the widget > > to call some function when an event happens. The simplest > > case is a button: you want to react to it, so you write a > > function for what's to be done when it's clicked on and > > then tell Qt to call that function once it gets clicked > > (there are different events even for a simple push button, > > it can be clicked, just pushed, released etc.). And if you > > have set up everything for that you tell Qt to start waiting > > for events. > > > > So the steps are: > > > > 1. Tell Qt that this is a program using it > > > > app = QtGui.QApplication( sys.argv ) > > > > 2. Create your graphical interface (what you seem to > > have done more or less) > > > > 3. Connect desired events (what's called "signals" in > > Qt lingo) for a certain widget to the function to be > > called with something like > > > > your_widget.clicked.connect( your_function ) > > > > (replace 'clicked' with e.g. 'pushed' or 'released' > > when interested in a push or release signal instead) > > > > 4. Start the event loop (i.e. have Qt wait for the user > > to do something and call one of your functions if the > > user did something you're interested in) with > > > > app.exec_( ) > > > > When this returns the game is over. > > > > So you don't wait for keyboard input with input() like in > > your original program but instead tell Qt to do the waiting > > for you and call the appropriate function you defined when > > something interesting happens. > > > > What you probably will have to change about the graphical > > interface is that instead of using QLabel widgets for 'Air', > > 'Earth', 'Fire', 'Water' to use e.g. QPushButtons since > > QLabels are rather static objects - they don't receive any > > "click" events and it's rather likely some kind of event > > like this is what you're going to want to react to. And for > > that QPushButtons seem to be the simplest choice to start > > with. > > > > So have an 'Air' button (let's call it 'bAir' and then do > > > > bAir.clicked.connect( air_clicked ) > > > > after defining a function air_clicked() in which you deal > > with that case. that might be as simple as > > > > def air_clicked( ) : > > # Randomly pick one of 'air', 'fire', 'water' or 'earth' > > > > z = [ 'air', 'fire', 'water', earth' ][ random.randrange( 4 ) ] > > > > if z == 'air' : > > print( 'Stalemate' ) > > elif z == 'water' : > > print( 'Air removes Water, you win!' ) > > ... > > > > Now, when during the game the 'Air' button is clicked this > > function will get called. > > > > Of course, it might be nicer to have a "result" label some- > > where in the graphical interface which you set to the text > > instead of printing it out to the console. And you also will > > probably add some "Quit" button to end the game. > > > > Regards, Jens > > -- > > \ Jens Thoms Toerring ___ jt at toerring.de > > \__________________________ http://toerring.de how would i go about adding print outcomes of all options to a label ? From jt at toerring.de Sat May 11 18:20:13 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 11 May 2013 22:20:13 GMT Subject: in need of some help... References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: Alex Norton wrote: > On Wednesday, 1 May 2013 13:15:28 UTC+1, Jens Thoms Toerring wrote: > > Of course, it might be nicer to have a "result" label some- > > where in the graphical interface which you set to the text > > instead of printing it out to the console. And you also will > > probably add some "Quit" button to end the game. > > how would i go about adding print outcomes of all options to a label ? If you have a QLabel you can set its text to anything you want using its setText() method. Regaeds, Jens PS: If I may ask you a favor: consider refraining from using Google's completely broken interface to newsgroups - your post consists of nearly 200 lines of text containing all I wrote, with an empty line inserted between each of them, and a single line of text you wrote. It's rather annoying to have to sieve through that much of unrelated stuff just to find thar one line that's re- levant. And this Google groups crap seems to make it nearly impossible to do it any other way. If you don't believe me see e.g. http://wiki.python.org/moin/GoogleGroupsPython There are much better alternatives to "Google groups", using a real usenet news server and a program that does not mess up content of news group postings. They've been developed with 30 years of experience with newsgroups. If I'd be conspiracy theorist I would conclude that Google is up to something bad in trying to make using newsgroups nearly impossible by their badly broken stuff (and, to add credibility to such a claim, their complete disregard for all the criticism they got over the years, actually making each version of Google groups even worse), but it's rather likely just another case of pure incompetence (or a "why should we care" attitude:-( -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From kwpolska at gmail.com Sun May 12 11:59:54 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Sun, 12 May 2013 17:59:54 +0200 Subject: in need of some help... In-Reply-To: References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: (slightly offtopic, sorry.) On Sun, May 12, 2013 at 12:20 AM, Jens Thoms Toerring wrote: > PS: If I may ask you a favor: consider refraining from using Google's > completely broken interface to newsgroups - your post consists > of nearly 200 lines of text containing all I wrote, with an empty > line inserted between each of them, and a single line of text > you wrote. It's rather annoying to have to sieve through that > much of unrelated stuff just to find thar one line that's re- > levant. Gmail automatically hides long quotes. This is helpful in situations like this one. More mail software should implement that functionality. Seriously: once you go Gmail, you never go back. > And this Google groups crap seems to make it nearly > impossible to do it any other way. If you don't believe me see > e.g. > > http://wiki.python.org/moin/GoogleGroupsPython > > There are much better alternatives to "Google groups", > using a real usenet news server and a program that does > not mess up content of news group postings. They've been > developed with 30 years of experience with newsgroups. Or something even better: a mailing list. http://mail.python.org/mailman/listinfo/python-list is where you can find it. Much friendlier than Usenet, and the software itself is developed by the FLUFL. > If I'd be conspiracy theorist I would conclude that Google > is up to something bad in trying to make using newsgroups > nearly impossible by their badly broken stuff (and, to add > credibility to such a claim, their complete disregard for > all the criticism they got over the years, actually making > each version of Google groups even worse), but it's rather > likely just another case of pure incompetence (or a "why > should we care" attitude:-( They shouldn?t care because Usenet users often yell ?Get off my lawn!?. Young people don?t use newsgroups. They don?t even know what Usenet is. -- Kwpolska | GPG KEY: 5EAAEA16 stop html mail | always bottom-post http://asciiribbon.org | http://caliburn.nl/topposting.html From rosuav at gmail.com Sun May 12 12:08:58 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 02:08:58 +1000 Subject: in need of some help... In-Reply-To: References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: On Mon, May 13, 2013 at 1:59 AM, Chris ?Kwpolska? Warrick wrote: > On Sun, May 12, 2013 at 12:20 AM, Jens Thoms Toerring wrote: >> PS: If I may ask you a favor: consider refraining from using Google's >> completely broken interface to newsgroups - your post consists >> of nearly 200 lines of text containing all I wrote, with an empty >> line inserted between each of them, and a single line of text >> you wrote. It's rather annoying to have to sieve through that >> much of unrelated stuff just to find thar one line that's re- >> levant. > > Gmail automatically hides long quotes. This is helpful in situations > like this one. More mail software should implement that > functionality. Seriously: once you go Gmail, you never go back. I use Gmail, but the automated hiding of long quotes is *disrupted* by the double-spacing. So it's no less important to avoid breaking protocol. ChrisA From ayjayn1101 at gmail.com Sun May 12 15:34:24 2013 From: ayjayn1101 at gmail.com (Alex Norton) Date: Sun, 12 May 2013 12:34:24 -0700 (PDT) Subject: in need of some help... In-Reply-To: References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: On Saturday, 11 May 2013 23:20:13 UTC+1, Jens Thoms Toerring wrote: > Alex Norton wrote: > > > On Wednesday, 1 May 2013 13:15:28 UTC+1, Jens Thoms Toerring wrote: > > > > Of course, it might be nicer to have a "result" label some- > > > > where in the graphical interface which you set to the text > > > > instead of printing it out to the console. And you also will > > > > probably add some "Quit" button to end the game. > > > > > > how would i go about adding print outcomes of all options to a label ? > > > > If you have a QLabel you can set its text to anything you want > > using its setText() method. > > Regaeds, Jens > > > > PS: If I may ask you a favor: consider refraining from using Google's > > completely broken interface to newsgroups - your post consists > > of nearly 200 lines of text containing all I wrote, with an empty > > line inserted between each of them, and a single line of text > > you wrote. It's rather annoying to have to sieve through that > > much of unrelated stuff just to find thar one line that's re- > > levant. And this Google groups crap seems to make it nearly > > impossible to do it any other way. If you don't believe me see > > e.g. > > > > http://wiki.python.org/moin/GoogleGroupsPython > > > > There are much better alternatives to "Google groups", > > using a real usenet news server and a program that does > > not mess up content of news group postings. They've been > > developed with 30 years of experience with newsgroups. > > > > If I'd be conspiracy theorist I would conclude that Google > > is up to something bad in trying to make using newsgroups > > nearly impossible by their badly broken stuff (and, to add > > credibility to such a claim, their complete disregard for > > all the criticism they got over the years, actually making > > each version of Google groups even worse), but it's rather > > likely just another case of pure incompetence (or a "why > > should we care" attitude:-( > > -- > > \ Jens Thoms Toerring ___ jt at toerring.de > > \__________________________ http://toerring.de i have taken your advice and have messaged the mailing group about the issues i have From jt at toerring.de Sun May 12 17:56:38 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 12 May 2013 21:56:38 GMT Subject: in need of some help... References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: Chris ?Kwpolska? Warrick wrote: > (slightly offtopic, sorry.) > On Sun, May 12, 2013 at 12:20 AM, Jens Thoms Toerring wrote: > > PS: If I may ask you a favor: consider refraining from using Google's > > completely broken interface to newsgroups - your post consists > > of nearly 200 lines of text containing all I wrote, with an empty > > line inserted between each of them, and a single line of text > > you wrote. It's rather annoying to have to sieve through that > > much of unrelated stuff just to find thar one line that's re- > > levant. > Gmail automatically hides long quotes. This is helpful in situations > like this one. More mail software should implement that > functionality. Seriously: once you go Gmail, you never go back. i giess you mean Gougle groups and not Gmail, which I can't comment on since I don't use it. You still have to "un-hide" the message when you want to under- stand what the other person is respoding to. And then you're back to square one (and the double-spacing issue seems to remain). That is why it has been a good tradition to remove everything when you reply that isn't relevant to what you're replying to. Of course, with the sorry excuse for an editor you habe in a web based form that's more difficult to do than with a real editor. That's one of the reasons I would advice to use a specialized program that can be made to use the editor of your choice. Another extremly annoying thing when reading via Google groups is that there's no reasonable threading, i.e. it's not immediately obvious what is meant as a reply to a specific post. That makes Google groups basically useless for longer discussions. And then Google groups overflow with spam messages that any self-respecting news server would discard, so you never get to see them. > > And this Google groups crap seems to make it nearly > > impossible to do it any other way. If you don't believe me see > > e.g. > > > > http://wiki.python.org/moin/GoogleGroupsPython > > > > There are much better alternatives to "Google groups", > > using a real usenet news server and a program that does > > not mess up content of news group postings. They've been > > developed with 30 years of experience with newsgroups. > Or something even better: a mailing list. > http://mail.python.org/mailman/listinfo/python-list is where you can > find it. Much friendlier than Usenet, and the software itself is > developed by the FLUFL. Mailing lists are quite fine and I'm on quite a number of them. But I don't want many more to further fill up my inbox. And there you again have the problem that there's no reasonable threading of messages when these messages arrive in your mail folder. > > If I'd be conspiracy theorist I would conclude that Google > > is up to something bad in trying to make using newsgroups > > nearly impossible by their badly broken stuff (and, to add > > credibility to such a claim, their complete disregard for > > all the criticism they got over the years, actually making > > each version of Google groups even worse), but it's rather > > likely just another case of pure incompetence (or a "why > > should we care" attitude:-( > They shouldn?t care because Usenet users often yell ?Get off my > lawn!?. Young people don?t use newsgroups. They don?t even know what > Usenet is. Well, it's a pity when younger people don't use newsgroups (no idea if this is true, though) since they can be extremely useful for all kinds of purposes - I e.g. learned a huge amount from just lurking. And while some groups may be nicer than others, that's no excuse for Google at all to actively trying to destroy them. I'd rather prefer Google to simply get rid of Google groups and put the long time archives they obtained from DajaNews into better hands that do care. Regards, Jens -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From rosuav at gmail.com Sun May 12 18:18:05 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 08:18:05 +1000 Subject: in need of some help... In-Reply-To: References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: On Mon, May 13, 2013 at 7:56 AM, Jens Thoms Toerring wrote: > Chris ?Kwpolska? Warrick wrote: >> Gmail automatically hides long quotes. This is helpful in situations >> like this one. More mail software should implement that >> functionality. Seriously: once you go Gmail, you never go back. > > i giess you mean Gougle groups and not Gmail, which I can't comment > on since I don't use it. > No, Chris (not me, the other Chris... *an*other Chris.... okay, one of the chorus of Chrises of this list!) did mean Gmail, the Google webmail client. It does threading (and does it better than SquirrelMail does), and it does the hiding of long quotes, long signatures, etc. ChrisA From rosuav at gmail.com Sun May 12 20:51:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 10:51:38 +1000 Subject: in need of some help... In-Reply-To: References: <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: On Mon, May 13, 2013 at 9:47 AM, Dennis Lee Bieber wrote: > On Mon, 13 May 2013 08:18:05 +1000, Chris Angelico > declaimed the following in gmane.comp.python.general: > >> >> No, Chris (not me, the other Chris... *an*other Chris.... okay, one of >> the chorus of Chrises of this list!) did mean Gmail, the Google > > Is that the accepted group noun? I'd think a "crisis of Chrises" is > more alliterative... That works too! I automatically went for "chorus" since I work in theatre, but I like your version. What do the other Chrises think? ChrisA From greg.ewing at canterbury.ac.nz Mon May 13 04:10:30 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 13 May 2013 20:10:30 +1200 Subject: in need of some help... In-Reply-To: References: <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: Dennis Lee Bieber wrote: > Is that the accepted group noun? I'd think a "crisis of Chrises" is > more alliterative... A "confusion of Chrises" might be more appropriate in this case. -- Greg From jt at toerring.de Sun May 12 18:58:21 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 12 May 2013 22:58:21 GMT Subject: in need of some help... References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: Chris Angelico wrote: > On Mon, May 13, 2013 at 7:56 AM, Jens Thoms Toerring wrote: > > Chris ?Kwpolska? Warrick wrote: > >> Gmail automatically hides long quotes. This is helpful in situations > >> like this one. More mail software should implement that > >> functionality. Seriously: once you go Gmail, you never go back. > > > > i giess you mean Gougle groups and not Gmail, which I can't comment > > on since I don't use it. > No, Chris (not me, the other Chris... *an*other Chris.... okay, one of > the chorus of Chrises of this list!) did mean Gmail, the Google > webmail client. It does threading (and does it better than > SquirrelMail does), and it does the hiding of long quotes, long > signatures, etc. Ok, sorry then about that - as I said I never have used Gmail (and don't plan using it for other reasons than usability - and then I would hardly consider anything with a web interface for a text medium to be very usable;-). But, as far as I understand, Gmail is about email, so I'm a bit at a loss to understand what got this to do with news groups and Google groups (were the post I originally was responing to according to the header seemed to be coming from) that I intended this to be about? Best regards, Jens -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From rosuav at gmail.com Sun May 12 19:08:21 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 09:08:21 +1000 Subject: in need of some help... In-Reply-To: References: <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com> Message-ID: On Mon, May 13, 2013 at 8:58 AM, Jens Thoms Toerring wrote: > Chris Angelico wrote: >> On Mon, May 13, 2013 at 7:56 AM, Jens Thoms Toerring wrote: >> > Chris ?Kwpolska? Warrick wrote: >> >> Gmail automatically hides long quotes. This is helpful in situations >> >> like this one. More mail software should implement that >> >> functionality. Seriously: once you go Gmail, you never go back. >> > >> > i giess you mean Gougle groups and not Gmail, which I can't comment >> > on since I don't use it. > >> No, Chris (not me, the other Chris... *an*other Chris.... okay, one of >> the chorus of Chrises of this list!) did mean Gmail, the Google >> webmail client. It does threading (and does it better than >> SquirrelMail does), and it does the hiding of long quotes, long >> signatures, etc. > > Ok, sorry then about that - as I said I never have used Gmail > (and don't plan using it for other reasons than usability - and > then I would hardly consider anything with a web interface for a > text medium to be very usable;-). But, as far as I understand, > Gmail is about email, so I'm a bit at a loss to understand what > got this to do with news groups and Google groups (were the post > I originally was responing to according to the header seemed to > be coming from) that I intended this to be about? Since you can subscribe to the mailing list python-list at python.org and get all of comp.lang.python (sans a pile of spam), you can read it as a threaded mailing list instead of a newsgroup. It comes to pretty much the same thing, so effectively you get your choice of technology. ChrisA From razpeitia at gmail.com Wed May 1 02:26:06 2013 From: razpeitia at gmail.com (Ricardo Azpeitia Pimentel) Date: Wed, 1 May 2013 01:26:06 -0500 Subject: Why chunks is not part of the python standard lib? Message-ID: After reading How do you split a list into evenly sized chunks in Python? and seeing this kind of mistakes happening https://code.djangoproject.com/ticket/18972 all the time. Why is not a chunks function in itertools? grouper from http://docs.python.org/2/library/itertools.html#recipes doesn't have the same behavior as chunks Example: chunks([1, 2, 3, 4, 5], 3)# Should return [[1, 2, 3], [4, 5]] or the iterator equivalent. Original Post on StackOverflow: http://stackoverflow.com/questions/16313008/why-chunks-is-not-part-of-the-python-standard-lib -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed May 1 03:10:11 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 01 May 2013 08:10:11 +0100 Subject: Why chunks is not part of the python standard lib? In-Reply-To: References: Message-ID: On 01/05/2013 07:26, Ricardo Azpeitia Pimentel wrote: > After reading How do you split a list into evenly sized chunks in > Python? > > and seeing this kind of mistakes happening > https://code.djangoproject.com/ticket/18972 all the time. > > Why is not a |chunks| function in itertools? > > |grouper| from > http://docs.python.org/2/library/itertools.html#recipes doesn't have the > same behavior as |chunks | > > Example: > | > > |chunks([1, 2, 3, 4, 5], 3) > # Should return [[1, 2, 3], [4, 5]] or the iterator equivalent.| > > |Original Post on StackOverflow: > http://stackoverflow.com/questions/16313008/why-chunks-is-not-part-of-the-python-standard-lib > > Asked and answered a trillion times. There's no concensus on how chucks should behave. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From oscar.j.benjamin at gmail.com Wed May 1 05:00:04 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 1 May 2013 10:00:04 +0100 Subject: Why chunks is not part of the python standard lib? In-Reply-To: References: Message-ID: On 1 May 2013 08:10, Mark Lawrence wrote: > On 01/05/2013 07:26, Ricardo Azpeitia Pimentel wrote: >> >> After reading How do you split a list into evenly sized chunks in >> Python? >> >> >> >> and seeing this kind of mistakes happening >> https://code.djangoproject.com/ticket/18972 all the time. >> >> Why is not a |chunks| function in itertools? >> >> |grouper| from >> http://docs.python.org/2/library/itertools.html#recipes doesn't have the >> same behavior as |chunks | >> >> Example: >> | >> >> |chunks([1, 2, 3, 4, 5], 3) >> # Should return [[1, 2, 3], [4, 5]] or the iterator equivalent.| >> >> |Original Post on StackOverflow: >> >> http://stackoverflow.com/questions/16313008/why-chunks-is-not-part-of-the-python-standard-lib >> > > Asked and answered a trillion times. There's no concensus on how chucks > should behave. I'm not sure that's a valid argument against it since a chunks function could just do a different thing depending on the arguments given. The issue is around how to deal with the last chunk if it isn't the same length as the others and I can only think of 4 reasonable responses: 1) Yield a shorter chunk 2) Extend the chunk with fill values 3) Raise an error 4) Ignore the last chunk Cases 2 and 4 can be achieved with current itertools primitives e.g.: 2) izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n) 4) zip(*[iter(iterable)] * n) However I have only ever had use cases for 1 and 3 and these are not currently possible without something additional (e.g. a generator function). In any case a chunks function can simply take arguments to give all 4 behaviours: def chunks(iterable, chunksize, uneven='return_short', fillvalue=None): # loop through yielding all even chunks # and then if uneven == 'return_short: yield chunk elif uneven == 'raise': raise ValueError('No items left') elif uneven == 'fill': yield chunk + [fillvalue] * (chunksize - len(chunk)) elif uneven == 'ignore': pass Oscar From breamoreboy at yahoo.co.uk Wed May 1 09:06:15 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 01 May 2013 14:06:15 +0100 Subject: Why chunks is not part of the python standard lib? In-Reply-To: References: Message-ID: On 01/05/2013 10:00, Oscar Benjamin wrote: > On 1 May 2013 08:10, Mark Lawrence wrote: >> On 01/05/2013 07:26, Ricardo Azpeitia Pimentel wrote: >>> >>> After reading How do you split a list into evenly sized chunks in >>> Python? >>> >>> >>> >>> and seeing this kind of mistakes happening >>> https://code.djangoproject.com/ticket/18972 all the time. >>> >>> Why is not a |chunks| function in itertools? >>> >>> |grouper| from >>> http://docs.python.org/2/library/itertools.html#recipes doesn't have the >>> same behavior as |chunks | >>> >>> Example: >>> | >>> >>> |chunks([1, 2, 3, 4, 5], 3) >>> # Should return [[1, 2, 3], [4, 5]] or the iterator equivalent.| >>> >>> |Original Post on StackOverflow: >>> >>> http://stackoverflow.com/questions/16313008/why-chunks-is-not-part-of-the-python-standard-lib >>> >> >> Asked and answered a trillion times. There's no concensus on how chucks >> should behave. > > I'm not sure that's a valid argument against it since a chunks > function could just do a different thing depending on the arguments > given. > > The issue is around how to deal with the last chunk if it isn't the > same length as the others and I can only think of 4 reasonable > responses: > > 1) Yield a shorter chunk > 2) Extend the chunk with fill values > 3) Raise an error > 4) Ignore the last chunk > > Cases 2 and 4 can be achieved with current itertools primitives e.g.: > 2) izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n) > 4) zip(*[iter(iterable)] * n) > > However I have only ever had use cases for 1 and 3 and these are not > currently possible without something additional (e.g. a generator > function). > > In any case a chunks function can simply take arguments to give all 4 > behaviours: > > def chunks(iterable, chunksize, uneven='return_short', fillvalue=None): > # loop through yielding all even chunks > # and then > if uneven == 'return_short: > yield chunk > elif uneven == 'raise': > raise ValueError('No items left') > elif uneven == 'fill': > yield chunk + [fillvalue] * (chunksize - len(chunk)) > elif uneven == 'ignore': > pass > > > Oscar > All that's needed to get this into the standard library is to overcome this quote "This has been rejected before." from http://bugs.python.org/issue6021#msg87745 -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From steve+comp.lang.python at pearwood.info Thu May 2 01:15:38 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 May 2013 05:15:38 GMT Subject: Why chunks is not part of the python standard lib? References: Message-ID: <5181f679$0$29882$c3e8da3$5496439d@news.astraweb.com> On Wed, 01 May 2013 10:00:04 +0100, Oscar Benjamin wrote: > On 1 May 2013 08:10, Mark Lawrence wrote: >> On 01/05/2013 07:26, Ricardo Azpeitia Pimentel wrote: >>> >>> After reading How do you split a list into evenly sized chunks in >>> Python? >>> >>> >>> >>> and seeing this kind of mistakes happening >>> https://code.djangoproject.com/ticket/18972 all the time. That bug is irrelevant to the question about chunking a sequence or iterator. >>> Why is not a |chunks| function in itertools? [...] >> Asked and answered a trillion times. There's no concensus on how >> chucks should behave. > > I'm not sure that's a valid argument against it since a chunks function > could just do a different thing depending on the arguments given. Yes, but that's a rubbish API. I'd rather have five separate functions. Or maybe five methods on a single Chunker object. I think the real reasons it's not in the standard library are: - there's no consensus on what chunking should do; - hence whatever gets added will disappoint some people; - unless you add "all of them", in which case you've now got a significantly harder API ("there are five chunk functions in itertools, which should I use?"); - and none of them are actually very hard to write. So the prospect of adding chunks somewhere is unattractive: lots of angst for very little benefit. Yes, it could be done, but none of the Python developers, and especially not the itertools "owner", Raymond Hettinger, think that the added complication is worth the benefit. > The issue is around how to deal with the last chunk if it isn't the same > length as the others and I can only think of 4 reasonable responses: That's not the only issue. What does chunking (grouping) even mean? Given: chunk("abcdef", 3) should I get this? [abc, def] or this? [abc, bcd, cde, def] There are good use-cases for both. If given a string, should chunking re-join the individual characters into strings, or leave them as lists of chars? Tuples of chars? E.g. chunk("abcdef", 3) => "abc" ... or ["a", "b", "c"] ... How about bytes? I have opinions on these questions, but I'm not going to give them to you. The point is that chunking means different things to different people. If you write your own, you get to pick whatever behaviour you like, instead of trying to satisfy everyone. > 1) Yield a shorter chunk > 2) Extend the chunk with fill values > 3) Raise an error > 4) Ignore the last chunk > > Cases 2 and 4 can be achieved with current itertools primitives e.g.: 2) > izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n) 4) > zip(*[iter(iterable)] * n) > > However I have only ever had use cases for 1 and 3 and these are not > currently possible without something additional (e.g. a generator > function). All of these are trivial. Start with the grouper recipe from the itertools documentation, which is your case 2) above, renaming if desired: http://docs.python.org/2/library/itertools.html#recipes def chunk_pad(n, iterable, fillvalue=None): args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) Now define: def chunk_short(n, iterable): # Case 1) above sentinel = object() for chunk in chunk_pad(n, iterable, fillvalue=sentinel): if sentinel not in chunk: yield chunk else: i = chunk.index(sentinel) yield chunk[:i] def chunk_strict(n, iterable): # Case 3) above sentinel = object() for chunk in chunk_pad(n, iterable, fillvalue=sentinel): if sentinel in chunk: raise ValueError yield chunk def chunk(n, iterable): # Case 4) above args = [iter(iterable)]*n return izip(*args) def chunk_other(n, iterable): # I suck at thinking up names... it = iter(iterable) values = [next(it) for _ in range(n)] # What if this is short? while True: yield tuple(values) values.pop(0) try: values.append(next(it)) except StopIteration: break -- Steven From wolfgang.maier at biologie.uni-freiburg.de Thu May 2 04:53:50 2013 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Thu, 2 May 2013 08:53:50 +0000 (UTC) Subject: Why chunks is not part of the python standard lib? References: <5181f679$0$29882$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano pearwood.info> writes: > > > 1) Yield a shorter chunk > > 2) Extend the chunk with fill values > > 3) Raise an error > > 4) Ignore the last chunk > > > > Cases 2 and 4 can be achieved with current itertools primitives e.g.: 2) > > izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n) 4) > > zip(*[iter(iterable)] * n) > > > > However I have only ever had use cases for 1 and 3 and these are not > > currently possible without something additional (e.g. a generator > > function). > > All of these are trivial. Start with the grouper recipe from the itertools > documentation, which is your case 2) above, renaming if desired: > > http://docs.python.org/2/library/itertools.html#recipes > > def chunk_pad(n, iterable, fillvalue=None): > args = [iter(iterable)] * n > return izip_longest(fillvalue=fillvalue, *args) > > Now define: > > def chunk_short(n, iterable): # Case 1) above > sentinel = object() > for chunk in chunk_pad(n, iterable, fillvalue=sentinel): > if sentinel not in chunk: > yield chunk > else: > i = chunk.index(sentinel) > yield chunk[:i] > > def chunk_strict(n, iterable): # Case 3) above > sentinel = object() > for chunk in chunk_pad(n, iterable, fillvalue=sentinel): > if sentinel in chunk: > raise ValueError > yield chunk > These are only trivial on the surface. I brought up this topic on python-ideas just weeks ago and it turns out there's a surprising numbers of alternate solutions that people use for these two cases. Yours is straightforward and simple, but comes at the price of the if sentinel clause being checked repeatedly. An optimized version suggested by Peter Otten replaces your for loop by: chunk_pad = zip_longest(*args, fillvalue=fillvalue) prev = next(chunks) for chunk in chunk_pad: yield prev prev = chunk then doing the sentinel test only once at the end. >> 1) Yield a shorter chunk >> 2) Extend the chunk with fill values >> 3) Raise an error >> 4) Ignore the last chunk >> >> Cases 2 and 4 can be achieved with current itertools primitives e.g.: 2) >> izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n) 4) >> zip(*[iter(iterable)] * n) In my opinion, it would make sense to have the 4 cases suggested by Oscar covered by itertools. As he says, cases 2 and 4 are already (and there is the grouper recipe in itertools giving the solution for case 2). It would prevent people from reinventing (often suboptimal) solutions to these common problems and it would bring a speed-gain even compared to the best Python implementations since things would be coded in C. I would advocate for either of the following two solutions: a) have an extra 'mode'-type argument for zip_longest() to control its behavior (default mode could be the current fillvalue padding, 'strict' mode would raise an error, and 'relaxed' mode would yield the shorter chunk. or b) have extra zip_strict and zip_relaxed (I'm also not too good at thinking up names :)) functions in itertools Either way, you could now very easily modify the existing grouper recipe in itertools to implement the four different 'chunks' functions (I would keep calling them 'grouper' functions in line with the current itertools version). > I think the real reasons it's not in the standard library are: > > - there's no consensus on what chunking should do; > > - hence whatever gets added will disappoint some people; > > - unless you add "all of them", in which case you've now got a > significantly harder API ("there are five chunk functions in itertools, > which should I use?"); > > What does chunking (grouping) even mean? Given: > > chunk("abcdef", 3) > > should I get this? [abc, def] > > or this? [abc, bcd, cde, def] I guess this suggestion would not compromise the API too much, after all, all these zip versions would still behave like a zip() function should. Itertools users would also know what a 'grouper' recipe does, i.e., it doesn't do the fancier alternative stuff you suggest like rejoining groups of characters obtained from a string. So this would be a relatively conservative addition. What do you think? Wolfgang From breamoreboy at yahoo.co.uk Thu May 2 05:23:57 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 02 May 2013 10:23:57 +0100 Subject: Why chunks is not part of the python standard lib? In-Reply-To: References: <5181f679$0$29882$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 02/05/2013 09:53, Wolfgang Maier wrote: > > What do you think? > Wolfgang > It ain't gonna happen, unless somebody puts Raymond Hettinger in the Comfy Chair :) -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From oscar.j.benjamin at gmail.com Thu May 2 08:52:16 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 2 May 2013 13:52:16 +0100 Subject: Why chunks is not part of the python standard lib? In-Reply-To: <5181f679$0$29882$c3e8da3$5496439d@news.astraweb.com> References: <5181f679$0$29882$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2 May 2013 06:15, Steven D'Aprano wrote: > On Wed, 01 May 2013 10:00:04 +0100, Oscar Benjamin wrote: >> >> I'm not sure that's a valid argument against it since a chunks function >> could just do a different thing depending on the arguments given. > > Yes, but that's a rubbish API. I'd rather have five separate functions. > Or maybe five methods on a single Chunker object. Fair enough. [snip] > - and none of them are actually very hard to write. They are all easy to write as generator functions but to me the point of itertools is that you can do things more efficiently than a generator function. Otherwise code that uses a combination of itertools primitives is usually harder to understand than an equivalent generator function so I'd probably avoid using itertools. > So the prospect of adding chunks somewhere is unattractive: lots of angst > for very little benefit. Yes, it could be done, but none of the Python > developers, and especially not the itertools "owner", Raymond Hettinger, > think that the added complication is worth the benefit. It's not necessarily that chunks should be added but that it would be good if itertools had the necessary primitives to be able to achieve something like chunks without a generator function. As Wolfgang says zip() and zip_longest() are only 2 of the 4 obvious types of zipping and correspond to the 2 of the 4 obvious types of chunks() that can be done with itertools primitives. >> The issue is around how to deal with the last chunk if it isn't the same >> length as the others and I can only think of 4 reasonable responses: > > That's not the only issue. What does chunking (grouping) even mean? Given: > > chunk("abcdef", 3) > > should I get this? [abc, def] > > or this? [abc, bcd, cde, def] I don't think many people expect that. In any case that type of chunking is already possible using itertools. > There are good use-cases for both. > > If given a string, should chunking re-join the individual characters into > strings, or leave them as lists of chars? Tuples of chars? E.g. > > chunk("abcdef", 3) => "abc" ... > > or ["a", "b", "c"] ... > > How about bytes? It should be tuples or lists. Anything in itertools should just treat everything as iterators/iterables and not alter its behaviour for different types of sequences. >> 1) Yield a shorter chunk >> 2) Extend the chunk with fill values >> 3) Raise an error >> 4) Ignore the last chunk >> >> Cases 2 and 4 can be achieved with current itertools primitives e.g.: 2) >> izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n) 4) >> zip(*[iter(iterable)] * n) >> >> However I have only ever had use cases for 1 and 3 and these are not >> currently possible without something additional (e.g. a generator >> function). > > All of these are trivial. Start with the grouper recipe from the itertools > documentation, which is your case 2) above, renaming if desired: > > http://docs.python.org/2/library/itertools.html#recipes > [snip] > > def chunk_short(n, iterable): # Case 1) above > sentinel = object() > for chunk in chunk_pad(n, iterable, fillvalue=sentinel): > if sentinel not in chunk: The point is it would be good if you could avoid the check in the line above and the overhead of having an if statement and a generator frame between every next call. You can do better by writing the line above as if chunk[-1] is not sentinel but it is still a redundant check. zip_relaxed() would know when StopIteration was raised and wouldn't need to retrospectively check every chunk. > yield chunk > else: > i = chunk.index(sentinel) > yield chunk[:i] > > > def chunk_strict(n, iterable): # Case 3) above > sentinel = object() > for chunk in chunk_pad(n, iterable, fillvalue=sentinel): > if sentinel in chunk: if chunk[-1] is sentinel > raise ValueError > yield chunk > > > def chunk(n, iterable): # Case 4) above > args = [iter(iterable)]*n > return izip(*args) > > > def chunk_other(n, iterable): # I suck at thinking up names... > it = iter(iterable) > values = [next(it) for _ in range(n)] # What if this is short? > while True: > yield tuple(values) > values.pop(0) > try: > values.append(next(it)) > except StopIteration: > break You can do this one somewhat wastefully with itertools instead of a generator function: def chunk_other(n, iterable): iterators = tee(iterable, n) for n, it in enumerate(iterators): for _ in range(n): try: next(it) except StopIteration: return iter([]) return izip(*iterators) I don't know which of those two would be faster but it's certainly easier to understand the generator function. Oscar From rosuav at gmail.com Thu May 2 08:55:50 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 2 May 2013 22:55:50 +1000 Subject: Why chunks is not part of the python standard lib? In-Reply-To: References: <5181f679$0$29882$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, May 2, 2013 at 10:52 PM, Oscar Benjamin wrote: > They are all easy to write as generator functions but to me the point > of itertools is that you can do things more efficiently than a > generator function. Otherwise code that uses a combination of > itertools primitives is usually harder to understand than an > equivalent generator function so I'd probably avoid using itertools. Aren't most of the itertools primitives written in Python anyway? If your code is harder to understand, just write the generator function! ChrisA From oscar.j.benjamin at gmail.com Thu May 2 09:23:57 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 2 May 2013 14:23:57 +0100 Subject: Why chunks is not part of the python standard lib? In-Reply-To: References: <5181f679$0$29882$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2 May 2013 13:55, Chris Angelico wrote: > On Thu, May 2, 2013 at 10:52 PM, Oscar Benjamin > wrote: >> They are all easy to write as generator functions but to me the point >> of itertools is that you can do things more efficiently than a >> generator function. Otherwise code that uses a combination of >> itertools primitives is usually harder to understand than an >> equivalent generator function so I'd probably avoid using itertools. > > Aren't most of the itertools primitives written in Python anyway? If > your code is harder to understand, just write the generator function! The documentation describes them by showing equivalent generator functions and there may be a pure Python version of the module but if you look here then you can see which are builtin for CPython: http://hg.python.org/cpython/file/c3656dca65e7/Modules/itertoolsmodule.c#l4070 The list covers all of the documented itertools functions (actually I now realise that they're mostly types not functions). Oscar From wolfgang.maier at biologie.uni-freiburg.de Thu May 2 10:02:06 2013 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Thu, 2 May 2013 14:02:06 +0000 (UTC) Subject: Why chunks is not part of the python standard lib? References: <5181f679$0$29882$c3e8da3$5496439d@news.astraweb.com> Message-ID: Oscar Benjamin gmail.com> writes: > > On 2 May 2013 13:55, Chris Angelico gmail.com> wrote: > > On Thu, May 2, 2013 at 10:52 PM, Oscar Benjamin > > gmail.com> wrote: > >> They are all easy to write as generator functions but to me the point > >> of itertools is that you can do things more efficiently than a > >> generator function. Otherwise code that uses a combination of > >> itertools primitives is usually harder to understand than an > >> equivalent generator function so I'd probably avoid using itertools. > > > > Aren't most of the itertools primitives written in Python anyway? If > > your code is harder to understand, just write the generator function! > > The documentation describes them by showing equivalent generator > functions and there may be a pure Python version of the module but if > you look here then you can see which are builtin for CPython: > > http://hg.python.org/cpython/file/c3656dca65e7/Modules/itertoolsmodule.c#l4070 > > The list covers all of the documented itertools functions (actually I > now realise that they're mostly types not functions). > > Oscar > Maybe it's helpful to agree on terminology here. Looks like Oscar is using *itertools primitives* for things like zip_longest() - (I'm referring to Python3 here, where the i in zip_longest has disappeared and the former izip() has been replaced with the built-in zip()) - , which the docs call "a core set of fast, memory efficient tools that are useful by themselves or in combination" and that are listed in the doc's subsection .1 ("Itertools functions"). I think these are written in C (and explained by giving equivalent Python code). Then there are the *recipes* (subsection .2), which consist of Python code that combines and uses *primitves*. Among those is the *grouper recipe*, which is a chunks() doing fillvalue padding on the last chunk. It's obvious that you could write an analogous *grouper* that ignores a truncated last chunk by using zip instead of zip_longest. Now, Oscar's and my point is that by adding two more *primitives*, call them zip_strict and zip_relaxed or whatever you like, written in C and behaving analogous to zip_longest, you could write analogous *grouper recipes* that raise an error or simply yield when the last chunk is truncated. Those *recipes* would still have Python code of course, but they would return an iterator implemented in C. From storchaka at gmail.com Wed May 1 12:19:07 2013 From: storchaka at gmail.com (Serhiy Storchaka) Date: Wed, 01 May 2013 19:19:07 +0300 Subject: Why chunks is not part of the python standard lib? In-Reply-To: References: Message-ID: 01.05.13 09:26, Ricardo Azpeitia Pimentel ???????(??): > After reading How do you split a list into evenly sized chunks in > Python? > > and seeing this kind of mistakes happening > https://code.djangoproject.com/ticket/18972 all the time. > > Why is not a |chunks| function in itertools? Because not every 1 line function needs to be in the standard library. > |chunks([1, 2, 3, 4, 5], 3) > # Should return [[1, 2, 3], [4, 5]] or the iterator equivalent.| def chunks(seq, size): return [seq[i: i + size] for i in range(0, len(seq), size)] From no.email at nospam.invalid Wed May 1 12:30:19 2013 From: no.email at nospam.invalid (Paul Rubin) Date: Wed, 01 May 2013 09:30:19 -0700 Subject: Why chunks is not part of the python standard lib? References: Message-ID: <7xy5byacis.fsf@ruckus.brouhaha.com> Serhiy Storchaka writes: > def chunks(seq, size): > return [seq[i: i + size] for i in range(0, len(seq), size)] That's just for lists. An itertools version would work with arbitrary iterables. I've also had to rewrite that function more times than seems proper. From fabiosantosart at gmail.com Thu May 2 09:06:40 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 2 May 2013 14:06:40 +0100 Subject: Why chunks is not part of the python standard lib? In-Reply-To: References: Message-ID: > > Why is not a chunks function in itertools? > It is in more-itertools. It's not the standard library, but it's better than rolling your own every time you need it. -------------- next part -------------- An HTML attachment was scrubbed... URL: From sigzero at gmail.com Wed May 1 08:01:52 2013 From: sigzero at gmail.com (Robert) Date: Wed, 1 May 2013 05:01:52 -0700 (PDT) Subject: ANN: ActivePython 3.2.2.3 is now available In-Reply-To: References: Message-ID: <131044e2-3385-4cee-8019-48e52dc364b8@googlegroups.com> Will this be the last one? It has been two years. From kw at codebykevin.com Wed May 1 09:52:49 2013 From: kw at codebykevin.com (Kevin Walzer) Date: Wed, 01 May 2013 09:52:49 -0400 Subject: ANN: ActivePython 3.2.2.3 is now available In-Reply-To: <131044e2-3385-4cee-8019-48e52dc364b8@googlegroups.com> References: <131044e2-3385-4cee-8019-48e52dc364b8@googlegroups.com> Message-ID: On 5/1/13 8:01 AM, Robert wrote: > Will this be the last one? It has been two years. > Hard to say. AS has been focusing on cloud-based stuff lately. ActivePerl hasn't been updated for a long time either. ActiveTcl is still maintained. --Kevin -- Kevin Walzer Code by Kevin/Mobile Code by Kevin http://www.codebykevin.com http://www.wtmobilesoftware.com From J.Butler at albynschool.co.uk Wed May 1 10:52:51 2013 From: J.Butler at albynschool.co.uk (Jennifer Butler) Date: Wed, 1 May 2013 15:52:51 +0100 Subject: to a human - about 2to3 Message-ID: <3C8565F3435F024E9BB3B36BE06393D80120F355@albyn-exch.albyn-sch.local.co.uk> Hiya I have trawled around your various pages and haven't found what I want. I will start teaching Python to my pupils shortly. I have been looking for materials and have gathered a collection of programs. The problem is they are written in v2 and I have v3 installed in my classroom. I read about the 2to3 conversion program, but I can't get it to work. Could you possibly give me a Noddy's guide to do this? Thanks in anticipation. Jennifer J -------------- next part -------------- An HTML attachment was scrubbed... URL: From paul.nospam at rudin.co.uk Wed May 1 11:11:17 2013 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 01 May 2013 16:11:17 +0100 Subject: to a human - about 2to3 References: Message-ID: <87bo8urazu.fsf@no-fixed-abode.cable.virginmedia.net> "Jennifer Butler" writes: > Hiya > > I have trawled around your various pages and haven?t found what I > want. > > I will start teaching Python to my pupils shortly. I have been looking > for materials and have gathered a collection of programs. The problem > is they are written in v2 and I have v3 installed in my classroom. I > read about the 2to3 conversion program, but I can?t get it to work. > > Could you possibly give me a Noddy?s guide to do this? Perhaps the first thing you should teach your students is how to ask questions :) When you say you "can't get it to work" what does that mean? What exactly did you do? What happened as a result? How did that differ from what you expected to happen or hoped would happen? Can you construct a minimal specific example that illustrates your problem? From rosuav at gmail.com Wed May 1 11:25:50 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 2 May 2013 01:25:50 +1000 Subject: to a human - about 2to3 In-Reply-To: <87bo8urazu.fsf@no-fixed-abode.cable.virginmedia.net> References: <87bo8urazu.fsf@no-fixed-abode.cable.virginmedia.net> Message-ID: On Thu, May 2, 2013 at 1:11 AM, Paul Rudin wrote: > "Jennifer Butler" writes: > >> Hiya >> >> I have trawled around your various pages and haven?t found what I >> want. >> >> I will start teaching Python to my pupils shortly. I have been looking >> for materials and have gathered a collection of programs. The problem >> is they are written in v2 and I have v3 installed in my classroom. I >> read about the 2to3 conversion program, but I can?t get it to work. >> >> Could you possibly give me a Noddy?s guide to do this? > > > Perhaps the first thing you should teach your students is how to ask > questions :) Specifically, you and your students would do well spending a couple of sessions here: http://www.catb.org/esr/faqs/smart-questions.html 2to3 has fairly good --help information. In my porting projects, I started there and worked forward. Seemed to go fairly well. ChrisA From mail at timgolden.me.uk Wed May 1 11:27:02 2013 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 01 May 2013 16:27:02 +0100 Subject: to a human - about 2to3 In-Reply-To: <3C8565F3435F024E9BB3B36BE06393D80120F355@albyn-exch.albyn-sch.local.co.uk> References: <3C8565F3435F024E9BB3B36BE06393D80120F355@albyn-exch.albyn-sch.local.co.uk> Message-ID: <51813446.3000703@timgolden.me.uk> On 01/05/2013 15:52, Jennifer Butler wrote: > I will start teaching Python to my pupils shortly. I have been looking > for materials and have gathered a collection of programs. The problem is > they are written in v2 and I have v3 installed in my classroom. I read > about the 2to3 conversion program, but I can?t get it to work. > > Could you possibly give me a Noddy?s guide to do this? This might be useful: http://python3porting.com/2to3.html although it's not clear from your question whether you're competent in Python and just a little unclear on 2to3 or coming new to the whole thing and hoping that 2to3 will give you a pain-free conversion. If it's the latter then I'm afraid your ride may be rockier than you hoped. :( Also, I note that you're UK-based (albeit at the other end of it from where I am). Depending on where this thread goes you might get some local help by posting to the python-uk mailing list: http://mail.python.org/mailman/listinfo/python-uk It's a quiet list, mostly for local events and jobs, but you might find someone in your neighbourhood (or at least in your own country) who could give you some help. TJG From breamoreboy at yahoo.co.uk Wed May 1 13:49:21 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 01 May 2013 18:49:21 +0100 Subject: to a human - about 2to3 In-Reply-To: <3C8565F3435F024E9BB3B36BE06393D80120F355@albyn-exch.albyn-sch.local.co.uk> References: <3C8565F3435F024E9BB3B36BE06393D80120F355@albyn-exch.albyn-sch.local.co.uk> Message-ID: On 01/05/2013 15:52, Jennifer Butler wrote: > Hiya > > I have trawled around your various pages and haven?t found what I want. > > I will start teaching Python to my pupils shortly. I have been looking > for materials and have gathered a collection of programs. The problem is > they are written in v2 and I have v3 installed in my classroom. I read > about the 2to3 conversion program, but I can?t get it to work. > > Could you possibly give me a Noddy?s guide to do this? > > Thanks in anticipation. > > Jennifer J > Here's the help output. I'd guess (as you don't specify exactly what doesn't work, 0/10, must try harder :) that you haven't specified -w, --write in which case the files don't get modified. c:\Users\Mark\MyPython>2to3 --help Usage: 2to3 [options] file|dir ... Options: -h, --help show this help message and exit -d, --doctests_only Fix up doctests only -f FIX, --fix=FIX Each FIX specifies a transformation; default: all -j PROCESSES, --processes=PROCESSES Run 2to3 concurrently -x NOFIX, --nofix=NOFIX Prevent a transformation from being run -l, --list-fixes List available transformations -p, --print-function Modify the grammar so that print() is a function -v, --verbose More verbose logging --no-diffs Don't show diffs of the refactoring -w, --write Write back modified files -n, --nobackups Don't write backups for modified files -o OUTPUT_DIR, --output-dir=OUTPUT_DIR Put output files in this directory instead of overwriting the input files. Requires -n. -W, --write-unchanged-files Also write files even if no changes were required (useful with --output-dir); implies -w. --add-suffix=ADD_SUFFIX Append this string to all output filenames. Requires -n if non-empty. ex: --add-suffix='3' will generate .py3 files. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From tjreedy at udel.edu Wed May 1 14:59:06 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Wed, 01 May 2013 14:59:06 -0400 Subject: to a human - about 2to3 In-Reply-To: <3C8565F3435F024E9BB3B36BE06393D80120F355@albyn-exch.albyn-sch.local.co.uk> References: <3C8565F3435F024E9BB3B36BE06393D80120F355@albyn-exch.albyn-sch.local.co.uk> Message-ID: On 5/1/2013 10:52 AM, Jennifer Butler wrote: > I will start teaching Python to my pupils shortly. I have been looking > for materials and have gathered a collection of programs. The problem is > they are written in v2 and I have v3 installed in my classroom. I read > about the 2to3 conversion program, but I can?t get it to work. You might find this page http://www.python.org/community/sigs/current/edu-sig/ and the education list itself. I am sure many programs useful in education have already been ported. Many simple programs, of the sort used in beginner classes need minimal change. Oftem 'print x' to 'print(x)' is all that is needed. 2to3 will do that for you, once you learn how to use it, but I have not. For the programs people post on the list, with just 1 or 2 prints, I make the change manually in the Idle editor before running. I often want to change other things as well, so no big deal. From drsalists at gmail.com Wed May 1 19:11:12 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 1 May 2013 16:11:12 -0700 Subject: Red Black Tree implementation? Message-ID: What's the best Red Black Tree implementation for Python with an opensource license? I started out looking at http://newcenturycomputers.net/projects/rbtree.htmlbecause it was pretty high in Google and had the operators I wanted, but it gets very slow at about half a million elements. I've been discussing this with a C programmer who believes that Red Black Trees should perform very similarly to an AVL tree, but that's not at all what I'm getting with the newcenturycomputers implementation. I'd prefer something that looks like a dictionary, runs on 2.x and 3.x, and passes pylint, but if that's not yet available I might make it so. This is part of a comparison of Python tree types I did a while back... I've been thinking that I've given Red Black Trees short shrift by using a poor implementation. The comparison so far is at http://stromberg.dnsalias.org/~strombrg/python-tree-and-heap-comparison/ Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From buzzard at invalid.invalid Wed May 1 22:06:04 2013 From: buzzard at invalid.invalid (duncan smith) Date: Thu, 02 May 2013 03:06:04 +0100 Subject: Red Black Tree implementation? In-Reply-To: References: Message-ID: <5181ca0e$0$13974$862e30e2@ngroups.net> On 02/05/13 00:11, Dan Stromberg wrote: > > What's the best Red Black Tree implementation for Python with an > opensource license? > > I started out looking at > http://newcenturycomputers.net/projects/rbtree.html because it was > pretty high in Google and had the operators I wanted, but it gets very > slow at about half a million elements. I've been discussing this with a > C programmer who believes that Red Black Trees should perform very > similarly to an AVL tree, but that's not at all what I'm getting with > the newcenturycomputers implementation. > > I'd prefer something that looks like a dictionary, runs on 2.x and 3.x, > and passes pylint, but if that's not yet available I might make it so. > > This is part of a comparison of Python tree types I did a while back... > I've been thinking that I've given Red Black Trees short shrift by using > a poor implementation. The comparison so far is at > http://stromberg.dnsalias.org/~strombrg/python-tree-and-heap-comparison/ > > Thanks! > > I have an implementation that you can try out. It's not based on any other implementation, so my bugs will be independent of any bugs in the code you're currently using. It looks more like a set - add, remove, discard. Not tried on Python 3 or run through pylint. I just tried adding a million items to a tree, and it takes about 25% longer to add items at the end compared to those at the beginning. Timing removals uncovered a bug. So if you want the code I'll fix the bug and send it (to your gmail e-mail address?). Cheers. Duncan From drsalists at gmail.com Thu May 2 22:00:09 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Thu, 2 May 2013 19:00:09 -0700 Subject: Red Black Tree implementation? In-Reply-To: <5181ca0e$0$13974$862e30e2@ngroups.net> References: <5181ca0e$0$13974$862e30e2@ngroups.net> Message-ID: On Wed, May 1, 2013 at 7:06 PM, duncan smith wrote: > I have an implementation that you can try out. It's not based on any other > implementation, so my bugs will be independent of any bugs in the code > you're currently using. It looks more like a set - add, remove, discard. > Not tried on Python 3 or run through pylint. I just tried adding a million > items to a tree, and it takes about 25% longer to add items at the end > compared to those at the beginning. Timing removals uncovered a bug. So if > you want the code I'll fix the bug and send it (to your gmail e-mail > address?). Cheers. > > Duncan > -- > http://mail.python.org/**mailman/listinfo/python-list > What license? Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From buzzard at invalid.invalid Mon May 6 20:55:20 2013 From: buzzard at invalid.invalid (duncan smith) Date: Tue, 07 May 2013 01:55:20 +0100 Subject: Red Black Tree implementation? In-Reply-To: References: <5181ca0e$0$13974$862e30e2@ngroups.net> Message-ID: <518850f8$0$8157$862e30e2@ngroups.net> On 03/05/13 03:00, Dan Stromberg wrote: > > On Wed, May 1, 2013 at 7:06 PM, duncan smith > wrote: > > I have an implementation that you can try out. It's not based on any > other implementation, so my bugs will be independent of any bugs in > the code you're currently using. It looks more like a set - add, > remove, discard. Not tried on Python 3 or run through pylint. I just > tried adding a million items to a tree, and it takes about 25% > longer to add items at the end compared to those at the beginning. > Timing removals uncovered a bug. So if you want the code I'll fix > the bug and send it (to your gmail e-mail address?). Cheers. > > Duncan > -- > http://mail.python.org/__mailman/listinfo/python-list > > > > What license? > > Thanks! > Here's the text I usually prepend. ##Copyright (c) 2013 duncan g. smith ## ##Permission is hereby granted, free of charge, to any person obtaining a ##copy of this software and associated documentation files (the "Software"), ##to deal in the Software without restriction, including without limitation ##the rights to use, copy, modify, merge, publish, distribute, sublicense, ##and/or sell copies of the Software, and to permit persons to whom the ##Software is furnished to do so, subject to the following conditions: ## ##The above copyright notice and this permission notice shall be included ##in all copies or substantial portions of the Software. ## ##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ##OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ##THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR ##OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ##ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR ##OTHER DEALINGS IN THE SOFTWARE. Basically, "do what you want with it but don't blame me if it goes tits up". I'm happy to consider tidying it up a bit and using a more recognized form of licence. Just had a bank holiday here, so bug not yet squashed. But it is the sort of bug that might account for what you've seen (if a similar bug exists in the code you've been using). The tree doesn't always get properly rebalanced on node removals. I'll attack the problem later tomorrow (technically, later today). Cheers. Duncan From drsalists at gmail.com Mon May 6 21:20:08 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Mon, 6 May 2013 18:20:08 -0700 Subject: Red Black Tree implementation? In-Reply-To: <518850f8$0$8157$862e30e2@ngroups.net> References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> Message-ID: On Mon, May 6, 2013 at 5:55 PM, duncan smith wrote: > > What license? >> >> Thanks! >> >> > Here's the text I usually prepend. > > > ##Copyright (c) 2013 duncan g. smith > ## > ##Permission is hereby granted, free of charge, to any person obtaining a > ##copy of this software and associated documentation files (the > "Software"), > ##to deal in the Software without restriction, including without limitation > ##the rights to use, copy, modify, merge, publish, distribute, sublicense, > ##and/or sell copies of the Software, and to permit persons to whom the > ##Software is furnished to do so, subject to the following conditions: > ## > ##The above copyright notice and this permission notice shall be included > ##in all copies or substantial portions of the Software. > ## > ##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS > ##OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF > MERCHANTABILITY, > ##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL > ##THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR > ##OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, > ##ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR > ##OTHER DEALINGS IN THE SOFTWARE. > > > Basically, "do what you want with it but don't blame me if it goes tits > up". I'm happy to consider tidying it up a bit and using a more recognized > form of licence. Just had a bank holiday here, so bug not yet squashed. But > it is the sort of bug that might account for what you've seen (if a similar > bug exists in the code you've been using). The tree doesn't always get > properly rebalanced on node removals. I'll attack the problem later > tomorrow (technically, later today). Cheers. > I'd prefer Apache or MIT or BSD 3-clause, but I could probably work with this. http://joinup.ec.europa.eu/community/eupl/news/licence-proliferation-way-out I'm eager to see the code, and would love it if you sorted out the deletion rebalance issue. I just plunked some time into https://github.com/headius/redblack/blob/master/red_black_tree.py , only to find that it didn't appear to be doing deletions correctly - the tree would become unprintable after deleting one element. It's possible I introduced the bug, but right now I don't particularly suspect so, having not changed the __del__ method. I'm starting to think Red Black Trees are pretty complex. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon May 6 21:21:05 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 11:21:05 +1000 Subject: Red Black Tree implementation? In-Reply-To: <518850f8$0$8157$862e30e2@ngroups.net> References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> Message-ID: On Tue, May 7, 2013 at 10:55 AM, duncan smith wrote: > Here's the text I usually prepend. > > > ##Copyright (c) 2013 duncan g. smith > ## > ##Permission is hereby granted, free of charge, to any person obtaining a > ##copy of this software and associated documentation files (the "Software"), > ##to deal in the Software without restriction, including without limitation > ##the rights to use, copy, modify, merge, publish, distribute, sublicense, > ##and/or sell copies of the Software, and to permit persons to whom the > ##Software is furnished to do so, subject to the following conditions: > ## > ##The above copyright notice and this permission notice shall be included > ##in all copies or substantial portions of the Software. > ## > ##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS > ##OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF > MERCHANTABILITY, > ##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL > ##THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR > ##OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, > ##ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR > ##OTHER DEALINGS IN THE SOFTWARE. > > > Basically, "do what you want with it but don't blame me if it goes tits up". > I'm happy to consider tidying it up a bit and using a more recognized form > of licence. Is that the MIT license? If not, consider using it; it's well known and trusted. I haven't eyeballed yours closely but it looks extremely similar, at least. ChrisA From buzzard at invalid.invalid Tue May 7 09:28:39 2013 From: buzzard at invalid.invalid (duncan smith) Date: Tue, 07 May 2013 14:28:39 +0100 Subject: Red Black Tree implementation? In-Reply-To: References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> Message-ID: <51890188$0$3095$862e30e2@ngroups.net> On 07/05/13 02:20, Dan Stromberg wrote: > [snip] > > I'm starting to think Red Black Trees are pretty complex. > > A while ago I looked at a few different types of self-balancing binary tree. Most look much easier to implement. BTW, the licence might be MIT - I just copied it from someone else's code. Duncan From buzzard at invalid.invalid Wed May 8 19:21:50 2013 From: buzzard at invalid.invalid (duncan smith) Date: Thu, 09 May 2013 00:21:50 +0100 Subject: Red Black Tree implementation? In-Reply-To: References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> Message-ID: <518ade0f$1$60230$862e30e2@ngroups.net> On 07/05/13 02:20, Dan Stromberg wrote: > > On Mon, May 6, 2013 at 5:55 PM, duncan smith > wrote: > > [snip] > > I'd prefer Apache or MIT or BSD 3-clause, but I could probably work with > this. > http://joinup.ec.europa.eu/community/eupl/news/licence-proliferation-way-out > > I'm eager to see the code, and would love it if you sorted out the > deletion rebalance issue. > > I just plunked some time into > https://github.com/headius/redblack/blob/master/red_black_tree.py , only > to find that it didn't appear to be doing deletions correctly - the tree > would become unprintable after deleting one element. It's possible I > introduced the bug, but right now I don't particularly suspect so, > having not changed the __del__ method. > > I'm starting to think Red Black Trees are pretty complex. > > Mine is fixed now (sent to your gmail address). Restoring the tree properties after deletion is awkward to get right, and doesn't affect the performance noticeably for smallish trees if you get it wrong. I realised my code was buggy when I tried adding, then removing a million items and ran into the recursion limit. It now passes a test where I check the tree properties after each addition / deletion. Duncan From drsalists at gmail.com Wed May 8 21:40:06 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 8 May 2013 18:40:06 -0700 Subject: Red Black Tree implementation? In-Reply-To: <518ade0f$1$60230$862e30e2@ngroups.net> References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> <518ade0f$1$60230$862e30e2@ngroups.net> Message-ID: OK, I've got one copy of trees.py with md5 211f80c0fe7fb9cb42feb9645b4b3ffe. You seem to be saying I should have two though, but I don't know that I do... On 5/8/13, duncan smith wrote: > On 07/05/13 02:20, Dan Stromberg wrote: >> >> On Mon, May 6, 2013 at 5:55 PM, duncan smith > > wrote: >> >> > > [snip] > >> >> I'd prefer Apache or MIT or BSD 3-clause, but I could probably work with >> this. >> http://joinup.ec.europa.eu/community/eupl/news/licence-proliferation-way-out >> >> I'm eager to see the code, and would love it if you sorted out the >> deletion rebalance issue. >> >> I just plunked some time into >> https://github.com/headius/redblack/blob/master/red_black_tree.py , only >> to find that it didn't appear to be doing deletions correctly - the tree >> would become unprintable after deleting one element. It's possible I >> introduced the bug, but right now I don't particularly suspect so, >> having not changed the __del__ method. >> >> I'm starting to think Red Black Trees are pretty complex. >> >> > > Mine is fixed now (sent to your gmail address). Restoring the tree > properties after deletion is awkward to get right, and doesn't affect > the performance noticeably for smallish trees if you get it wrong. > > I realised my code was buggy when I tried adding, then removing a > million items and ran into the recursion limit. It now passes a test > where I check the tree properties after each addition / deletion. > > Duncan > -- > http://mail.python.org/mailman/listinfo/python-list > From buzzard at invalid.invalid Thu May 9 00:31:31 2013 From: buzzard at invalid.invalid (duncan smith) Date: Thu, 09 May 2013 05:31:31 +0100 Subject: Red Black Tree implementation? In-Reply-To: References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> <518ade0f$1$60230$862e30e2@ngroups.net> Message-ID: <518b26a4$0$4018$862e30e2@ngroups.net> On 09/05/13 02:40, Dan Stromberg wrote: > OK, I've got one copy of trees.py with md5 > 211f80c0fe7fb9cb42feb9645b4b3ffe. You seem to be saying I should have > two though, but I don't know that I do... > > I've just re-sent it. Duncan From buzzard at invalid.invalid Thu May 9 09:52:37 2013 From: buzzard at invalid.invalid (duncan smith) Date: Thu, 09 May 2013 14:52:37 +0100 Subject: Red Black Tree implementation? In-Reply-To: References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> <518ade0f$1$60230$862e30e2@ngroups.net> Message-ID: <518baa27$0$7930$862e30e2@ngroups.net> On 09/05/13 02:40, Dan Stromberg wrote: > OK, I've got one copy of trees.py with md5 > 211f80c0fe7fb9cb42feb9645b4b3ffe. You seem to be saying I should have > two though, but I don't know that I do... > [snip] Yes, 211f80c0fe7fb9cb42feb9645b4b3ffe is the correct checksum for the latest version. The previous version had an issue when adding non-distinct items (items that compare equal to items already in the tree). Cheers. Duncan From drsalists at gmail.com Sat May 11 19:24:42 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Sat, 11 May 2013 16:24:42 -0700 Subject: Red Black Tree implementation? In-Reply-To: <518baa27$0$7930$862e30e2@ngroups.net> References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> <518ade0f$1$60230$862e30e2@ngroups.net> <518baa27$0$7930$862e30e2@ngroups.net> Message-ID: I'm afraid I'm having some trouble with the module. I've checked it into my SVN at http://stromberg.dnsalias.org/svn/red-black-tree-mod/trunk/duncan I have two versions of your tests in there now - "t" is minimally changed, and test-red_black_tree_mod is pretty restructured to facilitate adding more tests later. I get the same problem with either version of the tests. The problem I'm seeing is that the tree, when built from items, isn't looking quite right. I inserted a print(tree) into the for loop, and I'm getting the following, where I expected the tree to grow by one element on each iteration: $ python t 6 False None None 6 False 3 None 6 False 3 15 6 False 3 15 6 False 3 11 6 False 3 11 6 False 3 11 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 Thoughts? BTW, printing an empty tree seems to say "sentinel". 'not sure if that was intended. Thanks! On Thu, May 9, 2013 at 6:52 AM, duncan smith wrote: > On 09/05/13 02:40, Dan Stromberg wrote: > >> OK, I've got one copy of trees.py with md5 >> 211f80c0fe7fb9cb42feb9645b4b3f**fe. You seem to be saying I should have >> two though, but I don't know that I do... >> >> > [snip] > > Yes, 211f80c0fe7fb9cb42feb9645b4b3f**fe is the correct checksum for the > latest version. The previous version had an issue when adding non-distinct > items (items that compare equal to items already in the tree). Cheers. > > Duncan > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From drsalists at gmail.com Sat May 11 21:29:23 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Sat, 11 May 2013 18:29:23 -0700 Subject: Red Black Tree implementation? In-Reply-To: References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> <518ade0f$1$60230$862e30e2@ngroups.net> <518baa27$0$7930$862e30e2@ngroups.net> Message-ID: On Sat, May 11, 2013 at 4:24 PM, Dan Stromberg wrote: > > I'm afraid I'm having some trouble with the module. I've checked it into > my SVN at > http://stromberg.dnsalias.org/svn/red-black-tree-mod/trunk/duncan > > I have two versions of your tests in there now - "t" is minimally changed, > and test-red_black_tree_mod is pretty restructured to facilitate adding > more tests later. I get the same problem with either version of the tests. > > The problem I'm seeing is that the tree, when built from items, isn't > looking quite right. I inserted a print(tree) into the for loop, and I'm > getting the following, where I expected the tree to grow by one element on > each iteration: > > $ python t > 6 False None None > 6 False 3 None > 6 False 3 15 > 6 False 3 15 > I figured out that this was printing a single node and some of its attributes, not an entire tree. I changed it to print an entire tree using self.in_order(). I've also changed around the comparisons a bit, to use a __cmp__ method but still provide __eq__, __neq__ and a new __lt__. I'm up against a new problem now that it'd be nice if you could look at: In BinaryTree.find(), it sometimes compares the item being searched for against None. In 2.x, this gives strange results, but may be benign in this code. In 3.x, this raises an exception. I've added a comment about this in the SVN repo I mentioned above. You can see the traceback yourself with python3 test-red_black_tree_mod . What should BinaryTree.find() do if it finds a data.node that is None? Thanks! PS: Is it about time we moved this discussion off python-list? -------------- next part -------------- An HTML attachment was scrubbed... URL: From buzzard at invalid.invalid Sat May 11 22:02:33 2013 From: buzzard at invalid.invalid (duncan smith) Date: Sun, 12 May 2013 03:02:33 +0100 Subject: Red Black Tree implementation? In-Reply-To: References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> <518ade0f$1$60230$862e30e2@ngroups.net> <518baa27$0$7930$862e30e2@ngroups.net> Message-ID: <518ef83b$0$24347$862e30e2@ngroups.net> On 12/05/13 02:29, Dan Stromberg wrote: > > On Sat, May 11, 2013 at 4:24 PM, Dan Stromberg > wrote: > > > I'm afraid I'm having some trouble with the module. I've checked it > into my SVN at > http://stromberg.dnsalias.org/svn/red-black-tree-mod/trunk/duncan > > I have two versions of your tests in there now - "t" is minimally > changed, and test-red_black_tree_mod is pretty restructured to > facilitate adding more tests later. I get the same problem with > either version of the tests. > > The problem I'm seeing is that the tree, when built from items, > isn't looking quite right. I inserted a print(tree) into the for > loop, and I'm getting the following, where I expected the tree to > grow by one element on each iteration: > > $ python t > 6 False None None > 6 False 3 None > 6 False 3 15 > 6 False 3 15 > > I figured out that this was printing a single node and some of its > attributes, not an entire tree. I changed it to print an entire tree > using self.in_order(). Yes, I've just posted regarding that. > > I've also changed around the comparisons a bit, to use a __cmp__ method > but still provide __eq__, __neq__ and a new __lt__. I have implemented a lot (maybe all?) of the set methods in a subclass. I should probably root that out and have a think about what should be in the RedBlackTree class and what subclasses might look like. > > I'm up against a new problem now that it'd be nice if you could look at: > In BinaryTree.find(), it sometimes compares the item being searched for > against None. In 2.x, this gives strange results, but may be benign in > this code. In 3.x, this raises an exception. I've added a comment > about this in the SVN repo I mentioned above. > > You can see the traceback yourself with python3 test-red_black_tree_mod . > > What should BinaryTree.find() do if it finds a data.node that is None? > A call to "find(data)" should find and return either a node containing "data"; or the sentinel node where "data" should be added. It should not get as far as the left or right child of a sentinel node (which would equal None). I'll look at this tomorrow. I did have the truth value of a node depending on it's data value (None implying False). Then I considered the possibility of actually wanting None as a value in the tree and changed it, so I could have introduced a bug here. > Thanks! > > PS: Is it about time we moved this discussion off python-list? > Maybe. You have my official e-mail address. Cheers. Duncan From buzzard at invalid.invalid Sun May 12 14:15:13 2013 From: buzzard at invalid.invalid (duncan smith) Date: Sun, 12 May 2013 19:15:13 +0100 Subject: Red Black Tree implementation? In-Reply-To: <518ef83b$0$24347$862e30e2@ngroups.net> References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> <518ade0f$1$60230$862e30e2@ngroups.net> <518baa27$0$7930$862e30e2@ngroups.net> <518ef83b$0$24347$862e30e2@ngroups.net> Message-ID: <518fdc34$0$18764$862e30e2@ngroups.net> On 12/05/13 03:02, duncan smith wrote: > On 12/05/13 02:29, Dan Stromberg wrote: >> >> On Sat, May 11, 2013 at 4:24 PM, Dan Stromberg > > wrote: [snip] >> >> What should BinaryTree.find() do if it finds a data.node that is None? >> > > A call to "find(data)" should find and return either a node containing > "data"; or the sentinel node where "data" should be added. It should not > get as far as the left or right child of a sentinel node (which would > equal None). I'll look at this tomorrow. I did have the truth value of a > node depending on it's data value (None implying False). Then I > considered the possibility of actually wanting None as a value in the > tree and changed it, so I could have introduced a bug here. > It's a Python3 thing. The initial sentinel node was evaluating to True. __nonzero__ needs to be changed to __bool__. >> Thanks! >> >> PS: Is it about time we moved this discussion off python-list? >> Let's do that from now. Duncan From buzzard at invalid.invalid Sat May 11 21:34:32 2013 From: buzzard at invalid.invalid (duncan smith) Date: Sun, 12 May 2013 02:34:32 +0100 Subject: Red Black Tree implementation? In-Reply-To: References: <5181ca0e$0$13974$862e30e2@ngroups.net> <518850f8$0$8157$862e30e2@ngroups.net> <518ade0f$1$60230$862e30e2@ngroups.net> <518baa27$0$7930$862e30e2@ngroups.net> Message-ID: <518ef1aa$0$33126$862e30e2@ngroups.net> On 12/05/13 00:24, Dan Stromberg wrote: > > I'm afraid I'm having some trouble with the module. I've checked it > into my SVN at > http://stromberg.dnsalias.org/svn/red-black-tree-mod/trunk/duncan > > I have two versions of your tests in there now - "t" is minimally > changed, and test-red_black_tree_mod is pretty restructured to > facilitate adding more tests later. I get the same problem with either > version of the tests. > > The problem I'm seeing is that the tree, when built from items, isn't > looking quite right. I inserted a print(tree) into the for loop, and > I'm getting the following, where I expected the tree to grow by one > element on each iteration: > > $ python t > 6 False None None > 6 False 3 None > 6 False 3 15 > 6 False 3 15 > 6 False 3 11 > 6 False 3 11 > 6 False 3 11 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > 11 False 6 15 > > Thoughts? > > BTW, printing an empty tree seems to say "sentinel". 'not sure if that > was intended. > > Thanks! > The leaf node has parent equal to None. All tree nodes have two children. One or both children may be sentinels, and a sentinel is signified by having both left and right (children) equal to None. So an empty tree is a sentinel node that is also root. So the string "sentinel" is expected (although possibly not the most sensible option). For non-sentinel nodes the string is generated by, return '%s %s %s' % (self.data, self.left.data, self.right.data) for the BinaryTree class, and by return '%s %s %s %s' % (self.data, self.is_red, self.left.data, self.right.data) for the RedBlackTree class. So what is being printed above is (in each case) the value contained in the root node, followed by its colour (True if red), and the values contained in the root node's left and right children. The root node remains root, although it's value and its children (and their values) might change due to tree rotations. It looks OK to me. The empty tree would print "sentinel". After adding the value 6 there is one tree node with sentinels as children (values equal to None). Adding 3 results in 3 being the value of the root's left child. It's right child is still a sentinel. Adding 15 results in that value being assigned to the right child. Adding 9 results in no change to the values in the root or its children. Adding 11 results in a tree rotation and 11 becomes the value in the right child of the root. At a later point a tree rotation results in the value of the root node being changed. I haven't implemented a way of representing the structure of the whole red black tree. I would probably write some code to generate a dot file and use that to generate a png. But you could add something like, print tree.height, tree.size, list(tree) and get output like, 0 1 [6] 1 2 [3, 6] 1 3 [3, 6, 15] 2 4 [3, 6, 9, 15] 3 5 [3, 6, 9, 11, 15] 4 6 [3, 6, 9, 11, 12, 15] 4 7 [3, 6, 9, 11, 12, 15, 16] 5 8 [3, 6, 9, 11, 12, 14, 15, 16] 5 9 [3, 6, 9, 11, 12, 14, 15, 16, 17] 5 10 [3, 6, 7, 9, 11, 12, 14, 15, 16, 17] 5 11 [3, 6, 7, 9, 11, 12, 14, 15, 16, 17, 18] 5 12 [3, 5, 6, 7, 9, 11, 12, 14, 15, 16, 17, 18] 5 13 [3, 5, 6, 7, 8, 9, 11, 12, 14, 15, 16, 17, 18] 6 14 [3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 15 [0, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 16 [0, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 17 [0, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 18 [-1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 19 [-1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] It doesn't give you the structure, but it does show that it seems to be growing reasonably. Cheers. Duncan From christian at python.org Thu May 2 06:46:47 2013 From: christian at python.org (Christian Heimes) Date: Thu, 02 May 2013 12:46:47 +0200 Subject: Red Black Tree implementation? In-Reply-To: References: Message-ID: <51824417.1@python.org> Am 02.05.2013 01:11, schrieb Dan Stromberg: > > What's the best Red Black Tree implementation for Python with an > opensource license? > > I started out looking at > http://newcenturycomputers.net/projects/rbtree.html > because it was > pretty high in Google and had the operators I wanted, but it gets very > slow at about half a million elements. I've been discussing this with a > C programmer who believes that Red Black Trees should perform very > similarly to an AVL tree, but that's not at all what I'm getting with > the newcenturycomputers implementation. No wonder it's getting slow and doesn't stand a change against Python's dict implementation. The rbtree implementation from newcenturycomputers.net is written entirely in Python. It's good code for academic research in order to study the properties of a rbtree. If you need something production ready then you have to use an implemetation with an optimized backend like a C code, PyPy or Cython. Christian From drsalists at gmail.com Thu May 2 13:04:07 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Thu, 2 May 2013 10:04:07 -0700 Subject: Red Black Tree implementation? In-Reply-To: <51824417.1@python.org> References: <51824417.1@python.org> Message-ID: On Thu, May 2, 2013 at 3:46 AM, Christian Heimes wrote: > Am 02.05.2013 01:11, schrieb Dan Stromberg: > No wonder it's getting slow and doesn't stand a change against Python's > dict implementation. The rbtree implementation from > newcenturycomputers.net is written entirely in Python. It's good code > for academic research in order to study the properties of a rbtree. > > If you need something production ready then you have to use an > implemetation with an optimized backend like a C code, PyPy or Cython. The newcenturycomputers RBTree is similarly slow on CPython 2.x, CPython 3.x, PyPy and Jython. I imagine the tree depth is getting deeper than it should, but I've not verified that. Also, the other datastructures I'm comparing it to: B+ Tree, AVL Tree, Treap, etcetera are pure python too, but they're performing reasonably - in fact, my expectations are pretty much defined by these. (OK, the treap code has a Cython version, but I'm not using that in this project) Thanks for the response. -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact_george at thiruva.com Wed May 1 20:13:44 2013 From: contact_george at thiruva.com (George T) Date: Wed, 1 May 2013 17:13:44 -0700 (PDT) Subject: Failure to build Python 3.3.2 SSL Module Message-ID: <9920f7c0-1c9d-4cb4-881f-e09e4cbed73f@googlegroups.com> Anyone have any thoughts on building Python 3.3.2 with the SSL module? I'm on RedHat Enterprise 6.4 and used Yum to install the openssl and openssl-devel 1.0.1e packages. But I keep getting a failure to build the SSL module - even if I set CFLAGS and LDFLAGS to find the headers and libs or additionally install the 0.9.8e OpenSSL. Any thoughts on what the build scripts are looking for or failing on? """ gcc -pthread -shared -pg -L/usr/lib64 -L/usr/lib64/openssl/engines -L/usr/lib64/openssl098e/engines -L/usr/lib64 -L/usr/lib64/openssl/engines -L/usr/lib64/openssl098e/engines -L/usr/lib64 -L/usr/lib64/openssl/engines -L/usr/lib64/openssl098e/engines -I/usr/include build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/_ctypes.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/callbacks.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/callproc.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/stgdict.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/cfield.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/libffi/src/prep_cif.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/libffi/src/closures.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/libffi/src/dlmalloc.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/libffi/src/x86/ffi64.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/libffi/src/x86/unix64.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/libffi/src/x86/ffi.o build/temp.linux-x86_64-3.3/home/idcuser/ftp/Python-3.3.1/Modules/_ctypes/libffi/src/x86/sysv.o -L/usr/lib64 -L/usr/lib64/openssl/engines -L/usr/lib64/openssl098e/engines -L/usr/local/lib -o build/lib.linux-x86_64-3.3/_ctypes.cpython-33m.so Failed to build these modules: _ssl running build_scripts creating build/scripts-3.3 copying and adjusting /home/idcuser/ftp/Python-3.3.1/Tools/scripts/pydoc3 -> build/scripts-3.3 copying and adjusting /home/idcuser/ftp/Python-3.3.1/Tools/scripts/idle3 -> build/scripts-3.3 copying and adjusting /home/idcuser/ftp/Python-3.3.1/Tools/scripts/2to3 -> build/scripts-3.3 copying and adjusting /home/idcuser/ftp/Python-3.3.1/Tools/scripts/pyvenv -> build/scripts-3.3 changing mode of build/scripts-3.3/pydoc3 from 664 to 775 changing mode of build/scripts-3.3/idle3 from 664 to 775 changing mode of build/scripts-3.3/2to3 from 664 to 775 changing mode of build/scripts-3.3/pyvenv from 664 to 775 renaming build/scripts-3.3/pydoc3 to build/scripts-3.3/pydoc3.3 renaming build/scripts-3.3/idle3 to build/scripts-3.3/idle3.3 renaming build/scripts-3.3/2to3 to build/scripts-3.3/2to3-3.3 renaming build/scripts-3.3/pyvenv to build/scripts-3.3/pyvenv-3.3 real 1m20.759s user 2m6.167s sys 0m8.557s """ Thanks in advance! Cheers, George From holdenweb at gmail.com Thu May 2 00:16:16 2013 From: holdenweb at gmail.com (holdenweb at gmail.com) Date: Wed, 1 May 2013 21:16:16 -0700 (PDT) Subject: how to get next month string? In-Reply-To: <1185278216.225757.195290@d55g2000hsg.googlegroups.com> References: <1185278216.225757.195290@d55g2000hsg.googlegroups.com> Message-ID: Hello. Is that you? Wondered what you had been up to lately and discovered that your email address hadn't transferred across to my new computer, so I had to resort to Google and found you on camp.lang.python - I hope! regards Steve On Tuesday, July 24, 2007 4:56:56 AM UTC-7, BartlebyScrivener wrote: > On Jul 24, 5:31 am, "Yinghe Chen" wrote: > > Hi, > > Could someone help on how to use python to output the next month string like > > this? > > > > "AUG07", suppose now is July 2007. > > > > I usually find time and date answers somewhere in here: > > http://pleac.sourceforge.net/pleac_python/datesandtimes.html > > rd From anadionisio257 at gmail.com Thu May 2 05:10:11 2013 From: anadionisio257 at gmail.com (=?ISO-8859-1?Q?Ana_Dion=EDsio?=) Date: Thu, 2 May 2013 02:10:11 -0700 (PDT) Subject: Sum operation in numpy arrays Message-ID: <756832e9-4579-498e-b55c-f1a98f42a113@googlegroups.com> Hello! I have several numpy arrays in my script and i want to add them. For example: a=[1,2,3,4,5] b=[1,1,1,1,1] c=[1,0,1,0,1] for i in range(5): d[i]=a[i]+b[i]+c[i] print d [3,3,5,5,7] I did it like that but I get an error: "TypeError: unsupported operand type(s) for +: 'float' and 'numpy.string_'" How can I sum my arrays? From jt at toerring.de Thu May 2 06:24:13 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 2 May 2013 10:24:13 GMT Subject: Sum operation in numpy arrays References: <756832e9-4579-498e-b55c-f1a98f42a113@googlegroups.com> Message-ID: Ana Dion?sio wrote: > Hello! I have several numpy arrays in my script and i want to add them. For example: > a=[1,2,3,4,5] > b=[1,1,1,1,1] > c=[1,0,1,0,1] > for i in range(5): > d[i]=a[i]+b[i]+c[i] > print d > [3,3,5,5,7] > I did it like that but I get an error: "TypeError: unsupported operand type(s) for +: 'float' and 'numpy.string_'" > How can I sum my arrays? There's no numpy array at all in use in your example program, you only have normal lists. If you had numpy arrays you wouldn't need to loop over the indivindual elements, you could do just d = a + b + c to add up the (numpy) arrays. The error message you get may indicate that somewhere in your real code (not the one you posted) one of the elements of one of the (numpy) arrays is not a number but a string, e.g. by doing some- thing like a = numpy.array( [ 1, '2', 3, 4, 5 ] ) (note the single quotes around 2, that will result in the element having a type of 'numpy.string'. But if it's like that is impossible to tell without seeing the real code you're using;-) I guess you will have to give us what you actually use if you want something more helpful. Regards, Jens -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From steve+comp.lang.python at pearwood.info Thu May 2 06:33:57 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 May 2013 10:33:57 GMT Subject: Sum operation in numpy arrays References: <756832e9-4579-498e-b55c-f1a98f42a113@googlegroups.com> Message-ID: <51824115$0$29969$c3e8da3$5496439d@news.astraweb.com> On Thu, 02 May 2013 02:10:11 -0700, Ana Dion?sio wrote: > Hello! I have several numpy arrays in my script and i want to add them. > For example: > > a=[1,2,3,4,5] > b=[1,1,1,1,1] > c=[1,0,1,0,1] These are not numpy arrays, they are lists of ints. Based on the error message you quote: TypeError: unsupported operand type(s) for +: 'float' and 'numpy.string_'" you have at least one numpy array of floats, and one numpy array of strings. Identify which array has got strings in it instead of floats, then identify how it got filled with strings, and change it to fill it with floats instead. Start by printing this: print(type(a), type(b), type(c)) print(type(a[0]), type(b[0]), type(c[0])) and see what they say. My guess is that somewhere in your code you have a list of strings: l = ['1', '2', '3'] (perhaps because you read them from a file), and then you converted it to a numpy array: x = numpy.array(l) without converting the strings to floats. -- Steven From l.selmi at icloud.com Thu May 2 09:56:28 2013 From: l.selmi at icloud.com (leonardo selmi) Date: Thu, 02 May 2013 15:56:28 +0200 Subject: help.. Message-ID: <57E38A20-B52B-4CAE-B1FB-487DD99B48AD@icloud.com> i have also this program: write a function called rental_car_costthat takes days as input and returns the cost for renting a car for said number of days. The cost must abide by the following conditions: Every day you rent the car is $40. If you rent the car for 3 or more days, you get $20 off your total. If you rent the car for 7 or more days, you get $50 off your total. (This does not stack with the 20 dollars you get for renting the car over 3 days.) so i wrote the following code: def rental_car_cost(days): cost = 40*days if days >= 7: return cost - 50 elif days >= 3 and days < 7: return cost - 20 else: return cost but it seems not to be right cause the computer answers: Oops, try again! Did you create a function called rental_car_cost? ii wrote also: def rental_car_cost(days): if days >= 7: return (40*days) - 50 if days >= 3 and < 7: return (40*days) - 20 else: return (40*days) they both work in python shell/idle but not in the exercise in "codecademy"..what do you think? is there a bug? thanks! kindest regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu May 2 10:06:11 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 May 2013 00:06:11 +1000 Subject: help.. In-Reply-To: <57E38A20-B52B-4CAE-B1FB-487DD99B48AD@icloud.com> References: <57E38A20-B52B-4CAE-B1FB-487DD99B48AD@icloud.com> Message-ID: On Thu, May 2, 2013 at 11:56 PM, leonardo selmi wrote: > so i wrote the following code: > def rental_car_cost(days): > cost = 40*days > if days >= 7: > return cost - 50 > elif days >= 3 and days < 7: > return cost - 20 > else: > return cost > > but it seems not to be right cause the computer answers: Oops, try again! > Did you create a function called rental_car_cost? That code looks fine to me (though I'd drop the "and days < 7" as it's redundant); but the error you're getting isn't a Python exception. What's giving that error? You may need to check the framing of your code. How does codecademy (which I'm not familiar with) locate your function? ChrisA From tampucciolina at libero.it Thu May 2 10:20:48 2013 From: tampucciolina at libero.it (leonardo) Date: Thu, 2 May 2013 16:20:48 +0200 Subject: help.. In-Reply-To: References: <57E38A20-B52B-4CAE-B1FB-487DD99B48AD@icloud.com> Message-ID: on codecademy there is an interactive box where you type your code, it is a kind of exercise program to practice. but i don't understand what is wrong. this is the website address of that if you want to give a look: http://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096 Il giorno 02/mag/2013, alle ore 16:06, Chris Angelico ha scritto: > On Thu, May 2, 2013 at 11:56 PM, leonardo selmi wrote: >> so i wrote the following code: >> def rental_car_cost(days): >> cost = 40*days >> if days >= 7: >> return cost - 50 >> elif days >= 3 and days < 7: >> return cost - 20 >> else: >> return cost >> >> but it seems not to be right cause the computer answers: Oops, try again! >> Did you create a function called rental_car_cost? > > That code looks fine to me (though I'd drop the "and days < 7" as it's > redundant); but the error you're getting isn't a Python exception. > What's giving that error? You may need to check the framing of your > code. How does codecademy (which I'm not familiar with) locate your > function? > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Thu May 2 10:30:02 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 May 2013 00:30:02 +1000 Subject: help.. In-Reply-To: References: <57E38A20-B52B-4CAE-B1FB-487DD99B48AD@icloud.com> Message-ID: On Fri, May 3, 2013 at 12:20 AM, leonardo wrote: > on codecademy there is an interactive box where you type your code, it is a kind of exercise program to practice. but i don't understand what is wrong. this is the website address of that if you want to give a look: > > http://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096 Well, I gave it a try, and it seemed to work :) Your second example has a syntactic error (probably you want to say "days" again in the second half of the 'and' condition), but with that fixed, both work and are accepted by the opposite end. Though that last consideration may not be all that indicative; this code was accepted and gave the "Way to go!" success message: def rental_car_cost(days): raise OSError() Yeah, I don't think that quite worked :) ChrisA From robert.kern at gmail.com Thu May 2 10:30:36 2013 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 02 May 2013 15:30:36 +0100 Subject: help.. In-Reply-To: References: <57E38A20-B52B-4CAE-B1FB-487DD99B48AD@icloud.com> Message-ID: On 2013-05-02 15:20, leonardo wrote: > on codecademy there is an interactive box where you type your code, it is a kind of exercise program to practice. but i don't understand what is wrong. this is the website address of that if you want to give a look: > > http://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096 I think the problem is in the codecademy interface (or your use of it) rather than the Python code. You will probably get better help in the codecademy forums or help documentation: http://www.codecademy.com/forums/python-beginner-P5YtY/1 http://help.codecademy.com/ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From hermanmu at gmail.com Thu May 2 10:56:33 2013 From: hermanmu at gmail.com (Michael Herman) Date: Thu, 2 May 2013 07:56:33 -0700 Subject: help.. In-Reply-To: References: <57E38A20-B52B-4CAE-B1FB-487DD99B48AD@icloud.com> Message-ID: the problem is in your code: http://screencast.com/t/haF1NY5RvpMv On Thu, May 2, 2013 at 7:30 AM, Robert Kern wrote: > > On 2013-05-02 15:20, leonardo wrote: >> >> on codecademy there is an interactive box where you type your code, it is a kind of exercise program to practice. but i don't understand what is wrong. this is the website address of that if you want to give a look: >> >> http://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096 > > > I think the problem is in the codecademy interface (or your use of it) rather than the Python code. You will probably get better help in the codecademy forums or help documentation: > > http://www.codecademy.com/forums/python-beginner-P5YtY/1 > http://help.codecademy.com/ > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list From tampucciolina at libero.it Thu May 2 10:43:05 2013 From: tampucciolina at libero.it (leonardo) Date: Thu, 2 May 2013 16:43:05 +0200 Subject: help.. In-Reply-To: References: <57E38A20-B52B-4CAE-B1FB-487DD99B48AD@icloud.com> Message-ID: <18E5788F-3B66-4652-A101-EBAE7D4F63E2@libero.it> thanks at all Il giorno 02/mag/2013, alle ore 16:30, Robert Kern ha scritto: > On 2013-05-02 15:20, leonardo wrote: >> on codecademy there is an interactive box where you type your code, it is a kind of exercise program to practice. but i don't understand what is wrong. this is the website address of that if you want to give a look: >> >> http://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096 > > I think the problem is in the codecademy interface (or your use of it) rather than the Python code. You will probably get better help in the codecademy forums or help documentation: > > http://www.codecademy.com/forums/python-beginner-P5YtY/1 > http://help.codecademy.com/ > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Thu May 2 10:36:48 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 May 2013 00:36:48 +1000 Subject: Python teaching book recommendations: 3.3+ and with exercises Message-ID: One of my younger brothers, still school age, is to be studying some aspect of computing for the next term or two. I strongly recommended he learn Python (it has a bit more future than studying the internals of OS/2), and my/his father then asked me what book I'd recommend him to study through. I know this has come up a few times, but searching back in my emails didn't give a clear answer. Whether it's an ebook or in paper doesn't matter, but there need to be exercises that the student can perform on his own, and then an external tutor (aka my dad) then check up on. Python 3 is a must; preferably something designed for 3.3+ as that's what he'll be using. Beyond that, I was just asked to recommend "the best", which is unfortunately vague... but I gather they'll be deferring to the expertise of the list in figuring out what that means :) So, within those not-very-many restrictions, what would you all recommend? Let's get a nice discussion going and see where it leads us (most likely to a resident troll ranting about a favorite topic, but hey, maybe Dihedral will come out with some nugget of wisdom for us)! Chris Angelico From wingusr at gmail.com Thu May 2 21:06:03 2013 From: wingusr at gmail.com (TP) Date: Thu, 2 May 2013 18:06:03 -0700 Subject: Python teaching book recommendations: 3.3+ and with exercises In-Reply-To: References: Message-ID: On Thu, May 2, 2013 at 7:36 AM, Chris Angelico wrote: > One of my younger brothers, still school age, is to be studying some > aspect of computing for the next term or two. I strongly recommended > he learn Python (it has a bit more future than studying the internals > of OS/2), and my/his father then asked me what book I'd recommend him > to study through. I know this has come up a few times, but searching > back in my emails didn't give a clear answer. > > Whether it's an ebook or in paper doesn't matter, but there need to be > exercises that the student can perform on his own, and then an > external tutor (aka my dad) then check up on. Python 3 is a must; > preferably something designed for 3.3+ as that's what he'll be using. > Beyond that, I was just asked to recommend "the best", which is > unfortunately vague... but I gather they'll be deferring to the > expertise of the list in figuring out what that means :) > > So, within those not-very-many restrictions, what would you all > recommend? Let's get a nice discussion going and see where it leads us > (most likely to a resident troll ranting about a favorite topic, but > hey, maybe Dihedral will come out with some nugget of wisdom for us)! > Since I've been using Python for years, I've only skimmed new books. Personally, I found the standard Python tutorial [1] (and reading the What's New sections) more than adequate for originally learning Python. Anyway, given your constraints, here's what I've come up with (stipulating Python 3, limits the number of available books. And requiring exercises is even stricter): Python for Kids - A Playful Introduction to Programming - J. Briggs (No Starch, 2013) [2]. Python 3.2? Not necessarily just for kids but definitely slanted towards younger readers. Has "Programming Puzzles". Think Python - A. Downey (O'Reilly, 2012) [3]. For Python 2 & 3. Exercises but no answers? There is also a Python 3 specific version available. Free. Beginning Python - Using Python 2.6 and Python 3.1 - J. Payne (Wrox, 2010) [4]. Has exercises but... they seem a bit simplistic to me. Answers in Appendix A. Head First Python - Barry (OReilly,2011) [5] Python 3. Says for people who already know another language (and I would have to agree with that). Exercises. Introduction to Programming Using Python - Y. Liang (Pearson, 2013) [6]. A bit dull looking. Probably a college textbook? Lots of exercises. Author also has similar books on C++ & Java. Expensive. Ignoring the constraints, here's another one that looks really interesting: Hello Python - A. Briggs (Manning, 2012) [7]. Python 2 only. Learn by incrementally writing and improving working programs. Less focused on syntax and more on getting the job done. No exercises per se, but since goal is working programs, progress can be still be checked. More about the "batteries" (including somewhat surprisingly pyglet, twisted & django). From the Preface: "I thought back to how I first learned to program. I didn't read an entire programming book from cover to cover and then write a program after I knew everything there was to know. Instead I started with a goal, something that I wanted to do, and worked toward it, figuring things out as I went. I read programming books from time to time, but really only to figure out the bits I was stuck on. When I was done, my programs weren't particularly elegant or fast, but they were mine---I knew how they worked, and they solved a real problem that I was having." Page 9: "We'll be using the latest version of Python 2, because most of the libraries that we'll use in this book don?t yet support Python 3." [1] http://docs.python.org/3/tutorial/index.html [2] http://nostarch.com/pythonforkids [3] http://www.greenteapress.com/thinkpython/thinkpython.html [4] http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Python-Using-Python-2-6-and-Python-3-1.productCd-0470414634.html [5] http://shop.oreilly.com/product/0636920003434.do [6] http://www.pearsonhighered.com/educator/product/Introduction-to-Programming-Using-Python/9780132747189.page [7] http://www.manning.com/briggs/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri May 3 05:04:43 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 May 2013 19:04:43 +1000 Subject: Python teaching book recommendations: 3.3+ and with exercises In-Reply-To: References: Message-ID: On Fri, May 3, 2013 at 11:06 AM, TP wrote: > Since I've been using Python for years, I've only skimmed new books. Personally, I found the standard Python tutorial [1] (and reading the What's New sections) more than adequate for originally learning Python. > > Anyway, given your constraints, here's what I've come up with (stipulating Python 3, limits the number of available books. And requiring exercises is even stricter): Thanks for the tips, TP! I'll discuss these with the appropriate people over the weekend. From first glance, this is how they seem to me: > Python for Kids - A Playful Introduction to Programming - J. Briggs (No Starch, 2013) [2]. Python 3.2? Not necessarily just for kids but definitely slanted towards younger readers. Has "Programming Puzzles". Might skip this one. He's already tinkering with his own Linux box, so I don't think we need to go childish. > Think Python - A. Downey (O'Reilly, 2012) [3]. For Python 2 & 3. Exercises but no answers? There is also a Python 3 specific version available. Free. That looks good. Lack of answers isn't a problem; if Dad can't figure it out, he can always ask me. (But I'm hoping to be involved as little as possible with the detaily bits.) > Beginning Python - Using Python 2.6 and Python 3.1 - J. Payne (Wrox, 2010) [4]. Has exercises but... they seem a bit simplistic to me. Answers in Appendix A. Perhaps, but I'll keep it on the list anyway. > Head First Python - Barry (OReilly,2011) [5] Python 3. Says for people who already know another language (and I would have to agree with that). Exercises. Pass on that, then; Python's his first real language study. > Introduction to Programming Using Python - Y. Liang (Pearson, 2013) [6]. A bit dull looking. Probably a college textbook? Lots of exercises. Author also has similar books on C++ & Java. Expensive. I'll mention it, but expensive is a downside when there are a number of free and/or cheap options! > Ignoring the constraints, here's another one that looks really interesting: > > Hello Python - A. Briggs (Manning, 2012) [7]. Python 2 only. Learn by incrementally writing and improving working programs. Less focused on syntax and more on getting the job done. No exercises per se, but since goal is working programs, progress can be still be checked. More about the "batteries" (including somewhat surprisingly pyglet, twisted & django). > > "We'll be using the latest version of Python 2, because most of the > libraries that we'll use in this book don?t yet support Python 3." I'll avoid that, then; I really don't want to have to get into a discussion of raw_input() vs input() after he's befuddled himself, nor do I intend to re-argue Unicode vs bytes! Many thanks for the suggestions. Was kinda hoping for multiple people's chimings-in; I know there'll be some strong opinions around the list... it's what we do best... :) ChrisA From alister.ware at ntlworld.com Fri May 3 06:49:03 2013 From: alister.ware at ntlworld.com (Alister) Date: Fri, 03 May 2013 10:49:03 GMT Subject: Python teaching book recommendations: 3.3+ and with exercises References: Message-ID: On Fri, 03 May 2013 00:36:48 +1000, Chris Angelico wrote: > One of my younger brothers, still school age, is to be studying some > aspect of computing for the next term or two. I strongly recommended he > learn Python (it has a bit more future than studying the internals of > OS/2), and my/his father then asked me what book I'd recommend him to > study through. I know this has come up a few times, but searching back > in my emails didn't give a clear answer. > > Whether it's an ebook or in paper doesn't matter, but there need to be > exercises that the student can perform on his own, and then an external > tutor (aka my dad) then check up on. Python 3 is a must; preferably > something designed for 3.3+ as that's what he'll be using. Beyond that, > I was just asked to recommend "the best", which is unfortunately > vague... but I gather they'll be deferring to the expertise of the list > in figuring out what that means :) > > So, within those not-very-many restrictions, what would you all > recommend? Let's get a nice discussion going and see where it leads us > (most likely to a resident troll ranting about a favorite topic, but > hey, maybe Dihedral will come out with some nugget of wisdom for us)! > > Chris Angelico A guy of your abilities & knowledge, why not write your own. you could use the experience of teaching your brother to hone it to perfection. Ok this was slightly tounge in cheek ( but not intended to be in any way offensive). I will now offer to put my money where my mouth is an make a contribution if you were to put it up on kickstarter. -- "Cogito ergo I'm right and you're wrong." -- Blair Houghton From rosuav at gmail.com Fri May 3 07:09:42 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 May 2013 21:09:42 +1000 Subject: Python teaching book recommendations: 3.3+ and with exercises In-Reply-To: References: Message-ID: On Fri, May 3, 2013 at 8:49 PM, Alister wrote: > A guy of your abilities & knowledge, why not write your own. you could > use the experience of teaching your brother to hone it to perfection. > > Ok this was slightly tounge in cheek ( but not intended to be in any way > offensive). I will now offer to put my money where my mouth is an make a > contribution if you were to put it up on kickstarter. Not at all offensive... it just implies that I have time to spend, during the day, somewhere in between holding down my own full-time job :) I'm hoping to be involved as little as possible in the day-to-day of it, though of course I'll be as happy to answer his questions as I am to answer them here on the list. Also, the suggestion implies that I'm a Python expert, which isn't exactly true; I actually have not done many nontrivial Python programs (for instance, I've never used _any_ of the GUI toolkits available). Hence my hope of finding a book written by an expert and recommended by experts. :) ChrisA From wingusr at gmail.com Fri May 3 10:54:53 2013 From: wingusr at gmail.com (TP) Date: Fri, 3 May 2013 07:54:53 -0700 Subject: Python teaching book recommendations: 3.3+ and with exercises In-Reply-To: References: Message-ID: [Just a note, all the book links in my original post have complete table of contents listing, so don't just take my word on their suitability.] Here's some I missed: Programming in Python 3, 2nd Edition - Mark Summerfield (Addison-Wesley, 2009) [1a]. Exercises. Solutions available online. At a quick glance I'd say the exercises are also better for the beginner. Less of a computer science orientation? Learning Python, 4th Edition, Mark Lutz (Oreilly,2009) [1]. The 5th edition [2] is supposed to come out in June. Python 2.6 & 3.x. Has exercises at the end of every Part and answers in Appendix B. Also has "Test Your Knowledge" quizzes & answers. 1200+ pages! Too overwhelming for a first Python book? Ideal for a 2nd or 3rd however. After seeing your response, I'd say, just one book isn't really going to work. So I would recommend: 1) Quickly go through Python for Kids. While it may say it's for kids, it actually covers quite a bit of ground (including writing a game using tkinter). I doubt it's so easy as to be boring. The lazy can also download sample code and solutions to the Programming Puzzles from the webpage. 2) Then Summerfield's Programming in Python 3. It has a lot more detail and also spends time on the Python Standard Library. Or maybe Think Python. A *lot* drier presentation than Python for Kids -- after all, the subtitle which I forgot to mention is "How to Think Like a Computer Scientist". Newer than Summerfield , but only 1/3 the length . Since I've only skimmed both I find it hard to recommend one over the other. 3) Finally, when he *really* wants to learn all the gory details about Python programming, tackle Learning Python (and by that time hopefully the 5th edition will be out). Once you've "learned" P ython then I would also recommend Programming Python, 4e - Mark Lutz (Oreilly,2010) [3] for detailed info on using some of the standard modules. Also Python Cookbook: Recipes for Mastering Python 3 - Beazley & Jone (OReilly, 2013) [4]. [Off Topic] -------- Depending on his age and what else he is learning at school, I also kinda think NumPy/SciPy/Matplotlib can be very enlightening. SciPy and NumPy - E. Bressert (O'Reilly, 2013) [5]. A brief overview. NumPy 1.5 Beginner's Guide - I. Idris (Packt, 2011) [6] NumPy Cookbook - I. Idris (Packt, 2012) [7] Already started back in February and ending in a month, there is MIT's free online video edX course "6.00x: Introduction to Computer Science and Programming" [8] which uses Python 2.7 and the book "Introduction to Computation and Programming Using Python (Spring 2013 edition)" by John Guttag [9]. This is also viewable free online if you are registered for the course. Estimated effort 12 hours/week, eeek! [1a] http://www.qtrac.eu/py3book.html [1] http://shop.oreilly.com/product/9780596158071.do [2] http://shop.oreilly.com/product/0636920028154.do [3] http://shop.oreilly.com/product/9780596158118.do [4] http://shop.oreilly.com/product/0636920027072.do [5] http://shop.oreilly.com/product/0636920020219.do [6] http://www.packtpub.com/numpy-1-5-using-real-world-examples-beginners-guide/book [7] http://www.packtpub.com/numpy-for-python-cookbook/book [8] https://www.edx.org/courses/MITx/6.00x/2013_Spring/about [9] http://mitpress.mit.edu/books/introduction-computation-and-programming-using-python -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu May 30 08:32:19 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 22:32:19 +1000 Subject: Python teaching book recommendations: 3.3+ and with exercises In-Reply-To: References: Message-ID: On Sat, May 4, 2013 at 12:54 AM, TP wrote: > Or maybe Think Python. A *lot* drier presentation than Python for Kids -- > after all, the subtitle which I forgot to mention is "How to Think Like a > Computer Scientist". Newer than Summerfield > , but only 1/3 the length > . Since I've only skimmed both I find it hard to recommend one over the > other. Many thanks to those who responded. A decision has been reached, and Think Python is being used. We'll see how it goes! I knew I could trust this list for good advice :) ChrisA From ethan at stoneleaf.us Fri May 3 13:50:07 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 03 May 2013 10:50:07 -0700 Subject: Python teaching book recommendations: 3.3+ and with exercises In-Reply-To: References: Message-ID: <5183F8CF.9040801@stoneleaf.us> Try http://inventwithpython.com/ Al Sweigert is the author, and he has three free ebooks there, and you can also purchase the paper versions if you like. Looks like it targets 3.1. -- ~Ethan~ From l.selmi at icloud.com Thu May 2 09:50:58 2013 From: l.selmi at icloud.com (leonardo selmi) Date: Thu, 02 May 2013 15:50:58 +0200 Subject: help to code... Message-ID: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> dear python community, i wrote the following program: from datetime import datetime now = datetime.now() current_month = now.month current_day = now.day current_year = now.year current_hour = now.hour current_minute = now.minute current_second = now.second print str(current_month) + '/' + str(current_day) + '/' + str(current_year) +' '+ print str(current_hour) + str(current_minute) + str(current_second) and i got this error: Traceback (most recent call last): File "runner.py", line 125, in compilecode File "python", line 9 print str(current_month) + '/' + str(current_day) + '/' + str(current_year) +' '+ ^ SyntaxError: invalid syntax how can i write the last two lines correctly? thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu May 2 10:59:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 May 2013 00:59:03 +1000 Subject: help to code... In-Reply-To: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> References: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> Message-ID: On Thu, May 2, 2013 at 11:50 PM, leonardo selmi wrote: > dear python community, > > i wrote the following program: > > print str(current_month) + '/' + str(current_day) + '/' + str(current_year) > +' '+ > print str(current_hour) + str(current_minute) + str(current_second) > > SyntaxError: invalid syntax > > how can i write the last two lines correctly? You're doing two separate print statements. Either join them into one (if you want it to be one line), or drop the last + on the first line, which is causing your syntax error. But there's an even easier way to do this: Use formatted printing. print("%d/%d/%d %d%d%d"%(current_month,current_day,current_year,current_hour,current_minute,current_second)) Or, since you're getting those straight from 'now': print("%d/%d/%d %d%d%d"%(now.month,now.day,now.year,now.hour,now.minute,now.second)) I strongly suspect that you want to put delimiters in the time, though (colons, perhaps?). It'd be really nice, by the way, if you'd avoid the messy American format date with the month first; put the year first and it's unambiguous! ChrisA From breamoreboy at yahoo.co.uk Thu May 2 11:26:17 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 02 May 2013 16:26:17 +0100 Subject: help to code... In-Reply-To: References: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> Message-ID: On 02/05/2013 15:59, Chris Angelico wrote: > On Thu, May 2, 2013 at 11:50 PM, leonardo selmi wrote: >> dear python community, >> >> i wrote the following program: >> >> print str(current_month) + '/' + str(current_day) + '/' + str(current_year) >> +' '+ >> print str(current_hour) + str(current_minute) + str(current_second) >> >> SyntaxError: invalid syntax >> >> how can i write the last two lines correctly? > > You're doing two separate print statements. Either join them into one > (if you want it to be one line), or drop the last + on the first line, > which is causing your syntax error. But there's an even easier way to > do this: Use formatted printing. > > print("%d/%d/%d > %d%d%d"%(current_month,current_day,current_year,current_hour,current_minute,current_second)) > > Or, since you're getting those straight from 'now': > > print("%d/%d/%d > %d%d%d"%(now.month,now.day,now.year,now.hour,now.minute,now.second)) > > I strongly suspect that you want to put delimiters in the time, though > (colons, perhaps?). It'd be really nice, by the way, if you'd avoid > the messy American format date with the month first; put the year > first and it's unambiguous! > > ChrisA > Better IMHO is to use strftime http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior so the complete code could be from datetime import datetime print(datetime.now().strftime('%m/%Y/%d %H %m %S')) -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From python at mrabarnett.plus.com Thu May 2 11:37:06 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 May 2013 16:37:06 +0100 Subject: help to code... In-Reply-To: References: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> Message-ID: <51828822.5080102@mrabarnett.plus.com> On 02/05/2013 16:26, Mark Lawrence wrote: > On 02/05/2013 15:59, Chris Angelico wrote: >> On Thu, May 2, 2013 at 11:50 PM, leonardo selmi wrote: >>> dear python community, >>> >>> i wrote the following program: >>> >>> print str(current_month) + '/' + str(current_day) + '/' + str(current_year) >>> +' '+ >>> print str(current_hour) + str(current_minute) + str(current_second) >>> >>> SyntaxError: invalid syntax >>> >>> how can i write the last two lines correctly? >> >> You're doing two separate print statements. Either join them into one >> (if you want it to be one line), or drop the last + on the first line, >> which is causing your syntax error. But there's an even easier way to >> do this: Use formatted printing. >> >> print("%d/%d/%d >> %d%d%d"%(current_month,current_day,current_year,current_hour,current_minute,current_second)) >> >> Or, since you're getting those straight from 'now': >> >> print("%d/%d/%d >> %d%d%d"%(now.month,now.day,now.year,now.hour,now.minute,now.second)) >> >> I strongly suspect that you want to put delimiters in the time, though >> (colons, perhaps?). It'd be really nice, by the way, if you'd avoid >> the messy American format date with the month first; put the year >> first and it's unambiguous! >> >> ChrisA >> > > Better IMHO is to use strftime > http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior > so the complete code could be > > from datetime import datetime > print(datetime.now().strftime('%m/%Y/%d %H %m %S')) > Except, of course, putting the year first, and using "%m" for the minutes: print(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) :-) From breamoreboy at yahoo.co.uk Thu May 2 12:08:45 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 02 May 2013 17:08:45 +0100 Subject: help to code... In-Reply-To: <51828822.5080102@mrabarnett.plus.com> References: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> <51828822.5080102@mrabarnett.plus.com> Message-ID: On 02/05/2013 16:37, MRAB wrote: > On 02/05/2013 16:26, Mark Lawrence wrote: >> On 02/05/2013 15:59, Chris Angelico wrote: >>> On Thu, May 2, 2013 at 11:50 PM, leonardo selmi >>> wrote: >>>> dear python community, >>>> >>>> i wrote the following program: >>>> >>>> print str(current_month) + '/' + str(current_day) + '/' + >>>> str(current_year) >>>> +' '+ >>>> print str(current_hour) + str(current_minute) + str(current_second) >>>> >>>> SyntaxError: invalid syntax >>>> >>>> how can i write the last two lines correctly? >>> >>> You're doing two separate print statements. Either join them into one >>> (if you want it to be one line), or drop the last + on the first line, >>> which is causing your syntax error. But there's an even easier way to >>> do this: Use formatted printing. >>> >>> print("%d/%d/%d >>> %d%d%d"%(current_month,current_day,current_year,current_hour,current_minute,current_second)) >>> >>> >>> Or, since you're getting those straight from 'now': >>> >>> print("%d/%d/%d >>> %d%d%d"%(now.month,now.day,now.year,now.hour,now.minute,now.second)) >>> >>> I strongly suspect that you want to put delimiters in the time, though >>> (colons, perhaps?). It'd be really nice, by the way, if you'd avoid >>> the messy American format date with the month first; put the year >>> first and it's unambiguous! >>> >>> ChrisA >>> >> >> Better IMHO is to use strftime >> http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior >> >> so the complete code could be >> >> from datetime import datetime >> print(datetime.now().strftime('%m/%Y/%d %H %m %S')) >> > Except, of course, putting the year first, and using "%m" for the > minutes: > > print(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) > > :-) > Congratulations on spotting my deliberate mistake :) -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From python at mrabarnett.plus.com Thu May 2 11:04:20 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 May 2013 16:04:20 +0100 Subject: help to code... In-Reply-To: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> References: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> Message-ID: <51828074.6050109@mrabarnett.plus.com> On 02/05/2013 14:50, leonardo selmi wrote: > dear python community, > > i wrote the following program: > > from datetime import datetime > now = datetime.now() > current_month = now.month > current_day = now.day > current_year = now.year > current_hour = now.hour > current_minute = now.minute > current_second = now.second > print str(current_month) + '/' + str(current_day) + '/' + > str(current_year) +' '+ > print str(current_hour) + str(current_minute) + str(current_second) > > and i got this error: > Traceback (most recent call last): > File "runner.py", line 125, in compilecode > File "python", line 9 > print str(current_month) + '/' + str(current_day) + '/' + str(current_year) +' '+ > ^ > SyntaxError: invalid syntax > > how can i write the last two lines correctly? > Look at where the caret (the "^") is pointing; there's a trailing "+". From bgailer at gmail.com Thu May 2 18:40:09 2013 From: bgailer at gmail.com (bob gailer) Date: Thu, 02 May 2013 18:40:09 -0400 Subject: [Python-Help] help to code... In-Reply-To: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> References: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> Message-ID: <5182EB49.3040805@gmail.com> On 5/2/2013 9:50 AM, leonardo selmi wrote: > Please in future post plain text. -------------- next part -------------- An HTML attachment was scrubbed... URL: From matteo.boscolo at boscolini.eu Fri May 3 02:50:27 2013 From: matteo.boscolo at boscolini.eu (Matteo Boscolo) Date: Fri, 03 May 2013 08:50:27 +0200 Subject: help to code... In-Reply-To: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> References: <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> Message-ID: <51835E33.7040600@boscolini.eu> The error is self expleined .. print str(current_month) + '/' + str(current_day) + '/' + str(current_year) +' *'+ * this line have a + at the end,the interpreter need something to add .. so remove it and it will work regards, Matteo Il 02/05/2013 15:50, leonardo selmi ha scritto: > dear python community, > > i wrote the following program: > > from datetime import datetime > now = datetime.now() > current_month = now.month > current_day = now.day > current_year = now.year > current_hour = now.hour > current_minute = now.minute > current_second = now.second > print str(current_month) + '/' + str(current_day) + '/' + > str(current_year) +' '+ > print str(current_hour) + str(current_minute) + str(current_second) > > and i got this error: > Traceback (most recent call last): > File "runner.py", line 125, in compilecode > File "python", line 9 > print str(current_month) + '/' + str(current_day) + '/' + str(current_year) +' '+ > ^ > SyntaxError: invalid syntax > > how can i write the last two lines correctly? > > thanks! > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bv8bv8bv8 at gmail.com Thu May 2 14:46:31 2013 From: bv8bv8bv8 at gmail.com (BV BV) Date: Thu, 2 May 2013 11:46:31 -0700 (PDT) Subject: ???????? DID MUHAMMAD AUTHOR THE QURAN Message-ID: <6e5fef5a-6fe3-4e83-aa9e-59901728d46e@o19g2000vbm.googlegroups.com> DID MUHAMMAD AUTHOR THE QURAN? Did Muhammad Author the Quran? Who authored the Quran? Someone must have produced it! After all, how many desert men have stood up in the history of man and given the world a book like the Quran? The book has amazing details of past nations, prophets, and religions as well as accurate scientific information unavailable at the time. What was the source of all this? If we were to deny the divine origin of the Quran, we are left with only a few possibilities: - The Prophet Muhammad authored it himself. - He took it from someone else. In this case, he either took it from a Jew or a Christian or one of the foreigners in Arabia. The Meccans did not bother to accuse him of having taken it from one of them. A brief response from God is: ?And they say, ?Legends of the former peoples which he has written down, and they are dictated to him morning and afternoon.? Say, [O Muhammad], ?It has been revealed by He who knows [every] secret within the heavens and the earth. Indeed, He is ever Forgiving and Merciful.?? (Quran 25:5-6) It was well known to his detractors that Muhammad, who was raised among them, never learned how to read or write from the time of his birth. They knew whom he befriended and where he had traveled; they acknowledged his integrity and honesty by calling him ?Al-Ameen,? the Reliable, the Trustworthy, the Honest.[1] Only in their revulsion against his preaching did they accuse him ? and then it was anything they could dream up: He was accused of being a sorcerer, a poet and even an imposter! They could not make up their minds. God says: ?Look how they strike for you comparisons; but they have strayed, so they cannot [find] a way.? (Quran 17:47) Simply, God is aware of what is in the heavens and the earth, He knows the past and the present, and reveals the truth to His prophet. Could Muhammad Have Authored It? It is impossible that Muhammad could have authored the Quran due to the following reasons: First, several occasions presented themselves where he could have fabricated revelation. For example, after the first revelation came, people awaited to hear more, but the Prophet did not receive anything new for months. The Meccans began making fun of him, ?His Lord has abandoned him!? This continued until the 93rd chapter, Ad-Doha, was revealed. The Prophet could have compiled something and presented it as the latest revelation to end the mockery, but he did not. Also, at one point during his prophethood, some of the hypocrites accused his beloved wife Aisha of being unchaste. The Prophet could have easily fabricated something to free her of blame, but he waited for many excruciating days, all spent in pain, mockery, and anguish, until revelation came from God freeing her from the accusation. Second, there is internal evidence within the Quran that Muhammad was not its author. Several verses criticized him, and were on occasion strongly worded. How can an imposter prophet blame himself when it may run him into the danger of losing the respect, perhaps following, of his followers? Here are some examples: ?O Prophet! Why do you prohibit [yourself from] what God has made lawful for you, seeking the approval of your wives? And God is Forgiving and Merciful.? (Quran 66:1) ??while you concealed within yourself that which God is to disclose and you feared the people, while God has more right that you fear Him..? (Quran 33:43) ?It is not for the Prophet and those who have believed to ask forgiveness for the polytheists, even if they were relatives, after it has become clear to them that they are companions of Hellfire.? (Quran 9:113) ?But as for he who came to you striving [for knowledge] while he fears [God], from him you are distracted. No! Indeed, they [these verses] are a reminder.? (Quran 80:8-11) If he were to hide anything, he would have hid these verses, but he recited them faithfully. ?And he [Muhammad] is not a withholder of [knowledge of] the unseen. And it [the Quran] is not the word of a devil, expelled [from the heavens]. So where are you going? It is nothing but a reminder to the worlds.? (Quran 81:24-27) The Prophet is cautioned, perhaps warned, in the following verses: ?Indeed, We have revealed to you, [O Muhammad], the Book in truth so you may judge between the people by that which God has shown you. And do not be an advocate for the deceitful. And seek forgiveness of God. Indeed, God is ever Forgiving and Merciful. And do not argue on behalf of those who deceive themselves. Indeed, God loves not the one who is a habitually sinful deceiver. They conceal [their evil intentions and deeds] from the people, but they cannot conceal [them] from God, and He is with them [in His knowledge] when they spend the night in such as He does not accept of speech. And God ever is encompassing of what they do,. Here you are ? those who argue on their behalf in [this] worldly life ? but who will argue with God for them on the Day of Resurrection, or who will [then] be their representative? And whoever does a wrong or wrongs himself but then seeks forgiveness of God will find God Forgiving and Merciful. And whoever earns [i.e., commits] a sin only earns it against himself. And God is ever Knowing and Wise. But whoever earns an offense or a sin and then blames it on an innocent [person] has taken upon himself slander and manifest sin. And if it was not for the favor of God upon you, [O Muhammad], and His mercy, a group of them would have determined to mislead you. But they do not mislead except themselves, and they will not harm you at all. And God has revealed to you the Book and wisdom and has taught you that which you did not know. And ever has the favor of God upon you been great.? (Quran 4:105-113) These verses explain a situation in which a man from the Muslim inhabitants of Medina stole a piece of armor and hid it in the property of his Jewish neighbor. When the owners of the armor caught up with him he denied any wrongdoing, and the armor was discovered with the Jewish man. He, however, pointed to his Muslim neighbor, also denying his involvement in the crime. The people from the Muslim?s tribe went to the Prophet to plead on his behalf, and the Prophet began to incline towards them till the above verses were revealed clearing the Jewish man of wrongdoing. All this despite the Jew?s rejection of Muhammad?s prophethood! The verses instructed Prophet Muhammad himself not to side with the deceitful! The verses: ??and do not be an advocate for the deceitful and seek forgiveness of God?and if it was not for the favor of God upon you, [O Muhammad], and His mercy, a group of them would have determined to mislead you.? If Muhammad himself authored the Quran, thus being a lying imposter, he would have made sure that there was nothing in existence which could jeopardize the gaining of followers and supporters. The fact that the Quran, on various occasions, reprimands the Prophet in certain issues in which he had made in incorrect judgment is in itself a proof that it was not authored by him. [1] ?Muhammad: His Life Based on the Earliest Sources? by Martin Lings, p. 34. http://www.islamreligion.com/articles/338/ thank you From karthik.sharma at gmail.com Thu May 2 18:14:22 2013 From: karthik.sharma at gmail.com (karthik.sharma at gmail.com) Date: Thu, 2 May 2013 15:14:22 -0700 (PDT) Subject: query from sqlalchemy returns AttributeError: 'NoneType' object Message-ID: from pox.core import core import pox.openflow.libopenflow_01 as of import re import datetime from sqlalchemy import create_engine, ForeignKey from sqlalchemy import Column, Date, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship, backref from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.sql.expression import exists log = core.getLogger() engine = create_engine('sqlite:///nwtopology.db', echo=False) Base = declarative_base() Session = sessionmaker(bind=engine) session = Session() ######################################################################## class SourcetoPort(Base): """""" __tablename__ = 'source_to_port' id = Column(Integer, primary_key=True) port_no = Column(Integer) src_address = Column(String,index=True) #---------------------------------------------------------------------- def __init__(self, src_address,port_no): """""" self.src_address = src_address self.port_no = port_no ######################################################################## #create tables Base.metadata.create_all(engine) class Tutorial (object): def __init__ (self, connection): self.connection = connection connection.addListeners(self) # Use this table to keep track of which ethernet address is on # which switch port (keys are MACs, values are ports). self.mac_to_port = {} self.matrix={} #This will keep track of the traffic matrix. #matrix[i][j]=number of times a packet from i went to j def send_packet (self, buffer_id, raw_data, out_port, in_port): #print "calling send_packet" #Sends a packet out of the specified switch port. msg = of.ofp_packet_out() msg.in_port = in_port msg.data = raw_data # Add an action to send to the specified port action = of.ofp_action_output(port = out_port) msg.actions.append(action) # Send message to switch self.connection.send(msg) def act_like_hub (self, packet, packet_in): #flood packet on all ports self.send_packet(packet_in.buffer_id, packet_in.data, of.OFPP_FLOOD, packet_in.in_port) def act_like_switch (self, packet, packet_in): """ Implement switch-like behavior. """ # Learn the port for the source MAC #print "RECIEVED FROM PORT ",packet_in.in_port , "SOURCE ",packet.src # create a Session #Session = sessionmaker(bind=engine) #session = Session() self.mac_to_port[packet.src]=packet_in.in_port #if self.mac_to_port.get(packet.dst)!=None: #print "count for dst",session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count(),str(packet.dst) #if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count(): if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None: #send this packet print "got info from the database" q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).one() self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port) #create a flow modification message msg = of.ofp_flow_mod() #set the fields to match from the incoming packet msg.match = of.ofp_match.from_packet(packet) #send the rule to the switch so that it does not query the controller again. msg.actions.append(of.ofp_action_output(port=q_res.port_no)) #push the rule self.connection.send(msg) else: #flood this packet out as we don't know about this node. print "flooding the first packet" self.send_packet(packet_in.buffer_id, packet_in.data, of.OFPP_FLOOD, packet_in.in_port) #self.matrix[(packet.src,packet.dst)]+=1 entry = SourcetoPort(src_address=str(packet.src) , port_no=packet_in.in_port) #add the record to the session object session.add(entry) #add the record to the session object session.commit() def _handle_PacketIn (self, event): """ Handles packet in messages from the switch. """ packet = event.parsed # This is the parsed packet data. if not packet.parsed: log.warning("Ignoring incomplete packet") return packet_in = event.ofp # The actual ofp_packet_in message. #self.act_like_hub(packet, packet_in) self.act_like_switch(packet, packet_in) def launch (): """ Starts the component """ def start_switch (event): log.debug("Controlling %s" % (event.connection,)) Tutorial(event.connection) core.openflow.addListenerByName("ConnectionUp", start_switch) When I run the above code I get the following error: The problem that I am facing is for some reason if I use if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None: in place of count query. #if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count(): The querying from the database q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).first() self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port) is giving the following error: DEBUG:core:POX 0.1.0 (betta) going up... DEBUG:core:Running on CPython (2.7.3/Aug 1 2012 05:14:39) DEBUG:core:Platform is Linux-3.5.0-23-generic-x86_64-with-Ubuntu-12.04-precise INFO:core:POX 0.1.0 (betta) is up. DEBUG:openflow.of_01:Listening on 0.0.0.0:6633 INFO:openflow.of_01:[00-00-00-00-00-02 1] connected DEBUG:tutorial:Controlling [00-00-00-00-00-02 1] got info from the database ERROR:core:Exception while handling Connection!PacketIn... Traceback (most recent call last): File "/home/karthik/pox/pox/lib/revent/revent.py", line 234, in raiseEventNoErrors return self.raiseEvent(event, *args, **kw) File "/home/karthik/pox/pox/lib/revent/revent.py", line 281, in raiseEvent rv = event._invoke(handler, *args, **kw) File "/home/karthik/pox/pox/lib/revent/revent.py", line 159, in _invoke return handler(self, *args, **kw) File "/home/karthik/pox/tutorial.py", line 118, in _handle_PacketIn self.act_like_switch(packet, packet_in) File "/home/karthik/pox/tutorial.py", line 86, in act_like_switch self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port) AttributeError: 'NoneType' object has no attribute 'port_no' got info from the database ERROR:core:Exception while handling Connection!PacketIn... From python at mrabarnett.plus.com Thu May 2 19:17:41 2013 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 May 2013 00:17:41 +0100 Subject: query from sqlalchemy returns AttributeError: 'NoneType' object In-Reply-To: References: Message-ID: <5182F415.8080003@mrabarnett.plus.com> On 02/05/2013 23:14, karthik.sharma at gmail.com wrote: [snip] > > ######################################################################## > class SourcetoPort(Base): > """""" > __tablename__ = 'source_to_port' > id = Column(Integer, primary_key=True) > port_no = Column(Integer) > src_address = Column(String,index=True) > > #---------------------------------------------------------------------- > def __init__(self, src_address,port_no): > """""" > self.src_address = src_address The indentation of this line looks wrong: > self.port_no = port_no > > ######################################################################## [snip] From davea at davea.name Thu May 2 19:29:11 2013 From: davea at davea.name (Dave Angel) Date: Thu, 02 May 2013 19:29:11 -0400 Subject: query from sqlalchemy returns AttributeError: 'NoneType' object In-Reply-To: References: Message-ID: <5182F6C7.2030108@davea.name> On 05/02/2013 06:14 PM, karthik.sharma at gmail.com wrote: > from pox.core import core > import pox.openflow.libopenflow_01 as of > import re > import datetime > You're mixing tabs and space, so all bets are off. No promise that the compiler will interpret the indentations the same way you see them. Use your editor to expand all the tabs to space, then check whether the code still makes sense to you. Then if you want to post a new question, show us the new version of code. Also this time post the error traceback that matches the version of the code you're showing us. And don't bother double-posting on tutor. -- DaveA From titanix88 at gmail.com Thu May 2 19:34:40 2013 From: titanix88 at gmail.com (Mr. Joe) Date: Fri, 3 May 2013 05:34:40 +0600 Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. Message-ID: Is there any way to raise the original exception that made the call to __getattr__? I seem to stumble upon a problem where multi-layered attribute failure gets obscured due to use of __getattr__. Here's a dummy code to demonstrate my problems: """ import traceback class BackupAlphabet(object): pass class Alphabet(object): @property def a(self): return backupalphabet.a def __getattr__(self, name): if name == "b": return "banana" raise AttributeError( "'{0} object has no attribute '{1}'" .format(self.__class__.__name__, name)) alphabet = Alphabet() backupalphabet = BackupAlphabet() print(alphabet.a) print(alphabet.b) """ Running the above code produces this: """ Traceback (most recent call last): File "example.py", line 26, in print(alphabet.a) File "example.py", line 20, in __getattr__ .format(self.__class__.__name__, name)) AttributeError: 'Alphabet object has no attribute 'a' """ While it's easy enough to identify the problem here, the traceback is rather unhelpful in complex situations. Any comments? Regards, TB -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Thu May 2 20:20:39 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 May 2013 00:20:39 GMT Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. References: Message-ID: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 May 2013 05:34:40 +0600, Mr. Joe wrote: > Is there any way to raise the original exception that made the call to > __getattr__? No. There is some discussion on the Python-Dev mailing list about adding better error reporting to AttributeError, but that may not go anywhere, and even if it does, it won't help you until you have dropped all support for versions below Python 3.4 or 3.5. > class BackupAlphabet(object): > pass > > > class Alphabet(object): > @property > def a(self): > return backupalphabet.a This raises AttributeError. Since __getattr__ uses AttributeError to decide whether or not the name exists, the behaviour shown is correct: Alphabet.a does not exist, as far as the Python semantics of attribute access are concerned. Either write better code *wink* that doesn't raise AttributeError from inside properties, or wrap them with something like this: class Alphabet(object): @property def a(self): try: return backupalphabet.a except AttributeError: raise MyCustomError where MyCustomError does NOT inherit from AttributeError. If you're doing this a lot, you can create a decorator to do the wrapping. -- Steven From titanix88 at gmail.com Fri May 3 03:52:23 2013 From: titanix88 at gmail.com (Mr. Joe) Date: Fri, 3 May 2013 13:52:23 +0600 Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. In-Reply-To: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> References: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> Message-ID: Thanks for clearing up. Developers of python should address this issue, in my opinion. 3.4/3.5 maybe, but better late than never. Recently, I've been beaten back for using some exotic features of python. One is this[ Took me hours to get to the bottom ]. The other one is 'property' decorator. I was using it extensively until I needed to make a child class. Then I came to know that 'property' does not play well with polymorphic code. :( I resorted to some lambda hacks learned from stackoverflow.com to solve the problem. I know that it's the correct way for decorators to work, but still, it would be nice to have a language level solution. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dieter at handshake.de Sat May 4 01:56:22 2013 From: dieter at handshake.de (dieter) Date: Sat, 04 May 2013 07:56:22 +0200 Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. References: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87a9obz3sp.fsf@handshake.de> "Mr. Joe" writes: > ... > Then I came to know that 'property' does not play well > with polymorphic code. :( Can you elaborate? I like "polymorphic code" and decorators (such a "property") never met a problem with the two working nicely together. > I resorted to some lambda hacks learned from > stackoverflow.com to solve the problem. I know that it's the correct way > for decorators to work, but still, it would be nice to have a language > level solution. There are two approaches: change the language or learn how the language works. I am very happy that the Python developers try hard to retain backward compatible and, consequently, are very reluctant towards language changes. I would hate should the decorator behavior change in an incompatible way. On the other hand, decorators are very easy to understand: they are just syntactic sugar: @ def f(...): ... is equivalent to: def f(...): ... f = (f) The "property" decorator uses an additional concept: "descriptor"s. A "descriptor" allows you to customize attribute access. These two concepts graped, the "property" decorator should no longer cause surprises -- even in "polymorphic code". From steve+comp.lang.python at pearwood.info Sat May 4 02:29:22 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 May 2013 06:29:22 GMT Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. References: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5184aac2$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 May 2013 13:52:23 +0600, Mr. Joe wrote: > Thanks for clearing up. Developers of python should address this issue, > in my opinion. 3.4/3.5 maybe, but better late than never. > > Recently, I've been beaten back for using some exotic features of > python. What do you consider "exotic"? Neither properties nor __getattr__ are exotic, although of course like all features of a language they have their own quirks. > One is this[ Took me hours to get to the bottom ]. Well, of course hindsight is 20:20, and I don't know how complicated your actual code is, but "hours" seems a bit poor. I could believe half an hour. Maybe an hour. But of course everything is easy to the guy who doesn't have to do it. > The other one > is 'property' decorator. I was using it extensively until I needed to > make a child class. Then I came to know that 'property' does not play > well with polymorphic code. :( I don't understand what you are trying to say. Properties can be as polymorphic as any other Python function. > I resorted to some lambda hacks learned > from stackoverflow.com to solve the problem. That's not a good sign. Any sentence containing "hacks" and "stackoverflow" is a warning that you're probably doing something wrong. -- Steven From titanix88 at gmail.com Tue May 14 11:09:54 2013 From: titanix88 at gmail.com (Mr. Joe) Date: Tue, 14 May 2013 21:09:54 +0600 Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. In-Reply-To: <5184aac2$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> <5184aac2$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: Sorry for digging this old topic back. I see that my "'property' does not play well with polymorphic code" comment generated some controversy. So here's something in my defense: Here's the link to stackoveflow topic I am talking about: http://stackoverflow.com/questions/237432/python-properties-and-inheritance The solution that fits my taste: http://stackoverflow.com/a/14349742 A related blogpost: http://requires-thinking.blogspot.com/2006/03/note-to-self-python-properties-are-non.html Yes, I like decorators and descriptors. I also like the ability to create a "virtual property" in a python class by binding a bunch of methods as setter/getter. But I find the implementation of this "virtual property feature" a bit awkward sometimes - every time I need to override a getter/setter in a child class, I need to decorate them again. Some of you may like this "explicitness", but I don't. To Steven D'Aprano: Seriously, what's all the bashing in your last reply about? You dissected my "thank-you reply" more strictly than the python interpreter checking for syntax errors. Not in a mood for fight, but I find your opinions about "bug finding time", "hacks" and "stackoverflow" quite silly. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dieter at handshake.de Wed May 15 02:15:53 2013 From: dieter at handshake.de (dieter) Date: Wed, 15 May 2013 08:15:53 +0200 Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. References: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> <5184aac2$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87bo8cvkdi.fsf@handshake.de> "Mr. Joe" writes: > ... > Sorry for digging this old topic back. I see that my "'property' does not > play well with polymorphic code" comment generated some controversy. So > here's something in my defense: I did not intend to "attack" you. > ... > Yes, I like decorators and descriptors. I also like the ability to create a > "virtual property" in a python class by binding a bunch of methods as > setter/getter. But I find the implementation of this "virtual property > feature" a bit awkward sometimes - every time I need to override a > getter/setter in a child class, I need to decorate them again. Some of you > may like this "explicitness", but I don't. True, I have been hit by this, too - not with "property" but with other decorators (those for caching). But, after reflection, I came to the conclusion that I should be happy with this feature: If Python would automatically redecorate overridden methods in a derived class, I would have no control over the process. What if I need the undecorated method or a differently decorated method (an uncached or differently cached method, in my case)? Your getter/setter use case can quite easily be solved with a class "DelayedMethodAccessor": class DelayedMethodAccess(object): """ def __init__(self, name): self.__name = name def __call__(self, inst, *args, **kw): return getattr(inst, self.__name)(*args, **kw) You can then use: prop = property(DelayedMethodAccess(""), ...) Or define a new decorator "property_by_name" and then use prop = property_by_name("", ...) Of course, this requires that the property name and the getter/setter names differ, but this should be naturally enough. Python's current behavior is very natural once you know that decorators are just syntactic sugar and @ def ... simply means: def ... f = (f) True, this is no perfect fit for all use cases - but such a fit (if it existed at all) would have to be so complex that only experts could understand it. From titanix88 at gmail.com Wed May 15 12:38:10 2013 From: titanix88 at gmail.com (Mr. Joe) Date: Wed, 15 May 2013 22:38:10 +0600 Subject: Debugging difficulty in python with __getattr__, decorated properties and AttributeError. In-Reply-To: <87bo8cvkdi.fsf@handshake.de> References: <518302d7$0$29971$c3e8da3$5496439d@news.astraweb.com> <5184aac2$0$29997$c3e8da3$5496439d@news.astraweb.com> <87bo8cvkdi.fsf@handshake.de> Message-ID: On Wed, May 15, 2013 at 12:15 PM, dieter wrote: > > If Python would automatically redecorate overridden methods in a derived > class, I would have no control over the process. What if I need > the undecorated method or a differently decorated method (an > uncached or differently cached method, in my case)? > On a second thought, I am convinced by your argument. > Your getter/setter use case can quite easily be solved > with a class "DelayedMethodAccessor": > > class DelayedMethodAccess(object): > """ > def __init__(self, name): self.__name = name > def __call__(self, inst, *args, **kw): > return getattr(inst, self.__name)(*args, **kw) > > You can then use: > > prop = property(DelayedMethodAccess(""), ...) > > Or define a new decorator "property_by_name" and then > use > > prop = property_by_name("", ...) > > Of course, this requires that the property name and the getter/setter names > differ, but this should be naturally enough. > > > Python's current behavior is very natural once you know that > decorators are just syntactic sugar and > > @ > def ... > > simply means: > > def ... > f = (f) > > True, this is no perfect fit for all use cases - but > such a fit (if it existed at all) would have to be so complex > that only experts could understand it. Thanks for these really nice patterns. They fits my problem very well. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dreadpiratejeff at gmail.com Thu May 2 21:54:29 2013 From: dreadpiratejeff at gmail.com (J) Date: Thu, 2 May 2013 21:54:29 -0400 Subject: question about try/except blocks Message-ID: I have this function in a class: def write_file(self, data, dest): with open(dest, 'wb', 0) as outfile: try: print("IN WRITE_FILE") outfile.write(self.data) except IOError as exc: logging.error("Unable to write data to %s: %s", dest, exc) return False else: outfile.flush() os.fsync(outfile.fileno()) return True Which is simply called like this: test.write_file(test.data, target_file) However, when this is called on a read-only filesystem, it throws an OSError: File "./scripts/removable_storage_test", line 118, in write_file with open(dest, 'wb', 0) as outfile: OSError: [Errno 30] Read-only file system: '/media/bladernr/5747-AD2E/tmpfbzsxk.0' So what I am not sure of is how to handle this? Would it be better to wrap the call and catch the OSError there, or wrap the whole with open() block in the function itself? My thought is to wrap the with open() call in the function so that I'm not wrapping the function call every time I use the class somewhere, but then I am not sure of that as it leads to nested try blocks like so: try: with open(dest, 'wb', 0) as outfile: try: stuff except IOError as exec: more stuff else: other stuff except OSError as exc: error handling stuff return False I think, functionally, that should work, but should nested try/except blocks be avoided? From jeanpierreda at gmail.com Thu May 2 22:23:11 2013 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Thu, 2 May 2013 22:23:11 -0400 Subject: question about try/except blocks In-Reply-To: References: Message-ID: On Thu, May 2, 2013 at 9:54 PM, J wrote: > Would it be better to wrap the call and catch the OSError there, or > wrap the whole with open() block in the function itself? > > My thought is to wrap the with open() call in the function so that I'm > not wrapping the function call every time I use the class somewhere, > but then I am not sure of that as it leads to nested try blocks like > so: It definitely shouldn't be done that way, since you might catch exceptions in other circumstances too. Try this: try: f = open(dest, 'wb', 0) except OSError as exc: ... with f: try: ... except IOError as exc: ... else: ... -- Devin > try: > with open(dest, 'wb', 0) as outfile: > try: > stuff > except IOError as exec: > more stuff > else: > other stuff > except OSError as exc: > error handling stuff > return False > > > I think, functionally, that should work, but should nested try/except > blocks be avoided? From steve+comp.lang.python at pearwood.info Thu May 2 23:02:34 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 May 2013 03:02:34 GMT Subject: question about try/except blocks References: Message-ID: <518328ca$0$29997$c3e8da3$5496439d@news.astraweb.com> On Thu, 02 May 2013 21:54:29 -0400, J wrote: > I have this function in a class: > > def write_file(self, data, dest): > with open(dest, 'wb', 0) as outfile: > try: > print("IN WRITE_FILE") > outfile.write(self.data) > except IOError as exc: > logging.error("Unable to write data to %s: %s", dest, > exc) return False > else: > outfile.flush() > os.fsync(outfile.fileno()) > return True [...] > I think, functionally, that should work, but should nested try/except > blocks be avoided? Not particularly. Try/except is cheap to set up, nesting them doesn't cost much. But having said that, your code as given does not protect against rare but possible errors. For example, just because you can open the file for writing doesn't mean you can write to it; just because you can write to it doesn't mean flush will succeed; just because flush succeeds doesn't mean that syncing to disk will succeed. Any IO operation might fail. While you can wrap each one individually, it's probably better to wrap the whole lot at once. I'd write it like this: def write_file(self, data, dest): try: with open(dest, 'wb', 0) as outfile: print("IN WRITE_FILE") outfile.write(self.data) outfile.flush() os.fsync(outfile.fileno()) except (OSError, IOError) as exc: logging.error("Error writing data to %s: %s", dest, exc) return False return True File IO is one of the rare exceptions to the idea that try/except blocks should wrap the least amount of code possible. -- Steven From valbendan at outlook.com Fri May 3 03:16:09 2013 From: valbendan at outlook.com (dabaichi) Date: Fri, 3 May 2013 15:16:09 +0800 Subject: python2 input Message-ID: Hello guys: Do you have found that the built-in input() function is dangerous. Here is my code(python 2.x only): a = input("Input anything:") print("go here") If I input: sys.exit(0) print("go here") will not be execute, and the script will exit. --- news://freenews.netfront.net/ - complaints: news at netfront.net --- From steve+comp.lang.python at pearwood.info Fri May 3 03:29:56 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 May 2013 07:29:56 GMT Subject: python2 input References: Message-ID: <51836774$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 03 May 2013 15:16:09 +0800, dabaichi wrote: > Hello guys: > Do you have found that the built-in input() function is dangerous. Correct. Use raw_input instead. -- Steven From rosuav at gmail.com Fri May 3 03:31:59 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 May 2013 17:31:59 +1000 Subject: python2 input In-Reply-To: References: Message-ID: On Fri, May 3, 2013 at 5:16 PM, dabaichi wrote: > Hello guys: > Do you have found that the built-in input() function is dangerous. > Here is my code(python 2.x only): > > a = input("Input anything:") > print("go here") > > If I input: > sys.exit(0) > print("go here") will not be execute, and the script will exit. Yeah, that's kinda discovered. In Python 2, you should be using raw_input() for that kind of thing. In Python 3, raw_input() was renamed to input(), and if you want the semantics of Py2 input(), it's eval(input()), which makes it obvious what's going on. ChrisA From michele.simionato at gmail.com Fri May 3 07:24:18 2013 From: michele.simionato at gmail.com (Michele Simionato) Date: Fri, 3 May 2013 04:24:18 -0700 (PDT) Subject: Trying to understand the memory occupation of big lists Message-ID: I have a memory leak in a program using big arrays. With the goal of debugging it I run into the memory_profiler module. Then I discovered something which is surprising to me. Please consider the following script: $ cat memtest.py import gc from memory_profiler import profile @profile def test1(): a = [0] * 1024 * 1024 del a gc.collect() # nothing change if I comment this @profile def test2(): for i in range(10): a = [0] * 1024 * 1024 del a gc.collect() # nothing change if I comment this test1() test2() Here is its output, on a Linux 64 bit machine: $ python memtest.py Filename: memtest.py Line # Mem usage Increment Line Contents ================================================ 5 @profile 6 9.250 MB 0.000 MB def test1(): 7 17.246 MB 7.996 MB a = [0] * 1024 * 1024 8 9.258 MB -7.988 MB del a 9 9.258 MB 0.000 MB gc.collect() # nothing change if I comment this Filename: memtest.py Line # Mem usage Increment Line Contents ================================================ 12 @profile 13 9.262 MB 0.000 MB def test2(): 14 17.270 MB 8.008 MB for i in range(10): 15 17.270 MB 0.000 MB a = [0] * 1024 * 1024 16 17.270 MB 0.000 MB del a 17 17.270 MB 0.000 MB gc.collect() # nothing change if I comment this In the first case the memory is released (even if strangely not completely, 7.996 != 7.988), in the second case the memory is not. Why it is so? I did expect gc.collect() to free the memory but it is completely ininfluent. In the second cases there are 10 lists with 8 MB each, so 80 MB are allocated and 72 released, but 8 MB are still there apparently. It does not look like a problem of mem_profile, this is what observe with top too. Any ideas? From davea at davea.name Fri May 3 08:16:34 2013 From: davea at davea.name (Dave Angel) Date: Fri, 03 May 2013 08:16:34 -0400 Subject: Trying to understand the memory occupation of big lists In-Reply-To: References: Message-ID: <5183AAA2.7000809@davea.name> On 05/03/2013 07:24 AM, Michele Simionato wrote: > I have a memory leak in a program using big arrays. Actually, big lists. Python also has arrays, and they're entirely different. With the goal of debugging it I run into the memory_profiler module. Then I discovered something which is surprising to me. Please consider the following script: > > $ cat memtest.py > import gc > from memory_profiler import profile > > > @profile > def test1(): > a = [0] * 1024 * 1024 > del a > gc.collect() # nothing change if I comment this > > > @profile > def test2(): > for i in range(10): > a = [0] * 1024 * 1024 > del a > gc.collect() # nothing change if I comment this > > > test1() > test2() > > Here is its output, on a Linux 64 bit machine: > > $ python memtest.py > Filename: memtest.py > > Line # Mem usage Increment Line Contents > ================================================ > 5 @profile > 6 9.250 MB 0.000 MB def test1(): > 7 17.246 MB 7.996 MB a = [0] * 1024 * 1024 > 8 9.258 MB -7.988 MB del a > 9 9.258 MB 0.000 MB gc.collect() # nothing change if I comment this > > > Filename: memtest.py > > Line # Mem usage Increment Line Contents > ================================================ > 12 @profile > 13 9.262 MB 0.000 MB def test2(): > 14 17.270 MB 8.008 MB for i in range(10): > 15 17.270 MB 0.000 MB a = [0] * 1024 * 1024 > 16 17.270 MB 0.000 MB del a > 17 17.270 MB 0.000 MB gc.collect() # nothing change if I comment this > > In the first case the memory is released (even if strangely not > completely, 7.996 != 7.988), in the second case the memory is not. Why it is so? I did expect gc.collect() to free the memory but it is completely ininfluent. In the second cases there are 10 lists with 8 MB each, so > 80 MB are allocated and 72 released, but 8 MB are still there apparently. > It does not look like a problem of mem_profile, this is what observe with > top too. > > Any ideas? > I haven't played with profile, so my comments are limited to the direct code. gd.collect() has nothing to do in either of these functions, since the memory has already been released by the ref-count logic. Only in the case of a circular reference is the gc.collect() useful. If you want to see gc.collect() in action create two large objects that reference each other and a small one that references one of them. Del the first two and then the third, and the memory cannot be released since the ref counts are nonzero. Then do a gc.collect() which will realize that you have no way to reference either of the two large objects. I suspect that profile is only looking at the memory from the point of view of the OS. No block of memory can be released to the OS unless it's entirely freed. My guess is that in the second case the variable i (or some other internal one relating to the loop) is in the same block with one of those lists. The point is that CPython uses the C malloc() and free() functions, and they have their own limitations. Most of the time when free() is called, the memory is NOT released to the OS, but is still made available within Python for future use. -- DaveA From maarten.sneep at knmi.nl Fri May 3 11:20:27 2013 From: maarten.sneep at knmi.nl (Maarten) Date: Fri, 3 May 2013 08:20:27 -0700 (PDT) Subject: Trying to understand the memory occupation of big lists In-Reply-To: References: Message-ID: <216083a7-fb00-471e-b123-af19ea6a7aef@googlegroups.com> I made a few changes: import gc from memory_profiler import profile @profile def test1(): a = [0] * 1024**2 del a a = [0] * 1024**2 del a a = [0] * 1024**2 del a a = [0] * 1024**2 del a a = [0] * 1024**2 del a a = [0] * 1024**2 del a a = [0] * 1024**2 del a a = [0] * 1024**2 del a a = [0] * 1024**2 del a a = [0] * 1024**2 del a gc.collect() # nothing change if I comment this @profile def test2(): for i in range(10): a = [0] * 1024**2 del a del i gc.collect() # nothing change if I comment this test1() test2() # end of code Output: Filename: profile.py Line # Mem usage Increment Line Contents ================================================ 5 @profile 6 8.688 MB 0.000 MB def test1(): 7 16.691 MB 8.004 MB a = [0] * 1024**2 8 8.688 MB -8.004 MB del a 9 16.680 MB 7.992 MB a = [0] * 1024**2 10 16.680 MB 0.000 MB del a 11 16.680 MB 0.000 MB a = [0] * 1024**2 12 16.680 MB 0.000 MB del a 13 16.680 MB 0.000 MB a = [0] * 1024**2 14 16.680 MB 0.000 MB del a 15 16.680 MB 0.000 MB a = [0] * 1024**2 16 16.680 MB 0.000 MB del a 17 16.680 MB 0.000 MB a = [0] * 1024**2 18 16.680 MB 0.000 MB del a 19 16.680 MB 0.000 MB a = [0] * 1024**2 20 16.680 MB 0.000 MB del a 21 16.680 MB 0.000 MB a = [0] * 1024**2 22 16.680 MB 0.000 MB del a 23 16.680 MB 0.000 MB a = [0] * 1024**2 24 16.680 MB 0.000 MB del a 25 16.680 MB 0.000 MB a = [0] * 1024**2 26 16.680 MB 0.000 MB del a 27 16.680 MB 0.000 MB gc.collect() # nothing change if I comment this Filename: profile.py Line # Mem usage Increment Line Contents ================================================ 30 @profile 31 16.691 MB 0.000 MB def test2(): 32 16.691 MB 0.000 MB for i in range(10): 33 16.691 MB 0.000 MB a = [0] * 1024**2 34 16.691 MB 0.000 MB del a 35 16.691 MB 0.000 MB del i 36 16.691 MB 0.000 MB gc.collect() # nothing change if I comment this If I make the two functions identical, the behave the same. Maarten From alec.taylor6 at gmail.com Fri May 3 10:22:53 2013 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Sat, 4 May 2013 00:22:53 +1000 Subject: Highest performance Python framework for API exposure? Message-ID: Which Python framework has benchmarked the highest? I am looking for a solution to initially run on Heroku or OpenShift; but eventually run on a pure IaaS. Will expose the interfaces RESTfully as JSON, purposed to act as the "glue" between the endpoints, databases (incl. caches) and queues. An additional requirement is support for serving static, downloadable content efficiently. Currently looking at Falcon, Bottle and Werkzeug. Suggestions are greatly appreciated. Thanks, Alec Taylor From roy at panix.com Fri May 3 20:18:49 2013 From: roy at panix.com (Roy Smith) Date: Fri, 03 May 2013 20:18:49 -0400 Subject: Highest performance Python framework for API exposure? References: Message-ID: In article , Alec Taylor wrote: > Which Python framework has benchmarked the highest? > > I am looking for a solution to initially run on Heroku or OpenShift; > but eventually run on a pure IaaS. > > Will expose the interfaces RESTfully as JSON, purposed to act as the > "glue" between the endpoints, databases (incl. caches) and queues. Based on my experience, it doesn't matter what framework you use. All that's going to matter is how fast you can pump data in and out of your database. The overhead added by the Python front-end probably won't be significant. Pick a framework that you find easy to use and don't sweat it. Invest your time worrying about your database. > An additional requirement is support for serving static, downloadable > content efficiently. You don't want to do that with Python. You want to do that with something that's optimized for serving static content (such as nginx). From ryankoch38 at gmail.com Fri May 3 13:18:58 2013 From: ryankoch38 at gmail.com (ryankoch38 at gmail.com) Date: Fri, 3 May 2013 10:18:58 -0700 (PDT) Subject: First python program, syntax error in while loop Message-ID: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> title = "Guess my number game:" print title.title() raw_input("Press any key to continue..") import random tries = 0 number = random.randrange(99) + 1 guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :") while (guess != number): if (guess > number): number = int(raw_input("Sorry, my number is lower than that! \n Try again:") tries += 1 else if (guess < number): number = int(raw_input("Sorry, my number is higher than that! \n Try again:") tries += 1 print "Congratulations, you guessed my number! \n And it only took you" tries "tries!" raw_input("\n\n Press any key to exit..") ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P From kwpolska at gmail.com Fri May 3 13:26:46 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Fri, 3 May 2013 19:26:46 +0200 Subject: First python program, syntax error in while loop In-Reply-To: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: On Fri, May 3, 2013 at 7:18 PM, wrote: > title = "Guess my number game:" > print title.title() > raw_input("Press any key to continue..") > > import random > > tries = 0 > number = random.randrange(99) + 1 > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :") > > while (guess != number): > if (guess > number): > number = int(raw_input("Sorry, my number is lower than that! \n Try again:") > tries += 1 > else if (guess < number): > number = int(raw_input("Sorry, my number is higher than that! \n Try again:") > tries += 1 > print "Congratulations, you guessed my number! \n And it only took you" tries "tries!" > > raw_input("\n\n Press any key to exit..") > > ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P > -- > http://mail.python.org/mailman/listinfo/python-list 1. post full tracebacks. 2. The contents of your while loop must be indented, just like the contents of the if/else if statement you have there. So, four spaces before the 'if', 'else if' and 'print' lines; eight before 'number' and 'tries' lines. -- Kwpolska | GPG KEY: 5EAAEA16 stop html mail | always bottom-post http://asciiribbon.org | http://caliburn.nl/topposting.html From gordon at panix.com Fri May 3 13:27:05 2013 From: gordon at panix.com (John Gordon) Date: Fri, 3 May 2013 17:27:05 +0000 (UTC) Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: In <24c5856e-a30a-41bd-aa4a-0e594734e1f8 at googlegroups.com> ryankoch38 at gmail.com writes: > title = "Guess my number game:" > print title.title() > raw_input("Press any key to continue..") > import random > tries = 0 > number = random.randrange(99) + 1 > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :") > while (guess != number): > if (guess > number): > number = int(raw_input("Sorry, my number is lower than that! \n Try again:") > tries += 1 > else if (guess < number): > number = int(raw_input("Sorry, my number is higher than that! \n Try again:") > tries += 1 > print "Congratulations, you guessed my number! \n And it only took you" tries "tries!" > raw_input("\n\n Press any key to exit..") > ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P You're missing a second closing parentheses on the line where the user inputs their guess. Also, you need to indent the 'if' statement underneath the while loop. -- 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 Fri May 3 13:36:52 2013 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 May 2013 18:36:52 +0100 Subject: First python program, syntax error in while loop In-Reply-To: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: <5183F5B4.5030801@mrabarnett.plus.com> On 03/05/2013 18:18, ryankoch38 at gmail.com wrote: > title = "Guess my number game:" > print title.title() > raw_input("Press any key to continue..") > > import random > > tries = 0 > number = random.randrange(99) + 1 > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :") > > while (guess != number): > if (guess > number): > number = int(raw_input("Sorry, my number is lower than that! \n Try again:") > tries += 1 > else if (guess < number): > number = int(raw_input("Sorry, my number is higher than that! \n Try again:") > tries += 1 > print "Congratulations, you guessed my number! \n And it only took you" tries "tries!" > > raw_input("\n\n Press any key to exit..") > > ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P > Indentation in important in Python. Also, "else if" should be "elif", and you ask for a guess only once. title = "Guess my number game:" print title.title() raw_input("Press any key to continue..") import random tries = 0 number = random.randrange(99) + 1 # Ask for first guess. guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :") while guess != number: if guess > number: number = int(raw_input("Sorry, my number is lower than that! \n Try again:") tries += 1 elif guess < number: number = int(raw_input("Sorry, my number is higher than that! \n Try again:") tries += 1 # Ask for next guess. guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :") print "Congratulations, you guessed my number! \n And it only took you" tries "tries!" raw_input("\n\n Press any key to exit..") From zachary.ware+pylist at gmail.com Fri May 3 13:37:39 2013 From: zachary.ware+pylist at gmail.com (Zachary Ware) Date: Fri, 3 May 2013 12:37:39 -0500 Subject: First python program, syntax error in while loop In-Reply-To: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: On Fri, May 3, 2013 at 12:18 PM, wrote: > title = "Guess my number game:" > print title.title() > raw_input("Press any key to continue..") > > import random > > tries = 0 > number = random.randrange(99) + 1 > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :") First up, there's a missing ) on each call to int(), which is causing the syntax error you see. The error points at "while" because "while" doesn't make sense in the call to int(). > > while (guess != number): > if (guess > number): After fixing the error above, you'll have another one here: you haven't indented the block to be executed in the while loop. Indentation is important in Python, it delimits code blocks and makes things more readable for people. > number = int(raw_input("Sorry, my number is lower than that! \n Try again:") > tries += 1 There's also another indentation error here: everything in the same block has to be indented to the same level. > else if (guess < number): > number = int(raw_input("Sorry, my number is higher than that! \n Try again:") > tries += 1 > print "Congratulations, you guessed my number! \n And it only took you" tries "tries!" And here will be another syntax error: you have to separate your arguments to 'print' with commas. > > raw_input("\n\n Press any key to exit..") > > ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P Once you have those errors fixed, I'll bet I can guess your number on the second try every time. I'll let you figure out why yourself, though ;). From gordon at panix.com Fri May 3 13:37:56 2013 From: gordon at panix.com (John Gordon) Date: Fri, 3 May 2013 17:37:56 +0000 (UTC) Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: In =?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?= writes: > 1. post full tracebacks. I almost responded with the same advice, but in this case the full traceback doesn't really tell us anything more: File "foo.py", line 11 while guess != number: ^ SyntaxError: invalid syntax -- 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 ryankoch38 at gmail.com Fri May 3 13:57:00 2013 From: ryankoch38 at gmail.com (ryankoch38 at gmail.com) Date: Fri, 3 May 2013 10:57:00 -0700 (PDT) Subject: First python program, syntax error in while loop In-Reply-To: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: <02b65e11-89c6-4639-9d93-27e1f90eed66@googlegroups.com> Okay, thank you very much for the timely replies, heres what i have now: title = "Guess my number game:" print title.title() raw_input("Press any key to continue..") import random number = random.randrange(99) + 1 tries = 0 guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")) while (guess != number): if (guess > number): number = int(raw_input("Sorry, my number is lower than that! \n Try again:")) tries += 1 else: number = int(raw_input("Sorry, my number is higher than that! \n Try again:")) tries += 1 print "good job, you won!" raw_input("\n\n Press any key to exit..") ## it seems to stick with "higher" or "lower" after my first guess, whichever it ## is From gordon at panix.com Fri May 3 14:21:50 2013 From: gordon at panix.com (John Gordon) Date: Fri, 3 May 2013 18:21:50 +0000 (UTC) Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <02b65e11-89c6-4639-9d93-27e1f90eed66@googlegroups.com> Message-ID: In <02b65e11-89c6-4639-9d93-27e1f90eed66 at googlegroups.com> ryankoch38 at gmail.com writes: > Okay, thank you very much for the timely replies, heres what i have now: > title = "Guess my number game:" > print title.title() > raw_input("Press any key to continue..") > import random > number = random.randrange(99) + 1 > tries = 0 > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")) > while (guess != number): > if (guess > number): > number = int(raw_input("Sorry, my number is lower than that! \n Try again:")) > tries += 1 > else: > number = int(raw_input("Sorry, my number is higher than that! \n Try again:")) > tries += 1 > > print "good job, you won!" > > raw_input("\n\n Press any key to exit..") > ## it seems to stick with "higher" or "lower" after my first guess, whichever it > ## is When the user re-enters their guess in the while loop, you're assigning the input to "number" instead of "guess". -- 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 ryankoch38 at gmail.com Fri May 3 14:34:42 2013 From: ryankoch38 at gmail.com (ryankoch38 at gmail.com) Date: Fri, 3 May 2013 11:34:42 -0700 (PDT) Subject: First python program, syntax error in while loop In-Reply-To: References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <02b65e11-89c6-4639-9d93-27e1f90eed66@googlegroups.com> Message-ID: <324dbba9-42d5-40c7-959d-c00e914925b6@googlegroups.com> Oh wow I can't believed I derped that hard!!!! Thanks Lol. From ryankoch38 at gmail.com Fri May 3 15:52:14 2013 From: ryankoch38 at gmail.com (ryankoch38 at gmail.com) Date: Fri, 3 May 2013 12:52:14 -0700 (PDT) Subject: First python program, syntax error in while loop In-Reply-To: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails.. title = "Guess my number game:" print title.title() raw_input("Press any key to continue..") import random number = random.randrange(99) + 1 tries = 0 guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")) while (guess != number): if (guess > number): guess = int(raw_input("Sorry, my number is lower than that! \n Try again:")) tries += 1 if (guess < number): guess = int(raw_input("Sorry, my number is higher than that! \n Try again:")) tries += 1 if (tries > 5): print "Sorry, you lost!" break print "\nCongratulations! You guessed my number in", tries, "tries" raw_input("\n\n Press any key to exit..") ## this results in the congratulations printing after the second/third guess and ## continuing From gordon at panix.com Fri May 3 16:04:00 2013 From: gordon at panix.com (John Gordon) Date: Fri, 3 May 2013 20:04:00 +0000 (UTC) Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: In ryankoch38 at gmail.com writes: > I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails The "Congratulations" message is inside the while loop; that's why it always prints. To break after five guesses, you could change the while loop to this: while (guess != number) and (tries < 5): And then after the while loop, put this if statement: if (tries < 5): print "\nCongratulations! You guessed my number in", tries, "tries" else: print "Sorry, you lost!" -- 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 ryankoch38 at gmail.com Fri May 3 16:15:50 2013 From: ryankoch38 at gmail.com (ryankoch38 at gmail.com) Date: Fri, 3 May 2013 13:15:50 -0700 (PDT) Subject: First python program, syntax error in while loop In-Reply-To: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Thank you! It's 100% functional now, here's the final project: title = "Guess my number game:" print title.title() raw_input("Press any key to continue..") import random number = random.randrange(99) + 1 tries = 0 guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")) while (guess != number) and (tries <5): if (guess > number): guess = int(raw_input("Sorry, my number is lower than that! \n Try again:")) tries += 1 if (guess < number): guess = int(raw_input("Sorry, my number is higher than that! \n Try again:")) tries += 1 if (tries <5): print "\nCongratulations! You guessed my number in", tries, "tries" else: print "\nSorry, you took too many tries to guess my number!" raw_input("\n\n Press any key to exit..") ## Maybe now I can work on a useful project From neilc at norwich.edu Fri May 3 16:30:23 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 3 May 2013 20:30:23 GMT Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On 2013-05-03, ryankoch38 at gmail.com wrote: > Thank you! It's 100% functional now, here's the final project: > > title = "Guess my number game:" > print title.title() > raw_input("Press any key to continue..") > > import random > > number = random.randrange(99) + 1 > tries = 0 > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")) > > while (guess != number) and (tries <5): > [...] > > if (tries <5): > print "\nCongratulations! You guessed my number in", tries, "tries" > else: > print "\nSorry, you took too many tries to guess my number!" > raw_input("\n\n Press any key to exit..") > > ## Maybe now I can work on a useful project Not quite yet. Players who guess correctly on the fifth try don't get credit. -- Neil Cerutti From gordon at panix.com Fri May 3 16:38:47 2013 From: gordon at panix.com (John Gordon) Date: Fri, 3 May 2013 20:38:47 +0000 (UTC) Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: In Neil Cerutti writes: > Not quite yet. Players who guess correctly on the fifth try don't > get credit. Are you sure? tries is initialized to zero and isn't incremented for the initial guess. -- 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 neilc at norwich.edu Mon May 6 08:06:36 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 6 May 2013 12:06:36 GMT Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On 2013-05-03, John Gordon wrote: > In Neil Cerutti writes: > >> Not quite yet. Players who guess correctly on the fifth try don't >> get credit. > > Are you sure? tries is initialized to zero and isn't > incremented for the initial guess. while (number != guess) and (tries < 5): Is the condition that concludes the game. After the game, you are told you lost if tries is not less than five, regardless of if number == guess. -- Neil Cerutti From breamoreboy at yahoo.co.uk Mon May 6 08:37:48 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 06 May 2013 13:37:48 +0100 Subject: First python program, syntax error in while loop In-Reply-To: References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On 06/05/2013 13:06, Neil Cerutti wrote: > On 2013-05-03, John Gordon wrote: >> In Neil Cerutti writes: >> >>> Not quite yet. Players who guess correctly on the fifth try don't >>> get credit. >> >> Are you sure? tries is initialized to zero and isn't >> incremented for the initial guess. > > while (number != guess) and (tries < 5): > > Is the condition that concludes the game. > > After the game, you are told you lost if tries is not less than > five, regardless of if number == guess. > One of these days I'll work out why some people insist on using superfluous parentheses in Python code. Could it be that they enjoy exercising their fingers by reaching for the shift key in conjunction with the 9 or 0 key? -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From neilc at norwich.edu Mon May 6 09:07:20 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 6 May 2013 13:07:20 GMT Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On 2013-05-06, Mark Lawrence wrote: > On 06/05/2013 13:06, Neil Cerutti wrote: >> On 2013-05-03, John Gordon wrote: >>> In Neil Cerutti writes: >>> >>>> Not quite yet. Players who guess correctly on the fifth try don't >>>> get credit. >>> >>> Are you sure? tries is initialized to zero and isn't >>> incremented for the initial guess. >> >> while (number != guess) and (tries < 5): >> >> Is the condition that concludes the game. >> >> After the game, you are told you lost if tries is not less than >> five, regardless of if number == guess. > > One of these days I'll work out why some people insist on using > superfluous parentheses in Python code. Could it be that they > enjoy exercising their fingers by reaching for the shift key in > conjunction with the 9 or 0 key? Superflous parenthesis are sometimes a nice aid to comprehension. I don't know about the above case, though. I don't think it hurts anything, but it doesn't add much. -- Neil Cerutti From roy at panix.com Mon May 6 09:08:44 2013 From: roy at panix.com (Roy Smith) Date: Mon, 06 May 2013 09:08:44 -0400 Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: In article , Mark Lawrence wrote: > > while (number != guess) and (tries < 5): > > One of these days I'll work out why some people insist on using > superfluous parentheses in Python code. Could it be that they enjoy > exercising their fingers by reaching for the shift key in conjunction > with the 9 or 0 key? There's lots of reasons. Some valid, some not. Lets dispense with the invalid reason first. They've come from C/C++/Java/whatever and are used to typing parens around the conditions for if/for/while statements. To them, I say, "Stop trying to write FORTRAN code in languages that aren't FORTRAN". In this case, however, I have no problem with the extra parens. Look at these two statements: >> while (number != guess) and (tries < 5): >> while number != guess and tries < 5: They have the same meaning. To correctly interpret the second one, you need to know that != and < bind tighter than "and". One could say that you should know that, and maybe you would be right. On the other hand, I've long since given up trying to remember operator precedence in various languages. If I ever have even the slightest doubt, I just go ahead and put in the extra parens. It takes another few ms to type, and it removes all ambiguity (for both me, and every future person who has to read my code). And, every once in a while, it keeps me from writing a subtle and hard-to-find bug because the precedence rules I was sure I had remembered correctly turned out to be wrong for the language I happened to be typing that day. BTW, in C, I used to write: return (foo) for years until somebody pointed out to me that return foo works. I just assumed that if I had to write: if (foo) while (foo) for (foo; bar; baz) then return (foo) made sense too. From rosuav at gmail.com Mon May 6 09:37:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 23:37:56 +1000 Subject: First python program, syntax error in while loop In-Reply-To: References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On Mon, May 6, 2013 at 11:08 PM, Roy Smith wrote: > On the other hand, I've long since given up trying to remember operator > precedence in various languages. If I ever have even the slightest > doubt, I just go ahead and put in the extra parens. If I ever have even the slightest doubt, I just go ahead and type " operator precedence" into a web search and check it :) Aside from utter insanity like PHP's ternary operator being wrongly associative, that covers pretty much everything. ChrisA From rustompmody at gmail.com Mon May 6 09:36:31 2013 From: rustompmody at gmail.com (rusi) Date: Mon, 6 May 2013 06:36:31 -0700 (PDT) Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On May 6, 6:08?pm, Roy Smith wrote: > BTW, in C, I used to write: > > return (foo) > > for years until somebody pointed out to me that > > return foo > > works. ?I just assumed that if I had to write: > > if (foo) > while (foo) > for (foo; bar; baz) > > then > > return (foo) > > made sense too. I guess its because K&R always show their examples with return (expr); and so we all assume its the propah way From roy at panix.com Mon May 6 11:31:54 2013 From: roy at panix.com (Roy Smith) Date: 6 May 2013 11:31:54 -0400 Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: In article , Chris Angelico wrote: >On Mon, May 6, 2013 at 11:08 PM, Roy Smith wrote: >> On the other hand, I've long since given up trying to remember operator >> precedence in various languages. If I ever have even the slightest >> doubt, I just go ahead and put in the extra parens. > >If I ever have even the slightest doubt, I just go ahead and type >" operator precedence" into a web search and check it :) Well, that solves the problem once, and it solves it for me. I figure if I'm not 100% sure, then maybe other people aren't 100% sure either, and my adding the extra parens helps them too. From tjreedy at udel.edu Mon May 6 16:11:35 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Mon, 06 May 2013 16:11:35 -0400 Subject: First python program, syntax error in while loop In-Reply-To: References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: On 5/6/2013 11:31 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: >> On Mon, May 6, 2013 at 11:08 PM, Roy Smith wrote: >>> On the other hand, I've long since given up trying to remember operator >>> precedence in various languages. If I ever have even the slightest >>> doubt, I just go ahead and put in the extra parens. >> >> If I ever have even the slightest doubt, I just go ahead and type >> " operator precedence" into a web search and check it :) > > Well, that solves the problem once, and it solves it for me. I figure > if I'm not 100% sure, then maybe other people aren't 100% sure either, > and my adding the extra parens helps them too. If you keep the Python docs handy, on or off line, the Language manual Expressions chapter ends with this single page (but better formatted as a table than here). But I sometimes add parens for quickness or readability. 6.15. Operator precedence The following table summarizes the operator precedences in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right ? see section Comparisons ? and exponentiation, which groups from right to left). Operator Description lambda Lambda expression if ? else Conditional expression or Boolean OR and Boolean AND not x Boolean NOT in, not in, is, is not, <, <=, >, >=, !=, == Comparisons, including membership tests and identity tests, | Bitwise OR ^ Bitwise XOR & Bitwise AND <<, >> Shifts +, - Addition and subtraction *, /, //, % Multiplication, division, remainder [5] +x, -x, ~x Positive, negative, bitwise NOT ** Exponentiation [6] x[index], x[index:index], x(arguments...), x.attribute Subscription, slicing, call, attribute reference (expressions...), [expressions...], {key: value...}, {expressions...} Binding or tuple display, list display, dictionary display, set display Footnotes [1] While abs(x%y) < abs(y) is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double-precision number, in order that -1e-100 % 1e100 have the same sign as 1e100, the computed result is -1e-100 + 1e100, which is numerically exactly equal to 1e100. The function math.fmod() returns a result whose sign matches the sign of the first argument instead, and so returns -1e-100 in this case. Which approach is more appropriate depends on the application. [2] If x is very close to an exact integer multiple of y, it?s possible for x//y to be one larger than (x-x%y)//y due to rounding. In such cases, Python returns the latter result, in order to preserve that divmod(x,y)[0] * y + x % y be very close to x. [3] While comparisons between strings make sense at the byte level, they may be counter-intuitive to users. For example, the strings "\u00C7" and "\u0327\u0043" compare differently, even though they both represent the same unicode character (LATIN CAPITAL LETTER C WITH CEDILLA). To compare strings in a human recognizable way, compare using unicodedata.normalize(). [4] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info. [5] The % operator is also used for string formatting; the same precedence applies. [6] The power operator ** binds less tightly than an arithmetic or bitwise unary operator on its right, that is, 2**-1 is 0.5. From rosuav at gmail.com Mon May 6 17:50:59 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 07:50:59 +1000 Subject: First python program, syntax error in while loop In-Reply-To: References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> Message-ID: On Tue, May 7, 2013 at 6:11 AM, Terry Jan Reedy wrote: > On 5/6/2013 11:31 AM, Roy Smith wrote: >> >> In article , >> Chris Angelico wrote: >>> If I ever have even the slightest doubt, I just go ahead and type >>> " operator precedence" into a web search and check it :) >> >> >> Well, that solves the problem once, and it solves it for me. I figure >> if I'm not 100% sure, then maybe other people aren't 100% sure either, >> and my adding the extra parens helps them too. > > > If you keep the Python docs handy, on or off line, the Language manual > Expressions chapter ends with this single page (but better formatted as a > table than here). But I sometimes add parens for quickness or readability. > > or Boolean OR > and Boolean AND Actually, this is one exception. I tend to parenthesize any case where there's a complex set of conditions and mixed AND and OR operations. Part of the reason for this is that any expression that can be affected by the precedence of and and or will most likely be fairly long and/or complex anyway, so a few extra parens won't hurt. ChrisA From wuwei23 at gmail.com Mon May 6 20:17:01 2013 From: wuwei23 at gmail.com (alex23) Date: Mon, 6 May 2013 17:17:01 -0700 (PDT) Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On May 6, 10:37?pm, Mark Lawrence wrote: > One of these days I'll work out why some people insist on using > superfluous parentheses in Python code. ?Could it be that they enjoy > exercising their fingers by reaching for the shift key in conjunction > with the 9 or 0 key? One of these days I'll work out why some programmers consider typing to be "effort". From roy at panix.com Mon May 6 20:18:59 2013 From: roy at panix.com (Roy Smith) Date: Mon, 06 May 2013 20:18:59 -0400 Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: In article , alex23 wrote: > On May 6, 10:37?pm, Mark Lawrence wrote: > > One of these days I'll work out why some people insist on using > > superfluous parentheses in Python code. ?Could it be that they enjoy > > exercising their fingers by reaching for the shift key in conjunction > > with the 9 or 0 key? > > One of these days I'll work out why some programmers consider typing > to be "effort". RSI. From dan at tombstonezero.net Tue May 7 01:03:22 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Tue, 07 May 2013 05:03:22 GMT Subject: First python program, syntax error in while loop References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On Mon, 06 May 2013 17:17:01 -0700, alex23 wrote: > One of these days I'll work out why some programmers consider typing to > be "effort". An ironic comment from someone who goes by the moniker wu wei! ;-) The effort, of course, comes in determining what to type, and often how to type less. From breamoreboy at yahoo.co.uk Tue May 7 02:10:28 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 07 May 2013 07:10:28 +0100 Subject: First python program, syntax error in while loop In-Reply-To: References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On 07/05/2013 01:17, alex23 wrote: > On May 6, 10:37 pm, Mark Lawrence wrote: >> One of these days I'll work out why some people insist on using >> superfluous parentheses in Python code. Could it be that they enjoy >> exercising their fingers by reaching for the shift key in conjunction >> with the 9 or 0 key? > > One of these days I'll work out why some programmers consider typing > to be "effort". > I think it's very important to consider this aspect. E.g. any one using dynamically typed languages has to put in more effort as they should be typing up their test code, whereas people using statically typed languages can simply head down to the pub once their code has compiled. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From rosuav at gmail.com Tue May 7 05:19:49 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 19:19:49 +1000 Subject: First python program, syntax error in while loop In-Reply-To: References: <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> Message-ID: On Tue, May 7, 2013 at 4:10 PM, Mark Lawrence wrote: > On 07/05/2013 01:17, alex23 wrote: >> >> On May 6, 10:37 pm, Mark Lawrence wrote: >>> >>> One of these days I'll work out why some people insist on using >>> superfluous parentheses in Python code. Could it be that they enjoy >>> exercising their fingers by reaching for the shift key in conjunction >>> with the 9 or 0 key? >> >> >> One of these days I'll work out why some programmers consider typing >> to be "effort". >> > > I think it's very important to consider this aspect. E.g. any one using > dynamically typed languages has to put in more effort as they should be > typing up their test code, whereas people using statically typed languages > can simply head down to the pub once their code has compiled. And those who are porting other people's code to a new platform have weeks of work just to get ./configure to run, but after that it's smooth... Everything is variable. ChrisA From rosuav at gmail.com Tue May 7 09:29:57 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 23:29:57 +1000 Subject: First python program, syntax error in while loop In-Reply-To: References: Message-ID: On Tue, May 7, 2013 at 10:44 PM, Ombongi Moraa Fe wrote: > My first language was Pascal. It was at a time in 2005 when computers were > finally becoming popular in Africa and our year was the first time a girls > school from our Province did a computer coursework for National Exams. (That > was such an achievement *sigh*) > > "The teacher said ... Good Programming Practice ... Use parentheses to > format code.. or I will deduct a point from your work when I feel like it." > > Cant seem to let go of the behavior. I use parentheses in all languages. Pretty much all such blanket advice is flawed. I cannot at present think of any example of a "Good Programming Practice" suggestion that doesn't have its exceptions and caveats. The only ones that don't are the ones that get codified into language/syntax rules, and even there, most of them have their detractors. Python's indentation rule is a prime example; most people follow the advice to always indent blocks of code, Python makes it mandatory, and some people hate Python for it. (And yes, there have been times when I've deliberately misindented a block of C code, because it made more logical sense that way. I can quote examples if you like.) The only principle that you should follow is: Think about what you're doing. Everything else is an elaboration on that. [1] ChrisA [1] Matthew 22:37-40 :) From info at wingware.com Fri May 3 15:11:03 2013 From: info at wingware.com (Wingware) Date: Fri, 03 May 2013 15:11:03 -0400 Subject: ANN: Wing IDE 4.1.13 released Message-ID: <51840BC7.70301@wingware.com> Hi, Wingware has released version 4.1.13 of Wing IDE, our integrated development environment designed specifically for the Python programming language. Wing IDE provides a professional quality code editor with vi, emacs, and other key bindings, auto-completion, call tips, refactoring, context-aware auto-editing, a powerful graphical debugger, version control, unit testing, search, and many other features. For details see http://wingware.com/ This minor release includes: * Sped up Search in Files, particularly when looking in Project Files * Sped up project load * Several auto-editing fixes * Several VI mode fixes * Correctly detect snippet context in files with tab indentation * Fix goto-definition in the Debug Probe * Added How-To with tips for using Wing IDE with GTK via PyGObject * 14 other bug fixes and minor improvements For a complete change log see http://wingware.com/pub/wingide/4.1.13/CHANGELOG.txt Free trial: http://wingware.com/wingide/trial Downloads: http://wingware.com/downloads Feature matrix: http://wingware.com/wingide/features More information: http://wingware.com/ Sales: http://wingware.com/store/purchase Upgrades: https://wingware.com/store/upgrade Questions? Don't hesitate to email us at sales at wingware.com. Thanks, -- Stephan Deibel Wingware | Python IDE Advancing Software Development www.wingware.com From ethan at stoneleaf.us Fri May 3 15:24:59 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 03 May 2013 12:24:59 -0700 Subject: ANN: Wing IDE 4.1.13 released In-Reply-To: <51840BC7.70301@wingware.com> References: <51840BC7.70301@wingware.com> Message-ID: <51840F0B.2030500@stoneleaf.us> I remember seeing one of the IDE's recently saying they had better support for dark color schemes. Was it Wing, or does Wing support dark color schemes? The main reason I stick with the older text editors is because I can have a black background -- looking at bright white screens all day kills my eyes. -- ~Ethan~ From hossamalagmy at gmail.com Fri May 3 17:48:13 2013 From: hossamalagmy at gmail.com (23alagmy) Date: Fri, 3 May 2013 14:48:13 -0700 (PDT) Subject: https://www.facebook.com/pages/%D9%86%D8%AA%D8%A7%D8%A6%D8%AC-%D8%A7%D9%84%D8%A7%D9%85%D8%AA%D8%AD%D8%A7%D9%86%D8%A7%D8%AA-%D9%88%D8%A7%D9%84%D8%AC%D8%A7%D9%85%D8%B9%D8%A7%D8%AA-%D9%88%D8%A7%D8%AC%D8%AA%D9%85%D8%A7%D8%B9%D9%8A%D8%A7%D8%AA/29971916 Message-ID: https://www.facebook.com/pages/%D9%86%D8%AA%D8%A7%D8%A6%D8%AC-%D8%A7%D9%84%D8%A7%D9%85%D8%AA%D8%AD%D8%A7%D9%86%D8%A7%D8%AA-%D9%88%D8%A7%D9%84%D8%AC%D8%A7%D9%85%D8%B9%D8%A7%D8%AA-%D9%88%D8%A7%D8%AC%D8%AA%D9%85%D8%A7%D8%B9%D9%8A%D8%A7%D8%AA/299719160065550?ref=hl From agardner210 at gmail.com Fri May 3 18:23:35 2013 From: agardner210 at gmail.com (Alex Gardner) Date: Fri, 3 May 2013 15:23:35 -0700 (PDT) Subject: Collision of Two Rect Message-ID: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> When rect A collides with rect B they stick when I am wanting A to bounce off of B. I have tried different methods, but none seem to work. My source is here: http://pastebin.com/CBYPcubL The collision code itself is below: ------ # Bounce off of the paddle if paddle_rect.colliderect(ball_rect): y_vel*=-1 x_vel*=-1 From ian.g.kelly at gmail.com Fri May 3 19:42:30 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 3 May 2013 17:42:30 -0600 Subject: Collision of Two Rect In-Reply-To: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> References: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> Message-ID: On Fri, May 3, 2013 at 4:23 PM, Alex Gardner wrote: > When rect A collides with rect B they stick when I am wanting A to bounce off of B. I have tried different methods, but none seem to work. My source is here: http://pastebin.com/CBYPcubL > > The collision code itself is below: > ------ > # Bounce off of the paddle > if paddle_rect.colliderect(ball_rect): > y_vel*=-1 > x_vel*=-1 What may be happening is that even though you reversed the direction of the ball's movement, in the next frame the paddle and ball are still detected as colliding, so they get reversed again. If that kept happening over and over again, then the ball would appear to be "sticking". You should check the current direction of movement after detecting the collision and only reverse it if it is not already going the proper direction. The other thing that is suspicious about the code you posted is that it has two different notions of the ball's position that are not necessarily in agreement. There is the ball_rect, and there are also the x and y variables. You center the ball_rect from the x and y variables (somewhat awkwardly; see below), so at that point they would agree, but then you update the x and y variables without updating the rect accordingly. So before you get to the collision detection code, the position of the ball on screen and the one that is used for the collision detection may be somewhat different from the *actual* position of the ball. You should be careful to make sure these variables agree at all times -- or better yet, get rid of x and y entirely, so that you only have one notion of the ball's position to worry about. By the way, this code: boundsball_rect = pygame.Rect(x,y,0,0) ball_rect.clamp_ip(boundsball_rect) could more simply be expressed as: ball_rect.center = (x, y) Because that's all you're doing here: updating the position of the rect. If you take my advice and do away with the x and y variables, then your position updating code: x += x_vel y += y_vel could simply be: ball_rect.move_ip(x_vel, y_vel) From joshua.landau.ws at gmail.com Mon May 6 12:38:54 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 6 May 2013 17:38:54 +0100 Subject: Collision of Two Rect In-Reply-To: References: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> Message-ID: On 4 May 2013 00:42, Ian Kelly wrote: > The other thing that is suspicious about the code you posted is that > it has two different notions of the ball's position that are not > necessarily in agreement. There is the ball_rect, and there are also > the x and y variables. > You should be careful to make sure these > variables agree at all times -- or better yet, get rid of x and y > entirely, so that you only have one notion of the ball's position to > worry about. Pygame uses integers for its Rects and thus I advise much the opposite: *never* store position in Rects. Sorry, but it's true. You'll need to keep x and y around and try to use Rects only when representing pixels on the screen. Pygame has a lot of deficiencies with its default set of objects and functions, so it's something to get used to. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Mon May 6 16:18:14 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 6 May 2013 14:18:14 -0600 Subject: Collision of Two Rect In-Reply-To: References: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> Message-ID: On May 6, 2013 10:39 AM, "Joshua Landau" wrote: > > On 4 May 2013 00:42, Ian Kelly wrote: >> >> The other thing that is suspicious about the code you posted is that >> it has two different notions of the ball's position that are not >> necessarily in agreement. There is the ball_rect, and there are also >> the x and y variables. > > >> >> You should be careful to make sure these >> variables agree at all times -- or better yet, get rid of x and y >> entirely, so that you only have one notion of the ball's position to >> worry about. > > > Pygame uses integers for its Rects and thus I advise much the opposite: *never* store position in Rects. > > Sorry, but it's true. You'll need to keep x and y around and try to use Rects only when representing pixels on the screen. Pygame has a lot of deficiencies with its default set of objects and functions, so it's something to get used to. You don't need subpixel positioning for many games -- arguably including Pong, although I suppose the argument would be stronger if the OP were not using a ludicrously high frame rate of 200 fps, which is going to limit the number of reasonable integer velocities available. For games where logical coordinates and screen coordinates need to be separated though, I agree. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Fri May 3 20:51:01 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 04 May 2013 01:51:01 +0100 Subject: Collision of Two Rect In-Reply-To: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> References: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> Message-ID: <51845B75.8030701@mrabarnett.plus.com> On 03/05/2013 23:23, Alex Gardner wrote: > When rect A collides with rect B they stick when I am wanting A to bounce off of B. I have tried different methods, but none seem to work. My source is here: http://pastebin.com/CBYPcubL > > The collision code itself is below: > ------ > # Bounce off of the paddle > if paddle_rect.colliderect(ball_rect): > y_vel*=-1 > x_vel*=-1 > Apart from the other comments, I'm not sure about this line: y_vel = (math.fabs(2 - x_vel)**2) ** .5 Firstly, instead of math.fabs you could use abs: y_vel = (abs(2 - x_vel)**2) ** .5 Then again, you're squaring the result, and the square of (2 - x_vel) will always be positive anyway: y_vel = ((2 - x_vel)**2) ** .5 You're also calculating getting the square root, for which math.sqrt would be simpler: y_vel = math.sqrt((2 - x_vel)**2) Then again, you're calculating the square root of a squared number, so you might as well just write: y_vel = abs(2 - x_vel) From timr at probo.com Sun May 5 01:28:28 2013 From: timr at probo.com (Tim Roberts) Date: Sat, 04 May 2013 22:28:28 -0700 Subject: Collision of Two Rect References: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> Message-ID: Alex Gardner wrote: > >When rect A collides with rect B they stick when I am wanting A to bounce off of B. I have tried different methods, but none seem to work. My source is here: http://pastebin.com/CBYPcubL > >The collision code itself is below: >------ ># Bounce off of the paddle >if paddle_rect.colliderect(ball_rect): > y_vel*=-1 > x_vel*=-1 I haven't looked at the rest of your code, but the lines you have here are going to send the ball back in exactly the direction it came from -- a 180 degree reversal. When a ball hits a paddle at an angle other than head on, only ONE of the velocities is reversed. When you hit a horizontal paddle, only the Y velocity is negated. The ball continues in the same X direction: \ O \ / \/ ======== See? The X velocity continues unchanged until it hits a vertical wall. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From pedro at ncf.ca Fri May 3 22:13:16 2013 From: pedro at ncf.ca (Pedro) Date: Fri, 3 May 2013 19:13:16 -0700 (PDT) Subject: socket programming Message-ID: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> I'm writing a simple app that uses socket programming, I'm using the following from tutorialspoint as a framework but I'm having a couple of issues implementing it. First - this code constantly loops around an open socket. Is there a way to use something like an interrupt so I don't have to loop constantly to monitor the socket? Second, if any part of the program fails execution (crashes) the port often remains open on my windows machine and the only way to close it that i know of is through task manager or by rebooting the machine. Is there an easy way around this problem ? If I don't close the port the program can't open it again and crashes. Thanks for looking SERVER: import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print 'Got connection from', addr c.send('Thank you for connecting') c.close() # Close the connection CLIENT: import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) s.close # Close the socket when done From rosuav at gmail.com Fri May 3 22:23:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 4 May 2013 12:23:38 +1000 Subject: socket programming In-Reply-To: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> Message-ID: On Sat, May 4, 2013 at 12:13 PM, Pedro wrote: > First - this code constantly loops around an open socket. Is there a way to use something like an interrupt so I don't have to loop constantly to monitor the socket? The accept() call should block. It's not going to spin or anything. If you need to monitor multiple sockets, have a look at select(). > Second, if any part of the program fails execution (crashes) the port often remains open on my windows machine and the only way to close it that i know of is through task manager or by rebooting the machine. Is there an easy way around this problem ? If I don't close the port the program can't open it again and crashes. It remains for a short time to ensure that there's no lurking connections. You can bypass this check by setting the SO_REUSEADDR option - lemme hunt that down in the Python docs, haven't done that in Python for a while... http://docs.python.org/3.3/library/socket.html#socket.socket.setsockopt s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) That should do the job. ChrisA From pedro at ncf.ca Fri May 3 23:37:35 2013 From: pedro at ncf.ca (Pedro) Date: Fri, 3 May 2013 20:37:35 -0700 (PDT) Subject: socket programming In-Reply-To: References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> Message-ID: On Friday, May 3, 2013 10:23:38 PM UTC-4, Chris Angelico wrote: > On Sat, May 4, 2013 at 12:13 PM, Pedro wrote: > > > First - this code constantly loops around an open socket. Is there a way to use something like an interrupt so I don't have to loop constantly to monitor the socket? > > > > The accept() call should block. It's not going to spin or anything. If > > you need to monitor multiple sockets, have a look at select(). > > > > > Second, if any part of the program fails execution (crashes) the port often remains open on my windows machine and the only way to close it that i know of is through task manager or by rebooting the machine. Is there an easy way around this problem ? If I don't close the port the program can't open it again and crashes. > > > > It remains for a short time to ensure that there's no lurking > > connections. You can bypass this check by setting the SO_REUSEADDR > > option - lemme hunt that down in the Python docs, haven't done that in > > Python for a while... > > > > http://docs.python.org/3.3/library/socket.html#socket.socket.setsockopt > > > > s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) > > > > That should do the job. > > > > ChrisA Thanks Chris, can you elaborate on the accept() call should block? From rosuav at gmail.com Fri May 3 23:56:01 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 4 May 2013 13:56:01 +1000 Subject: socket programming In-Reply-To: References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> Message-ID: On Sat, May 4, 2013 at 1:37 PM, Pedro wrote: > On Friday, May 3, 2013 10:23:38 PM UTC-4, Chris Angelico wrote: >> The accept() call should block. It's not going to spin or anything. If >> >> you need to monitor multiple sockets, have a look at select(). > > Thanks Chris, can you elaborate on the accept() call should block? When you call accept(), your program stops running until there's a connection. It's like calling input() (or raw_input()) and your program stopping until you type something. You can disable that by setting the socket nonblocking, but I don't think you're doing that here (and you probably don't want to). Consider the accept() call to be, effectively, like reading from the bound socket. In many ways it functions that way. ChrisA From pedro at ncf.ca Sat May 4 00:03:25 2013 From: pedro at ncf.ca (Pedro) Date: Fri, 3 May 2013 21:03:25 -0700 (PDT) Subject: socket programming In-Reply-To: References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> Message-ID: <21c9450f-1da9-4acd-8f23-d90f71758bfb@googlegroups.com> On Friday, May 3, 2013 11:56:01 PM UTC-4, Chris Angelico wrote: > On Sat, May 4, 2013 at 1:37 PM, Pedro wrote: > > > On Friday, May 3, 2013 10:23:38 PM UTC-4, Chris Angelico wrote: > > >> The accept() call should block. It's not going to spin or anything. If > > >> > > >> you need to monitor multiple sockets, have a look at select(). > > > > > > Thanks Chris, can you elaborate on the accept() call should block? > > > > When you call accept(), your program stops running until there's a > > connection. It's like calling input() (or raw_input()) and your > > program stopping until you type something. You can disable that by > > setting the socket nonblocking, but I don't think you're doing that > > here (and you probably don't want to). > > > > Consider the accept() call to be, effectively, like reading from the > > bound socket. In many ways it functions that way. > > > > ChrisA Brilliant explanation Chris, I swear I read three different docs on that question and could not deduce what you just wrote. Thank you. From irmen.NOSPAM at xs4all.nl Sat May 4 05:37:42 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sat, 04 May 2013 11:37:42 +0200 Subject: socket programming In-Reply-To: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> Message-ID: <5184d6e7$0$15976$e4fe514c@news.xs4all.nl> On 4-5-2013 4:13, Pedro wrote: > SERVER: > > import socket # Import socket module > > s = socket.socket() # Create a socket object > host = socket.gethostname() # Get local machine name > port = 12345 # Reserve a port for your service. > s.bind((host, port)) # Bind to the port This won't always work as expected, particularly on machines with multiple network interfaces. Depending on what your situation requires, a simpler s.bind(('', port)) can be more suitable. (empty string means INADDR_ANY) > > s.listen(5) # Now wait for client connection. > while True: > c, addr = s.accept() # Establish connection with client. > print 'Got connection from', addr > c.send('Thank you for connecting') The send() call returns the number of bytes actually sent. This number can be LESS than the length of the buffer you wanted to send! So you will need a loop here to make sure all data is actually transmitted. The easiest way out is to use this instead: c.sendall('Thank you for connecting') However, this might fail due to some I/O error and then it leaves your socket in an undetermined state because you can't tell how much of the data was actually transmitted. (Recovering from errors is problematic if not impossible with sendall, as far as I know the only thing you can do is just close the bad socket and create a new one) > c.close() # Close the connection > > CLIENT: > import socket # Import socket module > > s = socket.socket() # Create a socket object > host = socket.gethostname() # Get local machine name > port = 12345 # Reserve a port for your service. > > s.connect((host, port)) > print s.recv(1024) While this usually seems to work (especially with small buffer sizes) it has the same problem as pointed out above with send(): recv() might not retrieve all data at once. It returns the amount of data actually received in that call. [warning, hairy details below] You HAVE to make a loop here to get all chunks of data until they add up to the total data size that you expected. (There's a flag MSG_WAITALL you can pass to recv to get around this. But it is not available on all systems, and on some systems where it is provided, it doesn't work correctly.) Also you need to be careful with the buffer size passed to recv, too large and it causes problems on some systems. Around 60kb seems a good upper bound here. This usually also means you need to somehow tell the receiving end of the socket how much data is to be expected, and only then send that actual data. One way to do this is to send the length first as a struct-packed integer (4 bytes), and the data after that. Note that even reading the 4 bytes on the receiving side to determine the expected length might be broken up in multiple chunks: you can't even expect recv(4) to always return those 4 bytes. So even that needs to be in a loop... Other ways to know on the receiving end when to stop reading from the socket is to standardize on some sort of termination character (such as '\0' or perhaps '\n'), fixed length buffers, or to always close the socket after every single message (but that is hugely inefficient if you need to send multiple messages). > s.close # Close the socket when done > Oh, you forgot the parentheses here: s.close() Bottom line: Socket programming on this level is hugely complicated. It doesn't seem too bad if you start of with these simple example programs, but that's false hope. If at all possible, avoid direct socket programming, and use a high-level protocol or library instead (ftp/http/some IPC library/Twisted). Let them deal with the complexity of the socket layer. Regards Irmen de Jong From rosuav at gmail.com Sat May 4 06:00:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 4 May 2013 20:00:39 +1000 Subject: socket programming In-Reply-To: <5184d6e7$0$15976$e4fe514c@news.xs4all.nl> References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> <5184d6e7$0$15976$e4fe514c@news.xs4all.nl> Message-ID: On Sat, May 4, 2013 at 7:37 PM, Irmen de Jong wrote: > Bottom line: > Socket programming on this level is hugely complicated. It doesn't seem too bad if you > start of with these simple example programs, but that's false hope. If at all possible, > avoid direct socket programming, and use a high-level protocol or library instead > (ftp/http/some IPC library/Twisted). Let them deal with the complexity of the socket layer. > It's not quite as bad as this paragraph seems to imply. Sure, there are a million things that can go wrong, but once you master that, it's the very easiest way to do cross-language, cross-platform, cross-computer communication. Practically EVERY language can do basic TCP sockets, but not every language has higher level bindings. ChrisA From pedro at ncf.ca Mon May 6 11:54:57 2013 From: pedro at ncf.ca (Pedro) Date: Mon, 6 May 2013 08:54:57 -0700 (PDT) Subject: socket programming In-Reply-To: <5184d6e7$0$15976$e4fe514c@news.xs4all.nl> References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> <5184d6e7$0$15976$e4fe514c@news.xs4all.nl> Message-ID: <5456a5e5-17f6-4e9a-a29c-490a17c1de44@googlegroups.com> On Saturday, May 4, 2013 5:37:42 AM UTC-4, Irmen de Jong wrote: > On 4-5-2013 4:13, Pedro wrote: > SERVER: > > import socket # Import socket module > > s = socket.socket() # Create a socket object > host = socket.gethostname() # Get local machine name > port = 12345 # Reserve a port for your service. > s.bind((host, port)) # Bind to the port This won't always work as expected, particularly on machines with multiple network interfaces. Depending on what your situation requires, a simpler s.bind(('', port)) can be more suitable. (empty string means INADDR_ANY) > > s.listen(5) # Now wait for client connection. > while True: > c, addr = s.accept() # Establish connection with client. > print 'Got connection from', addr > c.send('Thank you for connecting') The send() call returns the number of bytes actually sent. This number can be LESS than the length of the buffer you wanted to send! So you will need a loop here to make sure all data is actually transmitted. The easiest way out is to use this instead: c.sendall('Thank you for connecting') However, this might fail due to some I/O error and then it leaves your socket in an undetermined state because you can't tell how much of the data was actually transmitted. (Recovering from errors is problematic if not impossible with sendall, as far as I know the only thing you can do is just close the bad socket and create a new one) > c.close() # Close the connection > > CLIENT: > import socket # Import socket module > > s = socket.socket() # Create a socket object > host = socket.gethostname() # Get local machine name > port = 12345 # Reserve a port for your service. > > s.connect((host, port)) > print s.recv(1024) While this usually seems to work (especially with small buffer sizes) it has the same problem as pointed out above with send(): recv() might not retrieve all data at once. It returns the amount of data actually received in that call. [warning, hairy details below] You HAVE to make a loop here to get all chunks of data until they add up to the total data size that you expected. (There's a flag MSG_WAITALL you can pass to recv to get around this. But it is not available on all systems, and on some systems where it is provided, it doesn't work correctly.) Also you need to be careful with the buffer size passed to recv, too large and it causes problems on some systems. Around 60kb seems a good upper bound here. This usually also means you need to somehow tell the receiving end of the socket how much data is to be expected, and only then send that actual data. One way to do this is to send the length first as a struct-packed integer (4 bytes), and the data after that. Note that even reading the 4 bytes on the receiving side to determine the expected length might be broken up in multiple chunks: you can't even expect recv(4) to always return those 4 bytes. So even that needs to be in a loop... Other ways to know on the receiving end when to stop reading from the socket is to standardize on some sort of termination character (such as '\0' or perhaps '\n'), fixed length buffers, or to always close the socket after every single message (but that is hugely inefficient if you need to send multiple messages). > s.close # Close the socket when done > Oh, you forgot the parentheses here: s.close() Bottom line: Socket programming on this level is hugely complicated. It doesn't seem too bad if you start of with these simple example programs, but that's false hope. If at all possible, avoid direct socket programming, and use a high-level protocol or library instead (ftp/http/some IPC library/Twisted). Let them deal with the complexity of the socket layer. Regards Irmen de Jong Thanks for the reply. I'm sending short strings as commands to my server machine so the socket module seems to be doing the trick reliably. I'll try to add Twisted to my arsenal though. Cheers From rosuav at gmail.com Mon May 6 12:05:43 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 02:05:43 +1000 Subject: socket programming In-Reply-To: <5456a5e5-17f6-4e9a-a29c-490a17c1de44@googlegroups.com> References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> <5184d6e7$0$15976$e4fe514c@news.xs4all.nl> <5456a5e5-17f6-4e9a-a29c-490a17c1de44@googlegroups.com> Message-ID: On Tue, May 7, 2013 at 1:54 AM, Pedro wrote: > Thanks for the reply. I'm sending short strings as commands to my server machine so the socket module seems to be doing the trick reliably. I'll try to add Twisted to my arsenal though. > Cheers I've never used Twisted, so I can't say how good it is. All I know is that what I learned about socket programming in C on OS/2 is still valid on Windows and on Linux, and in every language I've ever used (bar JavaScript and ActionScript, which are deliberately sandboxed and thus don't have direct socket calls available). So take your pick: Go with Twisted, and bind yourself to a particular library, or go with BSD sockets, and do the work yourself. Like everything else in programming, it's a tradeoff. ChrisA From arnodel at gmail.com Mon May 6 14:54:17 2013 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 6 May 2013 19:54:17 +0100 Subject: socket programming In-Reply-To: References: <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> <5184d6e7$0$15976$e4fe514c@news.xs4all.nl> <5456a5e5-17f6-4e9a-a29c-490a17c1de44@googlegroups.com> Message-ID: On 6 May 2013 17:05, Chris Angelico wrote: > I've never used Twisted, so I can't say how good it is. All I know is > that what I learned about socket programming in C on OS/2 is still > valid on Windows and on Linux, and in every language I've ever used > (bar JavaScript and ActionScript, which are deliberately sandboxed and > thus don't have direct socket calls available). So take your pick: Go > with Twisted, and bind yourself to a particular library, or go with > BSD sockets, and do the work yourself. Like everything else in > programming, it's a tradeoff. OTOH Twisted can handle much more than socket programming. On the third hand Twisted has its own learning curve as well... -- Arnaud From jimmie.he at gmail.com Sat May 4 06:37:54 2013 From: jimmie.he at gmail.com (Jimmie He) Date: Sat, 4 May 2013 03:37:54 -0700 (PDT) Subject: About py2exe "bundle_files" setting? Message-ID: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> Hi Guys, Now I met an issue to use py2exe when I use bundle_files = 1.As the statement by py2exe,it means it will generate standalone EXE in stead of lots of ".dll & .pyc".But it crash when I use bundle_files = 1 or bundle_files = 2,if I use =3 it is okey.I 've search this issue by google, it is not the Individual case only by me!!! Any guru have the advice for this?I use python 2.7.4 and the py2exe version is 0.6.9(latest).If anyone have the direction I'll share my full package. Attach my setup.py as below-> from distutils.core import setup import py2exe includes = ["encodings", "encodings.*"] options = {"py2exe": {"compressed": 1, "optimize": 2, "ascii": 1, "includes":includes, "bundle_files": 3} } setup (options = options, zipfile=None, windows=["./Gui.py"]) From steve+comp.lang.python at pearwood.info Sat May 4 07:31:04 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 May 2013 11:31:04 GMT Subject: About py2exe "bundle_files" setting? References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> Message-ID: <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 May 2013 03:37:54 -0700, Jimmie He wrote: > Hi Guys, > Now I met an issue to use py2exe when I use bundle_files = 1.As the > statement by py2exe,it means it will generate standalone EXE in stead > of lots of ".dll & .pyc".But it crash when I use bundle_files = 1 or > bundle_files = 2,if I use =3 it is okey. It crashes? Do you mean a Blue Screen of Death? Can you copy and paste the error message that you get please. -- Steven From jimmie.he at gmail.com Sat May 4 07:59:02 2013 From: jimmie.he at gmail.com (Jimmie He) Date: Sat, 4 May 2013 04:59:02 -0700 (PDT) Subject: About py2exe "bundle_files" setting? In-Reply-To: <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <60ef30af-bc46-433a-9f1e-c31879352fa3@googlegroups.com> ? 2013?5?4????UTC+8??7?31?04??Steven D'Aprano??? > On Sat, 04 May 2013 03:37:54 -0700, Jimmie He wrote: > > > > > Hi Guys, > > > Now I met an issue to use py2exe when I use bundle_files = 1.As the > > > statement by py2exe,it means it will generate standalone EXE in stead > > > of lots of ".dll & .pyc".But it crash when I use bundle_files = 1 or > > > bundle_files = 2,if I use =3 it is okey. > > > > It crashes? Do you mean a Blue Screen of Death? > > > > Can you copy and paste the error message that you get please. > > > > > > -- > > Steven Hi Steven, I 've already put my project into GitHub,find the link below,in the folder "Bug",I capture the expection pops up,it is not fatal blue screen.Sorry for my non-english windows:-) https://github.com/jimmiehe/BMPtool/tree/master/Bug From jimmie.he at gmail.com Sat May 4 07:59:13 2013 From: jimmie.he at gmail.com (Jimmie He) Date: Sat, 4 May 2013 04:59:13 -0700 (PDT) Subject: About py2exe "bundle_files" setting? In-Reply-To: <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <75fbdb5d-e7c1-49e8-bd5b-ebf6fe78e970@googlegroups.com> ? 2013?5?4????UTC+8??7?31?04??Steven D'Aprano??? > On Sat, 04 May 2013 03:37:54 -0700, Jimmie He wrote: > > > > > Hi Guys, > > > Now I met an issue to use py2exe when I use bundle_files = 1.As the > > > statement by py2exe,it means it will generate standalone EXE in stead > > > of lots of ".dll & .pyc".But it crash when I use bundle_files = 1 or > > > bundle_files = 2,if I use =3 it is okey. > > > > It crashes? Do you mean a Blue Screen of Death? > > > > Can you copy and paste the error message that you get please. > > > > > > -- > > Steven Hi Steven, I 've already put my project into GitHub,find the link below,in the folder "Bug",I capture the expection pops up,it is not fatal blue screen.Sorry for my non-english windows:-) https://github.com/jimmiehe/BMPtool/tree/master/Bug From steve+comp.lang.python at pearwood.info Sat May 4 09:42:07 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 May 2013 13:42:07 GMT Subject: About py2exe "bundle_files" setting? References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> <75fbdb5d-e7c1-49e8-bd5b-ebf6fe78e970@googlegroups.com> Message-ID: <5185102f$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sat, 04 May 2013 04:59:13 -0700, Jimmie He wrote: > I 've already put my project into GitHub,find the link below,in the > folder "Bug",I capture the expection pops up,it is not fatal blue > screen.Sorry for my non-english windows:-) > https://github.com/jimmiehe/BMPtool/tree/master/Bug I cannot access that page, it just loads as a blank page, then locks up my browser. (This is what happens when stupid websites use too much buggy javascript for simple things which are best handled by ordinary html.) Please copy and paste the exception *as text* here, if you can. -- Steven From rosuav at gmail.com Sat May 4 10:50:14 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 5 May 2013 00:50:14 +1000 Subject: About py2exe "bundle_files" setting? In-Reply-To: <5185102f$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> <75fbdb5d-e7c1-49e8-bd5b-ebf6fe78e970@googlegroups.com> <5185102f$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, May 4, 2013 at 11:42 PM, Steven D'Aprano wrote: > On Sat, 04 May 2013 04:59:13 -0700, Jimmie He wrote: > >> I 've already put my project into GitHub,find the link below,in the >> folder "Bug",I capture the expection pops up,it is not fatal blue >> screen.Sorry for my non-english windows:-) >> https://github.com/jimmiehe/BMPtool/tree/master/Bug > > > I cannot access that page, it just loads as a blank page, then locks up > my browser. (This is what happens when stupid websites use too much buggy > javascript for simple things which are best handled by ordinary html.) > > Please copy and paste the exception *as text* here, if you can. It's not a Python exception. The message has just a few words of English, interspersed with a lot of Chinese, which I am in no way skilled enough to transcribe, much less translate; the title says "Gui.exe - [#######]" and the body says: "0x00fb1cb" [#######] "0x00000000" [########] "written",, and then two lines of all Chinese. Hope that's of at least some help! Sorry I can't be more accurate in the translation. ChrisA From jimmie.he at gmail.com Sat May 4 11:42:53 2013 From: jimmie.he at gmail.com (Jimmie He) Date: Sat, 4 May 2013 08:42:53 -0700 (PDT) Subject: About py2exe "bundle_files" setting? In-Reply-To: References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> <75fbdb5d-e7c1-49e8-bd5b-ebf6fe78e970@googlegroups.com> <5185102f$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3fb60011-b8db-4af1-929d-0c5bd7cd5db7@googlegroups.com> ? 2013?5?4????UTC+8??10?50?14??Chris Angelico??? > On Sat, May 4, 2013 at 11:42 PM, Steven D'Aprano > > wrote: > > > On Sat, 04 May 2013 04:59:13 -0700, Jimmie He wrote: > > > > > >> I 've already put my project into GitHub,find the link below,in the > > >> folder "Bug",I capture the expection pops up,it is not fatal blue > > >> screen.Sorry for my non-english windows:-) > > >> https://github.com/jimmiehe/BMPtool/tree/master/Bug > > > > > > > > > I cannot access that page, it just loads as a blank page, then locks up > > > my browser. (This is what happens when stupid websites use too much buggy > > > javascript for simple things which are best handled by ordinary html.) > > > > > > Please copy and paste the exception *as text* here, if you can. > > > > It's not a Python exception. The message has just a few words of > > English, interspersed with a lot of Chinese, which I am in no way > > skilled enough to transcribe, much less translate; the title says > > "Gui.exe - [#######]" and the body says: > > > > "0x00fb1cb" [#######] "0x00000000" [########] "written",, > > and then two lines of all Chinese. > > > > Hope that's of at least some help! Sorry I can't be more accurate in > > the translation. > > > > ChrisA ChrisA & Steven, Let me translet it:-) "Application error. The instruction at "0x00fb1cb" referenced memory at "0x00000000 can not be written" It seems miss some lib after package by py2exe. It is not an python error,which I mean is that after I package my application by py2exe(without any error),then I run the target .exe which packaged by py2exe, it crashed. From rosuav at gmail.com Sat May 4 11:49:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 5 May 2013 01:49:33 +1000 Subject: About py2exe "bundle_files" setting? In-Reply-To: <3fb60011-b8db-4af1-929d-0c5bd7cd5db7@googlegroups.com> References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> <75fbdb5d-e7c1-49e8-bd5b-ebf6fe78e970@googlegroups.com> <5185102f$0$29997$c3e8da3$5496439d@news.astraweb.com> <3fb60011-b8db-4af1-929d-0c5bd7cd5db7@googlegroups.com> Message-ID: On Sun, May 5, 2013 at 1:42 AM, Jimmie He wrote: > ? 2013?5?4????UTC+8??10?50?14??Chris Angelico??? >> On Sat, May 4, 2013 at 11:42 PM, Steven D'Aprano >> >> wrote: >> >> > On Sat, 04 May 2013 04:59:13 -0700, Jimmie He wrote: >> >> > >> >> >> I 've already put my project into GitHub,find the link below,in the >> >> >> folder "Bug",I capture the expection pops up,it is not fatal blue >> >> >> screen.Sorry for my non-english windows:-) >> >> >> https://github.com/jimmiehe/BMPtool/tree/master/Bug >> >> > >> >> > >> >> > I cannot access that page, it just loads as a blank page, then locks up >> >> > my browser. (This is what happens when stupid websites use too much buggy >> >> > javascript for simple things which are best handled by ordinary html.) >> >> > >> >> > Please copy and paste the exception *as text* here, if you can. >> >> >> >> It's not a Python exception. The message has just a few words of >> >> English, interspersed with a lot of Chinese, which I am in no way >> >> skilled enough to transcribe, much less translate; the title says >> >> "Gui.exe - [#######]" and the body says: >> >> >> >> "0x00fb1cb" [#######] "0x00000000" [########] "written",, >> >> and then two lines of all Chinese. >> >> >> >> Hope that's of at least some help! Sorry I can't be more accurate in >> >> the translation. >> >> >> >> ChrisA > > ChrisA & Steven, > Let me translet it:-) > "Application error. The instruction at "0x00fb1cb" referenced memory at "0x00000000 can not be written" It seems miss some lib after package by py2exe. > > It is not an python error,which I mean is that after I package my application by py2exe(without any error),then I run the target .exe which packaged by py2exe, it crashed. Thanks for the translation, Jimmie! I figured it was some sort of C-level crash. Unfortunately it's fairly non-specific. Are you able to make _any_ program work with py2exe using bundle_files = 1? If you can make a simple "Hello, world" work but your current program isn't working, I'd start looking through the dependencies; if not, I'd check py2exe itself. But I can't help much there, as I've never used py2exe. ChrisA From jimmie.he at gmail.com Sat May 4 12:27:20 2013 From: jimmie.he at gmail.com (Jimmie He) Date: Sat, 4 May 2013 09:27:20 -0700 (PDT) Subject: About py2exe "bundle_files" setting? In-Reply-To: References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> <75fbdb5d-e7c1-49e8-bd5b-ebf6fe78e970@googlegroups.com> <5185102f$0$29997$c3e8da3$5496439d@news.astraweb.com> <3fb60011-b8db-4af1-929d-0c5bd7cd5db7@googlegroups.com> Message-ID: <6d24ba51-db8a-4e3a-86ce-8704e90f4374@googlegroups.com> ? 2013?5?4????UTC+8??11?49?33??Chris Angelico??? > On Sun, May 5, 2013 at 1:42 AM, Jimmie He wrote: > > > ? 2013?5?4????UTC+8??10?50?14??Chris Angelico??? > > >> On Sat, May 4, 2013 at 11:42 PM, Steven D'Aprano > > >> > > >> wrote: > > >> > > >> > On Sat, 04 May 2013 04:59:13 -0700, Jimmie He wrote: > > >> > > >> > > > >> > > >> >> I 've already put my project into GitHub,find the link below,in the > > >> > > >> >> folder "Bug",I capture the expection pops up,it is not fatal blue > > >> > > >> >> screen.Sorry for my non-english windows:-) > > >> > > >> >> https://github.com/jimmiehe/BMPtool/tree/master/Bug > > >> > > >> > > > >> > > >> > > > >> > > >> > I cannot access that page, it just loads as a blank page, then locks up > > >> > > >> > my browser. (This is what happens when stupid websites use too much buggy > > >> > > >> > javascript for simple things which are best handled by ordinary html.) > > >> > > >> > > > >> > > >> > Please copy and paste the exception *as text* here, if you can. > > >> > > >> > > >> > > >> It's not a Python exception. The message has just a few words of > > >> > > >> English, interspersed with a lot of Chinese, which I am in no way > > >> > > >> skilled enough to transcribe, much less translate; the title says > > >> > > >> "Gui.exe - [#######]" and the body says: > > >> > > >> > > >> > > >> "0x00fb1cb" [#######] "0x00000000" [########] "written",, > > >> > > >> and then two lines of all Chinese. > > >> > > >> > > >> > > >> Hope that's of at least some help! Sorry I can't be more accurate in > > >> > > >> the translation. > > >> > > >> > > >> > > >> ChrisA > > > > > > ChrisA & Steven, > > > Let me translet it:-) > > > "Application error. The instruction at "0x00fb1cb" referenced memory at "0x00000000 can not be written" It seems miss some lib after package by py2exe. > > > > > > It is not an python error,which I mean is that after I package my application by py2exe(without any error),then I run the target .exe which packaged by py2exe, it crashed. > > > > Thanks for the translation, Jimmie! I figured it was some sort of C-level crash. > > > > Unfortunately it's fairly non-specific. Are you able to make _any_ > > program work with py2exe using bundle_files = 1? If you can make a > > simple "Hello, world" work but your current program isn't working, I'd > > start looking through the dependencies; if not, I'd check py2exe > > itself. But I can't help much there, as I've never used py2exe. > > > > ChrisA Hi ChrisA, I tested an 'hello world' by the set (bundle_files = 1),it is well worked! Yes,maybe due to the dependencies. From rosuav at gmail.com Sat May 4 12:36:45 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 5 May 2013 02:36:45 +1000 Subject: About py2exe "bundle_files" setting? In-Reply-To: <6d24ba51-db8a-4e3a-86ce-8704e90f4374@googlegroups.com> References: <219a9209-5c6a-4380-bd11-3db00584737d@googlegroups.com> <5184f178$0$29997$c3e8da3$5496439d@news.astraweb.com> <75fbdb5d-e7c1-49e8-bd5b-ebf6fe78e970@googlegroups.com> <5185102f$0$29997$c3e8da3$5496439d@news.astraweb.com> <3fb60011-b8db-4af1-929d-0c5bd7cd5db7@googlegroups.com> <6d24ba51-db8a-4e3a-86ce-8704e90f4374@googlegroups.com> Message-ID: On Sun, May 5, 2013 at 2:27 AM, Jimmie He wrote: > ? 2013?5?4????UTC+8??11?49?33??Chris Angelico??? >> Unfortunately it's fairly non-specific. Are you able to make _any_ >> >> program work with py2exe using bundle_files = 1? If you can make a >> >> simple "Hello, world" work but your current program isn't working, I'd >> >> start looking through the dependencies; if not, I'd check py2exe >> >> itself. But I can't help much there, as I've never used py2exe. >> > > Hi ChrisA, > I tested an 'hello world' by the set (bundle_files = 1),it is well worked! Yes,maybe due to the dependencies. Alright! Now starts the progressive search to find out _which_ dependency it is :) By the way, can you please trim your quoted text? Also, you're double-spacing it all. This suggests to my mind that you're most likely using Google Groups, which is hated by quite a few on this list and defended by a handful; if you can get yourself off it, you'll likely find a better audience to your posts - as there are some who hate it enough to filter out everything that comes from there. If you _must_ use GG, please have a read of this page: http://wiki.python.org/moin/GoogleGroupsPython That'll help you avoid some of the problems. If it feels very manual, that's because it is; I strongly recommend finding a different way to post. Chris Angelico From techgeek201 at gmail.com Sat May 4 14:57:48 2013 From: techgeek201 at gmail.com (eli m) Date: Sat, 4 May 2013 11:57:48 -0700 (PDT) Subject: Video tutorial for making a guess the number game in python Message-ID: <75cf0c56-2352-4f52-a731-5da9c773bd6c@googlegroups.com> I made a video tutorial for making a guess the number game in python. You can check it out here: http://www.youtube.com/watch?v=0WSQb-7wMJQ From pwberrett at gmail.com Sun May 5 02:06:59 2013 From: pwberrett at gmail.com (peter berrett) Date: Sat, 4 May 2013 23:06:59 -0700 (PDT) Subject: Help with loading file into an array Message-ID: Hi all I am trying to build a program that can find comets in a series of astronomical images. I have already written functions to find the comet in a series of images, the data of which is stored in embedded lists. The area I am having difficulty with is take a standard gif file (1024 x 1024) and reading it into an array or embedded lists. In a nutshell here is an example of what I want to do Let's say I have a gif file called 20130428_0000_c2_1024.gif in a folder called c:\comets I want to read the data from that gif file taking the red data (excluding the green and blue data) and store that in an array called Image[][] which is a nested array length 1024 with a list in each item of 1024 length (ie 1024 x 1024) Could someone please provide a piece of code to do the above so I can then go on to modify it to pick up different files from different folders? In particular I am keen to seen how you read in the data and also how you change the directory from which you are reading the image. For the record this is not for homework but is a pet project of mine. I have already written a version of the program in Justbasic but python is faster. I am also interested in readers views as to which is the simplest and best way to achieve what I am trying to do. Thanks Peter From fabiosantosart at gmail.com Sun May 5 03:43:21 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sun, 5 May 2013 08:43:21 +0100 Subject: Help with loading file into an array In-Reply-To: References: Message-ID: Using a nested array should waste a lot of memory. I think you should use PIL to load and read the image. > > I want to read the data from that gif file taking the red data (excluding the green and blue data) and store that in an array called Image[][] which is a nested array length 1024 with a list in each item of 1024 length (ie 1024 x 1024) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cjw at ncf.ca Sun May 5 07:42:22 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Sun, 05 May 2013 07:42:22 -0400 Subject: Help with loading file into an array In-Reply-To: References: Message-ID: On 05/05/2013 3:43 AM, F?bio Santos wrote: > Using a nested array should waste a lot of memory. I think you should > use PIL to load and read the image. > > > > > I want to read the data from that gif file taking the red data > (excluding the green and blue data) and store that in an array called > Image[][] which is a nested array length 1024 with a list in each item > of 1024 length (ie 1024 x 1024) > > > Fabio, Have you considered numpy? Colin W. From jt at toerring.de Sun May 5 07:44:19 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 5 May 2013 11:44:19 GMT Subject: Help with loading file into an array References: Message-ID: peter berrett wrote: > I am trying to build a program that can find comets in a series of > astronomical images. I have already written functions to find the comet in a > series of images, the data of which is stored in embedded lists. > The area I am having difficulty with is take a standard gif file (1024 x > 1024) and reading it into an array or embedded lists. > In a nutshell here is an example of what I want to do > Let's say I have a gif file called 20130428_0000_c2_1024.gif in a folder > called c:\comets > I want to read the data from that gif file taking the red data (excluding > the green and blue data) and store that in an array called Image[][] which > is a nested array length 1024 with a list in each item of 1024 length (ie > 1024 x 1024) > Could someone please provide a piece of code to do the above so I can then > go on to modify it to pick up different files from different folders? In > particular I am keen to seen how you read in the data and also how you > change the directory from which you are reading the image. > the following should do the trick using, as F?bio already suggested, the Python Image Library (PIL): ----------------8<------------------------- #!/ur/bin/env python import Image im = Image.open( 'c:/comets/20130428_0000_c2_1024.gif' ) w, h = im.size red_arr = [ ] for y in range( h ) : red_line = [ ] for x in range( w ) : red_line.append( im.getpixel( ( x, y ) )[ 0 ] ) red_arr.append( red_line ) ----------------8<------------------------- For obvious reasons I couldn't use 'Image' as the name of the list of lists, so it's 'red_arr' instead. This is probably not the fas- test solution, but it's simple and hopefully will get you started. Concerning reading other files: here I may not understand your problem since it looks rather trivial to me by simply passing the open() method of 'Image' the name of a file in a different directory. Regards, Jens -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From joshua.landau.ws at gmail.com Mon May 6 12:28:34 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 6 May 2013 17:28:34 +0100 Subject: Help with loading file into an array In-Reply-To: References: Message-ID: On 5 May 2013 07:06, peter berrett wrote: > I am trying to build a program that can find comets in a series of > astronomical images. I have already written functions to find the comet in > a series of images, the data of which is stored in embedded lists. > > The area I am having difficulty with is take a standard gif file (1024 x > 1024) and reading it into an array or embedded lists. > This is not what you should really be doing. There are specialised things for this. Take a look at PIL (http://www.pythonware.com/products/pil/). It will install as "Image" and from there you can do: import Image image = Image.open("FILE") pixels = image.load() and access the pixels like: pixels[x, y] # You get (r, g, b) tuple of integers from 0 to 255 > In a nutshell here is an example of what I want to do > > Let's say I have a gif file called 20130428_0000_c2_1024.gif in a folder > called c:\comets > > I want to read the data from that gif file taking the red data (excluding > the green and blue data) and store that in an array called Image[][] which > is a nested array length 1024 with a list in each item of 1024 length (ie > 1024 x 1024) > *Ahem*: 1) Python standard is to only uppercase class (and sometimes module) names, so it should just be "image". 2) Technically, you mean list. 3) Chose the above method instead, so instead of indexing it with "image[x][y]" you use "image[x, y]". Now, you only want the red channel. What this means is that you want to "split" the image into channels before using image.load(): import Image image = Image.open("FILE") image.load() # .open is lazy, so you have to load now red, green, blue = image.split() red_pixels = red.load() And you access as before: red_pixels[x, y] # You get integer from 0 to 255 Could someone please provide a piece of code to do the above so I can then > go on to modify it to pick up different files from different folders? In > particular I am keen to seen how you read in the data and also how you > change the directory from which you are reading the image. > Changing directories is done with "os.chdir" ( http://docs.python.org/3/library/os.html#os.chdir). > For the record this is not for homework but is a pet project of mine. I > have already written a version of the program in Justbasic but python is > faster. I am also interested in readers views as to which is the simplest > and best way to achieve what I am trying to do. > Thanks for the clarification, btw. Good luck, as well. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rama29065 at gmail.com Sun May 5 09:43:25 2013 From: rama29065 at gmail.com (rama29065 at gmail.com) Date: Sun, 5 May 2013 06:43:25 -0700 (PDT) Subject: Python not starting Message-ID: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> I was using python from over an year and half.Suddenly from yesterday i'm unable to run it. The error is as follows Traceback (most recent call last): File "C:\Python27\lib\site.py", line 563, in main() File "C:\Python27\lib\site.py", line 546, in main known_paths = addsitepackages(known_paths) File "C:\Python27\lib\site.py", line 324, in addsitepackages if os.path.isdir(sitedir): File "C:\Python27\lib\genericpath.py", line 44, in isdir return stat.S_ISDIR(st.st_mode) AttributeError: 'module' object has no attribute 'S_ISDIR' From roy at panix.com Sun May 5 09:51:59 2013 From: roy at panix.com (Roy Smith) Date: Sun, 05 May 2013 09:51:59 -0400 Subject: Python not starting References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: In article <9ace60b8-a07d-41bc-ac9f-507f6c61f955 at googlegroups.com>, rama29065 at gmail.com wrote: > I was using python from over an year and half.Suddenly from yesterday i'm > unable to run it. > The error is as follows > Traceback (most recent call last): > File "C:\Python27\lib\site.py", line 563, in > main() > File "C:\Python27\lib\site.py", line 546, in main > known_paths = addsitepackages(known_paths) > File "C:\Python27\lib\site.py", line 324, in addsitepackages > if os.path.isdir(sitedir): > File "C:\Python27\lib\genericpath.py", line 44, in isdir > return stat.S_ISDIR(st.st_mode) > AttributeError: 'module' object has no attribute 'S_ISDIR' Just a wild guess, but did you happen to create a module of your own named "stat", which is getting imported instead of the one from the library? Try doing: >>> print stat.__file__ and see what it says. From rama29065 at gmail.com Sun May 5 10:00:05 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Sun, 5 May 2013 07:00:05 -0700 (PDT) Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: On Sunday, May 5, 2013 7:21:59 PM UTC+5:30, Roy Smith wrote: > In article <9ace60b8-a07d-41bc-ac9f-507f6c61f955 at googlegroups.com>, > > rama29065 at gmail.com wrote: > > > > > I was using python from over an year and half.Suddenly from yesterday i'm > > > unable to run it. > > > The error is as follows > > > Traceback (most recent call last): > > > File "C:\Python27\lib\site.py", line 563, in > > > main() > > > File "C:\Python27\lib\site.py", line 546, in main > > > known_paths = addsitepackages(known_paths) > > > File "C:\Python27\lib\site.py", line 324, in addsitepackages > > > if os.path.isdir(sitedir): > > > File "C:\Python27\lib\genericpath.py", line 44, in isdir > > > return stat.S_ISDIR(st.st_mode) > > > AttributeError: 'module' object has no attribute 'S_ISDIR' > > > > Just a wild guess, but did you happen to create a module of your own > > named "stat", which is getting imported instead of the one from the > > library? > > > > Try doing: > > > > >>> print stat.__file__ > > > > and see what it says. Even from command prompt i can't start python.The error is coming up.Python in Windows7 box. From roy at panix.com Sun May 5 10:16:48 2013 From: roy at panix.com (Roy Smith) Date: Sun, 05 May 2013 10:16:48 -0400 Subject: Python not starting References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: > On Sunday, May 5, 2013 7:21:59 PM UTC+5:30, Roy Smith wrote: > > In article <9ace60b8-a07d-41bc-ac9f-507f6c61f955 at googlegroups.com>, > > Just a wild guess, but did you happen to create a module of your own > > named "stat", which is getting imported instead of the one from the > > library? In article , DRJ Reddy wrote: > Even from command prompt i can't start python.The error is coming up.Python > in Windows7 box. I don't know Windows, but my guess is still that it's finding some other file called stat.py before it's finding the system library one. Try doing a file system search for all files named "stat.py" and see what you find. On unix, I would do "find / -name stat.py". I assume there's something similar on Windows. The other thing I would try is tracing the python process as it starts up. On unix, I would do something like "strace -e trace=file python" and see if it's finding a stat.py in some unexpected place. Again, I can only assume there's something similar on Windows. Oh, and please don't post with Google Groups. It double-spaces everything and makes your message really difficult to read. From rama29065 at gmail.com Sun May 5 10:28:42 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Sun, 5 May 2013 07:28:42 -0700 (PDT) Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: <5f96a641-d12e-4974-9de5-7e36a63f1238@googlegroups.com> On Sunday, May 5, 2013 7:46:48 PM UTC+5:30, Roy Smith wrote: > > On Sunday, May 5, 2013 7:21:59 PM UTC+5:30, Roy Smith wrote: > > > > In article <9ace60b8-a07d-41bc-ac9f-507f6c61f955 at googlegroups.com>, > > > > > > Just a wild guess, but did you happen to create a module of your own > > > > named "stat", which is getting imported instead of the one from the > > > > library? > > > > In article , > > DRJ Reddy wrote: > > > Even from command prompt i can't start python.The error is coming up.Python > > > in Windows7 box. > > > > I don't know Windows, but my guess is still that it's finding some other > > file called stat.py before it's finding the system library one. Try > > doing a file system search for all files named "stat.py" and see what > > you find. On unix, I would do "find / -name stat.py". I assume there's > > something similar on Windows. > > > > The other thing I would try is tracing the python process as it starts > > up. On unix, I would do something like "strace -e trace=file python" > > and see if it's finding a stat.py in some unexpected place. Again, I > > can only assume there's something similar on Windows. > > > > Oh, and please don't post with Google Groups. It double-spaces > > everything and makes your message really difficult to read. I found a stat.py in python27/idlelib i haven't created one. From rosuav at gmail.com Sun May 5 11:00:59 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 01:00:59 +1000 Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: On Mon, May 6, 2013 at 12:16 AM, Roy Smith wrote: > In article , > DRJ Reddy wrote: >> Even from command prompt i can't start python.The error is coming up.Python >> in Windows7 box. > > I don't know Windows, but my guess is still that it's finding some other > file called stat.py before it's finding the system library one. Try > doing a file system search for all files named "stat.py" and see what > you find. On unix, I would do "find / -name stat.py". I assume there's > something similar on Windows. Or alternatively, disable site.py by invoking python -S, and then manually import stat and see what its file is. ChrisA From rama29065 at gmail.com Sun May 5 11:11:45 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Sun, 5 May 2013 08:11:45 -0700 (PDT) Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: On Sunday, May 5, 2013 8:30:59 PM UTC+5:30, Chris Angelico wrote: > On Mon, May 6, 2013 at 12:16 AM, Roy Smith wrote: > > > In article , > > > DRJ Reddy wrote: > > >> Even from command prompt i can't start python.The error is coming up.Python > > >> in Windows7 box. > > > > > > I don't know Windows, but my guess is still that it's finding some other > > > file called stat.py before it's finding the system library one. Try > > > doing a file system search for all files named "stat.py" and see what > > > you find. On unix, I would do "find / -name stat.py". I assume there's > > > something similar on Windows. > > > > Or alternatively, disable site.py by invoking python -S, and then > > manually import stat and see what its file is. > > > > ChrisA Thanks all of you,i have done it,by disabling,but what is the permanent solution.How can i start python idle From rosuav at gmail.com Sun May 5 11:20:52 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 01:20:52 +1000 Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: On Mon, May 6, 2013 at 1:11 AM, DRJ Reddy wrote: > On Sunday, May 5, 2013 8:30:59 PM UTC+5:30, Chris Angelico wrote: >> On Mon, May 6, 2013 at 12:16 AM, Roy Smith wrote: >> >> > In article , >> >> > DRJ Reddy wrote: >> >> >> Even from command prompt i can't start python.The error is coming up.Python >> >> >> in Windows7 box. >> >> > >> >> > I don't know Windows, but my guess is still that it's finding some other >> >> > file called stat.py before it's finding the system library one. Try >> >> > doing a file system search for all files named "stat.py" and see what >> >> > you find. On unix, I would do "find / -name stat.py". I assume there's >> >> > something similar on Windows. >> >> >> >> Or alternatively, disable site.py by invoking python -S, and then >> >> manually import stat and see what its file is. >> >> >> >> ChrisA > > Thanks all of you,i have done it,by disabling,but what is the permanent solution.How can i start python idle Here's the steps: 1) Start Python with the -S option. You have apparently figured this part out. This should give you a working interactive Python. 2) Type: import stat stat.__file__ 3) See what the file is that was named there. If you created it, you now know the problem. 4) Read Mark Lawrence's signature. This post adds nothing to what has already been said; it's just coalescing the previously-given advice into exact steps. ChrisA From rama29065 at gmail.com Sun May 5 12:25:13 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Sun, 5 May 2013 09:25:13 -0700 (PDT) Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: <55cf0585-eae9-48b9-8fd0-e599b48b9fb0@googlegroups.com> Chris i have seen stat.__file__. It gives me 'C:\\Python27\\lib\\stat.pyc'. What should i do now. From breamoreboy at yahoo.co.uk Sun May 5 10:26:58 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 05 May 2013 15:26:58 +0100 Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: On 05/05/2013 15:00, DRJ Reddy wrote: > On Sunday, May 5, 2013 7:21:59 PM UTC+5:30, Roy Smith wrote: >> In article <9ace60b8-a07d-41bc-ac9f-507f6c61f955 at googlegroups.com>, >> >> rama29065 at gmail.com wrote: >> >>> I was using python from over an year and half.Suddenly from yesterday i'm >>> unable to run it. >>> The error is as follows >> >>> Traceback (most recent call last): >>> File "C:\Python27\lib\site.py", line 563, in >>> main() >>> File "C:\Python27\lib\site.py", line 546, in main >>> known_paths = addsitepackages(known_paths) >>> File "C:\Python27\lib\site.py", line 324, in addsitepackages >>> if os.path.isdir(sitedir): >>> File "C:\Python27\lib\genericpath.py", line 44, in isdir >>> return stat.S_ISDIR(st.st_mode) >>> AttributeError: 'module' object has no attribute 'S_ISDIR' >> >> Just a wild guess, but did you happen to create a module of your own >> named "stat", which is getting imported instead of the one from the >> library? >> >> Try doing: >>>>> print stat.__file__ >> >> and see what it says. > > Even from command prompt i can't start python.The error is coming up.Python in Windows7 box. > Place the call to print stat.__file__ in the file genericpath.py immediately before the line that gives the attribute error. Would you also be kind enough to read and use the guidance given in the link in my signature. My eyesight is bad enough without parsing double spaced stuff courtesy of google groups, thanks. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From rama29065 at gmail.com Sun May 5 10:32:19 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Sun, 5 May 2013 07:32:19 -0700 (PDT) Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: <9d2513ed-2738-4b6f-92af-82c1faa54da2@googlegroups.com> On Sunday, May 5, 2013 7:56:58 PM UTC+5:30, Mark Lawrence wrote: > On 05/05/2013 15:00, DRJ Reddy wrote: > > > On Sunday, May 5, 2013 7:21:59 PM UTC+5:30, Roy Smith wrote: > > >> In article <9ace60b8-a07d-41bc-ac9f-507f6c61f955 at googlegroups.com>, > > >> > > >> rama29065 at gmail.com wrote: > > >> > > >>> I was using python from over an year and half.Suddenly from yesterday i'm > > >>> unable to run it. > > >>> The error is as follows > > >> > > >>> Traceback (most recent call last): > > >>> File "C:\Python27\lib\site.py", line 563, in > > >>> main() > > >>> File "C:\Python27\lib\site.py", line 546, in main > > >>> known_paths = addsitepackages(known_paths) > > >>> File "C:\Python27\lib\site.py", line 324, in addsitepackages > > >>> if os.path.isdir(sitedir): > > >>> File "C:\Python27\lib\genericpath.py", line 44, in isdir > > >>> return stat.S_ISDIR(st.st_mode) > > >>> AttributeError: 'module' object has no attribute 'S_ISDIR' > > >> > > >> Just a wild guess, but did you happen to create a module of your own > > >> named "stat", which is getting imported instead of the one from the > > >> library? > > >> > > >> Try doing: > > >>>>> print stat.__file__ > > >> > > >> and see what it says. > > > > > > Even from command prompt i can't start python.The error is coming up.Python in Windows7 box. > > > > > > > Place the call to print stat.__file__ in the file genericpath.py > > immediately before the line that gives the attribute error. > > > > Would you also be kind enough to read and use the guidance given in the > > link in my signature. My eyesight is bad enough without parsing double > > spaced stuff courtesy of google groups, thanks. > > > > -- > > If you're using GoogleCrap??? please read this > > http://wiki.python.org/moin/GoogleGroupsPython. > > > > Mark Lawrence Sorry for double spaced stuff,how can i get rid of it. From roy at panix.com Sun May 5 13:55:54 2013 From: roy at panix.com (Roy Smith) Date: Sun, 05 May 2013 13:55:54 -0400 Subject: Python not starting References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <9d2513ed-2738-4b6f-92af-82c1faa54da2@googlegroups.com> Message-ID: In article <9d2513ed-2738-4b6f-92af-82c1faa54da2 at googlegroups.com>, DRJ Reddy wrote: > > If you're using GoogleCrap??? please read this > > > > http://wiki.python.org/moin/GoogleGroupsPython. > > > > > > > > Mark Lawrence > > Sorry for double spaced stuff,how can i get rid of it. I don't mean to be disrespectful, but did you actually read the page you were asked to read? It says, about halfway down the page, "You should remove the excess quoted blank lines before posting. There are several way to do this.", and then goes on to describe three different ways. From rama29065 at gmail.com Sun May 5 14:15:23 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Sun, 5 May 2013 11:15:23 -0700 (PDT) Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <9d2513ed-2738-4b6f-92af-82c1faa54da2@googlegroups.com> Message-ID: I did read and understood that while replying if > is there we will get a blank line unnecessarily. From rosuav at gmail.com Sun May 5 18:29:01 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 08:29:01 +1000 Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <9d2513ed-2738-4b6f-92af-82c1faa54da2@googlegroups.com> Message-ID: On Mon, May 6, 2013 at 4:15 AM, DRJ Reddy wrote: > I did read and understood that while replying if > is there we will get a blank line unnecessarily. If you read that page, you'll know that it does NOT advocate the total elimination of quoted text, which is what you've now done. Please don't. Your posts now lack any form of context. ChrisA From drjreddy7 at gmail.com Sun May 5 23:15:08 2013 From: drjreddy7 at gmail.com (drjreddy7 at gmail.com) Date: Sun, 5 May 2013 20:15:08 -0700 (PDT) Subject: Python not starting In-Reply-To: References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <9d2513ed-2738-4b6f-92af-82c1faa54da2@googlegroups.com> Message-ID: <80b237bb-7990-4c48-9060-ca3f957de4de@googlegroups.com> On Monday, May 6, 2013 3:59:01 AM UTC+5:30, Chris Angelico wrote: > On Mon, May 6, 2013 at 4:15 AM, DRJ Reddy wrote: > I did read and understood that while replying if > is there we will get a blank line unnecessarily. > If you read that page, you'll know that it does NOT advocate the total > elimination of quoted text, which is what you've now done. Please > don't. Your posts now lack any form of context. > ChrisA Sorry ChrisA,not only him for all, for the mess i have created.It was the first time for me on google groups. I am very happy to inform all of you that the problem is solved. The problem was due to the prescence of duplicates for genericpath.pyc and stat.pyc.I have deleted them and new ones were generated as i started python. Thanking all of you for assisting me in solving the issue. From rosuav at gmail.com Mon May 6 00:02:31 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 14:02:31 +1000 Subject: Python not starting In-Reply-To: <80b237bb-7990-4c48-9060-ca3f957de4de@googlegroups.com> References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <9d2513ed-2738-4b6f-92af-82c1faa54da2@googlegroups.com> <80b237bb-7990-4c48-9060-ca3f957de4de@googlegroups.com> Message-ID: On Mon, May 6, 2013 at 1:15 PM, wrote: > On Monday, May 6, 2013 3:59:01 AM UTC+5:30, Chris Angelico wrote: >> On Mon, May 6, 2013 at 4:15 AM, DRJ Reddy wrote: >> I did read and understood that while replying if > is there we will get a blank line unnecessarily. >> If you read that page, you'll know that it does NOT advocate the total >> elimination of quoted text, which is what you've now done. Please >> don't. Your posts now lack any form of context. >> ChrisA > > Sorry ChrisA,not only him for all, for the mess i have created.It was the first time for me on google groups. > I am very happy to inform all of you that the problem is solved. The problem was due to the prescence of duplicates for genericpath.pyc and stat.pyc.I have deleted them and new ones were generated as i started python. > Thanking all of you for assisting me in solving the issue. Excellent! Glad it's sorted. The .pyc problem is, if I understand correctly, more permanently solved in newer versions of Python. So this won't ever be an issue again :) ChrisA From steve+comp.lang.python at pearwood.info Mon May 6 02:06:07 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 May 2013 06:06:07 GMT Subject: Python not starting References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <9d2513ed-2738-4b6f-92af-82c1faa54da2@googlegroups.com> <80b237bb-7990-4c48-9060-ca3f957de4de@googlegroups.com> Message-ID: <5187484f$0$21747$c3e8da3$76491128@news.astraweb.com> On Sun, 05 May 2013 20:15:08 -0700, drjreddy7 wrote: > I am very happy to inform all of > you that the problem is solved. The problem was due to the prescence of > duplicates for genericpath.pyc and stat.pyc.I have deleted them and new > ones were generated as i started python. Thanking all of you for > assisting me in solving the issue. Well, I'm glad it's sorted, but the solution raises as many questions as it answers. How did you get duplicate genericpath.pyc and stat.pyc files? If they were duplicates, why didn't they have the right code in them? It's probably not worth spending any more time investigating, but it is still rather mysterious. -- Steven From rama29065 at gmail.com Mon May 6 06:33:23 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Mon, 6 May 2013 03:33:23 -0700 (PDT) Subject: Python not starting In-Reply-To: <5187484f$0$21747$c3e8da3$76491128@news.astraweb.com> References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <9d2513ed-2738-4b6f-92af-82c1faa54da2@googlegroups.com> <80b237bb-7990-4c48-9060-ca3f957de4de@googlegroups.com> <5187484f$0$21747$c3e8da3$76491128@news.astraweb.com> Message-ID: <66d5fd71-2f98-4206-9197-d30279e5a50f@googlegroups.com> > How did you get duplicate genericpath.pyc and stat.pyc files? > If they were duplicates, why didn't they have the right code in them? > It's probably not worth spending any more time investigating, but it is > still rather mysterious. > Steven When i was running idle on 4th of May,some error came out saying Idle sub-process error i don't remember it correctly,but i wonder how my windows box kept both the files and always loaded the older one instead of new one :D. From steve+comp.lang.python at pearwood.info Sun May 5 14:55:49 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 May 2013 18:55:49 GMT Subject: Python not starting References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> Message-ID: <5186ab34$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sun, 05 May 2013 06:43:25 -0700, rama29065 wrote: > I was using python from over an year and half.Suddenly from yesterday > i'm unable to run it. Well, the obvious question is, what did you do yesterday to change your system? Did you install any new packages? Run a Windows update? Delete some stuff? Something changed. What was it? > The error is as follows > > Traceback (most recent call last): > File "C:\Python27\lib\site.py", line 563, in > main() > File "C:\Python27\lib\site.py", line 546, in main > known_paths = addsitepackages(known_paths) > File "C:\Python27\lib\site.py", line 324, in addsitepackages > if os.path.isdir(sitedir): > File "C:\Python27\lib\genericpath.py", line 44, in isdir > return stat.S_ISDIR(st.st_mode) > AttributeError: 'module' object has no attribute 'S_ISDIR' This is a Python error, so Python is definitely starting. It's starting, hitting an error, and then failing with an exception. Interestingly, the error is in os.path.isdir. The os module should be importing the ntpath module. The ntpath module tries to import _isdir from the nt module, and if that fails, falls back on genericpath.isdir. Which is what fails. So you have two problems: - why is your nt module missing? - why does genericpath.isdir fail? Try running Python from the command line with the -S switch: python -S (you might need to use /S on Windows instead, I'm not sure.) Note that this is uppercase S, not lowercase. -S will disable the import of site.py module, which hopefully will then give you a prompt so you can run this: import nt print nt.__file__ which hopefully will show you have created a file called "nt.py" which is interfering with the actual nt file needed by Python. Get rid of that, and you should be right. -- Steven From bahamutzero8825 at gmail.com Sun May 5 15:47:09 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sun, 05 May 2013 14:47:09 -0500 Subject: Python not starting In-Reply-To: <5186ab34$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <5186ab34$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5186B73D.2050007@gmail.com> On 2013.05.05 13:55, Steven D'Aprano wrote: > (you might need to use /S on Windows instead, I'm not sure.) That is only a convention among Microsoft's CLI utilities. Very few others follow it (even for programs written specifically for Windows), and it is certainly not a necessity on Windows. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From drjreddy7 at gmail.com Sun May 5 22:28:39 2013 From: drjreddy7 at gmail.com (drjreddy7 at gmail.com) Date: Sun, 5 May 2013 19:28:39 -0700 (PDT) Subject: Python not starting In-Reply-To: <5186ab34$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <9ace60b8-a07d-41bc-ac9f-507f6c61f955@googlegroups.com> <5186ab34$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: import nt print nt.__file__ I have done above ones as you stated. I'm getting an error Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute '__file__' From jiang.adam at gmail.com Sun May 5 12:00:54 2013 From: jiang.adam at gmail.com (Adam Jiang) Date: Mon, 6 May 2013 01:00:54 +0900 Subject: How to avoid PEP8 'imported but unused' Message-ID: <20130505160054.GA10521@capricorn> I am new to python. Now, I am woring on an application within Django framework. When I checked my code with pep8 and pyflakes, some warning messages show up-'Foobar imported but unused'. Obviously, it indicates that some modules are imprted to current module but never get references. However, it seems the message is wrong in this case: # file: urls.py urlpattens = patterns( '', url('^signup/$', 'signup') } # file: register.py def signup(request): return ... # file: views.py import signup from register The warning message is shown in file views.py. It seems to me that the code is okay because Django requires all functions serve as 'view' is typically go into views.py. 'import' is about get 'signup' function into module 'views.py'. Or, I am totally wrong? Is there a proper way to avoid this warnning? Best regards, /Adam From fabiosantosart at gmail.com Sun May 5 12:18:40 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sun, 5 May 2013 17:18:40 +0100 Subject: How to avoid PEP8 'imported but unused' In-Reply-To: <20130505160054.GA10521@capricorn> References: <20130505160054.GA10521@capricorn> Message-ID: I usually do this on pyflakes: import whatever assert whatever # silence pyflakes Pyflakes and pep8 have no way of knowing django will import and use your module, or whether you are just importing a module for the side effects, so they issue a warning anyway. Assert'ing counts as using the module, so it counts as an used import. On 5 May 2013 17:05, "Adam Jiang" wrote: > > I am new to python. Now, I am woring on an application within Django > framework. When I checked my code with pep8 and pyflakes, some warning > messages show up-'Foobar imported but unused'. Obviously, it indicates > that some modules are imprted to current module but never get > references. However, it seems the message is wrong in this case: > > # file: urls.py > urlpattens = patterns( > '', > url('^signup/$', 'signup') > } > > # file: register.py > def signup(request): > return ... > > # file: views.py > import signup from register > > The warning message is shown in file views.py. It seems to me that the > code is okay because Django requires all functions serve as 'view' is > typically go into views.py. 'import' is about get 'signup' function > into module 'views.py'. Or, I am totally wrong? Is there a proper way > to avoid this warnning? > > Best regards, > /Adam > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From jiang.adam at gmail.com Sun May 5 12:40:31 2013 From: jiang.adam at gmail.com (Adam Jiang) Date: Mon, 6 May 2013 01:40:31 +0900 Subject: How to avoid PEP8 'imported but unused' In-Reply-To: References: <20130505160054.GA10521@capricorn> Message-ID: <20130505164030.GA12637@capricorn> Thanks. It works very well. One more question. In this particular case it seems 'assert' should be safe as a workaround, doesn't it? 'assert' will check if the symbol is imported and not NULL. Is there side effect if I just applied this rule as a generic one. /Adam On Sun, May 05, 2013 at 05:18:40PM +0100, F?bio Santos wrote: > I usually do this on pyflakes: > > import whatever > assert whatever? # silence pyflakes > > Pyflakes and pep8 have no way of knowing django will import and use your > module, or whether you are just importing a module for the side effects, so > they issue a warning anyway. Assert'ing counts as using the module, so it > counts as an used import. > > On 5 May 2013 17:05, "Adam Jiang" wrote: > > > > I am new to python. Now, I am woring on an application within Django > > framework. When I checked my code with pep8 and pyflakes, some warning > > messages show up-'Foobar imported but unused'. Obviously, it indicates > > that some modules are imprted to current module but never get > > references. However, it seems the message is wrong in this case: > > > > # file: urls.py > > urlpattens = patterns( > > ? ? '', > > ? ? url('^signup/$', 'signup') > > } > > > > # file: register.py > > def signup(request): > > ? ? return ... > > > > # file: views.py > > import signup from register > > > > The warning message is shown in file views.py. It seems to me that the > > code is okay because Django requires all functions serve as 'view' is > > typically go into views.py. 'import' is about get 'signup' function > > into module 'views.py'. Or, I am totally wrong? Is there a proper way > > to avoid this warnning? > > > > Best regards, > > /Adam > > -- > > http://mail.python.org/mailman/listinfo/python-list > From fabiosantosart at gmail.com Sun May 5 13:27:44 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sun, 5 May 2013 18:27:44 +0100 Subject: How to avoid PEP8 'imported but unused' In-Reply-To: <20130505164030.GA12637@capricorn> References: <20130505160054.GA10521@capricorn> <20130505164030.GA12637@capricorn> Message-ID: That assert will never fail. If the symbol is not imported, the import statement raises ImportError. And actually "assert" makes sure that the value is not false-ish, not None/Null. And AFAIK a module object is *always* true. > One more question. In this particular case it seems 'assert' should be > safe as a workaround, doesn't it? 'assert' will check if the symbol > is imported and not NULL. Is there side effect if I just applied this > rule as a generic one. > -- F?bio Santos From jiang.adam at gmail.com Sun May 5 22:47:57 2013 From: jiang.adam at gmail.com (Adam Jiang) Date: Mon, 6 May 2013 11:47:57 +0900 Subject: How to avoid PEP8 'imported but unused' In-Reply-To: References: <20130505160054.GA10521@capricorn> <20130505164030.GA12637@capricorn> Message-ID: <20130506024757.GA18201@capricorn> Thank you. Problem solved. /Adam On Sun, May 05, 2013 at 06:27:44PM +0100, F?bio Santos wrote: > That assert will never fail. If the symbol is not imported, the import > statement raises ImportError. And actually "assert" makes sure that > the value is not false-ish, not None/Null. And AFAIK a module object > is *always* true. > > > One more question. In this particular case it seems 'assert' should be > > safe as a workaround, doesn't it? 'assert' will check if the symbol > > is imported and not NULL. Is there side effect if I just applied this > > rule as a generic one. > > > > -- > F?bio Santos From python at mrabarnett.plus.com Sun May 5 12:21:36 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 05 May 2013 17:21:36 +0100 Subject: How to avoid PEP8 'imported but unused' In-Reply-To: <20130505160054.GA10521@capricorn> References: <20130505160054.GA10521@capricorn> Message-ID: <51868710.1070000@mrabarnett.plus.com> On 05/05/2013 17:00, Adam Jiang wrote: > I am new to python. Now, I am woring on an application within Django > framework. When I checked my code with pep8 and pyflakes, some warning > messages show up-'Foobar imported but unused'. Obviously, it indicates > that some modules are imprted to current module but never get > references. However, it seems the message is wrong in this case: > > # file: urls.py > urlpattens = patterns( > '', > url('^signup/$', 'signup') > } > > # file: register.py > def signup(request): > return ... > > # file: views.py > import signup from register > > The warning message is shown in file views.py. It seems to me that the > code is okay because Django requires all functions serve as 'view' is > typically go into views.py. 'import' is about get 'signup' function > into module 'views.py'. Or, I am totally wrong? Is there a proper way > to avoid this warnning? > It's not: import signup from register (that's an error) but: from register import signup After fixing that, does it still show the warning? From __peter__ at web.de Sun May 5 12:38:46 2013 From: __peter__ at web.de (Peter Otten) Date: Sun, 05 May 2013 18:38:46 +0200 Subject: How to avoid PEP8 'imported but unused' References: <20130505160054.GA10521@capricorn> Message-ID: Adam Jiang wrote: > I am new to python. Now, I am woring on an application within Django > framework. When I checked my code with pep8 and pyflakes, some warning > messages show up-'Foobar imported but unused'. Obviously, it indicates > that some modules are imprted to current module but never get > references. However, it seems the message is wrong in this case: > > # file: urls.py > urlpattens = patterns( > '', > url('^signup/$', 'signup') > } > > # file: register.py > def signup(request): > return ... > > # file: views.py > import signup from register > > The warning message is shown in file views.py. It seems to me that the > code is okay because Django requires all functions serve as 'view' is > typically go into views.py. 'import' is about get 'signup' function > into module 'views.py'. Or, I am totally wrong? Is there a proper way > to avoid this warnning? pylint has a way to suppress such warnings with a comment like from signup import register # pylint:disable=W0611 but personally I find the magic comment more annoying than the false warning... From l.selmi at icloud.com Sun May 5 12:08:47 2013 From: l.selmi at icloud.com (leonardo selmi) Date: Sun, 05 May 2013 18:08:47 +0200 Subject: learning python Message-ID: hi guys i need to find a good book to learn python with exercises and solutions, any suggestions? thanks! best regards leonardo -------------- next part -------------- An HTML attachment was scrubbed... URL: From rama29065 at gmail.com Sun May 5 12:28:25 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Sun, 5 May 2013 09:28:25 -0700 (PDT) Subject: learning python In-Reply-To: References: Message-ID: A byte of python with learning python by Mark Lutz is a good combination. From brunson at brunson.com Sun May 5 12:58:22 2013 From: brunson at brunson.com (Eric Brunson) Date: Sun, 05 May 2013 10:58:22 -0600 Subject: [Python-Help] learning python In-Reply-To: References: Message-ID: <51868FAE.1090301@brunson.com> On 05/05/2013 10:08 AM, leonardo selmi wrote: > hi guys > > i need to find a good book to learn python with exercises and > solutions, any suggestions? > > thanks! > Leonardo, There are several good online tutorials available, many listed here: http://wiki.python.org/moin/BeginnersGuide There is also the Python Tutorial written by the creator of Python here: http://docs.python.org/2/tutorial/index.html If you want a hands on approach with exercises, try Learn Python the Hard Way: http://learnpythonthehardway.org/ If you already have a background in programming I find Dive Into Python to be an excellent introduction: http://www.diveintopython.net/ as well as Think Python: How to Think Like a Computer Scientist: http://www.greenteapress.com/thinkpython/html/ If you want something you can hold in your hand and has paper pages, I can recommend Learning Python by Mark Lutz: http://shop.oreilly.com/product/9780596158071.do followed by The Python Cookbook, by Alex Martelli and David Ascher: http://shop.oreilly.com/product/9780596001674.do The Massachusetts Institute of Technology uses Python to teach their introduction to programming course which is available online (free) here: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/index.htm I hope that helps. Sincerely, e. -------------- next part -------------- An HTML attachment was scrubbed... URL: From l.selmi at icloud.com Sun May 5 13:10:32 2013 From: l.selmi at icloud.com (leonardo selmi) Date: Sun, 05 May 2013 19:10:32 +0200 Subject: [Python-Help] learning python In-Reply-To: <51868FAE.1090301@brunson.com> References: <51868FAE.1090301@brunson.com> Message-ID: thanks! Il giorno 05/mag/2013, alle ore 18:58, Eric Brunson ha scritto: > On 05/05/2013 10:08 AM, leonardo selmi wrote: >> hi guys >> >> i need to find a good book to learn python with exercises and solutions, any suggestions? >> >> thanks! >> > > Leonardo, > > There are several good online tutorials available, many listed here: http://wiki.python.org/moin/BeginnersGuide > > There is also the Python Tutorial written by the creator of Python here: http://docs.python.org/2/tutorial/index.html > > If you want a hands on approach with exercises, try Learn Python the Hard Way: http://learnpythonthehardway.org/ > > If you already have a background in programming I find Dive Into Python to be an excellent introduction: http://www.diveintopython.net/ as well as Think Python: How to Think Like a Computer Scientist: http://www.greenteapress.com/thinkpython/html/ > > If you want something you can hold in your hand and has paper pages, I can recommend Learning Python by Mark Lutz: http://shop.oreilly.com/product/9780596158071.do followed by The Python Cookbook, by Alex Martelli and David Ascher: http://shop.oreilly.com/product/9780596001674.do > > The Massachusetts Institute of Technology uses Python to teach their introduction to programming course which is available online (free) here: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/index.htm > > I hope that helps. > > Sincerely, > e. > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hermanmu at gmail.com Mon May 6 10:43:56 2013 From: hermanmu at gmail.com (Michael Herman) Date: Mon, 6 May 2013 07:43:56 -0700 Subject: learning python In-Reply-To: References: Message-ID: realpython.com - just launched On Sun, May 5, 2013 at 9:08 AM, leonardo selmi wrote: > hi guys > > i need to find a good book to learn python with exercises and solutions, any > suggestions? > > thanks! > > best regards > > leonardo > > -- > http://mail.python.org/mailman/listinfo/python-list > From ignoramus16992 at NOSPAM.16992.invalid Sun May 5 13:11:11 2013 From: ignoramus16992 at NOSPAM.16992.invalid (Ignoramus16992) Date: Sun, 05 May 2013 12:11:11 -0500 Subject: Why do Perl programmers make more money than Python programmers Message-ID: According to CIO.com, Python programmers make only $83,000 per year, while Perl programmers make $93,000 per year. http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 I would like to know, what explains the discrepancy. Thank you! i From sbugaj at pixar.com Sun May 5 13:32:01 2013 From: sbugaj at pixar.com (Stephan Vladimir Bugaj) Date: Sun, 5 May 2013 10:32:01 -0700 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: Message-ID: <5B2FE106-9F37-4272-B62E-B2F07DF91D45@pixar.com> Most likely more legacy Perl code in mission critical systems S Sent from my pocket UNIVAC. On May 5, 2013, at 10:11 AM, Ignoramus16992 wrote: > According to CIO.com, Python programmers make only $83,000 per year, > while Perl programmers make $93,000 per year. > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > > I would like to know, what explains the discrepancy. > > Thank you! > > i > -- > http://mail.python.org/mailman/listinfo/python-list From fabiosantosart at gmail.com Sun May 5 13:52:21 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sun, 5 May 2013 18:52:21 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: <5B2FE106-9F37-4272-B62E-B2F07DF91D45@pixar.com> References: <5B2FE106-9F37-4272-B62E-B2F07DF91D45@pixar.com> Message-ID: > Most likely more legacy Perl code in mission critical systems Which is unfair because when Python is ever surpassed by an even better language/technology then we get paid more to work Python and not move the industry forward by moving to the new technology and hacking on it. -- F?bio Santos From sbugaj at pixar.com Sun May 5 13:33:19 2013 From: sbugaj at pixar.com (Stephan Vladimir Bugaj) Date: Sun, 5 May 2013 10:33:19 -0700 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: Message-ID: <2C66876A-D362-4F8B-B6E1-DFB49FC2C494@pixar.com> And seniority combined with annual cost of living raises, due to Perl being in use longer S Sent from my pocket UNIVAC. On May 5, 2013, at 10:11 AM, Ignoramus16992 wrote: > According to CIO.com, Python programmers make only $83,000 per year, > while Perl programmers make $93,000 per year. > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > > I would like to know, what explains the discrepancy. > > Thank you! > > i > -- > http://mail.python.org/mailman/listinfo/python-list From fabiosantosart at gmail.com Sun May 5 13:34:57 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sun, 5 May 2013 18:34:57 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: Message-ID: I wouldn't touch perl code with a ten foot pole. On the other hand, python is pleasing to the eye and easy to write, read and modify. This means that you can easily be replaced with someone else who also knows python, so your company doesn't care much about paying you well and keeping you there. On Sun, May 5, 2013 at 6:11 PM, Ignoramus16992 wrote: > According to CIO.com, Python programmers make only $83,000 per year, > while Perl programmers make $93,000 per year. > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > > I would like to know, what explains the discrepancy. > > Thank you! > > i > -- > http://mail.python.org/mailman/listinfo/python-list -- F?bio Santos From rosuav at gmail.com Sun May 5 13:35:14 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 03:35:14 +1000 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: Message-ID: On Mon, May 6, 2013 at 3:11 AM, Ignoramus16992 wrote: > According to CIO.com, Python programmers make only $83,000 per year, > while Perl programmers make $93,000 per year. > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > > I would like to know, what explains the discrepancy. > > Thank you! Once, I was young and foolish too, and an ignoramus, just like you. [1] There's a big problem with comparing statistical averages without any indication of their spread. Suppose these are the salaries involved: Perl = [15000, 30000, 80000, 100000, 143000, 190000] Python = [15000, 30000, 80000, 100000, 190000] That is, the guy who's making 143K a year didn't mention that he's using Python. Voila! Your averages differ, yet statistically, there's not a lot of difference. The best way to know how useful the averages are is to look at the distribution, eg look at the difference between the highest and lowest values, or the standard deviation of the sample, or something of that sort. Without that, there's no way of knowing whether a 10K difference is at all significant. I would posit that, among salaries, it's meaningless. ChrisA [1] I don't know if you're trolling or not, so I'll give a serious response. But I'm going to start with a quote from Emerald Isle. http://diamond.boisestate.edu/gas/sullivan/emerald_isle/web_opera/ei14.html From rustompmody at gmail.com Sun May 5 13:35:29 2013 From: rustompmody at gmail.com (rusi) Date: Sun, 5 May 2013 10:35:29 -0700 (PDT) Subject: Why do Perl programmers make more money than Python programmers References: Message-ID: <217e90de-851c-4fbb-b04b-9e1c77710102@wg15g2000pbb.googlegroups.com> On May 5, 10:11?pm, Ignoramus16992 wrote: > According to CIO.com, Python programmers make only $83,000 per year, > while Perl programmers make $93,000 per year. > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > > I would like to know, what explains the discrepancy. > > Thank you! > > i I expect Cobol programmers earn more than either Its called supply and demand From breamoreboy at yahoo.co.uk Sun May 5 14:24:34 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 05 May 2013 19:24:34 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: <217e90de-851c-4fbb-b04b-9e1c77710102@wg15g2000pbb.googlegroups.com> References: <217e90de-851c-4fbb-b04b-9e1c77710102@wg15g2000pbb.googlegroups.com> Message-ID: On 05/05/2013 18:35, rusi wrote: > On May 5, 10:11 pm, Ignoramus16992 16992.invalid> wrote: >> According to CIO.com, Python programmers make only $83,000 per year, >> while Perl programmers make $93,000 per year. >> >> http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 >> >> I would like to know, what explains the discrepancy. >> >> Thank you! >> >> i > > I expect Cobol programmers earn more than either > Its called supply and demand > They might get paid more, whether or not they earn it is an entirely different question. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From roy at panix.com Sun May 5 13:58:51 2013 From: roy at panix.com (Roy Smith) Date: Sun, 05 May 2013 13:58:51 -0400 Subject: Why do Perl programmers make more money than Python programmers References: Message-ID: In article , Ignoramus16992 wrote: > According to CIO.com, Python programmers make only $83,000 per year, > while Perl programmers make $93,000 per year. It's amazing the depths to which people are willing to sink for an extra $10k per year. From steve+comp.lang.python at pearwood.info Sun May 5 15:13:57 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 May 2013 19:13:57 GMT Subject: Why do Perl programmers make more money than Python programmers References: Message-ID: <5186af75$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sun, 05 May 2013 13:58:51 -0400, Roy Smith wrote: > In article , > Ignoramus16992 wrote: > >> According to CIO.com, Python programmers make only $83,000 per year, >> while Perl programmers make $93,000 per year. > > It's amazing the depths to which people are willing to sink for an extra > $10k per year. Right now, I'd consider learning PHP for an extra $100 a month. Or peddling my arse down at the docks for twenty cents a time, which will be less embarrassing and much less painful. -- Steven From petite.abeille at gmail.com Sun May 5 17:05:07 2013 From: petite.abeille at gmail.com (Petite Abeille) Date: Sun, 5 May 2013 23:05:07 +0200 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: <5186af75$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <5186af75$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9B4DAC3C-70F9-4FD6-88B5-E13A4CFD60BA@gmail.com> On May 5, 2013, at 9:13 PM, Steven D'Aprano wrote: > On Sun, 05 May 2013 13:58:51 -0400, Roy Smith wrote: > >> In article , >> Ignoramus16992 wrote: >> >>> According to CIO.com, Python programmers make only $83,000 per year, >>> while Perl programmers make $93,000 per year. >> >> It's amazing the depths to which people are willing to sink for an extra >> $10k per year. > > Right now, I'd consider learning PHP for an extra $100 a month. Or > peddling my arse down at the docks for twenty cents a time, which will be > less embarrassing and much less painful. I, for one, I'm glad to let the Pythonistas take the high road, while other, more, err, pragmatic types, take the dough. ching ching. From roy at panix.com Sun May 5 17:07:41 2013 From: roy at panix.com (Roy Smith) Date: Sun, 05 May 2013 17:07:41 -0400 Subject: Why do Perl programmers make more money than Python programmers References: <5186af75$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <5186af75$0$29997$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Right now, I'd consider learning PHP for an extra $100 a month. Or > peddling my arse down at the docks for twenty cents a time, which will be > less embarrassing and much less painful. Having spent the better part of a year doing one of those activities, I'm inclined to agree. There *are* programming languages worse than PHP. Have you ever tried britescript? From roy at panix.com Sun May 5 17:49:04 2013 From: roy at panix.com (Roy Smith) Date: Sun, 05 May 2013 17:49:04 -0400 Subject: Why do Perl programmers make more money than Python programmers References: <5186af75$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Dennis Lee Bieber wrote: > On Sun, 05 May 2013 17:07:41 -0400, Roy Smith declaimed > the following in gmane.comp.python.general: > > > In article <5186af75$0$29997$c3e8da3$5496439d at news.astraweb.com>, > > Steven D'Aprano wrote: > > > > > Right now, I'd consider learning PHP for an extra $100 a month. Or > > > peddling my arse down at the docks for twenty cents a time, which will be > > > less embarrassing and much less painful. > > > > Having spent the better part of a year doing one of those activities, > > I'm inclined to agree. > > > > There *are* programming languages worse than PHP. Have you ever tried > > britescript? > > Is that a toothpaste, kitchen cleaner, or device for doing gold-leaf > illuminated manuscripts? It is a programming language. It is what Roku apps are written in. From m at rtij.nl.invlalid Tue May 7 16:31:44 2013 From: m at rtij.nl.invlalid (Martijn Lievaart) Date: Tue, 7 May 2013 22:31:44 +0200 Subject: Why do Perl programmers make more money than Python programmers References: <5186af75$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, 05 May 2013 17:07:41 -0400, Roy Smith wrote: > There *are* programming languages worse than PHP. Have you ever tried > britescript? Have you tried MUMPS? :-) M4 From wrw at mac.com Tue May 7 17:02:54 2013 From: wrw at mac.com (William Ray Wing) Date: Tue, 07 May 2013 17:02:54 -0400 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186af75$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <43D5AAA3-6DCE-4AF9-A652-B9F2E28E7C8A@mac.com> On May 7, 2013, at 4:31 PM, Martijn Lievaart wrote: > On Sun, 05 May 2013 17:07:41 -0400, Roy Smith wrote: > >> There *are* programming languages worse than PHP. Have you ever tried >> britescript? > > Have you tried MUMPS? :-) > > M4 > Which one? The original MUMPS (Massachusetts General Hospital Utility Multi-Programming System), or the one that came out of Europe in the '90s and stole the MUMPS name (MUltifrontal Massively Parallel sparse direct Solver)? I used both the original and its baby brother FOCAL which, considering that they ran on DEC PDP-7 (MUMPS) and DEC PDP-8 (FOCAL) systems with as little as 4k words of 18-bit memory on the PDP-7 and 8-bit memory on the PDP-8 is pretty spectacular. They should be forgiven for a somewhat constrained list of key words and operations. -Bill From steve+comp.lang.python at pearwood.info Sun May 5 15:10:47 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 May 2013 19:10:47 GMT Subject: Why do Perl programmers make more money than Python programmers References: Message-ID: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sun, 05 May 2013 12:11:11 -0500, Ignoramus16992 wrote: > According to CIO.com, Python programmers make only $83,000 per year, > while Perl programmers make $93,000 per year. > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > > I would like to know, what explains the discrepancy. Perl is much harder to use, so the average Perl programmer burns out after a few years and takes up a less stressful career, like going undercover in the Russian mob or the Taliban. So only the most dedicated, brilliant and extreme programmers last long enough to become a Perl expert, and consequently can demand higher pay, while any idiot can learn to program Python, as I have. Also, Perl programmers are an unprincipled, devious bunch, always looking for an opportunity to blackmail their employers into paying them extra. Python programmers are a decent, law-abiding people with a strong moral code who would never stoop to the sort of things that Perl coders are proud of doing. -- Steven From jt at toerring.de Sun May 5 16:37:19 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 5 May 2013 20:37:19 GMT Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In comp.lang.python Steven D'Aprano wrote: > On Sun, 05 May 2013 12:11:11 -0500, Ignoramus16992 wrote: > > According to CIO.com, Python programmers make only $83,000 per year, > > while Perl programmers make $93,000 per year. > > > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > > > > I would like to know, what explains the discrepancy. > Perl is much harder to use, so the average Perl programmer burns out > after a few years and takes up a less stressful career, like going > undercover in the Russian mob or the Taliban. So only the most dedicated, > brilliant and extreme programmers last long enough to become a Perl > expert, and consequently can demand higher pay, while any idiot can learn > to program Python, as I have. > Also, Perl programmers are an unprincipled, devious bunch, always looking > for an opportunity to blackmail their employers into paying them extra. > Python programmers are a decent, law-abiding people with a strong moral > code who would never stoop to the sort of things that Perl coders are > proud of doing. Now you got me badly worried, using both Perl and Python (and other, unspeakable languages, but not VB I promise!) Will I end up as a Python hacker for the mob or worse - or is there a chance of redemption (perhaps after a few years in Guanta- namo bay)? And should I, while it lasts, get the Perl or the Python salary, or the mean or both combined? Got to consider that when applying for my next job! Regards, Jens -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From rweikusat at mssgmbh.com Sun May 5 17:09:09 2013 From: rweikusat at mssgmbh.com (Rainer Weikusat) Date: Sun, 05 May 2013 22:09:09 +0100 Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87a9o9b0cq.fsf@sapphire.mobileactivedefense.com> jt at toerring.de (Jens Thoms Toerring) writes: > In comp.lang.python Steven D'Aprano wrote: >> On Sun, 05 May 2013 12:11:11 -0500, Ignoramus16992 wrote: > >> > According to CIO.com, Python programmers make only $83,000 per year, >> > while Perl programmers make $93,000 per year. >> > >> > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 >> > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 >> > >> > I would like to know, what explains the discrepancy. > >> Perl is much harder to use, so the average Perl programmer burns out >> after a few years and takes up a less stressful career, like going >> undercover in the Russian mob or the Taliban. So only the most dedicated, >> brilliant and extreme programmers last long enough to become a Perl >> expert, and consequently can demand higher pay, while any idiot can learn >> to program Python, as I have. > >> Also, Perl programmers are an unprincipled, devious bunch, always looking >> for an opportunity to blackmail their employers into paying them extra. >> Python programmers are a decent, law-abiding people with a strong moral >> code who would never stoop to the sort of things that Perl coders are >> proud of doing. > > Now you got me badly worried, using both Perl and Python (and > other, unspeakable languages, but not VB I promise!) Will I > end up as a Python hacker for the mob or worse https://en.wikipedia.org/wiki/Strange_Case_of_Dr_Jekyll_and_Mr_Hyde [SCNR] From jt at toerring.de Sun May 5 17:33:36 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 5 May 2013 21:33:36 GMT Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> <87a9o9b0cq.fsf@sapphire.mobileactivedefense.com> Message-ID: In comp.lang.python Rainer Weikusat wrote: > jt at toerring.de (Jens Thoms Toerring) writes: > > In comp.lang.python Steven D'Aprano wrote: > >> On Sun, 05 May 2013 12:11:11 -0500, Ignoramus16992 wrote: > > > >> > According to CIO.com, Python programmers make only $83,000 per year, > >> > while Perl programmers make $93,000 per year. > >> > > >> > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 > >> > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > >> > > >> > I would like to know, what explains the discrepancy. > > > >> Perl is much harder to use, so the average Perl programmer burns out > >> after a few years and takes up a less stressful career, like going > >> undercover in the Russian mob or the Taliban. So only the most dedicated, > >> brilliant and extreme programmers last long enough to become a Perl > >> expert, and consequently can demand higher pay, while any idiot can learn > >> to program Python, as I have. > > > >> Also, Perl programmers are an unprincipled, devious bunch, always looking > >> for an opportunity to blackmail their employers into paying them extra. > >> Python programmers are a decent, law-abiding people with a strong moral > >> code who would never stoop to the sort of things that Perl coders are > >> proud of doing. > > > > Now you got me badly worried, using both Perl and Python (and > > other, unspeakable languages, but not VB I promise!) Will I > > end up as a Python hacker for the mob or worse > https://en.wikipedia.org/wiki/Strange_Case_of_Dr_Jekyll_and_Mr_Hyde > [SCNR] Well, that didn't have a happy ending:-( Should have listened to my parents when they told me again and again "Never use Perl, just say no!". Seems I'm doomed - what's the proper way to apply for a job with the mob? -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From roy at panix.com Sun May 5 17:35:51 2013 From: roy at panix.com (Roy Smith) Date: Sun, 05 May 2013 17:35:51 -0400 Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> <87a9o9b0cq.fsf@sapphire.mobileactivedefense.com> Message-ID: In article , jt at toerring.de (Jens Thoms Toerring) wrote: > Well, that didn't have a happy ending:-( Should have listened to > my parents when they told me again and again "Never use Perl, just > say no!". Seems I'm doomed - what's the proper way to apply for a > job with the mob? I don't think you apply. If they want you, they'll find you. From jt at toerring.de Sun May 5 17:43:58 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 5 May 2013 21:43:58 GMT Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> <87a9o9b0cq.fsf@sapphire.mobileactivedefense.com> Message-ID: In comp.lang.python Roy Smith wrote: > In article , > jt at toerring.de (Jens Thoms Toerring) wrote: > > Well, that didn't have a happy ending:-( Should have listened to > > my parents when they told me again and again "Never use Perl, just > > say no!". Seems I'm doomed - what's the proper way to apply for a > > job with the mob? > I don't think you apply. If they want you, they'll find you. I see, that's what's called headhuntering, isn't it? -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From rosuav at gmail.com Sun May 5 18:24:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 08:24:51 +1000 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: <87a9o9b0cq.fsf@sapphire.mobileactivedefense.com> References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> <87a9o9b0cq.fsf@sapphire.mobileactivedefense.com> Message-ID: On Mon, May 6, 2013 at 7:09 AM, Rainer Weikusat wrote: > jt at toerring.de (Jens Thoms Toerring) writes: >> Now you got me badly worried, using both Perl and Python (and >> other, unspeakable languages, but not VB I promise!) Will I >> end up as a Python hacker for the mob or worse > > https://en.wikipedia.org/wiki/Strange_Case_of_Dr_Jekyll_and_Mr_Hyde Ah, that would be me. Every night, I work on open source projects written in good languages; but four days a week from 9:00 till 5:00 my other nature takes over, and there is a struggle between the forces of Good and Evil. The Evil side is strong, but even there the Good side is not completely unheard; there are occasional times when I fight off the PHP influence. (I'm still confident that we will eventually move off PHP altogether. My boss reckons it'll never happen, but I can be patient...) ChrisA From dihedral88888 at googlemail.com Sun May 5 17:34:28 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 5 May 2013 14:34:28 -0700 (PDT) Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano? 2013?5?6????UTC+8??3?10?47???? > On Sun, 05 May 2013 12:11:11 -0500, Ignoramus16992 wrote: > > > > > According to CIO.com, Python programmers make only $83,000 per year, > > > while Perl programmers make $93,000 per year. > > > > > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10 > > > http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11 > > > > > > I would like to know, what explains the discrepancy. > > > > Perl is much harder to use, so the average Perl programmer burns out > > after a few years and takes up a less stressful career, like going > > undercover in the Russian mob or the Taliban. So only the most dedicated, > > brilliant and extreme programmers last long enough to become a Perl > > expert, and consequently can demand higher pay, while any idiot can learn > > to program Python, as I have. > > > > Also, Perl programmers are an unprincipled, devious bunch, always looking > > for an opportunity to blackmail their employers into paying them extra. > > Python programmers are a decent, law-abiding people with a strong moral > > code who would never stoop to the sort of things that Perl coders are > > proud of doing. > > > > > > > > -- > > Steven Some bosses just like the 1 to 5 liners of the Perl style in some cryptic forms from villain Perl programmers. I did see the same tricks in the Lisp or C/C++ before but with extremely long fat source codes in tens of thousands of lines that could also pleased some managers or bosses. From llanitedave at veawb.coop Mon May 6 02:13:53 2013 From: llanitedave at veawb.coop (llanitedave) Date: Sun, 5 May 2013 23:13:53 -0700 (PDT) Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sunday, May 5, 2013 12:10:47 PM UTC-7, Steven D'Aprano wrote: > > > Also, Perl programmers are an unprincipled, devious bunch, always looking > > for an opportunity to blackmail their employers into paying them extra. > > Python programmers are a decent, law-abiding people with a strong moral > > code who would never stoop to the sort of things that Perl coders are > > proud of doing. > > > > -- > > Steven And of course, the Python Programmer's moral code is only 80 characters wide. From fabiosantosart at gmail.com Mon May 6 02:28:36 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 6 May 2013 07:28:36 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: > And of course, the Python Programmer's moral code is only 80 characters wide. No! Was it not seventy characters wide? Was I fooled my entire life? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon May 6 03:30:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 17:30:33 +1000 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, May 6, 2013 at 4:28 PM, F?bio Santos wrote: >> And of course, the Python Programmer's moral code is only 80 characters >> wide. > > No! Was it not seventy characters wide? Was I fooled my entire life? Well you see, it was 70 bytes back in the Python 2 days (I'll defer to Steven for data points earlier than that), but with Python 3, there were two versions: one was 140 bytes representing 70 characters, the other 280 bytes representing 70 characters. In Python 3.3, they were merged, and a trivial amount of overhead added, so now it's 80 bytes representing 70 characters. But you have an absolute guarantee that it's correct now. Of course, the entire code can be represented as a single int now. You used to have to use a long. ChrisA From fabiosantosart at gmail.com Mon May 6 03:49:54 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 6 May 2013 08:49:54 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 6 May 2013 08:34, "Chris Angelico" wrote: > Well you see, it was 70 bytes back in the Python 2 days (I'll defer to > Steven for data points earlier than that), but with Python 3, there > were two versions: one was 140 bytes representing 70 characters, the > other 280 bytes representing 70 characters. In Python 3.3, they were > merged, and a trivial amount of overhead added, so now it's 80 bytes > representing 70 characters. But you have an absolute guarantee that > it's correct now. > > Of course, the entire code can be represented as a single int now. You > used to have to use a long. > > ChrisA > -- Thanks. You have made my day. I may rise the average pay of a Python programmer in Portugal. I have asked for a raise back in December, and was told that it wouldn't happen before this year. I have done well. I think I deserve better pay than a supermarket employee now. I am sure that my efforts were appreciated and I will be rewarded. I am being sarcastic. The above paragraph wouldn't be true if I programmed in perl, c++ or lisp. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kliateni at gmail.com Mon May 6 03:55:55 2013 From: kliateni at gmail.com (Karim) Date: Mon, 06 May 2013 09:55:55 +0200 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5187620B.3030106@gmail.com> Le 06/05/2013 09:49, F?bio Santos a ?crit : > have asked for a raise back in December, and was told that it wouldn't > happen before this year. I have done well. I think I deserve better > pay than a supermarket employee now. I am sure that my efforts were > appreciated and I will be rewarded. I am being sarc Austerity for python programmers in Portugal !? ;o) Karim From fabiosantosart at gmail.com Mon May 6 03:58:31 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 6 May 2013 08:58:31 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: <5187620B.3030106@gmail.com> References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> <5187620B.3030106@gmail.com> Message-ID: On 6 May 2013 08:55, "Karim" wrote: > > Austerity for python programmers in Portugal !? Actually, lack of a market. I can't seem to find any other job programming python. It's all php, VB, c#, and I think there is some COBOL in there too. But who knows. Maybe I'm looking in the wrong places. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon May 6 04:02:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 18:02:25 +1000 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, May 6, 2013 at 5:49 PM, F?bio Santos wrote: > I may rise the average pay of a Python programmer in Portugal. I have asked > for a raise back in December, and was told that it wouldn't happen before > this year. I have done well. I think I deserve better pay than a supermarket > employee now. I am sure that my efforts were appreciated and I will be > rewarded. I am being sarcastic. > > The above paragraph wouldn't be true if I programmed in perl, c++ or lisp. I dunno, it depends more on where you work than what you work with. I'm the top employee at my workplace and use C++, Pike, PHP, Javascript, and bash, plus ancillaries like SQL... and my salary is definitely on the low end. Why? Because I work for a startup. No doubt Google or IBM would pay their top people way more than I'm getting, but that's not something a little internet startup can afford. So I bide my time :) Some day we'll be massively profitable... some day. And if not, hey, I'm making enough to survive. Of course, there's the whole thing of "how easy would it be to replace you" too. If the only language you know is PHP, chances are you can be replaced by any idiot straight out of secondary skool (sorry, that's school_real_secondary now isn't it), but someone who speaks FORTRAN and knows the bank's internal systems well enough to maintain them can ask for whatever salary he likes and still be cheaper than finding a replacement. But that expert FORTRAN programmer, if he quit his job and went searching, would quite probably find himself at the lower end again if he joined a small company. ChrisA From fabiosantosart at gmail.com Mon May 6 05:00:54 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 6 May 2013 10:00:54 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: All good points. I should probably blame the smallness of my company aswell. On 6 May 2013 09:11, "Chris Angelico" wrote: > On Mon, May 6, 2013 at 5:49 PM, F?bio Santos > wrote: > > I may rise the average pay of a Python programmer in Portugal. I have > asked > > for a raise back in December, and was told that it wouldn't happen before > > this year. I have done well. I think I deserve better pay than a > supermarket > > employee now. I am sure that my efforts were appreciated and I will be > > rewarded. I am being sarcastic. > > > > The above paragraph wouldn't be true if I programmed in perl, c++ or > lisp. > > I dunno, it depends more on where you work than what you work with. > I'm the top employee at my workplace and use C++, Pike, PHP, > Javascript, and bash, plus ancillaries like SQL... and my salary is > definitely on the low end. Why? Because I work for a startup. No doubt > Google or IBM would pay their top people way more than I'm getting, > but that's not something a little internet startup can afford. So I > bide my time :) Some day we'll be massively profitable... some day. > And if not, hey, I'm making enough to survive. > > Of course, there's the whole thing of "how easy would it be to replace > you" too. If the only language you know is PHP, chances are you can be > replaced by any idiot straight out of secondary skool (sorry, that's > school_real_secondary now isn't it), but someone who speaks FORTRAN > and knows the bank's internal systems well enough to maintain them can > ask for whatever salary he likes and still be cheaper than finding a > replacement. But that expert FORTRAN programmer, if he quit his job > and went searching, would quite probably find himself at the lower end > again if he joined a small company. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wxjmfauth at gmail.com Tue May 7 09:22:47 2013 From: wxjmfauth at gmail.com (jmfauth) Date: Tue, 7 May 2013 06:22:47 -0700 (PDT) Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 6 mai, 09:49, F?bio Santos wrote: > On 6 May 2013 08:34, "Chris Angelico" wrote: > > > Well you see, it was 70 bytes back in the Python 2 days (I'll defer to > > Steven for data points earlier than that), but with Python 3, there > > were two versions: one was 140 bytes representing 70 characters, the > > other 280 bytes representing 70 characters. In Python 3.3, they were > > merged, and a trivial amount of overhead added, so now it's 80 bytes > > representing 70 characters. But you have an absolute guarantee that > > it's correct now. > > > Of course, the entire code can be represented as a single int now. You > > used to have to use a long. > > > ChrisA > > -- > > Thanks. You have made my day. > > I may rise the average pay of a Python programmer in Portugal. I have asked > for a raise back in December, and was told that it wouldn't happen before > this year. I have done well. I think I deserve better pay than a > supermarket employee now. I am sure that my efforts were appreciated and I > will be rewarded. I am being sarcastic. > > The above paragraph wouldn't be true if I programmed in perl, c++ or lisp. ----- 1) The memory gain for many of us (usually non ascii users) just become irrelevant. >>> sys.getsizeof('ma??') 41 >>> sys.getsizeof('abcd') 29 2) More critical, Py 3.3, just becomes non unicode compliant, (eg European languages or "ascii" typographers !) >>> import timeit >>> timeit.timeit("'abcd'*1000 + 'a'") 2.186670111428325 >>> timeit.timeit("'abcd'*1000 + '?'") 2.9951699820528432 >>> timeit.timeit("'abcd'*1000 + '?'") 3.0036780444886233 >>> timeit.timeit("'abcd'*1000 + '?'") 3.004992278824048 >>> timeit.timeit("'ma??'*1000 + '?'") 3.231025618708202 >>> timeit.timeit("'ma??'*1000 + '?'") 3.215894398100758 >>> timeit.timeit("'ma??'*1000 + '?'") 3.224407974255655 >>> timeit.timeit("'ma??'*1000 + '?'") 3.2206342273566406 >>> timeit.timeit("'abcd'*1000 + '?'") 2.9914403449067777 3) Python is "pround" to cover the whole unicode range, unfortunately it "breaks" the BMP range. Small GvR exemple (ascii) from the the bug list, but with non ascii characters. # Py 3.2, all chars >>> timeit.repeat("a = 'hundred'; 'x' in a") [0.09087790617297742, 0.07456871885972305, 0.07449940353376405] >>> timeit.repeat("a = 'ma?????'; 'x' in a") [0.10088136800095526, 0.07488497003487282, 0.07497594640028638] # Py 3.3 ascii and non ascii chars >>> timeit.repeat("a = 'hundred'; 'x' in a") [0.11426985953005442, 0.10040049292649655, 0.09920834808588097] >>> timeit.repeat("a = 'ma?????'; '?' in a") [0.2345595188256766, 0.21637172864154763, 0.2179096624382737] There are plenty of good reasons to use Python. There are also plenty of good reasons to not use (or now to drop) Python and to realize that if you wish to process text seriously, you are better served by using "corporate products" or tools using Unicode properly. jmf From rosuav at gmail.com Tue May 7 09:32:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 23:32:55 +1000 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, May 7, 2013 at 11:22 PM, jmfauth wrote: > There are plenty of good reasons to use Python. There are > also plenty of good reasons to not use (or now to drop) > Python and to realize that if you wish to process text > seriously, you are better served by using "corporate > products" or tools using Unicode properly. There are plenty of good reasons to use Python. One of them is the laughs you can get any time jmf posts here. There are also plenty of good reasons to drop Python. One of them is because corporate products like Microsoft Visual Studio are inherently better specifically because they cost you money, and there's no way that something you paid nothing for can ever be as good as that. Plus, you get to write code that works on only one platform, and that's really good. Finally, moving off Python would mean you don't feel obliged to respond to jmf, which will increase your productivity measurably. ChrisA From fabiosantosart at gmail.com Tue May 7 09:35:05 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 7 May 2013 14:35:05 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: > > > ----- > > > 1) The memory gain for many of us (usually non ascii users) > just become irrelevant. > > >>> sys.getsizeof('ma??') > 41 > >>> sys.getsizeof('abcd') > 29 > > 2) More critical, Py 3.3, just becomes non unicode compliant, > (eg European languages or "ascii" typographers !) > > >>> import timeit > >>> timeit.timeit("'abcd'*1000 + 'a'") > 2.186670111428325 > >>> timeit.timeit("'abcd'*1000 + '?'") > 2.9951699820528432 > >>> timeit.timeit("'abcd'*1000 + '?'") > 3.0036780444886233 > >>> timeit.timeit("'abcd'*1000 + '?'") > 3.004992278824048 > >>> timeit.timeit("'ma??'*1000 + '?'") > 3.231025618708202 > >>> timeit.timeit("'ma??'*1000 + '?'") > 3.215894398100758 > >>> timeit.timeit("'ma??'*1000 + '?'") > 3.224407974255655 > >>> timeit.timeit("'ma??'*1000 + '?'") > 3.2206342273566406 > >>> timeit.timeit("'abcd'*1000 + '?'") > 2.9914403449067777 > > 3) Python is "pround" to cover the whole unicode range, > unfortunately it "breaks" the BMP range. > Small GvR exemple (ascii) from the the bug list, > but with non ascii characters. > > # Py 3.2, all chars > > >>> timeit.repeat("a = 'hundred'; 'x' in a") > [0.09087790617297742, 0.07456871885972305, 0.07449940353376405] > >>> timeit.repeat("a = 'ma?????'; 'x' in a") > [0.10088136800095526, 0.07488497003487282, 0.07497594640028638] > > > # Py 3.3 ascii and non ascii chars > >>> timeit.repeat("a = 'hundred'; 'x' in a") > [0.11426985953005442, 0.10040049292649655, 0.09920834808588097] > >>> timeit.repeat("a = 'ma?????'; '?' in a") > [0.2345595188256766, 0.21637172864154763, 0.2179096624382737] > > > There are plenty of good reasons to use Python. There are > also plenty of good reasons to not use (or now to drop) > Python and to realize that if you wish to process text > seriously, you are better served by using "corporate > products" or tools using Unicode properly. > > jmf This is so off-topic that, after reading this, I feel I have just returned from the Moon. OTOH, it would seem like you know the Portuguese word for apple, so I also feel home. I am so confused. -------------- next part -------------- An HTML attachment was scrubbed... URL: From square.steve at gmail.com Tue May 7 10:17:52 2013 From: square.steve at gmail.com (Steve Simmons) Date: Tue, 07 May 2013 15:17:52 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4a070992-b5bd-4889-ac9a-286806b4389c@email.android.com> "F?bio Santos" wrote: >> >> >> ----- >> >> >> 1) The memory gain for many of us (usually non ascii users) >> just become irrelevant. >> >> >>> sys.getsizeof('ma??') >> 41 >> >>> sys.getsizeof('abcd') >> 29 >> >> 2) More critical, Py 3.3, just becomes non unicode compliant, >> (eg European languages or "ascii" typographers !) >> >> >>> import timeit >> >>> timeit.timeit("'abcd'*1000 + 'a'") >> 2.186670111428325 >> >>> timeit.timeit("'abcd'*1000 + '?'") >> 2.9951699820528432 >> >>> timeit.timeit("'abcd'*1000 + '?'") >> 3.0036780444886233 >> >>> timeit.timeit("'abcd'*1000 + '?'") >> 3.004992278824048 >> >>> timeit.timeit("'ma??'*1000 + '?'") >> 3.231025618708202 >> >>> timeit.timeit("'ma??'*1000 + '?'") >> 3.215894398100758 >> >>> timeit.timeit("'ma??'*1000 + '?'") >> 3.224407974255655 >> >>> timeit.timeit("'ma??'*1000 + '?'") >> 3.2206342273566406 >> >>> timeit.timeit("'abcd'*1000 + '?'") >> 2.9914403449067777 >> >> 3) Python is "pround" to cover the whole unicode range, >> unfortunately it "breaks" the BMP range. >> Small GvR exemple (ascii) from the the bug list, >> but with non ascii characters. >> >> # Py 3.2, all chars >> >> >>> timeit.repeat("a = 'hundred'; 'x' in a") >> [0.09087790617297742, 0.07456871885972305, 0.07449940353376405] >> >>> timeit.repeat("a = 'ma?????'; 'x' in a") >> [0.10088136800095526, 0.07488497003487282, 0.07497594640028638] >> >> >> # Py 3.3 ascii and non ascii chars >> >>> timeit.repeat("a = 'hundred'; 'x' in a") >> [0.11426985953005442, 0.10040049292649655, 0.09920834808588097] >> >>> timeit.repeat("a = 'ma?????'; '?' in a") >> [0.2345595188256766, 0.21637172864154763, 0.2179096624382737] >> >> >> There are plenty of good reasons to use Python. There are >> also plenty of good reasons to not use (or now to drop) >> Python and to realize that if you wish to process text >> seriously, you are better served by using "corporate >> products" or tools using Unicode properly. >> >> jmf > >This is so off-topic that, after reading this, I feel I have just >returned >from the Moon. > >OTOH, it would seem like you know the Portuguese word for apple, so I >also >feel home. > >I am so confused. > > >------------------------------------------------------------------------ > >-- >http://mail.python.org/mailman/listinfo/python-list Good to see jmf finally comparing apples with apples :-) Sent from a Galaxy far far away -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Tue May 7 23:06:02 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 May 2013 03:06:02 GMT Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5189c11a$0$11094$c3e8da3@news.astraweb.com> On Tue, 07 May 2013 15:17:52 +0100, Steve Simmons wrote: > Good to see jmf finally comparing apples with apples :-) *groans* Truly the terrible pun that the terrible hijacking deserves. -- Steven From tjreedy at udel.edu Tue May 7 14:40:03 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Tue, 07 May 2013 14:40:03 -0400 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 5/7/2013 9:22 AM, jmfauth road forth on his dead hobbyhorse to hijack yet another thread: > # Py 3.3 ascii and non ascii chars >>>> timeit.repeat("a = 'hundred'; 'x' in a") > [0.11426985953005442, 0.10040049292649655, 0.09920834808588097] >>>> timeit.repeat("a = 'ma?????'; '?' in a") > [0.2345595188256766, 0.21637172864154763, 0.2179096624382737] Python 3.3 is a language. Languages do not have timings. CPython 3.3.0 is an implementation compiled and run under a particular OS and hardware. With respect to Unicode timings, especially for find/replace, it is obsolete. On my Win7 machine with fresh debug builds from the current repository, I see these times. Python 3.3.1+ (default, May 7 2013, 14:03:12) [MSC v.1600 32 bit (Int >>> from timeit import repeat >>> repeat("a = 'hundred'; 'x' in a") [0.19007337649622968, 0.190116721780754, 0.1900149679567562] >>> repeat("a = 'ma?a???'; '?' in a") [0.20568874581187716, 0.20568782357178053, 0.20577051776710914] Python 3.4.0a0 (default:32067784f198, May 7 2013, 13:59:10) [MSC v.1600 >>> from timeit import repeat >>> repeat("a = 'hundred'; 'x' in a") [0.1708080882915779, 0.17062978853956826, 0.1706740560642051] >>> repeat("a = 'ma?a???'; '?' in a") [0.17612111348809734, 0.17562925210324565, 0.17549245315558437] Note 1: debug builds are slower than install builds, especially for microbenchmarks with trivial statements. My installed 3.3.1 on a different machine has timings of about .1 for the ascii test. It is slower for the non-ascii test because the latest improvements were made after 3.3.1 was released. Note 2: 3.4 has additional improvements that speed up everything, so that the 3.4 non-ascii time is faster that even the 3.3 ascii time. Terry From dihedral88888 at googlemail.com Tue May 7 16:41:58 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 7 May 2013 13:41:58 -0700 (PDT) Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9fcf8320-a54a-43cb-9ded-0c8ee7e2c41c@googlegroups.com> Chris Angelico? 2013?5?7????UTC+8??9?32?55???? > On Tue, May 7, 2013 at 11:22 PM, jmfauth wrote: > > > There are plenty of good reasons to use Python. There are > > > also plenty of good reasons to not use (or now to drop) > > > Python and to realize that if you wish to process text > > > seriously, you are better served by using "corporate > > > products" or tools using Unicode properly. > > > > There are plenty of good reasons to use Python. One of them is the > > laughs you can get any time jmf posts here. There are also plenty of > > good reasons to drop Python. One of them is because corporate products > > like Microsoft Visual Studio are inherently better specifically > > because they cost you money, and there's no way that something you > > paid nothing for can ever be as good as that. Plus, you get to write > People used MS products because most bosses did not want to pay the prices of work stations, the minis, or the main-frames and the salaries of the system administrators in 199x. > code that works on only one platform, and that's really good. Finally, > > moving off Python would mean you don't feel obliged to respond to jmf, > > which will increase your productivity measurably. > > > > ChrisA The price issue of a software package or a platform is not the only way to judge a programming language. From walterhurry at lavabit.com Tue May 7 17:14:08 2013 From: walterhurry at lavabit.com (Walter Hurry) Date: Tue, 7 May 2013 21:14:08 +0000 (UTC) Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, 07 May 2013 23:32:55 +1000, Chris Angelico wrote: > On Tue, May 7, 2013 at 11:22 PM, jmfauth wrote: >> There are plenty of good reasons to use Python. There are also plenty >> of good reasons to not use (or now to drop) Python and to realize that >> if you wish to process text seriously, you are better served by using >> "corporate products" or tools using Unicode properly. > > There are plenty of good reasons to use Python. One of them is the > laughs you can get any time jmf posts here. There are also plenty of > good reasons to drop Python. One of them is because corporate products > like Microsoft Visual Studio are inherently better specifically because > they cost you money, and there's no way that something you paid nothing > for can ever be as good as that. Plus, you get to write code that works > on only one platform, and that's really good. Finally, > moving off Python would mean you don't feel obliged to respond to jmf, > which will increase your productivity measurably. TMML. Thanks! From nhodgson at iinet.net.au Tue May 7 20:34:35 2013 From: nhodgson at iinet.net.au (Neil Hodgson) Date: Wed, 08 May 2013 10:34:35 +1000 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: jmfauth: > 2) More critical, Py 3.3, just becomes non unicode compliant, > (eg European languages or "ascii" typographers !) > ... This is not demonstrating non-compliance. It is comparing performance, not compliance. Please show an example where Python 3.3 is not compliant with Unicode. Neil From benjamin.kaplan at case.edu Tue May 7 21:11:26 2013 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Tue, 7 May 2013 18:11:26 -0700 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On May 7, 2013 5:42 PM, "Neil Hodgson" wrote: > > jmfauth: > >> 2) More critical, Py 3.3, just becomes non unicode compliant, >> (eg European languages or "ascii" typographers !) >> ... > > > This is not demonstrating non-compliance. It is comparing performance, not compliance. > > Please show an example where Python 3.3 is not compliant with Unicode. > > Neil > -- > http://mail.python.org/mailman/listinfo/python-list It's violating page 1+1j of the Unicode spec, where it says precisely how long each operation is allowed to take. Only wise people can see that page. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Tue May 7 21:23:21 2013 From: davea at davea.name (Dave Angel) Date: Tue, 07 May 2013 21:23:21 -0400 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5189A909.8080306@davea.name> On 05/07/2013 09:11 PM, Benjamin Kaplan wrote: > On May 7, 2013 5:42 PM, "Neil Hodgson" wrote: >> >> jmfauth: >> >>> 2) More critical, Py 3.3, just becomes non unicode compliant, >>> (eg European languages or "ascii" typographers !) >>> ... >> >> >> This is not demonstrating non-compliance. It is comparing performance, > not compliance. >> >> Please show an example where Python 3.3 is not compliant with Unicode. >> >> Neil >> -- >> http://mail.python.org/mailman/listinfo/python-list > > It's violating page 1+1j of the Unicode spec, where it says precisely how > long each operation is allowed to take. Only wise people can see that page. > > > Of course! It's a complex page. -- DaveA From breamoreboy at yahoo.co.uk Tue May 7 21:22:50 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 08 May 2013 02:22:50 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 08/05/2013 01:34, Neil Hodgson wrote: > jmfauth: > >> 2) More critical, Py 3.3, just becomes non unicode compliant, >> (eg European languages or "ascii" typographers !) >> ... > > This is not demonstrating non-compliance. It is comparing > performance, not compliance. > > Please show an example where Python 3.3 is not compliant with Unicode. > > Neil Surely nobody expects an answer, although I suppose there is always a first time for everything. Once again stealing from Tommy Docherty, jmfauth is to Python what King Herod was to baby sitting. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From rustompmody at gmail.com Wed May 8 06:43:00 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 8 May 2013 03:43:00 -0700 (PDT) Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On May 8, 6:11?am, Benjamin Kaplan wrote: > On May 7, 2013 5:42 PM, "Neil Hodgson" wrote: > > jmfauth: > > >> 2) More critical, Py 3.3, just becomes non unicode compliant, > >> (eg European languages or "ascii" typographers !) > >> ... > > > ? ?This is not demonstrating non-compliance. It is comparing performance, > not compliance. > > > ? ?Please show an example where Python 3.3 is not compliant with Unicode. > > > ? ?Neil > > It's violating page 1+1j of the Unicode spec, where it says precisely how > long each operation is allowed to take. Only wise people can see that page. Ha Ha! -- I infer some Harry Potter here! [Boarding train at platform 7.3 ] From steve+comp.lang.python at pearwood.info Mon May 6 08:03:15 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 May 2013 12:03:15 GMT Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51879c03$0$29997$c3e8da3$5496439d@news.astraweb.com> On Mon, 06 May 2013 17:30:33 +1000, Chris Angelico wrote: > On Mon, May 6, 2013 at 4:28 PM, F?bio Santos > wrote: >>> And of course, the Python Programmer's moral code is only 80 >>> characters wide. >> >> No! Was it not seventy characters wide? Was I fooled my entire life? > > Well you see, it was 70 bytes back in the Python 2 days (I'll defer to > Steven for data points earlier than that), You had bytes? You were lucky. When I were a lad, we used to have to program by punching holes in stone tablets with our head. You-tell-young-people-this-today-and-they-don't-believe-you-ly y'rs, -- Steven From joshua.landau.ws at gmail.com Mon May 6 12:04:30 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 6 May 2013 17:04:30 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: <51879c03$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> <51879c03$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 6 May 2013 13:03, Steven D'Aprano wrote: > On Mon, 06 May 2013 17:30:33 +1000, Chris Angelico wrote: > > > On Mon, May 6, 2013 at 4:28 PM, F?bio Santos > > wrote: > >>> And of course, the Python Programmer's moral code is only 80 > >>> characters wide. > >> > >> No! Was it not seventy characters wide? Was I fooled my entire life? > > > > Well you see, it was 70 bytes back in the Python 2 days (I'll defer to > > Steven for data points earlier than that), > > > You had bytes? You were lucky. When I were a lad, we used to have to > program by punching holes in stone tablets with our head. > > > You-tell-young-people-this-today-and-they-don't-believe-you-ly y'rs, I do have my doubts, yes. -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Mon May 6 07:41:49 2013 From: roy at panix.com (Roy Smith) Date: Mon, 06 May 2013 07:41:49 -0400 Subject: Why do Perl programmers make more money than Python programmers References: <5186aeb6$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , F?bio Santos wrote: > > And of course, the Python Programmer's moral code is only 80 characters > wide. > > No! Was it not seventy characters wide? Was I fooled my entire life? The entire moral code in a semi-tweet. How convenient. From no.email at nospam.invalid Sun May 5 17:16:17 2013 From: no.email at nospam.invalid (Paul Rubin) Date: Sun, 05 May 2013 14:16:17 -0700 Subject: Why do Perl programmers make more money than Python programmers References: Message-ID: <7xehdlp1pa.fsf@ruckus.brouhaha.com> Ignoramus16992 writes: > I would like to know, what explains the discrepancy. I see "New York" listed as a location for Perl but not for Python. That implies: 1) some general skew because of the very high cost of living in NY (even compared to San Francisco or Silicon Valley); 2) further skew because a good chuck of the NY programming jobs are in the financial sector, which shovels money around with heavy farm equipment (but is reportedly otherwise unpleasant to work in). Wall Street has done very well in the past few years, and some of that shows up as bonuses for the involved parties. I remember seeing a ridiculously high figure listed for Haskell, and then realized the reason for it was similar to the above. Most Haskell programmers I know can't actually get Haskell jobs. From no.email at nospam.invalid Sun May 5 17:17:35 2013 From: no.email at nospam.invalid (Paul Rubin) Date: Sun, 05 May 2013 14:17:35 -0700 Subject: Why do Perl programmers make more money than Python programmers References: <7xehdlp1pa.fsf@ruckus.brouhaha.com> Message-ID: <7xa9o9p1n4.fsf@ruckus.brouhaha.com> Paul Rubin writes: > I see "New York" listed as a location for Perl but not for Python. Whaat? It's there for Python, though in the #3 position rather than #2. I must have flipped through the slides too fast. From ignoramus16992 at NOSPAM.16992.invalid Sun May 5 19:05:40 2013 From: ignoramus16992 at NOSPAM.16992.invalid (Ignoramus16992) Date: Sun, 05 May 2013 18:05:40 -0500 Subject: Why do Perl programmers make more money than Python programmers References: <7xehdlp1pa.fsf@ruckus.brouhaha.com> <7xa9o9p1n4.fsf@ruckus.brouhaha.com> Message-ID: <17idnc73z7tZeBvMnZ2dnUVZ_vWdnZ2d@giganews.com> On 2013-05-05, Paul Rubin wrote: > Paul Rubin writes: >> I see "New York" listed as a location for Perl but not for Python. > > Whaat? It's there for Python, though in the #3 position rather than #2. > I must have flipped through the slides too fast. My website algebra.com is written in perl, it is now #1,198 in the US rankings, based on Quantcast. I could not be happier with maintainability and robustness features of perl. i going undercover for the russian mob next week From news at lawshouse.org Mon May 6 02:07:12 2013 From: news at lawshouse.org (Henry Law) Date: Mon, 06 May 2013 07:07:12 +0100 Subject: Why do Perl programmers make more money than Python programmers In-Reply-To: References: Message-ID: On 05/05/13 18:11, Ignoramus16992 wrote: > According to CIO.com What an amusing thread; lightened my (non-programmer) day. -- Henry Law Manchester, England From ignoramus30465 at NOSPAM.30465.invalid Mon May 6 21:53:13 2013 From: ignoramus30465 at NOSPAM.30465.invalid (Ignoramus30465) Date: Mon, 06 May 2013 20:53:13 -0500 Subject: Why do Perl programmers make more money than Python programmers References: Message-ID: On 2013-05-06, Henry Law wrote: > On 05/05/13 18:11, Ignoramus16992 wrote: >> According to CIO.com > > What an amusing thread; lightened my (non-programmer) day. > I guess you are from the python newsgroup then. :) From bradley.wright.biz at gmail.com Sun May 5 20:59:15 2013 From: bradley.wright.biz at gmail.com (Bradley Wright) Date: Sun, 5 May 2013 17:59:15 -0700 (PDT) Subject: (Learner-here) Lists + Functions = headache Message-ID: Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced. I'm writing a function that takes a list(one they supply during runtime) here's what my function is supposed to do 1. for each instance of the string "fizz" make a count 2. Finally return that count here's my code: def fizz_cout(x): count = 0 for item in x: while item == "fizz": count += 1 return count Please remember that i am a eager beginner, where am i going wrong? From wuwei23 at gmail.com Sun May 5 21:21:33 2013 From: wuwei23 at gmail.com (alex23) Date: Sun, 5 May 2013 18:21:33 -0700 (PDT) Subject: (Learner-here) Lists + Functions = headache References: Message-ID: <31f31fc5-65a8-46af-8be8-69c71901278a@tz3g2000pbb.googlegroups.com> On May 6, 10:59?am, Bradley Wright wrote: > def fizz_cout(x): > ? ? count = 0 > ? ? for item in x: > ? ? ? ? while item == "fizz": > ? ? ? ? ? ? count += 1 > ? ? ? ? ? ? return count > > Please remember that i am a eager beginner, where am i going wrong? There are several problems with your code: > ? ? for item in x: > ? ? ? ? while item == "fizz": > ? ? ? ? ? ? count += 1 The `for` takes an item out of the list `x`. If that item is the string 'fizz', it increments count. As it's a `while` loop, it will continue to increment for as long as `item` is 'fizz'. Since the while loop doesn't look up another list item, it will remain as 'fizz' until the end of time. Well, it would except for your second bug: > ? ? ? ? while item == "fizz": > ? ? ? ? ? ? count += 1 > ? ? ? ? ? ? return count The very first time it encounters a list item that is 'fizz', it adds one to `count`, then exits the function passing back `count`. You want to move the return to _outside_ the for loop, and you want to change your `while` condition to an `if` instead. From bradley.wright.biz at gmail.com Sun May 5 21:24:44 2013 From: bradley.wright.biz at gmail.com (Bradley Wright) Date: Sun, 5 May 2013 18:24:44 -0700 (PDT) Subject: (Learner-here) Lists + Functions = headache In-Reply-To: <31f31fc5-65a8-46af-8be8-69c71901278a@tz3g2000pbb.googlegroups.com> References: <31f31fc5-65a8-46af-8be8-69c71901278a@tz3g2000pbb.googlegroups.com> Message-ID: On Sunday, May 5, 2013 9:21:33 PM UTC-4, alex23 wrote: > On May 6, 10:59?am, Bradley Wright > > wrote: > > > def fizz_cout(x): > > > ? ? count = 0 > > > ? ? for item in x: > > > ? ? ? ? while item == "fizz": > > > ? ? ? ? ? ? count += 1 > > > ? ? ? ? ? ? return count > > > > > > Please remember that i am a eager beginner, where am i going wrong? > > > > There are several problems with your code: > > > > > ? ? for item in x: > > > ? ? ? ? while item == "fizz": > > > ? ? ? ? ? ? count += 1 > > > > The `for` takes an item out of the list `x`. If that item is the > > string 'fizz', it increments count. As it's a `while` loop, it will > > continue to increment for as long as `item` is 'fizz'. Since the while > > loop doesn't look up another list item, it will remain as 'fizz' until > > the end of time. Well, it would except for your second bug: > > > > > ? ? ? ? while item == "fizz": > > > ? ? ? ? ? ? count += 1 > > > ? ? ? ? ? ? return count > > > > The very first time it encounters a list item that is 'fizz', it adds > > one to `count`, then exits the function passing back `count`. > > > > You want to move the return to _outside_ the for loop, and you want to > > change your `while` condition to an `if` instead. Thank you Alex - much appreciated, about to implement right now! From bradley.wright.biz at gmail.com Sun May 5 21:30:04 2013 From: bradley.wright.biz at gmail.com (Bradley Wright) Date: Sun, 5 May 2013 18:30:04 -0700 (PDT) Subject: (Learner-here) Lists + Functions = headache In-Reply-To: References: <31f31fc5-65a8-46af-8be8-69c71901278a@tz3g2000pbb.googlegroups.com> Message-ID: <44cd4048-c988-4594-91d9-72ab2b56ad4c@googlegroups.com> On Sunday, May 5, 2013 9:24:44 PM UTC-4, Bradley Wright wrote: > On Sunday, May 5, 2013 9:21:33 PM UTC-4, alex23 wrote: > > > On May 6, 10:59?am, Bradley Wright > > > > > > wrote: > > > > > > > def fizz_cout(x): > > > > > > > ? ? count = 0 > > > > > > > ? ? for item in x: > > > > > > > ? ? ? ? while item == "fizz": > > > > > > > ? ? ? ? ? ? count += 1 > > > > > > > ? ? ? ? ? ? return count > > > > > > > > > > > > > > Please remember that i am a eager beginner, where am i going wrong? > > > > > > > > > > > > There are several problems with your code: > > > > > > > > > > > > > ? ? for item in x: > > > > > > > ? ? ? ? while item == "fizz": > > > > > > > ? ? ? ? ? ? count += 1 > > > > > > > > > > > > The `for` takes an item out of the list `x`. If that item is the > > > > > > string 'fizz', it increments count. As it's a `while` loop, it will > > > > > > continue to increment for as long as `item` is 'fizz'. Since the while > > > > > > loop doesn't look up another list item, it will remain as 'fizz' until > > > > > > the end of time. Well, it would except for your second bug: > > > > > > > > > > > > > ? ? ? ? while item == "fizz": > > > > > > > ? ? ? ? ? ? count += 1 > > > > > > > ? ? ? ? ? ? return count > > > > > > > > > > > > The very first time it encounters a list item that is 'fizz', it adds > > > > > > one to `count`, then exits the function passing back `count`. > > > > > > > > > > > > You want to move the return to _outside_ the for loop, and you want to > > > > > > change your `while` condition to an `if` instead. > > > > Thank you Alex - much appreciated, about to implement right now! Aha! lessons learned - got it! From breamoreboy at yahoo.co.uk Mon May 6 05:25:59 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 06 May 2013 10:25:59 +0100 Subject: (Learner-here) Lists + Functions = headache In-Reply-To: <44cd4048-c988-4594-91d9-72ab2b56ad4c@googlegroups.com> References: <31f31fc5-65a8-46af-8be8-69c71901278a@tz3g2000pbb.googlegroups.com> <44cd4048-c988-4594-91d9-72ab2b56ad4c@googlegroups.com> Message-ID: On 06/05/2013 02:30, Bradley Wright wrote: > > Aha! lessons learned - got it! > The next lesson is to read the link given in my signature, digest it and take action to avoid masses of superfluous newlines in your responses. TIA. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From tjreedy at udel.edu Sun May 5 21:30:25 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 05 May 2013 21:30:25 -0400 Subject: (Learner-here) Lists + Functions = headache In-Reply-To: References: Message-ID: On 5/5/2013 8:59 PM, Bradley Wright wrote: > Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced. > > I'm writing a function that takes a list(one they supply during runtime) > here's what my function is supposed to do Do they supply an example so you can test both your comprehension and code? I think most specs given in natural language need such. > 1. for each instance of the string "fizz" make a count > 2. Finally return that count Did you create an example problem to test your code? If not, do so. Did you run your function with example data? If not, do so. > here's my code: > > def fizz_cout(x): > count = 0 > for item in x: > while item == "fizz": > count += 1 > return count Here is hint as to some of what needs to be improved: this function will return either 1 or None. You should have discovered that by testings. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Sun May 5 21:31:48 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 May 2013 01:31:48 GMT Subject: (Learner-here) Lists + Functions = headache References: Message-ID: <51870803$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sun, 05 May 2013 17:59:15 -0700, Bradley Wright wrote: > Hey guys and gals doing this tutorial(codecademy) and needed a bit help > from the experienced. > > I'm writing a function that takes a list(one they supply during runtime) > here's what my function is supposed to do > > 1. for each instance of the string "fizz" make a count > 2. Finally return that count > > here's my code: > > def fizz_cout(x): > count = 0 > for item in x: > while item == "fizz": > count += 1 > return count > > Please remember that i am a eager beginner, where am i going wrong? Lots of places, sorry. The first thing you're doing is hoping that we will guess what error you are getting. In this case, it so happens that we can, but that will not always be the case. You should always give us the code (which you have done), the data it is running on, the expected result, and the actual result. Within reason: don't bombard us with 10,000 lines of code and 3MB of data. Now, moving on to your function: try walking through it yourself in your head. You start off by defining a value, then start iterating over each value in x, one at a time. count = 0 for item in x: Suppose x = ["buzz", "fizz", "buzz", "fizz"]. Then you should get a result of 2. So far, you start with count = 0. Then you enter the for loop, and item gets the value "buzz". The next line enters a while loop: while item == "fizz": Since item does *not* equal "fizz", the body of the while loop does not run at all, and Python jumps past the while loop, which takes it to the end of the for loop. So Python goes on to the next item. This time item gets set to "fizz". So you enter the while loop again, only this time item *does* equal "fizz": while item == "fizz": count += 1 return count Oh-oh, trouble ahead. But luckily, you have two bugs, and they *almost* cancel themselves out. The first problem is that the while loop would be an infinite loop, going around and around and around over and over again, since you enter it with item == "fizz" but item always stays equal to "fizz". So the first two lines would keep adding one to count, over and over again, until count is so big your computer runs out of memory (and that might take *months*). Fortunately, the very next line *almost* overcomes that bug. It doesn't *fix* it, but it does reduce the severity. After adding one to count the very first time, Python hits the line "return count", which immediately exits the function, jumping out of the (infinite) while loop and the for- loop. So your function always returns either 0 (if there are no "fizz" in the list at all) or 1 (if there is any "fizz"). So, you have two problems, and they both need to be fixed: 1) The "return count" line must not happen until the for-loop has completed looking at each item in the list. So it must be outside the for- loop, not inside it. Remember that Python decides what is inside the loop by its indentation. 2) You don't want an infinite loop inside the for-loop. There is no need to have two loops at all. The outer for-loop is sufficient. You look at each item *once*, not over and over again, and decide *once* if you should add one to count, then go on to the next item. -- Steven From steve+comp.lang.python at pearwood.info Sun May 5 21:48:50 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 May 2013 01:48:50 GMT Subject: (Learner-here) Lists + Functions = headache References: <51870803$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51870c02$0$29997$c3e8da3$5496439d@news.astraweb.com> On Mon, 06 May 2013 01:31:48 +0000, Steven D'Aprano wrote: > So your function always returns either 0 (if there are no > "fizz" in the list at all) or 1 (if there is any "fizz"). Correction: (thanks to Terry for pointing this out). It will return None or 1, not 0. How easy it is to fall into the trap of assuming the function will do what you intend it to do, instead of what you actually tell it to do :-( -- Steven From dihedral88888 at googlemail.com Mon May 6 01:39:21 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 5 May 2013 22:39:21 -0700 (PDT) Subject: (Learner-here) Lists + Functions = headache In-Reply-To: References: Message-ID: <3a5447e4-4f20-45bb-be22-cf820f2c7737@googlegroups.com> Bradley Wright? 2013?5?6????UTC+8??8?59?15???? > Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced. > > > > I'm writing a function that takes a list(one they supply during runtime) > > here's what my function is supposed to do > > > > 1. for each instance of the string "fizz" make a count > > 2. Finally return that count > > > > here's my code: > > > > def fizz_cout(x): > > count = 0 > > for item in x: > > while item == "fizz": > > count += 1 > > return count > This is not indented right in the scope to return the total count. From rosuav at gmail.com Mon May 6 03:32:23 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 May 2013 17:32:23 +1000 Subject: (Learner-here) Lists + Functions = headache In-Reply-To: <3a5447e4-4f20-45bb-be22-cf820f2c7737@googlegroups.com> References: <3a5447e4-4f20-45bb-be22-cf820f2c7737@googlegroups.com> Message-ID: On Mon, May 6, 2013 at 3:39 PM, 88888 Dihedral wrote: > Bradley Wright? 2013?5?6????UTC+8??8?59?15???? >> def fizz_cout(x): >> >> count = 0 >> >> for item in x: >> >> while item == "fizz": >> >> count += 1 >> > >> return count >> > This is not indented right in the scope to return > the total count. Wow. You know a question's been asked frequently when even the bots can learn to answer it correctly. ChrisA From info at egenix.com Mon May 6 04:00:52 2013 From: info at egenix.com (eGenix Team: M.-A. Lemburg) Date: Mon, 06 May 2013 10:00:52 +0200 Subject: ANN: eGenix mxODBC 3.2.3 - Python ODBC Database Interface Message-ID: <51876334.1060807@egenix.com> ________________________________________________________________________ ANNOUNCING eGenix.com mxODBC Python ODBC Database Interface Version 3.2.3 mxODBC is our commercially supported Python extension providing ODBC database connectivity to Python applications on Windows, Mac OS X, Unix and BSD platforms This announcement is also available on our web-site for online reading: http://www.egenix.com/company/news/eGenix-mxODBC-3.2.3-GA.html ________________________________________________________________________ INTRODUCTION mxODBC provides an easy-to-use, high-performance, reliable and robust Python interface to ODBC compatible databases such as MS SQL Server, MS Access, Oracle Database, IBM DB2 and Informix , Sybase ASE and Sybase Anywhere, MySQL, PostgreSQL, SAP MaxDB and many more: http://www.egenix.com/products/python/mxODBC/ The "eGenix mxODBC - Python ODBC Database Interface" product is a commercial extension to our open-source eGenix mx Base Distribution: http://www.egenix.com/products/python/mxBase/ ________________________________________________________________________ NEWS The 3.2.3 release of our mxODBC is the latest patch level release of our popular Python ODBC Interface. In this release, we've included the following the following enhancements and fixes: Compatibility Enhancements -------------------------- * Added a work-around for a regression in Python 2.7.4 that results in a segfault when exiting Python after loading mxODBC. The regression will be fixed in Python 2.7.5, but we don't want to expose our users to segfaults, so added a work-around. See http://bugs.python.org/issue17703 for the bug ticket. * Please also see the mxODBC 3.2.2 announcement for an important new feature which allows to dramatically increase the fetch performance when working with MS SQL Server and IBM DB2 databases: http://www.egenix.com/company/news/eGenix-mxODBC-3.2.2-GA.html Fixes ----- * Fixed a bugglet which caused connection.__members__ to no longer list connection attributes. For the full set of changes please check the mxODBC change log: http://www.egenix.com/products/python/mxODBC/changelog.html ________________________________________________________________________ FEATURES mxODBC 3.2 was released on 2012-08-28. Please see the full announcement for highlights of the 3.2 release: http://www.egenix.com/company/news/eGenix-mxODBC-3.2.0-GA.html For the full set of features mxODBC has to offer, please see: http://www.egenix.com/products/python/mxODBC/#Features ________________________________________________________________________ EDITIONS mxODBC is available in these three editions: * The low-cost Standard Edition which provides data connectivity to a single database type, e.g. just MS SQL Server. * The Professional Edition, which gives full access to all mxODBC features. * The Product Development Edition, which allows including mxODBC in applications you develop. Compared to mxODBC 3.0, we have simplified our license terms to clarify the situation on multi-core and virtual machines. In most cases, you no longer need to purchase more than one license per processor or virtual machine, scaling down the overall license costs significantly compared to earlier mxODBC releases. For a complete overview of the new editions, please see the product page. http://www.egenix.com/products/python/mxODBC/#mxODBCEditions ________________________________________________________________________ DOWNLOADS The download archives and instructions for installing the package can be found at: http://www.egenix.com/products/python/mxODBC/ In order to use the eGenix mxODBC package you will first need to install the eGenix mx Base package: http://www.egenix.com/products/python/mxBase/ ________________________________________________________________________ UPGRADING Users are encouraged to upgrade to this latest mxODBC release to benefit from the new features and updated ODBC driver support. We have taken special care, not to introduce backwards incompatible changes, making the upgrade experience as smooth as possible. Customers who have purchased mxODBC 3.2 license can continue to use their licenses with this patch level release. Customers who have purchased mxODBC 2.x, 3.0 or 3.1 licenses, can benefit from upgrade discounts. We will give out 20% discount coupons going from mxODBC 2.x to 3.2 and 50% coupons for upgrades from mxODBC 3.x to 3.2. After upgrade, use of the original license from which you upgraded is no longer permitted. Please contact the eGenix.com Sales Team at sales at egenix.com with your existing license serials for details for an upgrade discount coupon. If you want to try the new release before purchace, you can request 30-day evaluation licenses by visiting our web-site http://www.egenix.com/products/python/mxODBC/#Evaluation or by writing to sales at egenix.com, stating your name (or the name of the company) and the number of eval licenses that you need. _______________________________________________________________________ SUPPORT Commercial support for this product is available from eGenix.com. Please see http://www.egenix.com/services/support/ for details about our support offerings. _______________________________________________________________________ INFORMATION About Python (http://www.python.org/): Python is an object-oriented Open Source programming language which runs on all modern platforms. By integrating ease-of-use, clarity in coding, enterprise application connectivity and rapid application design, Python establishes an ideal programming platform for today's IT challenges. About eGenix (http://www.egenix.com/): eGenix is a software project, consulting and product company focusing on expert services and professional quality products for companies, Python users and developers. Enjoy, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, May 06 2013) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ 2013-04-30: Released eGenix PyRun 1.2.0 ... http://egenix.com/go44 ::::: Try our mxODBC.Connect Python Database Interface for free ! :::::: 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/ From amirouche.boubekki at gmail.com Mon May 6 06:16:37 2013 From: amirouche.boubekki at gmail.com (Amirouche Boubekki) Date: Mon, 6 May 2013 12:16:37 +0200 Subject: pyC11 initial publication In-Reply-To: References: Message-ID: H?llo, Does it support macros ? Is it possible to retrieve the list of macros and define macro values ? Thanks, Amirouche 2013/5/4 Volker Birk > pyC11 is a grammar to parse programs in the C programming language > following ISO/IEC 9899:2011. It is written using pyPEG, a parsing > framework for Python. The grammar supports Python 2.7 and 3.x. The test > bench requires py.test. > > This is the initial upload. The grammar is incomplete. Parsing C > expressions works, but parsing complete C programs does not work yet. > > pyC11 is meant to be used for parsing, modifying and generating C > expressions and programs. It is a real world sample how to use pyPEG. > pyC11 is provided under the GNU GPL 2.0. > > You can find pyC11 on Bitbucket at . > > Yours, > VB. > -- > Volker Birk, Oberer Graben 4, CH-8400 Winterthur, Schweiz, > Erde, Solar-System, Orion Arm, Milchstrasse, Lokale Gruppe, > Virgo-Superhaufen, Coma-Virgo-Filament. mailto:dingens at bumens.org > http://fdik.org D-IRCNet fdik!vb at dragon.pibit.ch > -- > http://mail.python.org/mailman/listinfo/python-announce-list > > Support the Python Software Foundation: > http://www.python.org/psf/donations/ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eric.frederich at gmail.com Mon May 6 14:11:46 2013 From: eric.frederich at gmail.com (Eric Frederich) Date: Mon, 6 May 2013 14:11:46 -0400 Subject: distributing a binary package Message-ID: Hello, Hopefully a simple question. Basically, I'd like to know how to create a proper setup.py script to install a package. The package exists as a single directory with a single __init__.py file and (currently) 93 .so files. Right now I just copy it into the site-packages directory but I'd like to start using virtualenv / pip so I'd like to do the installation via "python setup.py install". I need to keep my build process external to this for performance reasons (with a Makefile I can do parallel builds and I have a machine with 12 cores). My Makefile does all the work. It produces a directory that simply needs to be copied to site-packages.... but how do I craft a setup.py script to do the actually installation? Thanks, ~Eric From miki.tebeka at gmail.com Mon May 6 21:13:15 2013 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 6 May 2013 18:13:15 -0700 (PDT) Subject: distributing a binary package In-Reply-To: References: Message-ID: > Basically, I'd like to know how to create a proper setup.py script http://docs.python.org/2/distutils/setupscript.html From eric.frederich at gmail.com Tue May 7 09:48:32 2013 From: eric.frederich at gmail.com (Eric Frederich) Date: Tue, 7 May 2013 09:48:32 -0400 Subject: distributing a binary package In-Reply-To: References: Message-ID: I see where I can specify a module that distutils will try to compile. I already have the .so files compiled. I'm sure its simple, I just can't find it or don't know what to look for. On Mon, May 6, 2013 at 9:13 PM, Miki Tebeka wrote: > >> Basically, I'd like to know how to create a proper setup.py script > http://docs.python.org/2/distutils/setupscript.html > -- > http://mail.python.org/mailman/listinfo/python-list From miki.tebeka at gmail.com Tue May 7 20:58:44 2013 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 7 May 2013 17:58:44 -0700 (PDT) Subject: distributing a binary package In-Reply-To: References: Message-ID: > I already have the .so files compiled. http://docs.python.org/2/distutils/setupscript.html#installing-package-data ? From lm.raaijmakers at gmail.com Mon May 6 14:39:23 2013 From: lm.raaijmakers at gmail.com (Linsey Raaijmakers) Date: Mon, 6 May 2013 11:39:23 -0700 (PDT) Subject: find overlapping lines & output times observed Message-ID: <05aadf46-6cf2-4040-84e6-4e480528f6fd@googlegroups.com> Hello, I have a file like this: action start end 50 5321 5321 7 5323 5347 12 5339 5351 45 5373 5373 45 5420 5420 25 5425 5425 26 5425 5425 50 5451 5451 45 5452 5452 14 5497 5503 26 5513 5513 25 5517 5517 45 5533 5533 50 5533 5533 5 5537 5540 25 5580 5580 45 5586 5586 26 5595 5595 45 5603 5603 50 5634 5634 45 5645 5645 7 5657 5689 25 5682 5682 26 5682 5690 26 5708 5708 45 5717 5717 50 5740 5740 45 5777 5777 45 5804 5804 7 5805 5845 and want to find how many times combinations occur in a time frame (between column 2 and 3 ). This can be multiple combinations, which is my problem now. I have no problems finding overlap between 2 actions. I want to start with the first line, action 50. and check for all lines in the rest of the file if there are lines that overlap this action. So the first line has no overlap, but action 25 and 26 would be a combination that overlaps, and 45 and 50 (5533 -5533). but 7,25,26 would be a combination of 3(5682-5682 & 5682-5690 & 5657-5689 because these three overlap each other. I have a script now that identifies overlap between two actions (see bottom page), but how can I change this so that it outputs all possible combinations? My desired output would be: action times observed apex 50 5 5321, 5451, 5533, 5634, 5740 50,45 1 5533;5533 7 4 5347, 5689, 5688, 5845 7,25 2 5347;5425, 5689;5682 7,25,26 1 5689;5682;5690 CODE: from collections import Counter f = open('and.txt','r'); action_list = [] onset_list = [] apex_list = [] offset_list = [] action_counter = 0 combination_list = [] for line in f: fields = line.split("\t") for col in fields: action = fields[0] onset = fields[1] apex = fields[2] offset = fields[3] action_list.append(action) onset_list.append(onset) apex_list.append(apex) offset_list.append(offset) action_cnvrt = map(int, action_list) c = Counter(action_cnvrt) filtered = list(set(action_list)) filtered_cnvrt = map(int, filtered) for a in filtered_cnvrt: action_count = str(a)+"\t"+str(c[a]) print action_count for i in range (0,len(onset_list)): combination_list.append(action_list[i]) for j in range(0,len(apex_list)): if i != j: if onset_list[j]>= onset_list[i] and apex_list[j] <= apex_list[i]: print action_list[j]+","+action_list[i]+'\t'+onset_list[j]+'\t'+apex_list[j]+'\t'+onset_list[i]+'\t'+apex_list[i] I hope somebody can help me :) From oscar.j.benjamin at gmail.com Mon May 6 16:32:33 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 6 May 2013 21:32:33 +0100 Subject: find overlapping lines & output times observed In-Reply-To: <05aadf46-6cf2-4040-84e6-4e480528f6fd@googlegroups.com> References: <05aadf46-6cf2-4040-84e6-4e480528f6fd@googlegroups.com> Message-ID: On 6 May 2013 19:39, Linsey Raaijmakers wrote: > I have a file like this: > action start end > 50 5321 5321 > 7 5323 5347 > 12 5339 5351 > 45 5373 5373 > 45 5420 5420 > 25 5425 5425 [snip] your code below suggests that your file also has an "apex" column. If that's correct what do you what is it and what do you want to do with it? [snip] > I have a script now that identifies overlap between two actions (see bottom page), but how can I change this so that it outputs all possible combinations? I looked at that code and I don't think it does what you describe. Are you sure that it works? > My desired output would be: > > action times observed apex > 50 5 5321, 5451, 5533, 5634, 5740 > 50,45 1 5533;5533 > 7 4 5347, 5689, 5688, 5845 > 7,25 2 5347;5425, 5689;5682 > 7,25,26 1 5689;5682;5690 > > CODE: > > from collections import Counter > f = open('and.txt','r'); > > action_list = [] > onset_list = [] > apex_list = [] > offset_list = [] > action_counter = 0 > combination_list = [] > > > for line in f: > fields = line.split("\t") > for col in fields: > action = fields[0] > onset = fields[1] > apex = fields[2] > offset = fields[3] The above code suggests that the file has four columns. Also since you're not actually using the loop variable "col" you can just delete the "for" line and unindent the rest. In fact the easiest way to do all of this is just: action, onset, apex, offset = line.split() > > action_list.append(action) > onset_list.append(onset) > apex_list.append(apex) > offset_list.append(offset) > > action_cnvrt = map(int, action_list) > c = Counter(action_cnvrt) > > filtered = list(set(action_list)) There's no need to convert this back to a list if you're just going to iterate over it again with map. > filtered_cnvrt = map(int, filtered) > > for a in filtered_cnvrt: > action_count = str(a)+"\t"+str(c[a]) > print action_count The above counts the number of times each event occurs which is one part of your desired output. > > for i in range (0,len(onset_list)): > combination_list.append(action_list[i]) > for j in range(0,len(apex_list)): > if i != j: > if onset_list[j]>= onset_list[i] and apex_list[j] <= apex_list[i]: > print action_list[j]+","+action_list[i]+'\t'+onset_list[j]+'\t'+apex_list[j]+'\t'+onset_list[i]+'\t'+apex_list[i] What is combination_list for? It should just end up containing the same thing as action_list if I understand correctly. It's generally better in Python to loop directly over things rather than using indices so, instead of something like: for i in range(len(onset_list)): print offset_list[i] - onset_list[i] you should do something like for offset, onset in zip(offset_list, onset_list): print offset - onset and your code will be a lot clearer. The algorithm you are using is to loop over all events and then to loop over all other events comparing all pairs of events. This will not scale very well if you want to look at a large file or to compare simultaneous occurrences of more than two events. It looks as if your input data is ordered by the onset column. Is that the case? If so then you can use an algorithm that just loops once over all the events. The way the algorithm works is that you store which events are currently active and loop through the events keeping track of the start time of the most recently added event adding and removing events as they start and stop. In pseudocode: now = start of first event active = [first event] for next_starting in events (not including first): next_ending = event that ends soonest from active while start of next_starting > end of next_ending: report active events from now to end of next_ending now = end of next_ending remove next_ending from active report active events from now until start of next_starting now = start of next_starting add next_starting to active And some more code to deal with what happens when you get to the end of the list of events... The report steps probably mean adding to a Counter or dict to remember that the currently active events were active during each particular time window. Oscar From miltmobley at gmail.com Mon May 6 14:50:47 2013 From: miltmobley at gmail.com (Milton Mobley) Date: Mon, 6 May 2013 11:50:47 -0700 Subject: module install failures Message-ID: Hi there, I hate to bother you with this, but there doesn't seem to be any ordinary online help for such issues at python.org: Lately I have tried to install several Python modules that are wrappers for C/C++ base libraries on a Windows 7 64 bit system. I used pip in a virtualenv environment, and some script fails complaining that it can't find vcvarsall.bat. I found the file in my Visual Studio 10 Express tree, but the script can't find it. How can I fix the script to look in the right place? -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon May 6 15:49:19 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 06 May 2013 20:49:19 +0100 Subject: module install failures In-Reply-To: References: Message-ID: On 06/05/2013 19:50, Milton Mobley wrote: > Hi there, I hate to bother you with this, but there doesn't seem to be > any ordinary online help for such issues at python.org : > > Lately I have tried to install several Python modules that are wrappers > for C/C++ base libraries on a Windows 7 64 bit system. > I used pip in a virtualenv environment, and some script fails > complaining that it can't find vcvarsall.bat. I found the file in my > Visual Studio 10 Express tree, but the script can't find it. How can I > fix the script to look in the right place? > > Hopefully this helps http://old.nabble.com/Re%3A-Unable-to-install-PyWin32-with-Python-2.6.2-p25202902.html -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From programmer.toronto at gmail.com Mon May 6 15:01:06 2013 From: programmer.toronto at gmail.com (MMZ) Date: Mon, 6 May 2013 12:01:06 -0700 (PDT) Subject: python backup script Message-ID: I am trying to backup database on CentOS linux server,I'm getting error when running the following script. anyone can help? #!/usr/bin/env python import ConfigParser import os import time config = ConfigParser.ConfigParser() config.read("~/my.cnf") username = config.get('client', 'mmz') password = config.get('client', 'pass1') hostname = config.get('client', 'localhost') filestamp = time.strftime('%Y-%m-%d') # Get a list of databases with : Database_list_command="mysql -u %s -p%s -h %s --silent -N -e 'show databases'" % (username, password, hostname) for database in os.popen(database_list_command).readlines(): database = database.strip() if database == 'information_schema': continue if database == 'db_dev': continue filename = "/backups/mysql/%s-%s.sql" % (database, filestamp) os.popen("mysqldump -u %s -p%s -h %s -e --opt -c %s | gzip -c > %s.gz" % (username, password, hostname, database, filename)) ............Error.......... Traceback (most recent call last): File "./backup.py", line 8, in ? username = config.get('client', 'mmz') File "/usr/lib/python2.4/ConfigParser.py", line 511, in get raise NoSectionError(section) From malaclypse2 at gmail.com Mon May 6 15:11:33 2013 From: malaclypse2 at gmail.com (Jerry Hill) Date: Mon, 6 May 2013 15:11:33 -0400 Subject: python backup script In-Reply-To: References: Message-ID: On Mon, May 6, 2013 at 3:01 PM, MMZ wrote: > I am trying to backup database on CentOS linux server,I'm getting error > when running the following script. anyone can help? > > Traceback (most recent call last): > File "./backup.py", line 8, in ? > username = config.get('client', 'mmz') > File "/usr/lib/python2.4/ConfigParser.py", line 511, in get > raise NoSectionError(section) > ?I've never used ConfigParser, but that ? ?error message looks pretty simple to interpret. You've set up a ConfigParser object, told it to read in ~/my.cnf, the asked for the value of section 'client', option 'mmz'. The error indicates that your config files doesn't have a section named 'client'. What is the content of your ~/my.cnf file? -- Jerry -------------- next part -------------- An HTML attachment was scrubbed... URL: From programmer.toronto at gmail.com Mon May 6 15:20:51 2013 From: programmer.toronto at gmail.com (MMZ) Date: Mon, 6 May 2013 12:20:51 -0700 (PDT) Subject: python backup script In-Reply-To: References: Message-ID: <51f5c8c4-b40b-4a1e-b72b-905caa62aaf6@googlegroups.com> On Monday, May 6, 2013 3:11:33 PM UTC-4, Jerry Hill wrote: > On Mon, May 6, 2013 at 3:01 PM, MMZ wrote: > > > > I am trying to backup database on CentOS linux server,I'm getting error when running the following script. anyone can help? > > > > > Traceback (most recent call last): > > ? File "./backup.py", line 8, in ? > > ? ? username = config.get('client', 'mmz') > > ? File "/usr/lib/python2.4/ConfigParser.py", line 511, in get > > ? ? raise NoSectionError(section) > > > > I've never used ConfigParser, but that ? > > error message looks pretty simple to interpret.? You've set up a ConfigParser object, told it to read in ~/my.cnf, the asked for the value of section 'client', option 'mmz'.? The error indicates that your config files doesn't have a section named 'client'. > > > > What is the content of your ~/my.cnf file? > > -- > > > Jerry Thank you for helping Jerry. Actually I found this script for debian but I want to use it for CentOS server so I replaced /etc/mysql/debian.cnf with ~/my.cnf the file content is: Example MySQL config file for medium systems. # # This is for a system with little memory (32M - 64M) where MySQL plays # an important part, or systems up to 128M where MySQL is used together with # other programs (such as a web server) # # MySQL programs look for option files in a set of # locations which depend on the deployment platform. # You can copy this option file to one of those # locations. For information about these locations, see: # http://dev.mysql.com/doc/mysql/en/option-files.html # # In this file, you can use all long options that a program supports. # If you want to know which options a program supports, run the program # with the "--help" option. # The following options will be passed to all MySQL clients [client] #password = your_password port = 3306 socket = /tmp/mysql.sock # Here follows entries for some specific programs # The MySQL server [mysqld] port = 3306 socket = /tmp/mysql.sock skip-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M # Don't listen on a TCP/IP port at all. This can be a security enhancement, # if all processes that need to connect to mysqld run on the same host. # All interaction with mysqld must be made via Unix sockets or named pipes. # Note that using this option without enabling named pipes on Windows # (via the "enable-named-pipe" option) will render mysqld useless! # #skip-networking # Replication Master Server (default) # binary logging is required for replication log-bin=mysql-bin # binary logging format - mixed recommended binlog_format=mixed # required unique id between 1 and 2^32 - 1 # defaults to 1 if master-host is not set # but will not function as a master if omitted server-id = 1 From matt.walker.jones at gmail.com Mon May 6 15:46:04 2013 From: matt.walker.jones at gmail.com (Matt Jones) Date: Mon, 6 May 2013 14:46:04 -0500 Subject: python backup script In-Reply-To: <51f5c8c4-b40b-4a1e-b72b-905caa62aaf6@googlegroups.com> References: <51f5c8c4-b40b-4a1e-b72b-905caa62aaf6@googlegroups.com> Message-ID: I've never used ConfigParser either, but shouldn't the "[client]" section have the options "mmz", "pass1", or "localhost" somewhere? Do you need to add them to that file? *Matt Jones* On Mon, May 6, 2013 at 2:20 PM, MMZ wrote: > On Monday, May 6, 2013 3:11:33 PM UTC-4, Jerry Hill wrote: > > On Mon, May 6, 2013 at 3:01 PM, MMZ wrote: > > > > > > > > I am trying to backup database on CentOS linux server,I'm getting error > when running the following script. anyone can help? > > > > > > > > > > Traceback (most recent call last): > > > > File "./backup.py", line 8, in ? > > > > username = config.get('client', 'mmz') > > > > File "/usr/lib/python2.4/ConfigParser.py", line 511, in get > > > > raise NoSectionError(section) > > > > > > > > I've never used ConfigParser, but that > > > > error message looks pretty simple to interpret. You've set up a > ConfigParser object, told it to read in ~/my.cnf, the asked for the value > of section 'client', option 'mmz'. The error indicates that your config > files doesn't have a section named 'client'. > > > > > > > > What is the content of your ~/my.cnf file? > > > > -- > > > > > > Jerry > > Thank you for helping Jerry. Actually I found this script for debian but I > want to use it for CentOS server so I replaced /etc/mysql/debian.cnf with > ~/my.cnf > the file content is: > > Example MySQL config file for medium systems. > # > # This is for a system with little memory (32M - 64M) where MySQL plays > # an important part, or systems up to 128M where MySQL is used together > with > # other programs (such as a web server) > # > # MySQL programs look for option files in a set of > # locations which depend on the deployment platform. > # You can copy this option file to one of those > # locations. For information about these locations, see: > # http://dev.mysql.com/doc/mysql/en/option-files.html > # > # In this file, you can use all long options that a program supports. > # If you want to know which options a program supports, run the program > # with the "--help" option. > > # The following options will be passed to all MySQL clients > [client] > #password = your_password > port = 3306 > socket = /tmp/mysql.sock > > # Here follows entries for some specific programs > > # The MySQL server > [mysqld] > port = 3306 > socket = /tmp/mysql.sock > skip-locking > key_buffer_size = 16M > max_allowed_packet = 1M > table_open_cache = 64 > sort_buffer_size = 512K > net_buffer_length = 8K > read_buffer_size = 256K > read_rnd_buffer_size = 512K > myisam_sort_buffer_size = 8M > > # Don't listen on a TCP/IP port at all. This can be a security enhancement, > # if all processes that need to connect to mysqld run on the same host. > # All interaction with mysqld must be made via Unix sockets or named pipes. > # Note that using this option without enabling named pipes on Windows > # (via the "enable-named-pipe" option) will render mysqld useless! > # > #skip-networking > > # Replication Master Server (default) > # binary logging is required for replication > log-bin=mysql-bin > > # binary logging format - mixed recommended > binlog_format=mixed > > # required unique id between 1 and 2^32 - 1 > # defaults to 1 if master-host is not set > # but will not function as a master if omitted > server-id = 1 > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From programmer.toronto at gmail.com Mon May 6 16:37:34 2013 From: programmer.toronto at gmail.com (MMZ) Date: Mon, 6 May 2013 13:37:34 -0700 (PDT) Subject: python backup script In-Reply-To: References: <51f5c8c4-b40b-4a1e-b72b-905caa62aaf6@googlegroups.com> Message-ID: <489bad9e-4a96-4d51-98ea-ce59c2573e4f@googlegroups.com> Thanks Matt. my.cnf is a readonly file and cannot be changed or modified but do you know of a file that stores similar information on CentOS?I think I'm not reading from a right file maybe. On Monday, May 6, 2013 3:46:04 PM UTC-4, Matt Jones wrote: > I've never used ConfigParser either, but shouldn't the "[client]" section have the options "mmz", "pass1", or "localhost" somewhere? ?Do you need to add them to that file? > > > > > Matt Jones > > > > On Mon, May 6, 2013 at 2:20 PM, MMZ wrote: > > > On Monday, May 6, 2013 3:11:33 PM UTC-4, Jerry Hill wrote: > > > > On Mon, May 6, 2013 at 3:01 PM, MMZ wrote: > > > > > > > > > > > > I am trying to backup database on CentOS linux server,I'm getting error when running the following script. anyone can help? > > > > > > > > > > > > > > > > Traceback (most recent call last): > > > > > > ? File "./backup.py", line 8, in ? > > > > > > ? ? username = config.get('client', 'mmz') > > > > > > ? File "/usr/lib/python2.4/ConfigParser.py", line 511, in get > > > > > > ? ? raise NoSectionError(section) > > > > > > > > > > > > > I've never used ConfigParser, but that ? > > > > > > error message looks pretty simple to interpret.? You've set up a ConfigParser object, told it to read in ~/my.cnf, the asked for the value of section 'client', option 'mmz'.? The error indicates that your config files doesn't have a section named 'client'. > > > > > > > > > > > > > > What is the content of your ~/my.cnf file? > > > > > > -- > > > > > > > > > Jerry > > > > Thank you for helping Jerry. Actually I found this script for debian but I want to use it for CentOS server so I replaced /etc/mysql/debian.cnf with ~/my.cnf > > the file content is: > > > > ?Example MySQL config file for medium systems. > > # > > # This is for a system with little memory (32M - 64M) where MySQL plays > > # an important part, or systems up to 128M where MySQL is used together with > > # other programs (such as a web server) > > # > > # MySQL programs look for option files in a set of > > # locations which depend on the deployment platform. > > # You can copy this option file to one of those > > # locations. For information about these locations, see: > > # http://dev.mysql.com/doc/mysql/en/option-files.html > > # > > # In this file, you can use all long options that a program supports. > > # If you want to know which options a program supports, run the program > > # with the "--help" option. > > > > # The following options will be passed to all MySQL clients > > [client] > > #password ? ? ? = your_password > > port ? ? ? ? ? ?= 3306 > > socket ? ? ? ? ?= /tmp/mysql.sock > > > > # Here follows entries for some specific programs > > > > # The MySQL server > > [mysqld] > > port ? ? ? ? ? ?= 3306 > > socket ? ? ? ? ?= /tmp/mysql.sock > > skip-locking > > key_buffer_size = 16M > > max_allowed_packet = 1M > > table_open_cache = 64 > > sort_buffer_size = 512K > > net_buffer_length = 8K > > read_buffer_size = 256K > > read_rnd_buffer_size = 512K > > myisam_sort_buffer_size = 8M > > > > # Don't listen on a TCP/IP port at all. This can be a security enhancement, > > # if all processes that need to connect to mysqld run on the same host. > > # All interaction with mysqld must be made via Unix sockets or named pipes. > > # Note that using this option without enabling named pipes on Windows > > # (via the "enable-named-pipe" option) will render mysqld useless! > > # > > #skip-networking > > > > # Replication Master Server (default) > > # binary logging is required for replication > > log-bin=mysql-bin > > > > # binary logging format - mixed recommended > > binlog_format=mixed > > > > # required unique id between 1 and 2^32 - 1 > > # defaults to 1 if master-host is not set > > # but will not function as a master if omitted > > server-id ? ? ? = 1 > > -- > > http://mail.python.org/mailman/listinfo/python-list From matt.walker.jones at gmail.com Mon May 6 17:08:20 2013 From: matt.walker.jones at gmail.com (Matt Jones) Date: Mon, 6 May 2013 16:08:20 -0500 Subject: python backup script In-Reply-To: <489bad9e-4a96-4d51-98ea-ce59c2573e4f@googlegroups.com> References: <51f5c8c4-b40b-4a1e-b72b-905caa62aaf6@googlegroups.com> <489bad9e-4a96-4d51-98ea-ce59c2573e4f@googlegroups.com> Message-ID: Why do you have to use that file? Why can't you copy its contents into a new file in your working directory and make whatever changes necessary? Example of the changes your code makes me think you want: ************************ # The following options will be passed to all MySQL clients [client] #password = your_password port = 3306 socket = /tmp/mysql.sock mmz = bleh pass1 = blah localhost = bluargh *Matt Jones* On Mon, May 6, 2013 at 3:37 PM, MMZ wrote: > Thanks Matt. > my.cnf is a readonly file and cannot be changed or modified but do you > know of a file that stores similar information on CentOS?I think I'm not > reading from a right file maybe. > > On Monday, May 6, 2013 3:46:04 PM UTC-4, Matt Jones wrote: > > I've never used ConfigParser either, but shouldn't the "[client]" > section have the options "mmz", "pass1", or "localhost" somewhere? Do you > need to add them to that file? > > > > > > > > > > Matt Jones > > > > > > > > On Mon, May 6, 2013 at 2:20 PM, MMZ wrote: > > > > > > On Monday, May 6, 2013 3:11:33 PM UTC-4, Jerry Hill wrote: > > > > > > > On Mon, May 6, 2013 at 3:01 PM, MMZ wrote: > > > > > > > > > > > > > > > > > > > > I am trying to backup database on CentOS linux server,I'm getting > error when running the following script. anyone can help? > > > > > > > > > > > > > > > > > > > > > > > > > > > Traceback (most recent call last): > > > > > > > > > > File "./backup.py", line 8, in ? > > > > > > > > > > username = config.get('client', 'mmz') > > > > > > > > > > File "/usr/lib/python2.4/ConfigParser.py", line 511, in get > > > > > > > > > > raise NoSectionError(section) > > > > > > > > > > > > > > > > > > > > > > I've never used ConfigParser, but that > > > > > > > > > > error message looks pretty simple to interpret. You've set up a > ConfigParser object, told it to read in ~/my.cnf, the asked for the value > of section 'client', option 'mmz'. The error indicates that your config > files doesn't have a section named 'client'. > > > > > > > > > > > > > > > > > > > > > > > > What is the content of your ~/my.cnf file? > > > > > > > > > > -- > > > > > > > > > > > > > > > Jerry > > > > > > > > Thank you for helping Jerry. Actually I found this script for debian but > I want to use it for CentOS server so I replaced /etc/mysql/debian.cnf with > ~/my.cnf > > > > the file content is: > > > > > > > > Example MySQL config file for medium systems. > > > > # > > > > # This is for a system with little memory (32M - 64M) where MySQL plays > > > > # an important part, or systems up to 128M where MySQL is used together > with > > > > # other programs (such as a web server) > > > > # > > > > # MySQL programs look for option files in a set of > > > > # locations which depend on the deployment platform. > > > > # You can copy this option file to one of those > > > > # locations. For information about these locations, see: > > > > # http://dev.mysql.com/doc/mysql/en/option-files.html > > > > # > > > > # In this file, you can use all long options that a program supports. > > > > # If you want to know which options a program supports, run the program > > > > # with the "--help" option. > > > > > > > > # The following options will be passed to all MySQL clients > > > > [client] > > > > #password = your_password > > > > port = 3306 > > > > socket = /tmp/mysql.sock > > > > > > > > # Here follows entries for some specific programs > > > > > > > > # The MySQL server > > > > [mysqld] > > > > port = 3306 > > > > socket = /tmp/mysql.sock > > > > skip-locking > > > > key_buffer_size = 16M > > > > max_allowed_packet = 1M > > > > table_open_cache = 64 > > > > sort_buffer_size = 512K > > > > net_buffer_length = 8K > > > > read_buffer_size = 256K > > > > read_rnd_buffer_size = 512K > > > > myisam_sort_buffer_size = 8M > > > > > > > > # Don't listen on a TCP/IP port at all. This can be a security > enhancement, > > > > # if all processes that need to connect to mysqld run on the same host. > > > > # All interaction with mysqld must be made via Unix sockets or named > pipes. > > > > # Note that using this option without enabling named pipes on Windows > > > > # (via the "enable-named-pipe" option) will render mysqld useless! > > > > # > > > > #skip-networking > > > > > > > > # Replication Master Server (default) > > > > # binary logging is required for replication > > > > log-bin=mysql-bin > > > > > > > > # binary logging format - mixed recommended > > > > binlog_format=mixed > > > > > > > > # required unique id between 1 and 2^32 - 1 > > > > # defaults to 1 if master-host is not set > > > > # but will not function as a master if omitted > > > > server-id = 1 > > > > -- > > > > http://mail.python.org/mailman/listinfo/python-list > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From henryx_b at yahoo.it Mon May 6 17:44:21 2013 From: henryx_b at yahoo.it (Enrico 'Henryx' Bianchi) Date: Mon, 06 May 2013 23:44:21 +0200 Subject: python backup script References: Message-ID: MMZ wrote: > config.read("~/my.cnf") > username = config.get('client', 'mmz') > password = config.get('client', 'pass1') > hostname = config.get('client', 'localhost') ### A simple config file ### [client] user = mmz password = pass1 host = localhost ### EOF ### #!/usr/bin/env python import ConfigParser config = ConfigParser.ConfigParser() config.read("~/configfile.cfg") username = config.get('client', 'user') password = config.get('client', 'password') hostname = config.get('client', 'host') [...] > # Get a list of databases with : > Database_list_command="mysql -u %s -p%s -h %s --silent -N -e 'show databases'" % (username, password, hostname) > for database in os.popen(database_list_command).readlines(): > database = database.strip() > if database == 'information_schema': > continue > if database == 'db_dev': > continue > filename = "/backups/mysql/%s-%s.sql" % (database, filestamp) > os.popen("mysqldump -u %s -p%s -h %s -e --opt -c %s | gzip -c > %s.gz" % (username, password, hostname, database, filename)) command = subprocess.Popen(['mysql', '-u ' + username, '-p' + password, '-h ' + hostname, '--silent', '-N', "-e 'show databases'"], shell=False, stdout=subprocess.PIPE) status = command.wait() for line in command.stdout.readlines(): if line.strip() not in ['information_schema', 'db_dev']: filename = "/backups/mysql/%s-%s.sql" % (database, filestamp) cmd1 = subprocess.Popen(['mysqldump', '-u ' + username, '-p' + password, '-h ' + hostname, '-e', '--opt', '-c ' + database], shell=False, stdout=subprocess.PIPE) cmd2 = subprocess.Popen(['gzip' '-c'], shell=False, stdout=filename) Enrico From henryx_b at yahoo.it Mon May 6 17:48:44 2013 From: henryx_b at yahoo.it (Enrico 'Henryx' Bianchi) Date: Mon, 06 May 2013 23:48:44 +0200 Subject: python backup script References: Message-ID: Enrico 'Henryx' Bianchi wrote: > cmd2 = subprocess.Popen(['gzip' '-c'], > shell=False, > stdout=filename) Doh, my fault: cmd2 = subprocess.Popen(['gzip' '-c'], shell=False, stdout=filename stdin=cmd1.stdout) Enrico From mina at socialassets.org Mon May 6 18:12:59 2013 From: mina at socialassets.org (mina at socialassets.org) Date: Mon, 6 May 2013 15:12:59 -0700 (PDT) Subject: python backup script In-Reply-To: References: Message-ID: <36d2b7cf-2537-46a6-b984-9fce7ddd33ff@googlegroups.com> On Monday, May 6, 2013 5:48:44 PM UTC-4, Enrico 'Henryx' Bianchi wrote: > Enrico 'Henryx' Bianchi wrote: > > > > > cmd2 = subprocess.Popen(['gzip' '-c'], > > > shell=False, > > > stdout=filename) > > > > Doh, my fault: > > > > cmd2 = subprocess.Popen(['gzip' '-c'], > > shell=False, > > stdout=filename > > stdin=cmd1.stdout) > > > > Enrico Thank you Enrico. I've just tried your script and got this error: stdin=cmd1.stdout) ^ SyntaxError: invalid syntax any idea? From gordon at panix.com Mon May 6 18:15:44 2013 From: gordon at panix.com (John Gordon) Date: Mon, 6 May 2013 22:15:44 +0000 (UTC) Subject: python backup script References: <36d2b7cf-2537-46a6-b984-9fce7ddd33ff@googlegroups.com> Message-ID: In <36d2b7cf-2537-46a6-b984-9fce7ddd33ff at googlegroups.com> mina at socialassets.org writes: > > cmd2 = subprocess.Popen(['gzip' '-c'], > > > > shell=False, > > > > stdout=filename > > > > stdin=cmd1.stdout) > Thank you Enrico. I've just tried your script and got this error: > stdin=cmd1.stdout) > ^ > SyntaxError: invalid syntax Looks like you need a comma after 'stdout=filename'. -- 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 henryx_b at yahoo.it Tue May 7 15:11:22 2013 From: henryx_b at yahoo.it (Enrico 'Henryx' Bianchi) Date: Tue, 07 May 2013 21:11:22 +0200 Subject: python backup script References: <36d2b7cf-2537-46a6-b984-9fce7ddd33ff@googlegroups.com> Message-ID: John Gordon wrote: > Looks like you need a comma after 'stdout=filename'. Sigh, yesterday was a terrible day (yes, it lacks a comma)... Anyway, when it is possible, is recommended to use the drivers for communicate with databases, because subprocess (or os.*open*) is more expensive compared to (python needs to spawn an external process to execute the command) Enrico From python at mrabarnett.plus.com Mon May 6 18:28:07 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 May 2013 23:28:07 +0100 Subject: python backup script In-Reply-To: <36d2b7cf-2537-46a6-b984-9fce7ddd33ff@googlegroups.com> References: <36d2b7cf-2537-46a6-b984-9fce7ddd33ff@googlegroups.com> Message-ID: <51882E77.6010809@mrabarnett.plus.com> On 06/05/2013 23:12, mina at socialassets.org wrote: > On Monday, May 6, 2013 5:48:44 PM UTC-4, Enrico 'Henryx' Bianchi wrote: >> Enrico 'Henryx' Bianchi wrote: >> >> > cmd2 = subprocess.Popen(['gzip' '-c'], >> > shell=False, >> > stdout=filename) >> >> Doh, my fault: >> >> cmd2 = subprocess.Popen(['gzip' '-c'], >> shell=False, >> stdout=filename >> stdin=cmd1.stdout) >> >> Enrico > > Thank you Enrico. I've just tried your script and got this error: > stdin=cmd1.stdout) > ^ > SyntaxError: invalid syntax > > any idea? > Missing comma on the previous line. From programmer.toronto at gmail.com Mon May 6 18:15:16 2013 From: programmer.toronto at gmail.com (MMZ) Date: Mon, 6 May 2013 15:15:16 -0700 (PDT) Subject: python backup script In-Reply-To: References: Message-ID: <2c1e5a9b-cd5a-4fbf-9a09-bf2e566b83f3@googlegroups.com> stdin=cmd1.stdout) ^ SyntaxError: invalid syntax On Monday, May 6, 2013 5:48:44 PM UTC-4, Enrico 'Henryx' Bianchi wrote: > Enrico 'Henryx' Bianchi wrote: > > > > > cmd2 = subprocess.Popen(['gzip' '-c'], > > > shell=False, > > > stdout=filename) > > > > Doh, my fault: > > > > cmd2 = subprocess.Popen(['gzip' '-c'], > > shell=False, > > stdout=filename > > stdin=cmd1.stdout) > > > > Enrico From rosuav at gmail.com Mon May 6 18:12:28 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 08:12:28 +1000 Subject: python backup script In-Reply-To: References: Message-ID: On Tue, May 7, 2013 at 5:01 AM, MMZ wrote: > username = config.get('client', 'mmz') > password = config.get('client', 'pass1') > hostname = config.get('client', 'localhost') Are 'mmz', 'pass1', and 'localhost' the actual values you want for username, password, and hostname? If so, don't pass them through config.get() at all - just use them directly. In fact, I'd be inclined to just stuff them straight into the Database_list_command literal; that way, it's clear how they're used, and the fact that you aren't escaping them in any way isn't going to be a problem (tip: an apostrophe in your password would currently break your script). It's also worth noting that the ~/ notation is a shell feature. You may or may not be able to use it in config.read(). ChrisA From __peter__ at web.de Tue May 7 02:18:11 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 May 2013 08:18:11 +0200 Subject: python backup script References: Message-ID: Chris Angelico wrote: > It's also worth noting that the ~/ notation is a shell feature. You > may or may not be able to use it in config.read(). The latter. Combined with """ read(self, filenames) method of ConfigParser.ConfigParser instance Read and parse a filename or a list of filenames. Files that cannot be opened are silently ignored; this is ... """ you'll get no configuration at all. You have to apply os.path.expanduser() to get a valid path. $ cd $ touch tmp.config $ cd / $ python Python 2.7.2+ (default, Jul 20 2012, 22:15:08) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from ConfigParser import ConfigParser >>> p = ConfigParser() >>> p.read("~/tmp.config") [] >>> import os >>> p.read(os.path.expanduser("~/tmp.config")) ['/home/peter/tmp.config'] From programmer.toronto at gmail.com Mon May 6 18:40:40 2013 From: programmer.toronto at gmail.com (MMZ) Date: Mon, 6 May 2013 15:40:40 -0700 (PDT) Subject: python backup script In-Reply-To: References: Message-ID: On Monday, May 6, 2013 6:12:28 PM UTC-4, Chris Angelico wrote: > On Tue, May 7, 2013 at 5:01 AM, MMZ wrote: > > > username = config.get('client', 'mmz') > > > password = config.get('client', 'pass1') > > > hostname = config.get('client', 'localhost') > > > > Are 'mmz', 'pass1', and 'localhost' the actual values you want for > > username, password, and hostname? If so, don't pass them through > > config.get() at all - just use them directly. In fact, I'd be inclined > > to just stuff them straight into the Database_list_command literal; > > that way, it's clear how they're used, and the fact that you aren't > > escaping them in any way isn't going to be a problem (tip: an > > apostrophe in your password would currently break your script). > > > > It's also worth noting that the ~/ notation is a shell feature. You > > may or may not be able to use it in config.read(). > > > > ChrisA Thanks Chris. you are right. So I used them directly and removed configParser. The new error is: Traceback (most recent call last): File "./bbk.py", line 11, in ? for database in os.popen(database_list_command).readlines(): NameError: name 'database_list_command' is not defined any idea? From rosuav at gmail.com Mon May 6 18:51:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 08:51:56 +1000 Subject: python backup script In-Reply-To: References: Message-ID: On Tue, May 7, 2013 at 8:40 AM, MMZ wrote: > Thanks Chris. you are right. > So I used them directly and removed configParser. The new error is: > > Traceback (most recent call last): > File "./bbk.py", line 11, in ? > for database in os.popen(database_list_command).readlines(): > NameError: name 'database_list_command' is not defined Python names are case-sensitive. If you create it with a capital D, you can't reference it with a lower-case d. For consistency with the rest of your code, I'd advise lowercasing it. ChrisA From python at mrabarnett.plus.com Mon May 6 18:52:53 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 May 2013 23:52:53 +0100 Subject: python backup script In-Reply-To: References: Message-ID: <51883445.8080506@mrabarnett.plus.com> On 06/05/2013 23:40, MMZ wrote: > On Monday, May 6, 2013 6:12:28 PM UTC-4, Chris Angelico wrote: >> On Tue, May 7, 2013 at 5:01 AM, MMZ wrote: >> >> > username = config.get('client', 'mmz') >> >> > password = config.get('client', 'pass1') >> >> > hostname = config.get('client', 'localhost') >> >> >> >> Are 'mmz', 'pass1', and 'localhost' the actual values you want for >> >> username, password, and hostname? If so, don't pass them through >> >> config.get() at all - just use them directly. In fact, I'd be inclined >> >> to just stuff them straight into the Database_list_command literal; >> >> that way, it's clear how they're used, and the fact that you aren't >> >> escaping them in any way isn't going to be a problem (tip: an >> >> apostrophe in your password would currently break your script). >> >> >> >> It's also worth noting that the ~/ notation is a shell feature. You >> >> may or may not be able to use it in config.read(). >> >> >> >> ChrisA > > Thanks Chris. you are right. > So I used them directly and removed configParser. The new error is: > > Traceback (most recent call last): > File "./bbk.py", line 11, in ? > for database in os.popen(database_list_command).readlines(): > NameError: name 'database_list_command' is not defined > > any idea? > Check the spelling (remember that the name is case-sensitive). From svenito at gmail.com Mon May 6 16:57:01 2013 From: svenito at gmail.com (Sven) Date: Mon, 6 May 2013 21:57:01 +0100 Subject: PIL: check if image is animated Message-ID: Hello, I am trying to check if an image is animated. I can't rely on the extension as it may be a gif that's been renamed to .jpg or something else and is still animated. I thought that this used to work: from PIL import Image def check_animated(img): try: img.seek(1) except (EOFError): return 0 return 1 img = Image('image.jpg') print "animated?", check_animated(img) Regardless if it's animated or not I get this exception: ValueError: cannot seek to frame 1 I need to return 0 or 1, so excuse not using True or False. Did the above get deprecated/change in a version at some point? Perhaps there's something I missed during install (using PIP). Are there any other ways to accomplish what I am trying to do, with or without PIL? Python 2.7, linux -- ./Sven -------------- next part -------------- An HTML attachment was scrubbed... URL: From svenito at gmail.com Thu May 9 04:22:49 2013 From: svenito at gmail.com (Sven) Date: Thu, 9 May 2013 09:22:49 +0100 Subject: PIL: check if image is animated In-Reply-To: References: Message-ID: Figured out my issue. I did called the check_animated function more than once and the second call causes the exception unless I seek back to 0 On 6 May 2013 21:57, Sven wrote: > Hello, > > I am trying to check if an image is animated. I can't rely on the > extension as it may be a gif that's been renamed to .jpg or something else > and is still animated. > > I thought that this used to work: > > from PIL import Image > > > def check_animated(img): > try: > img.seek(1) > except (EOFError): > return 0 > return 1 > > img = Image('image.jpg') > print "animated?", check_animated(img) > > Regardless if it's animated or not I get this exception: > ValueError: cannot seek to frame 1 > > I need to return 0 or 1, so excuse not using True or False. > > Did the above get deprecated/change in a version at some point? Perhaps > there's something I missed during install (using PIP). Are there any other > ways to accomplish what I am trying to do, with or without PIL? > > Python 2.7, linux > > -- > ./Sven > -- ./Sven -------------- next part -------------- An HTML attachment was scrubbed... URL: From livewebcamsexs at gmail.com Tue May 7 01:04:07 2013 From: livewebcamsexs at gmail.com (MoneyMaker) Date: Mon, 6 May 2013 22:04:07 -0700 (PDT) Subject: Horror!!!! Message-ID: HORROR !!!!!!!!! This site is intended for all horror fans. The site have been collected and collected links to horror fans and joining a membership you can add the best moments of terror. Member can share everything horror-related. for example, videos, photos, music, stories. We hope you enjoy here with us, and help build the exciting site http://horrorhorrorhorror.webs.com/ From info at egenix.com Tue May 7 04:01:05 2013 From: info at egenix.com (eGenix Team: M.-A. Lemburg) Date: Tue, 07 May 2013 10:01:05 +0200 Subject: ANN: eGenix mxODBC Zope/Plone Database Adapter 2.1.2 Message-ID: <5188B4C1.9010901@egenix.com> ________________________________________________________________________ ANNOUNCEMENT mxODBC Zope/Plone Database Adapter Version 2.1.2 for Zope and the Plone CMS Available for Plone 4.0, 4.1 and 4.2, Zope 2.12 and 2.13, on Windows, Linux, Mac OS X, FreeBSD and other platforms This announcement is also available on our web-site for online reading: http://www.egenix.com/company/news/eGenix-mxODBC-Zope-DA-2.1.2-GA.html ________________________________________________________________________ INTRODUCTION The eGenix mxODBC Zope DA allows you to easily connect your Zope or Plone CMS installation to just about any database backend on the market today, giving you the reliability of the commercially supported eGenix product mxODBC and the flexibility of the ODBC standard as middle-tier architecture. The mxODBC Zope Database Adapter is highly portable, just like Zope itself and provides a high performance interface to all your ODBC data sources, using a single well-supported interface on Windows, Linux, Mac OS X, FreeBSD and other platforms. This makes it ideal for deployment in ZEO Clusters and Zope hosting environments where stability and high performance are a top priority, establishing an excellent basis and scalable solution for your Plone CMS. Product page: http://www.egenix.com/products/zope/mxODBCZopeDA/ ________________________________________________________________________ NEWS We are pleased to announce the new version 2.1.2 of our mxODBC Zope/Plone Database Adapter product. Compatibility Enhancements -------------------------- * Added a work-around for a regression in Python 2.7.4 that results in a segfault when exiting Zope/Plone after loading the mxODBC Zope DA. The regression will be fixed in Python 2.7.5, but we don't want to expose our users to segfaults, so added a work-around. See http://bugs.python.org/issue17703 for the bug ticket. * Upgraded the underlying mxODBC library to version 3.2.3. Please see the mxODBC 3.2.3 release announcement for additional details: http://www.egenix.com/company/news/eGenix-mxODBC-3.2.3-GA.html Driver Compatibility -------------------- * Please also see the mxODBC Zope DA 2.1.1 announcement for an important new feature which allows to dramatically increase the fetch performance when working with MS SQL Server and IBM DB2 databases. http://www.egenix.com/company/news/eGenix-mxODBC-Zope-DA-2.1.1-GA.html For the full set of changes please check the change log: http://www.egenix.com/products/zope/mxODBCZopeDA/changelog.html ________________________________________________________________________ FEATURES Version 2.1.0 of our mxODBC Zope/Plone Database Adapter product was released on 2012-09-18. Please see the announcement for highlights of the 2.1 release: http://www.egenix.com/company/news/eGenix-mxODBC-Zope-DA-2.1.0-GA.html For the full set of features mxODBC Zope DA has to offer, please see: http://www.egenix.com/products/zope/mxODBCZopeDA/#Features ________________________________________________________________________ UPGRADING Users are encouraged to upgrade to this latest mxODBC Zope/Plone DA release to benefit from the new features and updated ODBC driver support. We have taken special care not to introduce backwards incompatible changes, making the upgrade experience as smooth as possible. As always, patch level upgrades (e.g. from 2.1.0 to 2.1.2) are free of charge. The licenses you have purchased for 2.1 will continue to work with this new release. For major and minor upgrade purchases, we will give out 20% discount coupons going from mxODBC Zope DA 1.x to 2.1 and 50% coupons for upgrades from mxODBC 2.x to 2.1. After upgrade, use of the original license from which you upgraded is no longer permitted. Please contact the eGenix.com Sales Team with your existing license serials for details for an upgrade discount coupon. If you want to try the new release before purchase, you can request 30-day evaluation licenses by visiting our web-site or writing to sales at egenix.com, stating your name (or the name of the company) and the number of evaluation licenses that you need. _______________________________________________________________________ SUPPORT Commercial support for this product is available from eGenix.com. Please see http://www.egenix.com/services/support/ for details about our support offerings. ________________________________________________________________________ MORE INFORMATION For more information on the mxODBC Zope Database Adapter, licensing and download instructions, please visit our web-site: http://www.egenix.com/products/zope/mxODBCZopeDA/ You can buy mxODBC Zope DA licenses online from the eGenix.com shop at: http://shop.egenix.com/ About Python (http://www.python.org/): Python is an object-oriented Open Source programming language which runs on all modern platforms. By integrating ease-of-use, clarity in coding, enterprise application connectivity and rapid application design, Python establishes an ideal programming platform for today's IT challenges. About eGenix (http://www.egenix.com/): eGenix is a software project, consulting and product company focusing on expert project services and professional quality products for companies, Python users and developers. Enjoy, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, May 07 2013) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ 2013-05-06: Released mxODBC 3.2.3 ... http://egenix.com/go45 2013-04-30: Released eGenix PyRun 1.2.0 ... http://egenix.com/go44 ::::: Try our mxODBC.Connect Python Database Interface for free ! :::::: 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/ From cheirasacan at gmail.com Tue May 7 05:27:42 2013 From: cheirasacan at gmail.com (cheirasacan at gmail.com) Date: Tue, 7 May 2013 02:27:42 -0700 (PDT) Subject: Why sfml does not play the file inside a function in this python code? Message-ID: <69b77965-5ff5-4c6f-a73d-0826b06353f0@googlegroups.com> from tkinter import * import sfml window = Tk() window.minsize( 640, 480 ) def sonido(): file = sfml.Music.from_file('poco.ogg') file.play() test = Button ( window, text = 'Sound test', command=sonido ) test.place ( x = 10, y = 60) window.mainloop() Using Windows 7, Python 3.3, sfml 1.3.0 library, the file it is played if i put it out of the function. ? what am i doing wrong ? Thanks. From python at mrabarnett.plus.com Tue May 7 06:53:25 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 May 2013 11:53:25 +0100 Subject: Why sfml does not play the file inside a function in this python code? In-Reply-To: <69b77965-5ff5-4c6f-a73d-0826b06353f0@googlegroups.com> References: <69b77965-5ff5-4c6f-a73d-0826b06353f0@googlegroups.com> Message-ID: <5188DD25.3070107@mrabarnett.plus.com> On 07/05/2013 10:27, cheirasacan at gmail.com wrote: > from tkinter import * > import sfml > > > window = Tk() > window.minsize( 640, 480 ) > > > def sonido(): > file = sfml.Music.from_file('poco.ogg') > file.play() > > > test = Button ( window, text = 'Sound test', command=sonido ) > test.place ( x = 10, y = 60) > > window.mainloop() > > > > > Using Windows 7, Python 3.3, sfml 1.3.0 library, the file it is played if i put it out of the function. ? what am i doing wrong ? Thanks. > Perhaps what's happening is that sonido starts playing it and then returns, meaning that there's no longer a reference to it ('file' is local to the function), so it's collected by the garbage collector. If that's the case, try keeping a reference to it, perhaps by making 'file' global (in a simple program like this one, using global should be OK). From cheirasacan at gmail.com Tue May 7 09:56:28 2013 From: cheirasacan at gmail.com (cheirasacan at gmail.com) Date: Tue, 7 May 2013 06:56:28 -0700 (PDT) Subject: Why sfml does not play the file inside a function in this python code? In-Reply-To: References: <69b77965-5ff5-4c6f-a73d-0826b06353f0@googlegroups.com> Message-ID: <85771dda-4289-4cfb-a96f-23809664913e@googlegroups.com> El martes, 7 de mayo de 2013 12:53:25 UTC+2, MRAB escribi?: > On 07/05/2013 10:27, cheirasacan at gmail.com wrote: > > > from tkinter import * > > > import sfml > > > > > > > > > window = Tk() > > > window.minsize( 640, 480 ) > > > > > > > > > def sonido(): > > > file = sfml.Music.from_file('poco.ogg') > > > file.play() > > > > > > > > > test = Button ( window, text = 'Sound test', command=sonido ) > > > test.place ( x = 10, y = 60) > > > > > > window.mainloop() > > > > > > > > > > > > > > > Using Windows 7, Python 3.3, sfml 1.3.0 library, the file it is played if i put it out of the function. ??? what am i doing wrong ? Thanks. > > > > > Perhaps what's happening is that sonido starts playing it and then > > returns, meaning that there's no longer a reference to it ('file' is > > local to the function), so it's collected by the garbage collector. > > > > If that's the case, try keeping a reference to it, perhaps by making > > 'file' global (in a simple program like this one, using global should > > be OK). Thanks. A global use of 'sonido' fix the problem. The garbage collector must be the point. But this code is part of a longer project. What can i do to fix it without the use of globals? I will use more functions like this, and i would like to keep learning python as well good programming methodology. Thanks. From python at mrabarnett.plus.com Tue May 7 10:57:59 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 May 2013 15:57:59 +0100 Subject: Why sfml does not play the file inside a function in this python code? In-Reply-To: <85771dda-4289-4cfb-a96f-23809664913e@googlegroups.com> References: <69b77965-5ff5-4c6f-a73d-0826b06353f0@googlegroups.com> <85771dda-4289-4cfb-a96f-23809664913e@googlegroups.com> Message-ID: <51891677.1020009@mrabarnett.plus.com> On 07/05/2013 14:56, cheirasacan at gmail.com wrote: > El martes, 7 de mayo de 2013 12:53:25 UTC+2, MRAB escribi?: >> On 07/05/2013 10:27, cheirasacan at gmail.com wrote: >> > from tkinter import * >> > import sfml >> > >> > window = Tk() >> > window.minsize( 640, 480 ) >> > >> > def sonido(): >> > file = sfml.Music.from_file('poco.ogg') >> > file.play() >> > >> > test = Button ( window, text = 'Sound test', command=sonido ) >> > test.place ( x = 10, y = 60) >> > >> > window.mainloop() >> > >> > Using Windows 7, Python 3.3, sfml 1.3.0 library, the file it is played if i put it out of the function. ??? what am i doing wrong ? Thanks. >> > >> >> Perhaps what's happening is that sonido starts playing it and then >> returns, meaning that there's no longer a reference to it ('file' is >> local to the function), so it's collected by the garbage collector. >> >> If that's the case, try keeping a reference to it, perhaps by making >> 'file' global (in a simple program like this one, using global should >> be OK). > > Thanks. A global use of 'sonido' fix the problem. The garbage collector must be the point. But this code is part of a longer project. What can i do to fix it without the use of globals? I will use more functions like this, and i would like to keep learning python as well good programming methodology. > Thanks. > Presumably the details of the window are (or will be) hidden away in a class, so you could make 'file' an attribute of an instance. Also, please read this: http://wiki.python.org/moin/GoogleGroupsPython because gmail insists on adding extra linebreaks, which can be somewhat annoying. From rosuav at gmail.com Tue May 7 11:06:24 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 8 May 2013 01:06:24 +1000 Subject: Why sfml does not play the file inside a function in this python code? In-Reply-To: <51891677.1020009@mrabarnett.plus.com> References: <69b77965-5ff5-4c6f-a73d-0826b06353f0@googlegroups.com> <85771dda-4289-4cfb-a96f-23809664913e@googlegroups.com> <51891677.1020009@mrabarnett.plus.com> Message-ID: On Wed, May 8, 2013 at 12:57 AM, MRAB wrote: > Also, please read this: > > http://wiki.python.org/moin/GoogleGroupsPython > > because gmail insists on adding extra linebreaks, which can be somewhat > annoying. Accuracy correction: It's nothing to do with gmail, which is what I use (via python-list subscription). It's just Google Groups. ChrisA From vincent.vandevyvre at swing.be Tue May 7 13:03:00 2013 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Tue, 07 May 2013 19:03:00 +0200 Subject: dist-packages or site-packages in Python 3.2 ? In-Reply-To: References: <69b77965-5ff5-4c6f-a73d-0826b06353f0@googlegroups.com> <85771dda-4289-4cfb-a96f-23809664913e@googlegroups.com> <51891677.1020009@mrabarnett.plus.com> Message-ID: <518933C4.1050808@swing.be> Hi, I've one machine with python3.2 (on Ubuntu 12.04), in the folder /usr/lib/python3.2 it is a subfolder dist-packages, maybe created at the install or created when I've installed the binding pyexiv2, I don't know. Now, I've installed a new machine, again with Ubuntu 12.04 and therefore python3.2. This new install hasn't any *-packages subfolder in /usr/lib/python3.2 Today I install PyQt5, the make and make install are executed without error but when I try: >>> from PyQt5 import QtGui Traceback (most recent call last): File "", line 1, in ImportError: No module named PyQt5 I go to check in /usr/lib/python3.2 and I see the install of PyQt5 has created a subfolder site-packages. Is this naming dist-packages/site-packages critical for Python? (My intuition is yes!) So, I've tried with: >>> sys.path.append('/usr/lib/python3.2/site_packages') and, also, created a file __init__.py in /site-packages but that's not solved the problem. Thanks for your advices. -- Vincent V.V. Oqapy . Qarte . PaQager From cheirasacan at gmail.com Tue May 7 16:02:00 2013 From: cheirasacan at gmail.com (cheirasacan at gmail.com) Date: Tue, 7 May 2013 13:02:00 -0700 (PDT) Subject: Why sfml does not play the file inside a function in this python code? In-Reply-To: References: <69b77965-5ff5-4c6f-a73d-0826b06353f0@googlegroups.com> <85771dda-4289-4cfb-a96f-23809664913e@googlegroups.com> Message-ID: <6339279e-4f8e-43c8-bb04-b6607d7db4a1@googlegroups.com> El martes, 7 de mayo de 2013 16:57:59 UTC+2, MRAB escribi?: > On 07/05/2013 14:56, cheirasacan at gmail.com wrote: > > > El martes, 7 de mayo de 2013 12:53:25 UTC+2, MRAB escribi?: > > >> On 07/05/2013 10:27, cheirasacan at gmail.com wrote: > > >> > from tkinter import * > > >> > import sfml > > >> > > > >> > window = Tk() > > >> > window.minsize( 640, 480 ) > > >> > > > >> > def sonido(): > > >> > file = sfml.Music.from_file('poco.ogg') > > >> > file.play() > > >> > > > >> > test = Button ( window, text = 'Sound test', command=sonido ) > > >> > test.place ( x = 10, y = 60) > > >> > > > >> > window.mainloop() > > >> > > > >> > Using Windows 7, Python 3.3, sfml 1.3.0 library, the file it is played if i put it out of the function. ??? what am i doing wrong ? Thanks. > > >> > > > >> > > >> Perhaps what's happening is that sonido starts playing it and then > > >> returns, meaning that there's no longer a reference to it ('file' is > > >> local to the function), so it's collected by the garbage collector. > > >> > > >> If that's the case, try keeping a reference to it, perhaps by making > > >> 'file' global (in a simple program like this one, using global should > > >> be OK). > > > > > > Thanks. A global use of 'sonido' fix the problem. The garbage collector must be the point. But this code is part of a longer project. What can i do to fix it without the use of globals? I will use more functions like this, and i would like to keep learning python as well good programming methodology. > > > Thanks. > > > > > Presumably the details of the window are (or will be) hidden away in a > > class, so you could make 'file' an attribute of an instance. > > > > Also, please read this: > > > > http://wiki.python.org/moin/GoogleGroupsPython > > > > because gmail insists on adding extra linebreaks, which can be somewhat > > annoying. The reply is very useful. I will keep learning. Thanks for all. From robin at reportlab.com Tue May 7 07:02:38 2013 From: robin at reportlab.com (Robin Becker) Date: Tue, 07 May 2013 12:02:38 +0100 Subject: unexpected syntax errors Message-ID: <5188DF4E.3020801@chamonix.reportlab.co.uk> A user is getting this error > > New issue 8: bad raise syntax > https://bitbucket.org/rptlab/reportlab/issue/8/bad-raise-syntax > > ........ > > File "/usr/lib/python2.7/site-packages/svg2rlg.py", line 16, in > from reportlab.graphics import renderPDF > File "/usr/lib64/python2.7/site-packages/reportlab/graphics/renderPDF.py", line 168 > raise ValueError, 'bad value for textAnchor '+str(text_anchor) > ^ > SyntaxError: invalid syntax > > however, I believe that this older syntax is allowed in python 2.7. We've had other issues like this raised from various distros which are apparently making changes to 2.7 which change the external behaviour eg spelling corrections to attribute names. Could this be one of those? -- Robin Becker From redstone-cold at 163.com Tue May 7 07:15:38 2013 From: redstone-cold at 163.com (iMath) Date: Tue, 7 May 2013 04:15:38 -0700 (PDT) Subject: use python to split a video file into a set of parts Message-ID: I use the following python code to split a FLV video file into a set of parts ,when finished ,only the first part video can be played ,the other parts are corrupted.I wonder why and Is there some correct ways to split video files import sys, os kilobytes = 1024 megabytes = kilobytes * 1000 chunksize = int(1.4 * megabytes) # default: roughly a floppy print(chunksize , type(chunksize )) def split(fromfile, todir, chunksize=chunksize): if not os.path.exists(todir): # caller handles errors os.mkdir(todir) # make dir, read/write parts else: for fname in os.listdir(todir): # delete any existing files os.remove(os.path.join(todir, fname)) partnum = 0 input = open(fromfile, 'rb') # use binary mode on Windows while True: # eof=empty string from read chunk = input.read(chunksize) # get next part <= chunksize if not chunk: break partnum += 1 filename = os.path.join(todir, ('part{}.flv'.format(partnum))) fileobj = open(filename, 'wb') fileobj.write(chunk) fileobj.close() # or simply open().write() input.close() assert partnum <= 9999 # join sort fails if 5 digits return partnum if __name__ == '__main__': fromfile = input('File to be split? ') # input if clicked todir = input('Directory to store part files?') print('Splitting', fromfile, 'to', todir, 'by', chunksize) parts = split(fromfile, todir, chunksize) print('Split finished:', parts, 'parts are in', todir) From rosuav at gmail.com Tue May 7 08:00:10 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 7 May 2013 22:00:10 +1000 Subject: use python to split a video file into a set of parts In-Reply-To: References: Message-ID: On Tue, May 7, 2013 at 9:15 PM, iMath wrote: > I use the following python code to split a FLV video file into a set of parts ,when finished ,only the first part video can be played ,the other parts are corrupted.I wonder why and Is there some correct ways to split video files Most complex files of this nature have headers. You're chunking it in pure bytes, so chances are you're disrupting that. The only thing you can reliably do with your chunks is recombine them into the original file. > import sys, os > kilobytes = 1024 > megabytes = kilobytes * 1000 > chunksize = int(1.4 * megabytes) # default: roughly a floppy Hrm. Firstly, this is a very small chunksize for today's files. You hard-fail any file more than about 13GB, and for anything over a gig, you're looking at a thousand files or more. Secondly, why are you working with 1024 at the first level and 1000 at the second? You're still a smidge short of the 1440KB that was described as 1.44MB, and you have the same error of unit. Stick to binary kay OR decimal kay, don't mix and match! > print(chunksize , type(chunksize )) Since you passed chunksize through the int() constructor, you can be fairly confident it'll be an int :) > def split(fromfile, todir, chunksize=chunksize): > if not os.path.exists(todir): # caller handles errors > os.mkdir(todir) # make dir, read/write parts > else: > for fname in os.listdir(todir): # delete any existing files > os.remove(os.path.join(todir, fname)) Tip: Use os.mkdirs() in case some of its parents need to be made. And if you wrap it in try/catch rather than probing first, you eliminate a race condition. (By the way, it's pretty dangerous to just delete files from someone else's directory. I would recommend aborting with an error if you absolutely must work with an empty directory.) > input = open(fromfile, 'rb') # use binary mode on Windows As a general rule I prefer to avoid shadowing builtins, but it's not strictly a problem. > filename = os.path.join(todir, ('part{}.flv'.format(partnum))) > assert partnum <= 9999 # join sort fails if 5 digits > return partnum Why the assertion? Since this is all you do with the partnum, why does it matter how long the number is? Without seeing the join sort I can't know why that would fail; but there must surely be a solution to this. > fromfile = input('File to be split? ') # input if clicked "clicked"? I'm guessing this is a translation problem, but I've no idea what you mean by it. What you have seems to be a reasonably viable (not that I tested it or anything) file-level split. You should be able to re-join the parts quite easily. But the subsequent parts are highly unlikely to play. Even if you were working in a format that had no headers and could resynchronize, chances are a 1.4MB file won't have enough to play anything. Consider: A 1280x720 image contains 921,600 pixels; uncompressed, this would take 2-4 bytes per pixel, depending on color depth. To get a single playable frame, you would need an i-frame (ie not a difference frame) to start and end within a single 1.4MB unit; it would need to compress 50-75% just to fit, and that's assuming optimal placement. With random placement, you would need to be getting 87% compression on your index frames, and then you'd still get just one frame inside your chunk. That's not likely to be very playable. But hey. You can stitch 'em back together again :) ChrisA From davea at davea.name Tue May 7 08:10:08 2013 From: davea at davea.name (Dave Angel) Date: Tue, 07 May 2013 08:10:08 -0400 Subject: use python to split a video file into a set of parts In-Reply-To: References: Message-ID: <5188EF20.5080301@davea.name> On 05/07/2013 07:15 AM, iMath wrote: > I use the following python code to split a FLV video file into a set of parts ,when finished ,only the first part video can be played ,the other parts are corrupted.I wonder why and Is there some correct ways to split video files > There are two parts to answering the question. First, did it accurately chunk the file into separate pieces. That should be trivial to test -- simply concatenate them back together (eg. using copy /b) and make sure you get exactly the original. (using md5sum, for example) I think you will. And second, why the arbitrary pieces don't play in some unspecified video player. That one's more interesting, but hasn't anything to do with Python. I'm curious why you would expect that it would play. It won't have any of the header information, and the compressed data will be missing its context information. To split apart a binary file into useful pieces requires a lot of knowledge about the file format. -- DaveA From sjo.india at gmail.com Tue May 7 08:00:21 2013 From: sjo.india at gmail.com (Sudheer Joseph) Date: Tue, 7 May 2013 05:00:21 -0700 (PDT) Subject: formatted output Message-ID: Dear members, I need to print few arrays in a tabular form for example below array IL has 25 elements, is there an easy way to print this as 5x5 comma separated table? in python IL=[] for i in np.arange(1,bno+1): IL.append(i) print(IL) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% in fortran I could do it as below %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% integer matrix(5,5) in=0 do, k=1,5 do, l=1,5 in=in+1 matrix(k,l)=in enddo enddo m=5 n=5 do, i=1,m write(*,"(5i5)") ( matrix(i,j), j=1,n ) enddo end From roy at panix.com Tue May 7 08:42:21 2013 From: roy at panix.com (Roy Smith) Date: Tue, 07 May 2013 08:42:21 -0400 Subject: formatted output References: Message-ID: In article , Sudheer Joseph wrote: > Dear members, > I need to print few arrays in a tabular form for example below > array IL has 25 elements, is there an easy way to print this as > 5x5 comma separated table? in python > > IL=[] > for i in np.arange(1,bno+1): > IL.append(i) > print(IL) > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% > in fortran I could do it as below > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% > integer matrix(5,5) > in=0 > do, k=1,5 > do, l=1,5 > in=in+1 > matrix(k,l)=in > enddo > enddo > m=5 > n=5 > do, i=1,m > write(*,"(5i5)") ( matrix(i,j), j=1,n ) > enddo > end > Excellent. My kind of programming language! See http://www.python.org/doc/humor/#bad-habits. Anyway, that translates, more or less, as follows. Note that I'm modeling the Fortran 2-dimensional array as a dictionary keyed by (k, l) tuples. That's easy an convenient, but conceptually a poor fit and not terribly efficient. If efficiency is an issue (i.e. much larger values of (k, l), you probably want to be looking at numpy. Also, "in" is a keyword in python, so I changed that to "value". There's probably cleaner ways to do this. I did a pretty literal transliteration. matrix = {} value = 0 for k in range(1, 6): for l in range(1, 6): value += 1 matrix[(k, l)] = value for i in range(1, 6): print ",".join("%5d" % matrix[(i, j)] for j in range(1, 6)) This prints: 1, 2, 3, 4, 5 6, 7, 8, 9, 10 11, 12, 13, 14, 15 16, 17, 18, 19, 20 21, 22, 23, 24, 25 From __peter__ at web.de Tue May 7 09:28:27 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 May 2013 15:28:27 +0200 Subject: formatted output References: Message-ID: Roy Smith wrote: > In article , > Sudheer Joseph wrote: > >> Dear members, >> I need to print few arrays in a tabular form for example >> below array IL has 25 elements, is there an easy way to print >> this as 5x5 comma separated table? in python >> >> IL=[] >> for i in np.arange(1,bno+1): >> IL.append(i) >> print(IL) >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% >> in fortran I could do it as below >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% >> integer matrix(5,5) >> in=0 >> do, k=1,5 >> do, l=1,5 >> in=in+1 >> matrix(k,l)=in >> enddo >> enddo >> m=5 >> n=5 >> do, i=1,m >> write(*,"(5i5)") ( matrix(i,j), j=1,n ) >> enddo >> end >> > > Excellent. My kind of programming language! See > http://www.python.org/doc/humor/#bad-habits. > > Anyway, that translates, more or less, as follows. > > Note that I'm modeling the Fortran 2-dimensional array as a dictionary > keyed by (k, l) tuples. That's easy an convenient, but conceptually a > poor fit and not terribly efficient. If efficiency is an issue (i.e. > much larger values of (k, l), you probably want to be looking at numpy. > > Also, "in" is a keyword in python, so I changed that to "value". > There's probably cleaner ways to do this. I did a pretty literal > transliteration. > > > matrix = {} > value = 0 > for k in range(1, 6): > for l in range(1, 6): > value += 1 > matrix[(k, l)] = value > > for i in range(1, 6): > print ",".join("%5d" % matrix[(i, j)] for j in range(1, 6)) > > This prints: > > 1, 2, 3, 4, 5 > 6, 7, 8, 9, 10 > 11, 12, 13, 14, 15 > 16, 17, 18, 19, 20 > 21, 22, 23, 24, 25 Or, as the OP may be on the road to numpy anyway: >>> import numpy >>> a = numpy.arange(1, 26).reshape(5, 5) >>> a array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]) >>> import sys >>> numpy.savetxt(sys.stdout, a, delimiter=", ", fmt="%5d") 1, 2, 3, 4, 5 6, 7, 8, 9, 10 11, 12, 13, 14, 15 16, 17, 18, 19, 20 21, 22, 23, 24, 25 From bahamutzero8825 at gmail.com Tue May 7 15:58:59 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 14:58:59 -0500 Subject: Making safe file names Message-ID: <51895D03.4000300@gmail.com> Currently, I keep Last.fm artist data caches to avoid unnecessary API calls and have been naming the files using the artist name. However, artist names can have characters that are not allowed in file names for most file systems (e.g., C/A/T has forward slashes). Are there any recommended strategies for naming such files while avoiding conflicts (I wouldn't want to run into problems for an artist named C-A-T or CAT, for example)? I'd like to make the files easily identifiable, and there really are no limits on what characters can be in an artist name. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From tjreedy at udel.edu Tue May 7 18:01:34 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Tue, 07 May 2013 18:01:34 -0400 Subject: Making safe file names In-Reply-To: <51895D03.4000300@gmail.com> References: <51895D03.4000300@gmail.com> Message-ID: On 5/7/2013 3:58 PM, Andrew Berg wrote: > Currently, I keep Last.fm artist data caches to avoid unnecessary API calls and have been naming the files using the artist name. However, > artist names can have characters that are not allowed in file names for most file systems (e.g., C/A/T has forward slashes). Are there any > recommended strategies for naming such files while avoiding conflicts (I wouldn't want to run into problems for an artist named C-A-T or > CAT, for example)? I'd like to make the files easily identifiable, and there really are no limits on what characters can be in an artist name. Sounds like you want something like the html escape or urlencode functions, which serve the same purpose of encoding special chars. Rather than invent a new tranformation, you could use the same scheme used for html entities. (Sorry, I forget the details.) It is possible that one of the functions would work for you as is, or with little modification. Terry From bahamutzero8825 at gmail.com Tue May 7 19:00:43 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 18:00:43 -0500 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> Message-ID: <5189879B.6060001@gmail.com> On 2013.05.07 17:01, Terry Jan Reedy wrote: > Sounds like you want something like the html escape or urlencode > functions, which serve the same purpose of encoding special chars. > Rather than invent a new tranformation, you could use the same scheme > used for html entities. (Sorry, I forget the details.) It is possible > that one of the functions would work for you as is, or with little > modification. This has the problem of mangling non-ASCII characters (and artist names with non-ASCII characters are not rare). I most definitely want to keep as many characters untouched as possible so that the files are easy to identify by looking at the file name. Ideally, only characters that file systems don't like would be transformed. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From fabiosantosart at gmail.com Tue May 7 18:18:40 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 7 May 2013 23:18:40 +0100 Subject: Making safe file names In-Reply-To: <51895D03.4000300@gmail.com> References: <51895D03.4000300@gmail.com> Message-ID: I suggest Base64. b64encode (http://docs.python.org/2/library/base64.html#base64.b64encode) and b64decode take an argument which allows you to eliminate the pesky "/" character. It's reversible and simple. More suggestions: how about a hash? Or just use IDs from the database? On Tue, May 7, 2013 at 8:58 PM, Andrew Berg wrote: > Currently, I keep Last.fm artist data caches to avoid unnecessary API calls and have been naming the files using the artist name. However, > artist names can have characters that are not allowed in file names for most file systems (e.g., C/A/T has forward slashes). Are there any > recommended strategies for naming such files while avoiding conflicts (I wouldn't want to run into problems for an artist named C-A-T or > CAT, for example)? I'd like to make the files easily identifiable, and there really are no limits on what characters can be in an artist name. > -- > CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 > -- > http://mail.python.org/mailman/listinfo/python-list -- F?bio Santos From rosuav at gmail.com Tue May 7 19:04:23 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 8 May 2013 09:04:23 +1000 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> Message-ID: On Wed, May 8, 2013 at 8:18 AM, F?bio Santos wrote: > I suggest Base64. b64encode > (http://docs.python.org/2/library/base64.html#base64.b64encode) and > b64decode take an argument which allows you to eliminate the pesky "/" > character. It's reversible and simple. But it doesn't look anything like the original. I'd be inclined to go for something like quoted-printable or URL-encoding; special characters become much longer, but ordinary characters (mostly) stay as themselves. ChrisA From bahamutzero8825 at gmail.com Tue May 7 19:10:25 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 18:10:25 -0500 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> Message-ID: <518989E1.8010701@gmail.com> On 2013.05.07 17:18, F?bio Santos wrote: > I suggest Base64. b64encode > (http://docs.python.org/2/library/base64.html#base64.b64encode) and > b64decode take an argument which allows you to eliminate the pesky "/" > character. It's reversible and simple. > > More suggestions: how about a hash? Or just use IDs from the database? None of these would work because I would have no idea which file stores data for which artist without writing code to figure it out. If I were to end up writing a bug that messed up a few of my cache files and noticed it with a specific artist (e.g., doing a "now playing" and seeing the wrong tags), I would either have to manually match up the hash or base64 encoding in order to delete just that file so that it gets regenerated or nuke and regenerate my entire cache. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From roy at panix.com Wed May 8 20:16:25 2013 From: roy at panix.com (Roy Smith) Date: Wed, 08 May 2013 20:16:25 -0400 Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> Message-ID: In article , Dennis Lee Bieber wrote: > On Tue, 07 May 2013 18:10:25 -0500, Andrew Berg > declaimed the following in > gmane.comp.python.general: > > > None of these would work because I would have no idea which file stores > > data for which artist without writing code to figure it out. If I > > were to end up writing a bug that messed up a few of my cache files and > > noticed it with a specific artist (e.g., doing a "now playing" and > > seeing the wrong tags), I would either have to manually match up the hash > > or base64 encoding in order to delete just that file so that it > > gets regenerated or nuke and regenerate my entire cache. > > > And now you've seen why music players don't show the user the > physical file name, but maintain a database mapping the internal data > (name, artist, track#, album, etc.) to whatever mangled name was needed > to satisfy the file system. Yup. At Songza, we deal with this crap every day. It usually bites us the worst when trying to do keyword searches. When somebody types in "Blue Oyster Cult", they really mean "Blue Oyster Cult", and our search results need to reflect that. Likewise for Ke$ha, Beyonce, and I don't even want to think about the artist formerly known as an unpronounceable glyph. Pro-tip, guys. If you want to form a band, and expect people to be able to find your stuff in a search engine some day, don't play cute with your name. From rosuav at gmail.com Wed May 8 20:27:14 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 May 2013 10:27:14 +1000 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> Message-ID: On Thu, May 9, 2013 at 10:16 AM, Roy Smith wrote: > Pro-tip, guys. If you want to form a band, and expect people to be able > to find your stuff in a search engine some day, don't play cute with > your name. It's the modern equivalent of names like Catherine Withekay. ChrisA From steve+comp.lang.python at pearwood.info Wed May 8 21:49:22 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 01:49:22 GMT Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> Message-ID: <518b00a2$0$29997$c3e8da3$5496439d@news.astraweb.com> On Wed, 08 May 2013 20:16:25 -0400, Roy Smith wrote: > Yup. At Songza, we deal with this crap every day. It usually bites us > the worst when trying to do keyword searches. When somebody types in > "Blue Oyster Cult", they really mean "Blue Oyster Cult", Surely they really mean Blue ?yster Cult. > and our search > results need to reflect that. Likewise for Ke$ha, Beyonce, and I don't > even want to think about the artist formerly known as an unpronounceable > glyph. Dropped or incorrect accents are no different from any other misspelling, and good search engines (whether online or in a desktop application) should be able to deal with a tolerable number of misspellings. Googling for "Blue Oyster Cult" brings up four of the top ten hits spelled correctly with the accent, "Blue ?yster Cult". Even misspelled as "blew oytser cult", Google does the right thing. Even Bing manages to find Ke$ha's wikipedia page, her official website, youtube channel, facebook and myspace pages from the misspelling "kehsha". > Pro-tip, guys. If you want to form a band, and expect people to be able > to find your stuff in a search engine some day, don't play cute with > your name. Googling for "the the" (including quotes) brings up 145 million hits, nine of the first ten hits being relevant to the band. On the other hand, I wouldn't want to be in a band called "The Beetles". -- Steven From roy at panix.com Wed May 8 21:56:41 2013 From: roy at panix.com (Roy Smith) Date: Wed, 08 May 2013 21:56:41 -0400 Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> <518b00a2$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <518b00a2$0$29997$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > > When somebody types in > > "Blue Oyster Cult", they really mean "Blue Oyster Cult", > > Surely they really mean Blue ??yster Cult. Yes. The oomlaut was there when I typed it. Who knows what happened to it by the time it hit the wire. From bahamutzero8825 at gmail.com Wed May 8 22:11:28 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 08 May 2013 21:11:28 -0500 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> Message-ID: <518B05D0.7060206@gmail.com> On 2013.05.08 19:16, Roy Smith wrote: > Yup. At Songza, we deal with this crap every day. It usually bites us > the worst when trying to do keyword searches. When somebody types in > "Blue Oyster Cult", they really mean "Blue Oyster Cult", and our search > results need to reflect that. Likewise for Ke$ha, Beyonce, and I don't > even want to think about the artist formerly known as an unpronounceable > glyph. > > Pro-tip, guys. If you want to form a band, and expect people to be able > to find your stuff in a search engine some day, don't play cute with > your name. It's a thing (especially in witch house) to make names with odd glyphs in order to be harder to find and be more "underground". Very silly. Try doing searches for these artists with names like these: http://www.last.fm/music/%E2%96%BC%E2%96%A1%E2%96%A0%E2%96%A1%E2%96%A0%E2%96%A1%E2%96%A0 http://www.last.fm/music/ki%E2%80%A0%E2%80%A0y+c%E2%96%B2t -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From steve+comp.lang.python at pearwood.info Wed May 8 23:08:43 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 03:08:43 GMT Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> Message-ID: <518b133b$0$29997$c3e8da3$5496439d@news.astraweb.com> On Wed, 08 May 2013 21:11:28 -0500, Andrew Berg wrote: > It's a thing (especially in witch house) to make names with odd glyphs > in order to be harder to find and be more "underground". Very silly. Try > doing searches for these artists with names like these: Challenge accepted. > http://www.last.fm/music/%E2%96%BC%E2%96%A1%E2%96%A0%E2%96%A1%E2%96%A0% E2%96%A1%E2%96%A0 > http://www.last.fm/music/ki%E2%80%A0%E2%80%A0y+c%E2%96%B2t The second one is trivial. Googling for "kitty cat" "witch house" (including quotes) gives at least 3 relevant links out of the top 4 hits are relevant. (I'm not sure about the Youtube page.) That gets you the correct spelling, "ki??y c?t", and googling for that brings up many more hits. The first one is a tad trickier, since googling for "???????" brings up nothing at all, and "mourning star" doesn't give any relevant hits on the first page. But "mourning star" "witch house" (inc. quotes) is successful. I suspect that the only way to be completely ungoogleable would be to name yourself something common, not something obscure. Say, if you called yourself "Hard Rock Band", and did hard rock. But then, googling for "Heavy Metal" alone brings up the magazine as the fourth hit, so if you get famous enough, even that won't work. -- Steven From rosuav at gmail.com Wed May 8 23:53:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 May 2013 13:53:27 +1000 Subject: Making safe file names In-Reply-To: <518b133b$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> <518b133b$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, May 9, 2013 at 1:08 PM, Steven D'Aprano wrote: > I suspect that the only way to be completely ungoogleable would be to > name yourself something common, not something obscure. Say, if you called > yourself "Hard Rock Band", and did hard rock. But then, googling for > "Heavy Metal" alone brings up the magazine as the fourth hit, so if you > get famous enough, even that won't work. Yeah, so why are ubergeneric domain names worth so much? Whatevs. The best way to be findable in a web search is to have content on your web site. Real crawlable content. I guarantee you'll be found. Even if you're some tiny thing tucked away in a corner of teh interwebs, you can be found. http://www.google.com/search?q=minstrel+hall The song is there, but so is an obscure little D&D MUD. ChrisA From roy at panix.com Thu May 9 08:55:35 2013 From: roy at panix.com (Roy Smith) Date: Thu, 09 May 2013 08:55:35 -0400 Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> <518b133b$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <518b133b$0$29997$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > I suspect that the only way to be completely ungoogleable would be to > name yourself something common, not something obscure. http://en.wikipedia.org/wiki/The_band From greg.ewing at canterbury.ac.nz Thu May 9 20:04:58 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 10 May 2013 12:04:58 +1200 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> <518b133b$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: Roy Smith wrote: > In article <518b133b$0$29997$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >>I suspect that the only way to be completely ungoogleable would be to >>name yourself something common, not something obscure. > > http://en.wikipedia.org/wiki/The_band Nope... googling for "the band" brings that up as the very first result. The Google knows all. You cannot escape The Google... -- Greg From python.list at tim.thechases.com Thu May 9 20:17:07 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 9 May 2013 19:17:07 -0500 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> <518b133b$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20130509191707.4047935d@bigbox.christie.dr> On 2013-05-10 12:04, Gregory Ewing wrote: > Roy Smith wrote: > > http://en.wikipedia.org/wiki/The_band > > Nope... googling for "the band" brings that up as the > very first result. > > The Google knows all. You cannot escape The Google... That does it. I'm naming my band "Google". :-) -tkc From bahamutzero8825 at gmail.com Wed May 8 22:12:42 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 08 May 2013 21:12:42 -0500 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> <518989E1.8010701@gmail.com> Message-ID: <518B061A.6000007@gmail.com> On 2013.05.08 18:37, Dennis Lee Bieber wrote: > And now you've seen why music players don't show the user the > physical file name, but maintain a database mapping the internal data > (name, artist, track#, album, etc.) to whatever mangled name was needed > to satisfy the file system. Tags are used mainly for organization but a nice benefit of tags is that they are not subject to file system or URL or whatever other limits. If an audio file has no metadata, most players will show the file name. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From python at mrabarnett.plus.com Tue May 7 18:21:17 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 May 2013 23:21:17 +0100 Subject: Making safe file names In-Reply-To: <51895D03.4000300@gmail.com> References: <51895D03.4000300@gmail.com> Message-ID: <51897E5D.4070401@mrabarnett.plus.com> On 07/05/2013 20:58, Andrew Berg wrote: > Currently, I keep Last.fm artist data caches to avoid unnecessary API calls and have been naming the files using the artist name. However, > artist names can have characters that are not allowed in file names for most file systems (e.g., C/A/T has forward slashes). Are there any > recommended strategies for naming such files while avoiding conflicts (I wouldn't want to run into problems for an artist named C-A-T or > CAT, for example)? I'd like to make the files easily identifiable, and there really are no limits on what characters can be in an artist name. > Conflicts won't occur if: 1. All of the characters of the artist's name are mapped to an encoding. 2. Different characters map to different encodings. 3. No encoding is a prefix of another encoding. In practice, you'll be mapping most characters to themselves. From drsalists at gmail.com Tue May 7 18:29:32 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Tue, 7 May 2013 15:29:32 -0700 Subject: Making safe file names In-Reply-To: <51895D03.4000300@gmail.com> References: <51895D03.4000300@gmail.com> Message-ID: On 5/7/13, Andrew Berg wrote: > Currently, I keep Last.fm artist data caches to avoid unnecessary API calls > and have been naming the files using the artist name. However, > artist names can have characters that are not allowed in file names for most > file systems (e.g., C/A/T has forward slashes). Are there any > recommended strategies for naming such files while avoiding conflicts (I > wouldn't want to run into problems for an artist named C-A-T or > CAT, for example)? I'd like to make the files easily identifiable, and there > really are no limits on what characters can be in an artist name. You might consider: http://stromberg.dnsalias.org/svn/backshift/trunk/escape_mod.py http://stromberg.dnsalias.org/svn/backshift/trunk/test-escape_mod It doubles the length of the string, but it produces safe, easily readable escaped strings - which tends to make debugging easier. It requires a couple of other modules (easily obtained from the same SVN repo) though. From jt at toerring.de Tue May 7 18:37:01 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 7 May 2013 22:37:01 GMT Subject: Making safe file names References: Message-ID: Andrew Berg wrote: > Currently, I keep Last.fm artist data caches to avoid unnecessary API calls > and have been naming the files using the artist name. However, artist names > can have characters that are not allowed in file names for most file systems > (e.g., C/A/T has forward slashes). Are there any recommended strategies for > naming such files while avoiding conflicts (I wouldn't want to run into > problems for an artist named C-A-T or CAT, for example)? I'd like to make > the files easily identifiable, and there really are no limits on what > characters can be in an artist name. -- It's not clear what the context that you need this for. You could e.g. replace all characters not allowed by the file system by their hexidecimal (ASCII) values, preceeded by a '%" (so '/' would be changed to '%2F', and also encode a '%' itself in a name by '%25'). Then you have a well-defined two-way mapping ("isomorphic" if I remember my math-lear- nining days correctly) between the original name and the way you store it. E.g. "C/A/T" would become "C%2FA%2FT" and "C%2FA/T" would become "C%252FA%2FT" You can translate back and forth between them with not too much effort. Of course, that assumes that '%' is a character allowed by your file system - otherwise pick some other one, any one will do in principle. It's a bit harder for a human to in- terpret but rathe likely not that much of a problem. You probably will have seen that kind of scheme used in URLs. The concept is rather old and called 'escape character', i.e. have one character that assumes some special meaning and also "escaped" it. If, on the hand, those names are never to be translated back to the original name another strategy would be to use the SHA1 hash value of the artists name. Since clashes between SHA1 hash values are rather hard to produce it's a rather safe method of converting something (i.e. the artists name) to a number. The drawback, of course, is that you can't translate back from the hash value to the original name (if that would be simple the whole thing wouldn't work;-) Regards, Jens -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From bahamutzero8825 at gmail.com Tue May 7 19:30:53 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 18:30:53 -0500 Subject: Making safe file names In-Reply-To: References: Message-ID: <51898EAD.1070401@gmail.com> On 2013.05.07 17:37, Jens Thoms Toerring wrote: > You > could e.g. replace all characters not allowed by the file > system by their hexidecimal (ASCII) values, preceeded by a > '%" (so '/' would be changed to '%2F', and also encode a '%' > itself in a name by '%25'). Then you have a well-defined > two-way mapping ("isomorphic" if I remember my math-lear- > nining days correctly) between the original name and the > way you store it. E.g. > > "C/A/T" would become "C%2FA%2FT" > > and > > "C%2FA/T" would become "C%252FA%2FT" > > You can translate back and forth between them with not too > much effort. > > Of course, that assumes that '%' is a character allowed by > your file system - otherwise pick some other one, any one > will do in principle. It's a bit harder for a human to in- > terpret but rathe likely not that much of a problem. Yes, something like this is what I am trying to achieve. Judging by the responses I've gotten so far, I think I'll have to roll my own transformation scheme since URL encoding and the like transform Unicode characters. I can memorize that ???? is a Japanese composer who is well-known for his works in the Final Fantasy series of video games. Trying to match up the URL-encoded version to an artist would be almost impossible when I have several other artist names that have no ASCII characters. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From davea at davea.name Tue May 7 20:14:54 2013 From: davea at davea.name (Dave Angel) Date: Tue, 07 May 2013 20:14:54 -0400 Subject: Making safe file names In-Reply-To: <51895D03.4000300@gmail.com> References: <51895D03.4000300@gmail.com> Message-ID: <518998FE.6030805@davea.name> On 05/07/2013 03:58 PM, Andrew Berg wrote: > Currently, I keep Last.fm artist data caches to avoid unnecessary API calls and have been naming the files using the artist name. However, > artist names can have characters that are not allowed in file names for most file systems (e.g., C/A/T has forward slashes). Are there any > recommended strategies for naming such files while avoiding conflicts (I wouldn't want to run into problems for an artist named C-A-T or > CAT, for example)? I'd like to make the files easily identifiable, and there really are no limits on what characters can be in an artist name. > So what you need first is a list of allowable characters for all your target OS versions. And don't forget that the allowable characters may vary depending on the particular file system(s) mounted on a given OS. You also need to decide how to handle Unicode characters, since they're different for different OS. In Windows on NTFS, filenames are in Unicode, while on Unix, filenames are bytes. So on one of those, you will be encoding/decoding if your code is to be mostly portable. Don't forget that ls and rm may not use the same encoding you're using. So you may not consider it adequate to make the names legal, but you may also want they easily typeable in the shell. -- DaveA From bahamutzero8825 at gmail.com Tue May 7 20:51:24 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 19:51:24 -0500 Subject: Making safe file names In-Reply-To: <518998FE.6030805@davea.name> References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> Message-ID: <5189A18C.5060109@gmail.com> On 2013.05.07 19:14, Dave Angel wrote: > You also need to decide how to handle Unicode characters, since they're > different for different OS. In Windows on NTFS, filenames are in > Unicode, while on Unix, filenames are bytes. So on one of those, you > will be encoding/decoding if your code is to be mostly portable. Characters outside whatever sys.getfilesystemencoding() returns won't be allowed. If the user's locale settings don't support Unicode, my program will be far from the only one to have issues with it. Any problem reports that arise from a user moving between legacy encodings will generally be ignored. I haven't yet decided how I will handle artist names with characters outside UTF-8, but inside UTF-16/32 (UTF-16 is just fine on Windows/NTFS, but on Unix(-ish) systems, many use UTF-8 in their locale settings). > Don't forget that ls and rm may not use the same encoding you're using. > So you may not consider it adequate to make the names legal, but you > may also want they easily typeable in the shell. I don't understand. I have no intention of changing Unicode characters. This is not a Unicode issue since (modern) file systems will happily accept it. The issue is that certain characters (which are ASCII) are not allowed on some file systems: \ / : * ? " < > | @ and the NUL character The first 9 are not allowed on NTFS, the @ is not allowed on ext3cow, and NUL and / are not allowed on pretty much any file system. Locale settings and encodings aside, these 11 characters will need to be escaped. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From davea at davea.name Tue May 7 21:13:11 2013 From: davea at davea.name (Dave Angel) Date: Tue, 07 May 2013 21:13:11 -0400 Subject: Making safe file names In-Reply-To: <5189A18C.5060109@gmail.com> References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <5189A18C.5060109@gmail.com> Message-ID: <5189A6A7.4040104@davea.name> On 05/07/2013 08:51 PM, Andrew Berg wrote: > On 2013.05.07 19:14, Dave Angel wrote: >> You also need to decide how to handle Unicode characters, since they're >> different for different OS. In Windows on NTFS, filenames are in >> Unicode, while on Unix, filenames are bytes. So on one of those, you >> will be encoding/decoding if your code is to be mostly portable. > Characters outside whatever sys.getfilesystemencoding() returns won't be allowed. If the user's locale settings don't support Unicode, my > program will be far from the only one to have issues with it. Any problem reports that arise from a user moving between legacy encodings > will generally be ignored. I haven't yet decided how I will handle artist names with characters outside UTF-8, There aren't any characters "outside UTF-8". But a character is not "in utf-8", it can be encoded by utf-8. but inside UTF-16/32 (UTF-16 Nor outside UTF-16 or 32. > is just fine on Windows/NTFS, but on Unix(-ish) systems, many use UTF-8 in their locale settings). >> Don't forget that ls and rm may not use the same encoding you're using. >> So you may not consider it adequate to make the names legal, but you >> may also want they easily typeable in the shell. > I don't understand. I have no intention of changing Unicode characters. So you're comfortable typing arbitrary characters? what about all the characters that have identical displays in your font? What about viewing 0x07 in the terminal window? Or 0x04? > > > This is not a Unicode issue since (modern) file systems will happily accept it. The issue is that certain characters (which are ASCII) are > not allowed on some file systems: > \ / : * ? " < > | @ and the NUL character > The first 9 are not allowed on NTFS, the @ is not allowed on ext3cow, and NUL and / are not allowed on pretty much any file system. Locale > settings and encodings aside, these 11 characters will need to be escaped. > As soon as you have a small, finite list of invalid characters, writing an escape system is pretty easy. -- DaveA From bahamutzero8825 at gmail.com Tue May 7 22:21:41 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 21:21:41 -0500 Subject: Making safe file names In-Reply-To: <5189A6A7.4040104@davea.name> References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <5189A18C.5060109@gmail.com> <5189A6A7.4040104@davea.name> Message-ID: <5189B6B5.9020600@gmail.com> On 2013.05.07 20:13, Dave Angel wrote: > So you're comfortable typing arbitrary characters? what about all the > characters that have identical displays in your font? Identification is more important than typing. I can copy and paste into a terminal if necessary. I don't foresee typing out one of the filenames being anything more than a rare occurrence, but I will occasionally just read the list. > What about viewing > 0x07 in the terminal window? Or 0x04? I don't think Last.fm will even send those characters. In any case, control characters in artist names are rare enough that it's not worth the trouble to write the code to avoid the problems associated with them. > As soon as you have a small, finite list of invalid characters, writing > an escape system is pretty easy. Probably. I was just hoping there was an existing system that would work, but as I said in a different reply, it would seem I need to roll my own. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From nhodgson at iinet.net.au Tue May 7 21:28:11 2013 From: nhodgson at iinet.net.au (Neil Hodgson) Date: Wed, 08 May 2013 11:28:11 +1000 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> Message-ID: Andrew Berg: > This is not a Unicode issue since (modern) file systems will happily accept it. The issue is that certain characters (which are ASCII) are > not allowed on some file systems: > \ / : * ? "< > | @ and the NUL character > The first 9 are not allowed on NTFS, the @ is not allowed on ext3cow, and NUL and / are not allowed on pretty much any file system. Locale > settings and encodings aside, these 11 characters will need to be escaped. There's also the Windows device name hole. There may be trouble with artists named 'COM4', 'CLOCK$', 'Con', or similar. http://support.microsoft.com/kb/74496 http://en.wikipedia.org/wiki/Nul_%28band%29 Neil From davea at davea.name Tue May 7 21:45:08 2013 From: davea at davea.name (Dave Angel) Date: Tue, 07 May 2013 21:45:08 -0400 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> Message-ID: <5189AE24.7060002@davea.name> On 05/07/2013 09:28 PM, Neil Hodgson wrote: > Andrew Berg: > >> This is not a Unicode issue since (modern) file systems will happily >> accept it. The issue is that certain characters (which are ASCII) are >> not allowed on some file systems: >> \ / : * ? "< > | @ and the NUL character >> The first 9 are not allowed on NTFS, the @ is not allowed on ext3cow, >> and NUL and / are not allowed on pretty much any file system. Locale >> settings and encodings aside, these 11 characters will need to be >> escaped. > > There's also the Windows device name hole. There may be trouble with > artists named 'COM4', 'CLOCK$', 'Con', or similar. > In MSDOS 2, there was a switch that would tell the OS to ignore such names unless they were prefixed by \DEV. But like the switchar switch, it was largely ignored by the ignorant, and probably doesn't exist in current versions of M$OS > http://support.microsoft.com/kb/74496 > http://en.wikipedia.org/wiki/Nul_%28band%29 > > Neil While we're looking for trouble, there's also case insensitivity. Unclear if the user cares, but tom and TOM are the same file in most configurations of NT. -- DaveA From bahamutzero8825 at gmail.com Tue May 7 22:20:13 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 21:20:13 -0500 Subject: Making safe file names In-Reply-To: <5189AE24.7060002@davea.name> References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <5189AE24.7060002@davea.name> Message-ID: <5189B65D.6010103@gmail.com> On 2013.05.07 20:45, Dave Angel wrote: > While we're looking for trouble, there's also case insensitivity. > Unclear if the user cares, but tom and TOM are the same file in most > configurations of NT. Artist names on Last.fm cannot differ only in case. This does remind me to make sure to update the case of the artist name as necessary, though. For example, if Sam becomes SAM again (I have seen Last.fm change the case for artist names), I need to make sure that I don't end up with two file names differing only in case. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From bahamutzero8825 at gmail.com Tue May 7 22:06:27 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 21:06:27 -0500 Subject: Making safe file names In-Reply-To: References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> Message-ID: <5189B323.20700@gmail.com> On 2013.05.07 20:28, Neil Hodgson wrote: > http://support.microsoft.com/kb/74496 > http://en.wikipedia.org/wiki/Nul_%28band%29 I can indeed confirm that at least 'nul' cannot be used as a filename. However, I add an extension to the file names to identify them as caches. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From davea at davea.name Wed May 8 00:10:13 2013 From: davea at davea.name (Dave Angel) Date: Wed, 08 May 2013 00:10:13 -0400 Subject: Making safe file names In-Reply-To: <5189B323.20700@gmail.com> References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <5189B323.20700@gmail.com> Message-ID: <5189D025.3020204@davea.name> On 05/07/2013 10:06 PM, Andrew Berg wrote: > On 2013.05.07 20:28, Neil Hodgson wrote: >> http://support.microsoft.com/kb/74496 >> http://en.wikipedia.org/wiki/Nul_%28band%29 > I can indeed confirm that at least 'nul' cannot be used as a filename. However, I add an extension to the file names to identify them as caches. > Won't help. NUL.txt is just as reserved as NUL is. Extensions are ignored in this particular piece of historical nonsense. -- DaveA From roy at panix.com Tue May 7 22:21:53 2013 From: roy at panix.com (Roy Smith) Date: Tue, 07 May 2013 22:21:53 -0400 Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> Message-ID: In article , Dave Angel wrote: > While we're looking for trouble, there's also case insensitivity. > Unclear if the user cares, but tom and TOM are the same file in most > configurations of NT. OSX, too. From albert at spenarnc.xs4all.nl Tue May 28 09:44:10 2013 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 28 May 2013 13:44:10 GMT Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> Message-ID: <51a4b4aa$0$6063$e4fe514c@dreader36.news.xs4all.nl> In article , Neil Hodgson wrote: >Andrew Berg: > >> This is not a Unicode issue since (modern) file systems will happily >accept it. The issue is that certain characters (which are ASCII) are >> not allowed on some file systems: >> \ / : * ? "< > | @ and the NUL character >> The first 9 are not allowed on NTFS, the @ is not allowed on ext3cow, >and NUL and / are not allowed on pretty much any file system. Locale >> settings and encodings aside, these 11 characters will need to be escaped. > > There's also the Windows device name hole. There may be trouble with >artists named 'COM4', 'CLOCK$', 'Con', or similar. > >http://support.microsoft.com/kb/74496 That applies to MS-DOS names. God forbid that this still holds on more modern Microsoft operating systems? >http://en.wikipedia.org/wiki/Nul_%28band%29 > > Neil -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From rosuav at gmail.com Tue May 28 09:53:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 28 May 2013 23:53:03 +1000 Subject: Making safe file names In-Reply-To: <51a4b4aa$0$6063$e4fe514c@dreader36.news.xs4all.nl> References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <51a4b4aa$0$6063$e4fe514c@dreader36.news.xs4all.nl> Message-ID: On Tue, May 28, 2013 at 11:44 PM, Albert van der Horst wrote: > In article , > Neil Hodgson wrote: >> There's also the Windows device name hole. There may be trouble with >>artists named 'COM4', 'CLOCK$', 'Con', or similar. >> >>http://support.microsoft.com/kb/74496 > > That applies to MS-DOS names. God forbid that this still holds on more modern > Microsoft operating systems? Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> open("com1","w").write("Test\n") Traceback (most recent call last): File "", line 1, in FileNotFoundError: [Errno 2] No such file or directory: 'com1' >>> open("con","w").write("Test\n") Test 5 >>> ChrisA From invalid at invalid.invalid Tue May 28 12:03:16 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 16:03:16 +0000 (UTC) Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <51a4b4aa$0$6063$e4fe514c@dreader36.news.xs4all.nl> Message-ID: On 2013-05-28, Albert van der Horst wrote: >> There's also the Windows device name hole. There may be trouble with >> artists named 'COM4', 'CLOCK$', 'Con', or similar. >> >>http://support.microsoft.com/kb/74496 > > That applies to MS-DOS names. God forbid that this still holds on > more modern Microsoft operating systems? There are no more modern Microsoft operating systems. Only more recent ones. There are still lots of reserved filenames in recent versions of Windows. -- Grant Edwards grant.b.edwards Yow! I've got an IDEA!! at Why don't I STARE at you gmail.com so HARD, you forget your SOCIAL SECURITY NUMBER!! From steve+comp.lang.python at pearwood.info Tue May 7 23:40:50 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 May 2013 03:40:50 GMT Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> Message-ID: <5189c941$0$11094$c3e8da3@news.astraweb.com> On Tue, 07 May 2013 19:51:24 -0500, Andrew Berg wrote: > On 2013.05.07 19:14, Dave Angel wrote: >> You also need to decide how to handle Unicode characters, since they're >> different for different OS. In Windows on NTFS, filenames are in >> Unicode, while on Unix, filenames are bytes. So on one of those, you >> will be encoding/decoding if your code is to be mostly portable. > > Characters outside whatever sys.getfilesystemencoding() returns won't be > allowed. If the user's locale settings don't support Unicode, my program > will be far from the only one to have issues with it. Any problem > reports that arise from a user moving between legacy encodings will > generally be ignored. I haven't yet decided how I will handle artist > names with characters outside UTF-8, but inside UTF-16/32 (UTF-16 is > just fine on Windows/NTFS, but on Unix(-ish) systems, many use UTF-8 in > their locale settings). There aren't any characters outside of UTF-8 :-) UTF-8 covers the entire Unicode range, unlike other encodings like Latin-1 or ASCII. Well, that is to say, there may be characters that are not (yet) handled at all by Unicode, but there are no known legacy encodings that support such characters. To a first approximation, Unicode covers the entire set of characters in human use, and for those which it does not, there is always the private use area. So for example, if you wish to record the Artist Formerly Known As "The Artist Formerly Known As Prince" as Love Symbol, you could pick an arbitrary private use code point, declare that for your application that code point means Love Symbol, and use that code point as the artist name. You could even come up with a custom font that includes a rendition of that character glyph. However, there are byte combinations which are not valid UTF-8, which is a different story. If you're receiving bytes from (say) a file name, they may not necessarily make up a valid UTF-8 string. But this is not an issue if you are receiving data from something guaranteed to be valid UTF-8. >> Don't forget that ls and rm may not use the same encoding you're using. >> So you may not consider it adequate to make the names legal, but you >> may also want they easily typeable in the shell. > > I don't understand. I have no intention of changing Unicode characters. Of course you do. You even talk below about Unicode characters like * and ? not being allowed on NTFS systems. Perhaps you are thinking that there are a bunch of characters over here called "plain text ASCII characters", and a *different* bunch of characters with funny accents and stuff called "Unicode characters". If so, then you are labouring under a misapprehension, and you should start off by reading this: http://www.joelonsoftware.com/articles/Unicode.html then come back with any questions. > This is not a Unicode issue since (modern) file systems will happily > accept it. The issue is that certain characters (which are ASCII) are > not allowed on some file systems: > \ / : * ? " < > | @ and the NUL character These are all Unicode characters too. Unicode is a subset of ASCII, so anything which is ASCII is also Unicode. > The first 9 are not allowed on NTFS, the @ is not allowed on ext3cow, > and NUL and / are not allowed on pretty much any file system. Locale > settings and encodings aside, these 11 characters will need to be > escaped. If you have an artist with control characters in their name, like newline or carriage return or NUL, I think it is fair to just drop the control characters and then give the artist a thorough thrashing with a halibut. Does your mapping really need to be guaranteed reversible? If you have an artist called "JoeBlow", and another artist called "Joe\0Blow", and a third called "Joe\nBlow", does it *really* matter if your application conflates them? -- Steven From davea at davea.name Wed May 8 00:13:20 2013 From: davea at davea.name (Dave Angel) Date: Wed, 08 May 2013 00:13:20 -0400 Subject: Making safe file names In-Reply-To: <5189c941$0$11094$c3e8da3@news.astraweb.com> References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <5189c941$0$11094$c3e8da3@news.astraweb.com> Message-ID: <5189D0E0.3080007@davea.name> On 05/07/2013 11:40 PM, Steven D'Aprano wrote: > > > > These are all Unicode characters too. Unicode is a subset of ASCII, so > anything which is ASCII is also Unicode. > > Typo. You meant Unicode is a superset of ASCII. -- DaveA From steve+comp.lang.python at pearwood.info Wed May 8 00:47:24 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 May 2013 04:47:24 GMT Subject: Making safe file names References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <5189c941$0$11094$c3e8da3@news.astraweb.com> Message-ID: <5189d8dc$0$11094$c3e8da3@news.astraweb.com> On Wed, 08 May 2013 00:13:20 -0400, Dave Angel wrote: > On 05/07/2013 11:40 PM, Steven D'Aprano wrote: >> >> >> >> These are all Unicode characters too. Unicode is a subset of ASCII, so >> anything which is ASCII is also Unicode. >> >> >> > Typo. You meant Unicode is a superset of ASCII. Damn. Yes, you're right. I was thinking superset, but my fingers typed subset. Thanks for the correction. -- Steven From bahamutzero8825 at gmail.com Wed May 8 00:49:46 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 07 May 2013 23:49:46 -0500 Subject: Making safe file names In-Reply-To: <5189c941$0$11094$c3e8da3@news.astraweb.com> References: <51895D03.4000300@gmail.com> <518998FE.6030805@davea.name> <5189c941$0$11094$c3e8da3@news.astraweb.com> Message-ID: <5189D96A.8050209@gmail.com> On 2013.05.07 22:40, Steven D'Aprano wrote: > There aren't any characters outside of UTF-8 :-) UTF-8 covers the entire > Unicode range, unlike other encodings like Latin-1 or ASCII. You are correct. I'm not sure what I was thinking. >> I don't understand. I have no intention of changing Unicode characters. > > Of course you do. You even talk below about Unicode characters like * > and ? not being allowed on NTFS systems. I worded that incorrectly. What I meant, of course, is that I intend to preserve as many characters as possible and have no need to stay within ASCII. > If you have an artist with control characters in their name, like newline > or carriage return or NUL, I think it is fair to just drop the control > characters and then give the artist a thorough thrashing with a halibut. While the thrashing with a halibut may be warranted (though I personally would use a rubber chicken), conflicts are problematic. > Does your mapping really need to be guaranteed reversible? If you have an > artist called "JoeBlow", and another artist called "Joe\0Blow", and a > third called "Joe\nBlow", does it *really* matter if your application > conflates them? Yes and yes. Some artists like to be real cute with their names and make witch house artist names look tame in comparison, and some may choose to use names similar to some very popular artists. I've also seen people scrobble fake artists with names that look like real artist names (using things like a non-breaking space instead of a regular space) with different artist pictures in order to confuse and troll people. If I could remember the user profiles with this, I'd link them. Last.fm is a silly place. As I said before though, I don't think control characters are even allowed in artist names (likely for technical reasons). -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From roy at panix.com Tue May 7 20:22:17 2013 From: roy at panix.com (Roy Smith) Date: Tue, 07 May 2013 20:22:17 -0400 Subject: Making safe file names References: <51895D03.4000300@gmail.com> Message-ID: In article , Dave Angel wrote: > On 05/07/2013 03:58 PM, Andrew Berg wrote: > > Currently, I keep Last.fm artist data caches to avoid unnecessary API calls > > and have been naming the files using the artist name. However, > > artist names can have characters that are not allowed in file names for > > most file systems (e.g., C/A/T has forward slashes). Are there any > > recommended strategies for naming such files while avoiding conflicts (I > > wouldn't want to run into problems for an artist named C-A-T or > > CAT, for example)? I'd like to make the files easily identifiable, and > > there really are no limits on what characters can be in an artist name. > > > > So what you need first is a list of allowable characters for all your > target OS versions. And don't forget that the allowable characters may > vary depending on the particular file system(s) mounted on a given OS. > > You also need to decide how to handle Unicode characters, since they're > different for different OS. In Windows on NTFS, filenames are in > Unicode, while on Unix, filenames are bytes. So on one of those, you > will be encoding/decoding if your code is to be mostly portable. > > Don't forget that ls and rm may not use the same encoding you're using. > So you may not consider it adequate to make the names legal, but you > may also want they easily typeable in the shell. One possible tool that may help you here is unidecode (https://pypi.python.org/pypi/Unidecode). It doesn't solve your whole problem, but it does help get unicode text into a form which is both 7-bit clean and human readable. From cheirasacan at gmail.com Tue May 7 16:27:59 2013 From: cheirasacan at gmail.com (cheirasacan at gmail.com) Date: Tue, 7 May 2013 13:27:59 -0700 (PDT) Subject: Get filename using filefialog.askfilename Message-ID: Well. It's driving me crazy. So simple.... I use: file = filedialog.askopenfile ( mode....... ) to open a file with an open dialog box, OK. Made it. How i get the name of the opened file? i do : print(file) the output is: <......name="file.doc"...mode=......encoding.......... > How can i get the second member of 'file'? I had prove with print(file[1]) and print(file(1)) but does not work. And i am unable to find a detailed reference to this object in the i.net http://fossies.org/dox/Python-3.3.1/filedialog_8py_source.html#l00393 is where i could reach! Thanks From gordon at panix.com Tue May 7 16:37:26 2013 From: gordon at panix.com (John Gordon) Date: Tue, 7 May 2013 20:37:26 +0000 (UTC) Subject: Get filename using filefialog.askfilename References: Message-ID: In cheirasacan at gmail.com writes: > print(file) > the output is: <......name="file.doc"...mode=......encoding.......... > > How can i get the second member of 'file'? If you're using the interpreter, you can type this command: >>> help(file) And it will display documentation for using objects of that type. You can also use this command: >>> dir(file) And it will display all the members and methods that the object provides. -- 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 tjreedy at udel.edu Tue May 7 17:53:32 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Tue, 07 May 2013 17:53:32 -0400 Subject: Get filename using filefialog.askfilename In-Reply-To: References: Message-ID: On 5/7/2013 4:27 PM, cheirasacan at gmail.com wrote: > file = filedialog.askopenfile ( mode....... ) askopenfile is a convenience function that creates an Open dialog object, shows it, gets the name returned by the dialog, opens the file with that name, and returns an appropriate normal file object > to open a file with an open dialog box, OK. Made it. > > How i get the name of the opened file? file.name, (at least in 3.3), which in your example below is "file.doc" > print(file) > > the output is: <......name="file.doc"...mode=......encoding.......... > This is the standard string representation of a file object. It is created from the various attributes of the file instance, including file.name. > How can i get the second member of 'file'? Strings do not have fields. The second 'member', would be the second character, file[1], which is not what you want. > And i am unable to find a detailed reference to this object in the i.net Use the Fine Manual. The entry for builtin open() function, which you should read to understand the 'open' part of askopenfile, directs you to the Glossary entry 'file object' which says "There are actually three categories of file objects: raw binary files, buffered binary files and text files. Their interfaces are defined in the io module. The canonical way to create a file object is by using the open() function." The kind of file object you get is determined by the mode ('b' present or not), buffer arg, and maybe something else. You can look in the io chapter or use dir() and help() as John G. suggested. Python programmers should really learn to use dir(), help(), and the manuls, including the index and module index. -- Terry Jan Reedy From cheirasacan at gmail.com Wed May 8 16:14:42 2013 From: cheirasacan at gmail.com (cheirasacan at gmail.com) Date: Wed, 8 May 2013 13:14:42 -0700 (PDT) Subject: Get filename using filefialog.askfilename In-Reply-To: References: Message-ID: El martes, 7 de mayo de 2013 23:53:32 UTC+2, Terry Jan Reedy escribi?: > On 5/7/2013 4:27 PM, cheirasacan at gmail.com wrote: > > > > > file = filedialog.askopenfile ( mode....... ) > > > > askopenfile is a convenience function that creates an Open dialog > > object, shows it, gets the name returned by the dialog, opens the file > > with that name, and returns an appropriate normal file object > > > > > to open a file with an open dialog box, OK. Made it. > > > > > > How i get the name of the opened file? > > > > file.name, (at least in 3.3), which in your example below is "file.doc" > > > > > print(file) > > > > > > the output is: <......name="file.doc"...mode=......encoding.......... > > > > > This is the standard string representation of a file object. It is > > created from the various attributes of the file instance, including > > file.name. > > > > > How can i get the second member of 'file'? > > > > Strings do not have fields. The second 'member', would be the second > > character, file[1], which is not what you want. > > > > > And i am unable to find a detailed reference to this object in the i.net > > > > Use the Fine Manual. The entry for builtin open() function, which you > > should read to understand the 'open' part of askopenfile, directs you to > > the Glossary entry 'file object' which says "There are actually three > > categories of file objects: raw binary files, buffered binary files and > > text files. Their interfaces are defined in the io module. The canonical > > way to create a file object is by using the open() function." The kind > > of file object you get is determined by the mode ('b' present or not), > > buffer arg, and maybe something else. You can look in the io chapter or > > use dir() and help() as John G. suggested. > > > > Python programmers should really learn to use dir(), help(), and the > > manuls, including the index and module index. > > > > -- > > Terry Jan Reedy Yeah. This is an answer. A lot of thanks. From davea at davea.name Wed May 8 18:52:48 2013 From: davea at davea.name (Dave Angel) Date: Wed, 08 May 2013 18:52:48 -0400 Subject: Get filename using filefialog.askfilename In-Reply-To: References: Message-ID: <518AD740.9070207@davea.name> On 05/08/2013 04:14 PM, cheirasacan at gmail.com wrote: > El martes, 7 de mayo de 2013 23:53:32 UTC+2, Terry Jan Reedy escribi?: >> On 5/7/2013 4:27 PM, cheirasacan at gmail.com wrote: >> >> >> >>> > > Yeah. This is an answer. A lot of thanks. > For a moment there, I thought you were being sarcastic, and ungrateful at that. But I figured that it must have been my imagination. Usually, it's better to teach a man to fish, rather than just handing him one. But some would rather just have an answer, but not the understanding of how to acquire it. So, some unsolicited advice. And mixed in, maybe the actual answer to your original question. 1) Lose googlegroups, or figure a way to avoid its bad habits. Your responses here are doublespaced, which makes it hard to read, especially when you include so much that has nothing to do with your response. Also, triple posting leads to a number of problems, not the least of which is the number of other responders who killfile anything from googlegroups. See http://wiki.python.org/moin/GoogleGroupsPython. 2) Include enough of your code that people can actually figure out what you're up to. You're asking a question about a method of an object filedialog, but you never show how it's created. 3) Include enough of your error messages or diagnostic prints that we can see what's going on. > output is: <......name="file.doc"...mode=......encoding.......... > You elided the only important part of that line, the type of the object you're asking about. Most of us would know that a file object has an attribute of name. But instead I spent quite a while trying to find what GUI library you were using that might be handing back something representing a file. 4) include the environment you're running in. In your first thread, you included a line: Using Windows 7, Python 3.3, sfml 1.3.0 library, ... Very helpful. But no such hints here. 5) try not to override builtin names with your own local variables. In this case, you defined a local variable called file, which overwrites, presumably by coincidence, its own type name. Thanks for pointing out the "annotated source code" on fossies.org. I wasn't aware of that location. -- DaveA From sokovic.anamarija at gmail.com Tue May 7 18:26:47 2013 From: sokovic.anamarija at gmail.com (sokovic.anamarija at gmail.com) Date: Tue, 7 May 2013 15:26:47 -0700 (PDT) Subject: multiple versions of python Message-ID: <72f93710-9812-441e-8d3d-f221d5698f69@googlegroups.com> Hi, what is the generally recommended structure when we have into play this type of problem: multiple versions of python (both in the sense of main versions and sub versions, e.g., 2.7 : 2.7.1 2.7.3 3: 3.3 3.3.1 Different versions of gcc different compilation strategies (-vanilla and non-vanilla) different modules (numpy,scipy) together with the different versions of all the rest. any help is appreciated Ana From roy at panix.com Tue May 7 19:59:52 2013 From: roy at panix.com (Roy Smith) Date: Tue, 07 May 2013 19:59:52 -0400 Subject: multiple versions of python References: <72f93710-9812-441e-8d3d-f221d5698f69@googlegroups.com> Message-ID: In article <72f93710-9812-441e-8d3d-f221d5698f69 at googlegroups.com>, sokovic.anamarija at gmail.com wrote: > Hi, > > what is the generally recommended structure when we have into play this type > of problem: > multiple versions of python (both in the sense of main versions and sub > versions, e.g., > 2.7 : > 2.7.1 > 2.7.3 > 3: > 3.3 > 3.3.1 > Different versions of gcc > different compilation strategies (-vanilla and non-vanilla) > different modules (numpy,scipy) together with the different versions of all > the rest. > > any help is appreciated > > Ana Virtualenv is your friend. From fabiosantosart at gmail.com Wed May 8 03:07:25 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 8 May 2013 08:07:25 +0100 Subject: multiple versions of python In-Reply-To: References: <72f93710-9812-441e-8d3d-f221d5698f69@googlegroups.com> Message-ID: On 8 May 2013 01:03, "Roy Smith" wrote: > > In article <72f93710-9812-441e-8d3d-f221d5698f69 at googlegroups.com>, > sokovic.anamarija at gmail.com wrote: > > > Hi, > > > > what is the generally recommended structure when we have into play this type > > of problem: > > multiple versions of python (both in the sense of main versions and sub > > versions, e.g., > > 2.7 : > > 2.7.1 > > 2.7.3 > > 3: > > 3.3 > > 3.3.1 > > Different versions of gcc > > different compilation strategies (-vanilla and non-vanilla) > > different modules (numpy,scipy) together with the different versions of all > > the rest. > > > > any help is appreciated > > > > Ana > > Virtualenv is your friend. > -- > http://mail.python.org/mailman/listinfo/python-list Have you looked at tox? It manages a folder full of virtualenvs for you, including your dependencies, and allows you to run your tests against every version. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cjw at ncf.ca Tue May 7 21:35:50 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Tue, 07 May 2013 21:35:50 -0400 Subject: multiple versions of python In-Reply-To: <72f93710-9812-441e-8d3d-f221d5698f69@googlegroups.com> References: <72f93710-9812-441e-8d3d-f221d5698f69@googlegroups.com> Message-ID: On 07/05/2013 6:26 PM, sokovic.anamarija at gmail.com wrote: > Hi, > > what is the generally recommended structure when we have into play this type of problem: > multiple versions of python (both in the sense of main versions and sub versions, e.g., > 2.7 : > 2.7.1 > 2.7.3 > 3: > 3.3 > 3.3.1 > Different versions of gcc > different compilation strategies (-vanilla and non-vanilla) > different modules (numpy,scipy) together with the different versions of all the rest. > > any help is appreciated > > Ana > Do you really need more than 2.7.3 and 3.3.1. Typically, these go to C:\Python27 and C:\Python33 with windows. Colin W. From roy at panix.com Tue May 7 21:38:36 2013 From: roy at panix.com (Roy Smith) Date: Tue, 07 May 2013 21:38:36 -0400 Subject: multiple versions of python References: <72f93710-9812-441e-8d3d-f221d5698f69@googlegroups.com> Message-ID: In article , "Colin J. Williams" wrote: > Do you really need more than 2.7.3 and 3.3.1. It's often useful to have older versions around, so you can test your code against them. Lots of projects try to stay compatible with older releases. From breamoreboy at yahoo.co.uk Tue May 7 22:25:24 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 08 May 2013 03:25:24 +0100 Subject: multiple versions of python In-Reply-To: References: <72f93710-9812-441e-8d3d-f221d5698f69@googlegroups.com> Message-ID: On 08/05/2013 02:35, Colin J. Williams wrote: > On 07/05/2013 6:26 PM, sokovic.anamarija at gmail.com wrote: >> Hi, >> >> what is the generally recommended structure when we have into play >> this type of problem: >> multiple versions of python (both in the sense of main versions and >> sub versions, e.g., >> 2.7 : >> 2.7.1 >> 2.7.3 >> 3: >> 3.3 >> 3.3.1 >> Different versions of gcc >> different compilation strategies (-vanilla and non-vanilla) >> different modules (numpy,scipy) together with the different versions >> of all the rest. >> >> any help is appreciated >> >> Ana >> > Do you really need more than 2.7.3 and 3.3.1. > > Typically, these go to C:\Python27 and C:\Python33 with windows. > > Colin W. In which case you'll normally be doing a binary installation. If you're compiling it's more likely to be VC++ not gcc. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From steve+comp.lang.python at pearwood.info Wed May 8 04:52:12 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 May 2013 08:52:12 GMT Subject: object.enable() anti-pattern Message-ID: <518a123c$0$11094$c3e8da3@news.astraweb.com> I'm looking for some help in finding a term, it's not Python-specific but does apply to some Python code. This is an anti-pattern to avoid. The idea is that creating a resource ought to be the same as "turning it on", or enabling it, or similar. For example, we don't do this in Python: f = file("some_file.txt") f.open() data = f.read() because reading the file can fail if you forget to call open first. Instead, Python uses a factory function that creates the file object and opens it: f = open("some_file.txt") # if this succeeds, f is ready to use data = f.read() Basically, any time you have two steps required for using an object, e.g. like this: obj = someobject(arg1, arg2) obj.enable() you should move the make-it-work functionality out of the enable method and into __init__ so that creating the object creates it in a state ready to work. I read a blog some time ago (a year ago?) that discusses this anti- pattern, and I'm pretty sure gave it a specific name, but I cannot find it again. Can anyone find any references to this anti-pattern? My google- fu is letting me down. (For what it's worth, I'm refactoring some of my own code that falls into this anti-pattern. Die, enable method, die die die!) -- Steven From christian at python.org Wed May 8 05:51:47 2013 From: christian at python.org (Christian Heimes) Date: Wed, 08 May 2013 11:51:47 +0200 Subject: object.enable() anti-pattern In-Reply-To: <518a123c$0$11094$c3e8da3@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: Am 08.05.2013 10:52, schrieb Steven D'Aprano: > Basically, any time you have two steps required for using an object, e.g. > like this: > > obj = someobject(arg1, arg2) > obj.enable() > > you should move the make-it-work functionality out of the enable method > and into __init__ so that creating the object creates it in a state ready > to work. In general I agree that an object.enable() function is ugly. ;) But it's not necessarily the best solution for all problems. If the resource needs some kind of cleanup, the context api (__enter__() / __exit__()) is perfectly fine way to enable and disable the object. For example: class MyFile: def __init__(self, filename): self.filename = filename def __enter__(self): self.open() return self def __exit__(self, exc_type, exc_val, exc_tb): self.close() I suggest that you mention the context API in your blog post, too. Christian From robert.kern at gmail.com Wed May 8 06:13:33 2013 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 08 May 2013 11:13:33 +0100 Subject: object.enable() anti-pattern In-Reply-To: <518a123c$0$11094$c3e8da3@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: On 2013-05-08 09:52, Steven D'Aprano wrote: > I'm looking for some help in finding a term, it's not Python-specific but > does apply to some Python code. > > This is an anti-pattern to avoid. The idea is that creating a resource > ought to be the same as "turning it on", or enabling it, or similar. I don't think the anti-pattern has a name, but it's opposite pattern is named: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From steve+comp.lang.python at pearwood.info Wed May 8 08:30:30 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 May 2013 12:30:30 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: <518a4566$0$29997$c3e8da3$5496439d@news.astraweb.com> On Wed, 08 May 2013 11:13:33 +0100, Robert Kern wrote: > On 2013-05-08 09:52, Steven D'Aprano wrote: >> I'm looking for some help in finding a term, it's not Python-specific >> but does apply to some Python code. >> >> This is an anti-pattern to avoid. The idea is that creating a resource >> ought to be the same as "turning it on", or enabling it, or similar. > > I don't think the anti-pattern has a name, but it's opposite pattern is > named: > > http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization That sounds like it should be related, but it actually isn't since RAII actually has little to do with *acquiring* the resource and everything to do with *releasing* it. See, for example: http://stackoverflow.com/questions/2321511/what-is-meant-by-resource- acquisition-is-initialization-raii where it is pointed out that the resource may be acquired outside of the object constructor and passed in as an argument. RAII is actually about deterministic destruction of objects and the release of their resources. But thanks for the attempt :-) -- Steven From roy at panix.com Wed May 8 09:17:17 2013 From: roy at panix.com (Roy Smith) Date: Wed, 08 May 2013 09:17:17 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: In article <518a123c$0$11094$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: > I'm looking for some help in finding a term, it's not Python-specific but > does apply to some Python code. > > This is an anti-pattern to avoid. The idea is that creating a resource > ought to be the same as "turning it on", or enabling it, or similar. For > example, we don't do this in Python: > > > f = file("some_file.txt") > f.open() > data = f.read() I've worked with C++ code that did this. At one point in the evolution of OOP group consciousness, there was a feeling that constructors must never fail. I don't remember if it was a general language-agnostic pattern, or a specific C++ reaction to poor exception handling code in early compilers. What came out of that was the pattern you describe. All the code that could fail was factored out of the constructor into an "enable" method. That being said, sometimes there are good reasons for doing this. One example might be something like: frobnicator = Frobnicator() for file in my_file_list: frobnicator.munch(file) for line in frobnicator: process(line) If creating a Frobnicator instance is very expensive, it might pay to create an instance once and keep reusing it on multiple files. Here, munch() is your enable() method. But, that's not quite what you were talking about. From duncan.booth at invalid.invalid Wed May 8 10:27:53 2013 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 8 May 2013 14:27:53 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > I'm looking for some help in finding a term, it's not Python-specific > but does apply to some Python code. > > This is an anti-pattern to avoid. The idea is that creating a resource > ought to be the same as "turning it on", or enabling it, or similar I've come across this under the name 'two-phase construction', but as a desirable(!?) pattern rathern than as an anti-pattern. In particular Symbian used it throughout as originally their C++ implementation didn't support exceptions. Instead they had a separate cleanup stack and objects that require cleanup were pushed onto that stack after being fully constructed but before calling the initialisation that required cleanup. See http://www.developer.nokia.com/Community/Wiki/Two-phase_construction -- Duncan Booth From dan at tombstonezero.net Wed May 8 22:38:36 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Thu, 09 May 2013 02:38:36 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: On Wed, 08 May 2013 14:27:53 +0000, Duncan Booth wrote: > Steven D'Aprano wrote: >> I'm looking for some help in finding a term, it's not Python-specific >> but does apply to some Python code. >> >> This is an anti-pattern to avoid. The idea is that creating a >> resource ought to be the same as "turning it on", or enabling it, or >> similar > > I've come across this under the name 'two-phase construction', but as > a desirable(!?) pattern rathern than as an anti-pattern. Think of spinning off a thread: initialize it synchronously, and then let it execute asynchronously. We tend towards that pattern even when we know that execution will be synchronous, because we also that it could be asynchronous one day. Yes, we could wrap that up in a neat bundle, but explicit is better than implicit. Dan From steve+comp.lang.python at pearwood.info Thu May 9 01:37:35 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 05:37:35 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: <518b361f$0$11120$c3e8da3@news.astraweb.com> On Thu, 09 May 2013 02:38:36 +0000, Dan Sommers wrote: > Think of spinning off a thread: initialize it synchronously, and then > let it execute asynchronously. We tend towards that pattern even when > we know that execution will be synchronous, because we also that it > could be asynchronous one day. Whether it is synchronous or asynchronous is irrelevant. I can see use-cases for separating "make it go" from initialisation. It all depends on what you might want to do to the object before making it go. If the answer is "Nothing", then there is no reason not to have the constructor make it go. If the answer is, "Well, sometimes we need to do things to the object before making it go", then it makes sense to separate the two: blob = MyBlob(arg1, arg2, agr3) blob.poke("with a stick") blob.append(data) blob.foo = "spam" blob.make_it_go() I'm not talking about this case. I'm talking about the case where there's nothing you can do with the blob before making it go. > Yes, we could wrap that up in a neat > bundle, but explicit is better than implicit. "And that is why we always write code like this: n = int(int(len(something)) + int(1)) just to be sure that the result is explicitly an int and not just implicitly an int. Suppose some Javascript programmer was reading the code, and they thought that 1 was a floating point value. That would be bad!" -- Steven From rosuav at gmail.com Thu May 9 01:52:02 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 May 2013 15:52:02 +1000 Subject: object.enable() anti-pattern In-Reply-To: <518b361f$0$11120$c3e8da3@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b361f$0$11120$c3e8da3@news.astraweb.com> Message-ID: On Thu, May 9, 2013 at 3:37 PM, Steven D'Aprano wrote: > I can see use-cases for separating "make it go" from initialisation. It > all depends on what you might want to do to the object before making it > go. If the answer is "Nothing", then there is no reason not to have the > constructor make it go. If the answer is, "Well, sometimes we need to do > things to the object before making it go", then it makes sense to > separate the two: > > blob = MyBlob(arg1, arg2, agr3) > blob.poke("with a stick") > blob.append(data) > blob.foo = "spam" > blob.make_it_go() Example use-case: Most GUI frameworks. You create a window, then populate it, then show it. When you create the window object in Python, you expect an actual window to exist, it should have its handle etc. So it's still the same thing; the object is fully created in its constructor. ChrisA From steve+comp.lang.python at pearwood.info Wed May 8 23:12:09 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 03:12:09 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: <518b1409$0$29997$c3e8da3$5496439d@news.astraweb.com> On Wed, 08 May 2013 14:27:53 +0000, Duncan Booth wrote: > Steven D'Aprano wrote: > >> I'm looking for some help in finding a term, it's not Python-specific >> but does apply to some Python code. >> >> This is an anti-pattern to avoid. The idea is that creating a resource >> ought to be the same as "turning it on", or enabling it, or similar > > I've come across this under the name 'two-phase construction', but as a > desirable(!?) pattern rathern than as an anti-pattern. > > In particular Symbian used it throughout as originally their C++ > implementation didn't support exceptions. Instead they had a separate > cleanup stack and objects that require cleanup were pushed onto that > stack after being fully constructed but before calling the > initialisation that required cleanup. See > http://www.developer.nokia.com/Community/Wiki/Two-phase_construction Thanks for the link. It's obviously not the blog post I was looking for, but it is interesting. -- Steven From dan at tombstonezero.net Wed May 8 22:42:01 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Thu, 09 May 2013 02:42:01 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: On Wed, 08 May 2013 08:52:12 +0000, Steven D'Aprano wrote: > This is an anti-pattern to avoid. The idea is that creating a resource > ought to be the same as "turning it on", or enabling it, or similar. For > example, we don't do this in Python: > > f = file("some_file.txt") > f.open() > data = f.read() So why don't we do this? data = read("some_file.txt") > because reading the file can fail if you forget to call open first. > Instead, Python uses a factory function that creates the file object and > opens it: > > f = open("some_file.txt") # if this succeeds, f is ready to use > data = f.read() That's just the "enable" paradigm in a socially acceptable and traditional wrapper. Opening and closing the file is an implementation detail, often defeated by caching (application level or OS level) anyway. Dan From steve+comp.lang.python at pearwood.info Thu May 9 01:23:59 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 05:23:59 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: <518b32ef$0$11120$c3e8da3@news.astraweb.com> On Thu, 09 May 2013 02:42:01 +0000, Dan Sommers wrote: > On Wed, 08 May 2013 08:52:12 +0000, Steven D'Aprano wrote: > >> This is an anti-pattern to avoid. The idea is that creating a resource >> ought to be the same as "turning it on", or enabling it, or similar. >> For example, we don't do this in Python: >> >> f = file("some_file.txt") >> f.open() >> data = f.read() > > So why don't we do this? > > data = read("some_file.txt") Because there is a lot more that you might want to do to a file than just read from it. py> f = open('/tmp/x') py> dir(f) ['_CHUNK_SIZE', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines'] That's 24 public methods and attributes, excluding private and dunder attributes. There is no sensible use-case for creating a file without opening it. What would be the point? Any subsequent calls to just about any method will fail. Since you have to open the file after creating the file object anyway, why make them two different calls? Besides, this is not to denigrate the idea of a read() function that takes a filename and returns its contents. But that is not an object constructor. It may construct a file object internally, but it doesn't return the file object, so it is completely unrelated to the scenario I described. >> because reading the file can fail if you forget to call open first. >> Instead, Python uses a factory function that creates the file object >> and opens it: >> >> f = open("some_file.txt") # if this succeeds, f is ready to use >> data = f.read() > > That's just the "enable" paradigm in a socially acceptable and > traditional wrapper. Well duh. That's like saying that for loops, while loops and co-routines are just GOTO in a socially acceptable and traditional wrapper. Of course they are. That's the whole point. -- Steven From tjreedy at udel.edu Thu May 9 02:41:56 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Thu, 09 May 2013 02:41:56 -0400 Subject: object.enable() anti-pattern In-Reply-To: <518b32ef$0$11120$c3e8da3@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> Message-ID: On 5/9/2013 1:23 AM, Steven D'Aprano wrote: > Besides, this is not to denigrate the idea of a read() function that > takes a filename and returns its contents. But that is not an object > constructor. It may construct a file object internally, but it doesn't > return the file object, so it is completely unrelated to the scenario I > described. At least a few stdlib modules that define classes *also* have such functions. They create an instance of the class, call one or more methods, and return the result of the method, discarding the instance. For instance, see the subprocess module, its POpen class, and module functions; or the timeit module, its Timer class, and functions. From greg.ewing at canterbury.ac.nz Thu May 9 03:54:56 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Thu, 09 May 2013 19:54:56 +1200 Subject: object.enable() anti-pattern In-Reply-To: <518b32ef$0$11120$c3e8da3@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > There is no sensible use-case for creating a file without opening it. > What would be the point? Early unix systems often used this as a form of locking. -- Greg From cs at zip.com.au Thu May 9 04:23:31 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 9 May 2013 18:23:31 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: Message-ID: <20130509082331.GA46022@cskk.homeip.net> On 09May2013 19:54, Greg Ewing wrote: | Steven D'Aprano wrote: | >There is no sensible use-case for creating a file without opening | >it. What would be the point? | | Early unix systems often used this as a form of locking. Not just early systems: it's a nice lightweight method of making a lockfile even today if you expect to work over NFS, where not that many things are synchronous. You open a file with "0" modes, so that it is _immediately_ not writable. Other attempts to make the lock file thus fail because of the lack of write, even over NFS. Cheers, -- Cameron Simpson You can listen to what everybody says, but the fact remains that you've got to get out there and do the thing yourself. - Joan Sutherland From greg.ewing at canterbury.ac.nz Thu May 9 18:56:45 2013 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Fri, 10 May 2013 10:56:45 +1200 Subject: object.enable() anti-pattern In-Reply-To: <20130509082331.GA46022@cskk.homeip.net> References: <20130509082331.GA46022@cskk.homeip.net> Message-ID: <518C29AD.7070302@canterbury.ac.nz> Cameron Simpson wrote: > You open a file with "0" modes, so > that it is _immediately_ not writable. Other attempts to make the > lock file thus fail because of the lack of write, I don't think that's quite right. You open it with O_CREAT+O_EXCL, which atomically fails if the file already exists. The read/write modes don't really come into it, as far as I know. -- Greg From cs at zip.com.au Thu May 9 19:29:33 2013 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 10 May 2013 09:29:33 +1000 Subject: object.enable() anti-pattern In-Reply-To: <518C29AD.7070302@canterbury.ac.nz> References: <518C29AD.7070302@canterbury.ac.nz> Message-ID: <20130509232933.GA94065@cskk.homeip.net> On 10May2013 10:56, Greg Ewing wrote: | Cameron Simpson wrote: | >You open a file with "0" modes, so | >that it is _immediately_ not writable. Other attempts to make the | >lock file thus fail because of the lack of write, | | I don't think that's quite right. You open it with | O_CREAT+O_EXCL, which atomically fails if the file | already exists. The read/write modes don't really | come into it, as far as I know. Interesting. That is more direct. My understanding that it was enough to create the file with 0 modes. Maybe either will do. Must check... Cheers, -- Cameron Simpson Piracy gets easier every day, but listening to legally purchased music gets harder by the day. Firehed - http://politics.slashdot.org/comments.pl?sid=179175&cid=14846089 From steve+comp.lang.python at pearwood.info Thu May 9 07:30:32 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 11:30:32 GMT Subject: object.enable() anti-pattern References: Message-ID: <518b88d8$0$29997$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 May 2013 18:23:31 +1000, Cameron Simpson wrote: > On 09May2013 19:54, Greg Ewing wrote: > | Steven D'Aprano wrote: > | > There is no sensible use-case for creating a file WITHOUT OPENING > | > it. What would be the point? > | > | Early unix systems often used this as a form of locking. > > Not just early systems: it's a nice lightweight method of making a > lockfile even today if you expect to work over NFS, where not that many > things are synchronous. You OPEN A FILE with "0" modes [emphasis added] This is all very well and good, but for the life of me, I cannot see how opening a file is a good example of not opening a file. Perhaps it is a Zen thing, like the sound no spoon makes when you don't tap it against a glass that isn't there. -- Steven From cs at zip.com.au Thu May 9 19:36:43 2013 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 10 May 2013 09:36:43 +1000 Subject: object.enable() anti-pattern In-Reply-To: <518b88d8$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518b88d8$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20130509233643.GA95502@cskk.homeip.net> On 09May2013 11:30, Steven D'Aprano wrote: | On Thu, 09 May 2013 18:23:31 +1000, Cameron Simpson wrote: | | > On 09May2013 19:54, Greg Ewing wrote: | > | Steven D'Aprano wrote: | > | > There is no sensible use-case for creating a file WITHOUT OPENING | > | > it. What would be the point? | > | | > | Early unix systems often used this as a form of locking. | > | > Not just early systems: it's a nice lightweight method of making a | > lockfile even today if you expect to work over NFS, where not that many | > things are synchronous. You OPEN A FILE with "0" modes | | [emphasis added] | This is all very well and good, but for the life of me, I cannot see how | opening a file is a good example of not opening a file. Perhaps it is a | Zen thing, like the sound no spoon makes when you don't tap it against a | glass that isn't there. Because a file usually does not exist in isolation (yes sometimes we want an isolated file). Files usually exist in the filesystem, which is a namespace. And this is effectively a namespace operation, not a data storage operation. Of course, I can take this the other way: just because I opened it with a 0 mode field doesn't mean _I_, the opener, cannot read/write it. I've got an open file handle... A race free way to make a scratch file in a shared area, for example. The point is probably that a file isn't merely a feature free byte storage container; in the real world they usually come with all sorts of features like names and permissions. Those features will always imply creative uses. Anyway, this has little to do with your antipattern (about which I'm not totally convinced anyway unless it is a rule of thumb or code smell). It might apply to a Platonicly ideal file, but real files have more than one use case. Cheers, -- Cameron Simpson I just kept it wide-open thinking it would correct itself. Then I ran out of talent. - C. Fittipaldi From steve+comp.lang.python at pearwood.info Fri May 10 01:00:53 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 May 2013 05:00:53 GMT Subject: object.enable() anti-pattern References: <518b88d8$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <518c7f05$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 May 2013 09:36:43 +1000, Cameron Simpson wrote: > On 09May2013 11:30, Steven D'Aprano > wrote: > | On Thu, 09 May 2013 18:23:31 +1000, Cameron Simpson wrote: > | > | > On 09May2013 19:54, Greg Ewing wrote: > | > | Steven D'Aprano wrote: > | > | > There is no sensible use-case for creating a file WITHOUT > | > | > OPENING it. What would be the point? > | > | > | > | Early unix systems often used this as a form of locking. > | > > | > Not just early systems: it's a nice lightweight method of making a > | > lockfile even today if you expect to work over NFS, where not that > | > many things are synchronous. You OPEN A FILE with "0" modes > | [emphasis added] > > | This is all very well and good, but for the life of me, I cannot see > | how opening a file is a good example of not opening a file. Perhaps it > | is a Zen thing, like the sound no spoon makes when you don't tap it > | against a glass that isn't there. > > Because a file usually does not exist in isolation (yes sometimes we > want an isolated file). Files usually exist in the filesystem, which is > a namespace. And this is effectively a namespace operation, not a data > storage operation. > > Of course, I can take this the other way: just because I opened it with > a 0 mode field doesn't mean _I_, the opener, cannot read/write it. I've > got an open file handle... A race free way to make a scratch file in a > shared area, for example. But you are opening the file. Therefore, it cannot possibly be an example of not opening the file. Unlike Pascal, there is no way to create a C file descriptor in a closed state. Such a thing does not exist. If you have a file descriptor, the file is open. Once you close it, the file descriptor is no longer valid. But even if C allowed you to do so, doesn't mean that it is a good idea. At least some variants of Pascal force you to do the following: # Pseudo-code. f = open(filename) really_open(f) data = read(f) # or write, or *any other operation at all* Surely we can agree that having to call both open() and really_open() before you get an actually opened file that you can use is one call too many? There is *nothing* you can do with f before calling really_open(). So why require it? (For the record, "really_open" is spelled "reset" or "rewrite" depending on whether you want to read or write to the file.) > The point is probably that a file isn't merely a feature free byte > storage container; in the real world they usually come with all sorts of > features like names and permissions. Those features will always imply > creative uses. There are at least three related but separate things here. * file objects, or their low-level equivalent, file descriptors; * pathnames; * files on disk, which are an abstraction for data in a particular kind of data structure. They are obviously related, but they are also obviously independent. You can have a pathname that doesn't refer to any file on disk; you can have a file on disk that has been lost, and therefore is no longer accessible via a file name. (If you run fsck or equivalent, the file may be recoverable, but the name will be lost.) You can open a file object, then unlink it so it no longer points to a file on disk, but does still accept read or write calls. The counter-examples given so far apply to pathnames or files on disk. They don't apply to file objects and file descriptors. I have tried really hard to be clear that I am talking about file objects. To the extent that I have failed, I am sorry. -- Steven From roy at panix.com Fri May 10 01:50:09 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 01:50:09 -0400 Subject: object.enable() anti-pattern References: <518b88d8$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f05$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <518c7f05$0$29997$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > there is no way to create a C file descriptor in a closed state. Such > a thing does not exist. If you have a file descriptor, the file is > open. Once you close it, the file descriptor is no longer valid. Of course there is. int fd = 37; I've just created a file descriptor. There is not enough information given to know if it corresponds to an open file or not. Before you protest that "it's just an int, not a file descriptor", I should point out that they're the same thing. It's pretty common to do something like: for (int fd = 0; fd <= MAX_FD; fd++) { close(fd) } before forking, to make sure all file descriptors are closed. From steve+comp.lang.python at pearwood.info Fri May 10 05:47:37 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 May 2013 09:47:37 GMT Subject: object.enable() anti-pattern References: <518b88d8$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f05$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <518cc239$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 May 2013 01:50:09 -0400, Roy Smith wrote: > In article <518c7f05$0$29997$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> there is no way to create a C file descriptor in a closed state. Such a >> thing does not exist. If you have a file descriptor, the file is open. >> Once you close it, the file descriptor is no longer valid. > > Of course there is. > > int fd = 37; > > I've just created a file descriptor. There is not enough information > given to know if it corresponds to an open file or not. No, you haven't created a file descriptor. You've made up a number which C will allow you to use as an index into the file descriptor table, because C is a high-level assembler with very little in the way of type safety, and what little there is you can normally bypass. What you haven't done is create the record in the file descriptor table. You can't expect that read(fd) or write(fd) will work, although both should fail safe rather than segfault if 37 happens to not be an actual file descriptor. What you've done is the moral equivalent of choosing an integer at random, coercing it to a pointer, then dereferencing it to peek or poke at some memory address. (Although fortunately much safer.) It's a nice hack, but not one that takes away from what I'm saying. -- Steven From roy at panix.com Fri May 10 09:22:31 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 09:22:31 -0400 Subject: object.enable() anti-pattern References: <518b88d8$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f05$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cc239$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <518cc239$0$29997$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > > int fd = 37; > > > > I've just created a file descriptor. There is not enough information > > given to know if it corresponds to an open file or not. > > No, you haven't created a file descriptor. You've made up a number which > C will allow you to use as an index into the file descriptor table, > because C is a high-level assembler with very little in the way of type > safety, and what little there is you can normally bypass. No, I've created a file descriptor, which is, by definition, an integer. It has nothing to do with C. This is all defined by the POSIX interface. For example, the getdtablesize(2) man page says: "The entries in the descriptor table are numbered with small integers starting at 0. The call getdtablesize() returns the size of this table." So, I am now guaranteed that fds will be ints. I also know the guaranteed minimum and maximum values. The system even makes certain guarantees which let me predict what file descriptor I'll get next in certain situations. For example, from the dup(2) page on my OSX box: "The new descriptor returned by the call is the lowest numbered descriptor currently not in use by the process." > What you haven't done is create the record in the file descriptor table. That's correct. But, as described above, the system makes certain guarantees which allow me to reason about the existence or non-existence os such entries. > You can't expect that read(fd) or write(fd) will work I can expect that they will work if I have reasoned correctly about the POSIX-guaranteed semantics. For example, POSIX says(*) that this C program is guaranteed to print, "hello, fd world" (assuming the assertion passes): #include #include #include #include int main(int argc, char** argv) { int max_files = getdtablesize(); assert(max_files >= 4); for (int i = 3; i < max_files; ++i) { close(i); } dup(2); char* message = "hello, fd world\n"; write(3, message, strlen(message)); } > What you've done is the moral equivalent of choosing an integer at > random, coercing it to a pointer, then dereferencing it to peek or poke > at some memory address. (Although fortunately much safer.) No, what I've done is taken advantage of behaviors which are guaranteed by POSIX. But, we're going off into the weeds here. Where this started was you said: > There is no sensible use-case for creating a file WITHOUT OPENING > it. What would be the point? I agree with you, in general, that it is usually poor design to have classes which require instances to be initialized after they are created. The problem is, you chose as your example a particular domain where the underlying objects being modeled have unusual semantics imposed by an interface that's 40 years old. And then you made absolute statements about there not possibly ever being certain use cases, when clearly there are (for that domain). ----------------------- (*) Technically, getdtablesize() isn't POSIX, but POSIX does define other ways to get the same information. From cs at zip.com.au Fri May 10 18:25:41 2013 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 11 May 2013 08:25:41 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: Message-ID: <20130510222541.GA82778@cskk.homeip.net> On 10May2013 09:22, Roy Smith wrote: | In article <518cc239$0$29997$c3e8da3$5496439d at news.astraweb.com>, | Steven D'Aprano wrote: | > > int fd = 37; | > > | > > I've just created a file descriptor. There is not enough information | > > given to know if it corresponds to an open file or not. | > | > No, you haven't created a file descriptor. You've made up a number which | > C will allow you to use as an index into the file descriptor table, | > because C is a high-level assembler with very little in the way of type | > safety, and what little there is you can normally bypass. | | No, I've created a file descriptor, which is, by definition, an integer. | It has nothing to do with C. This is all defined by the POSIX | interface. For example, the getdtablesize(2) man page says: | | "The entries in the descriptor table are numbered with small integers | starting at 0. The call getdtablesize() returns the size of this table." [... snip ...] I'm with Steven here. You've made a number that can be used with calls that access the OS file descriptor table. But it isn't a file descriptor. (Yes, the in-program number is just a number either way.) The descriptor table is an in-kernel data structure, filled with file descriptors. All you have is a label that may or may not access a file descriptor. Anyway, we all know _what_ goes on. We're just having terminology issues. Cheers, -- Cameron Simpson My computer always does exactly what I tell it to do but sometimes I have trouble finding out what it was that I told it to do. - Dick Wexelblat From dreamingforward at gmail.com Fri May 10 23:16:11 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Fri, 10 May 2013 20:16:11 -0700 Subject: object.enable() anti-pattern In-Reply-To: <20130510222541.GA82778@cskk.homeip.net> References: <20130510222541.GA82778@cskk.homeip.net> Message-ID: > | No, I've created a file descriptor, which is, by definition, an integer. > | It has nothing to do with C. This is all defined by the POSIX > | interface. For example, the getdtablesize(2) man page says: > | > | "The entries in the descriptor table are numbered with small integers > | starting at 0. The call getdtablesize() returns the size of this table." > [... snip ...] > > I'm with Steven here. > > You've made a number that can be used with calls that access the > OS file descriptor table. But it isn't a file descriptor. (Yes, the > in-program number is just a number either way.) Steven, don't be misled. POSIX is not the model to look to -- it does not acknowledge that files are actual objects that reside on a piece of hardware. It is not simply an integer. Mark From dreamingforward at gmail.com Sat May 11 00:00:02 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Fri, 10 May 2013 21:00:02 -0700 Subject: object.enable() anti-pattern In-Reply-To: References: <20130510222541.GA82778@cskk.homeip.net> Message-ID: > Steven, don't be misled. POSIX is not the model to look to -- it does > not acknowledge that files are actual objects that reside on a piece > of hardware. It is not simply an integer. Please disregard this (my own) flame bait. -- MarkJ Tacoma, Washington From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sat May 11 00:21:44 2013 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sat, 11 May 2013 06:21:44 +0200 Subject: object.enable() anti-pattern In-Reply-To: References: <518b88d8$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f05$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cc239$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 10.05.2013 15:22 schrieb Roy Smith: > That's correct. But, as described above, the system makes certain > guarantees which allow me to reason about the existence or non-existence > os such entries. Nevertheless, your 37 is not a FD yet. Let's take your program: > #include > #include > #include > #include > > int main(int argc, char** argv) { > int max_files = getdtablesize(); > assert(max_files >= 4); Until here, the numbers 3 toll max_files may or may not be FDs. > for (int i = 3; i < max_files; ++i) { > close(i); > } Now they are closed; they are definitely no longer FDs even if they were. If you would use them in a file operation, you'd get a EBADF which means "fd is not a valid file descriptor". > dup(2); From now on, 3 is a FD and you can use it as such. > char* message = "hello, fd world\n"; > write(3, message, strlen(message)); > } > > No, what I've done is taken advantage of behaviors which are guaranteed > by POSIX. Maybe, but the integer numbers get or los their property as a file descriptor with open() and close() and not by assigning them to an int. Thomas From roy at panix.com Thu May 9 09:07:42 2013 From: roy at panix.com (Roy Smith) Date: Thu, 09 May 2013 09:07:42 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> Message-ID: In article <518b32ef$0$11120$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: > There is no sensible use-case for creating a file without opening it. Sure there is. Sometimes just creating the name in the file system is all you want to do. That's why, for example, the unix "touch" command exists. From oscar.j.benjamin at gmail.com Thu May 9 09:51:16 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 9 May 2013 14:51:16 +0100 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> Message-ID: On 9 May 2013 14:07, Roy Smith wrote: > In article <518b32ef$0$11120$c3e8da3 at news.astraweb.com>, > Steven D'Aprano wrote: > >> There is no sensible use-case for creating a file without opening it. > > Sure there is. Sometimes just creating the name in the file system is > all you want to do. That's why, for example, the unix "touch" command > exists. Wouldn't the code that implements the touch command just look something like this: f = open(filename) f.close() Or is there some other way of creating the file that doesn't open it (I mean in general not just in Python)? Oscar From steve+comp.lang.python at pearwood.info Thu May 9 14:21:37 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 18:21:37 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> Message-ID: <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 May 2013 09:07:42 -0400, Roy Smith wrote: > In article <518b32ef$0$11120$c3e8da3 at news.astraweb.com>, > Steven D'Aprano wrote: > >> There is no sensible use-case for creating a file without opening it. > > Sure there is. Sometimes just creating the name in the file system is > all you want to do. That's why, for example, the unix "touch" command > exists. Since I neglected to make it clear above that I was still talking about file objects, rather than files on disk, I take responsibility for this misunderstanding. I thought that since I kept talking about file *objects* and *constructors*, people would understand that I was talking about in-memory objects rather than on-disk files. Mea culpa. So, let me rephrase that sentence, and hopefully clear up any further misunderstandings. There is no sensible use-case for creating a file OBJECT unless it initially wraps an open file pointer. This principle doesn't just apply to OOP languages. The standard C I/O library doesn't support creating a file descriptor unless it is a file descriptor to an open file. open() has the semantics: "It shall create an open file description that refers to a file and a file descriptor that refers to that open file description." http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html and there is no corresponding function to create a *closed* file description. (Because such a thing would be pointless.) Of course language designers are free to design their language to work under whatever anti-patterns they desire. I quote from the Pascal Language Reference: "open associates the permanent file file [sic] with a file variable for reading or writing. open does not actually open the file; you must call reset or rewrite before reading or writing to that file." http://www.amath.unc.edu/sysadmin/DOC4.0/pascal/lang_ref/ ref_builtin.doc.html but since that's not a part of standard Pascal, other Pascals may behave differently. -- Steven From python at mrabarnett.plus.com Thu May 9 14:34:25 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 May 2013 19:34:25 +0100 Subject: object.enable() anti-pattern In-Reply-To: <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <518BEC31.8030301@mrabarnett.plus.com> On 09/05/2013 19:21, Steven D'Aprano wrote: > On Thu, 09 May 2013 09:07:42 -0400, Roy Smith wrote: > >> In article <518b32ef$0$11120$c3e8da3 at news.astraweb.com>, >> Steven D'Aprano wrote: >> >>> There is no sensible use-case for creating a file without opening it. >> >> Sure there is. Sometimes just creating the name in the file system is >> all you want to do. That's why, for example, the unix "touch" command >> exists. > > Since I neglected to make it clear above that I was still talking about > file objects, rather than files on disk, I take responsibility for this > misunderstanding. I thought that since I kept talking about file > *objects* and *constructors*, people would understand that I was talking > about in-memory objects rather than on-disk files. Mea culpa. > > So, let me rephrase that sentence, and hopefully clear up any further > misunderstandings. > > There is no sensible use-case for creating a file OBJECT unless it > initially wraps an open file pointer. > You might want to do this: f = File(path) if f.exists(): ... This would be an alternative to: if os.path.exists(path): ... > This principle doesn't just apply to OOP languages. The standard C I/O > library doesn't support creating a file descriptor unless it is a file > descriptor to an open file. open() has the semantics: > > "It shall create an open file description that refers to a file and a > file descriptor that refers to that open file description." > > http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html > > and there is no corresponding function to create a *closed* file > description. (Because such a thing would be pointless.) > [snip] From roy at panix.com Thu May 9 14:59:26 2013 From: roy at panix.com (Roy Smith) Date: 9 May 2013 14:59:26 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <518be931$0$29997$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > There is no sensible use-case for creating a file OBJECT unless it > initially wraps an open file pointer. OK, I guess that's a fair statement. But mostly because a python file object only exposes those subset of operations you can do on file descriptors which deal with reading and writing the contents of a file. It would not be true if python file objects included methods for querying and manipulating file metadata. It's not hard to imagine a file class which could be used like: f = file("/path/to/my/file") f.delete() That would be a totally different model from the current python file object. And then there would be plenty of things you might want to do to a file other than open it... file("/path/to/my/directory").chdir() file("/dev/sdf").mount("/var/lib/whatever") file("/mnt/swapfile").swapon() > The standard C I/O library doesn't support creating a file > descriptor unless it is a file descriptor to an open file [...] > there is no corresponding function to create a *closed* file > description. (Because such a thing would be pointless.) What about sockets? From the python standard library: s = socket.socket() Now what? You can't do much with your shiny new socket until you call bind() or connect(), or a few other things. At least not for TCP. This is essentially the two-phased construction pattern. Of course, the python socket module just exposes the semantics of the underlying OS sockets, so there's not a lot of choice there. From rosuav at gmail.com Thu May 9 17:55:49 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 07:55:49 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, May 10, 2013 at 4:59 AM, Roy Smith wrote: > It's not hard to imagine a > file class which could be used like: > > f = file("/path/to/my/file") > f.delete() > > That would be a totally different model from the current python file > object. And then there would be plenty of things you might want to do > to a file other than open it... > > file("/path/to/my/directory").chdir() > file("/dev/sdf").mount("/var/lib/whatever") > file("/mnt/swapfile").swapon() Sure, you can imagine it. But what does it do that can't be done with a moduleful of flat functions accepting strings? This makes sense in Java, I guess, but why do it in Python? ChrisA From steve+comp.lang.python at pearwood.info Thu May 9 22:30:21 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 May 2013 02:30:21 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 May 2013 19:34:25 +0100, MRAB wrote: >> There is no sensible use-case for creating a file OBJECT unless it >> initially wraps an open file pointer. >> > You might want to do this: > > f = File(path) > if f.exists(): > ... > > This would be an alternative to: > > if os.path.exists(path): > ... Sure, but your made-up File object does not represent a file, it represents a pathname which *may or may not* exist. Pathnames are not files. Not all files have a pathname that refers to them, and not all pathnames point to an actual file. Since it has an exists() method that can return False, there are so-called "File" objects that aren't files and the name is a misnomer. A much better name for the class would be "Path", not "File". I'm not saying that there is never a use-case for some objects to have an "enable" or "start" or "open" method. That would clearly be a silly thing to say. In the real world, we design many objects to have a start switch. It would be pretty horrible if your car was running all the time. But that's because there are actual use-cases for having cars *not* run, and "make it stop" is the safe default behaviour. Your fridge, on the other hand, doesn't have a "make it go" button. So long as power is applied to it, your fridge automatically runs. Likewise, your watch is an "always on" device, provided it hasn't wound down or have a flat battery. Your fire alarm is "always on". I must admit I am astonished at how controversial the opinion "if your object is useless until you call 'start', you should automatically call 'start' when the object is created" has turned out to be. -- Steven From rosuav at gmail.com Thu May 9 23:08:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 13:08:03 +1000 Subject: object.enable() anti-pattern In-Reply-To: <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, May 10, 2013 at 12:30 PM, Steven D'Aprano wrote: > I must admit I am astonished at how controversial the opinion "if your > object is useless until you call 'start', you should automatically call > 'start' when the object is created" has turned out to be. I share your astonishment. This is a very simple point: If, after constructing an object, the caller MUST call some method on it prior to the object being of use, then better design is to embed that call directly into the constructor. As always, it has its exceptions, but that doesn't stop it being a useful rule. The Path() equivalent would be: p = Path() p.set_path("/foo/bar") if p.exists(): pass Even if you have a set_path() method, it makes good sense to symlink it to __init__ to avoid this anti-pattern. C level APIs often have these sorts of initialization requirements. fd_set selectme; FD_ZERO(&selectme); This is because declaring a variable in C cannot initialize it. Anything that *has* constructors should be using them to set objects up... that's what they're for. Where's the controversy? ChrisA From roy at panix.com Thu May 9 23:09:55 2013 From: roy at panix.com (Roy Smith) Date: Thu, 09 May 2013 23:09:55 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <518c5bbc$0$29997$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > I must admit I am astonished at how controversial the opinion "if your > object is useless until you call 'start', you should automatically call > 'start' when the object is created" has turned out to be. I'm sorry. I thought you were here for an argument. I think where things went pear shaped is when you made the statement: >> There is no sensible use-case for creating a file OBJECT unless it >> initially wraps an open file pointer. That's a pretty absolute point of view. Life is rarely so absolute. From dreamingforward at gmail.com Thu May 9 23:19:05 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Thu, 9 May 2013 20:19:05 -0700 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: > I think where things went pear shaped is when you made the statement: > >>> There is no sensible use-case for creating a file OBJECT unless it >>> initially wraps an open file pointer. > > That's a pretty absolute point of view. Life is rarely so absolute. In the old days, it was useful to have fine-grained control over the file object because you didn't know where it might fail, and the OS didn't necessarily give you give good status codes. So being able to step through the entire process was the job of the progammers. Now, with languages so high like python and hardware so common, it almost is never necessary, so he has some point. A closed file pointer is useful from a OS-progamming point-of-view though because you generally never want to leave files open where they'd block other I/O. -- MarkJ Tacoma, Washington From rosuav at gmail.com Thu May 9 23:46:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 13:46:25 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, May 10, 2013 at 1:19 PM, Mark Janssen wrote: >> I think where things went pear shaped is when you made the statement: >> >>>> There is no sensible use-case for creating a file OBJECT unless it >>>> initially wraps an open file pointer. >> >> That's a pretty absolute point of view. Life is rarely so absolute. > > In the old days, it was useful to have fine-grained control over the > file object because you didn't know where it might fail, and the OS > didn't necessarily give you give good status codes. So being able to > step through the entire process was the job of the progammers. I don't know what you mean by the "old days", but a couple of decades ago, there were no such things as "file objects". You call a function to open a file, you get back a number. You explicitly close that by calling another function and passing it that number. In fact, there is no way to have a "file object" that doesn't have an open file associated with it, because it's simply... a number. > Now, with languages so high like python and hardware so common, it > almost is never necessary, so he has some point. A closed file > pointer is useful from a OS-progamming point-of-view though because > you generally never want to leave files open where they'd block other > I/O. I'm beginning to wonder if you and Dihedral are swapping notes. Dihedral's been sounding fairly coherent lately. ChrisA From dreamingforward at gmail.com Thu May 9 23:51:51 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Thu, 9 May 2013 20:51:51 -0700 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: >> In the old days, it was useful to have fine-grained control over the >> file object because you didn't know where it might fail, and the OS >> didn't necessarily give you give good status codes. So being able to >> step through the entire process was the job of the progammers. > > I don't know what you mean by the "old days", but a couple of decades > ago, there were no such things as "file objects". My apologies. I used the word "object" when I shouldn't have. > I'm beginning to wonder if you and Dihedral are swapping notes. > Dihedral's been sounding fairly coherent lately. Dihedral... That's my dream-self. Where did you encounter him? heh -- MarkJ Tacoma, Washington From steve+comp.lang.python at pearwood.info Fri May 10 01:03:10 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 May 2013 05:03:10 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 May 2013 23:09:55 -0400, Roy Smith wrote: > In article <518c5bbc$0$29997$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> I must admit I am astonished at how controversial the opinion "if your >> object is useless until you call 'start', you should automatically call >> 'start' when the object is created" has turned out to be. > > I'm sorry. I thought you were here for an argument. No, I'm here for the abuse. > I think where things went pear shaped is when you made the statement: > >>> There is no sensible use-case for creating a file OBJECT unless it >>> initially wraps an open file pointer. > > That's a pretty absolute point of view. Life is rarely so absolute. So far the only counter-examples given aren't counter-examples. One involves opening the file. The other involves something which isn't a file, but a string instead. If there are any counter-examples, they are impossible in Python and C: you cannot create a file object in Python without opening it, and you cannot create a file descriptor in C without opening it. But not in Pascal, which actually supports my claim that this is an anti-pattern: while some Pascal implementations do allow you to create a non-open file, you cannot do *anything* with it until you open it, except generate bugs. -- Steven From dan at tombstonezero.net Fri May 10 02:22:31 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Fri, 10 May 2013 06:22:31 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, 10 May 2013 05:03:10 +0000, Steven D'Aprano wrote: >>>> There is no sensible use-case for creating a file OBJECT unless it >>>> initially wraps an open file pointer. > So far the only counter-examples given aren't counter-examples ... Well, sure, if you discount operations like "create this file" and queries like "could I delete this file if I wanted to?" [0] as methods of the file system rather than of a hypothetical file object. What about a distributed system? Suppose I want to create a file object in one place, and send that object to the another place for the file to be read from or written to [1]? Suppose that for security reasons, I have to do it that way, because the first place can only create the objects, and the second place can only access the underly file contents through an existing object? I suppose that this case exists even in a non-distributed system that allows whatever execution unit exists to change its own security settings (POSIX has setuid and setgid functions), or exposes file ACLs as methods of file objects rather than of the OS or file system. What, exactly, does a "file object" represent? And going back to your original comment (which was actually in response to one of my posts), at least some operations on python file objects *could* succeed without having to open the file. An OS could provide truncate, or writeable, on un-opened files; and certainly Python could provide encoding, or isatty, on un-opened files. Of these, truncate might be the closest use case of creating a file object without any intent to write to it, for some definition of "write to it." Dan [0] Yes, I understand that asking first instead of trying to delete the file is just asking to lose any number of potential race conditions, assuming that your system even supports race conditions. [1] Think about a multi-threaded (or otherwise distributed) FTP or HTTP server. No, don't think about the server vs. the client, but rather a "core" server overseeing "sub" servers for different sessions. From steve+comp.lang.python at pearwood.info Fri May 10 07:00:49 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 May 2013 11:00:49 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 May 2013 06:22:31 +0000, Dan Sommers wrote: > On Fri, 10 May 2013 05:03:10 +0000, Steven D'Aprano wrote: > >>>>> There is no sensible use-case for creating a file OBJECT unless it >>>>> initially wraps an open file pointer. > >> So far the only counter-examples given aren't counter-examples ... > > Well, sure, if you discount operations like "create this file" and > queries like "could I delete this file if I wanted to?" [0] as methods > of the file system rather than of a hypothetical file object. > > What about a distributed system? Suppose I want to create a file object > in one place, and send that object to the another place for the file to > be read from or written to [1]? Suppose that for security reasons, I > have to do it that way, because the first place can only create the > objects, and the second place can only access the underly file contents > through an existing object? Unless you have re-implemented the file I/O system, it doesn't matter. If your file objects are based on C I/O, then even if the first server cannot read or write to the files it still creates file objects in an open state, because that is how C works. Or maybe the first server only creates some sort of proxy to the real underlying file object. Or maybe you're re-implemented the I/O system, and aren't following C's design. Since you're just making this up as a thought experiment, anything is possible. But either way, that's fine. You've found an object where it does make sense to have an explicit "make it go" method: first one entity has permission to construct the object, but not to open the underlying file. Another entity has permission to open the underlying file, but not to create the object. I have no idea whether this is a reasonable security design or not, it actually sounds a bit rubbish to me but what do I know? So let's treat it as a reasonable design. As I've said, repeatedly, that's not what I'm talking about. When you DON'T have useful things that can be done with the object before calling "enable", then it is an anti-pattern to require a separate call to "enable" method, and the enable functionality should be moved into the object constructor. If you DO have useful things that can be done, like pass the object to another entity, for security, then that's a whole 'nuther story. Really, what I'm describing is *normal* behaviour for most objects. We don't usually design APIs like this: n = int("42") n.enable() m = n + 1 m.enable() x = m*2 + n*3 print x - 1 # oops, raises because I forgot to call x.enable() That's a rubbish API, and for simple data-like objects, we all agree it is a rubbish API. So why accept the same rubbish API just because the object is more complicated? If you don't have a good, reasonable, non- contrived use-case for a separate "make it go" method, don't use one. For my next controversial opinion, I'm going to argue that we should do arithmetic using numbers rather than by inserting lists inside other lists: # Do this: count = 0 count += 1 # Not this: count = [] count.insert(0, []) *wink* -- Steven From robert.kern at gmail.com Fri May 10 08:19:05 2013 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 May 2013 13:19:05 +0100 Subject: object.enable() anti-pattern In-Reply-To: <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-10 12:00, Steven D'Aprano wrote: > But either way, that's fine. You've found an object where it does make > sense to have an explicit "make it go" method: first one entity has > permission to construct the object, but not to open the underlying file. > Another entity has permission to open the underlying file, but not to > create the object. I have no idea whether this is a reasonable security > design or not, it actually sounds a bit rubbish to me but what do I know? > So let's treat it as a reasonable design. > > As I've said, repeatedly, that's not what I'm talking about. > > When you DON'T have useful things that can be done with the object before > calling "enable", then it is an anti-pattern to require a separate call > to "enable" method, and the enable functionality should be moved into the > object constructor. If you DO have useful things that can be done, like > pass the object to another entity, for security, then that's a whole > 'nuther story. I'd be curious to see in-the-wild instances of the anti-pattern that you are talking about, then. I think everyone agrees that entirely unmotivated "enable" methods should be avoided, but I have my doubts that they come up very often. Do programmers have a natural tendency to make an extra, completely unnecessary method? I would think that they have a natural tendency to the opposite. In my experience, everyone has a reason in mind when they follow a pattern/anti-pattern. It is pretty rare that someone just does some specific, nameable thing for no reason at all. There is no need to call out an anti-pattern for which no one has a reason to do it. But there is a continuum of reasons. Some reasons are better than others. Some reasons only apply in a small set of circumstances but seem like they would apply more generally, at least to novice programmers. Programmers can be wrong about what they think the (anti-)pattern actually achieves. The whole point of naming an anti-pattern is to discuss those reasons, show where they are misapplied, where YAGNI, why novices overuse it, other patterns that should be used instead, and also the circumstances where it is actually a good pattern instead. To artificially limit the discussion of the anti-pattern to the trivial, entirely unmotivated case forbids most of the interesting and instructive parts of the conversation. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From storchaka at gmail.com Fri May 10 11:44:35 2013 From: storchaka at gmail.com (Serhiy Storchaka) Date: Fri, 10 May 2013 18:44:35 +0300 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: 10.05.13 15:19, Robert Kern ???????(??): > I'd be curious to see in-the-wild instances of the anti-pattern that you > are talking about, then. Many (if not most) GUI frameworks use this pattern. button = Button("text") button.setForegroundColor(...) button.setBackgoundColor(...) button.setFont(...) button.setRelief(...) button.setBorder(...) button.setWidth(...) button.setAction(...) button.setMouseListener(...) button.place(...) Another example is running a subprocess in Unix-like systems. fork() open/close file descriptors, set limits, etc exec*() From robert.kern at gmail.com Fri May 10 13:20:34 2013 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 May 2013 18:20:34 +0100 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-10 16:44, Serhiy Storchaka wrote: > 10.05.13 15:19, Robert Kern ???????(??): >> I'd be curious to see in-the-wild instances of the anti-pattern that you >> are talking about, then. > > Many (if not most) GUI frameworks use this pattern. > > button = Button("text") > button.setForegroundColor(...) > button.setBackgoundColor(...) > button.setFont(...) > button.setRelief(...) > button.setBorder(...) > button.setWidth(...) > button.setAction(...) > button.setMouseListener(...) > button.place(...) > > Another example is running a subprocess in Unix-like systems. > > fork() > open/close file descriptors, set limits, etc > exec*() According to Steven's criteria, neither of these are instances of the anti-pattern because there are good reasons they are this way. He is reducing the anti-pattern to just those cases where there is no reason for doing so. That is why I asked for in-the-wild instances. I should have qualified my sentence to include "according to your criteria" because people seem to be answering my query out of that context. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From rosuav at gmail.com Fri May 10 13:24:59 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 May 2013 03:24:59 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, May 11, 2013 at 1:44 AM, Serhiy Storchaka wrote: > 10.05.13 15:19, Robert Kern ???????(??): > >> I'd be curious to see in-the-wild instances of the anti-pattern that you >> are talking about, then. > > > Many (if not most) GUI frameworks use this pattern. > > button = Button("text") > button.setForegroundColor(...) > button.setBackgoundColor(...) > button.setFont(...) > button.setRelief(...) > button.setBorder(...) > button.setWidth(...) > button.setAction(...) > button.setMouseListener(...) > button.place(...) The button really exists, though. You could merge the creation and placement (or in the case of a window/dialog, the creation and showing), but it's often useful to not. However, in the specific case you have there, there's an alternative: a mapping of attributes and values passed to the constructor, and then you could pass the constructed object directly to a place(). That would let you, if you wished, effectively construct a Button with a parent right there, which makes reasonable sense. > Another example is running a subprocess in Unix-like systems. > > fork() > open/close file descriptors, set limits, etc > exec*() Hrm. Not really a corresponding example. After you fork, you have two actual processes. Following up with an exec is only one of the possible options; I've done code that forks and execs, and code that forks and keeps running, and neither of them feels "wrong" in any way. There IS a function that's similar to what you're saying, and that's vfork: """ (From POSIX.1) The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit(2) or one of the exec(3) family of functions. """ It's deprecated because it's so fragile (and because regular fork() isn't that much less efficient now; AIUI, vfork was meant to be a lightweight fork). I would say that the deprecation of vfork in favour of fork is a strong indication that the object.enable() anti-pattern can come up in kernel APIs too, and isn't liked there either. ChrisA From steve+comp.lang.python at pearwood.info Sat May 11 03:51:53 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 May 2013 07:51:53 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <518df898$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 May 2013 18:20:34 +0100, Robert Kern wrote: > According to Steven's criteria, neither of these are instances of the > anti-pattern because there are good reasons they are this way. He is > reducing the anti-pattern to just those cases where there is no reason > for doing so. But isn't that the case for all anti-patterns? We agree that GOTO is an anti-pattern. That doesn't mean that there aren't valid reasons for using GOTO. Sometimes there are good use-cases for GOTO that outweigh the harm. By calling it an anti-pattern, though, we shift the onus onto the person wanting to use GOTO: justify why you need it, or use something else. Would you object less if I called it a "code smell" than an "anti- pattern"? If so, I accept your criticism, and call it a code smell: the less temporal coupling your API has, the better. > That is why I asked for in-the-wild instances. How about this? http://legacy.thecodewhisperer.com/post/366626867/improving-clarity-by-removing-temporal-coupling Another example of temporal coupling is json_decode in PHP: you must follow it by a call to json_last_error, otherwise you have no way of telling whether the json_decode function succeeded or not. http://php.net/manual/en/function.json-last-error.php > I should > have qualified my sentence to include "according to your criteria" > because people seem to be answering my query out of that context. Now you know how I feel :-) I started this thread asking for help tracking down a blog post describing this code smell, or at least the name of such. Thanks again to Wayne Werner, who came through with the name of the anti-pattern, temporal coupling, and a blog post describing it, although not the one I read all those many moons ago. I never intended to give the impression that *any* use of a separate "enable" method call was bad. I certainly didn't intend to be bogged down into a long discussion about the minutia of file descriptors in C, but it was educational :-) -- Steven From roy at panix.com Sat May 11 09:31:12 2013 From: roy at panix.com (Roy Smith) Date: Sat, 11 May 2013 09:31:12 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> <518df898$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <518df898$0$29997$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > I never intended to give the impression that *any* use of a separate > "enable" method call was bad. I certainly didn't intend to be bogged > down into a long discussion about the minutia of file descriptors in > C, but it was educational :-) Well, you did say you were here for abuse. I think you got your money's worth. Can I interest you in a course of getting hit on the head lessons? And just to be clear to the studio audience and all of you who are watching at home... For all the effort I put into nit-picking, I do agree with Steven's basic premise. Two-phase construction is usually not the right way to be designing classes. Especially in languages like Python where constructors raising exceptions is both inexpensive and universally accepted as normal behavior. From robert.kern at gmail.com Sat May 11 15:55:49 2013 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 11 May 2013 20:55:49 +0100 Subject: object.enable() anti-pattern In-Reply-To: <518df898$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> <518df898$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-11 08:51, Steven D'Aprano wrote: > On Fri, 10 May 2013 18:20:34 +0100, Robert Kern wrote: > >> According to Steven's criteria, neither of these are instances of the >> anti-pattern because there are good reasons they are this way. He is >> reducing the anti-pattern to just those cases where there is no reason >> for doing so. > > But isn't that the case for all anti-patterns? > > We agree that GOTO is an anti-pattern. That doesn't mean that there > aren't valid reasons for using GOTO. Sometimes there are good use-cases > for GOTO that outweigh the harm. By calling it an anti-pattern, though, > we shift the onus onto the person wanting to use GOTO: justify why you > need it, or use something else. Yes, that was the point I was making. You seemed to be defining away the legitimate instances as not instances of the pattern at all because they were legitimate, and that appeared to me to be defeating the purpose of having the discussion. On a related note, I *don't* think it's a good idea to phrase it as "justify why you need it". I don't think that gives very good guidance to a novice when they are given the task of designing something. People can come up with a justification for just about anything, especially when they are only justifying things to themselves. I think it's more important to just talk about the situations where a pattern is useful, and the common situations where people, for whatever reason, *think* that a pattern is useful, but isn't. Naming it a Pattern or Anti-pattern is really just a measure of how bad the latter half of that is compared to the first half, and is less interesting than the discussion itself. That's why I had a bug up my ass about what looked like the exclusion of the "good" uses. It's the good examples that give novices an idea of what a good justification looks like, so they can tell if the justification they are giving themselves measures up. > Would you object less if I called it a "code smell" than an "anti- > pattern"? If so, I accept your criticism, and call it a code smell: the > less temporal coupling your API has, the better. That was not really my objection. I was objecting to the way you appeared to be defining the particular pattern in question. But as we appear to agree on the important matters, I won't press it further. >> That is why I asked for in-the-wild instances. > > How about this? > > http://legacy.thecodewhisperer.com/post/366626867/improving-clarity-by-removing-temporal-coupling There's something about Java mixedCase that makes my eyes glaze, so I'll take your word for it. :-) > Another example of temporal coupling is json_decode in PHP: you must > follow it by a call to json_last_error, otherwise you have no way of > telling whether the json_decode function succeeded or not. > > http://php.net/manual/en/function.json-last-error.php I suspect that the author might say something about error checking being optional. But yeah, terrible API. :-) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From rosuav at gmail.com Sat May 11 18:39:26 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 May 2013 08:39:26 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> <518df898$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, May 12, 2013 at 5:55 AM, Robert Kern wrote: >> Another example of temporal coupling is json_decode in PHP: you must >> follow it by a call to json_last_error, otherwise you have no way of >> telling whether the json_decode function succeeded or not. >> >> http://php.net/manual/en/function.json-last-error.php > > > I suspect that the author might say something about error checking being > optional. But yeah, terrible API. :-) The problem with that one isn't that error checking is optional, but that errors are signalled with a perfectly legal return value (FALSE, if I recall correctly - which is also returned if you json_decode a boolean false). Better design would either throw an exception on error, or have a unique sentinel representing the errorness of the return value. ChrisA From ndparker at gmail.com Sat May 11 11:33:51 2013 From: ndparker at gmail.com (=?UTF-8?B?QW5kcsOp?= Malo) Date: Sat, 11 May 2013 17:33:51 +0200 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2048586.oD9bP8C7rC@news.perlig.de> * Serhiy Storchaka wrote: > Another example is running a subprocess in Unix-like systems. > > fork() > open/close file descriptors, set limits, etc > exec*() For running a subprocess, only fork() is needed. For starting another executable, only exec() is needed. For running the new executable in a subprocess fork() and exec() are needed. I think, that's a bad example. These APIs are actually well-designed. nd -- Gefunden auf einer "Webdesigner"-Seite: > Programmierung in HTML, XML, WML, CGI, FLASH < # Andr? Malo # http://pub.perlig.de/ # From rosuav at gmail.com Sat May 11 12:04:50 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 May 2013 02:04:50 +1000 Subject: object.enable() anti-pattern In-Reply-To: <2048586.oD9bP8C7rC@news.perlig.de> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> <2048586.oD9bP8C7rC@news.perlig.de> Message-ID: On Sun, May 12, 2013 at 1:33 AM, Andr? Malo wrote: > * Serhiy Storchaka wrote: > >> Another example is running a subprocess in Unix-like systems. >> >> fork() >> open/close file descriptors, set limits, etc >> exec*() > > For running a subprocess, only fork() is needed. For starting another > executable, only exec() is needed. For running the new executable in a > subprocess fork() and exec() are needed. I think, that's a bad example. > These APIs are actually well-designed. That said, though, there's certainly plenty of room for one-call APIs like popen. For the simple case where you want to start a process with some other executable, wait for it to finish, and then work with its return value, it makes sense to hide the details of fork/exec/wait - especially since that simple API can be cross-platform, where fork() itself is quite hard to implement on Windows. ChrisA From wayne at waynewerner.com Sun May 12 12:48:58 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Sun, 12 May 2013 11:48:58 -0500 (CDT) Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, 10 May 2013, Robert Kern wrote: > On 2013-05-10 12:00, Steven D'Aprano wrote: > >> But either way, that's fine. You've found an object where it does make >> sense to have an explicit "make it go" method: first one entity has >> permission to construct the object, but not to open the underlying file. >> Another entity has permission to open the underlying file, but not to >> create the object. I have no idea whether this is a reasonable security >> design or not, it actually sounds a bit rubbish to me but what do I know? >> So let's treat it as a reasonable design. >> >> As I've said, repeatedly, that's not what I'm talking about. >> >> When you DON'T have useful things that can be done with the object before >> calling "enable", then it is an anti-pattern to require a separate call >> to "enable" method, and the enable functionality should be moved into the >> object constructor. If you DO have useful things that can be done, like >> pass the object to another entity, for security, then that's a whole >> 'nuther story. > > I'd be curious to see in-the-wild instances of the anti-pattern that you are > talking about, then. I think everyone agrees that entirely unmotivated > "enable" methods should be avoided, but I have my doubts that they come up > very often. Do programmers have a natural tendency to make an extra, > completely unnecessary method? I would think that they have a natural > tendency to the opposite. > > In my experience, everyone has a reason in mind when they follow a > pattern/anti-pattern. It is pretty rare that someone just does some specific, > nameable thing for no reason at all. There is no need to call out an > anti-pattern for which no one has a reason to do it. But there is a continuum > of reasons. Some reasons are better than others. Some reasons only apply in a > small set of circumstances but seem like they would apply more generally, at > least to novice programmers. Programmers can be wrong about what they think > the (anti-)pattern actually achieves. The whole point of naming an > anti-pattern is to discuss those reasons, show where they are misapplied, > where YAGNI, why novices overuse it, other patterns that should be used > instead, and also the circumstances where it is actually a good pattern > instead. I'll share the anti-pattern that I've seen many times (not actually in Python) class CoolPresenter: def __init__(self): self.view = None self.some_property = None self.other_property = None def initialize(self): self.view.disable() data = self.load_data() self.view.data = data self.view.enable() def reload(self): if self.view is None: raise NotInitializedError("Error: Please setup class") self.view.disable() data = self.load_data() self.view.data = data self.view.enable() Then you would see code like this: presenter = CoolPresenter() presenter.view = CoolView() This is just plain silly for a few reasons: - It's ambiguous. I don't know what's required for the CoolPresenter to function properly. - The temporal coupling mentioned earlier. I can create an instance of a class and then call a function (say `reload`) and then boom! My program crashes. There is *no possible* use case of this class where you can use it without a view. The motivation behind this anti-pattern that I've seen is the desire to not have a constructor that "does too much". So you end out with an empty constructor and temporal coupling, and a terrible API that doesn't clearly explain the requirements of the class. Your class constructor should *require* everything that is necessary to have a stable state when the class is created (i.e. you should be able to properly call any function, set any property without an exception happening) Why? Less bugs, easier to comprehend, change/update your code. Easier to use the class. -W From tjreedy at udel.edu Sun May 12 16:23:58 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 12 May 2013 16:23:58 -0400 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 5/12/2013 12:48 PM, Wayne Werner wrote: > I'll share the anti-pattern that I've seen many times (not actually in > Python) > > class CoolPresenter: > def __init__(self): > self.view = None > self.some_property = None > self.other_property = None > > def initialize(self): > self.view.disable() > data = self.load_data() > self.view.data = data > self.view.enable() > > > def reload(self): > if self.view is None: > raise NotInitializedError("Error: Please setup class") > self.view.disable() > data = self.load_data() > self.view.data = data > self.view.enable() > > > > Then you would see code like this: > > presenter = CoolPresenter() > presenter.view = CoolView() > > This is just plain silly for a few reasons: > > - It's ambiguous. I don't know what's required for the CoolPresenter > to function properly. > > - The temporal coupling mentioned earlier. I can create an instance of > a class and then call a function (say `reload`) and then boom! My > program crashes. There is *no possible* use case of this class where > you can use it without a view. Thank you for this examples. It makes Steven's point clearer than the 'file object' example. The problem with the latter is that objectors could could point to file path objects, which are used to do some manipulations of files and directory entries on the storage device *without looking at the specific file data*. Such file data, as opposed to directory data, never enters the program data space and with smart enough devices, need not enter the CPU and its RAM. They could then confuse them with file access objects which are used to transfer specific data *between* files and program data space. The confusion is adied by the fact that file path objects are used to create file access objects. They do separate jobs and it seems that they are well kept separate, even if there are languages where file path objects can have file access functions turned on and off. > The motivation behind this anti-pattern that I've seen is the desire to > not have a constructor that "does too much". So you end out with an > empty constructor and temporal coupling, and a terrible API that doesn't > clearly explain the requirements of the class. Your class constructor > should *require* everything that is necessary to have a stable state > when the class is created (i.e. you should be able to properly call any > function, set any property without an exception happening) > > Why? Less bugs, easier to comprehend, change/update your code. Easier to > use the class. -- Terry Jan Reedy From roy at panix.com Fri May 10 10:01:14 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 10:01:14 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Robert Kern wrote: > I'd be curious to see in-the-wild instances of the anti-pattern that > you are talking about, then. I think everyone agrees that entirely > unmotivated "enable" methods should be avoided, but I have my doubts > that they come up very often. As I mentioned earlier in this thread, this was a common pattern in the early days of C++, when exceptions were a new concept and handled poorly by many compilers (and, for that matter, programmers). There was a school of thought that constructors should never be able to fail (because the only way for a constructor to fail is to throw an exception). The pattern was to always have the constructor succeed, and then either have a way to check to see if the newly-constructed object was valid, or have a separate post-construction initialization step which could fail. See, for example, the isValid() and Exists() calls for RogueWave's RWFile class (http://tinyurl.com/c8kv26g). And also, http://tinyurl.com/cgs6clx. Even today, there are C++ implementations which do not use exceptions. Some are for use in embedded or real-time systems where things need to be strictly time-bound and/or memory-bound. Others are for historical reasons (http://tinyurl.com/6hn4zo). Once people were used to writing "can't fail" constructors in C++, they often continued using that pattern in other languages, where the underlying reasons no longer made sense. Quite possibly, they never even knew the underlying reasons; they were taught, "Constructors must never fail", and assumed it was a universal rule. This, BTW, is one of my biggest beefs with the classic Gang Of Four pattern book. It presents a bunch of patterns as being universally applicable, when in reality many (if not most) of them are highly C++ specific. BTW, whenever I read things like, "I think everyone agrees", I automatically assume what the writer really meant was, "I, and all the people who agree with me, think". From oscar.j.benjamin at gmail.com Fri May 10 10:29:00 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 10 May 2013 15:29:00 +0100 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 10 May 2013 15:01, Roy Smith wrote: > In article , > Robert Kern wrote: > >> I'd be curious to see in-the-wild instances of the anti-pattern that >> you are talking about, then. I think everyone agrees that entirely >> unmotivated "enable" methods should be avoided, but I have my doubts >> that they come up very often. > > As I mentioned earlier in this thread, this was a common pattern in the > early days of C++, when exceptions were a new concept and handled poorly > by many compilers (and, for that matter, programmers). > > There was a school of thought that constructors should never be able to > fail (because the only way for a constructor to fail is to throw an > exception). The pattern was to always have the constructor succeed, and > then either have a way to check to see if the newly-constructed object > was valid, or have a separate post-construction initialization step > which could fail. It's not just because of exceptions. In C++ virtual method calls in a constructor for a class A will always call the methods of class A even if the object being constructed is actually of a subclass B because the B part of the object isn't initialised when the A constructor is called. There may be a better way to do this since I last used C++ but as I remember it the two-phase pattern was a recommended way to implement polymorphic behaviour during initialisation. Oscar From roy at panix.com Fri May 10 10:37:37 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 10:37:37 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Oscar Benjamin wrote: > It's not just because of exceptions. In C++ virtual method calls in a > constructor for a class A will always call the methods of class A even > if the object being constructed is actually of a subclass B because > the B part of the object isn't initialised when the A constructor is > called. There may be a better way to do this since I last used C++ but > as I remember it the two-phase pattern was a recommended way to > implement polymorphic behaviour during initialisation. Mind. Blown. One of the things I love (FSVO love) about C++ is that no matter how much I learn, there's always whole new areas of wonderment to explore behind doors I didn't even know existed. Thank you. I suppose, if I had a class like this, I would write a factory function which called the constructor and post-construction initializer. And then I would make the constructor protected. From rosuav at gmail.com Fri May 10 10:46:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 May 2013 00:46:56 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, May 11, 2013 at 12:37 AM, Roy Smith wrote: > I suppose, if I had a class like this, I would write a factory function > which called the constructor and post-construction initializer. And > then I would make the constructor protected. That sounds like a reasonable plan, with the possible exception of protected. Since meeting Python, I've stopped using private and protected anywhere. ChrisA From roy at panix.com Fri May 10 10:54:54 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 10:54:54 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Chris Angelico wrote: > On Sat, May 11, 2013 at 12:37 AM, Roy Smith wrote: > > I suppose, if I had a class like this, I would write a factory function > > which called the constructor and post-construction initializer. And > > then I would make the constructor protected. > > That sounds like a reasonable plan, with the possible exception of > protected. Since meeting Python, I've stopped using private and > protected anywhere. > > ChrisA Each language has its own set of best practices. Trying to write C++ code using Python patterns is as bad as trying to write Python code using C++ patterns. From rosuav at gmail.com Fri May 10 11:09:04 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 May 2013 01:09:04 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, May 11, 2013 at 12:54 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> On Sat, May 11, 2013 at 12:37 AM, Roy Smith wrote: >> > I suppose, if I had a class like this, I would write a factory function >> > which called the constructor and post-construction initializer. And >> > then I would make the constructor protected. >> >> That sounds like a reasonable plan, with the possible exception of >> protected. Since meeting Python, I've stopped using private and >> protected anywhere. >> >> ChrisA > > Each language has its own set of best practices. Trying to write C++ > code using Python patterns is as bad as trying to write Python code > using C++ patterns. Agreed, in generality. But what is actually gained by hiding data from yourself? Compare: class Foo { int asdf; public: Foo(int _asdf):asdf(_asdf) {} int get_asdf() {return asdf;} void set_asdf(int _asdf) {asdf=_asdf;} void frob() {printf("Hi, I am %d\n",asdf);} }; struct Foo { int asdf; Foo(int _asdf):asdf(_asdf) {} void frob() {printf("Hi, I am %d\n",asdf);} }; Is there anything worse about the second one? Oh, and by logical extension, here's something that doesn't (AFAIK) work in C++, but does in another language that's syntactically similar: class Foo(int asdf) { void frob() {write("Hi, I am %d\n",asdf);} } Now that's brevity. Why bother saying what's patently obvious? (Pike's "class" keyword is like C++'s "struct", members are public by default.) ChrisA From roy at panix.com Fri May 10 11:21:33 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 11:21:33 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Chris Angelico wrote: > > Each language has its own set of best practices. Trying to write C++ > > code using Python patterns is as bad as trying to write Python code > > using C++ patterns. > > Agreed, in generality. But what is actually gained by hiding data from > yourself? You're not hiding it from yourself. You're hiding it from the other people who are using your code and may not understand all the subtle gotchas as well as you do. In the calling-virtual-methods-in-the-constructor case we're talking about here, constructing an object by calling B() without immediately following it up with a call to B::PostInit() is dangerous. If you document that you shouldn't do that, but allow it anyway, people will do it. Better to disallow it completely. People will bitch when their code doesn't compile, but that's better than compiling and running and producing the wrong results. You solve this problem in Python by simply having the constructor do the right thing. From rosuav at gmail.com Fri May 10 11:44:31 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 May 2013 01:44:31 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, May 11, 2013 at 1:21 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: >> >> Agreed, in generality. But what is actually gained by hiding data from >> yourself? > > You're not hiding it from yourself. You're hiding it from the other > people who are using your code and may not understand all the subtle > gotchas as well as you do. True. And on looking over my code, I find that there are a few cases where I've used private members: I have a buffer class that acts pretty much like a non-refcounted string, and it declares a private copy constructor to prevent accidental misuse. But it's the exception, not the rule. My main point isn't about the cases where you actually want to prevent access, but the all-too-common case where the member itself is private and there are two public methods to get and set it. Massive boilerplate. Completely unnecessary in 99%+ of cases. ChrisA From robert.kern at gmail.com Fri May 10 11:33:09 2013 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 May 2013 16:33:09 +0100 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-10 15:01, Roy Smith wrote: > In article , > Robert Kern wrote: > >> I'd be curious to see in-the-wild instances of the anti-pattern that >> you are talking about, then. I think everyone agrees that entirely >> unmotivated "enable" methods should be avoided, but I have my doubts >> that they come up very often. > > As I mentioned earlier in this thread, this was a common pattern in the > early days of C++, when exceptions were a new concept and handled poorly > by many compilers (and, for that matter, programmers). > > There was a school of thought that constructors should never be able to > fail (because the only way for a constructor to fail is to throw an > exception). The pattern was to always have the constructor succeed, and > then either have a way to check to see if the newly-constructed object > was valid, or have a separate post-construction initialization step > which could fail. > > See, for example, the isValid() and Exists() calls for RogueWave's > RWFile class (http://tinyurl.com/c8kv26g). And also, > http://tinyurl.com/cgs6clx. > > Even today, there are C++ implementations which do not use exceptions. > Some are for use in embedded or real-time systems where things need to > be strictly time-bound and/or memory-bound. Others are for historical > reasons (http://tinyurl.com/6hn4zo). > > Once people were used to writing "can't fail" constructors in C++, they > often continued using that pattern in other languages, where the > underlying reasons no longer made sense. Quite possibly, they never > even knew the underlying reasons; they were taught, "Constructors must > never fail", and assumed it was a universal rule. Right, this is one of the "bad reasons" I talk about later in my message. The authors had a reason in their mind for doing it (they thought it was a universal rule); it was just a bad one. It's more useful to talk about why people thought they should follow that pattern than to just say "there is no reason to do this". > This, BTW, is one of my biggest beefs with the classic Gang Of Four > pattern book. It presents a bunch of patterns as being universally > applicable, when in reality many (if not most) of them are highly C++ > specific. > > BTW, whenever I read things like, "I think everyone agrees", I > automatically assume what the writer really meant was, "I, and all the > people who agree with me, think". Hah! Fair enough. I actually meant it to emphasize that I thought that Steven was overly reducing his statements to something that was trivially true, sacrificing content for validity. I will suggest that your interpretation of that phrase is more appropriate when the speaker is proposing something of their own rather than (partially) conceding a point. The exaggeration is only self-aggrandizing in the former case. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From dreamingforward at gmail.com Fri May 10 22:43:52 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Fri, 10 May 2013 19:43:52 -0700 Subject: object.enable() anti-pattern In-Reply-To: <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> <518be931$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c5bbc$0$29997$c3e8da3$5496439d@news.astraweb.com> <518c7f8e$0$29997$c3e8da3$5496439d@news.astraweb.com> <518cd360$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: >>>>>> There is no sensible use-case for creating a file OBJECT unless it >>>>>> initially wraps an open file pointer. >> >>> So far the only counter-examples given aren't counter-examples ... >> >> Well, sure, if you discount operations like "create this file" and >> queries like "could I delete this file if I wanted to?" [0] as methods >> of the file system rather than of a hypothetical file object. >> >> What about a distributed system? Suppose I want to create a file object >> in one place, and send that object to the another place for the file to >> be read from or written to [1]? Suppose that for security reasons, I >> have to do it that way, because the first place can only create the >> objects, and the second place can only access the underly file contents >> through an existing object? > > Unless you have re-implemented the file I/O system, it doesn't matter. If > your file objects are based on C I/O, then even if the first server > cannot read or write to the files it still creates file objects in an > open state, because that is how C works. > > Or maybe the first server only creates some sort of proxy to the real > underlying file object. Or maybe you're re-implemented the I/O system, > and aren't following C's design. Since you're just making this up as a > thought experiment, anything is possible. > > But either way, that's fine. You've found an object where it does make > sense to have an explicit "make it go" method: first one entity has > permission to construct the object, but not to open the underlying file. > Another entity has permission to open the underlying file, but not to > create the object. I have no idea whether this is a reasonable security > design or not, it actually sounds a bit rubbish to me but what do I know? > So let's treat it as a reasonable design. > > As I've said, repeatedly, that's not what I'm talking about. > > When you DON'T have useful things that can be done with the object before > calling "enable", then it is an anti-pattern to require a separate call > to "enable" method, and the enable functionality should be moved into the > object constructor. If you DO have useful things that can be done, like > pass the object to another entity, for security, then that's a whole > 'nuther story. You're missing one other case: if there's useful things that can be checked before calling enable(). Remember, on multi-user and/or multi-processing systems, there could be contention for a slow resource. If you automatically open a file for write, you're preventing everyone else from writing and potentially reading it. So there is something useful: did that file exist? Is that resource available for writing? Prior to such hi-level languages like Python and reliable hardware, such fine-grained control was important and vital. Now it can probably be relegated to special OS libraries. Mark > Really, what I'm describing is *normal* behaviour for most objects. We > don't usually design APIs like this: > > n = int("42") > n.enable() > m = n + 1 > m.enable() > x = m*2 + n*3 > print x - 1 # oops, raises because I forgot to call x.enable() Again, you only do that for shared resources. In this case, memory would have to be a (protected) shared resources, but the OS manages memory allocation, so it's not an issue. Mark > That's a rubbish API, and for simple data-like objects, we all agree it > is a rubbish API. So why accept the same rubbish API just because the > object is more complicated? I think I just told you, but let me know ..... :) > For my next controversial opinion, I'm going to argue that we should do > arithmetic using numbers rather than by inserting lists inside other > lists: Try arguing that we should have a common message-passing syntax. > # Do this: > > count = 0 > count += 1 > > # Not this: > > count = [] > count.insert(0, []) That's actually what they do in "set theory", believe it or not. MarkJ Tacoma, Washington From nobody at nowhere.com Fri May 10 12:59:26 2013 From: nobody at nowhere.com (Nobody) Date: Fri, 10 May 2013 17:59:26 +0100 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> Message-ID: On Thu, 09 May 2013 05:23:59 +0000, Steven D'Aprano wrote: > There is no sensible use-case for creating a file without opening it. > What would be the point? Any subsequent calls to just about any method > will fail. Since you have to open the file after creating the file object > anyway, why make them two different calls? As a counterpoint, some OSes (e.g. Plan 9) allow you to get a "handle" to a file without opening it. This can then be used for deleting, renaming or stat()-type operations without either the risk of race conditions (if another process renames files between operations, the operations may be performed on different files) or the side-effects of actually opening the file (particularly for device files, e.g. opening a tape drive may rewind the tape). Python's file model doesn't allow for this, so there isn't really anything meaningful that you can do on a file object which isn't open (although these actually exist; any file object on which the .close() method has been called will be in this state). However: there are situations where it is useful to be able to separate the simple task of creating an object from more invasive actions such as system calls. Particularly in multi-threaded or real-time code (although the latter is a non-starter in Python for many other reasons). From roy at panix.com Fri May 10 13:32:50 2013 From: roy at panix.com (Roy Smith) Date: Fri, 10 May 2013 13:32:50 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> Message-ID: In article , Nobody wrote: > However: there are situations where it is useful to be able to separate > the simple task of creating an object from more invasive actions such as > system calls. Particularly in multi-threaded or real-time code (although > the latter is a non-starter in Python for many other reasons). Sure. I can serialize a path name. I can't serialize an open file descriptor. From steve+comp.lang.python at pearwood.info Sat May 11 03:05:00 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 May 2013 07:05:00 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <518b32ef$0$11120$c3e8da3@news.astraweb.com> Message-ID: <518ded9c$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 10 May 2013 17:59:26 +0100, Nobody wrote: > On Thu, 09 May 2013 05:23:59 +0000, Steven D'Aprano wrote: > >> There is no sensible use-case for creating a file without opening it. >> What would be the point? Any subsequent calls to just about any method >> will fail. Since you have to open the file after creating the file >> object anyway, why make them two different calls? > > As a counterpoint, some OSes (e.g. Plan 9) allow you to get a "handle" > to a file without opening it. This can then be used for deleting, > renaming or stat()-type operations without either the risk of race > conditions (if another process renames files between operations, the > operations may be performed on different files) or the side-effects of > actually opening the file (particularly for device files, e.g. opening a > tape drive may rewind the tape). Ah, now that's a fantastic counter-example. But I think that says more about the primitiveness of the Unix file model than of the idea of temporal coupling. > Python's file model doesn't allow for this, so there isn't really > anything meaningful that you can do on a file object which isn't open > (although these actually exist; any file object on which the .close() > method has been called will be in this state). Absolutely correct, and I'm amazed it's taken this long for anyone to point this out :-) -- Steven From dreamingforward at gmail.com Wed May 8 22:53:36 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Wed, 8 May 2013 19:53:36 -0700 Subject: object.enable() anti-pattern In-Reply-To: <518a123c$0$11094$c3e8da3@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: > I'm looking for some help in finding a term, it's not Python-specific but > does apply to some Python code. > > This is an anti-pattern to avoid. The idea is that creating a resource > ought to be the same as "turning it on", or enabling it, or similar. For > example, we don't do this in Python: I would call it "user-mediated resource allocation" as distinct from "object-mediated" resource allocation. -- MarkJ Tacoma, Washington From dreamingforward at gmail.com Wed May 8 22:56:02 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Wed, 8 May 2013 19:56:02 -0700 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: >> This is an anti-pattern to avoid. The idea is that creating a resource >> ought to be the same as "turning it on", or enabling it, or similar. For >> example, we don't do this in Python: > > I would call it "user-mediated resource allocation" as distinct from > "object-mediated" resource allocation. I should point out, though, it's not really an anti-pattern (coming form the C community). -- MarkJ Tacoma, Washington From wayne at waynewerner.com Thu May 9 07:08:25 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Thu, 9 May 2013 06:08:25 -0500 (CDT) Subject: object.enable() anti-pattern In-Reply-To: <518a123c$0$11094$c3e8da3@news.astraweb.com> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: On Wed, 8 May 2013, Steven D'Aprano wrote: > I'm looking for some help in finding a term, it's not Python-specific but > does apply to some Python code. > > This is an anti-pattern to avoid. The idea is that creating a resource > ought to be the same as "turning it on", or enabling it, or similar. For > example, we don't do this in Python: I'm not entirely sure what the name of it is, but the basic concept is that you should never partially create, or create a class that can be in an unstable state. Which isn't to say you should prevent invalid input, only that with every valid input or single operation (including construction) your class should be valid. Ah, that's it - the problem is that it introduces /Temporal Coupling/ to one's code: http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling/ You don't ever want a class that has functions that need to be called in a certain order to *not* crash. That's fine if you have to call them in a certain sequence in order to get the correct data - that's what programming is all about, after all. But if you provide me a class with a constructor you better make sure that when I do this: thing = YourSuperAwesomeClass() thing.do_stuff() that I don't get some horrid stack trace ending with InvalidStateError: initialize() needs to be called before do_stuff() Or something worse. HTH, Wayne p.s. I'm interested in reading whatever is evenually written on the topic From steve+comp.lang.python at pearwood.info Thu May 9 07:51:22 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 11:51:22 GMT Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: <518b8db9$0$29997$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 May 2013 06:08:25 -0500, Wayne Werner wrote: > Ah, that's it - the problem is that it introduces /Temporal Coupling/ to > one's code: http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling/ Good catch! That's not the blog post I read, but that's the same concept. "Temporal Coupling" -- yes, that is an excellent description of the problem. > You don't ever want a class that has functions that need to be called in > a certain order to *not* crash. That's fine if you have to call them in > a certain sequence in order to get the correct data - that's what > programming is all about, after all. But if you provide me a class with > a constructor you better make sure that when I do this: > > thing = YourSuperAwesomeClass() > thing.do_stuff() > > that I don't get some horrid stack trace ending with > > InvalidStateError: initialize() needs to be called before > do_stuff() > > Or something worse. Exactly. Thank you Wayne, that's a great help. -- Steven From greg.ewing at canterbury.ac.nz Thu May 9 19:43:42 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 10 May 2013 11:43:42 +1200 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: Wayne Werner wrote: > You don't ever want a class that has functions that need to be called in > a certain order to *not* crash. That seems like an overly broad statement. What do you think the following should do? f = open("myfile.dat") f.close() data = f.read() -- Greg From knomenet at gmail.com Thu May 9 20:18:39 2013 From: knomenet at gmail.com (Michael Speer) Date: Thu, 9 May 2013 20:18:39 -0400 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: By his reasoning it simply shouldn't exist. Instead you would access the information only like this: with open("myfile.dat") as f: data = f.read() Which is my preferred way to work with resources requiring cleanup in python anyways, as it ensures I have the least chance of messing things up, and that all of my resources are disposed of properly during the unwind. It's a hell of a lot cleaner than remembering to call the appropriate cleanup functions at every callsite, and the resultant values can never be in a bad state ( at least due to programmer function-ordering fault, underlying file i/o errors and things can still cause errors, but not due to API mis-usage ). Python 3's multiple-with-statement-target syntax was backported to 2.7 as well, flattening the deep nests of with statements otherwise needed to implement this pattern http://docs.python.org/dev/whatsnew/2.7.html#other-language-changes On Thu, May 9, 2013 at 7:43 PM, Gregory Ewing wrote: > Wayne Werner wrote: > >> You don't ever want a class that has functions that need to be called in >> a certain order to *not* crash. >> > > That seems like an overly broad statement. What > do you think the following should do? > > f = open("myfile.dat") > f.close() > data = f.read() > > > -- > Greg > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Thu May 9 20:50:16 2013 From: roy at panix.com (Roy Smith) Date: Thu, 09 May 2013 20:50:16 -0400 Subject: object.enable() anti-pattern References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: In article , Michael Speer wrote: > By his reasoning it simply shouldn't exist. Instead you would access the > information only like this: > > with open("myfile.dat") as f: > data = f.read() The problem with things like file objects is they model external real-world entities, which have externally-imposed real-world behaviors. f.close() can fail, most commonly because some buffered output couldn't be written when being flushed as part of the close(). Sometimes it's important to find out about that. From wayne at waynewerner.com Sun May 12 13:14:33 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Sun, 12 May 2013 12:14:33 -0500 (CDT) Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: On Fri, 10 May 2013, Gregory Ewing wrote: > Wayne Werner wrote: >> You don't ever want a class that has functions that need to be called in a >> certain order to *not* crash. > > That seems like an overly broad statement. What > do you think the following should do? > > f = open("myfile.dat") > f.close() > data = f.read() To clarify - you don't want a class that has functions that need to be called in a certain order with *valid input* in order to not crash. Exactly what does happen - a ValueError is raised because you're(*) passing self into the file.read() function, and that input is invalid input - specifically: ValueError: I/O operation on closed file *where you actually means python, because when you call `your_instance.method()`, it works effectively like a call to `YourClass.method(your_instance)` -W From tjreedy at udel.edu Sun May 12 16:03:39 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 12 May 2013 16:03:39 -0400 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: On 5/12/2013 1:14 PM, Wayne Werner wrote: > On Fri, 10 May 2013, Gregory Ewing wrote: > >> Wayne Werner wrote: >>> You don't ever want a class that has functions that need to be called >>> in a certain order to *not* crash. >> >> That seems like an overly broad statement. What >> do you think the following should do? >> >> f = open("myfile.dat") >> f.close() >> data = f.read() > > To clarify - you don't want a class that has functions that need to be > called in a certain order with *valid input* in order to not crash. > > Exactly what does happen - a ValueError is raised because you're(*) > passing self into the file.read() function, and that input is invalid > input - specifically: > > ValueError: I/O operation on closed file > > *where you actually means python, because when you call > `your_instance.method()`, it works effectively like a call to > `YourClass.method(your_instance)` The new idiom with open("myfile.dat") as f: data = f.read() partially encapsulates operations on the opened file before the automatic close. It is true however, that after the with statement, f in still uselessly bound to the closed file. (Well, you can check the mode, encoding, and other attributes, so not totally useless.) To completely encapsulate, one can end the block with 'del f'. I checked and this works. Since the external 'as x' name binding is optional, an internal reference to the context manager (the io.xxx instance) is kept in order to call the __exit__ method, and that is not affected by deleting the optional visible binding. tjr From greg.ewing at canterbury.ac.nz Sun May 12 19:18:10 2013 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Mon, 13 May 2013 11:18:10 +1200 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Message-ID: <51902332.8090301@canterbury.ac.nz> Wayne Werner wrote: > On Fri, 10 May 2013, Gregory Ewing wrote: > >> f = open("myfile.dat") >> f.close() >> data = f.read() > > To clarify - you don't want a class that has functions that need to be > called in a certain order with *valid input* in order to not crash. > > Exactly what does happen - a ValueError is raised because you're(*) > passing self into the file.read() function, and that input is invalid The same argument can be applied to: foo = Foo() foo.do_something() foo.enable() # should have done this first You're passing an invalid input to Foo.do_something, namely a Foo that hasn't been enabled yet. -- Greg From fabiosantosart at gmail.com Mon May 13 02:32:36 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 13 May 2013 07:32:36 +0100 Subject: object.enable() anti-pattern In-Reply-To: <51902332.8090301@canterbury.ac.nz> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <51902332.8090301@canterbury.ac.nz> Message-ID: On 13 May 2013 00:22, "Greg Ewing" wrote: > > Wayne Werner wrote: >> >> On Fri, 10 May 2013, Gregory Ewing wrote: >> >>> f = open("myfile.dat") >>> f.close() >>> data = f.read() >> >> >> To clarify - you don't want a class that has functions that need to be called in a certain order with *valid input* in order to not crash. >> >> Exactly what does happen - a ValueError is raised because you're(*) passing self into the file.read() function, and that input is invalid > > > The same argument can be applied to: > > foo = Foo() > foo.do_something() > foo.enable() # should have done this first > > You're passing an invalid input to Foo.do_something, > namely a Foo that hasn't been enabled yet. > > > -- > Greg > -- > http://mail.python.org/mailman/listinfo/python-list I don't think you can really count that as invalid input in OOP terms. After all in most languages `self` / `this` / whatever is not an argument to every method. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon May 13 03:36:41 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 17:36:41 +1000 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <51902332.8090301@canterbury.ac.nz> Message-ID: On Mon, May 13, 2013 at 4:32 PM, F?bio Santos wrote: > > On 13 May 2013 00:22, "Greg Ewing" wrote: >> The same argument can be applied to: >> >> foo = Foo() >> foo.do_something() >> foo.enable() # should have done this first >> >> You're passing an invalid input to Foo.do_something, >> namely a Foo that hasn't been enabled yet. > > I don't think you can really count that as invalid input in OOP terms. After > all in most languages `self` / `this` / whatever is not an argument to every > method. Yes, it is; it's just often implicit. C++ lets you poke around with the internals, and it's pretty clear that 'this' is an argument. (See for instance what happens with the gcc 'format' attribute - I can't find a convenient docs page, but it's been mentioned on SO [1] and can be easily verified.) EMCAScript lets you call any function with any 'this' by using the .call() or .apply() methods - which, in my extremely not-humble opinionated opinion, is bad design (closures work implicitly, but the 'this' pointer doesn't??). Python turns an attribute lookup on an instance into an attribute lookup on the class plus a currying. One way or another, the bit-before-the-dot is an argument to the function. [1] http://stackoverflow.com/questions/11621043/how-should-i-properly-use-attribute-format-printf-x-y-inside-a-class ChrisA From fabiosantosart at gmail.com Mon May 13 04:09:37 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 13 May 2013 09:09:37 +0100 Subject: object.enable() anti-pattern In-Reply-To: References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <51902332.8090301@canterbury.ac.nz> Message-ID: On 13 May 2013 08:40, "Chris Angelico" wrote: > > On Mon, May 13, 2013 at 4:32 PM, F?bio Santos wrote: > > > > On 13 May 2013 00:22, "Greg Ewing" wrote: > >> The same argument can be applied to: > >> > >> foo = Foo() > >> foo.do_something() > >> foo.enable() # should have done this first > >> > >> You're passing an invalid input to Foo.do_something, > >> namely a Foo that hasn't been enabled yet. > > > > I don't think you can really count that as invalid input in OOP terms. After > > all in most languages `self` / `this` / whatever is not an argument to every > > method. > > Yes, it is; it's just often implicit. C++ lets you poke around with > the internals, and it's pretty clear that 'this' is an argument. (See > for instance what happens with the gcc 'format' attribute - I can't > find a convenient docs page, but it's been mentioned on SO [1] and can > be easily verified.) EMCAScript lets you call any function with any > 'this' by using the .call() or .apply() methods - which, in my > extremely not-humble opinionated opinion, is bad design (closures work > implicitly, but the 'this' pointer doesn't??). Python turns an > attribute lookup on an instance into an attribute lookup on the class > plus a currying. One way or another, the bit-before-the-dot is an > argument to the function. > > [1] http://stackoverflow.com/questions/11621043/how-should-i-properly-use-attribute-format-printf-x-y-inside-a-class > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list I know ECMAScript does that. It would be nice to be able to pass an instance method as a callback argument without using `.bind(theInstance)`. At any rate, exposed or not, that is all still internals. Exposing the ability to set the ` this ` as an argument is, I think, a functional feature (map(str.strip, file) is a good example) That said, I didn't know c++ did that, but it makes sense with what I read somewhere about c++ starting out as a transcompiler-to-c-based language. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wayne at waynewerner.com Mon May 13 09:15:07 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Mon, 13 May 2013 08:15:07 -0500 (CDT) Subject: object.enable() anti-pattern In-Reply-To: <51902332.8090301@canterbury.ac.nz> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <51902332.8090301@canterbury.ac.nz> Message-ID: On Mon, 13 May 2013, Greg Ewing wrote: > Wayne Werner wrote: >> On Fri, 10 May 2013, Gregory Ewing wrote: >> >>> f = open("myfile.dat") >>> f.close() >>> data = f.read() >> >> To clarify - you don't want a class that has functions that need to be >> called in a certain order with *valid input* in order to not crash. >> >> Exactly what does happen - a ValueError is raised because you're(*) passing >> self into the file.read() function, and that input is invalid > > The same argument can be applied to: > > foo = Foo() > foo.do_something() > foo.enable() # should have done this first > > You're passing an invalid input to Foo.do_something, > namely a Foo that hasn't been enabled yet. That is the crux of the argument - as designer of the class *you* need to ensure that when your constructor is done, your class is in a stable state. And that every other state transition (with valid input) results in your class then being in a stable state. If anything, the stronger argument is that `file.close()` is not a well designed function because it leaves your object in an unstable state. Which I would be inclined to agree with, but I couldn't give you the answer for what makes it better. Because the answer is the best one you can get in computer science: It depends. The reason that it depends, is because it depends on what you want to do. Do you want a program that seems purely functional? Do you want a program that's easy to maintain? Do you want a program that more accurately models the "real world"? Personally, I think the file object API in Python is about as good as it can get - but that's because it's working with "physical" things (i.e. files - bits on a platter, or flash/SSD drive...) which necessarily have a temporal nature. And it's much less badness to blow up on a call to `read` than it is to remove the `read` function and die with a NameError when the underlying file is in a closed state. At least in my opinion ;) -W From breamoreboy at yahoo.co.uk Wed May 8 05:39:50 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 08 May 2013 10:39:50 +0100 Subject: cello library Message-ID: Hi folks, I thought some of you might find this interesting http://libcello.org/ -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From rosuav at gmail.com Wed May 8 06:01:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 8 May 2013 20:01:56 +1000 Subject: cello library In-Reply-To: References: Message-ID: On Wed, May 8, 2013 at 7:39 PM, Mark Lawrence wrote: > Hi folks, > > I thought some of you might find this interesting [link redacted] If this is a legit post, can you please elaborate on just _why_ we would find it interesting? I'm leery of clicking random links like that without at least some indication. It could be some cool Python library, or it could be something relating to music, or it could be a malicious site ready to compromise my browser and your Yahoo account was compromised to send spam. ChrisA From tjreedy at udel.edu Wed May 8 17:06:31 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Wed, 08 May 2013 17:06:31 -0400 Subject: cello library In-Reply-To: References: Message-ID: On 5/8/2013 6:01 AM, Chris Angelico wrote: > On Wed, May 8, 2013 at 7:39 PM, Mark Lawrence wrote: >> Hi folks, >> >> I thought some of you might find this interesting [link redacted] Unredacted: http://libcello.org/ > If this is a legit post, can you please elaborate on just _why_ we > would find it interesting? I'm leery of clicking random links like > that without at least some indication. It could be some cool Python > library, or it could be something relating to music, or it could be a > malicious site ready to compromise my browser and your Yahoo account > was compromised to send spam. Legitimate request, like some I have made of others. Since I trust Mark: "Cello is a GNU99 C library which brings higher level programming to C. Interfaces allow for structured design Duck Typing allows for generic functions Exceptions control error handling Constructors/Destructors aid memory management Syntactic Sugar increases readability C Library means excellent performance and integration ...Cello is licensed under BSD3." Partly inspired by Python, including this: /* "with" automatically closes file at end of scope. */ with (file in open($(File, NULL), "prices.bin", "wb"))) { ... } An interesting question is whether it could be used to convert or rewrite Python to C. __ Terry Jan Reedy From rosuav at gmail.com Wed May 8 20:08:24 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 May 2013 10:08:24 +1000 Subject: cello library In-Reply-To: References: Message-ID: On Thu, May 9, 2013 at 7:06 AM, Terry Jan Reedy wrote: > Legitimate request, like some I have made of others. Since I trust Mark: It's actually nothing to do with trusting Mark. I've often seen posts from addresses of people I trust, but with links that I definitely would not want to click... and Mark posts from Yahoo, which is one of the common targets of that sort of attack. Anyway, a little context would have solved the whole thing. ChrisA From samjnaa at gmail.com Wed May 8 07:19:07 2013 From: samjnaa at gmail.com (jamadagni) Date: Wed, 8 May 2013 04:19:07 -0700 (PDT) Subject: Help with implementing callback functions using ctypes Message-ID: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com> Hello. I am running Kubuntu Raring with Python 3.3.1. (I mostly don't myself use Py2.) I have the below C program spiro.c (obviously a simplified testcase) which I compile to a sharedlib using clang -fPIC -shared -o libspiro.so spiro.c, sudo cp to /usr/lib and am trying to call from a Python script spiro.py using ctypes. However it would seem that the first call to the callback results in a segfault. (The line before the callback is executed.) I tried debugging using gdb but perhaps because of my insufficient knowledge I couldn't find out what is causing the problem. I hope some of the kind knowledgeable souls here can help me out: -- spiro.c # include # include typedef struct _bezctx bezctx ; struct _bezctx { void ( * moveto ) ( bezctx * bc, double x, double y, bool starts_open_path ) ; void ( * lineto ) ( bezctx * bc, double x, double y ) ; void ( * curveto ) ( bezctx * bc, double c1x, double c1y, double c2x, double c2y, double x, double y ) ; void ( * track_seg ) ( bezctx * bc, unsigned int start_node_idx ) ; } ; typedef struct { double x, y ; char ty ; } spiro_cp ; bool spiro_to_bezier_strict ( const spiro_cp src [], int src_len, bezctx * bc ) { for ( int i = 0 ; i < src_len ; ++i ) { const spiro_cp * p = &(src[i]) ; printf ( "%g %g %c\n", p->x, p->y, p->ty ) ; if ( i % 2 == 0 ) bc -> moveto ( bc, p->x, p->y, i % 3 ) ; else bc -> lineto ( bc, p->x, p->y ) ; bc -> curveto ( bc, 233, 150, 267, 150, 300, 100 ) ; bc -> track_seg ( bc, i ) ; } return src_len % 2 == 0 ; } -- spiro.py from ctypes import * # incomplete struct definition needed for pointer below class bezctx ( Structure ) : pass # pointer to function types for callback MoveToFnType = CFUNCTYPE ( None, POINTER ( bezctx ), c_double, c_double, c_bool ) LineToFnType = CFUNCTYPE ( None, POINTER ( bezctx ), c_double, c_double ) CurveToFnType = CFUNCTYPE ( None, POINTER ( bezctx ), c_double, c_double, c_double, c_double, c_double, c_double ) TrackSegmentFnType = CFUNCTYPE ( None, POINTER ( bezctx ), c_uint ) # completing the struct definition bezctx._fields_ = [ ( "moveto" , MoveToFnType ), ( "lineto" , LineToFnType ), ( "curveto" , CurveToFnType ), ( "track_seg", TrackSegmentFnType ) ] # the python callbacks def moveTo ( bc, x, y, startsOpenPath ) : print ( "moveTo", bc, x, y, startsOpenPath ) def lineTo ( bc, x, y ) : print ( "lineTo", bc, x, y ) def curveTo ( bc, c1x, c1y, c2x, c2y, x, y ) : print ( "curveTo", bc, c1x, c1y, c2x, c2y, x, y ) def trackSegment ( bc, start_node_idx ) : print ( "trackSegment", bc, start_node_idx ) # instance of struct bc = bezctx () bc . moveto = MoveToFnType ( moveTo ) bc . lineto = LineToFnType ( lineTo ) bc . curveto = CurveToFnType ( curveTo ) bc . track_seg = TrackSegmentFnType ( trackSegment ) # another struct class spiro_cp ( Structure ) : _fields_ = [ ( "x", c_double ), ( "y", c_double ), ( "ty", c_char ) ] # array of struct instances srclist = [ spiro_cp ( 147, 215, b'o' ), spiro_cp ( 168, 136, b'o' ), spiro_cp ( 294, 184, b'[' ), spiro_cp ( 381, 136, b']' ), spiro_cp ( 445, 204, b'c' ), spiro_cp ( 364, 235, b'c' ), spiro_cp ( 266, 285, b'v' ) ] src = ( spiro_cp * len ( srclist ) ) ( *srclist ) # open the sharedlib libspiro = CDLL ( "libspiro.so" ) spiro_to_bezier_strict = libspiro.spiro_to_bezier_strict # call the C function spiro_to_bezier_strict ( src, len ( src ), bc ) -- bash session $ python3 spiro-ctypes.py 147 215 o Segmentation fault (core dumped) From dieter at handshake.de Thu May 9 01:54:57 2013 From: dieter at handshake.de (dieter) Date: Thu, 09 May 2013 07:54:57 +0200 Subject: Help with implementing callback functions using ctypes References: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com> Message-ID: <87mws4bsum.fsf@handshake.de> jamadagni writes: > ... I cannot help you with "ctypes". But, if you might be able to use "cython", then calling callbacks is not too difficult (you can find an example in e.g. my "dm.xmlsec.binding"). Note, however, that properly handling the GIL ("Global Interpreter Lock") may be of great importance when the Python/C boundary is crossed -- at least when you intend to use your code in a multi thread environment. From stefan_ml at behnel.de Thu May 9 02:57:55 2013 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 09 May 2013 08:57:55 +0200 Subject: Help with implementing callback functions using ctypes In-Reply-To: <87mws4bsum.fsf@handshake.de> References: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com> <87mws4bsum.fsf@handshake.de> Message-ID: dieter, 09.05.2013 07:54: > jamadagni writes: >> ... > I cannot help you with "ctypes". But, if you might be able to use > "cython", then calling callbacks is not too difficult +1 for using Cython. It also has (multi-)source level gdb support, which greatly helps in debugging crashes like this one. http://docs.cython.org/src/userguide/debugging.html > (you can find an example in e.g. my "dm.xmlsec.binding"). An "official" example is here: https://github.com/cython/cython/tree/master/Demos/callback > Note, however, that properly handling the GIL ("Global Interpreter Lock") > may be of great importance when the Python/C boundary is crossed -- > at least when you intend to use your code in a multi thread environment. Cython makes this really easy. You can use the "with" statement to release the GIL when you don't need it, and the compiler will produce errors when you try to do things that require the GIL while you don't own it. Stefan From drsalists at gmail.com Thu May 23 23:26:28 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Thu, 23 May 2013 20:26:28 -0700 Subject: Help with implementing callback functions using ctypes In-Reply-To: <87mws4bsum.fsf@handshake.de> References: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com> <87mws4bsum.fsf@handshake.de> Message-ID: On Wed, May 8, 2013 at 10:54 PM, dieter wrote: > jamadagni writes: > > ... > I cannot help you with "ctypes". But, if you might be able to use > "cython", then calling callbacks is not too difficult > (you can find an example in e.g. my "dm.xmlsec.binding"). > > Note, however, that properly handling the GIL ("Global Interpreter Lock") > may be of great importance when the Python/C boundary is crossed -- > at least when you intend to use your code in a multi thread environment. > Cython is good. So is the new cffi, which might be thought of as a safer (API-level) version of ctypes (which is ABI-level). -------------- next part -------------- An HTML attachment was scrubbed... URL: From samjnaa at gmail.com Sun May 26 12:12:10 2013 From: samjnaa at gmail.com (Shriramana Sharma) Date: Sun, 26 May 2013 09:12:10 -0700 (PDT) Subject: Help with implementing callback functions using ctypes In-Reply-To: References: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com> <87mws4bsum.fsf@handshake.de> Message-ID: <8de1876e-c157-4012-9921-b981e079327d@googlegroups.com> On Friday, May 24, 2013 8:56:28 AM UTC+5:30, Dan Stromberg wrote: > Cython is good. So is the new cffi, which might be thought of as a > safer (API-level) version of ctypes (which is ABI-level). Hi -- can you clarify what is this new CFFI and where I can get it? In the Python 3 library reference I seem to see only CTypes. From carlosnepomuceno at outlook.com Sun May 26 14:43:28 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 21:43:28 +0300 Subject: Help with implementing callback functions using ctypes In-Reply-To: <8de1876e-c157-4012-9921-b981e079327d@googlegroups.com> References: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com>, <87mws4bsum.fsf@handshake.de>, , <8de1876e-c157-4012-9921-b981e079327d@googlegroups.com> Message-ID: https://cffi.readthedocs.org/en/release-0.6/ ---------------------------------------- > Date: Sun, 26 May 2013 09:12:10 -0700 > Subject: Re: Help with implementing callback functions using ctypes > From: samjnaa at gmail.com > To: python-list at python.org > > On Friday, May 24, 2013 8:56:28 AM UTC+5:30, Dan Stromberg wrote: >> Cython is good. So is the new cffi, which might be thought of as a >> safer (API-level) version of ctypes (which is ABI-level). > > Hi -- can you clarify what is this new CFFI and where I can get it? In the Python 3 library reference I seem to see only CTypes. > -- > http://mail.python.org/mailman/listinfo/python-list From drsalists at gmail.com Sun May 26 15:10:14 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Sun, 26 May 2013 12:10:14 -0700 Subject: Help with implementing callback functions using ctypes In-Reply-To: <8de1876e-c157-4012-9921-b981e079327d@googlegroups.com> References: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com> <87mws4bsum.fsf@handshake.de> <8de1876e-c157-4012-9921-b981e079327d@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 9:12 AM, Shriramana Sharma wrote: > On Friday, May 24, 2013 8:56:28 AM UTC+5:30, Dan Stromberg wrote: > > Cython is good. So is the new cffi, which might be thought of as a > > safer (API-level) version of ctypes (which is ABI-level). > > Hi -- can you clarify what is this new CFFI and where I can get it? In the > Python 3 library reference I seem to see only CTypes. > CFFI is like CTypes, but it allows you to do things like see C preprocessor symbols. https://cffi.readthedocs.org/en/release-0.6/ I don't think it comes with CPython, at least not yet. But I believe it comes with the latest Pypy. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nobody at nowhere.com Thu May 9 14:32:08 2013 From: nobody at nowhere.com (Nobody) Date: Thu, 09 May 2013 19:32:08 +0100 Subject: Help with implementing callback functions using ctypes References: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com> Message-ID: On Wed, 08 May 2013 04:19:07 -0700, jamadagni wrote: > I have the below C program spiro.c (obviously a simplified testcase) > which I compile to a sharedlib using clang -fPIC -shared -o libspiro.so > spiro.c, sudo cp to /usr/lib and am trying to call from a Python script > spiro.py using ctypes. However it would seem that the first call to the > callback results in a segfault. > # call the C function > spiro_to_bezier_strict ( src, len ( src ), bc ) This line should be: spiro_to_bezier_strict ( src, len ( src ), byref(bc) ) Without the byref(...), it will try to pass a copy of the structure rather than passing a pointer to it. From samjnaa at gmail.com Thu May 23 23:11:40 2013 From: samjnaa at gmail.com (jamadagni) Date: Thu, 23 May 2013 20:11:40 -0700 (PDT) Subject: Help with implementing callback functions using ctypes In-Reply-To: References: <8931a89c-0d00-4c8b-ba19-cf683d8d266d@googlegroups.com> Message-ID: <4dd3853f-476b-47e7-9a20-d2752ca5cde1@googlegroups.com> On Friday, May 10, 2013 12:02:08 AM UTC+5:30, Nobody wrote: > This line should be: > spiro_to_bezier_strict ( src, len ( src ), byref(bc) ) > Without the byref(...), it will try to pass a copy of the structure rather > than passing a pointer to it. Wow silly silly mistake of mine, passing an object where a pointer is expected. Alternatively, if I specify: spro_to_bezier_strict.argtypes = [ POINTER(spiro_cp), c_int, POINTER(bezctx) ] it works equally good and apparently automatically converts the object specified to a pointer to it (which is good and intuitive, I guess, though against C practice). I guess if one wants to be safe when interfacing to C functions, one should always specify argtypes (even if not restype). But really, a segfault? I would expect a more meaningful error or an exception to be raised. Or is the black magic occurring at a level lower than where Python's control goes? If I had seen this post I might not have even spent time on Cython. (For some reason Google Groups doesn't send me email updates even if I ask it to.) Using CTYpes I was able to implement a callback with less coding (for this particular requirement) than Cython which required an extra wrapper layer on top of the library to pass through a void* representing the Python callback (or a state variable would have to be used which may mean no thread-safety). But I guess Cython would be faster for those who want that. Even though there is an extra function call every time (for the wrapper) it would be faster since it's at the C level rather than at the Python level (or something like that). Thanks to all who helped on this, and also the people over at the cython-users list who helped me on the Cython wrapper too. From roy at panix.com Wed May 8 09:19:13 2013 From: roy at panix.com (Roy Smith) Date: Wed, 08 May 2013 09:19:13 -0400 Subject: Unicode humor Message-ID: Apropos to any of the myriad unicode threads that have been going on recently: http://xkcd.com/1209/ From svenito at gmail.com Wed May 8 10:30:22 2013 From: svenito at gmail.com (Sven) Date: Wed, 8 May 2013 15:30:22 +0100 Subject: Unicode humor In-Reply-To: References: Message-ID: I saw this this morning and the first thing I thought of was this list... unlike you I was too lazy to post it :) On 8 May 2013 14:19, Roy Smith wrote: > Apropos to any of the myriad unicode threads that have been going on > recently: > > http://xkcd.com/1209/ > -- > http://mail.python.org/mailman/listinfo/python-list > -- ./Sven -------------- next part -------------- An HTML attachment was scrubbed... URL: From auriocus at gmx.de Wed May 8 13:16:03 2013 From: auriocus at gmx.de (Christian Gollwitzer) Date: Wed, 08 May 2013 19:16:03 +0200 Subject: Unicode humor In-Reply-To: References: Message-ID: Am 08.05.13 15:19, schrieb Roy Smith: > Apropos to any of the myriad unicode threads that have been going on > recently: > > http://xkcd.com/1209/ > http://xkcd.com/1137/ From wxjmfauth at gmail.com Fri May 10 11:06:00 2013 From: wxjmfauth at gmail.com (jmfauth) Date: Fri, 10 May 2013 08:06:00 -0700 (PDT) Subject: Unicode humor References: Message-ID: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> On 8 mai, 15:19, Roy Smith wrote: > Apropos to any of the myriad unicode threads that have been going on > recently: > > http://xkcd.com/1209/ ------ This reflects a lack of understanding of Unicode. jmf From rosuav at gmail.com Fri May 10 11:16:36 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 May 2013 01:16:36 +1000 Subject: Unicode humor In-Reply-To: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> Message-ID: On Sat, May 11, 2013 at 1:06 AM, jmfauth wrote: > On 8 mai, 15:19, Roy Smith wrote: >> Apropos to any of the myriad unicode threads that have been going on >> recently: >> >> http://xkcd.com/1209/ > > ------ > > > This reflects a lack of understanding of Unicode. By the skywriter, or by the two on the ground, or by Randall Munroe? Or by jmf? ChrisA From ned at nedbatchelder.com Fri May 10 11:24:36 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Fri, 10 May 2013 11:24:36 -0400 Subject: Unicode humor In-Reply-To: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> Message-ID: <518D1134.40708@nedbatchelder.com> On 5/10/2013 11:06 AM, jmfauth wrote: > On 8 mai, 15:19, Roy Smith wrote: >> Apropos to any of the myriad unicode threads that have been going on >> recently: >> >> http://xkcd.com/1209/ > ------ > > > This reflects a lack of understanding of Unicode. > > jmf And this reflects a lack of a sense of humor. :) --Ned. From rosuav at gmail.com Fri May 10 11:32:13 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 May 2013 01:32:13 +1000 Subject: Unicode humor In-Reply-To: <518D1134.40708@nedbatchelder.com> References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> <518D1134.40708@nedbatchelder.com> Message-ID: On Sat, May 11, 2013 at 1:24 AM, Ned Batchelder wrote: > On 5/10/2013 11:06 AM, jmfauth wrote: >> >> On 8 mai, 15:19, Roy Smith wrote: >>> >>> Apropos to any of the myriad unicode threads that have been going on >>> recently: >>> >>> http://xkcd.com/1209/ >> >> ------ >> >> >> This reflects a lack of understanding of Unicode. >> >> jmf > > > And this reflects a lack of a sense of humor. :) Isn't that a crime in the UK? ChrisA From rustompmody at gmail.com Fri May 10 12:07:45 2013 From: rustompmody at gmail.com (rusi) Date: Fri, 10 May 2013 09:07:45 -0700 (PDT) Subject: Unicode humor References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> <518D1134.40708@nedbatchelder.com> Message-ID: <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> On May 10, 8:32?pm, Chris Angelico wrote: > On Sat, May 11, 2013 at 1:24 AM, Ned Batchelder wrote: > > On 5/10/2013 11:06 AM, jmfauth wrote: > > >> On 8 mai, 15:19, Roy Smith wrote: > > >>> Apropos to any of the myriad unicode threads that have been going on > >>> recently: > > >>>http://xkcd.com/1209/ > > >> ------ > > >> This reflects a lack of understanding of Unicode. > > >> jmf > > > And this reflects a lack of a sense of humor. ?:) > > Isn't that a crime in the UK? > > ChrisA The problem with English humour (as against standard humor) is that its not unicode compliant From python at mrabarnett.plus.com Fri May 10 12:24:52 2013 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 10 May 2013 17:24:52 +0100 Subject: Unicode humor In-Reply-To: <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> <518D1134.40708@nedbatchelder.com> <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> Message-ID: <518D1F54.8070900@mrabarnett.plus.com> On 10/05/2013 17:07, rusi wrote: > On May 10, 8:32 pm, Chris Angelico wrote: >> On Sat, May 11, 2013 at 1:24 AM, Ned Batchelder wrote: >> > On 5/10/2013 11:06 AM, jmfauth wrote: >> >> >> On 8 mai, 15:19, Roy Smith wrote: >> >> >>> Apropos to any of the myriad unicode threads that have been going on >> >>> recently: >> >> >>>http://xkcd.com/1209/ >> >> >> ------ >> >> >> This reflects a lack of understanding of Unicode. >> >> >> jmf >> >> > And this reflects a lack of a sense of humor. :) >> >> Isn't that a crime in the UK? >> >> ChrisA > > The problem with English humour (as against standard humor) is that > its not unicode compliant > British humour includes "double entendre", which is not French-compliant. From jeanmichel at sequans.com Wed May 15 09:19:00 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 May 2013 15:19:00 +0200 (CEST) Subject: Unicode humor In-Reply-To: <518D1F54.8070900@mrabarnett.plus.com> Message-ID: <1988041148.13863452.1368623940251.JavaMail.root@sequans.com> > >> >> This reflects a lack of understanding of Unicode. > >> > >> >> jmf > >> > >> > And this reflects a lack of a sense of humor. :) > >> > >> Isn't that a crime in the UK? > >> > >> ChrisA > > > > The problem with English humour (as against standard humor) is that > > its not unicode compliant > > > British humour includes "double entendre", which is not > French-compliant. I didn't get that one. Which possibly confirm MRAB's statement. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From darcy at druid.net Wed May 15 10:09:59 2013 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Wed, 15 May 2013 10:09:59 -0400 Subject: Unicode humor In-Reply-To: <1988041148.13863452.1368623940251.JavaMail.root@sequans.com> References: <518D1F54.8070900@mrabarnett.plus.com> <1988041148.13863452.1368623940251.JavaMail.root@sequans.com> Message-ID: <20130515100959.44d8e334@imp> On Wed, 15 May 2013 15:19:00 +0200 (CEST) Jean-Michel Pichavant wrote: > > British humour includes "double entendre", which is not > > French-compliant. > > I didn't get that one. Which possibly confirm MRAB's statement. It's sort of like when Bush said "The French don't even have a word for entrepreneur." -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 788 2246 (DoD#0082) (eNTP) | what's for dinner. IM: darcy at Vex.Net, VOIP: sip:darcy at Vex.Net From jeanmichel at sequans.com Wed May 15 13:26:39 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 May 2013 19:26:39 +0200 (CEST) Subject: Unicode humor In-Reply-To: <20130515100959.44d8e334@imp> Message-ID: <49316535.14184594.1368638799329.JavaMail.root@sequans.com> ----- Original Message ----- > On Wed, 15 May 2013 15:19:00 +0200 (CEST) > Jean-Michel Pichavant wrote: > > > British humour includes "double entendre", which is not > > > French-compliant. > > > > I didn't get that one. Which possibly confirm MRAB's statement. > > It's sort of like when Bush said "The French don't even have a word > for > entrepreneur." > > -- > D'Arcy J.M. Cain | Democracy is three > wolves > http://www.druid.net/darcy/ | and a sheep voting on > +1 416 788 2246 (DoD#0082) (eNTP) | what's for dinner. > IM: darcy at Vex.Net, VOIP: sip:darcy at Vex.Net Nice one (to MRAB's credit his humor was intentional :o) ) JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From python at mrabarnett.plus.com Wed May 15 10:23:19 2013 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 15 May 2013 15:23:19 +0100 Subject: Unicode humor In-Reply-To: <1988041148.13863452.1368623940251.JavaMail.root@sequans.com> References: <1988041148.13863452.1368623940251.JavaMail.root@sequans.com> Message-ID: <51939A57.6000607@mrabarnett.plus.com> On 15/05/2013 14:19, Jean-Michel Pichavant wrote: >> >> >> This reflects a lack of understanding of Unicode. >> >> >> >> >> jmf >> >> >> >> > And this reflects a lack of a sense of humor. :) >> >> >> >> Isn't that a crime in the UK? >> >> >> >> ChrisA >> > >> > The problem with English humour (as against standard humor) is that >> > its not unicode compliant >> > >> British humour includes "double entendre", which is not >> French-compliant. > > I didn't get that one. Which possibly confirm MRAB's statement. > It's called "double entendre" in English (using French words, from "? double entente"), but that isn't correct French ("double sens"). From jeanmichel at sequans.com Wed May 15 13:04:50 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 May 2013 19:04:50 +0200 (CEST) Subject: Unicode humor In-Reply-To: <51939A57.6000607@mrabarnett.plus.com> Message-ID: <1389756472.14167743.1368637490516.JavaMail.root@sequans.com> ----- Original Message ----- > On 15/05/2013 14:19, Jean-Michel Pichavant wrote: > >> >> >> This reflects a lack of understanding of Unicode. > >> >> > >> >> >> jmf > >> >> > >> >> > And this reflects a lack of a sense of humor. :) > >> >> > >> >> Isn't that a crime in the UK? > >> >> > >> >> ChrisA > >> > > >> > The problem with English humour (as against standard humor) is > >> > that > >> > its not unicode compliant > >> > > >> British humour includes "double entendre", which is not > >> French-compliant. > > > > I didn't get that one. Which possibly confirm MRAB's statement. > > > It's called "double entendre" in English (using French words, from "? > double entente"), but that isn't correct French ("double sens"). Thanks for clarifying, I didn't know "double entendre" had actually a meaning in english, it's obviously 2 french words but this is the first time I see them used together. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From python at mrabarnett.plus.com Wed May 15 13:21:01 2013 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 15 May 2013 18:21:01 +0100 Subject: Unicode humor In-Reply-To: <1389756472.14167743.1368637490516.JavaMail.root@sequans.com> References: <1389756472.14167743.1368637490516.JavaMail.root@sequans.com> Message-ID: <5193C3FD.2040304@mrabarnett.plus.com> On 15/05/2013 18:04, Jean-Michel Pichavant wrote: > ----- Original Message ----- >> On 15/05/2013 14:19, Jean-Michel Pichavant wrote: >>>>>>>> This reflects a lack of understanding of Unicode. >>>>>> >>>>>>>> jmf >>>>>> >>>>>>> And this reflects a lack of a sense of humor. :) >>>>>> >>>>>> Isn't that a crime in the UK? >>>>>> >>>>>> ChrisA >>>>> >>>>> The problem with English humour (as against standard humor) >>>>> is that its not unicode compliant >>>>> >>>> British humour includes "double entendre", which is not >>>> French-compliant. >>> >>> I didn't get that one. Which possibly confirm MRAB's statement. >>> >> It's called "double entendre" in English (using French words, from >> "? double entente"), but that isn't correct French ("double >> sens"). > > Thanks for clarifying, I didn't know "double entendre" had actually a > meaning in english, it's obviously 2 french words but this is the > first time I see them used together. > Occasionally speakers of one language will borrow a word or phrase from another language and use it in a way a native speaker wouldn't (or even understand). From cjw at ncf.ca Wed May 15 15:37:21 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Wed, 15 May 2013 15:37:21 -0400 Subject: Unicode humor In-Reply-To: References: <1389756472.14167743.1368637490516.JavaMail.root@sequans.com> Message-ID: On 15/05/2013 1:21 PM, MRAB wrote: > On 15/05/2013 18:04, Jean-Michel Pichavant wrote: >> ----- Original Message ----- >>> On 15/05/2013 14:19, Jean-Michel Pichavant wrote: >>>>>>>>> This reflects a lack of understanding of Unicode. >>>>>>> >>>>>>>>> jmf >>>>>>> >>>>>>>> And this reflects a lack of a sense of humor. :) >>>>>>> >>>>>>> Isn't that a crime in the UK? >>>>>>> >>>>>>> ChrisA >>>>>> >>>>>> The problem with English humour (as against standard humor) >>>>>> is that its not unicode compliant >>>>>> >>>>> British humour includes "double entendre", which is not >>>>> French-compliant. >>>> >>>> I didn't get that one. Which possibly confirm MRAB's statement. >>>> >>> It's called "double entendre" in English (using French words, from >>> "? double entente"), but that isn't correct French ("double >>> sens"). >> >> Thanks for clarifying, I didn't know "double entendre" had actually a >> meaning in english, it's obviously 2 french words but this is the >> first time I see them used together. >> > Occasionally speakers of one language will borrow a word or phrase from > another language and use it in a way a native speaker wouldn't (or even > understand). double-entendre - Chambers Fails, but see Wiktionary: http://en.wiktionary.org/wiki/double_entendre Colin W. From wuwei23 at gmail.com Wed May 15 19:57:13 2013 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 May 2013 16:57:13 -0700 (PDT) Subject: Unicode humor References: <518D1F54.8070900@mrabarnett.plus.com> <1988041148.13863452.1368623940251.JavaMail.root@sequans.com> Message-ID: On May 16, 12:09?am, "D'Arcy J.M. Cain" wrote: > It's sort of like when Bush said "The French don't even have a word for > entrepreneur." Or "The Russians have no word for it, therefore detente must be a one- way street." From ethan at stoneleaf.us Fri May 10 12:26:13 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 10 May 2013 09:26:13 -0700 Subject: Unicode humor In-Reply-To: <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> <518D1134.40708@nedbatchelder.com> <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> Message-ID: <518D1FA5.2080104@stoneleaf.us> On 05/10/2013 09:07 AM, rusi wrote: > On May 10, 8:32 pm, Chris Angelico wrote: >> On Sat, May 11, 2013 at 1:24 AM, Ned Batchelder wrote: >>> On 5/10/2013 11:06 AM, jmfauth wrote: >> >>>> On 8 mai, 15:19, Roy Smith wrote: >> >>>>> Apropos to any of the myriad unicode threads that have been going on >>>>> recently: >> >>>>> http://xkcd.com/1209/ >> >>>> ------ >> >>>> This reflects a lack of understanding of Unicode. >> >>>> jmf >> >>> And this reflects a lack of a sense of humor. :) >> >> Isn't that a crime in the UK? >> >> ChrisA > > The problem with English humour (as against standard humor) is that > its not unicode compliant Is so! It fits inside the first 127 code points!! As a bonus it also takes less brain power^H^H^H^H^H^H^H^H^H space. ;) -- ~Ethan~ From rosuav at gmail.com Fri May 10 12:25:46 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 May 2013 02:25:46 +1000 Subject: Unicode humor In-Reply-To: <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> <518D1134.40708@nedbatchelder.com> <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> Message-ID: On Sat, May 11, 2013 at 2:07 AM, rusi wrote: > On May 10, 8:32 pm, Chris Angelico wrote: >> On Sat, May 11, 2013 at 1:24 AM, Ned Batchelder wrote: >> > On 5/10/2013 11:06 AM, jmfauth wrote: >> >> >> On 8 mai, 15:19, Roy Smith wrote: >> >> >>> Apropos to any of the myriad unicode threads that have been going on >> >>> recently: >> >> >>>http://xkcd.com/1209/ >> >> >> ------ >> >> >> This reflects a lack of understanding of Unicode. >> >> >> jmf >> >> > And this reflects a lack of a sense of humor. :) >> >> Isn't that a crime in the UK? >> >> ChrisA > > The problem with English humour (as against standard humor) is that > its not unicode compliant Unicode humour was carefully laid out to incorporate English humour. In fact, if you use the standard variable-length-joke encoding, it's possible for a Unicode joke to be decoded as if it were an English joke, without any actual knowledge of Unicode. Unfortunately, this can result in non-compliant English humour publishers producing jokes that come out as gibberish in the rest of the world. Fortunately, we then get to laugh at them. ChrisA From breamoreboy at yahoo.co.uk Sat May 11 08:40:58 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 11 May 2013 13:40:58 +0100 Subject: Unicode humor In-Reply-To: References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> <518D1134.40708@nedbatchelder.com> <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> Message-ID: On 10/05/2013 17:25, Chris Angelico wrote: > On Sat, May 11, 2013 at 2:07 AM, rusi wrote: >> On May 10, 8:32 pm, Chris Angelico wrote: >>> On Sat, May 11, 2013 at 1:24 AM, Ned Batchelder wrote: >>>> On 5/10/2013 11:06 AM, jmfauth wrote: >>> >>>>> On 8 mai, 15:19, Roy Smith wrote: >>> >>>>>> Apropos to any of the myriad unicode threads that have been going on >>>>>> recently: >>> >>>>>> http://xkcd.com/1209/ >>> >>>>> ------ >>> >>>>> This reflects a lack of understanding of Unicode. >>> >>>>> jmf >>> >>>> And this reflects a lack of a sense of humor. :) >>> >>> Isn't that a crime in the UK? >>> >>> ChrisA >> >> The problem with English humour (as against standard humor) is that >> its not unicode compliant > > Unicode humour was carefully laid out to incorporate English humour. > In fact, if you use the standard variable-length-joke encoding, it's > possible for a Unicode joke to be decoded as if it were an English > joke, without any actual knowledge of Unicode. Unfortunately, this can > result in non-compliant English humour publishers producing jokes that > come out as gibberish in the rest of the world. Fortunately, we then > get to laugh at them. > > ChrisA > This simply shows bias to the English speaking world, as does Python unicode, at least in 3.3+. I wouldn't mind betting that other languages can't cope, e.g. can 3.3+ manage the top secret joke that's so deadly even the Germans die laughing? -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From rosuav at gmail.com Sat May 11 08:48:37 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 11 May 2013 22:48:37 +1000 Subject: Unicode humor In-Reply-To: References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> <518D1134.40708@nedbatchelder.com> <1d320295-df6a-4feb-9921-0d6cce7f68a2@yb1g2000pbc.googlegroups.com> Message-ID: On Sat, May 11, 2013 at 10:40 PM, Mark Lawrence wrote: > This simply shows bias to the English speaking world, as does Python > unicode, at least in 3.3+. I wouldn't mind betting that other languages > can't cope, e.g. can 3.3+ manage the top secret joke that's so deadly even > the Germans die laughing? It most certainly can. However, the space it takes up depends on how you encode the combining characters; for maximal efficiency of transmission, you would want to use the fully-composed version, because like music, if it isn't composed, it's decomposed. ChrisA From steve+comp.lang.python at pearwood.info Sat May 11 03:01:34 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 May 2013 07:01:34 GMT Subject: Unicode humor References: <3e870f17-e7f5-4bcf-b848-f1947bad6859@k5g2000vbq.googlegroups.com> Message-ID: <518decce$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sat, 11 May 2013 01:16:36 +1000, Chris Angelico wrote: > On Sat, May 11, 2013 at 1:06 AM, jmfauth wrote: >> On 8 mai, 15:19, Roy Smith wrote: >>> Apropos to any of the myriad unicode threads that have been going on >>> recently: >>> >>> http://xkcd.com/1209/ >> >> ------ >> >> >> This reflects a lack of understanding of Unicode. > > By the skywriter, or by the two on the ground, or by Randall Munroe? Obviously by all three. It takes *hours* to execute '?'*1000.replace('?', '?') using a skywriting plane, so obviously it isn't Unicode compliant. -- Steven From kdawg44 at gmail.com Wed May 8 14:52:59 2013 From: kdawg44 at gmail.com (Kevin Holleran) Date: Wed, 8 May 2013 14:52:59 -0400 Subject: MySQL Database Message-ID: Hello, I want to connect to a MySQL database, query for some records, manipulate some data, and then update the database. When I do something like this: db_c.execute("SELECT a, b FROM Users") for row in db_c.fetchall(): (r,d) = row[0].split('|') (g,e) = domain.split('.') db_c.execute("UPDATE Users SET g = '" + g + "' WHERE a ='" + row[0]) Will using db_c to update the database mess up the loop that is cycling through db_c.fetchall()? Thanks for your help. Kevin -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed May 8 14:56:59 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 May 2013 04:56:59 +1000 Subject: MySQL Database In-Reply-To: References: Message-ID: On Thu, May 9, 2013 at 4:52 AM, Kevin Holleran wrote: > Will using db_c to update the database mess up the loop that is cycling > through db_c.fetchall()? Nope; fetchall() returns a list, which you're then iterating over. Nothing the database does can disrupt that. ChrisA From kdawg44 at gmail.com Wed May 8 15:04:27 2013 From: kdawg44 at gmail.com (Kevin Holleran) Date: Wed, 8 May 2013 15:04:27 -0400 Subject: MySQL Database In-Reply-To: References: Message-ID: On Wed, May 8, 2013 at 2:56 PM, Chris Angelico wrote: > On Thu, May 9, 2013 at 4:52 AM, Kevin Holleran wrote: > > Will using db_c to update the database mess up the loop that is cycling > > through db_c.fetchall()? > > Nope; fetchall() returns a list, which you're then iterating over. > Nothing the database does can disrupt that. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > Awesome. That was my hope... Thanks for your help & quick response. Kevin -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Wed May 8 15:07:39 2013 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 08 May 2013 20:07:39 +0100 Subject: MySQL Database In-Reply-To: References: Message-ID: <518AA27B.20409@mrabarnett.plus.com> On 08/05/2013 19:52, Kevin Holleran wrote: > Hello, > > I want to connect to a MySQL database, query for some records, > manipulate some data, and then update the database. > > When I do something like this: > > db_c.execute("SELECT a, b FROM Users") > > for row in db_c.fetchall(): > > (r,d) = row[0].split('|') > > (g,e) = domain.split('.') > > db_c.execute("UPDATE Users SET g = '"+ g + "' WHERE a ='"+ row[0]) > > > Will using db_c to update the database mess up the loop that is cycling > through db_c.fetchall()? > You shouldn't be building an SQL string like that because it's susceptible to SQL injection. You should be doing it more like this: db_c.execute("UPDATE Users SET g = %s WHERE a = %s", (g, row[0])) The values will then be handled safely for you. From kdawg44 at gmail.com Wed May 8 15:31:02 2013 From: kdawg44 at gmail.com (Kevin Holleran) Date: Wed, 8 May 2013 15:31:02 -0400 Subject: MySQL Database In-Reply-To: <518AA27B.20409@mrabarnett.plus.com> References: <518AA27B.20409@mrabarnett.plus.com> Message-ID: Thanks, I actually intend to, was just whipping something up to be an example for my question. -- Kevin Holleran Master of Science, Computer Information Systems Grand Valley State University Master of Business Administration Western Michigan University GCFA, GCFE, CCNA, ISA, MCSA, MCDST, MCP "Do today what others won't, do tomorrow what others can't" - SEALFit "We are what we repeatedly do. Excellence, then, is not an act, but a habit." - Aristotle On Wed, May 8, 2013 at 3:07 PM, MRAB wrote: > On 08/05/2013 19:52, Kevin Holleran wrote: > >> Hello, >> >> I want to connect to a MySQL database, query for some records, >> manipulate some data, and then update the database. >> >> When I do something like this: >> >> db_c.execute("SELECT a, b FROM Users") >> >> for row in db_c.fetchall(): >> >> (r,d) = row[0].split('|') >> >> (g,e) = domain.split('.') >> >> db_c.execute("UPDATE Users SET g = '"+ g + "' WHERE a ='"+ >> row[0]) >> >> >> Will using db_c to update the database mess up the loop that is cycling >> through db_c.fetchall()? >> >> You shouldn't be building an SQL string like that because it's > susceptible to SQL injection. You should be doing it more like this: > > db_c.execute("UPDATE Users SET g = %s WHERE a = %s", (g, row[0])) > > The values will then be handled safely for you. > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From haoyi.sg at gmail.com Wed May 8 16:09:53 2013 From: haoyi.sg at gmail.com (Haoyi Li) Date: Wed, 8 May 2013 13:09:53 -0700 (PDT) Subject: ANN: MacroPy: quasiquotes, case classes, LINQ and more! Now available on PyPI Message-ID: Hey All, MacroPy is an implementation of Macros in Python which lets you very easily modify the semantics of a python program. Apart from the implementation of macros themselves, we also have a pretty impressive list of feature demos that were implemented on top of macros: - Quasiquotes, a quick way to manipulate fragments of a program - String Interpolation, a common feature in many languages - Pyxl, integrating XML markup into a Python program - Tracing and Smart Asserts - Case Classes, easy Algebraic Data Types from Scala - Pattern Matching from the Functional Programming world - LINQ to SQL from C# - Quick Lambdas from Scala and Groovy, - Parser Combinators, inspired by Scala's. The full documentation is over on github (https://github.com/lihaoyi/macropy) if anyone wants to check it out. It runs fine on both CPython 2.7 and PyPy 1.9, and I've just pushed the last up-to-date version of MacroPy to PyPI: https://pypi.python.org/pypi/MacroPy Hope someone finds this useful! Thanks! -Haoyi From roy at panix.com Wed May 8 16:20:48 2013 From: roy at panix.com (Roy Smith) Date: 8 May 2013 16:20:48 -0400 Subject: Style question -- plural of class name? Message-ID: FooEntry is a class. How would you describe a list of these in a docstring? "A list of FooEntries" "A list of FooEntrys" "A list of FooEntry's" "A list of FooEntry instances" The first one certainly sounds the best, but it seems wierd to change the spelling of the class name to make it plural. From skip at pobox.com Wed May 8 16:33:07 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 8 May 2013 15:33:07 -0500 Subject: Style question -- plural of class name? In-Reply-To: References: Message-ID: This one: > "A list of FooEntry instances" Besides the obvious spelling issues in the others, it's not immediately clear if the list contains just FooEntry instances, FooEntry classes (perhaps subclasses) or a mix of the two. #4 makes it clear. Skip From john.a.downs at gmail.com Wed May 8 16:37:05 2013 From: john.a.downs at gmail.com (John Downs) Date: Wed, 8 May 2013 16:37:05 -0400 Subject: Style question -- plural of class name? In-Reply-To: References: Message-ID: On Wed, May 8, 2013 at 4:20 PM, Roy Smith wrote: > FooEntry is a class. How would you describe a list of these in a > docstring? > > "A list of FooEntries" > > "A list of FooEntrys" > > "A list of FooEntry's" > > "A list of FooEntry instances" > > The first one certainly sounds the best, but it seems wierd to change > the spelling of the class name to make it plural. > -- > http://mail.python.org/mailman/listinfo/python-list > How about: "A list with elements of type FooEntry"? I also like the last one: "A list of FooEntry instances". -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Wed May 8 16:45:14 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 8 May 2013 14:45:14 -0600 Subject: Style question -- plural of class name? In-Reply-To: References: Message-ID: On Wed, May 8, 2013 at 2:37 PM, John Downs wrote: > On Wed, May 8, 2013 at 4:20 PM, Roy Smith wrote: >> >> FooEntry is a class. How would you describe a list of these in a >> docstring? >> >> "A list of FooEntries" >> >> "A list of FooEntrys" >> >> "A list of FooEntry's" >> >> "A list of FooEntry instances" >> >> The first one certainly sounds the best, but it seems wierd to change >> the spelling of the class name to make it plural. >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > How about: "A list with elements of type FooEntry"? I also like the last > one: "A list of FooEntry instances". list *ducks* From denismfmcmahon at gmail.com Wed May 8 18:40:54 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Wed, 8 May 2013 22:40:54 +0000 (UTC) Subject: Style question -- plural of class name? References: Message-ID: On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote: > FooEntry is a class. How would you describe a list of these in a > docstring? > > "A list of FooEntries" > > "A list of FooEntrys" > > "A list of FooEntry's" > > "A list of FooEntry instances" > > The first one certainly sounds the best, but it seems wierd to change > the spelling of the class name to make it plural. I wouldn't use an apostrophe for pluralisation. The Normal pluralisation of FooEntry would be FooEntries. Who are you expecting to read the docstring? English majors, grammar nazis, wikipedia editors, programmers, or all 4? -- Denis McMahon, denismfmcmahon at gmail.com From neilc at norwich.edu Thu May 9 11:14:17 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 9 May 2013 15:14:17 GMT Subject: Style question -- plural of class name? References: Message-ID: On 2013-05-08, Denis McMahon wrote: > On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote: > >> FooEntry is a class. How would you describe a list of these in a >> docstring? >> >> "A list of FooEntries" >> >> "A list of FooEntrys" >> >> "A list of FooEntry's" >> >> "A list of FooEntry instances" >> >> The first one certainly sounds the best, but it seems wierd to change >> the spelling of the class name to make it plural. > > I wouldn't use an apostrophe for pluralisation. If there's no chance for confusion between a class named FooEntry and another named FooEntries, then the first attempt seems best. Pluralize a class name by following the usual rules, e.g., "strings" and "ints". -- Neil Cerutti From jpiitula at ling.helsinki.fi Thu May 9 12:43:23 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 09 May 2013 19:43:23 +0300 Subject: Style question -- plural of class name? References: Message-ID: Neil Cerutti writes: > If there's no chance for confusion between a class named FooEntry > and another named FooEntries, then the first attempt seems best. > Pluralize a class name by following the usual rules, e.g., > "strings" and "ints". Like "strings" would be "foo entries". Which might work well. (I mean, isn't the class named "str"?) From neilc at norwich.edu Thu May 9 13:28:41 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 9 May 2013 17:28:41 GMT Subject: Style question -- plural of class name? References: Message-ID: On 2013-05-09, Jussi Piitulainen wrote: > Neil Cerutti writes: >> If there's no chance for confusion between a class named >> FooEntry and another named FooEntries, then the first attempt >> seems best. Pluralize a class name by following the usual >> rules, e.g., "strings" and "ints". > > Like "strings" would be "foo entries". Which might work well. > > (I mean, isn't the class named "str"?) Yeah, that's not such a good Python example. I used it to replace "chars" and felt good at the time. ;) -- Neil Cerutti From steve+comp.lang.python at pearwood.info Wed May 8 20:02:18 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 00:02:18 GMT Subject: Style question -- plural of class name? References: Message-ID: <518ae78a$0$29997$c3e8da3$5496439d@news.astraweb.com> On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote: > FooEntry is a class. How would you describe a list of these in a > docstring? Which language are you writing your docstrings in? Obey the normal rules of spelling, grammar and punctuation for your language, which I assume is English. > "A list of FooEntries" Perfectly acceptable. > "A list of FooEntrys" There is no standard variant or dialect of English (British English, American English, etc.) that pluralises Entry as Entrys, so that would be "absolutely not". > "A list of FooEntry's" "Here come's an S! Quick, jam on an apostrophe!" This is called the grocer's apostrophe, and is universally held in contempt no matter what variant of English you write in. Don't do this. The only acceptable use of an apostrophe to make a plural is if the thing being pluralised is a single letter. E.g. one a, two a's. > "A list of FooEntry instances" This is also acceptable, although a little wordy. Do you write "a list of strings" or "a list of str instances"? > The first one certainly sounds the best, but it seems wierd to change > the spelling of the class name to make it plural. No weirder (note spelling) than changing any other noun. Whether you change "int" to "ints" or FooEntry" to "FooEntries", you're still changing it. That's how you make it plural. -- Steven From cs at zip.com.au Wed May 8 20:42:44 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 9 May 2013 10:42:44 +1000 Subject: Style question -- plural of class name? In-Reply-To: <518ae78a$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <518ae78a$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20130509004244.GA17531@cskk.homeip.net> On 09May2013 00:02, Steven D'Aprano wrote: | On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote: | > "A list of FooEntry's" | | "Here come's an S! Quick, jam on an apostrophe!" | | This is called the grocer's apostrophe, and is universally held in | contempt no matter what variant of English you write in. Don't do this. | | The only acceptable use of an apostrophe to make a plural is if the thing | being pluralised is a single letter. E.g. one a, two a's. Frankly, not even then for me. I spell that "one A, two As". | > "A list of FooEntry instances" | | This is also acceptable, although a little wordy. Do you write "a list of | strings" or "a list of str instances"? How about "a FooEntry list"? -- Cameron Simpson Yes Officer, yes Officer, I will Officer. Thank you. From rosuav at gmail.com Wed May 8 20:12:46 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 May 2013 10:12:46 +1000 Subject: Style question -- plural of class name? In-Reply-To: References: Message-ID: On Thu, May 9, 2013 at 6:20 AM, Roy Smith wrote: > "A list of FooEntry's" Only if you put another apostrophe in: "A list of 'FooEntry's" But the delimited style is almost never of use. I'd go for this only if there were some sort of automated markup being applied - if the word FooEntry were turned into a hyperlink or something. ChrisA From steve+comp.lang.python at pearwood.info Wed May 8 20:13:09 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 00:13:09 GMT Subject: Style question -- plural of class name? References: Message-ID: <518aea14$0$29997$c3e8da3$5496439d@news.astraweb.com> On Wed, 08 May 2013 15:33:07 -0500, Skip Montanaro wrote: > This one: > >> "A list of FooEntry instances" > > Besides the obvious spelling issues in the others, it's not immediately > clear if the list contains just FooEntry instances, FooEntry classes > (perhaps subclasses) or a mix of the two. #4 makes it clear. I don't think this is a real issue. There isn't normally any ambiguity between instances and subclasses. When you read "a list of ints", do you assume that the list looks like [int, MyInt, AnotherInt, FooInt] or do you expect it to look like [2, 7, 6, 1]? The normal interpretation of "one or more Foo" is that we're talking about Foo *instances*, not subclasses of Foo. If that is not that case, then the onus is on the author of the documentation to make it clear that they are talking about subclasses. -- Steven From cjw at ncf.ca Wed May 8 20:38:36 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Wed, 08 May 2013 20:38:36 -0400 Subject: Style question -- plural of class name? In-Reply-To: References: Message-ID: <518AF00C.8030207@ncf.ca> On 08/05/2013 4:20 PM, Roy Smith wrote: > FooEntry is a class. How would you describe a list of these in a > docstring? > > "A list of FooEntries" 0 > > "A list of FooEntrys" -1 > > "A list of FooEntry's" +1 > > "A list of FooEntry instances" No FooEntry is specified as a class. > > The first one certainly sounds the best, but it seems wierd to change > the spelling of the class name to make it plural. > Colin W. From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Thu May 9 06:21:10 2013 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Thu, 09 May 2013 12:21:10 +0200 Subject: Style question -- plural of class name? In-Reply-To: <518AF00C.8030207@ncf.ca> References: <518AF00C.8030207@ncf.ca> Message-ID: Am 09.05.2013 02:38 schrieb Colin J. Williams: > On 08/05/2013 4:20 PM, Roy Smith wrote: >> >> "A list of FooEntry's" +1 Go back to school. Both of you... That is NOT the way to build a plural form... Thomas From robert.kern at gmail.com Thu May 9 13:44:17 2013 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 09 May 2013 18:44:17 +0100 Subject: Style question -- plural of class name? In-Reply-To: References: Message-ID: On 2013-05-08 21:20, Roy Smith wrote: > FooEntry is a class. How would you describe a list of these in a > docstring? > > "A list of FooEntries" > > "A list of FooEntrys" > > "A list of FooEntry's" > > "A list of FooEntry instances" > > The first one certainly sounds the best, but it seems wierd to change > the spelling of the class name to make it plural. I'm using services like Github more and more to talk about code, so I have taken to adopting its inline markup for `code` when referring to identifiers. Thus, I will often write A list of `FooEntry`s But I don't mind A list of FooEntries Hopefully there isn't also a `FooEntries` class. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From flexage at gmail.com Wed May 8 16:50:43 2013 From: flexage at gmail.com (flexage at gmail.com) Date: Wed, 8 May 2013 13:50:43 -0700 (PDT) Subject: Globally available I/O connection (K8055 I/O board) Message-ID: I'm having a bit of an issue trying to make a globally available connection to my Velleman K8055 I/O board... I've documented my issue as best I can here: http://stackoverflow.com/questions/16449706/python-access-global-instance-of-connection Could anybody shed some light on a way to combat my problem? From davea at davea.name Wed May 8 17:20:54 2013 From: davea at davea.name (Dave Angel) Date: Wed, 08 May 2013 17:20:54 -0400 Subject: Globally available I/O connection (K8055 I/O board) In-Reply-To: References: Message-ID: <518AC1B6.9070402@davea.name> On 05/08/2013 04:50 PM, flexage at gmail.com wrote: > I'm having a bit of an issue trying to make a globally available connection to my Velleman K8055 I/O board... > > I've documented my issue as best I can here: http://stackoverflow.com/questions/16449706/python-access-global-instance-of-connection > I don't think that's a Python question at all. You have some constraint your hardware gives you that requires you to run function k8055(0) once, and to use that result object to access the board subsequently. Looks to me like you're doing exactly that. Are there any other calls to that function in your code? If you were messing up with your access to the global object globalK8055, you'd get an exception. The only Python mistake I can think of that you might be doing is if you're using your script as a module, or otherwise doing circular imports, or if you're accessing some module under more than one name. In particular, if you run SmartyPi/appglobals.py as a script, it'd be calling the k8055() function once and saving the value. And then when somebody imports it as from smartypi.appglobals import globalK8055 as k they'd get a NEW instance of the module and a new value for globalK8055. If you have some form of logging mechanism (including print), you could record each time the k8055() is called. -- DaveA From ricardo.lelis3 at gmail.com Wed May 8 19:47:47 2013 From: ricardo.lelis3 at gmail.com (rlelis) Date: Wed, 8 May 2013 16:47:47 -0700 (PDT) Subject: help on Implementing a list of dicts with no data pattern Message-ID: Hi guys, I'm working on this long file, where i have to keep reading and storing different excerpts of text (data) in different variables (list). Once done that i want to store in dicts the data i got from the lists mentioned before. I want them on a list of dicts for later RDBMs purpose's. The data i'm working with, don't have fixed pattern (see example bellow), so what i'm doing is for each row, i want to store combinations of word/value (Key-value) to keep track of all the data. My problem is that once i'm iterating over the list (original one a.k.a file_content in the link), then i'm nesting several if clause to match the keys i want. Done that i select the keys i want to give them values and lastly i append that dict into a new list. The problem here is that i end up always with the last line repeated several times for each row it found's. Please take a look on what i have now: http://pastebin.com/A9eka7p9 From davea at davea.name Wed May 8 20:05:15 2013 From: davea at davea.name (Dave Angel) Date: Wed, 08 May 2013 20:05:15 -0400 Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: References: Message-ID: <518AE83B.1070702@davea.name> On 05/08/2013 07:47 PM, rlelis wrote: > Hi guys, > Please read this http://wiki.python.org/moin/GoogleGroupsPython. > I'm working on this long file, where i have to keep reading and > storing different excerpts of text (data) in different variables (list). > > Once done that i want to store in dicts the data i got from the lists mentioned before. I want them on a list of dicts for later RDBMs purpose's. > > The data i'm working with, don't have fixed pattern (see example bellow), so what i'm doing is for each row, i want to store combinations of word/value (Key-value) to keep track of all the data. > > My problem is that once i'm iterating over the list (original one a.k.a file_content in the link), then i'm nesting several if clause to match > the keys i want. Done that i select the keys i want to give them values and lastly i append that dict into a new list. The problem here is that i end up always with the last line repeated several times for each row it found's. > > Please take a look on what i have now: > http://pastebin.com/A9eka7p9 > If you want a response here, please post the code here. If it's over 50 lines, simplify it first. And be sure and tell us what version of Python and what other dependencies you might have. And what OS you're targeting. -- DaveA From python at mrabarnett.plus.com Wed May 8 20:16:10 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 May 2013 01:16:10 +0100 Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: References: Message-ID: <518AEACA.5040206@mrabarnett.plus.com> On 09/05/2013 00:47, rlelis wrote: > Hi guys, > > I'm working on this long file, where i have to keep reading and > storing different excerpts of text (data) in different variables (list). > > Once done that i want to store in dicts the data i got from the lists mentioned before. I want them on a list of dicts for later RDBMs purpose's. > > The data i'm working with, don't have fixed pattern (see example bellow), so what i'm doing is for each row, i want to store combinations of word/value (Key-value) to keep track of all the data. > > My problem is that once i'm iterating over the list (original one a.k.a file_content in the link), then i'm nesting several if clause to match > the keys i want. Done that i select the keys i want to give them values and lastly i append that dict into a new list. The problem here is that i end up always with the last line repeated several times for each row it found's. > > Please take a look on what i have now: > http://pastebin.com/A9eka7p9 > You're creating a dict for highway_dict and a dict for aging_dict, and then using those dicts for every iteration of the 'for' loop. You're also appending both of the dicts onto the 'queue_row' list for every iteration of the 'for' loop. I think that what you meant to do was, for each match, to create a dict, populate it, and then append it to the list. From ricardo.lelis3 at gmail.com Thu May 9 05:57:47 2013 From: ricardo.lelis3 at gmail.com (rlelis) Date: Thu, 9 May 2013 02:57:47 -0700 (PDT) Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: References: Message-ID: <826082ef-43e2-4835-8621-6ef677eb922c@googlegroups.com> On Thursday, May 9, 2013 12:47:47 AM UTC+1, rlelis wrote: > Hi guys, > > > > I'm working on this long file, where i have to keep reading and > > storing different excerpts of text (data) in different variables (list). > > > > Once done that i want to store in dicts the data i got from the lists mentioned before. I want them on a list of dicts for later RDBMs purpose's. > > > > The data i'm working with, don't have fixed pattern (see example bellow), so what i'm doing is for each row, i want to store combinations of word/value (Key-value) to keep track of all the data. > > > > My problem is that once i'm iterating over the list (original one a.k.a file_content in the link), then i'm nesting several if clause to match > > the keys i want. Done that i select the keys i want to give them values and lastly i append that dict into a new list. The problem here is that i end up always with the last line repeated several times for each row it found's. > > > > Please take a look on what i have now: > > http://pastebin.com/A9eka7p9 Sorry, i thought that a link to pastebin could be helpfully since it captures the syntax highlights and spacings. I don't have a fifty line code there. The 25 lines below, where to show you guys a picture of what is going on, to be more intuitive. This is what i have for now: highway_dict = {} aging_dict = {} queue_row = [] for content in file_content: if 'aging' in content: # aging 0 100 collumns = ''.join(map(str, content[:1])).replace('-','_').lower() total_values =''.join(map(str, content[1:2])) aging_values = ''.join(map(str, content[2:])) aging_dict['total'], aging_dict[collumns] = total, aging_values queue_row.append(aging_dict) if 'highway' in content: #highway | 4 | disable | 25 collumns = ''.join(map(str, content[:1])).replace('-','_').lower() lanes_values =''.join(map(str, content[1:2])) state_values = ''.join(map(str, content[2:3])).strip('') limit_values = ''.join(map(str, content[3:4])).strip('') highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes, state, limit_values queue_row.append(highway_dict) From davea at davea.name Thu May 9 08:26:03 2013 From: davea at davea.name (Dave Angel) Date: Thu, 09 May 2013 08:26:03 -0400 Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: <826082ef-43e2-4835-8621-6ef677eb922c@googlegroups.com> References: <826082ef-43e2-4835-8621-6ef677eb922c@googlegroups.com> Message-ID: <518B95DB.2000207@davea.name> On 05/09/2013 05:57 AM, rlelis wrote: > On Thursday, May 9, 2013 12:47:47 AM UTC+1, rlelis wrote: >> Hi guys, >> >> >> >> I'm working on this long file, where i have to keep reading and >> >> storing different excerpts of text (data) in different variables (list). >> >> >> >> Once done that i want to store in dicts the data i got from the lists mentioned before. I want them on a list of dicts for later RDBMs purpose's. >> >> >> >> The data i'm working with, don't have fixed pattern (see example bellow), so what i'm doing is for each row, i want to store combinations of word/value (Key-value) to keep track of all the data. >> >> >> >> My problem is that once i'm iterating over the list (original one a.k.a file_content in the link), then i'm nesting several if clause to match >> >> the keys i want. Done that i select the keys i want to give them values and lastly i append that dict into a new list. The problem here is that i end up always with the last line repeated several times for each row it found's. >> >> >> >> Please take a look on what i have now: >> >> http://pastebin.com/A9eka7p9 > > Sorry, i thought that a link to pastebin could be helpfully since it captures the syntax highlights and spacings. I don't have a fifty line code there. The 25 lines below, where to show you guys a picture of what is going on, to be more intuitive. > This is what i have for now: > The entire following set of comments is probably outdated since you apparently did NOT use readlines() or equivalent to get file_content. So you'd better give us some sample data, a program that can actually run without getting exceptions due to misnamed variables, and a description of just what you expected to be in each result variable. It'd also be smart to mention what version of Python you're targeting. .... what follows was a waste of my time ... file_content is not defined, but we can guess you have read it from a text file with readlines(), or more efficiently that it's simply a file object for a file opened with "r". Can we see sample data, maybe for 3 or four lines? file_content = [ "A4 value2 aging", "b8 value99 paging", "-1 this is aging a test", "B2 repeaagingts", ] The sample, or the description, should indicate if repeats of the "columns" column are allowed, as with b and B above. > highway_dict = {} > aging_dict = {} > queue_row = [] > for content in file_content: > if 'aging' in content: > # aging 0 100 > collumns = ''.join(map(str, content[:1])).replace('-','_').lower() > total_values =''.join(map(str, content[1:2])) > aging_values = ''.join(map(str, content[2:])) Those three lines would be much more reasonable and readable if you eliminated all the list stuff, and just did what was needed. Also, calling a one-character string "collumns" or "total_values" makes no sense to me. collumns = content[:1].replace('-','_').lower() total_values = content[1:2] aging_values = content[2:] > > aging_dict['total'], aging_dict[collumns] = total, aging_values That line tries to get clever, and ends up obscuring what's really happening. Further, the value in total, if any is NOT what you just extracted in total_values. aging_dict['total'] = total aging_dict[collumns] = aging_values > queue_row.append(aging_dict) Just what do you expect to be in the aging_dict here? If you intended that each item of queue_row contains a dict with just one item, then you need to clear aging_dict each time through the loop. As it stands the list ends up with a bunch of dicts, each with possibly one more entry than the previous dict. All the same remarks apply to the following code. Additionally, you don't use collumns for anything, and you use lanes and state when you presumably meant lanes_values and state_values. > > if 'highway' in content: > #highway | 4 | disable | 25 > collumns = ''.join(map(str, content[:1])).replace('-','_').lower() > lanes_values =''.join(map(str, content[1:2])) > state_values = ''.join(map(str, content[2:3])).strip('') > limit_values = ''.join(map(str, content[3:4])).strip('') > > highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes, state, limit_values > queue_row.append(highway_dict) > Now, when -- DaveA From neilc at norwich.edu Thu May 9 09:08:56 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 9 May 2013 13:08:56 GMT Subject: help on Implementing a list of dicts with no data pattern References: <826082ef-43e2-4835-8621-6ef677eb922c@googlegroups.com> Message-ID: On 2013-05-09, rlelis wrote: > This is what i have for now: > > highway_dict = {} > aging_dict = {} > queue_row = [] > for content in file_content: > if 'aging' in content: > # aging 0 100 > collumns = ''.join(map(str, content[:1])).replace('-','_').lower() > total_values =''.join(map(str, content[1:2])) > aging_values = ''.join(map(str, content[2:])) > > aging_dict['total'], aging_dict[collumns] = total, aging_values > queue_row.append(aging_dict) > > if 'highway' in content: > #highway | 4 | disable | 25 > collumns = ''.join(map(str, content[:1])).replace('-','_').lower() > lanes_values =''.join(map(str, content[1:2])) > state_values = ''.join(map(str, content[2:3])).strip('') > limit_values = ''.join(map(str, content[3:4])).strip('') > > highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes, state, limit_values > queue_row.append(highway_dict) Can you provide a short example of input and what you had hoped to see in the lists and dicts at the end? -- Neil Cerutti From ricardo.lelis3 at gmail.com Thu May 9 10:33:28 2013 From: ricardo.lelis3 at gmail.com (rlelis) Date: Thu, 9 May 2013 07:33:28 -0700 (PDT) Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: References: Message-ID: I apologize once again. Is my first post here and i'm getting used to the group as long as i get the feedback of my errors by you guys. I'm using Python 2.7.3 with no dependencies, i'm simply using the standard library. Here is the "big picture" of the scenario(i have added it in the pastebin link too) FILE OUTPUT DESIRED: aging: aging |total |age aging |0 |100 aging |2 |115 aging |3 |1 aging |4 |10 highway: highway | lanes | state | limit(mph) highway | 4 | disable | 25 highway | 2 | disable | 245 highway | 3 | disable | 125 highway | 2 | enable | 255 highway | 3 | disable | 212 highway | 8 | disable | 78 FILE INPUT EXCERPT EXAMPLE: aging 0 100 aging 2 115 aging 3 1 highway 4 disable 25 highway 2 disable 245 highway 0 enable 125 Meanwhile i have change the code a little bit and achieve a output closer to what i want: highway_dict = {} aging_dict = {} queue_counters={} queue_row = [] for content in file_content: if 'aging' in content: # aging 0 100 columns = ', '.join(map(str, content[:1])).replace('-','_').lower() total_values =''.join(map(str, content[1:2])) aging_values = '\t'.join(map(str, content[2:])) aging_dict['total'], aging_dict[columns] = total, aging_values queue_counters[columns] = aging_dict if 'highway' in content: #highway | 4 | disable | 25 columns = ''.join(map(str, content[:1])).replace('-','_').lower() lanes_values =''.join(map(str, content[1:2])) state_values = ''.join(map(str, content[2:3])).strip('') limit_values = ''.join(map(str, content[3:4])).strip('') highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes, state, limit_values queue_counters[columns] = highway_dict queue_row.append(queue_counters) Now i'm adding the different dicts to a "main" one (queue_counters). The problem here is that i'm keeping falling on the iteration issue. I only get the last row on my ouput. My last printout was: queue_counters: {'aging': {'age': '10', 'total': '4'}, 'highway': {'lanes': '8','state': 'disable', 'limit': '78'}} @Dave Angel "The sample, or the description, should indicate if repeats of the "columns" column are allowed, as with b and B above. " - Yes the columns repetition are allowed. "That line tries to get clever, and ends up obscuring what's really happening. Further, the value in total, if any is NOT what you just extracted in total_values." - this variable name total_values means that i'm storing the content (different values)of the total column, and the same applies to the other columns (might not be the best names, but don't forget that we are just prototyping here). The total, age, etc etc variable name i had to set them, once they don't come with the original file, but is important to give them names to help on RDBMs purposes later, and not only that, it's handy right? The column variable refers to the object name (aging and highway). I'm doing that because in the original source i have to deal with string formatting of strange names. Remember that this is for prototyping, that's why i'm trying to resume things here. From davea at davea.name Thu May 9 10:50:45 2013 From: davea at davea.name (Dave Angel) Date: Thu, 09 May 2013 10:50:45 -0400 Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: References: Message-ID: <518BB7C5.2060900@davea.name> On 05/09/2013 10:33 AM, rlelis wrote: > I apologize once again. > Is my first post here and i'm getting used to the group as long as i get the feedback of my errors by you guys. > I'm using Python 2.7.3 with no dependencies, i'm simply using the standard > library. > Here is the "big picture" of the scenario(i have added it in the pastebin link too) > > FILE OUTPUT DESIRED: > aging: > aging |total |age > aging |0 |100 > aging |2 |115 > aging |3 |1 > aging |4 |10 > > highway: > highway | lanes | state | limit(mph) > highway | 4 | disable | 25 > highway | 2 | disable | 245 > highway | 3 | disable | 125 > highway | 2 | enable | 255 > highway | 3 | disable | 212 > highway | 8 | disable | 78 > > FILE INPUT EXCERPT EXAMPLE: > aging 0 100 > aging 2 115 > aging 3 1 > highway 4 disable 25 > highway 2 disable 245 > highway 0 enable 125 > > Meanwhile i have change the code a little bit and achieve a output closer to what i want: You're still missing the file read code. My earlier assumption that it was a simple readlines() was bogus, or the line: if "aging" in file-content: would never work. Perhaps you have something like: infile = open("xxxx","r") file_content = [line.split() for line in infile] if you confirm it, I'll try to go through the code again, trying to make sense of it. -- DaveA From ricardo.lelis3 at gmail.com Thu May 9 12:14:14 2013 From: ricardo.lelis3 at gmail.com (rlelis) Date: Thu, 9 May 2013 09:14:14 -0700 (PDT) Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: References: Message-ID: <5bc1d8b8-61b1-446d-8487-1e72c1ddf925@googlegroups.com> On Thursday, May 9, 2013 12:47:47 AM UTC+1, rlelis wrote: @Dave Angel this is how i mange to read and store the data in file. data = [] # readdata f = open(source_file, 'r') for line in f: header = (line.strip()).lower() # conditions(if/else clauses) on the header content to filter desired data data.append(header) From davea at davea.name Thu May 9 14:19:38 2013 From: davea at davea.name (Dave Angel) Date: Thu, 09 May 2013 14:19:38 -0400 Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: <5bc1d8b8-61b1-446d-8487-1e72c1ddf925@googlegroups.com> References: <5bc1d8b8-61b1-446d-8487-1e72c1ddf925@googlegroups.com> Message-ID: <518BE8BA.6050501@davea.name> On 05/09/2013 12:14 PM, rlelis wrote: > On Thursday, May 9, 2013 12:47:47 AM UTC+1, rlelis wrote: > @Dave Angel > this is how i mange to read and store the data in file. > data = [] > # readdata > f = open(source_file, 'r') > for line in f: > header = (line.strip()).lower() > # conditions(if/else clauses) on the header content to filter desired data > data.append(header) > From earlier message: > highway_dict = {} > aging_dict = {} > queue_counters={} > queue_row = [] > for content in file_content: if 'aging' in content: So your data is a list of strings, each representing one line of the file. When you run this, you'll get something like: NameError: name 'file_content' is not defined If I assume you've just neglected to include the assignment, file_content = data, then the next problem is if 'aging' in content: will never fire. Likewise if 'highway' in content: will never fire. So the rest of the code is irrelevant. -- DaveA From ricardo.lelis3 at gmail.com Thu May 9 17:22:59 2013 From: ricardo.lelis3 at gmail.com (rlelis) Date: Thu, 9 May 2013 14:22:59 -0700 (PDT) Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: References: <5bc1d8b8-61b1-446d-8487-1e72c1ddf925@googlegroups.com> Message-ID: <3bcb3a3a-f1e2-4e31-b917-6a4391ddb9fa@googlegroups.com> On Thursday, May 9, 2013 7:19:38 PM UTC+1, Dave Angel wrote: Yes it's a list of string. I don't get the NameError: name 'file_content' is not defined in my code. After i appended the headers i wanted to cut the data list it little bit more because there was some data (imagine some other collumns) to the left that didn't needed. file_content = [] for d in data: file_content.append(d[1:]) from this point on i've showed the code, highway_dict = {} aging_dict = {} queue_counters={} queue_row = [] for content in file_content: if 'aging' in content: # aging 0 100 # code here etc, etc From davea at davea.name Thu May 9 18:32:21 2013 From: davea at davea.name (Dave Angel) Date: Thu, 09 May 2013 18:32:21 -0400 Subject: help on Implementing a list of dicts with no data pattern In-Reply-To: <3bcb3a3a-f1e2-4e31-b917-6a4391ddb9fa@googlegroups.com> References: <5bc1d8b8-61b1-446d-8487-1e72c1ddf925@googlegroups.com> <3bcb3a3a-f1e2-4e31-b917-6a4391ddb9fa@googlegroups.com> Message-ID: <518C23F5.3070002@davea.name> On 05/09/2013 05:22 PM, rlelis wrote: > On Thursday, May 9, 2013 7:19:38 PM UTC+1, Dave Angel wrote: > > Yes it's a list of string. I don't get the NameError: name 'file_content' is not defined in my code. That's because you have the 3 lines below which we hadn't seen yet. > > After i appended the headers i wanted to cut the data list it little bit more because there was some data (imagine some other collumns) to the left that didn't needed. > > file_content = [] > for d in data: > file_content.append(d[1:]) > > from this point on i've showed the code, > highway_dict = {} > aging_dict = {} > queue_counters={} > queue_row = [] > for content in file_content: > if 'aging' in content: > # aging 0 100 > # code here > OK, so I now have some code I can actually run. Unfortunately, it produces an error: Traceback (most recent call last): File "ricardo.py", line 23, in aging_dict['total'], aging_dict[columns] = total, aging_values NameError: name 'total' is not defined So I'll make a reasonable guess that you meant total_values there. I still can't understand how you're testing this code, when there are trivial bugs in it. Next, I get: Traceback (most recent call last): File "ricardo.py", line 32, in highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes, state, limit_values NameError: name 'lanes' is not defined and then: Traceback (most recent call last): File "ricardo.py", line 32, in highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes_values, state, limit_values NameError: name 'state' is not defined Each of those not-defined errors was pointed out by me earlier in the thread. I don't see any output logic, so I guess it's up to us to guess what the meanings and scope of the various lists and dicts are. I figure the queue_row is your final collection that you hope to get results from. It's a list containing many references to a single queue_counters object. So naturally, they all look the same. If you want them to be different, you have to create a new one each time. Move the line: queue_counters={} inside the loop, right after the line: for content in file_content: There are a bunch of other things wrong, like not lining up the columns when you're substringing content, but this may be your major stumbling block. Note: you may have to also move the highway_dict and aging_dict; I haven't figured out what they're for, yet. Following is the code I've been using to try to figure out what you were intending: file_content = [ "aging 0 100", "aging 2 115", "aging 3 1", "highway 4 disable 25", "highway 2 disable 245", "highway 0 enable 125", ] highway_dict = {} aging_dict = {} #queue_counters={} queue_row = [] for content in file_content: queue_counters={} if 'aging' in content: # aging 0 100 columns = ', '.join(map(str, content[:1])).replace('-','_').lower() print "columns:", columns total_values =''.join(map(str, content[1:2])) aging_values = '\t'.join(map(str, content[2:])) aging_dict['total'], aging_dict[columns] = total_values, aging_values queue_counters[columns] = aging_dict if 'highway' in content: #highway | 4 | disable | 25 columns = ''.join(map(str, content[:1])).replace('-','_').lower() lanes_values =''.join(map(str, content[1:2])) state_values = ''.join(map(str, content[2:3])).strip('') limit_values = ''.join(map(str, content[3:4])).strip('') highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes_values, state_values, limit_values queue_counters[columns] = highway_dict queue_row.append(queue_counters) print print "h dict:", highway_dict print print "aging dict:", aging_dict print print "q counters:", queue_counters for key, item in queue_counters.iteritems(): print key, item print print "q row:", queue_row for item in queue_row: print item -- DaveA From neilc at norwich.edu Fri May 10 08:54:43 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 10 May 2013 12:54:43 GMT Subject: help on Implementing a list of dicts with no data pattern References: <5bc1d8b8-61b1-446d-8487-1e72c1ddf925@googlegroups.com> <3bcb3a3a-f1e2-4e31-b917-6a4391ddb9fa@googlegroups.com> Message-ID: On 2013-05-09, Dave Angel wrote: > On 05/09/2013 05:22 PM, rlelis wrote: >> On Thursday, May 9, 2013 7:19:38 PM UTC+1, Dave Angel wrote: >> >> Yes it's a list of string. I don't get the NameError: name 'file_content' is not defined in my code. > > That's because you have the 3 lines below which we hadn't seen yet. Heroic efforts, Dave! To rlelis: Do not start to program until you understand what you want to do. Work it out on a sheet of paper, or at least in your mind. If you can't provide sample input and the expected output from it, chances are you aren't ready to start writing code. -- Neil Cerutti From valbendan at outlook.com Thu May 9 00:10:40 2013 From: valbendan at outlook.com (dabaichi) Date: Thu, 9 May 2013 12:10:40 +0800 Subject: Anybody familiar with pygments ? Message-ID: Hello guys: pygments version: 1.6 python version :3.3.1(64bits, use msi installer install) os:windows 7 (64bits) Hereis my python source code: #!/usr/bin/env python #-*- coding=utf-8 -*- import os import sys from pygments import highlight as html_highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter from pygments.styles import get_style_by_name def highlight(filename): formatter = HtmlFormatter(linenos=True, style="vim") filename_split = filename.split('.') if len(filename_split) == 1: return None lexer = get_lexer_by_name(filename_split[len(filename_split) - 1]) try: with open(filename, 'r') as fp: content = fp.read() except Exception: return None html_content = html_highlight(content, lexer, formatter) return html_content def main(): filename = r'test.c' print(highlight(filename)) if __name__ == "__main__": main() Here is test.c source code: #include int main(int argc, char * argv[]) { printf("This is a test!\n"); return 0; } And hereis the output file:
1
2
3
4
5
6
7
#include <stdio.h>

int main(int argc, char 
* argv[])
{
    printf("This is a test!\n");
    return 0;
}
It looks like: 1 #include 2 3 int main(int argc, char * argv[]) 4 { 5 printf("This is a test!\n"); 6 return 0; 7 } 8 with no color in firefox (20.0.1) I want to know why output html file with no color ? Is my code error ? Best regard! --peipei --- news://freenews.netfront.net/ - complaints: news at netfront.net --- From fabiosantosart at gmail.com Thu May 9 04:40:09 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 9 May 2013 09:40:09 +0100 Subject: Anybody familiar with pygments ? In-Reply-To: References: Message-ID: On 9 May 2013 05:19, "dabaichi" wrote: > > And hereis the output file: That's not the output file. That is just an HTML fragment to put on your page. A full HTML file will need more things, which is the reason why you don't see color output. > I want to know why output html file with no color ? Because there is no CSS. The output has a lot of tags with classes. You are supposed to use a CSS file along with it. So, first put that output into a complete HTML document (with a head, a body...) And in that document add or link to your CSS file with the color information. I never used pygments but there may be some readily available. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Fri May 10 11:24:10 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Fri, 10 May 2013 17:24:10 +0200 Subject: Anybody familiar with pygments ? In-Reply-To: References: Message-ID: On Thu, May 9, 2013 at 10:40 AM, F?bio Santos wrote: > > On 9 May 2013 05:19, "dabaichi" wrote: >> >> And hereis the output file: > > That's not the output file. That is just an HTML fragment to put on your > page. A full HTML file will need more things, which is the reason why you > don't see color output. > >> I want to know why output html file with no color ? > > Because there is no CSS. The output has a lot of tags with classes. > You are supposed to use a CSS file along with it. > > So, first put that output into a complete HTML document (with a head, a > body...) And in that document add or link to your CSS file with the color > information. I never used pygments but there may be some readily available. > > > -- > http://mail.python.org/mailman/listinfo/python-list > First off, the Python code was unnecessary in this case, because it would be cheaper to use the pygmentize app included. Second off, CSS is available at https://github.com/richleland/pygments-css or by running `pygmentize -f html -O full FILE`. -- Kwpolska | GPG KEY: 5EAAEA16 stop html mail | always bottom-post http://asciiribbon.org | http://caliburn.nl/topposting.html From omrahulrajcse at gmail.com Thu May 9 02:36:28 2013 From: omrahulrajcse at gmail.com (RAHUL RAJ) Date: Wed, 8 May 2013 23:36:28 -0700 (PDT) Subject: Append to python List Message-ID: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Checkout the following code: sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] output=[] output=[x for x in sample2 if x not in output] the output I get is 3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11 12 13 6 7 8 9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16 9 10 11 12 13 14 15 17 10 11 12 13 14 15 16 17 which contains duplicate values. But if I do like this: sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] output=[] for x in sample2: if x not in output: output.append(x) the value of 'output' I get like this: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 I know that both the programs have the same functionality, but why do I have different outputs? Please help! From jpiitula at ling.helsinki.fi Thu May 9 02:55:20 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 09 May 2013 09:55:20 +0300 Subject: Append to python List References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Message-ID: RAHUL RAJ writes: > Checkout the following code: > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > output=[] > output=[x for x in sample2 if x not in output] > > the output I get is > 3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11 > 12 13 6 7 8 9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16 > 9 10 11 12 13 14 15 17 10 11 12 13 14 15 16 17 > > which contains duplicate values. The second comprehension, [x for x in sample2 if x not in output], in the context, is equivalent to [x for x in sample2 if x not in []]. It does not refer to an incomplete version of the list that gets assigned to the variable after it's done. From dihedral88888 at googlemail.com Thu May 9 07:24:53 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 9 May 2013 04:24:53 -0700 (PDT) Subject: Append to python List In-Reply-To: References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Message-ID: <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> Jussi Piitulainen? 2013?5?9????UTC+8??2?55?20???? > RAHUL RAJ writes: > > > > > Checkout the following code: > > > > > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > > > output=[] > > > output=[x for x in sample2 if x not in output] > > > > > > the output I get is > > > 3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11 > > > 12 13 6 7 8 9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16 > > > 9 10 11 12 13 14 15 17 10 11 12 13 14 15 16 17 > > > > > > which contains duplicate values. > > > > The second comprehension, [x for x in sample2 if x not in output], in > > the context, is equivalent to [x for x in sample2 if x not in []]. It > > does not refer to an incomplete version of the list that gets assigned > > to the variable after it's done. This is just the handy style for a non-critical loop. In a critical loop, the number of the total operation counts does matter in the execution speed. From jpiitula at ling.helsinki.fi Thu May 9 07:30:05 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 09 May 2013 14:30:05 +0300 Subject: Append to python List References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> Message-ID: 88888 Dihedral writes: > This is just the handy style for a non-critical loop. > In a critical loop, the number of the total operation counts > does matter in the execution speed. Do you use speed often? From rosuav at gmail.com Thu May 9 07:49:42 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 May 2013 21:49:42 +1000 Subject: Append to python List In-Reply-To: References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> Message-ID: On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen wrote: > 88888 Dihedral writes: > >> This is just the handy style for a non-critical loop. >> In a critical loop, the number of the total operation counts >> does matter in the execution speed. > > Do you use speed often? Dihedral is a bot. Quite a good one, but a bot. ChrisA From dihedral88888 at googlemail.com Fri May 10 05:51:57 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 10 May 2013 02:51:57 -0700 (PDT) Subject: Append to python List In-Reply-To: References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> Message-ID: <34c6abdd-91f3-4c24-bb82-bd5063902bd0@googlegroups.com> Jussi Piitulainen? 2013?5?9????UTC+8??7?30?05???? > 88888 Dihedral writes: > > > > > This is just the handy style for a non-critical loop. > > > In a critical loop, the number of the total operation counts > > > does matter in the execution speed. > > > > Do you use speed often? There is another concern about the list construction part in programming. Although a typical PC is installed with gaga bytes of DRAM now, anything that will use more memory from the heap dynamically could fail in the run time. It is the programmer's job to identify this kind of sources in minds. From as at sci.fi Sat May 11 11:47:18 2013 From: as at sci.fi (Anssi Saari) Date: Sat, 11 May 2013 18:47:18 +0300 Subject: Append to python List References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> Message-ID: Chris Angelico writes: > On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen > wrote: >> 88888 Dihedral writes: >> >>> This is just the handy style for a non-critical loop. >>> In a critical loop, the number of the total operation counts >>> does matter in the execution speed. >> >> Do you use speed often? > > Dihedral is a bot. Quite a good one, but a bot. That's been said often enough. Is the source available and is it in Python? From rosuav at gmail.com Sat May 11 12:00:44 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 May 2013 02:00:44 +1000 Subject: Append to python List In-Reply-To: References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> Message-ID: On Sun, May 12, 2013 at 1:47 AM, Anssi Saari wrote: > Chris Angelico writes: > >> On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen >> wrote: >>> 88888 Dihedral writes: >>> >>>> This is just the handy style for a non-critical loop. >>>> In a critical loop, the number of the total operation counts >>>> does matter in the execution speed. >>> >>> Do you use speed often? >> >> Dihedral is a bot. Quite a good one, but a bot. > > That's been said often enough. Is the source available and is it in > Python? Not to my knowledge. Technically Dihedral is merely _rumoured_ to be a bot, as we have no actual proof; but we've been conducting a variety of Turing tests via this list and have yet to see any strong argument for his being deemed human. Most humans would get defensive, or at least protest, if treated as bots; Dihedral never has, despite being referred to in this way a number of times. ChrisA From dihedral88888 at googlemail.com Sat May 11 22:29:31 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 11 May 2013 19:29:31 -0700 (PDT) Subject: Append to python List In-Reply-To: References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> Message-ID: <458e28f3-56ad-4fea-87ec-1f1858f5d414@googlegroups.com> Chris Angelico? 2013?5?12????UTC+8??12?00?44???? > On Sun, May 12, 2013 at 1:47 AM, Anssi Saari wrote: > > > Chris Angelico writes: > > > > > >> On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen > > >> wrote: > > >>> 88888 Dihedral writes: > > >>> > > >>>> This is just the handy style for a non-critical loop. > > >>>> In a critical loop, the number of the total operation counts > > >>>> does matter in the execution speed. > > >>> > > >>> Do you use speed often? > > >> > > >> Dihedral is a bot. Quite a good one, but a bot. > > > > > > That's been said often enough. Is the source available and is it in > > > Python? > > > > Not to my knowledge. Technically Dihedral is merely _rumoured_ to be a > > bot, as we have no actual proof; but we've been conducting a variety > > of Turing tests via this list and have yet to see any strong argument > > for his being deemed human. Most humans would get defensive, or at > > least protest, if treated as bots; Dihedral never has, despite being > > referred to in this way a number of times. > > > > ChrisA Don't you get the practices of POSIX ? From rosuav at gmail.com Sat May 11 22:40:32 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 May 2013 12:40:32 +1000 Subject: Append to python List In-Reply-To: <458e28f3-56ad-4fea-87ec-1f1858f5d414@googlegroups.com> References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> <458e28f3-56ad-4fea-87ec-1f1858f5d414@googlegroups.com> Message-ID: On Sun, May 12, 2013 at 12:29 PM, 88888 Dihedral wrote: > Chris Angelico? 2013?5?12????UTC+8??12?00?44???? >> Most humans would get defensive, or at >> least protest, if treated as bots; Dihedral never has, despite being >> referred to in this way a number of times. >> >> ChrisA > > Don't you get the practices of POSIX ? I rest my case, m'lud. ChrisA From jpiitula at ling.helsinki.fi Sun May 12 02:02:57 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 May 2013 09:02:57 +0300 Subject: Append to python List References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> <11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com> Message-ID: Chris Angelico writes: > On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen wrote: > > 88888 Dihedral writes: > > > >> This is just the handy style for a non-critical loop. > >> In a critical loop, the number of the total operation counts > >> does matter in the execution speed. > > > > Do you use speed often? > > Dihedral is a bot. Quite a good one, but a bot. Yes, I understood why people say so when it followed up to something I wrote myself, and what it wrote made no sense in the context. My response was also generated by a bot: M-x doctor in Emacs, meant to be funny. Don't worry, I'm not going to engage it further. From rosuav at gmail.com Thu May 9 02:52:42 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 9 May 2013 16:52:42 +1000 Subject: Append to python List In-Reply-To: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Message-ID: On Thu, May 9, 2013 at 4:36 PM, RAHUL RAJ wrote: > output=[x for x in sample2 if x not in output] > > output=[] > for x in sample2: > if x not in output: > output.append(x) The first one constructs a list, then points the name 'output' at it. The second one builds up a list, with 'output' pointing at it all the way. Your first one is more like: sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] output=[] _temp=[] for x in sample2: if x not in output: _temp.append(x) output=_temp You may want to consider using a set, instead. >>> {x+y for x in range(1,10) for y in range(1,10) if x!=y} {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17} ChrisA From gary.herron at islandtraining.com Thu May 9 02:54:24 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Wed, 08 May 2013 23:54:24 -0700 Subject: Append to python List In-Reply-To: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Message-ID: <518B4820.40507@islandtraining.com> On 05/08/2013 11:36 PM, RAHUL RAJ wrote: > Checkout the following code: > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > output=[] > output=[x for x in sample2 if x not in output] This statement is not doing what you expect. It is not building a list in the variable named output, it is building a list (anonymously) then binding it to the variable output once it's built. Therefore output is [] for the whole list building operation. The later operation works, because your *are* building the list in place as you go. > > the output I get is > 3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11 12 13 6 7 8 9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16 9 10 11 12 13 14 15 17 10 11 12 13 14 15 16 17 > > which contains duplicate values. > > > > > But if I do like this: > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > output=[] > for x in sample2: > if x not in output: > output.append(x) > > > the value of 'output' I get like this: > 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 > > I know that both the programs have the same functionality, but why do I have different outputs? > > Please help! From omrahulrajcse at gmail.com Thu May 9 04:18:51 2013 From: omrahulrajcse at gmail.com (RAHUL RAJ) Date: Thu, 9 May 2013 01:18:51 -0700 (PDT) Subject: Append to python List In-Reply-To: References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Message-ID: Then what about this code part? [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] and the following code part: for x in [1,2,3]: for y in [3,1,4]: if x != y: combs.append((x, y)) On Thursday, May 9, 2013 12:24:24 PM UTC+5:30, Gary Herron wrote: > On 05/08/2013 11:36 PM, RAHUL RAJ wrote: > > > Checkout the following code: > > > > > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > > > output=[] > > > > > output=[x for x in sample2 if x not in output] > > This statement is not doing what you expect. It is not building a list > > in the variable named output, it is building a list (anonymously) then > > binding it to the variable output once it's built. Therefore output is > > [] for the whole list building operation. > > > > The later operation works, because your *are* building the list in place > > as you go. > > > > > > > > the output I get is > > > 3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11 12 13 6 7 8 9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16 9 10 11 12 13 14 15 17 10 11 12 13 14 15 16 17 > > > > > > which contains duplicate values. > > > > > > > > > > > > > > > But if I do like this: > > > > > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > > > output=[] > > > for x in sample2: > > > if x not in output: > > > output.append(x) > > > > > > > > > the value of 'output' I get like this: > > > 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 > > > > > > I know that both the programs have the same functionality, but why do I have different outputs? > > > > > > Please help! From omrahulrajcse at gmail.com Thu May 9 04:19:43 2013 From: omrahulrajcse at gmail.com (RAHUL RAJ) Date: Thu, 9 May 2013 01:19:43 -0700 (PDT) Subject: Append to python List In-Reply-To: References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Message-ID: <51959fcc-8653-47c0-bc46-b30e82e40f35@googlegroups.com> I'm getting same output for both code parts, why not for th code parts in question? On Thursday, May 9, 2013 1:48:51 PM UTC+5:30, RAHUL RAJ wrote: > Then what about this code part? > > > > [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] > > > > and the following code part: > > > > for x in [1,2,3]: > > for y in [3,1,4]: > > if x != y: > > combs.append((x, y)) > > > > > > On Thursday, May 9, 2013 12:24:24 PM UTC+5:30, Gary Herron wrote: > > > On 05/08/2013 11:36 PM, RAHUL RAJ wrote: > > > > > > > Checkout the following code: > > > > > > > > > > > > > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > > > > > > > output=[] > > > > > > > > > > > > > > > > > output=[x for x in sample2 if x not in output] > > > > > > This statement is not doing what you expect. It is not building a list > > > > > > in the variable named output, it is building a list (anonymously) then > > > > > > binding it to the variable output once it's built. Therefore output is > > > > > > [] for the whole list building operation. > > > > > > > > > > > > The later operation works, because your *are* building the list in place > > > > > > as you go. > > > > > > > > > > > > > > > > > > > > the output I get is > > > > > > > 3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11 12 13 6 7 8 9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16 9 10 11 12 13 14 15 17 10 11 12 13 14 15 16 17 > > > > > > > > > > > > > > which contains duplicate values. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > But if I do like this: > > > > > > > > > > > > > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > > > > > > > output=[] > > > > > > > for x in sample2: > > > > > > > if x not in output: > > > > > > > output.append(x) > > > > > > > > > > > > > > > > > > > > > the value of 'output' I get like this: > > > > > > > 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 > > > > > > > > > > > > > > I know that both the programs have the same functionality, but why do I have different outputs? > > > > > > > > > > > > > > Please help! From steve+comp.lang.python at pearwood.info Thu May 9 07:14:47 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 May 2013 11:14:47 GMT Subject: Append to python List References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Message-ID: <518b8527$0$29997$c3e8da3$5496439d@news.astraweb.com> On Thu, 09 May 2013 01:18:51 -0700, RAHUL RAJ wrote: > Then what about this code part? What about it? > [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] > > and the following code part: > > for x in [1,2,3]: > for y in [3,1,4]: > if x != y: > combs.append((x, y)) Apart from not defined combs, those two pieces of code are equivalent. So what is your question? -- Steven From kreta06 at gmail.com Thu May 9 02:59:16 2013 From: kreta06 at gmail.com (kreta06) Date: Thu, 9 May 2013 08:59:16 +0200 Subject: Forming a small python programming group Message-ID: Hi All, I'm looking for one or two medium-advanced python programmers to practice programming on a Windows 7 platform. In addition, any interests in writing python code to query Microsoft SQL databases (2005-2008) is also welcomed. I've coded in python 2.7 and currently am trying to make the switch to 3.2 as there seem to be some changes to the syntax. I'm only available on a weekend basis and occasionally some week days. Therefore, in terms of output, probably 2-3 scripts per month. If you are interested in working with me, please send me an email on similar interests and we can start this next week. Best regards, Sue From tjreedy at udel.edu Thu May 9 04:11:55 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Thu, 09 May 2013 04:11:55 -0400 Subject: Forming a small python programming group In-Reply-To: References: Message-ID: On 5/9/2013 2:59 AM, kreta06 wrote: > Hi All, > > I'm looking for one or two medium-advanced python programmers to > practice programming on a Windows 7 platform. In addition, any > interests in writing python code to query Microsoft SQL databases > (2005-2008) is also welcomed. > > I've coded in python 2.7 and currently am trying to make the switch to > 3.2 as there seem to be some changes to the syntax. Start with 3.3 unless you *must* use 3.2. There is one less syntax change and a better unicode string implementation. From chandan_psr at yahoo.co.in Thu May 9 11:35:53 2013 From: chandan_psr at yahoo.co.in (chandan kumar) Date: Thu, 9 May 2013 23:35:53 +0800 (SGT) Subject: Urgent:Serial Port Read/Write Message-ID: <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com> Hi all,I'm new to python and facing issue using serial in python.I'm facing the below error? ? ? ser.write(port,command)NameError: global name 'ser' is not defined Please find the attached script and let me know whats wrong in my script and also how can i read data from serial port for the ?same script. Best Regards,Chandan. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: RunScripts.py URL: From rosuav at gmail.com Thu May 9 11:49:05 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 01:49:05 +1000 Subject: Urgent:Serial Port Read/Write In-Reply-To: <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com> References: <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com> Message-ID: On Fri, May 10, 2013 at 1:35 AM, chandan kumar wrote: > > Hi all, > I'm new to python and facing issue using serial in python.I'm facing the below error > > ser.write(port,command) > NameError: global name 'ser' is not defined > > Please find the attached script and let me know whats wrong in my script and also how can i read data from serial port for the same script. You're assigning to 'ser' inside OpenPort(), but then trying to use it in WriteSerialData(). You'll need to declare 'global ser' in OpenPort to make this work. Alternatively, you may want to cut down on the number of functions you have, since they're called in only one place anyway and have to share state. ChrisA From fpm at u.washington.edu Thu May 9 11:44:52 2013 From: fpm at u.washington.edu (Frank Miles) Date: Thu, 9 May 2013 15:44:52 +0000 (UTC) Subject: Urgent:Serial Port Read/Write References: Message-ID: On Thu, 09 May 2013 23:35:53 +0800, chandan kumar wrote: > Hi all,I'm new to python and facing issue using serial in python.I'm > facing the below error > ? ? ser.write(port,command)NameError: global name 'ser' is not defined > Please find the attached script and let me know whats wrong in my script > and also how can i read data from serial port for the ?same script. [snip] > if __name__ == "__main__": > > CurrDir=os.getcwd() > files = glob.glob('./*pyc') > for f in files: > os.remove(f) > OpenPort(26,9600) > SetRequest(ER_Address) > #SysAPI.SetRequest('ER',ER_Address) > > print "Test Scripts Execution complete" What kind of 'port' is 26? Is that valid on your machine? My guess is that is NULL (because the open is failing, likely due to the port selection), leading to your subsequent problems. HTH.. From python at mrabarnett.plus.com Thu May 9 11:54:34 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 May 2013 16:54:34 +0100 Subject: Urgent:Serial Port Read/Write In-Reply-To: <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com> References: <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com> Message-ID: <518BC6BA.3030208@mrabarnett.plus.com> On 09/05/2013 16:35, chandan kumar wrote: > Hi all, > I'm new to python and facing issue using serial in python.I'm facing the > below error > > * ser.write(port,command)* > *NameError: global name 'ser' is not defined* > * > * > Please find the attached script and let me know whats wrong in my script > and also how can i read data from serial port for the same script. > > > Best Regards, > Chandan. > > > RunScripts.py > > > import time > import os > import serial > import glob > > > ER_Address = [[0x0A,0x01,0x08,0x99,0xBB,0xBB,0xBB,0xBB,0xBB,0xBB,0xBB]] > """ > ---------------------------------------------------------------------------------------------------------------------------- > Function Name: RunSequence > ---------------------------------------------------------------------------------------------------------------------------- > Function Description: > --------------------- > A RunSequence function has Multiple calls to the RunSuite function, each call is a single testcase consisting of all the parameters > required by the RunTesSuite. > ----------------------------------------------------------------------------------------------------------------------------- > """ > def WriteSerialData(command): 'ser' isn't a local variable (local to this function, that is), nor is it a global variable (global in this file). > ser.write(port,command) > > def SendPacket(Packet): > str = chr(len(Packet)) + Packet #Concatenation of Packet with the PacketLength > print str > WriteSerialData(str) > > > def CreateFrame(Edata): It's more efficient to build a list of the characters and then join them together into a string in one step than to build the string one character at a time. Also, indexing into the list is considered 'unPythonic'; it's much simpler to do it this way: return chr(0x12) + "".join(chr(d) for d in Edata) > evt = chr(0x12) > evt = evt + chr(Edata[0]) > for i in range (1, len(Edata)): > evt = evt + chr(Edata[i]) > return evt > > def SetRequest(data): > print data > new = [] > new = sum(data, []) > Addr = CreateFrame(new) > SendPacket(Addr) > print "SendPacket Done" > ReadPacket() > > > def OpenPort(COMPort,BAUDRATE): > """ > This function reads the serial port and writes it. > """ > comport=COMPort > BaudRate=BAUDRATE > try: > ser = serial.Serial( > port=comport, > baudrate=BaudRate, > bytesize=serial.EIGHTBITS, > parity=serial.PARITY_NONE, > stopbits=serial.STOPBITS_ONE, > timeout=10, > xonxoff=0, > rtscts=0, > dsrdtr=0 > ) > > if ser.isOpen(): > print "Port Opened" > ser.write("Chandan") > string1 = ser.read(8) > print string1 This function returns either ser ... > return ser > else: > print "Port CLosed" > ser.close() ... or 3 ... > return 3 > except serial.serialutil.SerialException: > print "Exception" > ser.close() > ... or None! > > > > > > if __name__ == "__main__": > > CurrDir=os.getcwd() > files = glob.glob('./*pyc') > for f in files: > os.remove(f) OpenPort returns either ser or 3 or None, but the result is just discarded. > OpenPort(26,9600) > SetRequest(ER_Address) > #SysAPI.SetRequest('ER',ER_Address) > > print "Test Scripts Execution complete" > From rosuav at gmail.com Thu May 9 11:54:48 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 10 May 2013 01:54:48 +1000 Subject: Urgent:Serial Port Read/Write In-Reply-To: <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com> References: <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com> Message-ID: On Fri, May 10, 2013 at 1:35 AM, chandan kumar wrote: > Please find the attached script and let me know whats wrong in my script > and also how can i read data from serial port for the same script. Don't do this: except serial.serialutil.SerialException: print "Exception" ser.close() Just let it propagate up and show you a traceback. There is absolutely no value in suppressing an exception only to print an unhelpful message. ChrisA From tavares at fe.up.pt Thu May 9 13:27:23 2013 From: tavares at fe.up.pt (tavares at fe.up.pt) Date: Thu, 9 May 2013 10:27:23 -0700 (PDT) Subject: IV ECCOMAS Thematic Conference VipIMAGE 2013: LAST CALL Message-ID: Dear Colleague, Attending several requests, the organizing committee has extended the submission of abstracts for the International Conference VipIMAGE 2013 - IV ECCOMAS THEMATIC CONFERENCE ON COMPUTATIONAL VISION AND MEDICAL IMAGE PROCESSING (www.fe.up.pt/~vipimage) to be held October 14-16, 2013, in Melia Madeira Mare Hotel, Madeira Island, Funchal, Portugal, to May 20th. Possible Topics (not limited to) ? Signal and Image Processing ? Computational Vision ? Medical Imaging ? Physics of Medical Imaging ? Tracking and Analysis of Movement ? Simulation and Modeling ? Image Acquisition ? Industrial Applications ? Shape Reconstruction ? Objects Segmentation, Matching, Simulation ? Data Interpolation, Registration, Acquisition and Compression ? 3D Vision ? Virtual Reality ? Visual Inspection ? Software Development for Image Processing and Analysis ? Computer Aided Diagnosis, Surgery, Therapy, and Treatment ? Computational Bioimaging and Visualization ? Telemedicine Systems and their Applications Invited Lecturers ? Daniel Rueckert - Imperial College London, UK ? Dimitris N. Metaxas - Rutgers University, USA ? Durval C. Costa - Champalimaud Foundation, Portugal ? James S Duncan - Yale School of Medicine, USA ? Milan Sonka - The University of Iowa, USA ? Richard Bowden - University of Surrey, UK Thematic Sessions Proposals to organize Thematic Session under the auspicious of VipIMAGE 2013 are welcome. The proposals should be submitted by email to the conference co-chairs (tavares at fe.up.pt, rnatal at fe.up.pt). Confirmed Thematic Sessions ? Imaging of Biological Flows: trends and challenges ? Trabecular Bone Characterization: New trends and challenges ? Computational Vision and Image Processing applied to Dental Medicine Publications ? Proceedings: The proceedings book will be published by the Taylor & Francis Group (www.balkema.nl/instructions.asp) and indexed by Thomson Reuters Conference Proceedings Citation Index, IET Inspect and Elsevier Scopus. ? Springer Book: A book with 20 invited works from the ones presented in the conference will be published by Springer under the book series ?Lecture Notes in Computational Vision and Biomechanics? (www.springer.com/series/8910). ? Journal Publication: A dedicated special issue of the Taylor & Francis International Journal ?Computer Methods in Biomechanics and Biomedical Engineering: Imaging & Visualization? (www.tandfonline.com/tciv) will be published with extended versions of the best works presented in the conference. Important dates ? Deadline for (2-4 pages) Abstracts: May 20, 2013 (LAST POSTPONED) ? Authors Notification: June 10, 2013 ? Deadline to upload Lectures and Papers: July 1, 2013 We are looking forward to see you in Funchal next October. Kind regards, Jo?o Manuel R. S. Tavares Renato Natal Jorge (conference co-chairs) PS. For further details, please, have a look in the conference website at: www.fe.up.pt/~vipimage, or in the conference Facebook page at: www.facebook.com/pages/Vipimage/237980719665456, or join the LinkedIn conference group at: http://www.linkedin.com/groups?gid=4752820&trk=hb_side_g From demianbrecht at gmail.com Thu May 9 19:07:52 2013 From: demianbrecht at gmail.com (Demian Brecht) Date: Thu, 9 May 2013 16:07:52 -0700 Subject: [ANN] flask-canvas Message-ID: A Flask extension for Facebook canvas-based applications. https://github.com/demianbrecht/flask-canvas Docs available on RTD: https://flask-canvas.readthedocs.org/en/latest/ -- Demian Brecht http://demianbrecht.github.com From yanxiaopei199 at gmail.com Thu May 9 22:33:20 2013 From: yanxiaopei199 at gmail.com (Thanatos xiao) Date: Fri, 10 May 2013 10:33:20 +0800 Subject: python call golang Message-ID: Hey ! Now! I have written a python script . I want to call a golang script in python script. Who can give me some advices? thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiosantosart at gmail.com Fri May 10 03:01:32 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Fri, 10 May 2013 08:01:32 +0100 Subject: python call golang In-Reply-To: References: Message-ID: On 10 May 2013 03:37, "Thanatos xiao" wrote: > > Hey ! > > Now! I have written a python script . I want to call a golang script in python script. > Who can give me some advices? > > thanks! Check out the subprocess module. You can use it to call other programs from python and get their error codes and output. -------------- next part -------------- An HTML attachment was scrubbed... URL: From miki.tebeka at gmail.com Fri May 10 18:21:29 2013 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Fri, 10 May 2013 15:21:29 -0700 (PDT) Subject: python call golang In-Reply-To: References: Message-ID: > Now! I have written a python script . I want to call a golang script in python script. > Who can give me some advices? See http://gopy.qur.me/extensions/examples.html and http://www.artima.com/weblogs/viewpost.jsp?thread=333589 From bv8bv8bv8 at gmail.com Fri May 10 15:44:59 2013 From: bv8bv8bv8 at gmail.com (BV BV) Date: Fri, 10 May 2013 12:44:59 -0700 (PDT) Subject: ?????????? WHY DID PROPHET MUHAMMAD MARRY AISHA THE YOUNG GIRL Message-ID: <969305c6-aa72-41ec-b889-2e2b2d4b28e8@gm7g2000vbb.googlegroups.com> WHY DID PROPHET MUHAMMAD MARRY AISHA THE YOUNG GIRL? This is an important book talks about a common issue misunderstood but misused by lots of thinkers and orientalists. It is ?Why did Prophet Muhammad marry Aisha the young girl?? The author shows the reason behind their discussion. They want to distort the picture of Prophet Muhammad not criticize the marriage of young girl. Also if this kind of marriage was strange, why did not the disbelievers of Quraish use it as a pretext against Muhammad?! The author discusses other topics such as: Europe also allows marrying young girls, the age of consent in most countries worldwide. http://www.islamhouse.com/330161/en/en/books/Why_Did_Prophet_Muhammad_Marry_Aisha_the_Young_Girl? thank you From francis.brosnan at gmail.com Sat May 11 14:23:55 2013 From: francis.brosnan at gmail.com (francis.brosnan at gmail.com) Date: Sat, 11 May 2013 11:23:55 -0700 (PDT) Subject: Right way to initialize python embedded in a multi-threaded application In-Reply-To: <19ed5abe-7283-4dad-bd8f-b5209b3d974b@3g2000vbx.googlegroups.com> References: <19ed5abe-7283-4dad-bd8f-b5209b3d974b@3g2000vbx.googlegroups.com> Message-ID: <0edf6083-4f49-4a6e-834a-7d5fd1f0c774@googlegroups.com> Hi, Maybe you already fixed the issue, but for the record, I've got the same problem and finally it turned out that I was calling PyEval_InitThreads twice and also after fixing that, I also had to move the call to PyEval_ReleaseLock(); at the end of the entire initialization (not just after PyEval_initThreads). The key thing there is to follow: <> Py_Initialize (); PyEval_InitThreads(); /* now call here to initialize all python code by loading external files or internal module loading (i.e. Py_InitModule3) */ /* ..and now, once no more Python C/API call is required, release the GIL so other threads can come into play */ PyEval_ReleaseLock (); <> /* wait til gil acquired */ state = PyGILState_Ensure(); /* your code */ /* release GIL */ PyGILState_Release (state); Hope it helps, Cheers! > I'm embedding python in a multi-threaded C application. > > I've taken care to wrap every call to the Python C API with > > > > gstate = PyGILState_Ensure(); > > // call python code > > PyGILState_Release(gstate); > > > > But I'm stumped with what to do in the initialization. > > Right after the call to Py_IsInitialized() I've added a call: > > > > PyEval_InitThreads(); > > > > The docs say that this function leaves the GIL locked when it returns. > > I do some more initializations like importing modules and then I call > > > > PyEval_ReleaseLock(); > > > > This seems to cause a problem since not long after a call to > > PyGILState_Release(gstate) that's made in a different thread crashes. > > with > > > > "Fatal Python error: This thread state must be current when releasing" > > > > If I don't do the call to PyEval_ReleaseLock() in the main thread > > right after initialization, the GIL seems to be released > > after the first PyGILState_Ensure() - PyGILState_Release() pair. > > > > So what am I doing wrong here? > > What is the correct way of initializing a multi-threaded application? From francis.brosnan at gmail.com Sat May 11 16:00:44 2013 From: francis.brosnan at gmail.com (francis.brosnan at gmail.com) Date: Sat, 11 May 2013 13:00:44 -0700 (PDT) Subject: Right way to initialize python embedded in a multi-threaded application In-Reply-To: <0edf6083-4f49-4a6e-834a-7d5fd1f0c774@googlegroups.com> References: <19ed5abe-7283-4dad-bd8f-b5209b3d974b@3g2000vbx.googlegroups.com> <0edf6083-4f49-4a6e-834a-7d5fd1f0c774@googlegroups.com> Message-ID: Just clarify there's no problem about calling twice to PyEval_InitThreads () as indicated by Python's doc. > Hi, > > > > Maybe you already fixed the issue, but for the record, I've got the same > > problem and finally it turned out that I was calling PyEval_InitThreads twice > > and also after fixing that, I also had to move the call to PyEval_ReleaseLock(); at the end of the entire initialization (not just after PyEval_initThreads). > > > > The key thing there is to follow: > > > > <> > > Py_Initialize (); > > PyEval_InitThreads(); > > /* now call here to initialize all python code by loading external files > > or internal module loading (i.e. Py_InitModule3) */ > > /* ..and now, once no more Python C/API call is required, release > > the GIL so other threads can come into play */ > > PyEval_ReleaseLock (); > > > > <> > > /* wait til gil acquired */ > > state = PyGILState_Ensure(); > > /* your code */ > > /* release GIL */ > > PyGILState_Release (state); > > > > Hope it helps, Cheers! > > > > > > > I'm embedding python in a multi-threaded C application. > > > > > > I've taken care to wrap every call to the Python C API with > > > > > > > > > > > > gstate = PyGILState_Ensure(); > > > > > > // call python code > > > > > > PyGILState_Release(gstate); > > > > > > > > > > > > But I'm stumped with what to do in the initialization. > > > > > > Right after the call to Py_IsInitialized() I've added a call: > > > > > > > > > > > > PyEval_InitThreads(); > > > > > > > > > > > > The docs say that this function leaves the GIL locked when it returns. > > > > > > I do some more initializations like importing modules and then I call > > > > > > > > > > > > PyEval_ReleaseLock(); > > > > > > > > > > > > This seems to cause a problem since not long after a call to > > > > > > PyGILState_Release(gstate) that's made in a different thread crashes. > > > > > > with > > > > > > > > > > > > "Fatal Python error: This thread state must be current when releasing" > > > > > > > > > > > > If I don't do the call to PyEval_ReleaseLock() in the main thread > > > > > > right after initialization, the GIL seems to be released > > > > > > after the first PyGILState_Ensure() - PyGILState_Release() pair. > > > > > > > > > > > > So what am I doing wrong here? > > > > > > What is the correct way of initializing a multi-threaded application? From citizenkant at gmail.com Sat May 11 16:03:15 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Sat, 11 May 2013 22:03:15 +0200 Subject: Python for philosophers Message-ID: Hi, this could be seen as an extravagant subject but that is not my original purpose. I still don't know if I want to become a programmer or not. At this moment I'm just inspecting the environment. I'm making my way to Python (and OOP in general) from a philosophical perspective or point of view and try to set the more global definition of Python's core as an "entity". In order to do that, and following Wittgenstein's indication about that the true meaning of words doesn't reside on dictionaries but in the use that we make of them, the starting question I make to myself about Python is: which is the single and most basic use of Python as the entity it is? I mean, beside programming, what's the single and most basic result one can expect from "interacting" with it directly (interactive mode)? I roughly came to the idea that Python could be considered as an *economic mirror for data*, one that mainly *mirrors* the data the programmer types on its black surface, not exactly as the programmer originally typed it, but expressed in the most economic way possible. That's to say, for example, if one types >>>1+1 Python reflects >>>2. When data appears between apostrophes, then the mirror reflects, again, the same but expressed in the most economic way possible (that's to say without the apostrophes). So, would it be legal (true) to define Python's core as an entity that mirrors whatever data one presents to it (or feed it with) showing back the most shortened expression of that data? Don't get me wrong. I can see the big picture and the amazing things that programmers write on Python, it's just that my question points to the lowest level of it's existence. Thanks a lot for your time. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dreamingforward at gmail.com Sat May 11 16:10:45 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Sat, 11 May 2013 13:10:45 -0700 Subject: Python for philosophers In-Reply-To: References: Message-ID: On Sat, May 11, 2013 at 1:03 PM, Citizen Kant wrote: >[...] the starting question I make to myself about Python is: which is the single > and most basic use of Python as the entity it is? I mean, beside > programming, what's the single and most basic result one can expect from > "interacting" with it directly (interactive mode)? I roughly came to the > idea that Python could be considered as an economic mirror for data, one > that mainly mirrors the data the programmer types on its black surface, not > exactly as the programmer originally typed it, but expressed in the most > economic way possible. That's to say, for example, if one types >>>1+1 > Python reflects >>>2. When data appears between apostrophes, then the mirror > reflects, again, the same but expressed in the most economic way possible > (that's to say without the apostrophes). Wow. You must be from another planet. Find Socrates if you wish to know these things. He's from there also. A-dam. From fabiosantosart at gmail.com Sat May 11 16:21:47 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sat, 11 May 2013 21:21:47 +0100 Subject: Python for philosophers In-Reply-To: References: Message-ID: On 11 May 2013 21:07, "Citizen Kant" wrote: > > Hi, > this could be seen as an extravagant subject but that is not my original purpose. I still don't know if I want to become a programmer or not. At this moment I'm just inspecting the environment. I'm making my way to Python (and OOP in general) from a philosophical perspective or point of view and try to set the more global definition of Python's core as an "entity". In order to do that, and following Wittgenstein's indication about that the true meaning of words doesn't reside on dictionaries but in the use that we make of them, the starting question I make to myself about Python is: which is the single and most basic use of Python as the entity it is? I mean, beside programming, what's the single and most basic result one can expect from "interacting" with it directly (interactive mode)? I roughly came to the idea that Python could be considered as an economic mirror for data, one that mainly mirrors the data the programmer types on its black surface, not exactly as the programmer originally typed it, but expressed in the most economic way possible. That's to say, for example, if one types >>>1+1 Python reflects >>>2. When data appears between apostrophes, then the mirror reflects, again, the same but expressed in the most economic way possible (that's to say without the apostrophes). > > So, would it be legal (true) to define Python's core as an entity that mirrors whatever data one presents to it (or feed it with) showing back the most shortened expression of that data? > > Don't get me wrong. I can see the big picture and the amazing things that programmers write on Python, it's just that my question points to the lowest level of it's existence. > > Thanks a lot for your time. > I can't tell if you are being sarcastic but I'll reply anyway. Python does not necessarily shorten data. The Python machine is the house for your representations of data, your own "mirrors". When you program you are asking python to acknowledge your representations and to do work on them as you specify. Both of these tasks are expressed in code. The first is the simplest, where you create your classes. It is optional since you may use no classes at all and instead use files, text and numbers, or classes given by someone else. The second is where you give the orders and lay a script (as in a movie script, or a game script) out. You can create and command many representations of data in order to make your program fulfill its purpose. You can also make choices according to the current state of your data. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wuwei23 at gmail.com Sat May 11 18:16:04 2013 From: wuwei23 at gmail.com (alex23) Date: Sat, 11 May 2013 15:16:04 -0700 (PDT) Subject: Python for philosophers References: Message-ID: On 12 May, 06:10, Mark Janssen wrote: > Wow. ?You must be from another planet. ?Find Socrates if you wish to > know these things. ?He's from there also. Now now, there's no need for a turf war, there's plenty of room on this list for crazies. From rustompmody at gmail.com Sun May 12 00:22:11 2013 From: rustompmody at gmail.com (rusi) Date: Sat, 11 May 2013 21:22:11 -0700 (PDT) Subject: Python for philosophers References: Message-ID: <3ecb79eb-4b62-4da9-bdaa-a8d4f8d008c3@zo5g2000pbb.googlegroups.com> On May 12, 3:16?am, alex23 wrote: > On 12 May, 06:10, Mark Janssen wrote: > > > Wow. ?You must be from another planet. ?Find Socrates if you wish to > > know these things. ?He's from there also. > > Now now, there's no need for a turf war, there's plenty of room on > this list for crazies. I'm reminded of this: Conversation between inmate and attendant in an asylum Inmate: I am Napoleon Attendant: Yes of course. But how did you know that? Inmate: God himself told me s? [Loud voice from another corner] I told you no such thing! From rosuav at gmail.com Sun May 12 00:47:45 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 12 May 2013 14:47:45 +1000 Subject: Python for philosophers In-Reply-To: <3ecb79eb-4b62-4da9-bdaa-a8d4f8d008c3@zo5g2000pbb.googlegroups.com> References: <3ecb79eb-4b62-4da9-bdaa-a8d4f8d008c3@zo5g2000pbb.googlegroups.com> Message-ID: On Sun, May 12, 2013 at 2:22 PM, rusi wrote: > On May 12, 3:16 am, alex23 wrote: >> On 12 May, 06:10, Mark Janssen wrote: >> >> > Wow. You must be from another planet. Find Socrates if you wish to >> > know these things. He's from there also. >> >> Now now, there's no need for a turf war, there's plenty of room on >> this list for crazies. > > I'm reminded of this: > > Conversation between inmate and attendant in an asylum > > Inmate: I am Napoleon > Attendant: Yes of course. But how did you know that? > Inmate: God himself told me s? > [Loud voice from another corner] I told you no such thing! Who's been telling you of private conversations between Steven and me? ChrisA From rustompmody at gmail.com Sun May 12 00:45:12 2013 From: rustompmody at gmail.com (rusi) Date: Sat, 11 May 2013 21:45:12 -0700 (PDT) Subject: Python for philosophers References: <3ecb79eb-4b62-4da9-bdaa-a8d4f8d008c3@zo5g2000pbb.googlegroups.com> Message-ID: On May 12, 9:22?am, rusi wrote: > On May 12, 3:16?am, alex23 wrote: > > > On 12 May, 06:10, Mark Janssen wrote: > > > > Wow. ?You must be from another planet. ?Find Socrates if you wish to > > > know these things. ?He's from there also. > > > Now now, there's no need for a turf war, there's plenty of room on > > this list for crazies. > > I'm reminded of this: > > Conversation between inmate and attendant in an asylum > > Inmate: I am Napoleon > Attendant: Yes of course. But how did you know that? > Inmate: God himself told me s? > [Loud voice from another corner] I told you no such thing! I have on occasion expressed that newcomers to this list should be treated with more gentleness than others. And since my own joking may be taken amiss, let me hasten to add (to the OP -- Citizen Kant) What you are looking for is more in line with what is called 'rewriting systems' And the shortening you talk of is usually called 'canonical form' or 'normal form' Python is closer to such than traditional imperative/OO languages like C/C++/Java, though other languages -- usually called 'functional language' are generally closer to this ideal. The most mainstream of these today is probably 'Haskell' For your purposes however you may want to look at functional languages that are more explicitly based on rewriting such as 'Pure' (earlier 'Q') For last http://en.wikipedia.org/wiki/Pure_%28programming_language%29 For rest: Ive tried to put into quotes things that could he helpful starting points for search engine research From steve+comp.lang.python at pearwood.info Sun May 12 01:17:57 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 May 2013 05:17:57 GMT Subject: Python for philosophers References: <3ecb79eb-4b62-4da9-bdaa-a8d4f8d008c3@zo5g2000pbb.googlegroups.com> Message-ID: <518f2605$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sat, 11 May 2013 21:45:12 -0700, rusi wrote: > I have on occasion expressed that newcomers to this list should be > treated with more gentleness than others. And since my own joking may be > taken amiss, let me hasten to add (to the OP -- Citizen Kant) A noble aim, but I have a feeling that "Citizen Kant" is version 2.0 of 88888 Dihedral. Of course, I could be wrong. -- Steven From jeanpierreda at gmail.com Sun May 12 04:15:30 2013 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sun, 12 May 2013 04:15:30 -0400 Subject: Python for philosophers In-Reply-To: <518f2605$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <3ecb79eb-4b62-4da9-bdaa-a8d4f8d008c3@zo5g2000pbb.googlegroups.com> <518f2605$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, May 12, 2013 at 1:17 AM, Steven D'Aprano wrote: > On Sat, 11 May 2013 21:45:12 -0700, rusi wrote: > >> I have on occasion expressed that newcomers to this list should be >> treated with more gentleness than others. And since my own joking may be >> taken amiss, let me hasten to add (to the OP -- Citizen Kant) > > A noble aim, but I have a feeling that "Citizen Kant" is version 2.0 of > 88888 Dihedral. > > Of course, I could be wrong. Without benefit of the doubt, kindness is impossible. I would suggest giving newcomers at least that much. -- Devin From steve+comp.lang.python at pearwood.info Sun May 12 10:32:07 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 May 2013 14:32:07 GMT Subject: Python for philosophers References: <3ecb79eb-4b62-4da9-bdaa-a8d4f8d008c3@zo5g2000pbb.googlegroups.com> <518f2605$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <518fa7e7$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sun, 12 May 2013 04:15:30 -0400, Devin Jeanpierre wrote: > On Sun, May 12, 2013 at 1:17 AM, Steven D'Aprano > wrote: >> On Sat, 11 May 2013 21:45:12 -0700, rusi wrote: >> >>> I have on occasion expressed that newcomers to this list should be >>> treated with more gentleness than others. And since my own joking may >>> be taken amiss, let me hasten to add (to the OP -- Citizen Kant) >> >> A noble aim, but I have a feeling that "Citizen Kant" is version 2.0 of >> 88888 Dihedral. >> >> Of course, I could be wrong. > > Without benefit of the doubt, kindness is impossible. That is a logical non sequitor. One can choose to be kind to someone even if you have no doubt that they do not deserve it. Besides, kindness is hard to define. Is it kinder to give somebody what they want, or what they need? > I would suggest giving newcomers at least that much. I'm happy to say that, based on Citizen Kant's second post, I'm now reasonably confident that (s)he is not a bot. No mere artificial intelligence could have written that second post. -- Steven From greg.ewing at canterbury.ac.nz Sat May 11 21:26:02 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sun, 12 May 2013 13:26:02 +1200 Subject: Python for philosophers In-Reply-To: References: Message-ID: Citizen Kant wrote: > I roughly came to the idea that Python could be > considered as an *economic mirror for data*, one that mainly *mirrors* > the data the programmer types on its black surface, not exactly as the > programmer originally typed it, but expressed in the most economic way > possible. At best, this would be true only for a very small subset of things that you can enter into the interactive interpreter. Even confining yourself to arithmetic expressions, there are problems. Consider: >>> 12**34 4922235242952026704037113243122008064L The input is 6 characters long, and the output is 37 characters long. Is that more "economical"? -- Greg From steve+comp.lang.python at pearwood.info Sun May 12 22:46:59 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 May 2013 02:46:59 GMT Subject: Python for philosophers References: Message-ID: <51905423$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sat, 11 May 2013 22:03:15 +0200, Citizen Kant wrote: > Hi, > this could be seen as an extravagant subject but that is not my original > purpose. I still don't know if I want to become a programmer or not. At > this moment I'm just inspecting the environment. Towards what purpose? Do you want to learn to program? If not, then why do you care about Python programming? What do you aim to get out of this exercise? > I'm making my way to > Python (and OOP in general) from a philosophical perspective or point of > view and try to set the more global definition of Python's core as an > "entity". What do you think "Python's core" means? What do you mean by "global definition"? What is an "entity"? > In order to do that, and following Wittgenstein's indication > about that the true meaning of words doesn't reside on dictionaries but > in the use that we make of them, the starting question I make to myself > about Python is: which is the single and most basic use of Python as the > entity it is? Programming. A programming language is an abstract system for performing computations. Or, if you prefer simple English, programming. Programming is what programming languages are for. That is *all* they are for. > I mean, beside programming, Your question pre-supposes a counter-factual. Namely that there exists something *more fundamental* to programming that Python is for. One might as well ask: "Aside from driving screws, what is the single and most basic use of a screwdriver?" Just because you can pound a small nail into soft wood using the handle of a screwdriver, does not mean that pounding nails is more fundamental to the screwdriver than driving screws. > what's the single and most > basic result one can expect from "interacting" with it directly > (interactive mode)? For your purposes, what is so special about interactive mode that you single it out in this way? Interactive mode is just like non-interactive mode, only the user interacts directly with the compiler, instead of indirectly. -- Steven From maniandram01 at gmail.com Tue May 14 23:51:44 2013 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Tue, 14 May 2013 20:51:44 -0700 (PDT) Subject: Python for philosophers In-Reply-To: References: Message-ID: <5f0543e3-7e26-42fa-a5dc-f4319d23da4e@googlegroups.com> On Sunday, 12 May 2013 01:33:15 UTC+5:30, Citizen Kant wrote: > Hi, > this could be seen as an extravagant subject but that is not my original purpose. I still don't know if I want to become a programmer or not. At this moment I'm just inspecting the environment. I'm making my way to Python (and OOP in general) from a philosophical perspective or point of view and try to set the more global definition of Python's core as an "entity". In order to do that, and following Wittgenstein's indication about that the true meaning of words doesn't reside on dictionaries but in the use that we make of them, the starting question I make to myself about Python is: which is the single and most basic use of Python as the entity it is? I mean, beside programming, what's the single and most basic result one can expect from "interacting" with it directly (interactive mode)? I roughly came to the idea that Python could be considered as an economic mirror for data, one that mainly mirrors the data the programmer types on its black surface, not exactly as the programmer originally typed it, but expressed in the most economic way possible. That's to say, for example, if one types >>>1+1 Python reflects >>>2. When data appears between apostrophes, then the mirror reflects, again, the same but expressed in the most economic way possible (that's to say without the apostrophes). > So, would it be legal (true) to define Python's core as an entity that mirrors whatever data one presents to it (or feed it with) showing back the most shortened expression of that data? > Don't get me wrong. I can see the big picture and the amazing things that programmers write on Python, it's just that my question points to the lowest level of it's existence. > Thanks a lot for your time. I expected some spam but this actually makes some sense. From ned at nedbatchelder.com Sat May 11 22:46:38 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Sat, 11 May 2013 22:46:38 -0400 Subject: Python for philosophers In-Reply-To: References: Message-ID: <518F028E.4080102@nedbatchelder.com> On 5/11/2013 4:03 PM, Citizen Kant wrote: > Hi, > this could be seen as an extravagant subject but that is not my > original purpose. I still don't know if I want to become a programmer > or not. At this moment I'm just inspecting the environment. I'm making > my way to Python (and OOP in general) from a philosophical perspective > or point of view and try to set the more global definition of Python's > core as an "entity". In order to do that, and following Wittgenstein's > indication about that the true meaning of words doesn't reside on > dictionaries but in the use that we make of them, the starting > question I make to myself about Python is: which is the single and > most basic use of Python as the entity it is? I mean, beside > programming, what's the single and most basic result one can expect > from "interacting" with it directly (interactive mode)? I roughly came > to the idea that Python could be considered as an *economic mirror for > data*, one that mainly *mirrors* the data the programmer types on its > black surface, not exactly as the programmer originally typed it, but > expressed in the most economic way possible. That's to say, for > example, if one types >>>1+1 Python reflects >>>2. When data appears > between apostrophes, then the mirror reflects, again, the same but > expressed in the most economic way possible (that's to say without the > apostrophes). > > So, would it be legal (true) to define Python's core as an entity that > mirrors whatever data one presents to it (or feed it with) showing > back the most shortened expression of that data? > > Don't get me wrong. I can see the big picture and the amazing things > that programmers write on Python, it's just that my question points to > the lowest level of it's existence. > > Thanks a lot for your time. > > Python is straightforward: you write instructions, and it executes them. At its core, that's all it does. Why does the core have to be any different than that? --Ned. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Sat May 11 23:14:19 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 11 May 2013 23:14:19 -0400 Subject: Python for philosophers In-Reply-To: References: Message-ID: On Sat, May 11, 2013 at 4:03 PM, Citizen Kant wrote: > Hi, > this could be seen as an extravagant subject but that is not my original > purpose. I still don't know if I want to become a programmer or not. My guess is that you don't want to be a programmer. Otherwise you would know that you did. > At this moment I'm just inspecting the environment. I'm making my way to > Python (and OOP in general) from a philosophical perspective or point of > view and try to set the more global definition of Python's core as an > "entity". In order to do that, and following Wittgenstein's indication > about that the true meaning of words doesn't reside on dictionaries but in > the use that we make of them, the starting question I make to myself about > Python is: which is the single and most basic use of Python as the entity > it is? I mean, beside programming, what's the single and most basic result > one can expect from "interacting" with it directly (interactive mode)? I > roughly came to the idea that Python could be considered as an *economic > mirror for data*, one that mainly *mirrors* the data the programmer types > on its black surface, not exactly as the programmer originally typed it, > but expressed in the most economic way possible. That's to say, for > example, if one types >>>1+1 Python reflects >>>2. When data appears > between apostrophes, then the mirror reflects, again, the same but > expressed in the most economic way possible (that's to say without the > apostrophes). > > So, would it be legal (true) to define Python's core as an entity that > mirrors whatever data one presents to it (or feed it with) showing back the > most shortened expression of that data? > > Don't get me wrong. I can see the big picture and the amazing things that > programmers write on Python, it's just that my question points to the > lowest level of it's existence. > > Thanks a lot for your time. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Joel Goldstick http://joelgoldstick.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From citizenkant at gmail.com Sun May 12 10:17:02 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Sun, 12 May 2013 16:17:02 +0200 Subject: Python for philosophers In-Reply-To: References: Message-ID: Thank you very much for your answers. I'm afraid that, at this stage, I must prevent myself from "knowing too much" about the subject. My idea here is trying to fill the gaps, mostly, using intuition. What I do here is to try to "understand". That's different from just knowing. Knowledge growth must be consequence of understanding's increasing. As the scope of my understanding increases, the more I look for increasing my knowledge. Never vice versa, because, knowing isn't like to be right, it's just knowing. Greg: your point is that ------------------------------------- >>> 12**34 492223524295202670403711324312 2008064L The input is 6 characters long, and the output is 37 characters long. Is that more "economical"? -------------------------------------------------------------------------- and that's a good one. But take in account that with "shortening" I refer to "according to Python's axiomatic parameters". What's "shorten" if expressed in Python? For example: I'm plainly aware that the word "python" looks shorten than "01110000 01111001 01110100 01101000 01101111 01101110". But it's shorten just for me and you and maybe for every single human, not for the computer. You type "python", and the language (so to speak) thinks "in my opinion you're not being economical enough coz with this you mean 01110000 01111001 01110100 01101000 01101111 01101110", and then mirrors the supposedly result in its particular context. My "shorten" points to what's shorten during and inside Python's runtime. Maybe It'd be good if I explain myself a bit more. What I'm trying here is to grasp Python from the game's abstraction point of view, as if it were, for example, chess. That's why I need a real_player to point me to: (so to speak, I wish I could express the ideas according to the python's syntax but that's out of my scope by now) games.python.board(like the chessboard but in abstract) # with board I'm not necessarily referring to the black on Python's command line or any other substitutes for that, but to its axiomatic context. Which are those unchangeable things that limit so firmly the dynamics of the game, and that in this context must be considered like hardware, the most material part of Python. Coz (in my neophyte opinion) these are the things that (in a more or less obvious way) limit "the dynamic of any attempt to introduce a change in this game". games.python.pieces(a finite number of named elements) # one example of what in Python (unless for me) seems to look like pieces are python.keywords(python2). Might be more entities that can be considered like pieces... games.python.start(the position that each piece must assume over the given board in order to conform "the sign of this kind of game's starting point") # following the example of the only pieces I've recognized so far, I would drive myself to think about the state of python.keywords Of course with "state" I could be referring to any kind of state. The only clue is that, as far I can see, is expected that those hypothetical states come in pairs, like state(available, unavailable) games.python.end(game's final point or highest achievement) #I never forget that checkmate is just a sign that can be observed looking at the board or "axiomatic structure that in the game remains static". That end_sign usually is, no more nor less than a transformation of the start_sign, transformation that, sometimes, shorten it. games.python.pieces.behavior(the legal or non erroneous modifications that can be made to the start_sign in order to convert it to end_sign) games.python.player.rear.avoid(what it must be avoided by any legal -non erroneous- means) # seems to be that the kind of things to be avoided are not precisely making errors, since Python will tell. Making errors is just something that's so illegal that simply doesn't take place. With "avoid" I mean what happens when you get checked by running code that doesn't lead to any error messages but at the same time doesn't give the expected result. My goal right now is to produce one or more abstracts that explain (mainly for myself but extensive to others) about how I deal with some problems related to our "search for emphaty" nature at the time of what we tend to consider "interaction" (learning to write in Python in interactive mode, or just programming, is one of that cases). In essence, due to Python's lack of empathy, one must adopt its shape and ways to (so to speak) "interact" with it. Python could be considered like a solitary game, and in my opinion could be taken as it is from the beginning, in order to properly understand what one exactly is doing. That seems to be the best way to properly "understand" Python; then knowledge will come, naturally as a perfect search of, exactly, the things that "understanding" needs. Any clue about this would be highly appreciated. 2013/5/11 Citizen Kant > Hi, > this could be seen as an extravagant subject but that is not my original > purpose. I still don't know if I want to become a programmer or not. At > this moment I'm just inspecting the environment. I'm making my way to > Python (and OOP in general) from a philosophical perspective or point of > view and try to set the more global definition of Python's core as an > "entity". In order to do that, and following Wittgenstein's indication > about that the true meaning of words doesn't reside on dictionaries but in > the use that we make of them, the starting question I make to myself about > Python is: which is the single and most basic use of Python as the entity > it is? I mean, beside programming, what's the single and most basic result > one can expect from "interacting" with it directly (interactive mode)? I > roughly came to the idea that Python could be considered as an *economic > mirror for data*, one that mainly *mirrors* the data the programmer types > on its black surface, not exactly as the programmer originally typed it, > but expressed in the most economic way possible. That's to say, for > example, if one types >>>1+1 Python reflects >>>2. When data appears > between apostrophes, then the mirror reflects, again, the same but > expressed in the most economic way possible (that's to say without the > apostrophes). > > So, would it be legal (true) to define Python's core as an entity that > mirrors whatever data one presents to it (or feed it with) showing back the > most shortened expression of that data? > > Don't get me wrong. I can see the big picture and the amazing things that > programmers write on Python, it's just that my question points to the > lowest level of it's existence. > > Thanks a lot for your time. > -- ------------------------------------------------------------------ ?Has le?do ?Las Novelas Prohibidas? ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sun May 12 10:51:28 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 00:51:28 +1000 Subject: Python for philosophers In-Reply-To: References: Message-ID: On Mon, May 13, 2013 at 12:17 AM, Citizen Kant wrote: > Maybe It'd be good if I explain myself a bit more. What I'm trying here is > to grasp Python from the game's abstraction point of view, as if it were, > for example, chess. Maybe you're going for something a little too complicated. Let's boil the question down to a much MUCH simpler subset: expression evaluation. And let's take Python itself right out of the picture, and work with only the following elements: * Parentheses ( ) * Multiplication and division * / * Addition and subtraction + - * Decimal integer constants 0123456789 This will give the basics of algebraic evaluation. It's quite fun to build a simple little expression evaluator that just does these few operations, and it's surprisingly useful (embed it within a command interpreter, for instance). Some time ago I wrote one into a Windows app (written in C++), and when I pulled it out just now and made it a stand-alone tool, the whole thing was only ~60 lines of code. Nice and easy to work with. So, let's take an expression and see what it really means. 2*(3+4)+6 One way to interpret this is with a stack. Here's how Python evaluates that expression: >>> def foo(): return 2*(three+4)+6 >>> dis.dis(foo) 2 0 LOAD_CONST 1 (2) 3 LOAD_GLOBAL 0 (three) 6 LOAD_CONST 2 (4) 9 BINARY_ADD 10 BINARY_MULTIPLY 11 LOAD_CONST 3 (6) 14 BINARY_ADD 15 RETURN_VALUE (I had to replace one of the constants with a global, to foil the optimizer) The LOAD statements add to an internal stack, the BINARY operations pop two operands and push the result. This is more-or-less the same technique as I used in my evaluator, except that instead of compiling it to code, I just march straight through, left to right, and so I had to maintain two stacks (one of operands, one of operators). Is this what you had in mind when you wanted to grasp Python's internals? Because it's pretty easy to explore, thanks to the dis module. There's a huge amount that can be learned about the interpreter, its optimizers, and so on, just by disassembling functions. Alternatively, if you're thinking on a more abstract level, leave Python aside altogether and pick up a book on language design. Also can be awesome fun, but completely different. ChrisA From llanitedave at veawb.coop Sun May 12 14:13:30 2013 From: llanitedave at veawb.coop (llanitedave) Date: Sun, 12 May 2013 11:13:30 -0700 (PDT) Subject: Python for philosophers In-Reply-To: References: Message-ID: <6e40860b-c0e5-46f9-9ba9-4ea4f4dc77c2@googlegroups.com> On Sunday, May 12, 2013 7:51:28 AM UTC-7, Chris Angelico wrote: > On Mon, May 13, 2013 at 12:17 AM, Citizen Kant wrote: > > > Maybe It'd be good if I explain myself a bit more. What I'm trying here is > > > to grasp Python from the game's abstraction point of view, as if it were, > > > for example, chess. > > > > Maybe you're going for something a little too complicated. Let's boil > > the question down to a much MUCH simpler subset: expression > > evaluation. And let's take Python itself right out of the picture, and > > work with only the following elements: > > > > * Parentheses ( ) > > * Multiplication and division * / > > * Addition and subtraction + - > > * Decimal integer constants 0123456789 > > > > This will give the basics of algebraic evaluation. It's quite fun to > > build a simple little expression evaluator that just does these few > > operations, and it's surprisingly useful (embed it within a command > > interpreter, for instance). Some time ago I wrote one into a Windows > > app (written in C++), and when I pulled it out just now and made it a > > stand-alone tool, the whole thing was only ~60 lines of code. Nice and > > easy to work with. > > > > So, let's take an expression and see what it really means. > > > > 2*(3+4)+6 > > > > One way to interpret this is with a stack. Here's how Python evaluates > > that expression: > > > > >>> def foo(): > > return 2*(three+4)+6 > > > > >>> dis.dis(foo) > > 2 0 LOAD_CONST 1 (2) > > 3 LOAD_GLOBAL 0 (three) > > 6 LOAD_CONST 2 (4) > > 9 BINARY_ADD > > 10 BINARY_MULTIPLY > > 11 LOAD_CONST 3 (6) > > 14 BINARY_ADD > > 15 RETURN_VALUE > > > > (I had to replace one of the constants with a global, to foil the optimizer) > > > > The LOAD statements add to an internal stack, the BINARY operations > > pop two operands and push the result. This is more-or-less the same > > technique as I used in my evaluator, except that instead of compiling > > it to code, I just march straight through, left to right, and so I had > > to maintain two stacks (one of operands, one of operators). > > > > Is this what you had in mind when you wanted to grasp Python's > > internals? Because it's pretty easy to explore, thanks to the dis > > module. There's a huge amount that can be learned about the > > interpreter, its optimizers, and so on, just by disassembling > > functions. Alternatively, if you're thinking on a more abstract level, > > leave Python aside altogether and pick up a book on language design. > > Also can be awesome fun, but completely different. > > > > ChrisA No, that won't help. You're trying to give him knowledge; he wants understanding. From rosuav at gmail.com Sun May 12 17:48:28 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 07:48:28 +1000 Subject: Python for philosophers In-Reply-To: <6e40860b-c0e5-46f9-9ba9-4ea4f4dc77c2@googlegroups.com> References: <6e40860b-c0e5-46f9-9ba9-4ea4f4dc77c2@googlegroups.com> Message-ID: On Mon, May 13, 2013 at 4:13 AM, llanitedave wrote: > On Sunday, May 12, 2013 7:51:28 AM UTC-7, Chris Angelico wrote: >> On Mon, May 13, 2013 at 12:17 AM, Citizen Kant wrote: >> >> > Maybe It'd be good if I explain myself a bit more. What I'm trying here is >> >> > to grasp Python from the game's abstraction point of view, as if it were, >> >> > for example, chess. >> >> >> >> Maybe you're going for something a little too complicated. Let's boil >> >> the question down to a much MUCH simpler subset: expression >> >> evaluation. And let's take Python itself right out of the picture, and >> >> work with only the following elements: >> >> >> >> * Parentheses ( ) >> >> * Multiplication and division * / >> >> * Addition and subtraction + - >> >> * Decimal integer constants 0123456789 >> >> >> >> This will give the basics of algebraic evaluation. It's quite fun to >> >> build a simple little expression evaluator that just does these few >> >> operations, and it's surprisingly useful (embed it within a command >> >> interpreter, for instance). Some time ago I wrote one into a Windows >> >> app (written in C++), and when I pulled it out just now and made it a >> >> stand-alone tool, the whole thing was only ~60 lines of code. Nice and >> >> easy to work with. >> >> >> >> So, let's take an expression and see what it really means. >> >> >> >> 2*(3+4)+6 >> >> >> >> One way to interpret this is with a stack. Here's how Python evaluates >> >> that expression: >> >> >> >> >>> def foo(): >> >> return 2*(three+4)+6 >> >> >> >> >>> dis.dis(foo) >> >> 2 0 LOAD_CONST 1 (2) >> >> 3 LOAD_GLOBAL 0 (three) >> >> 6 LOAD_CONST 2 (4) >> >> 9 BINARY_ADD >> >> 10 BINARY_MULTIPLY >> >> 11 LOAD_CONST 3 (6) >> >> 14 BINARY_ADD >> >> 15 RETURN_VALUE >> >> >> >> (I had to replace one of the constants with a global, to foil the optimizer) >> >> >> >> The LOAD statements add to an internal stack, the BINARY operations >> >> pop two operands and push the result. This is more-or-less the same >> >> technique as I used in my evaluator, except that instead of compiling >> >> it to code, I just march straight through, left to right, and so I had >> >> to maintain two stacks (one of operands, one of operators). >> >> >> >> Is this what you had in mind when you wanted to grasp Python's >> >> internals? Because it's pretty easy to explore, thanks to the dis >> >> module. There's a huge amount that can be learned about the >> >> interpreter, its optimizers, and so on, just by disassembling >> >> functions. Alternatively, if you're thinking on a more abstract level, >> >> leave Python aside altogether and pick up a book on language design. >> >> Also can be awesome fun, but completely different. >> >> >> >> ChrisA > > No, that won't help. You're trying to give him knowledge; he wants understanding. I can't give him understanding. Best I can do is offer facts, which lead to knowledge, which lead to understanding if absorbed appropriately. Also, sources of knowledge (like dis.dis) can be VERY powerful tools in gaining understanding. ChrisA From steve+comp.lang.python at pearwood.info Sun May 12 10:30:56 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 May 2013 14:30:56 GMT Subject: Python for philosophers References: Message-ID: <518fa7a0$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sun, 12 May 2013 16:17:02 +0200, Citizen Kant wrote: > Any clue about this would be highly appreciated. If you are interested in the intersection of programming and philosophy, I strongly recommend that you read "G?del, Escher, Bach: An Eternal Golden Braid" by Douglas R. Hofstadter. -- Steven From wuwei23 at gmail.com Mon May 13 00:19:47 2013 From: wuwei23 at gmail.com (alex23) Date: Sun, 12 May 2013 21:19:47 -0700 (PDT) Subject: Python for philosophers References: <518fa7a0$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On May 13, 12:30?am, Steven D'Aprano wrote: > If you are interested in the intersection of programming and philosophy, > I strongly recommend that you read "G?del, Escher, Bach: An Eternal > Golden Braid" by Douglas R. Hofstadter. +1 From rustompmody at gmail.com Sun May 12 12:00:18 2013 From: rustompmody at gmail.com (rusi) Date: Sun, 12 May 2013 09:00:18 -0700 (PDT) Subject: Python for philosophers References: Message-ID: <12751738-0de3-415b-8535-20d5d4e9ae1f@zo5g2000pbb.googlegroups.com> On May 12, 7:17?pm, Citizen Kant wrote: > Maybe It'd be good if I explain myself a bit more. What I'm trying here is > to grasp Python from the game's abstraction point of view, as if it were, > for example, chess. That's why I need a real_player to point me to: (so to > speak, I wish I could express the ideas according to the python's syntax > but that's out of my scope by now) On seeing the interest in games, I can only reiterate the suggestion to look at rewrite systems Here is on of the classics on rewrite systems http://rewriting.loria.fr/documents/survey-draft.ps.gz whose starting example is a game. From greg.ewing at canterbury.ac.nz Sun May 12 20:34:13 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 13 May 2013 12:34:13 +1200 Subject: Python for philosophers In-Reply-To: References: Message-ID: Citizen Kant wrote: > What I do here is to try to "understand". > That's different from just knowing. Knowledge growth must be consequence > of understanding's increasing. As the scope of my understanding > increases, the more I look for increasing my knowledge. Never vice > versa, because, knowing isn't like to be right, it's just knowing. It doesn't always work that way. With some facts plus a theory, you can deduce more facts. But it's always possible for there to be more facts that you can't deduce from what you already know. > But take in account that with "shortening" I > refer to "according to Python's axiomatic parameters". I think what you're trying to say is that it takes an expression and reduces it to a canonical form, such as a single number or single string. That's true as far as it goes, but it barely scratches the surface of what the Python interpreter is capable of doing. In the most general terms, the Python interpeter (or any other computer system, for that matter) can be thought of as something with an internal state, and a transition function that takes the state together with some input and produces another state together with some output: F(S1, I) --> (S2, O) (Computer scientists call this a "finite state machine", because there is a limited number of possible internal states -- the computer only has so much RAM, disk space, etc.) This seems to be what you're trying to get at with your game-of-chess analogy. What distinguishes one computer system from another is the transition function. The transition function of the Python interpreter is rather complicated, and it's unlikely that you would be able to figure out all its details just by poking in inputs and observing the outputs. If you really want to understand it, you're going to have to learn some facts, I'm sorry to say. :-) -- Greg From steve+comp.lang.python at pearwood.info Sun May 12 22:41:35 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 May 2013 02:41:35 GMT Subject: Python for philosophers References: Message-ID: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> On Mon, 13 May 2013 12:34:13 +1200, Gregory Ewing wrote: > In the most general terms, the Python interpeter (or any other computer > system, for that matter) can be thought of as something with an internal > state, and a transition function that takes the state together with some > input and produces another state together with some output: > > F(S1, I) --> (S2, O) > > (Computer scientists call this a "finite state machine", because there > is a limited number of possible internal states -- the computer only has > so much RAM, disk space, etc.) That's not what finite state machine means. A finite state machine doesn't refer to the number of physical states of the underlying hardware implementing the device, it refers to the number of external states of the computation being performed. For example, a traffic light may be modelled by a Finite State Machine with three states: Red, Amber, Green. It may actually be implemented by a general purpose computer with trillions of internal states, or by a specialised electrical circuit, or by clockwork. The implementation doesn't matter. What matters is the three external states, any input to the device, plus the transition rules between them. Python is not well-modelled as a Finite State Machine. Python is equivalent in computing power to a Turing Machine, while Finite State Machines are much weaker, so there are things that Python can do that a FSM cannot. -- Steven From rustompmody at gmail.com Mon May 13 10:53:10 2013 From: rustompmody at gmail.com (rusi) Date: Mon, 13 May 2013 07:53:10 -0700 (PDT) Subject: Python for philosophers References: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> On May 13, 7:41?am, Steven D'Aprano wrote: > Python is not well-modelled as a Finite State Machine. Python is > equivalent in computing power to a Turing Machine, while Finite State > Machines are much weaker, so there are things that Python can do that a > FSM cannot. Consider the following. Python is turing-equivalent; so is C and scheme. I now write a recursive factorial function in all 3. [To level the pitch all three are written tail-recursively] ------------Python------------------ def fact(n,acc=1): return acc if not n else fact(n-1,n*acc) ------------C------------------------- #include main(int argc, char **argv) { printf("fact %d is %d\n", atoi(argv[1]), fact(atoi(argv[1],1))); } int fact(int n, int acc) { return !n? acc : fact(n-1,acc*n); } --------------------------------- When I run these, the C happily keeps giving answers until a million The python crashes around a thousand. However examined closely we find that though the C is giving answers its giving junk after around 12 fact 17 is -288522240 And 35 onwards its 0 (!!) So finally we do it in scheme: (define (fact n ac) (if (zero? n) ac (fact (1- n) (* ac n)))) -------------------------------------------------------- This program neither crashes (because of tail recursion) nor gives wrong answers However around 90000 my entire machine becomes completely unusable with top showing guile (scheme) taking all the memory and kswapd next in line. So whats the moral? The Turing model is essentially infinite. [A TM to compute factorial would never crash because it can never be built] The machines we use are finite. As a first approx we may say that languages like C,Python,Scheme are Turing-complete. However in fact when we have to stuff these (conceptually beautiful) infinite objects into messy finite objects such as Intel hardware, some corners have to be cut. And these 3 -- C, Python, Scheme -- CUT THE CORNERS DIFFERENTLY So when these corners dont matter -- Turing-equivalence is fine When they do, we must make do living in a more messy finite world From rosuav at gmail.com Mon May 13 12:24:44 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 02:24:44 +1000 Subject: Python for philosophers In-Reply-To: <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> References: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> Message-ID: On Tue, May 14, 2013 at 12:53 AM, rusi wrote: > int fact(int n, int acc) > { > return !n? acc : fact(n-1,acc*n); > } > --------------------------------- > When I run these, the C happily keeps giving answers until a million > > However examined closely we find that though the C is giving answers > its giving junk after around 12 > fact 17 is -288522240 > And 35 onwards its 0 (!!) That'll depend on your integer size. If it's a 32-bit integer, you can't store numbers greater than 2**31-1 (if you declared it as 'unsigned int', you could go all the way to 2**32-1). I'm sure you could write this to use the GNU Multi-Precision library, but it'd be a lot more complicated. However, as far as I know, the Turing machine never promises that; its cells aren't meant to be infinite integers. The Python script is, of course, governed by sys.setrecursionlimit(). But by adding a simple driver, we can test the real limit: import sys def fact(n,acc=1): return acc if not n else fact(n-1,n*acc) n=2 while True: sys.setrecursionlimit(n+2) print("fact",n,"has",len(str(fact(n))),"digits") n*=2 On my 64-bit system, running a recent trunk build of CPython 3.4, it can calculate 8192! but not 16384! (segfault). The limit seems to be 12772; after that, boom. Your crash-point will quite probably vary, and I'd say there'll be compile-time options that would change that. Of course, after playing with this in Python, I had to try out Pike. Using the exact same code you proposed for C, but with a different main() to save me the hassle of keying in numbers manually, I get this: fact 8192 has 28504 digits - 0.026 secs fact 16384 has 61937 digits - 0.097 secs fact 32768 has 133734 digits - 0.389 secs fact 65536 has 287194 digits - 1.628 secs fact 131072 has 613842 digits - 7.114 secs fact 262144 has 1306594 digits - 31.291 secs fact 524288 has 2771010 digits - 133.146 secs It's still going. One core consumed, happily working on 1048576!, and not actually using all that much memory. Hmm.... looks like the Pike optimizer has turned this non-recursive. Okay, let's tweak it so it's not tail-recursion-optimizable: return n?n*fact(n-1,1):1; Now it bombs at 65536, saying: Svalue stack overflow. (99624 of 100000 entries on stack, needed 256 more entries) Hey, it's better than a segfault, which is what C would have done :) And it's a tweakable value, though I don't know what the consequences are of increasing it (presumably increased RAM usage for all Pike programs). Your final conclusion is of course correct; nothing we build can be truly infinite. But we can certainly give some very good approximations, if we're prepared to pay for them. The reality is, though, that we usually do not want to pay for approximations to infinity; why is IEEE 754 floating point so much more used than, say, arbitrary-precision rational? Most of the time, we'd rather have good performance and adequate accuracy than abysmal performance and perfect accuracy. But hey, if you want to render a Mandelbrot set and zoom in to infinity, the option IS there. ChrisA From rustompmody at gmail.com Mon May 13 14:14:16 2013 From: rustompmody at gmail.com (rusi) Date: Mon, 13 May 2013 11:14:16 -0700 (PDT) Subject: Python for philosophers References: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> Message-ID: On May 13, 9:24?pm, Chris Angelico wrote: > > Your final conclusion is of course correct; nothing we build can be > truly infinite. But we can certainly give some very good > approximations, if we're prepared to pay for them. The reality is, > though, that we usually do not want to pay for approximations to > infinity; why is IEEE 754 floating point so much more used than, say, > arbitrary-precision rational? Most of the time, we'd rather have good > performance and adequate accuracy than abysmal performance and perfect > accuracy. But hey, if you want to render a Mandelbrot set and zoom in > to infinity, the option IS there. Lets look at the costs of locomotion (which I am rounding to neat figures for an easy discussion) It costs $10K for a car which goes at around 80 kmph Now if I want to move at 800 kmph I need to switch from car to plane and that will cost me in millions And if I want to move at 8000 kmph I need to be in a rocket in outer space. Cost perhaps in billions And maybe if I spend in trillions (leaving aside the question where I got the trillions) maybe my rocket can go at 80,000 kmph So what will it cost me to have a rocket that will go at 300,000 m/sec (186,000 miles per second may be more familiar)? So what am I driving at? Some limitations are technological. Some are fundamental. Earlier I talked of the fact that C, python and scheme hit the finiteness wall in different ways/places. machine word size is C's casualty stack size is python's memory is scheme's The details are technological; the fact of finiteness is fundamental just like the speed-of-light bar is fundamental. The example of numbers is another such case. The set of real numbers is in general not computable Even if we restrict ourselves to the so-called computable real numbers... [interestingly Turing's original paper was on computable (real) numbers] we get into a soup because: One can (somewhat simplistically) think of a real number as a (potentially) infinite list of digits. Now comparing two finite lists for equality is a trivial operation However comparing two infinite lists gets us into trouble because both lists may go on and on... and be same and same... for ever and ever... In short equality for infinite lists (and therefore real numbers) is undecidable. [Well technically semidecidable because if they are different we get a 'not-equal' answer] Sorry if this all sounds abstruse... The other day I was reading someone saying that java -- which is after all such an 'enterprisey' language -- had goofed by not providing a half-decent money-type. If you think about it, this is saying the opposite of your: > But we can certainly give some very good approximations, if we're prepared to pay for them. He cannot use int and have a billionaire (or more correctly a 2147483648-aire) add a dollar and get to zero Nor is it a great idea to use floating point and have some clever programmer skim off the sub-penny round-off errors into his personal account. tl;dr Computers are hopelessly finite Much harder to deal with this finitude than to hand-wave the problem away From rosuav at gmail.com Mon May 13 14:22:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 04:22:51 +1000 Subject: Python for philosophers In-Reply-To: References: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> Message-ID: On Tue, May 14, 2013 at 4:14 AM, rusi wrote: > It costs $10K for a car which goes at around 80 kmph > > Now if I want to move at 800 kmph I need to switch from car to plane > and that will cost me in millions > > And if I want to move at 8000 kmph I need to be in a rocket in outer > space. Cost perhaps in billions > > And maybe if I spend in trillions (leaving aside the question where I > got the trillions) maybe my rocket can go at 80,000 kmph > > So what will it cost me to have a rocket that will go at 300,000 m/sec > (186,000 miles per second may be more familiar)? A $1 pocket flashlight. :) ChrisA From dihedral88888 at googlemail.com Sat May 18 19:56:41 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 18 May 2013 16:56:41 -0700 (PDT) Subject: Python for philosophers In-Reply-To: References: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> Message-ID: <52b7a255-51f2-4b62-9c99-f8fcda5b7e97@googlegroups.com> Chris Angelico? 2013?5?14????UTC+8??12?24?44???? > On Tue, May 14, 2013 at 12:53 AM, rusi wrote: > > > int fact(int n, int acc) > > > { > > > return !n? acc : fact(n-1,acc*n); > > > } > > > --------------------------------- > > > When I run these, the C happily keeps giving answers until a million > > > > > > However examined closely we find that though the C is giving answers > > > its giving junk after around 12 > > > fact 17 is -288522240 > > > And 35 onwards its 0 (!!) > > > > That'll depend on your integer size. If it's a 32-bit integer, you > > can't store numbers greater than 2**31-1 (if you declared it as > > 'unsigned int', you could go all the way to 2**32-1). I'm sure you > > could write this to use the GNU Multi-Precision library, but it'd be a > > lot more complicated. However, as far as I know, the Turing machine > > never promises that; its cells aren't meant to be infinite integers. > > > > The Python script is, of course, governed by sys.setrecursionlimit(). > > But by adding a simple driver, we can test the real limit: > > > > import sys > > > > def fact(n,acc=1): > > return acc if not n else fact(n-1,n*acc) > > > > n=2 > > while True: > > sys.setrecursionlimit(n+2) > > print("fact",n,"has",len(str(fact(n))),"digits") > > n*=2 > > > > On my 64-bit system, running a recent trunk build of CPython 3.4, it > > can calculate 8192! but not 16384! (segfault). The limit seems to be > > 12772; after that, boom. Your crash-point will quite probably vary, > > and I'd say there'll be compile-time options that would change that. > > > > Of course, after playing with this in Python, I had to try out Pike. > > Using the exact same code you proposed for C, but with a different > > main() to save me the hassle of keying in numbers manually, I get > > this: > > > > fact 8192 has 28504 digits - 0.026 secs > > fact 16384 has 61937 digits - 0.097 secs > > fact 32768 has 133734 digits - 0.389 secs > > fact 65536 has 287194 digits - 1.628 secs > > fact 131072 has 613842 digits - 7.114 secs > > fact 262144 has 1306594 digits - 31.291 secs > > fact 524288 has 2771010 digits - 133.146 secs > > > > It's still going. One core consumed, happily working on 1048576!, and > > not actually using all that much memory. Hmm.... looks like the Pike > > optimizer has turned this non-recursive. Okay, let's tweak it so it's > > not tail-recursion-optimizable: > > > > return n?n*fact(n-1,1):1; > > > > Now it bombs at 65536, saying: > > > > Svalue stack overflow. (99624 of 100000 entries on stack, needed 256 > > more entries) > > > > Hey, it's better than a segfault, which is what C would have done :) > > And it's a tweakable value, though I don't know what the consequences > > are of increasing it (presumably increased RAM usage for all Pike > > programs). > > > > Your final conclusion is of course correct; nothing we build can be > > truly infinite. But we can certainly give some very good > > approximations, if we're prepared to pay for them. The reality is, > > though, that we usually do not want to pay for approximations to > > infinity; why is IEEE 754 floating point so much more used than, say, > > arbitrary-precision rational? Most of the time, we'd rather have good > > performance and adequate accuracy than abysmal performance and perfect > > accuracy. But hey, if you want to render a Mandelbrot set and zoom in > > to infinity, the option IS there. > > > > ChrisA Hey, ChisA, are you delibrately to write a recursive version to demonstrate the stack depth problem in Python? def fact(n): ret=1 if n>1: # integer checking is not used but can be added for x in xrange(n): ret*=x #print ret # debugging only for long integers return ret In a 32 or 64 bit system, this non-recursive verssion will be limited by the heap space not by the stack limit. From rosuav at gmail.com Sat May 18 20:04:45 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 19 May 2013 10:04:45 +1000 Subject: Python for philosophers In-Reply-To: <52b7a255-51f2-4b62-9c99-f8fcda5b7e97@googlegroups.com> References: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> <52b7a255-51f2-4b62-9c99-f8fcda5b7e97@googlegroups.com> Message-ID: On Sun, May 19, 2013 at 9:56 AM, 88888 Dihedral wrote: > Hey, ChisA, are you delibrately to write a recursive version > to demonstrate the stack depth problem in Python? > > def fact(n): > ret=1 > if n>1: # integer checking is not used but can be added > for x in xrange(n): ret*=x > #print ret # debugging only for long integers > return ret > > > > In a 32 or 64 bit system, this non-recursive verssion > will be limited by the heap space not by the stack limit. And just when we're sure Dihedral's a bot, a post like this comes through. Dihedral, are you intelligent? I'm still in two minds about this... which may be why you so often appear to have no minds. I dunno. Mathematics somewhere I fancy. ChrisA From dihedral88888 at googlemail.com Sat May 18 22:30:43 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 18 May 2013 19:30:43 -0700 (PDT) Subject: Python for philosophers In-Reply-To: References: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> <52b7a255-51f2-4b62-9c99-f8fcda5b7e97@googlegroups.com> Message-ID: <2751ccb7-a882-4ad6-ad14-386cb7ba4c65@googlegroups.com> Chris Angelico? 2013?5?19????UTC+8??8?04?45???? > On Sun, May 19, 2013 at 9:56 AM, 88888 Dihedral > > wrote: > > > Hey, ChisA, are you delibrately to write a recursive version > > > to demonstrate the stack depth problem in Python? > > > > > > def fact(n): > > > ret=1 > > > if n>1: # integer checking is not used but can be added > > > for x in xrange(n): ret*=x > > > #print ret # debugging only for long integers > > > return ret > > > > > > > > > > > > In a 32 or 64 bit system, this non-recursive verssion > > > will be limited by the heap space not by the stack limit. > > > > And just when we're sure Dihedral's a bot, a post like this comes through. > > > > Dihedral, are you intelligent? I'm still in two minds about this... > > which may be why you so often appear to have no minds. I dunno. > > Mathematics somewhere I fancy. > > > > ChrisA I am too lazy to write a factorial computations with primes here. From torriem at gmail.com Sun May 19 00:46:22 2013 From: torriem at gmail.com (Michael Torrie) Date: Sat, 18 May 2013 22:46:22 -0600 Subject: Python for philosophers In-Reply-To: <2751ccb7-a882-4ad6-ad14-386cb7ba4c65@googlegroups.com> References: <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com> <52b7a255-51f2-4b62-9c99-f8fcda5b7e97@googlegroups.com> <2751ccb7-a882-4ad6-ad14-386cb7ba4c65@googlegroups.com> Message-ID: <5198591E.60508@gmail.com> On 05/18/2013 08:30 PM, 88888 Dihedral wrote: > I am too lazy to write a factorial computations with primes > here. Ahh, that's better. From js at globe.de Thu May 16 10:13:49 2013 From: js at globe.de (Schneider) Date: Thu, 16 May 2013 16:13:49 +0200 Subject: Python for philosophers In-Reply-To: References: Message-ID: <5194E99D.4070805@globe.de> Maybe the implementation of the Python Interpreter could be seen as transition function. This can be understand in detail, but it even if you know how the interpreter works, you don't really know to work _with_ the interpreter. Even more, there are a lot of decisions, which are made 'by design' and don't have a clear answer. to see why somethings are done in the way they are done you have to understand the philosophy of programming with python. bg, Johannes On 13.05.2013 02:34, Gregory Ewing wrote: > Citizen Kant wrote: >> What I do here is to try to "understand". That's different from just >> knowing. Knowledge growth must be consequence of understanding's >> increasing. As the scope of my understanding increases, the more I >> look for increasing my knowledge. Never vice versa, because, knowing >> isn't like to be right, it's just knowing. > > It doesn't always work that way. With some facts plus a > theory, you can deduce more facts. But it's always possible > for there to be more facts that you can't deduce from what > you already know. > >> But take in account that with "shortening" I refer to "according to >> Python's axiomatic parameters". > > I think what you're trying to say is that it takes an > expression and reduces it to a canonical form, such as > a single number or single string. > > That's true as far as it goes, but it barely scratches > the surface of what the Python interpreter is capable > of doing. > > In the most general terms, the Python interpeter (or > any other computer system, for that matter) can be thought > of as something with an internal state, and a transition > function that takes the state together with some input > and produces another state together with some output: > > F(S1, I) --> (S2, O) > > (Computer scientists call this a "finite state machine", > because there is a limited number of possible internal > states -- the computer only has so much RAM, disk space, > etc.) > > This seems to be what you're trying to get at with your > game-of-chess analogy. > > What distinguishes one computer system from another is > the transition function. The transition function of the > Python interpreter is rather complicated, and it's > unlikely that you would be able to figure out all its > details just by poking in inputs and observing the > outputs. If you really want to understand it, you're > going to have to learn some facts, I'm sorry to say. :-) > -- GLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nsterGLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nster 0251/5205 390 From steve+comp.lang.python at pearwood.info Sun May 12 23:25:30 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 May 2013 03:25:30 GMT Subject: Python for philosophers References: Message-ID: <51905d2a$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sun, 12 May 2013 16:17:02 +0200, Citizen Kant wrote: > Thank you very much for your answers. > > I'm afraid that, at this stage, I must prevent myself from "knowing too > much" about the subject. My idea here is trying to fill the gaps, > mostly, using intuition. Then you are doomed to failure. Intuition is greatly over-rated. Most of the time, it's just an excuse for prejudice, and a way to avoid understanding. > What I do here is to try to "understand". > That's different from just knowing. Knowledge growth must be consequence > of understanding's increasing. As the scope of my understanding > increases, the more I look for increasing my knowledge. Never vice > versa, because, knowing isn't like to be right, it's just knowing. Define your terms. What do you think "to know" means? What do you think "to understand" means? [...] > But take in account that with "shortening" I > refer to "according to Python's axiomatic parameters". You cannot hypothesis about Python's behaviour in terms of its "axiomatic parameters" unless they know what those axiomatic parameters are. So what do you think Python's "axiomatic parameters" are, and how did you come to that conclusion given that you know virtually nothing about Python? > What's "shorten" > if expressed in Python? For example: I'm plainly aware that the word > "python" looks shorten than "01110000 01111001 01110100 01101000 > 01101111 01101110". But it's shorten just for me and you and maybe for > every single human, not for the computer. You type "python", and the > language (so to speak) thinks "in my opinion you're not being economical > enough coz with this you mean 01110000 01111001 01110100 01101000 > 01101111 01101110", Sheer nonsense, and I don't mean the part where you anthropomorphise Python. (The "intentional stance" is often an excellent way of reasoning about complex systems.) Your premise that Python tries to be "economical" is incorrect. If anything, Python is the opposite: it is often profligate with resources (memory mostly) in order to save the human programmer time and effort. For example, Python dicts and lists may easily be four times as large as they could be (sometimes even bigger), *by design*. A list with 10 items may easily have space for 40 items. This is hardly economical, but it is a good trade-off in order to get better, and more predictable, performance. [...] > Maybe It'd be good if I explain myself a bit more. What I'm trying here > is to grasp Python from the game's abstraction point of view, as if it > were, for example, chess. A lousy analogy. Python is nothing like chess. You might as well try to understand Python as if it were a toothbrush. -- Steven From invalid at invalid.invalid Mon May 13 10:33:21 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 13 May 2013 14:33:21 +0000 (UTC) Subject: Python for philosophers References: <51905d2a$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-13, Steven D'Aprano wrote: > Your premise that Python tries to be "economical" is incorrect. If > anything, Python is the opposite: it is often profligate with resources > (memory mostly) in order to save the human programmer time and effort. IOW, Python is designed to be economical, but the resource of concern is not computer memory, it's the programmer's time/effort. Computer memory gets cheaper every year. My time gets more valuable every year (hopefully). -- Grant Edwards grant.b.edwards Yow! The entire CHINESE at WOMEN'S VOLLEYBALL TEAM all gmail.com share ONE personality -- and have since BIRTH!! From steve+comp.lang.python at pearwood.info Mon May 13 03:13:23 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 May 2013 07:13:23 GMT Subject: Python for philosophers References: Message-ID: <51909292$0$29978$c3e8da3$5496439d@news.astraweb.com> Some further details on something mentioned about Python being "economical". On Sun, 12 May 2013 16:17:02 +0200, Citizen Kant wrote: > For example: I'm plainly aware that the word "python" looks shorten than > "01110000 01111001 01110100 01101000 01101111 01101110". But it's > shorten just for me and you and maybe for every single human, not for > the computer. You type "python", and the language (so to speak) thinks > "in my opinion you're not being economical enough coz with this you mean > 01110000 01111001 01110100 01101000 01101111 01101110", and then mirrors > the supposedly result in its particular context. My "shorten" points to > what's shorten during and inside Python's runtime. In fact, the string "python" is implemented differently in different versions of Python. Picking Python 2.7, it may be implemented something like this binary string: ???????? ???????? ???????? ???????? ???????? ???????? ???????? ???????? ???????? ???????? ???????? ???????? ???????? 00000000 01110000 00000000 01111001 00000000 01110100 00000000 01101000 00000000 01101111 00000000 01101110 00000000 00000000 where the spaces are purely for the convenience of the reader, and the question marks are internal values that I don't know enough to give the correct value. (Although I can predict that at least two sets of eight question marks is probably "00000000 00000110", the length of the string.) To be precise, this is with a "narrow build", a "wide build" is even more profligate with memory. I'm not trying to beat the original Poster up for making an error, but demonstrating just how badly off track you can get by trying to reason from first principles (as Plato may have done) instead of empirical study (as Aristotle or Bacon may have done). Knowledge of how things actually are beats understanding of how they ought to be every time. -- Steven From wuwei23 at gmail.com Mon May 13 05:05:05 2013 From: wuwei23 at gmail.com (alex23) Date: Mon, 13 May 2013 02:05:05 -0700 (PDT) Subject: Python for philosophers References: <51909292$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3a6f27d8-275f-40eb-8aee-8f9083853b8e@yb1g2000pbc.googlegroups.com> On May 13, 5:13?pm, Steven D'Aprano wrote: > I'm not trying to beat the original Poster up for making an error, but > demonstrating just how badly off track you can get by trying to reason > from first principles (as Plato may have done) instead of empirical study > (as Aristotle or Bacon may have done). Knowledge of how things actually > are beats understanding of how they ought to be every time. Nicely put, I may have to copy & paste this into another thread here... I always did have a soft spot for William James. From neilc at norwich.edu Mon May 13 09:52:56 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 13 May 2013 13:52:56 GMT Subject: Python for philosophers References: <51909292$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-13, Steven D'Aprano wrote: > I'm not trying to beat the original Poster up for making an > error, but demonstrating just how badly off track you can get > by trying to reason from first principles (as Plato may have > done) instead of empirical study (as Aristotle or Bacon may > have done). Knowledge of how things actually are beats > understanding of how they ought to be every time. Wasn't it Aristarchus who was considered a trouble-maker for dropping different sized lead balls from a tower? They would land simultaneously just as his teacher, who taught that heavier objects fall faster, was walking past. -- Neil Cerutti From citizenkant at gmail.com Mon May 13 19:32:43 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Tue, 14 May 2013 01:32:43 +0200 Subject: Python for philosophers In-Reply-To: References: Message-ID: I'm amazed with your feedback, even when due to a lack of knowledge I'm not able to discuss some of them. I've been inspecting the stuff about rewriting and that drew my attention to my first intuition of Python being economic. Maybe could it support my impression about a thing thats behind the language and got to do with condensing expressions until their end point is reached. I'll absolutely read the book you recommended, coz looks perfect. The dis module thing sounds and looks perfect too. Then again something that was discussed here about Python being economic or not and how or in which sense also threw some light on my first impression about the language. Everything here is interesting and illustrative. Anyway, I think that maybe I'm missing the point and I'm not being capable of expressing the fundamentals of the reason why I'm here. I thought that the most convenient thing to do is trying to keep myself attached to the natural language I master (so to speak) and answer the a set of questions that has been formulated. Maybe with this I'm helping myself. Towards what purpose I'm just inspecting Python's environment? Towards what purpose one would be just inspecting Chess' environment. Eventually, I could end up playing; but that isn't told yet. Do I want to learn to program? I didn't say I've wanted to learn to program neither said the opposite. I've said that I wasn't sure. And I said that because it's true. I'm not sure. Sureness tends to proliferate at its highest rate when one is looking to know. I'm looking to understand this something called Python. I've came here as explorer. I know_about numbers of things that go_about a number of topics of various supposedly most separated sciences. Since I sometimes have the capacity for combining these knowledge units in a fancy way and "realize" a great deal of things, is that I use a lot the verb "realize". These constant instantiations of mine are like well done objects of real true knowledge, made somehow by myself, by calling a method called "understanding" from the class that corresponds and apply to any number of memorized_data_objects that were previously instantiated in my mind coming from my senses. For me this seems to look like what follows: >>> understanding(combination(a_set_of _memorized_data_objects)) >>> def real_knowledge >>> understanding(a_set_of_memorized_data_objects) # How does this look? I'm positive about that being told all the time about everything is pretty much an economic issue, it just saves time, which in this environment saves money, but at the cost of not playing with real knowledge that's verified by each self (checksummed so to speak). Monkeys didn't developed our actual brains just by being told about everything, but experiencing the phenomena, that now we humans are talking about. If not, then why do I care about Python programming? In part is like a gut_decision. Internet is plenty of information about one or another thing that one could be looking for, I've taken a look to Ruby and Java and C++, but was a set of Python characteristics that really matched with something inside of me. An entity named Python must be somehow as a serpent. Don't forget that I'm with the freeing up of my memory, now I'm not trying to follow the path of what's told but acting like the monkey and pushing with my finger against the skin of the snake. Could be the case that a stimulus_response method is being called inside of me. If that's the case, objects instantiated by the stimulus_response method are the first ones that can be considered scientific like, inside of me. Python also must be an entity that's able to swallow, doesn't matter that it's chicken object. Then it will throw whatever by its tail. For me that's interesting and, in me, interestingness use to call the understanding method. Then I realize that what's stated above implies that I can feed Python, and (here starts the magic) see what type of whatever throws back by its tail. Then I'll approach to smell any possible profit. What do I aim to get out of this exercise? Since actually I'm not running for programmer, my reason for understanding Python must be sui generis and it is. What do I think "Python's core" means? More than thinking I'm just trying to guess what Python's core must be. Any phenomena has a core. Maybe Python is economic as a snake and it is almost all core. What would be the core of a digestive system covered with skin? Considering Python as which in itself is all its truth and nothing but its truth (that's to say thinking it without all its optionals) I tend to look at it as if one of the most economic living creatures, and maybe a core in itself. One color note is that in the serpent class there's no attachment method. Serpents are unemotional, they use to drop their eggs here and there without a care. Serpent class lacks of empathy method. What do I mean by "global definition"? I mean one that would generic enough that includes myself. What's an "entity"? It could be any phenomena. I just wanted to frame something and draw the attention to it, even if I'm still not in the position of label the phenomena in a correct manner nor conceptualize it at all. Why do my affirmation pre-suppose that exists something *more fundamental* to programming that Python is for? With this I don't mean more important but fundamental, that comes from foundation, that's to say something meta or previous. Aside from driving screws, what is the single and most basic use of a screwdriver? Aside the use that materialistic marketing tends to include in its eternal propaganda, there's another use of whatever tool that I, the monkey, am able to manipulate. My hand and my thought are engaged in the closest relationship one can ever imagine. Manipulating, sets a foo in my brains, foo that doesn't set the just listening to what someone would tell_about. The case is I'm not able to get my material hands over Python, but that doesn't mean that I must merely observe it as if it were non material. I'm trying at least to emulate certain conditions to fill this gap. Modeling something that could be called object_manipulation in order to understand sounds crazy and maybe it is, it's paradoxical too, at the same time sounds logic. For my purposes, what is so special about interactive mode that I single it out in this way? Using the command line I'm setting myself closer to what I'm trying to understand. That doesn't seem to be what one would consider doing wrong. Why do I tend to believe that interactive mode isn't just like non-interactive mode? It seems that there are tiny differences between typing on the command line and running a .py file. This drew my attention to the fact that being economic has a lot to do with my purpose, so I decided to avoid the tiny differences. Why do I insist on the fact that "I must prevent myself from knowing too much about a subject, that the best for me here is trying to fill the gaps, mostly, using intuition? This is an important question that I've tried to answer close to the start. Why do I believe that intuition isn't greatly over-rated, and that most of the time, isn't just an excuse for prejudice, and a way to avoid understanding? This is another good question that I've already tried to answer. What do I think "to know" means? What do I think "to understand" means? I've already tried to answer this. What do I think Python's "axiomatic parameters" are, and how did I come to that conclusion given that I know virtually nothing about Python? I'm coping with this, as I've already stated, as if Python and Chess inherit from Games. Games are known for being a particular kind of phenomena, phenomena that not always but often includes something called board, that's to say whatever in that game that remains immutable and serves as its basic constant. With "axiomatic parameters" I've tried to illustrate this immutable. That could be called perimeter or edge or boundary, and even if all of those labels denote a limit, all of them, unless for me, sound like... static. To think about Python in terms of something that's static seems incorrect. "Axiomatic parameters" looked like an initial limit that one can set, it just sounded accurate for a Python's kind of thing. Why do I maintain that Python could be something like chess. >From the "trying to understand" point of view, everything can be considered a game. In my opinion even science could be considered a game that could be played in solitary mode. Am I getting closer to the point? 2013/5/11 Citizen Kant > Hi, > this could be seen as an extravagant subject but that is not my original > purpose. I still don't know if I want to become a programmer or not. At > this moment I'm just inspecting the environment. I'm making my way to > Python (and OOP in general) from a philosophical perspective or point of > view and try to set the more global definition of Python's core as an > "entity". In order to do that, and following Wittgenstein's indication > about that the true meaning of words doesn't reside on dictionaries but in > the use that we make of them, the starting question I make to myself about > Python is: which is the single and most basic use of Python as the entity > it is? I mean, beside programming, what's the single and most basic result > one can expect from "interacting" with it directly (interactive mode)? I > roughly came to the idea that Python could be considered as an *economic > mirror for data*, one that mainly *mirrors* the data the programmer types > on its black surface, not exactly as the programmer originally typed it, > but expressed in the most economic way possible. That's to say, for > example, if one types >>>1+1 Python reflects >>>2. When data appears > between apostrophes, then the mirror reflects, again, the same but > expressed in the most economic way possible (that's to say without the > apostrophes). > > So, would it be legal (true) to define Python's core as an entity that > mirrors whatever data one presents to it (or feed it with) showing back the > most shortened expression of that data? > > Don't get me wrong. I can see the big picture and the amazing things that > programmers write on Python, it's just that my question points to the > lowest level of it's existence. > > Thanks a lot for your time. > -- ------------------------------------------------------------------ ?Has le?do ?Las Novelas Prohibidas? ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Mon May 13 21:47:48 2013 From: davea at davea.name (Dave Angel) Date: Mon, 13 May 2013 21:47:48 -0400 Subject: Python for philosophers In-Reply-To: References: Message-ID: <519197C4.5090601@davea.name> On 05/13/2013 07:32 PM, Citizen Kant wrote: > > > Am I getting closer to the point? > Depends on whom you think you're talking to. Clearly, you've replied to yourself, and top-posted besides. That's not a conversation, it's a monologue. -- DaveA From jake.angulo at gmail.com Tue May 14 02:31:00 2013 From: jake.angulo at gmail.com (Jake Angulo) Date: Tue, 14 May 2013 16:31:00 +1000 Subject: Python for philosophers In-Reply-To: References: Message-ID: On Tue, May 14, 2013 at 9:32 AM, Citizen Kant wrote: > Do I want to learn to program? > I didn't say I've wanted to learn to program neither said the > opposite. I've said that I wasn't sure. Hmmmm... i'd say you'll make very good business applications analyst. In fact i'd hazard to say you can make it to CIO. Recommended reading: * PERL for dummies by: Paul Hoffman * Crime & Punishment by: Fyodor Dostoyevsky With your natural philosophical talent, and just a little more supplementary knowledge you would Pwn & ruLZ! Just... pls... dont do programming... and Never do Python. On Tue, May 14, 2013 at 9:32 AM, Citizen Kant wrote: > I'm amazed with your feedback, even when due to a lack of knowledge I'm > not able to discuss some of them. I've been inspecting the stuff about > rewriting and that drew my attention to my first intuition of Python being > economic. Maybe could it support my impression about a thing thats behind > the language and got to do with condensing expressions until their end > point is reached. I'll absolutely read the book you recommended, coz looks > perfect. The dis module thing sounds and looks perfect too. Then again > something that was discussed here about Python being economic or not and > how or in which sense also threw some light on my first impression about > the language. Everything here is interesting and illustrative. Anyway, I > think that maybe I'm missing the point and I'm not being capable of > expressing the fundamentals of the reason why I'm here. I thought that the > most convenient thing to do is trying to keep myself attached to the > natural language I master (so to speak) and answer the a set of questions > that has been formulated. Maybe with this I'm helping myself. > > Towards what purpose I'm just inspecting Python's environment? > Towards what purpose one would be just inspecting Chess' environment. > Eventually, I could end up playing; but that isn't told yet. > > Do I want to learn to program? > I didn't say I've wanted to learn to program neither said the opposite. > I've said that I wasn't sure. And I said that because it's true. I'm not > sure. Sureness tends to proliferate at its highest rate when one is looking > to know. I'm looking to understand this something called Python. I've came > here as explorer. I know_about numbers of things that go_about a number of > topics of various supposedly most separated sciences. Since I sometimes > have the capacity for combining these knowledge units in a fancy way and > "realize" a great deal of things, is that I use a lot the verb "realize". > These constant instantiations of mine are like well done objects of real > true knowledge, made somehow by myself, by calling a method called > "understanding" from the class that corresponds and apply to any number of > memorized_data_objects that were previously instantiated in my mind coming > from my senses. For me this seems to look like what follows: > > >>> understanding(combination(a_set_of _memorized_data_objects)) > > >>> def real_knowledge > >>> understanding(a_set_of_memorized_data_objects) # How does this > look? > > I'm positive about that being told all the time about everything is pretty > much an economic issue, it just saves time, which in this environment saves > money, but at the cost of not playing with real knowledge that's verified > by each self (checksummed so to speak). Monkeys didn't developed our actual > brains just by being told about everything, but experiencing the phenomena, > that now we humans are talking about. > > If not, then why do I care about Python programming? > In part is like a gut_decision. Internet is plenty of information about > one or another thing that one could be looking for, I've taken a look to > Ruby and Java and C++, but was a set of Python characteristics that really > matched with something inside of me. An entity named Python must be somehow > as a serpent. Don't forget that I'm with the freeing up of my memory, now > I'm not trying to follow the path of what's told but acting like the monkey > and pushing with my finger against the skin of the snake. Could be the case > that a stimulus_response method is being called inside of me. If that's the > case, objects instantiated by the stimulus_response method are the first > ones that can be considered scientific like, inside of me. Python also must > be an entity that's able to swallow, doesn't matter that it's chicken > object. Then it will throw whatever by its tail. For me that's interesting > and, in me, interestingness use to call the understanding method. Then I > realize that what's stated above implies that I can feed Python, and (here > starts the magic) see what type of whatever throws back by its tail. Then > I'll approach to smell any possible profit. > > What do I aim to get out of this exercise? > Since actually I'm not running for programmer, my reason for understanding > Python must be sui generis and it is. > > What do I think "Python's core" means? > More than thinking I'm just trying to guess what Python's core must be. > Any phenomena has a core. Maybe Python is economic as a snake and it is > almost all core. What would be the core of a digestive system covered with > skin? Considering Python as which in itself is all its truth and nothing > but its truth (that's to say thinking it without all its optionals) I tend > to look at it as if one of the most economic living creatures, and maybe a > core in itself. > > One color note is that in the serpent class there's no attachment method. > Serpents are unemotional, they use to drop their eggs here and there > without a care. Serpent class lacks of empathy method. > > What do I mean by "global definition"? > I mean one that would generic enough that includes myself. > > What's an "entity"? > It could be any phenomena. I just wanted to frame something and draw the > attention to it, even if I'm still not in the position of label the > phenomena in a correct manner nor conceptualize it at all. > > Why do my affirmation pre-suppose that exists something *more fundamental* > to programming that Python is for? > With this I don't mean more important but fundamental, that comes from > foundation, that's to say something meta or previous. > > Aside from driving screws, what is the single and most basic use of a > screwdriver? > Aside the use that materialistic marketing tends to include in its eternal > propaganda, there's another use of whatever tool that I, the monkey, am > able to manipulate. My hand and my thought are engaged in the closest > relationship one can ever imagine. Manipulating, sets a foo in my brains, > foo that doesn't set the just listening to what someone would tell_about. > The case is I'm not able to get my material hands over Python, but that > doesn't mean that I must merely observe it as if it were non material. I'm > trying at least to emulate certain conditions to fill this gap. Modeling > something that could be called object_manipulation in order to understand > sounds crazy and maybe it is, it's paradoxical too, at the same time sounds > logic. > > > For my purposes, what is so special about interactive mode that I single > it out in this way? > Using the command line I'm setting myself closer to what I'm trying to > understand. That doesn't seem to be what one would consider doing wrong. > > Why do I tend to believe that interactive mode isn't just like > non-interactive mode? > It seems that there are tiny differences between typing on the command > line and running a .py file. This drew my attention to the fact that being > economic has a lot to do with my purpose, so I decided to avoid the tiny > differences. > > Why do I insist on the fact that "I must prevent myself from knowing too > much about a subject, that the best for me here is trying to fill the gaps, > mostly, using intuition? > This is an important question that I've tried to answer close to the start. > > Why do I believe that intuition isn't greatly over-rated, and that most of > the time, isn't just an excuse for prejudice, and a way to avoid > understanding? > This is another good question that I've already tried to answer. > > What do I think "to know" means? What do I think "to understand" means? > I've already tried to answer this. > > What do I think Python's "axiomatic parameters" are, and how did I come to > that conclusion given that I know virtually nothing about Python? > I'm coping with this, as I've already stated, as if Python and Chess > inherit from Games. Games are known for being a particular kind of > phenomena, phenomena that not always but often includes something called > board, that's to say whatever in that game that remains immutable and > serves as its basic constant. With "axiomatic parameters" I've tried to > illustrate this immutable. That could be called perimeter or edge or > boundary, and even if all of those labels denote a limit, all of them, > unless for me, sound like... static. To think about Python in terms of > something that's static seems incorrect. "Axiomatic parameters" looked like > an initial limit that one can set, it just sounded accurate for a Python's > kind of thing. > > Why do I maintain that Python could be something like chess. > From the "trying to understand" point of view, everything can be > considered a game. In my opinion even science could be considered a game > that could be played in solitary mode. > > Am I getting closer to the point? > > > 2013/5/11 Citizen Kant > >> Hi, >> this could be seen as an extravagant subject but that is not my original >> purpose. I still don't know if I want to become a programmer or not. At >> this moment I'm just inspecting the environment. I'm making my way to >> Python (and OOP in general) from a philosophical perspective or point of >> view and try to set the more global definition of Python's core as an >> "entity". In order to do that, and following Wittgenstein's indication >> about that the true meaning of words doesn't reside on dictionaries but in >> the use that we make of them, the starting question I make to myself about >> Python is: which is the single and most basic use of Python as the entity >> it is? I mean, beside programming, what's the single and most basic result >> one can expect from "interacting" with it directly (interactive mode)? I >> roughly came to the idea that Python could be considered as an *economic >> mirror for data*, one that mainly *mirrors* the data the programmer >> types on its black surface, not exactly as the programmer originally typed >> it, but expressed in the most economic way possible. That's to say, for >> example, if one types >>>1+1 Python reflects >>>2. When data appears >> between apostrophes, then the mirror reflects, again, the same but >> expressed in the most economic way possible (that's to say without the >> apostrophes). >> >> So, would it be legal (true) to define Python's core as an entity that >> mirrors whatever data one presents to it (or feed it with) showing back the >> most shortened expression of that data? >> >> Don't get me wrong. I can see the big picture and the amazing things that >> programmers write on Python, it's just that my question points to the >> lowest level of it's existence. >> >> Thanks a lot for your time. >> > > > > -- > ------------------------------------------------------------------ > ?Has le?do ?Las Novelas Prohibidas? ? > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From llanitedave at veawb.coop Mon May 13 23:33:06 2013 From: llanitedave at veawb.coop (llanitedave) Date: Mon, 13 May 2013 20:33:06 -0700 (PDT) Subject: Python for philosophers In-Reply-To: References: Message-ID: On Monday, May 13, 2013 4:32:43 PM UTC-7, Citizen Kant wrote: >An entity named Python must be > somehow as a serpent. Moe like a dead parrot, actually. From citizenkant at gmail.com Tue May 14 05:11:34 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Tue, 14 May 2013 11:11:34 +0200 Subject: Fwd: Python for philosophers In-Reply-To: References: Message-ID: >>From: llanitedave >>On Monday, May 13, 2013 4:32:43 PM UTC-7, Citizen Kant wrote: >>>An entity named Python must be >>> somehow as a serpent. llanitedave wrote: >>>> Moe like a dead parrot, actually. That's a good one! Even If doesn't lead to the fact that Python (so to speak) use to give an answer. -------------- next part -------------- An HTML attachment was scrubbed... URL: From citizenkant at gmail.com Tue May 14 06:36:14 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Tue, 14 May 2013 12:36:14 +0200 Subject: Fwd: Python for philosophers In-Reply-To: References: Message-ID: Case study (kind of) Imagine that I use to explore with my mind a particular topic and I want to map and model the mechanics of that exploration. That's mostly metaphysical. I have a partner called Python with whom I must communicate in Python. Which would be the basics that I must "know" in order to pass my ideas to him properly. With this I mean using the units of my natural language skills that match with his language, and using them in Python's language context, in order to "program" at the highest level possible. It's true that my program won't run yet but for me this is not an obstacle at all, as when one writes a book one can start writing an index. For some people, this index or highest level programming could look mostly like a void thing coz lacks of the proper meat that a Python use to eat, and in a sense they are right. But that's not the point. The point is that as soon as I can I would start to dig deeper in that structure and build the proper meat that my highest level labels are just naming. What if using my ability to name what I actually think and recognize the path of whichever method I call from my object of thinking, I'd like to start setting a context for further immersion (inmersion with advanced mathematical notation an that? Somebody commented about a couple of basic elements which I'm familiarized with, like +, -, /, =, 1,2,3,4,5,6,7,8,9,0, (), etc. I know that Python has a set of keywords, there's also a proper way in which one must express wholes in Python, proper way which I'm not familiarized with but I'm able to learn. Does this help? For me, starting with Python is an affair of connecting with it. It's not about including it in me or including me in it, but a kind of symbiotic relationship. Unless for me, using my natural language as far as I can, but constrained (formalized) by Python syntax in order to model using objects and methods and classes that are still unable to run in Python (yet) seems to be a good starting point for a symbiotic relationship. Understanding might depend in our ability to set ourselves in the shoes of another. Any clues? Since this is a real goal that I'm looking to accomplish, any question that would clarify a bit more my states will be highly appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Tue May 14 01:29:45 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 May 2013 05:29:45 GMT Subject: Python for philosophers References: Message-ID: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> On Tue, 14 May 2013 01:32:43 +0200, Citizen Kant wrote: > An entity named Python must be somehow as a serpent. Don't forget that > I'm with the freeing up of my memory, now I'm not trying to follow the > path of what's told but acting like the monkey and pushing with my > finger against the skin of the snake. Python is not named after the snake, but after Monty Python the British comedy troupe. And they picked their name because it sounded funny. http://en.wikipedia.org/wiki/Monty_Python -- Steven From citizenkant at gmail.com Tue May 14 04:34:58 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Tue, 14 May 2013 10:34:58 +0200 Subject: Fwd: Python for philosophers In-Reply-To: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: 2013/5/14 Steven D'Aprano > On Tue, 14 May 2013 01:32:43 +0200, Citizen Kant wrote: > > >> An entity named Python must be somehow as a serpent. Don't forget that > >> I'm with the freeing up of my memory, now I'm not trying to follow the > >> path of what's told but acting like the monkey and pushing with my > >> finger against the skin of the snake. > > >Python is not named after the snake, but after Monty Python the British > >comedy troupe. And they picked their name because it sounded funny. > > > http://en.wikipedia.org/wiki/Monty_Python > > > > > I'm sorry to hear that. Mostly because, as an answer, seems to example > very well the "taken because I've been told how things are" kind of > actions, which is exactly the opposite of the point I'm trying to state. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Tue May 14 10:55:46 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Tue, 14 May 2013 10:55:46 -0400 Subject: Fwd: Python for philosophers In-Reply-To: References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: > 2013/5/14 Steven D'Aprano > > >Python is not named after the snake, but after Monty Python the > British > >comedy troupe. And they picked their name because it sounded funny. That does not mean they were unaware that Pythons are snakes. "requiring a "slippery-sounding" surname, they settled on "Python"" > > http://en.wikipedia.org/wiki/Monty_Python > > From djc at news.invalid Tue May 14 16:27:34 2013 From: djc at news.invalid (DJC) Date: Tue, 14 May 2013 21:27:34 +0100 Subject: Fwd: Python for philosophers In-Reply-To: References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 14/05/13 09:34, Citizen Kant wrote: > 2013/5/14 Steven D'Aprano > >> On Tue, 14 May 2013 01:32:43 +0200, Citizen Kant wrote: >> >>>> An entity named Python must be somehow as a serpent. Don't forget that >>>> I'm with the freeing up of my memory, now I'm not trying to follow the >>>> path of what's told but acting like the monkey and pushing with my >>>> finger against the skin of the snake. >> >>> Python is not named after the snake, but after Monty Python the British >>> comedy troupe. And they picked their name because it sounded funny. >> >>> http://en.wikipedia.org/wiki/Monty_Python >> >> >> >> >> I'm sorry to hear that. Mostly because, as an answer, seems to example >> very well the "taken because I've been told how things are" kind of >> actions, which is exactly the opposite of the point I'm trying to state. >> > Emanual Kant was a real piss ant Who was very rarely stable From invalid at invalid.invalid Wed May 15 10:37:57 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 15 May 2013 14:37:57 +0000 (UTC) Subject: Fwd: Python for philosophers References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-14, Citizen Kant wrote: > 2013/5/14 Steven D'Aprano > >> On Tue, 14 May 2013 01:32:43 +0200, Citizen Kant wrote: >> >> >> An entity named Python must be somehow as a serpent. Don't forget that >> >> I'm with the freeing up of my memory, now I'm not trying to follow the >> >> path of what's told but acting like the monkey and pushing with my >> >> finger against the skin of the snake. >> >> >Python is not named after the snake, but after Monty Python the British >> >comedy troupe. And they picked their name because it sounded funny. >> >> > http://en.wikipedia.org/wiki/Monty_Python >> >> I'm sorry to hear that. Mostly because, as an answer, seems to example >> very well the "taken because I've been told how things are" kind of >> actions, which is exactly the opposite of the point I'm trying to state. Firstly, watch your quoting, Steve D'Aprano didn't write that despite your claim that he did. Secondly, if a the person who named something tells you they named it after A rather than B, what are you going to do other than "taken because I've been told". Are you claiming Guido lied about the source of the name? -- Grant Edwards grant.b.edwards Yow! My life is a patio at of fun! gmail.com From citizenkant at gmail.com Wed May 15 15:55:17 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Wed, 15 May 2013 21:55:17 +0200 Subject: Fwd: Fwd: Python for philosophers In-Reply-To: References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-14, Citizen Kant wrote: > 2013/5/14 Steven D'Aprano > >> On Tue, 14 May 2013 01:32:43 +0200, Citizen Kant wrote: >> >> >> An entity named Python must be somehow as a serpent. Don't forget that >> >> I'm with the freeing up of my memory, now I'm not trying to follow the >> >> path of what's told but acting like the monkey and pushing with my >> >> finger against the skin of the snake. >> >> >Python is not named after the snake, but after Monty Python the British >> >comedy troupe. And they picked their name because it sounded funny. >> >> > http://en.wikipedia.org/wiki/Monty_Python< http://en.wikipedia.org/wiki/Monty_Python> >> >> I'm sorry to hear that. Mostly because, as an answer, seems to example >> very well the "taken because I've been told how things are" kind of >> actions, which is exactly the opposite of the point I'm trying to state. Grant Edwards said: >>>> Firstly, watch your quoting, Steve D'Aprano didn't write that despite >>>> your claim that he did. >>>> Secondly, if a the person who named something tells you they named it >>>> after A rather than B, what are you going to do other than "taken >>>> because I've been told". Are you claiming Guido lied about the source >>>> of the name? Of course not. I'm just claiming that the tree (what's been told) is preventing him from seeing the forest (what it is). If what's been told was (put, by the very God of Uranus) that "the name's origin resides on a string, that string's made up with the entire text of The Bible's Genesis chapter with the word Python inserted not exactly in the middle but upper on that tree", now I would be claiming the very same thing. As a matter of class, the word python names first a python snake than a Monty Python, which is 50% inspired by that python word, word that's been being considered the given name of a particular kind of snake since times in which Terry Gilliam wasn't even alive. Of course one always may want to perform random hacking and turn tables just because and treat the word python as a variable's name, setting that python equals Monty Python in order to checkmate any given conversation. In that case we'll have to cope then with the long lasting problem of being forced to name every python snake as a Monty Python snake, due to the caprice of a programmer . -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiosantosart at gmail.com Wed May 15 20:12:57 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 16 May 2013 01:12:57 +0100 Subject: Fwd: Fwd: Python for philosophers In-Reply-To: References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 15 May 2013 20:59, "Citizen Kant" wrote: > Of course one always may want to perform random hacking and turn tables just because and treat the word python as a variable's name, setting that python equals Monty Python in order to checkmate any given conversation. In that case we'll have to cope then with the long lasting problem of being forced to name every python snake as a Monty Python snake, due to the caprice of a programmer . > In Python all variables are actually labels. Labels refer to an object. An object can be referred to by any amount of labels, but when no labels and other references remain "pointed at" it, garbage collection destroys the object. So if we set python equals Monty Python the actual python snake will actually cease to exist. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wuwei23 at gmail.com Wed May 15 19:55:47 2013 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 May 2013 16:55:47 -0700 (PDT) Subject: Fwd: Fwd: Python for philosophers References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <97a2c8b5-af49-4b05-aded-b326d17a211c@a15g2000pbu.googlegroups.com> On May 16, 5:55?am, Citizen Kant wrote: > As a matter of > class, the word python names first a python snake than a Monty Python, > which is 50% inspired by that python word, word that's been being > considered the given name of a particular kind of snake since times in > which Terry Gilliam wasn't even alive. "Namespaces are one honking great idea -- let's do more of those!" Or to put it another way: context is important. I find it both funny and sad that you think the name of the language is preventing _others_ from seeing it as it is, when you're the only one who seems to be fixated upon it. From citizenkant at gmail.com Thu May 16 08:28:11 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Thu, 16 May 2013 14:28:11 +0200 Subject: Fwd: Fwd: Fwd: Python for philosophers In-Reply-To: <97a2c8b5-af49-4b05-aded-b326d17a211c@a15g2000pbu.googlegroups.com> References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> <97a2c8b5-af49-4b05-aded-b326d17a211c@a15g2000pbu.googlegroups.com> Message-ID: On May 16, 5:55 am, Citizen Kant wrote: > As a matter of > class, the word python names first a python snake than a Monty Python, > which is 50% inspired by that python word, word that's been being > considered the given name of a particular kind of snake since times in > which Terry Gilliam wasn't even alive. alex23 wrote: >> "Namespaces are one honking great idea -- let's do more of those!" Or >> to put it another way: context is important. >> I find it both funny and sad that you think the name of the language >> is preventing _others_ from seeing it as it is, when you're the only >> one who seems to be fixated upon it. Maybe would be good if I share with you guys some basics of scientific humor. As alex23 well pointed out, there's a kind of funny and sad behind this typical human reaction. There's a lot of science observing you as a plain humans, science has been being observing you for a long time, and science says that people tend to laugh_at when their intuition realizes that is in front of whatever phenomena that could undermine its thought foundations. Believe it or not, laughing_at is mostly a sign that one's afraid of losing his reason, a manifest sign of psychological fear. For example, being informed about that in a few hours an asteroid named X-21 will crash your planet destroying it would also first make you "react" neglecting it, then react with the typical smile_at the messenger, then if this given messenger insists you would normally tend to "overreact" and laugh_at the previously mentioned messenger, and all this will happen even if the information brought to you says the truth. Same happens if one's mom come one day and tells that the guy one always believed is his father is in fact not, that there's a real father of yours that will remain forever lost in the crowd with whom she once had an occasional sex intercourse inside the bathroom of a bar. Then first you'll smile_at her, then if she keeps on insisting with the funny/sad subject that alex23 well pointed out you'll eventually start overreacting and laugh_at her. At this point, only if she keeps on insisting with her truth until you're tired enough of overreacting because overreacting won't ever change the fact that the guy you (need to) believe is your biological father could keep on being whatever you please but not that, she can reach the goal of making you understand. I'm just an honest and polite guy asking you guys a couple of simple out of the box questions that are important for me. Everyone here has the freedom to keep on with their own assumptions and beliefs. If someone's interested on thinking outside the box with me for the sake of helping me, that would be great and highly appreciated. Thinking outside the box isn't just a cheap thing since it's highly creative. Take note that being able to think and write in English doesn't make you writers as, put, Faulkner. Same happens with any other language, same happens with Python. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rustompmody at gmail.com Thu May 16 09:46:09 2013 From: rustompmody at gmail.com (rusi) Date: Thu, 16 May 2013 06:46:09 -0700 (PDT) Subject: Fwd: Fwd: Fwd: Python for philosophers References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> <97a2c8b5-af49-4b05-aded-b326d17a211c@a15g2000pbu.googlegroups.com> Message-ID: On May 16, 5:28?pm, Citizen Kant wrote: > > I'm just an honest and polite guy asking you guys a couple of simple out of > the box questions that are important for me. Everyone here has the freedom > to keep on with their own assumptions and beliefs. If someone's interested > on thinking outside the box with me for the sake of helping me, that would > be great and highly appreciated. Thinking outside the box isn't just a > cheap thing since it's highly creative. Take note that being able to think > and write in English doesn't make you writers as, put, Faulkner. Same > happens with any other language, same happens with Python. Let me quote your first post (OP): > I roughly came to the idea that Python could be considered as an *economic > mirror for data*, one that mainly *mirrors* the data the programmer types > on its black surface, not exactly as the programmer originally typed it, > but expressed in the most economic way possible. And let me suggest that you follow your own advise -- Can you say what you have to say in 1/10th the number of words? Ok if not 1/10th then 1/5th? 1-third? If you can, you are on the way to appreciating something which you almost came to and then lost in interminable prolixity, to wit: > The starting question I make to myself about Python is: > which is the single and most basic use of Python as the entity it is? IOW a programmer is one who quickly and easily comes to the nub/core/ kernel/essence of a problem and as easily and adroitly shaves off the irrelevant. Else: (you cant /wont reduce your prolixity) You are bullshitting us and we are being trolled by you From rosuav at gmail.com Thu May 16 10:00:12 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 May 2013 00:00:12 +1000 Subject: Fwd: Fwd: Fwd: Python for philosophers In-Reply-To: References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> <97a2c8b5-af49-4b05-aded-b326d17a211c@a15g2000pbu.googlegroups.com> Message-ID: On Thu, May 16, 2013 at 11:46 PM, rusi wrote: > IOW a programmer is one who quickly and easily comes to the nub/core/ > kernel/essence of a problem and as easily and adroitly shaves off the > irrelevant. +1. This is a fairly good description of a programmer's job. Of course, that's the theoretical and pure programmer... a professional programmer often has to: * Figure out what the problem *is* based on an incomplet description from an incompetent user via a bored telephone operator * Traverse a morass of bureaucratic requirements and politicking just to get the necessary hardware/software to do his work * Deal with the Layer Eight firewalling against the implementation of the solution he comes up with * Attend inane meetings with bikeshedding non-technical people who have some kind of authority over the project * Etcetera, etcetera, etcetera. But yeah, that's what a programmer is. :) ChrisA From citizenkant at gmail.com Thu May 16 16:06:34 2013 From: citizenkant at gmail.com (Citizen Kant) Date: Thu, 16 May 2013 22:06:34 +0200 Subject: Fwd: Fwd: Fwd: Fwd: Python for philosophers In-Reply-To: References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> <97a2c8b5-af49-4b05-aded-b326d17a211c@a15g2000pbu.googlegroups.com> Message-ID: rusi said: > And let me suggest that you follow your own advise -- Can you say what > you have to say in 1/10th the number of words? Ok if not 1/10th then > 1/5th? 1-third? Thanks for the suggestion. I apologize for being that expansive; maybe you are right about this. In my world less use to be less. I'll try to review my doubts in order to express them in a much more concise format. Of course this is not trolling at all, and I'm intrigued by how fast someone can fall into that kind of conclusions... I'm pretty much interested in the topic, so I'll review the stuff. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rustompmody at gmail.com Sun May 19 13:46:07 2013 From: rustompmody at gmail.com (rusi) Date: Sun, 19 May 2013 10:46:07 -0700 (PDT) Subject: Python for philosophers References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> <97a2c8b5-af49-4b05-aded-b326d17a211c@a15g2000pbu.googlegroups.com> Message-ID: <0c5a945a-2ec2-4e8a-bb2c-cc7fce9b3357@pd6g2000pbc.googlegroups.com> On May 17, 1:06?am, Citizen Kant wrote: > rusi said: > > > And let me suggest that you follow your own advise -- Can you say what > > you have to say in 1/10th the number of words? Ok if not 1/10th then > > 1/5th? 1-third? > > Thanks for the suggestion. I apologize for being that expansive; maybe you > are right about this. In my world less use to be less. I'll try to review > my doubts in order to express them in a much more concise format. > > Of course this is not trolling at all, and I'm intrigued by how fast > someone can fall into that kind of conclusions... > > I'm pretty much interested in the topic, so I'll review the stuff. You are doing well -- Glad to see that. Except for the subject line. What's with the Fwd-loop? Anyway I have attempted to correct it From llanitedave at veawb.coop Fri May 17 01:42:01 2013 From: llanitedave at veawb.coop (llanitedave) Date: Thu, 16 May 2013 22:42:01 -0700 (PDT) Subject: Fwd: Fwd: Python for philosophers In-Reply-To: References: <5191cbc9$0$29978$c3e8da3$5496439d@news.astraweb.com> <97a2c8b5-af49-4b05-aded-b326d17a211c@a15g2000pbu.googlegroups.com> Message-ID: <855dcc14-20b8-4426-b1a7-2f1538b6c093@googlegroups.com> On Thursday, May 16, 2013 5:28:11 AM UTC-7, Citizen Kant wrote: > On May 16, 5:55?am, Citizen Kant wrote: > If someone's interested on thinking outside the box with me for the sake of helping me, that would be great and highly appreciated. Sorry, but you're asking for more than just thinking outside the box. What you want would seem to require thinking from within the bottle. From dreamingforward at gmail.com Tue May 14 14:56:10 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Tue, 14 May 2013 11:56:10 -0700 Subject: Python for philosophers In-Reply-To: References: Message-ID: On Sat, May 11, 2013 at 1:03 PM, Citizen Kant wrote: >I'm making my way to Python (and > OOP in general) from a philosophical perspective or point of view and try to > set the more global definition of Python's core as an "entity". In order to > do that, and following Wittgenstein's indication about that the true meaning > of words doesn't reside on dictionaries but in the use that we make of them, > the starting question I make to myself about Python is: which is the single > and most basic use of Python as the entity it is? It is a way to form order from ideas, an *experimental* philosophy. One can apply and implement a philosophy, taking it out of the realm of ideas and simulate them in the machine. > I mean, beside > programming, what's the single and most basic result one can expect from > "interacting" with it directly (interactive mode)? A game of interactions. > I roughly came to the > idea that Python could be considered as an economic mirror for data, one > that mainly mirrors the data the programmer types on its black surface, That is called the "editor window" in our world that is displayed on an electronic device called a computer display, but in Samael's world it is a mirror into our world. He misused it to rape the crown of the Hebrew story (found in the Bible). > So, would it be legal (true) to define Python's core as an entity that > mirrors whatever data one presents to it (or feed it with) showing back the > most shortened expression of that data? No, that is me, Marcos. -- MarkJ Tacoma, Washington From ned at nedbatchelder.com Wed May 15 21:01:27 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Wed, 15 May 2013 21:01:27 -0400 Subject: Python for philosophers In-Reply-To: References: Message-ID: <51942FE7.8080008@nedbatchelder.com> On 5/11/2013 4:03 PM, Citizen Kant wrote: > Don't get me wrong. I can see the big picture and the amazing things > that programmers write on Python, it's just that my question points to > the lowest level of it's existence. Sometimes a cigar is just a cigar. Python is a tool, it does what you tell it. To make an analogy, or maybe to clarify your philosophical view of the world, consider a hammer. What is the "lowest level of its existence"? --Ned. From tundra at tundraware.com Wed May 15 21:17:07 2013 From: tundra at tundraware.com (Tim Daneliuk) Date: Wed, 15 May 2013 20:17:07 -0500 Subject: Python for philosophers In-Reply-To: References: Message-ID: On 05/15/2013 08:01 PM, Ned Batchelder wrote: > On 5/11/2013 4:03 PM, Citizen Kant wrote: >> Don't get me wrong. I can see the big picture and the amazing things that programmers write on Python, it's just that my question points to the lowest level of it's existence. > > Sometimes a cigar is just a cigar. Python is a tool, it does what you tell it. To make an analogy, or maybe to clarify your philosophical view of the world, consider a hammer. What is the "lowest level of its existence"? > > --Ned. All You People are making this way too hard. To understand how questions like the OPs ought be resolved, please read: http://pvspade.com/Sartre/cookbook.html -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From tjreedy at udel.edu Wed May 15 23:43:11 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Wed, 15 May 2013 23:43:11 -0400 Subject: Python for philosophers In-Reply-To: References: Message-ID: On 5/15/2013 9:17 PM, Tim Daneliuk wrote: > http://pvspade.com/Sartre/cookbook.html Wikedly funny. From bahamutzero8825 at gmail.com Thu May 16 02:24:23 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 16 May 2013 01:24:23 -0500 Subject: Python for philosophers In-Reply-To: References: Message-ID: <51947B97.7090808@gmail.com> Tim Daneliuk wrote: > All You People are making this way too hard. To understand how > questions like the OPs ought be resolved, please read: > > http://pvspade.com/Sartre/cookbook.html On this list, I would expect a Sartre reference to be something like this: https://www.youtube.com/watch?v=crIJvcWkVcs -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From wuwei23 at gmail.com Thu May 16 00:49:19 2013 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 May 2013 21:49:19 -0700 (PDT) Subject: Python for philosophers References: Message-ID: <8f0d57d5-e68e-4de0-bb61-a5dbfa9edeea@ys5g2000pbc.googlegroups.com> On May 16, 11:17?am, Tim Daneliuk wrote: > ? ?http://pvspade.com/Sartre/cookbook.html Best recipe for tuna casserole ever! Cheers for this :) From tundra at tundraware.com Thu May 16 00:55:37 2013 From: tundra at tundraware.com (Tim Daneliuk) Date: Wed, 15 May 2013 23:55:37 -0500 Subject: Python for philosophers In-Reply-To: <8f0d57d5-e68e-4de0-bb61-a5dbfa9edeea@ys5g2000pbc.googlegroups.com> References: <8f0d57d5-e68e-4de0-bb61-a5dbfa9edeea@ys5g2000pbc.googlegroups.com> Message-ID: <519466C9.5070306@tundraware.com> On 05/15/2013 11:49 PM, alex23 wrote: > On May 16, 11:17 am, Tim Daneliuk wrote: >> http://pvspade.com/Sartre/cookbook.html > > Best recipe for tuna casserole ever! Cheers for this :) > "I have have realized that the traditional omelet form (eggs and cheese) is bourgeois." -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From tundra at tundraware.com Thu May 16 00:57:24 2013 From: tundra at tundraware.com (Tim Daneliuk) Date: Wed, 15 May 2013 23:57:24 -0500 Subject: Python for philosophers In-Reply-To: References: Message-ID: On 05/15/2013 10:43 PM, Terry Jan Reedy wrote: > On 5/15/2013 9:17 PM, Tim Daneliuk wrote: > >> http://pvspade.com/Sartre/cookbook.html > > Wikedly funny. > > "Today I made a Black Forest cake out of five pounds of cherries and a live beaver," -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From rustompmody at gmail.com Thu May 16 01:08:31 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 15 May 2013 22:08:31 -0700 (PDT) Subject: Python for philosophers References: Message-ID: On May 16, 6:17?am, Tim Daneliuk wrote: > On 05/15/2013 08:01 PM, Ned Batchelder wrote: > > > On 5/11/2013 4:03 PM, Citizen Kant wrote: > >> Don't get me wrong. I can see the big picture and the amazing things that programmers write on Python, it's just that my question points to the lowest level of it's existence. > > > Sometimes a cigar is just a cigar. ?Python is a tool, it does what you tell it. ?To make an analogy, or maybe to clarify your philosophical view of the world, consider a hammer. ?What is the "lowest level of its existence"? > > > --Ned. > > All You People are making this way too hard. ?To understand how > questions like the OPs ought be resolved, please read: > > ? ?http://pvspade.com/Sartre/cookbook.html Ha Ha! Very funny! Also a serious reminder of what philosophy tends to become. [Robert Pirsig wrote about the diff between philosophy and philosophology] > > -- > ---------------------------------------------------------------------------- > Tim Daneliuk ? ? tun... at tundraware.com > PGP Key: ? ? ? ?http://www.tundraware.com/PGP/ From invalid at invalid.invalid Thu May 16 10:27:07 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 16 May 2013 14:27:07 +0000 (UTC) Subject: Python for philosophers References: Message-ID: On 2013-05-16, Tim Daneliuk wrote: > On 05/15/2013 08:01 PM, Ned Batchelder wrote: >> On 5/11/2013 4:03 PM, Citizen Kant wrote: >>> Don't get me wrong. I can see the big picture and the amazing things that programmers write on Python, it's just that my question points to the lowest level of it's existence. >> >> Sometimes a cigar is just a cigar. Python is a tool, it does what you tell it. To make an analogy, or maybe to clarify your philosophical view of the world, consider a hammer. What is the "lowest level of its existence"? >> >> --Ned. > > All You People are making this way too hard. To understand how > questions like the OPs ought be resolved, please read: > > http://pvspade.com/Sartre/cookbook.html Yea, I've decided we're being trolled... -- Grant Edwards grant.b.edwards Yow! I'd like MY data-base at JULIENNED and stir-fried! gmail.com From tundra at tundraware.com Thu May 16 13:50:44 2013 From: tundra at tundraware.com (Tim Daneliuk) Date: Thu, 16 May 2013 12:50:44 -0500 Subject: Python for philosophers In-Reply-To: References: Message-ID: On 05/16/2013 09:27 AM, Grant Edwards wrote: > On 2013-05-16, Tim Daneliuk wrote: >> On 05/15/2013 08:01 PM, Ned Batchelder wrote: >>> On 5/11/2013 4:03 PM, Citizen Kant wrote: >>>> Don't get me wrong. I can see the big picture and the amazing things that programmers write on Python, it's just that my question points to the lowest level of it's existence. >>> >>> Sometimes a cigar is just a cigar. Python is a tool, it does what you tell it. To make an analogy, or maybe to clarify your philosophical view of the world, consider a hammer. What is the "lowest level of its existence"? >>> >>> --Ned. >> >> All You People are making this way too hard. To understand how >> questions like the OPs ought be resolved, please read: >> >> http://pvspade.com/Sartre/cookbook.html > > Yea, I've decided we're being trolled... > " I want to create an omelet that expresses the meaninglessness of existence, and instead they taste like cheese. " -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From rosuav at gmail.com Sun May 12 10:12:50 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 00:12:50 +1000 Subject: Old version docs don't link to current version Message-ID: Not sure if this is an oversight or something deliberate... could be either. >From http://docs.python.org/3.0/library/http.server.html there's no link to the current docs, even though from http://docs.python.org/3/library/http.server.html it's possible to switch to 3.2 and then back to 3.3 (or to 2.7, but that fails and redirects since http.server doesn't exist in 2.7). This wouldn't be much of a bother, except that a Google search for 'python http.server' (which is the way I normally pull up Python docs) landed me on /3.0/ instead of /3/. Was this intentional, along the lines of not touching the old and unsupported docs? ChrisA From tjreedy at udel.edu Sun May 12 15:32:58 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 12 May 2013 15:32:58 -0400 Subject: Old version docs don't link to current version In-Reply-To: References: Message-ID: On 5/12/2013 10:12 AM, Chris Angelico wrote: > Not sure if this is an oversight or something deliberate... could be either. > > From http://docs.python.org/3.0/library/http.server.html there's no > link to the current docs, even though from > http://docs.python.org/3/library/http.server.html it's possible to > switch to 3.2 and then back to 3.3 (or to 2.7, but that fails and > redirects since http.server doesn't exist in 2.7). This wouldn't be > much of a bother, except that a Google search for 'python http.server' > (which is the way I normally pull up Python docs) landed me on /3.0/ > instead of /3/. > > Was this intentional, along the lines of not touching the old and > unsupported docs? Cross referencing was added while 3.2 docs were still subject to revision. x.y docs are essentially frozen once the final x.y.z bugfix comes out. Since I always enter the docs through docs.python.org/3/ (or /2/) typing just 'd' on Firefox history bar brings up both on the short history list to select. I then use module or general index. From rosuav at gmail.com Sun May 12 17:51:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 13 May 2013 07:51:38 +1000 Subject: Old version docs don't link to current version In-Reply-To: References: Message-ID: On Mon, May 13, 2013 at 5:32 AM, Terry Jan Reedy wrote: > On 5/12/2013 10:12 AM, Chris Angelico wrote: >> >> Was this intentional, along the lines of not touching the old and >> unsupported docs? > > Cross referencing was added while 3.2 docs were still subject to revision. > x.y docs are essentially frozen once the final x.y.z bugfix comes out. So, yeah, not touching the old docs. Okay! It's not a particularly big deal, I can just delete a couple of characters out of the URL. But it'd be nice if people who link to the docs could start doing so using /3/ so that we have a chance of getting past this eventually. ChrisA From ayjayn1101 at gmail.com Sun May 12 15:33:44 2013 From: ayjayn1101 at gmail.com (Alex Norton) Date: Sun, 12 May 2013 20:33:44 +0100 Subject: in need of some help regarding my rock paper scissors game Message-ID: im new to python and im in the middle of making a RPS game for a college unit. i have used PyQt to create the GUI and i have received help regarding adding the code to the buttons. but its missing something as the error 'Traceback (most recent call last): File "C:\Users\Me\Desktop\testy.py", line 174, in bWater.clicked.connect( water_clicked ) AttributeError: 'int' object has no attribute 'clicked'' appears when i run the module. i was wondering if somebody could walk me through making the game complete and to add the results of the game (win/lose/stalemate) to the loutcome label attached is the game file. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: testy.py Type: application/octet-stream Size: 8340 bytes Desc: not available URL: From benjamin.kaplan at case.edu Sun May 12 16:03:13 2013 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sun, 12 May 2013 13:03:13 -0700 Subject: in need of some help regarding my rock paper scissors game In-Reply-To: References: Message-ID: On Sun, May 12, 2013 at 12:33 PM, Alex Norton wrote: > im new to python and im in the middle of making a RPS game for a college > unit. > > i have used PyQt to create the GUI and i have received help regarding adding > the code to the buttons. > > but its missing something as the error > > 'Traceback (most recent call last): File "C:\Users\Me\Desktop\testy.py", > line 174, in bWater.clicked.connect( water_clicked ) AttributeError: 'int' > object has no attribute 'clicked'' appears when i run the module. > > i was wondering if somebody could walk me through making the game complete > and to add the results of the game (win/lose/stalemate) to the loutcome > label > > attached is the game file. > > -- bWater is an int. Integers don't have a clicked attribute. So bWater.clicked.connect(water_clicked) looks for the clicked attribute of the integer bWater, can't find it, and throws an error. Also, Python scripts are executed line by line- at the time you make that call, the water_clicked function doesn't exist yet, so even if that was the correct call, it wouldn't work here. I haven't used PyQT before so I can't say what the correct way to set this up would be, but that's at least what's causing this error. From davea at davea.name Sun May 12 19:04:48 2013 From: davea at davea.name (Dave Angel) Date: Sun, 12 May 2013 19:04:48 -0400 Subject: in need of some help regarding my rock paper scissors game In-Reply-To: References: Message-ID: <51902010.8020700@davea.name> On 05/12/2013 03:33 PM, Alex Norton wrote: > im new to python and im in the middle of making a RPS game for a college > unit. > > i have used PyQt to create the GUI and i have received help regarding > adding the code to the buttons. I'm not at all familiar with PyQT, but I have used other GUIs, and I'm quite familiar with Python itself. > > but its missing something as the error > > 'Traceback (most recent call last): File "C:\Users\Me\Desktop\testy.py", > line 174, in bWater.clicked.connect( water_clicked ) AttributeError: 'int' > object has no attribute 'clicked'' appears when i run the module. You've defined two very different bWater objects. One is an instance attribute of the class Ui_MainWindow, while the other is an int at global scope. The latter is never usefully referenced, but it's there to cause a confusing error message. Those three calls through the clicked attrbute are evidently intended for the button object, which is inside the class instance. My guess is that those three lines should be inside the setupUI method, and should have self. prefix on each. Next problem is that there's a comment at the beginning: # Form implementation generated from reading ui file 'mygui.ui' # # Created: Fri May 10 20:27:13 2013 # by: PyQt4 UI code generator 4.10.1 # # WARNING! All changes made in this file will be lost! which implies this file is a generated one. If you ever rerun that UI generator, you'll wipe out any changes you've made. Again, I'm not familiar with PyQT, so I don't know how likely that is. Next problem is that you have no top-level code that calls main(), which is evidently the code that gets things moving. Generally, you want to put all the top-level code inside main(), in which case the call to main() would be the only top-level code left. But there are exceptions. imports should nearly always go to the top, at top level. And you conditional functions need to stay at top-level just as you have them (the two clauses with try/cath in them). Final problem that I can spot is that if you DO need to reference those four functions from mainline code, you'll have to move them earlier in the file. But if you agreed with my earlier suggestion to move the three lines inside that method, then this wouldn't be a problem any more. > > i was wondering if somebody could walk me through making the game complete > and to add the results of the game (win/lose/stalemate) to the loutcome > label > Once you get to the innards of PyQT, I'll be no help at all. But I hope I've given you a little push in the right direction. -- DaveA From denismfmcmahon at gmail.com Mon May 13 16:36:07 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Mon, 13 May 2013 20:36:07 +0000 (UTC) Subject: in need of some help regarding my rock paper scissors game References: Message-ID: On Sun, 12 May 2013 20:33:44 +0100, Alex Norton wrote: > 'Traceback (most recent call last): File "C:\Users\Me\Desktop\testy.py", > line 174, in bWater.clicked.connect( water_clicked ) AttributeError: > 'int' > object has no attribute 'clicked'' appears when i run the module. It looks to me as if bWater is an integer value and doesn't have a .clicked attribute. Where is bWater assigned, and what is it assigned to? If you have a clickable "water" widget of some sort in your gui, what is the click handler for that widget defined as? -- Denis McMahon, denismfmcmahon at gmail.com From titanix88 at gmail.com Sun May 12 19:23:16 2013 From: titanix88 at gmail.com (Mr. Joe) Date: Mon, 13 May 2013 05:23:16 +0600 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] Message-ID: I seem to stumble upon a situation where "!=" operator misbehaves in python2.x. Not sure if it's my misunderstanding or a bug in python implementation. Here's a demo code to reproduce the behavior - """ # -*- coding: utf-8 -*- from __future__ import unicode_literals, print_function class DemoClass(object): def __init__(self, val): self.val = val def __eq__(self, other): return self.val == other.val x = DemoClass('a') y = DemoClass('a') print("x == y: {0}".format(x == y)) print("x != y: {0}".format(x != y)) print("not x == y: {0}".format(not x == y)) """ In python3, the output is as expected: """ x == y: True x != y: False not x == y: False """ In python2.7.3, the output is: """ x == y: True x != y: True not x == y: False """ Which is not correct!! Thanks in advance for clarifications. Regards, TB From ned at nedbatchelder.com Sun May 12 19:35:39 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Sun, 12 May 2013 19:35:39 -0400 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: Message-ID: <5190274B.5090308@nedbatchelder.com> On 5/12/2013 7:23 PM, Mr. Joe wrote: > I seem to stumble upon a situation where "!=" operator misbehaves in > python2.x. Not sure if it's my misunderstanding or a bug in python > implementation. Here's a demo code to reproduce the behavior - > """ > # -*- coding: utf-8 -*- > from __future__ import unicode_literals, print_function > > class DemoClass(object): > def __init__(self, val): > self.val = val > > def __eq__(self, other): > return self.val == other.val > > x = DemoClass('a') > y = DemoClass('a') > > print("x == y: {0}".format(x == y)) > print("x != y: {0}".format(x != y)) > print("not x == y: {0}".format(not x == y)) > """ > > In python3, the output is as expected: > """ > x == y: True > x != y: False > not x == y: False > """ In Python 3, if __ne__ isn't defined, "!=" will call __eq__ and negate the result. > In python2.7.3, the output is: > """ > x == y: True > x != y: True > not x == y: False > """ > Which is not correct!! In Python 2, "!=" only calls __ne__. Since you don't have one defined, it's using the built-in object comparison, and since x and y are different objects, they are not equal to each other, so x != y is True. > Thanks in advance for clarifications. > Regards, > TB From christian at python.org Sun May 12 19:39:03 2013 From: christian at python.org (Christian Heimes) Date: Mon, 13 May 2013 01:39:03 +0200 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: Message-ID: Am 13.05.2013 01:23, schrieb Mr. Joe: > I seem to stumble upon a situation where "!=" operator misbehaves in > python2.x. Not sure if it's my misunderstanding or a bug in python > implementation. Here's a demo code to reproduce the behavior - > """ Python 2.7 doesn't use the negation of __eq__ when your class doesn't provide a __ne__ function. Just add a print() to your __eq__ method and you'll notice the different. You have to provide both: class DemoClass(object): def __init__(self, val): self.val = val def __eq__(self, other): if not isinstance(other, DemoClass): return NotImplemented return self.val == other.val def __ne__(self, other): if not isinstance(other, DemoClass): return NotImplemented return self.val != other.val or def __ne__(self, other): result = self.__eq__(other) if result is NotImplemented: return NotImplemented return not result From ian.g.kelly at gmail.com Sun May 12 19:40:17 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 12 May 2013 17:40:17 -0600 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: Message-ID: On Sun, May 12, 2013 at 5:23 PM, Mr. Joe wrote: > I seem to stumble upon a situation where "!=" operator misbehaves in > python2.x. Not sure if it's my misunderstanding or a bug in python > implementation. Here's a demo code to reproduce the behavior - The != operator is implemented by the __ne__ special method. In Python 3, the default implementation of __ne__ is to call __eq__ and return the opposite of whatever it returns. In Python 2, __ne__ calls the older __cmp__ method instead, which is no longer meaningful in Python 3. From sg552 at hotmail.co.uk Sun May 12 19:52:18 2013 From: sg552 at hotmail.co.uk (Rotwang) Date: Mon, 13 May 2013 00:52:18 +0100 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: Message-ID: On 13/05/2013 00:40, Ian Kelly wrote: > On Sun, May 12, 2013 at 5:23 PM, Mr. Joe wrote: >> I seem to stumble upon a situation where "!=" operator misbehaves in >> python2.x. Not sure if it's my misunderstanding or a bug in python >> implementation. Here's a demo code to reproduce the behavior - > > The != operator is implemented by the __ne__ special method. In > Python 3, the default implementation of __ne__ is to call __eq__ and > return the opposite of whatever it returns. One should be aware, however, that this doesn't necessarily apply to classes inheriting from builtins other than object (a fact that recently bit me on the a***): >>> class spam: def __eq__(self, other): print('spam') return super().__eq__(other) >>> class eggs(list): def __eq__(self, other): print('eggs') return super().__eq__(other) >>> spam() != spam() spam spam True >>> eggs() != eggs() False From alister.ware at ntlworld.com Mon May 13 05:59:03 2013 From: alister.ware at ntlworld.com (Alister) Date: Mon, 13 May 2013 09:59:03 GMT Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] References: Message-ID: On Mon, 13 May 2013 05:23:16 +0600, Mr. Joe wrote: > I seem to stumble upon a situation where "!=" operator misbehaves in > python2.x. Not sure if it's my misunderstanding or a bug in python > implementation. Here's a demo code to reproduce the behavior - > """ > # -*- coding: utf-8 -*- > from __future__ import unicode_literals, print_function > > class DemoClass(object): > def __init__(self, val): > self.val = val > > def __eq__(self, other): > return self.val == other.val > > x = DemoClass('a') > y = DemoClass('a') > > print("x == y: {0}".format(x == y)) > print("x != y: {0}".format(x != y)) > print("not x == y: {0}".format(not x == y)) > """ > > In python3, the output is as expected: > """ > x == y: True x != y: False not x == y: False """ > > In python2.7.3, the output is: > """ > x == y: True x != y: True not x == y: False """ > Which is not correct!! > > Thanks in advance for clarifications. > Regards, > TB this looks to me like an issue with operator precidence you code is evaluating as (Not x) == y rather than not (x == y) why the difference between 2.7 & 3.X is beyond my knowledge of the language The other explanations seem to detail things at low level but it is all Greek to me -- Nice guys finish last. -- Leo Durocher From fabiosantosart at gmail.com Mon May 13 13:26:50 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 13 May 2013 18:26:50 +0100 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: Message-ID: On 13 May 2013 11:04, "Alister" wrote: > this looks to me like an issue with operator precidence > > you code is evaluating as (Not x) == y > rather than not (x == y) I can say for sure that the precedence is as expected. I always use "not ... == ..." Instead of !=. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ned at nedbatchelder.com Mon May 13 14:08:39 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Mon, 13 May 2013 14:08:39 -0400 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: Message-ID: <51912C27.3020809@nedbatchelder.com> On 5/13/2013 1:26 PM, F?bio Santos wrote: > > > On 13 May 2013 11:04, "Alister" > wrote: > > this looks to me like an issue with operator precidence > > > > you code is evaluating as (Not x) == y > > rather than not (x == y) > > I can say for sure that the precedence is as expected. I always use > "not ... == ..." Instead of !=. > > If you don't mind my asking, why do you do that? --Ned. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon May 13 14:15:22 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 04:15:22 +1000 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: <51912C27.3020809@nedbatchelder.com> References: <51912C27.3020809@nedbatchelder.com> Message-ID: On Tue, May 14, 2013 at 4:08 AM, Ned Batchelder wrote: > > On 5/13/2013 1:26 PM, F?bio Santos wrote: > > > On 13 May 2013 11:04, "Alister" wrote: >> this looks to me like an issue with operator precidence >> >> you code is evaluating as (Not x) == y >> rather than not (x == y) > > I can say for sure that the precedence is as expected. I always use "not ... > == ..." Instead of !=. > > > > If you don't mind my asking, why do you do that? I think it's fairly obvious. Like the stumpy-tailed dog from "The Loaded Dog" [1], he's saving up the != operator in case he needs it later. ChrisA [1] Full text here. http://jendi.bowmeow.com.au/loadeddog1.html From fabiosantosart at gmail.com Mon May 13 14:28:29 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 13 May 2013 19:28:29 +0100 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: <51912C27.3020809@nedbatchelder.com> References: <51912C27.3020809@nedbatchelder.com> Message-ID: I think it is more readable. When doing more complicated statements I use != instead, but when it's a single test I prefer not ? == It's a personal thing. It may also have to do with the fact that I didn't know python had != when I was a novice. On 13 May 2013 19:08, "Ned Batchelder" wrote: > > On 5/13/2013 1:26 PM, F?bio Santos wrote: > > > On 13 May 2013 11:04, "Alister" wrote: > > this looks to me like an issue with operator precidence > > > > you code is evaluating as (Not x) == y > > rather than not (x == y) > > I can say for sure that the precedence is as expected. I always use "not > ... == ..." Instead of !=. > > > If you don't mind my asking, why do you do that? > > --Ned. > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alister.ware at ntlworld.com Mon May 13 17:17:41 2013 From: alister.ware at ntlworld.com (Alister) Date: Mon, 13 May 2013 21:17:41 GMT Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] References: <51912C27.3020809@nedbatchelder.com> Message-ID: On Mon, 13 May 2013 19:28:29 +0100, F?bio Santos wrote: > I think it is more readable. When doing more complicated statements I > use != instead, but when it's a single test I prefer not ? == > > It's a personal thing. It may also have to do with the fact that I > didn't know python had != when I was a novice. > On 13 May 2013 19:08, "Ned Batchelder" wrote: > I would then still write it as not (x == y) to make it clear to myself & avoid any possible confusion although I think that X != Y is much cleaner. 2 lines from the zen stand out here:- Explicit is better than implicit. in the face of ambiguity refuse the temptation to guess. there are many features of Python (& other languages) i did not now when I started but have adopted once I understood what they were & how they worked. then again use what you are most comfortable with. Practicality beats purity From fabiosantosart at gmail.com Mon May 13 17:48:28 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 13 May 2013 22:48:28 +0100 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: <51912C27.3020809@nedbatchelder.com> Message-ID: On Mon, May 13, 2013 at 10:17 PM, Alister wrote: > On Mon, 13 May 2013 19:28:29 +0100, F?bio Santos wrote: > >> I think it is more readable. When doing more complicated statements I >> use != instead, but when it's a single test I prefer not ? == >> >> It's a personal thing. It may also have to do with the fact that I >> didn't know python had != when I was a novice. >> On 13 May 2013 19:08, "Ned Batchelder" wrote: >> > > I would then still write it as not (x == y) to make it clear to myself & > avoid any possible confusion although I think that X != Y is much > cleaner. > 2 lines from the zen stand out here:- > > Explicit is better than implicit. > in the face of ambiguity refuse the temptation to guess. > And here I was, thinking I was being pythonic. I hope other people using my code will be able to understand it well, not just myself, so using the most common idioms should be the best way to go. -- F?bio Santos From breamoreboy at yahoo.co.uk Mon May 13 18:53:01 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 13 May 2013 23:53:01 +0100 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: <51912C27.3020809@nedbatchelder.com> Message-ID: On 13/05/2013 22:17, Alister wrote: > On Mon, 13 May 2013 19:28:29 +0100, F?bio Santos wrote: > >> I think it is more readable. When doing more complicated statements I >> use != instead, but when it's a single test I prefer not ? == >> >> It's a personal thing. It may also have to do with the fact that I >> didn't know python had != when I was a novice. >> On 13 May 2013 19:08, "Ned Batchelder" wrote: >> > > I would then still write it as not (x == y) to make it clear to myself & > avoid any possible confusion although I think that X != Y is much > cleaner. > 2 lines from the zen stand out here:- > > Explicit is better than implicit. > in the face of ambiguity refuse the temptation to guess. > > there are many features of Python (& other languages) i did not now when > I started but have adopted once I understood what they were & how they > worked. then again use what you are most comfortable with. > > Practicality beats purity > I much prefer the alternative <> for != but some silly people insisted that this be removed from Python3. Just how stupid can you get? -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From davea at davea.name Mon May 13 19:22:24 2013 From: davea at davea.name (Dave Angel) Date: Mon, 13 May 2013 19:22:24 -0400 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: <51912C27.3020809@nedbatchelder.com> Message-ID: <519175B0.90603@davea.name> On 05/13/2013 06:53 PM, Mark Lawrence wrote: > On 13/05/2013 22:17, Alister wrote: >> On Mon, 13 May 2013 19:28:29 +0100, F?bio Santos wrote: >> >>> I think it is more readable. When doing more complicated statements I >>> use != instead, but when it's a single test I prefer not ? == >>> >>> It's a personal thing. It may also have to do with the fact that I >>> didn't know python had != when I was a novice. >>> On 13 May 2013 19:08, "Ned Batchelder" wrote: >>> >> >> I would then still write it as not (x == y) to make it clear to myself & >> avoid any possible confusion although I think that X != Y is much >> cleaner. >> 2 lines from the zen stand out here:- >> >> Explicit is better than implicit. >> in the face of ambiguity refuse the temptation to guess. >> >> there are many features of Python (& other languages) i did not now when >> I started but have adopted once I understood what they were & how they >> worked. then again use what you are most comfortable with. >> >> Practicality beats purity >> > > I much prefer the alternative <> for != but some silly people insisted > that this be removed from Python3. Just how stupid can you get? > So which special methods should the <> operator call? By rights it ought to call both __gt__ and __lt__ and return True if either of them is True. -- DaveA From cs at zip.com.au Mon May 13 19:30:40 2013 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 14 May 2013 09:30:40 +1000 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: <519175B0.90603@davea.name> References: <519175B0.90603@davea.name> Message-ID: <20130513233040.GA97066@cskk.homeip.net> On 13May2013 19:22, Dave Angel wrote: | On 05/13/2013 06:53 PM, Mark Lawrence wrote: | >I much prefer the alternative <> for != but some silly people insisted | >that this be removed from Python3. Just how stupid can you get? | | So which special methods should the <> operator call? By rights it | ought to call both __gt__ and __lt__ and return True if either of | them is True. Surely it should require both of them to be true... Personally I'm for != given we have ==. Aside from notational consistency it makes conceptual sense for unordered types, which <> does not really. Cheers, -- Cameron Simpson From davea at davea.name Mon May 13 21:41:33 2013 From: davea at davea.name (Dave Angel) Date: Mon, 13 May 2013 21:41:33 -0400 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: <20130513233040.GA97066@cskk.homeip.net> References: <519175B0.90603@davea.name> <20130513233040.GA97066@cskk.homeip.net> Message-ID: <5191964D.9040900@davea.name> On 05/13/2013 07:30 PM, Cameron Simpson wrote: > On 13May2013 19:22, Dave Angel wrote: > | On 05/13/2013 06:53 PM, Mark Lawrence wrote: > | >I much prefer the alternative <> for != but some silly people insisted > | >that this be removed from Python3. Just how stupid can you get? > | > | So which special methods should the <> operator call? By rights it > | ought to call both __gt__ and __lt__ and return True if either of > | them is True. > > Surely it should require both of them to be true... Then it would never be true. At least not for numbers. > > Personally I'm for != given we have ==. Aside from notational > consistency it makes conceptual sense for unordered types, which > <> does not really. That's the point of mentioning __gt__ and __lt__, they aren't available on unordered types. -- DaveA From cs at zip.com.au Mon May 13 23:20:36 2013 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 14 May 2013 13:20:36 +1000 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: <5191964D.9040900@davea.name> References: <5191964D.9040900@davea.name> Message-ID: <20130514032036.GA40794@cskk.homeip.net> On 13May2013 21:41, Dave Angel wrote: | On 05/13/2013 07:30 PM, Cameron Simpson wrote: | >On 13May2013 19:22, Dave Angel wrote: | >| On 05/13/2013 06:53 PM, Mark Lawrence wrote: | >| >I much prefer the alternative <> for != but some silly people insisted | >| >that this be removed from Python3. Just how stupid can you get? | >| | >| So which special methods should the <> operator call? By rights it | >| ought to call both __gt__ and __lt__ and return True if either of | >| them is True. | > | >Surely it should require both of them to be true... | | Then it would never be true. At least not for numbers. Well that was the point. The _symbol_ looks like it should want both. Next time I'll include the smiley. Cheers, -- Cameron Simpson I really don't like :-) symbols as well. I feel that if you can't see the pie coming, you deserve whipped cream up your nose. - robd at cherry.cray.com (rob derrick) From rosuav at gmail.com Tue May 14 03:31:41 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 17:31:41 +1000 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: <519175B0.90603@davea.name> References: <51912C27.3020809@nedbatchelder.com> <519175B0.90603@davea.name> Message-ID: On Tue, May 14, 2013 at 9:22 AM, Dave Angel wrote: > On 05/13/2013 06:53 PM, Mark Lawrence wrote: >> I much prefer the alternative <> for != but some silly people insisted >> that this be removed from Python3. Just how stupid can you get? >> > > So which special methods should the <> operator call? By rights it ought to > call both __gt__ and __lt__ and return True if either of them is True. Why do you think that? After all, the != operator doesn't call __factorial__ and __assignment__ and return True if either is True, does it? ChrisA PS. :-) From bahamutzero8825 at gmail.com Mon May 13 19:27:38 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 13 May 2013 18:27:38 -0500 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: References: <51912C27.3020809@nedbatchelder.com> Message-ID: <519176EA.7080502@gmail.com> On 2013.05.13 17:53, Mark Lawrence wrote: > I much prefer the alternative <> for != but some silly people insisted > that this be removed from Python3. It's not removed from Python 3, though: Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from __future__ import barry_as_FLUFL >>> 3 <> 2 True -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From ian.g.kelly at gmail.com Mon May 13 19:32:40 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 13 May 2013 17:32:40 -0600 Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] In-Reply-To: <519176EA.7080502@gmail.com> References: <51912C27.3020809@nedbatchelder.com> <519176EA.7080502@gmail.com> Message-ID: On Mon, May 13, 2013 at 5:27 PM, Andrew Berg wrote: > On 2013.05.13 17:53, Mark Lawrence wrote: >> I much prefer the alternative <> for != but some silly people insisted >> that this be removed from Python3. > It's not removed from Python 3, though: > > Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> from __future__ import barry_as_FLUFL >>>> 3 <> 2 > True I'd like to see ? added as a synonym for !=, even if nobody uses it. From steve+comp.lang.python at pearwood.info Tue May 14 01:09:48 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 May 2013 05:09:48 GMT Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] References: <51912C27.3020809@nedbatchelder.com> Message-ID: <5191c71c$0$29978$c3e8da3$5496439d@news.astraweb.com> On Mon, 13 May 2013 19:22:24 -0400, Dave Angel wrote: > So which special methods should the <> operator call? By rights it > ought to call both __gt__ and __lt__ and return True if either of them > is True. The <> operator comes from Pascal, where it was used as "not equal" since ASCII doesn't include the ? operator. Algol 60, by contrast, used ? since it was invented before ASCII. The use of ! as "not" is a C-ism and isn't universal. Given the data types Pascal had as standard back when it was invented, I think it is fair to say that "less than, or greater than" was logically equivalent to "not equal to". That's no longer the case though. Interestingly, later versions of Apple's "Standard Apple Numeric Environment" (SANE) included separate relational operations for "less than or greater than" and "not equal to". So if x was a NAN, then you could have pseudo-code like this: x != y # true x <> y # false -- Steven From steve+comp.lang.python at pearwood.info Tue May 14 20:15:53 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 May 2013 00:15:53 GMT Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] References: <51912C27.3020809@nedbatchelder.com> <5191c71c$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5192d3b8$0$29978$c3e8da3$5496439d@news.astraweb.com> On Tue, 14 May 2013 19:01:38 -0400, Dennis Lee Bieber wrote: > On 14 May 2013 05:09:48 GMT, Steven D'Aprano > declaimed the following in > gmane.comp.python.general: >> The <> operator comes from Pascal, where it was used as "not equal" >> since > > I thought it came from BASIC... > http://en.wikipedia.org/wiki/Dartmouth_BASIC Ah, well apparently BASIC used it earlier, but I don't know whether it was an influence on Pascal (except as "what not to do"). -- Steven From steve+comp.lang.python at pearwood.info Tue May 14 01:21:47 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 May 2013 05:21:47 GMT Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] References: <51912C27.3020809@nedbatchelder.com> Message-ID: <5191c9eb$0$29978$c3e8da3$5496439d@news.astraweb.com> On Mon, 13 May 2013 21:17:41 +0000, Alister wrote: > I would then still write it as not (x == y) to make it clear to myself & > avoid any possible confusion although I think that X != Y is much > cleaner. I think that is sad. If I read "not (x == y)" I would assume that the person writing the code didn't understand Python syntax or know its standard operators. The exception is if x or y was some unusual type where the __ne__ method does something different to the logical reverse of the __eq__ method. But if that is the case, I would expect a comment warning about it. If x and y were standard objects like floats, strings, etc. then writing out not (x == y) in full is as sensible as writing out these in full: x + -y # instead of x - y alist.insert(len(alist), 99) # instead of alist.append(99) # instead of adict.clear() for key in list(adict.keys()): del adict[key] x+x+x+x+x+x # instead of x*6 I'm sure any experienced Python programmer can come up with a hypothetical scenario where you would need to write things out "the long way", but that doesn't mean that we should do so all the time. > 2 lines from the zen stand out here:- > > Explicit is better than implicit. > in the face of ambiguity refuse the temptation to guess. != is explicit. There is no ambiguity that needs to be guessed. -- Steven From alister.ware at ntlworld.com Wed May 15 10:41:57 2013 From: alister.ware at ntlworld.com (Alister) Date: Wed, 15 May 2013 14:41:57 GMT Subject: Differences of "!=" operator behavior in python3 and python2 [ bug? ] References: <51912C27.3020809@nedbatchelder.com> <5191c9eb$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: > != is explicit. > > There is no ambiguity that needs to be guessed. Which is why i said it thought X != Y is cleaner i guess i wasn't totally clear, I would write X != Y its because the OP preferred to use the other format I recommended that he made the operator ordering explicit. -- Nature abhors a virgin -- a frozen asset. -- Clare Booth Luce From zcbenz at gmail.com Mon May 13 04:21:04 2013 From: zcbenz at gmail.com (zcbenz at gmail.com) Date: Mon, 13 May 2013 01:21:04 -0700 (PDT) Subject: How to make shutil.make_archive support symbol links? Message-ID: Hi comp.lang.python group, I'm trying to make a zip archive with shutil.make_archive, but it seems that it doesn't support symbol links, after creating a archive with shutil.make_archive, all the symbol links would be lost. What I want is something like `zip -y`, which "stores symbolic links as the link instead of the referenced file", is it possible to do in pure python? I also looked into the zipfile module, but it mentions no symbol links. Thanks. Cheng From breamoreboy at yahoo.co.uk Mon May 13 05:24:40 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 13 May 2013 10:24:40 +0100 Subject: Python's sad, unimaginative Enum Message-ID: That's the title of this little beast http://www.acooke.org/cute/Pythonssad0.html if anybody's interested. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From steve+comp.lang.python at pearwood.info Mon May 13 06:17:17 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 May 2013 10:17:17 GMT Subject: Python's sad, unimaginative Enum References: Message-ID: <5190bdac$0$29978$c3e8da3$5496439d@news.astraweb.com> On Mon, 13 May 2013 10:24:40 +0100, Mark Lawrence wrote: > That's the title of this little beast > http://www.acooke.org/cute/Pythonssad0.html if anybody's interested. Well, that's one way of looking at it. And I can't exactly *disagree*. But... but... In many ways, it's a dull language, borrowing solid old concepts from many other languages & styles: boring syntax, unsurprising semantics, few automatic coercions, etc etc. But that's one of the things I like about it. -- Tim Peters, 16 Sep 93 Being "sad and unimaginative" is a *good thing* when it comes to syntax. Which would you rather read? Python: try: int(astring) except ValueError: print(False) APL: ??VFI{w???((w='-')/w)?'?'?w} [pedant: okay, so the two code snippets are not *exactly* equivalent. So sue me.] Let's look at his major criticisms: 1) values aren't automatically generated. True. So what? That is the *least* important part of enums. The most important things about enums are: - they aren't strings, but look like symbolic values; - or they are ints (for compatibility with C libraries) that look like symbolic values. Probably half the use-cases for enums are for compatibility with C libraries, where you *need* to specify the value. There's no point you defining an enum SOCK_RAW, having Python decide to set it to 7, when the rest of the world agrees it should have the value 3. 2) The enum implementation allows duplicates. Yes, this *is* a feature. In the real world, enums are not unique. There are aliases (maybe you want FAMILY_NAME and SURNAME to be the same enum), and misspelled enums need to be corrected: class Insects(Enum): bee = 2 ant = 3 wasp = 4 # Preferred spelling. wsap = 4 # Oops, keep this for backward compatibility! I'm sorry for all those who can't avoid duplicating values, but really, Python doesn't protect them from other silly typos, why are Enums so special that they need to be treated differently? 3) the functional API for creating auto-numbered Enums "suffers from the same problems" as namedtuples: [quote] - you need to repeat the class name (in a string, which your IDE is unlikely to check) - the parameters are themselves in a string, which your IDE is unlikely to parse and provide in auto-complete (they can be separate strings, in a sequence, but that doesn't really help). [end quote] Then maybe you shouldn't be relying on such a lousy IDE then. Well, perhaps I'm being a tad harsh. After all, it's not like it is a *feature* that namedtuple *requires* you to include the class name. But really, it's a trivial inconvenience. Python has much worse, e.g.: - why aren't my CONSTANTS actually constant? and yet somehow we survive. 4) Auto-generated enums aren't strings: [quote] That would makes sense (red = 'red'), in that it would display nicely and is going to provide easy to debug values. So nope. [end quote] Missing the point entirely. The *whole point* of enum red is that it WILL display as 'red', even though it wraps an underlying value of . So this is a non-issue. I think Enums will be good addition to the standard library, and I look forward to dropping support for Python 3.3 so I can rely on them :-) -- Steven From rosuav at gmail.com Mon May 13 13:36:34 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 03:36:34 +1000 Subject: Python's sad, unimaginative Enum In-Reply-To: <5190bdac$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <5190bdac$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, May 13, 2013 at 8:17 PM, Steven D'Aprano wrote: > Let's look at his major criticisms: > > 1) values aren't automatically generated. > > True. So what? That is the *least* important part of enums. I stopped following the -ideas threads about enums, but IIRC autogeneration of values was in quite a few of the specs early on. So you can probably find the arguments against it in the list archives. FWIW, though, I do like C's autogeneration of enum values - but use it in only a small proportion of my enums. It's not a terrible loss, but it is a loss. ChrisA From dihedral88888 at googlemail.com Tue May 14 21:21:36 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 14 May 2013 18:21:36 -0700 (PDT) Subject: Python's sad, unimaginative Enum In-Reply-To: References: <5190bdac$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: Chris Angelico? 2013?5?14????UTC+8??1?36?34???? > On Mon, May 13, 2013 at 8:17 PM, Steven D'Aprano > > wrote: > > > Let's look at his major criticisms: > > > > > > 1) values aren't automatically generated. > > > > > > True. So what? That is the *least* important part of enums. > > > > I stopped following the -ideas threads about enums, but IIRC > > autogeneration of values was in quite a few of the specs early on. So > > you can probably find the arguments against it in the list archives. > > > > FWIW, though, I do like C's autogeneration of enum values - but use it > > in only a small proportion of my enums. It's not a terrible loss, but > > it is a loss. > > > > ChrisA Because a hash table can replace the enums in other languages, it is more pythonic to use a dictionary built first to replace the enums. I think it is the same style of programming in perl and ruby. From jeanmichel at sequans.com Mon May 13 07:00:36 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 13 May 2013 13:00:36 +0200 (CEST) Subject: Python's sad, unimaginative Enum In-Reply-To: Message-ID: <870762861.12001592.1368442836310.JavaMail.root@sequans.com> ----- Original Message ----- > That's the title of this little beast > http://www.acooke.org/cute/Pythonssad0.html if anybody's interested. > > -- > If you're using GoogleCrap? please read this > http://wiki.python.org/moin/GoogleGroupsPython. > > Mark Lawrence > > -- > http://mail.python.org/mailman/listinfo/python-list > python 2.5 class Enum: class __metaclass__(type): def __iter__(self): for attr in sorted(dir(self)): if not attr.startswith("__"): yield getattr(self, attr) class Colours(Enum): RED = "red" GREEN = "green" for c in Colours: print c green red JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From fabiosantosart at gmail.com Tue May 14 05:33:05 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 14 May 2013 10:33:05 +0100 Subject: Python's sad, unimaginative Enum In-Reply-To: <870762861.12001592.1368442836310.JavaMail.root@sequans.com> References: <870762861.12001592.1368442836310.JavaMail.root@sequans.com> Message-ID: On 13 May 2013 12:05, "Jean-Michel Pichavant" wrote: > > class Enum: > class __metaclass__(type): That's some cool metaclass fu! I didn't know that to be possible -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Mon May 13 22:00:13 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 May 2013 02:00:13 GMT Subject: Python's sad, unimaginative Enum References: Message-ID: <51919aad$0$29997$c3e8da3$5496439d@news.astraweb.com> On Mon, 13 May 2013 13:00:36 +0200, Jean-Michel Pichavant wrote: > ----- Original Message ----- >> That's the title of this little beast >> http://www.acooke.org/cute/Pythonssad0.html if anybody's interested. >> >> -- >> If you're using GoogleCrap? please read this >> http://wiki.python.org/moin/GoogleGroupsPython. >> >> Mark Lawrence >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > python 2.5 > > class Enum: > class __metaclass__(type): > def __iter__(self): > for attr in sorted(dir(self)): > if not attr.startswith("__"): > yield getattr(self, attr) > > class Colours(Enum): > RED = "red" > GREEN = "green" py> class Experience(Enum): ... NOVICE = 'novice' ... GREEN = 'green' ... EXPERIENCED = 'experienced' ... MASTER = 'master' ... py> py> Colours.GREEN == Experience.GREEN True Oops. It's very easy to make something which does a few things that enums should do, and call it an Enum. It's much harder to do a lot of things that enums should do. -- Steven From jeanmichel at sequans.com Tue May 14 05:24:02 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 14 May 2013 11:24:02 +0200 (CEST) Subject: Python's sad, unimaginative Enum In-Reply-To: <51919aad$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <441622996.12720717.1368523441993.JavaMail.root@sequans.com> ----- Original Message ----- > On Mon, 13 May 2013 13:00:36 +0200, Jean-Michel Pichavant wrote: > > > ----- Original Message ----- > >> That's the title of this little beast > >> http://www.acooke.org/cute/Pythonssad0.html if anybody's > >> interested. > >> > >> -- > >> If you're using GoogleCrap? please read this > >> http://wiki.python.org/moin/GoogleGroupsPython. > >> > >> Mark Lawrence > >> > >> -- > >> http://mail.python.org/mailman/listinfo/python-list > >> > >> > > python 2.5 > > > > class Enum: > > class __metaclass__(type): > > def __iter__(self): > > for attr in sorted(dir(self)): > > if not attr.startswith("__"): > > yield getattr(self, attr) > > > > class Colours(Enum): > > RED = "red" > > GREEN = "green" > > py> class Experience(Enum): > ... NOVICE = 'novice' > ... GREEN = 'green' > ... EXPERIENCED = 'experienced' > ... MASTER = 'master' > ... > py> > py> Colours.GREEN == Experience.GREEN > True > > > Oops. > > > It's very easy to make something which does a few things that enums > should do, and call it an Enum. It's much harder to do a lot of > things > that enums should do. > > > > -- > Steven I was just proposing a solution I've been using and found quite satisfactory. As for the perfect "enumness" of that solution, I don't know. To be honest, I'm not sure I know the exact definition of an enum, and whether or not the C enum fits 100% that definition. It does the job in python. Some people may find it useful, others may just ignore it. Additionally, the "bug" you mentioned can be written in C as well, casts allow to compare apples and oranges: (Colours::GREEN == (enum Coulours::Colour)Experiences::GREEN) JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From rustompmody at gmail.com Tue May 14 22:40:43 2013 From: rustompmody at gmail.com (rusi) Date: Tue, 14 May 2013 19:40:43 -0700 (PDT) Subject: Python's sad, unimaginative Enum References: Message-ID: <64ab830a-f257-4b92-a8f3-6e57981a194f@a10g2000pbr.googlegroups.com> On May 14, 2:24?pm, Jean-Michel Pichavant wrote: > ----- Original Message ----- > > On Mon, 13 May 2013 13:00:36 +0200, Jean-Michel Pichavant wrote: > > > > ----- Original Message ----- > > >> That's the title of this little beast > > >>http://www.acooke.org/cute/Pythonssad0.htmlif anybody's > > >> interested. > > > >> -- > > >> If you're using GoogleCrap? please read this > > >>http://wiki.python.org/moin/GoogleGroupsPython. > > > >> Mark Lawrence > > > >> -- > > >>http://mail.python.org/mailman/listinfo/python-list > > > > python 2.5 > > > > class Enum: > > > ? class __metaclass__(type): > > > ? ? def __iter__(self): > > > ? ? ? for attr in sorted(dir(self)): > > > ? ? ? ? if not attr.startswith("__"): > > > ? ? ? ? ? yield getattr(self, attr) > > > > class Colours(Enum): > > > ? RED = "red" > > > ? GREEN = "green" > > > py> class Experience(Enum): > > ... ? ? NOVICE = 'novice' > > ... ? ? GREEN = 'green' > > ... ? ? EXPERIENCED = 'experienced' > > ... ? ? MASTER = 'master' > > ... > > py> > > py> Colours.GREEN == Experience.GREEN > > True > > > Oops. > > > It's very easy to make something which does a few things that enums > > should do, and call it an Enum. It's much harder to do a lot of > > things > > that enums should do. > > > -- > > Steven > > I was just proposing a solution I've been using and found quite satisfactory. As for the perfect "enumness" of that solution, I don't know. To be honest, I'm not sure I know the exact definition of an enum, and whether or not the C enum fits 100% that definition. It does the job in python. Some people may find it useful, others may just ignore it. > Additionally, the "bug" you mentioned can be written in C as well, casts allow to compare apples and oranges: > > (Colours::GREEN == (enum Coulours::Colour)Experiences::GREEN) Enums are like names. And like names are impossible to do right: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ From steve+comp.lang.python at pearwood.info Mon May 13 05:20:09 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 May 2013 09:20:09 GMT Subject: [Off topic] Software epigrams Message-ID: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> My, it's been a long time since I've seen these: http://pu.inf.uni-tuebingen.de/users/klaeren/epigrams.html They pre-date the Zen of Python by at least a decade, and quite frankly I think many of them miss the mark. But whether you agree or disagree with them, they're worth reading. -- Steven From marduk at python.net Mon May 13 10:56:39 2013 From: marduk at python.net (marduk at python.net) Date: Mon, 13 May 2013 10:56:39 -0400 Subject: [Off topic] Software epigrams In-Reply-To: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1368456999.30979.140661230081921.36DBAF8A@webmail.messagingengine.com> On Mon, May 13, 2013, at 05:20 AM, Steven D'Aprano wrote: > My, it's been a long time since I've seen these: > > http://pu.inf.uni-tuebingen.de/users/klaeren/epigrams.html > > They pre-date the Zen of Python by at least a decade, and quite frankly I > think many of them miss the mark. But whether you agree or disagree with > them, they're worth reading. Yes, they're all over the place, but worth reading. My favorite is: 101: Dealing with failure is easy: Work hard to improve. Success is also easy to handle: You've solved the wrong problem. Work hard to improve. From rosuav at gmail.com Mon May 13 13:47:28 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 03:47:28 +1000 Subject: [Off topic] Software epigrams In-Reply-To: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, May 13, 2013 at 7:20 PM, Steven D'Aprano wrote: > My, it's been a long time since I've seen these: > > http://pu.inf.uni-tuebingen.de/users/klaeren/epigrams.html > > They pre-date the Zen of Python by at least a decade, and quite frankly I > think many of them miss the mark. But whether you agree or disagree with > them, they're worth reading. 8. A programming language is low level when its programs require attention to the irrelevant. So much a matter of debate. Indentation is irrelevant, why should Python programs pay attention to it? Block delimiters are irrelevant too, the interpreter should be able to figure them out from the code layout. But this one is absolutely right: 16. Every program has (at least) two purposes: the one for which it was written and another for which it wasn't. 48. The best book on programming for the layman is "Alice in Wonderland"; but that's because it's the best book on anything for the layman. LGTM. ChrisA From skip at pobox.com Mon May 13 14:02:42 2013 From: skip at pobox.com (Skip Montanaro) Date: Mon, 13 May 2013 13:02:42 -0500 Subject: [Off topic] Software epigrams In-Reply-To: References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: > 8. A programming language is low level when its programs require > attention to the irrelevant. > > So much a matter of debate. Indentation is irrelevant, why should > Python programs pay attention to it? Block delimiters are irrelevant > too, the interpreter should be able to figure them out from the code > layout. But this one is absolutely right: I think "irrelevant" in this context means stuff like memory management. My fave was #7: "It is easier to write an incorrect program than understand a correct one," which explains in large part why the wheel is so often reinvented... Skip From rosuav at gmail.com Mon May 13 14:12:53 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 04:12:53 +1000 Subject: [Off topic] Software epigrams In-Reply-To: References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, May 14, 2013 at 4:02 AM, Skip Montanaro wrote: >> 8. A programming language is low level when its programs require >> attention to the irrelevant. >> >> So much a matter of debate. Indentation is irrelevant, why should >> Python programs pay attention to it? Block delimiters are irrelevant >> too, the interpreter should be able to figure them out from the code >> layout. But this one is absolutely right: > > I think "irrelevant" in this context means stuff like memory management. Sure. That one's pretty clear (if you care about memory management, you want a low level language). But there's still plenty that are less clear. Though in generalities, I think I agree; paying attention to minutiae is the consequence of taking a lower-level language. It's just that the difference between relevant and irrelevant is hard to define. ChrisA From dan at tombstonezero.net Mon May 13 23:08:01 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Tue, 14 May 2013 03:08:01 GMT Subject: [Off topic] Software epigrams References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, 14 May 2013 04:12:53 +1000, Chris Angelico wrote: > On Tue, May 14, 2013 at 4:02 AM, Skip Montanaro wrote: >>> 8. A programming language is low level when its programs require >>> attention to the irrelevant. >> I think "irrelevant" in this context means stuff like memory >> management. > Sure. That one's pretty clear (if you care about memory management, > you want a low level language) ... http://www.memorymanagement.org/articles/lang.html says: C programmers think memory management is too important to be left to the computer. Lisp programmers think memory management is too important to be left to the user. (from Ellis and Stroustrup's The Annotated C++ Reference Manual) > ... It's just that the difference between relevant and irrelevant is > hard to define. And if I've designed my program the right way, what's relevant in one place (package, module, function, line of code) is different from what's relevant in another. Dan From rosuav at gmail.com Mon May 13 23:28:26 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 13:28:26 +1000 Subject: [Off topic] Software epigrams In-Reply-To: References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, May 14, 2013 at 1:08 PM, Dan Sommers wrote: > And if I've designed my program the right way, what's relevant in one > place (package, module, function, line of code) is different from what's > relevant in another. Absolutely. Layered systems FTW! Nothing matters but your current layer and those it touches. ChrisA From rustompmody at gmail.com Wed May 15 23:55:01 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 15 May 2013 20:55:01 -0700 (PDT) Subject: Software epigrams References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On May 14, 8:08?am, Dan Sommers wrote: > On Tue, 14 May 2013 04:12:53 +1000, Chris Angelico wrote: > > On Tue, May 14, 2013 at 4:02 AM, Skip Montanaro wrote: > >>> 8. A programming language is low level when its programs require > >>> attention to the irrelevant. > >> I think "irrelevant" in this context means stuff like memory > >> management. > > Sure. That one's pretty clear (if you care about memory management, > > you want a low level language) ... > > http://www.memorymanagement.org/articles/lang.htmlsays: > > ? ? C programmers think memory management is too important to be > ? ? left to the computer. Lisp programmers think memory management > ? ? is too important to be left to the user. > > ? ? (from Ellis and Stroustrup's The Annotated C++ Reference Manual) Notable physicist David Bohm wrote that the difficulty in understanding quantum physics is largely a result of the limitation of the subject-predicate format of Indo-European languages. He suggested some experiments in languaging called rheomode that makes English more process-oriented and less (abstract)noun oriented. One part of this project is to learn to use the word 'relevate' -- which is 'relevant' verbified with an element of 'elevate' as in 'lift into relief' I guess the Ellis and Stroupstrup quote above just shows that C++ programmers relevate in one direction whereas Lisp programmers relevate in another. More http://www.mindstructures.com/2010/04/meaning-and-context/ Twenty two years ago I wrote about this http://www.the-magus.in/Publications/chor.pdf Suddenly I am finding glowing references to this http://dieswaytoofast.blogspot.in/2013/01/why-i-grown-to-loathe-c.html And I am uneasy because these questions are far less rhetorical/ tautological than I imagines in 1990! So here's a (rather incomplete/preliminary) rebuttal to myself http://blog.languager.org/2013/02/c-in-education-and-software-engineering.html From neilc at norwich.edu Mon May 13 14:42:28 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 13 May 2013 18:42:28 GMT Subject: [Off topic] Software epigrams References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-13, Skip Montanaro wrote: >> 8. A programming language is low level when its programs >> require attention to the irrelevant. >> >> So much a matter of debate. Indentation is irrelevant, why >> should Python programs pay attention to it? Block delimiters >> are irrelevant too, the interpreter should be able to figure >> them out from the code layout. But this one is absolutely >> right: > > I think "irrelevant" in this context means stuff like memory > management. I thought I liked that one at first, but upon reflection it speciously inserts the word "irrelevant" in order to avoid stating a tautology: A programming language is low level when its programs require attention to low level details. -- Neil Cerutti From fabiosantosart at gmail.com Mon May 13 16:49:23 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 13 May 2013 21:49:23 +0100 Subject: [Off topic] Software epigrams In-Reply-To: References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13 May 2013 19:48, "Neil Cerutti" wrote: > > On 2013-05-13, Skip Montanaro wrote: > >> 8. A programming language is low level when its programs > >> require attention to the irrelevant. > >> > >> So much a matter of debate. Indentation is irrelevant, why > >> should Python programs pay attention to it? Block delimiters > >> are irrelevant too, the interpreter should be able to figure > >> them out from the code layout. But this one is absolutely > >> right: > > > > I think "irrelevant" in this context means stuff like memory > > management. > > I thought I liked that one at first, but upon reflection it > speciously inserts the word "irrelevant" in order to avoid > stating a tautology: A programming language is low level when its > programs require attention to low level details. > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list It's not a tautology in disguise. Irrelevant != low level. When low level details are relevant to the scope of my program, I use a low level language. -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Wed May 15 13:24:29 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 15 May 2013 17:24:29 GMT Subject: [Off topic] Software epigrams References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-13, F?bio Santos wrote: > > On 13 May 2013 19:48, "Neil Cerutti" wrote: >> >> On 2013-05-13, Skip Montanaro wrote: >> >> 8. A programming language is low level when its programs >> >> require attention to the irrelevant. >> >> >> >> So much a matter of debate. Indentation is irrelevant, why >> >> should Python programs pay attention to it? Block delimiters >> >> are irrelevant too, the interpreter should be able to figure >> >> them out from the code layout. But this one is absolutely >> >> right: >> > >> > I think "irrelevant" in this context means stuff like memory >> > management. >> >> I thought I liked that one at first, but upon reflection it >> speciously inserts the word "irrelevant" in order to avoid >> stating a tautology: A programming language is low level when its >> programs require attention to low level details. >> >> -- >> Neil Cerutti >> -- >> http://mail.python.org/mailman/listinfo/python-list > > It's not a tautology in disguise. Irrelevant != low level. When > low level details are relevant to the scope of my program, I > use a low level language. It is a tautology is disguise. When you use a low level language, low level details are relevant to the scope of your program. -- Neil Cerutti From fabiosantosart at gmail.com Wed May 15 13:49:34 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 15 May 2013 18:49:34 +0100 Subject: [Off topic] Software epigrams In-Reply-To: References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 15 May 2013 18:29, "Neil Cerutti" wrote: > > On 2013-05-13, F?bio Santos wrote: > > > > On 13 May 2013 19:48, "Neil Cerutti" wrote: > >> > >> On 2013-05-13, Skip Montanaro wrote: > >> >> 8. A programming language is low level when its programs > >> >> require attention to the irrelevant. > >> >> (...) > > It's not a tautology in disguise. Irrelevant != low level. When > > low level details are relevant to the scope of my program, I > > use a low level language. > > It is a tautology is disguise. When you use a low level language, > low level details are relevant to the scope of your program. > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list I don't see it that way. I think relevance and level are two unrelated concepts. For example, in python you are handling irrelevant things if you are trying to start a program and redirecting its standard output into another program's standard input instead of just using the shell and a pipe to do it. And in C you are just at the right level to write something for a microchip, but then again you are doing a load of irrelevant stuff if you need to work numbers larger than the maximum permitted. -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Wed May 15 14:27:39 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 15 May 2013 18:27:39 GMT Subject: [Off topic] Software epigrams References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-15, F?bio Santos wrote: >> It is a tautology is disguise. When you use a low level >> language, low level details are relevant to the scope of your >> program. > > I don't see it that way. I think relevance and level are two > unrelated concepts. > > For example, in python you are handling irrelevant things if > you are trying to start a program and redirecting its standard > output into another program's standard input instead of just > using the shell and a pipe to do it. > > And in C you are just at the right level to write something for > a microchip, but then again you are doing a load of irrelevant > stuff if you need to work numbers larger than the maximum > permitted. If you need numbers larger than the maximum permitted then all the code you write to handle them is relevant. If I want to bake bread I hope I don't have to till a garden, plant the wheat, harvest the wheat, and grind the wheat. But gardening is relevant to bread baking weather or not I do it. -- Neil Cerutti From fabiosantosart at gmail.com Wed May 15 20:21:52 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 16 May 2013 01:21:52 +0100 Subject: [Off topic] Software epigrams In-Reply-To: References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 15 May 2013 19:33, "Neil Cerutti" wrote: > > On 2013-05-15, F?bio Santos wrote: > >> It is a tautology is disguise. When you use a low level > >> language, low level details are relevant to the scope of your > >> program. > > > > I don't see it that way. I think relevance and level are two > > unrelated concepts. > > > > For example, in python you are handling irrelevant things if > > you are trying to start a program and redirecting its standard > > output into another program's standard input instead of just > > using the shell and a pipe to do it. > > > > And in C you are just at the right level to write something for > > a microchip, but then again you are doing a load of irrelevant > > stuff if you need to work numbers larger than the maximum > > permitted. > > If you need numbers larger than the maximum permitted then all > the code you write to handle them is relevant. > > If I want to bake bread I hope I don't have to till a garden, > plant the wheat, harvest the wheat, and grind the wheat. But > gardening is relevant to bread baking weather or not I do it. > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list Then memory management t is relevant to every python program even though it's done by the interpreter? And in Java we have factories, builders and builderfactories. What's so relevant about them? Java is high level, no? -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Thu May 16 10:23:28 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 16 May 2013 14:23:28 GMT Subject: [Off topic] Software epigrams References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-16, F?bio Santos wrote: >> If I want to bake bread I hope I don't have to till a garden, >> plant the wheat, harvest the wheat, and grind the wheat. But >> gardening is relevant to bread baking weather or not I do it. > > Then memory management t is relevant to every python program > even though it's done by the interpreter? Yes, I think so. If you didn't understand how Python managed memory, you couldn't write it effectively. You would end up making unecessary copies, and other pathological programming practices. > And in Java we have factories, builders and builderfactories. > What's so relevant about them? Java is high level, no? When I tried to pin down what an irrelevant detail in a computer program could be, I couldn't do it. I guess comment decorations, maybe? But those would have no bearing on the level of problem for which a programming language is most appropriate. -- Neil Cerutti From rosuav at gmail.com Thu May 16 10:37:31 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 May 2013 00:37:31 +1000 Subject: [Off topic] Software epigrams In-Reply-To: References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, May 17, 2013 at 12:23 AM, Neil Cerutti wrote: > On 2013-05-16, F?bio Santos wrote: >> And in Java we have factories, builders and builderfactories. >> What's so relevant about them? Java is high level, no? > > When I tried to pin down what an irrelevant detail in a computer > program could be, I couldn't do it. I guess comment decorations, > maybe? But those would have no bearing on the level of problem > for which a programming language is most appropriate. Let me give you a real example. One of the programs I wrote at work is a simple daemon that runs on every node in a network. It periodically sends out a heartbeat signal that the other nodes hear, and if any node hasn't been heard from in X seconds, it is deemed "down". (It might technically not be down, if there's a network problem, but the point is that we don't care about the difference. It's down.) There's also some incidental statussy data included, for convenience. This is implemented using UDP. Do I care about how a UDP packet is structured? No. Do I care about the mechanics of IP routing? No. Do I care about MAC addresses? No. They might feature in our IPv6 addresses, but I still don't care - the IP addresses (v4 or v6) of the nodes are treated as opaque tokens. All I care about is that I call a function with a string of data and a corresponding function gets called in the other program with that same string. All the details of how that happens in between aren't important to my program. They're somewhat of note in the design phase, but not to the program itself. They are, in fact, irrelevant. ChrisA From rustompmody at gmail.com Thu May 16 11:06:10 2013 From: rustompmody at gmail.com (rusi) Date: Thu, 16 May 2013 08:06:10 -0700 (PDT) Subject: Software epigrams References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2a8e503f-50c0-4d13-8ea3-c310575c977e@wg15g2000pbb.googlegroups.com> On May 16, 7:37?pm, Chris Angelico wrote: > On Fri, May 17, 2013 at 12:23 AM, Neil Cerutti wrote: > > On 2013-05-16, F?bio Santos wrote: > >> And in Java we have factories, builders and builderfactories. > >> What's so relevant about them? Java is high level, no? > > > When I tried to pin down what an irrelevant detail in a computer > > program could be, I couldn't do it. I guess comment decorations, > > maybe? But those would have no bearing on the level of problem > > for which a programming language is most appropriate. > > Let me give you a real example. > > One of the programs I wrote at work is a simple daemon that runs on > every node in a network. It periodically sends out a heartbeat signal > that the other nodes hear, and if any node hasn't been heard from in X > seconds, it is deemed "down". (It might technically not be down, if > there's a network problem, but the point is that we don't care about > the difference. It's down.) There's also some incidental statussy data > included, for convenience. This is implemented using UDP. > > Do I care about how a UDP packet is structured? No. > > Do I care about the mechanics of IP routing? No. > > Do I care about MAC addresses? No. They might feature in our IPv6 > addresses, but I still don't care - the IP addresses (v4 or v6) of the > nodes are treated as opaque tokens. > > All I care about is that I call a function with a string of data and a > corresponding function gets called in the other program with that same > string. All the details of how that happens in between aren't > important to my program. They're somewhat of note in the design phase, > but not to the program itself. They are, in fact, irrelevant. > > ChrisA You are just saying (in specific detail) what I said, viz that the programmer is one who 'relevates' ie sifts the relevant from the irrelevant and a good programming language is one that gives good relevating tools: http://blog.languager.org/2013/02/c-in-education-and-software-engineering.html From rosuav at gmail.com Thu May 16 11:19:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 May 2013 01:19:38 +1000 Subject: Software epigrams In-Reply-To: <2a8e503f-50c0-4d13-8ea3-c310575c977e@wg15g2000pbb.googlegroups.com> References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> <2a8e503f-50c0-4d13-8ea3-c310575c977e@wg15g2000pbb.googlegroups.com> Message-ID: On Fri, May 17, 2013 at 1:06 AM, rusi wrote: > On May 16, 7:37 pm, Chris Angelico wrote: >> On Fri, May 17, 2013 at 12:23 AM, Neil Cerutti wrote: >> > When I tried to pin down what an irrelevant detail in a computer >> > program could be, I couldn't do it. I guess comment decorations, >> > maybe? But those would have no bearing on the level of problem >> > for which a programming language is most appropriate. >> >> Let me give you a real example. >> [snip] > > You are just saying (in specific detail) what I said, viz that the > programmer is one who 'relevates' ie sifts the relevant from the > irrelevant and a good programming language is one that gives good > relevating tools: > http://blog.languager.org/2013/02/c-in-education-and-software-engineering.html Right. All I'm adding is a concrete example - which, I think, will be helpful to Neil, and perhaps to others too. ChrisA From inq1ltd at inqvista.com Wed May 15 16:56:01 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Wed, 15 May 2013 16:56:01 -0400 Subject: python 2.7 vs 2.5 In-Reply-To: References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4773190.v3vY0HWMWS@mach-114-20> My web server is using python 2.5. My cgi file is trying to open a shelve. I xxx out some of the path below. I am trying to find out if this error is caused because I am using python 2.7 to write the cgi file and my web server is using python 2.5. I would appreciate a confirmation of this. line 85, in FentriesFilled dbase = shelve.open( vpath, 'c' ) File "/usr/local/lib/python-2.5/lib/python2.5/shelve.py", line 225, in open return DbfilenameShelf(filename, flag, protocol, writeback) File "/usr/local/lib/python-2.5/lib/python2.5/shelve.py", line 209, in __init__ Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback) File "/usr/local/lib/python-2.5/lib/python2.5/anydbm.py", line 83, in open return mod.open(file, flag, mode) File "/usr/local/lib/python-2.5/lib/python2.5/dbhash.py", line 16, in open return bsddb.hashopen(file, flag, mode) File "/usr/local/lib/python-2.5/lib/python2.5/bsddb/__init__.py", line 306, in hashopen d.open(file, db.DB_HASH, flags, mode) bsddb.db.DBInvalidArgError: (22, 'Invalid argument --/home/users/web/xxxxx/yyy.inqvista/public_html/xxxx- ssss/email: unsupported hash version: 9') thanks, jd -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeanmichel at sequans.com Thu May 16 13:17:57 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 16 May 2013 19:17:57 +0200 (CEST) Subject: python 2.7 vs 2.5 In-Reply-To: <4773190.v3vY0HWMWS@mach-114-20> Message-ID: <645094577.14849462.1368724677094.JavaMail.root@sequans.com> ----- Original Message ----- >I am using python 2.7 to write > the cgi file and my web server is using python 2.5. The answer lies in your question. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From inq1ltd at inqvista.com Thu May 16 17:21:04 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Thu, 16 May 2013 17:21:04 -0400 Subject: python 2.7 vs 2.5 In-Reply-To: <645094577.14849462.1368724677094.JavaMail.root@sequans.com> References: <645094577.14849462.1368724677094.JavaMail.root@sequans.com> Message-ID: <59526395.mz35FTIYGg@mach-114-20> On Thursday, May 16, 2013 07:17:57 PM Jean-Michel Pichavant wrote: > ----- Original Message ----- > > >I am using python 2.7 to write > > > > the cgi file and my web server is using python 2.5. > > The answer lies in your question. > > JM > I appreciate the response, However, My question was wrong. The traceback I got couldn't be because of a difference in versions because I programmed the cgi on the site. I found that the path to the cgi had to be complete, something like this: /home/users/web/xxx/yyy.inqvista/public_html/home/read/mail anything else gets a traceback. jd -------------- next part -------------- An HTML attachment was scrubbed... URL: From inq1ltd at inqvista.com Thu May 30 15:48:20 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Thu, 30 May 2013 15:48:20 -0400 Subject: sendmail smtplib.SMTP('localhost') Where is the email? In-Reply-To: <4773190.v3vY0HWMWS@mach-114-20> References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> <4773190.v3vY0HWMWS@mach-114-20> Message-ID: <7572490.mfSH6Zg6jY@mach-114-20> python help, I've tried this code which I got from: http://www.tutorialspoint.com/python/python_sending_email.htm I build this file and run it ---//--- #!/usr/bin/python import smtplib sender = "inq1ltd at inqvista.com" receivers = ["webmaster at inqvista.com"] message = """From: jol To: webmaster Subject: SMTP e-mail test This is a test e-mail message. """ try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email" --//-- After running the the file and I get "Successfully sent email" My question is why doesn't webmaster get an email? jol -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu May 30 18:13:53 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 08:13:53 +1000 Subject: sendmail smtplib.SMTP('localhost') Where is the email? In-Reply-To: <7572490.mfSH6Zg6jY@mach-114-20> References: <5190b049$0$29978$c3e8da3$5496439d@news.astraweb.com> <4773190.v3vY0HWMWS@mach-114-20> <7572490.mfSH6Zg6jY@mach-114-20> Message-ID: On Fri, May 31, 2013 at 5:48 AM, inq1ltd wrote: > python help, > > I've tried this code which I got from: > > http://www.tutorialspoint.com/python/python_sending_email.htm > > I build this file and run it > > After running the the file and I get > > "Successfully sent email" > > My question is why doesn't webmaster get an email? First point: Please send *plain text* emails. Your message came out double-spaced and with absolutely no indentation, which destroys the structure of Python code. (Your script is sufficiently simple that it's fairly clear how it had to be laid out, but when you force us to use our crystal balls for simple stuff like indentation, we can't use them again for something more important. The cooldown on these babies is pretty stiff sometimes.) Secondly, it helps a LOT to tell us what you're running under - Python version, OS, etc. One easy way to do that is to quote the text that the interactive interpreter opens with, eg: Python 2.4.5 (#1, Jul 22 2011, 02:01:04) [GCC 4.1.1] on mingw32 Python 3.4.0a0 (default:5dcd7ee0716a, Mar 30 2013, 08:17:06) [GCC 4.7.2] on linux Now, on to your actual code. The docs say: """This method will return normally if the mail is accepted for at least one recipient. Otherwise it will raise an exception. That is, if this method does not raise an exception, then someone should get your mail.""" That's nice confidence-inducing language, but I take exception (if you'll pardon the pun) to the last part. The only thing this function can know is that the mail has been accepted by the next hop. After that... well, the internet is a scary place, anything could happen. See if your SMTP server (the one running on localhost) has a nice log; you may be able to watch the message get processed by the next hop. By the way, I wouldn't bother with the try/except here. Just let the exception bubble up and be emitted to stderr. It'll be more useful that way :) ChrisA From cs at zip.com.au Thu May 30 18:55:12 2013 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 31 May 2013 08:55:12 +1000 Subject: sendmail smtplib.SMTP('localhost') Where is the email? In-Reply-To: <7572490.mfSH6Zg6jY@mach-114-20> References: <7572490.mfSH6Zg6jY@mach-114-20> Message-ID: <20130530225512.GA60695@cskk.homeip.net> On 30May2013 15:48, inq1ltd wrote: | python help, Please do not make new discussions by replying to an old discussion. It is not enough to change the subject line; unless you also remove any References: and In-Reply-To: header lines your message is still considered part of the old discussion. | I've tried this code which I got from: | http://www.tutorialspoint.com/python/python_sending_email.htm | | I build this file and run it [...] | smtpObj = smtplib.SMTP('localhost') | smtpObj.sendmail(sender, receivers, message) | print "Successfully sent email" [...] | After running the the file and I get | "Successfully sent email" | | My question is why doesn't webmaster get an email? Well, this suggests that the message has been accepted by the mail system on localhost. Not that final delivery was made anywhere else. You now have to read the log files on your mail system to see what happened. One easy check to do first is to see if it is still in your mail system but undelivered. On a UNIX system running the command: mailq should tell you that. If the queue is empty, the message has been sent somewhere and you must dig through the logs to find out where. If the message is in the queue then the "mailq" command will probably give a suggestion as to why. Cheers, -- Cameron Simpson As you can see, unraveling even a small part of 'sendmail' can introduce more complexity than answers. - Brian Costales, _sendmail_ From inq1ltd at inqvista.com Fri May 31 12:07:58 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Fri, 31 May 2013 12:07:58 -0400 Subject: sendmail smtplib.SMTP('localhost') Where is the email? In-Reply-To: <20130530225512.GA60695@cskk.homeip.net> References: <7572490.mfSH6Zg6jY@mach-114-20> <20130530225512.GA60695@cskk.homeip.net> Message-ID: <1713012.pPmM7KcTts@mach-114-20> Your responses helped. The mailg for linux gave me information I didn't expect. regards, jol On Friday, May 31, 2013 08:55:12 AM Cameron Simpson wrote: > On 30May2013 15:48, inq1ltd wrote: > | python help, > > Please do not make new discussions by replying to an old discussion. > > It is not enough to change the subject line; unless you also remove > any References: and In-Reply-To: header lines your message is still > considered part of the old discussion. > > | I've tried this code which I got from: > | http://www.tutorialspoint.com/python/python_sending_email.htm > | > | I build this file and run it > > [...] > > | smtpObj = smtplib.SMTP('localhost') > | smtpObj.sendmail(sender, receivers, message) > | print "Successfully sent email" > > [...] > > | After running the the file and I get > | "Successfully sent email" > | > | My question is why doesn't webmaster get an email? > > Well, this suggests that the message has been accepted by the mail > system on localhost. Not that final delivery was made anywhere else. > > You now have to read the log files on your mail system to see what happened. > > One easy check to do first is to see if it is still in your mail > system but undelivered. On a UNIX system running the command: > > mailq > > should tell you that. If the queue is empty, the message has been > sent somewhere and you must dig through the logs to find out where. > If the message is in the queue then the "mailq" command will probably > give a suggestion as to why. > > Cheers, -------------- next part -------------- An HTML attachment was scrubbed... URL: From feather.duster.kung.fu at gmail.com Mon May 13 10:02:28 2013 From: feather.duster.kung.fu at gmail.com (feather.duster.kung.fu at gmail.com) Date: Mon, 13 May 2013 07:02:28 -0700 (PDT) Subject: global variable not working inside function. Increment In-Reply-To: References: Message-ID: <7a4886ba-482d-4edc-a186-a9dc2a1e3714@googlegroups.com> On Friday, September 4, 2009 4:43:27 PM UTC-7, Helvin wrote: > Hi, > > This increment thing is driving me nearly to the nuts-stage. > < > > I have a function that allows me to pick points. I want to count the > number of times I have picked points. > > global no_picked > no_picked = 0 > > def picked(object, event): > no_picked += 1 > print no_picked > > Error msg says: UnboundLocalError: local variable 'no_picked' > referenced before assignment > For some reason, no_picked does not increment, but the printing > statement works. > > Do you know why? > > (I'm actually writing this for a vtkrenderwindowinteractor.) > > Helvin From feather.duster.kung.fu at gmail.com Mon May 13 10:10:50 2013 From: feather.duster.kung.fu at gmail.com (charles benoit) Date: Mon, 13 May 2013 07:10:50 -0700 (PDT) Subject: global variable not working inside function. Increment In-Reply-To: References: Message-ID: <4c334b0e-88ba-43b1-9201-7a2ef5a270fe@googlegroups.com> On Friday, September 4, 2009 4:52:11 PM UTC-7, Rami Chowdhury wrote: > > global no_picked > > no_picked = 0 > > > > def picked(object, event): > > no_picked += 1 > > print no_picked > > In order to be able to affect variables in the global scope, you need to > declare them global inside the function, and not at the global scope. So > your code should read: > > no_picked = 0 > > def picked(object, event): > global no_picked > no_picked += 1 > print no_picked > > I believe that will work. > > On Fri, 04 Sep 2009 16:43:27 -0700, Helvin wrote: > > > Hi, > > > > This increment thing is driving me nearly to the nuts-stage. > < > > > > I have a function that allows me to pick points. I want to count the > > number of times I have picked points. > > > > global no_picked > > no_picked = 0 > > > > def picked(object, event): > > no_picked += 1 > > print no_picked > > > > Error msg says: UnboundLocalError: local variable 'no_picked' > > referenced before assignment > > For some reason, no_picked does not increment, but the printing > > statement works. > > > > Do you know why? > > > > (I'm actually writing this for a vtkrenderwindowinteractor.) > > > > Helvin > > > > -- > Rami Chowdhury > "Never attribute to malice that which can be attributed to stupidity" -- > Hanlon's Razor > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) From feather.duster.kung.fu at gmail.com Mon May 13 10:25:40 2013 From: feather.duster.kung.fu at gmail.com (feather.duster.kung.fu) Date: Mon, 13 May 2013 07:25:40 -0700 (PDT) Subject: global variable not working inside function. Increment In-Reply-To: <4c334b0e-88ba-43b1-9201-7a2ef5a270fe@googlegroups.com> References: <4c334b0e-88ba-43b1-9201-7a2ef5a270fe@googlegroups.com> Message-ID: <688b6146-4601-47a7-8418-5fbc28650307@googlegroups.com> On Monday, May 13, 2013 7:10:50 AM UTC-7, charles benoit wrote: > On Friday, September 4, 2009 4:52:11 PM UTC-7, Rami Chowdhury wrote: > > > > global no_picked > > > > no_picked = 0 > > > > > > > > def picked(object, event): > > > > no_picked += 1 > > > > print no_picked > > > > > > In order to be able to affect variables in the global scope, you need to > > > declare them global inside the function, and not at the global scope. So > > > your code should read: > > > > > > no_picked = 0 > > > > > > def picked(object, event): > > > global no_picked > > > no_picked += 1 > > > print no_picked > > > > > > I believe that will work. > > > > > > On Fri, 04 Sep 2009 16:43:27 -0700, Helvin wrote: > > > > > > > Hi, > > > > > > > > This increment thing is driving me nearly to the nuts-stage. > < > > > > > > > > I have a function that allows me to pick points. I want to count the > > > > number of times I have picked points. > > > > > > > > global no_picked > > > > no_picked = 0 > > > > > > > > def picked(object, event): > > > > no_picked += 1 > > > > print no_picked > > > > > > > > Error msg says: UnboundLocalError: local variable 'no_picked' > > > > referenced before assignment > > > > For some reason, no_picked does not increment, but the printing > > > > statement works. > > > > > > > > Do you know why? > > > > > > > > (I'm actually writing this for a vtkrenderwindowinteractor.) > > > > > > > > Helvin > > > > > > > > > > > > -- > > > Rami Chowdhury > > > "Never attribute to malice that which can be attributed to stupidity" -- > > > Hanlon's Razor > > > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) Thank You for setting that straight. I'm just learning Python and NONE of the tutorials I read said anything about that . In fact they all say a global can be called from inside a Function. If possible please contact the ppl that write these things.....I've heard of Ocam's razor but not Hanlon's??? From jeanmichel at sequans.com Mon May 13 11:07:41 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 13 May 2013 17:07:41 +0200 (CEST) Subject: global variable not working inside function. Increment In-Reply-To: <688b6146-4601-47a7-8418-5fbc28650307@googlegroups.com> Message-ID: <124893356.12204414.1368457661367.JavaMail.root@sequans.com> > Thank You for setting that straight. I'm just learning Python and > NONE of the tutorials I read said anything about that . In fact they > all say a global can be called from inside a Function. If possible > please contact the ppl that write these things.....I've heard of > Ocam's razor but not Hanlon's??? > -- > http://mail.python.org/mailman/listinfo/python-list It's covered by this one: http://docs.python.org/2/tutorial/classes.html#python-scopes-and-namespaces It's flagged as a quirk python behavior, and probably could afford a code sample, it can be difficult to grasp from the formal description. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From andipersti at gmail.com Mon May 13 12:13:28 2013 From: andipersti at gmail.com (Andreas Perstinger) Date: Mon, 13 May 2013 18:13:28 +0200 Subject: global variable not working inside function. Increment In-Reply-To: <688b6146-4601-47a7-8418-5fbc28650307@googlegroups.com> References: <4c334b0e-88ba-43b1-9201-7a2ef5a270fe@googlegroups.com> <688b6146-4601-47a7-8418-5fbc28650307@googlegroups.com> Message-ID: <20130513181328.942e8f2303ebb9fa5b592e7e@gmail.com> "feather.duster.kung.fu" wrote: >I'm just learning Python and NONE of the tutorials I read said >anything about that . In fact they all say a global can be called from >inside a Function. If possible please contact the ppl that write these >things. Well, we don't know which tutorials you read. So why don't you tell them yourself? Bye, Andreas From sharon_couka at hotmail.com Mon May 13 11:41:51 2013 From: sharon_couka at hotmail.com (Sharon COUKA) Date: Mon, 13 May 2013 17:41:51 +0200 Subject: Fractal Message-ID: Hello, I'm new to python and i have to make a Mandelbrot fractal image for school but I don't know how to zoom in my image. Thank you for helping me. Envoy? de mon iPad From cjw at ncf.ca Wed May 15 08:07:03 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Wed, 15 May 2013 08:07:03 -0400 Subject: Fractal In-Reply-To: References: Message-ID: <51937A67.1000702@ncf.ca> On 13/05/2013 11:41 AM, Sharon COUKA wrote: > Hello, I'm new to python and i have to make a Mandelbrot fractal image for school but I don't know how to zoom in my image. > Thank you for helping me. > > Envoy? de mon iPad Google is your friend. Try "Mandelbrot Python" Colin W. > From cjw at ncf.ca Wed May 15 08:07:03 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Wed, 15 May 2013 08:07:03 -0400 Subject: Fractal In-Reply-To: References: Message-ID: <51937A67.1000702@ncf.ca> On 13/05/2013 11:41 AM, Sharon COUKA wrote: > Hello, I'm new to python and i have to make a Mandelbrot fractal image for school but I don't know how to zoom in my image. > Thank you for helping me. > > Envoy? de mon iPad Google is your friend. Try "Mandelbrot Python" Colin W. > From wuwei23 at gmail.com Wed May 15 20:00:24 2013 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 May 2013 17:00:24 -0700 (PDT) Subject: Fractal References: <51937A67.1000702@ncf.ca> Message-ID: <2ce10d3b-505b-4ba6-8fae-77facfaac654@hc4g2000pbb.googlegroups.com> On May 15, 10:07?pm, "Colin J. Williams" wrote: > Google is your friend. ?Try "Mandelbrot Python" My favourite is this one: http://preshing.com/20110926/high-resolution-mandelbrot-in-obfuscated-python From ulrich.eckhardt at dominolaser.com Thu May 16 03:11:24 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 16 May 2013 09:11:24 +0200 Subject: Fractal In-Reply-To: <2ce10d3b-505b-4ba6-8fae-77facfaac654@hc4g2000pbb.googlegroups.com> References: <51937A67.1000702@ncf.ca> <2ce10d3b-505b-4ba6-8fae-77facfaac654@hc4g2000pbb.googlegroups.com> Message-ID: Am 16.05.2013 02:00, schrieb alex23: > My favourite is this one: > http://preshing.com/20110926/high-resolution-mandelbrot-in-obfuscated-python Not only is this blog entry an interesting piece of art, there's other interesting things to read there, too. Thanks! Uli From rosuav at gmail.com Thu May 16 03:24:00 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 16 May 2013 17:24:00 +1000 Subject: Fractal In-Reply-To: References: <51937A67.1000702@ncf.ca> <2ce10d3b-505b-4ba6-8fae-77facfaac654@hc4g2000pbb.googlegroups.com> Message-ID: On Thu, May 16, 2013 at 5:11 PM, Ulrich Eckhardt wrote: > Am 16.05.2013 02:00, schrieb alex23: > >> My favourite is this one: >> >> http://preshing.com/20110926/high-resolution-mandelbrot-in-obfuscated-python > > > Not only is this blog entry an interesting piece of art, there's other > interesting things to read there, too. I'm quite impressed, actually. Most people don't use Python for code art. Significant indentation is not usually a good thing there :) ChrisA From invalid at invalid.invalid Wed May 15 10:24:18 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 15 May 2013 14:24:18 +0000 (UTC) Subject: Fractal References: Message-ID: On 2013-05-13, Sharon COUKA wrote: > Hello, I'm new to python and i have to make a Mandelbrot fractal image for school but I don't know how to zoom in my image. > Thank you for helping me. It's a fractal image, so you zoom in/out with the following Python instruction: pass ;) -- Grant Edwards grant.b.edwards Yow! I'm not available at for comment.. gmail.com From ian.g.kelly at gmail.com Wed May 15 11:20:24 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 15 May 2013 09:20:24 -0600 Subject: Fractal In-Reply-To: References: Message-ID: On Mon, May 13, 2013 at 9:41 AM, Sharon COUKA wrote: > Hello, I'm new to python and i have to make a Mandelbrot fractal image for school but I don't know how to zoom in my image. > Thank you for helping me. Is this a GUI application or does it just write the image to a file? What GUI / image library are you using? From ian.g.kelly at gmail.com Thu May 16 11:35:43 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 16 May 2013 09:35:43 -0600 Subject: Fractal In-Reply-To: References: Message-ID: On Thu, May 16, 2013 at 5:04 AM, Sharon COUKA wrote: > I have to write the script, and i have one but the zoom does not work That doesn't answer my question. Perhaps if you would share with us what you already have, then we could point out what you need to do and where to get your "zoom" working. From ian.g.kelly at gmail.com Thu May 16 13:53:47 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 16 May 2013 11:53:47 -0600 Subject: Fractal In-Reply-To: References: Message-ID: On Thu, May 16, 2013 at 10:55 AM, Sharon COUKA wrote: > # Register events > c.bind('i', zoom) > c.bind('i', unzoom) > c.bind('i', mouseMove) I'm not an expert at Tkinter so maybe one of the other residents can help you better with that. The code above looks wrong to me, though. As far as I know, 'i' is not a valid event sequence in Tkinter, and besides you probably want to bind these functions to three *different* events. See here for the docs on event sequences: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-sequences.html Based on your code, it looks like you would probably want something like: c.bind('', zoom) c.bind('', unzoom) c.bind('', mouseMove) From jonathan.hayward at pobox.com Mon May 13 11:59:33 2013 From: jonathan.hayward at pobox.com (Jonathan Hayward) Date: Mon, 13 May 2013 10:59:33 -0500 Subject: Getting ASCII encoding where unicode wanted under Py3k Message-ID: I have a Py3k script, pasted below. When I run it I get an error about ASCII codecs that can't handle byte values that are too high. The error that I am getting is: UnicodeEncodeError: 'ascii' codec can't encode character '\u0161' in position 1442: ordinal not in range(128) args = ('ascii', "Content-Type: text/html\n\n\n\n...ype='submit'>\n \n \n", 1442, 1443, 'ordinal not in range(128)') encoding = 'ascii' end = 1443 object = "Content-Type: text/html\n\n\n\n...ype='submit'>\n \n \n" reason = 'ordinal not in range(128)' start = 1442 with_traceback = (And that was posted to StackOverflow--one shot in the dark answer so far.) My code is below. What should I be doing differently to be, in the most immediate sense, calls to '''%(foo)s''' % locals()? #!/usr/bin/python3 import cgi import cgitb;cgitb.enable() import os import pickle import sys cgi_form = cgi.FieldStorage() message = '' def get_cgi(field, default = ""): return cgi_form.getfirst(field, default) try: sys.stderr.write('abc: 1') input_file = open( os.path.join(os.path.dirname(__file__), '../../../russian/pickled'), 'rb') sys.stderr.write('abc: 2') state = pickle.load(input_file) sys.stderr.write('abc: 3') state['changed'] = False sys.stderr.write('abc: 4') state['loaded'] = True sys.stderr.write('abc: 5') except IOError: state = {} state['phrases'] = [] state['changed'] = True state['loaded'] = False #except UnicodeDecodeError: #state = {} #state['phrases'] = [] #state['changed'] = True state['loaded'] = False if get_cgi('russian') and get_cgi('english'): state['phrases'].append([get_cgi('russian'), get_cgi('english')]) message = 'Your changes have been saved.' state['changed'] = True elif get_cgi('english'): state['phrases'].append([None, get_cgi('english')]) message = 'Your change has been saved.' state['changed'] = True if get_cgi('mode') == 'edit': to_delete = [] for index in range(len(state['phrases'])): if get_cgi('russian_' + str(index), None) != None: state['phrases'][index][0] = get_cgi('russian_' + str(index)) if get_cgi('english_' + str(index), None) != None: state['phrases'][index][1] = get_cgi('english_' + str(index)) if get_cgi('delete_' + str(index), None) != None: to_delete.insert(0, index) #to_delete.append(index) #to_delete.sort(lambda a, b: -cmp(a, b)) for element in to_delete: del state['phrases'][element] state['changed'] = True sys.stderr.write('abc: ' + repr(state)) if state['changed']: output_file = open( os.path.join(os.path.dirname(__file__), '../../../russian/pickled_new.' + str(os.getpid())), 'wb') pickle.dump(state, output_file) output_file.close() os.rename( os.path.join(os.path.dirname(__file__), '../../../russian/pickled_new.' + str(os.getpid())), os.path.join(os.path.dirname(__file__), '../../../russian/pickled')) if get_cgi('mode') == 'add': print('''Content-type: text/html
%(message)s

Russian:

English:

''' % locals()) elif get_cgi('mode') == 'edit': edit_table = '' edit_table += '' edit_table += '' edit_table += '' edit_table += '' edit_table += '' edit_table += '' for index in range(len(state['phrases'])): if state['phrases'][index][0]: russian = state['phrases'][index][0].replace('"', "''") else: russian = None english = state['phrases'][index][1].replace('"', "''") edit_table += '' edit_table += '\n' edit_table += '\n' edit_table += '' print ('''Content-Type: text/html Edit Phrases %(edit_table)s ''' % locals()) else: text = '' for phrase in state['phrases']: if phrase[0]: text += ('

' + phrase[0] + '

') else: text += '

' + phrase[1] + '

' print ('''Content-type: text/html ___________ %(text)s ''' % locals()) -- [image: Christos Jonathan Hayward] Christos Jonathan Hayward, an Orthodox Christian author. *Amazon * ? Author Bio ? *Author Site * ? *Email * ? Facebook ? Fan Page ? Google Plus ? LinkedIn ? *Professional * ? Twitter ? *Web * ? What's New? If you read just *one* of my books, you'll want *The Best of Jonathan's Corner *. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Mon May 13 12:34:15 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 13 May 2013 17:34:15 +0100 Subject: Getting ASCII encoding where unicode wanted under Py3k In-Reply-To: References: Message-ID: <51911607.9010609@mrabarnett.plus.com> On 13/05/2013 16:59, Jonathan Hayward wrote: > I have a Py3k script, pasted below. When I run it I get an error about > ASCII codecs that can't handle byte values that are too high. > > The error that I am getting is: > > |UnicodeEncodeError: 'ascii' codec can't encode character'\u0161' in position 1442: ordinal not in range(128) > args = ('ascii', "Content-Type: text/html\n\n\n\n...ype='submit'>\n \n \n", 1442, 1443,'ordinalnot in range(128)') > encoding = 'ascii' > end = 1443 > object = "Content-Type: text/html\n\n\n\n...ype='submit'>\n \n \n" > reason = 'ordinalnot in range(128)' > start = 1442 > with_traceback = | > > (And that was posted to StackOverflow--one shot in the dark answer so far.) > > My code is below. What should I be doing differently to be, in the most > immediate sense, calls to '''%(foo)s''' % locals()? > [snip] The 'print' functions send its output to sys.stdout, which, in your case, is set up to encode to ASCII for output, but '\u0161' can't be encoded to ASCII. Try encoding to UTF-8 instead: from codecs import getwriter sys.stdout = getwriter("utf-8")(sys.stdout.buffer) From ppearson at nowhere.invalid Mon May 13 12:32:57 2013 From: ppearson at nowhere.invalid (Peter Pearson) Date: 13 May 2013 16:32:57 GMT Subject: Getting ASCII encoding where unicode wanted under Py3k References: Message-ID: On Mon, 13 May 2013 10:59:33 -0500, Jonathan Hayward wrote: > --e89a8f3b9db145cbab04dc9b9a23 > Content-Type: text/plain; charset=windows-1252 > Content-Transfer-Encoding: quoted-printable > [snipped many lines of quoted-printable muck] > My code is below. What should I be doing differently to be, in the most > immediate sense, calls to '''%(foo)s''' % locals()? [snipped about 200 lines of code, presented in quoted-printable] You will greatly increase your odds of getting a useful response if you post the shortest possible program that exhibits your problem. Posting it as plain text would help, too. -- To email me, substitute nowhere->spamcop, invalid->net. From carl at personnelware.com Mon May 13 13:01:17 2013 From: carl at personnelware.com (Carl Karsten) Date: Mon, 13 May 2013 12:01:17 -0500 Subject: [Chicago] Getting ASCII encoding where unicode wanted under Py3k In-Reply-To: References: Message-ID: On Mon, May 13, 2013 at 10:59 AM, Jonathan Hayward wrote: That is way too much code for me to try and dig into. Remove everything not needed to demo it. Replace big strings with little strings. My guess is it should be 1-3 lines, like >>> print('123%(a)s' % {'a': u'\u0161' } ) 123? But that works. may need a few other lines, or something. It is also possible that there is a setting in your OS that has an effect. What OS? -- Carl K From fabiosantosart at gmail.com Mon May 13 17:44:04 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 13 May 2013 22:44:04 +0100 Subject: Writing a blog post on the new Enum. Message-ID: I have followed the process of the new PEP closely, and as such I know that there is a repository containing the reference implementation, a link to which was posted on the python-dev list. Is it okay to link to this repository in my new blog post about the Enum, so my readers can try it out? I will warn my readers about it not being final and not to use in production code. I am unsure whether the repository is not supposed to be shared with too many people. -- F?bio Santos From breamoreboy at yahoo.co.uk Mon May 13 18:56:55 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 13 May 2013 23:56:55 +0100 Subject: Writing a blog post on the new Enum. In-Reply-To: References: Message-ID: On 13/05/2013 22:44, F?bio Santos wrote: > I have followed the process of the new PEP closely, and as such I know > that there is a repository containing the reference implementation, a > link to which was posted on the python-dev list. Is it okay to link to > this repository in my new blog post about the Enum, so my readers can > try it out? I will warn my readers about it not being final and not to > use in production code. > > I am unsure whether the repository is not supposed to be shared with > too many people. > > -- > F?bio Santos > My understanding is that nobody can mention this on pain of being subject to the Comfy Chair. I hope this clarifies everything. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From ethan at stoneleaf.us Mon May 13 19:09:55 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 13 May 2013 16:09:55 -0700 Subject: Writing a blog post on the new Enum. In-Reply-To: References: Message-ID: <519172C3.4030108@stoneleaf.us> On 05/13/2013 02:44 PM, F?bio Santos wrote: > I have followed the process of the new PEP closely, and as such I know > that there is a repository containing the reference implementation, a > link to which was posted on the python-dev list. Is it okay to link to > this repository in my new blog post about the Enum, so my readers can > try it out? I will warn my readers about it not being final and not to > use in production code. > > I am unsure whether the repository is not supposed to be shared with > too many people. I just checked my settings there, and it looks like you're okay since the folks reading your blog would just be downloading, not logging and directly manipulating the repository. -- ~Ethan~ From fabiosantosart at gmail.com Mon May 13 19:40:38 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 14 May 2013 00:40:38 +0100 Subject: Writing a blog post on the new Enum. In-Reply-To: <519172C3.4030108@stoneleaf.us> References: <519172C3.4030108@stoneleaf.us> Message-ID: On Tue, May 14, 2013 at 12:09 AM, Ethan Furman wrote: > I just checked my settings there, and it looks like you're okay since the > folks reading your blog would just be downloading, not logging and directly > manipulating the repository. > > -- > ~Ethan~ Okay, so I'm good. I'll go ahead and publish it. On Mon, May 13, 2013 at 11:56 PM, Mark Lawrence wrote: > My understanding is that nobody can mention this on pain of being subject to > the Comfy Chair. I hope this clarifies everything. Well I am thus defying the law and order of this world by publishing it on the internets! --- And here it is: http://fabiosantoscode.blogspot.pt/2013/05/pythons-new-enum-class.html -- F?bio Santos From rosuav at gmail.com Tue May 14 03:52:43 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 14 May 2013 17:52:43 +1000 Subject: Writing a blog post on the new Enum. In-Reply-To: References: <519172C3.4030108@stoneleaf.us> Message-ID: On Tue, May 14, 2013 at 9:40 AM, F?bio Santos wrote: > Well I am thus defying the law and order of this world by publishing > it on the internets! > > --- > > And here it is: > http://fabiosantoscode.blogspot.pt/2013/05/pythons-new-enum-class.html class Text(unicode, Enum): one = u'one' two = u'two' three = u'three' That looks like Python 2 code. Are you backporting Enum to Py2 manually? AIUI the Python core won't be adding features like that to 2.7, and there won't be a 2.8, so PEP 435 will be Py3.4+ only. Or have I misunderstood it? ChrisA From tjreedy at udel.edu Tue May 14 11:07:58 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Tue, 14 May 2013 11:07:58 -0400 Subject: Writing a blog post on the new Enum. In-Reply-To: References: <519172C3.4030108@stoneleaf.us> Message-ID: On 5/14/2013 3:52 AM, Chris Angelico wrote: > On Tue, May 14, 2013 at 9:40 AM, F?bio Santos wrote: >> http://fabiosantoscode.blogspot.pt/2013/05/pythons-new-enum-class.html > > class Text(unicode, Enum): > one = u'one' > two = u'two' > three = u'three' Is this supposed to be a quote? or your rewrite? or did Santos rewrite after you posted? The blog currently has a 3.x version of that. > That looks like Python 2 code. Are you backporting Enum to Py2 > manually? AIUI the Python core won't be adding features like that to > 2.7, and there won't be a 2.8, so PEP 435 will be Py3.4+ only. Or have > I misunderstood it? As far as official CPython goes, Enum is 3.4+ only. I believe the module will continue to work on earlier 3.x and will remain externally available as a 3rd party module. Ethan said he plans to backport to 2.7 so 2 & 3 code can use enums. From rosuav at gmail.com Tue May 14 11:36:32 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 15 May 2013 01:36:32 +1000 Subject: Writing a blog post on the new Enum. In-Reply-To: References: <519172C3.4030108@stoneleaf.us> Message-ID: On Wed, May 15, 2013 at 1:07 AM, Terry Jan Reedy wrote: > On 5/14/2013 3:52 AM, Chris Angelico wrote: >> >> On Tue, May 14, 2013 at 9:40 AM, F?bio Santos >> wrote: > >>> http://fabiosantoscode.blogspot.pt/2013/05/pythons-new-enum-class.html >> >> >> class Text(unicode, Enum): >> one = u'one' >> two = u'two' >> three = u'three' > > > Is this supposed to be a quote? or your rewrite? or did Santos rewrite after > you posted? The blog currently has a 3.x version of that. It was a quote from the blog post as it had been when I posted it. Presumably he edited the post subsequently, as I'm now seeing the same thing you are, a 3.x-compat version. (The u'xx' prefixes wouldn't be a problem, incidentally, so this could be dual-versionned just by going "unicode = str".) ChrisA From fabiosantosart at gmail.com Tue May 14 13:09:08 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 14 May 2013 18:09:08 +0100 Subject: Writing a blog post on the new Enum. In-Reply-To: References: <519172C3.4030108@stoneleaf.us> Message-ID: I corrected it indeed. On Tue, May 14, 2013 at 4:36 PM, Chris Angelico wrote: > On Wed, May 15, 2013 at 1:07 AM, Terry Jan Reedy wrote: >> On 5/14/2013 3:52 AM, Chris Angelico wrote: >>> >>> On Tue, May 14, 2013 at 9:40 AM, F?bio Santos >>> wrote: >> >>>> http://fabiosantoscode.blogspot.pt/2013/05/pythons-new-enum-class.html >>> >>> >>> class Text(unicode, Enum): >>> one = u'one' >>> two = u'two' >>> three = u'three' >> >> >> Is this supposed to be a quote? or your rewrite? or did Santos rewrite after >> you posted? The blog currently has a 3.x version of that. > > It was a quote from the blog post as it had been when I posted it. > Presumably he edited the post subsequently, as I'm now seeing the same > thing you are, a 3.x-compat version. (The u'xx' prefixes wouldn't be a > problem, incidentally, so this could be dual-versionned just by going > "unicode = str".) > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list -- F?bio Santos From alok.jadhav at credit-suisse.com Tue May 14 02:54:25 2013 From: alok.jadhav at credit-suisse.com (Jadhav, Alok) Date: Tue, 14 May 2013 14:54:25 +0800 Subject: weave and 64 bit issues Message-ID: Hi everyone, I am facing a strange problem using weave on 64 bit machine. Specifically with weave's inline function. It has something to do with weave's catalog. Similar issues I found in the past (very old) http://mail.scipy.org/pipermail/scipy-dev/2006-June/005908.html http://mail.scipy.org/pipermail/scipy-dev/2005-June/003042.html I have a simple script to calculate moving average using weave's inline function. File mvg.py import numpy as np import scipy.weave as weave import distutils.sysconfig import distutils.dir_util import os distutils.sysconfig._config_vars["LDSHARED"]="-LC:\strawberry64\c\x86_64 -w64-mingw32\lib" def ExpMovAvg(data,time,lag): if (data.size!=time.size): print "error in EMA, data and time have different size" return None result=np.repeat(0.0,data.size) code=""" #line 66 "basics.py" result(0)=data(0); for (int i=0;i1) { alpha=10; } result(i+1)=(1-alpha)*data(i)+alpha*result(i); } """ weave.inline(code,["data","time","lag","result"],type_converters=weave.c onverters.blitz,headers=[""],compiler="gcc",verbose=2) return result file test.py import string import numpy as np import mvg print(mvg.ExpMovAvg(np.array(range(10)),np.array(range(10)),2)) Output: Working output: Y:\STMM\alpha\klse\PROD>c:\python27\python.exe s:\common\tools\python\python-2.7-64bit\test.py [ 0. 0. 0.63212774 1.49679774 2.44701359 3.42869938 4.42196209 5.41948363 6.41857187 7.41823646] Now if I keep running the script multiple times, sometimes I see correct output... but suddenly sometimes I get below error. Y:\STMM\alpha\klse\PROD>c:\python27\python.exe s:\common\tools\python\python-2.7-64bit\test.py repairing catalog by removing key Looking for python27.dll running build_ext running build_src build_src building extension "sc_44f3fe3c65d5c3feecb45d9269ac207f5" sources build_src: building npy-pkg config files Looking for python27.dll customize Mingw32CCompiler customize Mingw32CCompiler using build_ext Looking for python27.dll customize Mingw32CCompiler customize Mingw32CCompiler using build_ext building 'sc_44f3fe3c65d5c3feecb45d9269ac207f5' extension compiling C++ sources C compiler: g++ -g -DDEBUG -DMS_WIN64 -O0 -Wall compile options: '-Ic:\python27\lib\site-packages\scipy\weave -Ic:\python27\lib\site-packages\scipy\weave\scxx -Ic:\python27\lib\site-packages\scipy\weave\blitz -Ic:\python27\lib\site-packages\numpy\core\include -Ic:\python27\include -Ic:\python27\PC -c' g++ -g -DDEBUG -DMS_WIN64 -O0 -Wall -Ic:\python27\lib\site-packages\scipy\weave -Ic:\python27\lib\site-packages\scipy\weave\scxx -Ic:\python27\lib\site-packages\scipy\weave\blitz -Ic:\python27\lib\site-packages\numpy\core\include -Ic:\pytho n27\include -Ic:\python27\PC -c c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_compiled\sc_44f3f e3c65d5c3feecb45d9269ac207f5.cpp -o c:\users\ajadhav2\ap pdata\local\temp\ajadhav2\python27_intermediate\compiler_2d3e1e2e4de6a91 419d2376b162e5342\Release\users\ajadhav2\appdata\local\temp\ajadhav2\pyt hon27_compiled\s c_44f3fe3c65d5c3feecb45d9269ac207f5.o Found executable C:\strawberry\c\bin\g++.exe g++ -g -DDEBUG -DMS_WIN64 -O0 -Wall -Ic:\python27\lib\site-packages\scipy\weave-Ic:\python27\lib\site-packag es\scipy\weave\scxx -Ic:\python27\lib\site-packages \scipy\weave\blitz -Ic:\python27\lib\site-packages\numpy\core\include -Ic:\python27\include -Ic:\python27\PC -c c:\python27\lib\site-packages\scipy\weave\scxx\w eave_imp.cpp -o c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_intermediate\comp iler_2d3e1e2e4de6a91419d2376b162e5342\Release\python27\lib\site-packages \scipy\weave\scxx\weave_imp.o g++ -g -shared c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_intermediate\comp iler_2d3e1e2e4de6a91419d2376b162e5342\Release\users\ajadhav2\appdata\loc a l\temp\ajadhav2\python27_compiled\sc_44f3fe3c65d5c3feecb45d9269ac207f5.o c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_intermediate\comp iler_2d3e1e2e4d e6a91419d2376b162e5342\Release\python27\lib\site-packages\scipy\weave\sc xx\weave_imp.o -Lc:\python27\libs -Lc:\python27\PCbuild\amd64 -lpython27 -lmsvcr90 -o c: \users\ajadhav2\appdata\local\temp\ajadhav2\python27_compiled\sc_44f3fe3 c65d5c3feecb45d9269ac207f5.pyd running scons Traceback (most recent call last): File "s:\common\tools\python\python-2.7-64bit\test.py", line 5, in print(mvg.ExpMovAvg(np.array(range(10)),np.array(range(10)),2)) File "s:\common\tools\python\python-2.7-64bit\mvg.py", line 30, in ExpMovAvg weave.inline(code,["data","time","lag","result"],type_converters=weave.c onve rters.blitz,headers=[""],compiler="gcc",verbose=2) File "c:\python27\lib\site-packages\scipy\weave\inline_tools.py", line 355, in inline **kw) File "c:\python27\lib\site-packages\scipy\weave\inline_tools.py", line 488, in compile_function exec 'import ' + module_name File "", line 1, in ImportError: DLL load failed: Invalid access to memory location. Y:\STMM\alpha\klse\PROD> For everytime there is an error I have one observation, the message has notification "repairing catalog by removing key" which leads to DLL load failed error. Any ideas? - Alok =============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html =============================================================================== -------------- next part -------------- An HTML attachment was scrubbed... URL: From hossamalagmy at gmail.com Tue May 14 08:34:03 2013 From: hossamalagmy at gmail.com (23alagmy) Date: Tue, 14 May 2013 05:34:03 -0700 (PDT) Subject: ssl proxy server Message-ID: <0eb46c91-e290-458d-8be7-ded950bc1c6d@12g2000vba.googlegroups.com> ssl proxy server http://natigtas7ab.blogspot.com/2013/05/ssl-proxy-server.html From kwpolska at gmail.com Tue May 14 14:40:12 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Tue, 14 May 2013 20:40:12 +0200 Subject: ssl proxy server In-Reply-To: <0eb46c91-e290-458d-8be7-ded950bc1c6d@12g2000vba.googlegroups.com> References: <0eb46c91-e290-458d-8be7-ded950bc1c6d@12g2000vba.googlegroups.com> Message-ID: On Tue, May 14, 2013 at 2:34 PM, 23alagmy wrote: > ssl proxy server > > hxxp://natigtas7ab.blogspot.com/2013/05/ssl-proxy-server.html > -- > http://mail.python.org/mailman/listinfo/python-list I have been seeing those mails for a long time. Why didn?t anybody ban that guy? If it comes from Usenet (and headers say it does), and you can?t destroy stuff easily there, maybe just put a ban on the Mailman side of things, making the world much better for at least some people? From skip at pobox.com Tue May 14 15:14:55 2013 From: skip at pobox.com (Skip Montanaro) Date: Tue, 14 May 2013 14:14:55 -0500 Subject: ssl proxy server In-Reply-To: References: <0eb46c91-e290-458d-8be7-ded950bc1c6d@12g2000vba.googlegroups.com> Message-ID: I haven't touched the SpamBayes setup for the usenet-to-mail gateway in a long while. For whatever reason, this message was either held and then approved by the current list moderator(s), or (more likely) slipped through unscathed. No filter is perfect. Skip On Tue, May 14, 2013 at 1:40 PM, Chris ?Kwpolska? Warrick wrote: > On Tue, May 14, 2013 at 2:34 PM, 23alagmy wrote: >> ssl proxy server >> >> hxxp://natigtas7ab.blogspot.com/2013/05/ssl-proxy-server.html >> -- >> http://mail.python.org/mailman/listinfo/python-list > > I have been seeing those mails for a long time. Why didn?t anybody > ban that guy? If it comes from Usenet (and headers say it does), and > you can?t destroy stuff easily there, maybe just put a ban on the > Mailman side of things, making the world much better for at least some > people? > -- > http://mail.python.org/mailman/listinfo/python-list From kwpolska at gmail.com Wed May 15 14:58:15 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Wed, 15 May 2013 20:58:15 +0200 Subject: ssl proxy server In-Reply-To: References: <0eb46c91-e290-458d-8be7-ded950bc1c6d@12g2000vba.googlegroups.com> Message-ID: On Tue, May 14, 2013 at 9:14 PM, Skip Montanaro wrote: > I haven't touched the SpamBayes setup for the usenet-to-mail gateway > in a long while. For whatever reason, this message was either held > and then approved by the current list moderator(s), or (more likely) > slipped through unscathed. No filter is perfect. > > Skip A filter on this guy altogether would be. He sent 24 messages there since February 25. All of them spam. -- Kwpolska | GPG KEY: 5EAAEA16 stop html mail | always bottom-post http://asciiribbon.org | http://caliburn.nl/topposting.html From zachary.ware+pylist at gmail.com Wed May 15 15:10:38 2013 From: zachary.ware+pylist at gmail.com (Zachary Ware) Date: Wed, 15 May 2013 14:10:38 -0500 Subject: ssl proxy server In-Reply-To: References: <0eb46c91-e290-458d-8be7-ded950bc1c6d@12g2000vba.googlegroups.com> Message-ID: On Wed, May 15, 2013 at 1:58 PM, Chris ?Kwpolska? Warrick wrote: > On Tue, May 14, 2013 at 9:14 PM, Skip Montanaro wrote: >> I haven't touched the SpamBayes setup for the usenet-to-mail gateway >> in a long while. For whatever reason, this message was either held >> and then approved by the current list moderator(s), or (more likely) >> slipped through unscathed. No filter is perfect. >> >> Skip > > A filter on this guy altogether would be. He sent 24 messages there > since February 25. All of them spam. > You can always set your own filter. I had been somewhat confused by this thread until I realized the initial message had been killed by a filter I set up a while back. From commx at commx.ws Tue May 14 11:05:53 2013 From: commx at commx.ws (Christian Jurk) Date: Tue, 14 May 2013 08:05:53 -0700 (PDT) Subject: PDF generator decision Message-ID: <3f4500ad-784a-401d-a089-e2b7081821e9@googlegroups.com> Hi folks, This questions may be asked several times already, but the development of relevant software continues day-for-day. For some time now I've been using xhtml2pdf [1] to generate PDF documents from HTML templates (which are rendered through my Django-based web application. This have been working for some time now but I'm constantly adding new templates and they are not looking like I want it (sometimes bold text is bold, sometimes not, layout issues, etc). I'd like to use something else than xhtml2pdf. So far I'd like to ask which is the (probably) best way to create PDFs in Python (3)? It is important for me that I am able to specify not only background graphics, paragaphs, tables and so on but also to specify page headers/footers. The reason is that I have a bunch of documents to be generated (including Invoice templates, Quotes - stuff like that). Any advice is welcome. Thanks. [1] https://github.com/chrisglass/xhtml2pdf From fpm at u.washington.edu Tue May 14 11:21:36 2013 From: fpm at u.washington.edu (Frank Miles) Date: Tue, 14 May 2013 15:21:36 +0000 (UTC) Subject: PDF generator decision References: <3f4500ad-784a-401d-a089-e2b7081821e9@googlegroups.com> Message-ID: On Tue, 14 May 2013 08:05:53 -0700, Christian Jurk wrote: > Hi folks, > > This questions may be asked several times already, but the development > of relevant software continues day-for-day. For some time now I've been > using xhtml2pdf [1] to generate PDF documents from HTML templates (which > are rendered through my Django-based web application. This have been > working for some time now but I'm constantly adding new templates and > they are not looking like I want it (sometimes bold text is bold, > sometimes not, layout issues, etc). I'd like to use something else than > xhtml2pdf. > > So far I'd like to ask which is the (probably) best way to create PDFs > in Python (3)? It is important for me that I am able to specify not only > background graphics, paragaphs, tables and so on but also to specify > page headers/footers. The reason is that I have a bunch of documents to > be generated (including Invoice templates, Quotes - stuff like that). > > Any advice is welcome. Thanks. > > [1] https://github.com/chrisglass/xhtml2pdf Reportlab works well in Python 2.x. Their _next_ version is supposed to work with Python3... {yes, not much help there} From wxjmfauth at gmail.com Tue May 14 13:36:52 2013 From: wxjmfauth at gmail.com (jmfauth) Date: Tue, 14 May 2013 10:36:52 -0700 (PDT) Subject: PDF generator decision References: <3f4500ad-784a-401d-a089-e2b7081821e9@googlegroups.com> Message-ID: <39026458-6d08-49ec-8e35-802e17dae7dd@o10g2000vbp.googlegroups.com> On 14 mai, 17:05, Christian Jurk wrote: > Hi folks, > > This questions may be asked several times already, but the development of relevant software continues day-for-day. For some time now I've been using xhtml2pdf [1] to generate PDF documents from HTML templates (which are rendered through my Django-based web application. This have been working for some time now but I'm constantly adding new templates and they are not looking like I want it (sometimes bold text is bold, sometimes not, layout issues, etc). I'd like to use something else than xhtml2pdf. > > So far I'd like to ask which is the (probably) best way to create PDFs in Python (3)? It is important for me that I am able to specify not only background graphics, paragaphs, tables and so on but also to specify page headers/footers. The reason is that I have a bunch of documents to be generated (including Invoice templates, Quotes - stuff like that). > > Any advice is welcome. Thanks. > > [1]https://github.com/chrisglass/xhtml2pdf ----- 1) Use Python to collect your data (db, pictures, texts, ...) and/or to create the material (text, graphics, ...) that will be the contents (source) of your your pdf's. 2) Put this source in .tex file (a plain text file). 3) Let it compile with a TeX engine. - I can not figure out something more versatile and basically simple (writing a text file). - Do not forget you are the only one who knows the content and the layout of your document(s). jmf From dieter at handshake.de Wed May 15 02:22:25 2013 From: dieter at handshake.de (dieter) Date: Wed, 15 May 2013 08:22:25 +0200 Subject: PDF generator decision References: <3f4500ad-784a-401d-a089-e2b7081821e9@googlegroups.com> Message-ID: <877gj0vk2m.fsf@handshake.de> Christian Jurk writes: > ... > So far I'd like to ask which is the (probably) best way to create PDFs in Python (3)? It is important for me that I am able to specify not only background graphics, paragaphs, tables and so on but also to specify page headers/footers. The reason is that I have a bunch of documents to be generated (including Invoice templates, Quotes - stuff like that). High quality layouts, especially those containing (potentially complex tables), are very difficult to generate automatically. I do not suppose that you will find a generic solution - one applicable to a wide range of document classes and garanteeing high quality layout for almost all of its document instances. From carlosnepomuceno at outlook.com Wed May 15 02:48:33 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 15 May 2013 09:48:33 +0300 Subject: PDF generator decision In-Reply-To: <877gj0vk2m.fsf@handshake.de> References: <3f4500ad-784a-401d-a089-e2b7081821e9@googlegroups.com>, <877gj0vk2m.fsf@handshake.de> Message-ID: Christian, have you tried pod[1]? You can use create templates in OpenDocument format and then create the PDFs just passing the arguments, like: args = {'name':'John', 'email':'john at example.com'} renderer = Renderer('template.odt', args, 'result.odt')renderer.run() [1] http://appyframework.org/pod.html ---------------------------------------- > To: python-list at python.org > From: dieter at handshake.de > Subject: Re: PDF generator decision > Date: Wed, 15 May 2013 08:22:25 +0200 > > Christian Jurk writes: > >> ... >> So far I'd like to ask which is the (probably) best way to create PDFs in Python (3)? It is important for me that I am able to specify not only background graphics, paragaphs, tables and so on but also to specify page headers/footers. The reason is that I have a bunch of documents to be generated (including Invoice templates, Quotes - stuff like that). > > High quality layouts, especially those containing (potentially complex > tables), are very difficult to generate automatically. > > I do not suppose that you will find a generic solution - one applicable > to a wide range of document classes and garanteeing high quality > layout for almost all of its document instances. > > -- > http://mail.python.org/mailman/listinfo/python-list From carlosnepomuceno at outlook.com Tue May 14 11:46:30 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 14 May 2013 18:46:30 +0300 Subject: First post In-Reply-To: References: Message-ID: Hi guys! This is my first post on this list. I'd like have your opinion on how to safely implement WSGI on a production server. My benchmarks show no performance differences between our PHP and Python environments. I'm using mod_wsgi v3.4 with Apache 2.4. Is that ok or can it get faster? Thanks in advance. Regards, Carlos From krishna2prasad at gmail.com Tue May 14 15:00:22 2013 From: krishna2prasad at gmail.com (krishna2prasad at gmail.com) Date: Tue, 14 May 2013 12:00:22 -0700 (PDT) Subject: Illegal seek error with seek() and os.lseek() Message-ID: <50bf9366-46e0-4a7f-865b-3f7c7b0f607d@googlegroups.com> I am trying to use os.open() and os.lseek() methods to operate on a device file in Linux. My code goes something like this - # first, open the file as a plain binary try: self.file = open(/dev/relpcfpga, "r+b", buffering=0) except IOError: raise IOError ('Failed to open.') # Figure out file size self.file.seek(0, 2) self.file_size = self.file.tell() The method seek() complains "OSError: [Errno 29] Illegal seek" The device relpcfpga is a char device. The same code works with a normal text file. I have tried to use os.open() and os.lseek() methods, but see the same error. Is there a different method to operate on device files? Thanks! From marduk at python.net Tue May 14 16:00:44 2013 From: marduk at python.net (marduk at python.net) Date: Tue, 14 May 2013 16:00:44 -0400 Subject: Illegal seek error with seek() and os.lseek() In-Reply-To: <50bf9366-46e0-4a7f-865b-3f7c7b0f607d@googlegroups.com> References: <50bf9366-46e0-4a7f-865b-3f7c7b0f607d@googlegroups.com> Message-ID: <1368561644.772.140661231053781.62A9F526@webmail.messagingengine.com> On Tue, May 14, 2013, at 03:00 PM, krishna2prasad at gmail.com wrote: > I am trying to use os.open() and os.lseek() methods to operate on a > device file in Linux. My code goes something like this - > > # first, open the file as a plain binary > try: > self.file = open(/dev/relpcfpga, "r+b", buffering=0) > > except IOError: > raise IOError ('Failed to open.') > > # Figure out file size > self.file.seek(0, 2) > self.file_size = self.file.tell() > > > The method seek() complains "OSError: [Errno 29] Illegal seek" > The device relpcfpga is a char device. > > The same code works with a normal text file. > I have tried to use os.open() and os.lseek() methods, but see the same > error. > Is there a different method to operate on device files? Some file streams are not seekable. Specifically, some (all?) char devices aren't seekable (because e.g. they can't be rewound or they have no end). You'd get the same error in C (well it would return -1). See also: http://www.linuxintro.org/wiki/Device From roy at panix.com Tue May 14 16:02:16 2013 From: roy at panix.com (Roy Smith) Date: Tue, 14 May 2013 16:02:16 -0400 Subject: Illegal seek error with seek() and os.lseek() References: <50bf9366-46e0-4a7f-865b-3f7c7b0f607d@googlegroups.com> Message-ID: In article <50bf9366-46e0-4a7f-865b-3f7c7b0f607d at googlegroups.com>, krishna2prasad at gmail.com wrote: > I am trying to use os.open() and os.lseek() methods to operate on a device > file in Linux. My code goes something like this - > > # first, open the file as a plain binary > try: > self.file = open(/dev/relpcfpga, "r+b", buffering=0) > > except IOError: > raise IOError ('Failed to open.') > > # Figure out file size > self.file.seek(0, 2) > self.file_size = self.file.tell() > > > The method seek() complains "OSError: [Errno 29] Illegal seek" > The device relpcfpga is a char device. > > The same code works with a normal text file. > I have tried to use os.open() and os.lseek() methods, but see the same error. > Is there a different method to operate on device files? In general, seek() works on special files, when it makes sense. But, the "in general" part is critical. Not all devices support the seek operation. I have no idea what /dev/relpcfpga is (a google search for relpcfpga came up with exactly one hit -- your post!) so I can't tell you if it supports seek() or not. From andipersti at gmail.com Tue May 14 16:12:23 2013 From: andipersti at gmail.com (Andreas Perstinger) Date: Tue, 14 May 2013 22:12:23 +0200 Subject: Illegal seek error with seek() and os.lseek() In-Reply-To: <50bf9366-46e0-4a7f-865b-3f7c7b0f607d@googlegroups.com> References: <50bf9366-46e0-4a7f-865b-3f7c7b0f607d@googlegroups.com> Message-ID: <51929AA7.4020802@gmail.com> On 14.05.2013 21:00, krishna2prasad at gmail.com wrote: > # first, open the file as a plain binary > try: > self.file = open(/dev/relpcfpga, "r+b", buffering=0) Aren't you missing the quotes for "/dev/relpcfpga"? > The method seek() complains "OSError: [Errno 29] Illegal seek" > The device relpcfpga is a char device. Are you sure that your device is seekable? Try f = open("/dev/relpcfpga", "r+b", buffering=0) print(f.seekable()) Bye, Andreas From alok.jadhav at credit-suisse.com Tue May 14 20:44:10 2013 From: alok.jadhav at credit-suisse.com (Jadhav, Alok) Date: Wed, 15 May 2013 08:44:10 +0800 Subject: weave in 64 bit strange behavior Message-ID: Hi everyone, I realize my previous post was quite unreadable, thanks to my email client. I am going to report my question here, with slight enhancements. Apologies for inconvenience caused and spamming your mailboxes. I am facing a strange problem using weave on 64 bit machine. Specifically with weave's inline function. It has something to do with weave's catalog. Similar issues I found in the past (very old) http://mail.scipy.org/pipermail/scipy-dev/2006-June/005908.html http://mail.scipy.org/pipermail/scipy-dev/2005-June/003042.html Common things I have in my observation are: - Already working setup in 32 bit doesn't work in same manner in 64 bit env - Weave recompiles inline code which does not require any recompilation. This is random behavior. Whenever weave recompiles I see a notification "repairing catalog by removing key" in the output which ends up in the error message "ImportError: DLL load failed: Invalid access to memory location" - Sometimes gcc gets into an infinite loop printing error message "Looking for python27.dll". Even though the dll is on the path. This process doesn't end. Had to kill it forcefully. G++ process became ghost even after killing python process. Could someone advise what am I missing here. Is there any specific setup that I need to do? Is there an issue with python 27 64 bit weave implementation? Regards, Alok I have a simple script to calculate moving average using weave's inline function. source mvg.py import numpy as np import scipy.weave as weave import distutils.sysconfig import distutils.dir_util import os distutils.sysconfig._config_vars["LDSHARED"]="-LC:\strawberry\c\x86_64-w 64-mingw32\lib" def ExpMovAvg(data,time,lag): if (data.size!=time.size): print "error in EMA, data and time have different size" return None result=np.repeat(0.0,data.size) code=""" #line 66 "basics.py" result(0)=data(0); for (int i=0;i1) { alpha=10; } result(i+1)=(1-alpha)*data(i)+alpha*result(i); } """ weave.inline(code,["data","time","lag","result"],type_converters=weave.c onverters.blitz,headers=[""],compiler="gcc",verbose=2) return result source test.py import string import numpy as np import mvg print(mvg.ExpMovAvg(np.array(range(10)),np.array(range(10)),2)) Output: Working output: Y:\STMM\alpha\klse\PROD>c:\python27\python.exe s:\common\tools\python\python-2.7-64bit\test.py [ 0. 0. 0.63212774 1.49679774 2.44701359 3.42869938 4.42196209 5.41948363 6.41857187 7.41823646] Now if I keep running the script multiple times, sometimes I see correct output... but suddenly sometimes I get below error. Y:\STMM\alpha\klse\PROD>c:\python27\python.exe s:\common\tools\python\python-2.7-64bit\test.py repairing catalog by removing key Looking for python27.dll running build_ext running build_src build_src building extension "sc_44f3fe3c65d5c3feecb45d9269ac207f5" sources build_src: building npy-pkg config files Looking for python27.dll customize Mingw32CCompiler customize Mingw32CCompiler using build_ext Looking for python27.dll customize Mingw32CCompiler customize Mingw32CCompiler using build_ext building 'sc_44f3fe3c65d5c3feecb45d9269ac207f5' extension compiling C++ sources C compiler: g++ -g -DDEBUG -DMS_WIN64 -O0 -Wall compile options: '-Ic:\python27\lib\site-packages\scipy\weave -Ic:\python27\lib\site-packages\scipy\weave\scxx -Ic:\python27\lib\site-packages\scipy\weave\blitz -Ic:\python27\lib\site-packages\numpy\core\include -Ic:\python27\include -Ic:\python27\PC -c' g++ -g -DDEBUG -DMS_WIN64 -O0 -Wall -Ic:\python27\lib\site-packages\scipy\weave -Ic:\python27\lib\site-packages\scipy\weave\scxx -Ic:\python27\lib\site-packages\scipy\weave\blitz -Ic:\python27\lib\site-packages\numpy\core\include -Ic:\python27\include -Ic:\python27\PC -c c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_compiled\sc_44f3f e3c65d5c3feecb45d9269ac207f5.cpp -o c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_intermediate\comp iler_2d3e1e2e4de6a91419d2376b162e5342\Release\users\ajadhav2\appdata\loc al\temp\ajadhav2\python27_compiled\sc_44f3fe3c65d5c3feecb45d9269ac207f5. o Found executable C:\strawberry\c\bin\g++.exe g++ -g -DDEBUG -DMS_WIN64 -O0 -Wall-Ic:\python27\lib\site-packages\scipy\weave-Ic:\python27\lib\site-p ackages\scipy\weave\scxx -Ic:\python27\lib\site-packages\scipy\weave\blitz -Ic:\python27\lib\site-packages\numpy\core\include-Ic:\python27\include -Ic:\python27\PC -c c:\python27\lib\site-packages\scipy\weave\scxx\weave_imp.cpp -o c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_intermediate\comp iler_2d3e1e2e4de6a91419d2376b162e5342\Release\python27\lib\site-packages \scipy\weave\scxx\weave_imp.o g++ -g -shared c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_intermediate\comp iler_2d3e1e2e4de6a91419d2376b162e5342\Release\users\ajadhav2\appdata\loc al\temp\ajadhav2\python27_compiled\sc_44f3fe3c65d5c3feecb45d9269ac207f5. o c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_intermediate\comp iler_2d3e1e2e4de6a91419d2376b162e5342\Release\python27\lib\site-packages \scipy\weave\scxx\weave_imp.o -Lc:\python27\libs -Lc:\python27\PCbuild\amd64 -lpython27 -lmsvcr90 -o c:\users\ajadhav2\appdata\local\temp\ajadhav2\python27_compiled\sc_44f3f e3c65d5c3feecb45d9269ac207f5.pyd running scons Traceback (most recent call last): File "s:\common\tools\python\python-2.7-64bit\test.py", line 5, in print(mvg.ExpMovAvg(np.array(range(10)),np.array(range(10)),2)) File "s:\common\tools\python\python-2.7-64bit\mvg.py", line 30, in ExpMovAvgweave.inline(code,["data","time","lag","result"], type_converters=weave.converters.blitz,headers=[""],compiler="gc c",verbose=2) File "c:\python27\lib\site-packages\scipy\weave\inline_tools.py", line355, ininline **kw) File "c:\python27\lib\site-packages\scipy\weave\inline_tools.py", line 488, in compile_function exec 'import ' + module_name File "", line 1, in ImportError: DLL load failed: Invalid access to memory location. Y:\STMM\alpha\klse\PROD> =============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html =============================================================================== -------------- next part -------------- An HTML attachment was scrubbed... URL: From formisc at gmail.com Wed May 15 00:22:05 2013 From: formisc at gmail.com (Andrew Z) Date: Wed, 15 May 2013 00:22:05 -0400 Subject: IndexError: pop index out of range Message-ID: hello, going fru some basic examples and can't figureout why the following errors out. Help is very much appreciated: def front_x(words): # +++your code here+++ print "words passed : ", words list_xx = [] list_temp = words[:] print "list_temp -", list_temp print "words -", words for idx, val in enumerate(words): print val, idx # str_idx = val.find('x',0,2) if val[0] == 'x': vl = list_temp.pop(idx) list_xx.append(vl) print "appending list_xx", list_xx list_xx.sort list_temp.sort print "words sorted : " + str(words) print "list_temp sorted : ", list_temp list_xx.append(words) print "list_xx" + str(list_xx) return True front_x words passed : ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] list_temp - ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] words - ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] bbb 0 ccc 1 axx 2 xzz 3 appending list_xx ['xzz'] xaa 4 Traceback (most recent call last): File "/home/az/work/Python/Google_Course/google-python-exercises/basic/list1.py", line 119, in main() File "/home/az/work/Python/Google_Course/google-python-exercises/basic/list1.py", line 100, in main test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']), File "/home/az/work/Python/Google_Course/google-python-exercises/basic/list1.py", line 55, in front_x vl = list_temp.pop(idx) IndexError: pop index out of range -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Wed May 15 00:53:26 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 15 May 2013 07:53:26 +0300 Subject: IndexError: pop index out of range In-Reply-To: References: Message-ID: Your "for idx, val in enumerate(words):" is running on words not list_temp. As you remove from list_temp and keeps parsing words you get the IndexError. ________________________________ > From: formisc at gmail.com > Date: Wed, 15 May 2013 00:22:05 -0400 > Subject: IndexError: pop index out of range > To: python-list at python.org > > hello, > going fru some basic examples and can't figureout why the following > errors out. Help is very much appreciated: > > def front_x(words): > # +++your code here+++ > print "words passed : ", words > list_xx = [] > list_temp = words[:] > print "list_temp -", list_temp > print "words -", words > for idx, val in enumerate(words): > print val, idx > # str_idx = val.find('x',0,2) > if val[0] == 'x': > vl = list_temp.pop(idx) > list_xx.append(vl) > > print "appending list_xx", list_xx > > list_xx.sort > list_temp.sort > print "words sorted : " + str(words) > print "list_temp sorted : ", list_temp > list_xx.append(words) > print "list_xx" + str(list_xx) > return True > > front_x > words passed : ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] > list_temp - ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] > words - ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] > bbb 0 > ccc 1 > axx 2 > xzz 3 > appending list_xx ['xzz'] > xaa 4 > Traceback (most recent call last): > File > "/home/az/work/Python/Google_Course/google-python-exercises/basic/list1.py", > line 119, in > main() > File > "/home/az/work/Python/Google_Course/google-python-exercises/basic/list1.py", > line 100, in main > test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']), > File > "/home/az/work/Python/Google_Course/google-python-exercises/basic/list1.py", > line 55, in front_x > vl = list_temp.pop(idx) > IndexError: pop index out of range > > > > -- http://mail.python.org/mailman/listinfo/python-list From henry.leyh at ipp.mpg.de Wed May 15 02:34:27 2013 From: henry.leyh at ipp.mpg.de (Henry Leyh) Date: Wed, 15 May 2013 08:34:27 +0200 Subject: Determine actually given command line arguments Message-ID: Hello, I am writing a program that gets its parameters from a combination of config file (using configparser) and command line arguments (using argparse). Now I would also like the program to be able to _write_ a configparser config file that contains only the parameters actually given on the commandline. Is there a simple way to determine which command line arguments were actually given on the commandline, i.e. does argparse.ArgumentParser() know which of its namespace members were actually hit during parse_args(). I have tried giving the arguments default values and then looking for those having a non-default value but this is really awkward, especially if it comes to non-string arguments. Also, parsing sys.argv looks clumsy because you have to keep track of short and long options with and without argument etc. i.e. all things that I got argparse for in the first place. Thanks && Greetings, Henry From cjw at ncf.ca Wed May 15 08:03:28 2013 From: cjw at ncf.ca (Colin J. Williams) Date: Wed, 15 May 2013 08:03:28 -0400 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: On 15/05/2013 2:34 AM, Henry Leyh wrote: > Hello, > I am writing a program that gets its parameters from a combination of > config file (using configparser) and command line arguments (using > argparse). Now I would also like the program to be able to _write_ a > configparser config file that contains only the parameters actually > given on the commandline. Is there a simple way to determine which > command line arguments were actually given on the commandline, i.e. does > argparse.ArgumentParser() know which of its namespace members were > actually hit during parse_args(). > > I have tried giving the arguments default values and then looking for > those having a non-default value but this is really awkward, especially > if it comes to non-string arguments. Also, parsing sys.argv looks > clumsy because you have to keep track of short and long options with and > without argument etc. i.e. all things that I got argparse for in the > first place. > > Thanks && Greetings, > Henry Try sys.argv Colin W. From jpiitula at ling.helsinki.fi Wed May 15 08:51:37 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 15 May 2013 15:51:37 +0300 Subject: Determine actually given command line arguments References: Message-ID: Colin J. Williams writes: > On 15/05/2013 2:34 AM, Henry Leyh wrote: > > Hello, > > I am writing a program that gets its parameters from a combination > > of config file (using configparser) and command line arguments > > (using argparse). Now I would also like the program to be able to > > _write_ a configparser config file that contains only the > > parameters actually given on the commandline. Is there a simple > > way to determine which command line arguments were actually given > > on the commandline, i.e. does argparse.ArgumentParser() know which > > of its namespace members were actually hit during parse_args(). > > > > I have tried giving the arguments default values and then looking > > for those having a non-default value but this is really awkward, > > especially if it comes to non-string arguments. Also, parsing > > sys.argv looks clumsy because you have to keep track of short and > > long options with and without argument etc. i.e. all things that I > > got argparse for in the first place. > > > > Thanks && Greetings, > > Henry > Try sys.argv You people should read what you quote, or what you don't quote when you cut the relevant portion. Q. ... parsing sys.argv looks clumsy because ... A. Try sys.argv I mean, huh? From roy at panix.com Wed May 15 08:24:54 2013 From: roy at panix.com (Roy Smith) Date: Wed, 15 May 2013 08:24:54 -0400 Subject: Determine actually given command line arguments References: Message-ID: In article , Henry Leyh wrote: > Is there a simple way to determine which > command line arguments were actually given on the commandline, i.e. does > argparse.ArgumentParser() know which of its namespace members were > actually hit during parse_args(). I think what you're looking for is sys.argv: $ cat argv.py import sys print sys.argv $ python argv.py foo bar ['argv.py', 'foo', 'bar'] From davea at davea.name Wed May 15 08:51:30 2013 From: davea at davea.name (Dave Angel) Date: Wed, 15 May 2013 08:51:30 -0400 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: <519384D2.8070406@davea.name> On 05/15/2013 08:24 AM, Roy Smith wrote: > In article , > Henry Leyh wrote: > >> Is there a simple way to determine which >> command line arguments were actually given on the commandline, i.e. does >> argparse.ArgumentParser() know which of its namespace members were >> actually hit during parse_args(). > > I think what you're looking for is sys.argv: > > $ cat argv.py > import sys > print sys.argv > > $ python argv.py foo bar > ['argv.py', 'foo', 'bar'] > Colin & Roy: The OP mentioned sys.argv in his original query. -- DaveA From henry.leyh at ipp.mpg.de Wed May 15 08:52:59 2013 From: henry.leyh at ipp.mpg.de (Henry Leyh) Date: Wed, 15 May 2013 14:52:59 +0200 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: On 15.05.2013 14:24, Roy Smith wrote: > In article , > Henry Leyh wrote: > >> Is there a simple way to determine which >> command line arguments were actually given on the commandline, i.e. does >> argparse.ArgumentParser() know which of its namespace members were >> actually hit during parse_args(). > > I think what you're looking for is sys.argv: > > $ cat argv.py > import sys > print sys.argv > > $ python argv.py foo bar > ['argv.py', 'foo', 'bar'] Thanks, but as I wrote in my first posting I am aware of sys.argv and was hoping to _avoid_ using it because I'd then have to kind of re-implement a lot of the stuff already there in argparse, e.g. parsing sys.argv for short/long options, flag/parameter options etc. I was thinking of maybe some sort of flag that argparse sets on those optional arguments created with add_argument() that are really given on the command line, i.e. those that it stumbles upon them during parse_args(). Regards, Henry From oscar.j.benjamin at gmail.com Wed May 15 09:00:50 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 15 May 2013 14:00:50 +0100 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: On 15 May 2013 13:52, Henry Leyh wrote: > On 15.05.2013 14:24, Roy Smith wrote: >> >> In article , >> Henry Leyh wrote: >> >>> Is there a simple way to determine which >>> command line arguments were actually given on the commandline, i.e. does >>> argparse.ArgumentParser() know which of its namespace members were >>> actually hit during parse_args(). >> >> >> I think what you're looking for is sys.argv: >> >> $ cat argv.py >> import sys >> print sys.argv >> >> $ python argv.py foo bar >> ['argv.py', 'foo', 'bar'] > > Thanks, but as I wrote in my first posting I am aware of sys.argv and was > hoping to _avoid_ using it because I'd then have to kind of re-implement a > lot of the stuff already there in argparse, e.g. parsing sys.argv for > short/long options, flag/parameter options etc. > > I was thinking of maybe some sort of flag that argparse sets on those > optional arguments created with add_argument() that are really given on the > command line, i.e. those that it stumbles upon them during parse_args(). I don't know about that but I imagine that you could compare values with their defaults to see which have been changed. Oscar From henry.leyh at ipp.mpg.de Wed May 15 09:50:53 2013 From: henry.leyh at ipp.mpg.de (Henry Leyh) Date: Wed, 15 May 2013 15:50:53 +0200 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: On 15.05.2013 15:00, Oscar Benjamin wrote: > On 15 May 2013 13:52, Henry Leyh wrote: >> On 15.05.2013 14:24, Roy Smith wrote: >>> >>> In article , >>> Henry Leyh wrote: >>> >>>> Is there a simple way to determine which >>>> command line arguments were actually given on the commandline, i.e. does >>>> argparse.ArgumentParser() know which of its namespace members were >>>> actually hit during parse_args(). >>> >>> >>> I think what you're looking for is sys.argv: >>> >>> $ cat argv.py >>> import sys >>> print sys.argv >>> >>> $ python argv.py foo bar >>> ['argv.py', 'foo', 'bar'] >> >> Thanks, but as I wrote in my first posting I am aware of sys.argv and was >> hoping to _avoid_ using it because I'd then have to kind of re-implement a >> lot of the stuff already there in argparse, e.g. parsing sys.argv for >> short/long options, flag/parameter options etc. >> >> I was thinking of maybe some sort of flag that argparse sets on those >> optional arguments created with add_argument() that are really given on the >> command line, i.e. those that it stumbles upon them during parse_args(). > > I don't know about that but I imagine that you could compare values > with their defaults to see which have been changed. Yes, I was trying that and it sort of works with strings if I use something sufficiently improbable like "__UNSELECTED__" as default. But it gets difficult with boolean or even number arguments where you just may not have valid "improbable" defaults. You could now say, so what, it's the default anyway. But in my program I would like to distinguish between given and not given arguments rather than between default and non-default. Regards, Henry From skip at pobox.com Wed May 15 10:08:12 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 15 May 2013 09:08:12 -0500 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: > Yes, I was trying that and it sort of works with strings if I use something sufficiently improbable like "__UNSELECTED__" as default. But it gets difficult with boolean or even number arguments where you just may not have valid "improbable" defaults. You could now say, so what, it's the default anyway. But in my program I would like to distinguish between given and not given arguments rather than between default and non-default. Initialize all your arg variables to None, then after command line processing, any which remain as None weren't set on the command line. At that point, set them to the actual defaults. I think that's a pretty common idiom. Note: I am an old cranky dude and still use getopt. This idiom is pretty easy there. YMMV with argparse or optparse. Skip From wayne at waynewerner.com Wed May 15 10:16:35 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Wed, 15 May 2013 09:16:35 -0500 (CDT) Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: On Wed, 15 May 2013, Henry Leyh wrote: > Yes, I was trying that and it sort of works with strings if I use something > sufficiently improbable like "__UNSELECTED__" as default. But it gets > difficult with boolean or even number arguments where you just may not have > valid "improbable" defaults. You could now say, so what, it's the default > anyway. But in my program I would like to distinguish between given and not > given arguments rather than between default and non-default. Have you looked into docopt? It's pretty awesome, and might really help in this case. HTH, -W From henry.leyh at ipp.mpg.de Wed May 15 10:39:59 2013 From: henry.leyh at ipp.mpg.de (Henry Leyh) Date: Wed, 15 May 2013 16:39:59 +0200 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: On 15.05.2013 16:08, Skip Montanaro wrote: >> Yes, I was trying that and it sort of works with strings if I use something sufficiently improbable like "__UNSELECTED__" as default. But it gets difficult with boolean or even number arguments where you just may not have valid "improbable" defaults. You could now say, so what, it's the default anyway. But in my program I would like to distinguish between given and not given arguments rather than between default and non-default. > > Initialize all your arg variables to None, then after command line > processing, any which remain as None weren't set on the command line. > At that point, set them to the actual defaults. I think that's a > pretty common idiom. > > Note: I am an old cranky dude and still use getopt. This idiom is > pretty easy there. YMMV with argparse or optparse. Unfortunately, argparse wants to know the type of the argument and the boolean arguments (those with action=store_true) can't be initialized with None. However, maybe I could convert boolean arguments to something like parser.add_argument('--foo', type=str, nargs='?', const='True', default=None) I'd then have to check for string 'True' rather than for boolean True, though. Regards, Henry From skip at pobox.com Wed May 15 10:51:43 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 15 May 2013 09:51:43 -0500 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: > However, maybe I could ... ... switch to getopt? Skip From roy at panix.com Wed May 15 11:29:18 2013 From: roy at panix.com (Roy Smith) Date: 15 May 2013 11:29:18 -0400 Subject: Determine actually given command line arguments References: Message-ID: In article , Henry Leyh wrote: >On 15.05.2013 14:24, Roy Smith wrote: >> In article , >> Henry Leyh wrote: >> >>> Is there a simple way to determine which >>> command line arguments were actually given on the commandline, i.e. does >>> argparse.ArgumentParser() know which of its namespace members were >>> actually hit during parse_args(). >> >> I think what you're looking for is sys.argv: >> >> $ cat argv.py >> import sys >> print sys.argv >> >> $ python argv.py foo bar >> ['argv.py', 'foo', 'bar'] > >Thanks, but as I wrote in my first posting I am aware of sys.argv and >was hoping to _avoid_ using it because I'd then have to kind of >re-implement a lot of the stuff already there in argparse, e.g. parsing >sys.argv for short/long options, flag/parameter options etc. Sorry, I missed that. I'm not clear on exactly what you're trying to do. You say: > Now I would also like the program to be able to _write_ a > configparser config file that contains only the parameters actually > given on the commandline. I'm guessing what you're trying to do is parse the command line first, then anything that was set there can get overridden by a value in the config file? That seems backwards. Usually, the order is: 1) built-in default 2) config file (possibly a system config file, then a per-user one) 3) environment variable 4) command-line argument It sounds like you're doing it in the reverse order -- allowing the config file to override the command line. From henry.leyh at ipp.mpg.de Thu May 16 01:43:42 2013 From: henry.leyh at ipp.mpg.de (Henry Leyh) Date: Thu, 16 May 2013 07:43:42 +0200 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: On 15.05.2013 17:29, Roy Smith wrote: > In article , > Henry Leyh wrote: >> On 15.05.2013 14:24, Roy Smith wrote: >>> In article , >>> Henry Leyh wrote: >>> >>>> Is there a simple way to determine which >>>> command line arguments were actually given on the commandline, i.e. does >>>> argparse.ArgumentParser() know which of its namespace members were >>>> actually hit during parse_args(). >>> >>> I think what you're looking for is sys.argv: >>> >>> $ cat argv.py >>> import sys >>> print sys.argv >>> >>> $ python argv.py foo bar >>> ['argv.py', 'foo', 'bar'] >> >> Thanks, but as I wrote in my first posting I am aware of sys.argv and >> was hoping to _avoid_ using it because I'd then have to kind of >> re-implement a lot of the stuff already there in argparse, e.g. parsing >> sys.argv for short/long options, flag/parameter options etc. > > Sorry, I missed that. > > I'm not clear on exactly what you're trying to do. You say: > >> Now I would also like the program to be able to _write_ a >> configparser config file that contains only the parameters actually >> given on the commandline. > > I'm guessing what you're trying to do is parse the command line first, > then anything that was set there can get overridden by a value in the > config file? That seems backwards. Usually, the order is: > > 1) built-in default > 2) config file (possibly a system config file, then a per-user one) > 3) environment variable > 4) command-line argument > > It sounds like you're doing it in the reverse order -- allowing the > config file to override the command line. No. The program reads a general config file in $HOME, something like ~/.programrc; then parses the command like for '-c FILE' and, if FILE is present reads it; then parses the command line remains for more arguments which overwrite everything previously set. (For the record, this split parsing is done with two argparse parsers. The first parses for '-c FILE' with parse_known_args(). If there is a FILE, its contents is used as defaults for a second parser (using set_options()) which then parses the remains that were returned by the first parser's parse_known_args().) But now I would also like to be able to _write_ such a config file FILE that can be read in a later run. And FILE should contain only those arguments that were given on the command line. Say, I tell argparse to look for arguments -s|--sopt STRING, -i|--iopt INT, -b|--bopt [BOOL], -C CONFFILE. Then 'prog -s bla -i 42 -C cfile' should produce a confparser compatible cfile which contains [my_options] sopt = blah iopt = 42 and not 'bopt = False' (if False was the program's default for bopt). Regards, Henry From jpiitula at ling.helsinki.fi Thu May 16 02:08:45 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 16 May 2013 09:08:45 +0300 Subject: Determine actually given command line arguments References: Message-ID: Henry Leyh writes: > But now I would also like to be able to _write_ such a config file > FILE that can be read in a later run. And FILE should contain only > those arguments that were given on the command line. > > Say, I tell argparse to look for arguments -s|--sopt STRING, > -i|--iopt INT, -b|--bopt [BOOL], -C CONFFILE. Then 'prog -s bla -i > 42 -C cfile' should produce a confparser compatible cfile which > contains > > [my_options] > sopt = blah > iopt = 42 > > and not 'bopt = False' (if False was the program's default for > bopt). Could you instead write those options that differ from the defaults? You could parse an actual command line and an empty command line, and work out the difference. So 'prog -i 3' would not cause 'iopt = 3' to be written if 3 is the default for iopt, but would that be a problem? From henry.leyh at ipp.mpg.de Thu May 16 03:29:50 2013 From: henry.leyh at ipp.mpg.de (Henry Leyh) Date: Thu, 16 May 2013 09:29:50 +0200 Subject: Determine actually given command line arguments In-Reply-To: References: Message-ID: On 16.05.2013 08:08, Jussi Piitulainen wrote: > Henry Leyh writes: > >> But now I would also like to be able to _write_ such a config file >> FILE that can be read in a later run. And FILE should contain only >> those arguments that were given on the command line. >> >> Say, I tell argparse to look for arguments -s|--sopt STRING, >> -i|--iopt INT, -b|--bopt [BOOL], -C CONFFILE. Then 'prog -s bla -i >> 42 -C cfile' should produce a confparser compatible cfile which >> contains >> >> [my_options] >> sopt = blah >> iopt = 42 >> >> and not 'bopt = False' (if False was the program's default for >> bopt). > > Could you instead write those options that differ from the defaults? > You could parse an actual command line and an empty command line, and > work out the difference. > > So 'prog -i 3' would not cause 'iopt = 3' to be written if 3 is the > default for iopt, but would that be a problem? That's what the program does at the moment. However, I'm not quite happy with it. Generally, the user doesn't know what's the default and it would be confusing if 'prog -i 3' doesn't make 'iopt = 3' turn up in the file at the end. There may also be the case when the user (for whatever reason) _wants_ the default in the file. I think I will try the opposite instead: the program writes the whole set of options to the file. This would produce a complete and consistent configuration which automatically reflects the hierarchy in which the options were set. And the user can sort it out by hand if he wants. Regards, Henry From wzab01 at gmail.com Wed May 15 07:18:03 2013 From: wzab01 at gmail.com (wzab) Date: Wed, 15 May 2013 04:18:03 -0700 (PDT) Subject: Python 2.7.x - problem with obejct.__init__() not accepting *args and **kwargs Message-ID: I had to implement in Python 2.7.x a system which heavily relies on multiple inheritance. Working on that, I have came to very simplistic code which isolates the problem: (The essential thing is that each base class receives all arguments and uses only those, which it understands). class a(object): def __init__(self,*args,**kwargs): super(a,self).__init__(*args,**kwargs) print args print kwargs print "init in a" class b(object): def __init__(self,*args,**kwargs): super(b,self).__init__(*args,**kwargs) print args print kwargs print "init in b" class c(a,b): def __init__(self,*args,**kwargs): super(c,self).__init__(*args,**kwargs) print args print kwargs print "init in c" z=c(test=23,data="eee") In Python 2.5.2 the above code works correctly, and produces: $python test1.py () {'test': 23, 'data': 'eee'} init in b () {'test': 23, 'data': 'eee'} init in a () {'test': 23, 'data': 'eee'} init in c Unfortunately in Python 2.7 the above code generates an exception: $ python test1.py Traceback (most recent call last): File "test1.py", line 22, in z=c(test=23,data="eee") File "test1.py", line 17, in __init__ super(c,self).__init__(*args,**kwargs) File "test1.py", line 3, in __init__ super(a,self).__init__(*args,**kwargs) File "test1.py", line 10, in __init__ super(b,self).__init__(*args,**kwargs) TypeError: object.__init__() takes no parameters I have found a workaround: # Class my_object added only as workaround for a problem with # object.__init__() not accepting any arguments. class my_object(object): def __init__(self,*args,**kwargs): super(my_object,self).__init__() class a(my_object): def __init__(self,*args,**kwargs): super(a,self).__init__(*args,**kwargs) print args print kwargs print "init in a" class b(my_object): def __init__(self,*args,**kwargs): super(b,self).__init__(*args,**kwargs) print args print kwargs print "init in b" class c(a,b): def __init__(self,*args,**kwargs): super(c,self).__init__(*args,**kwargs) print args print kwargs print "init in c" z=c(test=23,data="eee") The above works correctly, producing the same results as the first code in Python 2.5.2, but anyway it seems to me just a dirty trick... What is the proper way to solve that problem in Python 2.7.3? -- TIA, Wojtek From oscar.j.benjamin at gmail.com Wed May 15 08:16:09 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 15 May 2013 13:16:09 +0100 Subject: Python 2.7.x - problem with obejct.__init__() not accepting *args and **kwargs In-Reply-To: References: Message-ID: On 15 May 2013 12:18, wzab wrote: > I had to implement in Python 2.7.x a system which heavily relies on > multiple inheritance. > Working on that, I have came to very simplistic code which isolates > the problem: > (The essential thing is that each base class receives all arguments > and uses only those, > which it understands). > [snip] > > I have found a workaround: > > # Class my_object added only as workaround for a problem with > # object.__init__() not accepting any arguments. [snip] > > The above works correctly, producing the same results as the first > code in Python 2.5.2, > but anyway it seems to me just a dirty trick... > What is the proper way to solve that problem in Python 2.7.3? I don't generally use super() but I did see some advice about it in this article: https://fuhm.net/super-harmful/ >From the conclusion: "Never use positional arguments in __init__ or __new__. Always use keyword args, and always call them as keywords, and always pass all keywords on to super." Oscar From ian.g.kelly at gmail.com Wed May 15 11:41:35 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 15 May 2013 09:41:35 -0600 Subject: Python 2.7.x - problem with obejct.__init__() not accepting *args and **kwargs In-Reply-To: References: Message-ID: On Wed, May 15, 2013 at 6:16 AM, Oscar Benjamin wrote: > I don't generally use super() but I did see some advice about it in > this article: > https://fuhm.net/super-harmful/ > > From the conclusion: > "Never use positional arguments in __init__ or __new__. Always use > keyword args, and always call them as keywords, and always pass all > keywords on to super." While that article is a good read, this one is a bit better on giving advice about how to practically use super: http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ From steve+comp.lang.python at pearwood.info Wed May 15 22:06:11 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 May 2013 02:06:11 GMT Subject: Python 2.7.x - problem with obejct.__init__() not accepting *args and **kwargs References: Message-ID: <51943f13$0$29997$c3e8da3$5496439d@news.astraweb.com> On Wed, 15 May 2013 13:16:09 +0100, Oscar Benjamin wrote: > I don't generally use super() Then you should, especially in Python 3. If you're not using super in single-inheritance classes, then you're merely making your own code harder to read and write, and unnecessarily difficult for others to use with multiple-inheritance. If you're not using super in multiple-inheritance[1] classes, then your code is probably buggy. There really is no good reason to avoid super in Python 3. > but I did see some advice about it in this article: > https://fuhm.net/super-harmful/ It's not a good article. The article started off claiming that super was harmful, hence the URL. He's had to back-pedal, and *hard*. The problem isn't that super is harmful, it is that the problem being solved -- generalized multiple inheritance -- is inherently a fiendishly difficult problem to solve. Using super and cooperative multiple inheritance makes it a merely difficult but tractable problem. The above article is useful to see the sorts of issues that can come up in multiple inheritance, and perhaps as an argument for avoiding MI (except in the tamed versions provided by mixins or straits). But as an argument against super? No. A much better article about super is: http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ > From the conclusion: > "Never use positional arguments in __init__ or __new__. Always use > keyword args, and always call them as keywords, and always pass all > keywords on to super." Even that advice is wrong. See Super Considered Super above. [1] To be precise: one can write mixin classes without super, and strictly speaking mixins are a form of multiple inheritance, but it is a simplified version of multiple inheritance that avoids most of the complications. -- Steven From ian.g.kelly at gmail.com Thu May 16 02:37:53 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 16 May 2013 00:37:53 -0600 Subject: Python 2.7.x - problem with obejct.__init__() not accepting *args and **kwargs In-Reply-To: <51943f13$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <51943f13$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, May 15, 2013 at 8:06 PM, Steven D'Aprano wrote: > On Wed, 15 May 2013 13:16:09 +0100, Oscar Benjamin wrote: > > >> I don't generally use super() > > Then you should, especially in Python 3. > > If you're not using super in single-inheritance classes, then you're > merely making your own code harder to read and write, and unnecessarily > difficult for others to use with multiple-inheritance. > > If you're not using super in multiple-inheritance[1] classes, then your > code is probably buggy. > > There really is no good reason to avoid super in Python 3. The Python 3 syntactic sugar is the primary reason that I've finally started using super in single-inheritance classes. The magicalness of it still disturbs me a bit, though. >>> class A: ... def __str__(self): ... return super().__str__() ... >>> class B: ... __str__ = A.__str__ ... >>> A().__str__ > >>> str(A()) '<__main__.A object at 0x0289E270>' >>> B().__str__ > The transplanted __str__ method is considered a method of B by Python... >>> str(B()) Traceback (most recent call last): File "", line 1, in File "", line 3, in __str__ TypeError: super(type, obj): obj must be an instance or subtype of type But you can't use it because the super() call is irrevocably tied to class A. :-P Of course the same is true with the syntax "super(A, self)", but at least with that syntax it is clear that the method is explicitly referencing class A, and so should not be expected to work correctly in class B. By contrast the syntax "super()" looks misleadingly generic. From oscar.j.benjamin at gmail.com Thu May 16 06:31:08 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 16 May 2013 11:31:08 +0100 Subject: Python 2.7.x - problem with obejct.__init__() not accepting *args and **kwargs In-Reply-To: <51943f13$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <51943f13$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 16 May 2013 03:06, Steven D'Aprano wrote: > On Wed, 15 May 2013 13:16:09 +0100, Oscar Benjamin wrote: > > >> I don't generally use super() > > Then you should, especially in Python 3. > > If you're not using super in single-inheritance classes, then you're > merely making your own code harder to read and write, and unnecessarily > difficult for others to use with multiple-inheritance. > > If you're not using super in multiple-inheritance[1] classes, then your > code is probably buggy. > > There really is no good reason to avoid super in Python 3. I should have been clearer. I don't generally use super() because I don't generally use Python in a very object-oriented way. My comment was intended as a qualification of my advice rather than a suggestion that there is something wrong with super(). I can certainly see how that would be misinterpreted given the article I linked to: >> but I did see some advice about it in this article: >> https://fuhm.net/super-harmful/ > > It's not a good article. The article started off claiming that super was > harmful, hence the URL. He's had to back-pedal, and *hard*. The problem > isn't that super is harmful, it is that the problem being solved -- > generalized multiple inheritance -- is inherently a fiendishly difficult > problem to solve. Using super and cooperative multiple inheritance makes > it a merely difficult but tractable problem. > > The above article is useful to see the sorts of issues that can come up > in multiple inheritance, and perhaps as an argument for avoiding MI > (except in the tamed versions provided by mixins or straits). But as an > argument against super? No. I read that article when I was trying to do something with multiple inheritance. It was helpful to me at that time as it explained why whatever I was trying to do (I don't remember) was never really going to work. > > A much better article about super is: > > http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ This is a good article and I read it after Ian posted it. > > >> From the conclusion: >> "Never use positional arguments in __init__ or __new__. Always use >> keyword args, and always call them as keywords, and always pass all >> keywords on to super." > > Even that advice is wrong. See Super Considered Super above. Raymond's two suggestions for signature are: ''' One approach is to stick with a fixed signature using positional arguments. This works well with methods like __setitem__ which have a fixed signature of two arguments, a key and a value. This technique is shown in the LoggingDict example where __setitem__ has the same signature in both LoggingDict and dict. A more flexible approach is to have every method in the ancestor tree cooperatively designed to accept keyword arguments and a keyword-arguments dictionary, to remove any arguments that it needs, and to forward the remaining arguments using **kwds, eventually leaving the dictionary empty for the final call in the chain. ''' The first cannot be used with object.__init__ and the second is not what the OP wants. I think from the article that the appropriate suggestion is to do precisely what the OP has done and make everything a subclass of a root class that has the appropriate signature. Perhaps instead of calling it my_object it could have a meaningful name related to what the subclasses are actually for and then it wouldn't seem so much like a dirty trick. > [1] To be precise: one can write mixin classes without super, and > strictly speaking mixins are a form of multiple inheritance, but it is a > simplified version of multiple inheritance that avoids most of the > complications. They're also mostly the only kind of multiple inheritance that I would think of using. Oscar From abradley201 at gmail.com Wed May 15 12:56:59 2013 From: abradley201 at gmail.com (Andrew Bradley) Date: Wed, 15 May 2013 12:56:59 -0400 Subject: Question re: objects and square grids Message-ID: Hello everyone. I am having a good time programming with Python 3.3 and Pygame. Pygame seems like the perfect platform for the kind of simple games that I want to make. What I have currently programmed is basically a drawn rectangle filled with 200 squares, each side of the squares being 43 pixels. The rectangle is 20 squares by 10 squares. This grid is what I want to have the entire game on. There will be pieces that move certain numbers of squares, perform actions that effect pieces on certain squares, etc. The possibilities are endless. So what I am now trying to do is organize these squares into objects that I can easily reference when programming things related to the squares. So far, I have done this: A1 = pygame.Rect(10, 12, 43, 43) A2 A3 A4 B1 B2 etc. where said integers are the precise location of the top left-most square for A1, and onward down the line. I would guess that I could continue on in such a fashion, with appropriate box names for each square. But I'm running into the problem of confirming that the code above actually does something useful. For example, I would love to be able to turn A1 a different color, but how does this work? How do I actually utilize these object variable names? Are there methods that I am not aware of, because most things I try tend to do nothing or crash the game. For example, A1.fill(BLUE) does not work. Any general advice about how to organize my squares into something that is easy to program for would be VERY appreciated. Basically all the games I want to make involve square grids like this, so I want to know as much about them as possible. Thank you very much for reading this, Andrew -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Wed May 15 13:55:34 2013 From: davea at davea.name (Dave Angel) Date: Wed, 15 May 2013 13:55:34 -0400 Subject: Question re: objects and square grids In-Reply-To: References: Message-ID: <5193CC16.80304@davea.name> On 05/15/2013 12:56 PM, Andrew Bradley wrote: > Hello everyone. > > I am having a good time programming with Python 3.3 and Pygame. Pygame > seems like the perfect platform for the kind of simple games that I want to > make. > Pygame indeed looks pretty good to me as well. But I haven't done anything with it, so I can't give you specific Pygame advice. > > > So far, I have done this: > > A1 = pygame.Rect(10, 12, 43, 43) > A2 > A3 > A4 > B1 > B2 > etc. > This is a code smell in Python (or any reasonable language). I'd suggest that instead of trying to have 200 separate global variables, you have one, maybe called grid, as follows: maxrows = 10 maxcols = 20 grid = [] for row in range(maxrows): rowdata = [] for column in range(maxcols): arg1 = ... arg2 = ... arg3 = ... arg4 = ... rowdata.append(pygame.Rect(arg1, arg2, arg3, arg4) grid.append(rowdata) Now, this can be done shorter with list comprehensions, but I figured this would be clearer for you. I also may have the 10 and 20 reversed, but you can work that out. You'd need to make four formulae to get the four arguments to the Rect call, using row and column respectively. Remember that row and column will be numbered from zero, not from one. But now, you can either address a single rect by grid[4][8] or you can do something to all of them, or to an entire row, or to an entire column, pretty easily. While you're testing your formulae, you might want to replace the rowdata.append line with something like: rowdata.append( (arg1, arg2, arg3, arg4) ) And perhaps your maxrows, maxcols might be lower while you're testing. And you can just print grid to see how those arguments are looking. -- DaveA From davea at davea.name Wed May 15 15:57:52 2013 From: davea at davea.name (Dave Angel) Date: Wed, 15 May 2013 15:57:52 -0400 Subject: Question re: objects and square grids In-Reply-To: <5193CC16.80304@davea.name> References: <5193CC16.80304@davea.name> Message-ID: <5193E8C0.1060004@davea.name> On 05/15/2013 02:14 PM, Andrew Bradley wrote: Please reply on the list, not privately, unless it's something like a simple thank-you. Typically, you'd do a reply-all, then delete the people other than the list itself. Or if you're using Thunderbird, you could just reply-list. > Thank you very much for your response: it seems excellent, but I'm afraid I > do not understand it fully. Your code here: > > maxrows = 10 > maxcols = 20 > grid = [] > for row in range(maxrows): > rowdata = [] > for column in range(maxcols): > arg1 = ... > arg2 = ... > arg3 = ... > arg4 = ... > rowdata.append(pygame.Rect(arg > 1, arg2, arg3, arg4) > grid.append(rowdata) > > Seems very good, but keep in mind I just started programming last week, and > this is hard for me to wrap my head around. Do I really just write grid = > []? or is this like a def grid(): function? This code was intended to replace the 200 lines you started, A1= pygame... A2= A3= etc. I'd have put them inside a function, but this is just one of the things I'd have initialized in such a function. grid is a list of lists, not a function. > What do you mean by rowdata = []? [] is the way you define an empty list. Another way might be: rowdata = list() > And how exactly would I make the formula for a rect call? Well, for row==0 and col==0, you say you wanted 10, 12, 43, and 43 for the four parameters. But you never said how you were going to (manually) calculate those numbers for other cells. Only once you've decided that can you fill in "formulas" for arg1 and arg2. I suspect that arg3 and arg4 are simply 43 and 43 respectively, since you want all the cells to be the same size. taking my clue from Ian, I might try: x_offset = 10 y_offset = 12 width = height = 43 arg1 = column * width + x_offset arg2 = row * height + y_offset arg3 = width arg4 = height That assumes that there is no gap between cells in this grid. If you want a gap, then the width value used in the arg1 formula would be more than 43 (width). Likewise the height value used in the arg2 formula would be more than 43 (height). > If there's a good website for these kind of details, I would appreciate that too. You cannot begin to write a non-trivial program in Python without understanding lists pretty thoroughly. Perhaps you should start with Alan Gauld's tutorial, which doesn't assume previous programming experience. http://www.alan-g.me.uk/ I haven't studied it, as Python was about my 35th programming language. But he's very active on Python-tutor, and explains things very well. So his website is probably very good as well. Now, as you can see from Ian's message, writing a game using pygame will require quite a bit of other understanding. He demonstrates with classes to represent cells, which is indeed what I'd do. But I suspect you're not nearly ready to consider writing classes. (You use classes all the time. For example, 5 is an instance of class int.) -- DaveA -- DaveA From abradley201 at gmail.com Wed May 15 16:35:57 2013 From: abradley201 at gmail.com (Andrew Bradley) Date: Wed, 15 May 2013 16:35:57 -0400 Subject: Question re: objects and square grids In-Reply-To: <5193E8C0.1060004@davea.name> References: <5193CC16.80304@davea.name> <5193E8C0.1060004@davea.name> Message-ID: Now I want to show you what I have written: row = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) column = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) SQUARESIZE = 43 grid = [] for row in range(10): row_squares = [] for column in range(20): rect = Rect(12 + column * SQUARESIZE, 10 + row * SQUARESIZE, SQUARESIZE, SQUARESIZE) row_squares.append(rect) grid.append(row_squares) It appears to be working (that is, the program still runs without crashing). So now, how can I utilize this new grid list? Thank you for the help so far, I feel like the entire grid is now being worked out. -Andrew On Wed, May 15, 2013 at 3:57 PM, Dave Angel wrote: > On 05/15/2013 02:14 PM, Andrew Bradley wrote: > > Please reply on the list, not privately, unless it's something like a > simple thank-you. Typically, you'd do a reply-all, then delete the people > other than the list itself. Or if you're using Thunderbird, you could just > reply-list. > > > Thank you very much for your response: it seems excellent, but I'm > afraid I > > do not understand it fully. Your code here: > > > > > maxrows = 10 > > maxcols = 20 > > grid = [] > > for row in range(maxrows): > > rowdata = [] > > for column in range(maxcols): > > arg1 = ... > > arg2 = ... > > arg3 = ... > > arg4 = ... > > rowdata.append(pygame.Rect(arg > > 1, arg2, arg3, arg4) > > grid.append(rowdata) > > > > Seems very good, but keep in mind I just started programming last week, > and > > this is hard for me to wrap my head around. Do I really just write grid = > > []? or is this like a def grid(): function? > > This code was intended to replace the 200 lines you started, A1= pygame... > A2= A3= etc. I'd have put them inside a function, but this is just one > of the things I'd have initialized in such a function. grid is a list of > lists, not a function. > > > > What do you mean by rowdata = []? > > [] is the way you define an empty list. Another way might be: > rowdata = list() > > > > And how exactly would I make the formula for a rect call? > > Well, for row==0 and col==0, you say you wanted 10, 12, 43, and 43 for the > four parameters. But you never said how you were going to (manually) > calculate those numbers for other cells. Only once you've decided that can > you fill in "formulas" for arg1 and arg2. I suspect that arg3 and arg4 are > simply 43 and 43 respectively, since you want all the cells to be the same > size. > > taking my clue from Ian, I might try: > > x_offset = 10 > y_offset = 12 > width = height = 43 > arg1 = column * width + x_offset > arg2 = row * height + y_offset > arg3 = width > arg4 = height > > That assumes that there is no gap between cells in this grid. If you want > a gap, then the width value used in the arg1 formula would be more than 43 > (width). Likewise the height value used in the arg2 formula would be more > than 43 (height). > > > If there's a good website for these kind of details, I would appreciate > that too. > > You cannot begin to write a non-trivial program in Python without > understanding lists pretty thoroughly. Perhaps you should start with Alan > Gauld's tutorial, which doesn't assume previous programming experience. > http://www.alan-g.me.uk/ > > I haven't studied it, as Python was about my 35th programming language. > But he's very active on Python-tutor, and explains things very well. So > his website is probably very good as well. > > Now, as you can see from Ian's message, writing a game using pygame will > require quite a bit of other understanding. He demonstrates with classes > to represent cells, which is indeed what I'd do. But I suspect you're not > nearly ready to consider writing classes. (You use classes all the time. > For example, 5 is an instance of class int.) > > > -- > DaveA > > > > -- > DaveA > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From abradley201 at gmail.com Wed May 15 18:48:07 2013 From: abradley201 at gmail.com (Andrew Bradley) Date: Wed, 15 May 2013 18:48:07 -0400 Subject: Question re: objects and square grids In-Reply-To: References: <5193CC16.80304@davea.name> <5193E8C0.1060004@davea.name> Message-ID: ok, now I have tested this more thoroughly, and it seems i can only do the grid[x][y] function up to grid[9][9], when i really should be able to be doing up to grid[10][20]. What exactly is the function of this row_squares list? On Wed, May 15, 2013 at 4:35 PM, Andrew Bradley wrote: > Now I want to show you what I have written: > > row = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) > column = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, > 18, 19, 20) > SQUARESIZE = 43 > > grid = [] > for row in range(10): > row_squares = [] > for column in range(20): > rect = Rect(12 + column * SQUARESIZE, 10 + row * SQUARESIZE, > SQUARESIZE, SQUARESIZE) > row_squares.append(rect) > grid.append(row_squares) > > It appears to be working (that is, the program still runs without > crashing). So now, how can I utilize this new grid list? Thank you for the > help so far, I feel like the entire grid is now being worked out. > -Andrew > > > > On Wed, May 15, 2013 at 3:57 PM, Dave Angel wrote: > >> On 05/15/2013 02:14 PM, Andrew Bradley wrote: >> >> Please reply on the list, not privately, unless it's something like a >> simple thank-you. Typically, you'd do a reply-all, then delete the people >> other than the list itself. Or if you're using Thunderbird, you could just >> reply-list. >> >> > Thank you very much for your response: it seems excellent, but I'm >> afraid I >> > do not understand it fully. Your code here: >> >> > >> > maxrows = 10 >> > maxcols = 20 >> > grid = [] >> > for row in range(maxrows): >> > rowdata = [] >> > for column in range(maxcols): >> > arg1 = ... >> > arg2 = ... >> > arg3 = ... >> > arg4 = ... >> > rowdata.append(pygame.Rect(arg >> > 1, arg2, arg3, arg4) >> > grid.append(rowdata) >> > >> > Seems very good, but keep in mind I just started programming last week, >> and >> > this is hard for me to wrap my head around. Do I really just write grid >> = >> > []? or is this like a def grid(): function? >> >> This code was intended to replace the 200 lines you started, A1= >> pygame... A2= A3= etc. I'd have put them inside a function, but this is >> just one of the things I'd have initialized in such a function. grid is a >> list of lists, not a function. >> >> >> > What do you mean by rowdata = []? >> >> [] is the way you define an empty list. Another way might be: >> rowdata = list() >> >> >> > And how exactly would I make the formula for a rect call? >> >> Well, for row==0 and col==0, you say you wanted 10, 12, 43, and 43 for >> the four parameters. But you never said how you were going to (manually) >> calculate those numbers for other cells. Only once you've decided that can >> you fill in "formulas" for arg1 and arg2. I suspect that arg3 and arg4 are >> simply 43 and 43 respectively, since you want all the cells to be the same >> size. >> >> taking my clue from Ian, I might try: >> >> x_offset = 10 >> y_offset = 12 >> width = height = 43 >> arg1 = column * width + x_offset >> arg2 = row * height + y_offset >> arg3 = width >> arg4 = height >> >> That assumes that there is no gap between cells in this grid. If you >> want a gap, then the width value used in the arg1 formula would be more >> than 43 (width). Likewise the height value used in the arg2 formula would >> be more than 43 (height). >> >> > If there's a good website for these kind of details, I would appreciate >> that too. >> >> You cannot begin to write a non-trivial program in Python without >> understanding lists pretty thoroughly. Perhaps you should start with Alan >> Gauld's tutorial, which doesn't assume previous programming experience. >> http://www.alan-g.me.uk/ >> >> I haven't studied it, as Python was about my 35th programming language. >> But he's very active on Python-tutor, and explains things very well. So >> his website is probably very good as well. >> >> Now, as you can see from Ian's message, writing a game using pygame will >> require quite a bit of other understanding. He demonstrates with classes >> to represent cells, which is indeed what I'd do. But I suspect you're not >> nearly ready to consider writing classes. (You use classes all the time. >> For example, 5 is an instance of class int.) >> >> >> -- >> DaveA >> >> >> >> -- >> DaveA >> -- >> http://mail.python.org/**mailman/listinfo/python-list >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Wed May 15 20:17:48 2013 From: davea at davea.name (Dave Angel) Date: Wed, 15 May 2013 20:17:48 -0400 Subject: Question re: objects and square grids In-Reply-To: References: <5193CC16.80304@davea.name> <5193E8C0.1060004@davea.name> Message-ID: <519425AC.3070605@davea.name> Please put new comments AFTER the part you're quoting. In other words, don't top-post. Also please trim off the stuff that's no longer relevant, so people don't have to read through it all wondering where your implied comments are. On 05/15/2013 06:48 PM, Andrew Bradley wrote: > ok, now I have tested this more thoroughly, and it seems i can only do the > grid[x][y] function up to grid[9][9], when i really should be able to be > doing up to grid[10][20]. No, you shouldn't expect it to go to 10,20. Remember I said that Python is zero-based. So it goes from 0 to 9 inclusive, and from 0 to 19 inclusive. Upper left corner is grid[0][0], while lower right is grid[9][19]. Check to make sure that range works, which I think it will. > What exactly is the function of this row_squares list? It's a temporary to hold one row. You could delete it after the outer loop ends, if you like. Normally, if this whole thing were inside a function, the variable would go away when the function ended, and you'd be returning the grid list only. You could have avoided the separate name by doing some tricky syntax. But I'm trying to show you the clearest way of doing things from a beginner perspective. > > > > On Wed, May 15, 2013 at 4:35 PM, Andrew Bradley wrote: > >> Now I want to show you what I have written: >> >> row = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) >> column = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, >> 18, 19, 20) No point in initializing them to tuples, since you're not going to use those. Besides, in a moment row and column become ints, and this just confuses things. The range() function below builds a temporary list which looks just like what you tediously typed in (except it's square brackets instead of round). >> SQUARESIZE = 43 >> >> grid = [] >> for row in range(10): >> row_squares = [] >> for column in range(20): >> rect = Rect(12 + column * SQUARESIZE, 10 + row * SQUARESIZE, >> SQUARESIZE, SQUARESIZE) >> row_squares.append(rect) >> grid.append(row_squares) >> >> It appears to be working (that is, the program still runs without >> crashing). Sorry, but that's no criteria. Question is whether it's doing what you want. Are the rows 20 across and are there 10 of them? Do the values of each individual rect look right? print is your friend. >> So now, how can I utilize this new grid list? Thank you for the >> help so far, I feel like the entire grid is now being worked out. >> -Andrew >> That's a Pygame question, and I told you at the beginning, I can't really help with that. I'd like to learn, but not this week. Others - can you show some minimal code to use these grid parameters to color selected squares of the pygame window? -- DaveA From abradley201 at gmail.com Wed May 15 20:53:05 2013 From: abradley201 at gmail.com (Andrew Bradley) Date: Wed, 15 May 2013 20:53:05 -0400 Subject: Question re: objects and square grids In-Reply-To: <519425AC.3070605@davea.name> References: <5193CC16.80304@davea.name> <5193E8C0.1060004@davea.name> <519425AC.3070605@davea.name> Message-ID: > > > SQUARESIZE = 43 >>> >>> grid = [] >>> for row in range(10): >>> row_squares = [] >>> for column in range(20): >>> rect = Rect(12 + column * SQUARESIZE, 10 + row * SQUARESIZE, >>> SQUARESIZE, SQUARESIZE) >>> row_squares.append(rect) >>> grid.append(row_squares) >>> >>> It appears to be working (that is, the program still runs without >>> crashing). >>> >> > Sorry, but that's no criteria. Question is whether it's doing what you > want. Are the rows 20 across and are there 10 of them? Do the values of > each individual rect look right? print is your friend. Yes, I have gotten rid of that part with the 1, 2, 3, 4, etc., and now the code appears to be working up to [9][19]. Thank you very much. The coordinates all do look correct, and there are 200 rectangles when I do list(grid). > > > So now, how can I utilize this new grid list? Thank you for the >>> help so far, I feel like the entire grid is now being worked out. >>> -Andrew >>> >>> > That's a Pygame question, and I told you at the beginning, I can't really > help with that. I'd like to learn, but not this week. > > Others - can you show some minimal code to use these grid parameters to > color selected squares of the pygame window? > > Yes, I would very much like some help or general advice now about utilizing this grid thing. How can I refer to these square's new numerical values to do things with them? Will I be using things like "grid[0][1]" in specific functions and methods to refer to squares now? That is what I would like to do, somehow. > > -- > DaveA > -- > http://mail.python.org/**mailman/listinfo/python-list > I apologize if these questions are too rudimentary--I am trying to wrap my head around how this language works in a more general sense so I can start applying it to things. -Andrew -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Wed May 15 21:20:31 2013 From: davea at davea.name (Dave Angel) Date: Wed, 15 May 2013 21:20:31 -0400 Subject: Question re: objects and square grids In-Reply-To: References: <5193CC16.80304@davea.name> <5193E8C0.1060004@davea.name> <519425AC.3070605@davea.name> Message-ID: <5194345F.9070209@davea.name> On 05/15/2013 08:53 PM, Andrew Bradley wrote: >> >> >> >> >> So now, how can I utilize this new grid list? Thank you for the >>>> help so far, I feel like the entire grid is now being worked out. >>>> -Andrew >>>> >>>> >> That's a Pygame question, and I told you at the beginning, I can't really >> help with that. I'd like to learn, but not this week. >> >> Others - can you show some minimal code to use these grid parameters to >> color selected squares of the pygame window? >> >> > Yes, I would very much like some help or general advice now about utilizing > this grid thing. How can I refer to these square's new numerical values to > do things with them? Will I be using things like "grid[0][1]" in specific > functions and methods to refer to squares now? Yes, exactly. Any place in your code (the part I cannot help with) that you need to specify a rect, you can specify something like grid[3][18]. Eventually, as Ian pointed out, you'll want to store more information about each cell. But at that time, you could have a more complex object (that YOU define) instead of just a rect. I just don't think you're ready for defining such an object. > That is what I would like to > do, somehow. > -- DaveA From orgnut at yahoo.com Thu May 16 02:54:37 2013 From: orgnut at yahoo.com (Larry Hudson) Date: Wed, 15 May 2013 23:54:37 -0700 Subject: Question re: objects and square grids In-Reply-To: References: <5193CC16.80304@davea.name> <5193E8C0.1060004@davea.name> <519425AC.3070605@davea.name> Message-ID: On 05/15/2013 05:53 PM, Andrew Bradley wrote: > I apologize if these questions are too rudimentary--I am trying to wrap my head around how this > language works in a more general sense so I can start applying it to things. > -Andrew Check out the book "Making Games with Python & Pygame" at http://inventwithpython.com/pygame/ It's a book you can buy or you can download the pdf or e-reader versions for free. Also on the same site, "Invent Your Own Computer Games with Python" Both are pretty good introductions to Python programming, "Invent..." is for non-graphic games, and may be the better to start with. Don't try to get too advanced too fast -- you'll only get frustrated and discouraged. But definitely do keep at it -- it's well worth the effort. -=- Larry -=- From ian.g.kelly at gmail.com Wed May 15 14:01:06 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 15 May 2013 12:01:06 -0600 Subject: Question re: objects and square grids In-Reply-To: References: Message-ID: On Wed, May 15, 2013 at 10:56 AM, Andrew Bradley wrote: > Hello everyone. > > I am having a good time programming with Python 3.3 and Pygame. Pygame seems > like the perfect platform for the kind of simple games that I want to make. > > What I have currently programmed is basically a drawn rectangle filled with > 200 squares, each side of the squares being 43 pixels. The rectangle is 20 > squares by 10 squares. It's more work, but I would recommend making the squares scalable in some manner, instead of arbitrarily hard-coding them at 43 pixels. What are you going to do when somebody has a 4K monitor and wants to run the game full screen? > This grid is what I want to have the entire game on. There will be pieces > that move certain numbers of squares, perform actions that effect pieces on > certain squares, etc. The possibilities are endless. > > So what I am now trying to do is organize these squares into objects that I > can easily reference when programming things related to the squares. > > So far, I have done this: > > A1 = pygame.Rect(10, 12, 43, 43) > A2 > A3 > A4 > B1 > B2 > etc. This is a tedious, repetitive, and error-prone way to build all the squares. Instead of having a separate variable for each square, consider putting them in a collection and building them up using nested loops. For example, the above could look something like: squares = [] for row in range(10): row_squares = [] for column in range(20): rect = Rect(column * width + x_offset, row * height + y_offset, width, height) row_squares.append(rect) squares.append(row_squares) Then the A1 rect is accessible as squares[0][0], A2 as squares[0][1], B1 as squares[1][0], etc. Alternatively, you could store the rects in a dict and use "A1", "B1", etc. as the keys, but I think you will find that the game logic will be simpler if you use integers internally for your rows and columns, rather than the square labels. > where said integers are the precise location of the top left-most square for > A1, and onward down the line. I would guess that I could continue on in such > a fashion, with appropriate box names for each square. But I'm running into > the problem of confirming that the code above actually does something > useful. For example, I would love to be able to turn A1 a different color, > but how does this work? How do I actually utilize these object variable > names? Are there methods that I am not aware of, because most things I try > tend to do nothing or crash the game. For example, A1.fill(BLUE) does not > work. A Rect only stores position and size information and provides methods for processing the same. It doesn't know anything about drawing. Use the pygame.draw or pygame.gfxdraw functions for that. For example, to fill a part of the screen as indicated by the position of a rect: BLUE = (0, 0, 255) pygame.draw.rect(screen, BLUE, squares[0][0]) That said, you probably don't just want to color a square blue. You probably also want to remember that the square is blue for when you are redrawing it later. You need to track not just the position of each square, but also their state. This means that each object in your "squares" container should not just be a rect, but some broader object that contains square data, *including* a rect. Something like this: class Square: def __init__(self, rect): self.rect = rect self.color = (0, 0, 0) self.contents = None # ... and whatever else you need to track Then when you fill the squares container, you would fill it with instances of your Square class instead of plain Rects, which gives you a place to keep track of the square's color, among other things. The drawing code above then might look something like this: pygame.draw.rect(screen, some_square.color, some_square.rect) From susheel.ssoft at gmail.com Wed May 15 16:41:30 2013 From: susheel.ssoft at gmail.com (susheel kumar) Date: Wed, 15 May 2013 13:41:30 -0700 (PDT) Subject: Immediate Position:::Business Analyst With BI EXP::::At Houston, TX Message-ID: <7f49d11b-2f0e-4240-93e8-ead55c5ed4dc@yb1g2000pbc.googlegroups.com> Hi, This is Girish, - Recruitment and Resources from SancroSoft USA Inc. We have an urgent requirement as follows: Please respond with resumes in MS-Word Format with the following details toGirish at sancrosoftusa.com Full Name : Location : Contact Number : Email : Availability : Visa Status : Business Analyst & Data Analyst Location: Houston, TX Duration: 12 Months Rate: Market Job Description: ? Total IT experience - 8-10 / 2-3 years as Business / Data Analyst ? Plan Requirement Gathering and Business Analysis efforts ? Liaise with Business and IT teams to coordinate interdependencies and resolve issues ? Interact with SMEs on functional needs to Gather & Analyze the requirements ? Outline Operational Process and Identify any shortages in terms of process or documentation ? Source System Data Analysis & Issue resolution ? Developing and maintaining technical documentation about the data ? Conduct reviews and provide assistance to BA team members in system documentation ? Co-ordination with technical team to resolve any data related issues like date quality, data integrity ? Coordinates and conduct user acceptance testing. ? Facilitating end user training ? Financial domain experience (Accounting, GL, AR, AP etc) would be preferred ? Experience in BI/DWH domain would be a plus ? Excellent verbal and written communication ? The candidate should be proactive, self motivated and resourceful ? Excellent interpersonal skills with an ability to build effective relationships ? Strong knowledge of all Microsoft Office tools ? Excellent time management skills and working experience on-shore/off-shore model Thanks & Regards, Girish IT Recruiter. The power of focus SancroSoft USA INC 4944 Sunrise Blvd, Suite B-4 || Fair Oaks, CA 95628 Phone : 916-671-5591|| Fax: 916-200-0305 E-Mail : girish at sancrosoftusa.com www.sancrosoftusa.com From susheel.ssoft at gmail.com Wed May 15 16:42:49 2013 From: susheel.ssoft at gmail.com (susheel kumar) Date: Wed, 15 May 2013 13:42:49 -0700 (PDT) Subject: Immediate Position:::Business Analyst With BI EXP::::At Houston, TX Message-ID: <75e3d5e6-3174-41b4-b3b9-3e056ac98a56@ve4g2000pbb.googlegroups.com> Hi, This is Girish, - Recruitment and Resources from SancroSoft USA Inc. We have an urgent requirement as follows: Please respond with resumes in MS-Word Format with the following details toGirish at sancrosoftusa.com Full Name : Location : Contact Number : Email : Availability : Visa Status : Business Analyst & Data Analyst Location: Houston, TX Duration: 12 Months Rate: Market Job Description: ? Total IT experience - 8-10 / 2-3 years as Business / Data Analyst ? Plan Requirement Gathering and Business Analysis efforts ? Liaise with Business and IT teams to coordinate interdependencies and resolve issues ? Interact with SMEs on functional needs to Gather & Analyze the requirements ? Outline Operational Process and Identify any shortages in terms of process or documentation ? Source System Data Analysis & Issue resolution ? Developing and maintaining technical documentation about the data ? Conduct reviews and provide assistance to BA team members in system documentation ? Co-ordination with technical team to resolve any data related issues like date quality, data integrity ? Coordinates and conduct user acceptance testing. ? Facilitating end user training ? Financial domain experience (Accounting, GL, AR, AP etc) would be preferred ? Experience in BI/DWH domain would be a plus ? Excellent verbal and written communication ? The candidate should be proactive, self motivated and resourceful ? Excellent interpersonal skills with an ability to build effective relationships ? Strong knowledge of all Microsoft Office tools ? Excellent time management skills and working experience on-shore/off-shore model Thanks & Regards, Girish IT Recruiter. The power of focus SancroSoft USA INC 4944 Sunrise Blvd, Suite B-4 || Fair Oaks, CA 95628 Phone : 916-671-5591|| Fax: 916-200-0305 E-Mail : girish at sancrosoftusa.com www.sancrosoftusa.com From halagamal2009 at gmail.com Wed May 15 18:00:36 2013 From: halagamal2009 at gmail.com (Hala Gamal) Date: Wed, 15 May 2013 15:00:36 -0700 (PDT) Subject: Translation API in Python Message-ID: I want to use A translator from Arabic to English and reverse in my application which is written in pyhton, Is there any open source Translator that can be used with python, Or Any Suggestion? Please Help, Thanks In Advance :) From rosuav at gmail.com Wed May 15 20:02:13 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 16 May 2013 10:02:13 +1000 Subject: Translation API in Python In-Reply-To: References: Message-ID: On Thu, May 16, 2013 at 8:00 AM, Hala Gamal wrote: > I want to use A translator from Arabic to English and reverse in my application which is written in pyhton, > Is there any open source Translator that can be used with python, > Or Any Suggestion? > Please Help, > Thanks In Advance :) I would recommend, in order: 1) Searching the Python modules [1] 2) Searching PyPI [2] 3) Searching the web [3] 4) Finding a translation program that uses stdin/stdout I suspect #1 will come up blank, but any of the others could well come up with something. Working with an external program is a bit harder than an actual Python module, but it'll work. ChrisA [1] http://docs.python.org/3/ [2] https://pypi.python.org/pypi [3] http://www.google.com/ or http://www.duckduckgo.com/ or http://yahoo.com/ or whatever From halagamal2009 at gmail.com Tue May 21 18:16:40 2013 From: halagamal2009 at gmail.com (Hala Gamal) Date: Tue, 21 May 2013 15:16:40 -0700 (PDT) Subject: Translation API in Python In-Reply-To: References: Message-ID: ok MR, I have searched before asking here,but i didn't find thing From rosuav at gmail.com Wed May 22 10:27:29 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 00:27:29 +1000 Subject: Translation API in Python In-Reply-To: References: Message-ID: On Wed, May 22, 2013 at 8:16 AM, Hala Gamal wrote: > ok MR, > I have searched before asking here,but i didn't find thing Your post doesn't demonstrate that. When you ask a question like this, it's helpful to give at least some indication of what you've tried and what you haven't. Also, I strongly suspect that #4 is going to turn something up; though of course people who read this post now have no idea what I'm talking about, because you're responding to last week's post without a shred of context. Here are some tips that might help you: http://www.catb.org/esr/faqs/smart-questions.html ChrisA PS. I say "might" only because they can't help if they're not followed. From miller.eric.t at gmail.com Wed May 15 21:47:06 2013 From: miller.eric.t at gmail.com (Eric Miller) Date: Wed, 15 May 2013 18:47:06 -0700 (PDT) Subject: python sockets question Message-ID: <6aa0447a-e6eb-47b7-9444-71922dad82e7@googlegroups.com> Can python sockets be used to capture IP traffic when the traffic is originating from a non-python source? Using a Lantronix UDS-1100 serial to IP converter. The goal is to write a small proof of concept piece in Python to capture serial data output by this device over IP. I've done a couple test projects using sockets in python, but they were all done between python processes (python > python): listen() on one end, and connect(), sendall() etc on the other. I think I can use sockets for this project, but before I invest a bunch of time into it, wanted to make sure it is a viable solution. Can python sockets be used to capture IP traffic when the traffic is originating from a non-python source? I have full control over the IP and port that the device sends the serial data to, but there will be no python connect() initiated by the client. I can pre-pend then serial data with some connect() string if needed. If sockets won't work, please recommend another solution...guessing it will be REST or similar. From bahamutzero8825 at gmail.com Wed May 15 22:28:17 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 15 May 2013 21:28:17 -0500 Subject: python sockets question In-Reply-To: <6aa0447a-e6eb-47b7-9444-71922dad82e7@googlegroups.com> References: <6aa0447a-e6eb-47b7-9444-71922dad82e7@googlegroups.com> Message-ID: <51944441.2050309@gmail.com> On 2013.05.15 20:47, Eric Miller wrote: > Can python sockets be used to capture IP traffic when the traffic is originating from a non-python source? Python just exposes the underlying OS socket interface. There is nothing special about sockets in Python. The whole point is to connect heterogeneous systems. I can use Python on Windows to create a client that will connect to a service written in C running on Solaris or to something written in Java running on Linux. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From benjamin at python.org Thu May 16 00:19:06 2013 From: benjamin at python.org (Benjamin Peterson) Date: Wed, 15 May 2013 23:19:06 -0500 Subject: [RELEASED] Python 2.7.5 Message-ID: It is my greatest pleasure to announce the release of Python 2.7.5. 2.7.5 is the latest maintenance release in the Python 2.7 series. You may be surprised to hear from me so soon, as Python 2.7.4 was released slightly more than a month ago. As it turns out, 2.7.4 had several regressions and incompatibilities with 2.7.3. Among them were regressions in the zipfile, gzip, and logging modules. 2.7.5 fixes these. In addition, a data file for testing in the 2.7.4 tarballs and binaries aroused the suspicion of some virus checkers. The 2.7.5 release removes this file to resolve that issue. For details, see the Misc/NEWS file in the distribution or view it at http://hg.python.org/cpython/file/ab05e7dd2788/Misc/NEWS Downloads are at http://python.org/download/releases/2.7.5/ As always, please report bugs to http://bugs.python.org/ (Thank you to those who reported these bugs in 2.7.4.) This is a production release. Happy May, Benjamin Peterson 2.7 Release Manager (on behalf of all of Python 2.7's contributors) From carlosnepomuceno at outlook.com Thu May 16 00:48:09 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 16 May 2013 07:48:09 +0300 Subject: [RELEASED] Python 2.7.5 In-Reply-To: References: Message-ID: test_asynchat still hangs! What it does? Should I care? ---------------------------------------- > Date: Wed, 15 May 2013 23:19:06 -0500 > Subject: [RELEASED] Python 2.7.5 > From: benjamin at python.org > To: python-dev at python.org; python-list at python.org; python-announce-list at python.org > > It is my greatest pleasure to announce the release of Python 2.7.5. > > 2.7.5 is the latest maintenance release in the Python 2.7 series. You may be > surprised to hear from me so soon, as Python 2.7.4 was released slightly more > than a month ago. As it turns out, 2.7.4 had several regressions and > incompatibilities with 2.7.3. Among them were regressions in the zipfile, gzip, > and logging modules. 2.7.5 fixes these. In addition, a data file for testing in > the 2.7.4 tarballs and binaries aroused the suspicion of some virus > checkers. The 2.7.5 release removes this file to resolve that issue. > > For details, see the Misc/NEWS file in the distribution or view it at > > http://hg.python.org/cpython/file/ab05e7dd2788/Misc/NEWS > > Downloads are at > > http://python.org/download/releases/2.7.5/ > > As always, please report bugs to > > http://bugs.python.org/ > > (Thank you to those who reported these bugs in 2.7.4.) > > This is a production release. > > Happy May, > Benjamin Peterson > 2.7 Release Manager > (on behalf of all of Python 2.7's contributors) > -- > http://mail.python.org/mailman/listinfo/python-list From georg at python.org Thu May 16 01:20:30 2013 From: georg at python.org (Georg Brandl) Date: Thu, 16 May 2013 07:20:30 +0200 Subject: [RELEASED] Python 3.2.5 and Python 3.3.2 Message-ID: <51946C9E.10607@python.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On behalf of the Python development team, I am pleased to announce the releases of Python 3.2.5 and 3.3.2. The releases fix a few regressions in 3.2.4 and 3.3.1 in the zipfile, gzip and xml.sax modules. Details can be found in the changelogs: http://hg.python.org/cpython/file/v3.2.5/Misc/NEWS and http://hg.python.org/cpython/file/v3.3.2/Misc/NEWS To download Python 3.2.5 or Python 3.3.2, visit: http://www.python.org/download/releases/3.2.5/ or http://www.python.org/download/releases/3.3.2/ respectively. As always, please report bugs to http://bugs.python.org/ (Thank you to those who reported these regressions.) Enjoy! - -- Georg Brandl, Release Manager georg at python.org (on behalf of the entire python-dev team and all contributors) -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (GNU/Linux) iEYEARECAAYFAlGUbJ4ACgkQN9GcIYhpnLDH8ACdEM4k7bobLJsFmCb49zuwQR3W EjgAoIWAOFNhJNdTAWEGSWqFWUP20wrb =YnPr -----END PGP SIGNATURE----- From steve+comp.lang.python at pearwood.info Thu May 16 01:22:38 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 May 2013 05:22:38 GMT Subject: Generating multi-part emails Message-ID: <51946d1e$0$29978$c3e8da3$5496439d@news.astraweb.com> I wish to generate some test data for a program that deals with emails. I need something that can produce multi-part emails, including "broken" emails that violate email standards, especially when it comes to Unicode. Does anyone know of something like this that already exists? It's not necessary for the package to actually send the emails, dumping them into an mbox is sufficient. -- Steven From dieter at handshake.de Thu May 16 02:01:55 2013 From: dieter at handshake.de (dieter) Date: Thu, 16 May 2013 08:01:55 +0200 Subject: Generating multi-part emails References: <51946d1e$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87a9nvtqcs.fsf@handshake.de> Steven D'Aprano writes: > I wish to generate some test data for a program that deals with emails. I > need something that can produce multi-part emails, including "broken" > emails that violate email standards, especially when it comes to Unicode. I would start producing legal messages (e.g. with the "email" package) and then put in common standard violations. I am using the XEmacs "vm" email package and it has difficulties with the quoting of non-ascii characters in header lines (it performes the quoting based on non-ascii character sequences rather than on words, as it should). Maybe, there is an email message catalog around for the test of email servers/clients. I would check for this in corresponding open source projects. From cts.private.yahoo at gmail.com Thu May 16 03:48:45 2013 From: cts.private.yahoo at gmail.com (Charles Smith) Date: Thu, 16 May 2013 00:48:45 -0700 (PDT) Subject: executing python scripts that are symlinked Message-ID: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> Hi. How can I say, from the cmd line, that python should take my CWD as my CWD, and not the directory where the script actually is? I have a python script that works fine when it sits in directory WC, but if I move it out of WC to H and put a symlink from H/script to WC, it doesn't find the packages that are in WC. Also, if I use the absolute path to H, it won't find them, but I guess I can understand that. Someone said on the net that python doesn't know whether a file is real or a symlink, but I think that somehow, python is able to find out where the real file is and treat that as its base of operations. Charles. ---------------------------------------------------------- http://www.creative-telcom-solutions.de From bahamutzero8825 at gmail.com Thu May 16 04:12:50 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 16 May 2013 03:12:50 -0500 Subject: executing python scripts that are symlinked In-Reply-To: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> References: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> Message-ID: <51949502.1030807@gmail.com> On 2013.05.16 02:48, Charles Smith wrote: > Hi. > > How can I say, from the cmd line, that python should take my CWD as my > CWD, and not the directory where the script actually is? > > > I have a python script that works fine when it sits in directory WC, > but if I move it out of WC to H and put a symlink from H/script to WC, > it doesn't find the packages that are in WC. Also, if I use the > absolute path to H, it won't find them, but I guess I can understand > that. Symlinks can find their targets, but targets have absolutely no way of knowing where symlinks to them are. It's one-way. It would work if the actual file were in WC and you created a symlink inside H. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1 From davea at davea.name Thu May 16 04:18:05 2013 From: davea at davea.name (Dave Angel) Date: Thu, 16 May 2013 04:18:05 -0400 Subject: executing python scripts that are symlinked In-Reply-To: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> References: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> Message-ID: <5194963D.1070602@davea.name> On 05/16/2013 03:48 AM, Charles Smith wrote: > Hi. > > How can I say, from the cmd line, that python should take my CWD as my > CWD, and not the directory where the script actually is? > > > I have a python script that works fine when it sits in directory WC, > but if I move it out of WC to H and put a symlink from H/script to WC, > it doesn't find the packages that are in WC. Also, if I use the > absolute path to H, it won't find them, but I guess I can understand > that. > > Someone said on the net that python doesn't know whether a file is > real or a symlink, but I think that somehow, python is able to find > out where the real file is and treat that as its base of operations. > You'd really better specify your environment - exact OS and Python version. symlink and cwd usually imply a Unix-type system, but cmd is a Windows thing. Then give examples of what your cwd is, what string you're typing at the shell prompt, and what's happening. For example windows is good at changing cwd long before the executable gets control, in some situations. -- DaveA From cts.private.yahoo at gmail.com Thu May 16 04:29:54 2013 From: cts.private.yahoo at gmail.com (Charles Smith) Date: Thu, 16 May 2013 01:29:54 -0700 (PDT) Subject: executing python scripts that are symlinked References: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> Message-ID: <8640d5b0-5e6e-4a54-9584-6e7ffcdf1389@k3g2000vbn.googlegroups.com> On 16 Mai, 10:18, Dave Angel wrote: > On 05/16/2013 03:48 AM, Charles Smith wrote: > > > Hi. > > > How can I say, from the cmd line, that python should take my CWD as my > > CWD, and not the directory where the script actually is? > > > I have a python script that works fine when it sits in directory WC, > > but if I move it out of WC to H and put a symlink from H/script to WC, > > it doesn't find the packages that are in WC. ?Also, if I use the > > absolute path to H, it won't find them, but I guess I can understand > > that. > > > Someone said on the net that python doesn't know whether a file is > > real or a symlink, but I think that somehow, python is able to find > > out where the real file is and treat that as its base of operations. > > You'd really better specify your environment - exact OS and Python > version. ?symlink and cwd usually imply a Unix-type system, but cmd is a > Windows thing. > > Then give examples of what your cwd is, what string you're typing at the > shell prompt, and what's happening. Well, I'm on a ubuntu platform, running subversion, but I can't commit this debugging tool into the working copy where I'm using it, so I maintain it in my home directory. The tool does use production packages, though. So, if I say, $ python fapi-test.py and fapi-test.py really is there, then it works, using the codec production package. But if I use a symlink instead, it says Traceback (most recent call last): File "test2", line 1, in from codec.support import * ImportError: No module named codec.support Python tells me Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) ---------------------------------------------------------- http://www.creative-telcom-solutions.de From davea at davea.name Thu May 16 10:45:21 2013 From: davea at davea.name (Dave Angel) Date: Thu, 16 May 2013 10:45:21 -0400 Subject: executing python scripts that are symlinked In-Reply-To: <8640d5b0-5e6e-4a54-9584-6e7ffcdf1389@k3g2000vbn.googlegroups.com> References: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> <8640d5b0-5e6e-4a54-9584-6e7ffcdf1389@k3g2000vbn.googlegroups.com> Message-ID: <5194F101.3000601@davea.name> On 05/16/2013 04:29 AM, Charles Smith wrote: > On 16 Mai, 10:18, Dave Angel wrote: >> On 05/16/2013 03:48 AM, Charles Smith wrote: >> >>> Hi. >> >>> How can I say, from the cmd line, that python should take my CWD as my >>> CWD, and not the directory where the script actually is? >> >>> I have a python script that works fine when it sits in directory WC, >>> but if I move it out of WC to H and put a symlink from H/script to WC, >>> it doesn't find the packages that are in WC. Also, if I use the >>> absolute path to H, it won't find them, but I guess I can understand >>> that. >> >>> Someone said on the net that python doesn't know whether a file is >>> real or a symlink, but I think that somehow, python is able to find >>> out where the real file is and treat that as its base of operations. >> >> You'd really better specify your environment - exact OS and Python >> version. symlink and cwd usually imply a Unix-type system, but cmd is a >> Windows thing. >> >> Then give examples of what your cwd is, what string you're typing at the >> shell prompt, and what's happening. > > > Well, I'm on a ubuntu platform, Good. Linux seldom gets in the way. > running subversion, but I can't commit > this debugging tool into the working copy where I'm using it, so I > maintain it in my home directory. The tool does use production > packages, though. > > So, if I say, > > $ python fapi-test.py > > and fapi-test.py really is there, where? Be explicit. When copying the python invocation line above, you never bothered to include the current directory that's generally displayed right in front of that $ prompt. > then it works, using the codec > production package. What codec production package. And where is it installed? And how do you intend that Python find it? But if I use a symlink instead, So you delete the pfapi-tst.py out of the current directory, and create instead a symlink in the current directory that points to its real location And without changing current directory, you run the same command and it runs till it gets to the import line, at which point it gives a traceback that has nothing to do with the file you're running??? The traceback thinks you're running test2.py. it says > > Traceback (most recent call last): > File "test2", line 1, in > from codec.support import * > ImportError: No module named codec.support > > > Python tells me Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) > > Take a look at Stephen's example. He runs some things, shows you exactly what he's running, and what happens. The things he runs may not match what you did, but we cannot tell. From within your script, you can display os.getcwd(), you can ... import sys, os print "cwd is", os.getcwd() print "file is", __file__ print "PATH is", os.getenv("PATH") print "python-path is", os.getenv("PYTHONPATH") print "sys.path is", sys.path import codec print "codec file is", codec.__file__ if you run that both as a regular file and on a symlink pointing to that file, comparing the two runs could give you a clue as to what's different. My suspicion is that you have a different cwd when you test it the second way. -- DaveA From steve+comp.lang.python at pearwood.info Thu May 16 05:04:02 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 May 2013 09:04:02 GMT Subject: executing python scripts that are symlinked References: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> Message-ID: <5194a102$0$29978$c3e8da3$5496439d@news.astraweb.com> On Thu, 16 May 2013 00:48:45 -0700, Charles Smith wrote: > Hi. > > How can I say, from the cmd line, that python should take my CWD as my > CWD, and not the directory where the script actually is? *scratches head* Python does use your current working directory as your current working directory. I think you are misdiagnosing the problem. Here's a demonstration: steve at runes:~$ cat test.py import os print os.getcwd() steve at runes:~$ ln -s ~/test.py /tmp/test steve at runes:~$ ls -l /tmp/test lrwxrwxrwx 1 steve steve 19 May 16 18:58 /tmp/test -> /home/steve/test.py steve at runes:~$ cd /etc/ steve at runes:/etc$ python /tmp/test /etc > I have a python script that works fine when it sits in directory WC, but > if I move it out of WC to H and put a symlink from H/script to WC, it > doesn't find the packages that are in WC. Also, if I use the absolute > path to H, it won't find them, but I guess I can understand that. The obvious solution is to make sure that WC is in the Python path. You can do that by adding it the environment variable PYTHONPATH, or by adding it to sys.path at the start of your script. I think you can also use a .pth file as well. Another solution may be to add this to the beginning of your script: os.setcwd('path/to/WC') but that's a crappy solution, you generally don't want to be changing the working directory from inside scripts if you can avoid it. -- Steven From cts.private.yahoo at gmail.com Thu May 16 05:23:35 2013 From: cts.private.yahoo at gmail.com (Charles Smith) Date: Thu, 16 May 2013 02:23:35 -0700 (PDT) Subject: executing python scripts that are symlinked References: <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> <5194a102$0$29978$c3e8da3$5496439d@news.astraweb.com> Message-ID: <216f1770-b7f0-44bb-9953-7a6d573663d5@dl10g2000vbb.googlegroups.com> On 16 Mai, 11:04, Steven D'Aprano wrote: > Python does use your current working directory as your current working > directory. I think you are misdiagnosing the problem. That's usually how it ends up ... > > Here's a demonstration: > > steve at runes:~$ cat test.py > import os > print os.getcwd() > > steve at runes:~$ ln -s ~/test.py /tmp/test > steve at runes:~$ ls -l /tmp/test > lrwxrwxrwx 1 steve steve 19 May 16 18:58 /tmp/test -> /home/steve/test.py > steve at runes:~$ cd /etc/ > steve at runes:/etc$ python /tmp/test > /etc You're right. I can believe that python doesn't really change it's notion of the CWD. It seems like when I'm in WC and invoke ./script (which is symlinked to H/script), python says, "I have a specification somewhere that says I can look for modules where my script is, and since I'm really smart, I happen to know that I'm in WC but the script is not, but is really in H, where the stupid operator better have his packages set up" > > > I have a python script that works fine when it sits in directory WC, but > > if I move it out of WC to H and put a symlink from H/script to WC, it > > doesn't find the packages that are in WC. ?Also, if I use the absolute > > path to H, it won't find them, but I guess I can understand that. > > The obvious solution is to make sure that WC is in the Python path. ... > but that's a crappy solution, Right. Other tools don't try to figure out what I really want to do with a symlink, but just accept that it's a file. Python should do the same ... But, as you say, I'm surely misdiagnosing the problem. From mal at egenix.com Thu May 16 03:53:33 2013 From: mal at egenix.com (M.-A. Lemburg) Date: Thu, 16 May 2013 09:53:33 +0200 Subject: EuroPython 2014/2015 Conference Team - Call for Proposals Message-ID: <5194907D.10603@egenix.com> The EuroPython Society (EPS) is happy to announce the Call for Proposals for the EuroPython Conference in 2014 and 2015. This Call for Proposals is meant to collect proposals from teams that volunteer for organizing the EuroPython conference in 2014-2015. The Call for Proposals document containing all the details and information about the proposals and selection process can be found here: https://docs.google.com/file/d/0B8YBdLoQM_6fbVpuM2ZWUGp3Slk/edit?usp=sharing If you are part of a great team organizing amazing Python events you could be the team organizing the next EuroPython! Please also forward this Call for Proposals to anyone that you feel may be interested. The Call for Proposals will run until Friday, June 14th. Proposals must be submitted to europython-contact at python.org before that day, and must adhere the requirements specified in the CFP document. Regards, -- Marc-Andre Lemburg Director EuroPython Society http://www.europython.eu/ From serge.weinstock at uk.bnpparibas.com Thu May 16 04:55:07 2013 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Thu, 16 May 2013 08:55:07 +0000 Subject: Python C-API: how to define nested classes? Message-ID: Hi, I'm currently writing a C extension module for python using the "raw" C-API. I would like to be able to define "nested classes" like in the following python code ============================================================ class A: class B: def __init__(self): self.i = 2 def __init__(self): self.b = A.B() a = A() b = A.B() print(a.b.i) print(b.i) ============================================================ How can I create such nested class with the C-API? Serge WEINSTOCK ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dihedral88888 at googlemail.com Thu May 16 17:22:42 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 16 May 2013 14:22:42 -0700 (PDT) Subject: Python C-API: how to define nested classes? In-Reply-To: References: Message-ID: Serge WEINSTOCK? 2013?5?16????UTC+8??4?55?07???? > Hi, > > ? > > I'm currently writing a C extension module for python using the "raw" C-API. I would like to be able to define "nested classes" like in the following python code > > ? > > ============================================================ > > class A: > > ??? class B: > > ??????? def __init__(self): > > ??????????? self.i = 2 > > ??? def __init__(self): > > ??????? self.b = A.B() > > a = A() > > b = A.B() > > print(a.b.i) > > print(b.i) > > ============================================================ > > ? > > How can I create such nested class with the C-API? > > ? > > Serge WEINSTOCK > > ? > > > > > ___________________________________________________________ > > This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. > > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. The nested class is just defining the scope of the nested class object that can be used solely in all outer proper classes. It is not very useful in a language which supports dynamical run-time attribute binding in the instance level. in the outer From stefan_ml at behnel.de Fri May 17 01:16:25 2013 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 17 May 2013 07:16:25 +0200 Subject: Python C-API: how to define nested classes? In-Reply-To: References: Message-ID: Serge WEINSTOCK, 16.05.2013 10:55: > I'm currently writing a C extension module for python using the "raw" C-API. I would like to be able to define "nested classes" like in the following python code > > ============================================================ > class A: > class B: > def __init__(self): > self.i = 2 > def __init__(self): > self.b = A.B() > a = A() > b = A.B() > print(a.b.i) > print(b.i) > ============================================================ > > How can I create such nested class with the C-API? Assuming you really mean Python classes and not extension types, you might get away with defining them separately and then assigning A.B = B Recent Python versions added a __qualname__ attribute for classes that you might want to fix up in this case. If you need better compatibility, consider writing your code in Cython. Stefan From fabiofz at gmail.com Thu May 16 07:27:03 2013 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Thu, 16 May 2013 08:27:03 -0300 Subject: PyDev 2.7.4 Released Message-ID: Hi All, PyDev 2.7.4 has been released Details on PyDev: http://pydev.org Details on its development: http://pydev.blogspot.com Release Highlights: ------------------------------- * Improved Jython scripting startup time (so, the editor should start up faster). * PyDev no longer causing JSP problem annotation disappear (fix by Danny Ju). * Restored invalidateTextPresentation on save due to issue on annotations kept. * Thank you everyone for helping to keep PyDev going: http://pydev.blogspot.com.br/2013/05/pydev-crowdfunding-finished.html What is PyDev? --------------------------- PyDev is a plugin that enables users to use Eclipse for Python, Jython and IronPython development -- making Eclipse a first class Python IDE -- It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others. Cheers, -- Fabio Zadrozny ------------------------------------------------------ Software Developer PyDev - Python Development Environment for Eclipse http://pydev.org http://pydev.blogspot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From jldunn2000 at gmail.com Thu May 16 11:00:25 2013 From: jldunn2000 at gmail.com (loial) Date: Thu, 16 May 2013 08:00:25 -0700 (PDT) Subject: spilt question Message-ID: I want to split a string so that I always return everything BEFORE the LAST underscore HELLO_xxxxxxxx.lst # should return HELLO HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE I have tried with rsplit but cannot get it to work. Any help appreciated From fabiosantosart at gmail.com Thu May 16 11:14:27 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 16 May 2013 16:14:27 +0100 Subject: spilt question In-Reply-To: References: Message-ID: str.split takes a limit argument. Try your_string.split('_', 1) On 16 May 2013 16:11, "loial" wrote: > I want to split a string so that I always return everything BEFORE the > LAST underscore > > HELLO_xxxxxxxx.lst # should return HELLO > HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE > > I have tried with rsplit but cannot get it to work. > > Any help appreciated > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From walterhurry at lavabit.com Thu May 16 11:10:14 2013 From: walterhurry at lavabit.com (Walter Hurry) Date: Thu, 16 May 2013 15:10:14 +0000 (UTC) Subject: spilt question References: Message-ID: On Thu, 16 May 2013 08:00:25 -0700, loial wrote: > I want to split a string so that I always return everything BEFORE the > LAST underscore > > HELLO_xxxxxxxx.lst # should return HELLO > HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE > > I have tried with rsplit but cannot get it to work. > mystr.rsplit("_",1)[0] From rosuav at gmail.com Thu May 16 11:15:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 May 2013 01:15:39 +1000 Subject: spilt question In-Reply-To: References: Message-ID: On Fri, May 17, 2013 at 1:00 AM, loial wrote: > I want to split a string so that I always return everything BEFORE the LAST underscore > > HELLO_xxxxxxxx.lst # should return HELLO > HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE > > I have tried with rsplit but cannot get it to work. > > Any help appreciated Try with a limit: >>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1) ['HELLO_GOODBYE', 'xxxxxxxx.ls'] >>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)[0] 'HELLO_GOODBYE' You can easily get docs on it: >>> help("".rsplit) Help on built-in function rsplit: rsplit(...) S.rsplit(sep=None, maxsplit=-1) -> list of strings Return a list of the words in S, using sep as the delimiter string, starting at the end of the string and working to the front. If maxsplit is given, at most maxsplit splits are done. If sep is not specified, any whitespace string is a separator. ChrisA From davea at davea.name Thu May 16 11:32:36 2013 From: davea at davea.name (Dave Angel) Date: Thu, 16 May 2013 11:32:36 -0400 Subject: spilt question In-Reply-To: References: Message-ID: <5194FC14.6060808@davea.name> On 05/16/2013 11:15 AM, Chris Angelico wrote: > On Fri, May 17, 2013 at 1:00 AM, loial wrote: >> I want to split a string so that I always return everything BEFORE the LAST underscore >> >> HELLO_xxxxxxxx.lst # should return HELLO >> HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE >> >> I have tried with rsplit but cannot get it to work. >> >> Any help appreciated > > Try with a limit: > >>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1) > ['HELLO_GOODBYE', 'xxxxxxxx.ls'] >>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)[0] > 'HELLO_GOODBYE' > > You can easily get docs on it: > >>>> help("".rsplit) > Help on built-in function rsplit: > > rsplit(...) > S.rsplit(sep=None, maxsplit=-1) -> list of strings > > Return a list of the words in S, using sep as the > delimiter string, starting at the end of the string and > working to the front. If maxsplit is given, at most maxsplit > splits are done. If sep is not specified, any whitespace string > is a separator. > > ChrisA > Now that you've all done his homework for him, see if The OP can spot the one case where that won't work. It's easy to test for, but still important to get right (for some definition of right). -- DaveA From davea at davea.name Thu May 16 11:20:25 2013 From: davea at davea.name (Dave Angel) Date: Thu, 16 May 2013 11:20:25 -0400 Subject: spilt question In-Reply-To: References: Message-ID: <5194F939.8010109@davea.name> On 05/16/2013 11:00 AM, loial wrote: > I want to split a string so that I always return everything BEFORE the LAST underscore > > HELLO_xxxxxxxx.lst # should return HELLO > HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE > > I have tried with rsplit but cannot get it to work. > > Any help appreciated > The rsplit() method was a good idea; it will work. So exactly what did you try and what about it did not work. How are you stuck going from your partial answer to a complete one? While you're at it, you need to consider at least two more cases HELLO.txt #should return "" HELLO_and_____goodbye #should return "HELLO_and____" -- DaveA From python.list at tim.thechases.com Thu May 16 11:23:42 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 16 May 2013 10:23:42 -0500 Subject: spilt question In-Reply-To: References: Message-ID: <20130516102342.694b6f48@bigbox.christie.dr> On 2013-05-16 08:00, loial wrote: > I want to split a string so that I always return everything BEFORE > the LAST underscore > > HELLO_xxxxxxxx.lst # should return HELLO > HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE > > I have tried with rsplit but cannot get it to work. .rsplit takes an optional "how many splits do you want?" parameter that defaults to giving you all of them. Just ask for one right-most split: TESTS = [ ("HELLO_xxxxxxx.lst", "HELLO"), ("HELLO_GOODBYE_xxxxx.ls", "HELLO_GOODBYE"), ] for input, expected in TESTS: result = input.rsplit('_', 1)[0] if result == expected: verdict = "passed" else: verdict = "failed" print "%r -> %r == %r (%s)" % ( input, result, expected, verdict, ) -tkc From ned at nedbatchelder.com Thu May 16 11:22:15 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Thu, 16 May 2013 11:22:15 -0400 Subject: spilt question In-Reply-To: References: Message-ID: <5194F9A7.7070200@nedbatchelder.com> On 5/16/2013 11:00 AM, loial wrote: > I want to split a string so that I always return everything BEFORE the LAST underscore > > HELLO_xxxxxxxx.lst # should return HELLO > HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE > > I have tried with rsplit but cannot get it to work. > > Any help appreciated > >>> "HELLO_GOODBYE_xxxxxxxx.ls".rpartition('_') ('HELLO_GOODBYE', '_', 'xxxxxxxx.ls') >>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit('_', 1) ['HELLO_GOODBYE', 'xxxxxxxx.ls'] >>> "HELLO_GOODBYE_xxxxxxxx.ls".rpartition('_')[0] 'HELLO_GOODBYE' >>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit('_', 1)[0] 'HELLO_GOODBYE' For the future, getting help works better if you show what you tried, and what it produced. --Ned. -------------- next part -------------- An HTML attachment was scrubbed... URL: From visphatesjava at gmail.com Thu May 16 14:14:44 2013 From: visphatesjava at gmail.com (visphatesjava at gmail.com) Date: Thu, 16 May 2013 11:14:44 -0700 (PDT) Subject: so if zodb has no index or search, how run fast query? Message-ID: how? and what package provides such? From wuwei23 at gmail.com Thu May 16 20:08:49 2013 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 May 2013 17:08:49 -0700 (PDT) Subject: so if zodb has no index or search, how run fast query? References: Message-ID: On May 17, 4:14?am, visphatesj... at gmail.com wrote: > how? > > and what package provides such? https://pypi.python.org/pypi/zope.index https://pypi.python.org/pypi/zope.app.catalog I can see how those would be pretty difficult to find... From dieter at handshake.de Fri May 17 02:47:46 2013 From: dieter at handshake.de (dieter) Date: Fri, 17 May 2013 08:47:46 +0200 Subject: so if zodb has no index or search, how run fast query? References: Message-ID: <87k3mym7al.fsf@handshake.de> visphatesjava at gmail.com writes: > ... The only service of the ZODB is storing (persistent) objects persistently and providing (almost) transparent acces to them. Persistent objects have a (usually) implicitely managed attribute which contains the object's "object id" and the ZODB can (quite) efficiently access the objects based on its "object id" (it is using caches and indexes for this). The objects managed by the ZODB form a (directed) graph with a distinguished root node - where all nodes are (persistent) Python objects. You essentially work with those persistent objects as with normal Python objects: the ZODB transparently accesses the persistent storage, when necessary, to give you the feeling as if those objects were normal Python objects. There are a few caveats with respect to modifications - but most modifications work as you would expect for "normal" Python objects (however, you must call "transaction.commit()", in order to persist the modifications). That is essentially all, the ZODB does for you. When you need searches, you must use appropriate datatypes -- in the same way, you would need to do this in a "normal" Python program. The ZODB comes with a collection of datatypes (so called "BTrees") which may form the base for searches. "BTrees" are essentially persistent dicts - however, they are not build from a fixed number of persistent subobjects but the persistent substructure is changed automatically to accommodate efficiently for various tree sizes. Someone else has already mentioned some higher level packages which can provide search capabilities. From visphatesjava at gmail.com Thu May 16 14:15:45 2013 From: visphatesjava at gmail.com (visphatesjava at gmail.com) Date: Thu, 16 May 2013 11:15:45 -0700 (PDT) Subject: wil anyone ressurect medusa and pypersist? Message-ID: <50e6d4bc-ce2f-4c55-97b0-ec0cad793682@googlegroups.com> www.prevayler.org in python = pypersist medusa = python epoll web server and ftp server eventy and async From visphatesjava at gmail.com Thu May 16 14:17:37 2013 From: visphatesjava at gmail.com (visphatesjava at gmail.com) Date: Thu, 16 May 2013 11:17:37 -0700 (PDT) Subject: any cherypy powred sites I can check out? Message-ID: anyone? From rosuav at gmail.com Thu May 16 14:30:54 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 May 2013 04:30:54 +1000 Subject: any cherypy powred sites I can check out? In-Reply-To: References: Message-ID: On Fri, May 17, 2013 at 4:17 AM, wrote: > anyone? > -- > http://mail.python.org/mailman/listinfo/python-list You're firing off a bunch of minimal-content threads that ask other people to do work for you. I recommend you put a bit more thought into your posts, and show that you're willing to do a basic web search before you just ask a question :) Have a read of this: http://www.catb.org/esr/faqs/smart-questions.html ChrisA From walterhurry at lavabit.com Thu May 16 15:11:13 2013 From: walterhurry at lavabit.com (Walter Hurry) Date: Thu, 16 May 2013 19:11:13 +0000 (UTC) Subject: any cherypy powred sites I can check out? References: Message-ID: On Thu, 16 May 2013 11:17:37 -0700, visphatesjava wrote: > anyone? Questions asked in that fashion stand little chance of eliciting helpful responses. From skip at pobox.com Thu May 16 15:24:02 2013 From: skip at pobox.com (Skip Montanaro) Date: Thu, 16 May 2013 14:24:02 -0500 Subject: any cherypy powred sites I can check out? In-Reply-To: References: Message-ID: On Thu, May 16, 2013 at 1:17 PM, wrote: > anyone? Perhaps asking this question (or a more precise one) on a CherryPy mailing list would elicit more (helpful) responses. If you just repeat the above though, I'm afraid most of the answers will just be, "yes." Hint: Read through this before asking the CherryPy folks for help: http://www.catb.org/esr/faqs/smart-questions.html Skip From visphatesjava at gmail.com Thu May 16 20:00:02 2013 From: visphatesjava at gmail.com (visphatesjava at gmail.com) Date: Thu, 16 May 2013 17:00:02 -0700 (PDT) Subject: any cherypy powred sites I can check out? In-Reply-To: References: Message-ID: <17260e56-d23f-4396-92dc-26c37546c35a@googlegroups.com> fuck straight off From breamoreboy at yahoo.co.uk Fri May 17 13:15:38 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 17 May 2013 18:15:38 +0100 Subject: any cherypy powred sites I can check out? In-Reply-To: <17260e56-d23f-4396-92dc-26c37546c35a@googlegroups.com> References: <17260e56-d23f-4396-92dc-26c37546c35a@googlegroups.com> Message-ID: On 17/05/2013 01:00, visphatesjava at gmail.com wrote: > fuck straight off > I assume you're the author of "How to win friends and influence people"? -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From rosuav at gmail.com Fri May 17 13:23:48 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 18 May 2013 03:23:48 +1000 Subject: any cherypy powred sites I can check out? In-Reply-To: References: <17260e56-d23f-4396-92dc-26c37546c35a@googlegroups.com> Message-ID: On Sat, May 18, 2013 at 3:15 AM, Mark Lawrence wrote: > On 17/05/2013 01:00, visphatesjava at gmail.com wrote: >> >> fuck straight off >> > > I assume you're the author of "How to win friends and influence people"? Not quite. He's the author of the social networking version, "How to win influence and friend people". ChrisA From walterhurry at lavabit.com Fri May 17 14:51:01 2013 From: walterhurry at lavabit.com (Walter Hurry) Date: Fri, 17 May 2013 18:51:01 +0000 (UTC) Subject: any cherypy powred sites I can check out? References: <17260e56-d23f-4396-92dc-26c37546c35a@googlegroups.com> Message-ID: On Fri, 17 May 2013 18:15:38 +0100, Mark Lawrence wrote: > On 17/05/2013 01:00, visphatesjava at gmail.com wrote: >> fuck straight off >> >> > I assume you're the author of "How to win friends and influence people"? There are very few posters to this NG in the Hurry bozo bin, but OP is now one. From neilc at norwich.edu Fri May 17 15:27:59 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 17 May 2013 19:27:59 GMT Subject: any cherypy powred sites I can check out? References: <17260e56-d23f-4396-92dc-26c37546c35a@googlegroups.com> Message-ID: On 2013-05-17, Mark Lawrence wrote: > On 17/05/2013 01:00, visphatesjava at gmail.com wrote: >> fuck straight off > > I assume you're the author of "How to win friends and influence > people"? '"I recently wrote a book called "How to Get Along with Everybody." I didn't write it myself--I wrote it some asshole.' --Steve Martin -- Neil Cerutti From kw at codebykevin.com Sun May 19 20:27:02 2013 From: kw at codebykevin.com (Kevin Walzer) Date: Sun, 19 May 2013 20:27:02 -0400 Subject: any cherypy powred sites I can check out? In-Reply-To: References: Message-ID: On 5/16/13 2:30 PM, Chris Angelico wrote: > On Fri, May 17, 2013 at 4:17 AM, wrote: >> anyone? >> -- >> http://mail.python.org/mailman/listinfo/python-list > > You're firing off a bunch of minimal-content threads that ask other > people to do work for you. I recommend you put a bit more thought into > your posts, and show that you're willing to do a basic web search > before you just ask a question :) > He's also known on the Tcl newsgroup as Gavino; a Google search for "gavino tcl" will turn up some interesting hits. I also see that he's posting on comp.lang.perl as "Johannes Falcone." The common thread of his recent postings are subjects posed as questions, usually about various web frameworks, sometimes without even a single line in the message body. On the Tcl list it's AOLServer and NavServer. I'm not familiar with the Perl frameworks he's curious about. --Kevin -- Kevin Walzer Code by Kevin/Mobile Code by Kevin http://www.codebykevin.com http://www.wtmobilesoftware.com From visphatesjava at gmail.com Thu May 16 20:00:31 2013 From: visphatesjava at gmail.com (visphatesjava at gmail.com) Date: Thu, 16 May 2013 17:00:31 -0700 (PDT) Subject: any cherypy powred sites I can check out? In-Reply-To: References: Message-ID: <314b2dd0-2f96-4ce7-bae0-92e8b7d939b3@googlegroups.com> is a cherrypy list accessible here on web thru google groups? From wuwei23 at gmail.com Thu May 16 20:09:38 2013 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 May 2013 17:09:38 -0700 (PDT) Subject: any cherypy powred sites I can check out? References: <314b2dd0-2f96-4ce7-bae0-92e8b7d939b3@googlegroups.com> Message-ID: <86905dc4-31cf-4bec-94dc-847797e54269@ul7g2000pbc.googlegroups.com> On May 17, 10:00?am, visphatesj... at gmail.com wrote: > is a cherrypy list accessible here on web thru google groups? Is an apology for your offensive response to Chris Angelico forthcoming? From rosuav at gmail.com Thu May 16 21:48:19 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 17 May 2013 11:48:19 +1000 Subject: any cherypy powred sites I can check out? In-Reply-To: <86905dc4-31cf-4bec-94dc-847797e54269@ul7g2000pbc.googlegroups.com> References: <314b2dd0-2f96-4ce7-bae0-92e8b7d939b3@googlegroups.com> <86905dc4-31cf-4bec-94dc-847797e54269@ul7g2000pbc.googlegroups.com> Message-ID: On Fri, May 17, 2013 at 10:09 AM, alex23 wrote: > On May 17, 10:00 am, visphatesj... at gmail.com wrote: >> is a cherrypy list accessible here on web thru google groups? > > Is an apology for your offensive response to Chris Angelico > forthcoming? In complete absence of all semblance of context, I do not see the profane post as addressing me personally. But in lieu of an apology, I'd settle for a little more content in the posts.... or some evidence that he's read ESR Question. You know what, I'd settle for the latter in lieu of pretty much anything. I still think we should use that as a penance. Go and say three Our Fathers, do a Stations of the Cross, and read esr's article on smart questions. You are forgiven, my son. ChrisA (Caveat: I am not a Catholic, so I haven't much of a clue as to how confession usually goes.) From walterhurry at lavabit.com Sun May 19 15:46:20 2013 From: walterhurry at lavabit.com (Walter Hurry) Date: Sun, 19 May 2013 19:46:20 +0000 (UTC) Subject: any cherypy powred sites I can check out? References: <314b2dd0-2f96-4ce7-bae0-92e8b7d939b3@googlegroups.com> <86905dc4-31cf-4bec-94dc-847797e54269@ul7g2000pbc.googlegroups.com> Message-ID: On Fri, 17 May 2013 11:48:19 +1000, Chris Angelico wrote: > (Caveat: I am not a Catholic, so I haven't much of a clue as to how > confession usually goes.) Forgive OP Father, for he has sinned... From tunacubes at gmail.com Thu May 16 14:29:25 2013 From: tunacubes at gmail.com (tunacubes at gmail.com) Date: Thu, 16 May 2013 11:29:25 -0700 (PDT) Subject: Number of cells, using CSV module Message-ID: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> I'm using the csv module to get information from a csv file. I have items listed in Column A. I want to know how many items are listed in Column A. import csv with open('test.csv', 'r') as f: reader = csv.reader(f) for column in reader: column = (column[0]) print(column) We are given >a >b >c >d >e >f How would I go about getting the amount of numbers that are printed? If I try printing len(column), I get >1 >1 >1 >1 >1 >1 From skip at pobox.com Thu May 16 14:40:08 2013 From: skip at pobox.com (Skip Montanaro) Date: Thu, 16 May 2013 13:40:08 -0500 Subject: Number of cells, using CSV module In-Reply-To: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> References: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> Message-ID: Perhaps you want len(reader) instead? Or a counter which increments for every row read which has an item in column A? Skip -------------- next part -------------- An HTML attachment was scrubbed... URL: From tunacubes at gmail.com Thu May 16 14:50:42 2013 From: tunacubes at gmail.com (tunacubes at gmail.com) Date: Thu, 16 May 2013 11:50:42 -0700 (PDT) Subject: Number of cells, using CSV module In-Reply-To: References: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> Message-ID: <8662be9c-e50c-48e9-86dc-1838c6188d0c@googlegroups.com> On Thursday, May 16, 2013 2:40:08 PM UTC-4, Skip Montanaro wrote: > Perhaps you want len(reader) instead? ?Or a counter which increments for every row read which has an item in column A? > > > > Skip len(reader) gives me an error. I tried a counter, but unfortunately due to the simplicity of CSV files, any formulas are not saved. From skip at pobox.com Thu May 16 15:07:49 2013 From: skip at pobox.com (Skip Montanaro) Date: Thu, 16 May 2013 14:07:49 -0500 Subject: Number of cells, using CSV module In-Reply-To: <8662be9c-e50c-48e9-86dc-1838c6188d0c@googlegroups.com> References: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> <8662be9c-e50c-48e9-86dc-1838c6188d0c@googlegroups.com> Message-ID: > len(reader) gives me an error. Apologies. len(list(reader)) should work. Of course, you'll wind up loading the entire CSV file into memory. You might want to just count row-by-row: n = 0 for row in reader: n += 1 Skip From python.list at tim.thechases.com Thu May 16 16:00:21 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 16 May 2013 15:00:21 -0500 Subject: Number of cells, using CSV module In-Reply-To: References: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> <8662be9c-e50c-48e9-86dc-1838c6188d0c@googlegroups.com> Message-ID: <20130516150021.7d467219@bigbox.christie.dr> On 2013-05-16 14:07, Skip Montanaro wrote: > > len(reader) gives me an error. > > Apologies. len(list(reader)) should work. Of course, you'll wind > up loading the entire CSV file into memory. You might want to just > count row-by-row: > > n = 0 > for row in reader: > n += 1 which can nicely be rewritten as n = sum(1 for row in reader) :-) -tkc From tunacubes at gmail.com Thu May 16 14:53:23 2013 From: tunacubes at gmail.com (tunacubes at gmail.com) Date: Thu, 16 May 2013 11:53:23 -0700 (PDT) Subject: Number of cells, using CSV module In-Reply-To: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> References: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> Message-ID: <2cd2fe5b-a242-442e-a3fa-e0c5588a370a@googlegroups.com> I guess another way to accomplish this would be, is there any way that I can turn the returned value for (column) into 1 list? So rather than >a >b >c >d >e >f I would get [a, b, c, d, e, f] From skip at pobox.com Thu May 16 15:08:29 2013 From: skip at pobox.com (Skip Montanaro) Date: Thu, 16 May 2013 14:08:29 -0500 Subject: Number of cells, using CSV module In-Reply-To: <2cd2fe5b-a242-442e-a3fa-e0c5588a370a@googlegroups.com> References: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> <2cd2fe5b-a242-442e-a3fa-e0c5588a370a@googlegroups.com> Message-ID: > So rather than >>a >>b >>c >>d >>e >>f > I would get [a, b, c, d, e, f] all_items = [] for row in reader: all_items.append(row[0]) Skip From python.list at tim.thechases.com Thu May 16 16:01:08 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 16 May 2013 15:01:08 -0500 Subject: Number of cells, using CSV module In-Reply-To: References: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> <2cd2fe5b-a242-442e-a3fa-e0c5588a370a@googlegroups.com> Message-ID: <20130516150108.2792c850@bigbox.christie.dr> On 2013-05-16 14:08, Skip Montanaro wrote: > > So rather than > >>a > >>b > >>c > >>d > >>e > >>f > > I would get [a, b, c, d, e, f] > > all_items = [] > for row in reader: > all_items.append(row[0]) And following up here, this could be tidily rewritten as all_items = [row[0] for row in reader] -tkc From tunacubes at gmail.com Thu May 16 16:11:50 2013 From: tunacubes at gmail.com (tunacubes at gmail.com) Date: Thu, 16 May 2013 13:11:50 -0700 (PDT) Subject: Number of cells, using CSV module In-Reply-To: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> References: <5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com> Message-ID: <28454e0b-b14d-4a71-9ac4-38e7397f44d7@googlegroups.com> Thank you Skip, worked great. And thank you Tim for Tidying things up! From chitrankdixit at gmail.com Thu May 16 16:05:49 2013 From: chitrankdixit at gmail.com (Chitrank Dixit) Date: Fri, 17 May 2013 01:35:49 +0530 Subject: How to use relative Import Message-ID: Hello Python developers I want to know how relative imports are different than import. I have found lots of examples on internet explaining about the relative import but I want to know about the basic aspects of relative import that make it different than import. *Regards * *Chitrank Dixit * *IIPS-DAVV * *Indore (M.P.) , India * *MCA * *trackleech.blogspot.in* -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Thu May 16 19:39:34 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 May 2013 23:39:34 GMT Subject: How to use relative Import References: Message-ID: <51956e35$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 17 May 2013 01:35:49 +0530, Chitrank Dixit wrote: > I want to know how relative imports are different than import. I have > found lots of examples on internet explaining about the relative import > but I want to know about the basic aspects of relative import that make > it different than import. Relative imports search the directory containing the current package, absolute imports search the entire contents of sys.path. That is the only difference. The purpose of relative imports is to make it easy for modules inside a package to import each other without worrying about other modules *outside* the package being imported by mistake. You can read the PEP here: http://www.python.org/dev/peps/pep-0328/ to learn about the motivation for absolute and relative imports. Optional in Python 2.5 and 2.6, and compulsory in 2.7, when you write: import moduleX Python looks at sys.path for a list of directories to search for "moduleX.py". This is an absolute import. But consider a package with this structure: package/ +-- __init__.py +-- moduleA.py +-- moduleB.py +-- moduleX.py +-- subpackage/ +-- __init__.py +-- moduleY.py +-- moduleZ.py Inside moduleA, if you write "import moduleX" it will be an absolute import, the entire sys.path will be searched, and the "top level" module.X will be found. But if you write this instead: from . import moduleX you have a relative import, and Python will only search the current package directory, so it will find the moduleX inside the package. You can also write: from .subpackage import moduleZ to find the moduleZ inside the subpackage, again without searching the entire sys.path. Inside moduleY in the subpackage, all of these will find the same moduleZ: # Relative to the current module: from . import moduleZ # Relative to the parent of the current module: from ..subpackage import moduleZ from .. import subpackage.moduleZ # Absolute: import package.subpackage.moduleZ -- Steven From lokeshkoppaka at gmail.com Thu May 16 23:20:26 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Thu, 16 May 2013 20:20:26 -0700 (PDT) Subject: How to write fast into a file in python? Message-ID: I need to write numbers into a file upto 50mb and it should be fast can any one help me how to do that? i had written the following code.. ----------------------------------------------------------------------------------------------------------- def create_file_numbers_old(filename, size): start = time.clock() value = 0 with open(filename, "w") as f: while f.tell()< size: f.write("{0}\n".format(value)) value += 1 end = time.clock() print "time taken to write a file of size", size, " is ", (end -start), "seconds \n" ------------------------------------------------------------------------------------------------------------------ it takes about 20sec i need 5 to 10 times less than that. From steve+comp.lang.python at pearwood.info Thu May 16 23:51:21 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 May 2013 03:51:21 GMT Subject: How to write fast into a file in python? References: Message-ID: <5195a939$0$29997$c3e8da3$5496439d@news.astraweb.com> On Thu, 16 May 2013 20:20:26 -0700, lokeshkoppaka wrote: > I need to write numbers into a file upto 50mb and it should be fast can > any one help me how to do that? > i had written the following code.. > ---------------------------------------------------------------------- > def create_file_numbers_old(filename, size): start = time.clock() > > value = 0 > with open(filename, "w") as f: > while f.tell()< size: > f.write("{0}\n".format(value)) > value += 1 > > end = time.clock() > > print "time taken to write a file of size", size, " is ", (end -start), > "seconds \n" > -------------------------------------------------------------------- > it takes about 20sec i need 5 to 10 times less than that. 20 seconds to write how many numbers? If you are doing create_file_numbers_old(filename, 5) then 20 seconds is really slow. But if you are doing: create_file_numbers_old(filename, 50000000000000) then 20 seconds is amazingly fast. Try this instead, it may be a little faster: def create_file_numbers_old(filename, size): count = value = 0 with open(filename, 'w') as f: while count < size: s = '%d\n' % value f.write(s) count += len(s) value += 1 If this is still too slow, you can try three other tactics: 1) pre-calculate the largest integer that will fit in `size` bytes, then use a for-loop instead of a while loop: maxn = calculation(...) with open(filename, 'w') as f: for i in xrange(maxn): f.write('%d\n' % i) 2) Write an extension module in C that writes to the file. 3) Get a faster hard drive, and avoid writing over a network. -- Steven From lokeshkoppaka at gmail.com Fri May 17 00:35:15 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Thu, 16 May 2013 21:35:15 -0700 (PDT) Subject: How to write fast into a file in python? In-Reply-To: References: Message-ID: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> On Friday, May 17, 2013 8:50:26 AM UTC+5:30, lokesh... at gmail.com wrote: > I need to write numbers into a file upto 50mb and it should be fast > > can any one help me how to do that? > > i had written the following code.. > > ----------------------------------------------------------------------------------------------------------- > > def create_file_numbers_old(filename, size): > > start = time.clock() > > > > value = 0 > > with open(filename, "w") as f: > > while f.tell()< size: > > f.write("{0}\n".format(value)) > > value += 1 > > > > end = time.clock() > > > > print "time taken to write a file of size", size, " is ", (end -start), "seconds \n" > > ------------------------------------------------------------------------------------------------------------------ > > it takes about 20sec i need 5 to 10 times less than that. size = 50mb From davea at davea.name Fri May 17 07:58:43 2013 From: davea at davea.name (Dave Angel) Date: Fri, 17 May 2013 07:58:43 -0400 Subject: How to write fast into a file in python? In-Reply-To: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> Message-ID: <51961B73.2070401@davea.name> On 05/17/2013 12:35 AM, lokeshkoppaka at gmail.com wrote: > On Friday, May 17, 2013 8:50:26 AM UTC+5:30, lokesh... at gmail.com wrote: >> I need to write numbers into a file upto 50mb and it should be fast >> >> can any one help me how to do that? >> >> i had written the following code.. >> >> >> value = 0 >> >> with open(filename, "w") as f: >> >> while f.tell()< size: >> >> f.write("{0}\n".format(value)) >> If you must use googlegroups, at least read this http://wiki.python.org/moin/GoogleGroupsPython. >> >> >> it takes about 20sec i need 5 to 10 times less than that. > size = 50mb > Most of the time is spent figuring out whether the file has reached its limit size. If you want Python to go fast, just specify the data. On my Linux system, it takes 11 seconds to write the first 6338888 values, which is just under 50mb. If I write the obvious loop, writing that many values takes .25 seconds. -- DaveA From carlosnepomuceno at outlook.com Fri May 17 11:20:33 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 17 May 2013 18:20:33 +0300 Subject: How to write fast into a file in python? In-Reply-To: <51961B73.2070401@davea.name> References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, <51961B73.2070401@davea.name> Message-ID: I've got the following results on my desktop PC (Win7/Python2.7.5): C:\src\Python>python -m timeit -cvn3 -r3 "execfile('fastwrite2.py')" raw times: 123 126 125 3 loops, best of 3: 41 sec per loop C:\src\Python>python -m timeit -cvn3 -r3 "execfile('fastwrite5.py')" raw times: 34 34.3 34 3 loops, best of 3: 11.3 sec per loop C:\src\Python>python -m timeit -cvn3 -r3 "execfile('fastwrite6.py')" raw times: 0.4 0.447 0.391 3 loops, best of 3: 130 msec per loop If you can just copy a preexisting file it will surely increase the speed to the levels you need, but doing the cStringIO operations can reduce the time in 72%. Strangely I just realised that the time it takes to complete such scripts is the same no matter what hard drive I choose to run them. The results are the same for an SSD (main drive) and a HDD. I think it's very strange to take 11.3s to write 50MB (4.4MB/s) sequentially on a SSD which is capable of 140MB/s. Is that a Python problem? Why does it take the same time on the HDD? ### fastwrite2.py ###? <<< this is your code size = 50*1024*1024 value = 0 filename = 'fastwrite2.dat' with open(filename, "w") as f: ??? while f.tell()< size: ??????? f.write("{0}\n".format(value)) ??????? value += 1 ??? f.close() ### fastwrite5.py ### import cStringIO size = 50*1024*1024 value = 0 filename = 'fastwrite5.dat' x = 0 b = cStringIO.StringIO() while x < size: ??? line = '{0}\n'.format(value) ??? b.write(line) ??? value += 1 ??? x += len(line)+1 f = open(filename, 'w') f.write(b.getvalue()) f.close() b.close() ### fastwrite6.py ### import shutil src = 'fastwrite.dat' dst = 'fastwrite6.dat' shutil.copyfile(src, dst) ---------------------------------------- > Date: Fri, 17 May 2013 07:58:43 -0400 > From: davea at davea.name > To: python-list at python.org > Subject: Re: How to write fast into a file in python? > > On 05/17/2013 12:35 AM, lokeshkoppaka at gmail.com wrote: >> On Friday, May 17, 2013 8:50:26 AM UTC+5:30, lokesh... at gmail.com wrote: >>> I need to write numbers into a file upto 50mb and it should be fast >>> >>> can any one help me how to do that? >>> >>> i had written the following code.. >>> >>> >>> value = 0 >>> >>> with open(filename, "w") as f: >>> >>> while f.tell()< size: >>> >>> f.write("{0}\n".format(value)) >>> > If you must use googlegroups, at least read this > http://wiki.python.org/moin/GoogleGroupsPython. >>> >>> >>> it takes about 20sec i need 5 to 10 times less than that. >> size = 50mb >> > > Most of the time is spent figuring out whether the file has reached its > limit size. If you want Python to go fast, just specify the data. On > my Linux system, it takes 11 seconds to write the first 6338888 values, > which is just under 50mb. If I write the obvious loop, writing that > many values takes .25 seconds. > > -- > DaveA > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Fri May 17 12:42:55 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 May 2013 16:42:55 GMT Subject: How to write fast into a file in python? References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> Message-ID: <51965e0e$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 17 May 2013 18:20:33 +0300, Carlos Nepomuceno wrote: > I've got the following results on my desktop PC (Win7/Python2.7.5): > > C:\src\Python>python -m timeit -cvn3 -r3 "execfile('fastwrite2.py')" raw > times: 123 126 125 > 3 loops, best of 3: 41 sec per loop Your times here are increased significantly by using execfile. Using execfile means that instead of compiling the code once, then executing many times, it gets compiled over and over and over and over again. In my experience, using exec, execfile or eval makes your code ten or twenty times slower: [steve at ando ~]$ python -m timeit 'x = 100; y = x/3' 1000000 loops, best of 3: 0.175 usec per loop [steve at ando ~]$ python -m timeit 'exec("x = 100; y = x/3")' 10000 loops, best of 3: 37.8 usec per loop > Strangely I just realised that the time it takes to complete such > scripts is the same no matter what hard drive I choose to run them. The > results are the same for an SSD (main drive) and a HDD. There's nothing strange here. The time you measure is dominated by three things, in reducing order of importance: * the poor choice of execfile dominates the time taken; * followed by choice of algorithm; * followed by the time it actually takes to write to the disk, which is probably insignificant compared to the other two, regardless of whether you are using a HDD or SSD. Until you optimize the code, optimizing the media is a waste of time. > I think it's very strange to take 11.3s to write 50MB (4.4MB/s) > sequentially on a SSD which is capable of 140MB/s. It doesn't. It takes 11.3 seconds to open a file, read it into memory, parse it, compile it into byte-code, and only then execute it. My prediction is that the call to f.write() and f.close() probably take a fraction of a second, and nearly all of the rest of the time is taken by other calculations. -- Steven From carlosnepomuceno at outlook.com Fri May 17 13:25:37 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 17 May 2013 20:25:37 +0300 Subject: How to write fast into a file in python? In-Reply-To: <51965e0e$0$29997$c3e8da3$5496439d@news.astraweb.com> References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, <51961B73.2070401@davea.name>, , <51965e0e$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: Thank you Steve! You are totally right! It takes about 0.2s for the f.write() to return. Certainly because it writes to the system file cache (~250MB/s). Using a little bit different approach I've got: C:\src\Python>python -m timeit -cvn3 -r3 -s"from fastwrite5r import run" "run()" raw times: 24 25.1 24.4 3 loops, best of 3: 8 sec per loop ??? This time it took 8s to complete from previous 11.3s. Does those 3.3s are the time to "open, read, parse, compile" steps you told me? If so, the execute step is really taking 8s, right? Why does it take so long to build the string to be written? Can it get faster? Thanks in advance! ### fastwrite5r.py ### def run(): ??? import cStringIO ??? size = 50*1024*1024 ??? value = 0 ??? filename = 'fastwrite5.dat' ??? x = 0 ??? b = cStringIO.StringIO() ??? while x < size: ??????? line = '{0}\n'.format(value) ??????? b.write(line) ??????? value += 1 ??????? x += len(line)+1 ??? f = open(filename, 'w') ??? f.write(b.getvalue()) ??? f.close() ??? b.close() if __name__ == '__main__': ??? run() ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: How to write fast into a file in python? > Date: Fri, 17 May 2013 16:42:55 +0000 > To: python-list at python.org > > On Fri, 17 May 2013 18:20:33 +0300, Carlos Nepomuceno wrote: > >> I've got the following results on my desktop PC (Win7/Python2.7.5): >> >> C:\src\Python>python -m timeit -cvn3 -r3 "execfile('fastwrite2.py')" raw >> times: 123 126 125 >> 3 loops, best of 3: 41 sec per loop > > Your times here are increased significantly by using execfile. Using > execfile means that instead of compiling the code once, then executing > many times, it gets compiled over and over and over and over again. In my > experience, using exec, execfile or eval makes your code ten or twenty > times slower: > > [steve at ando ~]$ python -m timeit 'x = 100; y = x/3' > 1000000 loops, best of 3: 0.175 usec per loop > [steve at ando ~]$ python -m timeit 'exec("x = 100; y = x/3")' > 10000 loops, best of 3: 37.8 usec per loop > > >> Strangely I just realised that the time it takes to complete such >> scripts is the same no matter what hard drive I choose to run them. The >> results are the same for an SSD (main drive) and a HDD. > > There's nothing strange here. The time you measure is dominated by three > things, in reducing order of importance: > > * the poor choice of execfile dominates the time taken; > > * followed by choice of algorithm; > > * followed by the time it actually takes to write to the disk, which is > probably insignificant compared to the other two, regardless of whether > you are using a HDD or SSD. > > Until you optimize the code, optimizing the media is a waste of time. > > >> I think it's very strange to take 11.3s to write 50MB (4.4MB/s) >> sequentially on a SSD which is capable of 140MB/s. > > It doesn't. It takes 11.3 seconds to open a file, read it into memory, > parse it, compile it into byte-code, and only then execute it. My > prediction is that the call to f.write() and f.close() probably take a > fraction of a second, and nearly all of the rest of the time is taken by > other calculations. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Fri May 17 13:47:02 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 May 2013 17:47:02 GMT Subject: How to write fast into a file in python? References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> Message-ID: <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 17 May 2013 18:20:33 +0300, Carlos Nepomuceno wrote: > ### fastwrite5.py ### > import cStringIO > size = 50*1024*1024 > value = 0 > filename = 'fastwrite5.dat' > x = 0 > b = cStringIO.StringIO() > while x < size: > ??? line = '{0}\n'.format(value) > ??? b.write(line) > ??? value += 1 > ??? x += len(line)+1 Oh, I forgot to mention: you have a bug in this function. You're already including the newline in the len(line), so there is no need to add one. The result is that you only generate 44MB instead of 50MB. > f = open(filename, 'w') > f.write(b.getvalue()) > f.close() > b.close() Here are the results of profiling the above on my computer. Including the overhead of the profiler, it takes just over 50 seconds to run your file on my computer. [steve at ando ~]$ python -m cProfile fastwrite5.py 17846645 function calls in 53.575 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 30.561 30.561 53.575 53.575 fastwrite5.py:1() 1 0.000 0.000 0.000 0.000 {cStringIO.StringIO} 5948879 5.582 0.000 5.582 0.000 {len} 1 0.004 0.004 0.004 0.004 {method 'close' of 'cStringIO.StringO' objects} 1 0.000 0.000 0.000 0.000 {method 'close' of 'file' objects} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 5948879 9.979 0.000 9.979 0.000 {method 'format' of 'str' objects} 1 0.103 0.103 0.103 0.103 {method 'getvalue' of 'cStringIO.StringO' objects} 5948879 7.135 0.000 7.135 0.000 {method 'write' of 'cStringIO.StringO' objects} 1 0.211 0.211 0.211 0.211 {method 'write' of 'file' objects} 1 0.000 0.000 0.000 0.000 {open} As you can see, the time is dominated by repeatedly calling len(), str.format() and StringIO.write() methods. Actually writing the data to the file is quite a small percentage of the cumulative time. So, here's another version, this time using a pre-calculated limit. I cheated and just copied the result from the fastwrite5 output :-) # fasterwrite.py filename = 'fasterwrite.dat' with open(filename, 'w') as f: for i in xrange(5948879): # Actually only 44MB, not 50MB. f.write('%d\n' % i) And the profile results are about twice as fast as fastwrite5 above, with only 8 seconds in total writing to my HDD. [steve at ando ~]$ python -m cProfile fasterwrite.py 5948882 function calls in 28.840 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 20.592 20.592 28.840 28.840 fasterwrite.py:1() 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 5948879 8.229 0.000 8.229 0.000 {method 'write' of 'file' objects} 1 0.019 0.019 0.019 0.019 {open} Without the overhead of the profiler, it is a little faster: [steve at ando ~]$ time python fasterwrite.py real 0m16.187s user 0m13.553s sys 0m0.508s Although it is still slower than the heavily optimized dd command, but not unreasonably slow for a high-level language: [steve at ando ~]$ time dd if=fasterwrite.dat of=copy.dat 90781+1 records in 90781+1 records out 46479922 bytes (46 MB) copied, 0.737009 seconds, 63.1 MB/s real 0m0.786s user 0m0.071s sys 0m0.595s -- Steven From carlosnepomuceno at outlook.com Fri May 17 14:18:15 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 17 May 2013 21:18:15 +0300 Subject: How to write fast into a file in python? In-Reply-To: <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, <51961B73.2070401@davea.name>, , <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: You've hit the bullseye! ;) Thanks a lot!!! > Oh, I forgot to mention: you have a bug in this function. You're already > including the newline in the len(line), so there is no need to add one. > The result is that you only generate 44MB instead of 50MB. That's because I'm running on Windows. What's the fastest way to check if '\n' translates to 2 bytes on file? > Here are the results of profiling the above on my computer. Including the > overhead of the profiler, it takes just over 50 seconds to run your file > on my computer. > > [steve at ando ~]$ python -m cProfile fastwrite5.py > 17846645 function calls in 53.575 seconds > Didn't know the cProfile module.Thanks a lot! > Ordered by: standard name > > ncalls tottime percall cumtime percall filename:lineno(function) > 1 30.561 30.561 53.575 53.575 fastwrite5.py:1() > 1 0.000 0.000 0.000 0.000 {cStringIO.StringIO} > 5948879 5.582 0.000 5.582 0.000 {len} > 1 0.004 0.004 0.004 0.004 {method 'close' of 'cStringIO.StringO' objects} > 1 0.000 0.000 0.000 0.000 {method 'close' of 'file' objects} > 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} > 5948879 9.979 0.000 9.979 0.000 {method 'format' of 'str' objects} > 1 0.103 0.103 0.103 0.103 {method 'getvalue' of 'cStringIO.StringO' objects} > 5948879 7.135 0.000 7.135 0.000 {method 'write' of 'cStringIO.StringO' objects} > 1 0.211 0.211 0.211 0.211 {method 'write' of 'file' objects} > 1 0.000 0.000 0.000 0.000 {open} > > > As you can see, the time is dominated by repeatedly calling len(), > str.format() and StringIO.write() methods. Actually writing the data to > the file is quite a small percentage of the cumulative time. > > So, here's another version, this time using a pre-calculated limit. I > cheated and just copied the result from the fastwrite5 output :-) > > # fasterwrite.py > filename = 'fasterwrite.dat' > with open(filename, 'w') as f: > for i in xrange(5948879): # Actually only 44MB, not 50MB. > f.write('%d\n' % i) > I had the same idea but kept the original method because I didn't want to waste time creating a function for calculating the actual number of iterations needed to deliver 50MB of data. ;) > And the profile results are about twice as fast as fastwrite5 above, with > only 8 seconds in total writing to my HDD. > > [steve at ando ~]$ python -m cProfile fasterwrite.py > 5948882 function calls in 28.840 seconds > > Ordered by: standard name > > ncalls tottime percall cumtime percall filename:lineno(function) > 1 20.592 20.592 28.840 28.840 fasterwrite.py:1() > 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} > 5948879 8.229 0.000 8.229 0.000 {method 'write' of 'file' objects} > 1 0.019 0.019 0.019 0.019 {open} > I thought there would be a call to format method by "'%d\n' % i". It seems the % operator is a lot faster than format. I just stopped using it because I read it was going to be deprecated. :( Why replace such a great and fast operator by a slow method? I mean, why format is been preferred over %? > Without the overhead of the profiler, it is a little faster: > > [steve at ando ~]$ time python fasterwrite.py > > real 0m16.187s > user 0m13.553s > sys 0m0.508s > > > Although it is still slower than the heavily optimized dd command, > but not unreasonably slow for a high-level language: > > [steve at ando ~]$ time dd if=fasterwrite.dat of=copy.dat > 90781+1 records in > 90781+1 records out > 46479922 bytes (46 MB) copied, 0.737009 seconds, 63.1 MB/s > > real 0m0.786s > user 0m0.071s > sys 0m0.595s > > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From carlosnepomuceno at outlook.com Fri May 17 14:33:23 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 17 May 2013 21:33:23 +0300 Subject: How to write fast into a file in python? In-Reply-To: <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, <51961B73.2070401@davea.name>, , <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: Think the following update will make the code more portable: x += len(line)+len(os.linesep)-1 Not sure if it's the fastest way to achieve that. :/ > On Fri, 17 May 2013 18:20:33 +0300, Carlos Nepomuceno wrote: > >> ### fastwrite5.py ### >> import cStringIO >> size = 50*1024*1024 >> value = 0 >> filename = 'fastwrite5.dat' >> x = 0 >> b = cStringIO.StringIO() >> while x < size: >> line = '{0}\n'.format(value) >> b.write(line) >> value += 1 >> x += len(line)+1 > > Oh, I forgot to mention: you have a bug in this function. You're already > including the newline in the len(line), so there is no need to add one. > The result is that you only generate 44MB instead of 50MB. From fabiosantosart at gmail.com Sat May 18 03:49:55 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sat, 18 May 2013 08:49:55 +0100 Subject: How to write fast into a file in python? In-Reply-To: References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 17 May 2013 19:38, "Carlos Nepomuceno" wrote: > > Think the following update will make the code more portable: > > x += len(line)+len(os.linesep)-1 > > Not sure if it's the fastest way to achieve that. :/ > Putting len(os.linesep)'s value into a local variable will make accessing it quite a bit faster. But why would you want to do that? You mentioned "\n" translating to two lines, but this won't happen. Windows will not mess with what you write to your file. It's just that traditionally windows and windows programs use \r\n instead of just \n. I think it was for compatibility with os/2 or macintosh (I don't remember which), which used \r for newlines. You don't have to follow this convention. If you open a \n-separated file with *any* text editor other than notepad, your newlines will be okay. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sat May 18 10:29:50 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 19 May 2013 00:29:50 +1000 Subject: How to write fast into a file in python? In-Reply-To: References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, May 18, 2013 at 5:49 PM, F?bio Santos wrote: > Putting len(os.linesep)'s value into a local variable will make accessing it > quite a bit faster. But why would you want to do that? > > You mentioned "\n" translating to two lines, but this won't happen. Windows > will not mess with what you write to your file. It's just that traditionally > windows and windows programs use \r\n instead of just \n. I think it was for > compatibility with os/2 or macintosh (I don't remember which), which used \r > for newlines. > > You don't have to follow this convention. If you open a \n-separated file > with *any* text editor other than notepad, your newlines will be okay. Into two characters, not two lines, but yes. A file opened in text mode on Windows will have its lines terminated with two characters. (And it's old Macs that used to use \r. OS/2 follows the DOS convention of \r\n, but again, many apps these days are happy with Unix newlines there too.) ChrisA From carlosnepomuceno at outlook.com Sat May 18 13:00:06 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 18 May 2013 20:00:06 +0300 Subject: How to write fast into a file in python? In-Reply-To: References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, <51961B73.2070401@davea.name>, , <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com>, , Message-ID: Python really writes '\n\r' on Windows. Just check the files. Internal representations only keep '\n' for simplicity, but if you wanna keep track of the file length you have to take that into account. ;) ________________________________ > Date: Sat, 18 May 2013 08:49:55 +0100 > Subject: RE: How to write fast into a file in python? > From: fabiosantosart at gmail.com > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > > > On 17 May 2013 19:38, "Carlos Nepomuceno" > > > wrote: > > > > Think the following update will make the code more portable: > > > > x += len(line)+len(os.linesep)-1 > > > > Not sure if it's the fastest way to achieve that. :/ > > > > Putting len(os.linesep)'s value into a local variable will make > accessing it quite a bit faster. But why would you want to do that? > > You mentioned "\n" translating to two lines, but this won't happen. > Windows will not mess with what you write to your file. It's just that > traditionally windows and windows programs use \r\n instead of just \n. > I think it was for compatibility with os/2 or macintosh (I don't > remember which), which used \r for newlines. > > You don't have to follow this convention. If you open a \n-separated > file with *any* text editor other than notepad, your newlines will be > okay. From davea at davea.name Sat May 18 22:41:32 2013 From: davea at davea.name (Dave Angel) Date: Sat, 18 May 2013 22:41:32 -0400 Subject: How to write fast into a file in python? In-Reply-To: References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, <51961B73.2070401@davea.name>, , <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com>, , Message-ID: <51983BDC.1060203@davea.name> On 05/18/2013 01:00 PM, Carlos Nepomuceno wrote: > Python really writes '\n\r' on Windows. Just check the files. That's backwards. '\r\n' on Windows, IF you omit the b in the mode when creating the file. -- DaveA From carlosnepomuceno at outlook.com Sat May 18 23:53:02 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 19 May 2013 06:53:02 +0300 Subject: How to write fast into a file in python? In-Reply-To: <51983BDC.1060203@davea.name> References: , , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, , <51961B73.2070401@davea.name>, , , , <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com>, , , , , , <51983BDC.1060203@davea.name> Message-ID: ---------------------------------------- > Date: Sat, 18 May 2013 22:41:32 -0400 > From: davea at davea.name > To: python-list at python.org > Subject: Re: How to write fast into a file in python? > > On 05/18/2013 01:00 PM, Carlos Nepomuceno wrote: >> Python really writes '\n\r' on Windows. Just check the files. > > That's backwards. '\r\n' on Windows, IF you omit the b in the mode when > creating the file. Indeed! My mistake just made me find out that Acorn used that inversion on Acorn MOS. According to this[1] (at page 449) the OSNEWL routine outputs '\n\r'. What the hell those guys were thinking??? :p "OSNEWL This call issues an LF CR (line feed, carriage return) to the currently selected output stream. The routine is entered at &FFE7." [1] http://regregex.bbcmicro.net/BPlusUserGuide-1.07.pdf From python at mrabarnett.plus.com Sun May 19 11:44:55 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 May 2013 16:44:55 +0100 Subject: How to write fast into a file in python? In-Reply-To: References: , , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, , <51961B73.2070401@davea.name>, , , , <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com>, , , , , , <51983BDC.1060203@davea.name> Message-ID: <5198F377.1050203@mrabarnett.plus.com> On 19/05/2013 04:53, Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Sat, 18 May 2013 22:41:32 -0400 >> From: davea at davea.name >> To: python-list at python.org >> Subject: Re: How to write fast into a file in python? >> >> On 05/18/2013 01:00 PM, Carlos Nepomuceno wrote: >>> Python really writes '\n\r' on Windows. Just check the files. >> >> That's backwards. '\r\n' on Windows, IF you omit the b in the mode when >> creating the file. > > Indeed! My mistake just made me find out that Acorn used that inversion on Acorn MOS. > > According to this[1] (at page 449) the OSNEWL routine outputs '\n\r'. > > What the hell those guys were thinking??? :p > Doing it that way saved a few bytes. Code was something like this: FFE3 .OSASCI CMP #&0D FFE5 BNE OSWRCH FFE7 .OSNEWL LDA #&0A FFE9 JSR OSWRCH FFEC LDA #&0D FFEE .OSWRCH ... This means that the contents of the accumulator would always be preserved by a call to OSASCI. > "OSNEWL > This call issues an LF CR (line feed, carriage return) to the currently selected > output stream. The routine is entered at &FFE7." > > [1] http://regregex.bbcmicro.net/BPlusUserGuide-1.07.pdf > From carlosnepomuceno at outlook.com Mon May 20 06:34:56 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 20 May 2013 13:34:56 +0300 Subject: How to write fast into a file in python? In-Reply-To: <5198F377.1050203@mrabarnett.plus.com> References: , ,,<87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, ,,<51961B73.2070401@davea.name>, ,,, ,,<51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com>, ,,, , , , , , , <51983BDC.1060203@davea.name>, , <5198F377.1050203@mrabarnett.plus.com> Message-ID: Oh well! Just got a flashback from the old times at the 8-bit assembly line. Dirty deeds done dirt cheap! lol ---------------------------------------- > Date: Sun, 19 May 2013 16:44:55 +0100 > From: python at mrabarnett.plus.com > To: python-list at python.org > Subject: Re: How to write fast into a file in python? > > On 19/05/2013 04:53, Carlos Nepomuceno wrote: >> ---------------------------------------- >>> Date: Sat, 18 May 2013 22:41:32 -0400 >>> From: davea at davea.name >>> To: python-list at python.org >>> Subject: Re: How to write fast into a file in python? >>> >>> On 05/18/2013 01:00 PM, Carlos Nepomuceno wrote: >>>> Python really writes '\n\r' on Windows. Just check the files. >>> >>> That's backwards. '\r\n' on Windows, IF you omit the b in the mode when >>> creating the file. >> >> Indeed! My mistake just made me find out that Acorn used that inversion on Acorn MOS. >> >> According to this[1] (at page 449) the OSNEWL routine outputs '\n\r'. >> >> What the hell those guys were thinking??? :p >> > Doing it that way saved a few bytes. > > Code was something like this: > > FFE3 .OSASCI CMP #&0D > FFE5 BNE OSWRCH > FFE7 .OSNEWL LDA #&0A > FFE9 JSR OSWRCH > FFEC LDA #&0D > FFEE .OSWRCH ... > > This means that the contents of the accumulator would always be > preserved by a call to OSASCI. > >> "OSNEWL >> This call issues an LF CR (line feed, carriage return) to the currently selected >> output stream. The routine is entered at &FFE7." >> >> [1] http://regregex.bbcmicro.net/BPlusUserGuide-1.07.pdf >> > > -- > http://mail.python.org/mailman/listinfo/python-list From roy at panix.com Sat May 18 15:37:06 2013 From: roy at panix.com (Roy Smith) Date: Sat, 18 May 2013 15:37:06 -0400 Subject: How to write fast into a file in python? References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Dennis Lee Bieber wrote: > tOn Sat, 18 May 2013 08:49:55 +0100, F?bio Santos > declaimed the following in > gmane.comp.python.general: > > > > You mentioned "\n" translating to two lines, but this won't happen. Windows > > will not mess with what you write to your file. It's just that > > traditionally windows and windows programs use \r\n instead of just \n. I > > think it was for compatibility with os/2 or macintosh (I don't remember > > which), which used \r for newlines. > > > Neither... It goes back to Teletype machines where one sent a > carriage return to move the printhead back to the left, then sent a line > feed to advance the paper (while the head was still moving left), and in > some cases also provided a rub-out character (a do-nothing) to add an > additional character time delay. The delay was important. It took more than one character time for the print head to get back to the left margin. If you kept sending printable characters while the print head was still flying back, they would get printed in the middle of the line (perhaps blurred a little). There was also a dashpot which cushioned the head assembly when it reached the left margin. Depending on how well adjusted things were this might take another character time or two to fully settle down. You can still see the remnants of this in modern Unix systems: $ stty -a speed 9600 baud; rows 40; columns 136; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe -echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke The "nl0" and "cr0" mean it's configured to insert 0 delay after newlines and carriage returns. Whether setting a non-zero delay actually does anything useful anymore is an open question, but the drivers still accept the settings. From steve+comp.lang.python at pearwood.info Sat May 18 18:23:31 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 May 2013 22:23:31 GMT Subject: How to write fast into a file in python? References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5197ff62$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sat, 18 May 2013 15:14:31 -0400, Dennis Lee Bieber wrote: > tOn Sat, 18 May 2013 08:49:55 +0100, F?bio Santos > declaimed the following in > gmane.comp.python.general: > > >> You mentioned "\n" translating to two lines, but this won't happen. >> Windows will not mess with what you write to your file. Windows certainly may mess with what you write to your file, if it is opened in Text mode instead of Binary mode. In text mode, Windows will: - interpret Ctrl-Z characters as End Of File when reading; - convert \r\n to \n when reading; - convert \n to \r\n when writing. http://msdn.microsoft.com/en-us/library/z5hh6ee9(v=vs.80).aspx >> It's just that >> traditionally windows and windows programs use \r\n instead of just \n. >> I think it was for compatibility with os/2 or macintosh (I don't >> remember which), which used \r for newlines. >> > Neither... It goes back to Teletype machines where one sent a > carriage return to move the printhead back to the left, then sent a line > feed to advance the paper (while the head was still moving left), and in > some cases also provided a rub-out character (a do-nothing) to add an > additional character time delay. Yes, if you go back far enough, you get to teletype machines. But Windows inherits its text-mode behaviour from DOS, which inherits it from CP/M. > TRS-80 Mod 1-4 used for "new line", I believe Apple used > for "new line"... I can't comment about TRS, but classic Apple Macs (up to System 9) used carriage return \r as the line separator. -- Steven From fabiosantosart at gmail.com Sat May 18 17:19:54 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sat, 18 May 2013 22:19:54 +0100 Subject: How to write fast into a file in python? In-Reply-To: References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 18 May 2013 20:19, "Dennis Lee Bieber" wrote: > > tOn Sat, 18 May 2013 08:49:55 +0100, F?bio Santos > declaimed the following in > gmane.comp.python.general: > > > > You mentioned "\n" translating to two lines, but this won't happen. Windows > > will not mess with what you write to your file. It's just that > > traditionally windows and windows programs use \r\n instead of just \n. I > > think it was for compatibility with os/2 or macintosh (I don't remember > > which), which used \r for newlines. > > > Neither... It goes back to Teletype machines where one sent a > carriage return to move the printhead back to the left, then sent a line > feed to advance the paper (while the head was still moving left), and in > some cases also provided a rub-out character (a do-nothing) to add an > additional character time delay. > > TRS-80 Mod 1-4 used for "new line", I believe Apple used > for "new line"... And both lost the ability to move down the page > without also resetting the carriage to the left. In a world where both > is used, one could draw a vertical line of | by just spacing > across the first line, printing |, then repeat | until done. > To do the same with conventional is "new line/return" one has to > transmit all those spaces for each line... > > At 300baud, that took time.... On Sat, May 18, 2013 at 6:00 PM, Carlos Nepomuceno wrote: > Python really writes '\n\r' on Windows. Just check the files. > > Internal representations only keep '\n' for simplicity, but if you wanna keep track of the file length you have to take that into account. ;) On Sat, May 18, 2013 at 3:29 PM, Chris Angelico wrote: > Into two characters, not two lines, but yes. A file opened in text > mode on Windows will have its lines terminated with two characters. > (And it's old Macs that used to use \r. OS/2 follows the DOS > convention of \r\n, but again, many apps these days are happy with > Unix newlines there too.) > > ChrisA Thanks for your corrections and explanations. I stand corrected and have learned something. From timr at probo.com Sun May 19 22:04:17 2013 From: timr at probo.com (Tim Roberts) Date: Sun, 19 May 2013 19:04:17 -0700 Subject: How to write fast into a file in python? References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, <51961B73.2070401@davea.name>, , <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com>, , Message-ID: Carlos Nepomuceno wrote: >Python really writes '\n\r' on Windows. Just check the files. It actually writes \r\n, but it's not Python that's doing it. It's the C runtime library. And, of course, you can eliminate all of that by opening the file in binary mode open(name,'wb'). -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From steve+comp.lang.python at pearwood.info Sat May 18 00:01:13 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 May 2013 04:01:13 GMT Subject: How to write fast into a file in python? References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5196fd08$0$29997$c3e8da3$5496439d@news.astraweb.com> On Fri, 17 May 2013 21:18:15 +0300, Carlos Nepomuceno wrote: > I thought there would be a call to format method by "'%d\n' % i". It > seems the % operator is a lot faster than format. I just stopped using > it because I read it was going to be deprecated. :( Why replace such a > great and fast operator by a slow method? I mean, why format is been > preferred over %? That is one of the most annoying, pernicious myths about Python, probably second only to "the GIL makes Python slow" (it actually makes it fast). String formatting with % is not deprecated. It will not be deprecated, at least not until Python 4000. The string format() method has a few advantages: it is more powerful, consistent and flexible, but it is significantly slower. Probably the biggest disadvantage to % formatting, and probably the main reason why it is "discouraged", is that it treats tuples specially. Consider if x is an arbitrary object, and you call "%s" % x: py> "%s" % 23 # works '23' py> "%s" % [23, 42] # works '[23, 42]' and so on for *almost* any object. But if x is a tuple, strange things happen: py> "%s" % (23,) # tuple with one item looks like an int '23' py> "%s" % (23, 42) # tuple with two items fails Traceback (most recent call last): File "", line 1, in TypeError: not all arguments converted during string formatting So when dealing with arbitrary objects that you cannot predict what they are, it is better to use format. -- Steven From rosuav at gmail.com Sat May 18 01:28:08 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 18 May 2013 15:28:08 +1000 Subject: How to write fast into a file in python? In-Reply-To: <5196fd08$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> <5196fd08$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, May 18, 2013 at 2:01 PM, Steven D'Aprano wrote: > Consider if x is an arbitrary object, and you call "%s" % x: > > py> "%s" % 23 # works > '23' > py> "%s" % [23, 42] # works > '[23, 42]' > > and so on for *almost* any object. But if x is a tuple, strange things > happen Which can be guarded against by wrapping it up in a tuple. All you're seeing is that the shortcut notation for a single parameter can't handle tuples. >>> def show(x): return "%s" % (x,) >>> show(23) '23' >>> show((23,)) '(23,)' >>> show([23,42]) '[23, 42]' One of the biggest differences between %-formatting and str.format is that one is an operator and the other a method. The operator is always going to be faster, but the method can give more flexibility (not that I've ever needed or wanted to override anything). >>> def show_format(x): return "{}".format(x) # Same thing using str.format >>> dis.dis(show) 2 0 LOAD_CONST 1 ('%s') 3 LOAD_FAST 0 (x) 6 BUILD_TUPLE 1 9 BINARY_MODULO 10 RETURN_VALUE >>> dis.dis(show_format) 2 0 LOAD_CONST 1 ('{}') 3 LOAD_ATTR 0 (format) 6 LOAD_FAST 0 (x) 9 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 12 RETURN_VALUE Attribute lookup and function call versus binary operator. Potentially a lot of flexibility, versus basically hard-coded functionality. But has anyone ever actually made use of it? str.format does have some cleaner features, like naming of parameters: >>> "{foo} vs {bar}".format(foo=1,bar=2) '1 vs 2' >>> "%(foo)s vs %(bar)s"%{'foo':1,'bar':2} '1 vs 2' Extremely handy when you're working with hugely complex format strings, and the syntax feels a bit clunky in % (also, it's not portable to other languages, which is one of %-formatting's biggest features). Not a huge deal, but if you're doing a lot with that, it might be a deciding vote. ChrisA From dihedral88888 at googlemail.com Sat May 18 07:09:58 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 18 May 2013 04:09:58 -0700 (PDT) Subject: How to write fast into a file in python? In-Reply-To: <5196fd08$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> <51961B73.2070401@davea.name> <51966d15$0$29997$c3e8da3$5496439d@news.astraweb.com> <5196fd08$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano? 2013?5?18????UTC+8??12?01?13???? > On Fri, 17 May 2013 21:18:15 +0300, Carlos Nepomuceno wrote: > > > > > I thought there would be a call to format method by "'%d\n' % i". It > > > seems the % operator is a lot faster than format. I just stopped using > > > it because I read it was going to be deprecated. :( Why replace such a > > > great and fast operator by a slow method? I mean, why format is been > > > preferred over %? > > > > That is one of the most annoying, pernicious myths about Python, probably > > second only to "the GIL makes Python slow" (it actually makes it fast). > > The print function in python is designed to print any printable object with a valid string representation. The format part of the print function has to construct a printable string according to the format string and the variables passed in on the fly. If the acctual string to be printed in the format processing is obtained in the high level OOP way , then it is definitely slow due to the high level overheads and generality requirements. From drsalists at gmail.com Sat May 18 15:38:30 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Sat, 18 May 2013 12:38:30 -0700 Subject: How to write fast into a file in python? In-Reply-To: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> Message-ID: With CPython 2.7.3: ./t time taken to write a file of size 52428800 is 15.86 seconds time taken to write a file of size 52428800 is 7.91 seconds time taken to write a file of size 52428800 is 9.64 seconds With pypy-1.9: ./t time taken to write a file of size 52428800 is 3.708232 seconds time taken to write a file of size 52428800 is 4.868304 seconds time taken to write a file of size 52428800 is 1.93612 seconds Here's the code: #!/usr/local/pypy-1.9/bin/pypy #!/usr/bin/python import sys import time import StringIO sys.path.insert(0, '/usr/local/lib') import bufsock def create_file_numbers_old(filename, size): start = time.clock() value = 0 with open(filename, "w") as f: while f.tell() < size: f.write("{0}\n".format(value)) value += 1 end = time.clock() print "time taken to write a file of size", size, " is ", (end -start), "seconds \n" def create_file_numbers_bufsock(filename, intended_size): start = time.clock() value = 0 with open(filename, "w") as f: bs = bufsock.bufsock(f) actual_size = 0 while actual_size < intended_size: string = "{0}\n".format(value) actual_size += len(string) + 1 bs.write(string) value += 1 bs.flush() end = time.clock() print "time taken to write a file of size", intended_size, " is ", (end -start), "seconds \n" def create_file_numbers_file_like(filename, intended_size): start = time.clock() value = 0 with open(filename, "w") as f: file_like = StringIO.StringIO() actual_size = 0 while actual_size < intended_size: string = "{0}\n".format(value) actual_size += len(string) + 1 file_like.write(string) value += 1 file_like.seek(0) f.write(file_like.read()) end = time.clock() print "time taken to write a file of size", intended_size, " is ", (end -start), "seconds \n" create_file_numbers_old('output.txt', 50 * 2**20) create_file_numbers_bufsock('output2.txt', 50 * 2**20) create_file_numbers_file_like('output3.txt', 50 * 2**20) On Thu, May 16, 2013 at 9:35 PM, wrote: > On Friday, May 17, 2013 8:50:26 AM UTC+5:30, lokesh... at gmail.com wrote: > > I need to write numbers into a file upto 50mb and it should be fast > > > > can any one help me how to do that? > > > > i had written the following code.. > > > > > ----------------------------------------------------------------------------------------------------------- > > > > def create_file_numbers_old(filename, size): > > > > start = time.clock() > > > > > > > > value = 0 > > > > with open(filename, "w") as f: > > > > while f.tell()< size: > > > > f.write("{0}\n".format(value)) > > > > value += 1 > > > > > > > > end = time.clock() > > > > > > > > print "time taken to write a file of size", size, " is ", (end -start), > "seconds \n" > > > > > ------------------------------------------------------------------------------------------------------------------ > > > > it takes about 20sec i need 5 to 10 times less than that. > size = 50mb > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Sun May 19 01:31:08 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 19 May 2013 08:31:08 +0300 Subject: How to write fast into a file in python? In-Reply-To: References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, Message-ID: Thanks Dan! I've never used CPython or PyPy. Will try them later. I think the main difference between your create_file_numbers_file_like() and the fastwrite5.py I sent earlier is that I've used cStringIO instead of StringIO. It took 12s less using cStringIO. My numbers are much greater, but I've used Python 2.7.5 instead: C:\src\Python>python create_file_numbers.py time taken to write a file of size 52428800? is? 39.1199457743 seconds time taken to write a file of size 52428800? is? 14.8704800436 seconds time taken to write a file of size 52428800? is? 23.0011990985 seconds I've downloaded bufsock.py and python2x3.py. The later one was hard to remove the source code from the web page. Can I use them on my projects? I'm not used to the UCI license[1]. What's the difference to the GPL? [1] http://stromberg.dnsalias.org/~dstromberg/UCI-license.html ________________________________ > Date: Sat, 18 May 2013 12:38:30 -0700 > Subject: Re: How to write fast into a file in python? > From: drsalists at gmail.com > To: lokeshkoppaka at gmail.com > CC: python-list at python.org > > > With CPython 2.7.3: > ./t > time taken to write a file of size 52428800 is 15.86 seconds > > time taken to write a file of size 52428800 is 7.91 seconds > > time taken to write a file of size 52428800 is 9.64 seconds > > > With pypy-1.9: > ./t > time taken to write a file of size 52428800 is 3.708232 seconds > > time taken to write a file of size 52428800 is 4.868304 seconds > > time taken to write a file of size 52428800 is 1.93612 seconds > > Here's the code: > #!/usr/local/pypy-1.9/bin/pypy > #!/usr/bin/python > > import sys > import time > import StringIO > > sys.path.insert(0, '/usr/local/lib') > import bufsock > > def create_file_numbers_old(filename, size): > start = time.clock() > > value = 0 > with open(filename, "w") as f: > while f.tell() < size: > f.write("{0}\n".format(value)) > value += 1 > > end = time.clock() > > print "time taken to write a file of size", size, " is ", (end > -start), "seconds \n" > > def create_file_numbers_bufsock(filename, intended_size): > start = time.clock() > > value = 0 > with open(filename, "w") as f: > bs = bufsock.bufsock(f) > actual_size = 0 > while actual_size < intended_size: > string = "{0}\n".format(value) > actual_size += len(string) + 1 > bs.write(string) > value += 1 > bs.flush() > > end = time.clock() > > print "time taken to write a file of size", intended_size, " is ", > (end -start), "seconds \n" > > > def create_file_numbers_file_like(filename, intended_size): > start = time.clock() > > value = 0 > with open(filename, "w") as f: > file_like = StringIO.StringIO() > actual_size = 0 > while actual_size < intended_size: > string = "{0}\n".format(value) > actual_size += len(string) + 1 > file_like.write(string) > value += 1 > file_like.seek(0) > f.write(file_like.read()) > > end = time.clock() > > print "time taken to write a file of size", intended_size, " is ", > (end -start), "seconds \n" > > create_file_numbers_old('output.txt', 50 * 2**20) > create_file_numbers_bufsock('output2.txt', 50 * 2**20) > create_file_numbers_file_like('output3.txt', 50 * 2**20) > > > > > On Thu, May 16, 2013 at 9:35 PM, > > wrote: > On Friday, May 17, 2013 8:50:26 AM UTC+5:30, > lokesh... at gmail.com wrote: > > I need to write numbers into a file upto 50mb and it should be fast > > > > can any one help me how to do that? > > > > i had written the following code.. > > > > > ----------------------------------------------------------------------------------------------------------- > > > > def create_file_numbers_old(filename, size): > > > > start = time.clock() > > > > > > > > value = 0 > > > > with open(filename, "w") as f: > > > > while f.tell()< size: > > > > f.write("{0}\n".format(value)) > > > > value += 1 > > > > > > > > end = time.clock() > > > > > > > > print "time taken to write a file of size", size, " is ", (end > -start), "seconds \n" > > > > > ------------------------------------------------------------------------------------------------------------------ > > > > it takes about 20sec i need 5 to 10 times less than that. > size = 50mb > -- > http://mail.python.org/mailman/listinfo/python-list > > > -- http://mail.python.org/mailman/listinfo/python-list From carlosnepomuceno at outlook.com Sun May 19 01:42:41 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 19 May 2013 08:42:41 +0300 Subject: How to write fast into a file in python? In-Reply-To: References: , , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, , , Message-ID: BTW, I've downloaded from the following places: http://stromberg.dnsalias.org/svn/bufsock/trunk/bufsock.py http://stromberg.dnsalias.org/~dstromberg/backshift/documentation/html/python2x3-pysrc.html Are those the latest versions? ---------------------------------------- > From: carlosnepomuceno at outlook.com > To: python-list at python.org > Subject: RE: How to write fast into a file in python? > Date: Sun, 19 May 2013 08:31:08 +0300 > CC: lokeshkoppaka at gmail.com > > Thanks Dan! I've never used CPython or PyPy. Will try them later. > > I think the main difference between your create_file_numbers_file_like() > and the fastwrite5.py I sent earlier is that I've used cStringIO > instead of StringIO. It took 12s less using cStringIO. > > My numbers are much greater, but I've used Python 2.7.5 instead: > > C:\src\Python>python create_file_numbers.py > time taken to write a file of size 52428800 is 39.1199457743 seconds > > time taken to write a file of size 52428800 is 14.8704800436 seconds > > time taken to write a file of size 52428800 is 23.0011990985 seconds > > > I've downloaded bufsock.py and python2x3.py. The later one was hard to remove the source code from the web page. > > Can I use them on my projects? I'm not used to the UCI license[1]. What's the difference to the GPL? > > > > > [1] http://stromberg.dnsalias.org/~dstromberg/UCI-license.html > > ________________________________ >> Date: Sat, 18 May 2013 12:38:30 -0700 >> Subject: Re: How to write fast into a file in python? >> From: drsalists at gmail.com >> To: lokeshkoppaka at gmail.com >> CC: python-list at python.org >> >> >> With CPython 2.7.3: >> ./t >> time taken to write a file of size 52428800 is 15.86 seconds >> >> time taken to write a file of size 52428800 is 7.91 seconds >> >> time taken to write a file of size 52428800 is 9.64 seconds >> >> >> With pypy-1.9: >> ./t >> time taken to write a file of size 52428800 is 3.708232 seconds >> >> time taken to write a file of size 52428800 is 4.868304 seconds >> >> time taken to write a file of size 52428800 is 1.93612 seconds >> > >> Here's the code: >> #!/usr/local/pypy-1.9/bin/pypy >> #!/usr/bin/python >> >> import sys >> import time >> import StringIO >> >> sys.path.insert(0, '/usr/local/lib') >> import bufsock >> >> def create_file_numbers_old(filename, size): >> start = time.clock() >> >> value = 0 >> with open(filename, "w") as f: >> while f.tell() < size: >> f.write("{0}\n".format(value)) >> value += 1 >> >> end = time.clock() >> >> print "time taken to write a file of size", size, " is ", (end >> -start), "seconds \n" >> >> def create_file_numbers_bufsock(filename, intended_size): >> start = time.clock() >> >> value = 0 >> with open(filename, "w") as f: >> bs = bufsock.bufsock(f) >> actual_size = 0 >> while actual_size < intended_size: >> string = "{0}\n".format(value) >> actual_size += len(string) + 1 >> bs.write(string) >> value += 1 >> bs.flush() >> >> end = time.clock() >> >> print "time taken to write a file of size", intended_size, " is ", >> (end -start), "seconds \n" >> >> >> def create_file_numbers_file_like(filename, intended_size): >> start = time.clock() >> >> value = 0 >> with open(filename, "w") as f: >> file_like = StringIO.StringIO() >> actual_size = 0 >> while actual_size < intended_size: >> string = "{0}\n".format(value) >> actual_size += len(string) + 1 >> file_like.write(string) >> value += 1 >> file_like.seek(0) >> f.write(file_like.read()) >> >> end = time.clock() >> >> print "time taken to write a file of size", intended_size, " is ", >> (end -start), "seconds \n" >> >> create_file_numbers_old('output.txt', 50 * 2**20) >> create_file_numbers_bufsock('output2.txt', 50 * 2**20) >> create_file_numbers_file_like('output3.txt', 50 * 2**20) >> >> >> >> >> On Thu, May 16, 2013 at 9:35 PM, >> > wrote: >> On Friday, May 17, 2013 8:50:26 AM UTC+5:30, >> lokesh... at gmail.com wrote: >>> I need to write numbers into a file upto 50mb and it should be fast >>> >>> can any one help me how to do that? >>> >>> i had written the following code.. >>> >>> >> ----------------------------------------------------------------------------------------------------------- >>> >>> def create_file_numbers_old(filename, size): >>> >>> start = time.clock() >>> >>> >>> >>> value = 0 >>> >>> with open(filename, "w") as f: >>> >>> while f.tell()< size: >>> >>> f.write("{0}\n".format(value)) >>> >>> value += 1 >>> >>> >>> >>> end = time.clock() >>> >>> >>> >>> print "time taken to write a file of size", size, " is ", (end >> -start), "seconds \n" >>> >>> >> ------------------------------------------------------------------------------------------------------------------ >>> >>> it takes about 20sec i need 5 to 10 times less than that. >> size = 50mb >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> >> -- http://mail.python.org/mailman/listinfo/python-list > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Sun May 19 05:21:54 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 19 May 2013 19:21:54 +1000 Subject: How to write fast into a file in python? In-Reply-To: References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> Message-ID: On Sun, May 19, 2013 at 3:31 PM, Carlos Nepomuceno wrote: > Thanks Dan! I've never used CPython or PyPy. Will try them later. CPython is the "classic" interpreter, written in C. It's the one you'll get from the obvious download links on python.org. ChrisA From carlosnepomuceno at outlook.com Sun May 19 05:41:23 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 19 May 2013 12:41:23 +0300 Subject: How to write fast into a file in python? In-Reply-To: References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, , , Message-ID: ooops! I meant to say Cython. nevermind... ---------------------------------------- > Date: Sun, 19 May 2013 19:21:54 +1000 > Subject: Re: How to write fast into a file in python? > From: rosuav at gmail.com > To: python-list at python.org > > On Sun, May 19, 2013 at 3:31 PM, Carlos Nepomuceno > wrote: >> Thanks Dan! I've never used CPython or PyPy. Will try them later. > > CPython is the "classic" interpreter, written in C. It's the one > you'll get from the obvious download links on python.org. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From diolu.remove_this_part at bigfoot.com Fri May 17 02:57:04 2013 From: diolu.remove_this_part at bigfoot.com (Olive) Date: Fri, 17 May 2013 08:57:04 +0200 Subject: Diacretical incensitive search Message-ID: <20130517085704.3f6609e8@pcolivier.chezmoi.net> One feature that seems to be missing in the re module (or any tools that I know for searching text) is "diacretical incensitive search". I would like to have a match for something like this: re.match("franc", "fran?ais") in about the same whay we can have a case incensitive search: re.match("(?i)fran", "Fran?ais"). Another related and more general problem (in the sense that it could easily be used to solve the first problem) would be to translate a string removing any diacritical mark: nodiac("Fran?ais") -> "Francais" The algorithm to write such a function is trivial but there are a lot of mark we can put on a letter. It would be necessary to have the list of "a"'s with something on it. i.e. "?,?,?", etc. and this for every letter. Trying to make such a list by hand would inevitably lead to some symbols forgotten (and would be tedious). Olive From petite.abeille at gmail.com Fri May 17 03:15:27 2013 From: petite.abeille at gmail.com (Petite Abeille) Date: Fri, 17 May 2013 09:15:27 +0200 Subject: Diacretical incensitive search In-Reply-To: <20130517085704.3f6609e8@pcolivier.chezmoi.net> References: <20130517085704.3f6609e8@pcolivier.chezmoi.net> Message-ID: On May 17, 2013, at 8:57 AM, Olive wrote: > The algorithm to write such a function is trivial but there are a lot of mark we can put on a letter. It would be necessary to have the list of "a"'s with something on it. i.e. "?,?,?", etc. and this for every letter. Trying to make such a list by hand would inevitably lead to some symbols forgotten (and would be tedious). Perhaps of interest? Sean M. Burke Unidecode? There appear to be several python implementations, e.g.: https://pypi.python.org/pypi/Unidecode From __peter__ at web.de Fri May 17 04:30:04 2013 From: __peter__ at web.de (Peter Otten) Date: Fri, 17 May 2013 10:30:04 +0200 Subject: Diacretical incensitive search References: <20130517085704.3f6609e8@pcolivier.chezmoi.net> Message-ID: Olive wrote: > One feature that seems to be missing in the re module (or any tools that I > know for searching text) is "diacretical incensitive search". I would like > to have a match for something like this: > > re.match("franc", "fran?ais") > > in about the same whay we can have a case incensitive search: > > re.match("(?i)fran", "Fran?ais"). > > Another related and more general problem (in the sense that it could > easily be used to solve the first problem) would be to translate a string > removing any diacritical mark: > > nodiac("Fran?ais") -> "Francais" > > The algorithm to write such a function is trivial but there are a lot of > mark we can put on a letter. It would be necessary to have the list of > "a"'s with something on it. i.e. "?,?,?", etc. and this for every letter. > Trying to make such a list by hand would inevitably lead to some symbols > forgotten (and would be tedious). [Python3.3] >>> unicodedata.normalize("NFKD", "Fran?ais").encode("ascii", "ignore").decode() 'Francais' import sys from collections import defaultdict from unicodedata import name, normalize d = defaultdict(list) for i in range(sys.maxunicode): c = chr(i) n = normalize("NFKD", c)[0] if ord(n) < 128 and n.isalpha(): # optional d[n].append(c) for k, v in d.items(): if len(v) > 1: print(k, "".join(v)) See also PS: Be warned that experiments on the console may be misleading: >>> unicodedata.normalize("NFKD", "?") 'c' >>> ascii(_) "'c\\u0327'" From diolu.remove_this_part at bigfoot.com Fri May 17 11:37:17 2013 From: diolu.remove_this_part at bigfoot.com (Olive) Date: Fri, 17 May 2013 17:37:17 +0200 Subject: Diacretical incensitive search References: <20130517085704.3f6609e8@pcolivier.chezmoi.net> Message-ID: <20130517173717.65f0aa31@pcolivier.chezmoi.net> Tanks a lot! From wxjmfauth at gmail.com Fri May 17 13:31:25 2013 From: wxjmfauth at gmail.com (jmfauth) Date: Fri, 17 May 2013 10:31:25 -0700 (PDT) Subject: Diacretical incensitive search References: <20130517085704.3f6609e8@pcolivier.chezmoi.net> <20130517173717.65f0aa31@pcolivier.chezmoi.net> Message-ID: -------- The handling of diacriticals is especially a nice case study. One can use it to toy with some specific features of Unicode, normalisation, decomposition, ... ... and also to show how Unicode can be badly implemented. First and quick example that came to my mind (Py325 and Py332): >>> timeit.repeat("ud.normalize('NFKC', ud.normalize('NFKD', '?????'))", "import unicodedata as ud") [2.929404406789672, 2.923327801150208, 2.923659417064755] >>> timeit.repeat("ud.normalize('NFKC', ud.normalize('NFKD', '?????'))", "import unicodedata as ud") [3.8437222586746884, 3.829490737203514, 3.819266963414293] jmf From grahn+nntp at snipabacken.se Mon May 20 05:10:15 2013 From: grahn+nntp at snipabacken.se (Jorgen Grahn) Date: 20 May 2013 09:10:15 GMT Subject: Diacretical incensitive search References: <20130517085704.3f6609e8@pcolivier.chezmoi.net> Message-ID: On Fri, 2013-05-17, Olive wrote: > One feature that seems to be missing in the re module (or any tools > that I know for searching text) is "diacretical incensitive search". I > would like to have a match for something like this: > re.match("franc", "fran?ais") ... > The algorithm to write such a function is trivial but there are a > lot of mark we can put on a letter. It would be necessary to have the > list of "a"'s with something on it. i.e. "?,?,?", etc. and this for > every letter. Trying to make such a list by hand would inevitably lead > to some symbols forgotten (and would be tedious). Ok, but please remember that the diacriticals are of varying importance. The english "na?ve" is easily recognizable when written as "naive". The swedish word "f?r" cannot be spelled "far" and still be understood. This is IMHO out of the scope of re, and perhaps case-insensitivity should have been too. Perhaps it /would/ have been, if regular expressions hadn't come from the ASCII world where these things are easy. /Jorgen -- // Jorgen Grahn O o . From robsuttonjr at yahoo.com Fri May 17 14:36:07 2013 From: robsuttonjr at yahoo.com (Rob Sutton) Date: Fri, 17 May 2013 11:36:07 -0700 (PDT) Subject: Back-end Python Developer Seeking Telecommute Work Message-ID: <200802d6-2edc-4628-b1f3-8633cd013c50@googlegroups.com> I am seeking part/full time work as a back-end Python developer (telecommute or Utah only). I have been maintaining a Debian/Python/Django/Apache/PostgreSQL/PHP/MySql web application for 3 years on my own. I do all the development, database and system management myself. I can setup a complete system from scratch remotely or setup the hosting service. I have experience with Virtualbox and ProxMox virtualization. I have expereince in all areas of IT including telecom, programming, networking, hardware and software. Worked with Retail stores, warehousing systems, accounting software, data interchange, hardware interfacing, etc. Regards, Rob From dihedral88888 at googlemail.com Fri May 17 15:00:23 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 17 May 2013 12:00:23 -0700 (PDT) Subject: Back-end Python Developer Seeking Telecommute Work In-Reply-To: <200802d6-2edc-4628-b1f3-8633cd013c50@googlegroups.com> References: <200802d6-2edc-4628-b1f3-8633cd013c50@googlegroups.com> Message-ID: <9bd559a3-8ea1-43fa-ae12-6fbe64b515a8@googlegroups.com> Rob Sutton? 2013?5?18????UTC+8??2?36?07???? > I am seeking part/full time work as a back-end Python developer (telecommute or Utah only). I have been maintaining a Debian/Python/Django/Apache/PostgreSQL/PHP/MySql web application for 3 years on my own. I do all the development, database and system management myself. I can setup a complete system from scratch remotely or setup the hosting service. I have experience with Virtualbox and ProxMox virtualization. I have expereince in all areas of IT including telecom, programming, networking, hardware and software. Worked with Retail stores, warehousing systems, accounting software, data interchange, hardware interfacing, etc. > > > > Regards, > > > > Rob Nic,e I am working on several similar projects including EDA automations with expert systems and some VLSI-FPGA IP cores. Anyway I have to get some US patents first. From nawkboy+python at gmail.com Fri May 17 15:25:10 2013 From: nawkboy+python at gmail.com (James Carpenter) Date: Fri, 17 May 2013 14:25:10 -0500 Subject: Continuous Deployment Style Build System for Python Message-ID: Defend Against Fruit is focused on providing a pragmatic, continuous deployment style build system for Python. Current Python build systems do not properly account for the needs of effective continuous deployment. This package extends the Python tooling to add the missing pieces, including integration with Artifactory. With an eye to agile development principles and fast-feedback, we want a build system which satisfies the following goals: * Every SCM change-set committed should result in a potentially shippable release candidate. * When a defect is introduced, we want to immediately detect and isolate the offending SCM change-set. This is true even if the defect was introduced into a library we depend upon. * Library management should be so easy as to never impede code changes, even in multi-component architecture. More details available at: http://teamfruit.github.io/defend_against_fruit/ License: Apache Public License v2 Authors: James Carpenter jcarpenter621 at yahoo.com LinkedIn: http://www.linkedin.com/in/jamescarpenter1 Matthew Tardiff mattrix at gmail.com LinkedIn: http://www.linkedin.com/in/matthewtardiff -------------- next part -------------- An HTML attachment was scrubbed... URL: From bradley.wright.biz at gmail.com Sat May 18 00:19:54 2013 From: bradley.wright.biz at gmail.com (Bradley Wright) Date: Fri, 17 May 2013 21:19:54 -0700 (PDT) Subject: Two Dictionaries and a Sum! Message-ID: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> Confusing subject for a confusing problem (to a novice like me of course!) Thx for the help in advance folks I have (2) dictionaries: prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 } stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } Here's my instructions: consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!) HERES MY CODE: for key in prices: print prices[key]*stock[key] HERES THE OUTPUT: 48.0 45 24 0 ISSUE: I need to find a way to add all of those together...any pointers? From fjctlzy at gmail.com Sat May 18 00:30:45 2013 From: fjctlzy at gmail.com (Spacelee) Date: Sat, 18 May 2013 12:30:45 +0800 Subject: Two Dictionaries and a Sum! In-Reply-To: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> References: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> Message-ID: for key in prices.keys(): print prices[key]*stock[key] On Sat, May 18, 2013 at 12:19 PM, Bradley Wright < bradley.wright.biz at gmail.com> wrote: > Confusing subject for a confusing problem (to a novice like me of course!) > Thx for the help in advance folks > > I have (2) dictionaries: > > prices = { > "banana": 4, > "apple": 2, > "orange": 1.5, > "pear": 3 > } > > stock = { > "banana": 6, > "apple": 0, > "orange": 32, > "pear": 15 > } > > Here's my instructions: > > consider this as an inventory and calculate the sum (thats 4*6 = 24 > bananas!) > > HERES MY CODE: > > for key in prices: > print prices[key]*stock[key] > > HERES THE OUTPUT: > > 48.0 > 45 > 24 > 0 > > ISSUE: > I need to find a way to add all of those together...any pointers? > -- > http://mail.python.org/mailman/listinfo/python-list > -- *Space Lee* -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sat May 18 01:10:30 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 18 May 2013 15:10:30 +1000 Subject: Two Dictionaries and a Sum! In-Reply-To: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> References: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> Message-ID: On Sat, May 18, 2013 at 2:19 PM, Bradley Wright wrote: > Confusing subject for a confusing problem (to a novice like me of course!) > Thx for the help in advance folks > > I have (2) dictionaries: > > prices = { > "banana": 4, > "apple": 2, > "orange": 1.5, > "pear": 3 > } > > stock = { > "banana": 6, > "apple": 0, > "orange": 32, > "pear": 15 > } > > Here's my instructions: > > consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!) Let me reword your problem a little, maybe it'll be a bit clearer. You're trying to calculate the total value of all stock on hand, eg for insurance purposes. That's not 24 bananas, that's $24 of bananas. And the part you want now is to get the total value of your entire stock. Great! You're very close to there... > HERES MY CODE: > > for key in prices: > print prices[key]*stock[key] > > ISSUE: > I need to find a way to add all of those together...any pointers? ... you just need to accumulate a sum. Since this is almost certainly homework, I won't give you the answer, but here are a few pointers: * You'll need a single variable (I use the term sloppily, Python doesn't actually have variables per se) which will collect the final total. * Inside your loop, you're currently printing out an int/float with the value of the current item. Just add it onto your accumulator. * Python will happily work with integers and floats together, so you can just do what's obvious and it'll work. See where that takes you. Have fun! :) ChrisA From hektorlg at gmail.com Sat May 18 01:20:25 2013 From: hektorlg at gmail.com (nanobio) Date: Fri, 17 May 2013 22:20:25 -0700 (PDT) Subject: Two Dictionaries and a Sum! In-Reply-To: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> References: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> Message-ID: <38fdb42f-b233-4da5-8936-7439072edfbd@googlegroups.com> total,amount=0,0 for key in prices.keys(): price=prices[key]*stock[key] total+=price print "%s %s" % (price,total) From hektorlg at gmail.com Sat May 18 01:22:45 2013 From: hektorlg at gmail.com (nanobio) Date: Fri, 17 May 2013 22:22:45 -0700 (PDT) Subject: Two Dictionaries and a Sum! In-Reply-To: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> References: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> Message-ID: total,amount=0,0 for key in prices.keys(): amount=prices[key]*stock[key] total+=amount print "%s %s" % (amount,total) From roy at panix.com Sat May 18 09:22:47 2013 From: roy at panix.com (Roy Smith) Date: Sat, 18 May 2013 09:22:47 -0400 Subject: Two Dictionaries and a Sum! References: <6012d69f-b65e-4d65-90c4-f04876853f5e@googlegroups.com> Message-ID: In article <6012d69f-b65e-4d65-90c4-f04876853f5e at googlegroups.com>, Bradley Wright wrote: > Confusing subject for a confusing problem (to a novice like me of course!) > Thx for the help in advance folks > > I have (2) dictionaries: > > prices = { > "banana": 4, > "apple": 2, > "orange": 1.5, > "pear": 3 > } > > stock = { > "banana": 6, > "apple": 0, > "orange": 32, > "pear": 15 > } > > Here's my instructions: Hmmm, homework for a class? > consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!) I suspect what you're trying to say is that bananas cost BTC 4 each, and since you've got 6 bananas, you've got BTC 24 worth of bananas, yes? And now you want to find the total value of your fruit supply? >> HERES MY CODE: > > for key in prices: > print prices[key]*stock[key] > > HERES THE OUTPUT: > > 48.0 > 45 > 24 > 0 So far, so good. A couple of things you may have noticed along the way: 1) Your orange unit price was a float, so the total value of all your oranges is a float as well. That's how math works in Python. 2) The keys are presented in random order. To make the output easier to interpret, you might want to do: print key, prices[key]*stock[key] > ISSUE: > I need to find a way to add all of those together...any pointers? The most straight-forward way would be something like: total = 0 for key in prices: fruit_subtotal = prices[key]*stock[key] total += fruit_subtotal print key, fruit_subtotal print total There are better ways to do this in Python, but start like this and get that to work. From avnesh.nitk at gmail.com Sat May 18 00:48:54 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Fri, 17 May 2013 21:48:54 -0700 (PDT) Subject: how to run another file inside current file? Message-ID: <67c4dd92-7e6a-4955-850b-0c21c2708e05@googlegroups.com> hi, I want to run a another file inside a ached.add_cron_job(..). how is it possible, please help me, I have a file otherFile.py for execution inside current file. I know it is very easy question but i m unable to get anything, please help me. example -- import otherFile from apscheduler.scheduler import Scheduler sched = Scheduler() sched.start() def job_function(): # Can I here add that file for execution, Or can i add that file directly inside cron? sched.add_cron_job(job_function, month='1-12', day='1-31', hour='0-23',minute='44-49') From fjctlzy at gmail.com Sat May 18 06:32:54 2013 From: fjctlzy at gmail.com (fjctlzy at gmail.com) Date: Sat, 18 May 2013 18:32:54 +0800 Subject: how to run another file inside current file? Message-ID: subprocess? ???????? Avnesh Shakya ??? >hi, > I want to run a another file inside a ached.add_cron_job(..). how is it possible, please help me, I have a file otherFile.py for execution inside current file. >I know it is very easy question but i m unable to get anything, please help me. >example -- > >import otherFile >from apscheduler.scheduler import Scheduler >sched = Scheduler() >sched.start() > >def job_function(): > # Can I here add that file for execution, Or can i add that file directly inside cron? > >sched.add_cron_job(job_function, month='1-12', day='1-31', hour='0-23',minute='44-49') >-- >http://mail.python.org/mailman/listinfo/python-list From kevin.xgr at gmail.com Sat May 18 07:15:14 2013 From: kevin.xgr at gmail.com (Kevin Xi) Date: Sat, 18 May 2013 19:15:14 +0800 Subject: how to run another file inside current file? In-Reply-To: <67c4dd92-7e6a-4955-850b-0c21c2708e05@googlegroups.com> References: <67c4dd92-7e6a-4955-850b-0c21c2708e05@googlegroups.com> Message-ID: Hi, It's better to specify version of python you work with. I know nothing about python 3 but in python 2 you can do this with `exec`. Example: > f = file('otherFile.py') > exec f For more, read the doc: http://docs.python.org/2.7/reference/simple_stmts.html#the-exec-statement HTH Kevin 2013/5/18 Avnesh Shakya > hi, > I want to run a another file inside a ached.add_cron_job(..). how is it > possible, please help me, I have a file otherFile.py for execution inside > current file. > I know it is very easy question but i m unable to get anything, please > help me. > example -- > > import otherFile > from apscheduler.scheduler import Scheduler > sched = Scheduler() > sched.start() > > def job_function(): > # Can I here add that file for execution, Or can i add that file > directly inside cron? > > sched.add_cron_job(job_function, month='1-12', day='1-31', > hour='0-23',minute='44-49') > -- > http://mail.python.org/mailman/listinfo/python-list > -- ???????????????? -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Sat May 18 12:39:16 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sat, 18 May 2013 12:39:16 -0400 Subject: how to run another file inside current file? In-Reply-To: References: <67c4dd92-7e6a-4955-850b-0c21c2708e05@googlegroups.com> Message-ID: On 5/18/2013 7:15 AM, Kevin Xi wrote: > Hi, > It's better to specify version of python you work with. Absolutely. I know nothing > about python 3 but in python 2 you can do this with `exec`. Example: > > > f = file('otherFile.py') > > exec f Py 2 has execfile that does the above. Py 3 do as above except that exec is a function. with open('otherfile.py') as f: exex(f) > For more, read the doc: > http://docs.python.org/2.7/reference/simple_stmts.html#the-exec-statement > HTH From debruinjj at gmail.com Sat May 18 04:58:13 2013 From: debruinjj at gmail.com (Jurgens de Bruin) Date: Sat, 18 May 2013 01:58:13 -0700 (PDT) Subject: Please help with Threading Message-ID: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> This is my first script where I want to use the python threading module. I have a large dataset which is a list of dict this can be as much as 200 dictionaries in the list. The final goal is a histogram for each dict 16 histograms on a page ( 4x4 ) - this already works. What I currently do is a create a nested list [ [ {} ], [ {} ] ] each inner list contains 16 dictionaries, thus each inner list is a single page of 16 histograms. Iterating over the outer-list and creating the graphs takes to long. So I would like multiple inner-list to be processes simultaneously and creating the graphs in "parallel". I am trying to use the python threading for this. I create 4 threads loop over the outer-list and send a inner-list to the thread. This seems to work if my nested lists only contains 2 elements - thus less elements than threads. Currently the scripts runs and then seems to get hung up. I monitor the resource on my mac and python starts off good using 80% and when the 4-thread is created the CPU usages drops to 0%. My thread creating is based on the following : http://www.tutorialspoint.com/python/python_multithreading.htm Any help would be create!!! From __peter__ at web.de Sat May 18 05:09:03 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 May 2013 11:09:03 +0200 Subject: Please help with Threading References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> Message-ID: Jurgens de Bruin wrote: > This is my first script where I want to use the python threading module. I > have a large dataset which is a list of dict this can be as much as 200 > dictionaries in the list. The final goal is a histogram for each dict 16 > histograms on a page ( 4x4 ) - this already works. > What I currently do is a create a nested list [ [ {} ], [ {} ] ] each > inner list contains 16 dictionaries, thus each inner list is a single page > of 16 histograms. Iterating over the outer-list and creating the graphs > takes to long. So I would like multiple inner-list to be processes > simultaneously and creating the graphs in "parallel". > I am trying to use the python threading for this. I create 4 threads loop > over the outer-list and send a inner-list to the thread. This seems to > work if my nested lists only contains 2 elements - thus less elements than > threads. Currently the scripts runs and then seems to get hung up. I > monitor the resource on my mac and python starts off good using 80% and > when the 4-thread is created the CPU usages drops to 0%. > > My thread creating is based on the following : > http://www.tutorialspoint.com/python/python_multithreading.htm > > Any help would be create!!! Can you show us the code? From debruinjj at gmail.com Sat May 18 07:23:12 2013 From: debruinjj at gmail.com (Jurgens de Bruin) Date: Sat, 18 May 2013 04:23:12 -0700 (PDT) Subject: Please help with Threading In-Reply-To: References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> Message-ID: I will post code - the entire scripts is 1000 lines of code - can I post the threading functions only? From __peter__ at web.de Sat May 18 08:01:04 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 May 2013 14:01:04 +0200 Subject: Please help with Threading References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> Message-ID: Jurgens de Bruin wrote: > I will post code - the entire scripts is 1000 lines of code - can I post > the threading functions only? Try to condense it to the relevant parts, but make sure that it can be run by us. As a general note, when you add new stuff to an existing longish script it is always a good idea to write it in such a way that you can test it standalone so that you can have some confidence that it will work as designed once you integrate it with your old code. From davea at davea.name Sat May 18 09:55:56 2013 From: davea at davea.name (Dave Angel) Date: Sat, 18 May 2013 09:55:56 -0400 Subject: Please help with Threading In-Reply-To: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> Message-ID: <5197886C.2020109@davea.name> On 05/18/2013 04:58 AM, Jurgens de Bruin wrote: > This is my first script where I want to use the python threading module. I have a large dataset which is a list of dict this can be as much as 200 dictionaries in the list. The final goal is a histogram for each dict 16 histograms on a page ( 4x4 ) - this already works. > What I currently do is a create a nested list [ [ {} ], [ {} ] ] each inner list contains 16 dictionaries, thus each inner list is a single page of 16 histograms. Iterating over the outer-list and creating the graphs takes to long. So I would like multiple inner-list to be processes simultaneously and creating the graphs in "parallel". > I am trying to use the python threading for this. I create 4 threads loop over the outer-list and send a inner-list to the thread. This seems to work if my nested lists only contains 2 elements - thus less elements than threads. Currently the scripts runs and then seems to get hung up. I monitor the resource on my mac and python starts off good using 80% and when the 4-thread is created the CPU usages drops to 0%. > > My thread creating is based on the following : http://www.tutorialspoint.com/python/python_multithreading.htm > > Any help would be create!!! > CPython, and apparently (all of?) the other current Python implementations, uses a GIL to prevent multi-threaded applications from shooting themselves in the foot. However the practical effect of the GIL is that CPU-bound applications do not multi-thread efficiently; the single-threaded version usually runs faster. The place where CPython programs gain from multithreading is where each thread spends much of its time waiting for some external trigger. (More specifically, if such a wait is inside well-written C code, it releases the GIL so other threads can get useful work done. Example is a thread waiting for internet activity, and blocks inside a system call) -- DaveA From carlosnepomuceno at outlook.com Sat May 18 20:02:29 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 19 May 2013 03:02:29 +0300 Subject: Please help with Threading In-Reply-To: <13lfp8lds6e2e41rtsnvqimcb6inu7p28o@invalid.netcom.com> References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com>, <13lfp8lds6e2e41rtsnvqimcb6inu7p28o@invalid.netcom.com> Message-ID: ---------------------------------------- > To: python-list at python.org > From: wlfraed at ix.netcom.com > Subject: Re: Please help with Threading > Date: Sat, 18 May 2013 15:28:56 -0400 > > On Sat, 18 May 2013 01:58:13 -0700 (PDT), Jurgens de Bruin > declaimed the following in > gmane.comp.python.general: > >> This is my first script where I want to use the python threading module. I have a large dataset which is a list of dict this can be as much as 200 dictionaries in the list. The final goal is a histogram for each dict 16 histograms on a page ( 4x4 ) - this already works. >> What I currently do is a create a nested list [ [ {} ], [ {} ] ] each inner list contains 16 dictionaries, thus each inner list is a single page of 16 histograms. Iterating over the outer-list and creating the graphs takes to long. So I would like multiple inner-list to be processes simultaneously and creating the graphs in "parallel". >> I am trying to use the python threading for this. I create 4 threads loop over the outer-list and send a inner-list to the thread. This seems to work if my nested lists only contains 2 elements - thus less elements than threads. Currently the scripts runs and then seems to get hung up. I monitor the resource on my mac and python starts off good using 80% and when the 4-thread is created the CPU usages drops to 0%. >> > > The odds are good that this is just going to run slower... Just been told that GIL doesn't make things slower, but as I didn't know that such a thing even existed I went out looking for more info and found that document: http://www.dabeaz.com/python/UnderstandingGIL.pdf Is it current? I didn't know Python threads aren't preemptive. Seems to be something really old considering the state of the art on parallel execution on multi-cores. What's the catch on making Python threads preemptive? Are there any ongoing projects to make that? > One: The common Python implementation uses a global interpreter lock > to prevent interpreted code from interfering with itself in multiple > threads. So "number cruncher" applications don't gain any speed from > being partitioned into thread -- even on a multicore processor, only one > thread can have the GIL at a time. On top of that, you have the overhead > of the interpreter switching between threads (GIL release on one thread, > GIL acquire for the next thread). > > Python threads work fine if the threads either rely on intelligent > DLLs for number crunching (instead of doing nested Python loops to > process a numeric array you pass it to something like NumPy which > releases the GIL while crunching a copy of the array) or they do lots of > I/O and have to wait for I/O devices (while one thread is waiting for > the write/read operation to complete, another thread can do some number > crunching). > > If you really need to do this type of number crunching in Python > level code, you'll want to look into the multiprocessing library > instead. That will create actual OS processes (each with a copy of the > interpreter, and not sharing memory) and each of those can run on a core > without conflicting on the GIL. Which library do you suggest? > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Sat May 18 20:38:14 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 19 May 2013 10:38:14 +1000 Subject: Please help with Threading In-Reply-To: References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> <13lfp8lds6e2e41rtsnvqimcb6inu7p28o@invalid.netcom.com> Message-ID: On Sun, May 19, 2013 at 10:02 AM, Carlos Nepomuceno wrote: > I didn't know Python threads aren't preemptive. Seems to be something really old considering the state of the art on parallel execution on multi-cores. > > What's the catch on making Python threads preemptive? Are there any ongoing projects to make that? Preemption isn't really the issue here. On the C level, preemptive vs cooperative usually means the difference between a stalled thread locking everyone else out and not doing so. Preemption is done at a lower level than user code (eg the operating system or the CPU), meaning that user code can't retain control of the CPU. With interpreted code eg in CPython, it's easy to implement preemption in the interpreter. I don't know how it's actually done, but one easy implementation would be "every N bytecode instructions, context switch". It's still done at a lower level than user code (N bytecode instructions might all actually be a single tight loop that the programmer didn't realize was infinite), but it's not at the OS level. But none of that has anything to do with multiple core usage. The problem there is that shared data structures need to be accessed simultaneously, and in CPython, there's a Global Interpreter Lock to simplify that; but the consequence of the GIL is that no two threads can simultaneously execute user-level code. There have been GIL-removal proposals at various times, but the fact remains that a global lock makes a huge amount of sense and gives pretty good performance across the board. There's always multiprocessing when you need multiple CPU-bound threads; it's an explicit way to separate the shared data (what gets transferred) from local (what doesn't). ChrisA From rosuav at gmail.com Sun May 19 17:52:23 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 20 May 2013 07:52:23 +1000 Subject: Please help with Threading In-Reply-To: References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> <13lfp8lds6e2e41rtsnvqimcb6inu7p28o@invalid.netcom.com> Message-ID: On Mon, May 20, 2013 at 7:46 AM, Dennis Lee Bieber wrote: > On Sun, 19 May 2013 10:38:14 +1000, Chris Angelico > declaimed the following in gmane.comp.python.general: >> With interpreted code eg in CPython, it's easy to implement preemption >> in the interpreter. I don't know how it's actually done, but one easy >> implementation would be "every N bytecode instructions, context >> switch". It's still done at a lower level than user code (N bytecode > > Which IS how the common Python interpreter does it -- barring the > thread making some system call that triggers a preemption ahead of time > (even time.sleep(0.0) triggers scheduling). Forget if the default is 20 > or 100 byte-code instructions -- as I recall, it DID change a few > versions back. Incidentally, is the context-switch check the same as the check for interrupt signal raising KeyboardInterrupt? ISTR that was another "every N instructions" check. ChrisA From davea at davea.name Sun May 19 21:04:42 2013 From: davea at davea.name (Dave Angel) Date: Sun, 19 May 2013 21:04:42 -0400 Subject: Please help with Threading In-Reply-To: References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> <13lfp8lds6e2e41rtsnvqimcb6inu7p28o@invalid.netcom.com> Message-ID: <519976AA.3040201@davea.name> On 05/19/2013 05:46 PM, Dennis Lee Bieber wrote: > On Sun, 19 May 2013 10:38:14 +1000, Chris Angelico > declaimed the following in gmane.comp.python.general: > >> On Sun, May 19, 2013 at 10:02 AM, Carlos Nepomuceno >> wrote: >>> I didn't know Python threads aren't preemptive. Seems to be something really old considering the state of the art on parallel execution on multi-cores. >>> >>> What's the catch on making Python threads preemptive? Are there any ongoing projects to make that? >> > > >> With interpreted code eg in CPython, it's easy to implement preemption >> in the interpreter. I don't know how it's actually done, but one easy >> implementation would be "every N bytecode instructions, context >> switch". It's still done at a lower level than user code (N bytecode > > Which IS how the common Python interpreter does it -- barring the > thread making some system call that triggers a preemption ahead of time > (even time.sleep(0.0) triggers scheduling). Forget if the default is 20 > or 100 byte-code instructions -- as I recall, it DID change a few > versions back. > > Part of the context switch is to transfer the GIL from the preempted > thread to the new thread. > > So, overall, on a SINGLE CORE processor running multiple CPU bound > threads takes a bit longer just due to the overhead of thread swapping. > > On a multi-core processor, the effect is the same, since -- even > though one may have a thread running on each core -- the GIL is only > assigned to one thread, and other threads get blocked when trying to > access runtime data structures. And you may have even more overhead from > processor cache misses if the a thread gets assigned to a different > core. > > (yes -- I'm restating the same thing as I had just trimmed below > this point... but the target is really the OP, where repetition may be > helpful in understanding) > So what's the mapping between real (OS) threads, and the fake ones Python uses? The OS keeps track of a separate stack and context for each thread it knows about; are they one-to-one with the ones you're describing here? If so, then any OS thread that gets scheduled will almost always find it can't get the GIL, and spend time thrashing. But the change that CPython does intentionally would be equivalent to a sleep(0). On the other hand, if these threads are distinct from the OS threads, is it done with some sort of thread pool, where CPython has its own stack, and doesn't really use the one managed by the OS? Understand the only OS threading I really understand is the one in Windows (which I no longer use). So assuming Linux has some form of lightweight threading, the distinction above may not map very well. -- DaveA From cs at zip.com.au Sat May 18 23:10:36 2013 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 19 May 2013 13:10:36 +1000 Subject: Please help with Threading In-Reply-To: References: Message-ID: <20130519031036.GA24155@cskk.homeip.net> On 19May2013 03:02, Carlos Nepomuceno wrote: | Just been told that GIL doesn't make things slower, but as I | didn't know that such a thing even existed I went out looking for | more info and found that document: | http://www.dabeaz.com/python/UnderstandingGIL.pdf | | Is it current? I didn't know Python threads aren't preemptive. | Seems to be something really old considering the state of the art | on parallel execution on multi-cores. | What's the catch on making Python threads preemptive? Are there any ongoing projects to make that? Depends what you mean by preemptive. If you have multiple CPU bound pure Python threads they will all get CPU time without any of them explicitly yeilding control. But thread switching happens between python instructions, mediated by the interpreter. The standard answers for using multiple cores is to either run multiple processes (either explicitly spawning other executables, or spawning child python processes using the multiprocessing module), or to use (as suggested) libraries that can do the compute intensive bits themselves, releasing the while doing so so that the Python interpreter can run other bits of your python code. Plenty of OS system calls (and calls to other libraries from the interpreter) release the GIL during the call. Other python threads can run during that window. And there are other Python implementations other than CPython. Cheers, -- Cameron Simpson Processes are like potatoes. - NCR device driver manual From carlosnepomuceno at outlook.com Mon May 20 03:45:22 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 20 May 2013 10:45:22 +0300 Subject: Please help with Threading In-Reply-To: <20130519031036.GA24155@cskk.homeip.net> References: , <20130519031036.GA24155@cskk.homeip.net> Message-ID: ---------------------------------------- > Date: Sun, 19 May 2013 13:10:36 +1000 > From: cs at zip.com.au > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > Subject: Re: Please help with Threading > > On 19May2013 03:02, Carlos Nepomuceno wrote: > | Just been told that GIL doesn't make things slower, but as I > | didn't know that such a thing even existed I went out looking for > | more info and found that document: > | http://www.dabeaz.com/python/UnderstandingGIL.pdf > | > | Is it current? I didn't know Python threads aren't preemptive. > | Seems to be something really old considering the state of the art > | on parallel execution on multi-cores. > | What's the catch on making Python threads preemptive? Are there any ongoing projects to make that? > > Depends what you mean by preemptive. If you have multiple CPU bound > pure Python threads they will all get CPU time without any of them > explicitly yeilding control. But thread switching happens between > python instructions, mediated by the interpreter. I meant operating system preemptive. I've just checked and Python does not start Windows threads. > The standard answers for using multiple cores is to either run > multiple processes (either explicitly spawning other executables, > or spawning child python processes using the multiprocessing module), > or to use (as suggested) libraries that can do the compute intensive > bits themselves, releasing the while doing so so that the Python > interpreter can run other bits of your python code. I've just discovered the multiprocessing module[1] and will make some tests with it later. Are there any other modules for that purpose? I've found the following articles about Python threads. Any suggestions? http://www.ibm.com/developerworks/aix/library/au-threadingpython/ http://pymotw.com/2/threading/index.html http://www.laurentluce.com/posts/python-threads-synchronization-locks-rlocks-semaphores-conditions-events-and-queues/ [1] http://docs.python.org/2/library/multiprocessing.html > Plenty of OS system calls (and calls to other libraries from the > interpreter) release the GIL during the call. Other python threads > can run during that window. > > And there are other Python implementations other than CPython. > > Cheers, > -- > Cameron Simpson > > Processes are like potatoes. - NCR device driver manual From fabiosantosart at gmail.com Mon May 20 02:25:17 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 20 May 2013 07:25:17 +0100 Subject: Please help with Threading In-Reply-To: <13lfp8lds6e2e41rtsnvqimcb6inu7p28o@invalid.netcom.com> References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> <13lfp8lds6e2e41rtsnvqimcb6inu7p28o@invalid.netcom.com> Message-ID: On 18 May 2013 20:33, "Dennis Lee Bieber" wrote: > Python threads work fine if the threads either rely on intelligent > DLLs for number crunching (instead of doing nested Python loops to > process a numeric array you pass it to something like NumPy which > releases the GIL while crunching a copy of the array) or they do lots of > I/O and have to wait for I/O devices (while one thread is waiting for > the write/read operation to complete, another thread can do some number > crunching). Has nobody thought of a context manager to allow a part of your code to free up the GIL? I think the GIL is not inherently bad, but if it poses a problem at times, there should be a way to get it out of your... Way. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cs at zip.com.au Mon May 20 03:45:14 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 20 May 2013 17:45:14 +1000 Subject: Please help with Threading In-Reply-To: References: Message-ID: <20130520074514.GA33353@cskk.homeip.net> On 20May2013 07:25, F?bio Santos wrote: | On 18 May 2013 20:33, "Dennis Lee Bieber" wrote: | > Python threads work fine if the threads either rely on intelligent | > DLLs for number crunching (instead of doing nested Python loops to | > process a numeric array you pass it to something like NumPy which | > releases the GIL while crunching a copy of the array) or they do lots of | > I/O and have to wait for I/O devices (while one thread is waiting for | > the write/read operation to complete, another thread can do some number | > crunching). | | Has nobody thought of a context manager to allow a part of your code to | free up the GIL? I think the GIL is not inherently bad, but if it poses a | problem at times, there should be a way to get it out of your... Way. The GIL makes individual python operations thread safe by never running two at once. This makes the implementation of the operations simpler, faster and safer. It is probably totally infeasible to write meaningful python code inside your suggested context manager that didn't rely on the GIL; if the GIL were not held the code would be unsafe. It is easy for a C extension to release the GIL, and then to do meaningful work until it needs to return to python land. Most C extensions will do that around non-trivial sections, and anything that may stall in the OS. So your use case for the context manager doesn't fit well. -- Cameron Simpson Gentle suggestions being those which are written on rocks of less than 5lbs. - Tracy Nelson in comp.lang.c From carlosnepomuceno at outlook.com Mon May 20 03:53:24 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 20 May 2013 10:53:24 +0300 Subject: Please help with Threading In-Reply-To: <20130520074514.GA33353@cskk.homeip.net> References: , <20130520074514.GA33353@cskk.homeip.net> Message-ID: ---------------------------------------- > Date: Mon, 20 May 2013 17:45:14 +1000 > From: cs at zip.com.au > To: fabiosantosart at gmail.com > Subject: Re: Please help with Threading > CC: python-list at python.org; wlfraed at ix.netcom.com > > On 20May2013 07:25, F?bio Santos wrote: > | On 18 May 2013 20:33, "Dennis Lee Bieber" wrote: > |> Python threads work fine if the threads either rely on intelligent > |> DLLs for number crunching (instead of doing nested Python loops to > |> process a numeric array you pass it to something like NumPy which > |> releases the GIL while crunching a copy of the array) or they do lots of > |> I/O and have to wait for I/O devices (while one thread is waiting for > |> the write/read operation to complete, another thread can do some number > |> crunching). > | > | Has nobody thought of a context manager to allow a part of your code to > | free up the GIL? I think the GIL is not inherently bad, but if it poses a > | problem at times, there should be a way to get it out of your... Way. > > The GIL makes individual python operations thread safe by never > running two at once. This makes the implementation of the operations > simpler, faster and safer. It is probably totally infeasible to > write meaningful python code inside your suggested context > manager that didn't rely on the GIL; if the GIL were not held the > code would be unsafe. I just got my hands dirty trying to synchronize Python prints from many threads. Sometimes they mess up when printing the newlines. I tried several approaches using threading.Lock and Condition. None of them worked perfectly and all of them made the code sluggish. Is there a 100% sure method to make print thread safe? Can it be fast??? > It is easy for a C extension to release the GIL, and then to do > meaningful work until it needs to return to python land. Most C > extensions will do that around non-trivial sections, and anything > that may stall in the OS. > > So your use case for the context manager doesn't fit well. > -- > Cameron Simpson > > Gentle suggestions being those which are written on rocks of less than 5lbs. > - Tracy Nelson in comp.lang.c > -- > http://mail.python.org/mailman/listinfo/python-list From cs at zip.com.au Mon May 20 04:35:20 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 20 May 2013 18:35:20 +1000 Subject: Please help with Threading In-Reply-To: References: Message-ID: <20130520083520.GA64848@cskk.homeip.net> On 20May2013 10:53, Carlos Nepomuceno wrote: | I just got my hands dirty trying to synchronize Python prints from many threads. | Sometimes they mess up when printing the newlines. | I tried several approaches using threading.Lock and Condition. | None of them worked perfectly and all of them made the code sluggish. Show us some code, with specific complaints. Did you try this? _lock = Lock() def lprint(*a, **kw): global _lock with _lock: print(*a, **kw) and use lprint() everywhere? For generality the lock should be per file: the above hack uses one lock for any file, so that's going to stall overlapping prints to different files; inefficient. There are other things than the above, but at least individual prints will never overlap. If you have interleaved prints, show us. | Is there a 100% sure method to make print thread safe? Can it be fast??? Depends on what you mean by "fast". It will be slower than code with no lock; how much would require measurement. Cheers, -- Cameron Simpson My own suspicion is that the universe is not only queerer than we suppose, but queerer than we *can* suppose. - J.B.S. Haldane "On Being the Right Size" in the (1928) book "Possible Worlds" From rosuav at gmail.com Mon May 20 05:09:13 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 20 May 2013 19:09:13 +1000 Subject: Please help with Threading In-Reply-To: <20130520083520.GA64848@cskk.homeip.net> References: <20130520083520.GA64848@cskk.homeip.net> Message-ID: On Mon, May 20, 2013 at 6:35 PM, Cameron Simpson wrote: > _lock = Lock() > > def lprint(*a, **kw): > global _lock > with _lock: > print(*a, **kw) > > and use lprint() everywhere? Fun little hack: def print(*args,print=print,lock=Lock(),**kwargs): with lock: print(*args,**kwargs) Question: Is this a cool use or a horrible abuse of the scoping rules? ChrisA From fabiosantosart at gmail.com Mon May 20 05:26:20 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 20 May 2013 10:26:20 +0100 Subject: Please help with Threading In-Reply-To: References: <20130520083520.GA64848@cskk.homeip.net> Message-ID: It is pretty cool although it looks like a recursive function at first ;) On 20 May 2013 10:13, "Chris Angelico" wrote: > On Mon, May 20, 2013 at 6:35 PM, Cameron Simpson wrote: > > _lock = Lock() > > > > def lprint(*a, **kw): > > global _lock > > with _lock: > > print(*a, **kw) > > > > and use lprint() everywhere? > > Fun little hack: > > def print(*args,print=print,lock=Lock(),**kwargs): > with lock: > print(*args,**kwargs) > > Question: Is this a cool use or a horrible abuse of the scoping rules? > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cs at zip.com.au Mon May 20 05:54:19 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 20 May 2013 19:54:19 +1000 Subject: Please help with Threading In-Reply-To: References: Message-ID: <20130520095419.GA14050@cskk.homeip.net> On 20May2013 19:09, Chris Angelico wrote: | On Mon, May 20, 2013 at 6:35 PM, Cameron Simpson wrote: | > _lock = Lock() | > | > def lprint(*a, **kw): | > global _lock | > with _lock: | > print(*a, **kw) | > | > and use lprint() everywhere? | | Fun little hack: | | def print(*args,print=print,lock=Lock(),**kwargs): | with lock: | print(*args,**kwargs) | | Question: Is this a cool use or a horrible abuse of the scoping rules? I carefully avoided monkey patching print itself:-) That's... mad! I can see what the end result is meant to be, but it looks like a debugging nightmare. Certainly my scoping-fu is too weak to see at a glance how it works. -- Cameron Simpson I will not do it as a hack I will not do it for my friends I will not do it on a Mac I will not write for Uncle Sam I will not do it on weekends I won't do ADA, Sam-I-Am - Gregory Bond From rosuav at gmail.com Mon May 20 06:09:13 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 20 May 2013 20:09:13 +1000 Subject: Please help with Threading In-Reply-To: <20130520095419.GA14050@cskk.homeip.net> References: <20130520095419.GA14050@cskk.homeip.net> Message-ID: On Mon, May 20, 2013 at 7:54 PM, Cameron Simpson wrote: > On 20May2013 19:09, Chris Angelico wrote: > | On Mon, May 20, 2013 at 6:35 PM, Cameron Simpson wrote: > | > _lock = Lock() > | > > | > def lprint(*a, **kw): > | > global _lock > | > with _lock: > | > print(*a, **kw) > | > > | > and use lprint() everywhere? > | > | Fun little hack: > | > | def print(*args,print=print,lock=Lock(),**kwargs): > | with lock: > | print(*args,**kwargs) > | > | Question: Is this a cool use or a horrible abuse of the scoping rules? > > I carefully avoided monkey patching print itself:-) > > That's... mad! I can see what the end result is meant to be, but > it looks like a debugging nightmare. Certainly my scoping-fu is too > weak to see at a glance how it works. Hehe. Like I said, could easily be called abuse. Referencing a function's own name in a default has to have one of these interpretations: 1) It's a self-reference, which can be used to guarantee recursion even if the name is rebound 2) It references whatever previously held that name before this def statement. Either would be useful. Python happens to follow #2; though I can't point to any piece of specification that mandates that, so all I can really say is that CPython 3.3 appears to follow #2. But both interpretations make sense, and both would be of use, and use of either could be called abusive of the rules. Figure that out. :) The second defaulted argument (lock=Lock()), of course, is a common idiom. No abuse there, that's pretty Pythonic. This same sort of code could be done as a decorator: def serialize(fn): lock=Lock() def locked(*args,**kw): with lock: fn(*args,**kw) return locked print=serialize(print) Spelled like this, it's obvious that the argument to serialize has to be the previous 'print'. The other notation achieves the same thing, just in a quirkier way :) ChrisA From ned at nedbatchelder.com Mon May 20 06:46:38 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Mon, 20 May 2013 06:46:38 -0400 Subject: Please help with Threading In-Reply-To: References: <20130520095419.GA14050@cskk.homeip.net> Message-ID: <5199FF0E.8000600@nedbatchelder.com> On 5/20/2013 6:09 AM, Chris Angelico wrote: > Referencing a function's own name in a default has to have one of > these interpretations: > > 1) It's a self-reference, which can be used to guarantee recursion > even if the name is rebound > 2) It references whatever previously held that name before this def statement. The meaning must be #2. A def statement is nothing more than a fancy assignment statement. This: def foo(a): return a + 1 is really just the same as: foo = lambda a: a+1 (in fact, they compile to identical bytecode). More complex def's don't have equivalent lambdas, but are still assignments to the name of the function. So your "apparently recursive" print function is no more ambiguous "x = x + 1". The x on the right hand side is the old value of x, the x on the left hand side will be the new value of x. # Each of these updates a name x = x + 1 def print(*args,print=print,lock=Lock(),**kwargs): with lock: print(*args,**kwargs) Of course, if you're going to use that code, a comment might be in order to help the next reader through the trickiness... --Ned. From rosuav at gmail.com Mon May 20 10:52:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 00:52:25 +1000 Subject: Please help with Threading In-Reply-To: <5199FF0E.8000600@nedbatchelder.com> References: <20130520095419.GA14050@cskk.homeip.net> <5199FF0E.8000600@nedbatchelder.com> Message-ID: =On Mon, May 20, 2013 at 8:46 PM, Ned Batchelder wrote: > On 5/20/2013 6:09 AM, Chris Angelico wrote: >> >> Referencing a function's own name in a default has to have one of >> these interpretations: >> >> 1) It's a self-reference, which can be used to guarantee recursion >> even if the name is rebound >> 2) It references whatever previously held that name before this def >> statement. > > > The meaning must be #2. A def statement is nothing more than a fancy > assignment statement. Sure, but the language could have been specced up somewhat differently, with the same syntax. I was fairly confident that this would be universally true (well, can't do it with 'print' per se in older Pythons, but for others); my statement about CPython 3.3 was just because I hadn't actually hunted down specification proof. > So your "apparently recursive" print function is no more > ambiguous "x = x + 1". The x on the right hand side is the old value of x, > the x on the left hand side will be the new value of x. > > # Each of these updates a name > x = x + 1 > > def print(*args,print=print,lock=Lock(),**kwargs): > with lock: > print(*args,**kwargs) Yeah. The decorator example makes that fairly clear. > Of course, if you're going to use that code, a comment might be in order to > help the next reader through the trickiness... Absolutely!! ChrisA From carlosnepomuceno at outlook.com Mon May 20 06:01:13 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 20 May 2013 13:01:13 +0300 Subject: Please help with Threading In-Reply-To: <20130520083520.GA64848@cskk.homeip.net> References: , <20130520083520.GA64848@cskk.homeip.net> Message-ID: ---------------------------------------- > Date: Mon, 20 May 2013 18:35:20 +1000 > From: cs at zip.com.au > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > Subject: Re: Please help with Threading > > On 20May2013 10:53, Carlos Nepomuceno wrote: > | I just got my hands dirty trying to synchronize Python prints from many threads. > | Sometimes they mess up when printing the newlines. > | I tried several approaches using threading.Lock and Condition. > | None of them worked perfectly and all of them made the code sluggish. > > Show us some code, with specific complaints. > > Did you try this? > > _lock = Lock() > > def lprint(*a, **kw): > global _lock > with _lock: > print(*a, **kw) > > and use lprint() everywhere? It works! Think I was running the wrong script... Anyway, the suggestion you've made is the third and latest attempt that I've tried to synchronize the print outputs from the threads. I've also used: ### 1st approach ### lock? = threading.Lock() [...] try: ??? lock.acquire() ??? [thread protected code] finally: ??? lock.release() ### 2nd approach ### cond? = threading.Condition() [...] try: ??? [thread protected code] ??? with cond: ? ?? ?? print '[...]' ### 3rd approach ### from __future__ import print_function def safe_print(*args, **kwargs): ??? global print_lock ??? with print_lock: ??????? print(*args, **kwargs) [...] try: ??? [thread protected code] ??? safe_print('[...]') Except for the first one all kind of have the same performance. The problem was I placed the acquire/release around the whole code block, instead of only the print statements. Thanks a lot! ;) > For generality the lock should be per file: the above hack uses one > lock for any file, so that's going to stall overlapping prints to > different files; inefficient. > > There are other things than the above, but at least individual prints will > never overlap. If you have interleaved prints, show us. > > | Is there a 100% sure method to make print thread safe? Can it be fast??? > > Depends on what you mean by "fast". It will be slower than code > with no lock; how much would require measurement. > > Cheers, > -- > Cameron Simpson > > My own suspicion is that the universe is not only queerer than we suppose, > but queerer than we *can* suppose. > - J.B.S. Haldane "On Being the Right Size" > in the (1928) book "Possible Worlds" From dihedral88888 at googlemail.com Mon May 20 21:44:46 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 20 May 2013 18:44:46 -0700 (PDT) Subject: Please help with Threading In-Reply-To: References: <20130520083520.GA64848@cskk.homeip.net> Message-ID: Chris Angelico? 2013?5?20????UTC+8??5?09?13???? > On Mon, May 20, 2013 at 6:35 PM, Cameron Simpson wrote: > > > _lock = Lock() > > > > > > def lprint(*a, **kw): > > > global _lock > > > with _lock: > > > print(*a, **kw) > > > > > > and use lprint() everywhere? > > > > Fun little hack: > > > > def print(*args,print=print,lock=Lock(),**kwargs): > > with lock: > > print(*args,**kwargs) > > > > Question: Is this a cool use or a horrible abuse of the scoping rules? > > > > ChrisA OK, if the python interpreter has a global hiden print out buffer of ,say, 2to 16 K bytes, and all string print functions just construct the output string from the format to this string in an efficient low level way, then the next question would be that whether the uses can use functions in this low level buffer for other string formatting jobs. From rosuav at gmail.com Mon May 20 21:50:17 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 11:50:17 +1000 Subject: Please help with Threading In-Reply-To: References: <20130520083520.GA64848@cskk.homeip.net> Message-ID: On Tue, May 21, 2013 at 11:44 AM, 88888 Dihedral wrote: > OK, if the python interpreter has a global hiden print out > buffer of ,say, 2to 16 K bytes, and all string print functions > just construct the output string from the format to this string > in an efficient low level way, then the next question > would be that whether the uses can use functions in this > low level buffer for other string formatting jobs. You remind me of George. http://www.chroniclesofgeorge.com/ Both make great reading when I'm at work and poking around with random stuff in our .SQL file of carefully constructed mayhem. ChrisA From carlosnepomuceno at outlook.com Mon May 20 22:53:46 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 21 May 2013 05:53:46 +0300 Subject: Please help with Threading In-Reply-To: References: , <20130520083520.GA64848@cskk.homeip.net>, , , Message-ID: sys.stdout.write() does not suffer from the newlines mess up when printing from many threads, like print statement does. The only usage difference, AFAIK, is to add '\n' at the end of the string. It's faster and thread safe (really?) by default. BTW, why I didn't find the source code to the sys module in the 'Lib' directory? ---------------------------------------- > Date: Tue, 21 May 2013 11:50:17 +1000 > Subject: Re: Please help with Threading > From: rosuav at gmail.com > To: python-list at python.org > > On Tue, May 21, 2013 at 11:44 AM, 88888 Dihedral > wrote: >> OK, if the python interpreter has a global hiden print out >> buffer of ,say, 2to 16 K bytes, and all string print functions >> just construct the output string from the format to this string >> in an efficient low level way, then the next question >> would be that whether the uses can use functions in this >> low level buffer for other string formatting jobs. > > You remind me of George. > http://www.chroniclesofgeorge.com/ > > Both make great reading when I'm at work and poking around with random > stuff in our .SQL file of carefully constructed mayhem. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From carlosnepomuceno at outlook.com Mon May 20 23:36:02 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 21 May 2013 06:36:02 +0300 Subject: Please help with Threading In-Reply-To: References: , <20130520083520.GA64848@cskk.homeip.net>, , , Message-ID: > On Tue, May 21, 2013 at 11:44 AM, 88888 Dihedral > wrote: >> OK, if the python interpreter has a global hiden print out >> buffer of ,say, 2to 16 K bytes, and all string print functions >> just construct the output string from the format to this string >> in an efficient low level way, then the next question >> would be that whether the uses can use functions in this >> low level buffer for other string formatting jobs. > > You remind me of George. > http://www.chroniclesofgeorge.com/ > > Both make great reading when I'm at work and poking around with random > stuff in our .SQL file of carefully constructed mayhem. > > ChrisA lol I need more cowbell!!! Please!!! lol From steve+comp.lang.python at pearwood.info Tue May 21 00:48:31 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 May 2013 04:48:31 GMT Subject: Please help with Threading References: <20130520083520.GA64848@cskk.homeip.net> Message-ID: <519afc9e$0$6574$c3e8da3$5496439d@news.astraweb.com> On Tue, 21 May 2013 05:53:46 +0300, Carlos Nepomuceno wrote: > BTW, why I didn't find the source code to the sys module in the 'Lib' > directory? Because sys is a built-in module. It is embedded in the Python interpreter. -- Steven From fabiosantosart at gmail.com Mon May 20 03:55:22 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 20 May 2013 08:55:22 +0100 Subject: Please help with Threading In-Reply-To: <20130520074514.GA33353@cskk.homeip.net> References: <20130520074514.GA33353@cskk.homeip.net> Message-ID: My use case was a tight loop processing an image pixel by pixel, or crunching a CSV file. If it only uses local variables (and probably hold a lock before releasing the GIL) it should be safe, no? My idea is that it's a little bad to have to write C or use multiprocessing just to do simultaneous calculations. I think an application using a reactor loop such as twisted would actually benefit from this. Sure, it will be slower than a C implementation of the same loop, but isn't fast prototyping a very important feature of the Python language? On 20 May 2013 08:45, "Cameron Simpson" wrote: > On 20May2013 07:25, F?bio Santos wrote: > | On 18 May 2013 20:33, "Dennis Lee Bieber" wrote: > | > Python threads work fine if the threads either rely on > intelligent > | > DLLs for number crunching (instead of doing nested Python loops to > | > process a numeric array you pass it to something like NumPy which > | > releases the GIL while crunching a copy of the array) or they do lots > of > | > I/O and have to wait for I/O devices (while one thread is waiting for > | > the write/read operation to complete, another thread can do some number > | > crunching). > | > | Has nobody thought of a context manager to allow a part of your code to > | free up the GIL? I think the GIL is not inherently bad, but if it poses a > | problem at times, there should be a way to get it out of your... Way. > > The GIL makes individual python operations thread safe by never > running two at once. This makes the implementation of the operations > simpler, faster and safer. It is probably totally infeasible to > write meaningful python code inside your suggested context > manager that didn't rely on the GIL; if the GIL were not held the > code would be unsafe. > > It is easy for a C extension to release the GIL, and then to do > meaningful work until it needs to return to python land. Most C > extensions will do that around non-trivial sections, and anything > that may stall in the OS. > > So your use case for the context manager doesn't fit well. > -- > Cameron Simpson > > Gentle suggestions being those which are written on rocks of less than > 5lbs. > - Tracy Nelson in comp.lang.c > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Mon May 20 07:06:04 2013 From: davea at davea.name (Dave Angel) Date: Mon, 20 May 2013 07:06:04 -0400 Subject: Please help with Threading In-Reply-To: References: <20130520074514.GA33353@cskk.homeip.net> Message-ID: <519A039C.5040606@davea.name> On 05/20/2013 03:55 AM, F?bio Santos wrote: > My use case was a tight loop processing an image pixel by pixel, or > crunching a CSV file. If it only uses local variables (and probably hold a > lock before releasing the GIL) it should be safe, no? > Are you making function calls, using system libraries, or creating or deleting any objects? All of these use the GIL because they use common data structures shared among all threads. At the lowest level, creating an object requires locked access to the memory manager. Don't forget, the GIL gets used much more for Python internals than it does for the visible stuff. -- DaveA From fabiosantosart at gmail.com Mon May 20 14:04:50 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 20 May 2013 19:04:50 +0100 Subject: Please help with Threading In-Reply-To: <519A039C.5040606@davea.name> References: <20130520074514.GA33353@cskk.homeip.net> <519A039C.5040606@davea.name> Message-ID: I didn't know that. On 20 May 2013 12:10, "Dave Angel" wrote: > Are you making function calls, using system libraries, or creating or deleting any objects? All of these use the GIL because they use common data structures shared among all threads. At the lowest level, creating an object requires locked access to the memory manager. > > > Don't forget, the GIL gets used much more for Python internals than it does for the visible stuff. I did not know that. It's both interesting and somehow obvious, although I didn't know it yet. -------------- next part -------------- An HTML attachment was scrubbed... URL: From avnesh.nitk at gmail.com Sat May 18 06:12:30 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Sat, 18 May 2013 03:12:30 -0700 (PDT) Subject: python script is not running Message-ID: hi, i want to run python script which generating data into json fromat, I am using crontab, but it's not executing... my python code-- try.py -- import json import simplejson as json import sys def tryJson(): saved = sys.stdout correctFile = file('data.json', 'a+') sys.stdout = correctFile result = [] i = 1 for i in range(5): info = { 'a': i+1, 'b': i+2, 'c': i+3, } result.append(info) if result: print json.dumps(result, indent=6) sys.stdout = saved correctFile.close() tryJson() now i m doing ion terminal- avin at hp:~$ crontab -e then type - */2 * * * * python /home/avin/data/try.py and save but it's not executing. From fjctlzy at gmail.com Sat May 18 07:18:40 2013 From: fjctlzy at gmail.com (fjctlzy at gmail.com) Date: Sat, 18 May 2013 19:18:40 +0800 Subject: python script is not running Message-ID: make sure data.json is absolute path make sure you test it directly without crontab in the crontab, execute the script in such way, python scripty > /tmp/log 2>&1 check the log ???????? Avnesh Shakya ??? >hi, > i want to run python script which generating data into json fromat, I am using crontab, but it's not executing... >my python code-- >try.py -- > >import json >import simplejson as json >import sys > >def tryJson(): > saved = sys.stdout > correctFile = file('data.json', 'a+') > sys.stdout = correctFile > result = [] > i = 1 > for i in range(5): > info = { > 'a': i+1, > 'b': i+2, > 'c': i+3, > } > result.append(info) > > if result: > print json.dumps(result, indent=6) > sys.stdout = saved > correctFile.close() >tryJson() > >now i m doing ion terminal- >avin at hp:~$ crontab -e >then type - >*/2 * * * * python /home/avin/data/try.py > >and save > >but it's not executing. >-- >http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Sat May 18 09:28:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 18 May 2013 23:28:03 +1000 Subject: python script is not running In-Reply-To: References: Message-ID: On Sat, May 18, 2013 at 8:12 PM, Avnesh Shakya wrote: > avin at hp:~$ crontab -e > then type - > */2 * * * * python /home/avin/data/try.py > You may need to put an explicit path to your Python interpreter. Type: $ which python and put that into your crontab. ChrisA From roy at panix.com Sat May 18 09:44:25 2013 From: roy at panix.com (Roy Smith) Date: Sat, 18 May 2013 09:44:25 -0400 Subject: python script is not running References: Message-ID: In article , Chris Angelico wrote: > On Sat, May 18, 2013 at 8:12 PM, Avnesh Shakya wrote: > > avin at hp:~$ crontab -e > > then type - > > */2 * * * * python /home/avin/data/try.py > > > > You may need to put an explicit path to your Python interpreter. Type: > > $ which python > > and put that into your crontab. True. Somewhat more generally, jobs run under cron have a far more barren environment than most people realize. Or, looking at it a different way, most people don't even realize all the ways they depend on their environment being set up properly by the login process. If you've set things like PYTHONPATH, you won't have them set right for cron jobs unless you explicitly reset them in your crontab. It's often instructive to run something like "env > /tmp/xxx" under cron, and compare that to what you get when you run "env" at a command-line prompt. From tjreedy at udel.edu Sat May 18 12:35:19 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sat, 18 May 2013 12:35:19 -0400 Subject: python script is not running In-Reply-To: References: Message-ID: On 5/18/2013 6:12 AM, Avnesh Shakya wrote: > hi, > i want to run python script which generating data into json fromat, I am using crontab, but it's not executing... > my python code-- > try.py -- > > import json > import simplejson as json > import sys > > def tryJson(): > saved = sys.stdout > correctFile = file('data.json', 'a+') > sys.stdout = correctFile Don't need to change stdout. > result = [] > i = 1 > for i in range(5): > info = { > 'a': i+1, > 'b': i+2, > 'c': i+3, > } > result.append(info) What is you want to add print result for debugging? > if result: > print json.dumps(result, indent=6) Either use print >> correctFile, correctFile.write (do you really want the extra '\n' that print adds?), or, do the future import of print as function and pass correctFile as the file argument > sys.stdout = saved > correctFile.close() > tryJson() From woooee at gmail.com Sat May 18 13:35:23 2013 From: woooee at gmail.com (woooee) Date: Sat, 18 May 2013 10:35:23 -0700 (PDT) Subject: python script is not running References: Message-ID: <185557b8-5cd0-4e9c-8c6d-855a8635492c@pd6g2000pbc.googlegroups.com> The obvious question, do you have the shebang on the first line so the OS knows it's to be run as a Python program? Also I would change tryJson() to if __name__ == "__main__': tryJson() This probably won't make any difference but you will have the bases covered. From rosuav at gmail.com Sat May 18 13:59:12 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 19 May 2013 03:59:12 +1000 Subject: python script is not running In-Reply-To: <185557b8-5cd0-4e9c-8c6d-855a8635492c@pd6g2000pbc.googlegroups.com> References: <185557b8-5cd0-4e9c-8c6d-855a8635492c@pd6g2000pbc.googlegroups.com> Message-ID: On Sun, May 19, 2013 at 3:35 AM, woooee wrote: > The obvious question, do you have the shebang on the first line so the > OS knows it's to be run as a Python program? That won't make any difference; the cron job specifically stipulates the interpreter. It just needs to be done with a full path. ChrisA From vincent.vandevyvre at swing.be Sat May 18 14:43:00 2013 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Sat, 18 May 2013 20:43:00 +0200 Subject: python script is not running In-Reply-To: References: <185557b8-5cd0-4e9c-8c6d-855a8635492c@pd6g2000pbc.googlegroups.com> Message-ID: <5197CBB4.4020501@swing.be> Le 18/05/2013 19:59, Chris Angelico a ?crit : > On Sun, May 19, 2013 at 3:35 AM, woooee wrote: >> The obvious question, do you have the shebang on the first line so the >> OS knows it's to be run as a Python program? > That won't make any difference; the cron job specifically stipulates > the interpreter. It just needs to be done with a full path. > > ChrisA Not necessary, I use crontab without problem with this line: 25 16 18 5 * export DISPLAY=:0 & LC_CTYPE="fr_FR.utf-8" Lang="fr_FR.utf-8" python /usr/bin/qarte -a 1 ... on Ubuntu. -- Vincent V.V. Oqapy . Qarte . PaQager From rosuav at gmail.com Sat May 18 19:33:43 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 19 May 2013 09:33:43 +1000 Subject: python script is not running In-Reply-To: <5197CBB4.4020501@swing.be> References: <185557b8-5cd0-4e9c-8c6d-855a8635492c@pd6g2000pbc.googlegroups.com> <5197CBB4.4020501@swing.be> Message-ID: On Sun, May 19, 2013 at 4:43 AM, Vincent Vande Vyvre wrote: > Le 18/05/2013 19:59, Chris Angelico a ?crit : > >> On Sun, May 19, 2013 at 3:35 AM, woooee wrote: >>> >>> The obvious question, do you have the shebang on the first line so the >>> OS knows it's to be run as a Python program? >> >> That won't make any difference; the cron job specifically stipulates >> the interpreter. It just needs to be done with a full path. >> >> ChrisA > > Not necessary, I use crontab without problem with this line: > > 25 16 18 5 * export DISPLAY=:0 & LC_CTYPE="fr_FR.utf-8" Lang="fr_FR.utf-8" > python /usr/bin/qarte -a 1 > > ... on Ubuntu. Everything's configurable. I'd like to hear back from the OP though; as Roy said, checking 'env' from a cron job will reveal much. ChrisA From li.beinan at gmail.com Sat May 18 10:03:02 2013 From: li.beinan at gmail.com (Beinan Li) Date: Sat, 18 May 2013 10:03:02 -0400 Subject: Future standard GUI library Message-ID: Not sure if this is the right place to talk about this. Even less sure if I can move this discussion to tkinter list, so here I am... I know this may sound a silly question because no one can see the future. But ... Do you think tkinter is going to be the standard python built-in gui solution as long as python exists? I couldn't help but wonder if wx or PySide receives better py2 and py3 support, or anything else that prevent them from getting into the standard python distributions, whether or not this scene could start to shift ... I believe this "which one of tkinter, wx, qt, is the best gui toolkit for python" flame war has been going on for ages. I love the fact that python ships a built-in gui solution which makes shipping a pure-python desktop application a viable choice. But tkinter does not appear to be the most time-saving way to write a gui app. The layout designer support, for one, is next to zero. I tried many 3rd-party designers and loved PAGE (http://page.sourceforge.net) for a few minutes, then came the author's comment: "For release 4.0, I spent about two months working with the ?Theme? part of Ttk and have had only partial success. I now believe that the ?Theme? part of Ttk is really a very poor piece of software at all levels - concept, implementation, and especially documentation. My guess is if it had been well documented it would have been recognized by even the author as junk. I find it hard to believe that the people who control Tcl/Tk allowed it in the code base. I continue to support ttk because of the paned window, notebook and treeview widgets." And ttk seems to be a major attraction that keeps people coming back to tk for the looks. This worries me very much about whether I should start a gui app using python. Because if ttk is not a "mature" technology, I'd avoid premature adoption. If ttk is out of the question, tkinter will be too. I'd then be forced to use a 3rd-party solution like wx or qt, which I really don't want to see. Anyways, this is just some concerns that I hope someone may give his/her opinions about. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From kw at codebykevin.com Sat May 18 11:32:04 2013 From: kw at codebykevin.com (Kevin Walzer) Date: Sat, 18 May 2013 11:32:04 -0400 Subject: Future standard GUI library In-Reply-To: References: Message-ID: Hello, On 5/18/13 10:03 AM, Beinan Li wrote: > I know this may sound a silly question because no one can see the > future. But ... > Do you think tkinter is going to be the standard python built-in gui > solution as long as python exists? I don't see any significant clamoring among the Python core developers to make a change. > > I couldn't help but wonder if wx or PySide receives better py2 and py3 > support, or anything else that prevent > them from getting into the standard python distributions, whether or not > this scene could start to shift ... I am not going to engage in the old UI toolkit flame ware; I will limit myself to factual observations and a few opinions about Tkinter without placing it against other toolkits. Python has the PEP process for suggesting changes to the core language and libraries. Changing the default UI toolkit would require someone to submit a proposal, get it approved, provide the implementation, and commit to maintaining the implementation over the long term. You propose it, you own it. Thus far no one has done this. I would think the only person who would be in a position to propose wxPython would be Robin Dunn since he is the primary copyright holder, and to my knowledge he has never done so. As for Pyside, it was not part of the transition of Qt from Nokia to Digia, and so it appears to be orphaned. PyQt might be an alternative, but I don't think Phil Thompson would ever submit it, as it would likely affect his livelihood. Given these facts, it's safe to say that Tkinter will remain the default GUI toolkit in the stdlib for some years to come. > > I believe this "which one of tkinter, wx, qt, is the best gui toolkit > for python" flame war has been going on > for ages. I love the fact that python ships a built-in gui solution > which makes shipping a pure-python desktop > application a viable choice. But tkinter does not appear to be the most > time-saving way to write a gui app. > The layout designer support, for one, is next to zero. I tried many > 3rd-party designers > and loved PAGE (http://page.sourceforge.net) for a few minutes, then > came the author's comment: PAGE strikes me as an odd choice for a Python developer to develop a Tk UI. It's a descendent of the old Visual Tcl tool, and is run from Tcl and not Python. VTcl was always famous for producing a huge pot of spaghetti UI code that couldn't be easily modified outside the tool. I have also tried many Tk UI tools including VTcl, SpecTcl, and the orphaned tool from ActiveState, and have concluded that writing my own code is both simpler and faster. As always, your mileage may vary. > > "For release 4.0, I spent about two months working with the ?Theme? part > of Ttk and have had only partial success. I now believe that the ?Theme? > part of Ttk is really a very poor piece of software at all levels - > concept, implementation, and especially documentation. My guess is if it > had been well documented it would have been recognized by even the > author as junk. I find it hard to believe that the people who control > Tcl/Tk allowed it in the code base. I continue to support ttk because of > the paned window, notebook and treeview widgets." The implementation of the ttk widgets is quite complex--that, in turn, makes their documentation complex, and the complexity is a drawback. It's hard to customize the ttk themes, especially compared to customizing standard Tk widgets. I'm one of the core Tk developers, and I don't fully understand the themed widgets' internals or how to customize them. But it's not fair to say that they are "junk." The author of PAGE may think so, but many would disagree. I think the widgets work quite well, especially if used in their default mode. It's difficult to overstate the improvement in the look and feel of my Tk apps on the Mac when I switched to using the themed widgets. > > And ttk seems to be a major attraction that keeps people coming back to > tk for the looks. This worries me very much > about whether I should start a gui app using python. Because if ttk is > not a "mature" technology, I'd avoid premature adoption. > If ttk is out of the question, tkinter will be too. I'd then be forced > to use a 3rd-party solution like wx or qt, which I really don't want to see. ttk is a mature technology. The initial specification, code, and docs were developed by Joe English nearly a decade ago (see http://tktable.sourceforge.net/tile/tile-tcl2004.pdf), and they had been accepted into Tcl/Tk's core in version 8.5 (released in 2007). The earliest work on integrating these widgets into Python began in 2008, and they were formally released in 2010. I would say that ttk is mature, stable, and unlikely to undergo radical change in the future. Guilherme Polo has done superb work in integrating the themed widgets into Python--it's the most significant UI advance in Python's stdlib in years. You are quite safe in developing against this API, unless your taste simply does not run to using the ttk widgets. Hope this helps, Kevin -- Kevin Walzer Code by Kevin/Mobile Code by Kevin http://www.codebykevin.com http://www.wtmobilesoftware.com From steve+comp.lang.python at pearwood.info Sat May 18 17:25:47 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 May 2013 21:25:47 GMT Subject: Future standard GUI library References: Message-ID: <5197f1da$0$29997$c3e8da3$5496439d@news.astraweb.com> On Sat, 18 May 2013 10:03:02 -0400, Beinan Li wrote: > Do you think tkinter is going to be the standard python built-in gui > solution as long as python exists? Probably. > I couldn't help but wonder if wx or PySide receives better py2 and py3 > support, or anything else that prevent them from getting into the > standard python distributions, whether or not this scene could start to > shift ... One of the major issues preventing projects being added to the standard library is the maintenance schedule. Python's schedule for new releases is quite conservative and slow. If, say, wxPython was added to the std lib, it would have to follow Python's schedule: * most backwards incompatible changes would be forbidden; * those that are allowed would require a minimum of two major releases (three years) before removing functionality; * about four bug-fix releases (I think?) per year. If a project is used to (say) weekly releases, dropping down to Python's schedule can be rather painful. Once something has been added to the standard library, it more or less has to have a stable API, become conservative about changes, and care more about backwards-compatibility than new features. Stability and consistency become paramount. Many excellent, popular packages cannot live under those restrictions, and so will never be part of the standard library. Tkinter is good in this regard, because it is a wrapper around Tk/Tcl, which is equally stable and conservative as Python. Possibly even more so. -- Steven From llanitedave at veawb.coop Sat May 18 23:01:31 2013 From: llanitedave at veawb.coop (llanitedave) Date: Sat, 18 May 2013 20:01:31 -0700 (PDT) Subject: Future standard GUI library In-Reply-To: References: Message-ID: <8c3c3ef4-f861-4363-b331-fec0a5782c45@googlegroups.com> I'm curious about how commonly tkinter is actually used among Python app developers as compared to wx, Pyside, or PyQT. I get the impression that more distributed apps are built with wxPython, at least, than tkinter. My impression is far from actual knowledge, of course. From kw at codebykevin.com Sun May 19 08:57:07 2013 From: kw at codebykevin.com (Kevin Walzer) Date: Sun, 19 May 2013 08:57:07 -0400 Subject: Future standard GUI library In-Reply-To: <8c3c3ef4-f861-4363-b331-fec0a5782c45@googlegroups.com> References: <8c3c3ef4-f861-4363-b331-fec0a5782c45@googlegroups.com> Message-ID: On 5/18/13 11:01 PM, llanitedave wrote: > I'm curious about how commonly tkinter is actually used among Python app developers as compared to wx, Pyside, or PyQT. I get the impression that more distributed apps are built with wxPython, at least, than tkinter. My impression is far from actual knowledge, of course. > I have two commercial apps developed with Tkinter: http://www.codebykevin.com/phynchronicity.html http://www.codebykevin.com/quickwho.html --Kevin -- Kevin Walzer Code by Kevin/Mobile Code by Kevin http://www.codebykevin.com http://www.wtmobilesoftware.com From feliphil at gmx.net Wed May 22 09:42:33 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Wed, 22 May 2013 15:42:33 +0200 Subject: Future standard GUI library References: Message-ID: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> > I know this may sound a silly question because no one can see the > future. But ... > Do you think tkinter is going to be the standard python built-in gui > solution as long as python exists? "Standard built-in" maybe, but by far most people who need a GUI for an actual application will keep using something else. > I couldn't help but wonder if wx or PySide receives better py2 and py3 > support, or anything else that prevent > them from getting into the standard python distributions, whether or > not this scene could start to shift ... Didn't Pyside have serious trouble recently, requiring a reanimation of the project? > I believe this "which one of tkinter, wx, qt, is the best gui toolkit > for python" flame war has been going on > for ages. If (Py)Qt wasn't so freaking fat, it might be the best. If wxPython had a more pythonic (and stable?) API, it might be the best. If PyGTK was more native on Windows and native at all on MacOS X, it might be the best. If PyGUI was more extensive, it might be the best. > This worries me very much about whether I should start a gui app > using python. What other open-source cross-platform programming language choices do yo have. Java? For GUIs? Excuse me while I vomit. C++? As a language for human beings? Oops, I have to throw up again. Sincerely, Wolfgang From rosuav at gmail.com Wed May 22 10:24:15 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 00:24:15 +1000 Subject: Future standard GUI library In-Reply-To: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> Message-ID: On Wed, May 22, 2013 at 11:42 PM, Wolfgang Keller wrote: > What other open-source cross-platform programming language choices do yo > have. > > Java? For GUIs? Excuse me while I vomit. > > C++? As a language for human beings? Oops, I have to throw up again. I personally like using Pike and GTK, so if I were to try a cross-platform Python GUI project, I'd probably give PyGTK a shot. But there's another option that is available to every platform and (practially) every high level language: the web browser. Make your app serve HTTP and do up your UI in HTML5/CSS3 - your facilities are pretty extensive. Plus you get networking support for free! Obviously this option isn't for everyone, but don't discount it out of hand. ChrisA From llanitedave at veawb.coop Wed May 22 22:31:55 2013 From: llanitedave at veawb.coop (llanitedave) Date: Wed, 22 May 2013 19:31:55 -0700 (PDT) Subject: Future standard GUI library In-Reply-To: References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> Message-ID: <19c925fc-dbf8-417c-9298-f682c9b2e558@googlegroups.com> On Wednesday, May 22, 2013 7:24:15 AM UTC-7, Chris Angelico wrote: > On Wed, May 22, 2013 at 11:42 PM, Wolfgang Keller wrote: > > > What other open-source cross-platform programming language choices do yo > > > have. > > > > > > Java? For GUIs? Excuse me while I vomit. > > > > > > C++? As a language for human beings? Oops, I have to throw up again. > > > > I personally like using Pike and GTK, so if I were to try a > > cross-platform Python GUI project, I'd probably give PyGTK a shot. But > > there's another option that is available to every platform and > > (practially) every high level language: the web browser. Make your app > > serve HTTP and do up your UI in HTML5/CSS3 - your facilities are > > pretty extensive. Plus you get networking support for free! Obviously > > this option isn't for everyone, but don't discount it out of hand. > > > > ChrisA I've been thinking about that myself for some future app ideas. If you have a stand-alone app working from your web browser, don't you need an embedded web server to utilize the file system? Is a system like Django for an app overkill? Or is its embedded development server underkill for a single-user browser-based application? From fabiosantosart at gmail.com Thu May 23 02:43:15 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 23 May 2013 07:43:15 +0100 Subject: Future standard GUI library In-Reply-To: <19c925fc-dbf8-417c-9298-f682c9b2e558@googlegroups.com> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <19c925fc-dbf8-417c-9298-f682c9b2e558@googlegroups.com> Message-ID: On 23 May 2013 03:39, "llanitedave" wrote: > > On Wednesday, May 22, 2013 7:24:15 AM UTC-7, Chris Angelico wrote: > > On Wed, May 22, 2013 at 11:42 PM, Wolfgang Keller wrote: ... > > there's another option that is available to every platform and > > (practially) every high level language: the web browser. Make your app > > serve HTTP and do up your UI in HTML5/CSS3 - your facilities are > > pretty extensive. Plus you get networking support for free! Obviously > > this option isn't for everyone, but don't discount it out of hand. > > > > ChrisA > > I've been thinking about that myself for some future app ideas. If you have a stand-alone app working from your web browser, don't you need an embedded web server to utilize the file system? Is a system like Django for an app overkill? Or is its embedded development server underkill for a single-user browser-based application? > -- > http://mail.python.org/mailman/listinfo/python-list JavaScript has this: http://appjs.org/ It's a node.js server app serving a folder of plain old HTML files to a chrome embedded browser. You can code in a node.js server using anything you like, serve requests for your client app (or use the server code directly, you can just put the functions you would like to share with the client in the window object), etc. I'm using it for a peer to peer configurable application. To do that I serve up the web application for me and my peers (by using a regular server instead of the fake server it comes with), and the browser is just a client which can connect wherever I want it to. Someone should fork this and make it work in python. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu May 23 02:48:53 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 16:48:53 +1000 Subject: Future standard GUI library In-Reply-To: References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <19c925fc-dbf8-417c-9298-f682c9b2e558@googlegroups.com> Message-ID: On Thu, May 23, 2013 at 4:43 PM, F?bio Santos wrote: > On 23 May 2013 03:39, "llanitedave" wrote: >> On Wednesday, May 22, 2013 7:24:15 AM UTC-7, Chris Angelico wrote: >> > there's another option that is available to every platform and >> > (practially) every high level language: the web browser. Make your app >> > serve HTTP and do up your UI in HTML5/CSS3 - your facilities are >> > pretty extensive. Plus you get networking support for free! Obviously >> > this option isn't for everyone, but don't discount it out of hand. >> > >> > ChrisA >> >> I've been thinking about that myself for some future app ideas. If you >> have a stand-alone app working from your web browser, don't you need an >> embedded web server to utilize the file system? Is a system like Django for >> an app overkill? Or is its embedded development server underkill for a >> single-user browser-based application? >> -- >> http://mail.python.org/mailman/listinfo/python-list > > JavaScript has this: > > http://appjs.org/ > > It's a node.js server app serving a folder of plain old HTML files to a > chrome embedded browser. > > You can code in a node.js server using anything you like, serve requests for > your client app (or use the server code directly, you can just put the > functions you would like to share with the client in the window object), > etc. Many high level languages today come with simple HTTP server modules. They may not scale "to infinity and beyond", but they'll work fine for a single-user system using a browser for its UI. Chances are they'll do well for everything up to a single CPU core. Depends on the language and library, of course. ChrisA From carlosnepomuceno at outlook.com Thu May 23 02:58:02 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 09:58:02 +0300 Subject: Future standard GUI library In-Reply-To: <19c925fc-dbf8-417c-9298-f682c9b2e558@googlegroups.com> References: , <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net>, , <19c925fc-dbf8-417c-9298-f682c9b2e558@googlegroups.com> Message-ID: You don't! If your app needs local content just use a regular open() (or your browser) to read the files and render them as you see fit. For remote content you just need the 'urllib2' module or something like 'requests' module to get the data. ---------------------------------------- > Date: Wed, 22 May 2013 19:31:55 -0700 > Subject: Re: Future standard GUI library > From: llanitedave at veawb.coop [...] > > I've been thinking about that myself for some future app ideas. If you have a stand-alone app working from your web browser, don't you need an embedded web server to utilize the file system? Is a system like Django for an app overkill? Or is its embedded development server underkill for a single-user browser-based application? > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Thu May 23 03:03:59 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 17:03:59 +1000 Subject: Future standard GUI library In-Reply-To: References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <19c925fc-dbf8-417c-9298-f682c9b2e558@googlegroups.com> Message-ID: On Thu, May 23, 2013 at 4:58 PM, Carlos Nepomuceno wrote: > You don't! If your app needs local content just use a regular open() (or your browser) to read the files and render them as you see fit. > > For remote content you just need the 'urllib2' module or something like 'requests' module to get the data. BTW, forgot the link. The part you DO need is something like this: http://docs.python.org/3/library/http.server.html It takes care of the irrelevant and lets you just write your app. The same sort of thing is available in quite a few languages. Great for knocking something together quickly; not designed for thousand-TPS web servers. ChrisA From fabiosantosart at gmail.com Thu May 23 03:08:12 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 23 May 2013 08:08:12 +0100 Subject: Future standard GUI library In-Reply-To: References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <19c925fc-dbf8-417c-9298-f682c9b2e558@googlegroups.com> Message-ID: It would be way more practical to have an embedded browser. Appjs doesn't even occupy a port on the client. We could totally benefit from that. Django applications practically make themselves. On 23 May 2013 08:02, "Carlos Nepomuceno" wrote: > You don't! If your app needs local content just use a regular open() (or > your browser) to read the files and render them as you see fit. > > For remote content you just need the 'urllib2' module or something like > 'requests' module to get the data. > > ---------------------------------------- > > Date: Wed, 22 May 2013 19:31:55 -0700 > > Subject: Re: Future standard GUI library > > From: llanitedave at veawb.coop > [...] > > > > I've been thinking about that myself for some future app ideas. If you > have a stand-alone app working from your web browser, don't you need an > embedded web server to utilize the file system? Is a system like Django for > an app overkill? Or is its embedded development server underkill for a > single-user browser-based application? > > -- > > http://mail.python.org/mailman/listinfo/python-list > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From feliphil at gmx.net Thu May 23 11:41:45 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Thu, 23 May 2013 17:41:45 +0200 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> Message-ID: <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> > But there's another option that is available to every platform and > (practially) every high level language: the web browser. Make your app > serve HTTP and do up your UI in HTML5/CSS3 - your facilities are > pretty extensive. Plus you get networking support for free! Obviously > this option isn't for everyone, but don't discount it out of hand. Both the concept and actually implemented examples of so-called "web applications" prove that they are just plain garbage and hopelessly unusable for anything remotely resembling actual screenwork. HTML forms may be at best useful for "web shops", but for actual screenwork, HTML is not a valid GUI, no matter how much javascript you add to it. Sincerely, Wolfgang From rosuav at gmail.com Thu May 23 11:51:32 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 01:51:32 +1000 Subject: Future standard GUI library In-Reply-To: <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> Message-ID: On Fri, May 24, 2013 at 1:41 AM, Wolfgang Keller wrote: >> But there's another option that is available to every platform and >> (practially) every high level language: the web browser. Make your app >> serve HTTP and do up your UI in HTML5/CSS3 - your facilities are >> pretty extensive. Plus you get networking support for free! Obviously >> this option isn't for everyone, but don't discount it out of hand. > > Both the concept and actually implemented examples of so-called "web > applications" prove that they are just plain garbage and hopelessly > unusable for anything remotely resembling actual screenwork. > > HTML forms may be at best useful for "web shops", but for actual > screenwork, HTML is not a valid GUI, no matter how much javascript you > add to it. All depends on your requirements. For the Yosemite Project, I wanted the networking aspect, so the web browser UI was a good one. It's been working beautifully for... what, four or six years now, I think; and it's just a few pages of Python. ChrisA From invalid at invalid.invalid Thu May 23 16:37:02 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 23 May 2013 20:37:02 +0000 (UTC) Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> Message-ID: On 2013-05-23, Wolfgang Keller wrote: >> But there's another option that is available to every platform and >> (practially) every high level language: the web browser. Make your app >> serve HTTP and do up your UI in HTML5/CSS3 - your facilities are >> pretty extensive. Plus you get networking support for free! Obviously >> this option isn't for everyone, but don't discount it out of hand. > > Both the concept and actually implemented examples of so-called "web > applications" prove that they are just plain garbage and hopelessly > unusable for anything remotely resembling actual screenwork. What is "screenwork"? -- Grant Edwards grant.b.edwards Yow! Am I elected yet? at gmail.com From feliphil at gmx.net Tue May 28 13:28:40 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Tue, 28 May 2013 19:28:40 +0200 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> Message-ID: <20130528192840.5913f7b6181464616061c015@gmx.net> > What is "screenwork"? Actually productive work of significant intensity at a computer screen. As opposed to leisurely "clicking around" like managers, administrators or home users (gaming, "webbing",...) do. Sincerely, Wolfgang From invalid at invalid.invalid Tue May 28 14:25:08 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 18:25:08 +0000 (UTC) Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130528192840.5913f7b6181464616061c015@gmx.net> Message-ID: On 2013-05-28, Wolfgang Keller wrote: > Actually productive work of significant intensity at a computer screen. Oh. You mean emacs. -- Grant Edwards grant.b.edwards Yow! Will it improve my at CASH FLOW? gmail.com From dihedral88888 at gmail.com Tue May 28 19:19:20 2013 From: dihedral88888 at gmail.com (88888 Dihedral) Date: Tue, 28 May 2013 16:19:20 -0700 (PDT) Subject: Future standard GUI library In-Reply-To: References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130528192840.5913f7b6181464616061c015@gmx.net> Message-ID: <5d7a9b77-963f-4964-a9f0-83295dc1b30e@googlegroups.com> Grant Edwards? 2013?5?29????UTC+8??2?25?08???? > On 2013-05-28, Wolfgang Keller wrote: > > > > > Actually productive work of significant intensity at a computer screen. > > > > Oh. You mean emacs. > > > > -- > > Grant Edwards grant.b.edwards Yow! Will it improve my > > at CASH FLOW? > > gmail.com Check http://www.pyamf.org/index.html for Python +flash in browser applications. Annyway the virtual server part can be obtained from the Adobe system in the cloudy services or whatever. As long as it is scalable in fees to provide services to web users in PCs or mobile phones, then it might be very profitable than 10 to 15 years ago. From feliphil at gmx.net Sun May 26 13:43:10 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Sun, 26 May 2013 19:43:10 +0200 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> Message-ID: <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> > > Both the concept and actually implemented examples of so-called "web > > applications" prove that they are just plain garbage and hopelessly > > unusable for anything remotely resembling actual screenwork. > > > > HTML forms may be at best useful for "web shops", but for actual > > screenwork, HTML is not a valid GUI, no matter how much javascript > > you add to it. > > All depends on your requirements. Obivously, if your requirements are to be compliant with latest IT management fads and to not give a darn for end-user productivity or even legal requirements for screen workplace ergonomics... > For the Yosemite Project, I wanted the networking aspect, so the web > browser UI was a good one. >From the description this looks like a simble database CRUD application. Somethign like that is definitely easier to implement and to deploy and a *lot* more functional with any of the RAD frameworks available for Python. And just like HTML never was a valid GUI framework and never will be one, HTTP will never be a suitable transport layer for a RPC protocol. Sincerely, Wolfgang From roy at panix.com Sun May 26 14:16:02 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 14:16:02 -0400 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: In article <20130526194310.9cdb1be80b42c7fdf0ba502f at gmx.net>, Wolfgang Keller wrote: > HTTP will never be a suitable transport layer for a RPC protocol. What, in particular, is wrong with HTTP for doing RPC? RPC is pretty straight-forward. Take this method, run it over there, with these arguments, and give me back the result. HTTP handles that just fine, with your choice of XML, JSON, or whatever turns you on for the content encoding. There are protocols that are more efficient (mostly binary ones like Thrift and Protocol Buffers), but for a lot of things, the simplicity and convenience of HTTP is worth than the efficiency hit. It's really nice to be able to slap together HTTP components like load balancers and caches by just editing a few config files. From feliphil at gmx.net Mon May 27 11:31:23 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Mon, 27 May 2013 17:31:23 +0200 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: <20130527173123.8ad4fc4af177161c8478be1a@gmx.net> > HTTP handles that just fine, with your choice of XML, And XML is definitely not suitable as a marshalling format for a RPC protocol. XML-over-HTTP is a true cerebral flatulance of some hopelessly clueless moron. Sincerely, Wolfgang From torriem at gmail.com Mon May 27 13:13:34 2013 From: torriem at gmail.com (Michael Torrie) Date: Mon, 27 May 2013 11:13:34 -0600 Subject: Future standard GUI library In-Reply-To: <20130527173123.8ad4fc4af177161c8478be1a@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527173123.8ad4fc4af177161c8478be1a@gmx.net> Message-ID: <51A3943E.8070107@gmail.com> On 05/27/2013 09:31 AM, Wolfgang Keller wrote: >> HTTP handles that just fine, with your choice of XML, > > And XML is definitely not suitable as a marshalling format for a RPC > protocol. > > XML-over-HTTP is a true cerebral flatulance of some hopelessly clueless > moron. Hmm. Well I think there are a lot of very smart developers that are using xml to marshal rpc and it's working fine. So either they know something you don't, or maybe they are just busy making code that functions and functions pretty well, instead of making inflammatory statements on USENET. Sure XML is not very efficient or compact, but it does handle unicode intrinsically through standard encodings, and a plethora of parsers makes it a decent choice. It's like what they say about the book: "for though it has many omissions and contains much that is apocryphal, or at least wildly inaccurate, it scores over the older, more pedestrian work in two important respects. First, it is slightly cheaper; and second, it has the words Don't Panic inscribed in large friendly letters on its cover." I've used LBER-encoded wire protocol before, and it was fine, but hard to debug on the wire, and didn't haven anything in it to handle unicode except base64-encoding utf8 byte streams. That said, all this reminds me of a good saying, "XML is like violence; if it doesn't solve your problem you're not using enough of it." Fortunately none of this really matters. Who cares what is used to marshall RPC over the wire? As a developer I certainly don't care. I'll use whatever is convenient and well supported. One reason I do like xmlrpc, though, because it's pretty much available to all web-based back ends for free (or at little cost) and you don't need to set up a special server process to handle it, or deal with odd port numbers that might be blocked, doing ssl yourself, etc. Having HTTP do the transport frees me from having to worry about all that, since in the case of a web app I am already using a web server. From rosuav at gmail.com Mon May 27 18:21:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 28 May 2013 08:21:25 +1000 Subject: Future standard GUI library In-Reply-To: <51A3943E.8070107@gmail.com> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527173123.8ad4fc4af177161c8478be1a@gmx.net> <51A3943E.8070107@gmail.com> Message-ID: On Tue, May 28, 2013 at 3:13 AM, Michael Torrie wrote: > On 05/27/2013 09:31 AM, Wolfgang Keller wrote: >>> HTTP handles that just fine, with your choice of XML, >> >> And XML is definitely not suitable as a marshalling format for a RPC >> protocol. >> >> XML-over-HTTP is a true cerebral flatulance of some hopelessly clueless >> moron. > > Hmm. Well I think there are a lot of very smart developers that are > using xml to marshal rpc and it's working fine. So either they know > something you don't, or maybe they are just busy making code that > functions and functions pretty well, instead of making inflammatory > statements on USENET. Sure XML is not very efficient or compact, but it > does handle unicode intrinsically through standard encodings, and a > plethora of parsers makes it a decent choice. I'll use XML when I have to, but if I'm inventing my own protocol, nope. There are just too many quirks with it. How do you represent an empty string named Foo? or equivalently How do you represent an empty list named Foo? The same way. How do you represent an empty dict/mapping named Foo? Lemme look up my documentation... ah, the same way. Does this seem right to you? ChrisA From roy at panix.com Mon May 27 19:10:35 2013 From: roy at panix.com (Roy Smith) Date: Mon, 27 May 2013 19:10:35 -0400 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527173123.8ad4fc4af177161c8478be1a@gmx.net> <51A3943E.8070107@gmail.com> Message-ID: In article , Chris Angelico wrote: > I'll use XML when I have to, but if I'm inventing my own protocol, > nope. There are just too many quirks with it. How do you represent an > empty string named Foo? > > > > or equivalently > > > > How do you represent an empty list named Foo? The same way. How do you > represent an empty dict/mapping named Foo? Lemme look up my > documentation... ah, the same way. Does this seem right to > you? XML doesn't represent strings, or lists, or dicts. It represents trees of nodes with labels. If you wish to invent some richer semantic meaning to impose on those nodes, that's up to you. JSON isn't better or worse than XML. They solve different problems. From rosuav at gmail.com Tue May 28 03:11:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 28 May 2013 17:11:55 +1000 Subject: Future standard GUI library In-Reply-To: References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527173123.8ad4fc4af177161c8478be1a@gmx.net> <51A3943E.8070107@gmail.com> Message-ID: On Tue, May 28, 2013 at 9:10 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> I'll use XML when I have to, but if I'm inventing my own protocol, >> nope. There are just too many quirks with it. How do you represent an >> empty string named Foo? >> >> >> >> or equivalently >> >> >> >> How do you represent an empty list named Foo? The same way. How do you >> represent an empty dict/mapping named Foo? Lemme look up my >> documentation... ah, the same way. Does this seem right to >> you? > > XML doesn't represent strings, or lists, or dicts. It represents trees > of nodes with labels. If you wish to invent some richer semantic > meaning to impose on those nodes, that's up to you. Sure it doesn't, but it's very often used that way. So I guess what I'm really saying is that XML is wrong for 90% of the places it's used. ChrisA From dihedral88888 at gmail.com Tue May 28 05:40:40 2013 From: dihedral88888 at gmail.com (88888 Dihedral) Date: Tue, 28 May 2013 02:40:40 -0700 (PDT) Subject: Future standard GUI library In-Reply-To: References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527173123.8ad4fc4af177161c8478be1a@gmx.net> <51A3943E.8070107@gmail.com> Message-ID: <077a358c-81b8-4529-8232-40cbaa331012@googlegroups.com> Chris Angelico? 2013?5?28????UTC+8??3?11?55???? > On Tue, May 28, 2013 at 9:10 AM, Roy Smith wrote: > > > In article , > > > Chris Angelico wrote: > > > > > >> I'll use XML when I have to, but if I'm inventing my own protocol, > > >> nope. There are just too many quirks with it. How do you represent an > > >> empty string named Foo? > > >> > > >> > > >> > > >> or equivalently > > >> > > >> > > >> > > >> How do you represent an empty list named Foo? The same way. How do you > > >> represent an empty dict/mapping named Foo? Lemme look up my > > >> documentation... ah, the same way. Does this seem right to > > >> you? > > > > > > XML doesn't represent strings, or lists, or dicts. It represents trees > > > of nodes with labels. If you wish to invent some richer semantic > > > meaning to impose on those nodes, that's up to you. > > > > Sure it doesn't, but it's very often used that way. So I guess what > > I'm really saying is that XML is wrong for 90% of the places it's > > used. > > > > ChrisA Please check the annual sales in Ebay or similar sites and the advertizement fees collected by Google and Yahoo. From denismfmcmahon at gmail.com Mon May 27 21:01:47 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Tue, 28 May 2013 01:01:47 +0000 (UTC) Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527173123.8ad4fc4af177161c8478be1a@gmx.net> <51A3943E.8070107@gmail.com> Message-ID: On Tue, 28 May 2013 08:21:25 +1000, Chris Angelico wrote: > I'll use XML when I have to, but if I'm inventing my own protocol, > nope. There are just too many quirks with it. How do you represent an > empty string named Foo? > > or equivalently > > How do you represent an empty list named Foo? The same way. How do you > represent an empty dict/mapping named Foo? Lemme look up my > documentation... ah, the same way. Does this seem right to > you? -- Denis McMahon, denismfmcmahon at gmail.com From carlosnepomuceno at outlook.com Sun May 26 14:32:56 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 21:32:56 +0300 Subject: Future standard GUI library In-Reply-To: <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> References: , <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net>, , <20130523174145.22a6c46f586b0a1f656d2412@gmx.net>, , <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: ---------------------------------------- > From: feliphil at gmx.net > Subject: Re: Future standard GUI library > Date: Sun, 26 May 2013 19:43:10 +0200 > To: python-list at python.org [...] > one, HTTP will never be a suitable transport layer for a RPC protocol. > > Sincerely, > > Wolfgang Please give me an example of a "suitable transport layer for a RPC protocol". From rosuav at gmail.com Sun May 26 14:56:19 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 04:56:19 +1000 Subject: Future standard GUI library In-Reply-To: <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: On Mon, May 27, 2013 at 3:43 AM, Wolfgang Keller wrote: >> For the Yosemite Project, I wanted the networking aspect, so the web >> browser UI was a good one. > > From the description this looks like a simble database CRUD > application. Somethign like that is definitely easier to implement and > to deploy and a *lot* more functional with any of the RAD frameworks > available for Python. > > And just like HTML never was a valid GUI framework and never will be > one, HTTP will never be a suitable transport layer for a RPC protocol. No, it's basically a modified version of a file browser; you get a directory listing, you can browse subdirectories, and selecting a file causes an action to happen on the server. You also have some "action buttons" that provide a measure of control: click a button and something is done (eg a key is pressed, or a 'killall' command is fired). The browser works just fine. ChrisA From torriem at gmail.com Sun May 26 15:41:44 2013 From: torriem at gmail.com (Michael Torrie) Date: Sun, 26 May 2013 13:41:44 -0600 Subject: Future standard GUI library In-Reply-To: <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: <51A26578.5040402@gmail.com> On 05/26/2013 11:43 AM, Wolfgang Keller wrote: > And just like HTML never was a valid GUI framework and never will be > one, HTTP will never be a suitable transport layer for a RPC protocol. On good thing web development has brought us is the knowledge that modularization and layers are a brilliant idea. Your back end exposes services and business logic, and your front end can be in HTMLv5 and Javascript, or QtQuick, PyGTK, or Visual Studio. If you do need a native interface, it's a heck of a lot easier to rewrite just the frontend then the entire stack. Who cares how the RPC is done; that's an implementation detail. HTTP does happen to work well though. Why do you say it is not suitable? > From the description this looks like a simble database CRUD > application. Somethign like that is definitely easier to implement and > to deploy and a *lot* more functional with any of the RAD frameworks > available for Python. Chuckle. Simple CRUD, eh. Almost all apps involve database CRUD interactions. And often in highly complex ways using business logic. Maybe it would have been faster to develop, but ultimately less useful and require more development time in the long run. suppose I now want the app natively on my phone (because that's all the rage). It's an iPhone. Oh. Apple doesn't support Python. Okay, rewrite the works, including business logic, in Objective C. Now I want it on my android phone. Oops rewrite the stack again in Java. Since his application by nature is network oriented (client/server I presume since he mentions multiple users), sticking it on a web server and having the front end be separate (be it HTML or not) gives him the flexibility to rapidly build native "screenable" UIs for his app on any platform he chooses, something your philosophy does not allow for. From ian at feete.org Sun May 26 17:02:02 2013 From: ian at feete.org (Ian Foote) Date: Sun, 26 May 2013 22:02:02 +0100 Subject: Future standard GUI library In-Reply-To: <51A26578.5040402@gmail.com> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <51A26578.5040402@gmail.com> Message-ID: <51A2784A.2060101@feete.org> On 26/05/13 20:41, Michael Torrie wrote: > On 05/26/2013 11:43 AM, Wolfgang Keller wrote: > > Maybe it would have been faster to develop, but ultimately less useful > and require more development time in the long run. suppose I now want > the app natively on my phone (because that's all the rage). It's an > iPhone. Oh. Apple doesn't support Python. Okay, rewrite the works, > including business logic, in Objective C. Now I want it on my android > phone. Oops rewrite the stack again in Java. > Kivy (http://kivy.org) is a python library that can be used to write python apps for Linux, Windows, MaxOSX, Android and IOS. Regards, Ian F From rosuav at gmail.com Sun May 26 18:09:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 08:09:38 +1000 Subject: Future standard GUI library In-Reply-To: <51A26578.5040402@gmail.com> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <51A26578.5040402@gmail.com> Message-ID: On Mon, May 27, 2013 at 5:41 AM, Michael Torrie wrote: > Chuckle. Simple CRUD, eh. Almost all apps involve database CRUD > interactions. And often in highly complex ways using business logic. Right. Sturgeon's Law of Applications. ChrisA From roy at panix.com Sun May 26 15:45:39 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 15:45:39 -0400 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: In article , Michael Torrie wrote: > On good thing web development has brought us is the knowledge that > modularization and layers are a brilliant idea. Modularization and layers were a brilliant idea long before the web came around. From torriem at gmail.com Sun May 26 17:13:09 2013 From: torriem at gmail.com (Michael Torrie) Date: Sun, 26 May 2013 15:13:09 -0600 Subject: Future standard GUI library In-Reply-To: References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: <51A27AE5.6050605@gmail.com> On 05/26/2013 01:45 PM, Roy Smith wrote: > In article , > Michael Torrie wrote: > >> On good thing web development has brought us is the knowledge that >> modularization and layers are a brilliant idea. > > Modularization and layers were a brilliant idea long before the web came > around. True. Though it seems like it fell out of fashion for a long time. I went to school before the advent of web development and though we talked about modularization and coupling in software engineering, really it was all rather monolithic. Web development changed our focus back to where it should have been all along. From invalid at invalid.invalid Tue May 28 11:40:25 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 15:40:25 +0000 (UTC) Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: On 2013-05-26, Roy Smith wrote: > Michael Torrie wrote: > >> On good thing web development has brought us is the knowledge that >> modularization and layers are a brilliant idea. > > Modularization and layers were a brilliant idea long before the web came > around. Once again USENET proves to be an unsuitable RPC protocol for implementing irony. :) [OTOH, perhaps Micheal wasn't being ironic...] -- Grant Edwards grant.b.edwards Yow! Am I having fun yet? at gmail.com From feliphil at gmx.net Mon May 27 11:22:50 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Mon, 27 May 2013 17:22:50 +0200 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: <20130527172250.a8b0ce44f29398d63a4ec650@gmx.net> > Your back end exposes services and business logic, and your front end > can be in HTMLv5 and Javascript, or QtQuick, PyGTK, or Visual > Studio. If you do need a native interface, it's a heck of a lot > easier to rewrite just the frontend then the entire stack. Any decent database CRUD framework will allow to implement the application model as "behaviour complete" domain objects, with a persistence layer totally independent from the GUI layer. In many Python RAD frameworks, this is done using SQLalchemy. > Who cares how the RPC is done; As an end-user I do care for how much an application makes we watch the cursor animation. > that's an implementation detail. HTTP does happen to work well > though. Why do you say it is not suitable? Because from personal experience I know too well that it does definitely not "work well though". > suppose I now want the app natively on my phone (because that's all > the rage). It's an iPhone. Oh. Apple doesn't support Python. > Okay, rewrite the works, including business logic, in Objective C. > Now I want it on my android phone. Those are gadgets, not work tools. Sincerely, Wolfgang From torriem at gmail.com Mon May 27 13:16:21 2013 From: torriem at gmail.com (Michael Torrie) Date: Mon, 27 May 2013 11:16:21 -0600 Subject: Future standard GUI library In-Reply-To: <20130527172250.a8b0ce44f29398d63a4ec650@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527172250.a8b0ce44f29398d63a4ec650@gmx.net> Message-ID: <51A394E5.1010409@gmail.com> On 05/27/2013 09:22 AM, Wolfgang Keller wrote: >> suppose I now want the app natively on my phone (because that's all >> the rage). It's an iPhone. Oh. Apple doesn't support Python. >> Okay, rewrite the works, including business logic, in Objective C. >> Now I want it on my android phone. > > Those are gadgets, not work tools. As a professional programmer I'm afraid you're going to soon find yourself out of work if you really see things that way. I honestly used to feel that way about graphical user interfaces. From feliphil at gmx.net Thu May 30 12:40:45 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Thu, 30 May 2013 18:40:45 +0200 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527172250.a8b0ce44f29398d63a4ec650@gmx.net> Message-ID: <20130530184045.6d15530be70e18d96e5654ad@gmx.net> > >> suppose I now want the app natively on my phone (because that's all > >> the rage). It's an iPhone. Oh. Apple doesn't support Python. > >> Okay, rewrite the works, including business logic, in Objective C. > >> Now I want it on my android phone. > > > > Those are gadgets, not work tools. > > As a professional programmer I'm afraid you're going to soon find > yourself out of work if you really see things that way. As a "domain expert", I come from the end-user side of "enterprise applications" and again; those are not tools for screenworkers to get actual work done, but consumer crap for fad-driven gadget-addicted kids (regardless of nominal age). > I honestly used to feel that way about graphical user interfaces. A GUI that can not be used without taking the ten fingers off the keyboard is indeed entirely unusable for any half-proficient screenworker. And anyone doing actual productive screenwork every day for more than just a few months will inevitably (have to) get proficient (unless completely braindead). Sincerely, Wolfgang From rosuav at gmail.com Thu May 30 12:53:14 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 02:53:14 +1000 Subject: Future standard GUI library In-Reply-To: <20130530184045.6d15530be70e18d96e5654ad@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130527172250.a8b0ce44f29398d63a4ec650@gmx.net> <20130530184045.6d15530be70e18d96e5654ad@gmx.net> Message-ID: On Fri, May 31, 2013 at 2:40 AM, Wolfgang Keller wrote: > A GUI that can not be used without taking the ten fingers off the > keyboard is indeed entirely unusable for any half-proficient > screenworker. And anyone doing actual productive screenwork every day > for more than just a few months will inevitably (have to) get proficient > (unless completely braindead). My ten fingers stay on my keyboard, which looks somewhat thus: http://www.xbitlabs.com/images/mobile/lenovo-thinkpad-t61/keyboard.jpg See the red dot in the middle? Mouse. See the keys all around it? My hands are all over that. I can casually mouse and keyboard at the same time. Doesn't bother me. THIS is a professional programmer's workspace. :) ChrisA From feliphil at gmx.net Tue May 28 13:26:55 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Tue, 28 May 2013 19:26:55 +0200 Subject: Future standard GUI library References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> Message-ID: <20130528192655.08861babbd86e890634e6d90@gmx.net> > Please give me an example of a "suitable transport layer for a RPC > protocol". I won't give you an example, but just some very basic criteria: - It must be very efficient for very small "datagrams" - It must provide connections - For asynchronous programming it must provide for callbacks No RPC-over-HTTP protocol that I know of does this. Besides, no one needs RPC just to logically separate GUI and application layer. And between application logic and database, you use the native database API for the RDBMS in question, of course. The whole idea to centralise application logic (and even the GUI with "web applications") is backwards, it dates from the 70s/early 80s when desktop computers weren't able to run application logic. Today, to make an application responsive (minimise latencies and maximise throughput), it's just obvious to *de*-centralise as much as possible. In fact, if Postgres-R was available for production, you could even distribute the persistence and run an entirely "serverless" application. Sincerely, Wolfgang From torriem at gmail.com Tue May 28 14:16:20 2013 From: torriem at gmail.com (Michael Torrie) Date: Tue, 28 May 2013 12:16:20 -0600 Subject: Future standard GUI library In-Reply-To: <20130528192655.08861babbd86e890634e6d90@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130528192655.08861babbd86e890634e6d90@gmx.net> Message-ID: <51A4F474.7050908@gmail.com> On 05/28/2013 11:26 AM, Wolfgang Keller wrote: >> Please give me an example of a "suitable transport layer for a RPC >> protocol". > > I won't give you an example, but just some very basic criteria: > > - It must be very efficient for very small "datagrams" I won't argue for XML here, but sometimes space efficiency is simply a premature optimization. > - It must provide connections How would you do this? Connections can and do get dropped regularly. To rebuild connections you need another layer to track them. > - For asynchronous programming it must provide for callbacks What do you mean by this? A transport layer has nothing to do with callbacks. All a client can do is make the call, and wait for the answer. The client library can make it look asynchronous of course. And I suppose one could implement an RPC system using UDP where answer packets are dispatched as they come in. From carlosnepomuceno at outlook.com Tue May 28 17:17:13 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 29 May 2013 00:17:13 +0300 Subject: Future standard GUI library In-Reply-To: <20130528192655.08861babbd86e890634e6d90@gmx.net> References: , <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net>, , <20130523174145.22a6c46f586b0a1f656d2412@gmx.net>, , <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net>, , <20130528192655.08861babbd86e890634e6d90@gmx.net> Message-ID: ---------------------------------------- > From: feliphil at gmx.net > Subject: Re: Future standard GUI library > Date: Tue, 28 May 2013 19:26:55 +0200 > To: python-list at python.org > >> Please give me an example of a "suitable transport layer for a RPC >> protocol". > > I won't give you an example, but just some very basic criteria: > > - It must be very efficient for very small "datagrams" Interoperability is much more expensive than efficiency. That's why you won't get the most optimized protocol for speed or size. > - It must provide connections What do you mean? > - For asynchronous programming it must provide for callbacks > No RPC-over-HTTP protocol that I know of does this. XHR implements asynchronous callbacks over HTTP. > Besides, no one needs RPC just to logically separate GUI and > application layer. And between application logic and database, you use > the native database API for the RDBMS in question, of course. > > The whole idea to centralise application logic (and even the GUI with > "web applications") is backwards, it dates from the 70s/early 80s when > desktop computers weren't able to run application logic. Today, to make > an application responsive (minimise latencies and maximise throughput), > it's just obvious to *de*-centralise as much as possible. In fact, > if Postgres-R was available for production, you could even distribute > the persistence and run an entirely "serverless" application. "web applications" is so nowadays! All of my recent software development projects (last 10 years) focus on business processes integration. In all of them the BLL (Business Logic Layer), or "application logic", is run on server side (as a controller in MVC) due to security and performance reasons. None of my clients want their business rules and internal workflows exposed, so the old ways of the 70/80's ain't gonna change. > Sincerely, > > Wolfgang > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Wed May 29 03:20:04 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 29 May 2013 17:20:04 +1000 Subject: Future standard GUI library In-Reply-To: <20130528192655.08861babbd86e890634e6d90@gmx.net> References: <20130522154233.fe5263cb231c375fc60c7c9b@gmx.net> <20130523174145.22a6c46f586b0a1f656d2412@gmx.net> <20130526194310.9cdb1be80b42c7fdf0ba502f@gmx.net> <20130528192655.08861babbd86e890634e6d90@gmx.net> Message-ID: On Wed, May 29, 2013 at 3:26 AM, Wolfgang Keller wrote: > I won't give you an example, but just some very basic criteria: > > - It must be very efficient for very small "datagrams" > - It must provide connections > - For asynchronous programming it must provide for callbacks In other words, a TELNET connection. I absolutely concur. I've made extensive use of that family of protocols; between MUDs (three active connections right at this moment), mail (SMTP and POP both), actual command execution (though that's usually encrypted via SSH), and *seven* separate protocols that I devised for work, I pretty much *always* am working with a system that parses text into lines and (usually) lines into commands with arguments. It's a broad concept that can be applied to *everything*. I'm firmly of the opinion that Magic: The Gathering could be played over a MUD connection. ChrisA From tjreedy at udel.edu Sat May 18 16:53:55 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sat, 18 May 2013 16:53:55 -0400 Subject: Future standard GUI library In-Reply-To: References: Message-ID: On 5/18/2013 10:03 AM, Beinan Li wrote: > Not sure if this is the right place to talk about this. It is. > Even less sure if I can > move this discussion to tkinter list, The idea of replacing tkinter is not about improving tkinter ;-). > Do you think tkinter is going to be the standard python built-in gui > solution as long as python exists? AT the moment, there is nothing really comparable that is a realistic candidate to replace tkinter. Tkinter is a tcl-based c gui library. wx and qt4 are gui application frameworks that include a gui library -- and much more that duplicate part of Python's stdlib. They, and consequently their Python wrappers, are too big. That is quite aside from the fact that the authors of the wrappers have good reason to not donate their code, but stay independent. PyGui was designed as a gui library comparable to tkinter and a possible replacement. But according to the web page, it is still not completely ready for py 3 (the last update was July 2011). http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/ I think a tkinter replacement should be written for at least 3.4+, so it can use the new 3.3 unicode implementation and use or at least integrate with the new 3.4 event loop that will be part of the planned new async library. One test for any new gui library is whether idle can be rewritten in it. It heavily uses the tag features of the tk text widget. TJR From vito.detullio at gmail.com Mon May 20 01:04:37 2013 From: vito.detullio at gmail.com (Vito De Tullio) Date: Mon, 20 May 2013 07:04:37 +0200 Subject: Future standard GUI library References: Message-ID: Terry Jan Reedy wrote: >> Do you think tkinter is going to be the standard python built-in gui >> solution as long as python exists? > > AT the moment, there is nothing really comparable that is a realistic > candidate to replace tkinter. FLTK? (http://www.fltk.org/index.php) -- ZeD From tjreedy at udel.edu Mon May 20 03:00:36 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Mon, 20 May 2013 03:00:36 -0400 Subject: Future standard GUI library In-Reply-To: References: Message-ID: On 5/20/2013 1:04 AM, Vito De Tullio wrote: > Terry Jan Reedy wrote: > >>> Do you think tkinter is going to be the standard python built-in gui >>> solution as long as python exists? >> >> AT the moment, there is nothing really comparable that is a realistic >> candidate to replace tkinter. > > FLTK? (http://www.fltk.org/index.php) tkinter is the Python wrapper of the tk library, just as wxpython is the python wrapper of the wx library. I do not see a py-fltk wrapper. From robert.kern at gmail.com Mon May 20 05:32:28 2013 From: robert.kern at gmail.com (Robert Kern) Date: Mon, 20 May 2013 10:32:28 +0100 Subject: Future standard GUI library In-Reply-To: References: Message-ID: On 2013-05-20 08:00, Terry Jan Reedy wrote: > On 5/20/2013 1:04 AM, Vito De Tullio wrote: >> Terry Jan Reedy wrote: >> >>>> Do you think tkinter is going to be the standard python built-in gui >>>> solution as long as python exists? >>> >>> AT the moment, there is nothing really comparable that is a realistic >>> candidate to replace tkinter. >> >> FLTK? (http://www.fltk.org/index.php) > > tkinter is the Python wrapper of the tk library, just as wxpython is the python > wrapper of the wx library. I do not see a py-fltk wrapper. It exists, but it's really old. http://pyfltk.sourceforge.net/ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From feliphil at gmx.net Wed May 22 09:40:44 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Wed, 22 May 2013 15:40:44 +0200 Subject: Future standard GUI library References: Message-ID: <20130522154044.2cf203bf3c9f1dbf13dbafbb@gmx.net> > >>>> Do you think tkinter is going to be the standard python built-in > >>>> gui solution as long as python exists? > >>> > >>> AT the moment, there is nothing really comparable that is a > >>> realistic candidate to replace tkinter. > >> > >> FLTK? (http://www.fltk.org/index.php) > > > > tkinter is the Python wrapper of the tk library, just as wxpython > > is the python wrapper of the wx library. I do not see a py-fltk > > wrapper. > > It exists, but it's really old. > > http://pyfltk.sourceforge.net/ And it's imho definitely not "native" (in terms of "look and feel") on *any* operating system, not even on Linux. In fact it's SGI's Irix Forms re-implemented, afaik. Sincerely, Wolfgang From invalid at invalid.invalid Mon May 20 16:18:21 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 20 May 2013 20:18:21 +0000 (UTC) Subject: Future standard GUI library References: Message-ID: On 2013-05-20, Terry Jan Reedy wrote: > On 5/20/2013 1:04 AM, Vito De Tullio wrote: >> Terry Jan Reedy wrote: >> >>>> Do you think tkinter is going to be the standard python built-in gui >>>> solution as long as python exists? >>> >>> AT the moment, there is nothing really comparable that is a realistic >>> candidate to replace tkinter. >> >> FLTK? (http://www.fltk.org/index.php) > > tkinter is the Python wrapper of the tk library, just as wxpython is > the python wrapper of the wx library. I do not see a py-fltk wrapper. http://pyfltk.sourceforge.net/ -- Grant Edwards grant.b.edwards Yow! Is it 1974? What's at for SUPPER? Can I spend gmail.com my COLLEGE FUND in one wild afternoon?? From kw at codebykevin.com Mon May 20 06:32:13 2013 From: kw at codebykevin.com (Kevin Walzer) Date: Mon, 20 May 2013 06:32:13 -0400 Subject: Future standard GUI library In-Reply-To: References: Message-ID: On 5/20/13 1:04 AM, Vito De Tullio wrote: > FLTK? (http://www.fltk.org/index.php) FLTK is even uglier than non-themed Tkinter: non-native on every platform. Tkinter wraps native widgets on MacOS and WIndows, but FLTK draws its own widgets everywhere. -- Kevin Walzer Code by Kevin/Mobile Code by Kevin http://www.codebykevin.com http://www.wtmobilesoftware.com From li.beinan at gmail.com Sat May 18 17:30:44 2013 From: li.beinan at gmail.com (Beinan Li) Date: Sat, 18 May 2013 17:30:44 -0400 Subject: Future standard GUI library Message-ID: Thanks for the clarification, Kevin. It's nice to have a tk dev standing out :-) This more or less convinced me about the trend. I also agree that it would be probably a simpler and more maintainable way to write my own tk(inter) code than using any existing 3rd-party designers. Beinan On Sat, May 18, 2013 at 1:40 PM, wrote: > > > ---------- Forwarded message ---------- > From: Kevin Walzer > To: python-list at python.org > Cc: > Date: Sat, 18 May 2013 11:32:04 -0400 > Subject: Re: Future standard GUI library > Hello, > > On 5/18/13 10:03 AM, Beinan Li wrote: > > I know this may sound a silly question because no one can see the >> future. But ... >> Do you think tkinter is going to be the standard python built-in gui >> solution as long as python exists? >> > > I don't see any significant clamoring among the Python core developers to > make a change. > > >> I couldn't help but wonder if wx or PySide receives better py2 and py3 >> support, or anything else that prevent >> them from getting into the standard python distributions, whether or not >> this scene could start to shift ... >> > > I am not going to engage in the old UI toolkit flame ware; I will limit > myself to factual observations and a few opinions about Tkinter without > placing it against other toolkits. > > Python has the PEP process for suggesting changes to the core language and > libraries. Changing the default UI toolkit would require someone to submit > a proposal, get it approved, provide the implementation, and commit to > maintaining the implementation over the long term. You propose it, you own > it. > > Thus far no one has done this. I would think the only person who would be > in a position to propose wxPython would be Robin Dunn since he is the > primary copyright holder, and to my knowledge he has never done so. As for > Pyside, it was not part of the transition of Qt from Nokia to Digia, and so > it appears to be orphaned. PyQt might be an alternative, but I don't think > Phil Thompson would ever submit it, as it would likely affect his > livelihood. > > Given these facts, it's safe to say that Tkinter will remain the default > GUI toolkit in the stdlib for some years to come. > > >> I believe this "which one of tkinter, wx, qt, is the best gui toolkit >> for python" flame war has been going on >> for ages. I love the fact that python ships a built-in gui solution >> which makes shipping a pure-python desktop >> application a viable choice. But tkinter does not appear to be the most >> time-saving way to write a gui app. >> The layout designer support, for one, is next to zero. I tried many >> 3rd-party designers >> and loved PAGE (http://page.sourceforge.net) for a few minutes, then >> came the author's comment: >> > > PAGE strikes me as an odd choice for a Python developer to develop a Tk > UI. It's a descendent of the old Visual Tcl tool, and is run from Tcl and > not Python. VTcl was always famous for producing a huge pot of spaghetti UI > code that couldn't be easily modified outside the tool. I have also tried > many Tk UI tools including VTcl, SpecTcl, and the orphaned tool from > ActiveState, and have concluded that writing my own code is both simpler > and faster. As always, your mileage may vary. > > >> "For release 4.0, I spent about two months working with the ?Theme? part >> of Ttk and have had only partial success. I now believe that the ?Theme? >> part of Ttk is really a very poor piece of software at all levels - >> concept, implementation, and especially documentation. My guess is if it >> had been well documented it would have been recognized by even the >> author as junk. I find it hard to believe that the people who control >> Tcl/Tk allowed it in the code base. I continue to support ttk because of >> the paned window, notebook and treeview widgets." >> > > The implementation of the ttk widgets is quite complex--that, in turn, > makes their documentation complex, and the complexity is a drawback. It's > hard to customize the ttk themes, especially compared to customizing > standard Tk widgets. I'm one of the core Tk developers, and I don't fully > understand the themed widgets' internals or how to customize them. But it's > not fair to say that they are "junk." The author of PAGE may think so, but > many would disagree. I think the widgets work quite well, especially if > used in their default mode. It's difficult to overstate the improvement in > the look and feel of my Tk apps on the Mac when I switched to using the > themed widgets. > > >> And ttk seems to be a major attraction that keeps people coming back to >> tk for the looks. This worries me very much >> about whether I should start a gui app using python. Because if ttk is >> not a "mature" technology, I'd avoid premature adoption. >> If ttk is out of the question, tkinter will be too. I'd then be forced >> to use a 3rd-party solution like wx or qt, which I really don't want to >> see. >> > > ttk is a mature technology. The initial specification, code, and docs were > developed by Joe English nearly a decade ago (see > http://tktable.sourceforge.**net/tile/tile-tcl2004.pdf), > and they had been accepted into Tcl/Tk's core in version 8.5 (released in > 2007). The earliest work on integrating these widgets into Python began in > 2008, and they were formally released in 2010. I would say that ttk is > mature, stable, and unlikely to undergo radical change in the future. > > Guilherme Polo has done superb work in integrating the themed widgets into > Python--it's the most significant UI advance in Python's stdlib in years. > You are quite safe in developing against this API, unless your taste simply > does not run to using the ttk widgets. > > Hope this helps, > Kevin > > -- > Kevin Walzer > Code by Kevin/Mobile Code by Kevin > http://www.codebykevin.com > http://www.wtmobilesoftware.**com > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vincent.vandevyvre at swing.be Sat May 18 10:09:40 2013 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Sat, 18 May 2013 16:09:40 +0200 Subject: Fwd: Re: python script is not running In-Reply-To: <519759C7.7000200@swing.be> References: <519759C7.7000200@swing.be> Message-ID: <51978BA4.1080000@swing.be> -------- Message original -------- Sujet: Re: python script is not running Date : Sat, 18 May 2013 12:36:55 +0200 De : Vincent Vande Vyvre Pour : Avnesh Shakya Le 18/05/2013 12:12, Avnesh Shakya a ?crit : > hi, > i want to run python script which generating data into json fromat, I am using crontab, but it's not executing... > my python code-- > try.py -- > > import json > import simplejson as json > import sys > > def tryJson(): > saved = sys.stdout > correctFile = file('data.json', 'a+') > sys.stdout = correctFile > result = [] > i = 1 > for i in range(5): > info = { > 'a': i+1, > 'b': i+2, > 'c': i+3, > } > result.append(info) > > if result: > print json.dumps(result, indent=6) > sys.stdout = saved > correctFile.close() > tryJson() > > now i m doing ion terminal- > avin at hp:~$ crontab -e > then type - > */2 * * * * python /home/avin/data/try.py > > and save > > but it's not executing. Have a look at /var/mail/yourname, this is a log created by crontab. -- Vincent V.V. Oqapy . Qarte . PaQager From phd at phdru.name Sat May 18 06:08:30 2013 From: phd at phdru.name (Oleg Broytman) Date: Sat, 18 May 2013 14:08:30 +0400 Subject: SQLObject 1.4.0 Message-ID: <20130518100829.GC9791@iskra.aviel.ru> Hello! I'm pleased to announce version 1.4.0, the first stable release of branch 1.4 of SQLObject. What's new in SQLObject ======================= Features & Interface -------------------- * Support for PostgreSQL 8.1 is dropped. The minimal supported version of PostgreSQL is 8.2 now. * Optimization in PostgresConnection: use INSERT...RETURNING id to get the autoincremented id in one query instead of two (INSERT + SELECT id). * Changed the way to get if the table has identity in MS SQL. * NCHAR/NVARCHAR and N''-quoted strings for MS SQL. Contributors for this release are Ken Lalonde and Andrew Ziem. For a more complete list, please see the news: http://sqlobject.org/News.html What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with. SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite, Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB). Where is SQLObject ================== Site: http://sqlobject.org Development: http://sqlobject.org/devel/ Mailing list: https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss Archives: http://news.gmane.org/gmane.comp.python.sqlobject Download: https://pypi.python.org/pypi/SQLObject/1.4.0 News and changes: http://sqlobject.org/News.html Oleg. -- Oleg Broytman http://phdru.name/ phd at phdru.name Programmers don't die, they just GOSUB without RETURN. From drsalists at gmail.com Sat May 18 15:01:12 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Sat, 18 May 2013 12:01:12 -0700 Subject: TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead) Message-ID: I'm getting the error in the subject, from the following code: def add(self, key): """ Adds a node containing I{key} to the subtree rooted at I{self}, returning the added node. """ node = self.find(key) if not node: node.key = key # placeholder node.left, node.right = self.__class__(parent=node), self.__class__(parent=node) return (False, node) else: if random.random() < 0.5: print('node.left is %s' % node.left) return BinaryTree.add(self=node.left, key=key) else: print('node.right is %s' % node.left) return BinaryTree.add(self=node.right, key=key) The above add() method is part of a BinaryTree(object) class, whose subclass is RedBlackTree. We need to explicitly call BinaryTree.add() with an explict self, to avoid inappropriately calling RedBlackTree.add().; BinaryTree.add() is being called with a RedBlackTree instance as self. The debugging print and traceback look like: node.left is 0 -1 red Traceback (most recent call last): File "app_main.py", line 51, in run_toplevel File "test-red_black_tree_mod", line 328, in test() File "test-red_black_tree_mod", line 316, in test all_good &= test_duplicates() File "test-red_black_tree_mod", line 194, in test_duplicates tree.add(value) File "/home/dstromberg/src/home-svn/red-black-tree-mod/trunk/duncan/red_black_bag_mod.py", line 919, in add (replaced, node) = super(RedBlackTree, self).add(key=key) File "/home/dstromberg/src/home-svn/red-black-tree-mod/trunk/duncan/red_black_bag_mod.py", line 376, in add return BinaryTree.add(self=node.left, key=key) TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead) Why is it complaining that .add() is getting nothing, when node.left isn't None? As you can see above the traceback, it's got a value represented by "node.left is 0 -1 red". python 2.x, python 3.x and pypy all give this same error, though jython errors out at a different point in the same method. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sat May 18 15:24:46 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 May 2013 21:24:46 +0200 Subject: TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead) References: Message-ID: Dan Stromberg wrote: > I'm getting the error in the subject, from the following code: > def add(self, key): > """ > Adds a node containing I{key} to the subtree > rooted at I{self}, returning the added node. > """ > node = self.find(key) > if not node: > node.key = key > # placeholder > node.left, node.right = self.__class__(parent=node), > self.__class__(parent=node) > return (False, node) > else: > if random.random() < 0.5: > print('node.left is %s' % node.left) > return BinaryTree.add(self=node.left, key=key) > else: > print('node.right is %s' % node.left) > return BinaryTree.add(self=node.right, key=key) > > The above add() method is part of a BinaryTree(object) class, whose > subclass is RedBlackTree. > > We need to explicitly call BinaryTree.add() with an explict self, to avoid > inappropriately calling RedBlackTree.add().; BinaryTree.add() is being > called with a RedBlackTree instance as self. > > The debugging print and traceback look like: > node.left is 0 -1 red > Traceback (most recent call last): > File "app_main.py", line 51, in run_toplevel > File "test-red_black_tree_mod", line 328, in > test() > File "test-red_black_tree_mod", line 316, in test > all_good &= test_duplicates() > File "test-red_black_tree_mod", line 194, in test_duplicates > tree.add(value) > File > "/home/dstromberg/src/home-svn/red-black-tree- mod/trunk/duncan/red_black_bag_mod.py", > line 919, in add > (replaced, node) = super(RedBlackTree, self).add(key=key) > File > "/home/dstromberg/src/home-svn/red-black-tree- mod/trunk/duncan/red_black_bag_mod.py", > line 376, in add > return BinaryTree.add(self=node.left, key=key) > TypeError: unbound method add() must be called with BinaryTree instance as > first argument (got nothing instead) > > Why is it complaining that .add() is getting nothing, when node.left isn't > None? As you can see above the traceback, it's got a value represented by > "node.left is 0 -1 red". > > python 2.x, python 3.x and pypy all give this same error, though jython > errors out at a different point in the same method. > > Thanks! I never ran into that, but apparently you cannot pass self as a keyword parameter: >>> class A(object): ... def add(self): pass ... >>> a = A() >>> A.add(a) >>> A.add(self=a) Traceback (most recent call last): File "", line 1, in TypeError: unbound method add() must be called with A instance as first argument (got nothing instead) From __peter__ at web.de Sat May 18 15:46:38 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 May 2013 21:46:38 +0200 Subject: TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead) References: Message-ID: Dan Stromberg wrote: > python 2.x, python 3.x and pypy all give this same error, though jython > errors out at a different point in the same method. By the way, 3.x doesn't have unbound methods, so that should work. From tjreedy at udel.edu Sat May 18 17:05:57 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sat, 18 May 2013 17:05:57 -0400 Subject: TypeError: unbound method add() must be called with BinaryTree instance as first argument (got nothing instead) In-Reply-To: References: Message-ID: On 5/18/2013 3:46 PM, Peter Otten wrote: > Dan Stromberg wrote: > >> python 2.x, python 3.x and pypy all give this same error, though jython >> errors out at a different point in the same method. > > By the way, 3.x doesn't have unbound methods, so that should work. It does for this example (3.3.1) >>> c = C() >>> c.m() <__main__.C object at 0x00000000033FC5F8> >>> C.m(c) <__main__.C object at 0x00000000033FC5F8> >>> C.m(self=c) <__main__.C object at 0x00000000033FC5F8> From cs at zip.com.au Sat May 18 20:26:29 2013 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 19 May 2013 10:26:29 +1000 Subject: mutable ints: I think I have painted myself into a corner Message-ID: <20130519002629.GA14025@cskk.homeip.net> TL;DR: I think I want to modify an int value "in place". Yesterday I was thinking about various "flag set" objects I have floating around which are essentially bare "object"s whose attributes I access, for example: flags = object() flags.this = True flags.that = False and then elsewhere: if flags.that: do that ... Nice and readable, but I thought to myself: so bulky! The use case for flags is essentially boolean/binary, and so a int accessed as a bitmask should be smaller. So I pulled out my BitMask int subclass (which mostly transcribes the int as "A|B|C" for readability purposes, partly to dillute Nick Coglan's liking for bulky strings over compact ints on readability grounds:-), and gave the subclass attribute access. This works just fine for querying the flags object, with code exactly like the "if" statement above. But setting up a flags object? What I _want_ to write is code like this: Flags = BitMask('this', 'that') # set default state flags = Flags() flags.this = False flags.that = True ... iterate over some options ...: flags.this = True and there's my problem. This would modify the int in place. There's no way to do that. For the base type (int) this makes perfect sense, as they're immutable. Before I toss this approach and retreat to my former "object" technique, does anyone see a way forward to modify an int subclass instance in place? (That doesn't break math, preferably; I don't do arithmetic with these things but they are, after all, ints...) Cheers, -- Cameron Simpson Why does "philosophy of consciousness/nature of reality" seem to interest you so much? Take away consciousness and reality and there's not much left. - Greg Egan, interview in Eidolon 15 From rosuav at gmail.com Sat May 18 21:11:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 19 May 2013 11:11:56 +1000 Subject: mutable ints: I think I have painted myself into a corner In-Reply-To: <20130519002629.GA14025@cskk.homeip.net> References: <20130519002629.GA14025@cskk.homeip.net> Message-ID: On Sun, May 19, 2013 at 10:26 AM, Cameron Simpson wrote: > Before I toss this approach and retreat to my former "object" > technique, does anyone see a way forward to modify an int subclass > instance in place? (That doesn't break math, preferably; I don't > do arithmetic with these things but they are, after all, ints...) > Why is it an int subclass? Because there are places where you want to use it as though it were an int? It might be easier to render those, instead, eg by creating a __int__ method. (Or is it "an __int__ method"? Not sure.) ChrisA From cs at zip.com.au Sat May 18 21:54:04 2013 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 19 May 2013 11:54:04 +1000 Subject: mutable ints: I think I have painted myself into a corner In-Reply-To: References: Message-ID: <20130519015404.GA78300@cskk.homeip.net> On 19May2013 11:11, Chris Angelico wrote: | On Sun, May 19, 2013 at 10:26 AM, Cameron Simpson wrote: | > Before I toss this approach and retreat to my former "object" | > technique, does anyone see a way forward to modify an int subclass | > instance in place? (That doesn't break math, preferably; I don't | > do arithmetic with these things but they are, after all, ints...) | | Why is it an int subclass? Because there are places where you want to | use it as though it were an int? It might be easier to render those, | instead, eg by creating a __int__ method. (Or is it "an __int__ | method"? Not sure.) I don't want to use it as an int, on the outside. I want to use an int on the inside as the implementation. It's an int _subclass_ so that it is no bigger than an int. Otherwise I may as well just make an ordinary object and be back where I started. Bulky:-( The reason it is an _int_ subclass, versus something else, is that a bitmap is a type of int. So the functional mapping is direct. I _do_ _not_ want to operate on it from the outside as an int (doing overt addition, for example, though I can imagine doing bitwise activities); I want to operate on in _internally_ as a int to decide what names-by-an-attribute flags are on or off. So an object with an __int__() method is right out; it's the wrong interface because it would mean an int() call (possibly implicit) in exterior code. Cheers, -- Cameron Simpson Hoping to shave precious seconds off the time it would take me to get through the checkout process and on my way home, I opted for the express line ("9 Items Or Less [sic]" Why nine items? Where do they come up with these rules, anyway? It's the same way at most stores -- always some oddball number like that, instead of a more understandable multiple of five. Like "five.") - Geoff Miller, geoffm at purplehaze.Corp.Sun.COM From greg.ewing at canterbury.ac.nz Sun May 19 21:23:28 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 20 May 2013 13:23:28 +1200 Subject: mutable ints: I think I have painted myself into a corner In-Reply-To: References: Message-ID: Cameron Simpson wrote: > It's an int _subclass_ so that it is no bigger than an int. If you use __slots__ to eliminate the overhead of an instance dict, you'll get an object consisting of a header plus one reference, which is probably about the size of an int. But you'll also need an int to put in that slot, so the total size will be about twice that of an int. Another approach would be to subclass array.array and give instances of it type integer and size 1. Together with empty __slots__, it will probably be a bit bigger than an int, but it might still be smaller than a custom object plus an int. If all of these are still too big, you might need to find some way of packing multiple instances into a single array.array. -- Greg From cs at zip.com.au Mon May 20 02:07:51 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 20 May 2013 16:07:51 +1000 Subject: mutable ints: I think I have painted myself into a corner In-Reply-To: References: Message-ID: <20130520060751.GA67145@cskk.homeip.net> On 20May2013 13:23, Greg Ewing wrote: | Cameron Simpson wrote: | >It's an int _subclass_ so that it is no bigger than an int. | | If you use __slots__ to eliminate the overhead of an | instance dict, you'll get an object consisting of a | header plus one reference, which is probably about the | size of an int. But you'll also need an int to put in | that slot, so the total size will be about twice that | of an int. Yeah, I was thinking I'd need to go that way. Thanks for the suggestion. | Another approach would be to subclass array.array and | give instances of it type integer and size 1. Together | with empty __slots__, it will probably be a bit bigger | than an int, but it might still be smaller than a | custom object plus an int. Really? Interesting. I thinik it crosses my "too baroque" threshold, but maybe not:-) | If all of these are still too big, you might need to | find some way of packing multiple instances into a | single array.array. Space isn't that real an issue at present; I'll keep that kind of approach in mind if it comes up. This really came up because I was feeling that the obvious object-with-boolean-attributes was terrbily wasteful for something that can be inplemented with a single int, in principle. Cheers, -- Cameron Simpson >>>How do you blip the throttle and wave? Do you blip it real high, then wave >>>before the revs drop back? >>Blip = right hand; Wave = left hand. Do both simultaneously. QED. >Doesnt this make the bike lurch forward thru the intersection? Not if the disk lock is in place... - Dean Woodward From __peter__ at web.de Sun May 19 03:01:49 2013 From: __peter__ at web.de (Peter Otten) Date: Sun, 19 May 2013 09:01:49 +0200 Subject: mutable ints: I think I have painted myself into a corner References: <20130519002629.GA14025@cskk.homeip.net> Message-ID: Cameron Simpson wrote: > TL;DR: I think I want to modify an int value "in place". > > Yesterday I was thinking about various "flag set" objects I have > floating around which are essentially bare "object"s whose attributes > I access, for example: > > flags = object() > flags.this = True > flags.that = False > > and then elsewhere: > > if flags.that: > do that ... > > Nice and readable, but I thought to myself: so bulky! Plus, it doesn't work: >>> object().this = True Traceback (most recent call last): File "", line 1, in AttributeError: 'object' object has no attribute 'this' > The use case for flags is essentially boolean/binary, and so a int > accessed as a bitmask should be smaller. > > So I pulled out my BitMask int subclass (which mostly transcribes > the int as "A|B|C" for readability purposes, partly to dillute Nick > Coglan's liking for bulky strings over compact ints on readability > grounds:-), and gave the subclass attribute access. > > This works just fine for querying the flags object, with code exactly > like the "if" statement above. > > But setting up a flags object? What I _want_ to write is code like this: > > Flags = BitMask('this', 'that') > > # set default state > flags = Flags() > flags.this = False > flags.that = True > ... iterate over some options ...: flags.this = True > > and there's my problem. This would modify the int in place. There's > no way to do that. For the base type (int) this makes perfect sense, > as they're immutable. > > Before I toss this approach and retreat to my former "object" > technique, does anyone see a way forward to modify an int subclass > instance in place? (That doesn't break math, preferably; I don't > do arithmetic with these things but they are, after all, ints...) No, but you could make flags = Flags(this=False, that=True) work. From cs at zip.com.au Mon May 20 01:57:51 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 20 May 2013 15:57:51 +1000 Subject: mutable ints: I think I have painted myself into a corner In-Reply-To: References: Message-ID: <20130520055751.GA62991@cskk.homeip.net> On 19May2013 09:01, Peter Otten <__peter__ at web.de> wrote: | Cameron Simpson wrote: | | > TL;DR: I think I want to modify an int value "in place". | > | > Yesterday I was thinking about various "flag set" objects I have | > floating around which are essentially bare "object"s whose attributes | > I access, for example: | > | > flags = object() | > flags.this = True | > flags.that = False | > | > and then elsewhere: | > | > if flags.that: | > do that ... | > | > Nice and readable, but I thought to myself: so bulky! | | Plus, it doesn't work: Yeah, sorry. My "old" code was a trite subclass of object. The new broken code is a subclass of int. | > But setting up a flags object? What I _want_ to write is code like this: | > | > Flags = BitMask('this', 'that') | > | > # set default state | > flags = Flags() | > flags.this = False | > flags.that = True | > ... iterate over some options ...: flags.this = True | > | > and there's my problem. This would modify the int in place. There's | > no way to do that. For the base type (int) this makes perfect sense, | > as they're immutable. | > | > Before I toss this approach and retreat to my former "object" | > technique, does anyone see a way forward to modify an int subclass | > instance in place? (That doesn't break math, preferably; I don't | > do arithmetic with these things but they are, after all, ints...) | | No, but you could make | | flags = Flags(this=False, that=True) Yes, that is true. Flags would need to be a factory function computing the corresponding bitmask value and then returning my new int, but it would work. It still wouldn't let me go the whole way of modifying the flags after instantiation. Cheers, -- Cameron Simpson Carpe Daemon - Seize the Background Process - Paul Tomblin From antilogeffects at gmail.com Sun May 19 06:52:27 2013 From: antilogeffects at gmail.com (Anti Log) Date: Sun, 19 May 2013 03:52:27 -0700 (PDT) Subject: Harmonic distortion of a input signal Message-ID: Hi, I have a task to calculate total distortion of a harmonics, of a signal that i imported from oscilloscope as numpy array. I had no problem drawing its spectrum, and time domain graph, but cant seem to find any functions that calculate TDH. Any help? Best regards From rosuav at gmail.com Sun May 19 11:34:53 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 20 May 2013 01:34:53 +1000 Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: On Sun, May 19, 2013 at 8:52 PM, Anti Log wrote: > total distortion of a harmonics I selected this part of your post, right-clicked, and Chrome offered to do a Google search for those words. And, surprise surprise, the first hit is a page that appears to have the mathematics behind it. Now, I don't know how much you trust Google and Wikipedia, but I'm sure you can confirm the maths in some other way. My guess is that there's no function in numpy to do what you're asking... but it shouldn't be too hard to render the formula/e given into Python code. Python's pretty expressive when it comes to algebra. :) ChrisA From killybeard91 at gmail.com Sun May 19 18:25:08 2013 From: killybeard91 at gmail.com (killybeard91 at gmail.com) Date: Sun, 19 May 2013 15:25:08 -0700 (PDT) Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: How can i at least find a peek in FFT spectrum of a square wave ? >From there i could easily build formula. Sorry for bothering but i am new to Python. From oscar.j.benjamin at gmail.com Sun May 19 18:49:52 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sun, 19 May 2013 23:49:52 +0100 Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: On 19 May 2013 23:25, wrote: > How can i at least find a peek in FFT spectrum of a square wave ? > From there i could easily build formula. Sorry for bothering but i am new to Python. Are you the same person who posted the original question? You probably want to use numpy for this. I'm not sure if I understand your question but here goes: First import numpy (you may need to install this first): >>> import numpy as np Create a square wave signal: >>> x = np.zeros(50) >>> x[:25] = -1 >>> x[25:] = +1 >>> x array([-1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) Compute the magnitude spectrum: >>> spect = abs(np.fft.fft(x)[:25]) >>> spect array([ 0. , 31.85194222, 0. , 10.67342282, 0. , 6.47213595, 0. , 4.69726931, 0. , 3.73254943, 0. , 3.13762901, 0. , 2.7436023 , 0. , 2.47213595, 0. , 2.28230601, 0. , 2.15105461, 0. , 2.06487174, 0. , 2.01589594, 0. ]) Find the index of the maximum element: >>> np.argmax(spect) 1 So the peak is the lowest non-zero frequency component of the DFT. In Hz this corresponds to a frequency of 1/T where T is the duration of the signal. Oscar From tjreedy at udel.edu Sun May 19 19:19:55 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 19 May 2013 19:19:55 -0400 Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: On 5/19/2013 6:49 PM, Oscar Benjamin wrote: >>>> import numpy as np > > Create a square wave signal: > >>>> x = np.zeros(50) >>>> x[:25] = -1 >>>> x[25:] = +1 >>>> x > array([-1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., > -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 1., > 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., > 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) > > Compute the magnitude spectrum: > >>>> spect = abs(np.fft.fft(x)[:25]) >>>> spect > array([ 0. , 31.85194222, 0. , 10.67342282, > 0. , 6.47213595, 0. , 4.69726931, > 0. , 3.73254943, 0. , 3.13762901, > 0. , 2.7436023 , 0. , 2.47213595, > 0. , 2.28230601, 0. , 2.15105461, > 0. , 2.06487174, 0. , 2.01589594, 0. ]) > > Find the index of the maximum element: > >>>> np.argmax(spect) > 1 > > So the peak is the lowest non-zero frequency component of the DFT. In > Hz this corresponds to a frequency of 1/T where T is the duration of > the signal. While you were answering a specific question, I think the above is a nice tutorial example, because it is more meaningful than arbitrary operations applied to random data. From killybeard91 at gmail.com Sun May 19 18:59:17 2013 From: killybeard91 at gmail.com (killybeard91 at gmail.com) Date: Sun, 19 May 2013 15:59:17 -0700 (PDT) Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: <7d4f07c6-7289-4c91-a829-338127862965@googlegroups.com> Yes, sorry logged from another account. Would that work on a numpy array ? Because this signal was imported from oscilloscope as a numpy array. Best regards, From killybeard91 at gmail.com Sun May 19 19:03:43 2013 From: killybeard91 at gmail.com (killybeard91 at gmail.com) Date: Sun, 19 May 2013 16:03:43 -0700 (PDT) Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: Got it working, thanks alot :) From killybeard91 at gmail.com Sun May 19 19:36:43 2013 From: killybeard91 at gmail.com (killybeard91 at gmail.com) Date: Sun, 19 May 2013 16:36:43 -0700 (PDT) Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: One more question. Function np.argmax returns max of non-complex numbers ? Because FFT array of my signal is complex. From oscar.j.benjamin at gmail.com Sun May 19 19:44:43 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sun, 19 May 2013 23:44:43 -0000 Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: On 20 May 2013 00:36, wrote: > One more question. Function np.argmax returns max of non-complex numbers ? > Because FFT array of my signal is complex. Use abs() like in my example. This will give the absolute value of the complex numbers: >>> z = 1+1j >>> z (1+1j) >>> abs(z) 1.4142135623730951 Oscar From greg.ewing at canterbury.ac.nz Sun May 19 21:09:36 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 20 May 2013 13:09:36 +1200 Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: killybeard91 at gmail.com wrote: > One more question. Function np.argmax returns max of non-complex numbers ? > Because FFT array of my signal is complex. You'll want the magnitudes of the complex numbers. Actually the squares of the magnitudes (assuming the data from the oscilloscope represents voltages), because you're after a ratio of powers. -- Greg From davea at davea.name Sun May 19 21:11:57 2013 From: davea at davea.name (Dave Angel) Date: Sun, 19 May 2013 21:11:57 -0400 Subject: Harmonic distortion of a input signal In-Reply-To: References: Message-ID: <5199785D.10900@davea.name> On 05/19/2013 07:36 PM, killybeard91 at gmail.com wrote: > One more question. Function np.argmax returns max of non-complex numbers ? > Because FFT array of my signal is complex. > It'd be easier to track the thread if you actually replied to the message you're responding to, and also if you included some context. But I'll paste the latter in here: Terry Reedy said: > Compute the magnitude spectrum: >>> spect = abs(np.fft.fft(x)[:25]) >>> spect > array([ 0. , 31.85194222, 0. , 10.67342282, > 0. , 6.47213595, 0. , 4.69726931, > 0. , 3.73254943, 0. , 3.13762901, > 0. , 2.7436023 , 0. , 2.47213595, > 0. , 2.28230601, 0. , 2.15105461, > 0. , 2.06487174, 0. , 2.01589594, > 0. ]) > Find the index of the maximum element: >>> np.argmax(spect) > 1 Notice that argmax's argument is the result of an abs() call. It's got real numbers representing the magnitude of the various complex numbers. -- DaveA From wxjmfauth at gmail.com Mon May 20 13:23:33 2013 From: wxjmfauth at gmail.com (jmfauth) Date: Mon, 20 May 2013 10:23:33 -0700 (PDT) Subject: Harmonic distortion of a input signal References: Message-ID: <7fa6c8f1-3a63-4017-9a57-db8516545da0@k3g2000vbn.googlegroups.com> Non sense. The discrete fft algorithm is valid only if the number of data points you transform does correspond to a power of 2 (2**n). Keywords to the problem: apodization, zero filling, convolution product, ... eg. http://en.wikipedia.org/wiki/Convolution jmf From auriocus at gmx.de Mon May 20 13:50:34 2013 From: auriocus at gmx.de (Christian Gollwitzer) Date: Mon, 20 May 2013 19:50:34 +0200 Subject: Harmonic distortion of a input signal In-Reply-To: <7fa6c8f1-3a63-4017-9a57-db8516545da0@k3g2000vbn.googlegroups.com> References: <7fa6c8f1-3a63-4017-9a57-db8516545da0@k3g2000vbn.googlegroups.com> Message-ID: Am 20.05.13 19:23, schrieb jmfauth: > Non sense. Dito. > The discrete fft algorithm is valid only if the number of data > points you transform does correspond to a power of 2 (2**n). Where did you get this? The DFT is defined for any integer point number the same way. Just if you want to get it fast, you need to worry about the length. For powers of two, there is the classic Cooley-Tukey. But there do exist FFT algorithms for any other length. For example, there is the Winograd transform for a set of small numbers, there is "mixed-radix" to reduce any length which can be factored, and there is finally Bluestein which works for any size, even for a prime. All of the aforementioned algorithms are O(log n) and are implemented in typical FFT packages. All of them should result (up to rounding differences) in the same thing as the naive DFT sum. Therefore, today > Keywords to the problem: apodization, zero filling, convolution > product, ... Not for a periodic signal of integer length. > eg. http://en.wikipedia.org/wiki/Convolution How long do you read this group? Christian From auriocus at gmx.de Mon May 20 13:56:05 2013 From: auriocus at gmx.de (Christian Gollwitzer) Date: Mon, 20 May 2013 19:56:05 +0200 Subject: Harmonic distortion of a input signal In-Reply-To: References: <7fa6c8f1-3a63-4017-9a57-db8516545da0@k3g2000vbn.googlegroups.com> Message-ID: Oops, I thought we were posting to comp.dsp. Nevertheless, I think numpy.fft does mixed-radix (can't check it now) Am 20.05.13 19:50, schrieb Christian Gollwitzer: > Am 20.05.13 19:23, schrieb jmfauth: >> Non sense. > > Dito. > >> The discrete fft algorithm is valid only if the number of data >> points you transform does correspond to a power of 2 (2**n). > > Where did you get this? The DFT is defined for any integer point number > the same way. > > Just if you want to get it fast, you need to worry about the length. For > powers of two, there is the classic Cooley-Tukey. But there do exist FFT > algorithms for any other length. For example, there is the Winograd > transform for a set of small numbers, there is "mixed-radix" to reduce > any length which can be factored, and there is finally Bluestein which > works for any size, even for a prime. All of the aforementioned > algorithms are O(log n) and are implemented in typical FFT packages. All > of them should result (up to rounding differences) in the same thing as > the naive DFT sum. Therefore, today > >> Keywords to the problem: apodization, zero filling, convolution >> product, ... > > Not for a periodic signal of integer length. > >> eg. http://en.wikipedia.org/wiki/Convolution > > How long do you read this group? > > Christian > From wxjmfauth at gmail.com Thu May 23 07:44:55 2013 From: wxjmfauth at gmail.com (jmfauth) Date: Thu, 23 May 2013 04:44:55 -0700 (PDT) Subject: Harmonic distortion of a input signal References: <7fa6c8f1-3a63-4017-9a57-db8516545da0@k3g2000vbn.googlegroups.com> Message-ID: <1348a986-7d94-4380-aba0-5ae8558a8c55@h5g2000vbg.googlegroups.com> On 20 mai, 19:56, Christian Gollwitzer wrote: > Oops, I thought we were posting to comp.dsp. Nevertheless, I think > numpy.fft does mixed-radix (can't check it now) > > Am 20.05.13 19:50, schrieb Christian Gollwitzer: > > > > > > > > > Am 20.05.13 19:23, schrieb jmfauth: > >> Non sense. > > > Dito. > > >> The discrete fft algorithm is valid only if the number of data > >> points you transform does correspond to a power of 2 (2**n). > > > Where did you get this? The DFT is defined for any integer point number > > the same way. > > > Just if you want to get it fast, you need to worry about the length. For > > powers of two, there is the classic Cooley-Tukey. But there do exist FFT > > algorithms for any other length. For example, there is the Winograd > > transform for a set of small numbers, there is "mixed-radix" to reduce > > any length which can be factored, and there is finally Bluestein which > > works for any size, even for a prime. All of the aforementioned > > algorithms are O(log n) and are implemented in typical FFT packages. All > > of them should result (up to rounding differences) in the same thing as > > the naive DFT sum. Therefore, today > > >> Keywords to the problem: apodization, zero filling, convolution > >> product, ... > > > Not for a periodic signal of integer length. > > >> eg.http://en.wikipedia.org/wiki/Convolution > > > How long do you read this group? > > > ? ? ?Christian ------ Forget what I wrote. I'm understanding what I wanted to say, it is badly formulated. jmf From oscar.j.benjamin at gmail.com Tue May 21 10:58:15 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 21 May 2013 15:58:15 +0100 Subject: Harmonic distortion of a input signal In-Reply-To: <7fa6c8f1-3a63-4017-9a57-db8516545da0@k3g2000vbn.googlegroups.com> References: <7fa6c8f1-3a63-4017-9a57-db8516545da0@k3g2000vbn.googlegroups.com> Message-ID: On 20 May 2013 18:23, jmfauth wrote: > Non sense. > > The discrete fft algorithm is valid only if the number of data > points you transform does correspond to a power of 2 (2**n). As with many of your comments about Python's unicode implementation you are confusing performance with validity. The DFT is defined and is a valid invertible map (barring roundoff) for complex vectors of any integer length. It is also a valid method for understanding the frequency content of periodic signals. The fastest FFT algorithms are for vectors whose length is a power of 2 but the other algorithms produce equally *valid* DFT results. In the example I posted the computation of the DFT using numpy.fft.fft was (as far as I could tell) instantaneous. I could use timeit to discover exactly how many microseconds it took but why when I already have the results I wanted? > Keywords to the problem: apodization, zero filling, convolution > product, ... > > eg. http://en.wikipedia.org/wiki/Convolution These points are not relevant to the example given. Oscar From ned at nedbatchelder.com Sun May 19 07:30:49 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Sun, 19 May 2013 07:30:49 -0400 Subject: What was the project that made you feel skilled in Python? Message-ID: <5198B7E9.7050505@nedbatchelder.com> Hi all, I'm trying to come up with more project ideas for intermediate learners, somewhat along the lines of http://bit.ly/intermediate-python-projects . So here's a question for people who remember coming up from beginner: as you moved from exercises like those in Learn Python the Hard Way, up to your own self-guided work on small projects, what project were you working on that made you feel independent and skilled? What program first felt like your own work rather than an exercise the teacher had assigned? I don't want anything too large, but big enough that there's room for design, and multiple approaches, etc. Thanks in advance! --Ned. From rosuav at gmail.com Sun May 19 07:48:58 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 19 May 2013 21:48:58 +1000 Subject: What was the project that made you feel skilled in Python? In-Reply-To: <5198B7E9.7050505@nedbatchelder.com> References: <5198B7E9.7050505@nedbatchelder.com> Message-ID: On Sun, May 19, 2013 at 9:30 PM, Ned Batchelder wrote: > Hi all, I'm trying to come up with more project ideas for intermediate > learners, somewhat along the lines of > http://bit.ly/intermediate-python-projects . > > So here's a question for people who remember coming up from beginner: as you > moved from exercises like those in Learn Python the Hard Way, up to your own > self-guided work on small projects, what project were you working on that > made you feel independent and skilled? What program first felt like your > own work rather than an exercise the teacher had assigned? > > I don't want anything too large, but big enough that there's room for > design, and multiple approaches, etc. Easy answer from me: The Yosemite project. And the code still looks like a n00b wrote it. (On that subject: Pull requests welcome.) The code is here: https://github.com/Rosuav/Yosemite I wrote it in Python because I wanted to be able to run it on either the Windows box that drives our TV system, or the Linux box that actually stores the content. And it's still doing that, quite nicely. Yosemite is a pretty simple system. It's broadly similar to a classic file-serving web server, but instead of making files available for download, it invokes them locally. There are plenty of other ways to achieve this, and I'm by no means sure I picked the best, but that's where it's at, and it was my first real Python project that's been published. ChrisA From roy at panix.com Sun May 19 10:05:28 2013 From: roy at panix.com (Roy Smith) Date: Sun, 19 May 2013 10:05:28 -0400 Subject: What was the project that made you feel skilled in Python? References: Message-ID: In article , Ned Batchelder wrote: > So here's a question for people who remember coming up from beginner: as > you moved from exercises like those in Learn Python the Hard Way, up to > your own self-guided work on small projects, what project were you > working on that made you feel independent and skilled? What program > first felt like your own work rather than an exercise the teacher had > assigned? IIRC, my first production python projects were a bunch of file parsers. We had a bunch of text file formats that we worked with often. I wrote some state-machine based parsers which slurped them up and gave back the contents in some useful data structure. Many of the files were big, so I added an option to write out a pickled version of the data. The parsing code could then check to see if there was a pickle file that was newer than the text version and read that instead. Big win for speed. Then, of course, a bunch of utilities which used this data to do useful things. I remember one of the utilities that turned out to be really popular was a smart data file differ. You feed it two files and it would tell you how they differed (in a way that was more useful than a plain text-based diff). From neilc at norwich.edu Mon May 20 11:16:55 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 20 May 2013 15:16:55 GMT Subject: What was the project that made you feel skilled in Python? References: Message-ID: On 2013-05-19, Ned Batchelder wrote: > Hi all, I'm trying to come up with more project ideas for > intermediate learners, somewhat along the lines of > http://bit.ly/intermediate-python-projects . > > So here's a question for people who remember coming up from > beginner: as you moved from exercises like those in Learn > Python the Hard Way, up to your own self-guided work on small > projects, what project were you working on that made you feel > independent and skilled? What program first felt like your own > work rather than an exercise the teacher had assigned? > > I don't want anything too large, but big enough that there's > room for design, and multiple approaches, etc. I wrote a library supporting fixed length field tabular data files. It supports reading specifications for such data files using configparser for maximum verbosity, plus a few other shorthand specification formats for brevity. Due to the nature of my work I need this library in virtually all my other projects, so I consider it a personal success and found it interesting to build. Similar packages on PYPI made many different design decisions from the ones I did, so it seems like fruitful design discussion points could arise. For example, two major design goals in the beginning where: 1. Ape the interface of the csv module as much as possible. 2. Support type declarations. The former was a big success. I've had instances were switching from csv to a fixed file required changing one line, and of course if a person were learning the library their knowledge of reader, writer, DictReader and DictWriter would help. The latter design goal was a failure. Most published fixed-length data file specifications include data types, so it seemed natural. But after trying to write programs using an early version I ended up removing all traces of that functionality. One advantage of this idea as a project for an intermediate programmer is that the implementation is not complicated; most of the fun is in the design. -- Neil Cerutti From demianbrecht at gmail.com Mon May 20 15:20:21 2013 From: demianbrecht at gmail.com (Demian Brecht) Date: Mon, 20 May 2013 12:20:21 -0700 Subject: What was the project that made you feel skilled in Python? In-Reply-To: <5198B7E9.7050505@nedbatchelder.com> References: <5198B7E9.7050505@nedbatchelder.com> Message-ID: TBH, I think that the first thing that I did that made me feel that I could hold my own was when I had my first (and only thus far) patch accepted into the stdlib. To me, there's a /big/ difference between throwing together a package that a few people may find useful and putting a patch together that's accepted by a particular module expert and committed to the stdlib. To intermediate learners, I would strongly advocate getting their hands dirty with some part of the stdlib. Really (imho), there's really no better place to learn. Yes, in whole, it's a large project, but there are quite a few small(er) modules that, once their environment is set up, are self-contained and easy to follow along. Even if they don't get anything committed, learning from Python experts is far more useful than any other path that I've personally taken. Having said that, another great learning experience for me was when I wrote my OAuth 2.0 client (https://github.com/demianbrecht/sanction) and brought the initial implementation (460'ish LoC) to 66 LoC (pre-2/3 support). In part, this was due to taking a different design approach, but it was also in part due to taking advantage of Python idioms rather than simply using approaches that I had used in other languages. On Sun, May 19, 2013 at 4:30 AM, Ned Batchelder wrote: > Hi all, I'm trying to come up with more project ideas for intermediate > learners, somewhat along the lines of > http://bit.ly/intermediate-python-projects . > > So here's a question for people who remember coming up from beginner: as you > moved from exercises like those in Learn Python the Hard Way, up to your own > self-guided work on small projects, what project were you working on that > made you feel independent and skilled? What program first felt like your > own work rather than an exercise the teacher had assigned? > > I don't want anything too large, but big enough that there's room for > design, and multiple approaches, etc. > > Thanks in advance! > > --Ned. > -- > http://mail.python.org/mailman/listinfo/python-list -- Demian Brecht http://demianbrecht.github.com From thomasmurphymusic at gmail.com Mon May 20 15:36:09 2013 From: thomasmurphymusic at gmail.com (Thomas Murphy) Date: Mon, 20 May 2013 15:36:09 -0400 Subject: What was the project that made you feel skilled in Python? In-Reply-To: References: <5198B7E9.7050505@nedbatchelder.com> Message-ID: Hi Demian, Can I ask what you mean by working through the stdlib? As in writing code pieces utilizing each module from the stdlib? Also, you're talking about "patches" in the stdlib? Is there a separate library of patches? Forgive me if I'm google-failing hard over here. From tjreedy at udel.edu Mon May 20 16:03:05 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Mon, 20 May 2013 16:03:05 -0400 Subject: What was the project that made you feel skilled in Python? In-Reply-To: References: <5198B7E9.7050505@nedbatchelder.com> Message-ID: On 5/20/2013 3:36 PM, Thomas Murphy wrote: > > talking about "patches" in the stdlib? Is there a separate library of > patches? http://bugs.python.org http://docs.python.org/devguide/ From ben+python at benfinney.id.au Wed May 22 09:05:58 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 22 May 2013 23:05:58 +1000 Subject: What was the project that made you feel skilled in Python? References: <5198B7E9.7050505@nedbatchelder.com> Message-ID: <7w61ybdv0p.fsf@benfinney.id.au> Ned Batchelder writes: > as you moved from exercises like those in Learn Python the Hard Way, > up to your own self-guided work on small projects, what project were > you working on that made you feel independent and skilled? What > program first felt like your own work rather than an exercise the > teacher had assigned? I wanted to simulate a particular board game, and had others in mind with some common mechanics. This resulted in a library for rolling dice in different combinations, and looking up result tables . Eventually I wanted to extend it to know about custom decks of cards, and the different ways those are handled in board games. The unifying theme was a library of routines for simulating the random elements (dice, cards, tables, spinners, etc.) in any board game. A little over-engineered, I'll freely admit. But it did give me a sense of being at home in Python and knowing that this is a good language for getting things done the right way. -- \ ?Creativity can be a social contribution, but only in so far as | `\ society is free to use the results.? ?Richard Stallman | _o__) | Ben Finney From rosuav at gmail.com Wed May 22 10:43:23 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 00:43:23 +1000 Subject: What was the project that made you feel skilled in Python? In-Reply-To: <7w61ybdv0p.fsf@benfinney.id.au> References: <5198B7E9.7050505@nedbatchelder.com> <7w61ybdv0p.fsf@benfinney.id.au> Message-ID: On Wed, May 22, 2013 at 11:05 PM, Ben Finney wrote: > I wanted to simulate a particular board game, and had others in mind > with some common mechanics. > > This resulted in a library for rolling dice in different combinations, > and looking up result tables . Fun fun! Of course, when I hear "rolling dice in different combinations", my mind immediately turns to Dungeons and Dragons, where it's plausible to roll d20+7, then roll 2d8+d6+12 to figure out how much damage you did... But the hard part of board games is usually the board. I used to spend ages trying to draw up a half-decent board, and ended up giving up. By "simulate", I'm guessing you mean that you didn't actually draw anything of the sort? ChrisA From ben+python at benfinney.id.au Thu May 23 19:32:38 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 24 May 2013 09:32:38 +1000 Subject: What was the project that made you feel skilled in Python? References: <5198B7E9.7050505@nedbatchelder.com> <7w61ybdv0p.fsf@benfinney.id.au> Message-ID: <7w1u8xuvah.fsf@benfinney.id.au> Chris Angelico writes: > Ben Finney wrote: > > This resulted in a library for rolling dice in different > > combinations, and looking up result tables > > . > > Fun fun! Of course, when I hear "rolling dice in different > combinations", my mind immediately turns to Dungeons and Dragons, > where it's plausible to roll d20+7, then roll 2d8+d6+12 to figure out > how much damage you did... Yeah, and lots of board games use custom dice with faces specific to that game (symbols, non-consecutive numbers, etc.), so the Die class allows the faces to be any object the application needs. > But the hard part of board games is usually the board. A lot of the board games I'm intrigued by don't have much of a board; they use custom cards and tokens and (maybe) dice, and the ?board? is an abstraction of where all the pieces are on the table. > I used to spend ages trying to draw up a half-decent board, and ended > up giving up. By "simulate", I'm guessing you mean that you didn't > actually draw anything of the sort? Right. The (never completed) application was to simulate the mechanics of that particular game so I could see how it would play out, not be an interactive playable game. I've long been aware there is an enormous amount of UI-programming work involved with interactive playable games. My ambition for that work was quenched from attempting it in my teenage years :-) -- \ ?With Lisp or Forth, a master programmer has unlimited power | `\ and expressiveness. With Python, even a regular guy can reach | _o__) for the stars.? ?Raymond Hettinger | Ben Finney From tjreedy at udel.edu Wed May 22 15:52:14 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Wed, 22 May 2013 15:52:14 -0400 Subject: What was the project that made you feel skilled in Python? In-Reply-To: <7w61ybdv0p.fsf@benfinney.id.au> References: <5198B7E9.7050505@nedbatchelder.com> <7w61ybdv0p.fsf@benfinney.id.au> Message-ID: On 5/22/2013 9:05 AM, Ben Finney wrote: > I wanted to simulate a particular board game, and had others in mind > with some common mechanics. > > This resulted in a library for rolling dice in different combinations, > and looking up result tables . Have you cosidered adding a description so it can be found be a search? A 3.3 version? "Simulate game randomizers and lookup: dice rolls, card drawing, spinners, ... > Eventually I wanted to extend it to know about custom decks of cards, > and the different ways those are handled in board games. > > The unifying theme was a library of routines for simulating the random > elements (dice, cards, tables, spinners, etc.) in any board game. > > A little over-engineered, I'll freely admit. But it did give me a sense > of being at home in Python and knowing that this is a good language for > getting things done the right way. > From nicholas.cole at gmail.com Sat May 25 04:17:08 2013 From: nicholas.cole at gmail.com (Nicholas Cole) Date: Sat, 25 May 2013 09:17:08 +0100 Subject: What was the project that made you feel skilled in Python? In-Reply-To: <5198B7E9.7050505@nedbatchelder.com> References: <5198B7E9.7050505@nedbatchelder.com> Message-ID: On Sun, May 19, 2013 at 12:30 PM, Ned Batchelder wrote: > Hi all, I'm trying to come up with more project ideas for intermediate > learners, somewhat along the lines of http://bit.ly/intermediate-** > python-projects . > > So here's a question for people who remember coming up from beginner: as > you moved from exercises like those in Learn Python the Hard Way, up to > your own self-guided work on small projects, what project were you working > on that made you feel independent and skilled? What program first felt > like your own work rather than an exercise the teacher had assigned? I had been doing some part-time work on server administration while I was a graduate student, and had written a python library to help display user-interfaces for some of the scripts we used internally. At some point, I discovered that as I had originally written it, I was triggering a small memory leak, caused by the interaction between python and the underlying curses c library. In the real world, it would never have mattered, but I minded very much that the problem existed at all. To fix the fault required rewriting the whole library from scratch, and I did the initial version of this while on a long train journey away from internet access. I could have just left it -- for the scripts we were running it didn't matter at all -- but it was a matter of pride to write code that didn't behave badly, even in theory. I ended up putting the code online, and 8 years and a huge number of public releases later it still seems to be useful to a few people, and I've learned an awful lot doing it. I think my proudest "python" moment was when I first got a bug report from someone I didn't know. http://code.google.com/p/npyscreen/ Best wishes, N. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dreamingforward at gmail.com Sun May 26 00:28:24 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Sat, 25 May 2013 21:28:24 -0700 Subject: What was the project that made you feel skilled in Python? In-Reply-To: References: <5198B7E9.7050505@nedbatchelder.com> Message-ID: Building a Set class (this was before python had them built-in). Such exercise stimulates thinking on designing objects that could be part of the standard library, the questions that arise on API design, questions on efficiency of implementation. Then a graph class which can be built from your newly-built set module. Mark Tacoma, Washington On 5/25/13, Nicholas Cole wrote: > On Sun, May 19, 2013 at 12:30 PM, Ned Batchelder > wrote: > >> Hi all, I'm trying to come up with more project ideas for intermediate >> learners, somewhat along the lines of http://bit.ly/intermediate-** >> python-projects . >> >> So here's a question for people who remember coming up from beginner: as >> you moved from exercises like those in Learn Python the Hard Way, up to >> your own self-guided work on small projects, what project were you >> working >> on that made you feel independent and skilled? What program first felt >> like your own work rather than an exercise the teacher had assigned? > > > I had been doing some part-time work on server administration while I was a > graduate student, and had written a python library to help display > user-interfaces for some of the scripts we used internally. At some point, > I discovered that as I had originally written it, I was triggering a small > memory leak, caused by the interaction between python and the underlying > curses c library. In the real world, it would never have mattered, but I > minded very much that the problem existed at all. > > To fix the fault required rewriting the whole library from scratch, and I > did the initial version of this while on a long train journey away from > internet access. I could have just left it -- for the scripts we were > running it didn't matter at all -- but it was a matter of pride to write > code that didn't behave badly, even in theory. > > I ended up putting the code online, and 8 years and a huge number of public > releases later it still seems to be useful to a few people, and I've > learned an awful lot doing it. I think my proudest "python" moment was > when I first got a bug report from someone I didn't know. > > http://code.google.com/p/npyscreen/ > > Best wishes, > > N. > -- MarkJ Tacoma, Washington From avnesh.nitk at gmail.com Sun May 19 23:54:11 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Sun, 19 May 2013 20:54:11 -0700 (PDT) Subject: How to run a python script twice randomly in a day? Message-ID: <8a44bb69-5e8c-4725-a0c8-505b3ecc1d89@googlegroups.com> hi, How to run a python script twice randomly in a day? Actually I want to run my script randomly in a day and twice only. Please help me.. how is it possible. Thanks From jsf80238 at gmail.com Mon May 20 00:27:15 2013 From: jsf80238 at gmail.com (Jason Friedman) Date: Sun, 19 May 2013 22:27:15 -0600 Subject: How to run a python script twice randomly in a day? In-Reply-To: <8a44bb69-5e8c-4725-a0c8-505b3ecc1d89@googlegroups.com> References: <8a44bb69-5e8c-4725-a0c8-505b3ecc1d89@googlegroups.com> Message-ID: > How to run a python script twice randomly in a day? Actually I want to run my script randomly in a day and twice only I can think of two basic approaches. One, use crontab or some other similar functionality to call it exactly twice. Two, use crontab or some other similar functionality to call it every minute, and add code to your script to execute exactly twice. Which are you preferring? From cs at zip.com.au Mon May 20 00:12:05 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 20 May 2013 14:12:05 +1000 Subject: How to run a python script twice randomly in a day? In-Reply-To: <8a44bb69-5e8c-4725-a0c8-505b3ecc1d89@googlegroups.com> References: <8a44bb69-5e8c-4725-a0c8-505b3ecc1d89@googlegroups.com> Message-ID: <20130520041205.GA97845@cskk.homeip.net> On 19May2013 20:54, Avnesh Shakya wrote: | How to run a python script twice randomly in a day? Actually | I want to run my script randomly in a day and twice only. Please | help me.. how is it possible. Do you mean "run twice a day, each at random times"? If so, do the obvious: at midnight, pick two random times. Sleep until the first time, run the script, sleep until the second time, run the script. There are various ways to do the sleeping and midnight bits; they're up to you. Enjoy, -- Cameron Simpson The ZZR-1100 is not the bike for me, but the day they invent "nerf" roads and ban radars I'll be the first in line......AMCN From cs at zip.com.au Mon May 20 03:40:02 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 20 May 2013 17:40:02 +1000 Subject: How to run a python script twice randomly in a day? In-Reply-To: References: Message-ID: <20130520074002.GA20235@cskk.homeip.net> On 20May2013 09:47, Avnesh Shakya wrote: | On Mon, May 20, 2013 at 9:42 AM, Cameron Simpson wrote: | > On 19May2013 20:54, Avnesh Shakya wrote: | > | How to run a python script twice randomly in a day? Actually | > | I want to run my script randomly in a day and twice only. Please | > | help me.. how is it possible. | > | > Do you mean "run twice a day, each at random times"? | > | > If so, do the obvious: at midnight, pick two random times. Sleep | > until the first time, run the script, sleep until the second time, | > run the script. | > | > There are various ways to do the sleeping and midnight bits; they're | > up to you. | | Thanks, Can you mail documentation or link for it? I am totally new for it. 1: Please reply on-list; you asked the list, the discussion should remain there. I have added the list to the CC line. 2: Please don't top-post. Quote only the relevant bits of the previous message and reply below. If the message is long, do that in pieces. [quote] reply [quote] reply. Like a conversation. Now, to your questions. A UNIX user would use "cron" to schedule the midnight job and from the midnight job then probably use "at" to schedule the other jobs to run at specific times. See "man 1 crontab", "man 5 crontab" to submit the cron jobs and "man at" to submit the once off jobs. UNIX means Linux, Solaris, AIX, any of the BSDs (includes MacOSX), etc. You could a small python script for the midnight job, and it would submit the "at" jobs. See the "random" module to compute a pseudorandom number (and thus a random time), the "datetime" module to compute the dates and times from that number, and the "subprocess" module to submit the "at" job for the chosen run times. The modules are here: http://docs.python.org/3/py-modindex.html That presumes python 3; if you're using python 2 the docs for that are available at the same web site. You don't need to use "at". Your midnight job could just compute the time in seconds to each job time, then use the "time.sleep" function to delay until then. See the "time" module at the above link. Nobody on this list will write your program for you. Attempt to write the program, then come to the list with your attempt and any problems. People will help if you've made an initial effort, and continue to try. This question has the "feel" of a homework question; the point of homework is for you to learn by solving a problem yourself. It is fine to seek help, but you must make the effort yourself. Come back with specific questions. Cheers, -- Cameron Simpson I'm not weird; I'm gifted. From cs at zip.com.au Mon May 20 21:12:20 2013 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 21 May 2013 11:12:20 +1000 Subject: How to run a python script twice randomly in a day? In-Reply-To: References: Message-ID: <20130521011220.GA62488@cskk.homeip.net> On 20May2013 15:05, Avnesh Shakya wrote: | Thanks a lot. No worries, but ... AGAIN: - please DO NOT top post. Post below, trimming the quoted material. - please POST TO THE LIST, not just to me. This is a public discussion. Now... | I did something. | I have created test.sh file in which i put- | | #!/bin/bash | cd /home/avin/cronJob | python try.py Ok, good. Some minor remarks: Personally, I always use: #!/bin/sh instead of requiring bash. All UNIX systems have sh, bash is only common. And even when present, it may not be in /bin. /bin/sh is always there, and unless you're doing something quite unusual, it works just fine. | then i went on terminal - | and run crontab -e | and wrote- | */2 * * * * bash /home/avin/cronJob/test.sh | and saved it. IIRC, this runs every two minutes. Good for testing, but not your original spec. Also, if you make the shell script (test.sh) executable you do not need to specify the interpreter. Treat your script like any other command! So: chmod +rx /home/avin/cronJob/test.sh and then your cron line can look like this: */2 * * * * /home/avin/cronJob/test.sh Also, treat your script the same way as your shell script, start it with a #! like this: #!/usr/bin/python Make it executable: chmod +rx /home/avin/cronJob/try.py and then you don't need to say "python" in your shell script: ./try.py (You need the ./ because the current directory is not in your command search path ($PATH).) | It's working fine. | but when I m using like | | import random | a = random.randrange(0, 59) | */a * * * * bash /home/avin/cronJob/test.sh | then it's showing error becose of varable 'a', so now how can i take | variable? I take it that this is your python program intended to schedule the two randomly timed runs? As a start, it must all be python. The first two lines are. The third line is a crontab line. So as a start, you need to look more like this: #!/usr/bin/python import random a = random.randrange(0, 59) cronline = '*/%d * * * * /home/avin/cronJob/test.sh' % (a,) print(cronline) At least then you can see the cron line you're making. It still does not add it to a cron job. Some remarks: - randrange() is like other python ranges: it does not include the end value. So your call picks a number from 0..58, not 0..59. Say randrange(0,60). Think "start, length". - My recollection is that you wanted to run a script twice a day at random times. Your cron line doesn't do that. - If you're picking random run times you want to schedule a once-off job for each to run at a particular times. Cron schedules repeating jobs. To run at a particular time you want an "at" job. - You need to do one of two things in the pick-a-time script: - pick a time, then sleep until that time and then directly invoke the try.py script or - pick a time, then use the "at" command to schedule the try.py (or test.sh) script. The first approach would look a bit like this (totally untested): #!/usr/bin/python import random import subporcess import time # choose range time in the next 24 hours when = random.randrange(0, 24 * 3600) # sleep that many seconds time.sleep(when) subprocess.call(['/home/avin/cronJob/test.sh']) For two runs, pick two times. Swap them into order. Sleep twice, once until the first time and then once until the second time. Etc. The second approach (using "at") would not sleep. instead, compute (using the datetime module) the date and time each job should run, and invoke "at" using the subprocess module, piping the text "/home/avin/cronJob/test.sh\n" to it. Cheers, -- Cameron Simpson On a related topic, has anyone looked at doing a clean-room copy of CSS a la RC2 and RC4 a few years back? I know one or two people have looked at this in an informal manner, but we couldn't find anyone who hadn't already seen the DeCSS code to act as the clean person (it says a lot for the status of their "trade secret" that we couldn't actually find anyone who didn't already know it). - Peter Gutmann From avnesh.nitk at gmail.com Tue May 21 00:36:00 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Tue, 21 May 2013 10:06:00 +0530 Subject: How to run a python script twice randomly in a day? In-Reply-To: <20130521011220.GA62488@cskk.homeip.net> References: <20130521011220.GA62488@cskk.homeip.net> Message-ID: Thanks a lot. I got it. On Tue, May 21, 2013 at 6:42 AM, Cameron Simpson wrote: > On 20May2013 15:05, Avnesh Shakya wrote: > | Thanks a lot. > > No worries, but ... > > AGAIN: > - please DO NOT top post. Post below, trimming the quoted material. > - please POST TO THE LIST, not just to me. This is a public discussion. > > Now... > > | I did something. > | I have created test.sh file in which i put- > | > | #!/bin/bash > | cd /home/avin/cronJob > | python try.py > > Ok, good. Some minor remarks: > > Personally, I always use: > > #!/bin/sh > > instead of requiring bash. All UNIX systems have sh, bash is only > common. And even when present, it may not be in /bin. /bin/sh is > always there, and unless you're doing something quite unusual, it > works just fine. > > | then i went on terminal - > | and run crontab -e > | and wrote- > | */2 * * * * bash /home/avin/cronJob/test.sh > | and saved it. > > IIRC, this runs every two minutes. Good for testing, but not your original > spec. > > Also, if you make the shell script (test.sh) executable you do not > need to specify the interpreter. Treat your script like any other > command! So: > > chmod +rx /home/avin/cronJob/test.sh > > and then your cron line can look like this: > > */2 * * * * /home/avin/cronJob/test.sh > > Also, treat your script the same way as your shell script, start > it with a #! like this: > > #!/usr/bin/python > > Make it executable: > > chmod +rx /home/avin/cronJob/try.py > > and then you don't need to say "python" in your shell script: > > ./try.py > > (You need the ./ because the current directory is not in your command > search path ($PATH).) > > | It's working fine. > | but when I m using like > | > | import random > | a = random.randrange(0, 59) > | */a * * * * bash /home/avin/cronJob/test.sh > | then it's showing error becose of varable 'a', so now how can i take > | variable? > > I take it that this is your python program intended to schedule the two > randomly timed runs? > > As a start, it must all be python. The first two lines are. The third line > is > a crontab line. > > So as a start, you need to look more like this: > > #!/usr/bin/python > import random > a = random.randrange(0, 59) > cronline = '*/%d * * * * /home/avin/cronJob/test.sh' % (a,) > print(cronline) > > At least then you can see the cron line you're making. It still > does not add it to a cron job. > > Some remarks: > > - randrange() is like other python ranges: it does not include the end > value. > So your call picks a number from 0..58, not 0..59. > Say randrange(0,60). Think "start, length". > > - My recollection is that you wanted to run a script twice a day at random > times. > Your cron line doesn't do that. > > - If you're picking random run times you want to schedule a once-off > job for each to run at a particular times. Cron schedules repeating > jobs. To run at a particular time you want an "at" job. > > - You need to do one of two things in the pick-a-time script: > - pick a time, then sleep until that time and then directly > invoke the try.py script > or > - pick a time, then use the "at" command to schedule the try.py > (or test.sh) script. > > The first approach would look a bit like this (totally untested): > > #!/usr/bin/python > import random > import subporcess > import time > # choose range time in the next 24 hours > when = random.randrange(0, 24 * 3600) > # sleep that many seconds > time.sleep(when) > subprocess.call(['/home/avin/cronJob/test.sh']) > > For two runs, pick two times. Swap them into order. Sleep twice, > once until the first time and then once until the second time. Etc. > > The second approach (using "at") would not sleep. instead, compute > (using the datetime module) the date and time each job should run, > and invoke "at" using the subprocess module, piping the text > "/home/avin/cronJob/test.sh\n" to it. > > Cheers, > -- > Cameron Simpson > > On a related topic, has anyone looked at doing a clean-room copy of CSS > a la RC2 and RC4 a few years back? I know one or two people have > looked at this in an informal manner, but we couldn't find anyone who > hadn't already seen the DeCSS code to act as the clean person (it says > a lot for the status of their "trade secret" that we couldn't actually > find anyone who didn't already know it). > - Peter Gutmann > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue May 21 03:56:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 17:56:27 +1000 Subject: How to run a python script twice randomly in a day? In-Reply-To: <20130521011220.GA62488@cskk.homeip.net> References: <20130521011220.GA62488@cskk.homeip.net> Message-ID: On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson wrote: > Ok, good. Some minor remarks: > > Personally, I always use: > > #!/bin/sh > > instead of requiring bash. All UNIX systems have sh, bash is only > common. And even when present, it may not be in /bin. /bin/sh is > always there, and unless you're doing something quite unusual, it > works just fine. Also, on many systems, /bin/sh is a much lighter interpreter than bash (eg Debian uses dash). It's more efficient to use that when you can, even if you use bash for your login shell. > On 20May2013 15:05, Avnesh Shakya wrote: > | but when I m using like > | > | import random > | a = random.randrange(0, 59) > | */a * * * * bash /home/avin/cronJob/test.sh > | then it's showing error becose of varable 'a', so now how can i take > | variable? You put that into your crontab? I do not think this means what you think it means; cron does not execute arbitrary Python code. > - randrange() is like other python ranges: it does not include the end value. > So your call picks a number from 0..58, not 0..59. > Say randrange(0,60). Think "start, length". Nitpick: It's not start, length; it's start, stop-before. If the start is 10 and the second argument is 20, you'll get numbers from 10 to 19. But your conclusion is still accurate :) ChrisA (two Princess Bride references in as many threads, doing well!) From cs at zip.com.au Tue May 21 06:32:39 2013 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 21 May 2013 20:32:39 +1000 Subject: How to run a python script twice randomly in a day? In-Reply-To: References: Message-ID: <20130521103239.GA24261@cskk.homeip.net> On 21May2013 17:56, Chris Angelico wrote: | On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson wrote: | > - randrange() is like other python ranges: it does not include the end value. | > So your call picks a number from 0..58, not 0..59. | > Say randrange(0,60). Think "start, length". | | Nitpick: It's not start, length; it's start, stop-before. If the start | is 10 and the second argument is 20, you'll get numbers from 10 to 19. | But your conclusion is still accurate :) But it's still a useful thing to think when you're trying to reason about ranges unless you're doing something unusual. Cheers, -- Cameron Simpson Life IS pain, highness... anyone who tries to tell you different is trying to sell you something. - Wesley, The_Princess_Bride From davea at davea.name Tue May 21 09:54:06 2013 From: davea at davea.name (Dave Angel) Date: Tue, 21 May 2013 09:54:06 -0400 Subject: How to run a python script twice randomly in a day? In-Reply-To: <20130521103239.GA24261@cskk.homeip.net> References: <20130521103239.GA24261@cskk.homeip.net> Message-ID: <519B7C7E.1010002@davea.name> On 05/21/2013 06:32 AM, Cameron Simpson wrote: > On 21May2013 17:56, Chris Angelico wrote: > | On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson wrote: > | > - randrange() is like other python ranges: it does not include the end value. > | > So your call picks a number from 0..58, not 0..59. > | > Say randrange(0,60). Think "start, length". > | > | Nitpick: It's not start, length; it's start, stop-before. If the start > | is 10 and the second argument is 20, you'll get numbers from 10 to 19. > | But your conclusion is still accurate :) > > But it's still a useful thing to think when you're trying to reason > about ranges unless you're doing something unusual. > No, it's only happens to look like length when start is zero. So as a mnemonic, it's highly misleading. -- DaveA From cs at zip.com.au Tue May 21 23:00:23 2013 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 22 May 2013 13:00:23 +1000 Subject: How to run a python script twice randomly in a day? In-Reply-To: <519B7C7E.1010002@davea.name> References: <519B7C7E.1010002@davea.name> Message-ID: <20130522030023.GA53359@cskk.homeip.net> On 21May2013 09:54, Dave Angel wrote: | On 05/21/2013 06:32 AM, Cameron Simpson wrote: | >On 21May2013 17:56, Chris Angelico wrote: | >| On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson wrote: | >| > - randrange() is like other python ranges: it does not include the end value. | >| > So your call picks a number from 0..58, not 0..59. | >| > Say randrange(0,60). Think "start, length". | >| | >| Nitpick: It's not start, length; it's start, stop-before. If the start | >| is 10 and the second argument is 20, you'll get numbers from 10 to 19. | >| But your conclusion is still accurate :) | > | >But it's still a useful thing to think when you're trying to reason | >about ranges unless you're doing something unusual. | | No, it's only happens to look like length when start is zero. So as | a mnemonic, it's highly misleading. Feh! No self respecting computer scientist would ever count from other than zero! Actually, yes, you're right there. Cheers, -- Cameron Simpson Q: How does a hacker fix a function which doesn't work for all of the elements in its domain? A: He changes the domain. From jpiitula at ling.helsinki.fi Tue May 21 04:21:39 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 21 May 2013 11:21:39 +0300 Subject: How to run a python script twice randomly in a day? References: <20130521011220.GA62488@cskk.homeip.net> Message-ID: Chris Angelico writes: > > On 20May2013 15:05, Avnesh Shakya wrote: > > So your call picks a number from 0..58, not 0..59. > > Say randrange(0,60). Think "start, length". > > Nitpick: It's not start, length; it's start, stop-before. If the > start is 10 and the second argument is 20, you'll get numbers from > 10 to 19. But your conclusion is still accurate :) I've sometimes named the latter index "past", as in just past the range. I'm also happy to call it just "end". The inclusive-style names might be "first" and "last", so "past" is "last + 1". The length of the range from "start" to "end" is "end - start" without a "pest" term that is either -1 or +1 though I forget which; two consecutive ranges are from b to m, then from m to e; an empty range is from b to b. From rosuav at gmail.com Tue May 21 05:18:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 19:18:25 +1000 Subject: How to run a python script twice randomly in a day? In-Reply-To: References: <20130521011220.GA62488@cskk.homeip.net> Message-ID: On Tue, May 21, 2013 at 6:21 PM, Jussi Piitulainen wrote: > Chris Angelico writes: > >> > On 20May2013 15:05, Avnesh Shakya wrote: >> > So your call picks a number from 0..58, not 0..59. >> > Say randrange(0,60). Think "start, length". >> >> Nitpick: It's not start, length; it's start, stop-before. If the >> start is 10 and the second argument is 20, you'll get numbers from >> 10 to 19. But your conclusion is still accurate :) > > I've sometimes named the latter index "past", as in just past the > range. I'm also happy to call it just "end". The inclusive-style names > might be "first" and "last", so "past" is "last + 1". > > The length of the range from "start" to "end" is "end - start" without > a "pest" term that is either -1 or +1 though I forget which; two > consecutive ranges are from b to m, then from m to e; an empty range > is from b to b. Agreed. The inclusive-exclusive range is by far the most useful. There's unfortunately a massive case of lock-in here, but Scripture references would be ever so much more convenient as inc-exc. For instance, today in family devotions we read Galatians 2:1 - 2:21. At a glance, do you know whether that's the entire chapter? What if it were written as Galatians 2 to Galatians 3? Simple! ChrisA From frank at chagford.com Mon May 20 03:05:48 2013 From: frank at chagford.com (Frank Millman) Date: Mon, 20 May 2013 09:05:48 +0200 Subject: Question about ast.literal_eval Message-ID: Hi all I am trying to emulate a SQL check constraint in Python. Quoting from the PostgreSQL docs, "A check constraint is the most generic constraint type. It allows you to specify that the value in a certain column must satisfy a Boolean (truth-value) expression." The problem is that I want to store the constraint as a string, and I was hoping to use ast.literal_eval to evaluate it, but it does not work. >>> x = 'abc' >>> x in ('abc', xyz') True >>> b = "x in ('abc', 'xyz')" >>> eval(b) True >>> from ast import literal_eval >>> literal_eval(b) ValueError: malformed node or string: <_ast.Compare object at ...> Is there a safe way to do what I want? I am using python 3.3. Thanks Frank Millman From carlosnepomuceno at outlook.com Mon May 20 03:34:47 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 20 May 2013 10:34:47 +0300 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: It seems to me you can't use ast.literal_eval()[1] to evaluate that kind of expression because it's just for literals[2]. Why don't you use eval()? [1] http://docs.python.org/2/library/ast.html#ast-helpers [2] http://docs.python.org/2/reference/lexical_analysis.html#literals ---------------------------------------- > To: python-list at python.org > From: frank at chagford.com > Subject: Question about ast.literal_eval > Date: Mon, 20 May 2013 09:05:48 +0200 > > Hi all > > I am trying to emulate a SQL check constraint in Python. Quoting from > the PostgreSQL docs, "A check constraint is the most generic constraint > type. It allows you to specify that the value in a certain column must > satisfy a Boolean (truth-value) expression." > > The problem is that I want to store the constraint as a string, and I > was hoping to use ast.literal_eval to evaluate it, but it does not work. > >>>> x = 'abc' >>>> x in ('abc', xyz') > True >>>> b = "x in ('abc', 'xyz')" >>>> eval(b) > True >>>> from ast import literal_eval >>>> literal_eval(b) > ValueError: malformed node or string: <_ast.Compare object at ...> > > Is there a safe way to do what I want? I am using python 3.3. > > Thanks > > Frank Millman > > -- > http://mail.python.org/mailman/listinfo/python-list From frank at chagford.com Mon May 20 03:50:02 2013 From: frank at chagford.com (Frank Millman) Date: Mon, 20 May 2013 09:50:02 +0200 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: [Corrected top-posting] >> To: python-list at python.org >> From: frank at chagford.com >> Subject: Question about ast.literal_eval >> Date: Mon, 20 May 2013 09:05:48 +0200 >> >> Hi all >> >> I am trying to emulate a SQL check constraint in Python. Quoting from >> the PostgreSQL docs, "A check constraint is the most generic constraint >> type. It allows you to specify that the value in a certain column must >> satisfy a Boolean (truth-value) expression." >> >> The problem is that I want to store the constraint as a string, and I >> was hoping to use ast.literal_eval to evaluate it, but it does not work. >> On 20/05/2013 09:34, Carlos Nepomuceno wrote: > It seems to me you can't use ast.literal_eval()[1] to evaluate that kind of expression > because it's just for literals[2]. > > Why don't you use eval()? > Because users can create their own columns, with their own constraints. Therefore the string is user-modifiable, so it cannot be trusted. Frank From rosuav at gmail.com Mon May 20 03:55:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 20 May 2013 17:55:03 +1000 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On Mon, May 20, 2013 at 5:50 PM, Frank Millman wrote: > On 20/05/2013 09:34, Carlos Nepomuceno wrote: >> Why don't you use eval()? >> > > Because users can create their own columns, with their own constraints. > Therefore the string is user-modifiable, so it cannot be trusted. Plenty of reason right there :) Is it a requirement that they be able to key in a constraint as a single string? We have a similar situation in one of the systems at work, so we divided the input into three(ish) parts: pick a field, pick an operator (legal operators vary according to field type - integers can't be compared against regular expressions, timestamps can use >= and < only), then enter the other operand. Sure, that cuts out a few possibilities, but you get 99.9%+ of all usage and it's easy to sanitize. ChrisA From frank at chagford.com Mon May 20 04:07:43 2013 From: frank at chagford.com (Frank Millman) Date: Mon, 20 May 2013 10:07:43 +0200 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On 20/05/2013 09:55, Chris Angelico wrote: > On Mon, May 20, 2013 at 5:50 PM, Frank Millman wrote: >> On 20/05/2013 09:34, Carlos Nepomuceno wrote: >>> Why don't you use eval()? >>> >> >> Because users can create their own columns, with their own constraints. >> Therefore the string is user-modifiable, so it cannot be trusted. > > Plenty of reason right there :) > > Is it a requirement that they be able to key in a constraint as a > single string? We have a similar situation in one of the systems at > work, so we divided the input into three(ish) parts: pick a field, > pick an operator (legal operators vary according to field type - > integers can't be compared against regular expressions, timestamps can > use >= and < only), then enter the other operand. Sure, that cuts out > a few possibilities, but you get 99.9%+ of all usage and it's easy to > sanitize. > > ChrisA > It is not a requirement, no. I just thought it would be a convenient short-cut. I had in mind something similar to your scheme above, so I guess I will have to bite the bullet and implement it. Thanks Frank From frank at chagford.com Mon May 20 09:26:02 2013 From: frank at chagford.com (Frank Millman) Date: Mon, 20 May 2013 15:26:02 +0200 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On 20/05/2013 10:07, Frank Millman wrote: > On 20/05/2013 09:55, Chris Angelico wrote: >> Is it a requirement that they be able to key in a constraint as a >> single string? We have a similar situation in one of the systems at >> work, so we divided the input into three(ish) parts: pick a field, >> pick an operator (legal operators vary according to field type - >> integers can't be compared against regular expressions, timestamps can >> use >= and < only), then enter the other operand. Sure, that cuts out >> a few possibilities, but you get 99.9%+ of all usage and it's easy to >> sanitize. >> >> ChrisA >> > > It is not a requirement, no. I just thought it would be a convenient > short-cut. > > I had in mind something similar to your scheme above, so I guess I will > have to bite the bullet and implement it. > Can anyone see anything wrong with the following approach. I have not definitely decided to do it this way, but I have been experimenting and it seems to work. I store the boolean test as a json'd list of 6-part tuples. Each element of the tuple is a string, defined as follows - 0 - for the first entry in the list, the word 'check' (a placeholder - it is discarded at evaluation time), for any subsequent entries the word 'and' or 'or'. 1 - left bracket - either '(' or ''. 2 - column name to check - it will be validated on entry. 3 - operator - must be one of '=', '!=', '<', '>', '<=', '>=', 'in', 'is', 'is not'. At evaluation time, '=' is changed to '=='. 4 - value to compare - at evaluation time I call str(literal_eval(value)) to ensure that it is safe. 5 - right bracket - either ')' or ''. At evaluation time I loop through the list, construct the boolean test as a string, and call eval() on it. Here are some examples - check = [] check.append(('check', '', 'name', 'in', "('abc', 'xyz')", '')) check = [] check.append(('check', '', 'value', '>=', '0', '')) check = [] check.append(('check', '(', 'descr', 'is not', 'None', '')) check.append(('and', '', 'alt', 'is', 'None', ')')) check.append(('or', '(', 'descr', 'is', 'None', '')) check.append(('and', '', 'alt', 'is not', 'None', ')')) I don't plan to check the logic - I will just display the exception if it does not evaluate. It seems safe to me. Can anyone see a problem with it? Frank From rosuav at gmail.com Mon May 20 12:13:47 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 02:13:47 +1000 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On Mon, May 20, 2013 at 11:26 PM, Frank Millman wrote: > 0 - for the first entry in the list, the word 'check' (a placeholder - it is > discarded at evaluation time), for any subsequent entries the word 'and' or > 'or'. > > 1 - left bracket - either '(' or ''. > > 5 - right bracket - either ')' or ''. I think what you have is safe, but extremely complicated to work with. Six separate pieces, and things have to be in the right slots... I think you've spent too many "complexity points" on the above three components, and you're getting too little return for them. What happens if the nesting is mucked up? Could get verrry messy to check. Combining multiple conditions with a mixture of ands and ors is a nightmare to try to explain (unless you just point to the Python docs, which IMO costs you even more complexity points); the only safe option is to parenthesize everything. The system I pushed for at work (which was finally approved and implemented a few months ago) is more or less this: conditions are grouped together into blocks; for each group, you can choose whether it's "all" or "any" (aka and/or), and you choose whether the overall result is all-groups or any-group. That still costs a few complexity points (and, btw, our *actual* implementation is a bit more complicated than that, but I think we could cut it down to what I just described here without loss of functionality), but it gives the bulk of what people will actually want without the complexities of point-and-click code. The downside of that sort of system is that it requires a two-level tree. On the flip side, that's often how people will be thinking about their conditions anyway (eg using a pair of conditions ">" and "<" to implement a range check - conceptually it's a single check), so that won't cost too much. ChrisA From frank at chagford.com Tue May 21 02:46:08 2013 From: frank at chagford.com (Frank Millman) Date: Tue, 21 May 2013 08:46:08 +0200 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On 20/05/2013 18:13, Chris Angelico wrote: > On Mon, May 20, 2013 at 11:26 PM, Frank Millman wrote: >> 0 - for the first entry in the list, the word 'check' (a placeholder - it is >> discarded at evaluation time), for any subsequent entries the word 'and' or >> 'or'. >> >> 1 - left bracket - either '(' or ''. >> >> 5 - right bracket - either ')' or ''. > > I think what you have is safe, but extremely complicated to work with. > Six separate pieces, and things have to be in the right slots... I > think you've spent too many "complexity points" on the above three > components, and you're getting too little return for them. What > happens if the nesting is mucked up? Could get verrry messy to check. > > Combining multiple conditions with a mixture of ands and ors is a > nightmare to try to explain (unless you just point to the Python docs, > which IMO costs you even more complexity points); the only safe option > is to parenthesize everything. The system I pushed for at work (which > was finally approved and implemented a few months ago) is more or less > this: conditions are grouped together into blocks; for each group, you > can choose whether it's "all" or "any" (aka and/or), and you choose > whether the overall result is all-groups or any-group. That still > costs a few complexity points (and, btw, our *actual* implementation > is a bit more complicated than that, but I think we could cut it down > to what I just described here without loss of functionality), but it > gives the bulk of what people will actually want without the > complexities of point-and-click code. > > The downside of that sort of system is that it requires a two-level > tree. On the flip side, that's often how people will be thinking about > their conditions anyway (eg using a pair of conditions ">" and "<" to > implement a range check - conceptually it's a single check), so that > won't cost too much. > You may be right, Chris, but I don't think my approach is all that bad. The vast majority of tests will be simple - either a single line, or two lines for a range check, with no brackets at all. If the requirement is more complicated than that, well, I don't think the complication can be avoided, and at least this approach gives full control. FWIW, I use the same approach to allow users to construct their own WHERE clauses in custom views. Again, the vast majority are simple, but there are times when it can get complicated. The proof of the pudding will be when I try to get ordinary users to get their own hands dirty - I am not there yet. If I ever get this released, the business model will be free software, but support will be charged for. So if a user gets out of his/her depth, there will be assistance available. Time will tell who is right ... ;-) Frank From rosuav at gmail.com Tue May 21 03:35:40 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 17:35:40 +1000 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On Tue, May 21, 2013 at 4:46 PM, Frank Millman wrote: > You may be right, Chris, but I don't think my approach is all that bad. Frankly, I'm not altogether convinced that our approach is right either :) But like the Oracle in the Matrix, I'm not here to push you to one decision or another so much as to just put the options in front of you and let you make up your own mind. Except in a few cases where I'm really certain of my ground (like "don't put any untrusted data through eval()"...). > The vast majority of tests will be simple - either a single line, or two > lines for a range check, with no brackets at all. > > If the requirement is more complicated than that, well, I don't think the > complication can be avoided, and at least this approach gives full control. Yeah, and this is where the issue of complexity points comes in. You're spending a lot of them on functionality that most users won't even use, and those who do will use only occasionally. You're forcing them to match their brackets (not just have the same number of each type, but also to get the nesting correct), and according to your current spec, there can be no more than one open/close bracket at each condition, so they'll have to arbitrarily add dummy conditions to make certain forms of nesting work. You're exposing a lot of the underlying interpreter, while forcing the user to dance wearing a body cast. Sure, it can work, but it's unnecessarily hard. > FWIW, I use the same approach to allow users to construct their own WHERE > clauses in custom views. Again, the vast majority are simple, but there are > times when it can get complicated. Our alpha system is actually online, and we have exactly that system - a query builder that renders down to a WHERE clause. If you're curious, message me offline and I'll give you the URL. > The proof of the pudding will be when I try to get ordinary users to get > their own hands dirty - I am not there yet. If I ever get this released, the > business model will be free software, but support will be charged for. So if > a user gets out of his/her depth, there will be assistance available. > > Time will tell who is right ... ;-) Who is right, and who is dead. Hey, are you aware that both Steven and I come from Australia, and that we are used to having people not trust us? Truly, you have a dizzying intellect! ChrisA ... couldn't resist... From steve+comp.lang.python at pearwood.info Mon May 20 12:12:27 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 May 2013 16:12:27 GMT Subject: Question about ast.literal_eval References: Message-ID: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> On Mon, 20 May 2013 15:26:02 +0200, Frank Millman wrote: > Can anyone see anything wrong with the following approach. I have not > definitely decided to do it this way, but I have been experimenting and > it seems to work. > > I store the boolean test as a json'd list of 6-part tuples. Each element > of the tuple is a string, defined as follows - > > 0 - for the first entry in the list, the word 'check' (a placeholder - > it is discarded at evaluation time), for any subsequent entries the word > 'and' or 'or'. > > 1 - left bracket - either '(' or ''. > > 2 - column name to check - it will be validated on entry. > > 3 - operator - must be one of '=', '!=', '<', '>', '<=', '>=', 'in', > 'is', 'is not'. At evaluation time, '=' is changed to '=='. > > 4 - value to compare - at evaluation time I call > str(literal_eval(value)) to ensure that it is safe. > > 5 - right bracket - either ')' or ''. > > At evaluation time I loop through the list, construct the boolean test > as a string, and call eval() on it. [...] > It seems safe to me. Can anyone see a problem with it? It seems safe to me too, but then any fool can come up with a system which they themselves cannot break :-) I think the real worry is validating the column name. That will be critical. Personally, I would strongly suggest writing your own mini- evaluator that walks the list and evaluates it by hand. It isn't as convenient as just calling eval, but *definitely* safer. If you do call eval, make sure you supply the globals and locals arguments. The usual way is: eval(expression, {'__builtins__': None}, {}) which gives you an empty locals() and a minimal, (mostly) safe globals. Finally, as a "belt-and-braces" approach, I wouldn't even call eval directly, but call a thin wrapper that raises an exception if the expression contains an underscore. Underscores are usually the key to breaking eval, so refusing to evaluate anything with an underscore raises the barrier very high. And even with all those defences, I wouldn't allow untrusted data from the Internet anywhere near this. Just because I can't break it, doesn't mean it's safe. -- Steven From rosuav at gmail.com Mon May 20 12:23:19 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 02:23:19 +1000 Subject: Question about ast.literal_eval In-Reply-To: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, May 21, 2013 at 2:12 AM, Steven D'Aprano wrote: > Personally, I would strongly suggest writing your own mini- > evaluator that walks the list and evaluates it by hand. It isn't as > convenient as just calling eval, but *definitely* safer. Probably faster, too, for what it's worth - eval is pretty expensive. ChrisA From frank at chagford.com Tue May 21 02:30:03 2013 From: frank at chagford.com (Frank Millman) Date: Tue, 21 May 2013 08:30:03 +0200 Subject: Question about ast.literal_eval In-Reply-To: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> References: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 20/05/2013 18:12, Steven D'Aprano wrote: > On Mon, 20 May 2013 15:26:02 +0200, Frank Millman wrote: > >> Can anyone see anything wrong with the following approach. I have not >> definitely decided to do it this way, but I have been experimenting and >> it seems to work. >> [...] > > It seems safe to me too, but then any fool can come up with a system > which they themselves cannot break :-) > Thanks for the detailed response. > I think the real worry is validating the column name. That will be > critical. I would not pass the actual column name to eval(), I would use it to retrieve a value from a data object and pass that to eval(). However, then your point becomes 'validating the value retrieved'. I had not thought about that. I will investigate further. > Personally, I would strongly suggest writing your own mini- > evaluator that walks the list and evaluates it by hand. It isn't as > convenient as just calling eval, but *definitely* safer. > I am not sure I can wrap my mind around mixed 'and's, 'or's, and brackets. [Thinking aloud] Maybe I can manually reduce each internal test to a True or False, substitute them in the list, and then call eval() on the result. eval('(True and False) or (False or True)') I will experiment with that. > If you do call eval, make sure you supply the globals and locals > arguments. The usual way is: > > eval(expression, {'__builtins__': None}, {}) > > which gives you an empty locals() and a minimal, (mostly) safe globals. > Thanks - I did not know about that. > Finally, as a "belt-and-braces" approach, I wouldn't even call eval > directly, but call a thin wrapper that raises an exception if the > expression contains an underscore. Underscores are usually the key to > breaking eval, so refusing to evaluate anything with an underscore raises > the barrier very high. > > And even with all those defences, I wouldn't allow untrusted data from > the Internet anywhere near this. Just because I can't break it, doesn't > mean it's safe. > All good advice - much appreciated. Frank From steve+comp.lang.python at pearwood.info Tue May 21 03:21:58 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 May 2013 07:21:58 GMT Subject: Question about ast.literal_eval References: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> Message-ID: <519b2096$0$6574$c3e8da3$5496439d@news.astraweb.com> On Tue, 21 May 2013 08:30:03 +0200, Frank Millman wrote: > On 20/05/2013 18:12, Steven D'Aprano wrote: >> Personally, I would strongly suggest writing your own mini- evaluator >> that walks the list and evaluates it by hand. It isn't as convenient as >> just calling eval, but *definitely* safer. >> >> > I am not sure I can wrap my mind around mixed 'and's, 'or's, and > brackets. Parsers are a solved problem in computer science, he says as if he had a clue what he was talking about *wink* Here's a sketch of a solution... suppose you have a sequence of records, looking like this: (bool_op, column_name, comparison_op, literal) with appropriate validation on each field. The very first record has bool_op set to "or". Then, you do something like this: import operator OPERATORS = { '=': operator.eq, 'is': operator.is_, '<': operator.lt, # etc. } def eval_op(column_name, op, literal): value = lookup(column_name) # whatever... return OPERATORS[op](value, literal) result = False for (bool_op, column_name, comparison_op, literal) in sequence: flag = eval_op(column_name, comparison_op, literal) if bool_op == 'and': result = result and flag else: assert bool_op == 'or' result = result or flag # Lazy processing? if result: break and in theory it should all Just Work. -- Steven From frank at chagford.com Tue May 21 04:06:35 2013 From: frank at chagford.com (Frank Millman) Date: Tue, 21 May 2013 10:06:35 +0200 Subject: Question about ast.literal_eval In-Reply-To: <519b2096$0$6574$c3e8da3$5496439d@news.astraweb.com> References: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> <519b2096$0$6574$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 21/05/2013 09:21, Steven D'Aprano wrote: > On Tue, 21 May 2013 08:30:03 +0200, Frank Millman wrote: > >> I am not sure I can wrap my mind around mixed 'and's, 'or's, and >> brackets. > > Parsers are a solved problem in computer science, he says as if he had a > clue what he was talking about *wink* > > Here's a sketch of a solution... suppose you have a sequence of records, > looking like this: > > (bool_op, column_name, comparison_op, literal) > > with appropriate validation on each field. The very first record has > bool_op set to "or". Then, you do something like this: > > import operator > OPERATORS = { > '=': operator.eq, > 'is': operator.is_, > '<': operator.lt, > # etc. > } > > def eval_op(column_name, op, literal): > value = lookup(column_name) # whatever... > return OPERATORS[op](value, literal) > > result = False > > for (bool_op, column_name, comparison_op, literal) in sequence: > flag = eval_op(column_name, comparison_op, literal) > if bool_op == 'and': > result = result and flag > else: > assert bool_op == 'or' > result = result or flag > # Lazy processing? > if result: > break > > and in theory it should all Just Work. That's very clever - thanks, Steven. It doesn't address the issue of brackets. I imagine that the answer is something like - maintain a stack of results for each left bracket, push a level for each right bracket, pop the result or something ... I am sure that with enough trial and error I can get it working, but I might cheat for now and use the trick I mentioned earlier of calling eval() on a sequence of manually derived True/False values. I really can't see anything going wrong with that. BTW, thanks to ChrisA for the following tip - import operator ops = { 'in':lambda x,y: x in y, # operator.contains has the args backwards I would have battled with that one. Frank From fabiosantosart at gmail.com Tue May 21 04:23:16 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 21 May 2013 09:23:16 +0100 Subject: Question about ast.literal_eval In-Reply-To: References: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> <519b2096$0$6574$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 21 May 2013 09:10, "Frank Millman" wrote: > It doesn't address the issue of brackets. I imagine that the answer is something like - > > maintain a stack of results > for each left bracket, push a level > for each right bracket, pop the result > > or something ... > Time for me to suggest pyparsing or PLY. You're better off creating your own AST and walking it to produce python or SQL than reinventing the wheel, I think. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Tue May 21 05:00:48 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 21 May 2013 10:00:48 +0100 Subject: Question about ast.literal_eval In-Reply-To: References: <519a4b6a$0$29997$c3e8da3$5496439d@news.astraweb.com> <519b2096$0$6574$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 21/05/2013 09:23, F?bio Santos wrote: > > On 21 May 2013 09:10, "Frank Millman" > wrote: > > It doesn't address the issue of brackets. I imagine that the answer > is something like - > > > > maintain a stack of results > > for each left bracket, push a level > > for each right bracket, pop the result > > > > or something ... > > > > Time for me to suggest pyparsing or PLY. You're better off creating your > own AST and walking it to produce python or SQL than reinventing the > wheel, I think. > Or pick one from this lot http://nedbatchelder.com/text/python-parsers.html -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From carlosnepomuceno at outlook.com Mon May 20 03:55:35 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 20 May 2013 10:55:35 +0300 Subject: Question about ast.literal_eval In-Reply-To: References: , , Message-ID: ---------------------------------------- > To: python-list at python.org > From: frank at chagford.com > Subject: Re: Question about ast.literal_eval > Date: Mon, 20 May 2013 09:50:02 +0200 > > [Corrected top-posting] > >>> To: python-list at python.org >>> From: frank at chagford.com >>> Subject: Question about ast.literal_eval >>> Date: Mon, 20 May 2013 09:05:48 +0200 >>> >>> Hi all >>> >>> I am trying to emulate a SQL check constraint in Python. Quoting from >>> the PostgreSQL docs, "A check constraint is the most generic constraint >>> type. It allows you to specify that the value in a certain column must >>> satisfy a Boolean (truth-value) expression." >>> >>> The problem is that I want to store the constraint as a string, and I >>> was hoping to use ast.literal_eval to evaluate it, but it does not work. >>> > > On 20/05/2013 09:34, Carlos Nepomuceno wrote: > >> It seems to me you can't use ast.literal_eval()[1] to evaluate that kind of expression >> because it's just for literals[2]. >> >> Why don't you use eval()? >> > > Because users can create their own columns, with their own constraints. > Therefore the string is user-modifiable, so it cannot be trusted. I understand your motivation but I don't know what protection ast.literal_eval() is offering that eval() doesn't. > Frank > > > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Mon May 20 04:13:18 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 20 May 2013 18:13:18 +1000 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On Mon, May 20, 2013 at 5:55 PM, Carlos Nepomuceno wrote: > I understand your motivation but I don't know what protection ast.literal_eval() is offering that eval() doesn't. eval will *execute code*, while literal_eval will not. That's the protection. With ast.literal_eval, all that can happen is that it produces a single result value. In this case, unfortunately, that's insufficient; a comparison needs to be done, ergo it's not an entire literal. But something else in the ast module may be able to serve, or maybe literal_eval can do the bulk of the work. ChrisA From frank at chagford.com Mon May 20 04:15:49 2013 From: frank at chagford.com (Frank Millman) Date: Mon, 20 May 2013 10:15:49 +0200 Subject: Question about ast.literal_eval In-Reply-To: References: , , Message-ID: On 20/05/2013 09:55, Carlos Nepomuceno wrote: > ---------------------------------------- >>> >>> Why don't you use eval()? >>> >> >> Because users can create their own columns, with their own constraints. >> Therefore the string is user-modifiable, so it cannot be trusted. > > I understand your motivation but I don't know what protection ast.literal_eval() is offering that eval() doesn't. > Quoting from the manual - "Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None." The operative word is 'safely'. I don't know the details, but it prevents the kinds of exploits that can be carried out by malicious code using eval(). I believe it is the same problem as SQL injection, which is solved by using parameterised queries. Frank From fabiosantosart at gmail.com Mon May 20 04:39:47 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 20 May 2013 09:39:47 +0100 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On 20 May 2013 09:19, "Frank Millman" wrote: > Quoting from the manual - > > "Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None." > > The operative word is 'safely'. I don't know the details, but it prevents the kinds of exploits that can be carried out by malicious code using eval(). Literals are only a subset of expressions. The documentation is a bit misleading, by stating it accepts a "python expression". This individual is rightfully confused. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Mon May 20 04:15:59 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 May 2013 08:15:59 GMT Subject: Question about ast.literal_eval References: Message-ID: <5199dbbf$0$6574$c3e8da3$5496439d@news.astraweb.com> On Mon, 20 May 2013 10:55:35 +0300, Carlos Nepomuceno wrote: > I understand your motivation but I don't know what protection > ast.literal_eval() is offering that eval() doesn't. eval will evaluate any legal Python expression: py> eval("__import__('os').system('echo Mwahaha! Now you are pwned!') or 42") Mwahaha! And now you are pwned! 42 ast.literal_eval() does exactly what the name says: it will evaluate any legal Python LITERAL, including ints, floats, lists, dicts and strings, but not arbitrary expressions. py> ast.literal_eval('123') 123 py> ast.literal_eval('[123, None, "spam"]') [123, None, 'spam'] -- Steven From rosuav at gmail.com Mon May 20 03:38:18 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 20 May 2013 17:38:18 +1000 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On Mon, May 20, 2013 at 5:05 PM, Frank Millman wrote: > Hi all > > I am trying to emulate a SQL check constraint in Python. Quoting from the > PostgreSQL docs, "A check constraint is the most generic constraint type. It > allows you to specify that the value in a certain column must satisfy a > Boolean (truth-value) expression." > > The problem is that I want to store the constraint as a string, and I was > hoping to use ast.literal_eval to evaluate it, but it does not work. > >>>> x = 'abc' >>>> x in ('abc', xyz') > True >>>> b = "x in ('abc', 'xyz')" >>>> eval(b) > True >>>> from ast import literal_eval >>>> literal_eval(b) > ValueError: malformed node or string: <_ast.Compare object at ...> > > Is there a safe way to do what I want? I am using python 3.3. An SQL constraint has a whole lot that Python can't do conveniently, and vice versa, so I think you do yourself a disservice by trying to tie yourself to that. The problem here is that 'in' is an operator, so this is not a literal. One possible solution would be to separate out your operator (and have just a handful permitted), and then use ast.literal_eval for just the second operand - something like this: import ast import operator ops = { 'in':lambda x,y: x in y, # operator.contains has the args backwards '==':operator.eq, # or use '=' for more SQL-like syntax '<':operator.lt, '>':operator.gt, } op, value = 'in', "('abc', 'xyz')" x = 'abc' if ops[op](x,ast.literal_eval(value)): print("Constraint passed") else: print("Ignore this one") ChrisA From matt.newville at gmail.com Mon May 20 22:39:59 2013 From: matt.newville at gmail.com (matt.newville at gmail.com) Date: Mon, 20 May 2013 19:39:59 -0700 (PDT) Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On Monday, May 20, 2013 2:05:48 AM UTC-5, Frank Millman wrote: > Hi all > > > > I am trying to emulate a SQL check constraint in Python. Quoting from > > the PostgreSQL docs, "A check constraint is the most generic constraint > > type. It allows you to specify that the value in a certain column must > > satisfy a Boolean (truth-value) expression." > > > > The problem is that I want to store the constraint as a string, and I > > was hoping to use ast.literal_eval to evaluate it, but it does not work. > > > > >>> x = 'abc' > > >>> x in ('abc', xyz') > > True > > >>> b = "x in ('abc', 'xyz')" > > >>> eval(b) > > True > > >>> from ast import literal_eval > > >>> literal_eval(b) > > ValueError: malformed node or string: <_ast.Compare object at ...> > > > > Is there a safe way to do what I want? I am using python 3.3. > > > > Thanks > > > > Frank Millman You might find the asteval module (https://pypi.python.org/pypi/asteval) useful. It provides a relatively safe "eval", for example: >>> import asteval >>> a = asteval.Interpreter() >>> a.eval('x = "abc"') >>> a.eval('x in ("abc", "xyz")') True >>> a.eval('import os') NotImplementedError import os 'Import' not supported >>> a.eval('__import__("os")') NameError __import__("os") name '__import__' is not defined This works by maintaining an internal namespace (a flat dictionary), and walking the AST generated for the expression. It supports most Python syntax, including if, for, while, and try/except blocks, and function definitions, and with the notable exceptions of eval, exec, class, lambda, yield, and import. This requires Python2.6 and higher, and does work with Python3.3. Of course, it is not guaranteed to be completely safe, but it does disallow imports, which seems like the biggest vulnerability concern listed here. Currently, there is no explicit protection against long-running calculations for denial of service attacks. If you're exposing an SQL database to user-generated code, that may be worth considering. Cheers, --Matt Newville From frank at chagford.com Tue May 21 01:54:28 2013 From: frank at chagford.com (Frank Millman) Date: Tue, 21 May 2013 07:54:28 +0200 Subject: Question about ast.literal_eval In-Reply-To: References: Message-ID: On 21/05/2013 04:39, matt.newville at gmail.com wrote: > > You might find the asteval module (https://pypi.python.org/pypi/asteval) useful. It provides a relatively safe "eval", for example: > > >>> import asteval > >>> a = asteval.Interpreter() > >>> a.eval('x = "abc"') > >>> a.eval('x in ("abc", "xyz")') > True > >>> a.eval('import os') > NotImplementedError > import os > 'Import' not supported > >>> a.eval('__import__("os")') > NameError > __import__("os") > name '__import__' is not defined > > This works by maintaining an internal namespace (a flat dictionary), and walking the AST generated for the expression. It supports most Python syntax, > including if, for, while, and try/except blocks, and function definitions, and with the notable exceptions of eval, exec, class, lambda, yield, and import. This requires Python2.6 and higher, and does work with Python3.3. > > Of course, it is not guaranteed to be completely safe, but it does disallow imports, which seems like the biggest vulnerability concern listed here. Currently, there is no explicit protection against long-running calculations for denial of service attacks. If you're exposing an SQL database to user-generated code, that may be worth considering. Thanks for this, Matt. I will definitely look into it. Frank From sabri.pllana at gmail.com Mon May 20 03:08:55 2013 From: sabri.pllana at gmail.com (SP) Date: Mon, 20 May 2013 00:08:55 -0700 (PDT) Subject: CFP: MuCoCoS Workshop at PACT-2013 (Edinburgh, Scotland, UK) Message-ID: <7ea671fa-f3fe-4c59-ab5f-47f660681195@googlegroups.com> CALL FOR PAPERS: 6th International Workshop on Multi/many-Core Computing Systems (MuCoCoS-2013) September 7, 2013, Edinburgh, Scotland, UK in conjunction with the 22nd Int. Conference on Parallel Architectures and Compilation Techniques (PACT-2013) http://www.ida.liu.se/conferences/mucocos2013 AIMS AND SCOPE The pervasiveness of homogeneous and heterogeneous multi-core and many-core processors, in a large spectrum of systems from embedded and general-purpose to high-end computing systems, poses major challenges to software industry. In general, there is no guarantee that software developed for a particular architecture will be executable (that is functional) on another architecture. Furthermore, ensuring that the software preserves some aspects of performance behavior (such as temporal or energy efficiency) across different such architectures is an open research issue. Therefore, this workshop focuses on language level, system software and architectural solutions for performance portability across different architectures and for automated performance tuning. The topics of the MuCoCoS workshop include but are not limited to: * Performance measurement, modeling, analysis and tuning * Portable programming models, languages and compilation techniques * Run-time systems and hardware support mechanisms for auto-tuning * Tunable algorithms and data structures * Case studies highlighting performance portability and tuning. As the sixth workshop in the series MuCoCoS 2008 (Barcelona, Spain), MuCoCoS 2009 (Fukuoka, Japan), MuCoCoS 2010 (Krakow, Poland), MuCoCoS 2011 (Seoul, Korea), and MuCoCoS 2012 (Salt Lake City, USA), MuCoCoS 2013 will be held in Edinburgh, UK, in conjunction with the 22nd International Conference on Parallel Architectures and Compilation Techniques (PACT 2013). SUBMISSION GUIDELINES The papers should be prepared using the IEEE format (two-column, 10pt, LaTeX users please use style IEEEtran.cls), and no longer than 10 pages. Submitted papers will be carefully evaluated based on originality, significance to workshop topics, technical soundness, and presentation quality. Please submit your paper (as PDF, viewable by Adobe Reader v5.0 or higher, with all fonts embedded please) electronically using the online submission system https://www.easychair.org/conferences/?conf=mucocos2013 Submission of the paper implies that, should the paper be accepted, at least one of the authors will register and present the paper at the workshop. Accepted papers will be published in electronic form in IEEE Xplore (confirmation pending). They will also be included in the PACT USB proceedings. We also plan to invite authors of the best MuCoCoS papers after the workshop to submit their extended workshop papers to a special issue of Computing Journal (Springer) (confirmation pending). IMPORTANT DATES Submission: May 27, 2013 (Firm Deadline) Notification: June 23, 2013 Camera-ready: July 7, 2013 Workshop: September 7, 2013 WORKSHOP ORGANIZATION Christoph Kessler, Link?ping University, Sweden, program chair Sabri Pllana, Linnaeus University, Sweden, co-chair PROGRAM COMMITTEE Marco Aldinucci, Univ. of Torino, Italy Beverly Bachmayer, Intel, Germany David Bader, Georgia Tech, USA Jacob Barhen, Oak Ridge National Lab, USA Siegfried Benkner, Univ. of Vienna, Austria Franz Franchetti, Carnegie Mellon University, USA Grigori Fursin, INRIA, France J?rg Keller, FernUniv. Hagen, Germany Lasse Natvig, NTNU Trondheim, Norway Mitsuhisa Sato, Univ. of Tsukuba, Japan Samuel Thibault, INRIA / Univ. of Bordeaux, France Philippas Tsigas, Chalmers University, Sweden Jakub Yaghob, Charles University Prague, Czech Republic From andrea.crotti.0 at gmail.com Mon May 20 14:00:30 2013 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Mon, 20 May 2013 20:00:30 +0200 Subject: pip and different branches? Message-ID: We use github and we work on many different branches at the same time. The problem is that we have >5 repos now, and for each repo we might have the same branches on all of them. Now we use pip and install requirements such as: git+ssh://git at github.com/repo.git at dev Now the problem is that the requirements file are also under revision control, and constantly we end up in the situation that when we merge branches the branch settings get messed up, because we forget to change them. I was looking for a solution for this that would allow me to: - use the branch of the "main" repo for all the dependencies - fallback on master if that branch doesn't exist I thought about a few options: 1. create a wrapper for PIP that manipulates the requirement file, that now would become templates. In this way I would have to know however if a branch exist or not, and I didn't find a way to do that without cloning the repo. 2. modify PIP to not fail when checking out a non existing branch, so that if it's not found it falls back on master automatically. 3. use some git magic hooks but I'm not sure what exactly 4. stop using virtualenv + pip and use something smarter that handles this. Any suggestions? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Tue May 21 01:17:36 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 21 May 2013 08:17:36 +0300 Subject: PEP 378: Format Specifier for Thousands Separator Message-ID: Is there a way to format integers with thousands separator (digit grouping) like the format specifier of str.format()? I'm currently using the following: >>> sys.stdout.write('Number = %s\n' % '{:,.0f}'.format(x)) Number = 12,345 'x' is unsigned integer so it's like using a sledgehammer to crack a nut! I'd like to have something like: sys.stdout.write('Number = %,u\n' % x) Is that possible? How can I do it if not already available? From nad at acm.org Tue May 21 01:44:08 2013 From: nad at acm.org (Ned Deily) Date: Mon, 20 May 2013 22:44:08 -0700 Subject: PEP 378: Format Specifier for Thousands Separator References: Message-ID: In article , Carlos Nepomuceno wrote: > Is there a way to format integers with thousands separator (digit grouping) > like the format specifier of str.format()?> > I'm currently using the following:> > >>> sys.stdout.write('Number = %s\n' % '{:,.0f}'.format(x)) > Number = 12,345> > 'x' is unsigned integer so it's like using a sledgehammer to crack a nut!> > I'd like to have something like: > sys.stdout.write('Number = %,u\n' % x) > Is that possible? How can I do it if not already available? For Python 3.2+ or 2.7, why not just: >>> print('Number = {:,}'.format(x)) Number = 12,345 -- Ned Deily, nad at acm.org From alyssonbruno at gmail.com Tue May 21 08:03:13 2013 From: alyssonbruno at gmail.com (Alysson Bruno) Date: Tue, 21 May 2013 09:03:13 -0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: Message-ID: This work in 3.1+: $ python3 Python 3.1.3 (r313:86834, Nov 28 2010, 11:28:10) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> one_number = 1234567 >>> print('number={:,}'.format(one_number)) number=1,234,567 >>> paz e amor (love and peace), Alysson Bruno =============================================== Palmas(TO) Brasil Blog: http://abruno.com Twitter: http://twitter.com/alyssonbruno Facebook: http://www.facebook.com/ProfessorAlyssonBruno ================================================================= *Meu alterego Escritor:* Leia alguns contos que escrevo, n?o esque?a de me dar sua opini?o: http://goo.gl/Wjn4p ================================================================= On Tue, May 21, 2013 at 2:44 AM, Ned Deily wrote: > In article , > Carlos Nepomuceno wrote: > > Is there a way to format integers with thousands separator (digit > grouping) > > like the format specifier of str.format()?> > > I'm currently using the following:> > > >>> sys.stdout.write('Number = %s\n' % '{:,.0f}'.format(x)) > > Number = 12,345> > > 'x' is unsigned integer so it's like using a sledgehammer to crack a > nut!> > > I'd like to have something like: > > sys.stdout.write('Number = %,u\n' % x) > > Is that possible? How can I do it if not already available? > > For Python 3.2+ or 2.7, why not just: > > >>> print('Number = {:,}'.format(x)) > Number = 12,345 > > -- > Ned Deily, > nad at acm.org > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Tue May 21 14:49:28 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 21 May 2013 21:49:28 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , Message-ID: ________________________________ > From: alyssonbruno at gmail.com > Date: Tue, 21 May 2013 09:03:13 -0300 > Subject: Re: PEP 378: Format Specifier for Thousands Separator > To: python-list at python.org > > This work in 3.1+: > > $ python3 > Python 3.1.3 (r313:86834, Nov 28 2010, 11:28:10) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> one_number = 1234567 > >>> print('number={:,}'.format(one_number)) > number=1,234,567 > >>> > Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. I've looked into the source code of CPython 2.7.5 and I've found no evidence of the thousands separator been implemented on formatint() in "Objects/unicodeobject.c". I also didn't find the _PyString_FormatLong() used in formatlong(). Where is _PyString_FormatLong() located? So, the question is: Where would I change the CPython 2.7.5 source code to enable '%' (BINARY_MODULO) to format using the thousands separator like str.format() does, such as: >>>sys.stderr.write('%,d\n' % 1234567) 1,234,567 From kwpolska at gmail.com Tue May 21 15:06:11 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Tue, 21 May 2013 21:06:11 +0200 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: Message-ID: On Tue, May 21, 2013 at 8:49 PM, Carlos Nepomuceno wrote: > Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. There is no real reason to do this. `str.format()` is the new shiny thing you should be using all the time. Also, '%' is BINARY_MODULO (where did you even get that name from?) if and only if you have two numbers, and it performs the modulo division (eg. 27 % 5 = 2) > So, the question is: Where would I change the CPython 2.7.5 source code to enable '%' (BINARY_MODULO) to format using the thousands separator like str.format() does, such as: > >>>>sys.stderr.write('%,d\n' % 1234567) > 1,234,567 This will make your code unportable and useless, depending on one patch you made. Please don?t do that. Instead, > >>> sys.stdout.write('Number = %s\n' % '{:,.0f}'.format(x)) > Number = 12,345 > > 'x' is unsigned integer so it's like using a sledgehammer to crack a nut! In Python? Tough luck, every int is signed. And it isn?t just a sledgehammer, it?s something worse. Just do that: >>> sys.stdout.write('Number = {:,.0f}\n'.format(x)) Much more peaceful. You can also do a print, like everyone sane would. Where did you learn Python from? ?Python Worst Practice for Dummies?? -- Kwpolska | GPG KEY: 5EAAEA16 stop html mail | always bottom-post http://asciiribbon.org | http://caliburn.nl/topposting.html From carlosnepomuceno at outlook.com Tue May 21 15:39:35 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 21 May 2013 22:39:35 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , Message-ID: ---------------------------------------- > From: kwpolska at gmail.com > Date: Tue, 21 May 2013 21:06:11 +0200 > Subject: Re: PEP 378: Format Specifier for Thousands Separator > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > > On Tue, May 21, 2013 at 8:49 PM, Carlos Nepomuceno > wrote: >> Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. > > There is no real reason to do this. `str.format()` is the new shiny > thing you should be using all the time. Also, '%' is BINARY_MODULO > (where did you even get that name from?) if and only if you have two > numbers, and it performs the modulo division (eg. 27 % 5 = 2) I did: >>> def fmt(s): ...???? return '%s' % s ... >>> import dis >>> dis.dis(fmt) ? 2?????????? 0 LOAD_CONST?????????????? 1 ('%s') ????????????? 3 LOAD_FAST??????????????? 0 (s) ????????????? 6 BINARY_MODULO ????????????? 7 RETURN_VALUE >>> Then I've looked for 'BINARY_MODULO' in "Python/ceval.c" and found: ??????? case BINARY_MODULO: ??????????? w = POP(); ??????????? v = TOP(); ??????????? if (PyString_CheckExact(v)) ??????????????? x = PyString_Format(v, w); ??????????? else ??????????????? x = PyNumber_Remainder(v, w); Then I've looked for 'PyString_Format' and found it in "Objects/stringobject.c" Analysing the code of "stringobject.c" I've found formatint() and formatlong(). I'm not using str.format() because it's slower than '%' and because I love '%'. str.format() looks like Java shit to me! >> So, the question is: Where would I change the CPython 2.7.5 source code to enable '%' (BINARY_MODULO) to format using the thousands separator like str.format() does, such as: >> >>>>>sys.stderr.write('%,d\n' % 1234567) >> 1,234,567 > > This will make your code unportable and useless, depending on one > patch you made. Please don?t do that. Instead, I'm just learning how to improve things! ;) >>>>> sys.stdout.write('Number = %s\n' % '{:,.0f}'.format(x)) >> Number = 12,345 >> >> 'x' is unsigned integer so it's like using a sledgehammer to crack a nut! > > In Python? Tough luck, every int is signed. And it isn?t just a > sledgehammer, it?s something worse. Just do that: > >>>> sys.stdout.write('Number = {:,.0f}\n'.format(x)) > > Much more peaceful. Indeed! I just cut and pasted my code to create an example for the message. The actual format operation isn't done at the sys.stdout.write(). > You can also do a print, like everyone sane would. Where did you > learn Python from? ?Python Worst Practice for Dummies?? lol I'm learning from my big mistakes up to now and a ton of online tutorials, besides "Dive into Python" (2004)[1], "Python Programming" (2012)[2] and "Programming Python, 3rd Ed" (2006) [print] print isn't thread safe. That's why I've chosen sys.stdout.write() -- it's faster and thread safe by default. I don't need any fancy formating, just need to send the string to screen. [1] http://www.diveintopython.net/ [1] http://en.wikibooks.org/wiki/Python_Programming > -- > Kwpolska | GPG KEY: 5EAAEA16 > stop html mail | always bottom-post > http://asciiribbon.org | http://caliburn.nl/topposting.html From carlosnepomuceno at outlook.com Tue May 21 15:41:51 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 21 May 2013 22:41:51 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , , , , Message-ID: > Analysing the code of "stringobject.c" I've found formatint() and formatlong(). I mean _PyString_FormatLong() From ethan at stoneleaf.us Tue May 21 15:41:36 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 21 May 2013 12:41:36 -0700 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: Message-ID: <519BCDF0.9050802@stoneleaf.us> On 05/21/2013 12:06 PM, Chris ?Kwpolska? Warrick wrote: > On Tue, May 21, 2013 at 8:49 PM, Carlos Nepomuceno wrote: >> >> Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. > > There is no real reason to do this. `str.format()` is the new shiny > thing you should be using all the time. .format() is useful, and has it's place, but % formatting is not going away. >> So, the question is: Where would I change the CPython 2.7.5 source code to enable '%' (BINARY_MODULO) to format using the thousands separator like str.format() does, such as: >> >>--> sys.stderr.write('%,d\n' % 1234567) >> 1,234,567 > > This will make your code unportable and useless, depending on one > patch you made. Please don?t do that. Agreed. Unless you're willing to have your programs either run differently, or not at all, on other systems this is the wrong way to fix it. > Where did you learn Python from? ?Python Worst Practice for Dummies?? Chris Warrick, Was that necessary? Useful? Helpful in any way? If you can't be civil, keep your posts to yourself. -- ~Ethan~ From skip at pobox.com Tue May 21 15:13:05 2013 From: skip at pobox.com (Skip Montanaro) Date: Tue, 21 May 2013 14:13:05 -0500 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: Message-ID: > Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. That's unlikely to change. If not deprecated already string interpolation using the modulo operator has lost favor to the string object's format method. You might be able to get by with a change of your LOCALE setting and/or a peek at the documentation for the locale module. Skip From breamoreboy at yahoo.co.uk Tue May 21 15:26:41 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 21 May 2013 20:26:41 +0100 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: Message-ID: On 21/05/2013 20:13, Skip Montanaro wrote: >> Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. > > That's unlikely to change. If not deprecated already string > interpolation using the modulo operator has lost favor to the string > object's format method. > Please stop perpetuating this myth, see http://mail.python.org/pipermail/python-dev/2012-February/116789.html and http://bugs.python.org/issue14123 -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From bahamutzero8825 at gmail.com Tue May 21 15:53:54 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 21 May 2013 14:53:54 -0500 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: Message-ID: <519BD0D2.5080203@gmail.com> On 2013.05.21 14:26, Mark Lawrence wrote: > On 21/05/2013 20:13, Skip Montanaro wrote: >>> Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. >> >> That's unlikely to change. If not deprecated already string >> interpolation using the modulo operator has lost favor to the string >> object's format method. >> > > Please stop perpetuating this myth, see > http://mail.python.org/pipermail/python-dev/2012-February/116789.html > and http://bugs.python.org/issue14123 > What myth? People should indeed be using .format(), but no one said % formatting was going away soon. Also, the suggested change to the docs wasn't made and the issue is closed. The current docs do not say that % formatting isn't going to be deprecated, but it does mention its caveats and suggests .format(). If you are trying to say that % formatting will never ever go away, then you are wrong. It is highly unlikely to go away in a 3.x release, but /may/ get phased out in Python 4.0. -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From carlosnepomuceno at outlook.com Tue May 21 16:22:24 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 21 May 2013 23:22:24 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519BD0D2.5080203@gmail.com> References: , , , , , , <519BD0D2.5080203@gmail.com> Message-ID: ---------------------------------------- > Date: Tue, 21 May 2013 14:53:54 -0500 > From: bahamutzero8825 at gmail.com > To: python-list at python.org [...] >> > What myth? People should indeed be using .format(), but no one said % formatting was going away soon. Also, the suggested change to the docs > wasn't made and the issue is closed. The current docs do not say that % formatting isn't going to be deprecated, but it does mention its > caveats and suggests .format(). If you are trying to say that % formatting will never ever go away, then you are wrong. It is highly > unlikely to go away in a 3.x release, but /may/ get phased out in Python 4.0. I vote for keeping str.__mod__()! Anyway, is it possible to overload str.__mod__() without deriving a class? I mean to have something like: old_mod = str.__mod__ def new_mod(x): ??? global old_mod ??? try: ? ?? ?? old_mod(x) ??? except ValueError, ex: ??????? #catch ValueError: unsupported format character ',' (0x2c) at index 1 ? ? ? ? #process new '%,d' format here ??????? return '{:,}'.format(x)? #just to illustrate the behaviour. it would have it's own faster code str.__mod__ = new_mod? #this raises TypeError: can't set attributes of built-in/extension type 'str' sys.stdout.write('num=%,d\n' % 1234567) > -- > CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Tue May 21 22:42:56 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 May 2013 02:42:56 GMT Subject: PEP 378: Format Specifier for Thousands Separator References: <519BD0D2.5080203@gmail.com> Message-ID: <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com> On Tue, 21 May 2013 23:22:24 +0300, Carlos Nepomuceno wrote: > Anyway, is it possible to overload str.__mod__() without deriving a > class? I mean to have something like: No, not in Python. If you want to monkey-patch built-in classes on the fly, with all the troubles that causes, use Ruby. -- Steven From carlosnepomuceno at outlook.com Tue May 21 22:56:53 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 22 May 2013 05:56:53 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com> References: , , , , , <519BD0D2.5080203@gmail.com>, , <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: PEP 378: Format Specifier for Thousands Separator > Date: Wed, 22 May 2013 02:42:56 +0000 > To: python-list at python.org > > On Tue, 21 May 2013 23:22:24 +0300, Carlos Nepomuceno wrote: > >> Anyway, is it possible to overload str.__mod__() without deriving a >> class? I mean to have something like: > > No, not in Python. If you want to monkey-patch built-in classes on the > fly, with all the troubles that causes, use Ruby. > So, the only alternative to have "'%,d' % x" rendering the thousands separator output would a C source code modification? > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Tue May 21 23:08:54 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 May 2013 03:08:54 GMT Subject: PEP 378: Format Specifier for Thousands Separator References: <519BD0D2.5080203@gmail.com> <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <519c36c6$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 22 May 2013 05:56:53 +0300, Carlos Nepomuceno wrote: > ---------------------------------------- >> From: steve+comp.lang.python at pearwood.info Subject: Re: PEP 378: Format >> Specifier for Thousands Separator Date: Wed, 22 May 2013 02:42:56 +0000 >> To: python-list at python.org >> >> On Tue, 21 May 2013 23:22:24 +0300, Carlos Nepomuceno wrote: >> >>> Anyway, is it possible to overload str.__mod__() without deriving a >>> class? I mean to have something like: >> >> No, not in Python. If you want to monkey-patch built-in classes on the >> fly, with all the troubles that causes, use Ruby. >> >> > So, the only alternative to have "'%,d' % x" rendering the thousands > separator output would a C source code modification? That's one alternative. But the language you would be then running will no longer be Python. Another alternative would be to write a pre-processor that parses your Python source code, extracts any reference to the above, and replaces it with a call to the appropriate format call. But not only is that a lot of work for very little gain, but it's also more or less impossible to do in full generality. And again, what you are running will be something different than Python, it will be Python plus a pre-processor. Don't fight the language. You will lose. -- Steven From carlosnepomuceno at outlook.com Tue May 21 23:38:45 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 22 May 2013 06:38:45 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519c36c6$0$6599$c3e8da3$5496439d@news.astraweb.com> References: , , , , , <519BD0D2.5080203@gmail.com>, , <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <519c36c6$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: PEP 378: Format Specifier for Thousands Separator > Date: Wed, 22 May 2013 03:08:54 +0000 > To: python-list at python.org [...] >> So, the only alternative to have "'%,d' % x" rendering the thousands >> separator output would a C source code modification? > > That's one alternative. But the language you would be then running will > no longer be Python. > > Another alternative would be to write a pre-processor that parses your > Python source code, extracts any reference to the above, and replaces it > with a call to the appropriate format call. But not only is that a lot of > work for very little gain, but it's also more or less impossible to do in > full generality. And again, what you are running will be something > different than Python, it will be Python plus a pre-processor. > > > Don't fight the language. You will lose. Not fighting the language. In fact it's not even a language issue. All I need is a standard library[1] improvement: "%,d"! That's all! Just to put in perspective the performance difference of str.__mod__() and str.format(): C:\Python27>python -m timeit -cv -n10000000 "'%d'%12345" raw times: 0.386 0.38 0.373 10000000 loops, best of 3: 0.0373 usec per loop C:\Python27>python -m timeit -cv -n10000000 "'{:d}'.format(12345)" raw times: 7.91 7.89 7.98 10000000 loops, best of 3: 0.789 usec per loop C:\Python27>python -m timeit -cv -n10000000 "'{:,d}'.format(12345)" raw times: 8.7 8.67 8.78 10000000 loops, best of 3: 0.867 usec per loop That shows str.format() is 20 times slower than str.__mod__() for a simple decimal integer literal formatting. And it's additionally 10% slower if the thousands separator format specifier (',') is used. [1] I think that translates to Python source code in 'Objects/stringobject.c' and maybe 'Objects/unicodeobject.c' > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From ned at nedbatchelder.com Wed May 22 07:25:13 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Wed, 22 May 2013 07:25:13 -0400 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , , , , <519BD0D2.5080203@gmail.com>, , <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <519c36c6$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <519CAB19.7080203@nedbatchelder.com> On 5/21/2013 11:38 PM, Carlos Nepomuceno wrote: >> From:steve+comp.lang.python at pearwood.info >> >Subject: Re: PEP 378: Format Specifier for Thousands Separator >> >Date: Wed, 22 May 2013 03:08:54 +0000 >> >To:python-list at python.org > [...] >>> >>So, the only alternative to have "'%,d' % x" rendering the thousands >>> >>separator output would a C source code modification? >> > >> >That's one alternative. But the language you would be then running will >> >no longer be Python. >> > >> >Another alternative would be to write a pre-processor that parses your >> >Python source code, extracts any reference to the above, and replaces it >> >with a call to the appropriate format call. But not only is that a lot of >> >work for very little gain, but it's also more or less impossible to do in >> >full generality. And again, what you are running will be something >> >different than Python, it will be Python plus a pre-processor. >> > >> > >> >Don't fight the language. You will lose. > Not fighting the language. In fact it's not even a language issue. > All I need is a standard library[1] improvement: "%,d"! That's all! You have to keep in mind that 2.7 is not getting any new features, no matter how small they seem. If you create a patch that implements the comma flag in %-formatting, it *might* go into 3.x, but it will not go into 2.7. --Ned. From carlosnepomuceno at outlook.com Wed May 22 07:52:15 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 22 May 2013 14:52:15 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519CAB19.7080203@nedbatchelder.com> References: , , , , , <519BD0D2.5080203@gmail.com>, , <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <519c36c6$0$6599$c3e8da3$5496439d@news.astraweb.com> , <519CAB19.7080203@nedbatchelder.com> Message-ID: ---------------------------------------- > Date: Wed, 22 May 2013 07:25:13 -0400 > From: ned at nedbatchelder.com [...] > You have to keep in mind that 2.7 is not getting any new features, no > matter how small they seem. If you create a patch that implements the > comma flag in %-formatting, it *might* go into 3.x, but it will not go > into 2.7. > > --Ned. No problem. I have just discovered i was measuring the wrong thing. My test case is been optimized at compile time by CPython that treats "'%d' % 12345" as a constant. My use case is different because I almost have no literals been used with % operator. So my gain isn't that great. In fact it's faster with str.format() than %, and it's even faster if I use the default format specifier. C:\Python27>python -m timeit -cv -n10000000 -s"v=12345" "'%d'%v" raw times: 10.5 10.7 10.7 10000000 loops, best of 3: 1.05 usec per loop C:\Python27>python -m timeit -cv -n10000000 -s"v=12345" "'{:d}'.format(v)" raw times: 8.11 8.09 8.02 10000000 loops, best of 3: 0.802 usec per loop C:\Users\Josue\Documents\Python>python -m timeit -cv -n10000000 -s"v=12345" "'{}'.format(v)" raw times: 5.3 5.5 5.62 10000000 loops, best of 3: 0.53 usec per loop Using variables (100% of cases) makes str.format() 50% faster than %. From dihedral88888 at gmail.com Wed May 22 03:14:13 2013 From: dihedral88888 at gmail.com (88888 Dihedral) Date: Wed, 22 May 2013 00:14:13 -0700 (PDT) Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: ,> , > , > , > , > <519BD0D2.5080203@gmail.com>,> , > <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com>, > , > <519c36c6$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <087e4cab-92bd-4a14-aab4-a986e426302b@googlegroups.com> Carlos Nepomuceno? 2013?5?22????UTC+8??11?38?45???? > ---------------------------------------- > > From: steve+comp.lang.python at pearwood.info > > Subject: Re: PEP 378: Format Specifier for Thousands Separator > > Date: Wed, 22 May 2013 03:08:54 +0000 > > To: python-list at python.org > [...] > >> So, the only alternative to have "'%,d' % x" rendering the thousands > >> separator output would a C source code modification? > > > > That's one alternative. But the language you would be then running will > > no longer be Python. > > > > Another alternative would be to write a pre-processor that parses your > > Python source code, extracts any reference to the above, and replaces it > > with a call to the appropriate format call. But not only is that a lot of > > work for very little gain, but it's also more or less impossible to do in > > full generality. And again, what you are running will be something > > different than Python, it will be Python plus a pre-processor. > > > > > > Don't fight the language. You will lose. > > Not fighting the language. In fact it's not even a language issue. > All I need is a standard library[1] improvement: "%,d"! That's all! > > Just to put in perspective the performance difference of str.__mod__() and str.format(): > > C:\Python27>python -m timeit -cv -n10000000 "'%d'%12345" > raw times: 0.386 0.38 0.373 > 10000000 loops, best of 3: 0.0373 usec per loop > > C:\Python27>python -m timeit -cv -n10000000 "'{:d}'.format(12345)" > raw times: 7.91 7.89 7.98 > 10000000 loops, best of 3: 0.789 usec per loop > > C:\Python27>python -m timeit -cv -n10000000 "'{:,d}'.format(12345)" > raw times: 8.7 8.67 8.78 > 10000000 loops, best of 3: 0.867 usec per loop > > That shows str.format() is 20 times slower than str.__mod__() for a simple decimal integer literal formatting. > And it's additionally 10% slower if the thousands separator format specifier (',') is used. > > [1] I think that translates to Python source code in 'Objects/stringobject.c' and maybe 'Objects/unicodeobject.c' > > > > > > > -- > > Steven > > -- > > http://mail.python.org/mailman/listinfo/python-list The conversions of the 32 bit integers and 64 bit floats to the strings of the base 10 digits require an efficint div and mod normally in the low level. From carlosnepomuceno at outlook.com Tue May 21 16:02:10 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 21 May 2013 23:02:10 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , , , , Message-ID: ---------------------------------------- > To: python-list at python.org > From: breamoreboy at yahoo.co.uk > Subject: Re: PEP 378: Format Specifier for Thousands Separator > Date: Tue, 21 May 2013 20:26:41 +0100 > > On 21/05/2013 20:13, Skip Montanaro wrote: >>> Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. >> >> That's unlikely to change. If not deprecated already string >> interpolation using the modulo operator has lost favor to the string >> object's format method. >> I used to think str.__mod__() was going to be deprecated until Steven told me it was a myth! Think I got that idea from the own Python docs, but it's gone by now. Nevermind... The fact is I have no need for str.format(). It's slow and awkward. str.format() looks like COBOL/Java idiom to me. I love Python! '%' is much more cool!!! > Please stop perpetuating this myth, see > http://mail.python.org/pipermail/python-dev/2012-February/116789.html > and http://bugs.python.org/issue14123 > > -- > If you're using GoogleCrap? please read this > http://wiki.python.org/moin/GoogleGroupsPython. > > Mark Lawrence > > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Tue May 21 22:59:22 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 May 2013 02:59:22 GMT Subject: PEP 378: Format Specifier for Thousands Separator References: Message-ID: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> On Tue, 21 May 2013 14:53:54 -0500, Andrew Berg wrote: > On 2013.05.21 14:26, Mark Lawrence wrote: >> Please stop perpetuating this myth, see >> http://mail.python.org/pipermail/python-dev/2012-February/116789.html >> and http://bugs.python.org/issue14123 >> > What myth? The myth that % string formatting is deprecated. It is not deprecated. > People should indeed be using .format(), If it is useful to them, and not too slow, or indeed if they merely want to. And if not, then not. This is a good case where the original poster *should* use str.format, since it does exactly what he wants, and % does not. Python gives you many tools, and the wise man uses the right tool for the job. Sometimes that is % and sometimes it is str.format and sometimes it is string.Template. That str.format looks like Java is irrelevant. Method call syntax is common in Python, and is not original to Java. Besides, Python looks like many languages: some parts look like Java, some parts look like C, some parts remind me of functional languages like Lisp and Haskell, and some parts look like Algol or Pascal. > but no one said % formatting was going away soon. True, but only for the definition "no one = all the people who insist that % is deprecated, or soon to be deprecated". > Also, the suggested change to the docs > wasn't made and the issue is closed. The current docs do not say that % > formatting isn't going to be deprecated, but it does mention its caveats > and suggests .format(). If you are trying to say that % formatting will > never ever go away, then you are wrong. It is highly unlikely to go away > in a 3.x release, but /may/ get phased out in Python 4.0. What happens in Python 4000 is irrelevant. If somebody is trying to "future proof" their code for a version that *may never exist*, and if it does exist is likely to be six or eight years away from even starting the design phase, they are wasting their time. It is hard enough to track a moving target, it is impossible to track a target that isn't even a gleam in GvR's eye yet. -- Steven From bahamutzero8825 at gmail.com Tue May 21 23:38:01 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 21 May 2013 22:38:01 -0500 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <519C3D99.5040107@gmail.com> On 2013.05.21 21:59, Steven D'Aprano wrote: > On Tue, 21 May 2013 14:53:54 -0500, Andrew Berg wrote: > >> On 2013.05.21 14:26, Mark Lawrence wrote: > >>> Please stop perpetuating this myth, see >>> http://mail.python.org/pipermail/python-dev/2012-February/116789.html >>> and http://bugs.python.org/issue14123 >>> >> What myth? > > The myth that % string formatting is deprecated. It is not deprecated. Skip didn't say that it was deprecated. >> but no one said % formatting was going away soon. > > True, but only for the definition "no one = all the people who insist > that % is deprecated, or soon to be deprecated". Perhaps I missed something, but who is insisting this? > What happens in Python 4000 is irrelevant. If somebody is trying to > "future proof" their code for a version that *may never exist*, and if it > does exist is likely to be six or eight years away from even starting the > design phase, they are wasting their time. It is hard enough to track a > moving target, it is impossible to track a target that isn't even a gleam > in GvR's eye yet. I think you misunderstand. I'm not suggesting that format() be used simply because % formatting could be deprecated at some unknown time years from now; I was clarifying the status of % formatting. -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From skip at pobox.com Wed May 22 06:45:12 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 22 May 2013 05:45:12 -0500 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519C3D99.5040107@gmail.com> References: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> <519C3D99.5040107@gmail.com> Message-ID: >>>> Please stop perpetuating this myth, see >>>> http://mail.python.org/pipermail/python-dev/2012-February/116789.html >>>> and http://bugs.python.org/issue14123 >>>> >>> What myth? >> >> The myth that % string formatting is deprecated. It is not deprecated. > Skip didn't say that it was deprecated. I didn't mean to create a tempest in a teapot. I was away from comp.lang.python, python-bugs, and python-dev for a few years. In particular, I didn't ever see the aforementioned thread from Feb 2012. Had I known of that thread I would have worded the sentence which shall not be repeated differently. My apologies... Skip From steve+comp.lang.python at pearwood.info Wed May 22 10:58:08 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 May 2013 14:58:08 GMT Subject: PEP 378: Format Specifier for Thousands Separator References: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> <519C3D99.5040107@gmail.com> Message-ID: <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 22 May 2013 05:45:12 -0500, Skip Montanaro wrote: > I didn't mean to create a tempest in a teapot. I was away from > comp.lang.python, python-bugs, and python-dev for a few years. In > particular, I didn't ever see the aforementioned thread from Feb 2012. > Had I known of that thread I would have worded the sentence which > shall not be repeated differently. > > My apologies... No problem, it's not about you specifically, it's just that some of us fans of % formatting can be a tad sensitive about it, especially since the idea that it has been deprecated (or soon will be deprecated, or one day will be deprecated, and therefore code using it is bad) is relatively widespread on the Internet. Glad to have you back here! -- Steven From ned at nedbatchelder.com Wed May 22 14:30:09 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Wed, 22 May 2013 14:30:09 -0400 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> <519C3D99.5040107@gmail.com> <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <519D0EB1.3090207@nedbatchelder.com> On 5/22/2013 10:58 AM, Steven D'Aprano wrote: > On Wed, 22 May 2013 05:45:12 -0500, Skip Montanaro wrote: > >> I didn't mean to create a tempest in a teapot. I was away from >> comp.lang.python, python-bugs, and python-dev for a few years. In >> particular, I didn't ever see the aforementioned thread from Feb 2012. >> Had I known of that thread I would have worded the sentence which >> shall not be repeated differently. >> >> My apologies... > No problem, it's not about you specifically, it's just that some of us > fans of % formatting can be a tad sensitive about it, especially since > the idea that it has been deprecated (or soon will be deprecated, or one > day will be deprecated, and therefore code using it is bad) is relatively > widespread on the Internet. Seems like maybe this should become a question in the Python FAQ. --Ned. > > Glad to have you back here! > > > From pruebauno at latinmail.com Wed May 22 16:26:23 2013 From: pruebauno at latinmail.com (nn) Date: Wed, 22 May 2013 13:26:23 -0700 (PDT) Subject: PEP 378: Format Specifier for Thousands Separator References: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> <519C3D99.5040107@gmail.com> <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com> On May 22, 2:30?pm, Ned Batchelder wrote: > On 5/22/2013 10:58 AM, Steven D'Aprano wrote: > > > On Wed, 22 May 2013 05:45:12 -0500, Skip Montanaro wrote: > > >> I didn't mean to create a tempest in a teapot. ?I was away from > >> comp.lang.python, python-bugs, and python-dev for a few years. ?In > >> particular, I didn't ever see the aforementioned thread from Feb 2012. > >> ? Had I known of that thread I would have worded the sentence which > >> shall not be repeated differently. > > >> My apologies... > > No problem, it's not about you specifically, it's just that some of us > > fans of % formatting can be a tad sensitive about it, especially since > > the idea that it has been deprecated (or soon will be deprecated, or one > > day will be deprecated, and therefore code using it is bad) is relatively > > widespread on the Internet. > > Seems like maybe this should become a question in the Python FAQ. > > --Ned. > > > > > > > > > > > Glad to have you back here! Maybe a cformat(formatstring, variables) function should be created in the string module so people who prefer that can use it. I don't mind the C formatting syntax but I don't like the fact that the % operator does something totally different when the first variable is an integer and the fact that it misbehaves if the second variable is a tuple. From carlosnepomuceno at outlook.com Wed May 22 18:31:20 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 01:31:20 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com> References: , , , , , , , <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com>, <519C3D99.5040107@gmail.com>, , <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com> Message-ID: ---------------------------------------- > Date: Wed, 22 May 2013 13:26:23 -0700 > Subject: Re: PEP 378: Format Specifier for Thousands Separator > From: pruebauno at latinmail.com > To: python-list at python.org [...] > > Maybe a cformat(formatstring, variables) function should be created > in the string module so people who prefer that can use it. I don't > mind the C formatting syntax but I don't like the fact that the % > operator does something totally different when the first variable is > an integer and the fact that it misbehaves if the second variable is a > tuple. > -- > http://mail.python.org/mailman/listinfo/python-list I still don't understand why % benefits from literals optimization ("'%d'%12345") while '{:d}'.format(12345) doesn't. What "totally different" you talking about? Please give me an example. From oscar.j.benjamin at gmail.com Wed May 22 20:30:53 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 23 May 2013 01:30:53 +0100 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> <519C3D99.5040107@gmail.com> <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com> <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com> Message-ID: On 22 May 2013 23:31, Carlos Nepomuceno wrote: > > I still don't understand why % benefits from literals optimization ("'%d'%12345") while '{:d}'.format(12345) doesn't. There's no reason why that optimisation can't happen in principle. However no one has written a patch for it. Why don't you look into what it would take to make it happen? Oscar From carlosnepomuceno at outlook.com Wed May 22 20:44:36 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 03:44:36 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , , , <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> <519C3D99.5040107@gmail.com>, <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com>, , Message-ID: ---------------------------------------- > From: oscar.j.benjamin at gmail.com > Date: Thu, 23 May 2013 01:30:53 +0100 > Subject: Re: PEP 378: Format Specifier for Thousands Separator > To: carlosnepomuceno at outlook.com > CC: pruebauno at latinmail.com; python-list at python.org > > On 22 May 2013 23:31, Carlos Nepomuceno wrote: >> >> I still don't understand why % benefits from literals optimization ("'%d'%12345") while '{:d}'.format(12345) doesn't. > > There's no reason why that optimisation can't happen in principle. > However no one has written a patch for it. Why don't you look into > what it would take to make it happen? > > > Oscar Maybe I'll look into that later, but I couldn't even find how the hell they made _Py_InsertThousandsGrouping() been called. That's what I got when analysing % formating: Thousands separator format specifier for str.__mod__() ====================================================== @Objects/stringobject.c: implements formatint() for '%' processing -Looking for code used in str.format() @Objects/stringlib/formatter.h: implements str.format() -It uses STRINGLIB_GROUPING() to do the job. @Objects/stringlib/stringdefs.h: #define STRINGLIB_GROUPING?????? _PyString_InsertThousandsGrouping @Objects/stringlib/unicodedefs.h: #define STRINGLIB_GROUPING?????? _PyUnicode_InsertThousandsGrouping @Objects/stringobject.c: #define _Py_InsertThousandsGrouping _PyString_InsertThousandsGrouping @Objects/stringobject.h: declares _PyString_InsertThousandsGrouping() @???: ??? _PyString_InsertThousandsGrouping ??? _Py_InsertThousandsGrouping @Objects/stringlib/localeutil.h: implements _Py_InsertThousandsGrouping() Let me explain what that means. I found no relating declarations/definitions that turn _PyString_InsertThousandsGrouping into _Py_InsertThousandsGrouping. So, I don't even know how that source code compiles without error. :/ really strange... Not to mention the lots of code inside header definition files! Weird!!!! From pruebauno at latinmail.com Thu May 23 09:44:05 2013 From: pruebauno at latinmail.com (nn) Date: Thu, 23 May 2013 06:44:05 -0700 (PDT) Subject: PEP 378: Format Specifier for Thousands Separator References: , , , , , , , <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com>, <519C3D99.5040107@gmail.com>, , <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com> Message-ID: On May 22, 6:31?pm, Carlos Nepomuceno wrote: > ---------------------------------------- > > > Date: Wed, 22 May 2013 13:26:23 -0700 > > Subject: Re: PEP 378: Format Specifier for Thousands Separator > > From: prueba... at latinmail.com > > To: python-l... at python.org > [...] > > > Maybe a cformat(formatstring, variables) function should be created > > in the string module so people who prefer that can use it. I don't > > mind the C formatting syntax but I don't like the fact that the % > > operator does something totally different when the first variable is > > an integer and the fact that it misbehaves if the second variable is a > > tuple. > > -- > >http://mail.python.org/mailman/listinfo/python-list > > I still don't understand why % benefits from literals optimization ("'%d'%12345") while '{:d}'.format(12345) doesn't. > > What "totally different" you talking about? Please give me an example. >>> def eggs(spam, ham): return spam % ham >>> def milk(beef, steak): return beef.format(steak) >>> a='%s' >>> c=9 >>> d=4 >>> e=[1,2] >>> f=(3,5) >>> d='{}' >>> eggs(a,4) '4' >>> eggs(c,4) 1 >>> eggs(a,e) '[1, 2]' >>> eggs(a,f) Traceback (most recent call last): File "", line 1, in eggs(a,f) File "", line 1, in eggs def eggs(spam, ham): return spam % ham TypeError: not all arguments converted during string formatting >>> '%s'%(5%3) '2' >>> milk(d,4) '4' >>> milk(c,4) Traceback (most recent call last): File "", line 1, in milk(c,4) File "", line 1, in milk def milk(beef, steak): return beef.format(steak) AttributeError: 'int' object has no attribute 'format' >>> milk(d,e) '[1, 2]' >>> milk(d,f) '(3, 5)' >>> '{}'.format(5%3) '2' From carlosnepomuceno at outlook.com Thu May 23 11:26:49 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 18:26:49 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , , , , , , , , , , , , , <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <519C3D99.5040107@gmail.com>, , , , <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, , , , <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com>, , Message-ID: ---------------------------------------- > Date: Thu, 23 May 2013 06:44:05 -0700 > Subject: Re: PEP 378: Format Specifier for Thousands Separator > From: pruebauno at latinmail.com > To: python-list at python.org [...] >>>> eggs(a,f) > Traceback (most recent call last): > File "", line 1, in > eggs(a,f) > File "", line 1, in eggs > def eggs(spam, ham): return spam % ham > TypeError: not all arguments converted during string formatting >>>> '%s'%(5%3) > '2' So % doesn't handle tuples! Why's that? Is it intentional (by design)? From davea at davea.name Thu May 23 14:42:47 2013 From: davea at davea.name (Dave Angel) Date: Thu, 23 May 2013 14:42:47 -0400 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , , , , , , , , , , , , , <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <519C3D99.5040107@gmail.com>, , , , <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, , , , <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com>, , Message-ID: <519E6327.8070204@davea.name> On 05/23/2013 11:26 AM, Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Thu, 23 May 2013 06:44:05 -0700 >> Subject: Re: PEP 378: Format Specifier for Thousands Separator >> From: pruebauno at latinmail.com >> To: python-list at python.org > [...] You left out the part where a and f are initialized: >>> a='%s' >>> f=(3,5) >>>>> eggs(a,f) >> Traceback (most recent call last): >> File "", line 1, in >> eggs(a,f) >> File "", line 1, in eggs >> def eggs(spam, ham): return spam % ham >> TypeError: not all arguments converted during string formatting >>>>> '%s'%(5%3) >> '2' > > So % doesn't handle tuples! Why's that? Is it intentional (by design)? > It's a conflict in the design. A tuple is used to supply multiple arguments to the % operator. So if you want to have a tuple as the first argument, you need to enclose it in another tuple. try the following: print a % (f,) The trouble is, it doesn't generalize very readily, so it's difficult to use in a function like eggs() -- DaveA From tjreedy at udel.edu Thu May 23 16:19:33 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Thu, 23 May 2013 16:19:33 -0400 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519E6327.8070204@davea.name> References: , , , , , , , , , , , , , , <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <519C3D99.5040107@gmail.com>, , , , <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, , , , <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com>, , <519E6327.8070204@davea.name> Message-ID: On 5/23/2013 2:42 PM, Dave Angel wrote: > On 05/23/2013 11:26 AM, Carlos Nepomuceno wrote: > >>>>>> eggs(a,f) >>> Traceback (most recent call last): >>> File "", line 1, in >>> eggs(a,f) >>> File "", line 1, in eggs >>> def eggs(spam, ham): return spam % ham >>> TypeError: not all arguments converted during string formatting >>>>>> '%s'%(5%3) >>> '2' >> >> So % doesn't handle tuples! Why's that? Is it intentional (by design)? >> > > It's a conflict in the design. A tuple is used to supply multiple > arguments to the % operator. So if you want to have a tuple as the > first argument, you need to enclose it in another tuple. The problem is that interpolating 1 to many items into a string is *not* a binary operation. The needed hack to pretend that it is, using a tuple to represent multiple items rather than just itself, was one of the reasons for making string formatting a honest function, where multiple items to be formatted are passed as multiple arguments. From carlosnepomuceno at outlook.com Thu May 23 18:20:26 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 01:20:26 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , ,,, , , , , , , , , , , , , ,,, ,,<519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com>, ,,<519C3D99.5040107@gmail.com>, ,,, ,,<519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, ,,, , , <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com>, , , , , , <519E6327.8070204@davea.name>, Message-ID: ---------------------------------------- > To: python-list at python.org > From: tjreedy at udel.edu [...] >> It's a conflict in the design. A tuple is used to supply multiple >> arguments to the % operator. So if you want to have a tuple as the >> first argument, you need to enclose it in another tuple. > > The problem is that interpolating 1 to many items into a string is *not* > a binary operation. The needed hack to pretend that it is, using a tuple > to represent multiple items rather than just itself, was one of the > reasons for making string formatting a honest function, where multiple > items to be formatted are passed as multiple arguments. Thanks so much guys! Now I understand why some people hate %-formatting! I don't think that's a problem because there's no format specifier for tuples in the %-formatting definition[1]. So it's up to the user to make the necessary conversion. I still love the %-formatting style! Can str.format() do the following? f = '%d %d %d' v = '1,2,3' print f % eval(v) If yes, how? [1] http://docs.python.org/2/library/stdtypes.html#string-formatting From malaclypse2 at gmail.com Thu May 23 21:17:54 2013 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 23 May 2013 21:17:54 -0400 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com> <519C3D99.5040107@gmail.com> <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com> <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com> <519E6327.8070204@davea.name> Message-ID: On Thu, May 23, 2013 at 6:20 PM, Carlos Nepomuceno < carlosnepomuceno at outlook.com> wrote: > Can str.format() do the following? > > f = '%d %d %d' > v = '1,2,3' > print f % eval(v) > ?Sure: Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 >>> f = "{} {} {}" >>> v = "1,2,3" >>> print(f.format(*eval(v))) 1 2 3 >>> The * unpacks the tuple returned from eval(), so that you get 3 separate parameters passed to format(), instead of the single tuple.? -- ? Jerry? -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Thu May 23 21:41:22 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 04:41:22 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , , , , , , , <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com>, <519C3D99.5040107@gmail.com>, , <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com>, , , , <519E6327.8070204@davea.name>, , , Message-ID: Thank you! Hail Eris!!! :) ________________________________ > Date: Thu, 23 May 2013 21:17:54 -0400 > Subject: Re: PEP 378: Format Specifier for Thousands Separator > From: malaclypse2 at gmail.com > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > > On Thu, May 23, 2013 at 6:20 PM, Carlos Nepomuceno > > > wrote: > Can str.format() do the following? > > f = '%d %d %d' > v = '1,2,3' > print f % eval(v) > > ?Sure: > > Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit > (Intel)] on win32 > >>> f = "{} {} {}" > >>> v = "1,2,3" > >>> print(f.format(*eval(v))) > 1 2 3 > >>> > > The * unpacks the tuple returned from eval(), so that you get 3 > separate parameters passed to format(), instead of the single tuple.? > > -- > ? > Jerry? > From pruebauno at latinmail.com Fri May 24 09:50:51 2013 From: pruebauno at latinmail.com (nn) Date: Fri, 24 May 2013 06:50:51 -0700 (PDT) Subject: PEP 378: Format Specifier for Thousands Separator References: , , , , , , <519c348a$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <519C3D99.5040107@gmail.com>, , , , <519cdcff$0$6599$c3e8da3$5496439d@news.astraweb.com>, , , , <87eaaf24-df6a-4f6b-b873-16ca96c12e24@b2g2000yqe.googlegroups.com>, , Message-ID: <3d927c74-a4ce-4d02-a107-443d059c32a3@s18g2000yqg.googlegroups.com> On May 23, 2:42?pm, Dave Angel wrote: > On 05/23/2013 11:26 AM, Carlos Nepomuceno wrote: > > > ---------------------------------------- > >> Date: Thu, 23 May 2013 06:44:05 -0700 > >> Subject: Re: PEP 378: Format Specifier for Thousands Separator > >> From: prueba... at latinmail.com > >> To: python-l... at python.org > > [...] > > You left out the part where a and f are initialized: > > ?>>> a='%s' > ?>>> f=(3,5) > > >>>>> eggs(a,f) > >> Traceback (most recent call last): > >> File "", line 1, in > >> eggs(a,f) > >> File "", line 1, in eggs > >> def eggs(spam, ham): return spam % ham > >> TypeError: not all arguments converted during string formatting > >>>>> '%s'%(5%3) > >> '2' > > > So % doesn't handle tuples! Why's that? Is it intentional (by design)? > > It's a conflict in the design. ?A tuple is used to supply multiple > arguments to the % operator. ?So if you want to have a tuple as the > first argument, you need to enclose it in another tuple. > > try the following: > > print a % (f,) > > The trouble is, it doesn't generalize very readily, so it's difficult to > use in a function like eggs() > > -- > DaveA It's all there, it's just that quoting ate it. Let's try this again: >>> def eggs(spam, ham): return spam % ham >>> def milk(beef, steak): return beef.format(steak) >>> a='%s' >>> c=9 >>> d=4 >>> e=[1,2] >>> f=(3,5) >>> d='{}' >>> eggs(a,4) '4' >>> eggs(c,4) 1 >>> eggs(a,e) '[1, 2]' >>> eggs(a,f) Traceback (most recent call last): File "", line 1, in eggs(a,f) File "", line 1, in eggs def eggs(spam, ham): return spam % ham TypeError: not all arguments converted during string formatting >>> '%s'%(5%3) '2' >>> milk(d,4) '4' >>> milk(c,4) Traceback (most recent call last): File "", line 1, in milk(c,4) File "", line 1, in milk def milk(beef, steak): return beef.format(steak) AttributeError: 'int' object has no attribute 'format' >>> milk(d,e) '[1, 2]' >>> milk(d,f) '(3, 5)' >>> '{}'.format(5%3) '2' The three issues represented: 1. Functions in which both values of the operator are parameters might not return a string but an integer instead. It is not always immediately obvious when reading such functions if modulus or formatting is intended. 2. Function doesn't handle tuple properly unless carefully written 3. Too much % noise because % is used for 3 things: the special placeholder character inside format strings, the format operator and the modulus operator. From dihedral88888 at gmail.com Thu May 23 22:29:14 2013 From: dihedral88888 at gmail.com (88888 Dihedral) Date: Thu, 23 May 2013 19:29:14 -0700 (PDT) Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: ,> , > Message-ID: Carlos Nepomuceno? 2013?5?22????UTC+8??2?49?28???? > ________________________________ > > From: alyssonbruno at gmail.com > > Date: Tue, 21 May 2013 09:03:13 -0300 > > Subject: Re: PEP 378: Format Specifier for Thousands Separator > > To: python-list at python.org > > > > This work in 3.1+: > > > > $ python3 > > Python 3.1.3 (r313:86834, Nov 28 2010, 11:28:10) > > [GCC 4.4.5] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> one_number = 1234567 > > >>> print('number={:,}'.format(one_number)) > > number=1,234,567 > > >>> > > > > Thank you, but let me rephrase it. I'm already using str.format() but I'd like to use '%' (BINARY_MODULO) operator instead. > > I've looked into the source code of CPython 2.7.5 and I've found no evidence of the thousands separator been implemented on formatint() in "Objects/unicodeobject.c". > > I also didn't find the _PyString_FormatLong() used in formatlong(). Where is _PyString_FormatLong() located? > > So, the question is: Where would I change the CPython 2.7.5 source code to enable '%' (BINARY_MODULO) to format using the thousands separator like str.format() does, such as: > > >>>sys.stderr.write('%,d\n' % 1234567) > 1,234,567 Could a separate instance like the I/O device of a subprocess to be easily available in Python? The next question would be whether the flow of several I/O data streams could be easily piped and manipulated in the high level programming designs without consuming too much resources. From carlosnepomuceno at outlook.com Fri May 24 11:25:45 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 18:25:45 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , >, , , > , , Message-ID: ---------------------------------------- > Date: Thu, 23 May 2013 19:29:14 -0700 > Subject: Re: PEP 378: Format Specifier for Thousands Separator > From: dihedral88888 at gmail.com [...] > Could a separate instance like the I/O device of a subprocess > to be easily available in Python? > > The next question would be whether the flow of several I/O data streams could be easily piped and manipulated in the high level > programming designs without consuming too much resources. I'm sorry but I don't understand your question. Do you mean returning the output of another process to the caller? Like: import subprocess output = subprocess.check_output(['ping', 'google.com']) From andipersti at gmail.com Fri May 24 13:28:29 2013 From: andipersti at gmail.com (Andreas Perstinger) Date: Fri, 24 May 2013 19:28:29 +0200 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: References: , >, , , > , , Message-ID: <519FA33D.5030209@gmail.com> On 24.05.2013 17:25, Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Thu, 23 May 2013 19:29:14 -0700 >> Subject: Re: PEP 378: Format Specifier for Thousands Separator >> From: dihedral88888 at gmail.com >> [some typical dihedral stuff] > > I'm sorry but I don't understand your question. Don't worry. I guess most people on this list don't understand it either. It looks like dihedral is a bot although nobody knows for sure. Search for some other posts from him/her/it in the archive and form your own opinion. IMHO you can simply ignore him/her/it. Bye, Andreas From carlosnepomuceno at outlook.com Fri May 24 17:07:41 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 00:07:41 +0300 Subject: PEP 378: Format Specifier for Thousands Separator In-Reply-To: <519FA33D.5030209@gmail.com> References: , >,,, ,,> , , , , , , <519FA33D.5030209@gmail.com> Message-ID: lol that reminds me of George! lol ;) ---------------------------------------- > Date: Fri, 24 May 2013 19:28:29 +0200 > From: andipersti at gmail.com > To: python-list at python.org > Subject: Re: PEP 378: Format Specifier for Thousands Separator > > On 24.05.2013 17:25, Carlos Nepomuceno wrote: >> ---------------------------------------- >>> Date: Thu, 23 May 2013 19:29:14 -0700 >>> Subject: Re: PEP 378: Format Specifier for Thousands Separator >>> From: dihedral88888 at gmail.com >>> [some typical dihedral stuff] >> >> I'm sorry but I don't understand your question. > > Don't worry. I guess most people on this list don't understand it either. > > It looks like dihedral is a bot although nobody knows for sure. Search > for some other posts from him/her/it in the archive and form your own > opinion. > > IMHO you can simply ignore him/her/it. > > Bye, Andreas > > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Tue May 21 04:26:14 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 May 2013 08:26:14 GMT Subject: sympy.nsimplify Message-ID: <519b2fa6$0$6574$c3e8da3$5496439d@news.astraweb.com> For maths nerds like me, this is too cool for words: http://www.johndcook.com/blog/2013/04/30/recognizing-numbers/ -- Steven From rosuav at gmail.com Tue May 21 05:08:05 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 19:08:05 +1000 Subject: sympy.nsimplify In-Reply-To: <519b2fa6$0$6574$c3e8da3$5496439d@news.astraweb.com> References: <519b2fa6$0$6574$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, May 21, 2013 at 6:26 PM, Steven D'Aprano wrote: > For maths nerds like me, this is too cool for words: > > http://www.johndcook.com/blog/2013/04/30/recognizing-numbers/ It is indeed, very cool. I think I need to conjure an excuse to use this someplace. ChrisA From skip at pobox.com Tue May 21 07:01:40 2013 From: skip at pobox.com (Skip Montanaro) Date: Tue, 21 May 2013 06:01:40 -0500 Subject: sympy.nsimplify In-Reply-To: References: <519b2fa6$0$6574$c3e8da3$5496439d@news.astraweb.com> Message-ID: Very cool indeed. In the comments was a link to an XKCD cartoon. Its tool tip mentioned "twin primes". Looked that up. Google pointed (of course) at Wikipedia. Read that. Backed up to the Google Search, and noticed there is a news item from 15 hours ago that an unknown mathematician at the University of New Hampshire has proven the twin primes conjecture: http://www.wired.com/wiredscience/2013/05/twin-primes/ This is nothing to do with the original post. It's just amazing to me how short the distance between one very interesting topic on the net and something almost unrelated can be. Skip From paul.nospam at rudin.co.uk Tue May 21 07:17:24 2013 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Tue, 21 May 2013 12:17:24 +0100 Subject: sympy.nsimplify References: <519b2fa6$0$6574$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87hahweg57.fsf@no-fixed-abode.cable.virginmedia.net> Skip Montanaro writes: > Very cool indeed. In the comments was a link to an XKCD cartoon. Its > tool tip mentioned "twin primes". Looked that up. Google pointed (of > course) at Wikipedia. Read that. Backed up to the Google Search, and > noticed there is a news item from 15 hours ago that an unknown > mathematician at the University of New Hampshire has proven the twin > primes conjecture: > http://www.wired.com/wiredscience/2013/05/twin-primes/ > > This is nothing to do with the original post. It's just amazing to me > how short the distance between one very interesting topic on the net > and something almost unrelated can be. AIUI the twin primes conjecture hasn't been proved. But a significant related fact - that there's an infinitude of primes no more that N apart where N <~ 70,000,000. That might not sound like a lot of progress - but the point is that the existence of some finite bound is very significant. From iman.memarpour at gmail.com Tue May 21 04:38:37 2013 From: iman.memarpour at gmail.com (iman.memarpour at gmail.com) Date: Tue, 21 May 2013 01:38:37 -0700 (PDT) Subject: please help Message-ID: <649f272e-022b-4a11-8431-2098aa299d7b@googlegroups.com> WAP in python to accept a list of words on STDIN and searches for a line containing all five vowels(a,e,i,o,u) From rosuav at gmail.com Tue May 21 05:05:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 21 May 2013 19:05:25 +1000 Subject: please help In-Reply-To: <649f272e-022b-4a11-8431-2098aa299d7b@googlegroups.com> References: <649f272e-022b-4a11-8431-2098aa299d7b@googlegroups.com> Message-ID: On Tue, May 21, 2013 at 6:38 PM, wrote: > WAP in python to accept a list of words on STDIN and searches for a line containing all five vowels(a,e,i,o,u) Homework. Have a shot at it yourself, post your code, show that you can put in some effort. Otherwise we won't see much reason to put in effort for you. ChrisA From breamoreboy at yahoo.co.uk Tue May 21 05:07:41 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 21 May 2013 10:07:41 +0100 Subject: please help In-Reply-To: <649f272e-022b-4a11-8431-2098aa299d7b@googlegroups.com> References: <649f272e-022b-4a11-8431-2098aa299d7b@googlegroups.com> Message-ID: On 21/05/2013 09:38, iman.memarpour at gmail.com wrote: > WAP in python to accept a list of words on STDIN and searches for a line containing all five vowels(a,e,i,o,u) > Sorry we don't do your homework for you. But your starter for 10 is to use raw_input on Python 2 or input on Python 3 to fetch data from stdin. I look forward to reviewing your code. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From maciej at opencsw.org Tue May 21 05:51:48 2013 From: maciej at opencsw.org (=?UTF-8?Q?Maciej_=28Matchek=29_Blizi=C5=84ski?=) Date: Tue, 21 May 2013 10:51:48 +0100 Subject: 64-bit Python for Solaris Message-ID: Hello python-list, I'm looking into creating a 32/64-bit Python (2.x and/or 3.x) package for Solaris. The specificity of that package is that I need to include both 32-bit and 64-bit binaries in it. The exact way in which the 32/64 support is done is described at [1]. There currently is a Python package that I maintain, which is 32-bit only[2]. I have made an attempt to build a 64-bit package, and my findings are that the ${prefix}/lib/pythonX.Y/_sysconfigdata.py file contains system-specific information. Note that it's not ${libdir}/pythonX.Y - that would have worked, because I'm specifying different ${libdir} directories when running the 32-bit and 64-bit builds. The Python installer specifically uses ${prefix}/lib/pythonX.Y. For the most part is fine, because most of files in there are not architecture-specific, and it would be quite good to share them among the 32-bit and 64-bit binaries at runtime. The problem is that some files differ. I've described it some more at [3]. Ideally, I'd make _sysconfigdata.py return/set different values depending on the Python runtime that reads it. Something like: if we're 64-bit: set values for the 64-bit platform else: set values for the 32-bit platform It's a similar approach to how we currently handle C header files. See the 'Development packages' section in [1] for more information. The problem is that it would involve somewhat intrusive patching of the Python source code, and in long term that means maintainability issues. Has this issue been seen before? Is there a better solution? Is there something that can be done upstream to accommodate this kind of packaging? Maciej [1] http://www.opencsw.org/manual/for-maintainers/32-bit-and-64-bit.html [2] http://www.opencsw.org/packages/python/ [3] http://lists.opencsw.org/pipermail/maintainers/2013-January/017583.html From maciej at opencsw.org Mon May 27 14:28:09 2013 From: maciej at opencsw.org (=?UTF-8?Q?Maciej_=28Matchek=29_Blizi=C5=84ski?=) Date: Mon, 27 May 2013 19:28:09 +0100 Subject: 64-bit Python for Solaris In-Reply-To: References: Message-ID: 2013/5/21 Maciej (Matchek) Blizi?ski : > the ${prefix}/lib/pythonX.Y/_sysconfigdata.py file contains > system-specific information ...and is installed in an architecture-independent directory by the Python installer. This looks broken to me. From kobewka at gmail.com Tue May 21 09:12:31 2013 From: kobewka at gmail.com (kobewka at gmail.com) Date: Tue, 21 May 2013 06:12:31 -0700 (PDT) Subject: Static Maps from Lat Long data in XLS file Message-ID: Hello, I'm new to Python, but I think it can solve my problem and I am looking for a someone to point me to tutorial or give me some tips here. I have an xls file that has about 1,000 latitude and longitude points. I want to do one of two things: 1) Save a static maps and street view image from the coordinates, or 2) create an html file with the map and street view image side by side. I need the urls to look like this: Map with a pin in the centre: http://maps.googleapis.com/maps/api/staticmap?center=43.65162,-79.40571&zoom=16&size=600x600&markers=color:blue%7Clabel:S%7C43.65162,-79.40571&sensor=false Image: http://maps.googleapis.com/maps/api/streetview?location=43.65162,%20-79.40571&size=600x600&sensor=false I am not sure if option 1 will work because the url doesn't actually lead to an image, but rather Google returns an image when that url is used. Any tips or pointers are much appreciated! From ken at bscientific.net Tue May 21 09:58:25 2013 From: ken at bscientific.net (Ken Bolton) Date: Tue, 21 May 2013 09:58:25 -0400 Subject: Static Maps from Lat Long data in XLS file In-Reply-To: References: Message-ID: On Tue, May 21, 2013 at 9:12 AM, wrote: > Hello, > > I'm new to Python, but I think it can solve my problem and I am looking > for a someone to point me to tutorial or give me some tips here. > Hi! I am a first-time poster to python-list, but I think I can help you. > I have an xls file that has about 1,000 latitude and longitude points. I > want to do one of two things: 1) Save a static maps and street view image > from the coordinates, or 2) create an html file with the map and street > view image side by side. If you save your xls file as a csv (comma-separated values), you can use python's built-in csv module, documented here - http://docs.python.org/2/library/csv.html, to read the file line by line. Store the values and substitute the strings into a new list of URLs. > I need the urls to look like this: > > Map with a pin in the centre: > > http://maps.googleapis.com/maps/api/staticmap?center=43.65162,-79.40571&zoom=16&size=600x600&markers=color:blue%7Clabel:S%7C43.65162,-79.40571&sensor=false > > Image: > > http://maps.googleapis.com/maps/api/streetview?location=43.65162,%20-79.40571&size=600x600&sensor=false I was able to use curl to grab the images you linked. I believe you can use urllib (or, better, requests - http://docs.python-requests.org/en/latest/) to get and save the images. hth. best, ken -------------- next part -------------- An HTML attachment was scrubbed... URL: From tundra at tundraware.com Tue May 21 11:27:42 2013 From: tundra at tundraware.com (Tim Daneliuk) Date: Tue, 21 May 2013 10:27:42 -0500 Subject: Static Maps from Lat Long data in XLS file In-Reply-To: References: Message-ID: On 05/21/2013 08:12 AM, kobewka at gmail.com wrote: > Hello, > > I'm new to Python, but I think it can solve my problem and I am looking for a someone to point me to tutorial or give me some tips here. > > I have an xls file that has about 1,000 latitude and longitude points. I want to do one of two things: 1) Save a static maps and street view image from the coordinates, or 2) create an html file with the map and street view image side by side. > > I need the urls to look like this: > > Map with a pin in the centre: > http://maps.googleapis.com/maps/api/staticmap?center=43.65162,-79.40571&zoom=16&size=600x600&markers=color:blue%7Clabel:S%7C43.65162,-79.40571&sensor=false > > Image: > http://maps.googleapis.com/maps/api/streetview?location=43.65162,%20-79.40571&size=600x600&sensor=false > > I am not sure if option 1 will work because the url doesn't actually lead to an image, but rather Google returns an image when that url is used. > > Any tips or pointers are much appreciated! > https://pypi.python.org/pypi/xlrd -- ----------------------------------------------------------------------- Tim Daneliuk From kobewka at gmail.com Wed May 22 07:21:59 2013 From: kobewka at gmail.com (kobewka at gmail.com) Date: Wed, 22 May 2013 04:21:59 -0700 (PDT) Subject: Static Maps from Lat Long data in XLS file In-Reply-To: References: Message-ID: <9dbdf222-8a2f-4edc-b9c9-7c1d7e8f4ddf@googlegroups.com> On Tuesday, May 21, 2013 11:27:42 AM UTC-4, Tim Daneliuk wrote: > On 05/21/2013 08:12 AM, @gmail.com wrote: > > > Hello, > > > > > > I'm new to Python, but I think it can solve my problem and I am looking for a someone to point me to tutorial or give me some tips here. > > > > > > I have an xls file that has about 1,000 latitude and longitude points. I want to do one of two things: 1) Save a static maps and street view image from the coordinates, or 2) create an html file with the map and street view image side by side. > > > > > > I need the urls to look like this: > > > > > > Map with a pin in the centre: > > > http://maps.googleapis.com/maps/api/staticmap?center=43.65162,-79.40571&zoom=16&size=600x600&markers=color:blue%7Clabel:S%7C43.65162,-79.40571&sensor=false > > > > > > Image: > > > http://maps.googleapis.com/maps/api/streetview?location=43.65162,%20-79.40571&size=600x600&sensor=false > > > > > > I am not sure if option 1 will work because the url doesn't actually lead to an image, but rather Google returns an image when that url is used. > > > > > > Any tips or pointers are much appreciated! > > > > > > > > > https://pypi.python.org/pypi/xlrd > > > > > > > > -- > > ----------------------------------------------------------------------- > > Tim Daneliuk Thanks Tim. Scott From kobewka at gmail.com Wed May 22 07:21:12 2013 From: kobewka at gmail.com (kobewka at gmail.com) Date: Wed, 22 May 2013 04:21:12 -0700 (PDT) Subject: Static Maps from Lat Long data in XLS file In-Reply-To: References: Message-ID: Thanks Ken. I'll have a closer look at those links. I also found Motionless, which creates a static map HTML file. Combined with what you said, I should be able to get what I need. https://github.com/ryancox/motionless Scott On Tuesday, May 21, 2013 9:58:25 AM UTC-4, Ken Bolton wrote: > On Tue, May 21, 2013 at 9:12 AM, wrote: > > > Hello, > > > > I'm new to Python, but I think it can solve my problem and I am looking for a someone to point me to tutorial or give me some tips here. > > ? > Hi! I am a first-time poster to python-list, but I think I can help you. > > > ? > I have an xls file that has about 1,000 latitude and longitude points. I want to do one of two things: 1) Save a static maps and street view image from the coordinates, or 2) create an html file with the map and street view image side by side. > > > > > If you save your xls file as a csv (comma-separated values), you can use python's built-in csv module, documented here - http://docs.python.org/2/library/csv.html, to read the file line by line. Store the values and substitute the strings into a new list of URLs. > > > ? > I need the urls to look like this: > > > > Map with a pin in the centre: > > http://maps.googleapis.com/maps/api/staticmap?center=43.65162,-79.40571&zoom=16&size=600x600&markers=color:blue%7Clabel:S%7C43.65162,-79.40571&sensor=false > > > > > > Image: > > http://maps.googleapis.com/maps/api/streetview?location=43.65162,%20-79.40571&size=600x600&sensor=false > > > > > I was able to use curl to grab the images you linked. I believe you can use urllib (or, better, requests - http://docs.python-requests.org/en/latest/) to get and save the images. > > > > > hth. > > > best, > ken From harry.percival at gmail.com Tue May 21 09:41:15 2013 From: harry.percival at gmail.com (Harry Percival) Date: Tue, 21 May 2013 14:41:15 +0100 Subject: suggestions for "best practices lite" for small site deployment with testing Message-ID: Hi everyone, We've been having a bit of a discussion of this topic over on the Python-UK list (http://mail.python.org/pipermail/python-uk/2013-May/thread.html#2949), and I was a bit shy about mailing out to the big, bad, worldwide Python list, but I'm forcing myself! So I'm writing a book, for O'Reilly, on TDD with Python, and I'm looking for some help and suggestions on my current chapter (well, I'm looking for help and suggestions in general too! i need all the help i can get.). http://www.obeythetestinggoat.com/what-to-say-about-deployment.html So far I've taken the user through building a basic site, and now I want to get them to deploy it. It's very early days, but minimum-viable-product and all that, deploying early + often is a good habit to get into. cf the blog post above for a bit of background. I'm currently leaning away from talking about the various PaaS offerings, they being too many + varied, and I have a bit of a conflict of interest since I work at PythonAnywere. Instead, I'm currently thinking we'll spin up a small linux box, put apache on it, and run both the staging and live site from there. All good stuff to learn... Hopefully some of the lessons will be applicable to PaaSes anyway. So, some questions: provisioning = spinning up a server, installing apache, setting up virtualhost config. confirm I should encourage people to automate this? Am leaning towards just using fabric, is bringing in another tool (chef/puppet/salt) overkill? deployment = updating the source code, database migration, static files. What are your favourite ways of doing this? Shell scripts? Git push hooks? I'm thinking fabric, again... Also: testing -- if the above two steps are automated using fabric, they'll be *functionally* tested because we'll run the functional test suite against the staging site. Any thoughts on whether + how to *unit* test deployment scripts? Take a look at the blog post, and let me know what you think? Bear in mind, at this stage, I'm looking for "best practices lite" -- at this stage, it's a tiny website, I can always bring in some more complexity later on. The idea is to get people introduced to the idea of deployment, how you automated, and where testing comes in. We don't have to talk about CDNs or zero-downtime or multi-server configuration management yet... Thanks in advance for any thoughts! Harry -- ------------------------------ Harry J.W. Percival ------------------------------ Twitter: @hjwp Mobile: +44 (0) 78877 02511 Skype: harry.percival -------------- next part -------------- An HTML attachment was scrubbed... URL: From jldunn2000 at gmail.com Tue May 21 11:26:13 2013 From: jldunn2000 at gmail.com (loial) Date: Tue, 21 May 2013 08:26:13 -0700 (PDT) Subject: How to raise a socket "104 connection reset by peer error" Message-ID: For testing purposes I want my code to raise a socket "connection reset by peer" error, so that I can test how I handle it, but I am not sure how to raise the error. Any advice appreciated From bahamutzero8825 at gmail.com Tue May 21 11:49:31 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 21 May 2013 10:49:31 -0500 Subject: How to raise a socket "104 connection reset by peer error" In-Reply-To: References: Message-ID: <519B978B.2070603@gmail.com> On 2013.05.21 10:26, loial wrote: > For testing purposes I want my code to raise a socket "connection reset by peer" error, so that I can test how I handle it, but I am not sure how to raise the error. Arbitrary exceptions can be raised with the raise keyword. In Python 3.3, that exact error got its own builtin exception: http://docs.python.org/3.3/tutorial/errors.html#raising-exceptions http://docs.python.org/3.3/library/exceptions.html#ConnectionResetError In earlier versions of Python, you will have to raise OSError and set its errno attribute. -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From mining.facts at gmail.com Tue May 21 11:26:46 2013 From: mining.facts at gmail.com (Christian) Date: Tue, 21 May 2013 08:26:46 -0700 (PDT) Subject: @staticmethods called more than once Message-ID: <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> Hi, i'm somewhat confused working with @staticmethods. My logger and configuration methods are called n times, but I have only one call. n is number of classes which import the loger and configuration class in the subfolder mymodule. What might be my mistake mistake? Many thanks Christian ### __init__.py ### from mymodule.MyLogger import MyLogger from mymodule.MyConfig import MyConfig ##### my_test.py ########## from mymodule import MyConfig,MyLogger #Both methods are static key,logfile,loglevel = MyConfig().get_config('Logging') log = MyLogger.set_logger(key,logfile,loglevel) log.critical(time.time()) #Output 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19 From skip at pobox.com Tue May 21 11:39:03 2013 From: skip at pobox.com (Skip Montanaro) Date: Tue, 21 May 2013 10:39:03 -0500 Subject: @staticmethods called more than once In-Reply-To: <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> References: <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> Message-ID: Don't confuse the use of "static" in Python with its use in C/C++. From a post on StackOverflow: A staticmethod is a method that knows nothing about the class or instance > it was called on. It just gets the arguments that were passed, no implicit > first argument. It is basically useless in Python -- you can just use a > module function instead of a staticmethod. That is, the "@staticmethod" decorator doesn't mean, "call this function once." Skip -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Tue May 21 12:30:22 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 21 May 2013 09:30:22 -0700 Subject: @staticmethods called more than once In-Reply-To: References: <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> Message-ID: <519BA11E.20408@stoneleaf.us> On 05/21/2013 08:39 AM, Skip Montanaro wrote: > Don't confuse the use of "static" in Python with its use in C/C++. From a post on StackOverflow: > > A staticmethod is a method that knows nothing about the class or instance it was called on. It just gets the > arguments that were passed, no implicit first argument. It is basically useless in Python -- you can just use a > module function instead of a staticmethod. For there record, staticmethod is useful when you want to make it possible for subclasses to change behavior. -- ~Ethan~ From dihedral88888 at googlemail.com Tue May 21 21:23:14 2013 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 21 May 2013 18:23:14 -0700 (PDT) Subject: @staticmethods called more than once In-Reply-To: References: <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> Message-ID: <83db731c-bc25-40cd-9271-815e11cb9088@googlegroups.com> Ethan Furman? 2013?5?22????UTC+8??12?30?22???? > On 05/21/2013 08:39 AM, Skip Montanaro wrote: > > > Don't confuse the use of "static" in Python with its use in C/C++. From a post on StackOverflow: > > > > > > A staticmethod is a method that knows nothing about the class or instance it was called on. It just gets the > > > arguments that were passed, no implicit first argument. It is basically useless in Python -- you can just use a > > > module function instead of a staticmethod. > > > > For there record, staticmethod is useful when you want to make it possible for subclasses to change behavior. > > > > -- > > ~Ethan~ I prefer objects in classes with slimer figures not heavily weighted with trivial methods in each instance construction and clone. But this is only my personal style of classes in python. From gordon at panix.com Tue May 21 12:34:46 2013 From: gordon at panix.com (John Gordon) Date: Tue, 21 May 2013 16:34:46 +0000 (UTC) Subject: @staticmethods called more than once References: <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> Message-ID: In <02f0123d-2f9e-4287-b983-cfa1db9db69c at googlegroups.com> Christian writes: > Hi, > i'm somewhat confused working with @staticmethods. My logger and configuration methods are called n times, but I have only one call. > n is number of classes which import the loger and configuration class > in the subfolder mymodule. What might be my mistake mistake? > Many thanks > Christian > ### __init__.py ### > from mymodule.MyLogger import MyLogger > from mymodule.MyConfig import MyConfig > ##### my_test.py ########## > from mymodule import MyConfig,MyLogger > #Both methods are static > key,logfile,loglevel = MyConfig().get_config('Logging') > log = MyLogger.set_logger(key,logfile,loglevel) > log.critical(time.time()) > #Output > 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19 > 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19 > 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19 > 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19 You haven't given us the code for your MyLogger class, so it's difficult to say exactly what the problem is. However, I have a guess. Does MyLogger.set_logger() contain a call to addHandler()? Each call to addHandler() adds another handler to your logger, and when you call log.critical() [or any other log function] you get one line of output for each handler. You should only call addHandler() once. -- 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 gordon at panix.com Tue May 21 12:48:07 2013 From: gordon at panix.com (John Gordon) Date: Tue, 21 May 2013 16:48:07 +0000 (UTC) Subject: @staticmethods called more than once References: <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> Message-ID: In John Gordon writes: > You should only call addHandler() once. ...for each intended logging output destination, of course. If you want logging output to appear in a file and on-screen, then you would call addHandler() once with a file handler and once with a screen handler. But I think you may be calling addHandler multiple times for the same file handler, which is causing the duplicate logging output. -- 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 mining.facts at gmail.com Tue May 21 13:17:53 2013 From: mining.facts at gmail.com (Christian) Date: Tue, 21 May 2013 10:17:53 -0700 (PDT) Subject: @staticmethods called more than once In-Reply-To: References: <02f0123d-2f9e-4287-b983-cfa1db9db69c@googlegroups.com> Message-ID: <42c56996-231f-476d-add8-375c584b77ee@googlegroups.com> Am Dienstag, 21. Mai 2013 18:48:07 UTC+2 schrieb John Gordon: > In John Gordon writes: > > > > > You should only call addHandler() once. > > > > ...for each intended logging output destination, of course. If you want > > logging output to appear in a file and on-screen, then you would call > > addHandler() once with a file handler and once with a screen handler. > > > > But I think you may be calling addHandler multiple times for the same > > file handler, which is causing the duplicate logging output. > > > > -- > > 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" Yes you're right. Many thanks! From stackoverflowuser95 at gmail.com Tue May 21 13:27:07 2013 From: stackoverflowuser95 at gmail.com (stackoverflowuser95 at gmail.com) Date: Tue, 21 May 2013 10:27:07 -0700 (PDT) Subject: More general way of generating PyODBC queries as a dict? Message-ID: <0f78102c-2e30-4858-ae62-b8d5cecfc345@googlegroups.com> Here are my averagely general class methods for creating a dictionary from the result of database queries: def make_schema_dict(self): schema = [i[2] for i in self.cursor.tables() if i[2].startswith('tbl_') or i[2].startswith('vw_')] self.schema = {table: {'scheme': [row.column_name for row in self.cursor.columns(table)]} for table in schema} def last_table_query_as_dict(self, table): return {'data': [{col: row.__getattribute__(col) for col in self.schema[table]['scheme'] if col != 'RowNum'} for row in self.cursor.fetchall()]} Unfortunately as you can see, there are many complications. For example, when multiple tables are queried; some hackish lambdas are required to generate the resulting dictionary. Can you think of some more general methods? (and yes I know this is against the PEP; and that this is also on SO) From stackoverflowuser95 at gmail.com Wed May 22 05:31:57 2013 From: stackoverflowuser95 at gmail.com (stackoverflowuser95 at gmail.com) Date: Wed, 22 May 2013 02:31:57 -0700 (PDT) Subject: More general way of generating PyODBC queries as a dict? In-Reply-To: <0f78102c-2e30-4858-ae62-b8d5cecfc345@googlegroups.com> References: <0f78102c-2e30-4858-ae62-b8d5cecfc345@googlegroups.com> Message-ID: <350aaf43-2259-44cc-9ca3-e86a6823164d@googlegroups.com> On Wednesday, May 22, 2013 9:33:18 AM UTC+10, Dennis Lee Bieber wrote: > On Tue, 21 May 2013 10:27:07 -0700 (PDT), stackoverflowuser95 at gmail.com > > declaimed the following in gmane.comp.python.general: > > > > > > > > For example, when multiple tables are queried; some hackish lambdas are required to generate the resulting dictionary. > > > > > > Can you think of some more general methods? > > > > > What about using the information from > > > > cursor.description > > > > You did state PyODBC, did you not? > > > > """ > > description > > > > This read-only attribute is a list of 7-item tuples, each containing > > (name, type_code, display_size, internal_size, precision, scale, > > null_ok). pyodbc only provides values for name, type_code, > > internal_size, and null_ok. The other values are set to None. > > """ > > -- > > Wulfraed Dennis Lee Bieber AF6VN > > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ YAY: `[{c[0]: v for (c, v) in zip(row.cursor_description, row)} for row in self.cursor.fetchall()]` From gvanem at broadpark.no Tue May 21 15:35:46 2013 From: gvanem at broadpark.no (Gisle Vanem) Date: Tue, 21 May 2013 21:35:46 +0200 Subject: Modules list-tool Message-ID: Are anyone aware of a tool that can show me at run-time which modules (pyd/dll) are loaded into a Python program at a specific time (or over time)? To clarify, e.g. when running a sample from PyQt4 (examples\tutorials\addressbook\part1.pyw) and using Process Explorer [1], I can launch WinDbg from it and get this list of modules: .... ModLoad: 1d000000 1d00a000 G:\ProgramFiler\Python27\python.EXE ModLoad: 7c900000 7c9b1000 F:\WINDOWS\system32\ntdll.dll ModLoad: 7c800000 7c8f7000 F:\WINDOWS\system32\kernel32.dll ModLoad: 1e000000 1e261000 f:\windows\system32\python27.dll ModLoad: 7e410000 7e4a1000 F:\WINDOWS\system32\USER32.dll ModLoad: 77f10000 77f59000 F:\WINDOWS\system32\GDI32.dll ModLoad: 77dc0000 77e6a000 F:\WINDOWS\system32\ADVAPI32.dll ModLoad: 77e70000 77f03000 F:\WINDOWS\system32\RPCRT4.dll ModLoad: 77fe0000 77ff1000 F:\WINDOWS\system32\Secur32.dll ModLoad: 7c9c0000 7d1d8000 F:\WINDOWS\system32\SHELL32.dll ModLoad: 77c00000 77c58000 F:\WINDOWS\system32\msvcrt.dll ModLoad: 77f60000 77fd6000 F:\WINDOWS\system32\SHLWAPI.dll ModLoad: 78520000 785c3000 f:\windows\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.6161_x-ww_31a54e43\MSVCR90.dll ModLoad: 76370000 7638d000 f:\windows\system32\IMM32.DLL ModLoad: 62f20000 62f29000 f:\windows\system32\LPK.DLL ModLoad: 75420000 7548b000 f:\windows\system32\USP10.dll ModLoad: 773c0000 774c3000 f:\windows\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.6028_x-ww_61e65202\comctl32.dll ModLoad: 5d5d0000 5d66a000 F:\WINDOWS\system32\comctl32.dll ModLoad: 78aa0000 78b5f000 f:\windows\system32\MSVCR100.dll ModLoad: 00d90000 00f29000 g:\ProgramFiler\Python27\lib\site-packages\PyQt4\QtCore.pyd ModLoad: 67000000 67260000 g:\ProgramFiler\Python27\lib\site-packages\PyQt4\QtCore4.dll ModLoad: 774d0000 7760e000 F:\WINDOWS\system32\ole32.dll ModLoad: 71aa0000 71ab7000 f:\windows\system32\WS2_32.dll ModLoad: 71a90000 71a98000 f:\windows\system32\WS2HELP.dll ModLoad: 78480000 7850e000 f:\windows\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.6161_x-ww_31a54e43\MSVCP90.dll ModLoad: 00a60000 00a73000 g:\ProgramFiler\Python27\lib\site-packages\sip.pyd ModLoad: 011f0000 0177f000 g:\ProgramFiler\Python27\lib\site-packages\PyQt4\QtGui.pyd ModLoad: 65000000 657c4000 g:\ProgramFiler\Python27\lib\site-packages\PyQt4\QtGui4.dll ... ------------- My example may be mooth since part1.pyw above (when I enter the debugger) is just waiting for events. The stack of pythonw.exe as shown in Process Explorer: ... ntdll.dll!ZwWaitForMultipleObjects+0xc kernel32.dll!WaitForMultipleObjectsEx+0x12c USER32.dll!RealMsgWaitForMultipleObjectsEx+0x13e QtCore4.dll!QEventDispatcherWin32::processEvents+0x3c3 ntdll.dll!RtlAcquirePebLock+0x28 Is there a tool that can do something similar? (written in Python maybe?). But a bit simpler to use than my current method. Just launch it from the command-line; something like "pyXX part1.pyw " [1] http://technet.microsoft.com/en-gb/sysinternals/bb896653 --gv From drsalists at gmail.com Thu May 23 00:39:52 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 22 May 2013 21:39:52 -0700 Subject: Modules list-tool In-Reply-To: References: Message-ID: On Tue, May 21, 2013 at 12:35 PM, Gisle Vanem wrote: > Are anyone aware of a tool that can show me at run-time > which modules (pyd/dll) are loaded into a Python program at a specific > time (or over time)? > > To clarify, e.g. when running a sample from PyQt4 > (examples\tutorials\**addressbook\part1.pyw) and using Process Explorer > [1], > I can launch WinDbg from it and get this list of modules: > .... > > ModLoad: 1d000000 1d00a000 G:\ProgramFiler\Python27\**python.EXE > ModLoad: 7c900000 7c9b1000 F:\WINDOWS\system32\ntdll.dll > ModLoad: 7c800000 7c8f7000 F:\WINDOWS\system32\kernel32.**dll > ModLoad: 1e000000 1e261000 f:\windows\system32\python27.**dll > ModLoad: 7e410000 7e4a1000 F:\WINDOWS\system32\USER32.dll > ModLoad: 77f10000 77f59000 F:\WINDOWS\system32\GDI32.dll > ModLoad: 77dc0000 77e6a000 F:\WINDOWS\system32\ADVAPI32.**dll > ModLoad: 77e70000 77f03000 F:\WINDOWS\system32\RPCRT4.dll > ModLoad: 77fe0000 77ff1000 F:\WINDOWS\system32\Secur32.**dll > ModLoad: 7c9c0000 7d1d8000 F:\WINDOWS\system32\SHELL32.**dll > ModLoad: 77c00000 77c58000 F:\WINDOWS\system32\msvcrt.dll > ModLoad: 77f60000 77fd6000 F:\WINDOWS\system32\SHLWAPI.**dll > ModLoad: 78520000 785c3000 f:\windows\WinSxS\x86_**Microsoft.VC90.CRT_** > 1fc8b3b9a1e18e3b_9.0.30729.**6161_x-ww_31a54e43\MSVCR90.dll > ModLoad: 76370000 7638d000 f:\windows\system32\IMM32.DLL > ModLoad: 62f20000 62f29000 f:\windows\system32\LPK.DLL > ModLoad: 75420000 7548b000 f:\windows\system32\USP10.dll > ModLoad: 773c0000 774c3000 f:\windows\WinSxS\x86_** > Microsoft.Windows.Common-**Controls_6595b64144ccf1df_6.0.2600.6028_x-** > ww_61e65202\comctl32.dll > ModLoad: 5d5d0000 5d66a000 F:\WINDOWS\system32\comctl32.**dll > ModLoad: 78aa0000 78b5f000 f:\windows\system32\MSVCR100.**dll > ModLoad: 00d90000 00f29000 g:\ProgramFiler\Python27\lib\** > site-packages\PyQt4\QtCore.pyd > ModLoad: 67000000 67260000 g:\ProgramFiler\Python27\lib\** > site-packages\PyQt4\QtCore4.**dll > ModLoad: 774d0000 7760e000 F:\WINDOWS\system32\ole32.dll > ModLoad: 71aa0000 71ab7000 f:\windows\system32\WS2_32.dll > ModLoad: 71a90000 71a98000 f:\windows\system32\WS2HELP.**dll > ModLoad: 78480000 7850e000 f:\windows\WinSxS\x86_**Microsoft.VC90.CRT_** > 1fc8b3b9a1e18e3b_9.0.30729.**6161_x-ww_31a54e43\MSVCP90.dll > ModLoad: 00a60000 00a73000 g:\ProgramFiler\Python27\lib\** > site-packages\sip.pyd > ModLoad: 011f0000 0177f000 g:\ProgramFiler\Python27\lib\** > site-packages\PyQt4\QtGui.pyd > ModLoad: 65000000 657c4000 g:\ProgramFiler\Python27\lib\** > site-packages\PyQt4\QtGui4.dll > ... > > ------------- > > My example may be mooth since part1.pyw above (when I enter > the debugger) is just waiting for events. The stack of pythonw.exe as > shown in Process Explorer: > ... > ntdll.dll!**ZwWaitForMultipleObjects+0xc > kernel32.dll!**WaitForMultipleObjectsEx+0x12c > USER32.dll!**RealMsgWaitForMultipleObjectsE**x+0x13e > QtCore4.dll!**QEventDispatcherWin32::**processEvents+0x3c3 > ntdll.dll!RtlAcquirePebLock+**0x28 > > Is there a tool that can do something similar? (written in Python maybe?). > But a bit simpler to use than my current method. Just launch it from the > command-line; something like "pyXX part1.pyw " > > [1] http://technet.microsoft.com/**en-gb/sysinternals/bb896653 > > --gv > -- > http://mail.python.org/**mailman/listinfo/python-list > Python -v reports on modules. -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Thu May 23 01:19:31 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 08:19:31 +0300 Subject: Modules list-tool In-Reply-To: References: , Message-ID: Have you tried Inspect Shell[1]? All you have to do to monitor your script is include "import inspect_shell" in the 1st line of you source code and then run: "python inspect_shell.py" When you get the prompt you can enter the following to show the list of modules: localhost:1234> '\n'.join(['{}={}'.format(k,v) for k,v in sys.modules.items()]) [1] https://github.com/amoffat/Inspect-Shell ________________________________ > Date: Wed, 22 May 2013 21:39:52 -0700 > Subject: Re: Modules list-tool > From: drsalists at gmail.com > To: gvanem at broadpark.no > CC: python-list at python.org > > On Tue, May 21, 2013 at 12:35 PM, Gisle Vanem > > wrote: > Are anyone aware of a tool that can show me at run-time > which modules (pyd/dll) are loaded into a Python program at a specific > time (or over time)? > > To clarify, e.g. when running a sample from PyQt4 > (examples\tutorials\addressbook\part1.pyw) and using Process Explorer [1], > I can launch WinDbg from it and get this list of modules: > .... > > ModLoad: 1d000000 1d00a000 G:\ProgramFiler\Python27\python.EXE > ModLoad: 7c900000 7c9b1000 F:\WINDOWS\system32\ntdll.dll > ModLoad: 7c800000 7c8f7000 F:\WINDOWS\system32\kernel32.dll > ModLoad: 1e000000 1e261000 f:\windows\system32\python27.dll > ModLoad: 7e410000 7e4a1000 F:\WINDOWS\system32\USER32.dll > ModLoad: 77f10000 77f59000 F:\WINDOWS\system32\GDI32.dll > ModLoad: 77dc0000 77e6a000 F:\WINDOWS\system32\ADVAPI32.dll > ModLoad: 77e70000 77f03000 F:\WINDOWS\system32\RPCRT4.dll > ModLoad: 77fe0000 77ff1000 F:\WINDOWS\system32\Secur32.dll > ModLoad: 7c9c0000 7d1d8000 F:\WINDOWS\system32\SHELL32.dll > ModLoad: 77c00000 77c58000 F:\WINDOWS\system32\msvcrt.dll > ModLoad: 77f60000 77fd6000 F:\WINDOWS\system32\SHLWAPI.dll > ModLoad: 78520000 785c3000 > f:\windows\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.6161_x-ww_31a54e43\MSVCR90.dll > ModLoad: 76370000 7638d000 f:\windows\system32\IMM32.DLL > ModLoad: 62f20000 62f29000 f:\windows\system32\LPK.DLL > ModLoad: 75420000 7548b000 f:\windows\system32\USP10.dll > ModLoad: 773c0000 774c3000 > f:\windows\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.6028_x-ww_61e65202\comctl32.dll > ModLoad: 5d5d0000 5d66a000 F:\WINDOWS\system32\comctl32.dll > ModLoad: 78aa0000 78b5f000 f:\windows\system32\MSVCR100.dll > ModLoad: 00d90000 00f29000 > g:\ProgramFiler\Python27\lib\site-packages\PyQt4\QtCore.pyd > ModLoad: 67000000 67260000 > g:\ProgramFiler\Python27\lib\site-packages\PyQt4\QtCore4.dll > ModLoad: 774d0000 7760e000 F:\WINDOWS\system32\ole32.dll > ModLoad: 71aa0000 71ab7000 f:\windows\system32\WS2_32.dll > ModLoad: 71a90000 71a98000 f:\windows\system32\WS2HELP.dll > ModLoad: 78480000 7850e000 > f:\windows\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.6161_x-ww_31a54e43\MSVCP90.dll > ModLoad: 00a60000 00a73000 > g:\ProgramFiler\Python27\lib\site-packages\sip.pyd > ModLoad: 011f0000 0177f000 > g:\ProgramFiler\Python27\lib\site-packages\PyQt4\QtGui.pyd > ModLoad: 65000000 657c4000 > g:\ProgramFiler\Python27\lib\site-packages\PyQt4\QtGui4.dll > ... > > ------------- > > My example may be mooth since part1.pyw above (when I enter > the debugger) is just waiting for events. The stack of pythonw.exe as > shown in Process Explorer: > ... > ntdll.dll!ZwWaitForMultipleObjects+0xc > kernel32.dll!WaitForMultipleObjectsEx+0x12c > USER32.dll!RealMsgWaitForMultipleObjectsEx+0x13e > QtCore4.dll!QEventDispatcherWin32::processEvents+0x3c3 > ntdll.dll!RtlAcquirePebLock+0x28 > > Is there a tool that can do something similar? (written in Python > maybe?). But a bit simpler to use than my current method. Just launch > it from the command-line; something like "pyXX part1.pyw " > > [1] http://technet.microsoft.com/en-gb/sysinternals/bb896653 > > --gv > -- > http://mail.python.org/mailman/listinfo/python-list > > Python -v reports on modules. > > -- http://mail.python.org/mailman/listinfo/python-list From info at DatabasePrograms.Biz Tue May 21 21:15:27 2013 From: info at DatabasePrograms.Biz (info at DatabasePrograms.Biz) Date: Wed, 22 May 2013 01:15:27 +0000 Subject: A computer programmer, web developer and network admin resume Message-ID: A computer programmer, web developer and network administrator resume. For a resume in HTML or .Doc format click on: www.DatabasePrograms.Biz If you would like this in text format instead, please let me know. Daniel Rapaport 1-949-307-2485 info at DatabasePrograms.Biz To unsubscribe click on: info at DatabasePrograms.Biz?Subject=unsubscribe -------------- next part -------------- An HTML attachment was scrubbed... URL: From denismfmcmahon at gmail.com Tue May 21 21:42:39 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Wed, 22 May 2013 01:42:39 +0000 (UTC) Subject: A computer programmer, web developer and network admin resume References: Message-ID: On Wed, 22 May 2013 01:15:27 +0000, info at DatabasePrograms.Biz wrote: > If you would like this in text format instead, please let me know. What if we don't want it at all? -- Denis McMahon, denismfmcmahon at gmail.com From python.list at tim.thechases.com Tue May 21 22:32:06 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 21 May 2013 21:32:06 -0500 Subject: A computer programmer, web developer and network admin resume In-Reply-To: References: Message-ID: <20130521213206.126ce7e0@bigbox.christie.dr> On 2013-05-22 01:15, info at DatabasePrograms.Biz wrote: > A computer programmer, web developer and network administrator ...walk into a bar... So what's the punchline? -tkc From rosuav at gmail.com Wed May 22 02:39:31 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 22 May 2013 16:39:31 +1000 Subject: A computer programmer, web developer and network admin resume In-Reply-To: <20130521213206.126ce7e0@bigbox.christie.dr> References: <20130521213206.126ce7e0@bigbox.christie.dr> Message-ID: On Wed, May 22, 2013 at 12:32 PM, Tim Chase wrote: > On 2013-05-22 01:15, info at DatabasePrograms.Biz wrote: >> A computer programmer, web developer and network administrator > > ...walk into a bar... > > So what's the punchline? ;steps up to the mike So yeah, as I was saying, a programmer, a web dev, and a BOFH walk into a bar. The other two buy the BOFH a drink, because they're not stupid, and anyway, so this luser walks up to them with a resume in his hand. "....," says the luser. "So what's the punchline?" says the computer programmer. The stand-up comic steps up to the mike, and he says, "So yeah, as I was saying, a programmer, a web dev, and a BOFH walk into a stable time loop..." ChrisA From python.list at tim.thechases.com Wed May 22 09:03:37 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 22 May 2013 08:03:37 -0500 Subject: A computer programmer, web developer and network admin resume In-Reply-To: References: <20130521213206.126ce7e0@bigbox.christie.dr> Message-ID: <20130522080337.5c431bb3@bigbox.christie.dr> On 2013-05-22 16:39, Chris Angelico wrote: > On Wed, May 22, 2013 at 12:32 PM, Tim Chase > wrote: > > On 2013-05-22 01:15, info at DatabasePrograms.Biz wrote: > >> A computer programmer, web developer and network administrator > > > > ...walk into a bar... > > > > So what's the punchline? > > ;steps up to the mike > > So yeah, as I was saying, a programmer, a web dev, and a BOFH walk > into a bar. The other two buy the BOFH a drink, because they're not > stupid, and anyway, so this luser walks up to them with a resume in > his hand. > > "....," says the luser. > > "So what's the punchline?" says the computer programmer. > > The stand-up comic steps up to the mike, and he says, "So yeah, as I > was saying, a programmer, a web dev, and a BOFH walk into a stable > time loop..." So a pirate programmer walks into a bar with a bird on his shoulder. The bird repeatedly squawks "pieces of nine! pieces of nine!". The bartender looks at him and asks "what's up with the bird?" to which the pirate says "Arrr, he's got a parroty error." The bartender replies, "Ah, I thought he was a bit off." -tkc From harry.percival at gmail.com Wed May 22 09:10:17 2013 From: harry.percival at gmail.com (Harry Percival) Date: Wed, 22 May 2013 14:10:17 +0100 Subject: A computer programmer, web developer and network admin resume In-Reply-To: <20130522080337.5c431bb3@bigbox.christie.dr> References: <20130521213206.126ce7e0@bigbox.christie.dr> <20130522080337.5c431bb3@bigbox.christie.dr> Message-ID: oh wow. great one, thanks for that tim :))))) On 22 May 2013 14:03, Tim Chase wrote: > On 2013-05-22 16:39, Chris Angelico wrote: > > On Wed, May 22, 2013 at 12:32 PM, Tim Chase > > wrote: > > > On 2013-05-22 01:15, info at DatabasePrograms.Biz wrote: > > >> A computer programmer, web developer and network administrator > > > > > > ...walk into a bar... > > > > > > So what's the punchline? > > > > ;steps up to the mike > > > > So yeah, as I was saying, a programmer, a web dev, and a BOFH walk > > into a bar. The other two buy the BOFH a drink, because they're not > > stupid, and anyway, so this luser walks up to them with a resume in > > his hand. > > > > "....," says the luser. > > > > "So what's the punchline?" says the computer programmer. > > > > The stand-up comic steps up to the mike, and he says, "So yeah, as I > > was saying, a programmer, a web dev, and a BOFH walk into a stable > > time loop..." > > So a pirate programmer walks into a bar with a bird on his shoulder. > The bird repeatedly squawks "pieces of nine! pieces of nine!". The > bartender looks at him and asks "what's up with the bird?" to which > the pirate says "Arrr, he's got a parroty error." The bartender > replies, "Ah, I thought he was a bit off." > > -tkc > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- ------------------------------ Harry J.W. Percival ------------------------------ Twitter: @hjwp Mobile: +44 (0) 78877 02511 Skype: harry.percival -------------- next part -------------- An HTML attachment was scrubbed... URL: From greg.ewing at canterbury.ac.nz Wed May 22 19:25:55 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Thu, 23 May 2013 11:25:55 +1200 Subject: A computer programmer, web developer and network admin resume In-Reply-To: References: <20130521213206.126ce7e0@bigbox.christie.dr> Message-ID: Tim Chase wrote: > So a pirate programmer walks into a bar with a bird on his shoulder. > The bird repeatedly squawks "pieces of nine! pieces of nine!". The > bartender looks at him and asks "what's up with the bird?" to which > the pirate says "Arrr, he's got a parroty error." No, he's just using half-open ranges. -- Greg From ken at bscientific.net Wed May 22 21:31:13 2013 From: ken at bscientific.net (Ken Bolton) Date: Wed, 22 May 2013 21:31:13 -0400 Subject: A computer programmer, web developer and network admin resume In-Reply-To: References: <20130521213206.126ce7e0@bigbox.christie.dr> Message-ID: On Wed, May 22, 2013 at 7:25 PM, Gregory Ewing wrote: > Tim Chase wrote: > >> So a pirate programmer walks into a bar with a bird on his shoulder. >> The bird repeatedly squawks "pieces of nine! pieces of nine!". The >> bartender looks at him and asks "what's up with the bird?" to which >> the pirate says "Arrr, he's got a parroty error." >> > > No, he's just using half-open ranges. That is the punchline for the one about the cowboy programmer. -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Thu May 23 08:28:43 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 23 May 2013 12:28:43 GMT Subject: A computer programmer, web developer and network admin resume References: Message-ID: On 2013-05-22, Tim Chase wrote: > On 2013-05-22 01:15, info at DatabasePrograms.Biz wrote: >> A computer programmer, web developer and network administrator > > ...walk into a bar... > > So what's the punchline? "Ow." Get it? "Ow." -- Neil Cerutti From rosuav at gmail.com Thu May 23 08:35:05 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 22:35:05 +1000 Subject: A computer programmer, web developer and network admin resume In-Reply-To: References: Message-ID: On Thu, May 23, 2013 at 10:28 PM, Neil Cerutti wrote: > On 2013-05-22, Tim Chase wrote: >> On 2013-05-22 01:15, info at DatabasePrograms.Biz wrote: >>> A computer programmer, web developer and network administrator >> >> ...walk into a bar... >> >> So what's the punchline? > > "Ow." Get it? "Ow." Better, better, but Waaaah! Wait. This isn't getting-hit-on-the-head-with-a-bad-joke lessons? ChrisA From carlosnepomuceno at outlook.com Tue May 21 22:26:11 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 22 May 2013 05:26:11 +0300 Subject: Myth Busters: % "this old style of formatting will eventually be removed from the language" Message-ID: I was looking for something else and just found what I think is the place where I was first exposed to the myth[1]: "Since?str.format()?is quite new, a lot of Python code still uses the?%?operator. However, because this old style of formatting will eventually be removed from the language,?str.format()?should generally be used." Is this tutorial outdated or this still an issue? [1] http://docs.python.org/2/tutorial/inputoutput.html#old-string-formatting From ethan at stoneleaf.us Tue May 21 22:37:32 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 21 May 2013 19:37:32 -0700 Subject: Myth Busters: % "this old style of formatting will eventually be removed from the language" In-Reply-To: References: Message-ID: <519C2F6C.3050901@stoneleaf.us> On 05/21/2013 07:26 PM, Carlos Nepomuceno wrote: > I was looking for something else and just found what I think is the place where I was first exposed to the myth[1]: > > "Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used." > > Is this tutorial outdated or this still an issue? It was exuberant wishful thinking. ;) While .format() does have its uses, % is in wide-spread use even among the core-devs. It's not going away any time soon. -- ~Ethan~ From ned at nedbatchelder.com Tue May 21 23:26:58 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Tue, 21 May 2013 23:26:58 -0400 Subject: Myth Busters: % "this old style of formatting will eventually be removed from the language" In-Reply-To: References: Message-ID: <519C3B02.1020706@nedbatchelder.com> On 5/21/2013 10:26 PM, Carlos Nepomuceno wrote: > I was looking for something else and just found what I think is the place where I was first exposed to the myth[1]: > > "Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used." > > Is this tutorial outdated or this still an issue? > > [1] http://docs.python.org/2/tutorial/inputoutput.html#old-string-formatting That tutorial is out of date. %-formatting isn't being removed. --Ned. From carlosnepomuceno at outlook.com Wed May 22 01:59:54 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 22 May 2013 08:59:54 +0300 Subject: Myth Busters: % "this old style of formatting will eventually be removed from the language" In-Reply-To: <519C3B02.1020706@nedbatchelder.com> References: , <519C3B02.1020706@nedbatchelder.com> Message-ID: Ok. Thanks! bugs.python.org/issue18031 ---------------------------------------- > Date: Tue, 21 May 2013 23:26:58 -0400 > From: ned at nedbatchelder.com > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > Subject: Re: Myth Busters: % "this old style of formatting will eventually be removed from the language" > > On 5/21/2013 10:26 PM, Carlos Nepomuceno wrote: >> I was looking for something else and just found what I think is the place where I was first exposed to the myth[1]: >> >> "Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used." >> >> Is this tutorial outdated or this still an issue? >> >> [1] http://docs.python.org/2/tutorial/inputoutput.html#old-string-formatting > > That tutorial is out of date. %-formatting isn't being removed. > > --Ned. From skip at pobox.com Wed May 22 06:35:42 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 22 May 2013 05:35:42 -0500 Subject: Myth Busters: % "this old style of formatting will eventually be removed from the language" In-Reply-To: <519C3B02.1020706@nedbatchelder.com> References: <519C3B02.1020706@nedbatchelder.com> Message-ID: >> Is this tutorial outdated or this still an issue? >> >> [1] >> http://docs.python.org/2/tutorial/inputoutput.html#old-string-formatting > > > That tutorial is out of date. %-formatting isn't being removed. OTOH, PEP 3101 also mentions deprecation, at the very end: "... both systems can co-exist until it comes time to deprecate the older system." I have been operating under the assumption since the days of that PEP that %-style formatting would eventually disappear, dreading the day when I'd have to learn the str.format language. I apologize for (inadvertently) spreading FUD. It does seem like the documentation should be updated in a few places. If the decision has been made to not remove the older system, it might be worthwhile to mention that somewhere. Clearly the tutorial and PEP 3101 should be updated. Skip From pruebauno at latinmail.com Wed May 22 15:49:51 2013 From: pruebauno at latinmail.com (nn) Date: Wed, 22 May 2013 12:49:51 -0700 (PDT) Subject: Myth Busters: % "this old style of formatting will eventually be removed from the language" References: <519C3B02.1020706@nedbatchelder.com> Message-ID: <252d4afd-9d8c-4ae3-83cc-6e9c830b1706@r3g2000yqe.googlegroups.com> On May 22, 6:35?am, Skip Montanaro wrote: > >> Is this tutorial outdated or this still an issue? > > >> [1] > >>http://docs.python.org/2/tutorial/inputoutput.html#old-string-formatting > > > That tutorial is out of date. ?%-formatting isn't being removed. > > OTOH, PEP 3101 also mentions deprecation, at the very end: "... both > systems can co-exist until it comes time to deprecate the older > system." > > I have been operating under the assumption since the days of that PEP > that %-style formatting would eventually disappear, dreading the day > when I'd have to learn the str.format language. ?I apologize for > (inadvertently) spreading FUD. > > It does seem like the documentation should be updated in a few places. > ?If the decision has been made to not remove the older system, it > might be worthwhile to mention that somewhere. ?Clearly the tutorial > and PEP 3101 should be updated. > > Skip I was of the impression that deprecating % was still planned for Py4k, whenever that is. I personally believe that eliminating % formatting would reduce bugs and generally make code clearer, but I realize that breaking backward compatibility has a high price. From denismfmcmahon at gmail.com Wed May 22 10:24:03 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Wed, 22 May 2013 14:24:03 +0000 (UTC) Subject: Myth Busters: % "this old style of formatting will eventually be removed from the language" References: Message-ID: On Tue, 21 May 2013 23:26:58 -0400, Ned Batchelder wrote: > On 5/21/2013 10:26 PM, Carlos Nepomuceno wrote: >> "Since str.format() is quite new, a lot of Python code still uses the % >> operator. However, because this old style of formatting will eventually >> be removed from the language, str.format() should generally be used." >> Is this tutorial outdated or this still an issue? > That tutorial is out of date. %-formatting isn't being removed. Indeed, removing %-formatting could break a substantial amount of live code, with potentially significant maintenance effort in the user community simply to make existing code work with the new interpreter. The effect of this on corporations using python code translates into "business risk", and the next step is "we can avoid the business risk by migrating our python scripts to some other language." For the designers and maintainers of any language to arbitrarily[1] (in the eyes of the user base) remove a widely used feature that would have a major effect on the user base could kill off a language, simply because many users will not want to take the risk of it happening again, even if they can easily automate the upgrade from removed obsolete language feature to new shiny language feature. [1] Some portion of the user base will always consider any such change that causes them headaches and additional effort as having been arbitrary, no matter how well the language designers and maintainers explain the need to break the old scripts. -- Denis McMahon, denismfmcmahon at gmail.com From tjreedy at udel.edu Wed May 22 15:40:22 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Wed, 22 May 2013 15:40:22 -0400 Subject: Myth Busters: % "this old style of formatting will eventually be removed from the language" In-Reply-To: References: Message-ID: On 5/22/2013 10:24 AM, Denis McMahon wrote: > Indeed, removing %-formatting could break a substantial amount of live > code, with potentially significant maintenance effort in the user While I would like to see % formatting go away everntually*, other developers would not. In any case, I agree that it should not disappear until there is a foolproof conversion tool, probably custom written. I am working on other things. * perhaps in 10 years?, when all 2.x code that is going to be converted has been converted From jcasale at activenetwerx.com Tue May 21 23:59:55 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Wed, 22 May 2013 03:59:55 +0000 Subject: Case insensitive dict Message-ID: I was doing some work with the ldap module and required a ci dict that was case insensitive but case preserving. It turned out the cidict class they implemented was broken with respect to pop, it is inherited and not re implemented to work. Before I set about re-inventing the wheel, anyone know of a working implementation? I noticed twisted has one but it seems to omit pop. Thanks! jlc From steve+comp.lang.python at pearwood.info Wed May 22 01:19:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 May 2013 05:19:52 GMT Subject: Case insensitive dict References: Message-ID: <519c5578$0$1591$c3e8da3$76491128@news.astraweb.com> On Wed, 22 May 2013 03:59:55 +0000, Joseph L. Casale wrote: > I was doing some work with the ldap module and required a ci dict that > was case insensitive but case preserving. It turned out the cidict class > they implemented was broken with respect to pop, it is inherited and not > re implemented to work. Before I set about re-inventing the wheel, > anyone know of a working implementation? class my_cidict(ldap.cidict): """Fix problems with pop.""" def pop(self): # insert code here You don't have to re-invent the entire wheel just to fix one broken spoke :-) Other than that, no, I don't know of any case-insensitive but preserving dicts. If you do decide to write one from scratch, please consider releasing it as Open Source (I recommend GPL or MIT licences), either as a module on PyPI or as a recipe on ActiveState. https://pypi.python.org/pypi http://code.activestate.com/recipes/langs/python/ Thank you. -- Steven From cndesrosiers at gmail.com Wed May 22 02:23:15 2013 From: cndesrosiers at gmail.com (C. N. Desrosiers) Date: Tue, 21 May 2013 23:23:15 -0700 (PDT) Subject: Newbie question about evaluating raw_input() responses Message-ID: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> Hi, I'm just starting out with Python and to practice I am trying to write a script that can have a simple conversation with the user. When I run the below code, it always ends up printing response to "if age > 18:" -- even if I enter a value below 18. Can anyone point me to what I am doing wrong? Many thanks in advance. age=raw_input('Enter your age: ') if age > 18: print ('Wow, %s. You can buy cigarettes.' % age) else: print ('You are a young grasshopper.') From fabiosantosart at gmail.com Wed May 22 02:35:18 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 22 May 2013 07:35:18 +0100 Subject: Newbie question about evaluating raw_input() responses In-Reply-To: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> Message-ID: You have to convert `age` to an integer. Use int() to do it. Then you can compare it to other numbers and obtain the expected results. On 22 May 2013 07:29, "C. N. Desrosiers" wrote: > Hi, > > I'm just starting out with Python and to practice I am trying to write a > script that can have a simple conversation with the user. > > When I run the below code, it always ends up printing response to "if age > > 18:" -- even if I enter a value below 18. > > Can anyone point me to what I am doing wrong? Many thanks in advance. > > age=raw_input('Enter your age: ') > if age > 18: > print ('Wow, %s. You can buy cigarettes.' % age) > else: > print ('You are a young grasshopper.') > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kevin.xgr at gmail.com Wed May 22 02:52:30 2013 From: kevin.xgr at gmail.com (Kevin Xi) Date: Tue, 21 May 2013 23:52:30 -0700 (PDT) Subject: Newbie question about evaluating raw_input() responses In-Reply-To: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> Message-ID: On Wednesday, May 22, 2013 2:23:15 PM UTC+8, C. N. Desrosiers wrote: > Hi, > Hi, > > I'm just starting out with Python and to practice I am trying to write a script that can have a simple conversation with the user. > So you may want to search the doc before you ask: http://docs.python.org > > When I run the below code, it always ends up printing response to "if age > 18:" -- even if I enter a value below 18. > > > > Can anyone point me to what I am doing wrong? Many thanks in advance. > > > > age=raw_input('Enter your age: ') > > if age > 18: > > print ('Wow, %s. You can buy cigarettes.' % age) > > else: > > print ('You are a young grasshopper.') You can either use `raw_input` to read data and convert it to right type, or use `input` to get an integer directly. Read this: http://docs.python.org/2/library/functions.html#raw_input http://docs.python.org/2/library/functions.html#input Kevin From rosuav at gmail.com Wed May 22 03:19:06 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 22 May 2013 17:19:06 +1000 Subject: Newbie question about evaluating raw_input() responses In-Reply-To: References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> Message-ID: On Wed, May 22, 2013 at 4:52 PM, Kevin Xi wrote: > On Wednesday, May 22, 2013 2:23:15 PM UTC+8, C. N. Desrosiers wrote: >> age=raw_input('Enter your age: ') >> if age > 18: > > You can either use `raw_input` to read data and convert it to right type, or use `input` to get an integer directly. Read this: http://docs.python.org/2/library/functions.html#raw_input > http://docs.python.org/2/library/functions.html#input No! No, please do NOT use input()! It does not return an integer; it *evaluates* (that is, executes) the input. >>> input('Enter your age: ') Enter your age: 18 18 >>> input('Enter your age: ') Enter your age: 1+2+4+5+6 18 >>> input('Enter your age: ') Enter your age: sys.stdout.write("Hello, world!\n") or 18 Hello, world! 18 >>> input('Enter your age: ') Enter your age: sys.exit(0) This is almost certainly NOT what you want to have in your script. If you want an integer, just pass it through int() as Fabio suggested. Please do not use, or advocate using, this steam-powered Izzet goblin hammer for cracking walnuts. ChrisA From alister.ware at ntlworld.com Wed May 22 18:31:04 2013 From: alister.ware at ntlworld.com (Alister) Date: Wed, 22 May 2013 22:31:04 GMT Subject: Newbie question about evaluating raw_input() responses References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> Message-ID: On Tue, 21 May 2013 23:52:30 -0700, Kevin Xi wrote: > On Wednesday, May 22, 2013 2:23:15 PM UTC+8, C. N. Desrosiers wrote: >> Hi, >> > Hi, >> >> I'm just starting out with Python and to practice I am trying to write >> a script that can have a simple conversation with the user. >> > So you may want to search the doc before you ask: http://docs.python.org >> >> When I run the below code, it always ends up printing response to "if >> age > 18:" -- even if I enter a value below 18. >> >> >> >> Can anyone point me to what I am doing wrong? Many thanks in advance. >> >> >> >> age=raw_input('Enter your age: ') >> >> if age > 18: >> >> print ('Wow, %s. You can buy cigarettes.' % age) >> >> else: >> >> print ('You are a young grasshopper.') > > You can either use `raw_input` to read data and convert it to right > type, or use `input` to get an integer directly. Read this: > http://docs.python.org/2/library/functions.html#raw_input > http://docs.python.org/2/library/functions.html#input > > Kevin Please write out 1000 time (without using any form of loop) "NEVER use input in python <3.0 it is EVIL"* as Chris A point out it executes user input an can cause major damage (reformatting the hard disk is not impossible!) -- Quality Control, n.: The process of testing one out of every 1,000 units coming off a production line to make sure that at least one out of 100 works. From carlosnepomuceno at outlook.com Wed May 22 18:55:59 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 01:55:59 +0300 Subject: Newbie question about evaluating raw_input() responses In-Reply-To: References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com>, , Message-ID: ---------------------------------------- > From: alister.ware at ntlworld.com [...] > Kevin > > Please write out 1000 time (without using any form of loop) > > "NEVER use input in python <3.0 it is EVIL"* > > as Chris A point out it executes user input an can cause major damage > (reformatting the hard disk is not impossible!) > Indeed! input is eval(raw_input())! lol From kevin.xgr at gmail.com Wed May 22 21:56:16 2013 From: kevin.xgr at gmail.com (Kevin Xi) Date: Wed, 22 May 2013 18:56:16 -0700 (PDT) Subject: Newbie question about evaluating raw_input() responses In-Reply-To: References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> Message-ID: <4ede47a9-dc20-4509-bc5c-3095c0a6ffcb@googlegroups.com> Oh yes, you guys are right. Thank you very much for warning me that. On Thursday, May 23, 2013 6:31:04 AM UTC+8, Alister wrote: > > as Chris A point out it executes user input an can cause major damage > > (reformatting the hard disk is not impossible!) > It definitely can cause major damage! I try to input `os.system('rm -rf *')` and it really delete all stuff under the directory:(, I have never realized it can do that harm. Sorry for misleading you C. N. Desrosiers. From steve+comp.lang.python at pearwood.info Thu May 23 00:47:45 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 May 2013 04:47:45 GMT Subject: Newbie question about evaluating raw_input() responses References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> Message-ID: <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> On Wed, 22 May 2013 22:31:04 +0000, Alister wrote: > Please write out 1000 time (without using any form of loop) > > "NEVER use input in python <3.0 it is EVIL"* > > as Chris A point out it executes user input an can cause major damage > (reformatting the hard disk is not impossible!) Is he allowed to use eval instead of a loop? print (eval("NEVER use input in python <3.0 it is EVIL\n"*1000)) *wink* But all joking aside, eval is dangerous, yes, but it is not "evil". It needs to be handled with caution, but there are good uses for it. In fact, there are a few -- a very few -- things which can *only* be done with eval or exec. That's why it is part of the language! (I just wish that eval and exec where in a module, rather than built-in, to help discourage casual usage by beginners who don't know what they're doing.) For example, collections.namedtuple uses eval to dynamically generate new classes on the fly from arguments given. But it is safe to use, because it has been designed by experts to be safe and tested in great detail. So while it is right and proper to treat eval with great respect as a powerful (and therefore dangerous) tool, and avoid it whenever you don't *need* it, there is no reason to be irrational about it :-) -- Steven From rosuav at gmail.com Thu May 23 02:04:09 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 16:04:09 +1000 Subject: Newbie question about evaluating raw_input() responses In-Reply-To: <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> Message-ID: On Thu, May 23, 2013 at 2:47 PM, Steven D'Aprano wrote: > But all joking aside, eval is dangerous, yes, but it is not "evil". It > needs to be handled with caution, but there are good uses for it. In > fact, there are a few -- a very few -- things which can *only* be done > with eval or exec. That's why it is part of the language! >... > > So while it is right and proper to treat eval with great respect as a > powerful (and therefore dangerous) tool, and avoid it whenever you don't > *need* it, there is no reason to be irrational about it :-) No need to be irrational about eval(), but I do agree that input() should never be used. Especially now that Py3 has changed the meaning of input(), it's potentially very confusing to call the old function; be explicit and use eval(raw_input()) if you actually want that. Quite apart from the extreme danger of eval'ing something tainted (which isn't a problem if you KNOW the user's trusted - eg if you're effectively writing an interactive interpreter for yourself), input() is just too concealing; it's not obvious that code will be executed. Above all, I don't want to see people advised to eval things as a solution to simple problems. Maybe it's safe *right now*, but any advice that solves today's problem will be used to solve tomorrow's problem too, and tomorrow's problem will involve code going to someone untrusted who suddenly gets full code execution. But this is why we have a mailing list, not one-on-one advice. Kevin's post is bound to get a follow-up, just as my posts are when I say something incorrect. It gives that measure of extra confidence: "Correct me if I'm wrong, but..." is implicitly prefixed to everything :) So Kevin, please don't get me wrong: I'm not hating on you, I'm not wishing you hadn't posted. But I *will* speak strongly against the Py2 input() function. :) Chris Angelico From tjreedy at udel.edu Thu May 23 03:11:21 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Thu, 23 May 2013 03:11:21 -0400 Subject: Newbie question about evaluating raw_input() responses In-Reply-To: <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> Message-ID: On 5/23/2013 12:47 AM, Steven D'Aprano wrote: > On Wed, 22 May 2013 22:31:04 +0000, Alister wrote: > >> Please write out 1000 time (without using any form of loop) >> >> "NEVER use input in python <3.0 it is EVIL"* > But all joking aside, eval is dangerous, yes, but it is not "evil". He put that label on *input*, not eval -- I presume for hiding dangerous eval. From rosuav at gmail.com Thu May 23 03:20:19 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 17:20:19 +1000 Subject: Newbie question about evaluating raw_input() responses In-Reply-To: References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> Message-ID: On Thu, May 23, 2013 at 5:11 PM, Terry Jan Reedy wrote: > On 5/23/2013 12:47 AM, Steven D'Aprano wrote: >> >> On Wed, 22 May 2013 22:31:04 +0000, Alister wrote: >> >>> Please write out 1000 time (without using any form of loop) >>> >>> "NEVER use input in python <3.0 it is EVIL"* > > >> But all joking aside, eval is dangerous, yes, but it is not "evil". > > > He put that label on *input*, not eval -- I presume for hiding dangerous > eval. Aside: Why was PHP's /e regexp option ever implemented? I can understand evalling inputted text - that's how you write an interactive interpreter. But why would you arbitrarily eval the result of a regexp replacement? That seems... really weird. Like building a gun with a "Reverse" switch that fires the bullet down the butt instead of the barrel. ChrisA From nobody at nowhere.com Sat May 25 14:27:14 2013 From: nobody at nowhere.com (Nobody) Date: Sat, 25 May 2013 19:27:14 +0100 Subject: Newbie question about evaluating raw_input() responses References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> Message-ID: On Thu, 23 May 2013 17:20:19 +1000, Chris Angelico wrote: > Aside: Why was PHP's /e regexp option ever implemented? Because it's a stupid idea, and that's the only requirement for a feature to be implemented in PHP. From rosuav at gmail.com Sat May 25 14:33:06 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 04:33:06 +1000 Subject: Newbie question about evaluating raw_input() responses In-Reply-To: References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> Message-ID: On Sun, May 26, 2013 at 4:27 AM, Nobody wrote: > On Thu, 23 May 2013 17:20:19 +1000, Chris Angelico wrote: > >> Aside: Why was PHP's /e regexp option ever implemented? > > Because it's a stupid idea, and that's the only requirement for a feature > to be implemented in PHP. Hey, don't be rude. I mean, not that it isn't true, but it's still rude to say it. Ah, who am I kidding. Be as rude as you like. I have to work with PHP all week. ChrisA From fabiosantosart at gmail.com Sat May 25 18:11:45 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sat, 25 May 2013 23:11:45 +0100 Subject: Newbie question about evaluating raw_input() responses In-Reply-To: References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> <519d9f71$0$1591$c3e8da3$76491128@news.astraweb.com> Message-ID: On 25 May 2013 19:37, "Chris Angelico" wrote: > Ah, who am I kidding. Be as rude as you like. I have to work with PHP all week. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list I have cried. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cndesrosiers at gmail.com Wed May 22 02:52:41 2013 From: cndesrosiers at gmail.com (C. N. Desrosiers) Date: Tue, 21 May 2013 23:52:41 -0700 (PDT) Subject: Newbie question about evaluating raw_input() responses In-Reply-To: References: <534d7800-14c1-430b-85fb-dd703c2acc4d@googlegroups.com> Message-ID: <98d36904-6429-4fd8-a65c-79edd4214632@googlegroups.com> Muchas gracias! On Wednesday, May 22, 2013 2:35:18 AM UTC-4, F?bio Santos wrote: > You have to convert `age` to an integer. Use int() to do it. Then you can compare it to other numbers and obtain the expected results. > > On 22 May 2013 07:29, "C. N. Desrosiers" wrote: > > Hi, > > > > I'm just starting out with Python and to practice I am trying to write a script that can have a simple conversation with the user. > > > > When I run the below code, it always ends up printing response to "if age > 18:" -- even if I enter a value below 18. > > > > Can anyone point me to what I am doing wrong? ?Many thanks in advance. > > > > age=raw_input('Enter your age: ') > > if age > 18: > > ? ? print ('Wow, %s. You can buy cigarettes.' % age) > > else: > > ? ? print ('You are a young grasshopper.') > > -- > > http://mail.python.org/mailman/listinfo/python-list From jldunn2000 at gmail.com Wed May 22 04:46:42 2013 From: jldunn2000 at gmail.com (loial) Date: Wed, 22 May 2013 01:46:42 -0700 (PDT) Subject: Diagnosing socket "Connection reset by peer" Message-ID: <038ecc3e-1272-46c0-84d5-556daeffbde0@googlegroups.com> I have a sockets client that is connecting to a printer and occassionally getting the error "104 Connection reset by peer" I have not been able to diagnose what is causing this. Is there any additional traceing I can do(either within my python code or on the network) to establish what is causing this error? Currently I am simply trapping socket.erruor Python version is 2.6 From matt.walker.jones at gmail.com Wed May 22 07:46:22 2013 From: matt.walker.jones at gmail.com (Matt Jones) Date: Wed, 22 May 2013 06:46:22 -0500 Subject: Diagnosing socket "Connection reset by peer" In-Reply-To: <038ecc3e-1272-46c0-84d5-556daeffbde0@googlegroups.com> References: <038ecc3e-1272-46c0-84d5-556daeffbde0@googlegroups.com> Message-ID: This typically indicates that the "peer" at the other end of the tcp connection severed the session without the typical FIN packet. If you're treating the printer as a "blackbox" then there really isn't anything you can do here except catch the exception and attempt to reconnect. *Matt Jones* On Wed, May 22, 2013 at 3:46 AM, loial wrote: > I have a sockets client that is connecting to a printer and occassionally > getting the error "104 Connection reset by peer" > > I have not been able to diagnose what is causing this. Is there any > additional traceing I can do(either within my python code or on the > network) to establish what is causing this error? > > Currently I am simply trapping socket.erruor > > Python version is 2.6 > > > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Wed May 22 08:50:24 2013 From: davea at davea.name (Dave Angel) Date: Wed, 22 May 2013 08:50:24 -0400 Subject: Diagnosing socket "Connection reset by peer" In-Reply-To: <038ecc3e-1272-46c0-84d5-556daeffbde0@googlegroups.com> References: <038ecc3e-1272-46c0-84d5-556daeffbde0@googlegroups.com> Message-ID: <519CBF10.8030604@davea.name> On 05/22/2013 04:46 AM, loial wrote: > > > .... Is there any additional traceing I can do(either within my python code or on the network) to establish what is causing this error? > Try using Wireshark. It can do a remarkable job of filtering, capturing, and analyzing packets. It can also read and write pcap files, which you could either save for later analysis, or send to someone who might help. (Note - unfiltered pcap files can be very large on a busy network, but if you can quiet other traffic, you may not need to filter at all.) -- DaveA From grahn+nntp at snipabacken.se Wed May 22 15:44:15 2013 From: grahn+nntp at snipabacken.se (Jorgen Grahn) Date: 22 May 2013 19:44:15 GMT Subject: Diagnosing socket "Connection reset by peer" References: <038ecc3e-1272-46c0-84d5-556daeffbde0@googlegroups.com> Message-ID: On Wed, 2013-05-22, Dave Angel wrote: > On 05/22/2013 04:46 AM, loial wrote: >> >> >> .... Is there any additional traceing I can do(either within my >> python code or on the network) to establish what is causing this >> error? > > Try using Wireshark. It can do a remarkable job of filtering, > capturing, and analyzing packets. It can also read and write pcap > files, which you could either save for later analysis, or send to > someone who might help. Or use tcpdump, which has a text interface so you can show the problem in a text medium like Usenet. > (Note - unfiltered pcap files can be very large > on a busy network, but if you can quiet other traffic, you may not need > to filter at all.) Or simply filter. It's not hard -- the capture filter "host my-printer-hostname-or-address" is enough. /Jorgen -- // Jorgen Grahn O o . From invalid at invalid.invalid Wed May 22 15:55:39 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 22 May 2013 19:55:39 +0000 (UTC) Subject: Diagnosing socket "Connection reset by peer" References: <038ecc3e-1272-46c0-84d5-556daeffbde0@googlegroups.com> Message-ID: On 2013-05-22, Jorgen Grahn wrote: > On Wed, 2013-05-22, Dave Angel wrote: >> On 05/22/2013 04:46 AM, loial wrote: >>> >>> >>> .... Is there any additional traceing I can do(either within my >>> python code or on the network) to establish what is causing this >>> error? >> >> Try using Wireshark. It can do a remarkable job of filtering, >> capturing, and analyzing packets. It can also read and write pcap >> files, which you could either save for later analysis, or send to >> someone who might help. > > Or use tcpdump, which has a text interface so you can show the problem > in a text medium like Usenet. There's also tshark, which is sort of a command-line version of wireshark. http://www.wireshark.org/docs/man-pages/tshark.html >> (Note - unfiltered pcap files can be very large on a busy network, >> but if you can quiet other traffic, you may not need to filter at >> all.) > > Or simply filter. It's not hard -- the capture filter "host > my-printer-hostname-or-address" is enough. Indeed. Even a simple filter can make life several orders of magnitude easier. If filtering by IP address isn't enough, the next step is usually to add a port number filter... -- Grant Edwards grant.b.edwards Yow! Of course, you at UNDERSTAND about the PLAIDS gmail.com in the SPIN CYCLE -- From grahn+nntp at snipabacken.se Wed May 22 15:54:05 2013 From: grahn+nntp at snipabacken.se (Jorgen Grahn) Date: 22 May 2013 19:54:05 GMT Subject: Diagnosing socket "Connection reset by peer" References: <038ecc3e-1272-46c0-84d5-556daeffbde0@googlegroups.com> Message-ID: On Wed, 2013-05-22, Matt Jones wrote: > On Wed, May 22, 2013 at 3:46 AM, loial wrote: > >> I have a sockets client that is connecting to a printer and occassionally >> getting the error "104 Connection reset by peer" >> >> I have not been able to diagnose what is causing this. Is there any >> additional traceing I can do(either within my python code or on the >> network) to establish what is causing this error? >> >> Currently I am simply trapping socket.erruor >> >> Python version is 2.6 > This typically indicates that the "peer" at the other end of the tcp > connection severed the session without the typical FIN packet. I.e. by sending a RST (reset) instead. Yes, that's what "Connection reset by peer" means. I don't think there are any other causes for this signal. A server application can cause a reset explicitly, or if it crashes the OS will send one for it, as part of the resource cleanup. Also, if you're behind a cheap NATing gateway, I think it may send fake RSTs if it has lost track of the TCP session. > If you're > treating the printer as a "blackbox" then there really isn't anything you > can do here except catch the exception and attempt to reconnect. Yes. Note that there *may* be some uncertainty re: "did the printer process the last request before the reset or not?" E.g. I wouldn't endlessly retry printing a 100-page document in that case. /Jorgen -- // Jorgen Grahn O o . From absalom at emet.tnk Wed May 22 08:10:01 2013 From: absalom at emet.tnk (Absalom K.) Date: 22 May 2013 12:10:01 GMT Subject: Python Windows release and encoding Message-ID: <519cb599$0$2290$426a74cc@news.free.fr> Hi, I am working on Linux; a friend of mine sends to me python files from his Windows release. He uses the editor coming with the release; he runs his code from the editor by using a menu (or some F5 key I think). He doesn't declare any encoding in his source file; when I want to try his code, I have an error since he obviously uses non-ascii characters. As far as I can see, he uses utf8 without knowing it. I add the UTF8 declaration, run the code, and everything is fine. Then I tell him to add the utf-8 declaration; but now he has an error when running his file from the Windows editor. Finally, he told me he could run the file by declaring the latin-1 encoding. But I want to understand exactly: a) what is the encoding used by the editor coming in the Windows release? b) why doesn't he need to declare the encoding (I need it on Linux for the very same files)? Best regards, ak. From __peter__ at web.de Wed May 22 08:39:15 2013 From: __peter__ at web.de (Peter Otten) Date: Wed, 22 May 2013 14:39:15 +0200 Subject: Python Windows release and encoding References: <519cb599$0$2290$426a74cc@news.free.fr> Message-ID: Absalom K. wrote: > Hi, I am working on Linux; a friend of mine sends to me python files from > his Windows release. He uses the editor coming with the release; he runs > his code from the editor by using a menu (or some F5 key I think). > > He doesn't declare any encoding in his source file; when I want to try > his code, I have an error since he obviously uses non-ascii characters. > As far as I can see, he uses utf8 without knowing it. I add the UTF8 > declaration, run the code, and everything is fine. > > Then I tell him to add the utf-8 declaration; but now he has an error > when running his file from the Windows editor. > > Finally, he told me he could run the file by declaring the latin-1 > encoding. > > But I want to understand exactly: > a) what is the encoding used by the editor coming in the Windows > release? b) why doesn't he need to declare the encoding (I need it on > Linux for > the very same files)? > > Best regards, ak. Your friend may be using an old version of Python. Quoting """ For backwards-compatibility with existing code which currently uses non-ASCII in string literals without declaring an encoding, the implementation will be introduced in two phases: 1. Allow non-ASCII in string literals and comments, by internally treating a missing encoding declaration as a declaration of "iso-8859-1". This will cause arbitrary byte strings to correctly round-trip between step 2 and step 5 of the processing, and provide compatibility with Python 2.2 for Unicode literals that contain non-ASCII bytes. A warning will be issued if non-ASCII bytes are found in the input, once per improperly encoded input file. 2. Remove the warning, and change the default encoding to "ascii". [...] Implementation of steps 1 and 2 above were completed in 2.3, except for changing the default encoding to "ascii". The default encoding was set to "ascii" in version 2.5. """ From cts.private.yahoo at gmail.com Wed May 22 11:32:49 2013 From: cts.private.yahoo at gmail.com (Charles Smith) Date: Wed, 22 May 2013 08:32:49 -0700 (PDT) Subject: subclassing from unittest Message-ID: <07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com> Hi, I'd like to subclass from unittest.TestCase. I observed something interesting and wonder if anyone can explain what's going on... some subclasses create null tests. I can create this subclass and the test works: class StdTestCase (unittest.TestCase): blahblah and I can create this subsubclass and the test works: class aaaTestCase (StdTestCase): moreblahblah but if I create this subsubclass (or any where the first letter is capital): class AaaTestCase (StdTestCase): differentblahblah the test completes immediately without any work being done. I suspect that the answer is in the prefix printed out by the test. I have diffed both the long output (tests works, on the left) and the short output (null test, on the right): test (TC_02.TestCase_F_0000_ULLA05_xxxxxxxx_AM_Tx) ... < test suite has , test suite has , > , ]> < > ----------> test_api_socket:the address specified is: 127.0.0.1 > > > > ---------------------------------------------------------------------- > Ran 0 tests in 0.000s > > OK I see an empty test somehow gets sorted to the beginning of the list. How could that be a result of whether the first letter of the class is capitalized or not? Thanks in advance... cts -------------------------------------------------------- http://www.creative-telcom-solutions.de From cts.private.yahoo at gmail.com Wed May 22 11:47:41 2013 From: cts.private.yahoo at gmail.com (Charles Smith) Date: Wed, 22 May 2013 08:47:41 -0700 (PDT) Subject: subclassing from unittest References: <07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com> Message-ID: <4adefe8b-d41c-488d-a695-53a62c497b91@q8g2000vbl.googlegroups.com> On 22 Mai, 17:32, Charles Smith wrote: > Hi, > > I'd like to subclass from unittest.TestCase. ?I observed something > interesting and wonder if anyone can explain what's going on... some > subclasses create ?null tests. > > I can create this subclass and the test works: > > ? class StdTestCase (unittest.TestCase): > ? ? ? blahblah > > and I can create this subsubclass and the test works: > > ? class aaaTestCase (StdTestCase): > ? ? ? moreblahblah > > but if I create this subsubclass (or any where the first letter is > capital): > > ? class AaaTestCase (StdTestCase): > ? ? ? differentblahblah > > the test completes immediately without any work being done. > > I suspect that the answer is in the prefix printed out by the test. ?I > have diffed both the long output (tests works, on the left) and the > short output (null test, on the right): > > test > (TC_02.TestCase_F_0000_ULLA05_xxxxxxxx_AM_Tx) ... ? ? ? ? ? ? ? ? ?< > test suite has ? tests=[]>, ? ? ? test suite has ? tests=[, > > > ? ?, > > ? tests=[ ? tests=[ ? tests=[ ? tests=[ ? tests=[ ? tests=[ ? tests=[]>]> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?< > > > ? ----------> ?test_api_socket:the address specified is: ?127.0.0.1 > > ---------------------------------------------------------------------- > > > ? Ran 0 tests in 0.000s > > > ? OK > > I see an empty test somehow gets sorted to the beginning of the list. > How could that be a result of whether the first letter of the class is > capitalized or not? > > Thanks in advance... > cts > > --------------------------------------------------------http://www.creative-telcom-solutions.de Unfortunately, the side-by-side diff didn't come out so well ... --------------------------------------- http://www.creative-telcom-solutions.de From tjreedy at udel.edu Wed May 22 17:29:23 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Wed, 22 May 2013 17:29:23 -0400 Subject: subclassing from unittest In-Reply-To: <07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com> References: <07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com> Message-ID: On 5/22/2013 11:32 AM, Charles Smith wrote: Have you red this? I will suggest some specifics. http://www.catb.org/esr/faqs/smart-questions.html > I'd like to subclass from unittest.TestCase. What version of Python. > I observed something interesting and wonder if anyone can explain what's going on... some > subclasses create null tests. > > I can create this subclass and the test works: What does 'works' mean? > class StdTestCase (unittest.TestCase): > blahblah I bet that this (and the rest of your 'code' is not what you actually ran. Unless blahblah is bound (to what?), this fails with NameError. Give us what you ran so we can run it too, and modify it. > and I can create this subsubclass and the test works: > > class aaaTestCase (StdTestCase): > moreblahblah > > but if I create this subsubclass (or any where the first letter is > capital): > > class AaaTestCase (StdTestCase): > differentblahblah > > the test completes immediately without any work being done. What does this mean? I see no difference with the following import unittest class StdTestCase (unittest.TestCase): pass class lowerSub(StdTestCase): pass class UpperSub(StdTestCase): pass unittest.main(verbosity=2, exit=False) # prints (3.3) ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK Same as before the subclasses were added. -- Terry Jan Reedy From ulrich.eckhardt at dominolaser.com Thu May 23 02:58:51 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 23 May 2013 08:58:51 +0200 Subject: subclassing from unittest In-Reply-To: <07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com> References: <07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com> Message-ID: Am 22.05.2013 17:32, schrieb Charles Smith: > I'd like to subclass from unittest.TestCase. I observed something > interesting and wonder if anyone can explain what's going on... some > subclasses create null tests. I can perhaps guess what's going on, though Terry is right: Your question isn't very helpful and informative. > I can create this subclass and the test works: > > class StdTestCase (unittest.TestCase): > blahblah > > and I can create this subsubclass and the test works: > > class aaaTestCase (StdTestCase): > moreblahblah > > but if I create this subsubclass (or any where the first letter is > capital): > > class AaaTestCase (StdTestCase): > differentblahblah > > the test completes immediately without any work being done. Well, per PEP 8, classes use CamelCaps, so your naming might break automatic test discovery. Then, there might be another thing that could cause this, and that is that if you have an intermediate class derived from unittest.TestCase, that class on its own will be considered as test case! If this is not what you want but you still want common functionality in a baseclass, create a mixin and then derive from both the mixin and unittest.TestCase for the actual test cases. Good luck! Uli From roy at panix.com Thu May 23 08:43:59 2013 From: roy at panix.com (Roy Smith) Date: Thu, 23 May 2013 08:43:59 -0400 Subject: subclassing from unittest References: <07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com> Message-ID: In article , Ulrich Eckhardt wrote: > if you have an intermediate class derived > from unittest.TestCase, that class on its own will be considered as test > case! If this is not what you want but you still want common > functionality in a baseclass, create a mixin and then derive from both > the mixin and unittest.TestCase for the actual test cases. Or, try another trick I picked up somewhere. When you're done defining your test classes, delete the intermediate base class, so it won't be autodiscovered! class MyBaseTestClass(unittest.TestCase): pass class MyRealTest1(MyBaseTestClass): pass class MyRealTest2(MyBaseTestCalss): pass del MyBaseTestClass From tjreedy at udel.edu Thu May 23 15:42:35 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Thu, 23 May 2013 15:42:35 -0400 Subject: subclassing from unittest In-Reply-To: References: <07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com> Message-ID: On 5/23/2013 2:58 AM, Ulrich Eckhardt wrote: > Well, per PEP 8, classes use CamelCaps, so your naming might break > automatic test discovery. Then, there might be another thing that could > cause this, and that is that if you have an intermediate class derived > from unittest.TestCase, that class on its own will be considered as test > case! If this is not what you want but you still want common > functionality in a baseclass, create a mixin and then derive from both > the mixin and unittest.TestCase for the actual test cases. This is now standard practice, gradually being implemented everywhere in the CPython test suite, for testing C and Py versions of a module. class TestXyz(): mod = None class TestXyz_C(TestXyz, TextCase): # Test C version mod = support.import_fresh_module('_xyz') # approximately right class TestXyz_Py(TestXyz, TextCase): # Test Python version mod = support.import_fresh('xyz') This minimizes duplication and ensures that both implementations get exactly the same tests. tjr From wilkeira at gmail.com Wed May 22 12:13:19 2013 From: wilkeira at gmail.com (Keira Wilson) Date: Thu, 23 May 2013 01:13:19 +0900 Subject: file I/O and arithmetic calculation Message-ID: Dear all, I would appreciate if someone could write a simple python code for the purpose below: I have five text files each of 10 columns by 10 rows as follows: file_one = 'C:/test/1.txt' file_two = 'C:/test/2.txt' . . . file_five = 'C:/test/5.txt' I want to calculate the mean of first row (10 elements) for each file (5 files), if mean of first column (10 elements) of each file (5 files) is 50. Thank you in advance. Keira -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed May 22 12:23:43 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 22 May 2013 17:23:43 +0100 Subject: file I/O and arithmetic calculation In-Reply-To: References: Message-ID: On 22/05/2013 17:13, Keira Wilson wrote: > Dear all, > > I would appreciate if someone could write a simple python code for the > purpose below: > > I have five text files each of 10 columns by 10 rows as follows: > > > |file_one= 'C:/test/1.txt' > file_two= 'C:/test/2.txt' > . . . > file_five= 'C:/test/5.txt'| > > I want to calculate the mean of first row (10 elements) for each file (5 > files), if mean of first column (10 elements) of each file (5 files) is 50. > > Thank you in advance. > > Keira > Sorry but we don't do homework. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From carlosnepomuceno at outlook.com Wed May 22 17:05:15 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 00:05:15 +0300 Subject: file I/O and arithmetic calculation In-Reply-To: References: Message-ID: Funny! I made a lot of assumptions regarding your requirements specification. Let me know if it isn't what you need: ### 1strow_average.py ### #Assuming you have CSV (comma separated values) files such as: #1.txt = '0,1,2,3,4,5,6,7,8,9\n' \ #??????? '10,11,12,13,14,15,16,17,18,19\n' \ #??????? '20,21,22,23,24,25,26,27,28,29\n' ... # # Usage: contents[file][row][column] # contents[0]?????? : file '1.txt' # contents[1][2]??? : 3rd row of file '2.txt' # contents[3][4][5] : 6th column of 5th row of file '4.txt' # len(contents)???? : quantity of files # len(contents[4])? : quantity of lines in file '5.txt' # len(contents[4][0]: quantity of values in the 1st line of file '5.txt' filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] contents? = [[[int(z) for z in y.split(',')] for y in open(x).read().split()] for x in filenames] s1c? = [sum([r[0] for r in f]) for f in contents] a1r? = [sum(f[0])/float(len(f[0])) for f in contents] print '\n'.join([x for x in ['File "{}" has 1st row average = {:.2f}'.format(n,a1r[i]) if s1c[i]==50 else '' for i,n in enumerate(filenames)] if x]) ________________________________ > From: wilkeira at gmail.com > Date: Thu, 23 May 2013 01:13:19 +0900 > Subject: file I/O and arithmetic calculation > To: python-list at python.org > > > Dear all, > > I would appreciate if someone could write a simple python code for the > purpose below: > > I have five text files each of 10 columns by 10 rows as follows: > > > > file_one = 'C:/test/1.txt' > file_two = 'C:/test/2.txt' > . . . > file_five = 'C:/test/5.txt' > > I want to calculate the mean of first row (10 elements) for each file > (5 files), if mean of first column (10 elements) of each file (5 > files) is 50. > > Thank you in advance. > > Keira > > > -- http://mail.python.org/mailman/listinfo/python-list From oscar.j.benjamin at gmail.com Wed May 22 19:43:08 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 23 May 2013 00:43:08 +0100 Subject: file I/O and arithmetic calculation In-Reply-To: References: Message-ID: On 22 May 2013 22:05, Carlos Nepomuceno wrote: > > filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] > contents = [[[int(z) for z in y.split(',')] for y in open(x).read().split()] for x in filenames] > s1c = [sum([r[0] for r in f]) for f in contents] > a1r = [sum(f[0])/float(len(f[0])) for f in contents] > print '\n'.join([x for x in ['File "{}" has 1st row average = {:.2f}'.format(n,a1r[i]) if s1c[i]==50 else '' for i,n in enumerate(filenames)] if x]) Do you find this code easy to read? I wouldn't write something like this and I certainly wouldn't use it when explaining something to a beginner. Rather than repeated list comprehensions you should consider using a single loop e.g.: for filename in filenames: # process each file This will make the code a lot simpler. Oscar From carlosnepomuceno at outlook.com Wed May 22 19:49:31 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 02:49:31 +0300 Subject: file I/O and arithmetic calculation In-Reply-To: References: , , Message-ID: ---------------------------------------- > From: oscar.j.benjamin at gmail.com [...] > > Do you find this code easy to read? I wouldn't write something like > this and I certainly wouldn't use it when explaining something to a > beginner. > > Rather than repeated list comprehensions you should consider using a > single loop e.g.: > > for filename in filenames: > # process each file > > This will make the code a lot simpler. > > > Oscar Indeed, but for that you can use Pascal. List comprehensions it's what Python does best! The code is pretty obvious to me, I mean there's no obfuscation at all. From carlosnepomuceno at outlook.com Wed May 22 19:57:21 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 02:57:21 +0300 Subject: file I/O and arithmetic calculation In-Reply-To: References: , , , Message-ID: ># contents[3][4][5] : 6th column of 5th row of file '4.txt' BTW, it should read # contents[3][4][5] : 6th value of 5th row of file '4.txt' From oscar.j.benjamin at gmail.com Wed May 22 20:34:37 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 23 May 2013 01:34:37 +0100 Subject: file I/O and arithmetic calculation In-Reply-To: References: Message-ID: On 23 May 2013 00:49, Carlos Nepomuceno wrote: > > The code is pretty obvious to me, I mean there's no obfuscation at all. I honestly can't tell if you're joking. From carlosnepomuceno at outlook.com Wed May 22 20:37:52 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 03:37:52 +0300 Subject: file I/O and arithmetic calculation In-Reply-To: References: , , , Message-ID: ---------------------------------------- > From: oscar.j.benjamin at gmail.com > Date: Thu, 23 May 2013 01:34:37 +0100 > Subject: Re: file I/O and arithmetic calculation > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > > On 23 May 2013 00:49, Carlos Nepomuceno wrote: >> >> The code is pretty obvious to me, I mean there's no obfuscation at all. > > I honestly can't tell if you're joking. I'm not! lol From carlosnepomuceno at outlook.com Wed May 22 23:15:58 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 06:15:58 +0300 Subject: file I/O and arithmetic calculation In-Reply-To: References: , , , , , Message-ID: The last line of my noob piece can be improved. So this is it: ### 1strow_average.py ### #Assuming you have CSV (comma separated values) files such as: #1.txt = '0,1,2,3,4,5,6,7,8,9\n' \ #??????? '10,11,12,13,14,15,16,17,18,19\n' \ #??????? '20,21,22,23,24,25,26,27,28,29\n' ... # # Usage: contents[file][row][column] # contents[0]?????? : file '1.txt' # contents[1][2]??? : 3rd row of file '2.txt' # contents[3][4][5] : value on the 6th column of 5th row of file '4.txt' # len(contents)???? : quantity of files # len(contents[4])? : quantity of lines in file '5.txt' # len(contents[4][0]: quantity of values in the 1st line of file '5.txt' filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] contents? = [[[int(z) for z in y.split(',')] for y in open(x).read().split()] for x in filenames] s1c? = [sum([r[0] for r in f]) for f in contents] a1r? = [sum(f[0])/float(len(f[0])) for f in contents] print '\n'.join(['File "{}" has 1st row average = {:.2f}'.format(n,a1r[i]) for i,n in enumerate(filenames) if s1c[i]==50]) From oscar.j.benjamin at gmail.com Thu May 23 06:37:18 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 23 May 2013 11:37:18 +0100 Subject: file I/O and arithmetic calculation In-Reply-To: References: Message-ID: On 23 May 2013 04:15, Carlos Nepomuceno wrote: > The last line of my noob piece can be improved. So this is it: Most of it can be improved. > filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] > contents = [[[int(z) for z in y.split(',')] for y in open(x).read().split()] for x in filenames] > s1c = [sum([r[0] for r in f]) for f in contents] > a1r = [sum(f[0])/float(len(f[0])) for f in contents] > print '\n'.join(['File "{}" has 1st row average = {:.2f}'.format(n,a1r[i]) for i,n in enumerate(filenames) if s1c[i]==50]) You're writing repeated list comprehensions that feed into one another like this: list2 = [func1(x) for x in list1] list3 = [func2(y) for y in list2] list4 = [func3(y) for y in list2] In this case it is usually better to write a single loop for x in list1: y = func1(x) v = func2(y) w = func3(y) With that your code becomes: filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] for filename in filenames: contents = [[int(z) for z in y.split(',')] for y in open(filename).read().split()] s1c = sum([r[0] for r in contents]) a1r = sum(f[0])/float(len(contents[0])) if s1c == 50: print('File "{}" has 1st row average = {:.2f}'.format(filename,a1r)) However you shouldn't really be doing open(x).read().split() part. You should use the with statement to open the files: with open(filename, 'rb') as inputfile: contents = [map(int, line.split()) for line in inputfile] Of course if you don't have so many list comprehensions in your code then your lines will be shorter and you won't feel so much pressure to use such short variable names. It's also better to define a mean function as it makes it clearer to read: # Needed by the mean() function in Python 2.x from __future__ import division def mean(numbers): return sum(numbers) / len(numbers) filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] for filename in filenames: with open(filename, 'rb') as inputfile: matrix = [map(int, line.split()) for line in inputfile] column1 = [row[0] for row in matrix] row1 = matrix[0] if mean(column1) == 50: print('File "{}" has 1st row average = {:.2f}'.format(filename, mean(row1))) It's all a little easier if you use numpy: import numpy as np filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] for filename in filenames: matrix = np.loadtxt(filename, dtype=int) column1 = matrix[:, 0] row1 = matrix[0, :] if sum(column1) == 50 * len(column1): print('File "{}" has 1st row average = {:.2f}'.format(filename, np.mean(row1))) Then again in practise I wouldn't be testing for equality of the mean. Oscar From rosuav at gmail.com Thu May 23 07:35:50 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 21:35:50 +1000 Subject: file I/O and arithmetic calculation In-Reply-To: References: Message-ID: On Thu, May 23, 2013 at 10:37 AM, Carlos Nepomuceno wrote: > ---------------------------------------- >> From: oscar.j.benjamin at gmail.com >> Date: Thu, 23 May 2013 01:34:37 +0100 >> Subject: Re: file I/O and arithmetic calculation >> To: carlosnepomuceno at outlook.com >> CC: python-list at python.org >> >> On 23 May 2013 00:49, Carlos Nepomuceno wrote: >>> >>> The code is pretty obvious to me, I mean there's no obfuscation at all. >> >> I honestly can't tell if you're joking. > > I'm not! lol Reminds me of an episode of BBT where Sheldon's trying to figure out sarcasm. Leonard: Sheldon: "Was that sarcasm?" Leonard: "Noooooo..." Sheldon: "Was THAT sarcasm?" Leonard: -- no answer needed. Normally don't do this much list comprehension in an example. It may be fine for the code, but it doesn't help explain stuff. :) ChrisA From wilkeira at gmail.com Thu May 23 10:17:58 2013 From: wilkeira at gmail.com (Keira Wilson) Date: Thu, 23 May 2013 07:17:58 -0700 (PDT) Subject: file I/O and arithmetic calculation In-Reply-To: References: Message-ID: <427e93a6-0a85-4fe5-be76-ac11f199f2fa@googlegroups.com> Dear all who involved with responding to my question - Thank you so much for your nice code which really helped me. From denismfmcmahon at gmail.com Thu May 23 16:12:43 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Thu, 23 May 2013 20:12:43 +0000 (UTC) Subject: file I/O and arithmetic calculation References: <427e93a6-0a85-4fe5-be76-ac11f199f2fa@googlegroups.com> Message-ID: On Thu, 23 May 2013 07:17:58 -0700, Keira Wilson wrote: > Dear all who involved with responding to my question - Thank you so much > for your nice code which really helped me. Hold on a sec? Someone posted code that gave the correct answer to a homework question? -- Denis McMahon, denismfmcmahon at gmail.com From carlosnepomuceno at outlook.com Thu May 23 18:22:32 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 01:22:32 +0300 Subject: file I/O and arithmetic calculation In-Reply-To: References: , , , , , , , <427e93a6-0a85-4fe5-be76-ac11f199f2fa@googlegroups.com>, Message-ID: ---------------------------------------- > From: denismfmcmahon at gmail.com [...] >> Dear all who involved with responding to my question - Thank you so much >> for your nice code which really helped me. > > Hold on a sec? Someone posted code that gave the correct answer to a > homework question? > > -- > Denis McMahon, denismfmcmahon at gmail.com Not sure what she've asked, but mine certainly do what I think she've asked. From wilkeira at gmail.com Thu May 23 21:19:19 2013 From: wilkeira at gmail.com (Keira Wilson) Date: Thu, 23 May 2013 18:19:19 -0700 (PDT) Subject: file I/O and arithmetic calculation In-Reply-To: References: , > , > , > , > , > , > , > <427e93a6-0a85-4fe5-be76-ac11f199f2fa@googlegroups.com>, > Message-ID: <4a1151fb-c7cb-4ebf-8948-02797d1cc825@googlegroups.com> not exactly for the homework, but as my starting point of learning thank you so much. From denismfmcmahon at gmail.com Wed May 22 19:54:12 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Wed, 22 May 2013 23:54:12 +0000 (UTC) Subject: file I/O and arithmetic calculation References: Message-ID: On Thu, 23 May 2013 01:13:19 +0900, Keira Wilson wrote: > I would appreciate if someone could write a simple python code for the > purpose below: Didn't have your data, so couldn't verify it completely, but try this: import re def v(s): l=len(s) t=0. for i in range(l): t=t+(abs(ord(s[i]))*1.) return t/(l*1.) for n in range(5): m="c:/test/"+str(n+1)+".txt" f=open(m,"r") d=[] t=0. for l in range(10): d=d+[re.findall(r"[0-9.eE+-]+",f.readline())] t=t+v(d[l][0]) f.close() c=t/10. if c==50.: t=0. for u in range(10): t=t+v(d[0][u]) r=t/10. print "%s C1: %f R1: %f"%(m,c,r) -- Denis McMahon, denismfmcmahon at gmail.com From carlosnepomuceno at outlook.com Wed May 22 20:02:09 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Thu, 23 May 2013 03:02:09 +0300 Subject: file I/O and arithmetic calculation In-Reply-To: References: , Message-ID: ---------------------------------------- > From: denismfmcmahon at gmail.com [...] > > import re > def v(s): > l=len(s) > t=0. > for i in range(l): > t=t+(abs(ord(s[i]))*1.) > return t/(l*1.) > for n in range(5): > m="c:/test/"+str(n+1)+".txt" > f=open(m,"r") > d=[] > t=0. > for l in range(10): > d=d+[re.findall(r"[0-9.eE+-]+",f.readline())] > t=t+v(d[l][0]) > f.close() > c=t/10. > if c==50.: > t=0. > for u in range(10): > t=t+v(d[0][u]) > r=t/10. > print "%s C1: %f R1: %f"%(m,c,r) > > -- > Denis McMahon, denismfmcmahon at gmail.com > -- > http://mail.python.org/mailman/listinfo/python-list Can you send it again without tabs? From drsalists at gmail.com Wed May 22 23:31:30 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 22 May 2013 20:31:30 -0700 Subject: Ordered dictionaries compared Message-ID: What kind of ordered dictionaries? Sorted by key. I've redone the previous comparison, this time with a better red-black tree implementation courtesy of Duncan G. Smith. The comparison is at http://stromberg.dnsalias.org/~strombrg/python-tree-and-heap-comparison/just-trees/ The Red-Black tree gave a much better showing this time, but it gave just one 2nd place on one workload-interpreter - still kinda lackluster. It took 1st place 0 times. -------------- next part -------------- An HTML attachment was scrubbed... URL: From solipsis at pitrou.net Thu May 23 06:00:19 2013 From: solipsis at pitrou.net (Antoine Pitrou) Date: Thu, 23 May 2013 10:00:19 +0000 (UTC) Subject: Ordered dictionaries compared References: Message-ID: Dan Stromberg gmail.com> writes: > > What kind of ordered dictionaries?? Sorted by key. Calling them "sorted dictionaries" avoids any confusions with Python's standard OrderedDict class: http://docs.python.org/3.3/library/collections.html#ordereddict-objects Regards Antoine. From buzzard at invalid.invalid Thu May 23 12:41:32 2013 From: buzzard at invalid.invalid (duncan smith) Date: Thu, 23 May 2013 17:41:32 +0100 Subject: Ordered dictionaries compared In-Reply-To: References: Message-ID: <519e46c0$0$26690$862e30e2@ngroups.net> On 23/05/13 04:31, Dan Stromberg wrote: > > What kind of ordered dictionaries? Sorted by key. > > I've redone the previous comparison, this time with a better red-black > tree implementation courtesy of Duncan G. Smith. > > The comparison is at > http://stromberg.dnsalias.org/~strombrg/python-tree-and-heap-comparison/just-trees/ > > The Red-Black tree gave a much better showing this time, but it gave > just one 2nd place on one workload-interpreter - still kinda > lackluster. It took 1st place 0 times. > > A quick test of my Red Black Tree and Treap (Python 2.7). >>> def test_trees(data, randomize=True): cpy = data[:] # for deletion if randomize: random.shuffle(data) random.shuffle(cpy) t = binary_trees.RedBlackTree() start = time.time() for datum in data: t.insert(datum) print 'Red Black Tree insertion %s' % (time.time() - start) start = time.time() for datum in data: t.find(datum) print 'Red Black Tree find %s' % (time.time() - start) start = time.time() for datum in cpy: t.delete(datum) print 'Red Black Tree deletion %s' % (time.time() - start) t = binary_trees.Treap() start = time.time() for datum in data: t.insert(datum) print print 'Treap insertion %s' % (time.time() - start) start = time.time() for datum in data: t.find(datum) print 'Treap find %s' % (time.time() - start) start = time.time() for datum in cpy: t.delete(datum) print 'Treap deletion %s' % (time.time() - start) >>> test_trees(range(100000)) Red Black Tree insertion 5.42807197571 Red Black Tree find 1.58799219131 Red Black Tree deletion 3.87580800056 Treap insertion 6.79647684097 Treap find 2.11693120003 Treap deletion 4.61243915558 >>> >>> test_trees(range(100000), False) Red Black Tree insertion 6.29647898674 Red Black Tree find 1.157143116 Red Black Tree deletion 2.74785804749 Treap insertion 3.87288999557 Treap find 1.48776102066 Treap deletion 1.88962197304 >>> RBT is quicker than Treap for insertion with randomized data, but slower with ordered data. Randomized data will tend to minimize the number of tree rotations needed to keep the RBT balanced, whilst the Treap will be performing rotations to maintain the heap property in an already reasonably well balanced tree. With ordered data the RBT will have to work harder to keep the tree balanced, whilst the Treap will be able to maintain the heap property with fewer rotations. No surprise that find() is generally quicker for RBTs, they tend to be better balanced. Deletion is a bit more confusing. I suppose deletion from a better balanced tree will tend to be quicker, but deletion from a treap constructed from ordered data is (for some reason) quickest of all. All these operations require a call to find(), and that is generally going to be quicker for RBTs. Treaps tend to require fewer subsequent rotations, but they have variable worth (in terms of rebalancing). Looks like RBTs are better than treaps if they are being populated with randomly ordered data, but not if they are being populated with ordered data. RBTs are better for use cases that are heavy on finds. Both types of tree appear to be better balanced (on the basis of the find results) if populated from ordered data. Treaps appear to perform better on insertion, find and deletion when populated from ordered data. Duncan From drsalists at gmail.com Thu May 23 13:44:53 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Thu, 23 May 2013 10:44:53 -0700 Subject: Ordered dictionaries compared In-Reply-To: <519e46c0$0$26690$862e30e2@ngroups.net> References: <519e46c0$0$26690$862e30e2@ngroups.net> Message-ID: On Thu, May 23, 2013 at 9:41 AM, duncan smith wrote: > > RBT is quicker than Treap for insertion with randomized data, but slower > with ordered data. Randomized data will tend to minimize the number of tree > rotations needed to keep the RBT balanced, whilst the Treap will be > performing rotations to maintain the heap property in an already reasonably > well balanced tree. With ordered data the RBT will have to work harder to > keep the tree balanced, whilst the Treap will be able to maintain the heap > property with fewer rotations. > > No surprise that find() is generally quicker for RBTs, they tend to be > better balanced. > > Deletion is a bit more confusing. I suppose deletion from a better > balanced tree will tend to be quicker, but deletion from a treap > constructed from ordered data is (for some reason) quickest of all. > > All these operations require a call to find(), and that is generally going > to be quicker for RBTs. Treaps tend to require fewer subsequent rotations, > but they have variable worth (in terms of rebalancing). > > Looks like RBTs are better than treaps if they are being populated with > randomly ordered data, but not if they are being populated with ordered > data. RBTs are better for use cases that are heavy on finds. > > Both types of tree appear to be better balanced (on the basis of the find > results) if populated from ordered data. Treaps appear to perform better on > insertion, find and deletion when populated from ordered data. > Strange. I was comparing randomized data (95% get, 50-50 get and set, 95% set) when I found that treaps were quite a bit faster than red black trees. The code I used is here: http://stromberg.dnsalias.org/svn/python-tree-and-heap-comparison/trunk/ See also https://en.wikipedia.org/wiki/Binary_search_tree#Performance_comparisons , which found that treaps were faster on average the red black trees. -------------- next part -------------- An HTML attachment was scrubbed... URL: From buzzard at invalid.invalid Thu May 23 19:57:51 2013 From: buzzard at invalid.invalid (duncan smith) Date: Fri, 24 May 2013 00:57:51 +0100 Subject: Ordered dictionaries compared In-Reply-To: References: <519e46c0$0$26690$862e30e2@ngroups.net> Message-ID: <519ead02$0$60192$862e30e2@ngroups.net> On 23/05/13 18:44, Dan Stromberg wrote: > > On Thu, May 23, 2013 at 9:41 AM, duncan smith > wrote: > > > RBT is quicker than Treap for insertion with randomized data, but > slower with ordered data. Randomized data will tend to minimize the > number of tree rotations needed to keep the RBT balanced, whilst the > Treap will be performing rotations to maintain the heap property in > an already reasonably well balanced tree. With ordered data the RBT > will have to work harder to keep the tree balanced, whilst the Treap > will be able to maintain the heap property with fewer rotations. > > No surprise that find() is generally quicker for RBTs, they tend to > be better balanced. > > Deletion is a bit more confusing. I suppose deletion from a better > balanced tree will tend to be quicker, but deletion from a treap > constructed from ordered data is (for some reason) quickest of all. > > All these operations require a call to find(), and that is generally > going to be quicker for RBTs. Treaps tend to require fewer > subsequent rotations, but they have variable worth (in terms of > rebalancing). > > Looks like RBTs are better than treaps if they are being populated > with randomly ordered data, but not if they are being populated with > ordered data. RBTs are better for use cases that are heavy on finds. > > Both types of tree appear to be better balanced (on the basis of the > find results) if populated from ordered data. Treaps appear to > perform better on insertion, find and deletion when populated from > ordered data. > > Strange. I was comparing randomized data (95% get, 50-50 get and set, > 95% set) when I found that treaps were quite a bit faster than red black > trees. > > The code I used is here: > http://stromberg.dnsalias.org/svn/python-tree-and-heap-comparison/trunk/ > > See also > https://en.wikipedia.org/wiki/Binary_search_tree#Performance_comparisons > , which found that treaps were faster on average the red black trees. > > Dan, Faster on average, but it depends what you're averaging over. As far as insertion and deletions are concerned my results agree with those in the paper, except they have treaps performing slightly faster than RBTs for insertion with randomly ordered data. Deletion in your code is slightly different to that in mine. It might make a difference. Also, your code doesn't use sentinels (pros and cons). It could be down to implementation details. Duncan From alexander_naumov at opensuse.org Thu May 23 03:20:15 2013 From: alexander_naumov at opensuse.org (Alex Naumov) Date: Thu, 23 May 2013 09:20:15 +0200 Subject: newbie question about subprocess.Popen() arguments Message-ID: Hello, I'm trying to call new process with some parameters. The problem is that the last parameter is a "string" that has a lot of spaces and different symbols like slash and so on. I can save it in file and use name of this file as parameter, but my question is: how to make it without additional saving? import subprocess as sp rc = sp.Popen(["prog", "--options", "<", msg], stdin=sp.PIPE, stdout=sp.PIPE) stdout = rc.communicate()[0] print stdout Thank you, Alex p.s. type(msg) => -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu May 23 03:32:40 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 May 2013 09:32:40 +0200 Subject: newbie question about subprocess.Popen() arguments References: Message-ID: Alex Naumov wrote: > I'm trying to call new process with some parameters. The problem is that > the last parameter is a "string" that has a lot of spaces and different > symbols like slash and so on. I can save it in file and use name of this > file as parameter, but my question is: how to make it without additional > saving? > > import subprocess as sp > > rc = sp.Popen(["prog", "--options", "<", msg], stdin=sp.PIPE, > stdout=sp.PIPE) > stdout = rc.communicate()[0] > print stdout > p.s. > type(msg) => The < operator is a shell feature, not an argument, and msg is intended to be send to prog's stdin. The communicate() method accepts a parameter for that. So: rc = sp.Popen(["prog", "--options"], stdin=sp.PIPE, stdout=sp.PIPE) stdout = rc.communicate(msg)[0] From alexander_naumov at opensuse.org Thu May 23 03:40:48 2013 From: alexander_naumov at opensuse.org (Alex Naumov) Date: Thu, 23 May 2013 09:40:48 +0200 Subject: newbie question about subprocess.Popen() arguments In-Reply-To: References: Message-ID: Thank you very much, Peter! It works! On Thu, May 23, 2013 at 9:32 AM, Peter Otten <__peter__ at web.de> wrote: > Alex Naumov wrote: > > > I'm trying to call new process with some parameters. The problem is that > > the last parameter is a "string" that has a lot of spaces and different > > symbols like slash and so on. I can save it in file and use name of this > > file as parameter, but my question is: how to make it without > additional > > saving? > > > > import subprocess as sp > > > > rc = sp.Popen(["prog", "--options", "<", msg], stdin=sp.PIPE, > > stdout=sp.PIPE) > > stdout = rc.communicate()[0] > > print stdout > > > p.s. > > type(msg) => > > The < operator is a shell feature, not an argument, and msg is intended to > be send to prog's stdin. The communicate() method accepts a parameter for > that. So: > > rc = sp.Popen(["prog", "--options"], stdin=sp.PIPE, stdout=sp.PIPE) > stdout = rc.communicate(msg)[0] > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lokeshkoppaka at gmail.com Thu May 23 05:51:36 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Thu, 23 May 2013 02:51:36 -0700 (PDT) Subject: Scope of a class..help??? Message-ID: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> i had written the following code i am unable to create the instance of the class "Node" in the method "number_to_LinkedList" can any one help me how to do ?? and what is the error?? class Node: def __init__(self, value=None): self.value = value self.next = None def number_to_LinkedList(numbers): pass list_numbers = list(numbers) head_node = Node() #unable to create the instance saying UnboundedLocal head_node.value = list_numbers[0] head_node.next = None current_node = head_node for i in range(1,len(list_numbers)): new_node = Node() new_node.value = list_numbers[i] new_node.next = current_node current_node = new_node current_node.next = None while Node: print Node.data Node = Node.next From rosuav at gmail.com Thu May 23 06:00:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 20:00:27 +1000 Subject: Scope of a class..help??? In-Reply-To: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> References: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> Message-ID: On Thu, May 23, 2013 at 7:51 PM, wrote: > i had written the following code i am unable to create the instance of the class "Node" in the method "number_to_LinkedList" can any one help me how to do ?? > and what is the error?? It would really help if you post the actual exception and traceback. It's UnboundLocal, not Unbounded... and here's the problem: > def number_to_LinkedList(numbers): > head_node = Node() #unable to create the instance saying UnboundedLocal > while Node: > print Node.data > Node = Node.next You're assigning to Node. I think you mean to have some other local variable here, for the iteration at the end. Since you assign to the name Node inside the function (and don't have a global declaration), the name Node is local to the function. Python doesn't let you reference the global "prior to" shadowing it with the local, so you get this error. ChrisA From __peter__ at web.de Thu May 23 06:18:16 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 May 2013 12:18:16 +0200 Subject: Scope of a class..help??? References: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> Message-ID: lokeshkoppaka at gmail.com wrote: > i had written the following code i am unable to create the instance of the > class "Node" in the method "number_to_LinkedList" can any one help me how > to do ?? and what is the error?? > > > class Node: > def __init__(self, value=None): > self.value = value > self.next = None > > > > def number_to_LinkedList(numbers): > pass > list_numbers = list(numbers) > head_node = Node() #unable to create the instance saying > UnboundedLocal head_node.value = list_numbers[0] > head_node.next = None > current_node = head_node > for i in range(1,len(list_numbers)): > new_node = Node() > new_node.value = list_numbers[i] > new_node.next = current_node > current_node = new_node > current_node.next = None > while Node: > print Node.data > Node = Node.next You have to decide if you want to use a name in a function locally or to access a global. Python treats names that are being assigned to anywhere in a function as local throughout the whole function. x = "global" def f(): print x # ok, access global variable x = "global" def g(): x = "local" print x # ok, accessing local variable x = "global" def h(): print x # error, accessing local variable that has not # been assigned a value x = "local" From lokeshkoppaka at gmail.com Thu May 23 06:23:26 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Thu, 23 May 2013 03:23:26 -0700 (PDT) Subject: Scope of a class..help??? In-Reply-To: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> References: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> Message-ID: <110b4762-392d-4430-b1aa-6e8168ced9f3@googlegroups.com> Thanks Chris Angelico, i am new to python can you suggest me how to remove the error and solve it. so,how can i create an instance for "Node" in that function??,is, it not possible to create an instance in such a way? From rosuav at gmail.com Thu May 23 06:27:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 20:27:27 +1000 Subject: Scope of a class..help??? In-Reply-To: <110b4762-392d-4430-b1aa-6e8168ced9f3@googlegroups.com> References: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> <110b4762-392d-4430-b1aa-6e8168ced9f3@googlegroups.com> Message-ID: On Thu, May 23, 2013 at 8:23 PM, wrote: > Thanks Chris Angelico, > i am new to python can you suggest me how to remove the error and solve it. > so,how can i create an instance for "Node" in that function??,is, it not possible to create an instance in such a way? The problem isn't the instantiation; the problem's further down, where you reuse the name "Node" to iterate over your list. Rename that to something else and you should be right. ChrisA From lokeshkoppaka at gmail.com Thu May 23 06:25:57 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Thu, 23 May 2013 03:25:57 -0700 (PDT) Subject: Scope of a class..help??? In-Reply-To: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> References: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> Message-ID: <6f05ea64-4a3a-4f92-a4c9-ade8bf7117c6@googlegroups.com> ok Peter Otten, but how to make a Class global?? From rosuav at gmail.com Thu May 23 07:40:14 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 21:40:14 +1000 Subject: Scope of a class..help??? In-Reply-To: <6f05ea64-4a3a-4f92-a4c9-ade8bf7117c6@googlegroups.com> References: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> <6f05ea64-4a3a-4f92-a4c9-ade8bf7117c6@googlegroups.com> Message-ID: On Thu, May 23, 2013 at 8:25 PM, wrote: > ok Peter Otten, > but how to make a Class global?? He gave some examples. It'd be helpful to quote some of his post, for context... and preferably, show some proof that you've understood it. You're starting a number of threads that look like you're trying to get us to do your homework. You need to demonstrate that you actually understand the material you're supposed to be learning. ChrisA From timr at probo.com Fri May 24 01:05:57 2013 From: timr at probo.com (Tim Roberts) Date: Thu, 23 May 2013 22:05:57 -0700 Subject: Scope of a class..help??? References: <913d7fc1-4608-4ff5-8b51-2ae2cd67781a@googlegroups.com> <6f05ea64-4a3a-4f92-a4c9-ade8bf7117c6@googlegroups.com> Message-ID: lokeshkoppaka at gmail.com wrote: > >ok Peter Otten, >but how to make a Class global?? Your class IS global. I still don't think you understand what you did wrong. You've tripped into a strange little quirk of scoping. Here is your code with some unimportant lines removes. class Node: def __init__(self, value=None): self.value = value self.next = None ## OK, at this point, you have a global called "Node", which is a class. def number_to_LinkedList(numbers): pass list_numbers = list(numbers) head_node = Node() # ... current_node.next = None while Node: print Node.data Python actually does a first scan through your function before it starts to compile it. When it does the scan, it sees you use the name "Node" as a local variable. At that point, it remembers that "in this function scope, 'Node' is a local name". Now, it codes to compile the class. When it encounters Node() for the first time, it sees that "Node" is not defined locally, even though it was supposed to be a local name. You are assuming that the first Node() in that function will automatically refer to the global class. It won't. The only way to solve this dilemma is to change the name you use in the "while" loop. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From lokeshkoppaka at gmail.com Thu May 23 05:54:02 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Thu, 23 May 2013 02:54:02 -0700 (PDT) Subject: 2's Complement in python help. Message-ID: <53dc6273-5aed-44e9-8f39-7eb7f4f3a1b1@googlegroups.com> Can anyone give me an idea of how to find the 2's Complement in python with an example From rosuav at gmail.com Thu May 23 06:03:43 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 20:03:43 +1000 Subject: 2's Complement in python help. In-Reply-To: <53dc6273-5aed-44e9-8f39-7eb7f4f3a1b1@googlegroups.com> References: <53dc6273-5aed-44e9-8f39-7eb7f4f3a1b1@googlegroups.com> Message-ID: On Thu, May 23, 2013 at 7:54 PM, wrote: > Can anyone give me an idea of how to find the 2's Complement in python with an example Do you know what two's complement is? (Not to be confused with two's compliment, which is when numbers start telling you how clever you are.) If not, you should probably look it up on Wikipedia or something. Once you know what the term means, you can start implementing in Python. ChrisA From js at globe.de Thu May 23 06:37:05 2013 From: js at globe.de (Schneider) Date: Thu, 23 May 2013 12:37:05 +0200 Subject: serialize a class to XML and back Message-ID: <519DF151.1080405@globe.de> Hi list, how can I serialize a python class to XML? Plus a way to get the class back from the XML? My aim is to store instances of this class in a database. bg, Johannes -- GLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nsterGLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nster 0251/5205 390 From burak.arslan at arskom.com.tr Thu May 23 07:16:34 2013 From: burak.arslan at arskom.com.tr (Burak Arslan) Date: Thu, 23 May 2013 14:16:34 +0300 Subject: serialize a class to XML and back In-Reply-To: <519DF151.1080405@globe.de> References: <519DF151.1080405@globe.de> Message-ID: <519DFA92.5050205@arskom.com.tr> On 05/23/13 13:37, Schneider wrote: > Hi list, > > how can I serialize a python class to XML? Plus a way to get the class > back from the XML? > > My aim is to store instances of this class in a database. Hi, I'm working on a project called Spyne (http://spyne.io). With one object definition, you can serialize to/from xml and save to database via SQLAlchemy. The code generator on the site has examples for RPC and SQL. If you're not interested in doing RPC with the resulting document, here's an example for just the xml part: https://github.com/arskom/spyne/blob/master/examples/xml_utils.py Best regards, Burak From wuwei23 at gmail.com Thu May 23 20:01:01 2013 From: wuwei23 at gmail.com (alex23) Date: Thu, 23 May 2013 17:01:01 -0700 (PDT) Subject: serialize a class to XML and back References: Message-ID: <31e0af40-d1e5-4b09-b0c2-f4a87b4399e2@wg15g2000pbb.googlegroups.com> On May 23, 8:37?pm, Schneider wrote: > My aim is to store instances of this class in a database. Have you considered just pickling the classes instead? From dieter at handshake.de Sat May 25 01:54:54 2013 From: dieter at handshake.de (dieter) Date: Sat, 25 May 2013 07:54:54 +0200 Subject: serialize a class to XML and back References: <519DF151.1080405@globe.de> Message-ID: <87zjvjppsh.fsf@handshake.de> Schneider writes: > how can I serialize a python class to XML? Plus a way to get the class > back from the XML? > > My aim is to store instances of this class in a database. In case you want to describe the XML data via an XML-schema (e.g. to exchange it with other applications; maybe via WebServices), you may have a look at "PyXB". The approach of "PyXB" may be a bit different from yours: It starts with an XML-schema description and from it generates Python classes corresponding to the types mentioned in the schema. Instances of those classes can then be easily serialized to XML and XML documents corresponding to types defined in the schema can easily be converted into corresponding class instances. It is not too difficult to customize the classes used for a given type - e.g. to give them special methods related to your application. You may want to start with your (arbitrary) Python classes and get their instances serialized into an adequate XML document. This will not work in all cases: some things are very difficult to serialize (maybe even not serializable at all - e.g. locks). If you plan to use anything already existing, then almost surely, this will impose restrictions of your classes. From js at globe.de Fri May 31 07:16:21 2013 From: js at globe.de (Schneider) Date: Fri, 31 May 2013 13:16:21 +0200 Subject: serialize a class to XML and back In-Reply-To: <87zjvjppsh.fsf@handshake.de> References: <519DF151.1080405@globe.de> <87zjvjppsh.fsf@handshake.de> Message-ID: <51A88685.70409@globe.de> On 25.05.2013 07:54, dieter wrote: > Schneider writes: > >> how can I serialize a python class to XML? Plus a way to get the class >> back from the XML? >> >> My aim is to store instances of this class in a database. > In case you want to describe the XML data via an XML-schema > (e.g. to exchange it with other applications; maybe via > WebServices), you may have a look at "PyXB". > > > The approach of "PyXB" may be a bit different from yours: > > It starts with an XML-schema description and from > it generates Python classes corresponding to the types > mentioned in the schema. > > Instances of those classes can then be easily serialized > to XML and XML documents corresponding to types defined > in the schema can easily be converted into corresponding > class instances. > > It is not too difficult to customize the classes > used for a given type - e.g. to give them special methods > related to your application. > > > You may want to start with your (arbitrary) Python classes > and get their instances serialized into an adequate XML document. > > This will not work in all cases: some things are very difficult > to serialize (maybe even not serializable at all - e.g. locks). I have just small classes containing text (strings) numbers (as ids) and references to other classes of this type. > > If you plan to use anything already existing, then almost > surely, this will impose restrictions of your classes. > -- GLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nsterGLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nster 0251/5205 390 From clp2 at rebertia.com Sun May 26 16:36:53 2013 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 26 May 2013 13:36:53 -0700 Subject: serialize a class to XML and back In-Reply-To: <519DF151.1080405@globe.de> References: <519DF151.1080405@globe.de> Message-ID: On May 23, 2013 3:42 AM, "Schneider" wrote: > > Hi list, > > how can I serialize a python class to XML? Plus a way to get the class back from the XML? There's pyxser: http://pythonhosted.org/pyxser/ > My aim is to store instances of this class in a database. Honestly, I would avoid XML if you can. Consider using JSON (Python includes the `json` module in the std lib) or pickle instead. Compared to XML: The former is more standardized (in the context of serializing objects) and less verbose; the latter is more efficient (if you don't care about cross-language accessibility); both have more convenient APIs. Cheers, Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Sun May 26 16:48:21 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 16:48:21 -0400 Subject: serialize a class to XML and back References: <519DF151.1080405@globe.de> Message-ID: In article , Chris Rebert wrote: > On May 23, 2013 3:42 AM, "Schneider" wrote: > > > > Hi list, > > > > how can I serialize a python class to XML? Plus a way to get the class > back from the XML? > > There's pyxser: http://pythonhosted.org/pyxser/ > > > My aim is to store instances of this class in a database. > > Honestly, I would avoid XML if you can. Consider using JSON (Python > includes the `json` module in the std lib) or pickle instead. Compared to > XML: The former is more standardized (in the context of serializing > objects) and less verbose; the latter is more efficient (if you don't care > about cross-language accessibility); both have more convenient APIs. Some other points... If you care about efficiency and want to use json, don't use the one that comes packaged with the standard library. There are lots of third-party json packages (ujson is the one we use) which are significantly faster. Not sure if that's true of the newest python releases, but it was certainly true in 2.6. The advantage of pickle over json is that pickle can serialize many types of objects that json can't. The other side of the coin is that pickle is python-specific, so if you think you'll ever need to read your data from other languages, pickle is right out. From irmen.NOSPAM at xs4all.nl Sun May 26 18:40:01 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Mon, 27 May 2013 00:40:01 +0200 Subject: serialize a class to XML and back In-Reply-To: References: <519DF151.1080405@globe.de> Message-ID: <51a28f42$0$15870$e4fe514c@news.xs4all.nl> On 26-5-2013 22:48, Roy Smith wrote: > The advantage of pickle over json is that pickle can serialize many > types of objects that json can't. The other side of the coin is that > pickle is python-specific, so if you think you'll ever need to read your > data from other languages, pickle is right out. That is not entirely true :) I've written a pickle implementation for Java and .NET that is almost feature complete; it is part of http://pythonhosted.org/Pyro4/pyrolite.html Still, pickle may not be the best choice here. Cheers Irmen From roy at panix.com Sun May 26 20:39:01 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 20:39:01 -0400 Subject: serialize a class to XML and back References: <519DF151.1080405@globe.de> <51a28f42$0$15870$e4fe514c@news.xs4all.nl> Message-ID: In article <51a28f42$0$15870$e4fe514c at news.xs4all.nl>, Irmen de Jong wrote: > On 26-5-2013 22:48, Roy Smith wrote: > > > The advantage of pickle over json is that pickle can serialize many > > types of objects that json can't. The other side of the coin is that > > pickle is python-specific, so if you think you'll ever need to read your > > data from other languages, pickle is right out. > > That is not entirely true :) I've written a pickle implementation for Java > and .NET > that is almost feature complete; it is part of > http://pythonhosted.org/Pyro4/pyrolite.html Very cool > Still, pickle may not be the best choice here. Perhaps not, but lots of points for the awesomeness factor. From irmen.NOSPAM at xs4all.nl Mon May 27 14:45:07 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Mon, 27 May 2013 20:45:07 +0200 Subject: serialize a class to XML and back In-Reply-To: References: <519DF151.1080405@globe.de> <51a28f42$0$15870$e4fe514c@news.xs4all.nl> Message-ID: <51a3a9b5$0$15963$e4fe514c@news.xs4all.nl> On 27-5-2013 2:39, Roy Smith wrote: > In article <51a28f42$0$15870$e4fe514c at news.xs4all.nl>, > Irmen de Jong wrote: > >> On 26-5-2013 22:48, Roy Smith wrote: >> >>> The advantage of pickle over json is that pickle can serialize many >>> types of objects that json can't. The other side of the coin is that >>> pickle is python-specific, so if you think you'll ever need to read your >>> data from other languages, pickle is right out. >> >> That is not entirely true :) I've written a pickle implementation for Java >> and .NET >> that is almost feature complete; it is part of >> http://pythonhosted.org/Pyro4/pyrolite.html > > Very cool > >> Still, pickle may not be the best choice here. > > Perhaps not, but lots of points for the awesomeness factor. > Thanks for the praise :) There's another interesting thing perhaps to also mention about Pyrolite. Its Pickle implementation obviously maps built in types to their counterparts in Java/.NET, but it is also capable of unpickling custom classes. It defaults to outputting a simple hashtable with the class's properties in it, but you can also provide a custom deserializer to recreate a custom object (it already does this automatically for a few complex types such as DateTime, BigDecimal, and a bunch of Pyro specific types). As the unpicklers are not executing any Java or .NET code dynamically they are not susceptible to the security issue that Python's pickle has. Still: tread with care. (Especially if you use the lib to pickle stuff and unpickle it on the Python side). Also, the one missing feature is memo-ing when pickling so that you can pickle recursive object graphs. It now throws a stack overflow exception instead. Irmen From js at globe.de Fri May 31 07:21:39 2013 From: js at globe.de (Schneider) Date: Fri, 31 May 2013 13:21:39 +0200 Subject: serialize a class to XML and back In-Reply-To: References: <519DF151.1080405@globe.de> Message-ID: <51A887C3.6010702@globe.de> On 26.05.2013 22:48, Roy Smith wrote: > In article , > Chris Rebert wrote: > >> On May 23, 2013 3:42 AM, "Schneider" wrote: >>> Hi list, >>> >>> how can I serialize a python class to XML? Plus a way to get the class >> back from the XML? >> >> There's pyxser: http://pythonhosted.org/pyxser/ >> >>> My aim is to store instances of this class in a database. >> Honestly, I would avoid XML if you can. Consider using JSON (Python >> includes the `json` module in the std lib) or pickle instead. Compared to >> XML: The former is more standardized (in the context of serializing >> objects) and less verbose; the latter is more efficient (if you don't care >> about cross-language accessibility); both have more convenient APIs. > Some other points... > > If you care about efficiency and want to use json, don't use the one > that comes packaged with the standard library. There are lots of > third-party json packages (ujson is the one we use) which are > significantly faster. Not sure if that's true of the newest python > releases, but it was certainly true in 2.6. I think performance can be a problem in future. This question is part of a multi-user rss-reader solution, which I'm going to develop. I want to store the feed entries (+ some additional data) as XML in a database. > The advantage of pickle over json is that pickle can serialize many > types of objects that json can't. The other side of the coin is that > pickle is python-specific, so if you think you'll ever need to read your > data from other languages, pickle is right out. -- GLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nsterGLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nster 0251/5205 390 From lokeshkoppaka at gmail.com Thu May 23 07:30:15 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Thu, 23 May 2013 04:30:15 -0700 (PDT) Subject: help in obtaining binary equivalent of a decimal number in python Message-ID: <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com> i need to get 32 bit binary equivalent of a decimal and need to change the 0's to 1's and 1's to 0's For Example if the input is 2 Output should be: the 32bit equivalent of 2 :0000 0000 0000 0000 0000 0000 0000 0010 and the 1's compliment is:1111 1111 1111 1111 1111 1111 1111 1101 is there any pre-defined function to get the above results in python?? From rosuav at gmail.com Thu May 23 07:37:30 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 23 May 2013 21:37:30 +1000 Subject: help in obtaining binary equivalent of a decimal number in python In-Reply-To: <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com> References: <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com> Message-ID: On Thu, May 23, 2013 at 9:30 PM, wrote: > i need to get 32 bit binary equivalent of a decimal and need to change the 0's to 1's and 1's to 0's > For Example > if the input is 2 > Output should be: > the 32bit equivalent of 2 :0000 0000 0000 0000 0000 0000 0000 0010 > and the 1's compliment is:1111 1111 1111 1111 1111 1111 1111 1101 > > > > is there any pre-defined function to get the above results in python?? You're asking for bitwise negation. Now that you know the keyword(s) to look for, you should be able to figure out the rest with a few quick docs and/or web searches. ChrisA From davea at davea.name Thu May 23 07:55:36 2013 From: davea at davea.name (Dave Angel) Date: Thu, 23 May 2013 07:55:36 -0400 Subject: help in obtaining binary equivalent of a decimal number in python In-Reply-To: <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com> References: <7f794f03-03c9-44de-8312-4ad33b68c266@googlegroups.com> Message-ID: <519E03B8.7020907@davea.name> On 05/23/2013 07:30 AM, lokeshkoppaka at gmail.com wrote: > i need to get 32 bit binary equivalent of a decimal and need to change the 0's to 1's and 1's to 0's > For Example > if the input is 2 > Output should be: > the 32bit equivalent of 2 :0000 0000 0000 0000 0000 0000 0000 0010 > and the 1's compliment is:1111 1111 1111 1111 1111 1111 1111 1101 > > > > is there any pre-defined function to get the above results in python?? > I'm curious as to the intent of the assignment. Are you supposed to be learning about base conversion, about ones and twos complement, or about Python? Assuming the intent is to learn about Python, the built-in function bin() will take a Python integer (which is not decimal) and convert it to a str. At that point, you can manipulate the string any way you like. x = 45 print bin(45) 0b101101 Perhaps you want to start by stripping off the leading '0b' using a slice. Then you want to pad it to 32 columns by prepending some number of zeroes. Then you want to insert some spaces at regular intervals. Presumably doing the ones-complement operation on that string is then pretty easy for you. -- DaveA From roy at panix.com Thu May 23 09:09:21 2013 From: roy at panix.com (Roy Smith) Date: Thu, 23 May 2013 09:09:21 -0400 Subject: Debugging parallel nose tests? Message-ID: I've got a suite of about 150 tests written using unittest. It takes 5-10 minutes to run, so I'd really like to use nose to run things in parallel. The problem is, when I do that, I get lots of test failures. Obviously, we've got some kind of inter-test dependency that I haven't been able to locate. Is there some way to make nose print a report of how it partitioned the tests across the various processes? If I could see that, it might help us reproduce the failures. We're using: nosetests --process-timeout=60 --processes=40 test_api.py and _multiprocess_can_split_ = True From davea at davea.name Thu May 23 14:31:36 2013 From: davea at davea.name (Dave Angel) Date: Thu, 23 May 2013 14:31:36 -0400 Subject: Debugging parallel nose tests? In-Reply-To: References: Message-ID: <519E6088.5070002@davea.name> On 05/23/2013 09:09 AM, Roy Smith wrote: > > > > nosetests --process-timeout=60 --processes=40 test_api.py > Do you have a 40-processor system? And do you have enough RAM to run all of those processes? -- DaveA From roy at panix.com Thu May 23 21:44:07 2013 From: roy at panix.com (Roy Smith) Date: Thu, 23 May 2013 21:44:07 -0400 Subject: Debugging parallel nose tests? References: Message-ID: In article , Dave Angel wrote: > On 05/23/2013 09:09 AM, Roy Smith wrote: > > > > > > > > nosetests --process-timeout=60 --processes=40 test_api.py > > > > Do you have a 40-processor system? No, but many of the tests are I/O bound. > And do you have enough RAM to run all of those processes? Yes. The box I'm on now has 8 gig. I'd doing a 40 process run right now, and I've still got 1/2 gig free. From jeanmichel at sequans.com Fri May 24 05:33:40 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 24 May 2013 09:33:40 -0000 Subject: Debugging parallel nose tests? In-Reply-To: Message-ID: <1442952133.19384735.1369386305536.JavaMail.root@sequans.com> ----- Original Message ----- > In article , > Dave Angel wrote: > > > On 05/23/2013 09:09 AM, Roy Smith wrote: > > > > > > > > > > > > nosetests --process-timeout=60 --processes=40 test_api.py > > > > > > > Do you have a 40-processor system? > > No, but many of the tests are I/O bound. Sorry to hijack your thread but what do you mean by that ? I have a lot of tests myself that spend most of their time writing and reading files. Should I try to multiprocess them ? JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From roy at panix.com Fri May 24 07:47:45 2013 From: roy at panix.com (Roy Smith) Date: Fri, 24 May 2013 07:47:45 -0400 Subject: Debugging parallel nose tests? In-Reply-To: <1442952133.19384735.1369386305536.JavaMail.root@sequans.com> References: <1442952133.19384735.1369386305536.JavaMail.root@sequans.com> Message-ID: <11040677-5704-4CEC-9F0C-4B24CBD611A6@panix.com> On May 24, 2013, at 5:05 AM, Jean-Michel Pichavant wrote: > ----- Original Message ----- >> In article , >> Dave Angel wrote: >> >>> On 05/23/2013 09:09 AM, Roy Smith wrote: >>>> >>>> >>>> >>>> nosetests --process-timeout=60 --processes=40 test_api.py >>>> >>> >>> Do you have a 40-processor system? >> >> No, but many of the tests are I/O bound. > > Sorry to hijack your thread but what do you mean by that ? That means that a lot of time running the test is spent waiting for I/O (in this case, network reads), as opposed to actually running on a CPU. > I have a lot of tests myself that spend most of their time writing and reading files. Should I try to multiprocess them ? Maybe :-) -- Roy Smith roy at panix.com From roy at panix.com Fri May 24 23:09:18 2013 From: roy at panix.com (Roy Smith) Date: Fri, 24 May 2013 23:09:18 -0400 Subject: Debugging parallel nose tests? References: Message-ID: In article , Roy Smith wrote: > Is there some way to make nose print a report of how it partitioned the > tests across the various processes? I never found such a feature, but I did figure out a way to do what I needed. We use a system of unique id's to track HTTP requests through our system. All you need do is add a X-Unique-Request-ID header and any of our server processes will log that. So, I just had my test suite add: X-Unique-Request-ID: name-of-test.process-id to each HTTP request. That got me what I was looking for (to know what tests were grouped into the same process). > We're using: > > nosetests --process-timeout=60 --processes=40 test_api.py It turned out, the answer was purely a timing issue. We had some tests that called out to facebook. Those were (intermittently) so slow they caused the 60 second timeout to be exceeded. The solution there was to pull the facebook tests out to their own suite (which we'll run in single-process mode). From kboo at ca.ibm.com Thu May 23 09:58:45 2013 From: kboo at ca.ibm.com (Kihup Boo) Date: Thu, 23 May 2013 09:58:45 -0400 Subject: how to get the socket module compiled with SSL support Message-ID: I am trying to make an HTTPS connection and read that HTTPS support is only available if the socket module was compiled with SSL support. http://www.jython.org/docs/library/httplib.html Can someone elaborate on this? Where can I get the socket module for HTTPS, or how do I build one if I have to? Thanks, - Kihup -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu May 23 10:16:06 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 00:16:06 +1000 Subject: how to get the socket module compiled with SSL support In-Reply-To: References: Message-ID: On Thu, May 23, 2013 at 11:58 PM, Kihup Boo wrote: > I am trying to make an HTTPS connection and read that HTTPS support is only > available if the socket module was compiled with SSL support. > > http://www.jython.org/docs/library/httplib.html > > Can someone elaborate on this? Where can I get the socket module for HTTPS, > or how do I build one if I have to? If you got a standard download, chances are it's going to have SSL support. What error are you getting? Is it specifically telling you that SSL support is not available? Post a minimal script and its actual failure. ChrisA From tjreedy at udel.edu Thu May 23 16:22:34 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Thu, 23 May 2013 16:22:34 -0400 Subject: how to get the socket module compiled with SSL support In-Reply-To: References: Message-ID: On 5/23/2013 9:58 AM, Kihup Boo wrote: > I am trying to make an HTTPS connection and read that HTTPS support is > only available if the socket module was compiled with SSL support. > > _http://www.jython.org/docs/library/httplib.html_ > > Can someone elaborate on this? Where can I get the socket module for > HTTPS, or how do I build one if I have to? This is mostly a CPython oriented list. Any prebuilt CPython binary you get likely has SSL support compiled in. If you want to know about the jython socket module, you should check on a jython list, or just try it. From asianavatar at gmail.com Thu May 23 11:48:59 2013 From: asianavatar at gmail.com (asianavatar at gmail.com) Date: Thu, 23 May 2013 08:48:59 -0700 (PDT) Subject: Error with python 3.3.2 and https Message-ID: <683480d3-9b36-40f3-af61-e767178ceb87@googlegroups.com> I am trying to write a program that requires me hitting a https web link. However, I can't seem to get it to work. The program works fine when dealing with http sites, however, when I try it with a https site I get socket.gaierror: [Errno 11001] getaddrinfo failed It seems like it has something to do with the ssl not working, however, I do have the ssl.py in the python library and I have no problem importing it. My code is below. Any help would be greatly appreciated. import urllib.request auth = urllib.request.HTTPSHandler() proxy = urllib.request.ProxyHandler({'http':'my proxy'}) opener = urllib.request.build_opener(proxy, auth) f = opener.open('http://www.google.ca/') From rosuav at gmail.com Thu May 23 11:56:11 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 01:56:11 +1000 Subject: Error with python 3.3.2 and https In-Reply-To: <683480d3-9b36-40f3-af61-e767178ceb87@googlegroups.com> References: <683480d3-9b36-40f3-af61-e767178ceb87@googlegroups.com> Message-ID: On Fri, May 24, 2013 at 1:48 AM, wrote: > I am trying to write a program that requires me hitting a https web link. However, I can't seem to get it to work. The program works fine when dealing with http sites, however, when I try it with a https site I get > > socket.gaierror: [Errno 11001] getaddrinfo failed > > It seems like it has something to do with the ssl not working, however, I do have the ssl.py in the python library and I have no problem importing it. > > My code is below. Any help would be greatly appreciated. > > import urllib.request > auth = urllib.request.HTTPSHandler() > proxy = urllib.request.ProxyHandler({'http':'my proxy'}) > opener = urllib.request.build_opener(proxy, auth) > f = opener.open('http://www.google.ca/') Check the name of your proxy; maybe you can't resolve it. Can you identify your proxy by IP address? By omitting the proxy part, I can quite happily make a direct request using code like yours. ChrisA From asianavatar at gmail.com Thu May 23 12:12:44 2013 From: asianavatar at gmail.com (asianavatar at gmail.com) Date: Thu, 23 May 2013 09:12:44 -0700 (PDT) Subject: Error with python 3.3.2 and https In-Reply-To: References: <683480d3-9b36-40f3-af61-e767178ceb87@googlegroups.com> Message-ID: <77525aba-65fc-41c3-aeb5-428d66719bee@googlegroups.com> I only have the http of the proxy. I guess I could find out the ip of it. However, even if I use the http proxy address, why would it work for a http site and not a https site if its the proxy that can't resolve. From rosuav at gmail.com Thu May 23 12:18:04 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 02:18:04 +1000 Subject: Error with python 3.3.2 and https In-Reply-To: <77525aba-65fc-41c3-aeb5-428d66719bee@googlegroups.com> References: <683480d3-9b36-40f3-af61-e767178ceb87@googlegroups.com> <77525aba-65fc-41c3-aeb5-428d66719bee@googlegroups.com> Message-ID: On Fri, May 24, 2013 at 2:12 AM, wrote: > I only have the http of the proxy. I guess I could find out the ip of it. However, even if I use the http proxy address, why would it work for a http site and not a https site if its the proxy that can't resolve. Can you post working code for HTTP and nonworking for HTTPS? There was no SSL in the code you posted. Or are you saying it's really just that you change the URL and it stops working? If so, I don't think we can test it without having your proxy... you may want to talk to whoever controls the proxy. ChrisA From asianavatar at gmail.com Thu May 23 13:49:55 2013 From: asianavatar at gmail.com (asianavatar at gmail.com) Date: Thu, 23 May 2013 10:49:55 -0700 (PDT) Subject: Error with python 3.3.2 and https In-Reply-To: References: <683480d3-9b36-40f3-af61-e767178ceb87@googlegroups.com> <77525aba-65fc-41c3-aeb5-428d66719bee@googlegroups.com> Message-ID: Yeah that is the case. Once I change the f = opener.open('website') line to a link that has a https I get that socket error. Nothing else changes. I was reading online and came across this site which shows you how if the version of python installed supports ssl. http://morlockhq.blogspot.ca/2008/05/python-tip-checking-to-see-if-your.html When I followed the instructions I got that it wasn't supported, although the ssl.py is in the lib directory. Python was installed from the official python page using the windows installer. Is somehow ssl disabled by default? Do I need to get a third party ssl module? From rosuav at gmail.com Thu May 23 18:23:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 08:23:55 +1000 Subject: Error with python 3.3.2 and https In-Reply-To: References: <683480d3-9b36-40f3-af61-e767178ceb87@googlegroups.com> <77525aba-65fc-41c3-aeb5-428d66719bee@googlegroups.com> Message-ID: On Fri, May 24, 2013 at 3:49 AM, wrote: > > Yeah that is the case. Once I change the f = opener.open('website') line to a link that has a https I get that socket error. Nothing else changes. I was reading online and came across this site which shows you how if the version of python installed supports ssl. > > http://morlockhq.blogspot.ca/2008/05/python-tip-checking-to-see-if-your.html > > When I followed the instructions I got that it wasn't supported, although the ssl.py is in the lib directory. Python was installed from the official python page using the windows installer. Is somehow ssl disabled by default? Do I need to get a third party ssl module? Hrm. Unfortunately those instructions don't work for Python 3... or at least, if they do, then both of my handy installations (one Windows, one Linux) don't have SSL. However, I *can* do 'import ssl' and 'import _ssl', the latter of which is what Lib/socket.py checks for. In Python 3.3's Lib/socket.py there's no mention of ssl anywhere, so I'd be inclined to look elsewhere for support checks. ChrisA From asianavatar at gmail.com Fri May 24 14:08:13 2013 From: asianavatar at gmail.com (asianavatar at gmail.com) Date: Fri, 24 May 2013 11:08:13 -0700 (PDT) Subject: Error with python 3.3.2 and https In-Reply-To: References: <683480d3-9b36-40f3-af61-e767178ceb87@googlegroups.com> <77525aba-65fc-41c3-aeb5-428d66719bee@googlegroups.com> Message-ID: So apparently switching the http to https in the proxyHandler call did the trick. Thanks for all the help. From aeaprog at gmail.com Thu May 23 12:09:31 2013 From: aeaprog at gmail.com (Andrew Edwards-Adams) Date: Thu, 23 May 2013 09:09:31 -0700 (PDT) Subject: Accessing Json data (I think I am nearly there) complete beginner Message-ID: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Hey guys I think its worth stating that I have been trying to code for 1 week. I am trying to access some Json data. My innitial code was the below: "import mechanize import urllib import re def getData(): post_url = "http://www.tweetnaps.co.uk/leaderboards/leaderboard_json/all_time" browser = mechanize.Browser() browser.set_handle_robots(False) browser.addheaders = [('User-agent', 'Firefox')] #These are the parameters you've got from checking with the aforementioned tools parameters = {'page' : '1', 'rp' : '10', 'sortname' : 'total_pl', 'sortorder' : 'desc'} #Encode the parameters data = urllib.urlencode(parameters) trans_array = browser.open(post_url,data).read().decode('UTF-8') #print trans_array myfile = open("test.txt", "w") myfile.write(trans_array) myfile.close() getData() raw_input("Complete")" I was recommended to use the following code to access the Json data directly, however I cannot get it to return anything. I think the guy that recommended me this method must have got something wrong? Or perhaps I am simply incompetent: import mechanize import urllib import json def getData(): post_url = "http://www.tweetnaps.co.uk/leaderboards/leaderboard_json/current_week" browser = mechanize.Browser() browser.set_handle_robots(False) browser.addheaders = [('User-agent', 'Firefox')] #These are the parameters you've got from checking with the aforementioned tools parameters = {'page' : '1', 'rp' : '50', 'sortname' : 'total_pl', 'sortorder' : 'desc' } #Encode the parameters data = urllib.urlencode(parameters) trans_array = browser.open(post_url,data).read().decode('UTF-8') text1 = json.loads(trans_array) print text1['rows'][0]['id'] #play around with these values to access different data.. getData() He told me to "#play around with these values to access different data.." really cant get anything out of this, any ideas? Many thanks AEA From bahamutzero8825 at gmail.com Thu May 23 12:40:49 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 23 May 2013 11:40:49 -0500 Subject: Accessing Json data (I think I am nearly there) complete beginner In-Reply-To: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> References: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Message-ID: <519E4691.4060600@gmail.com> On 2013.05.23 11:09, Andrew Edwards-Adams wrote: > I was recommended to use the following code to access the Json data directly, however I cannot get it to return anything. Where exactly is the problem? Do you not get JSON back? Do you get the wrong values? Do you get a KeyError or IndexError trying to get values from text1? Are there gremlins going around flipping bits in memory? It's good that you posted code, but "really cant get anything out of this" isn't very useful. -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From python at mrabarnett.plus.com Thu May 23 12:53:23 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 May 2013 17:53:23 +0100 Subject: Accessing Json data (I think I am nearly there) complete beginner In-Reply-To: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> References: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Message-ID: <519E4983.40003@mrabarnett.plus.com> On 23/05/2013 17:09, Andrew Edwards-Adams wrote: > Hey guys > I think its worth stating that I have been trying to code for 1 week. > I am trying to access some Json data. My innitial code was the below: > > "import mechanize > import urllib > import re > > def getData(): > post_url = "http://www.tweetnaps.co.uk/leaderboards/leaderboard_json/all_time" > browser = mechanize.Browser() > browser.set_handle_robots(False) > browser.addheaders = [('User-agent', 'Firefox')] > > #These are the parameters you've got from checking with the aforementioned tools > parameters = {'page' : '1', > 'rp' : '10', > 'sortname' : 'total_pl', > 'sortorder' : 'desc'} > #Encode the parameters > data = urllib.urlencode(parameters) > trans_array = browser.open(post_url,data).read().decode('UTF-8') > > #print trans_array > > myfile = open("test.txt", "w") > myfile.write(trans_array) > myfile.close() > > getData() > > raw_input("Complete")" > > I was recommended to use the following code to access the Json data directly, however I cannot get it to return anything. I think the guy that recommended me this method must have got something wrong? Or perhaps I am simply incompetent: > > import mechanize > import urllib > import json > def getData(): > post_url = "http://www.tweetnaps.co.uk/leaderboards/leaderboard_json/current_week" > browser = mechanize.Browser() > browser.set_handle_robots(False) > browser.addheaders = [('User-agent', 'Firefox')] > > #These are the parameters you've got from checking with the aforementioned tools > parameters = {'page' : '1', > 'rp' : '50', > 'sortname' : 'total_pl', > 'sortorder' : 'desc' > } > #Encode the parameters > data = urllib.urlencode(parameters) > trans_array = browser.open(post_url,data).read().decode('UTF-8') > > text1 = json.loads(trans_array) > print text1['rows'][0]['id'] #play around with these values to access different data.. > > getData() > > He told me to "#play around with these values to access different data.." really cant get anything out of this, any ideas? > > Many thanks AEA > I've just tried it. It prints "1048". From aeaprog at gmail.com Thu May 23 12:58:17 2013 From: aeaprog at gmail.com (Andrew Edwards-Adams) Date: Thu, 23 May 2013 09:58:17 -0700 (PDT) Subject: Accessing Json data (I think I am nearly there) complete beginner In-Reply-To: References: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Message-ID: On Thursday, May 23, 2013 5:40:49 PM UTC+1, Andrew Berg wrote: > On 2013.05.23 11:09, Andrew Edwards-Adams wrote: > > > I was recommended to use the following code to access the Json data directly, however I cannot get it to return anything. > > Where exactly is the problem? Do you not get JSON back? Do you get the wrong values? Do you get a KeyError or IndexError trying to get > > values from text1? Are there gremlins going around flipping bits in memory? It's good that you posted code, but "really cant get anything > > out of this" isn't very useful. > > -- > > CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 Hi thanks for the reply Andrew, my first bit of code was heading in the right direction I was managing to pull out the usernames from the JSON, using REGEX. I was told that the second piece of code is the more efficient way to pull data from the JSON. When I say it doesnt do anything I can adjust the areas suggested. But it returns nothing, I just get the "= Restart =" line appear in the python shell. If there was a trackback/debug I might know where to look, but its not yielding errors, its simply yielding nothing. What i dont know, is if it is the code that isnt working, or what I am inputting in the " print text1['rows'][0]['id']" that isnt working. Since this code: import mechanize import urllib import json def getData(): post_url = "http://www.tweetnaps.co.uk/leaderboards/leaderboard_json/current_week" browser = mechanize.Browser() browser.set_handle_robots(False) browser.addheaders = [('User-agent', 'Firefox')] #These are the parameters you've got from checking with the aforementioned tools parameters = {'page' : '1', 'rp' : '50', 'sortname' : 'total_pl', 'sortorder' : 'desc' } #Encode the parameters data = urllib.urlencode(parameters) trans_array = browser.open(post_url,data).read().decode('UTF-8') appears in both examples and my first attempt is definitely returning data from the JSON, I can only assume that the error is in the last two lines of his code, or as stated my imputing incorrect parameters. :S From bahamutzero8825 at gmail.com Thu May 23 13:59:54 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 23 May 2013 12:59:54 -0500 Subject: Accessing Json data (I think I am nearly there) complete beginner In-Reply-To: References: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Message-ID: <519E591A.6040400@gmail.com> On 2013.05.23 11:58, Andrew Edwards-Adams wrote: > If there was a trackback/debug I might know where to look, but its not yielding errors, its simply yielding nothing. What i dont know, is if it is the code that isnt working, or what I am inputting in the " print text1['rows'][0]['id']" that isnt working. If fed a valid JSON object, json.loads() will return a regular dictionary. You can print (or pretty print with the pprint module) text1 to see everything. If you're not familiar with dictionaries and lists, thoroughly read the tutorial before writing or modifying any more code. http://docs.python.org/2/library/json.html#json.loads -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From bahamutzero8825 at gmail.com Thu May 23 14:11:28 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 23 May 2013 13:11:28 -0500 Subject: Accessing Json data (I think I am nearly there) complete beginner In-Reply-To: References: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Message-ID: <519E5BD0.5080303@gmail.com> On 2013.05.23 11:58, Andrew Edwards-Adams wrote: > Hi thanks for the reply Andrew, my first bit of code was heading in the right direction I was managing to pull out the usernames from the JSON, using REGEX. It's also worth mentioning that regexes are almost always the wrong tool, especially for standardized formats like JSON. They can be very useful when nothing saner will get the job done, but are a nasty mess with no real benefit otherwise. -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From aeaprog at gmail.com Thu May 23 14:19:49 2013 From: aeaprog at gmail.com (Andrew Edwards-Adams) Date: Thu, 23 May 2013 11:19:49 -0700 (PDT) Subject: Accessing Json data (I think I am nearly there) complete beginner In-Reply-To: References: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Message-ID: On Thursday, May 23, 2013 7:11:28 PM UTC+1, Andrew Berg wrote: > On 2013.05.23 11:58, Andrew Edwards-Adams wrote: > > > Hi thanks for the reply Andrew, my first bit of code was heading in the right direction I was managing to pull out the usernames from the JSON, using REGEX. > > It's also worth mentioning that regexes are almost always the wrong tool, especially for standardized formats like JSON. They can be very > > useful when nothing saner will get the job done, but are a nasty mess with no real benefit otherwise. > > -- > > CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 Thanks Andrew, yes I believe this is what the guy who provided me with the code was thinking. I was about to embark on what was definitely going to be an inefficient long regex. From breamoreboy at yahoo.co.uk Thu May 23 14:56:19 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 23 May 2013 19:56:19 +0100 Subject: Accessing Json data (I think I am nearly there) complete beginner In-Reply-To: References: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Message-ID: On 23/05/2013 19:19, Andrew Edwards-Adams wrote: > On Thursday, May 23, 2013 7:11:28 PM UTC+1, Andrew Berg wrote: >> On 2013.05.23 11:58, Andrew Edwards-Adams wrote: >> >>> Hi thanks for the reply Andrew, my first bit of code was heading in the right direction I was managing to pull out the usernames from the JSON, using REGEX. >> >> It's also worth mentioning that regexes are almost always the wrong tool, especially for standardized formats like JSON. They can be very >> >> useful when nothing saner will get the job done, but are a nasty mess with no real benefit otherwise. >> >> -- >> >> CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 > > Thanks Andrew, yes I believe this is what the guy who provided me with the code was thinking. I was about to embark on what was definitely going to be an inefficient long regex. > Funny old world, there doesn't appear to be working code but someone is already thinking about definite inefficiency. What evidence do you have to support this claim? -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From aeaprog at gmail.com Thu May 23 17:30:05 2013 From: aeaprog at gmail.com (Andrew Edwards-Adams) Date: Thu, 23 May 2013 14:30:05 -0700 (PDT) Subject: Accessing Json data (I think I am nearly there) complete beginner In-Reply-To: References: <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com> Message-ID: On Thursday, May 23, 2013 7:56:19 PM UTC+1, Mark Lawrence wrote: > On 23/05/2013 19:19, Andrew Edwards-Adams wrote: > > > On Thursday, May 23, 2013 7:11:28 PM UTC+1, Andrew Berg wrote: > > >> On 2013.05.23 11:58, Andrew Edwards-Adams wrote: > > >> > > >>> Hi thanks for the reply Andrew, my first bit of code was heading in the right direction I was managing to pull out the usernames from the JSON, using REGEX. > > >> > > >> It's also worth mentioning that regexes are almost always the wrong tool, especially for standardized formats like JSON. They can be very > > >> > > >> useful when nothing saner will get the job done, but are a nasty mess with no real benefit otherwise. > > >> > > >> -- > > >> > > >> CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 > > > > > > Thanks Andrew, yes I believe this is what the guy who provided me with the code was thinking. I was about to embark on what was definitely going to be an inefficient long regex. > > > > > > > Funny old world, there doesn't appear to be working code but someone is > > already thinking about definite inefficiency. What evidence do you have > > to support this claim? > > > > -- > > If you're using GoogleCrap? please read this > > http://wiki.python.org/moin/GoogleGroupsPython. > > > > Mark Lawrence haha true Mark, I have made mistakes like this before become ok at coding VBA to achieve things in the most inefficient manner possible. Something that I will learn from for the current python coding project. From anadionisio257 at gmail.com Thu May 23 12:28:44 2013 From: anadionisio257 at gmail.com (=?ISO-8859-1?Q?Ana_Dion=EDsio?=) Date: Thu, 23 May 2013 09:28:44 -0700 (PDT) Subject: Pulp problem _dummy Message-ID: Hello! I'm using pulp for linear optimization but when I run my code I get the following output: Central Optimization: MAXIMIZE 0*__dummy + 0 SUBJECT TO _C1: 2 T0 >= 0 _C2: 2 T0 <= 0 _C3: 0.0686928673545 Frigorifico0 >= 0 _C4: 0.0686928673545 Frigorifico0 <= 0 _C5: 2 Termo0 >= 0 _C6: 2 Termo0 <= 0 _C7: 0.0686928673545 F0 >= 0 _C8: 0.0686928673545 F0 <= 0 _C9: 2 T1 >= 0 _C10: 2 T1 <= 0 _C11: 0.0686928673545 Frigorifico1 >= 0 _C12: 0.0686928673545 Frigorifico1 <= 0 _C13: 2 Termo0 >= 0 _C14: 2 Termo0 <= 0 _C15: 0.0686928673545 F0 >= 0 _C16: 0.0686928673545 F0 <= 0 _C17: - AC0 - VE0 - 2 T0 - 0.0686928673545 F0 - AC1 - VE1 - 2 T1 - 0.0686928673545 F1 >= -15 VARIABLES AC0 <= 2.75 Continuous VE0 = 0 Continuous 0 <= T0 <= 1 Integer 0 <= F0 <= 1 Integer AC1 <= 2.75 Continuous VE1 = 0 Continuous 0 <= T1 <= 1 Integer 0 <= F1 <= 1 Integer __dummy = 0 Continuous I don't know if this is enough information for you but if you couold help me I would be very very grateful Thank you! From m.gilson1 at gmail.com Thu May 23 14:52:07 2013 From: m.gilson1 at gmail.com (Matthew Gilson) Date: Thu, 23 May 2013 14:52:07 -0400 Subject: Non-identifiers in dictionary keys for **expression syntax Message-ID: <519E6557.6050405@gmail.com> This is a question regarding the documentation around dictionary unpacking. The documentation for the call syntax (http://docs.python.org/3/reference/expressions.html#grammar-token-call) says: "If the syntax **expression appears in the function call, expression must evaluate to a mapping, the contents of which are treated as additional keyword arguments." That's fine, but what is a keyword argument? According to the glossary (http://docs.python.org/3.3/glossary.html): /"keyword argument/: an argument preceded by an identifier (e.g. name=) in a function call or passed as a value in a dictionary preceded by **." As far as I'm concerned, this leads to some ambiguity in whether the keys of the mapping need to be valid identifiers or not. Using Cpython, we can do the following: def func(**kwargs): print kwargs d = {'foo bar baz':3} So that might lead us to believe that the keys of the mapping do not need to be valid identifiers. However, the previous function does not work with the following dictionary: d = {1:3} because not all the keys are strings. Is there a way to petition to get this more rigorously defined? Thanks, ~Matt -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Thu May 23 15:20:44 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 23 May 2013 19:20:44 GMT Subject: Non-identifiers in dictionary keys for **expression syntax References: Message-ID: On 2013-05-23, Matthew Gilson wrote: > That's fine, but what is a keyword argument? According to the glossary > (http://docs.python.org/3.3/glossary.html): > > /"keyword argument/: an argument preceded by an identifier (e.g. name=) > in a function call or passed as a value in a dictionary preceded by **." > > As far as I'm concerned, this leads to some ambiguity in > whether the keys of the mapping need to be valid identifiers or > not. I don't see any ambiguity. A keyword argument is an argument preceded by an identifier according to the definition. Where are you perceiving wiggle room? -- Neil Cerutti From ethan at stoneleaf.us Thu May 23 15:37:18 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 23 May 2013 12:37:18 -0700 Subject: Non-identifiers in dictionary keys for **expression syntax In-Reply-To: References: Message-ID: <519E6FEE.403@stoneleaf.us> On 05/23/2013 12:20 PM, Neil Cerutti wrote: > On 2013-05-23, Matthew Gilson wrote: >> >> That's fine, but what is a keyword argument? According to the glossary >> (http://docs.python.org/3.3/glossary.html): >> >> /"keyword argument/: an argument preceded by an identifier (e.g. name=) >> in a function call or passed as a value in a dictionary preceded by **." >> >> As far as I'm concerned, this leads to some ambiguity in >> whether the keys of the mapping need to be valid identifiers or >> not. > > I don't see any ambiguity. A keyword argument is an argument > preceded by an identifier according to the definition. Where are > you perceiving wiggle room? --> def func(**kwargs): ... print(kwargs) ... --> d = {'foo bar baz':3} --> func(**d) {'foo bar baz': 3} Even though 'foo bar baz' is not a valid identifier, and could not be passed as `func(foo bar baz = 3)`, it still worked when going through a dict. -- ~Ethan~ From m.gilson1 at gmail.com Thu May 23 16:49:19 2013 From: m.gilson1 at gmail.com (Matthew Gilson) Date: Thu, 23 May 2013 16:49:19 -0400 Subject: Non-identifiers in dictionary keys for **expression syntax In-Reply-To: References: Message-ID: <519E80CF.4060803@gmail.com> On 05/23/2013 03:20 PM, Neil Cerutti wrote: > On 2013-05-23, Matthew Gilson wrote: >> That's fine, but what is a keyword argument? According to the glossary >> (http://docs.python.org/3.3/glossary.html): >> >> /"keyword argument/: an argument preceded by an identifier (e.g. name=) >> in a function call or passed as a value in a dictionary preceded by **." >> >> As far as I'm concerned, this leads to some ambiguity in >> whether the keys of the mapping need to be valid identifiers or >> not. > I don't see any ambiguity. A keyword argument is an argument > preceded by an identifier according to the definition. Where are > you perceiving wiggle room? > The wiggle room comes from the "or passed as a value in a dictionary" clause. We sort of get caught in a infinite loop there because the stuff that can be passed in a dictionary is a keyword which is an identifer=expression or something passed as a value in a dictionary ... Also the fact that: func(**{"foo bar baz":1}) works even though `foo bar baz` isn't a valid identifier, but: func(**{1:3}) doesn't work. From ian.g.kelly at gmail.com Thu May 23 16:22:28 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 23 May 2013 14:22:28 -0600 Subject: Non-identifiers in dictionary keys for **expression syntax In-Reply-To: <519E6557.6050405@gmail.com> References: <519E6557.6050405@gmail.com> Message-ID: On Thu, May 23, 2013 at 12:52 PM, Matthew Gilson wrote: > Using Cpython, we can do the following: > > def func(**kwargs): > print kwargs > > d = {'foo bar baz':3} > > So that might lead us to believe that the keys of the mapping do not need to > be valid identifiers. However, the previous function does not work with the > following dictionary: > > d = {1:3} > > because not all the keys are strings. Is there a way to petition to get > this more rigorously defined? The string requirement is probably for optimization, but if the argument is both sent and received using the ** syntax, is there a good reason why only identifiers should be allowed? From tjreedy at udel.edu Thu May 23 16:52:11 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Thu, 23 May 2013 16:52:11 -0400 Subject: Non-identifiers in dictionary keys for **expression syntax In-Reply-To: <519E6557.6050405@gmail.com> References: <519E6557.6050405@gmail.com> Message-ID: On 5/23/2013 2:52 PM, Matthew Gilson wrote: > This is a question regarding the documentation around dictionary > unpacking. The documentation for the call syntax > (http://docs.python.org/3/reference/expressions.html#grammar-token-call) > says: > > "If the syntax **expression appears in the function call, expression > must evaluate to a mapping, the contents of which are treated as > additional keyword arguments." > > That's fine, but what is a keyword argument? According to the glossary > (http://docs.python.org/3.3/glossary.html): > > /"keyword argument/: an argument preceded by an identifier (e.g. name=) > in a function call or passed as a value in a dictionary preceded by **." It appears that the requirement has been relaxed (in the previous quote), so that 'dictionary' should also be changed to 'mapping'. It might not hurt to add 'The key for the value should be an identifier.' > > As far as I'm concerned, this leads to some ambiguity in whether the > keys of the mapping need to be valid identifiers or not. I think you are being too lawyerly. The pretty clear and sensible implication is that the key for the value should be a string with a valid identifier. If it is anything else, you are on your own and deserve any joy or pain that results ;=) > Using Cpython, we can do the following: > > def func(**kwargs): > print kwargs > > d = {'foo bar baz':3} > > So that might lead us to believe that the keys of the mapping do not > need to be valid identifiers. There are two ways to pass args to func to be gathered into kwargs; explicit key=val pairs and **mapping, or both. func(a=1, b='hi', **{'foo bar baz':3}) # {'foo bar baz': 3, 'a': 1, 'b': 'hi'} So func should not expect anything other than identifier strings. However, the previous function does not > work with the following dictionary: > > d = {1:3} > > because not all the keys are strings. So CPython checks that keys are strings, because that is cheap, but not that the strings are identifiers, because that would be more expensive. Just because an implementation allow somethings (omits a check) for efficiency does not mean you should do it. globals()[1] = 1 works, but is not normally very sensible or useful. > Is there a way to petition to get this more rigorously defined? bugs.python.org The problem is that mandating a rigorous check by implementations makes Python slower to the detriment of sensible programmers. From m.gilson1 at gmail.com Thu May 23 17:15:15 2013 From: m.gilson1 at gmail.com (Matthew Gilson) Date: Thu, 23 May 2013 17:15:15 -0400 Subject: Non-identifiers in dictionary keys for **expression syntax In-Reply-To: References: <519E6557.6050405@gmail.com> Message-ID: <519E86E3.5000409@gmail.com> On 05/23/2013 04:52 PM, Terry Jan Reedy wrote: > On 5/23/2013 2:52 PM, Matthew Gilson wrote: >> This is a question regarding the documentation around dictionary >> unpacking. The documentation for the call syntax >> (http://docs.python.org/3/reference/expressions.html#grammar-token-call) >> says: >> >> "If the syntax **expression appears in the function call, expression >> must evaluate to a mapping, the contents of which are treated as >> additional keyword arguments." >> >> That's fine, but what is a keyword argument? According to the glossary >> (http://docs.python.org/3.3/glossary.html): >> >> /"keyword argument/: an argument preceded by an identifier (e.g. name=) >> in a function call or passed as a value in a dictionary preceded by **." > > It appears that the requirement has been relaxed (in the previous > quote), so that 'dictionary' should also be changed to 'mapping'. It > might not hurt to add 'The key for the value should be an identifier.' >> >> As far as I'm concerned, this leads to some ambiguity in whether the >> keys of the mapping need to be valid identifiers or not. > > I think you are being too lawyerly. The pretty clear and sensible > implication is that the key for the value should be a string with a > valid identifier. If it is anything else, you are on your own and > deserve any joy or pain that results ;=) > >> Using Cpython, we can do the following: >> >> def func(**kwargs): >> print kwargs >> >> d = {'foo bar baz':3} >> >> So that might lead us to believe that the keys of the mapping do not >> need to be valid identifiers. > > There are two ways to pass args to func to be gathered into kwargs; > explicit key=val pairs and **mapping, or both. > func(a=1, b='hi', **{'foo bar baz':3}) > # > {'foo bar baz': 3, 'a': 1, 'b': 'hi'} > > So func should not expect anything other than identifier strings. > > However, the previous function does not >> work with the following dictionary: >> >> d = {1:3} >> >> because not all the keys are strings. > > So CPython checks that keys are strings, because that is cheap, but > not that the strings are identifiers, because that would be more > expensive. Just because an implementation allow somethings (omits a > check) for efficiency does not mean you should do it. > > globals()[1] = 1 > works, but is not normally very sensible or useful. > >> Is there a way to petition to get this more rigorously defined? > > bugs.python.org > The problem is that mandating a rigorous check by implementations > makes Python slower to the detriment of sensible programmers To be clear, you're saying that func(**{'foo bar baz':3}) is not supported (officially), but it works in CPython because checking that every string in the dict is a valid identifier would be costly. Of course that is sensible and I don't think the behaviour should be changed "to the detriment of sensible programmers". However, it would be nice if it was documented somewhere that the above function call is something that a non-sensible programmer would do. Perhaps with a "CPython implementation detail" type of block. From sloan949 at gmail.com Thu May 23 16:49:02 2013 From: sloan949 at gmail.com (sloan949 at gmail.com) Date: Thu, 23 May 2013 13:49:02 -0700 (PDT) Subject: suppress newlines in my script Message-ID: <52c74908-8bac-498e-9549-5b9500b152f1@googlegroups.com> I am importing lines from an external csv file and when I iterate through the lines and increment, new lines are introduced. How would I cut out the newlines. I have attempted several pythonic strip() and rstrip() how can i implent this? import sys, os f=open('europe_csv') lines=f.readlines() BU = 'Company,,,,,,,,,,,,,,' PPP = 'Pre-Prod,,,,,,,,Prod,,,,,,' C1 = ',,,,,,,,,,,,,,' Title = 'Site,Environment,' NET1 = lines[4] GW1 = lines[5] M1 = lines[6] PS1 = lines[7] PE1 = lines[8] C2 = ',,,,,,,,,,,,,,' NET2 = lines[10] GW2 = lines[11] M2 = lines[12] PS2 = lines[13] PE2 = lines[14] for count in range(64, 127): print NET1.format(count) print GW1.format(count) print M1 print PS1.format(count) print PE1.format(count) print C2 print NET2.format(count) print GW2.format(count) print M2 print PS2.format(count) print PE2.format(count) From sloan949 at gmail.com Fri May 24 09:59:43 2013 From: sloan949 at gmail.com (sloan949 at gmail.com) Date: Fri, 24 May 2013 06:59:43 -0700 (PDT) Subject: suppress newlines in my script In-Reply-To: <52c74908-8bac-498e-9549-5b9500b152f1@googlegroups.com> References: <52c74908-8bac-498e-9549-5b9500b152f1@googlegroups.com> Message-ID: On Thursday, May 23, 2013 1:49:02 PM UTC-7, sloa... at gmail.com wrote: > I am importing lines from an external csv file and when I iterate through the lines and increment, new lines are introduced. > > How would I cut out the newlines. I have attempted several pythonic strip() and rstrip() how can i implent this? > > > > > > import sys, os > > > > f=open('europe_csv') > > lines=f.readlines() > > > > BU = 'Company,,,,,,,,,,,,,,' > > PPP = 'Pre-Prod,,,,,,,,Prod,,,,,,' > > C1 = ',,,,,,,,,,,,,,' > > Title = 'Site,Environment,' > > NET1 = lines[4] > > GW1 = lines[5] > > M1 = lines[6] > > PS1 = lines[7] > > PE1 = lines[8] > > C2 = ',,,,,,,,,,,,,,' > > NET2 = lines[10] > > GW2 = lines[11] > > M2 = lines[12] > > PS2 = lines[13] > > PE2 = lines[14] > > > > > > > > for count in range(64, 127): > > print NET1.format(count) > > print GW1.format(count) > > print M1 > > print PS1.format(count) > > print PE1.format(count) > > print C2 > > print NET2.format(count) > > print GW2.format(count) > > print M2 > > print PS2.format(count) > > print PE2.format(count) Thanks for the tip about the CSV module. I did not know about that. Here are two lines from the CSV file: ,,172.20.{0}.0/27,172.20.{0}.32/27,172.20.{0}.64/27,29,172.20.{0}.96/27,,,,172.21.{0}.0/27,172.21.{0}.32/27,172.21.{0}.64/27,29,172.21.{0}.96/27 GW:,,172.20.{0}.1,172.20.{0}.33,172.20.{0}.65,,172.20.{0}.97,,GW:,,172.21.{0}.1,172.21.{0}.33,172.21.{0}.65,,172.21.{0}.97 This is the output: ,,,,,,,,,,,,,, ,,,,,,,,,,,,,, GW:,,172.20.126.129,172.20.126.161,172.20.126.193,,172.20.126.225,,GW:,,172.21.126.129,172.21.126.161,172.21.126.193,,172.21.126.225 '''''''''''''''''' There are blank lines between and I am assuming that these are from newlines being introduced. The idea of this script is/was to abstract the csv data so that I dont need to place them in the file as there are many seperate CSV files to import. I hope I answered your questions. The majority of the script is below. #!/usr/bin/python import sys, os #import pdb #pdb.set_trace() f=open('europe_germanyfinalcsv') lines=f.readlines() BU = 'Europe Germany,,,,,,,,,,,,,,' PPP = 'Pre-Prod,,,,,,,,Prod,,,,,,' C1 = ',,,,,,,,,,,,,,' Title = 'Site,Environment,vApp Web,vApp App,vApp Data,Hosts Per Net,Reserved,,Site,Environment,vApp Web,vApp App,vApp Data,Hosts Per Net,Reserved' NET1 = lines[4] GW1 = lines[5] M1 = lines[6] PS1 = lines[7] PE1 = lines[8] for count in range(64, 127): print NET1.format(count) print GW1.format(count) print M1 print PS1.format(count) print PE1.format(count) From davea at davea.name Fri May 24 15:24:26 2013 From: davea at davea.name (Dave Angel) Date: Fri, 24 May 2013 15:24:26 -0400 Subject: suppress newlines in my script In-Reply-To: References: <52c74908-8bac-498e-9549-5b9500b152f1@googlegroups.com> Message-ID: <519FBE6A.2070908@davea.name> On 05/24/2013 09:59 AM, sloan949 at gmail.com wrote: > >> print PE2.format(count) > > Thanks for the tip about the CSV module. I did not know about that. So why aren't you using it? There's not much point in solving "the newlines problem" if you're going to later switch to a library which doesn't have the problem. The csv module will eliminate the newlines, and the commas, organizing your data for you directly (assuming this really is a csv file, and that the separator is comma). None of your present code seems to believe it's a csv file, so maybe that's just a misdirection. -- DaveA From jsf80238 at gmail.com Fri May 24 23:49:24 2013 From: jsf80238 at gmail.com (Jason Friedman) Date: Fri, 24 May 2013 21:49:24 -0600 Subject: suppress newlines in my script In-Reply-To: References: <52c74908-8bac-498e-9549-5b9500b152f1@googlegroups.com> Message-ID: > Here are two lines from the CSV file: > ,,172.20.{0}.0/27,172.20.{0}.32/27,172.20.{0}.64/27,29,172.20.{0}.96/27,,,,172.21.{0}.0/27,172.21.{0}.32/27,172.21.{0}.64/27,29,172.21.{0}.96/27 > GW:,,172.20.{0}.1,172.20.{0}.33,172.20.{0}.65,,172.20.{0}.97,,GW:,,172.21.{0}.1,172.21.{0}.33,172.21.{0}.65,,172.21.{0}.97 > > This is the output: > ,,,,,,,,,,,,,, > > ,,,,,,,,,,,,,, > GW:,,172.20.126.129,172.20.126.161,172.20.126.193,,172.20.126.225,,GW:,,172.21.126.129,172.21.126.161,172.21.126.193,,172.21.126.225 > > '''''''''''''''''' When you say "this is the output" do you mean that is what you are getting or that is what you want? If that is what you are getting please reply with what you want for output. From avazquezr at grm.uci.cu Thu May 23 19:36:21 2013 From: avazquezr at grm.uci.cu (avazquezr at grm.uci.cu) Date: Thu, 23 May 2013 16:36:21 -0700 Subject: authentication with python-ldap In-Reply-To: References: Message-ID: <20130523163621.Horde.5xisTMkLBHdRnqf13O6lQRA@correo.grm.uci.cu> import ldap conn = ldap.initialize("ldap://ldap.uci.cu") conn.protocol_version = ldap.VERSION3 conn.simple_bind_s( "uid=xxx,dc=uci,dc=cu", "xxx" ) Result: Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 207, in simple_bind_s return self.result(msgid,all=1,timeout=self.timeout) File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 422, in result res_type,res_data,res_msgid = self.result2(msgid,all,timeout) File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 426, in result2 res_type, res_data, res_msgid, srv_ctrls = self.result3(msgid,all,timeout) File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 432, in result3 ldap_result = self._ldap_call(self._l.result3,msgid,all,timeout) File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 96, in _ldap_call result = func(*args,**kwargs) INVALID_CREDENTIALS: {'desc': 'Invalid credentials'} what is my mistake ???? From dieter at handshake.de Sat May 25 02:01:01 2013 From: dieter at handshake.de (dieter) Date: Sat, 25 May 2013 08:01:01 +0200 Subject: authentication with python-ldap References: <20130523163621.Horde.5xisTMkLBHdRnqf13O6lQRA@correo.grm.uci.cu> Message-ID: <87vc67ppia.fsf@handshake.de> avazquezr at grm.uci.cu writes: > import ldap > conn = ldap.initialize("ldap://ldap.uci.cu") > conn.protocol_version = ldap.VERSION3 > conn.simple_bind_s( "uid=xxx,dc=uci,dc=cu", "xxx" ) > > Result: > > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line > 207, in simple_bind_s > return self.result(msgid,all=1,timeout=self.timeout) > File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line > 422, in result > res_type,res_data,res_msgid = self.result2(msgid,all,timeout) > File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line > 426, in result2 > res_type, res_data, res_msgid, srv_ctrls = self.result3(msgid,all,timeout) > File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line > 432, in result3 > ldap_result = self._ldap_call(self._l.result3,msgid,all,timeout) > File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line > 96, in _ldap_call > result = func(*args,**kwargs) > INVALID_CREDENTIALS: {'desc': 'Invalid credentials'} You are accessing a protected operation of the LDAP server and it (the server) rejects it due to invalid credentials. You may have forgotten to pass on credentials (e.g. a password) or the credentials do not fit to the specified user (maybe the user does not exist at all). From jaorozco at estudiantes.uci.cu Sat May 25 14:00:28 2013 From: jaorozco at estudiantes.uci.cu (Jorge Alberto Diaz Orozco) Date: Sat, 25 May 2013 14:00:28 -0400 (EDT) Subject: authentication with python-ldap In-Reply-To: <87vc67ppia.fsf@handshake.de> Message-ID: <36e9845b-4b22-406d-b38c-056341454fd2@ucimail4.uci.cu> I have been doing the same thing and I tried to use java for testing the credentials and they are correct. It works perfectly with java. I really don?t know what we?re doing wrong. You are accessing a protected operation of the LDAP server and it (the server) rejects it due to invalid credentials. You may have forgotten to pass on credentials (e.g. a password) or the credentials do not fit to the specified user (maybe the user does not exist at all). http://www.uci.cu From jcasale at activenetwerx.com Sat May 25 14:25:18 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Sat, 25 May 2013 18:25:18 +0000 Subject: authentication with python-ldap In-Reply-To: <36e9845b-4b22-406d-b38c-056341454fd2@ucimail4.uci.cu> References: <87vc67ppia.fsf@handshake.de>, <36e9845b-4b22-406d-b38c-056341454fd2@ucimail4.uci.cu> Message-ID: > I have been doing the same thing and I tried to use java for testing the credentials and they are correct. It works perfectly with java. > I really don?t know what we?re doing wrong. > > > You are accessing a protected operation of the LDAP server > and it (the server) rejects it due to invalid credentials. > You may have forgotten to pass on credentials (e.g. a password) > or the credentials do not fit to the specified user > (maybe the user does not exist at all). Depending on the directory (which we don't know) and the code as well, the way you auth might be the problem. Possibly Java met the directory requirements with the methods you used whereas they did not with Python given your code. For example, certain operations in AD require specific transports to be used... jlc From dieter at handshake.de Sun May 26 02:00:36 2013 From: dieter at handshake.de (Dieter Maurer) Date: Sun, 26 May 2013 08:00:36 +0200 Subject: authentication with python-ldap In-Reply-To: <36e9845b-4b22-406d-b38c-056341454fd2@ucimail4.uci.cu> References: <87vc67ppia.fsf@handshake.de> <36e9845b-4b22-406d-b38c-056341454fd2@ucimail4.uci.cu> Message-ID: <20897.42244.578422.235154@localhost.localdomain> Jorge Alberto Diaz Orozco wrote at 2013-5-25 14:00 -0400: >I have been doing the same thing and I tried to use java for testing the credentials and they are correct. It works perfectly with java. >I really don?t know what we?re doing wrong. Neither do I. But the error message definitely originates from the LDAP server. This means that the server sees different things for the (successful) Java connection and the (unsuccessful) Python connection. Maybe, you can convince your LDAP server administrator to configure a form of logging that allows you to compare the two requests (this may not be easy - because sensitive information is involved). Comparing the requests may provide valuable clues towards the cause of the problem. One may also try some guesswork: There is an important difference between Java and Python 2. Java uses unicode as the typical type for text variables while in Python 2, you use normally the type "str" for text. "str" means no unicode but encoded text. When the Java-LDAP bridge passes text to the LDAP server, it must encode the text - and maybe, it uses the correct encoding (the one the LDAP server expects). The Python-LDAP bridge, on the other hand, does not get unicode but "str" and likely passes the "str" values directly. Thus, if your "str" values do not use the correct encoding (the one expected by the LDAP server), things will not work out correctly. I expect the LDAP server to expect the "utf-8" encoding. In this case, problems could be expected when the data passed on to the LDAP server contains non ascii characters while all ascii data should not see problems. -- Dieter From notreal at earthlink.net Thu May 23 22:44:38 2013 From: notreal at earthlink.net (JackM) Date: Thu, 23 May 2013 22:44:38 -0400 Subject: Read txt file, add to iptables not working on new host Message-ID: First, let me say that I have no knowledge of or experience with Python or Linux/Unix. I have a script which was written by a host tech person that ran via cron on my old server. It was designed to read IP addresses from a text file and add them to be blocked on iptables. That way, we could add or remove IPs without involving tech support daily. It worked great. Then we changed hosts and this script is now throwing errors on the new server. This host runs Python 2.6.6. This is the script: #!/usr/bin/python import os,time ##Input, Output, and TimeStamp inFile = open('/var/www/html/mydomain.com/banlist.txt','r') logFile = open('/var/log/banList.log','w') stamp = time.asctime(time.localtime()) ##Daily Flush of blockList rules before re-applying Blocks os.popen('/sbin/iptables -F INPUT') logFile.write(stamp), logFile.write('\n'), logFile.write('Flushing Rules..\n') ##Loop to read in file and Apply rules to IPtables for line in inFile.readlines(): tmp = line.split(';') IP = tmp[0] outPut = os.popen( '/sbin/iptables -A INPUT -s' + ' ' + IP + ' ' + '-j REJECT' ) logFile.write(IP), logFile.write(' - Has been blocked '), logFile.write(stamp),logFile.write The errors we're getting are like these: Bad argument `174.37.65.204' Try `iptables -h' or 'iptables --help' for more information. Bad argument `94.159.162.182' Try `iptables -h' or 'iptables --help' for more information. Bad argument `95.134.132.98' Try `iptables -h' or 'iptables --help' for more information. etc. Entries from the banlist.txt are like these: 200.193.54.138; February 9, 2013, 7:42 am
87.120.57.4; February 9, 2013, 7:42 am
82.206.129.160; February 9, 2013, 7:43 am
etc. I know the error points to a bad iptables command. Can someone tell me what change(s) I need to make to this script to get it working again? Thanks. -- My email address on the header is a non-monitored spam catching account. I can be reached via http://www.wvnh.net/contact.htm From carlosnepomuceno at outlook.com Thu May 23 23:10:48 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 06:10:48 +0300 Subject: Read txt file, add to iptables not working on new host In-Reply-To: References: Message-ID: Send the output of the following commands: uname -a /sbin/iptables -V ---------------------------------------- > From: notreal at earthlink.net > Subject: Read txt file, add to iptables not working on new host > Date: Thu, 23 May 2013 22:44:38 -0400 > To: python-list at python.org > > First, let me say that I have no knowledge of or experience with Python > or Linux/Unix. I have a script which was written by a host tech person > that ran via cron on my old server. It was designed to read IP addresses > from a text file and add them to be blocked on iptables. That way, we > could add or remove IPs without involving tech support daily. It worked > great. > > Then we changed hosts and this script is now throwing errors on the new > server. This host runs Python 2.6.6. This is the script: > > #!/usr/bin/python > import os,time > > ##Input, Output, and TimeStamp > inFile = open('/var/www/html/mydomain.com/banlist.txt','r') > logFile = open('/var/log/banList.log','w') > stamp = time.asctime(time.localtime()) > > > ##Daily Flush of blockList rules before re-applying Blocks > os.popen('/sbin/iptables -F INPUT') > logFile.write(stamp), logFile.write('\n'), logFile.write('Flushing > Rules..\n') > > ##Loop to read in file and Apply rules to IPtables > for line in inFile.readlines(): > tmp = line.split(';') > IP = tmp[0] > outPut = os.popen( '/sbin/iptables -A INPUT -s' + ' ' + IP + ' ' + '-j > REJECT' ) > logFile.write(IP), logFile.write(' - Has been blocked '), > logFile.write(stamp),logFile.write > > > The errors we're getting are like these: > > Bad argument `174.37.65.204' > Try `iptables -h' or 'iptables --help' for more information. > Bad argument `94.159.162.182' > Try `iptables -h' or 'iptables --help' for more information. > Bad argument `95.134.132.98' > Try `iptables -h' or 'iptables --help' for more information. > etc. > > Entries from the banlist.txt are like these: > > 200.193.54.138; February 9, 2013, 7:42 am
> 87.120.57.4; February 9, 2013, 7:42 am
> 82.206.129.160; February 9, 2013, 7:43 am
> etc. > > I know the error points to a bad iptables command. > Can someone tell me what change(s) I need to make to this script to get > it working again? Thanks. > > > > -- > My email address on the header is a non-monitored spam catching account. > I can be reached via http://www.wvnh.net/contact.htm > > -- > http://mail.python.org/mailman/listinfo/python-list From notreal at earthlink.net Fri May 24 09:08:26 2013 From: notreal at earthlink.net (JackM) Date: Fri, 24 May 2013 09:08:26 -0400 Subject: Read txt file, add to iptables not working on new host In-Reply-To: References: Message-ID: Thanks for answering. Do you mean something like this? outPut = os.popen('uname -a' '/sbin/iptables -V INPUT -s' + ' ' + IP + ' ' + '-j REJECT' ) Sorry but like I said, I have no experience with any of this. On 5/23/2013 11:10 PM, Carlos Nepomuceno wrote: > Send the output of the following commands: > > uname -a > /sbin/iptables -V > > > ---------------------------------------- >> From: notreal at earthlink.net >> Subject: Read txt file, add to iptables not working on new host >> Date: Thu, 23 May 2013 22:44:38 -0400 >> To: python-list at python.org >> >> First, let me say that I have no knowledge of or experience with Python >> or Linux/Unix. I have a script which was written by a host tech person >> that ran via cron on my old server. It was designed to read IP addresses >> from a text file and add them to be blocked on iptables. That way, we >> could add or remove IPs without involving tech support daily. It worked >> great. >> >> Then we changed hosts and this script is now throwing errors on the new >> server. This host runs Python 2.6.6. This is the script: >> >> #!/usr/bin/python >> import os,time >> >> ##Input, Output, and TimeStamp >> inFile = open('/var/www/html/mydomain.com/banlist.txt','r') >> logFile = open('/var/log/banList.log','w') >> stamp = time.asctime(time.localtime()) >> >> >> ##Daily Flush of blockList rules before re-applying Blocks >> os.popen('/sbin/iptables -F INPUT') >> logFile.write(stamp), logFile.write('\n'), logFile.write('Flushing >> Rules..\n') >> >> ##Loop to read in file and Apply rules to IPtables >> for line in inFile.readlines(): >> tmp = line.split(';') >> IP = tmp[0] >> outPut = os.popen( '/sbin/iptables -A INPUT -s' + ' ' + IP + ' ' + '-j >> REJECT' ) >> logFile.write(IP), logFile.write(' - Has been blocked '), >> logFile.write(stamp),logFile.write >> >> >> The errors we're getting are like these: >> >> Bad argument `174.37.65.204' >> Try `iptables -h' or 'iptables --help' for more information. >> Bad argument `94.159.162.182' >> Try `iptables -h' or 'iptables --help' for more information. >> Bad argument `95.134.132.98' >> Try `iptables -h' or 'iptables --help' for more information. >> etc. >> >> Entries from the banlist.txt are like these: >> >> 200.193.54.138; February 9, 2013, 7:42 am
>> 87.120.57.4; February 9, 2013, 7:42 am
>> 82.206.129.160; February 9, 2013, 7:43 am
>> etc. >> >> I know the error points to a bad iptables command. >> Can someone tell me what change(s) I need to make to this script to get >> it working again? Thanks. >> >> >> >> -- >> My email address on the header is a non-monitored spam catching account. >> I can be reached via http://www.wvnh.net/contact.htm >> >> -- From carlosnepomuceno at outlook.com Fri May 24 09:15:45 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 16:15:45 +0300 Subject: Read txt file, add to iptables not working on new host In-Reply-To: References: , , Message-ID: No, there's no need to change your python script, although it can be improved because as it is it may flush (delete all) iptables rules and let you vulnerable and don't create the new rules. All you need to do is enter the commands in the shell and send it's output. The 'iptables' have changed. ---------------------------------------- > From: notreal at earthlink.net > Subject: Re: Read txt file, add to iptables not working on new host > Date: Fri, 24 May 2013 09:08:26 -0400 > To: python-list at python.org > > Thanks for answering. Do you mean something like this? > > outPut = os.popen('uname -a' '/sbin/iptables -V INPUT -s' + ' ' + IP + ' > ' + '-j REJECT' ) > > Sorry but like I said, I have no experience with any of this. > > > > On 5/23/2013 11:10 PM, Carlos Nepomuceno wrote: >> Send the output of the following commands: >> >> uname -a >> /sbin/iptables -V >> >> >> ---------------------------------------- >>> From: notreal at earthlink.net >>> Subject: Read txt file, add to iptables not working on new host >>> Date: Thu, 23 May 2013 22:44:38 -0400 >>> To: python-list at python.org >>> >>> First, let me say that I have no knowledge of or experience with Python >>> or Linux/Unix. I have a script which was written by a host tech person >>> that ran via cron on my old server. It was designed to read IP addresses >>> from a text file and add them to be blocked on iptables. That way, we >>> could add or remove IPs without involving tech support daily. It worked >>> great. >>> >>> Then we changed hosts and this script is now throwing errors on the new >>> server. This host runs Python 2.6.6. This is the script: >>> >>> #!/usr/bin/python >>> import os,time >>> >>> ##Input, Output, and TimeStamp >>> inFile = open('/var/www/html/mydomain.com/banlist.txt','r') >>> logFile = open('/var/log/banList.log','w') >>> stamp = time.asctime(time.localtime()) >>> >>> >>> ##Daily Flush of blockList rules before re-applying Blocks >>> os.popen('/sbin/iptables -F INPUT') >>> logFile.write(stamp), logFile.write('\n'), logFile.write('Flushing >>> Rules..\n') >>> >>> ##Loop to read in file and Apply rules to IPtables >>> for line in inFile.readlines(): >>> tmp = line.split(';') >>> IP = tmp[0] >>> outPut = os.popen( '/sbin/iptables -A INPUT -s' + ' ' + IP + ' ' + '-j >>> REJECT' ) >>> logFile.write(IP), logFile.write(' - Has been blocked '), >>> logFile.write(stamp),logFile.write >>> >>> >>> The errors we're getting are like these: >>> >>> Bad argument `174.37.65.204' >>> Try `iptables -h' or 'iptables --help' for more information. >>> Bad argument `94.159.162.182' >>> Try `iptables -h' or 'iptables --help' for more information. >>> Bad argument `95.134.132.98' >>> Try `iptables -h' or 'iptables --help' for more information. >>> etc. >>> >>> Entries from the banlist.txt are like these: >>> >>> 200.193.54.138; February 9, 2013, 7:42 am
>>> 87.120.57.4; February 9, 2013, 7:42 am
>>> 82.206.129.160; February 9, 2013, 7:43 am
>>> etc. >>> >>> I know the error points to a bad iptables command. >>> Can someone tell me what change(s) I need to make to this script to get >>> it working again? Thanks. >>> >>> >>> >>> -- >>> My email address on the header is a non-monitored spam catching account. >>> I can be reached via http://www.wvnh.net/contact.htm >>> >>> -- > > > > > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Fri May 24 09:54:05 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 23:54:05 +1000 Subject: Read txt file, add to iptables not working on new host In-Reply-To: References: Message-ID: On Fri, May 24, 2013 at 12:44 PM, JackM wrote: > outPut = os.popen( '/sbin/iptables -A INPUT -s' + ' ' + IP + ' ' + > '-j REJECT' ) There's so much about this script that's less than Pythonic, but the one thing I'd really like to see is a log of the exact command being executed. Replace the above line with this: command = '/sbin/iptables -A INPUT -s' + ' ' + IP + ' ' + '-j REJECT' outPut = os.popen(command) logFile.write(command+"\n") That will show, in your log, exactly what's being executed. You should then be able to execute that command in the shell and see the exact same result. That might also show you the problem - it might be obvious from the commands logged. If that doesn't work, here's a rewrite of your code for cleanliness, which still does what I think your original code does. See if they act differently... -- cut -- #!/usr/bin/python import os import time # Input, Output, and TimeStamp inFile = open('/var/www/html/mydomain.com/banlist.txt','r') logFile = open('/var/log/banList.log','w') stamp = time.asctime(time.localtime()) # Daily Flush of blockList rules before re-applying Blocks os.popen('/sbin/iptables -F INPUT') logFile.write(stamp+'\nFlushing Rules..\n') # Loop to read in file and Apply rules to IPtables for line in inFile: # TODO: Use 'with' for a bit of protection ip = line.split(';')[0] output = os.popen( '/sbin/iptables -A INPUT -s ' + ip + ' -j REJECT' ) logFile.write(IP+' - Has been blocked\n') -- cut -- Since the timestamp doesn't change across a run anyway, there's not much point printing it out every time, and I'm also putting newlines in the logfile. Beyond that, it should function the same way as the original. ChrisA From notreal at earthlink.net Fri May 24 12:32:40 2013 From: notreal at earthlink.net (JackM) Date: Fri, 24 May 2013 12:32:40 -0400 Subject: Read txt file, add to iptables not working on new host In-Reply-To: References: Message-ID: So Chris, does this version look better? Changed to inFile to with. #!/usr/bin/python import os import time # Input, Output, and TimeStamp logFile = open('/var/www/html/statistics/logs/banList.log','w') stamp = time.asctime(time.localtime()) # Daily Flush of blockList rules before re-applying Blocks os.popen('/sbin/iptables -F INPUT') logFile.write(stamp+'\nFlushing Rules..\n') # Loop to read in file and Apply rules to IPtables with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile: for line in inFile: # TODO: Use 'with' for a bit of protection ip = line.split(';')[0] output = os.popen( '/sbin/iptables -A INPUT -s ' + ip + ' -j REJECT' ) logFile.write(ip+' - Has been blocked\n') On 5/24/2013 9:54 AM, Chris Angelico wrote: > On Fri, May 24, 2013 at 12:44 PM, JackM wrote: >> outPut = os.popen( '/sbin/iptables -A INPUT -s' + ' ' + IP + ' ' + >> '-j REJECT' ) > > There's so much about this script that's less than Pythonic, but the > one thing I'd really like to see is a log of the exact command being > executed. Replace the above line with this: > > command = '/sbin/iptables -A INPUT -s' + ' ' + IP + ' ' + '-j REJECT' > outPut = os.popen(command) > logFile.write(command+"\n") > > That will show, in your log, exactly what's being executed. You should > then be able to execute that command in the shell and see the exact > same result. That might also show you the problem - it might be > obvious from the commands logged. > > If that doesn't work, here's a rewrite of your code for cleanliness, > which still does what I think your original code does. See if they act > differently... > > -- cut -- > > #!/usr/bin/python > import os > import time > > # Input, Output, and TimeStamp > inFile = open('/var/www/html/mydomain.com/banlist.txt','r') > logFile = open('/var/log/banList.log','w') > stamp = time.asctime(time.localtime()) > > # Daily Flush of blockList rules before re-applying Blocks > os.popen('/sbin/iptables -F INPUT') > logFile.write(stamp+'\nFlushing Rules..\n') > > # Loop to read in file and Apply rules to IPtables > for line in inFile: # TODO: Use 'with' for a bit of protection > ip = line.split(';')[0] > output = os.popen( '/sbin/iptables -A INPUT -s ' + ip + ' -j REJECT' ) > logFile.write(IP+' - Has been blocked\n') > > > -- cut -- > > Since the timestamp doesn't change across a run anyway, there's not > much point printing it out every time, and I'm also putting newlines > in the logfile. Beyond that, it should function the same way as the > original. > > ChrisA > -- My email address on the header is a non-monitored spam catching account. I can be reached via http://www.wvnh.net/contact.htm From rosuav at gmail.com Fri May 24 12:56:47 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 02:56:47 +1000 Subject: Read txt file, add to iptables not working on new host In-Reply-To: References: Message-ID: On Sat, May 25, 2013 at 2:32 AM, JackM wrote: > So Chris, does this version look better? Changed to inFile to with. > Heh, I didn't know you knew about with :) Since you know how to use it, you probably also know why it's useful. Anyway, the main thing is to see the exact command that's being executed, which you then should be able to try at a shell prompt. ChrisA From davea at davea.name Fri May 24 15:29:27 2013 From: davea at davea.name (Dave Angel) Date: Fri, 24 May 2013 15:29:27 -0400 Subject: Read txt file, add to iptables not working on new host In-Reply-To: References: Message-ID: <519FBF97.3050504@davea.name> On 05/24/2013 12:32 PM, JackM wrote: > So Chris, does this version look better? Changed to inFile to with. > > > #!/usr/bin/python > import os > import time > > # Input, Output, and TimeStamp > logFile = open('/var/www/html/statistics/logs/banList.log','w') > stamp = time.asctime(time.localtime()) > > # Daily Flush of blockList rules before re-applying Blocks > os.popen('/sbin/iptables -F INPUT') > logFile.write(stamp+'\nFlushing Rules..\n') > > # Loop to read in file and Apply rules to IPtables > with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile: > for line in inFile: # TODO: Use 'with' for a bit of protection > ip = line.split(';')[0] You want to write the command to the logfile here, BEFORE you try the popen(). That way if there's a problem, you can see what it was about to try before it crashed. > output = os.popen( '/sbin/iptables -A INPUT -s ' + ip + ' -j > REJECT' ) > logFile.write(ip+' - Has been blocked\n') > > > > -- DaveA From lokeshkoppaka at gmail.com Fri May 24 04:04:51 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Fri, 24 May 2013 01:04:51 -0700 (PDT) Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? Message-ID: i need to write a code which can sort the list in order of 'n' without use builtin functions can anyone help me how to do? From rosuav at gmail.com Fri May 24 04:12:45 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 18:12:45 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: Message-ID: On Fri, May 24, 2013 at 6:04 PM, wrote: > i need to write a code which can sort the list in order of 'n' without use builtin functions > can anyone help me how to do? > -- > http://mail.python.org/mailman/listinfo/python-list http://lmgtfy.com/?q=sorting+algorithm http://www.catb.org/esr/faqs/smart-questions.html ChrisA From davea at davea.name Fri May 24 08:33:57 2013 From: davea at davea.name (Dave Angel) Date: Fri, 24 May 2013 08:33:57 -0400 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: Message-ID: <519F5E35.8030107@davea.name> On 05/24/2013 04:04 AM, lokeshkoppaka at gmail.com wrote: > i need to write a code which can sort the list in order of 'n' without use builtin functions > can anyone help me how to do? > You could sort, but you couldn't print out the results, so what's the point? In Python 3.3 at least, print() is a built-in function. Is the homework assignment more clearly worded than your summary? And if so, how far have you gotten on it? -- DaveA From carlosnepomuceno at outlook.com Fri May 24 09:06:59 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 16:06:59 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: Message-ID: lol wtf? If 'n' is the quantity of elements to be sorted there's no way you can write an algorithm with complexity O(n) for the worst case not knowing something special about the data. For example, Quicksort will give you O(n*(log(n)) on average case (O(n^2) in the worst case). You gotta be much more specific than that if you really need an O(n) code. There's probably assumptions you didn't mention about the data if O(n) it's really what you're looking for. PS: Looks like a joke as stated.. ---------------------------------------- > Date: Fri, 24 May 2013 01:04:51 -0700 > Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: lokeshkoppaka at gmail.com > To: python-list at python.org > > i need to write a code which can sort the list in order of 'n' without use builtin functions > can anyone help me how to do? > -- > http://mail.python.org/mailman/listinfo/python-list From lokeshkoppaka at gmail.com Sat May 25 01:15:21 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Fri, 24 May 2013 22:15:21 -0700 (PDT) Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: Message-ID: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh... at gmail.com wrote: > i need to write a code which can sort the list in order of 'n' without use builtin functions > > can anyone help me how to do? Note: the list only contains 0's,1's,2's need to sort them in order of 'n' From rosuav at gmail.com Sat May 25 01:24:01 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 15:24:01 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 3:15 PM, wrote: > On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh... at gmail.com wrote: >> i need to write a code which can sort the list in order of 'n' without use builtin functions >> >> can anyone help me how to do? > > Note: > the list only contains 0's,1's,2's > need to sort them in order of 'n' In that case, you're not really ordering them, you're counting them. Look at the collections module; you can very easily figure out how many of each there are, and then reconstruct the list afterward. ChrisA From lokeshkoppaka at gmail.com Sat May 25 01:39:06 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Fri, 24 May 2013 22:39:06 -0700 (PDT) Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> Message-ID: <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> On Saturday, May 25, 2013 10:54:01 AM UTC+5:30, Chris Angelico wrote: > On Sat, May 25, 2013 at 3:15 PM, wrote: > > > On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh... at gmail.com wrote: > > >> i need to write a code which can sort the list in order of 'n' without use builtin functions > > >> > > >> can anyone help me how to do? > > > > > > Note: > > > the list only contains 0's,1's,2's > > > need to sort them in order of 'n' > > > > In that case, you're not really ordering them, you're counting them. > > Look at the collections module; you can very easily figure out how > > many of each there are, and then reconstruct the list afterward. > > > > ChrisA but i need to do it with out using builtin functions From rosuav at gmail.com Sat May 25 01:43:30 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 15:43:30 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 3:39 PM, wrote: > On Saturday, May 25, 2013 10:54:01 AM UTC+5:30, Chris Angelico wrote: >> On Sat, May 25, 2013 at 3:15 PM, wrote: >> >> > On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh... at gmail.com wrote: >> >> >> i need to write a code which can sort the list in order of 'n' without use builtin functions >> >> >> >> >> >> can anyone help me how to do? >> >> > >> >> > Note: >> >> > the list only contains 0's,1's,2's >> >> > need to sort them in order of 'n' >> >> >> >> In that case, you're not really ordering them, you're counting them. >> >> Look at the collections module; you can very easily figure out how >> >> many of each there are, and then reconstruct the list afterward. >> >> >> >> ChrisA > > but i need to do it with out using builtin functions Depending on your definitions, that's either trivially easy (the 'collections' module is not builtin functions) or completely impossible. Have fun. ChrisA From steve+comp.lang.python at pearwood.info Sat May 25 01:57:38 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 May 2013 05:57:38 GMT Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> Message-ID: <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 24 May 2013 22:39:06 -0700, lokeshkoppaka wrote: > On Saturday, May 25, 2013 10:54:01 AM UTC+5:30, Chris Angelico wrote: >> In that case, you're not really ordering them, you're counting them. >> Look at the collections module; you can very easily figure out how >> many of each there are, and then reconstruct the list afterward. > > but i need to do it with out using builtin functions How would you, an intelligent human being count them? Describe how you would count them in English, or whatever your native language is. "First I would start a tally for the number of zeroes, tally = 0. Then I look at each item in turn, and if it is 0, I add one to the tally. When I get to the end, I the number of zeroes is equal to the tally. Then I do the same for the number of ones, and the number of twos." Now turn that into Python code. tally = 0 for item in list_of_items: if item == 0: tally = tally + 1 print "The number of zeroes equals", tally -- Steven From lokeshkoppaka at gmail.com Sat May 25 02:05:17 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Fri, 24 May 2013 23:05:17 -0700 (PDT) Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> On Saturday, May 25, 2013 11:27:38 AM UTC+5:30, Steven D'Aprano wrote: > On Fri, 24 May 2013 22:39:06 -0700, lokeshkoppaka wrote: > > > > > On Saturday, May 25, 2013 10:54:01 AM UTC+5:30, Chris Angelico wrote: > > > > >> In that case, you're not really ordering them, you're counting them. > > >> Look at the collections module; you can very easily figure out how > > >> many of each there are, and then reconstruct the list afterward. > > > > > > but i need to do it with out using builtin functions > > > > > > How would you, an intelligent human being count them? > > > > Describe how you would count them in English, or whatever your native > > language is. > > > > "First I would start a tally for the number of zeroes, tally = 0. Then I > > look at each item in turn, and if it is 0, I add one to the tally. When I > > get to the end, I the number of zeroes is equal to the tally. > > > > Then I do the same for the number of ones, and the number of twos." > > > > Now turn that into Python code. > > > > tally = 0 > > for item in list_of_items: > > if item == 0: > > tally = tally + 1 > > > > print "The number of zeroes equals", tally > > > > > > > > -- > > Steven ya steven i had done the similar logic but thats not satisfying my professor he had given the following constrains 1. No in-built functions should be used 2. we are expecting a O(n) solution 3. Don't use count method From rosuav at gmail.com Sat May 25 02:12:24 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 16:12:24 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 4:05 PM, wrote: > ya steven i had done the similar logic but thats not satisfying my professor > he had given the following constrains > 1. No in-built functions should be used > 2. we are expecting a O(n) solution > 3. Don't use count method And now you finally admit that it's homework. Solve it yourself, or go to your prof and say that you need help. ChrisA From carlosnepomuceno at outlook.com Sat May 25 03:53:19 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 10:53:19 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: ---------------------------------------- > Date: Fri, 24 May 2013 23:05:17 -0700 > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: lokeshkoppaka at gmail.com > To: python-list at python.org [...] > ya steven i had done the similar logic but thats not satisfying my professor > he had given the following constrains > 1. No in-built functions should be used > 2. we are expecting a O(n) solution > 3. Don't use count method That's trivial! # l is a list of numbers: 0,1,2 def order_n(l): ??? r = [] ??? count = [0]*3 ??? count[2] = len(l) ??? for i in range(len(l)): ??????? count[1] += abs((l[i]>0)-1) ??????? r.insert(count[l[i]], l[i]) ??? return r 'count' is a list I've defined to count the quantity of the elements in the argument, not the method. I don't think it needs any explanations, but if you have any doubts just ask. ;) Good luck! From rosuav at gmail.com Sat May 25 04:28:32 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 18:28:32 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 5:53 PM, Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Fri, 24 May 2013 23:05:17 -0700 >> 1. No in-built functions should be used > count[2] = len(l) Fail! :) ChrisA From carlosnepomuceno at outlook.com Sat May 25 04:43:55 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 11:43:55 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , Message-ID: ---------------------------------------- > Date: Sat, 25 May 2013 18:28:32 +1000 > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: rosuav at gmail.com > To: python-list at python.org > > On Sat, May 25, 2013 at 5:53 PM, Carlos Nepomuceno > wrote: >> ---------------------------------------- >>> Date: Fri, 24 May 2013 23:05:17 -0700 >>> 1. No in-built functions should be used >> count[2] = len(l) > > Fail! :) > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list lol I forgot to include this monkey patch! ;) def length(l): ??? x=0 ??? y=l[:] ??? while y: ??????? x+=1 ??????? y.pop() ??? return x From rosuav at gmail.com Sat May 25 04:47:24 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 18:47:24 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 6:43 PM, Carlos Nepomuceno wrote: > ---------------------------------------- > lol I forgot to include this monkey patch! ;) > > def length(l): > x=0 > y=l[:] > while y: > x+=1 > y.pop() > return x Nice. Now eliminate abs (easy) and range. :) ChrisA From carlosnepomuceno at outlook.com Sat May 25 04:54:53 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 11:54:53 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , , , Message-ID: lol def absolute(x): ??? return x if x>0 else -x def reach(x): ??? y=[] ??? z=0 ??? while z Date: Sat, 25 May 2013 18:47:24 +1000 > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: rosuav at gmail.com > To: python-list at python.org > > On Sat, May 25, 2013 at 6:43 PM, Carlos Nepomuceno > wrote: >> ---------------------------------------- >> lol I forgot to include this monkey patch! ;) >> >> def length(l): >> x=0 >> y=l[:] >> while y: >> x+=1 >> y.pop() >> return x > > Nice. Now eliminate abs (easy) and range. :) > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Sat May 25 05:01:09 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 19:01:09 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 6:54 PM, Carlos Nepomuceno wrote: > lol > > def absolute(x): > return x if x>0 else -x > > def reach(x): > y=[] > z=0 > while z y.append(z) > z+=1 > return y Very good. You are now in a position to get past the limitations of a restricted-environment eval/exec. Avoiding builtins is actually a fun skill to hone. ChrisA From carlosnepomuceno at outlook.com Sat May 25 05:10:06 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 12:10:06 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , , , , , Message-ID: ---------------------------------------- > Date: Sat, 25 May 2013 19:01:09 +1000 > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: rosuav at gmail.com > To: python-list at python.org [...] > Very good. You are now in a position to get past the limitations of a > restricted-environment eval/exec. Avoiding builtins is actually a fun > skill to hone. > > ChrisA I'm glad he didn't ask for a Pseudo-RNG without built-ins! ;) From rosuav at gmail.com Sat May 25 05:14:57 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 19:14:57 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 7:10 PM, Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Sat, 25 May 2013 19:01:09 +1000 >> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? >> From: rosuav at gmail.com >> To: python-list at python.org > [...] >> Very good. You are now in a position to get past the limitations of a >> restricted-environment eval/exec. Avoiding builtins is actually a fun >> skill to hone. >> >> ChrisA > > > I'm glad he didn't ask for a Pseudo-RNG without built-ins! ;) def random_number(): return 7 ChrisA From carlosnepomuceno at outlook.com Sat May 25 05:30:28 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 12:30:28 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , , , , , , , Message-ID: lol http://search.dilbert.com/comic/Random%20Nine ---------------------------------------- > Date: Sat, 25 May 2013 19:14:57 +1000 > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: rosuav at gmail.com > To: python-list at python.org > > On Sat, May 25, 2013 at 7:10 PM, Carlos Nepomuceno > wrote: >> ---------------------------------------- >>> Date: Sat, 25 May 2013 19:01:09 +1000 >>> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? >>> From: rosuav at gmail.com >>> To: python-list at python.org >> [...] >>> Very good. You are now in a position to get past the limitations of a >>> restricted-environment eval/exec. Avoiding builtins is actually a fun >>> skill to hone. >>> >>> ChrisA >> >> >> I'm glad he didn't ask for a Pseudo-RNG without built-ins! ;) > > def random_number(): > return 7 > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Sat May 25 10:28:33 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 May 2013 14:28:33 GMT Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote: > def random_number(): > return 7 I call shenanigans! That value isn't generated randomly, you just made it up! I rolled a die *hundreds* of times and not once did it come up seven! -- Steven From fabiosantosart at gmail.com Sat May 25 10:46:46 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sat, 25 May 2013 15:46:46 +0100 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 25 May 2013 15:35, "Steven D'Aprano" < steve+comp.lang.python at pearwood.info> wrote: > > On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote: > > > def random_number(): > > return 7 > > I call shenanigans! That value isn't generated randomly, you just made it > up! I rolled a die *hundreds* of times and not once did it come up seven! > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list Try flipping a coin. I flipped one a couple of times and got the seventh face once. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sat May 25 11:03:44 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 25 May 2013 16:03:44 +0100 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 25/05/2013 15:28, Steven D'Aprano wrote: > On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote: > >> def random_number(): >> return 7 > > I call shenanigans! That value isn't generated randomly, you just made it > up! I rolled a die *hundreds* of times and not once did it come up seven! > > > Lies, damn lies and statistics? :) -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From rosuav at gmail.com Sat May 25 11:41:58 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 01:41:58 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, May 26, 2013 at 12:28 AM, Steven D'Aprano wrote: > On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote: > >> def random_number(): >> return 7 > > I call shenanigans! That value isn't generated randomly, you just made it > up! I rolled a die *hundreds* of times and not once did it come up seven! You've obviously never used a REAL set of dice. Now, I have here with me a set used for maths drill (to be entirely accurate, what I have here is the company's stock of them, so there are multiples of each of these - anyone need to buy dice?) with everything except the classic 1 through 6 that everyone knows: * Six sides, faces marked 7 through 12 * Six sides, faces marked "+x-\xf7+" and a "wild" marker (yes, two of +) * Ten sides, numbered 0 through 9 * Eight sides, numbered 1 through 8 * Twelve sides, as above * Twenty sides, as above Now, tabletop roleplayers will recognize the latter four as the ones notated as d10, d8, d12, and d20, but these are NOT for gameplay, they are for serious educational purposes! Honest! Anyway, all of those can roll a 7... well, one of them has to roll a \xf7, but close enough right? Plus, if you roll 2d6 (that is, two regular six-sided dice and add them up), 7 is statistically the most likely number to come up with. Therefore it IS random. ChrisA From carlosnepomuceno at outlook.com Sat May 25 13:17:51 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 20:17:51 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , , , , , , , , <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com>, Message-ID: ---------------------------------------- > Date: Sun, 26 May 2013 01:41:58 +1000 > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: rosuav at gmail.com > To: python-list at python.org > > On Sun, May 26, 2013 at 12:28 AM, Steven D'Aprano > wrote: >> On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote: >> >>> def random_number(): >>> return 7 >> >> I call shenanigans! That value isn't generated randomly, you just made it >> up! I rolled a die *hundreds* of times and not once did it come up seven! > > You've obviously never used a REAL set of dice. Now, I have here with > me a set used for maths drill (to be entirely accurate, what I have > here is the company's stock of them, so there are multiples of each of > these - anyone need to buy dice?) with everything except the classic 1 > through 6 that everyone knows: > > * Six sides, faces marked 7 through 12 > * Six sides, faces marked "+x-\xf7+" and a "wild" marker (yes, two of +) > * Ten sides, numbered 0 through 9 > * Eight sides, numbered 1 through 8 > * Twelve sides, as above > * Twenty sides, as above > > Now, tabletop roleplayers will recognize the latter four as the ones > notated as d10, d8, d12, and d20, but these are NOT for gameplay, they > are for serious educational purposes! Honest! > > Anyway, all of those can roll a 7... well, one of them has to roll a > \xf7, but close enough right? Plus, if you roll 2d6 (that is, two > regular six-sided dice and add them up), 7 is statistically the most > likely number to come up with. Therefore it IS random. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list def f(x): ??? return x+1 or you can just go: f(roll_d6()) ;) From rosuav at gmail.com Sat May 25 13:23:44 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 03:23:44 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, May 26, 2013 at 3:17 AM, Carlos Nepomuceno wrote: > def f(x): > return x+1 > > or you can just go: > > f(roll_d6()) Hmm. Interesting. So now we have a question: Does adding 1 to a random number make it less random? It adds determinism to the number; can a number be more deterministic while still no less random? Ah! I know. The answer comes from common sense: a = random() # a is definitely a random number a -= random() # a is no longer random, we subtracted all the randomness from it Of course, since number-number => number, a is still a number. And so we can conclude that adding 1 to a random dice roll does indeed leave all the randomness still in it. But wait! That means we can do better!! a = random() # a is a random number a *= 5 # a is clearly five times as random now! ChrisA From carlosnepomuceno at outlook.com Sat May 25 13:34:02 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 20:34:02 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , , , , , , , , <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com>, , , Message-ID: ---------------------------------------- > Date: Sun, 26 May 2013 03:23:44 +1000 > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: rosuav at gmail.com > To: python-list at python.org > > On Sun, May 26, 2013 at 3:17 AM, Carlos Nepomuceno > wrote: >> def f(x): >> return x+1 >> >> or you can just go: >> >> f(roll_d6()) > > Hmm. Interesting. So now we have a question: Does adding 1 to a random > number make it less random? It adds determinism to the number; can a > number be more deterministic while still no less random? > > Ah! I know. The answer comes from common sense: > > a = random() # a is definitely a random number > a -= random() # a is no longer random, we subtracted all the randomness from it > > Of course, since number-number => number, a is still a number. And so > we can conclude that adding 1 to a random dice roll does indeed leave > all the randomness still in it. But wait! That means we can do > better!! > > a = random() # a is a random number > a *= 5 # a is clearly five times as random now! > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list It depends if the result (any operation) is in the required range. For example: "int(random.random()+1)" it's not random at all! But "int(random.random()*1000)" my look random if it fits your needs. ;) From steve+comp.lang.python at pearwood.info Sat May 25 23:38:12 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 May 2013 03:38:12 GMT Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51a183a4$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sun, 26 May 2013 03:23:44 +1000, Chris Angelico wrote: > Does adding 1 to a random > number make it less random? It adds determinism to the number; can a > number be more deterministic while still no less random? > > Ah! I know. The answer comes from common sense: [snip spurious answer] I know you're being funny, but in fact adding a constant to a random variable still leaves it equally random. Adding, multiplying, dividing or subtracting a constant from a random variable X just shifts the possible values X can take, it doesn't change the shape of the distribution. However, adding two random variables X and Y does change the distribution. In fact, a very cheap way of simulating an almost normally distributed random variable is to add up a whole lot of uniformly distributed random variables. Adding up 12 calls to random.random(), and subtracting 6, gives you a close approximation to a Gaussian random variable with mean 0 and standard deviation 1. -- Steven From dan at tombstonezero.net Sun May 26 00:06:34 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Sun, 26 May 2013 04:06:34 GMT Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> <51a183a4$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, 26 May 2013 03:38:12 +0000, Steven D'Aprano wrote: > ... adding a constant to a random variable still leaves it equally > random. Adding, multiplying, dividing or subtracting a constant from a > random variable X just shifts the possible values X can take ... That's mathematically true, but this is the Python mailing list. Adding, subtracting, or dividing by a sufficiently large constant loses all the randomness. Python 3.2.4 (default, May 8 2013, 20:55:18) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> random.random() + 1e200 1e+200 >>> random.random() - 1e200 -1e+200 >>> random.random() / 1e309 0.0 But you knew that. ;-) Dan From rosuav at gmail.com Sun May 26 00:28:37 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 14:28:37 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <51a183a4$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> <51a183a4$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, May 26, 2013 at 1:38 PM, Steven D'Aprano wrote: > On Sun, 26 May 2013 03:23:44 +1000, Chris Angelico wrote: > >> Does adding 1 to a random >> number make it less random? It adds determinism to the number; can a >> number be more deterministic while still no less random? >> >> Ah! I know. The answer comes from common sense: > [snip spurious answer] > > I know you're being funny, but in fact adding a constant to a random > variable still leaves it equally random. Adding, multiplying, dividing or > subtracting a constant from a random variable X just shifts the possible > values X can take, it doesn't change the shape of the distribution. In real numbers, that's correct. However, computers don't work with real numbers, so there's the very, uhh, REAL possibility that some of the entropy will be lost. For instance, multiplying and dividing when working with integers results in truncation, and adding huge numbers to small floats results in precision loss. I was deliberately playing around, but unfortunately there have been many people who've genuinely thought things similar to what I was saying - and then implemented into code. > However, adding two random variables X and Y does change the > distribution. In fact, a very cheap way of simulating an almost normally > distributed random variable is to add up a whole lot of uniformly > distributed random variables. Adding up 12 calls to random.random(), and > subtracting 6, gives you a close approximation to a Gaussian random > variable with mean 0 and standard deviation 1. Yep. The more dice you roll, the more normal the distribution. Which means that d100 is extremely swingy, but 11d10-10 is much less so, and 99d2-98 quite stable. The more randomness you add, the more predictable the result. Does that seem right to you? ChrisA From carlosnepomuceno at outlook.com Sat May 25 13:12:44 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 20:12:44 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , , , , , , , , <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > Date: Sat, 25 May 2013 14:28:33 +0000 > To: python-list at python.org > > On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote: > >> def random_number(): >> return 7 > > I call shenanigans! That value isn't generated randomly, you just made it > up! I rolled a die *hundreds* of times and not once did it come up seven! lol It worked fine on my d8! lol From steve+comp.lang.python at pearwood.info Sat May 25 23:09:51 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 May 2013 03:09:51 GMT Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51a17cff$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sun, 26 May 2013 01:41:58 +1000, Chris Angelico wrote: > On Sun, May 26, 2013 at 12:28 AM, Steven D'Aprano > wrote: >> On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote: >> >>> def random_number(): >>> return 7 >> >> I call shenanigans! That value isn't generated randomly, you just made >> it up! I rolled a die *hundreds* of times and not once did it come up >> seven! > > You've obviously never used a REAL set of dice. You're right, all my dice are eight-sided and complex: 1+0i 1+1i 1-1i -1+0i -1+1i -1-1i :-) But seriously, I have various D&D style gaming dice, d4, d6, d8, d12, d20 and d30. But I thought the opportunity for a joke was more important than pedantic correctness :-) > Now, I have here with me > a set used for maths drill (to be entirely accurate, what I have here is > the company's stock of them, so there are multiples of each of these - > anyone need to buy dice?) Are you serious? What's the cost, posted to Melbourne? > with everything except the classic 1 through 6 that everyone knows: > > * Six sides, faces marked 7 through 12 > * Six sides, faces marked "+x-\xf7+" and a "wild" marker > (yes, two of +) Oh, you mean ? (division sign)! Why didn't you say so? :-P And another thing, shame on you, you mean ? not x. It's easy to find too: py> from unicodedata import lookup py> print(lookup("MULTIPLICATION SIGN")) ? > * Ten sides, numbered 0 through 9 > * Eight sides, numbered 1 through 8 > * Twelve sides, as above > * Twenty sides, as above > > Now, tabletop roleplayers will recognize the latter four as the ones > notated as d10, d8, d12, and d20, but these are NOT for gameplay, they > are for serious educational purposes! Honest! > > Anyway, all of those can roll a 7... well, one of them has to roll a > \xf7, but close enough right? I don't think so... > Plus, if you roll 2d6 (that is, two > regular six-sided dice and add them up), 7 is statistically the most > likely number to come up with. Therefore it IS random. Yes, but if you subtract them the most common is 0, if you multiply the most common are 6 or 12, and if you divide the most common is 1. If you decide on the operation randomly, using the +-??+ die above (ignoring wildcards), the most common result is 6. The probability of getting a 7 is just 1/15. from collections import Counter from operator import add, sub, mul, truediv as div ops = (add, sub, mul, div, add) Counter(op(i, j) for op in ops for i in range(1, 7) for j in range(1, 7)) -- Steven From rosuav at gmail.com Sun May 26 00:02:29 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 14:02:29 +1000 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <51a17cff$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> <51a0ca91$0$30002$c3e8da3$5496439d@news.astraweb.com> <51a17cff$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, May 26, 2013 at 1:09 PM, Steven D'Aprano wrote: > You're right, all my dice are eight-sided and complex: > > 1+0i > 1+1i > 1-1i > -1+0i > -1+1i > -1-1i > > > :-) Now THAT is a dice of win! >> Now, I have here with me >> a set used for maths drill (to be entirely accurate, what I have here is >> the company's stock of them, so there are multiples of each of these - >> anyone need to buy dice?) > > Are you serious? What's the cost, posted to Melbourne? $1 each, postage probably $5 for any number. Or there may even be option to pick up / hand deliver, depending on where in Melb you are. http://www.kepl.com.au/ - company's winding down, but we still have stock. > Oh, you mean ? (division sign)! Why didn't you say so? :-P I tend to stick to ASCII in these posts. :) > And another thing, shame on you, you mean ? not x. It's easy to find too: > > py> from unicodedata import lookup > py> print(lookup("MULTIPLICATION SIGN")) > ? I'm aware of that, but see above, I stick to ASCII where possible. The faces would be better represented with some of the other digits (the bolded ones, perhaps), but I used the ASCII digits. :) >> Plus, if you roll 2d6 (that is, two >> regular six-sided dice and add them up), 7 is statistically the most >> likely number to come up with. Therefore it IS random. > > Yes, but if you subtract them the most common is 0, if you multiply the > most common are 6 or 12, and if you divide the most common is 1. If you > decide on the operation randomly, using the +-??+ die above (ignoring > wildcards), the most common result is 6. The probability of getting a 7 > is just 1/15. > > from collections import Counter > from operator import add, sub, mul, truediv as div > ops = (add, sub, mul, div, add) > Counter(op(i, j) for op in ops for i in range(1, 7) for j in range(1, 7)) LOL! I never thought to go THAT far into the analysis..... Nice one! ChrisA From breamoreboy at yahoo.co.uk Sat May 25 08:01:06 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 25 May 2013 13:01:06 +0100 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , , , Message-ID: On 25/05/2013 09:54, Carlos Nepomuceno wrote: > lol > > def absolute(x): > return x if x>0 else -x > > def reach(x): > y=[] > z=0 > while z y.append(z) > z+=1 > return y > In my book this is another fail as lists are inbuilt (yuck!) and so is the add function that'll be called for z+=1. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From carlosnepomuceno at outlook.com Sat May 25 13:07:47 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 20:07:47 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: , , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , , , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, , <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, , <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com>, , , , , , , , , , Message-ID: ---------------------------------------- > To: python-list at python.org > From: breamoreboy at yahoo.co.uk > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > Date: Sat, 25 May 2013 13:01:06 +0100 [...] > In my book this is another fail as lists are inbuilt (yuck!) and so is > the add function that'll be called for z+=1. Indeed! It's a joke Mark! lol ;) From roy at panix.com Sat May 25 10:03:24 2013 From: roy at panix.com (Roy Smith) Date: Sat, 25 May 2013 10:03:24 -0400 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: In article <74e33270-a79a-4878-a400-8a6cda6637b2 at googlegroups.com>, lokeshkoppaka at gmail.com wrote: > ya steven i had done the similar logic but thats not satisfying my professor > he had given the following constrains > 1. No in-built functions should be used > 2. we are expecting a O(n) solution > 3. Don't use count method A couple of points here: 1) In general, people on mailing lists are not into doing homework problems for other people. 2) If you're going to bring us a homework problem, at least describe the whole problem up front. It really doesn't help to dribble out new requirements one at a time. 3) rustompmody at gmail.com already posted a pointer to the wikipedia article describing the required algorithm in detail. 4) I don't know what "no built-in functions should be used" means. I assume it means, "don't call sort()"? If you can't even call int.__lt__(), it's going to be really hard to do this. From davea at davea.name Sat May 25 10:27:02 2013 From: davea at davea.name (Dave Angel) Date: Sat, 25 May 2013 10:27:02 -0400 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: <51A0CA36.4050404@davea.name> On 05/25/2013 10:03 AM, Roy Smith wrote: > In article <74e33270-a79a-4878-a400-8a6cda6637b2 at googlegroups.com>, > lokeshkoppaka at gmail.com wrote: > >> ya steven i had done the similar logic but thats not satisfying my professor >> he had given the following constrains >> 1. No in-built functions should be used >> 2. we are expecting a O(n) solution >> 3. Don't use count method > > A couple of points here: > > 1) In general, people on mailing lists are not into doing homework > problems for other people. > > 2) If you're going to bring us a homework problem, at least describe the > whole problem up front. It really doesn't help to dribble out new > requirements one at a time. > > 3) rustompmody at gmail.com already posted a pointer to the wikipedia > article describing the required algorithm in detail. > > 4) I don't know what "no built-in functions should be used" means. I > assume it means, "don't call sort()"? If you can't even call > int.__lt__(), it's going to be really hard to do this. > The OP has already admitted that he didn't want a sort at all. He wants to COUNT the items, not sort them. So nothing in his orginal post relates to the real homework assignment. -- DaveA From steve+comp.lang.python at pearwood.info Sat May 25 10:30:22 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 May 2013 14:30:22 GMT Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com> <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com> <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: <51a0cafd$0$30002$c3e8da3$5496439d@news.astraweb.com> On Fri, 24 May 2013 23:05:17 -0700, lokeshkoppaka wrote: > On Saturday, May 25, 2013 11:27:38 AM UTC+5:30, Steven D'Aprano wrote: >> tally = 0 >> for item in list_of_items: >> if item == 0: >> tally = tally + 1 >> >> print "The number of zeroes equals", tally > > > ya steven i had done the similar logic but thats not satisfying my > professor he had given the following constrains > 1. No in-built functions should be used The above does not use any built-in functions. > 2. we are expecting a O(n) solution The above is O(n). > 3. Don't use count method The above does not use the count method. -- Steven From carlosnepomuceno at outlook.com Sat May 25 13:45:40 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 20:45:40 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? In-Reply-To: <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> References: , <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com>, , <39ac7437-857e-483f-998c-8162c1039933@googlegroups.com>, <51a052d1$0$6599$c3e8da3$5496439d@news.astraweb.com>, <74e33270-a79a-4878-a400-8a6cda6637b2@googlegroups.com> Message-ID: ---------------------------------------- > Date: Fri, 24 May 2013 23:05:17 -0700 > Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions?? > From: lokeshkoppaka at gmail.com [...] > ya steven i had done the similar logic but thats not satisfying my professor > he had given the following constrains > 1. No in-built functions should be used > 2. we are expecting a O(n) solution > 3. Don't use count method PS: If you find something faster please let me know! From rustompmody at gmail.com Sat May 25 01:43:18 2013 From: rustompmody at gmail.com (rusi) Date: Fri, 24 May 2013 22:43:18 -0700 (PDT) Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> Message-ID: On May 25, 10:15?am, lokeshkopp... at gmail.com wrote: > On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh... at gmail.com wrote: > > i need to write a code which can sort the list in order of 'n' without use builtin functions > > > can anyone help me how to do? > > ?Note: > the list only contains 0's,1's,2's > need to sort them in order of 'n' Its a classic problem http://en.wikipedia.org/wiki/Dutch_national_flag_problem From roy at panix.com Sat May 25 09:29:57 2013 From: roy at panix.com (Roy Smith) Date: Sat, 25 May 2013 09:29:57 -0400 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> Message-ID: In article <78192328-b31b-49d9-9cd6-ec742c092a29 at googlegroups.com>, lokeshkoppaka at gmail.com wrote: > On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh... at gmail.com wrote: > > i need to write a code which can sort the list in order of 'n' without use > > builtin functions > > > > can anyone help me how to do? > > Note: > the list only contains 0's,1's,2's > need to sort them in order of 'n' What do you mean by "need to sort them in order of 'n'". Are you saying that you need to sort them into numerical order? Or that you need to running time of the algorithm to be O(n)? I'm assuming the later, in which case this is starting to sound like a classic interview question. Assuming you can accept an unstable sort, there's an easy solution. I'm not aware of any solution if you require a stable sort. Perhaps that's enough of a hint? From jpiitula at ling.helsinki.fi Sat May 25 10:59:30 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 25 May 2013 17:59:30 +0300 Subject: help how to sort a list in order of 'n' in python without using inbuilt functions?? References: <78192328-b31b-49d9-9cd6-ec742c092a29@googlegroups.com> Message-ID: Roy Smith writes: > In article <78192328-b31b-49d9-9cd6-ec742c092a29 at googlegroups.com>, > lokeshkoppaka at gmail.com wrote: > > > On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh... at gmail.com wrote: > > > i need to write a code which can sort the list in order of 'n' > > > without use builtin functions > > > > > > can anyone help me how to do? > > > > Note: > > the list only contains 0's,1's,2's > > need to sort them in order of 'n' > > What do you mean by "need to sort them in order of 'n'". Are you > saying that you need to sort them into numerical order? Or that you > need to running time of the algorithm to be O(n)? > > I'm assuming the later, in which case this is starting to sound like > a classic interview question. > > Assuming you can accept an unstable sort, there's an easy solution. > I'm not aware of any solution if you require a stable sort. Perhaps > that's enough of a hint? Surely it's assumed that each element of the input list can be classified as 0, 1, or 2 in O(1) time. If O(n) auxiliary space can be allocated in O(n) time, just put the 0's in their own list in the order they are encountered, 1's in their own list in the order they are encountered, 2's in their own list in the order they are encountered, then put the 0's back into the input list in the order they are encountered in their auxiliary list, followed by the 1's, followed by the 2's. Stable and O(n), no? Even ( [ x for x in input if x.key == 0 ] + [ x for x in input if x.key == 1 ] + [ x for x in input if x.key == 2 ] ). I suppose a list comprehension is not technically a function any more than a loop is. (Neither stability nor O(1) space has been required yet, I think.) From peter.h.m.brooks at gmail.com Fri May 24 04:14:45 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Fri, 24 May 2013 01:14:45 -0700 (PDT) Subject: Simple algorithm question - how to reorder a sequence economically Message-ID: What is the easiest way to reorder a sequence pseudo-randomly? That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg 2,1,4,3) that is different each time. I'm writing a simulation and would like to visit all the nodes in a different order at each iteration of the simulation to remove the risk of a fixed order introducing spurious evidence of correlation. From rosuav at gmail.com Fri May 24 04:37:49 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 18:37:49 +1000 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: References: Message-ID: On Fri, May 24, 2013 at 6:14 PM, Peter Brooks wrote: > What is the easiest way to reorder a sequence pseudo-randomly? > > That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg > 2,1,4,3) that is different each time. > > I'm writing a simulation and would like to visit all the nodes in a > different order at each iteration of the simulation to remove the risk > of a fixed order introducing spurious evidence of correlation. Permuting a sequence iteratively to cover every possibility? Good fun. Here's one way of looking at it. Imagine the indices of all elements are in some special "base" like so: [a, b, c, d] --> a*4+b*3+c*2+d*1 Then iterate up to the highest possible value (ie 4*3*2*1), picking indices for each accordingly. I don't know how efficient this will be, but here's a simple piece of code to do it: >>> def permute(lst,pos): lst=lst[:] # Take a copy ret=[None]*len(lst) for i in range(len(lst)): pos,idx=divmod(pos,len(lst)) ret[i]=lst[idx] del lst[idx] return ret >>> for i in range(4*3*2*1): permute([10,20,30,40],i) [10, 20, 30, 40] [20, 10, 30, 40] [30, 10, 20, 40] [40, 10, 20, 30] [10, 30, 20, 40] [20, 30, 10, 40] [30, 20, 10, 40] [40, 20, 10, 30] [10, 40, 20, 30] [20, 40, 10, 30] [30, 40, 10, 20] [40, 30, 10, 20] [10, 20, 40, 30] [20, 10, 40, 30] [30, 10, 40, 20] [40, 10, 30, 20] [10, 30, 40, 20] [20, 30, 40, 10] [30, 20, 40, 10] [40, 20, 30, 10] [10, 40, 30, 20] [20, 40, 30, 10] [30, 40, 20, 10] [40, 30, 20, 10] It works, it produces a unique list for any given index provided, but it's not the cleanest or most efficient. But I know someone'll improve on it... or tell me I'm an idiot for not taking a more obvious approach :) ChrisA From fabiosantosart at gmail.com Fri May 24 04:47:07 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Fri, 24 May 2013 09:47:07 +0100 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: References: Message-ID: On 24 May 2013 09:41, "Chris Angelico" wrote: > > On Fri, May 24, 2013 at 6:14 PM, Peter Brooks > wrote: > > What is the easiest way to reorder a sequence pseudo-randomly? > > > > That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg > > 2,1,4,3) that is different each time. > > ... > It works, it produces a unique list for any given index provided, but > it's not the cleanest or most efficient. But I know someone'll improve > on it... or tell me I'm an idiot for not taking a more obvious > approach :) > > ChrisA I think that is pretty much itertools.permutations from the standard library. The OP should check it out. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri May 24 05:11:11 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 19:11:11 +1000 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: References: Message-ID: On Fri, May 24, 2013 at 6:47 PM, F?bio Santos wrote: > > On 24 May 2013 09:41, "Chris Angelico" wrote: >> >> On Fri, May 24, 2013 at 6:14 PM, Peter Brooks >> wrote: >> > What is the easiest way to reorder a sequence pseudo-randomly? >> > >> > That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg >> > 2,1,4,3) that is different each time. >> > > ... > >> It works, it produces a unique list for any given index provided, but >> it's not the cleanest or most efficient. But I know someone'll improve >> on it... or tell me I'm an idiot for not taking a more obvious >> approach :) >> >> ChrisA > > I think that is pretty much itertools.permutations from the standard > library. The OP should check it out. That works if all the permutations are wanted at once. Is there a way, short of iterating over it N times, to request permutation #N? Or maybe I'm misreading the OP and that's not a requirement. ChrisA From buzzard at invalid.invalid Fri May 24 10:33:26 2013 From: buzzard at invalid.invalid (duncan smith) Date: Fri, 24 May 2013 15:33:26 +0100 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: References: Message-ID: <519f7a3a$0$32543$862e30e2@ngroups.net> On 24/05/13 10:11, Chris Angelico wrote: > On Fri, May 24, 2013 at 6:47 PM, F?bio Santos wrote: >> >> On 24 May 2013 09:41, "Chris Angelico" wrote: >>> >>> On Fri, May 24, 2013 at 6:14 PM, Peter Brooks >>> wrote: >>>> What is the easiest way to reorder a sequence pseudo-randomly? >>>> >>>> That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg >>>> 2,1,4,3) that is different each time. >>>> >> ... >> >>> It works, it produces a unique list for any given index provided, but >>> it's not the cleanest or most efficient. But I know someone'll improve >>> on it... or tell me I'm an idiot for not taking a more obvious >>> approach :) >>> >>> ChrisA >> >> I think that is pretty much itertools.permutations from the standard >> library. The OP should check it out. > > That works if all the permutations are wanted at once. Is there a way, > short of iterating over it N times, to request permutation #N? Or > maybe I'm misreading the OP and that's not a requirement. > > ChrisA > A long time ago I wrote some code to do that. import gmpy def LexPermFromIndex(items, index): n = len(items) inds = range(n) perm = [] for i in range(1, n+1): r, index = divmod(index, gmpy.fac(n-i)) r = int(r) perm.append(inds[r]) inds = inds[:r] + inds[r+1:] return [items[i] for i in perm] >>> LexPermFromIndex([1,2,3,4], 0) [1, 2, 3, 4] >>> LexPermFromIndex([1,2,3,4], 1) [1, 2, 4, 3] >>> LexPermFromIndex([1,2,3,4], 10) [2, 4, 1, 3] >>> I can't remember exactly why I wrote it. But I also have something for generating a permutation's index and similar functions for combinations. Duncan From tjreedy at udel.edu Fri May 24 06:10:26 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Fri, 24 May 2013 06:10:26 -0400 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: References: Message-ID: On 5/24/2013 4:14 AM, Peter Brooks wrote: > What is the easiest way to reorder a sequence pseudo-randomly? > > That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg > 2,1,4,3) that is different each time. > > I'm writing a simulation and would like to visit all the nodes in a > different order at each iteration of the simulation to remove the risk > of a fixed order introducing spurious evidence of correlation. random module has a shuffle function I believe From steve+comp.lang.python at pearwood.info Fri May 24 06:52:18 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 May 2013 10:52:18 GMT Subject: Simple algorithm question - how to reorder a sequence economically References: Message-ID: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 24 May 2013 01:14:45 -0700, Peter Brooks wrote: > What is the easiest way to reorder a sequence pseudo-randomly? import random random.shuffle(sequence) The sequence is modified in place, so it must be mutable. Lists are okay, tuples are not. > That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg > 2,1,4,3) that is different each time. You can't *guarantee* that it will be different each time. With a four- item list, there are only 4! = 24 combinations, so on average you'll get the original order one time in 24. For a ten-item list, that is once every 3628800 times, and for a twenty-item list, once in 2432902008176640000 times. But of course these are *random*, and there's always a chance of this: http://dilbert.com/strips/comic/2001-10-25 -- Steven From ned at nedbatchelder.com Fri May 24 07:26:00 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Fri, 24 May 2013 07:26:00 -0400 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <519F4E48.7090307@nedbatchelder.com> On 5/24/2013 6:52 AM, Steven D'Aprano wrote: > On Fri, 24 May 2013 01:14:45 -0700, Peter Brooks wrote: > >> What is the easiest way to reorder a sequence pseudo-randomly? > import random > random.shuffle(sequence) > > > The sequence is modified in place, so it must be mutable. Lists are okay, > tuples are not. > > >> That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg >> 2,1,4,3) that is different each time. > You can't *guarantee* that it will be different each time. With a four- > item list, there are only 4! = 24 combinations, so on average you'll get > the original order one time in 24. For a ten-item list, that is once > every 3628800 times, and for a twenty-item list, once in > 2432902008176640000 times. But of course these are *random*, and there's > always a chance of this: > > http://dilbert.com/strips/comic/2001-10-25 > > Also, heed the note in the docs: "Note that for even rather small len(x), the total number of permutations of /x/ is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated." The default random number generator has a period of 2**19937-1, which means that lists longer than 2080 have more permutations than the period of the generator (because factorial(2081) > 2**19937). Most shuffles involve lists far shorter than 2080, but it's still good to keep it in mind. --Ned. -------------- next part -------------- An HTML attachment was scrubbed... URL: From peter.h.m.brooks at gmail.com Fri May 24 09:23:14 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Fri, 24 May 2013 06:23:14 -0700 (PDT) Subject: Simple algorithm question - how to reorder a sequence economically References: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <0065152b-16ac-4ce9-8e99-3d0b8e88a31f@fv8g2000vbb.googlegroups.com> Thank you all for those most helpful suggestions! random.shuffle does precisely the job that I need quickly. Thank you for introducing me to itertools, though, I should have remembered APL did this in a symbol or two and I'm sure that itertools will come in handy in future. Thanks for the warnings about random numbers too - I hope my lists will be short enough for the permutations of the function to be irrelevant. I don't need every single sequence to be unique, only that the same sequence only occurs occasionally. My lists might get up to the ~2k length one day, and I'll keep in mind the need, at that stage, to use a different pseudo-random generator. Actually, thinking about it, there is probably a source of non-algorithmically-derived 'random' numbers somewhere on the net that would do the job nicely. From rosuav at gmail.com Fri May 24 09:43:16 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 24 May 2013 13:43:16 -0000 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: <0065152b-16ac-4ce9-8e99-3d0b8e88a31f@fv8g2000vbb.googlegroups.com> References: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> <0065152b-16ac-4ce9-8e99-3d0b8e88a31f@fv8g2000vbb.googlegroups.com> Message-ID: On Fri, May 24, 2013 at 11:23 PM, Peter Brooks wrote: > Actually, thinking about > it, there is probably a source of non-algorithmically-derived 'random' > numbers somewhere on the net that would do the job nicely. True entropy is usually provided by a source such as /dev/random (on Unix systems). It's sometimes referred to as "cryptographic" randomness, due to its necessity in secure encryption work. There are various ways to get this in a cross-platform way. ChrisA From steve+comp.lang.python at pearwood.info Fri May 24 09:57:21 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 May 2013 13:57:21 GMT Subject: Simple algorithm question - how to reorder a sequence economically References: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> <0065152b-16ac-4ce9-8e99-3d0b8e88a31f@fv8g2000vbb.googlegroups.com> Message-ID: <519f71c0$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 24 May 2013 06:23:14 -0700, Peter Brooks wrote: > Thanks for the warnings about random numbers too - I hope my lists will > be short enough for the permutations of the function to be irrelevant. I > don't need every single sequence to be unique, only that the same > sequence only occurs occasionally. My lists might get up to the ~2k > length one day, and I'll keep in mind the need, at that stage, to use a > different pseudo-random generator. Actually, thinking about it, there is > probably a source of non-algorithmically-derived 'random' numbers > somewhere on the net that would do the job nicely. That's massive overkill. Take a closer look at what Ned wrote: "The default random number generator has a period of 2**19937-1" and consider the numbers. For a list with 3000 items, there are 3000! possible permutations. That is approximately 10**21024. That is, a number with 21024 digits, or somewhere around a trillion trillion trillion ... trillion trillion, where there are 1752 "trillions". If you could generate a million permutations a second, it would take you on average 10**210988 centuries before getting the original permutation again. That's the expected result you would get with a source of true randomness. Instead, with Python's default pseudo-random number generator, you cannot get the full 3000! permutations, but only 2**19937-1 of them. Using the same calculation as above, that means that you will have to generate a million permutations per second for "only" 10**13783 centuries before getting the original permutation again. There are uses where being able to generate any possible permutation is important, and the default PRNG is not sufficient. But merely shuffling your data around to avoid spurious correlations is not one of them. Save yourself a lot of development effort, and debugging, and just use random.shuffle. -- Steven From john_ladasky at sbcglobal.net Fri May 24 13:33:47 2013 From: john_ladasky at sbcglobal.net (John Ladasky) Date: Fri, 24 May 2013 10:33:47 -0700 (PDT) Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Friday, May 24, 2013 3:52:18 AM UTC-7, Steven D'Aprano wrote: > On Fri, 24 May 2013 01:14:45 -0700, Peter Brooks wrote: > > > That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg > > 2,1,4,3) that is different each time. > > You can't *guarantee* that it will be different each time. Well, within limits, you can guarantee a LONG TIME between repeats. And that may be good enough for Peter's purpose. When the number of elements in your sequence is short enough (the right value of "short enough" is at least partially a matter of opinion, and the amount of RAM available, but I would guess n < 10 myself), use itertools.permutations to generate all the possibilities at once -- call this list p. Store p in memory. Then shuffle p, and use its elements one at a time. If you get to the end of p and need to keep working, it's up to you whether to shuffle p a second time. If you don't reshuffle p, it guarantees the maximum interval between reusing the same permutation. Use random.shuffle on your sequence directly, when your sequence has a larger number of elements. This doesn't guarantee that two successive permutations will not be identical, but the probability is of course very low. From john_ladasky at sbcglobal.net Sat May 25 21:25:16 2013 From: john_ladasky at sbcglobal.net (John Ladasky) Date: Sat, 25 May 2013 18:25:16 -0700 (PDT) Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: References: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <15a1bb3a-514c-454e-a966-243c84123be9@googlegroups.com> On Friday, May 24, 2013 10:33:47 AM UTC-7, Yours Truly wrote: > If you don't reshuffle p, it guarantees the maximum interval between reusing > the same permutation. Of course, that comes at a certain price. Given two permutations p[x] and p[x+1], they will ALWAYS be adjacent, in every repetition of the list, unless you reshuffle. So the "spurious correlation" problem that Peter is worrying about would still exist, albeit at a meta-level. You could play clever games, such as splitting p in half once you get to the end of it, shuffling each half independently, and then concatenating the halves. This algorithm scrambles the p[x]'s and p[x+1]'s pretty well, at the cost of cutting the average interval between repeats of a given p[x] from len(p) to something closer to len(p)/2. Because someone's got to say it... "The generation of random numbers is too important to be left to chance." ? Robert R. Coveyou From roy at panix.com Sat May 25 21:49:28 2013 From: roy at panix.com (Roy Smith) Date: Sat, 25 May 2013 21:49:28 -0400 Subject: Simple algorithm question - how to reorder a sequence economically References: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> <15a1bb3a-514c-454e-a966-243c84123be9@googlegroups.com> Message-ID: In article <15a1bb3a-514c-454e-a966-243c84123be9 at googlegroups.com>, John Ladasky wrote: > Because someone's got to say it... "The generation of random numbers is too > important to be left to chance." ? Robert R. Coveyou Absolutely. I know just enough about random number generation to understand that I don't really know anything about it :-) That being said, people who really care about random numbers, tend to rely on some sort of physical process instead of computer algorithms. A classic example would be /dev/random. A somewhat more fun example is http://www.youtube.com/watch?v=7n8LNxGbZbs. Something radioactive and a geiger counter are a good source of randomness (time intervals between decay events). From davea at davea.name Sat May 25 22:05:14 2013 From: davea at davea.name (Dave Angel) Date: Sun, 26 May 2013 02:05:14 -0000 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: References: <519f4661$0$6599$c3e8da3$5496439d@news.astraweb.com> <15a1bb3a-514c-454e-a966-243c84123be9@googlegroups.com> Message-ID: <51A16DCC.1010807@davea.name> On 05/25/2013 09:49 PM, Roy Smith wrote: > In article <15a1bb3a-514c-454e-a966-243c84123be9 at googlegroups.com>, > John Ladasky wrote: > >> Because someone's got to say it... "The generation of random numbers is too >> important to be left to chance." ? Robert R. Coveyou > > Absolutely. I know just enough about random number generation to > understand that I don't really know anything about it :-) > > That being said, people who really care about random numbers, tend to > rely on some sort of physical process instead of computer algorithms. A > classic example would be /dev/random. A somewhat more fun example is > http://www.youtube.com/watch?v=7n8LNxGbZbs. Something radioactive and a > geiger counter are a good source of randomness (time intervals between > decay events). > > That's good unless you're a cat, and you belong to Schroedinger... -- DaveA From carlosnepomuceno at outlook.com Fri May 24 11:00:59 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 18:00:59 +0300 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: References: Message-ID: ---------------------------------------- > Date: Fri, 24 May 2013 01:14:45 -0700 > Subject: Simple algorithm question - how to reorder a sequence economically > From: peter.h.m.brooks at gmail.com > To: python-list at python.org > > What is the easiest way to reorder a sequence pseudo-randomly? > > That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg > 2,1,4,3) that is different each time. > > I'm writing a simulation and would like to visit all the nodes in a > different order at each iteration of the simulation to remove the risk > of a fixed order introducing spurious evidence of correlation. > -- > http://mail.python.org/mailman/listinfo/python-list I don't know what "spurious evidence of correlation" is. Can you give a mathematical definition? Here's a snippet for creating a random shuffle by Fisher?Yates algorithm: def FY_shuffle(l): ??? from random import randint ??? for i in range(len(l)-1,0,-1): ??????? j = randint(0,i) ??????? l[j],l[i] = l[i],l[j] It looks just like random.shuffle() mentioned before, but you can change it as you see fit. If you can afford to test all permutations you can iterate over it by doing: >>> from itertools import permutations >>> l=range(4) >>> l [0, 1, 2, 3] >>> for i in permutations(l): print i ... (0, 1, 2, 3) (0, 1, 3, 2) (0, 2, 1, 3) [...] (3, 1, 2, 0) (3, 2, 0, 1) (3, 2, 1, 0) >>> Note that 'i' is a tuple. If you need a list of all permutations to make a selection: >>> l=range(4) >>> l [0, 1, 2, 3] >>> [list(i) for i in permutations(l)] [[0, 1, 2, 3], [0, 1, 3, 2], [0, 2, 1, 3], [0, 2, 3, 1], [0, 3, 1, 2], [0, 3, 2, 1], [1, 0, 2, 3], [1, 0, 3, 2], [1, 2, 0, 3], [1, 2, 3, 0], [1, 3, 0, 2], [1, 3, 2, 0], [2, 0, 1, 3], [2, 0, 3, 1], [2, 1, 0, 3], [2, 1, 3, 0], [2, 3, 0, 1], [2, 3, 1, 0], [3, 0, 1, 2], [3, 0, 2, 1], [3, 1, 0, 2], [3, 1, 2, 0], [3, 2, 0, 1], [3, 2, 1, 0]] This will produce big lists: -for 10 elements (l=range(10)) the size of the list is about 30MB (on Windows). -for 11 elements (l=range(11)) the size of the list is about 335MB (on Windows). It took more than 7GB for CPython 2.7.5 to create that list. Didn't try after that. From peter.h.m.brooks at gmail.com Fri May 24 15:01:35 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Fri, 24 May 2013 12:01:35 -0700 (PDT) Subject: Simple algorithm question - how to reorder a sequence economically References: Message-ID: <358d5be7-87e8-4655-82ba-859786cc3d47@l3g2000vbl.googlegroups.com> On May 24, 5:00?pm, Carlos Nepomuceno wrote: > > > I don't know what "spurious evidence of correlation" is. Can you give a mathematical definition? > If I run the simulation with the same sequence, then, because event E1 always comes before event E2, somebody might believe that there is a causative connection between them in the world that's being simulated, when, in fact, they only correlate in this way because the sequence is not being shuffled. That's what it means. Actually it'll be a bit more subtle than that, because each iteration of the simulation updates all nodes in one time interval, the events will not usually show the order of iteration - but, where there are any secondary effects, that are related to the order in which the nodes are updated, these will always happen the same way, which is my concern. From carlosnepomuceno at outlook.com Fri May 24 17:33:01 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 00:33:01 +0300 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: <358d5be7-87e8-4655-82ba-859786cc3d47@l3g2000vbl.googlegroups.com> References: , , <358d5be7-87e8-4655-82ba-859786cc3d47@l3g2000vbl.googlegroups.com> Message-ID: ---------------------------------------- > Date: Fri, 24 May 2013 12:01:35 -0700 > Subject: Re: Simple algorithm question - how to reorder a sequence economically > From: peter.h.m.brooks at gmail.com > To: python-list at python.org > > On May 24, 5:00 pm, Carlos Nepomuceno > wrote: >> >> >> I don't know what "spurious evidence of correlation" is. Can you give a mathematical definition? >> > If I run the simulation with the same sequence, then, because event E1 > always comes before event E2, somebody might believe that there is a > causative connection between them in the world that's being simulated, > when, in fact, they only correlate in this way because the sequence is > not being shuffled. That's what it means. Correlation does not imply causation. If "somebody" is an expert system and you want to avoid it's recognition and/or triggering of some kind, and you can't or don't want to change it's behavior, you may take the random way because it's cheaper. > Actually it'll be a bit more subtle than that, because each iteration > of the simulation updates all nodes in one time interval, the events > will not usually show the order of iteration - but, where there are > any secondary effects, that are related to the order in which the > nodes are updated, these will always happen the same way, which is my > concern. You should have a more precise understanding of the dependence of the variables you taking in consideration before randomizing the series of events your are using for tests. I suggest you start using PPMCC. If it's close to zero or negative you wouldn't have to mind about it! ;) From peter.h.m.brooks at gmail.com Fri May 24 20:28:07 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Fri, 24 May 2013 17:28:07 -0700 (PDT) Subject: Simple algorithm question - how to reorder a sequence economically References: , , <358d5be7-87e8-4655-82ba-859786cc3d47@l3g2000vbl.googlegroups.com> Message-ID: <6e0cf171-b4c7-46d1-a66a-dcc194a3e5ad@g9g2000vbl.googlegroups.com> On May 24, 11:33?pm, Carlos Nepomuceno wrote: > ---------------------------------------- > > > > > > > > > > > Date: Fri, 24 May 2013 12:01:35 -0700 > > Subject: Re: Simple algorithm question - how to reorder a sequence economically > > From: peter.h.m.bro... at gmail.com > > To: python-l... at python.org > > > On May 24, 5:00 pm, Carlos Nepomuceno > > wrote: > > >> I don't know what "spurious evidence of correlation" is. Can you give a mathematical definition? > > > If I run the simulation with the same sequence, then, because event E1 > > always comes before event E2, somebody might believe that there is a > > causative connection between them in the world that's being simulated, > > when, in fact, they only correlate in this way because the sequence is > > not being shuffled. That's what it means. > > Correlation does not imply causation. If "somebody" is an expert system and you want to avoid it's recognition and/or triggering of some kind, and you can't or don't want to change it's behavior, you may take the random way because it's cheaper. > > > Actually it'll be a bit more subtle than that, because each iteration > > of the simulation updates all nodes in one time interval, the events > > will not usually show the order of iteration - but, where there are > > any secondary effects, that are related to the order in which the > > nodes are updated, these will always happen the same way, which is my > > concern. > > You should have a more precise understanding of the dependence of the variables you taking in consideration before randomizing the series of events your are using for tests. > If the scenario could be modelled mathematically, then there'd be no point in writing the simulation. From carlosnepomuceno at outlook.com Fri May 24 21:52:36 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 01:52:36 -0000 Subject: Simple algorithm question - how to reorder a sequence economically In-Reply-To: <6e0cf171-b4c7-46d1-a66a-dcc194a3e5ad@g9g2000vbl.googlegroups.com> References: , , , , <358d5be7-87e8-4655-82ba-859786cc3d47@l3g2000vbl.googlegroups.com>, , <6e0cf171-b4c7-46d1-a66a-dcc194a3e5ad@g9g2000vbl.googlegroups.com> Message-ID: ---------------------------------------- > Date: Fri, 24 May 2013 17:28:07 -0700 > Subject: Re: Simple algorithm question - how to reorder a sequence economically > From: peter.h.m.brooks at gmail.com > To: python-list at python.org [...] > If the scenario could be modelled mathematically, then there'd be no > point in writing the simulation. I don't know what you mean. A computer simulation is an instance of a mathematical model. From peter.h.m.brooks at gmail.com Fri May 24 04:18:06 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Fri, 24 May 2013 01:18:06 -0700 (PDT) Subject: Using ACLs in JSON Message-ID: <6e35fb87-6c6b-4fcc-a99c-199af8b1a4c1@k3g2000vbn.googlegroups.com> I'm designing a system that should allow different views to different audiences. I understand that I can use application logic to control the access security, but it seems to me that it'd make more sense to have this documented in the data-stream so that it's data-driven. I was wondering if there was any standard way of doing this in JSON. Alternatively, is there a better way of organising this in Python that's compatible with JSON? I've put an example of the sort of thing that I mean below. The idea is that this object is accessible for viewing or deleting by the role 'HR' and available for change only to the owner of the record itself. In addition, the record can be viewed by those with the role 'manager'. The syntax may be wrong, but I hope that my intention is reasonably clear. Is there an existing practice or standard for doing this sort of thing? { "title" : "Example Schema", "type" : "object", "version" : "1.0", "properties": { "firstname" : { "type": "string" }, "lastname" : { "type": "string" }, "age" : { "description" : "Age in years", "type": "integer", "minimum": 0 } }, "ACL-view": ["HR","Manager",["firstname","lastname"]], "ACL-change": ["firstname","Lastname"], "ACL-delete": ["HR"], "required": ["firstname","lastname"] } From carlosnepomuceno at outlook.com Fri May 24 12:13:15 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 24 May 2013 19:13:15 +0300 Subject: Using ACLs in JSON In-Reply-To: <6e35fb87-6c6b-4fcc-a99c-199af8b1a4c1@k3g2000vbn.googlegroups.com> References: <6e35fb87-6c6b-4fcc-a99c-199af8b1a4c1@k3g2000vbn.googlegroups.com> Message-ID: Not exactly what you want but you may consider Google ACL XML[1]. If there aren't any system integration restrictions you can do what you think it's best... for now. [1] https://developers.google.com/storage/docs/accesscontrol#applyacls ---------------------------------------- > Date: Fri, 24 May 2013 01:18:06 -0700 > Subject: Using ACLs in JSON > From: peter.h.m.brooks at gmail.com > To: python-list at python.org > > I'm designing a system that should allow different views to different > audiences. I understand that I can use application logic to control > the access security, but it seems to me that it'd make more sense to > have this documented in the data-stream so that it's data-driven. > > I was wondering if there was any standard way of doing this in JSON. > Alternatively, is there a better way of organising this in Python > that's compatible with JSON? > > I've put an example of the sort of thing that I mean below. The idea > is that this object is accessible for viewing or deleting by the role > 'HR' and available for change only to the owner of the record itself. > In addition, the record can be viewed by those with the role > 'manager'. The syntax may be wrong, but I hope that my intention is > reasonably clear. > > Is there an existing practice or standard for doing this sort of > thing? > > { > "title" : "Example Schema", > "type" : "object", > "version" : "1.0", > "properties": { > "firstname" : { > "type": "string" > }, > "lastname" : { > "type": "string" > }, > "age" : { > "description" : "Age in years", > "type": "integer", > "minimum": 0 > } > }, > "ACL-view": ["HR","Manager",["firstname","lastname"]], > "ACL-change": ["firstname","Lastname"], > "ACL-delete": ["HR"], > "required": ["firstname","lastname"] > } > -- > http://mail.python.org/mailman/listinfo/python-list From torriem at gmail.com Fri May 24 12:42:04 2013 From: torriem at gmail.com (Michael Torrie) Date: Fri, 24 May 2013 10:42:04 -0600 Subject: Using ACLs in JSON In-Reply-To: <6e35fb87-6c6b-4fcc-a99c-199af8b1a4c1@k3g2000vbn.googlegroups.com> References: <6e35fb87-6c6b-4fcc-a99c-199af8b1a4c1@k3g2000vbn.googlegroups.com> Message-ID: <519F985C.2070501@gmail.com> On 05/24/2013 02:18 AM, Peter Brooks wrote: > I'm designing a system that should allow different views to different > audiences. I understand that I can use application logic to control > the access security, but it seems to me that it'd make more sense to > have this documented in the data-stream so that it's data-driven. > > I was wondering if there was any standard way of doing this in JSON. > Alternatively, is there a better way of organising this in Python > that's compatible with JSON? While I don't understand exactly what you're using JSON for, and nor do I understand the purpose of the JSON structure you posted, I can say that ACLs have nothing to do with JSON. JSON is simply a data markup format, like HTML, XML, plain text, or an INI file. It's merely data. If you want to restrict who sees what when they request a chunk of data formatted using JSON, then you have to enforce that in the code that's processing the request for data using another mechanism. And that mechanism depends on how your clients ask for JSON data, and what code is serving or generating the JSON data. From peter.h.m.brooks at gmail.com Fri May 24 15:06:12 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Fri, 24 May 2013 12:06:12 -0700 (PDT) Subject: Using ACLs in JSON References: <6e35fb87-6c6b-4fcc-a99c-199af8b1a4c1@k3g2000vbn.googlegroups.com> Message-ID: <6aa18966-3b98-4ec7-926e-11ca012de30a@gb2g2000vbb.googlegroups.com> On May 24, 6:42?pm, Michael Torrie wrote: > On 05/24/2013 02:18 AM, Peter Brooks wrote: > > > I'm designing a system that should allow different views to different > > audiences. I understand that I can use application logic to control > > the access security, but it seems to me that it'd make more sense to > > have this documented in the data-stream so that it's data-driven. > > > I was wondering if there was any standard way of doing this in JSON. > > Alternatively, is there a better way of organising this in Python > > that's compatible with JSON? > > While I don't understand exactly what you're using JSON for, and nor do > I understand the purpose of the JSON structure you posted, I can say > that ACLs have nothing to do with JSON. > > JSON is simply a data markup format, like HTML, XML, plain text, or an > INI file. ?It's merely data. ?If you want to restrict who sees what when > they request a chunk of data formatted using JSON, then you have to > enforce that in the code that's processing the request for data using > another mechanism. ?And that mechanism depends on how your clients ask > for JSON data, and what code is serving or generating the JSON data. > Yes, you're right, I know that. However, I want the permissions embedded in the data so that it's easy to verify, from the data, who has access to which objects. The reason is to enforce transparency of the access rights and to make them easy to grant and change without needing to make any coding changes. My question was whether anybody had taken a similar approach and found a way that worked well with python or, even better, if there was a standard way of doing this. If nobody has done this, then I'm happy to invent my own method, but I don't really want to reinvent wheels that might have been nicely designed already. From peter.h.m.brooks at gmail.com Fri May 24 15:08:03 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Fri, 24 May 2013 12:08:03 -0700 (PDT) Subject: Using ACLs in JSON References: <6e35fb87-6c6b-4fcc-a99c-199af8b1a4c1@k3g2000vbn.googlegroups.com> Message-ID: On May 24, 6:13?pm, Carlos Nepomuceno wrote: > Not exactly what you want but you may consider Google ACL XML[1]. > > If there aren't any system integration restrictions you can do what you think it's best... for now. > > [1]https://developers.google.com/storage/docs/accesscontrol#applyacls > Thank you for the reference. I specifically don't want to use XML itself, but I can adapt that to my purpose - great! From carlosnepomuceno at outlook.com Fri May 24 17:12:37 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 00:12:37 +0300 Subject: Using ACLs in JSON In-Reply-To: References: <6e35fb87-6c6b-4fcc-a99c-199af8b1a4c1@k3g2000vbn.googlegroups.com>, , Message-ID: You welcome! Can you send me whatever you decide is best to your case? I'd like to have an example just in case I have to do that in the future. I think that approach is gonna become more prevalent in the coming years. ;) ---------------------------------------- > Date: Fri, 24 May 2013 12:08:03 -0700 > Subject: Re: Using ACLs in JSON > From: peter.h.m.brooks at gmail.com > To: python-list at python.org > > On May 24, 6:13 pm, Carlos Nepomuceno > wrote: >> Not exactly what you want but you may consider Google ACL XML[1]. >> >> If there aren't any system integration restrictions you can do what you think it's best... for now. >> >> [1]https://developers.google.com/storage/docs/accesscontrol#applyacls >> > Thank you for the reference. I specifically don't want to use XML > itself, but I can adapt that to my purpose - great! > -- > http://mail.python.org/mailman/listinfo/python-list From rvince99 at gmail.com Fri May 24 07:40:22 2013 From: rvince99 at gmail.com (RVic) Date: Fri, 24 May 2013 04:40:22 -0700 (PDT) Subject: Polymoprhism question Message-ID: I'm trying to figure out (or find an example) of polymorphism whereby I pass a commandline argument (a string) which comports to a class (in java, you would say that it comports to a given interface bu I don't know if there is such a thing in Python) then that class of that name, somehow gets intantiated from that string. This way, I can have similar classes, but have my program use various ones by simply changing the commandline argument. Can anyone show me how this might be done in Python? Thanks. -RVic From steve+comp.lang.python at pearwood.info Fri May 24 08:10:16 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 May 2013 12:10:16 GMT Subject: Polymoprhism question References: Message-ID: <519f58a7$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 24 May 2013 04:40:22 -0700, RVic wrote: > I'm trying to figure out (or find an example) of polymorphism whereby I > pass a commandline argument (a string) which comports to a class (in > java, you would say that it comports to a given interface bu I don't > know if there is such a thing in Python) then that class of that name, > somehow gets intantiated from that string. This way, I can have similar > classes, but have my program use various ones by simply changing the > commandline argument. > > Can anyone show me how this might be done in Python? Thanks. I'm not 100% sure I understand what you want, but my guess is you want something like this: # A toy class. class AClass(object): def __init__(self, astring): self.astring = astring def __repr__(self): return "%s(%r)" % (self.__class__.__name__, self.astring) # And some variations. class BClass(AClass): pass class CClass(AClass): pass # Build a dispatch table, mapping the class name to the class itself. TABLE = {} for cls in (AClass, BClass, CClass): TABLE[cls.__name__] = cls # Get the name of the class, and an argument, from the command line. # Or from the user. Any source of two strings will do. # Data validation is left as an exercise. import sys argv = sys.argv[1:] if not argv: name = raw_input("Name of the class to use? ") arg = raw_input("And the argument to use? ") argv = [name, arg] # Instantiate. instance = TABLE[argv[0]](argv[1]) print instance -- Steven From rvince99 at gmail.com Fri May 24 15:19:20 2013 From: rvince99 at gmail.com (RVic) Date: Fri, 24 May 2013 12:19:20 -0700 (PDT) Subject: Polymoprhism question In-Reply-To: References: Message-ID: <3f9acff7-151b-4aa4-8b59-fcddc2755691@googlegroups.com> Thanks Steven, Yes, I see Python isn't going to do this very well, from what I can understand. Lets say I have a type of class, and this type of class will always have two methods, in() and out(). Here is, essentially, what I am trying to do, but I don't know if this will make sense to you or if it is really doable in Python: #thanks, RVic import sys argv = sys.argv[1:] ClassIamInstantiating = argv ClassIamInstantiating.in("something") x = ClassIamInstantiating.out() From neilc at norwich.edu Fri May 24 16:23:48 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 24 May 2013 20:23:48 GMT Subject: Polymoprhism question References: <3f9acff7-151b-4aa4-8b59-fcddc2755691@googlegroups.com> Message-ID: On 2013-05-24, RVic wrote: > Thanks Steven, > > Yes, I see Python isn't going to do this very well, from what I > can understand. > > Lets say I have a type of class, and this type of class will > always have two methods, in() and out(). > > Here is, essentially, what I am trying to do, but I don't know > if this will make sense to you or if it is really doable in > Python: #thanks, RVic > > import sys > argv = sys.argv[1:] > ClassIamInstantiating = argv > ClassIamInstantiating.in("something") > x = ClassIamInstantiating.out() This is pretty easy in Python using the __name__ attribute. import sys class A: def in(self): print("A in") def out(self): print("A out") class B: def in(self): print("B in") def out(self): print("B out") classes = {cls.__name__: cls for cls in (A, B)} ArgType = classes[sys.agrv[1]] arg = ArgType() arg.in("test") arg.out("test") -- Neil Cerutti From malte.forkel at berlin.de Fri May 24 08:58:24 2013 From: malte.forkel at berlin.de (Malte Forkel) Date: Fri, 24 May 2013 14:58:24 +0200 Subject: Utility to locate errors in regular expressions Message-ID: Finding out why a regular expression does not match a given string can very tedious. I would like to write a utility that identifies the sub-expression causing the non-match. My idea is to use a parser to create a tree representing the complete regular expression. Then I could simplify the expression by dropping sub-expressions one by one from right to left and from bottom to top until the remaining regex matches. The last sub-expression dropped should be (part of) the problem. As a first step, I am looking for a parser for Python regular expressions, or a Python regex grammar to create a parser from. But may be my idea is flawed? Or a similar (or better) tools already exists? Any advice will be highly appreciated! Malte From jeanpierreda at gmail.com Fri May 24 09:13:41 2013 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 24 May 2013 09:13:41 -0400 Subject: Utility to locate errors in regular expressions In-Reply-To: References: Message-ID: On Fri, May 24, 2013 at 8:58 AM, Malte Forkel wrote: > As a first step, I am looking for a parser for Python regular > expressions, or a Python regex grammar to create a parser from. the sre_parse module is undocumented, but very usable. > But may be my idea is flawed? Or a similar (or better) tools already > exists? Any advice will be highly appreciated! I think your task is made problematic by the possibility that no single part of the regexp causes a match failure. What causes failure depends on what branches are chosen with the |, *, +, ?, etc. operators -- it might be a different character/subexpression for each branch. And then there's exponentially many possible branches. -- Devin From roy at panix.com Fri May 24 09:12:16 2013 From: roy at panix.com (Roy Smith) Date: Fri, 24 May 2013 09:12:16 -0400 Subject: Utility to locate errors in regular expressions References: Message-ID: In article , Malte Forkel wrote: > Finding out why a regular expression does not match a given string can > very tedious. I would like to write a utility that identifies the > sub-expression causing the non-match. My idea is to use a parser to > create a tree representing the complete regular expression. Then I could > simplify the expression by dropping sub-expressions one by one from > right to left and from bottom to top until the remaining regex matches. > The last sub-expression dropped should be (part of) the problem. > > As a first step, I am looking for a parser for Python regular > expressions, or a Python regex grammar to create a parser from. > > But may be my idea is flawed? Or a similar (or better) tools already > exists? Any advice will be highly appreciated! I think this would be a really cool tool. The debugging process I've always used is essentially what you describe. I start try progressively shorter sub-patterns until I get a match, then try to incrementally add back little bits of the original pattern until it no longer matches. With luck, the problem will become obvious at that point. Having a tool which automated this would be really useful. Of course, most of Python user community are wimps and shy away from big hairy regexes [ducking and running]. From neilc at norwich.edu Fri May 24 09:58:37 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 24 May 2013 13:58:37 GMT Subject: Utility to locate errors in regular expressions References: Message-ID: On 2013-05-24, Roy Smith wrote: > Of course, most of Python user community are wimps and shy away > from big hairy regexes [ducking and running]. I prefer the simple, lumbering regular expressions like those in the original Night of the Regular Expressions. The fast, powerful ones from programs like the remake of Dawn of the GREP, just aren't as scary. -- Neil Cerutti From rustompmody at gmail.com Fri May 24 10:09:41 2013 From: rustompmody at gmail.com (rusi) Date: Fri, 24 May 2013 07:09:41 -0700 (PDT) Subject: Utility to locate errors in regular expressions References: Message-ID: On May 24, 5:58?pm, Malte Forkel wrote: > Finding out why a regular expression does not match a given string can > very tedious. I would like to write a utility that identifies the > sub-expression causing the non-match. My idea is to use a parser to > create a tree representing the complete regular expression. Then I could > simplify the expression by dropping sub-expressions one by one from > right to left and from bottom to top until the remaining regex matches. > The last sub-expression dropped should be (part of) the problem. > > As a first step, I am looking for a parser for Python regular > expressions, or a Python regex grammar to create a parser from. > > But may be my idea is flawed? Or a similar (or better) tools already > exists? Any advice will be highly appreciated! > > Malte python-specific: http://kodos.sourceforge.net/ Online: http://gskinner.com/RegExr/ emacs-specific: re-builder and regex-tool http://bc.tech.coop/blog/071103.html From auriocus at gmx.de Fri May 24 14:21:39 2013 From: auriocus at gmx.de (Christian Gollwitzer) Date: Fri, 24 May 2013 20:21:39 +0200 Subject: Utility to locate errors in regular expressions In-Reply-To: References: Message-ID: Am 24.05.13 14:58, schrieb Malte Forkel: > Finding out why a regular expression does not match a given string can > very tedious. I would like to write a utility that identifies the > sub-expression causing the non-match. Try http://laurent.riesterer.free.fr/regexp/ it shows the subexpressions which cause the match by coloring the parts. Not exacty what you want, but very intuitive and powerful. Beware this is Tcl and there might be subtle differences in RE syntax, but largely it's the same. Christian From roy at panix.com Fri May 24 09:40:12 2013 From: roy at panix.com (Roy Smith) Date: Fri, 24 May 2013 09:40:12 -0400 Subject: Utility to locate errors in regular expressions References: Message-ID: In article , Devin Jeanpierre wrote: > On Fri, May 24, 2013 at 8:58 AM, Malte Forkel wrote: > > As a first step, I am looking for a parser for Python regular > > expressions, or a Python regex grammar to create a parser from. > > the sre_parse module is undocumented, but very usable. > > > But may be my idea is flawed? Or a similar (or better) tools already > > exists? Any advice will be highly appreciated! > > I think your task is made problematic by the possibility that no > single part of the regexp causes a match failure. What causes failure > depends on what branches are chosen with the |, *, +, ?, etc. > operators -- it might be a different character/subexpression for each > branch. And then there's exponentially many possible branches. That's certainly true. The full power of regex makes stuff like this very hard to do in the general case. That being said, people tend to write regexen which match hunks of text from left to right. So, in theory, it's probably an intractable problem. But, in practice, such a tool would actually be useful in a large set of real-life cases. From luca.cerone at gmail.com Fri May 24 10:04:36 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Fri, 24 May 2013 07:04:36 -0700 (PDT) Subject: Piping processes works with 'shell = True' but not otherwise. Message-ID: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> Hi everybody, I am new to the group (and relatively new to Python) so I am sorry if this issues has been discussed (although searching for topics in the group I couldn't find a solution to my problem). I am using Python 2.7.3 to analyse the output of two 3rd parties programs that can be launched in a linux shell as: program1 | program2 To do this I have written a function that pipes program1 and program2 (using subprocess.Popen) and the stdout of the subprocess, and a function that parses the output: A basic example: from subprocess import Popen, STDOUT, PIPE def run(): p1 = Popen(['program1'], stdout = PIPE, stderr = STDOUT) p2 = Popen(['program2'], stdin = p1.stdout, stdout = PIPE, stderr = STDOUT) p1.stdout.close() return p2.stdout def parse(out): for row in out: print row #do something else with each line out.close() return parsed_output # main block here pout = run() parsed = parse(pout) #--- END OF PROGRAM ----# I want to parse the output of 'program1 | program2' line by line because the output is very large. When running the code above, occasionally some error occurs (IOERROR: [Errno 0]). However this error doesn't occur if I code the run() function as: def run(): p = Popen('program1 | program2', shell = True, stderr = STDOUT, stdout = PIPE) return p.stdout I really can't understand why the first version causes errors, while the second one doesn't. Can you please help me understanding what's the difference between the two cases? Thanks a lot in advance for the help, Cheers, Luca From luca.cerone at gmail.com Sun May 26 06:31:51 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Sun, 26 May 2013 03:31:51 -0700 (PDT) Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> Message-ID: <53d92a77-14ed-4999-90ac-e1c00f9889d5@googlegroups.com> > > Can you please help me understanding what's the difference between the two cases? > Hi guys has some of you ideas on what is causing my issue? From clp2 at rebertia.com Sun May 26 17:05:19 2013 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 26 May 2013 14:05:19 -0700 Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> Message-ID: On May 24, 2013 7:06 AM, "Luca Cerone" wrote: > > Hi everybody, > I am new to the group (and relatively new to Python) > so I am sorry if this issues has been discussed (although searching for topics in the group I couldn't find a solution to my problem). > > I am using Python 2.7.3 to analyse the output of two 3rd parties programs that can be launched in a linux shell as: > > program1 | program2 > > To do this I have written a function that pipes program1 and program2 (using subprocess.Popen) and the stdout of the subprocess, and a function that parses the output: > > A basic example: > > from subprocess import Popen, STDOUT, PIPE > def run(): > p1 = Popen(['program1'], stdout = PIPE, stderr = STDOUT) > p2 = Popen(['program2'], stdin = p1.stdout, stdout = PIPE, stderr = STDOUT) Could you provide the *actual* commands you're using, rather than the generic "program1" and "program2" placeholders? It's *very* common for people to get the tokenization of a command line wrong (see the Note box in http://docs.python.org/2/library/subprocess.html#subprocess.Popen for some relevant advice). > p1.stdout.close() > return p2.stdout > > > def parse(out): > for row in out: > print row > #do something else with each line > out.close() > return parsed_output > > > # main block here > > pout = run() > > parsed = parse(pout) > > #--- END OF PROGRAM ----# > > I want to parse the output of 'program1 | program2' line by line because the output is very large. > > When running the code above, occasionally some error occurs (IOERROR: [Errno 0]). Could you provide the full & complete error message and exception traceback? > However this error doesn't occur if I code the run() function as: > > def run(): > p = Popen('program1 | program2', shell = True, stderr = STDOUT, stdout = PIPE) > return p.stdout > > I really can't understand why the first version causes errors, while the second one doesn't. > > Can you please help me understanding what's the difference between the two cases? One obvious difference between the 2 approaches is that the shell doesn't redirect the stderr streams of the programs, whereas you /are/ redirecting the stderrs to stdout in the non-shell version of your code. But this is unlikely to be causing the error you're currently seeing. You may also want to provide /dev/null as p1's stdin, out of an abundance of caution. Lastly, you may want to consider using a wrapper library such as http://plumbum.readthedocs.org/en/latest/ , which makes it easier to do pipelining and other such "fancy" things with subprocesses, while still avoiding the many perils of the shell. Cheers, Chris -- Be patient; it's Memorial Day weekend. -------------- next part -------------- An HTML attachment was scrubbed... URL: From luca.cerone at gmail.com Sun May 26 19:58:57 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Sun, 26 May 2013 16:58:57 -0700 (PDT) Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> Message-ID: <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> > Could you provide the *actual* commands you're using, rather than the generic "program1" and "program2" placeholders? It's *very* common for people to get the tokenization of a command line wrong (see the Note box in http://docs.python.org/2/library/subprocess.html#subprocess.Popen for some relevant advice). > Hi Chris, first of all thanks for the help. Unfortunately I can't provide the actual commands because are tools that are not publicly available. I think I get the tokenization right, though.. the problem is not that the programs don't run.. it is just that sometimes I get that error.. Just to be clear I run the process like: p = subprocess.Popen(['program1','--opt1','val1',...'--optn','valn'], ... the rest) which I think is the right way to pass arguments (it works fine for other commands).. > > Could you provide the full & complete error message and exception traceback? > yes, as soon as I get to my work laptop.. > > One obvious difference between the 2 approaches is that the shell doesn't redirect the stderr streams of the programs, whereas you /are/ redirecting the stderrs to stdout in the non-shell version of your code. But this is unlikely to be causing the error you're currently seeing. > > > You may also want to provide /dev/null as p1's stdin, out of an abundance of caution. > I tried to redirect the output to /dev/null using the Popen argument: 'stdin = os.path.devnull' (having imported os of course).. But this seemed to cause even more troubles... > Lastly, you may want to consider using a wrapper library such as http://plumbum.readthedocs.org/en/latest/ , which makes it easier to do pipelining and other such "fancy" things with subprocesses, while still avoiding the many perils of the shell. > > Thanks, I didn't know this library, I'll give it a try. Though I forgot to mention that I was using the subprocess module, because I want the code to be portable (even though for now if it works in Unix platform is OK). Thanks a lot for your help, Cheers, Luca From carlosnepomuceno at outlook.com Sun May 26 20:14:24 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 27 May 2013 03:14:24 +0300 Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com>, , <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> Message-ID: pipes usually consumes disk storage at '/tmp'. Are you sure you have enough room on that filesystem? Make sure no other processes are competing against for that space. Just my 50c because I don't know what's causing Errno 0. I don't even know what are the possible causes of such error. Good luck! ---------------------------------------- > Date: Sun, 26 May 2013 16:58:57 -0700 > Subject: Re: Piping processes works with 'shell = True' but not otherwise. > From: luca.cerone at gmail.com > To: python-list at python.org [...] > I tried to redirect the output to /dev/null using the Popen argument: > 'stdin = os.path.devnull' (having imported os of course).. > But this seemed to cause even more troubles... > >> Lastly, you may want to consider using a wrapper library such as http://plumbum.readthedocs.org/en/latest/ , which makes it easier to do pipelining and other such "fancy" things with subprocesses, while still avoiding the many perils of the shell. >> >> > Thanks, I didn't know this library, I'll give it a try. > Though I forgot to mention that I was using the subprocess module, because I want the code to be portable (even though for now if it works in Unix platform is OK). > > Thanks a lot for your help, > Cheers, > Luca > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Mon May 27 04:28:32 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 18:28:32 +1000 Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> Message-ID: On Mon, May 27, 2013 at 9:58 AM, Luca Cerone wrote: >> Could you provide the *actual* commands you're using, rather than the generic "program1" and "program2" placeholders? It's *very* common for people to get the tokenization of a command line wrong (see the Note box in http://docs.python.org/2/library/subprocess.html#subprocess.Popen for some relevant advice). >> > Hi Chris, first of all thanks for the help. Unfortunately I can't provide the actual commands because are tools that are not publicly available. > I think I get the tokenization right, though.. the problem is not that the programs don't run.. it is just that sometimes I get that error.. Will it violate privacy / NDA to post the command line? Even if we can't actually replicate your system, we may be able to see something from the commands given. ChrisA From luca.cerone at gmail.com Mon May 27 07:33:19 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 27 May 2013 04:33:19 -0700 (PDT) Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> Message-ID: <4fd43d0e-9c72-41d0-8c8d-dbc0d0a50022@googlegroups.com> > > > Will it violate privacy / NDA to post the command line? Even if we > > can't actually replicate your system, we may be able to see something > > from the commands given. > > Unfortunately yes.. From clp2 at rebertia.com Wed May 29 13:17:37 2013 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 May 2013 10:17:37 -0700 Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 4:58 PM, Luca Cerone wrote: > Hi Chris, first of all thanks for the help. Unfortunately I can't provide the actual commands because are tools that are not publicly available. > I think I get the tokenization right, though.. the problem is not that the programs don't run.. it is just that sometimes I get that error.. > > Just to be clear I run the process like: > > p = subprocess.Popen(['program1','--opt1','val1',...'--optn','valn'], ... the rest) > > which I think is the right way to pass arguments (it works fine for other commands).. >> You may also want to provide /dev/null as p1's stdin, out of an abundance of caution. > > I tried to redirect the output to /dev/null using the Popen argument: > 'stdin = os.path.devnull' (having imported os of course).. > But this seemed to cause even more troubles... That's because stdin/stdout/stderr take file descriptors or file objects, not path strings. Cheers, Chris From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Wed May 29 13:39:40 2013 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Wed, 29 May 2013 19:39:40 +0200 Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com>, , <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> Message-ID: Am 27.05.2013 02:14 schrieb Carlos Nepomuceno: > pipes usually consumes disk storage at '/tmp'. Good that my pipes don't know about that. Why should that happen? Thomas From carlosnepomuceno at outlook.com Wed May 29 15:31:05 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 29 May 2013 22:31:05 +0300 Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com>, , , , <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com>, , Message-ID: ---------------------------------------- > From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de > Subject: Re: Piping processes works with 'shell = True' but not otherwise. > Date: Wed, 29 May 2013 19:39:40 +0200 > To: python-list at python.org > > Am 27.05.2013 02:14 schrieb Carlos Nepomuceno: >> pipes usually consumes disk storage at '/tmp'. > > Good that my pipes don't know about that. > > Why should that happen? > > > Thomas > -- > http://mail.python.org/mailman/listinfo/python-list Ooops! My mistake! We've been using 'tee' when in debugging mode and I though that would apply to this case. Nevermind! From cs at zip.com.au Wed May 29 18:18:30 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 30 May 2013 08:18:30 +1000 Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: References: Message-ID: <20130529221830.GA51388@cskk.homeip.net> On 29May2013 19:39, Thomas Rachel wrote: | Am 27.05.2013 02:14 schrieb Carlos Nepomuceno: | >pipes usually consumes disk storage at '/tmp'. | | Good that my pipes don't know about that. | Why should that happen? It probably doesn't on anything modern. On V7 UNIX at least there was a kernel notion of the "pipe fs", where pipe storage existed; usually /tmp; using small real (but unnamed) files is an easy way to implement them, especially on systems where RAM is very small and without a paging VM - for example, V7 UNIX ran on PDP-11s amongst other things. And files need a filesystem. But even then pipes are still small fixed length buffers; they don't grow without bound as you might have inferred from the quoted statement. Cheers, -- Cameron Simpson ERROR 155 - You can't do that. - Data General S200 Fortran error code list From luca.cerone at gmail.com Fri May 31 05:28:47 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Fri, 31 May 2013 02:28:47 -0700 (PDT) Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> Message-ID: <8c89fb10-63eb-446c-855b-f4c5406976ea@googlegroups.com> > > That's because stdin/stdout/stderr take file descriptors or file > > objects, not path strings. > Thanks Chris, how do I set the file descriptor to /dev/null then? From __peter__ at web.de Fri May 31 05:52:41 2013 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 May 2013 11:52:41 +0200 Subject: Piping processes works with 'shell = True' but not otherwise. References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> <8c89fb10-63eb-446c-855b-f4c5406976ea@googlegroups.com> Message-ID: Luca Cerone wrote: >> >> That's because stdin/stdout/stderr take file descriptors or file >> >> objects, not path strings. >> > > Thanks Chris, how do I set the file descriptor to /dev/null then? For example: with open(os.devnull, "wb") as stderr: p = subprocess.Popen(..., stderr=stderr) ... In Python 3.3 and above: p = subprocess.Popen(..., stderr=subprocess.DEVNULL) From logan.c.graham at gmail.com Fri May 24 13:32:26 2013 From: logan.c.graham at gmail.com (logan.c.graham at gmail.com) Date: Fri, 24 May 2013 10:32:26 -0700 (PDT) Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) Message-ID: Hey guys, I'm learning Python and I'm experimenting with different projects -- I like learning by doing. I'm wondering if you can help me here: http://i.imgur.com/KgvSKWk.jpg What this is is a publicly-accessible webpage that's a simple database of people who have used the website. Ideally what I'd like to end up with is an excel spreadsheet with data from the columns #fb, # vids, fb sent?, # email tm. I'd like to use Python to do it -- crawl the page and extract the data in a usable way. I'd love your input! I'm just a learner. From davea at davea.name Fri May 24 15:41:27 2013 From: davea at davea.name (Dave Angel) Date: Fri, 24 May 2013 15:41:27 -0400 Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: <519FC267.5050406@davea.name> On 05/24/2013 01:32 PM, logan.c.graham at gmail.com wrote: > Hey guys, > > I'm learning Python Welcome. > and I'm experimenting with different projects -- I like learning by doing. I'm wondering if you can help me here: > >na > > What this is is a publicly-accessible webpage No, it's just a jpeg file, an image. > that's a simple database of people who have used the website. Ideally what I'd like to end up with is an excel spreadsheet with data from the columns #fb, # vids, fb sent?, # email tm. > > I'd like to use Python to do it -- crawl the page and extract the data in a usable way. > But there's no page to crawl. You may have to start by finding an ocr to interpret the image as characters. Or find some other source for your data. > I'd love your input! I'm just a learner. > -- DaveA From carlosnepomuceno at outlook.com Fri May 24 19:36:35 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 02:36:35 +0300 Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: ### table_data_extraction.py ### # Usage: table[id][row][column] # tables[0]?????? : 1st table # tables[1][2]??? : 3rd row of 2nd table # tables[3][4][5] : cell content of 6th column of 5th row of 4th table # len(table)????? : quantity of tables # len(table[6])?? : quantity of rows of 7th table # len(table[7][8]): quantity of columns of 9th row of 8th table impor re import urllib2 #to retrieve the contents of the page page = urllib2.urlopen("http://example.com/page.html").read().strip() #to create the tables list tables=[[re.findall('
',r,re.S) for r in re.findall('(.*?)',t,re.S)] for t in re.findall('
RussianEnglishDelete
' if russian != None: edit_table += '''''' % locals() edit_table += '' edit_table += '''''' % locals() edit_table += '' edit_table += '''''' % locals() edit_table += '
(.*?)
(.*?)
',page,re.S)] Pretty simple. Good luck! ---------------------------------------- > Date: Fri, 24 May 2013 10:32:26 -0700 > Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) > From: logan.c.graham at gmail.com > To: python-list at python.org > > Hey guys, > > I'm learning Python and I'm experimenting with different projects -- I like learning by doing. I'm wondering if you can help me here: > > http://i.imgur.com/KgvSKWk.jpg > > What this is is a publicly-accessible webpage that's a simple database of people who have used the website. Ideally what I'd like to end up with is an excel spreadsheet with data from the columns #fb, # vids, fb sent?, # email tm. > > I'd like to use Python to do it -- crawl the page and extract the data in a usable way. > > I'd love your input! I'm just a learner. > -- > http://mail.python.org/mailman/listinfo/python-list From davea at davea.name Fri May 24 21:16:37 2013 From: davea at davea.name (Dave Angel) Date: Fri, 24 May 2013 21:16:37 -0400 Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: <51A010F5.4020607@davea.name> On 05/24/2013 07:36 PM, Carlos Nepomuceno wrote: > > > page = urllib2.urlopen("http://example.com/page.html").read().strip() > > #to create the tables list > tables=[[re.findall('(.*?)',r,re.S) for r in re.findall('(.*?)',t,re.S)] for t in re.findall('(.*?)
',page,re.S)] > > > Pretty simple. Good luck! Only if the page is html, which the OP's was not. It was an image. Try parsing that with regex. -- DaveA From rosuav at gmail.com Fri May 24 23:22:54 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 13:22:54 +1000 Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: On Sat, May 25, 2013 at 3:32 AM, wrote: > http://i.imgur.com/KgvSKWk.jpg > > What this is is a publicly-accessible webpage... If that's a screenshot of something that we'd be able to access directly, then why not just post a link to the actual thing? More likely I'm thinking it's NOT publicly accessible, which is why it's been censored. ChrisA From neil.suffield at gmail.com Sat May 25 06:13:41 2013 From: neil.suffield at gmail.com (neil.suffield at gmail.com) Date: Sat, 25 May 2013 03:13:41 -0700 (PDT) Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: If you are talking about accessing a web page, rather than an image, then you want to do what is known as screen scraping. One of the best tools for this is called BeautifulSoup. http://www.crummy.com/software/BeautifulSoup/ From neil.suffield at gmail.com Sat May 25 06:15:22 2013 From: neil.suffield at gmail.com (neil.suffield at gmail.com) Date: Sat, 25 May 2013 03:15:22 -0700 (PDT) Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: <29195880-e277-486e-a5dc-77110b093d4e@googlegroups.com> If you are talking about accessing a web page, rather than an image, then what you want to do is known as 'screen scraping'. One of the best tools for this is called BeautifulSoup. http://www.crummy.com/software/BeautifulSoup/ From logan.c.graham at gmail.com Sat May 25 20:48:50 2013 From: logan.c.graham at gmail.com (logan.c.graham at gmail.com) Date: Sat, 25 May 2013 17:48:50 -0700 (PDT) Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: <3126e4b6-d685-4ac8-8532-134f46a904ba@googlegroups.com> Sorry to be unclear -- it's a screenshot of the webpage, which is publicly accessible, but it contains sensitive information. A bad combination, admittedly, and something that'll be soon fixed. From john_ladasky at sbcglobal.net Sat May 25 21:33:25 2013 From: john_ladasky at sbcglobal.net (John Ladasky) Date: Sat, 25 May 2013 18:33:25 -0700 (PDT) Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: On Friday, May 24, 2013 4:36:35 PM UTC-7, Carlos Nepomuceno wrote: > #to create the tables list > tables=[[re.findall('(.*?)',r,re.S) for r in re.findall('(.*?)',t,re.S)] for t in re.findall('(.*?)
',page,re.S)] > > > Pretty simple. Two nested list comprehensions, with regex pattern matching? Logan did say he was a "total beginner." :^) From logan.c.graham at gmail.com Mon May 27 20:58:00 2013 From: logan.c.graham at gmail.com (logan.c.graham at gmail.com) Date: Mon, 27 May 2013 17:58:00 -0700 (PDT) Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: On Saturday, May 25, 2013 6:33:25 PM UTC-7, John Ladasky wrote: > On Friday, May 24, 2013 4:36:35 PM UTC-7, Carlos Nepomuceno wrote: > > > #to create the tables list > > > tables=[[re.findall('(.*?)',r,re.S) for r in re.findall('(.*?)',t,re.S)] for t in re.findall('(.*?)
',page,re.S)] > > > > > > > > > Pretty simple. > > > > Two nested list comprehensions, with regex pattern matching? > > > > Logan did say he was a "total beginner." :^) Oh goodness, yes, I have no clue. From carlosnepomuceno at outlook.com Mon May 27 21:21:07 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 04:21:07 +0300 Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: , , , Message-ID: ---------------------------------------- > Date: Mon, 27 May 2013 17:58:00 -0700 > Subject: Re: Total Beginner - Extracting Data from a Database Online (Screenshot) > From: logan.c.graham at gmail.com > To: python-list at python.org [...] > > Oh goodness, yes, I have no clue. For example: # to retrieve the contents of all column '# fb' (11th column from the image you sent) c11 = [tables[0][r][10] for r in range(len(tables[0]))] #????? ----------------??????????????? ------------- #????? this is the content???????????? this is the quantity #????? of the 11th cell??????????????? of rows in table[0] #????? of row 'r' From pconnell at gmail.com Tue May 28 02:40:07 2013 From: pconnell at gmail.com (Phil Connell) Date: Tue, 28 May 2013 07:40:07 +0100 Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: Message-ID: On 28 May 2013 02:21, "Carlos Nepomuceno" wrote: > > ---------------------------------------- > > Date: Mon, 27 May 2013 17:58:00 -0700 > > Subject: Re: Total Beginner - Extracting Data from a Database Online (Screenshot) > > From: logan.c.graham at gmail.com > > To: python-list at python.org > [...] > > > > Oh goodness, yes, I have no clue. > > For example: > > # to retrieve the contents of all column '# fb' (11th column from the image you sent) > > c11 = [tables[0][r][10] for r in range(len(tables[0]))] Or rather: c11 = [row[10] for row in tables[0]] In most cases, range(len(x)) is a sign that you're doing it wrong :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Tue May 28 02:52:10 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 06:52:10 -0000 Subject: Total Beginner - Extracting Data from a Database Online (Screenshot) In-Reply-To: References: , , , , , Message-ID: ________________________________ > Date: Tue, 28 May 2013 07:40:07 +0100 > Subject: RE: Total Beginner - Extracting Data from a Database Online > (Screenshot) > From: pconnell at gmail.com [...] > > > > c11 = [tables[0][r][10] for r in range(len(tables[0]))] > > Or rather: > > c11 = [row[10] for row in tables[0]] > > In most cases, range(len(x)) is a sign that you're doing it wrong :) Indeed! Much better that way! \o From sara.lochtie at gmail.com Fri May 24 15:58:50 2013 From: sara.lochtie at gmail.com (Sara Lochtie) Date: Fri, 24 May 2013 12:58:50 -0700 (PDT) Subject: Plotting with PyQwt Message-ID: <95315d83-2460-48b2-818f-412a911216cc@googlegroups.com> Hi, I'm trying to plot temperature vs. time using Qwt. I have temp data that i get in real time and I want to plot against time. So I thought I would have to use QwtPlot and QwtPlotCurve, so I did this first: self.tempGraph = Qwt.QwtPlot(self) curve = QwtPlotCurve() curve.setData(x, temp) x is the time and temp is the data I have collected. I know this isn't current, but I just don't know how to go about it. I haven't used Qwt before. If anyone has some tips or advice for me that would be great! From sara.lochtie at gmail.com Fri May 24 16:06:42 2013 From: sara.lochtie at gmail.com (Sara Lochtie) Date: Fri, 24 May 2013 13:06:42 -0700 (PDT) Subject: Plotting with PyQwt In-Reply-To: <95315d83-2460-48b2-818f-412a911216cc@googlegroups.com> References: <95315d83-2460-48b2-818f-412a911216cc@googlegroups.com> Message-ID: isn't correct* On Friday, May 24, 2013 12:58:50 PM UTC-7, Sara Lochtie wrote: > Hi, > > > > I'm trying to plot temperature vs. time using Qwt. I have temp data that i get in real time and I want to plot against time. So I thought I would have to use QwtPlot and QwtPlotCurve, so I did this first: > > > > self.tempGraph = Qwt.QwtPlot(self) > > > > curve = QwtPlotCurve() > > curve.setData(x, temp) > > > > > > > > x is the time and temp is the data I have collected. > > > > I know this isn't current, but I just don't know how to go about it. I haven't used Qwt before. > > > > If anyone has some tips or advice for me that would be great! From jcasale at activenetwerx.com Fri May 24 17:00:01 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Fri, 24 May 2013 21:00:01 +0000 Subject: Ldap module and base64 oncoding Message-ID: I have some data I am working with that is not being interpreted as a string requiring base64 encoding when sent to the ldif module for output. The base64 string parsed is ZGV0XDMzMTB3YmJccGc= and the raw string is det\3310wbb\pg. I'll admit my understanding of the handling requirements of non ascii data in 2.7 is weak and as such I am failing at adjusting the regex that deduces is the string contains characters requiring base64 encoding when being output. Any insight, or nudges in the right direction would be appreciated! Thanks, jlc From carlosnepomuceno at outlook.com Fri May 24 21:10:18 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 04:10:18 +0300 Subject: Ldap module and base64 oncoding In-Reply-To: References: Message-ID: Can you give an example of the code you have? ---------------------------------------- > From: jcasale at activenetwerx.com > To: python-list at python.org > Subject: Ldap module and base64 oncoding > Date: Fri, 24 May 2013 21:00:01 +0000 > > I have some data I am working with that is not being interpreted as a string requiring > base64 encoding when sent to the ldif module for output. > > The base64 string parsed is ZGV0XDMzMTB3YmJccGc= and the raw string is det\3310wbb\pg. > I'll admit my understanding of the handling requirements of non ascii data in 2.7 is weak > and as such I am failing at adjusting the regex that deduces is the string contains characters > requiring base64 encoding when being output. > > Any insight, or nudges in the right direction would be appreciated! > Thanks, > jlc > -- > http://mail.python.org/mailman/listinfo/python-list From jcasale at activenetwerx.com Sat May 25 11:42:14 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Sat, 25 May 2013 15:42:14 +0000 Subject: Ldap module and base64 oncoding In-Reply-To: References: , Message-ID: <3cf3b3670ad24416b17fd62c8dbc2e66@exch.activenetwerx.com> > Can you give an example of the code you have? I actually just overrode the regex used by the method in the LDIFWriter class to be far more broad about what it interprets as a safe string. I really need to properly handle reading, manipulating and writing non ascii data to solve this... Shame there is no ldap module (with the ldifwriter) in Python 3. jlc From michael at stroeder.com Sun May 26 11:16:28 2013 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Sun, 26 May 2013 17:16:28 +0200 Subject: Ldap module and base64 oncoding In-Reply-To: References: , Message-ID: Joseph L. Casale wrote: >> Can you give an example of the code you have? > > I actually just overrode the regex used by the method in the LDIFWriter class to be far more broad > about what it interprets as a safe string. Are you sure that you fully understood RFC 2849 before doing this? Which version of python-ldap are you using? > I really need to properly handle reading, manipulating and > writing non ascii data to solve this... Module ldif in python-ldap does that for you based on RFC 2849. Without seeing your code using it I cannot tell what's wrong. > Shame there is no ldap module (with the ldifwriter) in Python 3. 1. The module ldif is stand-alone. So you could easily make it available for Python 3. 2. "Shame" is the wrong term here. Personally I currently have no requirement to use Python 3 and I'm quite busy with other things. So contributors are welcome. But they should be willing to do some serious work giving continous support - not only a half-baken patch. Ciao, Michael. From michael at stroeder.com Sun May 26 11:07:35 2013 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Sun, 26 May 2013 17:07:35 +0200 Subject: Ldap module and base64 oncoding In-Reply-To: References: Message-ID: Joseph L. Casale wrote: > I have some data I am working with that is not being interpreted as a string requiring > base64 encoding when sent to the ldif module for output. > > The base64 string parsed is ZGV0XDMzMTB3YmJccGc= and the raw string is det\3310wbb\pg. > I'll admit my understanding of the handling requirements of non ascii data in 2.7 is weak > and as such I am failing at adjusting the regex that deduces is the string contains characters > requiring base64 encoding when being output. I'm not sure what exactly you're asking for. Especially "is not being interpreted as a string requiring base64 encoding" is written without giving the right context. So I'm just guessing that this might be the usual misunderstandings with use of base64 in LDIF. Read more about when LDIF requires base64-encoding here: http://tools.ietf.org/html/rfc2849 To me everything looks right: Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 'ZGV0XDMzMTB3YmJccGc='.decode('base64').decode('utf-8') u'det\\3310wbb\\pg' >>> What do you think is a problem? Ciao, Michael. From jcasale at activenetwerx.com Sun May 26 12:19:57 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Sun, 26 May 2013 16:19:57 +0000 Subject: Ldap module and base64 oncoding In-Reply-To: References: , Message-ID: <7fe2db0cd580425eaf01ded48e9f7b55@exch.activenetwerx.com> > I'm not sure what exactly you're asking for. > Especially "is not being interpreted as a string requiring base64 encoding" is > written without giving the right context. > > So I'm just guessing that this might be the usual misunderstandings with use > of base64 in LDIF. Read more about when LDIF requires base64-encoding here: > > http://tools.ietf.org/html/rfc2849 > > To me everything looks right: > > Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> 'ZGV0XDMzMTB3YmJccGc='.decode('base64').decode('utf-8') > u'det\\3310wbb\\pg' > >>> > > What do you think is a problem? Michael, Thanks for the reply. The issues I am sure are in my code, I read the ldif source file and up with a values such as 'det\3310wbb\pg' after the base64 encoded entries are decoded. The problem I am having is when I add this to an add/mod entry list and write it back out. As it does not get re-encoded to base64 the ldif file ends up seeing a text entry with a ^] character which if I re-read it with the parser it causes the handle method to break midway through the entry dict and so the last half re-appears disjoint without a dn. Like I said, I am pretty sure its my poor misunderstanding of decoding and encoding. I am using the build from http://www.lfd.uci.edu/~gohlke/pythonlibs/ on a windows 2008 r2 server. I have re-implemented handle to create a cidict holding all the dn/entry's that are parsed as I then perform some processing such as manipulating attribute values in the entry dict. I am pretty sure I am breaking things here. The data I am reading is coming from utf-16-le encoded files and has Unicode characters as the source directory is globally available, being written to in just about every country. Is there a process for manipulating/adding data to the entry dict before I write it out that I should adhere to? For example, if I am adding a new attribute to be composed of part of another parsed attr for use in a modlist: {'customAttr': ['foo.{}.bar'.format(entry['uid'])]} By looking at the value from above, 'det\3310wbb\pg', I gather the entry dict was parsed into byte strings. I should have decoded this, where as some of the data is Unicode and as such I should have encoded it? I really appreciate the time. Grazie per tutto, jlc From michael at stroeder.com Sun May 26 15:48:38 2013 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Sun, 26 May 2013 21:48:38 +0200 Subject: Ldap module and base64 oncoding In-Reply-To: References: , Message-ID: Joseph L. Casale wrote: >> I'm not sure what exactly you're asking for. >> Especially "is not being interpreted as a string requiring base64 encoding" is >> written without giving the right context. >> >> So I'm just guessing that this might be the usual misunderstandings with use >> of base64 in LDIF. Read more about when LDIF requires base64-encoding here: >> >> http://tools.ietf.org/html/rfc2849 >> >> To me everything looks right: >> >> Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> 'ZGV0XDMzMTB3YmJccGc='.decode('base64').decode('utf-8') >> u'det\\3310wbb\\pg' >>>>> >> >> What do you think is a problem? > > Thanks for the reply. The issues I am sure are in my code, I read the ldif source file and up > with a values such as 'det\3310wbb\pg' after the base64 encoded entries are decoded. > > The problem I am having is when I add this to an add/mod entry list and write it back out. > As it does not get re-encoded to base64 the ldif file ends up seeing a text entry with a ^] > character which if I re-read it with the parser it causes the handle method to break midway > through the entry dict and so the last half re-appears disjoint without a dn. > > Like I said, I am pretty sure its my poor misunderstanding of decoding and encoding. > I am using the build from http://www.lfd.uci.edu/~gohlke/pythonlibs/ on a windows > 2008 r2 server. > > I have re-implemented handle to create a cidict holding all the dn/entry's that are parsed as > I then perform some processing such as manipulating attribute values in the entry dict. I > am pretty sure I am breaking things here. The data I am reading is coming from utf-16-le > encoded files and has Unicode characters as the source directory is globally available, being > written to in just about every country. Processing LDIF is one thing, doing LDAP operations another. LDIF itself is meant to be ASCII-clean. But each attribute value can carry any byte sequence (e.g. attribute 'jpegPhoto'). There's no further processing by module LDIF - it simply returns byte sequences. The access protocol LDAPv3 mandates UTF-8 encoding for Unicode strings on the wire if attribute syntax is DirectoryString, IA5String (mainly ASCII) or similar. So if you're LDIF input returns UTF-16 encoded attribute values for e.g. attribute 'cn' or 'o' or another attribute not being of OctetString or Binary syntax something's wrong with the producer of the LDIF data. > Is there a process for manipulating/adding data to the entry dict before I write it out that I > should adhere to? For example, if I am adding a new attribute to be composed of part of > another parsed attr for use in a modlist: > > {'customAttr': ['foo.{}.bar'.format(entry['uid'])]} > > By looking at the value from above, 'det\3310wbb\pg', I gather the entry dict was parsed > into byte strings. I should have decoded this, where as some of the data is Unicode and > as such I should have encoded it? I wonder what the string really is. At least the base64-encoding you provided before decodes as UTF-8 but I'm not sure whether it's the right sequence of Unicode code points you're expecting. >>> 'ZGV0XDMzMTB3YmJccGc='.decode('base64').decode('utf-8') u'det\\3310wbb\\pg' I still can't figure out what you're really doing though. I'd recommend to strip down your operations to a very simple test code snippet illustrating the issue and post that here. Ciao, Michael. From jcasale at activenetwerx.com Mon May 27 01:15:01 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 27 May 2013 05:15:01 +0000 Subject: Ldap module and base64 oncoding In-Reply-To: References: , , Message-ID: <78b0eafba002480aba586b54db86abfc@exch.activenetwerx.com> Hi Michael, > Processing LDIF is one thing, doing LDAP operations another. > > LDIF itself is meant to be ASCII-clean. But each attribute value can carry any > byte sequence (e.g. attribute 'jpegPhoto'). There's no further processing by > module LDIF - it simply returns byte sequences. > > The access protocol LDAPv3 mandates UTF-8 encoding for Unicode strings on the > wire if attribute syntax is DirectoryString, IA5String (mainly ASCII) or similar. > > So if you're LDIF input returns UTF-16 encoded attribute values for e.g. > attribute 'cn' or 'o' or another attribute not being of OctetString or Binary > syntax something's wrong with the producer of the LDIF data. That could be, I am using ms's ldifde.exe to dump a domino and AD directory for comparative processing. The problem is I don't have much control on the data in the directory and I do know that DN's have non ascii characters unique to the > I wonder what the string really is. At least the base64-encoding you provided > before decodes as UTF-8 but I'm not sure whether it's the right sequence of > Unicode code points you're expecting. > > >>> 'ZGV0XDMzMTB3YmJccGc='.decode('base64').decode('utf-8') > u'det\\3310wbb\\pg' > > I still can't figure out what you're really doing though. I'd recommend to > strip down your operations to a very simple test code snippet illustrating the > issue and post that here. So I have removed all my likely broken attempts at working with this data and will soon have some simple code but at this point I may have an indication of what is awry with my data. After parsing the data for a user I am simply taking a value from the ldif file and writing it back out to another which fails, the value parsed is: officestreetaddress:: T3R0by1NZcOfbWVyLVN0cmHDn2UgMQ== File "C:\Python27\lib\site-packages\ldif.py", line 202, in unparse self._unparseChangeRecord(record) File "C:\Python27\lib\site-packages\ldif.py", line 181, in _unparseChangeRecord self._unparseAttrTypeandValue(mod_type,mod_val) File "C:\Python27\lib\site-packages\ldif.py", line 142, in _unparseAttrTypeandValue self._unfoldLDIFLine(':: '.join([attr_type,base64.encodestring(attr_value).replace('\n','')])) File "C:\Python27\lib\base64.py", line 315, in encodestring pieces.append(binascii.b2a_base64(chunk)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xdf' in position 7: ordinal not in range(128) > c:\python27\lib\base64.py(315)encodestring() -> pieces.append(binascii.b2a_base64(chunk)) (Pdb) l 310 def encodestring(s): 311 """Encode a string into multiple lines of base-64 data.""" 312 pieces = [] 313 for i in range(0, len(s), MAXBINSIZE): 314 chunk = s[i : i + MAXBINSIZE] 315 -> pieces.append(binascii.b2a_base64(chunk)) 316 return "".join(pieces) 317 318 319 def decodestring(s): 320 """Decode a string.""" (Pdb) args s = Otto-Me?mer-Stra?e 1 So moving up a frame or two and looking at the entry dict, I see a modlist entry of: ('streetAddress', [u'Otto-Me\xdfmer-Stra\xdfe 1']) which is correct: In [2]: 'T3R0by1NZcOfbWVyLVN0cmHDn2UgMQ=='.decode('base64').decode('utf-8') Out[2]: u'Otto-Me\xdfmer-Stra\xdfe 1' Looking at the stack trace, I think I see the issue: (Pdb) import base64 (Pdb) base64.encodestring(u'Otto-Me\xdfmer-Stra\xdfe 1'.encode('utf-8')).replace('\n','') 'T3R0by1NZcOfbWVyLVN0cmHDn2UgMQ==' I now have the exact the value I started with. Ensuring where I ever handle the original values that I return utf-8 decoded objects for use in a modlist to later write and Sub classing LDIFWriter and overriding _unparseAttrTypeandValue to do the encoding has eliminated all the errors. What remains finally is ldifde.exe's output of what looks like U+00BF, or an inverted question mark for some values, otherwise this issue looks solved. Thanks for everything, jlc From dieter at handshake.de Mon May 27 02:04:16 2013 From: dieter at handshake.de (dieter) Date: Mon, 27 May 2013 08:04:16 +0200 Subject: Ldap module and base64 oncoding References: <78b0eafba002480aba586b54db86abfc@exch.activenetwerx.com> Message-ID: <87ppwdot5r.fsf@handshake.de> "Joseph L. Casale" writes: > ... > After parsing the data for a user I am simply taking a value from the ldif file and writing > it back out to another which fails, the value parsed is: > > officestreetaddress:: T3R0by1NZcOfbWVyLVN0cmHDn2UgMQ== > > > File "C:\Python27\lib\site-packages\ldif.py", line 202, in unparse > self._unparseChangeRecord(record) > File "C:\Python27\lib\site-packages\ldif.py", line 181, in _unparseChangeRecord > self._unparseAttrTypeandValue(mod_type,mod_val) > File "C:\Python27\lib\site-packages\ldif.py", line 142, in _unparseAttrTypeandValue > self._unfoldLDIFLine(':: '.join([attr_type,base64.encodestring(attr_value).replace('\n','')])) > File "C:\Python27\lib\base64.py", line 315, in encodestring > pieces.append(binascii.b2a_base64(chunk)) > UnicodeEncodeError: 'ascii' codec can't encode character u'\xdf' in position 7: ordinal not in range(128) > >> c:\python27\lib\base64.py(315)encodestring() > -> pieces.append(binascii.b2a_base64(chunk)) This looks like a coding bug: "chunk" seems to be a unicode string; "b2a_base64" expects an encoded string ("str/bytes"); as a consequence, Python tries to convert the unicode to "str" by encoding with its "default enconding" ("ascii" by default) - and fails. You could try to find out, why "chunk" is unicode (rather than "str"). That will probably bring you to the real problem. From michael at stroeder.com Mon May 27 03:56:43 2013 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Mon, 27 May 2013 09:56:43 +0200 Subject: Ldap module and base64 oncoding In-Reply-To: References: , , Message-ID: Joseph L. Casale wrote: > After parsing the data for a user I am simply taking a value from the ldif file and writing > it back out to another which fails, the value parsed is: > > officestreetaddress:: T3R0by1NZcOfbWVyLVN0cmHDn2UgMQ== > > > File "C:\Python27\lib\site-packages\ldif.py", line 202, in unparse > self._unparseChangeRecord(record) > File "C:\Python27\lib\site-packages\ldif.py", line 181, in _unparseChangeRecord > self._unparseAttrTypeandValue(mod_type,mod_val) > File "C:\Python27\lib\site-packages\ldif.py", line 142, in _unparseAttrTypeandValue > self._unfoldLDIFLine(':: '.join([attr_type,base64.encodestring(attr_value).replace('\n','')])) > File "C:\Python27\lib\base64.py", line 315, in encodestring > pieces.append(binascii.b2a_base64(chunk)) > UnicodeEncodeError: 'ascii' codec can't encode character u'\xdf' in position 7: ordinal not in range(128) Note that all modules in python-ldap up to 2.4.10 including module 'ldif' expect raw byte strings to be passed as arguments. It seems to me you're passing a Unicode object in the entry dictionary which will fail in case an attribute value contains NON-ASCII chars. python-ldap expects raw strings since it's not schema-aware and therefore does not have any knowledge about the LDAP syntax used for a particular attribute type. So automagically convert Unicode strings will likely fail in many cases. => The calling application has to deal with it. > I now have the exact the value I started with. Ensuring where I ever handle the original > values that I return utf-8 decoded objects for use in a modlist to later write and Sub > classing LDIFWriter and overriding _unparseAttrTypeandValue to do the encoding has > eliminated all the errors. Don't muck with overriding _unparseAttrTypeandValue(). Simply pass the properly encoded data into ldif module. Ciao, Michael. From jcasale at activenetwerx.com Mon May 27 20:12:58 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Tue, 28 May 2013 00:12:58 +0000 Subject: Ldap module and base64 oncoding In-Reply-To: References: , , , Message-ID: > Note that all modules in python-ldap up to 2.4.10 including module 'ldif' > expect raw byte strings to be passed as arguments. It seems to me you're > passing a Unicode object in the entry dictionary which will fail in case an > attribute value contains NON-ASCII chars. Yup, I was. > python-ldap expects raw strings since it's not schema-aware and therefore does > not have any knowledge about the LDAP syntax used for a particular attribute > type. So automagically convert Unicode strings will likely fail in many cases. > => The calling application has to deal with it. I see, that recco went a long a way in cleaning up my code actually and making the handling of decoding and encoding more consistent. > Don't muck with overriding _unparseAttrTypeandValue(). Simply pass the > properly encoded data into ldif module. I had some time today, so I attempted to open the ldif files in binary mode to simply work with the raw byte strings but the moment the first entry was parsed, parse() stumbled on a character in the first entries dict and passed a dn of None for the last half? If the option to avoid worrying about decoding and encoding could work, I would be happy to process the whole lot in byte strings. Any idea what may cause this? Thanks a lot Michael, jlc From michael at stroeder.com Tue May 28 03:45:32 2013 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Tue, 28 May 2013 09:45:32 +0200 Subject: Ldap module and base64 oncoding In-Reply-To: References: , , , Message-ID: Joseph L. Casale wrote: > I had some time today, so I attempted to open the ldif files in binary mode to simply > work with the raw byte strings but the moment the first entry was parsed, parse() > stumbled on a character in the first entries dict and passed a dn of None for the last half? Without seeing the LDIF data and your code I can't tell what's going on. > If the option to avoid worrying about decoding and encoding could work, I would be > happy to process the whole lot in byte strings. Any idea what may cause this? I would not claim that module 'ldif' has really awesome docs. But did you follow the example with LDIFParser in the docs? http://www.python-ldap.org/doc/html/ldif.html#example It illustrates that for LDIF stream processing one basically derives a class from ldif.LDIFParser overriding method handle(). The most basic test would be something like this: [..] def handle(self,dn,entry): print '***dn',repr(dn) pprint.pprint(entry) And then carefully look at the output. Ciao, Michael. From thomasmurphymusic at gmail.com Fri May 24 18:53:52 2013 From: thomasmurphymusic at gmail.com (Thomas Murphy) Date: Fri, 24 May 2013 18:53:52 -0400 Subject: Prepending string "@" to usernames Message-ID: Hi beloved list, I'm having a dumb and SO doesn't seem to have this one answered. I was sent a long list of instagram usernames to tag for a nightlife announcement in this format(not real names(i hope)) cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834 I'd like to turn this raw text into a list and prepend the @ symbol to the front of each one, so they're good to go for pasting without me having to manually add the @ to each one. Here's where I got to: raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834" address_library = [raw_address.split()] print address_library for address in address_library: final_address = "@" + str(address) print final_address However my output is: [['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']] @['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834'] I know I'm iterating wrong. May I ask how? -- Sincerely, Thomas Murphy Code Ninja 646.957.6115 From drsalists at gmail.com Fri May 24 19:00:22 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Fri, 24 May 2013 16:00:22 -0700 Subject: Prepending string "@" to usernames In-Reply-To: References: Message-ID: On Fri, May 24, 2013 at 3:53 PM, Thomas Murphy wrote: > Hi beloved list, > > I'm having a dumb and SO doesn't seem to have this one answered. I was > sent a long list of instagram usernames to tag for a nightlife > announcement in this format(not real names(i hope)) > > cookielover93 > TheGermanHatesSaurkraut > WhatsThatBoy932834 > > I'd like to turn this raw text into a list and prepend the @ symbol to > the front of each one, so they're good to go for pasting without me > having to manually add the @ to each one. > > Here's where I got to: > > > raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834" > address_library = [raw_address.split()] > print address_library > > for address in address_library: > final_address = "@" + str(address) > print final_address > > > However my output is: > > [['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']] > @['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834'] > > > I know I'm iterating wrong. May I ask how? > > -- > Sincerely, > Thomas Murphy > Code Ninja > 646.957.6115 > -- > http://mail.python.org/mailman/listinfo/python-list > Maybe this is what you're looking for? raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834" address_library = raw_address.split() print address_library final_address = [] for address in address_library: final_address.append("@" + str(address)) print final_address -------------- next part -------------- An HTML attachment was scrubbed... URL: From thomasmurphymusic at gmail.com Fri May 24 19:04:31 2013 From: thomasmurphymusic at gmail.com (Thomas Murphy) Date: Fri, 24 May 2013 19:04:31 -0400 Subject: Prepending string "@" to usernames In-Reply-To: References: Message-ID: > Maybe this is what you're looking for? > > raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834" > address_library = raw_address.split() > print address_library > > final_address = [] > for address in address_library: > final_address.append("@" + str(address)) > print final_address > Exactly it. Thank you so much Dan. Perfect! -- Sincerely, Thomas Murphy Code Ninja 646.957.6115 From breamoreboy at yahoo.co.uk Fri May 24 19:16:34 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 25 May 2013 00:16:34 +0100 Subject: Prepending string "@" to usernames In-Reply-To: References: Message-ID: On 25/05/2013 00:04, Thomas Murphy wrote: >> Maybe this is what you're looking for? >> >> raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834" >> address_library = raw_address.split() >> print address_library >> >> final_address = [] >> for address in address_library: >> final_address.append("@" + str(address)) >> print final_address >> > Exactly it. Thank you so much Dan. Perfect! > > > > -- > Sincerely, > Thomas Murphy > Code Ninja > 646.957.6115 > You can safely remove the call to str as address is already a string. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From python.list at tim.thechases.com Fri May 24 19:19:40 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 24 May 2013 18:19:40 -0500 Subject: Prepending string "@" to usernames In-Reply-To: References: Message-ID: <20130524181940.620d18ad@bigbox.christie.dr> On 2013-05-24 19:04, Thomas Murphy wrote: >> raw_address = "cookielover93 TheGermanHatesSaurkraut >> WhatsThatBoy932834" address_library = raw_address.split() >> print address_library >> >> final_address = [] >> for address in address_library: >> final_address.append("@" + str(address)) >> print final_address >> > Exactly it. Thank you so much Dan. Perfect! Which can be tidily written as a list comprehension: final_address = ['@' + address for address in raw_address.split()] -tkc From thomasmurphymusic at gmail.com Fri May 24 20:02:23 2013 From: thomasmurphymusic at gmail.com (Thomas Murphy) Date: Fri, 24 May 2013 20:02:23 -0400 Subject: Prepending string "@" to usernames In-Reply-To: <20130524181940.620d18ad@bigbox.christie.dr> References: <20130524181940.620d18ad@bigbox.christie.dr> Message-ID: > Which can be tidily written as a list comprehension: > > final_address = ['@' + address for address in raw_address.split()] > > -tkc > > Ah! Thanks Tim?that tidiness is something I'm trying to head towards in my own code. I'm trying to transition from the need to visually write every little step out and write in these more powerful chunks. -- Sincerely, Thomas Murphy Code Ninja 646.957.6115 From bahamutzero8825 at gmail.com Fri May 24 19:14:27 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 24 May 2013 18:14:27 -0500 Subject: Prepending string "@" to usernames In-Reply-To: References: Message-ID: <519FF453.9080207@gmail.com> On 2013.05.24 17:53, Thomas Murphy wrote: > I know I'm iterating wrong. May I ask how? .split() already returns a list, so instead of iterating over the list and getting a single username, you iterate over the list and get a single list. -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From orgnut at yahoo.com Fri May 24 23:24:00 2013 From: orgnut at yahoo.com (Larry Hudson) Date: Fri, 24 May 2013 20:24:00 -0700 Subject: Prepending string "@" to usernames In-Reply-To: References: Message-ID: On 05/24/2013 03:53 PM, Thomas Murphy wrote: > Here's where I got to: > > > raw_address = "cookielover93 TheGermanHatesSaurkraut WhatsThatBoy932834" > address_library = [raw_address.split()] > print address_library > > for address in address_library: > final_address = "@" + str(address) > print final_address > > > However my output is: > > [['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834']] > @['cookielover93', 'TheGermanHatesSaurkraut', 'WhatsThatBoy932834'] > > > I know I'm iterating wrong. May I ask how? > > -- > Sincerely, > Thomas Murphy > Code Ninja > 646.957.6115 > No, you're not iterating wrong, but you do have two errors: 1: split() returns a list. You are putting this list (as a single element) inside a list. Drop the square brackets. Make it: address_library = raw_address.split() 2: In your for loop, you want the print _inside_ the loop not outside. IOW, indent the print line. The way you have it written it will only print the _last_ string. -=- Larry -=- From drsalists at gmail.com Fri May 24 19:05:51 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Fri, 24 May 2013 16:05:51 -0700 Subject: Survey of Python-in-browser technologies Message-ID: I'm putting together a spreadsheet about Python-in-the-browser technologies for my local python user group. I've been hitting the mailing lists for the various implementations already, but I thought I should run it by people here at least once. Anyway, here it is: http://stromberg.dnsalias.org/~strombrg/pybrowser/python-browser.html If you see something inaccurate or omitted, please let me know. -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Fri May 24 19:43:02 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 02:43:02 +0300 Subject: Survey of Python-in-browser technologies In-Reply-To: References: Message-ID: Thanks Dan! All of that is relevant but I'm specially concerned about security issues and think another column for that purpose would improve your database, although I'm not sure if there's data available or how it would be presented. ________________________________ > Date: Fri, 24 May 2013 16:05:51 -0700 > Subject: Survey of Python-in-browser technologies > From: drsalists at gmail.com > To: python-list at python.org > > > I'm putting together a spreadsheet about Python-in-the-browser > technologies for my local python user group. > > I've been hitting the mailing lists for the various implementations > already, but I thought I should run it by people here at least once. > > Anyway, here it is: > > http://stromberg.dnsalias.org/~strombrg/pybrowser/python-browser.html > > If you see something inaccurate or omitted, please let me know. > > > -- http://mail.python.org/mailman/listinfo/python-list From drsalists at gmail.com Fri May 24 20:11:18 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Fri, 24 May 2013 17:11:18 -0700 Subject: Survey of Python-in-browser technologies In-Reply-To: References: Message-ID: Security is an important topic... but I'm not sure how I could gather info about the security of these implementations. Still, it's an idea worth at least keeping in the back of my mind. On Fri, May 24, 2013 at 4:43 PM, Carlos Nepomuceno < carlosnepomuceno at outlook.com> wrote: > Thanks Dan! All of that is relevant but I'm specially concerned about > security issues and think another column for that purpose would improve > your database, although I'm not sure if there's data available or how it > would be presented. > > ________________________________ > > Date: Fri, 24 May 2013 16:05:51 -0700 > > Subject: Survey of Python-in-browser technologies > > From: drsalists at gmail.com > > To: python-list at python.org > > > > > > I'm putting together a spreadsheet about Python-in-the-browser > > technologies for my local python user group. > > > > I've been hitting the mailing lists for the various implementations > > already, but I thought I should run it by people here at least once. > > > > Anyway, here it is: > > > > http://stromberg.dnsalias.org/~strombrg/pybrowser/python-browser.html > > > > If you see something inaccurate or omitted, please let me know. > > > > > > -- http://mail.python.org/mailman/listinfo/python-list > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Fri May 24 20:38:10 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 03:38:10 +0300 Subject: Survey of Python-in-browser technologies In-Reply-To: References: , , Message-ID: ________________________________ > Date: Fri, 24 May 2013 17:11:18 -0700 > Subject: Re: Survey of Python-in-browser technologies > From: drsalists at gmail.com > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > > > Security is an important topic... but I'm not sure how I could gather > info about the security of these implementations. Still, it's an idea > worth at least keeping in the back of my mind. Security specialists opinions and facts about known vulnerabilities would be a start, although it may be time consuming to gather all data. Just a thought! ;) From davea at davea.name Fri May 24 21:29:21 2013 From: davea at davea.name (Dave Angel) Date: Sat, 25 May 2013 01:29:21 -0000 Subject: Survey of Python-in-browser technologies In-Reply-To: References: , , Message-ID: <51A012AF.6040003@davea.name> On 05/24/2013 08:38 PM, Carlos Nepomuceno wrote: > ________________________________ >> Date: Fri, 24 May 2013 17:11:18 -0700 >> Subject: Re: Survey of Python-in-browser technologies >> From: drsalists at gmail.com >> To: carlosnepomuceno at outlook.com >> CC: python-list at python.org >> >> >> Security is an important topic... but I'm not sure how I could gather >> info about the security of these implementations. Still, it's an idea >> worth at least keeping in the back of my mind. > > Security specialists opinions and facts about known vulnerabilities would be a start, although it may be time consuming to gather all data. > > Just a thought! ;) > Thank you for putting your response after the part you're quoting. And for trimming out the irrelevant parts. -- DaveA From yoursurrogategod at gmail.com Fri May 24 20:59:22 2013 From: yoursurrogategod at gmail.com (Yves S. Garret) Date: Fri, 24 May 2013 20:59:22 -0400 Subject: Why won't Python 2/3 compile when I have valgrind installed? Message-ID: This isn't a huge issue, but I'm wondering. I'm running Mac OS X, I tried to configure with --with-valgrind and this is the error that I got: configure: error: Valgrind support requested but headers not available Now, I have valgrind installed, so it should work, yes? If someone has any extra info on this, I'd appreciate it. -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Fri May 24 21:07:33 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 04:07:33 +0300 Subject: Why won't Python 2/3 compile when I have valgrind installed? In-Reply-To: References: Message-ID: You probably need 'valgrind-devel' package installed. ________________________________ > Date: Fri, 24 May 2013 20:59:22 -0400 > Subject: Why won't Python 2/3 compile when I have valgrind installed? > From: yoursurrogategod at gmail.com > To: python-list at python.org > > This isn't a huge issue, but I'm wondering. I'm running > Mac OS X, I tried to configure with --with-valgrind and > this is the error that I got: > > configure: error: Valgrind support requested but headers not available > > Now, I have valgrind installed, so it should work, yes? > If someone has any extra info on this, I'd appreciate it. > > -- http://mail.python.org/mailman/listinfo/python-list From carlosnepomuceno at outlook.com Fri May 24 22:08:36 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 05:08:36 +0300 Subject: Is python.org powered by CPython? Message-ID: Is python.org powered by CPython? Is it using WSGI? What Python version is been used? I already checked it's using Apache. Is it using mod_wsgi? Thanks in advance. From nad at acm.org Fri May 24 22:58:12 2013 From: nad at acm.org (Ned Deily) Date: Fri, 24 May 2013 19:58:12 -0700 Subject: Is python.org powered by CPython? References: Message-ID: In article , Carlos Nepomuceno wrote: > Is python.org powered by CPython? Like many websites, the python.org domain consists of a number of subdomains with several different webservers on various hosts. AFAIK, the main www.python.org server is currently all (or mainly) static content served by Apache, although the static files are, in part, generated by a home-grown content management system powered by CPython. I'm not sure about the details of the other servers. For example, both bugs.python.org and hg.python.org have CPython applications behind them: Roundup and Mercurial. FTR, the whole python.org web domain is being redesigned so the details of what is under the covers will be changing. Jesse Noller posted about the redesign process some months ago: http://jessenoller.com/blog/2012/11/28/the-great-python-org-redesign -- Ned Deily, nad at acm.org From roy at panix.com Fri May 24 23:12:17 2013 From: roy at panix.com (Roy Smith) Date: Fri, 24 May 2013 23:12:17 -0400 Subject: Preventing nose from finding setup.py Message-ID: We've got a package (with an empty __init__.py), which contains a setup.py file. When I run nosetests, the test discovery code finds setup.py, thinks it's a test, and tries to run it (with predictably poor results). Is there some way to mark this file as not a test? If it was a method in a file, I would decorate it with @nottest, but that doesn't work here. From roy at panix.com Fri May 24 23:16:57 2013 From: roy at panix.com (Roy Smith) Date: Fri, 24 May 2013 23:16:57 -0400 Subject: Preventing nose from finding setup.py References: Message-ID: In article , Roy Smith wrote: > We've got a package (with an empty __init__.py), which contains a > setup.py file. When I run nosetests, the test discovery code finds > setup.py, thinks it's a test, and tries to run it (with predictably poor > results). Ugh, I described that wrong. Setup is also a package (with it's own empty __init__.py file). But, the same problem applies. Nosetests discovers it, tries to run it, and barfs with: TypeError: Attribute setup of is not a python function. Only functions or callables may be used as fixtures. From rama29065 at gmail.com Fri May 24 23:19:06 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Fri, 24 May 2013 20:19:06 -0700 (PDT) Subject: Python Magazine Message-ID: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Planning to start a python online chronicle.What you want to see in it. :) From dreamingforward at gmail.com Fri May 24 23:38:28 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Fri, 24 May 2013 20:38:28 -0700 Subject: Python Magazine In-Reply-To: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: I always liked the daily Python-URL from Dr. Dobbs. Summaries of salient discussions on python-dev, ideas, list. interviews with devs on philosophies. quote of the week --m On 5/24/13, DRJ Reddy wrote: > Planning to start a python online chronicle.What you want to see in it. :) > -- > http://mail.python.org/mailman/listinfo/python-list > -- MarkJ Tacoma, Washington From carlosnepomuceno at outlook.com Fri May 24 23:43:56 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 06:43:56 +0300 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com>, Message-ID: In-depth articles about Python! Like security analisys of modules, packages, frameworks, everything Python related. Performance benchmarks. How a Python technology/solution compares to other competitor technologies. Python competitions/contests/challenges! ---------------------------------------- > Date: Fri, 24 May 2013 20:38:28 -0700 > Subject: Re: Python Magazine > From: dreamingforward at gmail.com > To: rama29065 at gmail.com > CC: python-list at python.org > > I always liked the daily Python-URL from Dr. Dobbs. > > Summaries of salient discussions on python-dev, ideas, list. > > interviews with devs on philosophies. > > quote of the week > > --m > > On 5/24/13, DRJ Reddy wrote: >> Planning to start a python online chronicle.What you want to see in it. :) >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > > -- > MarkJ > Tacoma, Washington > -- > http://mail.python.org/mailman/listinfo/python-list From rama29065 at gmail.com Sat May 25 00:10:02 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Fri, 24 May 2013 21:10:02 -0700 (PDT) Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com>,> Message-ID: On Saturday, May 25, 2013 9:13:56 AM UTC+5:30, Carlos Nepomuceno wrote: > In-depth articles about Python! Like security analisys of modules, packages, frameworks, everything Python related. > > Performance benchmarks. How a Python technology/solution compares to other competitor technologies. > > Python competitions/contests/challenges! Thankyou for your suggestions. :) From carlosnepomuceno at outlook.com Sat May 25 00:22:51 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 07:22:51 +0300 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com>, >, , , Message-ID: ---------------------------------------- > Date: Fri, 24 May 2013 21:10:02 -0700 > Subject: Re: Python Magazine > From: rama29065 at gmail.com > To: python-list at python.org > > On Saturday, May 25, 2013 9:13:56 AM UTC+5:30, Carlos Nepomuceno wrote: >> In-depth articles about Python! Like security analisys of modules, packages, frameworks, everything Python related. >> >> Performance benchmarks. How a Python technology/solution compares to other competitor technologies. >> >> Python competitions/contests/challenges! > > Thankyou for your suggestions. :) > -- > http://mail.python.org/mailman/listinfo/python-list Also, comparison of Python flavors (CPython, PyPy, Cython, Stackles, etc.) -- How do they differ? What's the benefits and hindrances? From rosuav at gmail.com Sat May 25 00:25:52 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 14:25:52 +1000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 2:22 PM, Carlos Nepomuceno wrote: > Also, comparison of Python flavors (CPython, PyPy, Cython, Stackles, etc.) -- How do they differ? What's the benefits and hindrances? Good point. Could go even more general than that: Just highlight some lesser-known Python and show it off. ChrisA From roy at panix.com Sat May 25 11:24:03 2013 From: roy at panix.com (Roy Smith) Date: Sat, 25 May 2013 11:24:03 -0400 Subject: Python Magazine References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: In article , Chris Angelico wrote: > > Also, comparison of Python flavors (CPython, PyPy, Cython, Stackles, etc.) Stackles? That sounds like breakfast cereal. "New all-natural stackles, with 12 essential vitamins, plus fiber!" From rosuav at gmail.com Sat May 25 11:30:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 01:30:56 +1000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 1:24 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> > Also, comparison of Python flavors (CPython, PyPy, Cython, Stackles, etc.) > > Stackles? That sounds like breakfast cereal. > > "New all-natural stackles, with 12 essential vitamins, plus fiber!" Heh!! But watch the citations please, I didn't say that - my email was quoting Carlos. ChrisA From carlosnepomuceno at outlook.com Sat May 25 13:28:08 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 20:28:08 +0300 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com>, , , , , , Message-ID: ---------------------------------------- > From: roy at panix.com > Subject: Re: Python Magazine > Date: Sat, 25 May 2013 11:24:03 -0400 > To: python-list at python.org > > In article , > Chris Angelico wrote: > >>> Also, comparison of Python flavors (CPython, PyPy, Cython, Stackles, etc.) > > Stackles? That sounds like breakfast cereal. > > "New all-natural stackles, with 12 essential vitamins, plus fiber!" > -- > http://mail.python.org/mailman/listinfo/python-list lol pardon my typo! I'm really considering that cereal! lol From roy at panix.com Fri May 24 23:35:32 2013 From: roy at panix.com (Roy Smith) Date: Fri, 24 May 2013 23:35:32 -0400 Subject: Python Magazine References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: In article <27969350-4dd8-4afa-881a-b4a2364b3cf1 at googlegroups.com>, DRJ Reddy wrote: > Planning to start a python online chronicle.What you want to see in it. :) Issue 1: "Whitespace as syntax: mistake or magic?" "Python 3 vs. IPv6: who will win the race for early adoption?" "Did Python 3 break unicode? The true story" "Tuplemania: 100 things you can do with immutable lists" From rama29065 at gmail.com Fri May 24 23:38:50 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Fri, 24 May 2013 20:38:50 -0700 (PDT) Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: <456f63a0-a515-493a-9565-5f3d79cd5ec2@googlegroups.com> > Issue 1: > "Whitespace as syntax: mistake or magic?" Thanks Roy :) From rosuav at gmail.com Fri May 24 23:48:40 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 13:48:40 +1000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 1:35 PM, Roy Smith wrote: > "Python 3 vs. IPv6: who will win the race for early adoption?" I think Py3 is winning that one so far. But really, both need to get moving. Neither of my ISPs does IPv6 :( Seconding the recommendation for QOTW, that's good fun. ChrisA From rama29065 at gmail.com Sat May 25 00:11:11 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Fri, 24 May 2013 21:11:11 -0700 (PDT) Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: <09c52944-9e80-4ae1-8490-c6451c95b17d@googlegroups.com> This is what i love with python community faster responses. :) From zoom at yahoo.com Sat May 25 02:38:23 2013 From: zoom at yahoo.com (zoom) Date: Sat, 25 May 2013 08:38:23 +0200 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: But why would anyone want to use IPv6? On 05/25/2013 05:48 AM, Chris Angelico wrote: > On Sat, May 25, 2013 at 1:35 PM, Roy Smith wrote: >> "Python 3 vs. IPv6: who will win the race for early adoption?" > > I think Py3 is winning that one so far. But really, both need to get > moving. Neither of my ISPs does IPv6 :( > > Seconding the recommendation for QOTW, that's good fun. > > ChrisA From rosuav at gmail.com Sat May 25 02:41:58 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 16:41:58 +1000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: On Sat, May 25, 2013 at 4:38 PM, zoom wrote: > But why would anyone want to use IPv6? I hope you're not serious :) ChrisA From steve+comp.lang.python at pearwood.info Sat May 25 10:29:00 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 May 2013 14:29:00 GMT Subject: Python Magazine References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sat, 25 May 2013 16:41:58 +1000, Chris Angelico wrote: > On Sat, May 25, 2013 at 4:38 PM, zoom wrote: >> But why would anyone want to use IPv6? > > I hope you're not serious :) He's planning to drop off the Internet once the IP address run out. -- Steven From roy at panix.com Sat May 25 11:30:19 2013 From: roy at panix.com (Roy Smith) Date: Sat, 25 May 2013 11:30:19 -0400 Subject: Python Magazine References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <51a0caac$0$30002$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > On Sat, 25 May 2013 16:41:58 +1000, Chris Angelico wrote: > > > On Sat, May 25, 2013 at 4:38 PM, zoom wrote: > >> But why would anyone want to use IPv6? > > > > I hope you're not serious :) > > He's planning to drop off the Internet once the IP address run out. We already have run out. People have gotten so used to being behind NAT gateways they don't even understand how evil it is. From my phone, I can call any other phone anywhere in the world. But I can't talk directly to the file server in my neighbor's house across the street? From rosuav at gmail.com Sat May 25 11:46:44 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 25 May 2013 15:46:44 -0000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, May 26, 2013 at 1:30 AM, Roy Smith wrote: > In article <51a0caac$0$30002$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> On Sat, 25 May 2013 16:41:58 +1000, Chris Angelico wrote: >> >> > On Sat, May 25, 2013 at 4:38 PM, zoom wrote: >> >> But why would anyone want to use IPv6? >> > >> > I hope you're not serious :) >> >> He's planning to drop off the Internet once the IP address run out. > > We already have run out. People have gotten so used to being behind NAT > gateways they don't even understand how evil it is. From my phone, I > can call any other phone anywhere in the world. But I can't talk > directly to the file server in my neighbor's house across the street? Of course, the shift from IPv4 and NAT to IPv6 and direct addressing does shift the firewalling burden somewhat. Instead of "I'll forward whatever ports I choose", it becomes "alright let's think about this whole firewall thang". I'm not 100% confident that home-grade routers will have proper firewall facilities in them; I suspect that moving home users onto IPv6 may result in a spate of attacks. On the flip side, it becomes less useful to simply port-scan across huge IP blocks - bulk-attacking an IPv4 /24 block is easy, but spamming even a single IPv6 /64 is practically impossible. ChrisA From john_ladasky at sbcglobal.net Sat May 25 21:28:58 2013 From: john_ladasky at sbcglobal.net (John Ladasky) Date: Sat, 25 May 2013 18:28:58 -0700 (PDT) Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> On Saturday, May 25, 2013 8:30:19 AM UTC-7, Roy Smith wrote: > From my phone, I > can call any other phone anywhere in the world. But I can't talk > directly to the file server in my neighbor's house across the street? Hmmm... I've been an advocate of IPv6, but... now you've got me thinking of what Iran's new cadre of hackers might do with it! :^) From roy at panix.com Sat May 25 21:54:43 2013 From: roy at panix.com (Roy Smith) Date: Sat, 25 May 2013 21:54:43 -0400 Subject: Python Magazine References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> Message-ID: In article <7cd17be8-d455-4db8-b8d0-ccc757db5cff at googlegroups.com>, John Ladasky wrote: > On Saturday, May 25, 2013 8:30:19 AM UTC-7, Roy Smith wrote: > > From my phone, I > > can call any other phone anywhere in the world. But I can't talk > > directly to the file server in my neighbor's house across the street? > > Hmmm... I've been an advocate of IPv6, but... now you've got me thinking of > what Iran's new cadre of hackers might do with it! :^) You (like many people) are confusing universal addressability with universal connectivity. The converse of that is people confusing NAT with security. Of course not every IPv6 endpoint will be able to talk to every other IPv6 endpoint, even if the both have globally unique addresses. But, the access controls will be implemented in firewalls with appropriately coded security policies. Not as an accident of being behind a NAT box. From rosuav at gmail.com Sat May 25 21:58:09 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 11:58:09 +1000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 11:54 AM, Roy Smith wrote: > In article <7cd17be8-d455-4db8-b8d0-ccc757db5cff at googlegroups.com>, > John Ladasky wrote: > >> On Saturday, May 25, 2013 8:30:19 AM UTC-7, Roy Smith wrote: >> > From my phone, I >> > can call any other phone anywhere in the world. But I can't talk >> > directly to the file server in my neighbor's house across the street? >> >> Hmmm... I've been an advocate of IPv6, but... now you've got me thinking of >> what Iran's new cadre of hackers might do with it! :^) > > You (like many people) are confusing universal addressability with > universal connectivity. The converse of that is people confusing NAT > with security. > > Of course not every IPv6 endpoint will be able to talk to every other > IPv6 endpoint, even if the both have globally unique addresses. But, > the access controls will be implemented in firewalls with appropriately > coded security policies. Not as an accident of being behind a NAT box. To be more specific: The control of who can talk to whom is in the hands of the admins of the two endpoints and the nodes in between, rather than being arbitrarily in the hands of the technology. So I would be able to talk to the file server across the street, but only IF its admin lets me. ChrisA From carlosnepomuceno at outlook.com Sat May 25 22:14:50 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 02:14:50 -0000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com>, , , , , <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com>, , <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com>, , Message-ID: I don't think IPv6 will change anything about NAPT usage. In fact, I guess, it will probably will make NAPT usage even more important and needed. From breamoreboy at yahoo.co.uk Sat May 25 23:20:27 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 04:20:27 +0100 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> Message-ID: On 26/05/2013 02:58, Chris Angelico wrote: > On Sun, May 26, 2013 at 11:54 AM, Roy Smith wrote: >> In article <7cd17be8-d455-4db8-b8d0-ccc757db5cff at googlegroups.com>, >> John Ladasky wrote: >> >>> On Saturday, May 25, 2013 8:30:19 AM UTC-7, Roy Smith wrote: >>>> From my phone, I >>>> can call any other phone anywhere in the world. But I can't talk >>>> directly to the file server in my neighbor's house across the street? >>> >>> Hmmm... I've been an advocate of IPv6, but... now you've got me thinking of >>> what Iran's new cadre of hackers might do with it! :^) >> >> You (like many people) are confusing universal addressability with >> universal connectivity. The converse of that is people confusing NAT >> with security. >> >> Of course not every IPv6 endpoint will be able to talk to every other >> IPv6 endpoint, even if the both have globally unique addresses. But, >> the access controls will be implemented in firewalls with appropriately >> coded security policies. Not as an accident of being behind a NAT box. > > To be more specific: The control of who can talk to whom is in the > hands of the admins of the two endpoints and the nodes in between, > rather than being arbitrarily in the hands of the technology. So I > would be able to talk to the file server across the street, but only > IF its admin lets me. > > ChrisA > By such means as leaving the top level admin password set to the factory default? :) -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From john_ladasky at sbcglobal.net Sat May 25 23:04:28 2013 From: john_ladasky at sbcglobal.net (John Ladasky) Date: Sat, 25 May 2013 20:04:28 -0700 (PDT) Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> Message-ID: <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> A perfectly fair point, Roy. It's just when you started suggesting connecting to your neighbor's file server -- well, that's not something that many people would ordinarily do. So, my mind leaped to the possibility of uninvited connections. Related question: would denial-of-service attacks be more pernicious without a NAT? From roy at panix.com Sat May 25 23:24:39 2013 From: roy at panix.com (Roy Smith) Date: Sat, 25 May 2013 23:24:39 -0400 Subject: Python Magazine References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> Message-ID: In article <8f19e20c-4f77-43dc-a732-4169e482d2b2 at googlegroups.com>, John Ladasky wrote: > A perfectly fair point, Roy. It's just when you started suggesting > connecting to your neighbor's file server -- well, that's not something that > many people would ordinarily do. So, my mind leaped to the possibility of > uninvited connections. > > Related question: would denial-of-service attacks be more pernicious without > a NAT? Not really. If I know the external IP address of your NAT box, I can throw as much traffic at it as your internet connection will deliver. Assuming you have sufficient bandwidth, eventually I'll melt down your router. This is equally true with NAT or without it. From rosuav at gmail.com Sat May 25 23:45:29 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 13:45:29 +1000 Subject: Python Magazine In-Reply-To: <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 1:04 PM, John Ladasky wrote: > A perfectly fair point, Roy. It's just when you started suggesting connecting to your neighbor's file server -- well, that's not something that many people would ordinarily do. So, my mind leaped to the possibility of uninvited connections. > > Related question: would denial-of-service attacks be more pernicious without a NAT? Not sure what you mean. If we assume that network topology doesn't change, then what we have is a single uplink (say, an ADSL connection, given that most home users don't have luxuries) going to a router (let's be generous here and say that's a Linux box with two NICs, and you have a smart admin in charge of it), behind which is a set of switches and computers making up a LAN of peers. On IPv4, the LAN would operate on one of the RFC 1918 address blocks - say, 192.168.0.x - and all external communication would be through one single IP address - 203.0.113.47 will do for the purposes of discussion. As far as other hosts on the internet are concerned, that entire network is one single host, with address 203.0.113.47. It's unaware of the three computers 192.168.0.4, .0.87, and .0.92; they merge into one. This means they share the 65536 ports, they share entries on blacklists, etc, etc. With IPv6, that ADSL connection would come with a /64 block - say, 2001:db8:142:857::/64. Within that block, each computer would be assigned a single address - perhaps 2001:db8:142:857::4, 2001:db8:142:857::87, and 2001:db8:142:857::92, or perhaps they'd be assigned them by their MAC addresses eg 2001:db8:142:857:200:5eff:fe00:531a, which can be done automatically. Now all your computers (including the router) are individually addressable; they can be identified separately, or treated as a group (the /64 representing the whole group). Their ports, blacklist entries, etc, are all unique. This means you can run three servers on port 80, etc. The question now is: What sort of DOS attack are you fearing? If it's a simple matter of saturating the connection, it makes absolutely no difference. As Roy said, that's just a question of overloading. If I command more bandwidth than you do, I can saturate you. Easy. (Very easy if I have a botnet, for instance.) Harder to judge are the amplifying attacks; a half-open-connection attack, for instance, attacks a TCP server's RAM allocation. It's possible that some attacks will be easier or harder with NAT than without, but you'd have to evaluate a specific attack technique. ChrisA From carlosnepomuceno at outlook.com Sun May 26 00:01:35 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 07:01:35 +0300 Subject: Python Magazine In-Reply-To: <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com>, , , , , <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com>, , <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com>, , <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> Message-ID: ---------------------------------------- > Date: Sat, 25 May 2013 20:04:28 -0700 > Subject: Re: Python Magazine > From: john_ladasky at sbcglobal.net > To: python-list at python.org > > A perfectly fair point, Roy. It's just when you started suggesting connecting to your neighbor's file server -- well, that's not something that many people would ordinarily do. So, my mind leaped to the possibility of uninvited connections. > > Related question: would denial-of-service attacks be more pernicious without a NAT? > -- > http://mail.python.org/mailman/listinfo/python-list I don't think so. IP blocking still a very common mitigation approach to DDoS, but it may cause denial of service to legitimate clients who share the same blocked public IP address used by the malicious clients. So, NAPT will still benefit DDoS attackers, at least temporarily (until the IP is unblocked). From rosuav at gmail.com Sun May 26 00:31:57 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 14:31:57 +1000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 2:01 PM, Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Sat, 25 May 2013 20:04:28 -0700 >> Subject: Re: Python Magazine >> From: john_ladasky at sbcglobal.net >> To: python-list at python.org >> >> A perfectly fair point, Roy. It's just when you started suggesting connecting to your neighbor's file server -- well, that's not something that many people would ordinarily do. So, my mind leaped to the possibility of uninvited connections. >> >> Related question: would denial-of-service attacks be more pernicious without a NAT? >> -- >> http://mail.python.org/mailman/listinfo/python-list > > I don't think so. > > IP blocking still a very common mitigation approach to DDoS, but it may cause denial of service to legitimate clients who share the same blocked public IP address used by the malicious clients. So, NAPT will still benefit DDoS attackers, at least temporarily (until the IP is unblocked). I expect that IP blocks will be upgraded to /64 block blocks, if that starts being a problem. But it often won't, and specific IP address blocks will still be the norm. ChrisA From carlosnepomuceno at outlook.com Sun May 26 01:00:08 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 08:00:08 +0300 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com>, , , , , <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com>, , <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com>, , <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com>, , Message-ID: ---------------------------------------- > Date: Sun, 26 May 2013 14:31:57 +1000 > Subject: Re: Python Magazine > From: rosuav at gmail.com > To: python-list at python.org [...] > I expect that IP blocks will be upgraded to /64 block blocks, if that > starts being a problem. But it often won't, and specific IP address > blocks will still be the norm. > > ChrisA Blocking a whole network (/65) is totally undesirable and may even become illegal. Currently it may not only happen at the target of the DDoS attack, but be spread all over the internet where block lists are enforced. I don't expect that to happen and if it happens I'm surely in favor of protection against this type of 'solution' because it will block not only malicious clients but potentially many other legitimate clients. From rosuav at gmail.com Sun May 26 01:17:11 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 15:17:11 +1000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 3:00 PM, Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Sun, 26 May 2013 14:31:57 +1000 >> Subject: Re: Python Magazine >> From: rosuav at gmail.com >> To: python-list at python.org > [...] >> I expect that IP blocks will be upgraded to /64 block blocks, if that >> starts being a problem. But it often won't, and specific IP address >> blocks will still be the norm. >> >> ChrisA > > > Blocking a whole network (/65) is totally undesirable and may even become illegal. Blocking a /64 is exactly the same as blocking a /32 with NAT behind it. And how could it be illegal? I provide service to those I choose to provide to. > Currently it may not only happen at the target of the DDoS attack, but be spread all over the internet where block lists are enforced. > > I don't expect that to happen and if it happens I'm surely in favor of protection against this type of 'solution' because it will block not only malicious clients but potentially many other legitimate clients. Banning a wide netblock is of course going to lock out legit clients. But IP rotation means that can happen anyway. You block a single IPv4 address that right now represents an abusive user; that user disconnects and reconnects, gets a new IP, and someone else gets the other one. Can happen all too easily. That's why IP-banning is at best a temporary solution anyway. ChrisA From carlosnepomuceno at outlook.com Sun May 26 01:44:10 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 05:44:10 -0000 Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com>, , , , , <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com>, , <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com>, , <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com>, , , , Message-ID: ---------------------------------------- > Date: Sun, 26 May 2013 15:17:11 +1000 > Subject: Re: Python Magazine > From: rosuav at gmail.com [...] >> Blocking a whole network (/65) is totally undesirable and may even become illegal. > > Blocking a /64 is exactly the same as blocking a /32 with NAT behind > it. And how could it be illegal? I provide service to those I choose > to provide to. I'm not a lawyer, so what I say about the IP blocking legal matter is just an opinion. I'm not responsible for any damages it may cause to anyone!!! lol It just looks like those damn software disclaimers, doesn't it? ;) Depending on the jurisdiction things maybe very different. I've been told that in California it is really illegal to block IP addresses without a court order. Any Californians available to confirm that? "The sender of information over the Internet is the "owner" of both the information and the IP address attached to the information. The practice of IP address blocking "records" the IP address and "destroys" the information in the message "without the intent or permission of the owner of the information," and "usurp[s] the normal operation of the . . computer network." Software designed to record IP addresses and use them to block messages is thus a "computer contaminant" according to the above definition. " Source: http://im-from-missouri.blogspot.com.br/2007/05/ip-address-blocking-is-illegal-in.html Also check California Penal Code Section 502, "Unauthorized Access To Computers, Computer Systems and Computer Data": http://www.leginfo.ca.gov/cgi-bin/displaycode?section=pen&group=00001-01000&file=484-502.9 The problem is serious and there are many cases, such as: http://www.theregister.co.uk/2011/10/13/dutch_isp_accuses_spamhaus/ >> Currently it may not only happen at the target of the DDoS attack, but be spread all over the internet where block lists are enforced. >> >> I don't expect that to happen and if it happens I'm surely in favor of protection against this type of 'solution' because it will block not only malicious clients but potentially many other legitimate clients. > > Banning a wide netblock is of course going to lock out legit clients. > But IP rotation means that can happen anyway. You block a single IPv4 > address that right now represents an abusive user; that user > disconnects and reconnects, gets a new IP, and someone else gets the > other one. Can happen all too easily. That's why IP-banning is at best > a temporary solution anyway. IP blocking isn't a perfect solution as you have confirmed. That's why using it is so problematic. It may hurt legitimate clients and other unrelated ones, while the "abusive user" as you said gets out unharmed. From rama29065 at gmail.com Fri May 31 07:11:06 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Fri, 31 May 2013 04:11:06 -0700 (PDT) Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> <8f19e20c-4f77-43dc-a732-4169e482d2b2@googlegroups.com> Message-ID: <29be50bf-b77c-4450-9d7c-026bfaf7fe89@googlegroups.com> Hello all, Was busy with work. Finally finished the job of registering the domain name. Will be live soon. The url is http://pythonmagazine.org. Hope we will be live soon. Regards, DRJ. From steve+comp.lang.python at pearwood.info Sat May 25 23:50:31 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 May 2013 03:50:31 GMT Subject: Python Magazine References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> Message-ID: <51a18686$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sat, 25 May 2013 21:54:43 -0400, Roy Smith wrote: > Of course not every IPv6 endpoint will be able to talk to every other > IPv6 endpoint, even if the both have globally unique addresses. But, > the access controls will be implemented in firewalls with appropriately > coded security policies. Or, more likely, *not* implemented in firewalls with *inappropriately* coded security policies. -- Steven From steve+comp.lang.python at pearwood.info Sun May 26 00:03:18 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 May 2013 04:03:18 GMT Subject: Python Magazine References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> Message-ID: <51a18986$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sun, 26 May 2013 11:58:09 +1000, Chris Angelico wrote: > On Sun, May 26, 2013 at 11:54 AM, Roy Smith wrote: >> Of course not every IPv6 endpoint will be able to talk to every other >> IPv6 endpoint, even if the both have globally unique addresses. But, >> the access controls will be implemented in firewalls with appropriately >> coded security policies. Not as an accident of being behind a NAT box. > > To be more specific: The control of who can talk to whom is in the hands > of the admins of the two endpoints and the nodes in between, rather than > being arbitrarily in the hands of the technology. So I would be able to > talk to the file server across the street, but only IF its admin lets > me. Or when (not if) you find a vulnerability in the particular firewall. Make no mistake: the most secure entry point is the one that isn't there. -- Steven From rosuav at gmail.com Sun May 26 00:37:21 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 14:37:21 +1000 Subject: Python Magazine In-Reply-To: <51a18986$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <51a0caac$0$30002$c3e8da3$5496439d@news.astraweb.com> <7cd17be8-d455-4db8-b8d0-ccc757db5cff@googlegroups.com> <51a18986$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, May 26, 2013 at 2:03 PM, Steven D'Aprano wrote: > On Sun, 26 May 2013 11:58:09 +1000, Chris Angelico wrote: > >> On Sun, May 26, 2013 at 11:54 AM, Roy Smith wrote: > >>> Of course not every IPv6 endpoint will be able to talk to every other >>> IPv6 endpoint, even if the both have globally unique addresses. But, >>> the access controls will be implemented in firewalls with appropriately >>> coded security policies. Not as an accident of being behind a NAT box. >> >> To be more specific: The control of who can talk to whom is in the hands >> of the admins of the two endpoints and the nodes in between, rather than >> being arbitrarily in the hands of the technology. So I would be able to >> talk to the file server across the street, but only IF its admin lets >> me. > > Or when (not if) you find a vulnerability in the particular firewall. > Make no mistake: the most secure entry point is the one that isn't there. Packets have to get somewhere. If they come into this computer, it has to deliberately forward them to that computer or they won't get there. Same thing. All it takes is # ip6tables -p FORWARD DROP and you have a "secure unless I specifically permit it" router. Obviously an attacker can target the router itself (which is exactly the same as current situation), but can't attack anything beyond it without an explicit forwarding rule (which is also exactly the same). ChrisA From rama29065 at gmail.com Fri May 24 23:41:55 2013 From: rama29065 at gmail.com (DRJ Reddy) Date: Fri, 24 May 2013 20:41:55 -0700 (PDT) Subject: Python Magazine In-Reply-To: References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: <1db38d4f-55a3-41c5-9f72-194458f90f9f@googlegroups.com> On Saturday, May 25, 2013 9:08:28 AM UTC+5:30, zipher wrote: > I always liked the daily Python-URL from Dr. Dobbs. > quote of the week Thanks zipher we have already planned to continue Python-URL and quote of the week. We will plan for other things that you have mentioned. Thanks again :) From michael.poeltl at univie.ac.at Sat May 25 01:29:37 2013 From: michael.poeltl at univie.ac.at (Michael Poeltl) Date: Sat, 25 May 2013 07:29:37 +0200 Subject: Python Magazine In-Reply-To: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> Message-ID: <20130525052937.GE1537@horus.cms.at> * DRJ Reddy [2013-05-25 05:26]: > Planning to start a python online chronicle.What you want to see in it. :) - idiomatic python (common mistakes; do it 'pythonically') - interviews - challenge of the week (how would you solve that?) - python for kids - scientific python news - new python-books - > -- > http://mail.python.org/mailman/listinfo/python-list -- Michael Poeltl Computational Materials Physics at University Wien, Sensengasse 8/12, A-1090 Wien, AUSTRIA http://cmp.univie.ac.at/ http://homepage.univie.ac.at/michael.poeltl/ using elinks-0.12, mutt-1.5.21, and vim-7.3, with python-3.2.3, on slackware-13.37 :-) fon: +43-1-4277-51409 From danielrr2 at gmail.com Sat May 25 10:56:21 2013 From: danielrr2 at gmail.com (Daniel) Date: Sat, 25 May 2013 16:56:21 +0200 Subject: Python Magazine In-Reply-To: <20130525052937.GE1537@horus.cms.at> References: <27969350-4dd8-4afa-881a-b4a2364b3cf1@googlegroups.com> <20130525052937.GE1537@horus.cms.at> Message-ID: <217502BF-6086-4102-98A6-51C9030D4F8D@gmail.com> All of the above, plus: - Best Pythonic tools for GUI - notorious projects (in science, education, NGOs, etc) using python Please keep us informed, and best wishes Daniel El 25/05/2013, a las 07:29, Michael Poeltl escribi?: > * DRJ Reddy [2013-05-25 05:26]: >> Planning to start a python online chronicle.What you want to see in it. :) > - idiomatic python (common mistakes; do it 'pythonically') > - interviews > - challenge of the week (how would you solve that?) > - python for kids > - scientific python news > - new python-books > > - > > >> -- >> http://mail.python.org/mailman/listinfo/python-list > > -- > Michael Poeltl > Computational Materials Physics at University > Wien, Sensengasse 8/12, A-1090 Wien, AUSTRIA > http://cmp.univie.ac.at/ > http://homepage.univie.ac.at/michael.poeltl/ > using elinks-0.12, mutt-1.5.21, and vim-7.3, > with python-3.2.3, on slackware-13.37 :-) > fon: +43-1-4277-51409 > -- > http://mail.python.org/mailman/listinfo/python-list From carlosnepomuceno at outlook.com Fri May 24 23:59:52 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 06:59:52 +0300 Subject: Source code as text/plain Message-ID: I'd like to have the option to download the source code as text/plain from the docs.python.org pages. For example: when I'm a docs page, such as: http://docs.python.org/2/library/string.html and I click the source code link I'm taken to a Mercurial page: http://hg.python.org/cpython/file/2.7/Lib/string.py but over there there's no way to get a clean text/plain version of the code because the line numbers are included. A link to the text/plain version on that page would be nice! From clp2 at rebertia.com Sat May 25 00:06:05 2013 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 May 2013 21:06:05 -0700 Subject: Source code as text/plain In-Reply-To: References: Message-ID: On May 24, 2013 9:02 PM, "Carlos Nepomuceno" wrote: > > I'd like to have the option to download the source code as text/plain from the docs.python.org pages. > > For example: when I'm a docs page, such as: > > http://docs.python.org/2/library/string.html > > and I click the source code link I'm taken to a Mercurial page: > > http://hg.python.org/cpython/file/2.7/Lib/string.py > > but over there there's no way to get a clean text/plain version of the code because the line numbers are included. > > A link to the text/plain version on that page would be nice! It's already there. Click the "raw" link in the sidebar. In this case, at this moment, it sends you to http://hg.python.org/cpython/raw-file/f4981d8eb401/Lib/string.py -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Sat May 25 00:12:05 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 07:12:05 +0300 Subject: Source code as text/plain In-Reply-To: References: , Message-ID: Oh great! Thank you! ________________________________ > Date: Fri, 24 May 2013 21:06:05 -0700 > Subject: Re: Source code as text/plain > From: clp2 at rebertia.com > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > > > On May 24, 2013 9:02 PM, "Carlos Nepomuceno" > > > wrote: > > > > I'd like to have the option to download the source code as text/plain > from the docs.python.org pages. > > > > For example: when I'm a docs page, such as: > > > > http://docs.python.org/2/library/string.html > > > > and I click the source code link I'm taken to a Mercurial page: > > > > http://hg.python.org/cpython/file/2.7/Lib/string.py > > > > but over there there's no way to get a clean text/plain version of > the code because the line numbers are included. > > > > A link to the text/plain version on that page would be nice! > > It's already there. Click the "raw" link in the sidebar. In this case, > at this moment, it sends you to > http://hg.python.org/cpython/raw-file/f4981d8eb401/Lib/string.py From as at sci.fi Tue May 28 09:31:33 2013 From: as at sci.fi (Anssi Saari) Date: Tue, 28 May 2013 16:31:33 +0300 Subject: Source code as text/plain References: Message-ID: Carlos Nepomuceno writes: > I'd like to have the option to download the source code as text/plain from the docs.python.org pages. > > For example: when I'm a docs page, such as: > > http://docs.python.org/2/library/string.html > > and I click the source code link I'm taken to a Mercurial page: > http://hg.python.org/cpython/file/2.7/Lib/string.py > > but over there there's no way to get a clean text/plain version of the code because the line numbers are included. > > A link to the text/plain version on that page would be nice! The 'raw' link on the left spews the source as application/binary so most likely your browser will download it instead of showing it. Which is what you asked but if you wanted to view in browser sans the line numbers, there doesn't seem to be a way. From renegrothmann at gmail.com Sat May 25 05:52:08 2013 From: renegrothmann at gmail.com (Rene Grothmann) Date: Sat, 25 May 2013 02:52:08 -0700 (PDT) Subject: Py_Initialize and Py_Finalize and MatPlotlib Message-ID: <5c9913be-5a59-4a78-a85d-ac87e7c0dd15@googlegroups.com> This is a known problem, but I want to ask the experts for the best way to solve it for me. I have a project (Euler Math Toolbox), which runs Python as a script language. For this, a library module "python.dll" is loaded at run time, which is linked against "python27.lib". Then Py_Initialize is called. This all works well. But Euler can be restarted by the user with a new session and notebook. Then I want Python to clear all variables and imports. For this, I call Py_Finalize and unload "python.dll". When Python is needed, loading and initializing starts Python again. This works. But Python crashes at the first call, if MatPlotlib is imported in the previous session. It seems that Py_Finalize does not completely clear Python, nor does unloading my "python.dll". I tried unloading "python27.dll" (the Python DLL), but this does not help. Most likey, another DLL remains active, but corrupts during Py_Finalize. To solve this, it would suffice to clear all variables and imports. I could live with not calling Py_Finalize. But how? PS: You may wonder, why I do not directly link euler.exe to Python. The reason is that this prevents Euler form starting, if Python is not installed, even if it is never needed. Thanks for any answers! You duplicate your answer to renegrothmann at gmail, if you like. That would help me. From rnyk1991 at gmail.com Sat May 25 06:52:24 2013 From: rnyk1991 at gmail.com (Rakshith Nayak) Date: Sat, 25 May 2013 03:52:24 -0700 (PDT) Subject: Text-to-Sound or Vice Versa (Method NOT the source code) Message-ID: <02102fc9-9826-4f0f-be77-8809ea179226@googlegroups.com> Always wondered how sound is generated from text. Googling couldn't help. Devs having knowledge about this could provide, the information, Links, URLs or anything that could help. From gd.usenet at spamfence.net Sat May 25 08:30:18 2013 From: gd.usenet at spamfence.net (=?ISO-8859-1?Q?G=FCnther?= Dietrich) Date: Sat, 25 May 2013 14:30:18 +0200 Subject: Text-to-Sound or Vice Versa (Method NOT the source code) References: <02102fc9-9826-4f0f-be77-8809ea179226@googlegroups.com> Message-ID: Rakshith Nayak wrote: >Always wondered how sound is generated from text. Googling couldn't help. Devs >having knowledge about this could provide, the information, Links, URLs or >anything that could help. Perhaps try 'text to speech' instead of 'text to sound'? Best regards, G?nther From rustompmody at gmail.com Sat May 25 12:29:18 2013 From: rustompmody at gmail.com (rusi) Date: Sat, 25 May 2013 09:29:18 -0700 (PDT) Subject: Text-to-Sound or Vice Versa (Method NOT the source code) References: <02102fc9-9826-4f0f-be77-8809ea179226@googlegroups.com> Message-ID: <45040d72-c611-43c9-b23e-8d536ff72800@wg15g2000pbb.googlegroups.com> On May 25, 3:52?pm, Rakshith Nayak wrote: > Always wondered how sound is generated from text. Googling couldn't help. Devs having knowledge about this could provide, the information, Links, URLs or anything that could help. > > look for speech synthesis http://en.wikipedia.org/wiki/Speech_synthesis From invalid at invalid.invalid Tue May 28 11:10:03 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 15:10:03 +0000 (UTC) Subject: Text-to-Sound or Vice Versa (Method NOT the source code) References: <02102fc9-9826-4f0f-be77-8809ea179226@googlegroups.com> Message-ID: On 2013-05-25, Rakshith Nayak wrote: > Always wondered how sound is generated from text. Googling couldn't > help. Devs having knowledge about this could provide, the > information, Links, URLs or anything that could help. > > http://www.cstr.ed.ac.uk/projects/festival/ http://code.google.com/p/pyfestival/ http://machakux.appspot.com/blog/44003/making_speech_with_python -- Grant Edwards grant.b.edwards Yow! at BI-BI-BI-BI-BI-BI gmail.com From rustompmody at gmail.com Wed May 29 06:08:54 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 29 May 2013 03:08:54 -0700 (PDT) Subject: Text-to-Sound or Vice Versa (Method NOT the source code) References: <02102fc9-9826-4f0f-be77-8809ea179226@googlegroups.com> Message-ID: <3460cbe9-8270-4634-9b47-18a7dc580588@li6g2000pbb.googlegroups.com> On May 29, 4:30?am, Dennis Lee Bieber wrote: > On Tue, 28 May 2013 15:10:03 +0000 (UTC), Grant Edwards > declaimed the following in > gmane.comp.python.general: > > > On 2013-05-25, Rakshith Nayak wrote: > > > > Always wondered how sound is generated from text. Googling couldn't > > > help. Devs having knowledge about this could provide, the > > > information, Links, URLs or anything that could help. > > > > > > >http://www.cstr.ed.ac.uk/projects/festival/ > >http://code.google.com/p/pyfestival/ > >http://machakux.appspot.com/blog/44003/making_speech_with_python > > ? ? ? ? I suppose one could go for archaic and complex... > > ? ? ? ? Obtain a working Amiga computer, install whatever the last Python > version was available pre-built. Then write a server application which > would take text over the net, and feed it to the appropriate Amiga > libraries -- translator and narrator as I recall (one converted plain > text to phoneme codings, the other then converted phonemes to sound, and > could return values for "mouth shape" to sync animation) [history: the > Amiga had text to speech in the late 80s -- it even allowed for > adjusting some formant parameters so one could create pseudo accents]. If venerable history is wanted, there is (always?!) emacs: http://emacspeak.sourceforge.net/ This seems to go back to version 19 of emacs which is (c) mid- nineties From pythonasad at gmail.com Sat May 25 08:41:11 2013 From: pythonasad at gmail.com (Asad Hasan) Date: Sat, 25 May 2013 18:11:11 +0530 Subject: Learning Python Message-ID: Hi All , I have started leaning Python through web . Would like to know if I should follow any book so that basics become clear with examples also want to know like in perl i use to invoke perl -d to get a debugged output is there any option in python. THanks, -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sat May 25 10:35:47 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 May 2013 14:35:47 GMT Subject: Learning Python References: Message-ID: <51a0cc42$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sat, 25 May 2013 18:11:11 +0530, Asad Hasan wrote: > I have started leaning Python through web . Would like to know > if I should follow any book so that basics become clear with examples Reading books is a good thing to do. > also > want to know like in perl i use to invoke perl -d to get a debugged > output is there any option in python. Yes. Instead of calling your script like this: python myscript.py call it like this: python -m pdb myscript.py -- Steven From carlosnepomuceno at outlook.com Sat May 25 13:26:53 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sat, 25 May 2013 20:26:53 +0300 Subject: Learning Python In-Reply-To: References: Message-ID: ________________________________ > Date: Sat, 25 May 2013 18:11:11 +0530 > Subject: Learning Python > From: pythonasad at gmail.com > To: Python-list at python.org > > Hi All , > > I have started leaning Python through web . Would like to know > if I should follow any book so that basics become clear with examples > also want to know like in perl i use to invoke perl -d to get a > debugged output is there any option in python. > > THanks, > > -- http://mail.python.org/mailman/listinfo/python-list I have used those: "Dive into Python" (2004)[1] "Python Programming" (2012)[2] "Programming Python, 3rd Ed" (2006) [print] ? [1] http://www.diveintopython.net/ [1] http://en.wikibooks.org/wiki/Python_Programming From darin.hensley at gmail.com Sat May 25 23:09:08 2013 From: darin.hensley at gmail.com (darin.hensley at gmail.com) Date: Sat, 25 May 2013 20:09:08 -0700 (PDT) Subject: Automate Grabbing Email From Outlook On Shared Folder Message-ID: I am trying to automate Outlook mail client by retrieving a message with an attachment, using win32com. The message box is a shared folder that is not really underneath root folder inbox, so I had no success with inbox = mapi.GetDefaultFolder(6). However, I did have some success with: import win32com.client outlook = win32com.client.Dispatch("Outlook.Application") mapi = outlook.GetNamespace("MAPI").Folders print mapi[7] In which the output is "Mailbox - Foo Operations"--- the folder I want. However, if I tried: import win32com.client outlook = win32com.client.Dispatch("Outlook.Application") mapi = outlook.GetNamespace("MAPI").Folders messages = mapi[7].Items message = messages.getLast() body_content = message.body print body_content I get no success. So I am pretty much stuck. I googled many things but no success. I prefer to win32com since I don't have to use a login in the script. But if this is possible with another Python module with no login requirement that would be great also. From cdorm245 at gmail.com Sat May 25 23:55:42 2013 From: cdorm245 at gmail.com (cdorm245 at gmail.com) Date: Sat, 25 May 2013 20:55:42 -0700 (PDT) Subject: CrazyHTTPd - HTTP Daemon in Python Message-ID: This is a small little Project that I have started. Its a light little Web Server (HTTPd) coded in python. Requirements: Python 2.7 =< And Linux / BSD. I believe this could work in a CLI Emulator in windows too. Welcome to check out the website powered by CrazyHTTPd: http://web.crazycoder.me:14081/index.html Please Email me with any bugs, errors, and/or comments about my little project: cdorm245 at gmail.com From breamoreboy at yahoo.co.uk Sun May 26 00:06:50 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 05:06:50 +0100 Subject: CrazyHTTPd - HTTP Daemon in Python In-Reply-To: References: Message-ID: On 26/05/2013 04:55, cdorm245 at gmail.com wrote: > This is a small little Project that I have started. Its a light little Web Server (HTTPd) coded in python. Requirements: Python 2.7 =< And Linux / BSD. I believe this could work in a CLI Emulator in windows too. > Welcome to check out the website powered by CrazyHTTPd: http://web.crazycoder.me:14081/index.html > > Please Email me with any bugs, errors, and/or comments about my little project: cdorm245 at gmail.com > IMHO writing a project right now that is limited to Python 2 is about as much use as a chocolate teapot. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From carlosnepomuceno at outlook.com Sun May 26 00:21:46 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 07:21:46 +0300 Subject: CrazyHTTPd - HTTP Daemon in Python In-Reply-To: References: , Message-ID: Your code isn't threaded. I suggest you consider[1] and take that road! ;) Good luck! [1] http://bulk.fefe.de/scalable-networking.pdf ---------------------------------------- > To: python-list at python.org > From: breamoreboy at yahoo.co.uk > Subject: Re: CrazyHTTPd - HTTP Daemon in Python > Date: Sun, 26 May 2013 05:06:50 +0100 > > On 26/05/2013 04:55, cdorm245 at gmail.com wrote: >> This is a small little Project that I have started. Its a light little Web Server (HTTPd) coded in python. Requirements: Python 2.7 =< And Linux / BSD. I believe this could work in a CLI Emulator in windows too. >> Welcome to check out the website powered by CrazyHTTPd: http://web.crazycoder.me:14081/index.html >> >> Please Email me with any bugs, errors, and/or comments about my little project: cdorm245 at gmail.com >> > > IMHO writing a project right now that is limited to Python 2 is about as > much use as a chocolate teapot. > > -- > If you're using GoogleCrap? please read this > http://wiki.python.org/moin/GoogleGroupsPython. > > Mark Lawrence > > -- > http://mail.python.org/mailman/listinfo/python-list From cdorm245 at gmail.com Sun May 26 00:58:13 2013 From: cdorm245 at gmail.com (cdorm245 at gmail.com) Date: Sat, 25 May 2013 21:58:13 -0700 (PDT) Subject: CrazyHTTPd - HTTP Daemon in Python In-Reply-To: References: ,> Message-ID: <16df12e2-f8c4-4551-bbb0-702056274914@googlegroups.com> On Sunday, May 26, 2013 12:21:46 PM UTC+8, Carlos Nepomuceno wrote: > Your code isn't threaded. I suggest you consider[1] and take that road! ;) Good luck! > > [1] http://bulk.fefe.de/scalable-networking.pdf > > ---------------------------------------- > > To: python-list at python.org > > From: breamoreboy at yahoo.co.uk > > Subject: Re: CrazyHTTPd - HTTP Daemon in Python > > Date: Sun, 26 May 2013 05:06:50 +0100 > > > > On 26/05/2013 04:55, cdorm245 at gmail.com wrote: > >> This is a small little Project that I have started. Its a light little Web Server (HTTPd) coded in python. Requirements: Python 2.7 =< And Linux / BSD. I believe this could work in a CLI Emulator in windows too. > >> Welcome to check out the website powered by CrazyHTTPd: http://web.crazycoder.me:14081/index.html > >> > >> Please Email me with any bugs, errors, and/or comments about my little project: cdorm245 at gmail.com > >> > > > > IMHO writing a project right now that is limited to Python 2 is about as > > much use as a chocolate teapot. > > > > -- > > If you're using GoogleCrap? please read this > > http://wiki.python.org/moin/GoogleGroupsPython. > > > > Mark Lawrence > > > > -- > > http://mail.python.org/mailman/listinfo/python-list Thanks, I plan on doing this. Then CrazyHTTPd will support bigger file downloading and multiple network connections at once. Thanks Again! From fabiosantosart at gmail.com Sun May 26 05:21:19 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sun, 26 May 2013 10:21:19 +0100 Subject: CrazyHTTPd - HTTP Daemon in Python In-Reply-To: References: Message-ID: On 26 May 2013 05:18, "Mark Lawrence" wrote: > > On 26/05/2013 04:55, cdorm245 at gmail.com wrote: >> >> This is a small little Project that I have started. Its a light little Web Server (HTTPd) coded in python. Requirements: Python 2.7 =< And Linux / BSD. I believe this could work in a CLI Emulator in windows too. >> Welcome to check out the website powered by CrazyHTTPd: http://web.crazycoder.me:14081/index.html >> >> Please Email me with any bugs, errors, and/or comments about my little project: cdorm245 at gmail.com >> > > IMHO writing a project right now that is limited to Python 2 is about as much use as a chocolate teapot. > > -- > If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. > > Mark Lawrence > > -- > http://mail.python.org/mailman/listinfo/python-list Agreed. And you should work on WSGI support too. -------------- next part -------------- An HTML attachment was scrubbed... URL: From usenetmail at solar-empire.de Sun May 26 06:06:39 2013 From: usenetmail at solar-empire.de (Marc Christiansen) Date: Sun, 26 May 2013 12:06:39 +0200 Subject: CrazyHTTPd - HTTP Daemon in Python References: Message-ID: Mark Lawrence wrote: > On 26/05/2013 04:55, cdorm245 at gmail.com wrote: >> This is a small little Project that I have started. Its a light >> little Web Server (HTTPd) coded in python. Requirements: Python 2.7 >> =< And Linux / BSD. I believe this could work in a CLI Emulator in >> windows too. >> Welcome to check out the website powered by CrazyHTTPd: >> http://web.crazycoder.me:14081/index.html >> >> Please Email me with any bugs, errors, and/or comments about my >> little project: cdorm245 at gmail.com >> > > IMHO writing a project right now that is limited to Python 2 is about as > much use as a chocolate teapot. I.e. not very useful, but slightly more so than you would expect: http://www.thenakedscientists.com/HTML/content/kitchenscience/exp/how-useless-is-a-chocolate-teapot/ Adia? Marc From nikos.gr33k at gmail.com Sun May 26 04:08:39 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 01:08:39 -0700 (PDT) Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' Message-ID: Hello this is the following snippet that is causing me the error i mention in the Subject: try: cur.execute( '''SELECT url, hits FROM counters ORDER BY hits DESC''' ) data = cur.fetchall() for row in data: (url, hits) = row print( "
%s " ) % (url, url) print( "
%s " ) % (hits) except pymysql.ProgrammingError as e: print( repr(e) ) you can slso see it at: http://superhost.gr/?show=stats From __peter__ at web.de Sun May 26 04:23:40 2013 From: __peter__ at web.de (Peter Otten) Date: Sun, 26 May 2013 10:23:40 +0200 Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' References: Message-ID: ????? ???33? wrote: > Hello this is the following snippet that is causing me the error i mention > in the Subject: > print( "
%s " ) % (url, url) Hint (Python 3): >>> print("a=%s, b=%s") % (1, 2) a=%s, b=%s Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' >>> print("a=%s, b=%s" % (1, 2)) a=1, b=2 From nikos.gr33k at gmail.com Sun May 26 05:12:51 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 02:12:51 -0700 (PDT) Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: ?? ???????, 26 ????? 2013 11:23:40 ?.?. UTC+3, ? ??????? Peter Otten ??????: > ????? ???33? wrote: > > > > > Hello this is the following snippet that is causing me the error i mention > > > in the Subject: > > > > > print( "
%s " ) % (url, url) > > > > Hint (Python 3): > > > > >>> print("a=%s, b=%s") % (1, 2) > > a=%s, b=%s > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' > >>> print("a=%s, b=%s" % (1, 2)) > > a=1, b=2 Thank you very much Peter, so as it seems in Python 3.3.1 all substitutuons must be nested in print(). From __peter__ at web.de Sun May 26 05:25:17 2013 From: __peter__ at web.de (Peter Otten) Date: Sun, 26 May 2013 11:25:17 +0200 Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' References: Message-ID: ????? ???33? wrote: > Thank you very much Peter, so as it seems in Python 3.3.1 all > substitutuons must be nested in print(). Yes; in other words: In Python 3 print() is a function. From nikos.gr33k at gmail.com Sun May 26 06:32:18 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 03:32:18 -0700 (PDT) Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: <09f62f17-9a36-4b8c-a7db-e0dfd95ec5c4@googlegroups.com> Perhaps cna you help a bit with tha too: used to work in 2.6 but not with 3.3.1 #!/usr/bin/python3 # coding=utf-8 import cgitb; cgitb.enable() import cgi, os, sys from http import cookies # initialize cookie cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') ) cookie.load( cookie ) nikos = cookie.get('nikos') # if visitor cookie does exist if nikos: msg = "??? ??? ??????? ???????? ??? ?? ?? ????????? ?? ????????? ?????????? ??? ???????!" cookie['nikos'] = 'admin' cookie['nikos']['path'] = '/' cookie['nikos']['expires'] = -1 #this cookie will expire now else: msg = "??? ?? ??? ??? ???? ??? ?? ????, ??? ?? ????, ??? ?? ??????! ?? ????? ????? ? ??????? ??????????!!" cookie['nikos'] = 'admin' cookie['nikos']['path'] = '/' cookie['nikos']['expires'] = 60*60*24*30*12 #this cookie will expire in a year print ( "Content-type: text/html; charset=utf-8\n" ) print( cookie, msg ) sys.exit(0) ima getting internal server error. From nikos.gr33k at gmail.com Sun May 26 09:00:51 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 06:00:51 -0700 (PDT) Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: Anyone seeign somethign wrong? From rosuav at gmail.com Sun May 26 09:10:02 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 23:10:02 +1000 Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: On Sun, May 26, 2013 at 11:00 PM, ????? ???33? wrote: > Anyone seeign somethign wrong? Yes. You're posting requests, then bumping the thread two hours later as though you're entitled to a response quicker than that. Plus, the problems you're seeing ought to be solved by the 2to3 utility. Use it. Look at its output. ChrisA From carlosnepomuceno at outlook.com Sun May 26 10:23:59 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 17:23:59 +0300 Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: , Message-ID: ---------------------------------------- > Date: Sun, 26 May 2013 06:00:51 -0700 > Subject: Re: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' > From: nikos.gr33k at gmail.com > To: python-list at python.org > > Anyone seeign somethign wrong? > -- > http://mail.python.org/mailman/listinfo/python-list I don't know any module 'http' in Python standard library. Think you mean the 'http.cookies' module in Python 3. From nagia.retsina at gmail.com Sun May 26 11:00:18 2013 From: nagia.retsina at gmail.com (nagia.retsina at gmail.com) Date: Sun, 26 May 2013 08:00:18 -0700 (PDT) Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: ?? ???????, 26 ????? 2013 4:10:02 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > On Sun, May 26, 2013 at 11:00 PM, ????? ???33? wrote: > > > Anyone seeign somethign wrong? > > > > Yes. You're posting requests, then bumping the thread two hours later > > as though you're entitled to a response quicker than that. Plus, the > > problems you're seeing ought to be solved by the 2to3 utility. Use it. > > Look at its output. > > > > ChrisA There is a tool that convert python 2.6.x code => python 3.x ? Can you post a link to that toll plz? From rosuav at gmail.com Sun May 26 11:24:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 01:24:55 +1000 Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: On Mon, May 27, 2013 at 1:00 AM, wrote: > ?? ???????, 26 ????? 2013 4:10:02 ?.?. UTC+3, ? ??????? Chris Angelico ??????: >> On Sun, May 26, 2013 at 11:00 PM, ????? ???33? wrote: >> >> > Anyone seeign somethign wrong? >> >> >> >> Yes. You're posting requests, then bumping the thread two hours later >> >> as though you're entitled to a response quicker than that. Plus, the >> >> problems you're seeing ought to be solved by the 2to3 utility. Use it. >> >> Look at its output. >> >> >> >> ChrisA > > There is a tool that convert python 2.6.x code => python 3.x ? > > Can you post a link to that toll plz? You have its name. You have Google. You have a mailing list that is getting tired of you not putting in any effort. ChrisA From breamoreboy at yahoo.co.uk Sun May 26 12:43:33 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 17:43:33 +0100 Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: On 26/05/2013 16:24, Chris Angelico wrote: > On Mon, May 27, 2013 at 1:00 AM, wrote: >> ?? ???????, 26 ????? 2013 4:10:02 ?.?. UTC+3, ? ??????? Chris Angelico ??????: >>> On Sun, May 26, 2013 at 11:00 PM, ????? ???33? wrote: >>> >>>> Anyone seeign somethign wrong? >>> >>> >>> >>> Yes. You're posting requests, then bumping the thread two hours later >>> >>> as though you're entitled to a response quicker than that. Plus, the >>> >>> problems you're seeing ought to be solved by the 2to3 utility. Use it. >>> >>> Look at its output. >>> >>> >>> >>> ChrisA >> >> There is a tool that convert python 2.6.x code => python 3.x ? >> >> Can you post a link to that toll plz? > > You have its name. You have Google. You have a mailing list that is > getting tired of you not putting in any effort. > > ChrisA > Not to mention double spaced google crap :( -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From nikos.gr33k at gmail.com Sun May 26 11:45:59 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 08:45:59 -0700 (PDT) Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: <7d1d3a51-6f83-4a4e-9083-c60c581af4f1@googlegroups.com> ?? ???????, 26 ????? 2013 6:24:55 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > On Mon, May 27, 2013 at 1:00 AM, wrote: > > > ?? ???????, 26 ????? 2013 4:10:02 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > > >> On Sun, May 26, 2013 at 11:00 PM, ????? ???33? wrote: > > >> > > >> > Anyone seeign somethign wrong? > > >> > > >> > > >> > > >> Yes. You're posting requests, then bumping the thread two hours later > > >> > > >> as though you're entitled to a response quicker than that. Plus, the > > >> > > >> problems you're seeing ought to be solved by the 2to3 utility. Use it. > > >> > > >> Look at its output. > > >> > > >> > > >> > > >> ChrisA > > > > > > There is a tool that convert python 2.6.x code => python 3.x ? > > > > > > Can you post a link to that toll plz? > > > > You have its name. You have Google. You have a mailing list that is > > getting tired of you not putting in any effort. > > > > ChrisA Yes i saw it its name is 2to3 conversion but my code is already written in python3 i ahve written in line byu line from sctatrch ,much like pelatologio.py which when i intreprtet in via cmd i get no errors. i dont understand why iam receiving an internal server error for both in browser mdoe though. From nikos.gr33k at gmail.com Sun May 26 12:10:10 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 09:10:10 -0700 (PDT) Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: <7d1d3a51-6f83-4a4e-9083-c60c581af4f1@googlegroups.com> References: <7d1d3a51-6f83-4a4e-9083-c60c581af4f1@googlegroups.com> Message-ID: <6cb9f223-d114-40aa-bf13-314b65c327b7@googlegroups.com> Here is the live error log coming form apacher when i request the webpage form browser: ==> /usr/local/apache/logs/error_log <== [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] suexec failure: could not open log file [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] fopen: Permission denied [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] fopen: Permission denied [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] Premature end of script headers: koukos.py [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] Premature end of script headers: koukos.py what is that suexec? From breamoreboy at yahoo.co.uk Sun May 26 12:45:42 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 17:45:42 +0100 Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: <6cb9f223-d114-40aa-bf13-314b65c327b7@googlegroups.com> References: <7d1d3a51-6f83-4a4e-9083-c60c581af4f1@googlegroups.com> <6cb9f223-d114-40aa-bf13-314b65c327b7@googlegroups.com> Message-ID: On 26/05/2013 17:10, ????? ???33? wrote: > Here is the live error log coming form apacher when i request the webpage form browser: > > ==> /usr/local/apache/logs/error_log <== > [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] suexec failure: could not open log file > [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] fopen: Permission denied > [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] fopen: Permission denied > [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] Premature end of script headers: koukos.py > [Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] Premature end of script headers: koukos.py > > what is that suexec? > What has this got to do with Python? -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From cs at zip.com.au Sun May 26 20:26:29 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 27 May 2013 10:26:29 +1000 Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: Message-ID: <20130527002629.GA27910@cskk.homeip.net> On 26May2013 17:45, Mark Lawrence wrote: | On 26/05/2013 17:10, ????? ???33? wrote: | >Here is the live error log coming form apacher when i request the webpage form browser: | > | >==> /usr/local/apache/logs/error_log <== | >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] suexec failure: could not open log file | >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] fopen: Permission denied | >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] fopen: Permission denied | >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] Premature end of script headers: koukos.py | >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] Premature end of script headers: koukos.py | > | >what is that suexec? | | What has this got to do with Python? Little, and I've already explained what suexec is to him some weeks ago, off list. Cheers, -- Cameron Simpson The road less traveled is FASTER !! - Luc Marcouiller, marcouil at ireq.hydro.qc.ca From nikos.gr33k at gmail.com Tue May 28 02:01:35 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Mon, 27 May 2013 23:01:35 -0700 (PDT) Subject: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' In-Reply-To: References: <7d1d3a51-6f83-4a4e-9083-c60c581af4f1@googlegroups.com> <6cb9f223-d114-40aa-bf13-314b65c327b7@googlegroups.com> Message-ID: <9e932231-d7e8-439f-84e9-a71246c2b773@googlegroups.com> ?? ???????, 26 ????? 2013 7:45:42 ?.?. UTC+3, ? ??????? Mark Lawrence ??????: > What has this got to do with Python? What do your questions have to do with a proper reply? From abdulshafy at gmail.com Sun May 26 07:11:56 2013 From: abdulshafy at gmail.com (Ahmed Abdulshafy) Date: Sun, 26 May 2013 04:11:56 -0700 (PDT) Subject: Short-circuit Logic Message-ID: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Hi, I'm having a hard time wrapping my head around short-circuit logic that's used by Python, coming from a C/C++ background; so I don't understand why the following condition is written this way!> if not allow_zero and abs(x) < sys.float_info.epsilon: print("zero is not allowed") The purpose of this snippet is to print the given line when allow_zero is False and x is 0. From roy at panix.com Sun May 26 07:38:38 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 07:38:38 -0400 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: In article <5f101d70-e51f-4531-9153-c92ee2486fd9 at googlegroups.com>, Ahmed Abdulshafy wrote: > Hi, > I'm having a hard time wrapping my head around short-circuit logic that's > used by Python, coming from a C/C++ background; so I don't understand why the > following condition is written this way!> > > if not allow_zero and abs(x) < sys.float_info.epsilon: > print("zero is not allowed") > > The purpose of this snippet is to print the given line when allow_zero is > False and x is 0. I don't understand your confusion. Short-circuit evaluation works in Python exactly the same way it works in C. When you have a boolean operation, the operands are evaluated left-to-right, and evaluation stops as soon as the truth value of the expression is known. In C, you would write: if (p && p->foo) { blah(); } to make sure that you don't dereference a null pointer. A similar example in Python might be: if d and d["foo"]: blah() which protects against trying to access an element of a dictionary if the dictionary is None (which might happen if d was an optional argument to a method and wasn't passed on this invocation). But, none of that applies to your example. The condition is not allow_zero and abs(x) < sys.float_info.epsilon: it's safe to evaluate "abs(x) < sys.float_info.epsilon" no matter what the value of "not allow_zero". For the purposes of understanding your code, you can pretend that short-circuit evaluation doesn't exist! So, what is your code doing that you don't understand? From steve+comp.lang.python at pearwood.info Sun May 26 08:13:47 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 May 2013 12:13:47 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sun, 26 May 2013 04:11:56 -0700, Ahmed Abdulshafy wrote: > Hi, > I'm having a hard time wrapping my head around short-circuit logic > that's used by Python, coming from a C/C++ background; so I don't > understand why the following condition is written this way! > > if not allow_zero and abs(x) < sys.float_info.epsilon: > print("zero is not allowed") Follow the logic. If allow_zero is a true value, then "not allow_zero" is False, and the "and" clause cannot evaluate to true. (False and X is always False.) So print is not called. If allow_zero is a false value, then "not allow_zero" is True, and the "and" clause depends on the second argument. (True and X is always X.) So abs(x) < sys.float_info.epsilon is tested, and if that is True, print is called. By the way, I don't think much of this logic. Values smaller than epsilon are not necessarily zero: py> import sys py> epsilon = sys.float_info.epsilon py> x = epsilon/10000 py> x == 0 False py> x * 3 == 0 False py> x + epsilon == 0 False py> x + epsilon == epsilon False The above logic throws away many perfectly good numbers and treats them as zero even though they aren't. > The purpose of this snippet is to print the given line when allow_zero > is False and x is 0. Then the snippet utterly fails at that, since it prints the line for many values of x which can be distinguished from zero. The way to test whether x equals zero is: x == 0 What the above actually tests for is whether x is so small that (1.0+x) cannot be distinguished from 1.0, which is not the same thing. It is also quite arbitrary. Why 1.0? Why not (0.0001+x)? Or (0.00000001+x)? Or (10000.0+x)? -- Steven From abdulshafy at gmail.com Mon May 27 16:11:28 2013 From: abdulshafy at gmail.com (Ahmed Abdulshafy) Date: Mon, 27 May 2013 13:11:28 -0700 (PDT) Subject: Short-circuit Logic In-Reply-To: <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> On Sunday, May 26, 2013 2:13:47 PM UTC+2, Steven D'Aprano wrote: > On Sun, 26 May 2013 04:11:56 -0700, Ahmed Abdulshafy wrote: > > > > > Hi, > > > I'm having a hard time wrapping my head around short-circuit logic > > > that's used by Python, coming from a C/C++ background; so I don't > > > understand why the following condition is written this way! > > > > > > if not allow_zero and abs(x) < sys.float_info.epsilon: > > > print("zero is not allowed") > > > > Follow the logic. > > > > If allow_zero is a true value, then "not allow_zero" is False, and the > > "and" clause cannot evaluate to true. (False and X is always False.) So > > print is not called. > > > > If allow_zero is a false value, then "not allow_zero" is True, and the > > "and" clause depends on the second argument. (True and X is always X.) So > > abs(x) < sys.float_info.epsilon is tested, and if that is True, print is > > called. > > > > By the way, I don't think much of this logic. Values smaller than epsilon > > are not necessarily zero: > > > > py> import sys > > py> epsilon = sys.float_info.epsilon > > py> x = epsilon/10000 > > py> x == 0 > > False > > py> x * 3 == 0 > > False > > py> x + epsilon == 0 > > False > > py> x + epsilon == epsilon > > False > > > > The above logic throws away many perfectly good numbers and treats them > > as zero even though they aren't. > > > > > > > The purpose of this snippet is to print the given line when allow_zero > > > is False and x is 0. > > > > Then the snippet utterly fails at that, since it prints the line for many > > values of x which can be distinguished from zero. The way to test whether > > x equals zero is: > > > > x == 0 > > > > What the above actually tests for is whether x is so small that (1.0+x) > > cannot be distinguished from 1.0, which is not the same thing. It is also > > quite arbitrary. Why 1.0? Why not (0.0001+x)? Or (0.00000001+x)? Or > > (10000.0+x)? > > > > > > > > -- > > Steven That may be true for integers, but for floats, testing for equality is not always precise From nobody at nowhere.com Mon May 27 20:10:05 2013 From: nobody at nowhere.com (Nobody) Date: Tue, 28 May 2013 01:10:05 +0100 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> Message-ID: On Mon, 27 May 2013 13:11:28 -0700, Ahmed Abdulshafy wrote: > On Sunday, May 26, 2013 2:13:47 PM UTC+2, Steven D'Aprano wrote: > >> What the above actually tests for is whether x is so small that (1.0+x) >> cannot be distinguished from 1.0, which is not the same thing. It is >> also quite arbitrary. Why 1.0? Why not (0.0001+x)? Or (0.00000001+x)? >> Or (10000.0+x)? > > That may be true for integers, What may be true for integers? > but for floats, testing for equality is not always precise And your point is? What Steven wrote is entirely correct: sys.float_info.epsilon is the smallest value x such that 1.0 and 1.0+x have distinct floating-point representations. It has no relevance for comparing to zero. From abdulshafy at gmail.com Tue May 28 04:39:09 2013 From: abdulshafy at gmail.com (Ahmed Abdulshafy) Date: Tue, 28 May 2013 01:39:09 -0700 (PDT) Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> Message-ID: <48a99056-fa56-4352-b8b4-f52d3467ac96@googlegroups.com> On Tuesday, May 28, 2013 2:10:05 AM UTC+2, Nobody wrote: > On Mon, 27 May 2013 13:11:28 -0700, Ahmed Abdulshafy wrote: > > > > > On Sunday, May 26, 2013 2:13:47 PM UTC+2, Steven D'Aprano wrote: > > > > > >> What the above actually tests for is whether x is so small that (1.0+x) > > >> cannot be distinguished from 1.0, which is not the same thing. It is > > >> also quite arbitrary. Why 1.0? Why not (0.0001+x)? Or (0.00000001+x)? > > >> Or (10000.0+x)? > > > > > > That may be true for integers, > > > > What may be true for integers? > > > > > but for floats, testing for equality is not always precise > > > > And your point is? > > > > What Steven wrote is entirely correct: sys.float_info.epsilon is the > > smallest value x such that 1.0 and 1.0+x have distinct floating-point > > representations. It has no relevance for comparing to zero. He just said that the way to test for zero equality is x == 0, and I meant that this is true for integers but not necessarily for floats. And that's not specific to Python. From carlosnepomuceno at outlook.com Tue May 28 05:32:20 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 12:32:20 +0300 Subject: Short-circuit Logic In-Reply-To: <48a99056-fa56-4352-b8b4-f52d3467ac96@googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com>, <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com>, <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com>, , <48a99056-fa56-4352-b8b4-f52d3467ac96@googlegroups.com> Message-ID: ---------------------------------------- > Date: Tue, 28 May 2013 01:39:09 -0700 > Subject: Re: Short-circuit Logic > From: abdulshafy at gmail.com [...] >> What Steven wrote is entirely correct: sys.float_info.epsilon is the >> >> smallest value x such that 1.0 and 1.0+x have distinct floating-point >> >> representations. It has no relevance for comparing to zero. > > He just said that the way to test for zero equality is x == 0, and I meant that this is true for integers but not necessarily for floats. And that's not specific to Python. Have you read [1]? There's a section "Infernal Zero" that discuss this problem. I think it's very interesting to know! ;) Just my 49.99999999999998?! lol [1] http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ From breamoreboy at yahoo.co.uk Tue May 28 07:45:21 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 28 May 2013 12:45:21 +0100 Subject: Short-circuit Logic In-Reply-To: <48a99056-fa56-4352-b8b4-f52d3467ac96@googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <48a99056-fa56-4352-b8b4-f52d3467ac96@googlegroups.com> Message-ID: On 28/05/2013 09:39, Ahmed Abdulshafy wrote: > > And that's not specific to Python. > Using google products is also not specific to Python. However whereever it's used it's a PITA as people are forced into reading double spaced crap. Please check out the link in my signature. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From steve+comp.lang.python at pearwood.info Tue May 28 09:51:37 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 May 2013 13:51:37 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <48a99056-fa56-4352-b8b4-f52d3467ac96@googlegroups.com> Message-ID: <51a4b669$0$29966$c3e8da3$5496439d@news.astraweb.com> On Tue, 28 May 2013 01:39:09 -0700, Ahmed Abdulshafy wrote: > He just said that the way to test for zero equality is x == 0, and I > meant that this is true for integers but not necessarily for floats. And > that's not specific to Python. Can you show me a value of x where x == 0.0 returns False, but x actually isn't zero? Built-in floats only, if you subclass you can do anything you like: class Cheating(float): def __eq__(self, other): return False -- Steven From invalid at invalid.invalid Tue May 28 11:14:03 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 15:14:03 +0000 (UTC) Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <48a99056-fa56-4352-b8b4-f52d3467ac96@googlegroups.com> <51a4b669$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-05-28, Steven D'Aprano wrote: > On Tue, 28 May 2013 01:39:09 -0700, Ahmed Abdulshafy wrote: > >> He just said that the way to test for zero equality is x == 0, and I >> meant that this is true for integers but not necessarily for floats. And >> that's not specific to Python. > > Can you show me a value of x where x == 0.0 returns False, but x actually > isn't zero? I'm confused. Don't all non-zero values satisfy your conditions? >>> x = 1.0 >>> x == 0.0 False >>> x is 0.0 False -- Grant Edwards grant.b.edwards Yow! I'm dressing up in at an ill-fitting IVY-LEAGUE gmail.com SUIT!! Too late... From steve+comp.lang.python at pearwood.info Tue May 28 11:55:42 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 May 2013 15:55:42 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <48a99056-fa56-4352-b8b4-f52d3467ac96@googlegroups.com> <51a4b669$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51a4d37e$0$29966$c3e8da3$5496439d@news.astraweb.com> On Tue, 28 May 2013 15:14:03 +0000, Grant Edwards wrote: > On 2013-05-28, Steven D'Aprano > wrote: >> On Tue, 28 May 2013 01:39:09 -0700, Ahmed Abdulshafy wrote: >> >>> He just said that the way to test for zero equality is x == 0, and I >>> meant that this is true for integers but not necessarily for floats. >>> And that's not specific to Python. >> >> Can you show me a value of x where x == 0.0 returns False, but x >> actually isn't zero? > > I'm confused. Don't all non-zero values satisfy your conditions? Of course they do :-( I meant "but x actually *is* zero". Sorry for the confusion. I blame the terrists. -- Steven From steve+comp.lang.python at pearwood.info Tue May 28 09:48:17 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 May 2013 13:48:17 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> Message-ID: <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> On Mon, 27 May 2013 13:11:28 -0700, Ahmed Abdulshafy wrote: > That may be true for integers, but for floats, testing for equality is > not always precise Incorrect. Testing for equality is always precise, and exact. The problem is not the *equality test*, but that you don't always have the number that you think you have. The problem lies elsewhere, not equality! Unfortunately, people who say "never test floats for equality" have misdiagnosed the problem, or they are giving a simple work-around which can be misleading to those who don't understand what is actually going on. Any floating point libraries that support IEEE-754 semantics can guarantee a few things, including: x == 0.0 if, and only if, x actually equals zero. This was not always the case for all floating point systems prior to IEEE-754. In his forward to the Apple Numerics Manual, William Kahan describes a Capriciously Designed Computer where 1/x can give a Division By Zero error even though x != 0. Fortunately, if you are programming in Python on Intel-compatible hardware, you do not have to worry about nightmares like that. Let me repeat that: in Python, you can trust that if x == 0.0 returns False, then x is definitely not zero. In any case, the test that you show is not a good test. I have already shown that it wrongly treats many non-zero numbers which can be distinguished from zero as if they were zero. But worse, it also fails as a guard against numbers which cannot be distinguished from zero! py> import sys py> epsilon = sys.float_info.epsilon py> x < epsilon # Is x so tiny it looks like zero? False py> y = 1e17 + x # x is not zero, so y should be > 1e17 py> 1/(1e17 - y) Traceback (most recent call last): File "", line 1, in ZeroDivisionError: float division by zero So as you can see, testing for "zero" by comparing to machine epsilon does not save you from Zero Division errors. -- Steven From rosuav at gmail.com Tue May 28 10:01:01 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 29 May 2013 00:01:01 +1000 Subject: Short-circuit Logic In-Reply-To: <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, May 28, 2013 at 11:48 PM, Steven D'Aprano wrote: > py> y = 1e17 + x # x is not zero, so y should be > 1e17 > py> 1/(1e17 - y) > Traceback (most recent call last): > File "", line 1, in > ZeroDivisionError: float division by zero You don't even need to go for 1e17. By definition: >>> sys.float_info.epsilon+1.0==1.0 False >>> sys.float_info.epsilon+2.0==2.0 True Therefore the same can be done with 2 as you did with 1e17. >>> y = 2 + sys.float_info.epsilon >>> 1/(2-y) Traceback (most recent call last): File "", line 1, in 1/(2-y) ZeroDivisionError: float division by zero Of course, since we're working with a number greater than epsilon, we need to go a little further, but we can still work with small numbers: >>> x = sys.float_info.epsilon * 2 # Definitely greater than epsilon >>> y = 4 + x >>> 1/(4-y) Traceback (most recent call last): File "", line 1, in 1/(4-y) ZeroDivisionError: float division by zero ChrisA From abdulshafy at gmail.com Wed May 29 10:27:40 2013 From: abdulshafy at gmail.com (Ahmed Abdulshafy) Date: Wed, 29 May 2013 07:27:40 -0700 (PDT) Subject: Short-circuit Logic In-Reply-To: <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> On Tuesday, May 28, 2013 3:48:17 PM UTC+2, Steven D'Aprano wrote: > On Mon, 27 May 2013 13:11:28 -0700, Ahmed Abdulshafy wrote: > > > > > That may be true for integers, but for floats, testing for equality is > > > not always precise > > > > Incorrect. Testing for equality is always precise, and exact. The problem > > is not the *equality test*, but that you don't always have the number > > that you think you have. The problem lies elsewhere, not equality! > > > Steven Well, this is taken from my python shell> >>> 0.33455857352426283 == 0.33455857352426282 True Anyway, man, those were not my words anyway, most programming books I've read state so. Here's an excerpt from the Python book, I'm currently reading> ">>> 0.0, 5.4, -2.5, 8.9e-4 (0.0, 5.4000000000000004, -2.5, 0.00088999999999999995) The inexactness is not a problem specific to Python?all programming languages have this problem with floating-point numbers." From rosuav at gmail.com Wed May 29 10:32:34 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 00:32:34 +1000 Subject: Short-circuit Logic In-Reply-To: <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> Message-ID: On Thu, May 30, 2013 at 12:27 AM, Ahmed Abdulshafy wrote: > Well, this is taken from my python shell> > >>>> 0.33455857352426283 == 0.33455857352426282 > True >>> 0.33455857352426283,0.33455857352426282 (0.3345585735242628, 0.3345585735242628) They're not representably different. ChrisA From rustompmody at gmail.com Wed May 29 10:33:39 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 29 May 2013 07:33:39 -0700 (PDT) Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> Message-ID: <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> On May 29, 7:27?pm, Ahmed Abdulshafy wrote: > On Tuesday, May 28, 2013 3:48:17 PM UTC+2, Steven D'Aprano wrote: > > On Mon, 27 May 2013 13:11:28 -0700, Ahmed Abdulshafy wrote: > > > > That may be true for integers, but for floats, testing for equality is > > > > not always precise > > > Incorrect. Testing for equality is always precise, and exact. The problem > > > is not the *equality test*, but that you don't always have the number > > > that you think you have. The problem lies elsewhere, not equality! > > > Steven > > Well, this is taken from my python shell> > > >>> 0.33455857352426283 == 0.33455857352426282 > > True > > Anyway, man, those were not my words anyway, most programming books I've read state so. Here's an excerpt from the Python book, I'm currently reading> > > ">>> 0.0, 5.4, -2.5, 8.9e-4 > (0.0, 5.4000000000000004, -2.5, 0.00088999999999999995) > > The inexactness is not a problem specific to Python?all programming languages have this problem with floating-point numbers." 0.0 == 0.0 implies 5.4 == 5.4 is not a true statement is what (I think) Steven is saying. 0 (or if you prefer 0.0) is special and is treated specially. Naturally if you reach (nearabout) 0.0 by some numerical process thats another matter... From ian.g.kelly at gmail.com Wed May 29 12:50:47 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 29 May 2013 10:50:47 -0600 Subject: Short-circuit Logic In-Reply-To: <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> Message-ID: On Wed, May 29, 2013 at 8:33 AM, rusi wrote: > 0.0 == 0.0 implies 5.4 == 5.4 > is not a true statement is what (I think) Steven is saying. > 0 (or if you prefer 0.0) is special and is treated specially. It has nothing to do with 0 being special. A floating point number will always equal itself (except for nan, which is even more special), and in particular 5.4 == 5.4. But if you have two different calculations that produce 0, or two different calculations that produce 5.4, you might actually get two different numbers that approximate 0 or 5.4 thanks to rounding error. If you then compare those two ever-so-slightly different numbers, you will find them unequal. From davea at davea.name Wed May 29 20:23:00 2013 From: davea at davea.name (Dave Angel) Date: Wed, 29 May 2013 20:23:00 -0400 Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> Message-ID: <51A69BE4.3060003@davea.name> On 05/29/2013 12:50 PM, Ian Kelly wrote: > On Wed, May 29, 2013 at 8:33 AM, rusi wrote: >> 0.0 == 0.0 implies 5.4 == 5.4 >> is not a true statement is what (I think) Steven is saying. >> 0 (or if you prefer 0.0) is special and is treated specially. > > It has nothing to do with 0 being special. A floating point number > will always equal itself (except for nan, which is even more special), > and in particular 5.4 == 5.4. But if you have two different > calculations that produce 0, or two different calculations that > produce 5.4, you might actually get two different numbers that > approximate 0 or 5.4 thanks to rounding error. If you then compare > those two ever-so-slightly different numbers, you will find them > unequal. > Rounding error is just one of the problems. Usually less obvious is quantization error. If you represent a floating number in decimal, but you're using a binary floating point representation, it just might change. Another error is roundoff error. Even in a pure decimal system of (say) 40 digits, I could type in a 42 digit number and it would get quantized. So just because two 42 digit numbers are different doesn't imply that the 40 digit internal format would be. -- DaveA From steve+comp.lang.python at pearwood.info Thu May 30 01:20:42 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 05:20:42 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> Message-ID: <51a6e1aa$0$11118$c3e8da3@news.astraweb.com> On Wed, 29 May 2013 20:23:00 -0400, Dave Angel wrote: > Even in a pure decimal system of (say) > 40 digits, I could type in a 42 digit number and it would get quantized. > So just because two 42 digit numbers are different doesn't imply that > the 40 digit internal format would be. Correct, and we can demonstrate it using Python: py> from decimal import * py> getcontext().prec = 3 py> a = +Decimal('1.0000') py> b = +Decimal('1.0009') py> a == b True (By default, the Decimal constructor does not honour the current precision. To force it to do so, use the unary + operator.) -- Steven From steve+comp.lang.python at pearwood.info Wed May 29 22:28:57 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 02:28:57 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> Message-ID: <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> On Wed, 29 May 2013 10:50:47 -0600, Ian Kelly wrote: > On Wed, May 29, 2013 at 8:33 AM, rusi wrote: >> 0.0 == 0.0 implies 5.4 == 5.4 >> is not a true statement is what (I think) Steven is saying. 0 (or if >> you prefer 0.0) is special and is treated specially. > > It has nothing to do with 0 being special. A floating point number will > always equal itself (except for nan, which is even more special), and in > particular 5.4 == 5.4. But if you have two different calculations that > produce 0, or two different calculations that produce 5.4, you might > actually get two different numbers that approximate 0 or 5.4 thanks to > rounding error. If you then compare those two ever-so-slightly > different numbers, you will find them unequal. EXACTLY! The problem does not lie with the *equality operator*, it lies with the calculations. And that is an intractable problem -- in general, floating point is *hard*. So the problem occurs when we start with a perfectly good statement of the facts: "If you naively test the results of a calculation for equality without understanding what you are doing, you will often get surprising results" which then turns into a general heuristic that is often, but not always, reasonable: "In general, you should test for floating point *approximate* equality, in some appropriate sense, rather than exact equality" which then gets mangled to: "Never test floating point numbers for equality" and then implemented badly by people who have no clue what they are doing and have misunderstood the nature of the problem, leading to either: * de facto exact equality testing, only slower and with the *illusion* of avoiding equality, e.g. "abs(x-y) < sys.float_info.epsilon" is just a long and slow way of saying "x == y" when both numbers are sufficiently large; * incorrectly accepting non-equal numbers as "equal" just because they happen to be "close". The problem is that there is *no one right answer*, except "have everyone become an expert in floating point, then judge every case on its merits", which will never happen. But if nothing else, I wish that we can get past the rank superstition that you should "never" test floats for equality. That would be a step forward. -- Steven From rosuav at gmail.com Wed May 29 23:45:13 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 13:45:13 +1000 Subject: Short-circuit Logic In-Reply-To: <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, May 30, 2013 at 12:28 PM, Steven D'Aprano wrote: > * de facto exact equality testing, only slower and with the *illusion* of > avoiding equality, e.g. "abs(x-y) < sys.float_info.epsilon" is just a > long and slow way of saying "x == y" when both numbers are sufficiently > large; > The problem here, I think, is that "epsilon" has two meanings: * sys.float_info.epsilon, which is an extremely specific value (the smallest x such that 1.0+x != x) * the mathematical concept, which is where the other got its name from. Let's suppose someone is told to compare floating point numbers by seeing if the absolute value of the difference is less than some epsilon. They look up "absolute value" and find abs(); they look up "epsilon" and think they've found it. Trouble is, they've found the wrong epsilon... and really, there's an engineering issue here too. Here's one of my favourite examples of equality comparisons: http://xkcd.com/1047/ # Let's say we measured this accurately to one part in 40 x = one_light_year_in_meters y = pow(99,8) x == y # False abs(x-y) < x/40 # True Measurement accuracy is usually far FAR worse than floating-point accuracy. It's pretty pointless to compare for some kind of "equality" that ignores this. Say you measure the diameter and circumference of a circle, accurate to one meter, and got values of 79 and 248; does this mean that pi is less than 3.14? No - in fact: pi = 248/79 # math.pi = 3.141592653589793 abs(pi-math.pi) < pi/79 # True Worst error is 1 in 79, so all comparisons are done with epsilon derived from that. ChrisA From steve+comp.lang.python at pearwood.info Thu May 30 01:42:17 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 05:42:17 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> On Thu, 30 May 2013 13:45:13 +1000, Chris Angelico wrote: > Let's suppose someone is told to compare floating point numbers by > seeing if the absolute value of the difference is less than some > epsilon. Which is usually the wrong way to do it! Normally one would prefer *relative* error, not absolute: # absolute error: abs(a - b) < epsilon # relative error: abs(a - b)/a < epsilon One problem with absolute error is that it can give an entirely spurious image of "fuzziness", when in reality it is actually performing the same exact equality as == only slower and more verbosely. If a and b are sufficiently large, the smallest possible difference between a and b may be greater than epsilon (for whichever epsilon you pick). When that happens, you might as well just use == and be done with it. But using relative error also raises questions: - what if a is negative? - why relative to a instead of relative to b? - what if a is zero? The first, at least, is easy to solve: take the absolute value of a. But strangely, you rarely see programming books mention that, so I expect that there is a lot of code in the real world that assumes a is positive and does the wrong thing when it isn't. Here's another way, mathematically equivalent (although not necessarily equivalent using floating point computations!) which avoids the divide-by- zero problem: abs(a - b) < epsilon*a Whichever method you choose, there are gotchas to watch out for. > http://xkcd.com/1047/ Nice! -- Steven From jpiitula at ling.helsinki.fi Thu May 30 03:22:02 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 30 May 2013 10:22:02 +0300 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano writes: > On Thu, 30 May 2013 13:45:13 +1000, Chris Angelico wrote: > > > Let's suppose someone is told to compare floating point numbers by > > seeing if the absolute value of the difference is less than some > > epsilon. > > Which is usually the wrong way to do it! Normally one would prefer > *relative* error, not absolute: > > # absolute error: > abs(a - b) < epsilon > > > # relative error: > abs(a - b)/a < epsilon > ... I wonder why floating-point errors are not routinely discussed in terms of ulps (units in last position). There is a recipe for calculating the difference of two floating point numbers in ulps, and it's possible to find the previous or next floating point number, but I don't know of any programming language having built-in support for these. Why isn't this considered the most natural measure of a floating point result being close to a given value? The meaning is roughly this: how many floating point numbers there are between these two. "close enough" if abs(ulps(a, b)) < 3 else "not close enough" "equal" if ulps(a, b) == 0 else "not equal" There must be some subtle technical issues here, too, but it puzzles me that this measure of closeness is not often even discussed when absolute and relative error are discussed - and computed using the same approximate arithmetic whose accuracy is being measured. Scary. Got light? From steve+comp.lang.python at pearwood.info Thu May 30 04:29:41 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 08:29:41 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: <51a70df4$0$11118$c3e8da3@news.astraweb.com> On Thu, 30 May 2013 10:22:02 +0300, Jussi Piitulainen wrote: > I wonder why floating-point errors are not routinely discussed in terms > of ulps (units in last position). There is a recipe for calculating the > difference of two floating point numbers in ulps, and it's possible to > find the previous or next floating point number, but I don't know of any > programming language having built-in support for these. That is an excellent question! I think it is because the traditional recipes for "close enough" equality either pre-date any standardization of floating point types, or because they're written by people who are thinking about abstract floating point numbers and not considering the implementation. Prior to most compiler and hardware manufacturers standardizing on IEEE 754, there was no real way to treat float's implementation in a machine independent way. Every machine laid their floats out differently, or used different number of bits. Some even used decimal, and in the case of a couple of Russian machines, trinary. (Although that's going a fair way back.) But we now have IEEE 754, and C has conquered the universe, so it's reasonable for programming languages to offer an interface for accessing floating point objects in terms of ULPs. Especially for a language like Python, which only has a single float type. I have a module that works with ULPs. I may clean it up and publish it. Would there be interest in seeing it in the standard library? > Why isn't this considered the most natural measure of a floating point > result being close to a given value? The meaning is roughly this: how > many floating point numbers there are between these two. There are some subtleties here also. Firstly, how many ULP should you care about? Three, as you suggest below, is awfully small, and chances are most practical, real-world calculations could not justify 3 ULP. Numbers that we normally care about, like "0.01mm", probably can justify thousands of ULP when it comes to C-doubles, which Python floats are. Another subtlety: small-but-positive numbers are millions of ULP away from small-but-negative numbers. Also, there are issues to do with +0.0 and -0.0, NANs and the INFs. -- Steven From jpiitula at ling.helsinki.fi Thu May 30 05:07:40 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 30 May 2013 12:07:40 +0300 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> <51a70df4$0$11118$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano writes: > On Thu, 30 May 2013 10:22:02 +0300, Jussi Piitulainen wrote: > > > I wonder why floating-point errors are not routinely discussed in > > terms of ulps (units in last position). There is a recipe for > > calculating the difference of two floating point numbers in ulps, > > and it's possible to find the previous or next floating point > > number, but I don't know of any programming language having > > built-in support for these. ... > But we now have IEEE 754, and C has conquered the universe, so it's > reasonable for programming languages to offer an interface for > accessing floating point objects in terms of ULPs. Especially for a > language like Python, which only has a single float type. Yes, that's what I'm thinking, that there is now a ubiquitous floating point format or two, so the properties of the format could be used. > I have a module that works with ULPs. I may clean it up and publish it. > Would there be interest in seeing it in the standard library? Yes, please. > There are some subtleties here also. Firstly, how many ULP should > you care about? Three, as you suggest below, is awfully small, and > chances are most practical, real-world calculations could not > justify 3 ULP. Numbers that we normally care about, like "0.01mm", > probably can justify thousands of ULP when it comes to C-doubles, > which Python floats are. I suppose this depends on the complexity of the process and the amount of data that produced the numbers of interest. Many individual floating point operations are required to be within an ulp or two of the mathematically correct result, I think, and the rounding error when parsing a written representation of a number should be similar. Either these add up to produce large errors, or the computation is approximate in other ways in addition to using floating point. One could develop a kind of sense for such differences. Ulps could be a tangible measure when comparing different algorithms. (That's what I tried to do with them in the first place. And that's how I began to notice their absence when floating point errors are discussed.) > Another subtlety: small-but-positive numbers are millions of ULP > away from small-but-negative numbers. Also, there are issues to do > with +0.0 and -0.0, NANs and the INFs. The usual suspects ^_^ and no reason to dismiss the ulp when the competing kinds of error have their corresponding subtleties. A matter of education, I'd say. Thank you much for an illuminating discussion. From nobody at nowhere.com Thu May 30 18:55:25 2013 From: nobody at nowhere.com (Nobody) Date: Thu, 30 May 2013 23:55:25 +0100 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> <51a70df4$0$11118$c3e8da3@news.astraweb.com> Message-ID: On Thu, 30 May 2013 12:07:40 +0300, Jussi Piitulainen wrote: > I suppose this depends on the complexity of the process and the amount > of data that produced the numbers of interest. Many individual > floating point operations are required to be within an ulp or two of > the mathematically correct result, I think, and the rounding error > when parsing a written representation of a number should be similar. Elementary operations (+, -, *, /, %, sqrt) are supposed to be within +/- 0.5 ULP (for round-to-nearest), i.e. the actual result should be the closest representable value to the exact result. Transcendental functions should ideally be within +/- 1 ULP, i.e. the actual result should be one of the two closest representable values to the exact result. Determining the closest value isn't always feasible due to the "table-maker's dilemma", i.e. the fact that regardless of the number of digits used for intermediate results, the upper and lower bounds can remain on opposite sides of the dividing line. From stefan at drees.name Fri May 31 11:34:36 2013 From: stefan at drees.name (Stefan Drees) Date: Fri, 31 May 2013 17:34:36 +0200 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> <51a70df4$0$11118$c3e8da3@news.astraweb.com> Message-ID: On 2013-05-30 08:29:41 +0000, Steven D'Aprano said: > On Thu, 30 May 2013 10:22:02 +0300, Jussi Piitulainen wrote: >> I wonder why floating-point errors are not routinely discussed in terms >> of ulps (units in last position). ... > That is an excellent question! ... > I have a module that works with ULPs. I may clean it up and publish it. > Would there be interest in seeing it in the standard library? ... I am definitely interested seeing this in the python standard library. But as I continued to read the lines following your proposal and the excellent article from Bruce pointed to by Carlos on this thread, maybe a package on pypi first grounding somewhat the presumably massive discussion thread on python-ideas :-?) All the best, Stefan. From roy at panix.com Thu May 30 08:48:59 2013 From: roy at panix.com (Roy Smith) Date: Thu, 30 May 2013 08:48:59 -0400 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: In article , Jussi Piitulainen wrote: > I wonder why floating-point errors are not routinely discussed in > terms of ulps (units in last position). Analysis of error is a complicated topic (and is much older than digital computers). These sorts of things come up in the real world, too. For example, let's say I have two stakes driven into the ground 1000 feet apart. One of them is near me and is my measurement datum. I want to drive a third stake which is 1001 feet away from the datum. Do I measure 1 foot from the second stake, or do I take out my super-long tape measure and measure 1001 feet from the datum? From carlosnepomuceno at outlook.com Thu May 30 20:03:08 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 31 May 2013 03:03:08 +0300 Subject: Short-circuit Logic In-Reply-To: <4dofq8pjvfbtjlkvks4lolsnltktvbuvf5@invalid.netcom.com> References: <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com>, <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com>, <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com>, , <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com>, , <51a6e6b8$0$11118$c3e8da3@news.astraweb.com>, , , <4dofq8pjvfbtjlkvks4lolsnltktvbuvf5@invalid.netcom.com> Message-ID: ---------------------------------------- > To: python-list at python.org > From: wlfraed at ix.netcom.com > Subject: Re: Short-circuit Logic > Date: Thu, 30 May 2013 19:38:31 -0400 > > On Thu, 30 May 2013 08:48:59 -0400, Roy Smith declaimed > the following in gmane.comp.python.general: > >> >> Analysis of error is a complicated topic (and is much older than digital >> computers). These sorts of things come up in the real world, too. For >> example, let's say I have two stakes driven into the ground 1000 feet >> apart. One of them is near me and is my measurement datum. >> >> I want to drive a third stake which is 1001 feet away from the datum. >> Do I measure 1 foot from the second stake, or do I take out my >> super-long tape measure and measure 1001 feet from the datum? > > On the same azimuth? Using the "super long tape" and ensuring it > traverses the 1000 foot stake is probably going to be most accurate -- > you only have the uncertainty of the positioning of the tape on the > datum, and the small uncertainty of azimuth over the 1000 foot stake. > And even the azimuth error isn't contributing to the distance error. > > Measuring 1 foot from the 1000 foot stake leaves you with any error > from datum to the 1000 foot, plus any error from the 1000 foot, PLUS any > azimuth error which would contribute to shortening the datum distance. Just because you have more causes of error doesn't mean you have lesser accurate measures. If fact, errors may compensate each other. It all depends on the bias (accuracy) and variation (precision) involved in the measurements you are considering. > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list From nobody at nowhere.com Thu May 30 21:10:03 2013 From: nobody at nowhere.com (Nobody) Date: Fri, 31 May 2013 02:10:03 +0100 Subject: Short-circuit Logic References: <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: On Thu, 30 May 2013 19:38:31 -0400, Dennis Lee Bieber wrote: > Measuring 1 foot from the 1000 foot stake leaves you with any error > from datum to the 1000 foot, plus any error from the 1000 foot, PLUS any > azimuth error which would contribute to shortening the datum distance. First, let's ignore azimuthal error. If you measure both distances from the same origin, and you have a measurement error of 0.1% (i.e. 1/1000), then the 1000' measurement will actually be between 999' and 1001', while the 1001' measurement will be between 1000' and 1002' (to the nearest whole foot). Meaning that the distance from the 1000' stake to the 1001' stake could be anywhere between -1' and 3' (i.e. the 1001' stake could be measured as being closer than the 1000' stake). This is why technical drawings which include regularly-spaced features will normally specify the positions of features relative to their neighbours instead of (or as well as) relative to some origin. When you're dealing with relative error, the obvious question is "relative to what?". From roy at panix.com Thu May 30 21:21:14 2013 From: roy at panix.com (Roy Smith) Date: Thu, 30 May 2013 21:21:14 -0400 Subject: Short-circuit Logic References: <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: In article , Nobody wrote: > On Thu, 30 May 2013 19:38:31 -0400, Dennis Lee Bieber wrote: > > > Measuring 1 foot from the 1000 foot stake leaves you with any error > > from datum to the 1000 foot, plus any error from the 1000 foot, PLUS any > > azimuth error which would contribute to shortening the datum distance. > > First, let's ignore azimuthal error. > > If you measure both distances from the same origin, and you have a > measurement error of 0.1% (i.e. 1/1000), then the 1000' measurement will > actually be between 999' and 1001', while the 1001' measurement will be > between 1000' and 1002' (to the nearest whole foot). > > Meaning that the distance from the 1000' stake to the 1001' stake could be > anywhere between -1' and 3' (i.e. the 1001' stake could be measured as > being closer than the 1000' stake). > > This is why technical drawings which include regularly-spaced features > will normally specify the positions of features relative to their > neighbours instead of (or as well as) relative to some origin. Not to mention "Do not scale drawing" warnings. Do they still put that on drawings? It was standard practice back when I was learning drafting. > When you're dealing with relative error, the obvious question is > "relative to what?". Exactly. Most programmers are very poorly training in these sorts of things (not to mention crypto, UX, etc). I put myself in that camp too. I know just enough about floating point to understand that I don't really know what I'm doing. I would never write a program where numerical accuracy was critical (say, stress analysis of a new airframe or a nuclear power plant control system) without having somebody who really knew that stuff on the team. From torriem at gmail.com Thu May 30 23:33:10 2013 From: torriem at gmail.com (Michael Torrie) Date: Thu, 30 May 2013 21:33:10 -0600 Subject: Short-circuit Logic In-Reply-To: References: <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: <51A819F6.3050605@gmail.com> On 05/30/2013 07:10 PM, Nobody wrote: > This is why technical drawings which include regularly-spaced features > will normally specify the positions of features relative to their > neighbours instead of (or as well as) relative to some origin. If I am planting trees, putting in fence posts, or drilling lots of little holes in steel, I am actually more likely to measure from the origin (or one arbitrary position). I trust that the errors accumulating as the tape measure marks were printed on the tape is less than the error I'd accumulate by digging a hole, and measuring from there to the next hole. And definitely when drilling a series of holes I'll never measure hole to hole to mark. If I measure from the origin than any error for the hole is limited to itself as much as possible rather than passing on the error to subsequent hole positions. If I was making a server rack, for example, having the holes consistently near their desired position is necessary. Tolerances are such that my hole can be off by as much as a 1/16" of inch of my desired position and it would still be fine, but not if each hole was off by an additional 1/16". I guess what I've described is accuracy vs precision. In the case of the server rack accuracy is important, and precision can be more coarse depending on the screw size and the mount type (threaded hole vs square hole with snap-in nut). From rosuav at gmail.com Thu May 30 04:29:23 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 18:29:23 +1000 Subject: Short-circuit Logic In-Reply-To: <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: On Thu, May 30, 2013 at 3:42 PM, Steven D'Aprano wrote: > On Thu, 30 May 2013 13:45:13 +1000, Chris Angelico wrote: > >> Let's suppose someone is told to compare floating point numbers by >> seeing if the absolute value of the difference is less than some >> epsilon. > > Which is usually the wrong way to do it! Normally one would prefer > *relative* error, not absolute: > > # absolute error: > abs(a - b) < epsilon > > > # relative error: > abs(a - b)/a < epsilon I was picking an epsilon based on a, though, which comes to pretty much the same thing as the relative error calculation you're using. > But using relative error also raises questions: > > - what if a is negative? > > - why relative to a instead of relative to b? > > - what if a is zero? > > The first, at least, is easy to solve: take the absolute value of a. One technique I saw somewhere is to use the average of a and b. But probably better is to take the lower absolute value (ie the larger epsilon). However, there's still the question of what epsilon should be - what percentage of a or b you take to mean equal - and that one is best answered by looking at the original inputs. Take these guys, for instance. Doing the same thing I was, only with more accuracy. http://www.youtube.com/watch?v=ZNiRzZ66YN0 ChrisA From carlosnepomuceno at outlook.com Thu May 30 17:03:13 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 31 May 2013 00:03:13 +0300 Subject: Short-circuit Logic In-Reply-To: <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com>, <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com>, <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com>, <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com>, <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com>, <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com>, , <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com>, , <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: Short-circuit Logic > Date: Thu, 30 May 2013 05:42:17 +0000 > To: python-list at python.org [...] > Here's another way, mathematically equivalent (although not necessarily > equivalent using floating point computations!) which avoids the divide-by- > zero problem: > > abs(a - b) < epsilon*a That's wrong! If abs(a) < abs(a-b)/epsilon you will break the commutative law. For example: import sys eps = sys.float_info.epsilon def almost_equalSD(a,b): ??? return abs(a-b) < eps*a #special case a=1 b=1/(1-eps) almost_equalSD(a,b) == almost_equalSD(b,a) Returns False. This discussion reminded me of TAOCP and I paid a visit and bring the following functions: #Floating Point Comparison Operations #Knuth, Donald (1981). The Art of Computer Programming. 2nd ed. Vol. 2. p. 218. Addison-Wesley. ISBN 0-201-03822-6. import sys #floating point comparison: u ? v(?) "definitely less than" (definition 21) def fpc_dlt(u,v,eps=sys.float_info.epsilon): ??? au=abs(u) ??? av=abs(v) ??? return (v-u)> (eps*(au if au>av else av))? # v-u> ?*max(|u|,|v|) #floating point comparison: u ~ v(?) "approximately equal to" (definition 22) def fpc_aeq(u,v,eps=sys.float_info.epsilon): ??? au=abs(u) ??? av=abs(v) ??? return abs(v-u) <= (eps*(au if au>av else av))? # |v-u| <= ?*max(|u|,|v|) #floating point comparison: u ? v(?) "definitely greater than" (definition 23) def fpc_dgt(u,v,eps=sys.float_info.epsilon): ??? au=abs(u) ??? av=abs(v) ??? return (u-v)> (eps*(au if au>av else av))? # u-v> ?*max(|u|,|v|) #floating point comparison: u ? v(?) "essentially equal to" (definition 24) def fpc_eeq(u,v,eps=sys.float_info.epsilon): ??? au=abs(u) ??? av=abs(v) ??? return abs(v-u) <= (eps*(au if au > Whichever method you choose, there are gotchas to watch out for. > >> http://xkcd.com/1047/ > > Nice! > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From oscar.j.benjamin at gmail.com Thu May 30 18:57:28 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 30 May 2013 23:57:28 +0100 Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: On 30 May 2013 22:03, Carlos Nepomuceno wrote: >> Here's another way, mathematically equivalent (although not necessarily >> equivalent using floating point computations!) which avoids the divide-by- >> zero problem: >> >> abs(a - b) < epsilon*a > > That's wrong! If abs(a) < abs(a-b)/epsilon you will break the commutative law. There is no commutative law for relative tolerance floating point comparisons. If you want to compare with a relative tolerance then you you should choose carefully what your tolerance is to be relative to (and how big your relative tolerance should be). In some applications it's obvious which of a or b you should use to scale the tolerance but in others it is not or you should compare with something more complex. For an example where it is obvious, when testing numerical code I might write something like: eps = 1e-7 true_answer = 123.4567879 estimate = myfunc(5) assert abs(estimate - true_answer) < eps * abs(true_answer) Oscar From carlosnepomuceno at outlook.com Thu May 30 19:06:09 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 31 May 2013 02:06:09 +0300 Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com>, <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com>, <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com>, <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com>, <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com>, <51a6e6b8$0$11118$c3e8da3@news.astraweb.com>, , Message-ID: ---------------------------------------- > From: oscar.j.benjamin at gmail.com > Date: Thu, 30 May 2013 23:57:28 +0100 > Subject: Re: Short-circuit Logic > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > > On 30 May 2013 22:03, Carlos Nepomuceno wrote: >>> Here's another way, mathematically equivalent (although not necessarily >>> equivalent using floating point computations!) which avoids the divide-by- >>> zero problem: >>> >>> abs(a - b) < epsilon*a >> >> That's wrong! If abs(a) < abs(a-b)/epsilon you will break the commutative law. > > There is no commutative law for relative tolerance floating point > comparisons. If you want to compare with a relative tolerance then you > you should choose carefully what your tolerance is to be relative to > (and how big your relative tolerance should be). Off course there is! It might not suite your specific needs though. I'll just quote Knuth because it's pretty damn good: "A. An axiomatic approach. Although the associative law is not valid, the commutative law u (+) v == v (+) u???? (2) does hold, and this law can be a valuable conceptual asset in programming and in the analysis of programs. This example suggests that we should look for important laws that are satified by (+), (-), (*), and (/); it is not unreasonable to say that floating point routines should be designed to preserve as many of the ordinary mathematical laws as possible. If more axioms are valid, it becomes easier to write good programs, and programs also become more portable from machine to machine." TAOCP, Vol .2, p. 214 > In some applications it's obvious which of a or b you should use to > scale the tolerance but in others it is not or you should compare with > something more complex. For an example where it is obvious, when > testing numerical code I might write something like: > > eps = 1e-7 > true_answer = 123.4567879 > estimate = myfunc(5) > assert abs(estimate - true_answer) < eps * abs(true_answer) > > > Oscar From steve+comp.lang.python at pearwood.info Fri May 31 01:13:51 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 May 2013 05:13:51 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Message-ID: <51a8318e$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 31 May 2013 00:03:13 +0300, Carlos Nepomuceno wrote: > ---------------------------------------- >> From: steve+comp.lang.python at pearwood.info Subject: Re: Short-circuit >> Logic >> Date: Thu, 30 May 2013 05:42:17 +0000 To: python-list at python.org > [...] >> Here's another way, mathematically equivalent (although not necessarily >> equivalent using floating point computations!) which avoids the >> divide-by- zero problem: >> >> abs(a - b) < epsilon*a > > That's wrong! If abs(a) < abs(a-b)/epsilon you will break the > commutative law. For example: What makes you think that the commutative law is relevant here? Many things break the commutative law, starting with division and subtraction: 20 - 10 != 10 - 20 1/2 != 2/1 Most comparison operators other than equality and inequality: (23 < 42) != (42 < 23) String concatenation: "Hello" + "World" != "World" + "Hello" Many operations in the real world: put on socks, then shoes != put on shoes, then socks. But you are correct that approximately-equal using *relative* error is not commutative. (Absolute error, on the other hand, is commutative.) As I said, any form of "approximate equality" has gotchas. But this gotcha is simple to overcome: abs(a -b) < eps*max(abs(a), abs(b)) (Knuth's "approximately equal to" which you give.) > This discussion reminded me of TAOCP and I paid a visit and bring the > following functions: "TAOCP"? -- Steven From carlosnepomuceno at outlook.com Fri May 31 02:42:38 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 31 May 2013 09:42:38 +0300 Subject: Short-circuit Logic In-Reply-To: <51a8318e$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com>, <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com>, <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com>, <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com>, <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com>, <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com>, , <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com>, , <51a6e6b8$0$11118$c3e8da3@news.astraweb.com>, , <51a8318e$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: Short-circuit Logic > Date: Fri, 31 May 2013 05:13:51 +0000 > To: python-list at python.org > > On Fri, 31 May 2013 00:03:13 +0300, Carlos Nepomuceno wrote: > >> ---------------------------------------- >>> From: steve+comp.lang.python at pearwood.info Subject: Re: Short-circuit >>> Logic >>> Date: Thu, 30 May 2013 05:42:17 +0000 To: python-list at python.org >> [...] >>> Here's another way, mathematically equivalent (although not necessarily >>> equivalent using floating point computations!) which avoids the >>> divide-by- zero problem: >>> >>> abs(a - b) < epsilon*a >> >> That's wrong! If abs(a) < abs(a-b)/epsilon you will break the >> commutative law. For example: > > What makes you think that the commutative law is relevant here? How can't you see? I'll requote a previous message: }On Thu, 30 May 2013 13:45:13 +1000, Chris Angelico wrote: } }> Let's suppose someone is told to compare floating point numbers by }> seeing if the absolute value of the difference is less than some }> epsilon. } }Which is usually the wrong way to do it! Normally one would prefer }*relative* error, not absolute: ? Since we are considering Chris's supposition ("to compare floating point numbers") it's totally relevant to understand how that operation can be correctly implemented. > Many things break the commutative law, starting with division and > subtraction: > > 20 - 10 != 10 - 20 > > 1/2 != 2/1 > > Most comparison operators other than equality and inequality: > > (23 < 42) != (42 < 23) > > String concatenation: > > "Hello" + "World" != "World" + "Hello" > > Many operations in the real world: > > put on socks, then shoes != put on shoes, then socks. > That's is totally irrelevant in this case. The commutative law is essential to the equality operation. > But you are correct that approximately-equal using *relative* error is > not commutative. (Absolute error, on the other hand, is commutative.) As > I said, any form of "approximate equality" has gotchas. But this gotcha > is simple to overcome: > > abs(a -b) < eps*max(abs(a), abs(b)) > > (Knuth's "approximately equal to" which you give.) > > >> This discussion reminded me of TAOCP and I paid a visit and bring the >> following functions: > > "TAOCP"? The Art of Computer Programming[1]! An old book full of excellent stuff! A MUST READ!!!! ;) http://www-cs-faculty.stanford.edu/~uno/taocp.html [1] Knuth, Donald (1981). The Art of Computer Programming. 2nd ed. Vol. 2. p. 218. Addison-Wesley. ISBN 0-201-03822-6. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Fri May 31 03:09:01 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 17:09:01 +1000 Subject: Short-circuit Logic In-Reply-To: <51a8318e$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> <51a8318e$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, May 31, 2013 at 3:13 PM, Steven D'Aprano wrote: > What makes you think that the commutative law is relevant here? > Equality should be commutative. If a == b, then b == a. Also, it's generally understood that if a == c and b == c, then a == b, though there are more exceptions to that (especially in loosely-typed languages). ChrisA From steve+comp.lang.python at pearwood.info Fri May 31 04:11:30 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 May 2013 08:11:30 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> <51a8318e$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51a85b31$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 31 May 2013 09:42:38 +0300, Carlos Nepomuceno wrote: >> From: steve+comp.lang.python at pearwood.info Subject: Re: Short-circuit >> Logic >> Date: Fri, 31 May 2013 05:13:51 +0000 To: python-list at python.org >> >> On Fri, 31 May 2013 00:03:13 +0300, Carlos Nepomuceno wrote: >>>> From: steve+comp.lang.python at pearwood.info Subject: Re: Short-circuit >>>> Logic >>>> Date: Thu, 30 May 2013 05:42:17 +0000 To: python-list at python.org >>> [...] >>>> Here's another way, mathematically equivalent (although not >>>> necessarily equivalent using floating point computations!) which >>>> avoids the divide-by- zero problem: >>>> >>>> abs(a - b) < epsilon*a >>> >>> That's wrong! If abs(a) < abs(a-b)/epsilon you will break the >>> commutative law. For example: >> >> What makes you think that the commutative law is relevant here? > > How can't you see? I can ask the same thing about you. How can you see that it is not relevant? > I'll requote a previous message: Thanks, but that's entirely irrelevant. It says nothing about the commutative law. [...] > Since we are considering Chris's supposition ("to compare floating point > numbers") it's totally relevant to understand how that operation can be > correctly implemented. Of course! But what does that have to do with the commutative law? >> Many things break the commutative law, starting with division and >> subtraction: >> >> 20 - 10 != 10 - 20 >> >> 1/2 != 2/1 >> >> Most comparison operators other than equality and inequality: >> >> (23 < 42) != (42 < 23) [...] > That's is totally irrelevant in this case. The commutative law is > essential to the equality operation. That's fine, but we're not talking about equality, we're talking about *approximately equality* or *almost equal*. Given the simple definition of relative error under discussion, the commutative law does not hold. The mere fact that it does not hold is no big deal. It doesn't hold for many comparison operators. Nor does the transitive law hold, even using absolute epsilon: eps = 0.5 a = 1.1 b = 1.5 c = 1.9 then a ? b, and b ? c, but a ? c. -- Steven From steve+comp.lang.python at pearwood.info Fri May 31 04:45:13 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 May 2013 08:45:13 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> <51a8318e$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51a86319$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 31 May 2013 17:09:01 +1000, Chris Angelico wrote: > On Fri, May 31, 2013 at 3:13 PM, Steven D'Aprano > wrote: >> What makes you think that the commutative law is relevant here? >> >> > Equality should be commutative. If a == b, then b == a. Also, it's > generally understood that if a == c and b == c, then a == b, though > there are more exceptions to that (especially in loosely-typed > languages). Who is talking about equality? Did I just pass through the Looking Glass into Wonderland again? *wink* We're talking about *approximate equality*, which is not the same thing, despite the presence of the word "equality" in it. It is non-commutative, just like other comparisons like "less than" and "greater than or equal to". Nobody gets their knickers in a twist because the >= operator is non- commutative. Approximate equality is not just non-commutative, it's also intransitive. I'm reminded of a story about Ken Iverson, the creator of APL. Iverson was a strong proponent of what he called "tolerant equality", and APL defined the = operator as a relative approximate equal, rather than the more familiar exactly-equal operator most programming languages use. In an early talk Ken was explaining the advantages of tolerant comparison. A member of the audience asked incredulously, ?Surely you don?t mean that when A=B and B=C, A may not equal C?? Without skipping a beat, Ken replied, ?Any carpenter knows that!? and went on to the next question. ? Paul Berry The intransitivity of [tolerant] equality is well known in practical situations and can be easily demonstrated by sawing several pieces of wood of equal length. In one case, use the first piece to measure subsequent lengths; in the second case, use the last piece cut to measure the next. Compare the lengths of the two final pieces. ? Richard Lathwell, APL Comparison Tolerance, APL76, 1976 See also here: http://www.jsoftware.com/papers/APLEvol.htm (search for "fuzz" or "tolerance". -- Steven From roy at panix.com Fri May 31 09:20:02 2013 From: roy at panix.com (Roy Smith) Date: Fri, 31 May 2013 09:20:02 -0400 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> <51a8318e$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a86319$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <51a86319$0$29966$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > In an early talk Ken was explaining the advantages of tolerant > comparison. A member of the audience asked incredulously, > ???Surely you don???t mean that when A=B and B=C, A may not equal C???? > Without skipping a beat, Ken replied, ???Any carpenter knows that!??? > and went on to the next question. ??? Paul Berry Any any good carpenter also knows it's better to copy than to measure. Let's say I have a door frame and I need to trim a door to fit it exactly. I can do one of two things. First, I could take out my tape measure and measure that the frame is 29 and 11/32 inches wide. Then, carry that tape measure to the door, measure off 29 and 11/32 inches, and make a mark. Or, I could take a handy stick of wood which is 30-something inches long, lay it down at the bottom of the door frame with one end up snug against one side, and make a mark at the other side of the frame. Then carry my stick to the door and keep trimming until it's the same width as the marked section on the stick. Google for "story stick". The tape measure is like digital floating point. It introduces all sorts of ways for errors to creep in and people who care about getting doors to properly fit into door frames understand all that. From dihedral88888 at gmail.com Thu May 30 23:11:38 2013 From: dihedral88888 at gmail.com (88888 Dihedral) Date: Thu, 30 May 2013 20:11:38 -0700 (PDT) Subject: Short-circuit Logic In-Reply-To: <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <987fed2a-e7c7-4bcc-8aaf-9608e21eff26@googlegroups.com> Steven D'Aprano? 2013?5?30????UTC+8??10?28?57???? > On Wed, 29 May 2013 10:50:47 -0600, Ian Kelly wrote: > > > > > On Wed, May 29, 2013 at 8:33 AM, rusi wrote: > > >> 0.0 == 0.0 implies 5.4 == 5.4 > > >> is not a true statement is what (I think) Steven is saying. 0 (or if > > >> you prefer 0.0) is special and is treated specially. > > > > > > It has nothing to do with 0 being special. A floating point number will > > > always equal itself (except for nan, which is even more special), and in > > > particular 5.4 == 5.4. But if you have two different calculations that > > > produce 0, or two different calculations that produce 5.4, you might > > > actually get two different numbers that approximate 0 or 5.4 thanks to > > > rounding error. If you then compare those two ever-so-slightly > > > different numbers, you will find them unequal. > > > > EXACTLY! > > > > The problem does not lie with the *equality operator*, it lies with the > > calculations. And that is an intractable problem -- in general, floating > > point is *hard*. So the problem occurs when we start with a perfectly > > good statement of the facts: > > > > "If you naively test the results of a calculation for equality without > > understanding what you are doing, you will often get surprising results" > > > > which then turns into a general heuristic that is often, but not always, > > reasonable: > > > > "In general, you should test for floating point *approximate* equality, > > in some appropriate sense, rather than exact equality" > > > > which then gets mangled to: > > > > "Never test floating point numbers for equality" > > > > and then implemented badly by people who have no clue what they are doing > > and have misunderstood the nature of the problem, leading to either: > > > > * de facto exact equality testing, only slower and with the *illusion* of > > avoiding equality, e.g. "abs(x-y) < sys.float_info.epsilon" is just a > > long and slow way of saying "x == y" when both numbers are sufficiently > > large; > > > > * incorrectly accepting non-equal numbers as "equal" just because they > > happen to be "close". > > > > > > The problem is that there is *no one right answer*, except "have everyone > > become an expert in floating point, then judge every case on its merits", > > which will never happen. > > > > But if nothing else, I wish that we can get past the rank superstition > > that you should "never" test floats for equality. That would be a step > > forward. > > > > > > > > -- > > Steven The string used to represent a floating number in a computer language is normally in the decimal base of very some limited digits. Anyway with the advances of A/D-converters in the past 10 years which are reflected in the anttena- transmitter parts in phones, the long integer part in Python can really beat the low cost 32- 64 bit floating computations in scientific calculations. From steve+comp.lang.python at pearwood.info Thu May 30 01:10:49 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 05:10:49 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> Message-ID: <51a6df59$0$11118$c3e8da3@news.astraweb.com> On Wed, 29 May 2013 07:27:40 -0700, Ahmed Abdulshafy wrote: > On Tuesday, May 28, 2013 3:48:17 PM UTC+2, Steven D'Aprano wrote: >> On Mon, 27 May 2013 13:11:28 -0700, Ahmed Abdulshafy wrote: >> >> >> >> > That may be true for integers, but for floats, testing for equality >> > is >> >> > not always precise >> >> >> >> Incorrect. Testing for equality is always precise, and exact. The >> problem >> >> is not the *equality test*, but that you don't always have the number >> >> that you think you have. The problem lies elsewhere, not equality! >> >> >> Steven > > Well, this is taken from my python shell> > >>>> 0.33455857352426283 == 0.33455857352426282 > True This is an excellent example of misunderstanding what you are seeing. Both 0.33455857352426283 and 0.33455857352426282 represent the same float, so it is hardly a surprise that they compare equal -- they compare equal because they are equal. py> a, b = 0.33455857352426283, 0.33455857352426282 py> a.as_integer_ratio() (6026871468229899, 18014398509481984) py> b.as_integer_ratio() (6026871468229899, 18014398509481984) You've made a common error: neglecting to take into account the finite precision of floats. Floats are not mathematical "real numbers", with infinite precision. The error is more obvious if we exaggerate it: py> 0.3 == 0.300000000000000000000000000000000000000000000000000001 True Most people who have seen an ordinary four-function calculator will realise that the issue here is *not* that the equality operator == is wrongly stating that two unequal numbers are equal, but that just because you enter 0.300...00001 doesn't mean that all those decimal places are actually used. > Anyway, man, those were not my words anyway, most programming books I've > read state so. Here's an excerpt from the Python book, I'm currently > reading> > > ">>> 0.0, 5.4, -2.5, 8.9e-4 > (0.0, 5.4000000000000004, -2.5, 0.00088999999999999995) > > > The inexactness is not a problem specific to Python?all programming > languages have this problem with floating-point numbers." I'm not denying that floats are tricky to use correctly, or that testing for exact equality is *sometimes* the wrong thing to do: # Wrong, don't do this! x = 0.1 while x != 17.3: print(x) x += 0.1 I'm just saying that a simple minded comparison with sys.float_info.epsilon is *also* often wrong. -- Steven From rosuav at gmail.com Thu May 30 01:22:23 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 15:22:23 +1000 Subject: Short-circuit Logic In-Reply-To: <51a6df59$0$11118$c3e8da3@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> Message-ID: On Thu, May 30, 2013 at 3:10 PM, Steven D'Aprano wrote: > # Wrong, don't do this! > x = 0.1 > while x != 17.3: > print(x) > x += 0.1 > Actually, I wouldn't do that with integers either. There are too many ways that a subsequent edit could get it wrong and go infinite, so I'd *always* use an inequality for that: x = 1 while x < 173: print(x) x += 1 Well, in Python I'd use for/range, but the equivalent still applies. A range() is still based on an inequality: >>> list(range(1,6)) [1, 2, 3, 4, 5] >>> list(range(1,6,3)) [1, 4] Stops once it's no longer less than the end. That's safe, since Python can't do integer wraparound. ChrisA From roy at panix.com Thu May 30 08:40:16 2013 From: roy at panix.com (Roy Smith) Date: Thu, 30 May 2013 08:40:16 -0400 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> Message-ID: In article , Chris Angelico wrote: > On Thu, May 30, 2013 at 3:10 PM, Steven D'Aprano > wrote: > > # Wrong, don't do this! > > x = 0.1 > > while x != 17.3: > > print(x) > > x += 0.1 > > > > Actually, I wouldn't do that with integers either. There are too many > ways that a subsequent edit could get it wrong and go infinite, so I'd > *always* use an inequality for that: > > x = 1 > while x < 173: > print(x) > x += 1 There's a big difference between these two. In the first case, using less-than instead of testing for equality, you are protecting against known and expected floating point behavior. In the second case, you're protecting against some vague, unknown, speculative future programming botch. So, what *is* the right behavior if somebody were to accidentally drop three zeros into the source code: > x = 1000 > while x < 173: > print(x) > x += 1 should the loop just quietly not execute (which is what it will do here)? Will that make your program correct again, or will it simply turn this into a difficult to find bug? If you're really worried about that, why not: > x = 1 > while x != 173: > assert < 172 > print(x) > x += 1 From rosuav at gmail.com Thu May 30 08:58:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 22:58:27 +1000 Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> Message-ID: On Thu, May 30, 2013 at 10:40 PM, Roy Smith wrote: > if somebody were to accidentally drop three zeros into the source code: > >> x = 1000 >> while x < 173: >> print(x) >> x += 1 > > should the loop just quietly not execute (which is what it will do > here)? Will that make your program correct again, or will it simply > turn this into a difficult to find bug? If you're really worried about > that, why not: If you iterate from 1000 to 173, you get nowhere. This is the expected behaviour; this is what a C-style for loop would be written as, it's what range() does, it's the normal thing. Going from a particular starting point to a particular ending point that's earlier than the start results in no iterations. The alternative would be an infinite number of iterations, which is far far worse. ChrisA From ethan at stoneleaf.us Thu May 30 11:02:10 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 30 May 2013 08:02:10 -0700 Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> Message-ID: <51A769F2.1050702@stoneleaf.us> On 05/30/2013 05:58 AM, Chris Angelico wrote: > On Thu, May 30, 2013 at 10:40 PM, Roy Smith wrote: >> if somebody were to accidentally drop three zeros into the source code: >> >>> x = 1000 >>> while x < 173: >>> print(x) >>> x += 1 >> >> should the loop just quietly not execute (which is what it will do >> here)? Will that make your program correct again, or will it simply >> turn this into a difficult to find bug? If you're really worried about >> that, why not: > > If you iterate from 1000 to 173, you get nowhere. This is the expected > behaviour; this is what a C-style for loop would be written as, it's > what range() does, it's the normal thing. Going from a particular > starting point to a particular ending point that's earlier than the > start results in no iterations. The alternative would be an infinite > number of iterations, which is far far worse. If the bug is the extra three zeros (maybe it should have been two), then silently skipping the loop is the "far, far worse" scenario. With the infinite loop you at least know something went wrong, and you know it pretty darn quick (since you are testing, right? ;). -- ~Ethan~ From rosuav at gmail.com Thu May 30 11:56:09 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 01:56:09 +1000 Subject: Short-circuit Logic In-Reply-To: <51A769F2.1050702@stoneleaf.us> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <51A769F2.1050702@stoneleaf.us> Message-ID: On Fri, May 31, 2013 at 1:02 AM, Ethan Furman wrote: > On 05/30/2013 05:58 AM, Chris Angelico wrote: >> If you iterate from 1000 to 173, you get nowhere. This is the expected >> behaviour; this is what a C-style for loop would be written as, it's >> what range() does, it's the normal thing. Going from a particular >> starting point to a particular ending point that's earlier than the >> start results in no iterations. The alternative would be an infinite >> number of iterations, which is far far worse. > > If the bug is the extra three zeros (maybe it should have been two), then > silently skipping the loop is the "far, far worse" scenario. With the > infinite loop you at least know something went wrong, and you know it pretty > darn quick (since you are testing, right? ;). You're assuming you can casually hit Ctrl-C to stop an infinite loop, meaning that it's trivial. It's not. Not everything lets you do that; or possibly halting the process will halt far more than you intended. What if you're editing live code in something that's had uninterrupted uptime for over a year? Doing nothing is much safer than getting stuck in an infinite loop. And yes, I have done exactly that, though not in Python. Don't forget, your start/stop figures mightn't be constants, so you might not see it in testing. I can't imagine ANY scenario where you'd actually *want* the infinite loop behaviour, while there are plenty where you want it to skip the loop, and would otherwise have to guard it with an if. ChrisA From ethan at stoneleaf.us Thu May 30 12:30:11 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 30 May 2013 09:30:11 -0700 Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <51A769F2.1050702@stoneleaf.us> Message-ID: <51A77E93.9050909@stoneleaf.us> On 05/30/2013 08:56 AM, Chris Angelico wrote: > On Fri, May 31, 2013 at 1:02 AM, Ethan Furman wrote: >> On 05/30/2013 05:58 AM, Chris Angelico wrote: >>> If you iterate from 1000 to 173, you get nowhere. This is the expected >>> behaviour; this is what a C-style for loop would be written as, it's >>> what range() does, it's the normal thing. Going from a particular >>> starting point to a particular ending point that's earlier than the >>> start results in no iterations. The alternative would be an infinite >>> number of iterations, which is far far worse. >> >> If the bug is the extra three zeros (maybe it should have been two), then >> silently skipping the loop is the "far, far worse" scenario. With the >> infinite loop you at least know something went wrong, and you know it pretty >> darn quick (since you are testing, right? ;). > > You're assuming you can casually hit Ctrl-C to stop an infinite loop, > meaning that it's trivial. It's not. Not everything lets you do that; > or possibly halting the process will halt far more than you intended. > What if you're editing live code in something that's had uninterrupted > uptime for over a year? Doing nothing is much safer than getting stuck > in an infinite loop. And yes, I have done exactly that, though not in > Python. Don't forget, your start/stop figures mightn't be constants, > so you might not see it in testing. I can't imagine ANY scenario where > you'd actually *want* the infinite loop behaviour, while there are > plenty where you want it to skip the loop, and would otherwise have to > guard it with an if. We're not talking about skipping the loop on purpose, but on accident. Sure, taking a system down is no fun -- on the other hand, how much data corruption can occur before somebody realises there's a problem, and then how long to track it down to a silently, accidently, skipped loop? -- ~Ethan~ From steve+comp.lang.python at pearwood.info Thu May 30 12:40:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 16:40:52 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <51A769F2.1050702@stoneleaf.us> Message-ID: <51a78113$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 31 May 2013 01:56:09 +1000, Chris Angelico wrote: > On Fri, May 31, 2013 at 1:02 AM, Ethan Furman > wrote: >> On 05/30/2013 05:58 AM, Chris Angelico wrote: >>> If you iterate from 1000 to 173, you get nowhere. This is the expected >>> behaviour; this is what a C-style for loop would be written as, it's >>> what range() does, it's the normal thing. Going from a particular >>> starting point to a particular ending point that's earlier than the >>> start results in no iterations. The alternative would be an infinite >>> number of iterations, which is far far worse. >> >> If the bug is the extra three zeros (maybe it should have been two), >> then silently skipping the loop is the "far, far worse" scenario. With >> the infinite loop you at least know something went wrong, and you know >> it pretty darn quick (since you are testing, right? ;). > > You're assuming you can casually hit Ctrl-C to stop an infinite loop, > meaning that it's trivial. It's not. Not everything lets you do that; or > possibly halting the process will halt far more than you intended. What > if you're editing live code in something that's had uninterrupted uptime > for over a year? Then more fool you for editing live code. By the way, this is Python. Editing live code is not easy, if it's possible at all. But even when possible, it's certainly not sensible. You don't insist on your car mechanic giving your car a grease and oil change while you're driving at 100kmh down the freeway, and you shouldn't insist that your developers modify your code while it runs. In any case, your arguing about such abstract, hypothetical ideas that, frankly, *anything at all* might be said about it. "What if Ctrl-C causes some great disaster?" can be answered with an equally hypothetical "What if Ctrl-C prevents some great disaster?" > Doing nothing is much safer than getting stuck in an > infinite loop. I disagree. And I agree. It all depends on the circumstances. But, given that we are talking about Python where infinite loops can be trivially broken out of, *in my experience* they are less-worse than silently doing nothing. I've occasionally written faulty code that enters an infinite loop. When that happens, it's normally pretty obvious: something which should complete in a millisecond is still running after ten minutes. That's a clear, obvious, *immediate* sign that I've screwed up, which leads to me fixing the problem. On the other hand, I've occasionally written faulty code that does nothing at all. The specific incident I am thinking of, I wrote a bunch of doctests which *weren't being run at all*. For nearly two weeks (not full time, but elapsed time) I was developing this code, before I started to get suspicious that *none* of the tests had failed, not even once. I mean, I'm not that good a programmer. Eventually I put in some deliberate errors, and they still didn't fail. In actuality, nearly every test was failing, my entire code base was rubbish, and I just didn't know it. So, in this specific case, I would have *much* preferred an obvious failure (such as an infinite loop) than code that silently does the wrong thing. We've drifted far from the original topic. There is a distinct difference between guarding against inaccuracies in floating point calculations: # Don't do this! total = 0.0 while total != 1.0: total += 0.1 and guarding against typos in source code: total = 90 # Oops, I meant 0 while total != 10: total += 1 The second case is avoidable by paying attention when you code. The first case is not easily avoidable, because it reflects a fundamental difficulty with floating point types. As a general rule, "defensive coding" does not extend to the idea of defending against mistakes in your code. The compiler, linter or unit tests are supposed to do that. Occasionally, I will code defensively when initialising tedious data sets: prefixes = ['y', 'z', 'a', 'f', 'p', 'n', '?', 'm', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'] assert len(prefixes) == 16 but that's about as far as I go. -- Steven From steve+comp.lang.python at pearwood.info Thu May 30 15:22:46 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 19:22:46 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <51A769F2.1050702@stoneleaf.us> <51a78113$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51a7a706$0$29966$c3e8da3$5496439d@news.astraweb.com> On Thu, 30 May 2013 16:40:52 +0000, Steven D'Aprano wrote: > On Fri, 31 May 2013 01:56:09 +1000, Chris Angelico wrote: >> You're assuming you can casually hit Ctrl-C to stop an infinite loop, >> meaning that it's trivial. It's not. Not everything lets you do that; >> or possibly halting the process will halt far more than you intended. >> What if you're editing live code in something that's had uninterrupted >> uptime for over a year? > > Then more fool you for editing live code. Ouch! That came out much harsher than it sounded in my head :( Sorry Chris, that wasn't intended as a personal attack against you, just as a comment on the general inadvisability of modifying code on the fly while it is being run. -- Steven From rosuav at gmail.com Thu May 30 17:46:04 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 07:46:04 +1000 Subject: Short-circuit Logic In-Reply-To: <51a7a706$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <51A769F2.1050702@stoneleaf.us> <51a78113$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a7a706$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, May 31, 2013 at 5:22 AM, Steven D'Aprano wrote: > On Thu, 30 May 2013 16:40:52 +0000, Steven D'Aprano wrote: > >> On Fri, 31 May 2013 01:56:09 +1000, Chris Angelico wrote: > >>> You're assuming you can casually hit Ctrl-C to stop an infinite loop, >>> meaning that it's trivial. It's not. Not everything lets you do that; >>> or possibly halting the process will halt far more than you intended. >>> What if you're editing live code in something that's had uninterrupted >>> uptime for over a year? >> >> Then more fool you for editing live code. > > Ouch! That came out much harsher than it sounded in my head :( > > Sorry Chris, that wasn't intended as a personal attack against you, just > as a comment on the general inadvisability of modifying code on the fly > while it is being run. Apology accepted :) You're right that, in theory, a staging area is a Good Thing. But it's not always feasible. At work, we have a lot of Pike code that really does keep running indefinitely (okay, we have yet to get anywhere near a year's uptime for administrative reasons, but it'll be plausible once we go live; the >1year figure came from one of my personal projects). While all's going well, code changes follow a sane progression: dev -> alpha -> beta -> live with testing at every stage. What happens when we get a problem, though? Maybe some process is leaking resources, maybe we come under some kind of crazy DOS attack, whatever. We need a solution, and we need to not break things for the currently-connected clients. That means editing the live code. Of course, there are *some* protections; the new code won't be switched in unless it passes the compilation phase (think "except ImportError: keep_existing_code", kinda), and hopefully I would at least spin it up on my dev box before pushing it to live, but even so, there's every possibility that there'll be a specific case that I didn't think of - remembering that we're not talking about iteration from constant to constant, but from variable to constant or constant to variable or variable to variable. That's why I would prefer, in language design, for a 'failed loop' to result in no iterations than an infinite number of them. The infinite loop might be easily caught on my dev test - but only if I pass the code through that exact code path. But to go back to your point about editing live code: You backed down from the implication that it's *foolish*, but I would maintain it at a weaker level. Editing code in a running process is a *rare* thing to do. MOST programming is not done that way. It's like the old joke about the car mechanic and the heart surgeon (see eg http://www.medindia.net/jokes/viewjokes.asp?hid=200 if you haven't heard it, and I will be spoiling the punch line in the next line or so); most programmers are mechanics, shutting down the system to do any work on it, but very occasionally there are times when you need to do it with the engine running. It's like C compilers. Most of us never write them, but a few people (relatively) actually need to drop to the uber-low-level coding and think about how it all works in assembly language. For everyone else, thinking about machine code is an utter waste of time/effort, but that doesn't mean that it's folly for a compiler writer. Does that make sense? ChrisA From rustompmody at gmail.com Thu May 30 12:58:44 2013 From: rustompmody at gmail.com (rusi) Date: Thu, 30 May 2013 09:58:44 -0700 (PDT) Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> Message-ID: <578a8e7f-4358-40ba-bcf8-d3a81b37c0d2@vy4g2000pbc.googlegroups.com> On May 30, 5:58?pm, Chris Angelico wrote: > The alternative would be an infinite number of iterations, which is far far worse. There was one heavyweight among programming teachers -- E.W. Dijkstra -- who had some rather extreme views on this. He taught that when writing a loop of the form i = 0 while i < n: some code i += 1 one should write the loop test as i != n rather than i < n, precisely because if i got erroneously initialized to some value greater than n, (and thereby broke the loop invariant), it would loop infinitely rather than stop with a wrong result. From rosuav at gmail.com Thu May 30 13:23:11 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 03:23:11 +1000 Subject: Short-circuit Logic In-Reply-To: <578a8e7f-4358-40ba-bcf8-d3a81b37c0d2@vy4g2000pbc.googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <578a8e7f-4358-40ba-bcf8-d3a81b37c0d2@vy4g2000pbc.googlegroups.com> Message-ID: On Fri, May 31, 2013 at 2:58 AM, rusi wrote: > On May 30, 5:58 pm, Chris Angelico wrote: >> The alternative would be an infinite number of iterations, which is far far worse. > > There was one heavyweight among programming teachers -- E.W. Dijkstra > -- who had some rather extreme views on this. > > He taught that when writing a loop of the form > > i = 0 > while i < n: > some code > i += 1 > > one should write the loop test as i != n rather than i < n, precisely > because if i got erroneously initialized to some value greater than n, > (and thereby broke the loop invariant), it would loop infinitely > rather than stop with a wrong result. And do you agree or disagree with him? :) I disagree with Dijkstra on a number of points, and this might be one of them. When you consider that the obvious Pythonic version of that code: for i in range(n,m): some code loops over nothing and does not go into an infinite loop (or throw an exception) when n >= m, you have to at least acknowledge that I'm in agreement with Python core code here :) That doesn't mean it's right, of course, but it's at least a viewpoint that someone has seen fit to enshrine in important core functionality. ChrisA From rantingrickjohnson at gmail.com Thu May 30 20:13:25 2013 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Thu, 30 May 2013 17:13:25 -0700 (PDT) Subject: Short-circuit Logic In-Reply-To: <578a8e7f-4358-40ba-bcf8-d3a81b37c0d2@vy4g2000pbc.googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <578a8e7f-4358-40ba-bcf8-d3a81b37c0d2@vy4g2000pbc.googlegroups.com> Message-ID: <9e4d113b-23ec-4eb3-836a-fc80c505c846@googlegroups.com> > On Fri, May 31, 2013 at 2:58 AM, rusi wrote: > > On May 30, 5:58 pm, Chris Angelico wrote: > > > The alternative would be an infinite number of iterations, which is far far worse. > > > > There was one heavyweight among programming teachers -- E.W. Dijkstra > > -- who had some rather extreme views on this. > > > > He taught that when writing a loop of the form > > > > i = 0 > > while i < n: > > some code > > i += 1 > > > > one should write the loop test as i != n rather than i < > > n, precisely because if i got erroneously initialized to > > some value greater than n, (and thereby broke the loop > > invariant), it would loop infinitely rather than stop > > with a wrong result. > > > > And do you agree or disagree with him? :) I disagree with > Dijkstra on a number of points, and this might be one of > them. When you consider that the obvious Pythonic version > of that code: > > for i in range(n,m): > some code Maybe from your limited view point. What if you need to perform operations on a sequence (more than once) in a non-linear fashion? What if you need to modify the sequence whilst looping? In many cases your simplistic "for loop" will fail miserably. py> lst = range(5) py> for n in lst: ... print lst.pop() 4 3 2 Oops, can't do that with a for loop! py> lst = range(5) py> while len(lst): ... print lst.pop() 4 3 2 1 0 From rosuav at gmail.com Thu May 30 22:29:26 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 12:29:26 +1000 Subject: Short-circuit Logic In-Reply-To: <9e4d113b-23ec-4eb3-836a-fc80c505c846@googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <578a8e7f-4358-40ba-bcf8-d3a81b37c0d2@vy4g2000pbc.googlegroups.com> <9e4d113b-23ec-4eb3-836a-fc80c505c846@googlegroups.com> Message-ID: On Fri, May 31, 2013 at 10:13 AM, Rick Johnson wrote: > What if you need to perform operations on a sequence (more than once) in a non-linear fashion? What if you need to modify the sequence whilst looping? In many cases your simplistic "for loop" will fail miserably. What has this to do with the original question of iterating across integers? What you're now saying is that both the meaning of the current index and the top boundary can change during iteration; that's unrelated to whether to use equality or inequality for comparisons. Oh wait. Rick's back. He's been away so long that I stopped looking for his name in the headers. ChrisA From neilc at norwich.edu Thu May 30 15:30:41 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 30 May 2013 19:30:41 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> Message-ID: On 2013-05-30, Chris Angelico wrote: > On Thu, May 30, 2013 at 3:10 PM, Steven D'Aprano > wrote: >> # Wrong, don't do this! >> x = 0.1 >> while x != 17.3: >> print(x) >> x += 0.1 > > Actually, I wouldn't do that with integers either. I propose borrowing the concept of significant digits from the world of Physics. The above has at least three significant digits. With that scheme x would approximately equal 17.3 when 17.25 <= x < 17.35. But I don't see immediately how to calculate 17.25 and 17.35 from 17.3, 00.1 and 3 significant digits. -- Neil Cerutti From ian.g.kelly at gmail.com Thu May 30 15:49:43 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 30 May 2013 13:49:43 -0600 Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> Message-ID: On Thu, May 30, 2013 at 1:30 PM, Neil Cerutti wrote: > On 2013-05-30, Chris Angelico wrote: >> On Thu, May 30, 2013 at 3:10 PM, Steven D'Aprano >> wrote: >>> # Wrong, don't do this! >>> x = 0.1 >>> while x != 17.3: >>> print(x) >>> x += 0.1 >> >> Actually, I wouldn't do that with integers either. > > I propose borrowing the concept of significant digits from the > world of Physics. > > The above has at least three significant digits. With that scheme > x would approximately equal 17.3 when 17.25 <= x < 17.35. > > But I don't see immediately how to calculate 17.25 and 17.35 from > 17.3, 00.1 and 3 significant digits. How about this: while round(x, 1) != round(17.3, 1): pass The second round call may be unnecessary. I would expect the parser to ensure that round(17.3, 1) == 17.3, but I'm not certain that is the case. From tjreedy at udel.edu Sun May 26 16:19:17 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 26 May 2013 16:19:17 -0400 Subject: Short-circuit Logic In-Reply-To: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote: > if not allow_zero and abs(x) < sys.float_info.epsilon: > print("zero is not allowed") The reason for the order is to do the easy calculation first and the harder one only if the first passes. From roy at panix.com Sun May 26 16:22:26 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 16:22:26 -0400 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: In article , Terry Jan Reedy wrote: > On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote: > > > if not allow_zero and abs(x) < sys.float_info.epsilon: > > print("zero is not allowed") > > The reason for the order is to do the easy calculation first and the > harder one only if the first passes. This is a particularly egregious case of premature optimization. You're worried about how long it takes to execute abs(x)? That's silly. From roy at panix.com Sun May 26 16:22:26 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 16:22:26 -0400 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: In article , Terry Jan Reedy wrote: > On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote: > > > if not allow_zero and abs(x) < sys.float_info.epsilon: > > print("zero is not allowed") > > The reason for the order is to do the easy calculation first and the > harder one only if the first passes. This is a particularly egregious case of premature optimization. You're worried about how long it takes to execute abs(x)? That's silly. From tjreedy at udel.edu Sun May 26 17:28:56 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 26 May 2013 17:28:56 -0400 Subject: Short-circuit Logic In-Reply-To: References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: On 5/26/2013 4:22 PM, Roy Smith wrote: > In article , > Terry Jan Reedy wrote: > >> On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote: >> >>> if not allow_zero and abs(x) < sys.float_info.epsilon: >>> print("zero is not allowed") >> >> The reason for the order is to do the easy calculation first and the >> harder one only if the first passes. > > This is a particularly egregious case of premature optimization. You're > worried about how long it takes to execute abs(x)? That's silly. This is a particularly egregious case of premature response. You're ignoring an extra name lookup and two extra attribute lookups. That's silly. That's beside the fact that one *must* choose, so any difference is a reason to act rather than being frozen like Buridan's ass. http://en.wikipedia.org/wiki/Buridan%27s_ass If you wish, replace 'The reason' with 'A reason'. I also the logical flow as better with the order given. From steve+comp.lang.python at pearwood.info Sun May 26 20:40:07 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 May 2013 00:40:07 GMT Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: <51a2ab67$0$30002$c3e8da3$5496439d@news.astraweb.com> On Sun, 26 May 2013 16:22:26 -0400, Roy Smith wrote: > In article , > Terry Jan Reedy wrote: > >> On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote: >> >> > if not allow_zero and abs(x) < sys.float_info.epsilon: >> > print("zero is not allowed") >> >> The reason for the order is to do the easy calculation first and the >> harder one only if the first passes. > > This is a particularly egregious case of premature optimization. You're > worried about how long it takes to execute abs(x)? That's silly. I don't think it's a matter of premature optimization so much as the general principle "run code only if it needs to run". Hence, first you check the flag to decide whether or not you care whether x is near zero, and *only if you care* do you then check whether x is near zero. # This is silly: if x is near zero: if we care: handle near zero condition() # This is better: if we care: if x is near zero handle near zero condition() Not only is this easier to understand because it matches how we do things in the real life, but it has the benefit that if the "near zero" condition ever changes to become much more expensive, you don't have to worry about reordering the tests because they're already in the right order. -- Steven From cs at zip.com.au Sun May 26 21:57:54 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 27 May 2013 11:57:54 +1000 Subject: Short-circuit Logic In-Reply-To: <51a2ab67$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <51a2ab67$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20130527015754.GA81324@cskk.homeip.net> On 27May2013 00:40, Steven D'Aprano wrote: | On Sun, 26 May 2013 16:22:26 -0400, Roy Smith wrote: | | > In article , | > Terry Jan Reedy wrote: | > | >> On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote: | >> | >> > if not allow_zero and abs(x) < sys.float_info.epsilon: | >> > print("zero is not allowed") | >> | >> The reason for the order is to do the easy calculation first and the | >> harder one only if the first passes. | > | > This is a particularly egregious case of premature optimization. You're | > worried about how long it takes to execute abs(x)? That's silly. | | I don't think it's a matter of premature optimization so much as the | general principle "run code only if it needs to run". Hence, first you | check the flag to decide whether or not you care whether x is near zero, | and *only if you care* do you then check whether x is near zero. | | # This is silly: | if x is near zero: | if we care: | handle near zero condition() | | # This is better: | if we care: | if x is near zero | handle near zero condition() | | | Not only is this easier to understand because it matches how we do things | in the real life, but it has the benefit that if the "near zero" | condition ever changes to become much more expensive, you don't have to | worry about reordering the tests because they're already in the right | order. I wouldn't even go that far, though nothing you say above is wrong. Terry's assertion "The reason for the order is to do the easy calculation first and the harder one only if the first passes" is only sometimes that case, though well worth considering if the second test _is_ expensive. There are other reasons also. The first is of course your response, that if the first test fails there's no need to even bother with the second one. Faster, for free! The second is that sometimes the first test is a guard against even being able to perform the second test. Example: if s is not None and len(s) > 0: ... do something with the non-empty string `s` ... In this example, None is a sentinel value for "no valid string" and calling "len(s)" would raise an exception because None doesn't have a length. With short circuiting logic you can write this clearly and intuitively in one line without extra control structure like the nested ifs above. Cheers, -- Cameron Simpson Who are all you people and why are you in my computer? - Kibo From vito.detullio at gmail.com Mon May 27 00:59:13 2013 From: vito.detullio at gmail.com (Vito De Tullio) Date: Mon, 27 May 2013 06:59:13 +0200 Subject: Short-circuit Logic References: <51a2ab67$0$30002$c3e8da3$5496439d@news.astraweb.com> <20130527015754.GA81324@cskk.homeip.net> Message-ID: Cameron Simpson wrote: > if s is not None and len(s) > 0: > ... do something with the non-empty string `s` ... > > In this example, None is a sentinel value for "no valid string" and > calling "len(s)" would raise an exception because None doesn't have > a length. obviously in this case an `if s: ...` is more than sufficient :P -- ZeD From cs at zip.com.au Mon May 27 02:13:20 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 27 May 2013 16:13:20 +1000 Subject: Short-circuit Logic In-Reply-To: References: Message-ID: <20130527061320.GA44368@cskk.homeip.net> On 27May2013 06:59, Vito De Tullio wrote: | Cameron Simpson wrote: | > if s is not None and len(s) > 0: | > ... do something with the non-empty string `s` ... | > | > In this example, None is a sentinel value for "no valid string" and | > calling "len(s)" would raise an exception because None doesn't have | > a length. | | obviously in this case an `if s: ...` is more than sufficient :P :P My fault for picking too similar a test. Cheers, -- Cameron Simpson Death is life's way of telling you you've been fired. - R. Geis From rustompmody at gmail.com Mon May 27 00:44:30 2013 From: rustompmody at gmail.com (rusi) Date: Sun, 26 May 2013 21:44:30 -0700 (PDT) Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a2ab67$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <80c667ec-429d-469d-b69f-1b3effc4a0ef@zo5g2000pbb.googlegroups.com> On May 27, 5:40?am, Steven D'Aprano wrote: > On Sun, 26 May 2013 16:22:26 -0400, Roy Smith wrote: > > In article , > > ?Terry Jan Reedy wrote: > > >> On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote: > > >> > ? ? ? if not allow_zero and abs(x) < sys.float_info.epsilon: > >> > ? ? ? ? ? ? ? ? ?print("zero is not allowed") > > >> The reason for the order is to do the easy calculation first and the > >> harder one only if the first passes. > > > This is a particularly egregious case of premature optimization. ?You're > > worried about how long it takes to execute abs(x)? ?That's silly. > > I don't think it's a matter of premature optimization so much as the > general principle "run code only if it needs to run". Hence, first you > check the flag to decide whether or not you care whether x is near zero, > and *only if you care* do you then check whether x is near zero. > > # This is silly: > if x is near zero: > ? ? if we care: > ? ? ? ? handle near zero condition() > > # This is better: > if we care: > ? ? if x is near zero > ? ? ? ? handle near zero condition() > > Not only is this easier to understand because it matches how we do things > in the real life, but it has the benefit that if the "near zero" > condition ever changes to become much more expensive, you don't have to > worry about reordering the tests because they're already in the right > order. > > -- > Steven Three points: 3. These arguments are based on a certain assumption: that the inputs are evenly distributed statistically. If however that is not so, ie say: "We-care" is mostly true and "x-is-near-zero" is more often false then doing the near-zero test first would be advantageous Well thats the 3rd point... 2. Nikalus Wirth deliberately did not use short-circuit boolean operators in his languages because he found these kind of distinctions to deteriorate into irrelevance and miss out the more crucial questions of correctness 1. As Roy pointed out in his initial response to the OP: "I dont understand your confusion... None of applies to your example" its not at all clear to me that anything being said has anything to do with what the OP asked! From nobody at nowhere.com Mon May 27 13:52:15 2013 From: nobody at nowhere.com (Nobody) Date: Mon, 27 May 2013 18:52:15 +0100 Subject: Short-circuit Logic References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: On Sun, 26 May 2013 04:11:56 -0700, Ahmed Abdulshafy wrote: > I'm having a hard time wrapping my head around short-circuit logic that's > used by Python, coming from a C/C++ background; so I don't understand why > the following condition is written this way!> > > if not allow_zero and abs(x) < sys.float_info.epsilon: > print("zero is not allowed") > > The purpose of this snippet is to print the given line when allow_zero is > False and x is 0. I don't understand your confusion. The above is directly equivalent to the following C code: if (!allow_zero && fabs(x) < DBL_EPSILON) printf("zero is not allowed\n"); In either case, the use of short-circuit evaluation isn't necessary here; it would work just as well with a strict[1] "and" operator. Short-circuit evaluation is useful if the second argument is expensive to compute, or (more significantly) if the second argument should not be evaluated if the first argument is false; e.g. if x is a pointer then: if (x && *x) ... relies upon short-circuit evaluation to avoid dereferencing a null pointer. On an unrelated note: the use of the "epsilon" value here is almost certainly wrong. If the intention is to determine if the result of a calculation is zero to within the limits of floating-point accuracy, then it should use a value which is proportional to the values used in the calculation. From abdulshafy at gmail.com Mon May 27 16:08:34 2013 From: abdulshafy at gmail.com (Ahmed Abdulshafy) Date: Mon, 27 May 2013 13:08:34 -0700 (PDT) Subject: Short-circuit Logic In-Reply-To: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> Message-ID: On Sunday, May 26, 2013 1:11:56 PM UTC+2, Ahmed Abdulshafy wrote: > Hi, > > I'm having a hard time wrapping my head around short-circuit logic that's used by Python, coming from a C/C++ background; so I don't understand why the following condition is written this way!> > > > > if not allow_zero and abs(x) < sys.float_info.epsilon: > > print("zero is not allowed") > > > > The purpose of this snippet is to print the given line when allow_zero is False and x is 0. Thank you guys! you gave me valuable insights! But regarding my original post, I don't know why for the past two days I was looking at the code *only* this way> if ( not allow_zero and abs(x) ) < sys.float_info.epsilon: I feel so stupid now :-/, may be it's the new syntax confusing me :)! Thanks again guys. From nikos.gr33k at gmail.com Sun May 26 07:11:56 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 04:11:56 -0700 (PDT) Subject: Error when trying to sort and presnt a dictionary in python 3.3.1 Message-ID: <95e674bf-0d96-479c-b7a4-9242768a7a46@googlegroups.com> python3 pelatologio.py gives me error in this line: Traceback (most recent call last): File "pelatologio.py", line 283, in <module> ''' % (months[key], key) ) KeyError: 1 The code is: #populating months into a dropdown menu years = ( 2010, 2011, 2012, 2013 ) months = { '??????????':1, '???????????':2, '???????':3, '????????':4, '?????':5, '???????':6, \ '???????':7, '?????????':8, '???????????':9, '?????????':10, '?????????':11, '??????????':12 } print('''
> ''') > > for key in sorted( months.values() ): and calling the value key doesn't help there. > print(''' > > ''' % (months[key], key) ) At some point you have to admit that coding isn't your cup of tea. Or Ouzo ;( From nikos.gr33k at gmail.com Sun May 26 08:58:12 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 05:58:12 -0700 (PDT) Subject: Error when trying to sort and presnt a dictionary in python 3.3.1 In-Reply-To: References: <95e674bf-0d96-479c-b7a4-9242768a7a46@googlegroups.com> Message-ID: <9ea1331c-2c23-43db-a4d0-68f6cdd5f321@googlegroups.com> ?? ???????, 26 ????? 2013 3:20:19 ?.?. UTC+3, ? ??????? Peter Otten ??????: > At some point you have to admit that coding isn't your cup of tea. > Or Ouzo ;( And i didn't evne drank anyhting, iam sober! Imagine what i would have written if i had some shots of Ouzo :-) I chnage my code to: months = { '??????????':1, '???????????':2, '???????':3, '????????':4, '?????':5, '???????':6, \ '???????':7, '?????????':8, '???????????':9, '?????????':10, '?????????':11, '??????????':12 } print(''' ''' Now, iam not getting any eroor from cmd when i 'python3 pelatologio.py' verythign interprets normally wiyohut an error. BUT i kepp getting an internal server error when i try to run the script via a browser (Chrome). python3 path is ok uploaded as ascii chmoded to 755 ok. How in cmd i get no error while on browser i get internal server error? From nikos.gr33k at gmail.com Sun May 26 12:09:11 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 09:09:11 -0700 (PDT) Subject: Error when trying to sort and presnt a dictionary in python 3.3.1 In-Reply-To: <9ea1331c-2c23-43db-a4d0-68f6cdd5f321@googlegroups.com> References: <95e674bf-0d96-479c-b7a4-9242768a7a46@googlegroups.com> <9ea1331c-2c23-43db-a4d0-68f6cdd5f321@googlegroups.com> Message-ID: ?? ???????, 26 ????? 2013 3:58:12 ?.?. UTC+3, ? ??????? ????? ???33? ??????: > ?? ???????, 26 ????? 2013 3:20:19 ?.?. UTC+3, ? ??????? Peter Otten ??????: > > > > > At some point you have to admit that coding isn't your cup of tea. > > > Or Ouzo ;( > > > > And i didn't evne drank anyhting, iam sober! Imagine what i would have written if i had some shots of Ouzo :-) > > > > I chnage my code to: > > > > months = { '??????????':1, '???????????':2, '???????':3, '????????':4, '?????':5, '???????':6, \ > > '???????':7, '?????????':8, '???????????':9, '?????????':10, '?????????':11, '??????????':12 } > > > > print(''' > > > > ''' > > > > > > Now, iam not getting any eroor from cmd when i 'python3 pelatologio.py' verythign interprets normally wiyohut an error. > > > > BUT i kepp getting an internal server error when i try to run the script via a browser (Chrome). > > > > python3 path is ok > > uploaded as ascii > > chmoded to 755 ok. > > > > How in cmd i get no error while on browser i get internal server error? Its okey i made it work, it wasnt an actuap python issue but an .htaccess and .htpasswd problem. All fine now! From peter.h.m.brooks at gmail.com Sun May 26 07:49:57 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Sun, 26 May 2013 04:49:57 -0700 (PDT) Subject: Solving the problem of mutual recursion Message-ID: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> I'm not sure if this'll interest anybody, but I expect that I'm going to get some mutual recursion in my simulation, so I needed to see how python handled it. Unfortunately, it falls over once it detects a certain level of recursion. This is reasonable as, otherwise, the stack eventually over-fills. Mostly the recommendation, when this happens, is to re-write the code as loops. Of course, that's not as satisfactory as using recursion. The problem really is that the methods or functions called recursively never exit because they're waiting for the return of the function they've called. Now, if your recursion relies upon knowing what's returned, then this solution won't help you. Often, though, you're not interested in what's returned and would just like the routine to exit once it's called itself, or another process, recursively. If that's the case, this solution, using threads, allows you to have functions call themselves, or each other, indefinitely. It works OK on a macbook pro and the thread count varies from 2-3, so it handles the thread queuing well. I'm not sure how well this will work on other architecture - it'd be interesting to know if anybody tries it. #!/usr/bin/python # Two functions, Jim and Fred, call each other recursively and indefinitely, while the main program continues to execute as well import threading, time def jim(arg,count): print "Running jim:", arg,count," Thread Count ",threading.active_count() thread = threading.Thread(target=fred, args=(" From Jim ",count +1)) thread.start() print count," Jim complete. Thread Count",threading.active_count() def fred(arg,counter): print "Running fred:", arg,counter thread = threading.Thread(target=jim, args=(" From Fred ",counter+1)) thread.start() print counter,"Fred complete",threading.currentThread() thread = threading.Thread(target=jim, args=(" From Main",0)) thread.start() print "Jim run from main" count = 0 while True: count += 1 print 'In main program',count From jpiitula at ling.helsinki.fi Sun May 26 11:09:27 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 26 May 2013 18:09:27 +0300 Subject: Solving the problem of mutual recursion References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> Message-ID: Peter Brooks writes: > I'm not sure if this'll interest anybody, but I expect that I'm > going to get some mutual recursion in my simulation, so I needed to ... > returned, then this solution won't help you. Often, though, you're > not interested in what's returned and would just like the routine to > exit once it's called itself, or another process, recursively. > > If that's the case, this solution, using threads, allows you to have > functions call themselves, or each other, indefinitely. It works OK > on a macbook pro and the thread count varies from 2-3, so it handles ... hard to resist ... but somehow I manage ... whew ... A light-weighter way is to have each task end by assigning the next task and returning, instead of calling the next task directly. When a task returns, a driver loop will call the assigned task, which again does a bounded amount of work, assigns the next task, and returns. Tasks can even pass parameters in the same way. Like so, Dr. Fred keeps adding to a pile as long as there is a pile, and Mr. Jim keeps taking from it as long as it's there to take from: from random import choice def fred(): global fun, arg print('Fred adds 1') fun, arg = choice((fred, jim)), arg + 1 def jim(): global fun, arg print('Jim takes 1') fun, arg = choice((fred, jim)), arg - 1 if __name__ == '__main__': fun, arg = choice((fred, jim)), 3 while arg: print('Pile is', arg, end = '\t') fun() else: print('Pile is gone') From roy at panix.com Sun May 26 11:23:04 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 11:23:04 -0400 Subject: Solving the problem of mutual recursion References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> Message-ID: In article , Jussi Piitulainen wrote: > A light-weighter way is to have each task end by assigning the next > task and returning, instead of calling the next task directly. When a > task returns, a driver loop will call the assigned task, which again > does a bounded amount of work, assigns the next task, and returns. > Tasks can even pass parameters in the same way. Yup. I've used this pattern for building state machines. Each state is a function which returns the next state (or, sometimes, a (next_state, output) tuple). The top level loop ends up looking very much like yours: state = start while state != end: state, output = state(get_next_input()) print output > > Like so, Dr. Fred keeps adding to a pile as long as there is a pile, > and Mr. Jim keeps taking from it as long as it's there to take from: > > from random import choice > > def fred(): > global fun, arg > print('Fred adds 1') > fun, arg = choice((fred, jim)), arg + 1 > > def jim(): > global fun, arg > print('Jim takes 1') > fun, arg = choice((fred, jim)), arg - 1 > > if __name__ == '__main__': > fun, arg = choice((fred, jim)), 3 > while arg: > print('Pile is', arg, end = '\t') > fun() > else: > print('Pile is gone') From peter.h.m.brooks at gmail.com Sun May 26 13:21:05 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Sun, 26 May 2013 10:21:05 -0700 (PDT) Subject: Solving the problem of mutual recursion References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> Message-ID: On May 26, 5:09?pm, Jussi Piitulainen wrote: > > A light-weighter way is to have each task end by assigning the next > task and returning, instead of calling the next task directly. When a > task returns, a driver loop will call the assigned task, which again > does a bounded amount of work, assigns the next task, and returns. > Tasks can even pass parameters in the same way. > Yes, that's true - there are a number of ways of making it linear. What I'm particularly pleased about with my method is the parallelism that it achieves - with so little effort! The simulation is going to be computationally intense and this is going to make sure that the CPUs are all giving it their best shot. When I run this on my macbook, the python interpreter takes over 140% of CPU - with a bit of fine- tuning, it should be possible to have more concurrent threads and to use the four cores optimally. Naturally I'll need to be careful with the concurrency, but this is so simple and clean that it should be easy to avoid the main problems with accessing the same variables. From carlosnepomuceno at outlook.com Sun May 26 14:09:29 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 21:09:29 +0300 Subject: Solving the problem of mutual recursion In-Reply-To: References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com>, , Message-ID: ---------------------------------------- > Date: Sun, 26 May 2013 10:21:05 -0700 > Subject: Re: Solving the problem of mutual recursion > From: peter.h.m.brooks at gmail.com > To: python-list at python.org > > On May 26, 5:09 pm, Jussi Piitulainen > wrote: >> >> A light-weighter way is to have each task end by assigning the next >> task and returning, instead of calling the next task directly. When a >> task returns, a driver loop will call the assigned task, which again >> does a bounded amount of work, assigns the next task, and returns. >> Tasks can even pass parameters in the same way. >> > Yes, that's true - there are a number of ways of making it linear. > > What I'm particularly pleased about with my method is the parallelism > that it achieves - with so little effort! The simulation is going to > be computationally intense and this is going to make sure that the > CPUs are all giving it their best shot. When I run this on my macbook, > the python interpreter takes over 140% of CPU - with a bit of fine- > tuning, it should be possible to have more concurrent threads and to > use the four cores optimally. > > Naturally I'll need to be careful with the concurrency, but this is so > simple and clean that it should be easy to avoid the main problems > with accessing the same variables. > -- > http://mail.python.org/mailman/listinfo/python-list Python threads run in the same process and won't run concurrently: "CPython implementation detail: In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously."[1] How can you get 140% of CPU? IS that a typo?? [1] http://docs.python.org/2/library/threading.html From peter.h.m.brooks at gmail.com Sun May 26 14:13:12 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Sun, 26 May 2013 11:13:12 -0700 (PDT) Subject: Solving the problem of mutual recursion References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com>, , Message-ID: <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> On 26 May, 20:09, Carlos Nepomuceno wrote: > ---------------------------------------- > > > > > > > > > > > Date: Sun, 26 May 2013 10:21:05 -0700 > > Subject: Re: Solving the problem of mutual recursion > > From: peter.h.m.bro... at gmail.com > > To: python-l... at python.org > > > On May 26, 5:09 pm, Jussi Piitulainen > > wrote: > > >> A light-weighter way is to have each task end by assigning the next > >> task and returning, instead of calling the next task directly. When a > >> task returns, a driver loop will call the assigned task, which again > >> does a bounded amount of work, assigns the next task, and returns. > >> Tasks can even pass parameters in the same way. > > > Yes, that's true - there are a number of ways of making it linear. > > > What I'm particularly pleased about with my method is the parallelism > > that it achieves - with so little effort! The simulation is going to > > be computationally intense and this is going to make sure that the > > CPUs are all giving it their best shot. When I run this on my macbook, > > the python interpreter takes over 140% of CPU - with a bit of fine- > > tuning, it should be possible to have more concurrent threads and to > > use the four cores optimally. > > > Naturally I'll need to be careful with the concurrency, but this is so > > simple and clean that it should be easy to avoid the main problems > > with accessing the same variables. > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Python threads run in the same process and won't run concurrently: > > "CPython implementation detail: In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously."[1] > > How can you get 140% of CPU? IS that a typo?? > No, on a multi-core machine it's normal. The machine shows python running multiple threads - and the number of threads change as the program runs. Perhaps the OS/X implementation of python does allow concurrency when others don't. It certainly looks like it! From carlosnepomuceno at outlook.com Sun May 26 14:22:19 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 21:22:19 +0300 Subject: Solving the problem of mutual recursion In-Reply-To: <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com>, , , , , , <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> Message-ID: ---------------------------------------- > Date: Sun, 26 May 2013 11:13:12 -0700 > Subject: Re: Solving the problem of mutual recursion > From: peter.h.m.brooks at gmail.com > To: python-list at python.org [...] >> How can you get 140% of CPU? IS that a typo?? >> > No, on a multi-core machine it's normal. The machine shows python > running multiple threads - and the number of threads change as the > program runs. Perhaps the OS/X implementation of python does allow > concurrency when others don't. It certainly looks like it! I pretty sure it doesn't run on multiple cores on Linux and Windows. I've tested it and have been trying to find a better way achieve concurrency in Python. One of the ways is the multiprocessing module[1]. Do Mac OS shows "140%" CPU load when more than a single core is been used? lol Apple sucks!!! lol [1] http://docs.python.org/2/library/multiprocessing.html From peter.h.m.brooks at gmail.com Sun May 26 15:05:02 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Sun, 26 May 2013 12:05:02 -0700 (PDT) Subject: Solving the problem of mutual recursion References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com>, , , , , , <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> Message-ID: <569927ff-876b-499f-99f8-7d27ccb27809@l3g2000vbl.googlegroups.com> On 26 May, 20:22, Carlos Nepomuceno wrote: > ---------------------------------------- > > > Date: Sun, 26 May 2013 11:13:12 -0700 > > Subject: Re: Solving the problem of mutual recursion > > From: peter.h.m.bro... at gmail.com > > To: python-l... at python.org > [...] > >> How can you get 140% of CPU? IS that a typo?? > > > No, on a multi-core machine it's normal. The machine shows python > > running multiple threads - and the number of threads change as the > > program runs. Perhaps the OS/X implementation of python does allow > > concurrency when others don't. It certainly looks like it! > > I pretty sure it doesn't run on multiple cores on Linux and Windows. > > I've tested it and have been trying to find a better way achieve concurrency in Python. One of the ways is the multiprocessing module[1]. > > Do Mac OS shows "140%" CPU load when more than a single core is been used? lol Apple sucks!!! lol > It's not uncommon - HP-UX does exactly the same thing. From ian.g.kelly at gmail.com Sun May 26 15:35:52 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 26 May 2013 13:35:52 -0600 Subject: Solving the problem of mutual recursion In-Reply-To: <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> Message-ID: On Sun, May 26, 2013 at 12:13 PM, Peter Brooks wrote: > No, on a multi-core machine it's normal. The machine shows python > running multiple threads - and the number of threads change as the > program runs. Perhaps the OS/X implementation of python does allow > concurrency when others don't. It certainly looks like it! I'm pretty sure that CPython uses the GIL regardless of platform. And yes you can have multiple OS-level threads, but because of the GIL only one will actually be running at a time. Other possibilities include: 1) You're using a different implementation of Python that does not have a GIL, e.g. Jython or IronPython (well, probably not the latter). I believe PyPy also has a GIL-less version, although I don't think this is in the current release yet. 2) You're using a fork of CPython that removes the GIL. There are a number of these, but none to my knowledge that are able to maintain the performance of CPython for a single thread. 3) You're mistakenly looking at multiple Python processes that are running simultaneously and adding their usages together. 4) The utility you're using is reporting the process CPU usage incorrectly. 5) Maybe for some reason the Mac OS X build of CPython is using spin-locks in the GIL. I can't imagine why this would be the case. From rosuav at gmail.com Sun May 26 18:16:34 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 08:16:34 +1000 Subject: Solving the problem of mutual recursion In-Reply-To: References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> Message-ID: On Mon, May 27, 2013 at 5:35 AM, Ian Kelly wrote: > I'm pretty sure that CPython uses the GIL regardless of platform. And > yes you can have multiple OS-level threads, but because of the GIL > only one will actually be running at a time. Other possibilities > include: 6) It's spinning in a function that has released the GIL. Python threads can certainly execute concurrently; they just can't be manipulating Python objects. Most of the I/O functions will release the GIL before doing a potentially-blocking operation, and some CPU-heavy functions can do the same (I'm given to understand that numpy does this) - just depends on having a long job that involves no refcounted objects. ChrisA From ian.g.kelly at gmail.com Mon May 27 02:19:30 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 27 May 2013 00:19:30 -0600 Subject: Solving the problem of mutual recursion In-Reply-To: References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> Message-ID: On Sun, May 26, 2013 at 4:16 PM, Chris Angelico wrote: > On Mon, May 27, 2013 at 5:35 AM, Ian Kelly wrote: >> I'm pretty sure that CPython uses the GIL regardless of platform. And >> yes you can have multiple OS-level threads, but because of the GIL >> only one will actually be running at a time. Other possibilities >> include: > > 6) It's spinning in a function that has released the GIL. Python > threads can certainly execute concurrently; they just can't be > manipulating Python objects. Most of the I/O functions will release > the GIL before doing a potentially-blocking operation, and some > CPU-heavy functions can do the same (I'm given to understand that > numpy does this) - just depends on having a long job that involves no > refcounted objects. 7) Since the program being tested does basically nothing except start and exit threads, the extra 40% probably represents the overhead of all that starting and stopping, which would be done outside the GIL. From ian.g.kelly at gmail.com Mon May 27 03:04:26 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 27 May 2013 01:04:26 -0600 Subject: Solving the problem of mutual recursion In-Reply-To: References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> Message-ID: On Mon, May 27, 2013 at 12:19 AM, Ian Kelly wrote: > 7) Since the program being tested does basically nothing except start > and exit threads, the extra 40% probably represents the overhead of > all that starting and stopping, which would be done outside the GIL. To test this, I tried running the script in Python 2.7 in Linux with the print statements removed and verified that it was using about 135% of the CPU. However, top also told me that only about 95% of that was user processes; the other 40% was kernel usage. The 40% doesn't seem to be threading overhead though, because I tried adding large xrange loops to slow down the thread creation process and it had no effect on the stats. Then I tried running the same program in Python 3.2, and I got the more expected 100% CPU usage with minimal kernel time. So I'm thinking now that the extra 40% may actually be overhead induced by the GIL. If that's the case then wow, the old GIL really did suck. From peter.h.m.brooks at gmail.com Mon May 27 00:36:39 2013 From: peter.h.m.brooks at gmail.com (Peter Brooks) Date: Sun, 26 May 2013 21:36:39 -0700 (PDT) Subject: Solving the problem of mutual recursion References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> Message-ID: <9890fd93-fd9b-4865-a01e-4cd1d558d5fc@w15g2000vbn.googlegroups.com> On May 27, 12:16?am, Chris Angelico wrote: > On Mon, May 27, 2013 at 5:35 AM, Ian Kelly wrote: > > I'm pretty sure that CPython uses the GIL regardless of platform. ?And > > yes you can have multiple OS-level threads, but because of the GIL > > only one will actually be running at a time. ?Other possibilities > > include: > > 6) It's spinning in a function that has released the GIL. Python > threads can certainly execute concurrently; they just can't be > manipulating Python objects. Most of the I/O functions will release > the GIL before doing a potentially-blocking operation, and some > CPU-heavy functions can do the same (I'm given to understand that > numpy does this) - just depends on having a long job that involves no > refcounted objects. > This makes complete sense - any atomic action should be atomic, so two threads can't be doing it at the same time. They can be doing anything else though. If two threads create a new object at the same time, for example, there's potentially the problem that they'll get the same space, so the object will be owned by both. To prevent this, the new object call should be run in only one thread. If you have two objects running their methods on their own local variables, then there's no potential for conflict, so there's no need for them to be blocked. This is an interesting subject.. There's nothing wrong with the tool I'm using to report threads - 'Activity Monitor' is the standard process monitor. It counts cores as 'CPUs', which seems perfectly reasonable to me. As I said, other Unixes, such as HP-UX, do the same thing. I'm not quite sure of the best way to examine the exact behaviour. Since the blocking works at the level of atomic operations, it's difficult to detect - a call to an atomic operation may block all other threads, but any snapshot of the number of active threads won't be running at this time, so will only report when there are multiple threads active. From ian.g.kelly at gmail.com Mon May 27 02:07:23 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 27 May 2013 00:07:23 -0600 Subject: Solving the problem of mutual recursion In-Reply-To: <9890fd93-fd9b-4865-a01e-4cd1d558d5fc@w15g2000vbn.googlegroups.com> References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> <9890fd93-fd9b-4865-a01e-4cd1d558d5fc@w15g2000vbn.googlegroups.com> Message-ID: On Sun, May 26, 2013 at 10:36 PM, Peter Brooks wrote: > This makes complete sense - any atomic action should be atomic, so two > threads can't be doing it at the same time. They can be doing anything > else though. > > If two threads create a new object at the same time, for example, > there's potentially the problem that they'll get the same space, so > the object will be owned by both. To prevent this, the new object call > should be run in only one thread. > > If you have two objects running their methods on their own local > variables, then there's no potential for conflict, so there's no need > for them to be blocked. That's not the way it works. The CPython interpreter always runs with the GIL held; the alternative would be to have individual mutex locks on every Python object, which is expensive for performance due to the reference counting mechanism. Python functions can't release the GIL. C functions that are called from the interpreter *can* release the GIL to allow concurrency, but are only permitted to do so as long as they're not working with Python objects, e.g. waiting on I/O or performing a long calculation on C data. There are some more detailed slides on how the GIL works at: http://www.dabeaz.com/python/UnderstandingGIL.pdf Note that the description in Part 1 describes how the GIL worked prior to Python 3.2. The new GIL is described in Part 4, but the basic underlying concept is the same. > This is an interesting subject.. There's nothing wrong with the tool > I'm using to report threads - 'Activity Monitor' is the standard > process monitor. It counts cores as 'CPUs', which seems perfectly > reasonable to me. As I said, other Unixes, such as HP-UX, do the same > thing. I have no problem with that. It's also the default in Linux, where I believe it is called "IRIX mode" (as opposed to "Solaris mode", where the difference is just a factor equal to the number of cores). What I was questioning was whether the actual number being reported was correct. If it's the standard tool for the OS, then it probably is. From rosuav at gmail.com Mon May 27 03:37:21 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 17:37:21 +1000 Subject: Solving the problem of mutual recursion In-Reply-To: References: <55942e65-e4a5-45fc-b2fc-ceb4020959dd@k4g2000vba.googlegroups.com> <074eac8a-1bc4-4fe0-afa9-1f52405f81d5@k3g2000vbn.googlegroups.com> <9890fd93-fd9b-4865-a01e-4cd1d558d5fc@w15g2000vbn.googlegroups.com> Message-ID: On Mon, May 27, 2013 at 4:07 PM, Ian Kelly wrote: > On Sun, May 26, 2013 at 10:36 PM, Peter Brooks > wrote: >> This makes complete sense - any atomic action should be atomic, so two >> threads can't be doing it at the same time. They can be doing anything >> else though. >> >> If two threads create a new object at the same time, for example, >> there's potentially the problem that they'll get the same space, so >> the object will be owned by both. To prevent this, the new object call >> should be run in only one thread. >> >> If you have two objects running their methods on their own local >> variables, then there's no potential for conflict, so there's no need >> for them to be blocked. > > That's not the way it works. [snip details] You're actually both saying the same thing, except that Peter went for finer granularity than Ian and CPython did. CPython figures that, with a refcounted heap, there's not a lot of point having separate mutex locks for different operations. Other language interpreters have made other choices. But Peter's analysis is still correct; it's just that guarding it is simplified down to a binary state: either you have the GIL, or you don't. ChrisA From mok-kong.shen at t-online.de Sun May 26 08:02:39 2013 From: mok-kong.shen at t-online.de (Mok-Kong Shen) Date: Sun, 26 May 2013 14:02:39 +0200 Subject: Output from to_bytes Message-ID: I don't understand why with the code: for k in range(8,12,1): print(k.to_bytes(2,byteorder='big')) one gets the following output: b'\x00\x08' b'\x00\t' b'\x00\n' b'\x00\x0b' I mean the 2nd and 3rd should be b'\x00\x09' and b'x00\x0a'. Anyway, how could I get the output in the forms I want? Thanks in advance. M. K. Shen From rosuav at gmail.com Sun May 26 09:26:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 23:26:33 +1000 Subject: Output from to_bytes In-Reply-To: References: Message-ID: On Sun, May 26, 2013 at 10:02 PM, Mok-Kong Shen wrote: > I don't understand why with the code: > > for k in range(8,12,1): > print(k.to_bytes(2,byteorder='big')) > > one gets the following output: > > b'\x00\x08' > b'\x00\t' > b'\x00\n' > b'\x00\x0b' > > I mean the 2nd and 3rd should be b'\x00\x09' and b'x00\x0a'. > Anyway, how could I get the output in the forms I want? They are. If you compare them, you'll find they're identical: >>> b'\x00\t' == b'\x00\x09' True >>> b'\x00\n' == b'\x00\x0a' True It's just a representation issue. The repr() of a bytes tries to go for the shorter representation \n rather than the more verbose \x0a. (Though I'm not sure why it doesn't also shorten \x00 to \0 - maybe the \0 notation isn't deemed Pythonic, even though it does work just fine.) So what you want is a more fixed representation. What you may find useful here is that iterating over the bytes object produces integers: >>> list(b'\0\t') [0, 9] So you might be able to do something like this: >>> print(''.join(('\\x%02x'%x for x in b'\0\t'))) \x00\x09 Or, in your whole loop: >>> for k in range(8,12,1): print(''.join(('\\x%02x'%x for x in k.to_bytes(2,byteorder='big')))) \x00\x08 \x00\x09 \x00\x0a \x00\x0b Does that help? ChrisA From tjreedy at udel.edu Sun May 26 16:45:50 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 26 May 2013 16:45:50 -0400 Subject: Output from to_bytes In-Reply-To: References: Message-ID: On 5/26/2013 8:02 AM, Mok-Kong Shen wrote: > for k in range(8,12,1): > print(k.to_bytes(2,byteorder='big')) http://bugs.python.org/issue9951 http://bugs.python.org/issue3532 import binascii as ba for k in range(8,12,1): print(ba.hexlify(k.to_bytes(2,byteorder='big'))) >>> b'0008' b'0009' b'000a' b'000b' From invalid at invalid.invalid Tue May 28 11:35:19 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 15:35:19 +0000 (UTC) Subject: Output from to_bytes References: Message-ID: On 2013-05-26, Mok-Kong Shen wrote: > I don't understand why with the code: > > for k in range(8,12,1): > print(k.to_bytes(2,byteorder='big')) > > one gets the following output: > > b'\x00\x08' > b'\x00\t' > b'\x00\n' > b'\x00\x0b' > > I mean the 2nd and 3rd should be b'\x00\x09' and b'x00\x0a'. > Anyway, how could I get the output in the forms I want? Well, it would help if you told us what output form you want. -- Grant Edwards grant.b.edwards Yow! I'm gliding over a at NUCLEAR WASTE DUMP near gmail.com ATLANTA, Georgia!! From nikos.gr33k at gmail.com Sun May 26 12:36:29 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 09:36:29 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) Message-ID: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> This is the code that although correct becaus it works with englisg(standARD ASCII letters) it wont with Greek: if( log ): name = log # print specific client header info cur.execute('''SELECT hits, money FROM clients WHERE name = %s''', (name,) ) data = cur.fetchone() ======================= The following is the live output of: tail -F /usr/local/apache/logs/error_log & [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Error in sys.excepthook:, referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] UnicodeEncodeError: 'ascii' codec can't encode characters in position 2050-2056: ordinal not in range(128), referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] , referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Original exception was:, referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Traceback (most recent call last):, referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] File "pelatologio.py", line 122, in , referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] cur.execute('''SELECT hits, money FROM clients WHERE name = %s''', (name,) ), referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] File "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute, referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] query = query.encode(charset), referer: http://superhost.gr/cgi-bin/pelatologio.py [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] UnicodeEncodeError: 'latin-1' codec can't encode characters in position 46-52: ordinal not in range(256), referer: http://superhost.gr/cgi-bin/pelatologio.py I can udnerstand that this is an encoding issue but i dont knwo how to fix this. please help. The search works only for english letters as expected but my cleints have Greek name and surnames. From nikos.gr33k at gmail.com Sun May 26 14:20:19 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 11:20:19 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> Message-ID: <435277a8-2821-4927-9c99-548d75600f9a@googlegroups.com> Any idea how to correct this encoding issue? From tjreedy at udel.edu Sun May 26 16:53:56 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 26 May 2013 16:53:56 -0400 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> Message-ID: On 5/26/2013 12:36 PM, ????? ???33? wrote: > This is the code that although correct becaus it works with englisg(standARD ASCII letters) it wont with Greek: > > if( log ): > name = log > # print specific client header info > cur.execute('''SELECT hits, money FROM clients WHERE name = %s''', (name,) ) > data = cur.fetchone() > ======================= > > The following is the live output of: tail -F /usr/local/apache/logs/error_log & > > > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Error in sys.excepthook:, referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] UnicodeEncodeError: 'ascii' codec can't encode characters in position 2050-2056: ordinal not in range(128), referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] , referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Original exception was:, referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Traceback (most recent call last):, referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] File "pelatologio.py", line 122, in , referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] cur.execute('''SELECT hits, money FROM clients WHERE name = %s''', (name,) ), referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] File "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute, referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] query = query.encode(charset), referer: http://superhost.gr/cgi-bin/pelatologio.py > [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] UnicodeEncodeError: 'latin-1' codec can't encode characters in position 46-52: ordinal not in range(256), referer: http://superhost.gr/cgi-bin/pelatologio.py This appears to be an issue with your mysql database and how you set it up.. It is using charset='latin-1' whereas you need it to consistently use charset='utf8' both for storing strings and for retrieval. Check the mysql manual or ask on a mysql list for how to do that. I have no idea so don't ask me, or send mail to my emall address. From nikos.gr33k at gmail.com Sun May 26 17:26:34 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 14:26:34 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> Message-ID: No thi is not a mysql issue becaus ei have this line above for storing and retrieval form database. con = pymysql.connect( db = 'metrites', host = 'localhost', user = 'me', passwd = 'somepass', init_command='SET NAMES UTF8' ) From rosuav at gmail.com Sun May 26 17:44:57 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 07:44:57 +1000 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> Message-ID: On Mon, May 27, 2013 at 7:26 AM, ????? ???33? wrote: > No thi is not a mysql issue becaus ei have this line above for storing and retrieval form database. > > con = pymysql.connect( db = 'metrites', host = 'localhost', user = 'me', passwd = 'somepass', init_command='SET NAMES UTF8' ) Research MySQL and UTF-8, then see how your command is actually flawed. Then research ESR and asking questions the smart way, and see how your post is also flawed. ChrisA From breamoreboy at yahoo.co.uk Sun May 26 18:21:25 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 23:21:25 +0100 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> Message-ID: On 26/05/2013 22:26, ????? ???33? wrote: > No thi is not a mysql issue becaus ei have this line above for storing and retrieval form database. > > con = pymysql.connect( db = 'metrites', host = 'localhost', user = 'me', passwd = 'somepass', init_command='SET NAMES UTF8' ) > No wonder the Greek economy is so screwed up. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From carlosnepomuceno at outlook.com Sun May 26 18:31:57 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 27 May 2013 01:31:57 +0300 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com>, , , Message-ID: ---------------------------------------- > To: python-list at python.org > From: breamoreboy at yahoo.co.uk [...] > No wonder the Greek economy is so screwed up. > > -- > If you're using GoogleCrap? please read this > http://wiki.python.org/moin/GoogleGroupsPython. > > Mark Lawrence LOL LOL LOL LOL LOL LOL HAHAHAHAHA!!!! From rosuav at gmail.com Sun May 26 18:32:12 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 08:32:12 +1000 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> Message-ID: On Mon, May 27, 2013 at 8:21 AM, Mark Lawrence wrote: > On 26/05/2013 22:26, ????? ???33? wrote: >> >> No thi is not a mysql issue becaus ei have this line above for storing and >> retrieval form database. >> >> con = pymysql.connect( db = 'metrites', host = 'localhost', user = 'me', >> passwd = 'somepass', init_command='SET NAMES UTF8' ) >> > > No wonder the Greek economy is so screwed up. Do you think the Greek economy would be improved by Python coming with PostgreSQL bindings? That'd push a lot of people onto a better database engine than MySQL. ChrisA From breamoreboy at yahoo.co.uk Sun May 26 18:51:41 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 23:51:41 +0100 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> Message-ID: On 26/05/2013 23:32, Chris Angelico wrote: > On Mon, May 27, 2013 at 8:21 AM, Mark Lawrence wrote: >> On 26/05/2013 22:26, ????? ???33? wrote: >>> >>> No thi is not a mysql issue becaus ei have this line above for storing and >>> retrieval form database. >>> >>> con = pymysql.connect( db = 'metrites', host = 'localhost', user = 'me', >>> passwd = 'somepass', init_command='SET NAMES UTF8' ) >>> >> >> No wonder the Greek economy is so screwed up. > > Do you think the Greek economy would be improved by Python coming with > PostgreSQL bindings? That'd push a lot of people onto a better > database engine than MySQL. > > ChrisA > Even the best economies would be improved by Python coming with PostgreSQL bindings, but if and only if the user knew how to use them :) -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From timr at probo.com Sun May 26 22:01:17 2013 From: timr at probo.com (Tim Roberts) Date: Sun, 26 May 2013 19:01:17 -0700 Subject: Encodign issue in Python 3.3.1 (once again) References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> Message-ID: <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> ????? ???33? wrote: > >This is the code that although correct becaus it works with englisg(standARD ASCII letters) it wont with Greek: >... >if( log ): > name = log > # print specific client header info > cur.execute('''SELECT hits, money FROM clients WHERE name = %s''', (name,) ) > data = cur.fetchone() >======================= > >The following is the live output of: tail -F /usr/local/apache/logs/error_log & >... > File "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute, referer: http://superhost.gr/cgi-bin/pelatologio.py > query = query.encode(charset), referer: http://superhost.gr/cgi-bin/pelatologio.py > UnicodeEncodeError: 'latin-1' codec can't encode characters in position 46-52: ordinal not in range(256), referer: http://superhost.gr/cgi-bin/pelatologio.py > >I can udnerstand that this is an encoding issue but i dont knwo how to fix this. >please help. While the other responders have a good laugh at your expense, let me actually point you toward a solution. The traceback line I left above is the key. It shows that the pymysql module is trying to encode your Unicode string into an 8-bit character set in order to send it to the MySQL server. It is THAT conversion that failed; this has nothing to do with the server (yet). So, why did it choose 'latin-1'? Because that's the default character set for pymysql. You could have learned this yourself -- you have the full source code for pymysql on your machine. You can override the default character set: con = pymysql.connect( db = 'metrites', host = 'localhost', user = 'me', passwd = 'somepass', charset='utf-8', init_command='SET NAMES UTF8' ) -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From nikos.gr33k at gmail.com Mon May 27 01:06:22 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Sun, 26 May 2013 22:06:22 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> Message-ID: <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> But iu have it set up for 'utf-8' as seen in this statement. con = pymysql.connect( db = 'metrites', host = 'localhost', user = 'me', passwd = 'somepass', charset='utf-8', init_command='SET NAMES UTF8' ) Yoiu mean i shoudl chnag eit to greek isoo= (iso-8859-7) but then i store english names and greek names as well, so it has to be a utf-8. From nikos.gr33k at gmail.com Mon May 27 10:16:42 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Mon, 27 May 2013 07:16:42 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: I cant solve this plz help! From breamoreboy at yahoo.co.uk Mon May 27 10:45:25 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 27 May 2013 15:45:25 +0100 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: On 27/05/2013 15:16, ????? ???33? wrote: > I cant solve this plz help! > Sure, all you need do is get your cheque book out. Also have you ever heard the expression "patience is a virtue"? -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From nikos.gr33k at gmail.com Mon May 27 11:37:52 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Mon, 27 May 2013 08:37:52 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com> ?? ???????, 27 ????? 2013 5:45:25 ?.?. UTC+3, ? ??????? Mark Lawrence ??????: > Sure, all you need do is get your cheque book out. Also have you ever > heard the expression "patience is a virtue"? Well, if i'am gonna pay someone and i will at some point because i want my script to be also re-written using a web framework(Django) it will certainly be someone that can actually provide a so;lution instead of aksing for cheque books. From rosuav at gmail.com Mon May 27 11:52:32 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 28 May 2013 01:52:32 +1000 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com> Message-ID: On Tue, May 28, 2013 at 1:37 AM, ????? ???33? wrote: > ?? ???????, 27 ????? 2013 5:45:25 ?.?. UTC+3, ? ??????? Mark Lawrence ??????: > >> Sure, all you need do is get your cheque book out. Also have you ever >> heard the expression "patience is a virtue"? > > Well, if i'am gonna pay someone and i will at some point because i want my script to be also re-written using a web framework(Django) it will certainly be someone that can actually provide a so;lution instead of aksing for cheque books. So you want to pay someone who won't ask for money? It may be sordid, but you're going to have to discuss money at some point if you're serious about paying someone. Oh, and you may want to hire a typist, too. At the moment, your posts make you appear not to care about the job. ChrisA From nikos.gr33k at gmail.com Mon May 27 12:56:37 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Mon, 27 May 2013 09:56:37 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com> Message-ID: <9d0cfcdd-0644-4d15-8165-5452c19e57a3@googlegroups.com> ?? ???????, 27 ????? 2013 6:52:32 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > So you want to pay someone who won't ask for money? > It may be sordid, but you're going to have to discuss money at some > point if you're serious about paying someone. > Oh, and you may want to hire a typist, too. At the moment, your posts > make you appear not to care about the job. I always make typos when iam typing fast. And yes at some point i will pay a person from this groups to alter my scripts to utilize web frameworks. To answer your question yes, i will be paying a person from this group that have been providing me solutions to previous questions without laughing at my expense and without requiring money frome to do so. I have 2-3 people in mind. I have managed to turn ALL my python 2.6 scripts to python 3.3.1 except this last problem i have to deal with. http://superhost.gr/?page=pelatologio.py works partiall but only displays info when i click on person names in English, not with Greek names. If someone want to give me a hand with this, please do. From rosuav at gmail.com Mon May 27 18:18:06 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 28 May 2013 08:18:06 +1000 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <9d0cfcdd-0644-4d15-8165-5452c19e57a3@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com> <9d0cfcdd-0644-4d15-8165-5452c19e57a3@googlegroups.com> Message-ID: On Tue, May 28, 2013 at 2:56 AM, ????? ???33? wrote: > ?? ???????, 27 ????? 2013 6:52:32 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > >> Oh, and you may want to hire a typist, too. At the moment, your posts >> make you appear not to care about the job. > > I always make typos when iam typing fast. Yes, but then you don't go back and correct them before hitting Send, which is what makes you appear to care very little. Typos in real-time chat are understandable (though even on MUDs, most people tend to broadcast less typos than your posts have, because even there there's time to edit before hitting Enter), but with email, it's very easy to spend 60 seconds glancing over your post before you send it. You're using gmail, so chances are all you need to do is use a web browser with a spellchecker and you'll catch all of the typos; I use Google Chrome, and it has the now-prevalent red squiggly lines under all suspected errors. (It's not perfect, of course; quite a few correctly-spelled words are mistakenly flagged. But it's still pretty effective at catching typos.) You're effectively asking people to put in a few minutes' work, sometimes quite a few minutes, to help you. Is it too much to hope that you'll spend one more minute on your posts? This is something that's utterly trivial, yet a window to your mind. It's like boarding an airliner and finding coffee stains on the flip-down trays - the plane flies just fine with those stains, yet the impression they give is that the crew don't care about engine maintenance either. ChrisA From carlosnepomuceno at outlook.com Mon May 27 21:00:19 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 01:00:19 -0000 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com>, <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com>, <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com>, , , <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com>, , <9d0cfcdd-0644-4d15-8165-5452c19e57a3@googlegroups.com>, Message-ID: ---------------------------------------- > Date: Tue, 28 May 2013 08:18:06 +1000 > Subject: Re: Encodign issue in Python 3.3.1 (once again) > From: rosuav at gmail.com > To: python-list at python.org [...] > > This is something that's utterly trivial, yet a window to your mind. > It's like boarding an airliner and finding coffee stains on the > flip-down trays - the plane flies just fine with those stains, yet the > impression they give is that the crew don't care about engine > maintenance either. > > ChrisA Stain Is Too Damn High!!! lol That's my party man!!! :) From nikos.gr33k at gmail.com Tue May 28 01:55:52 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Mon, 27 May 2013 22:55:52 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com> <9d0cfcdd-0644-4d15-8165-5452c19e57a3@googlegroups.com> Message-ID: <8d7651bd-a4f7-4f8f-bba8-99f5aee6f6b4@googlegroups.com> ?? ?????, 28 ????? 2013 1:18:06 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > You're effectively asking people to put in a few minutes' work, > sometimes quite a few minutes, to help you. Is it too much to hope > that you'll spend one more minute on your posts? No it is not, you are right, i should be more careful when typing and spend some time to correct before i send my responses. I promise i will not overlook this as well as the blank line google imposes. As fo my initial question any comments-remarks? From rustompmody at gmail.com Tue May 28 07:56:57 2013 From: rustompmody at gmail.com (rusi) Date: Tue, 28 May 2013 04:56:57 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com> <9d0cfcdd-0644-4d15-8165-5452c19e57a3@googlegroups.com> <8d7651bd-a4f7-4f8f-bba8-99f5aee6f6b4@googlegroups.com> Message-ID: <39469c94-ddef-4492-ab6f-db2a6c61db49@vy4g2000pbc.googlegroups.com> On May 28, 10:55?am, ????? ???33? wrote: > ?? ?????, 28 ????? 2013 1:18:06 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > > > You're effectively asking people to put in a few minutes' work, > > sometimes quite a few minutes, to help you. Is it too much to hope > > that you'll spend one more minute on your posts? > > No it is not, you are right, i should be more careful when typing and spend some time to correct before i send my responses. I promise i will not overlook this as well as the blank line google imposes. > > As fo my initial question any comments-remarks? You say you should be more careful to correct your typing and 'promise' to not overlook this. Your next line has a 'fo' instead of a 'for' From danielgagliardiramos at gmail.com Tue May 28 09:49:59 2013 From: danielgagliardiramos at gmail.com (Daniel Gagliardi Ramos) Date: Tue, 28 May 2013 10:49:59 -0300 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <39469c94-ddef-4492-ab6f-db2a6c61db49@vy4g2000pbc.googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <529f6d18-65a3-437a-944c-890bbacd9212@googlegroups.com> <9d0cfcdd-0644-4d15-8165-5452c19e57a3@googlegroups.com> <8d7651bd-a4f7-4f8f-bba8-99f5aee6f6b4@googlegroups.com> <39469c94-ddef-4492-ab6f-db2a6c61db49@vy4g2000pbc.googlegroups.com> Message-ID: don't ya have something intelligent to say motherfucker? 2013/5/28 rusi > On May 28, 10:55 am, ????? ???33? wrote: > > ?? ?????, 28 ????? 2013 1:18:06 ?.?. UTC+3, ? ??????? Chris Angelico > ??????: > > > > > You're effectively asking people to put in a few minutes' work, > > > sometimes quite a few minutes, to help you. Is it too much to hope > > > that you'll spend one more minute on your posts? > > > > No it is not, you are right, i should be more careful when typing and > spend some time to correct before i send my responses. I promise i will not > overlook this as well as the blank line google imposes. > > > > As fo my initial question any comments-remarks? > > You say you should be more careful to correct your typing and > 'promise' to not overlook this. > Your next line has a 'fo' instead of a 'for' > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From torriem at gmail.com Mon May 27 10:42:56 2013 From: torriem at gmail.com (Michael Torrie) Date: Mon, 27 May 2013 08:42:56 -0600 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: <51A370F0.8090909@gmail.com> On 05/26/2013 11:06 PM, ????? ???33? wrote: > But iu have it set up for 'utf-8' as seen in this statement. > > con = pymysql.connect( db = 'metrites', host = 'localhost', user = > 'me', passwd = 'somepass', charset='utf-8', init_command='SET NAMES UTF8' ) That might not help... see below. > > Yoiu mean i shoudl chnag eit to greek isoo= (iso-8859-7) > but then i store english names and greek names as well, so it has to be a utf-8. Right. UTF-8 is the encoding you want. I think your problem is that when the MySQL database was created, it was not created using the UTF-8 mode, but rather latin-1 (despite your init_command which has nothing to do with database creation). You'll want to recreate the database and make double sure you've selected the right character set and collation: http://dev.mysql.com/doc/refman/5.0/en/charset-applications.html At least that should be your first step in debugging this problem. If the database is indeed utf-8, then you can move on to debugging why the python code is choosing latin-1 to encode your query string. That's my two-cents for today. From nikos.gr33k at gmail.com Mon May 27 16:17:54 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Mon, 27 May 2013 13:17:54 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: I have checked the database through phpMyAdmin and it is indeed UTF-8. I have no idea why python 3.3.1 chooses to work with latin-iso only.... From torriem at gmail.com Tue May 28 10:34:25 2013 From: torriem at gmail.com (Michael Torrie) Date: Tue, 28 May 2013 08:34:25 -0600 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: <51A4C071.80609@gmail.com> On 05/27/2013 02:17 PM, ????? ???33? wrote: > I have checked the database through phpMyAdmin and it is indeed UTF-8. > > I have no idea why python 3.3.1 chooses to work with latin-iso only.... It's not python that is doing this here... If you look at the source code to pymysql, I'm sure you will identify the problem. In fact I'll even walk you through things. The traceback you posted tells you what's going on and why, with superfluous details removed for clarity: File "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute: query = query.encode(charset) UnicodeEncodeError: 'latin-1' codec can't encode characters in position 46-52: ordinal not in range(256) So there we have it. pymysql is actually explicitly calling .encode() on a string and is using whatever character set is specified by the local variable charset. If you look in cursors.py at that line, and then a few lines above, you will find that charset is assigned to conn.charset. This means that the charset is actually defined in the connection object. So go look at connections.py. Sure enough, it shows that charset is defined by default as "latin-1." That's no good for you. So take a look at the __init__ method in connections.py. In there you should find the necessary keyword argument you need to use when creating the mysql connection to make sure the charset is utf-8. From nikos.gr33k at gmail.com Tue May 28 12:00:18 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Tue, 28 May 2013 09:00:18 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: I do not know here to find connections.py Michael. But i do not understand since iam suing the following 2 statements, why a unicode error remains. #needed line, script does *not* work without it sys.stdout = os.fdopen(1, 'w', encoding='utf-8') # connect to database con = pymysql.connect( db = 'pelatologio', host = 'localhost', user = 'myself', passwd = 'mypass', init_command='SET NAMES UTF8' ) cur = con.cursor() Shall i chnage the connector form 'pymysql' => 'MySQLdb' ? From python at mrabarnett.plus.com Tue May 28 12:26:07 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 28 May 2013 17:26:07 +0100 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: <51A4DA9F.1040306@mrabarnett.plus.com> On 28/05/2013 17:00, ????? ???33? wrote: > I do not know here to find connections.py Michael. > > But i do not understand since iam suing the following 2 statements, why a unicode error remains. > > #needed line, script does *not* work without it > sys.stdout = os.fdopen(1, 'w', encoding='utf-8') > > # connect to database > con = pymysql.connect( db = 'pelatologio', host = 'localhost', user = 'myself', passwd = 'mypass', init_command='SET NAMES UTF8' ) > cur = con.cursor() > > Shall i chnage the connector form 'pymysql' => 'MySQLdb' ? > A quick look at the documentation tells me that the charset can be specified in the 'connect' call, something like this: con = pymysql.connect( db = 'pelatologio', host = 'localhost', user = 'myself', passwd = 'mypass', init_command='SET NAMES UTF8', charset = 'utf-8' ) From torriem at gmail.com Tue May 28 12:40:09 2013 From: torriem at gmail.com (Michael Torrie) Date: Tue, 28 May 2013 10:40:09 -0600 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: <51A4DDE9.4010607@gmail.com> On 05/28/2013 10:00 AM, ????? ???33? wrote: > I do not know here to find connections.py Michael. It's part of the pymysql package that you installed. Look in there (the traceback even shows you where the file is). But you actually don't even need to look at it on your server. You can see the source code for it here: https://github.com/petehunt/PyMySQL/tree/master/pymysql > But i do not understand since iam suing the following 2 statements, > why a unicode error remains. The traceback told you why the error is happening. inside the pymysql library, there is a call to .encode(charset) on your query string. And the default charset is latin-1. You've got to work out a way to change that and I've already told you exactly how to find out what you need to do. > #needed line, script does *not* work without it sys.stdout = > os.fdopen(1, 'w', encoding='utf-8') > > # connect to database con = pymysql.connect( db = 'pelatologio', host > = 'localhost', user = 'myself', passwd = 'mypass', init_command='SET > NAMES UTF8' ) cur = con.cursor() HINT: this line ^^^^^^ is what you have to modify by passing in a particular keyword argument that sets the character set (no it's not the init_command one). You will have to look at connections.py's __init__ declaration and you should see what you need to pass in. Yes I could have saved myself a lot of time and typing by giving you the answer, but that isn't going to help you. On the other hand, if you want the easy way out, paypal some money to me and I'll just give you the answer instead of trying to give you the tools you need to debug this issue. The answers are all in the source code to pymysql, which is open source so you can always read (either on your own server file system or on the git repository). > > Shall i chnage the connector form 'pymysql' => 'MySQLdb' ? No. From support at superhost.gr Tue May 28 12:45:24 2013 From: support at superhost.gr (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Tue, 28 May 2013 09:45:24 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: con = pymysql.connect( db = 'pelatologio', host = 'localhost', user = 'blabla', passwd = 'blabla', init_command='SET NAMES UTF8', charset = 'utf-8' ) produces this "God knows what" error traceback.... /home/nikos/public_html/cgi-bin/metrites.py in () 217 template = htmldata + counter 218 elif page.endswith('.py'): => 219 htmldata = subprocess.check_output( '/home/nikos/public_html/cgi-bin/' + page ) 220 template = htmldata.decode('utf-8').replace( 'Content-type: text/html; charset=utf-8', '' ) + counter 221 htmldata undefined, subprocess = , subprocess.check_output = , page = 'pelatologio.py' /opt/python3/lib/python3.3/subprocess.py in check_output(timeout=None, *popenargs=('/home/nikos/public_html/cgi-bin/pelatologio.py',), **kwargs={}) 584 retcode = process.poll() 585 if retcode: => 586 raise CalledProcessError(retcode, process.args, output=output) 587 return output 588 global CalledProcessError = , retcode = 1, process = , process.args = '/home/nikos/public_html/cgi-bin/pelatologio.py', output = b'\n\n' CalledProcessError: Command '/home/nikos/public_html/cgi-bin/pelatologio.py' returned non-zero exit status 1 args = (1, '/home/nikos/public_html/cgi-bin/pelatologio.py') cmd = '/home/nikos/public_html/cgi-bin/pelatologio.py' output = b'\n\n' returncode = 1 with_traceback = From torriem at gmail.com Tue May 28 13:17:05 2013 From: torriem at gmail.com (Michael Torrie) Date: Tue, 28 May 2013 11:17:05 -0600 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: <51A4E691.8050808@gmail.com> On 05/28/2013 10:45 AM, ????? ???33? wrote: > con = pymysql.connect( db = 'pelatologio', host = 'localhost', user = 'blabla', passwd = 'blabla', init_command='SET NAMES UTF8', charset = 'utf-8' ) > > produces this "God knows what" error traceback.... Hey at least your database code is now working. > /home/nikos/public_html/cgi-bin/metrites.py in () > 217 template = htmldata + counter > 218 elif page.endswith('.py'): > => 219 htmldata = subprocess.check_output( '/home/nikos/public_html/cgi-bin/' + page ) > 220 template = htmldata.decode('utf-8').replace( 'Content-type: text/html; charset=utf-8', '' ) + counter > 221 > htmldata undefined, subprocess = , subprocess.check_output = , page = 'pelatologio.py' > /opt/python3/lib/python3.3/subprocess.py in check_output(timeout=None, *popenargs=('/home/nikos/public_html/cgi-bin/pelatologio.py',), **kwargs={}) > 584 retcode = process.poll() > 585 if retcode: > => 586 raise CalledProcessError(retcode, process.args, output=output) > 587 return output > 588 > global CalledProcessError = , retcode = 1, process = , process.args = '/home/nikos/public_html/cgi-bin/pelatologio.py', output = b'\n\n' > CalledProcessError: Command '/home/nikos/public_html/cgi-bin/pelatologio.py' returned non-zero exit status 1 > args = (1, '/home/nikos/public_html/cgi-bin/pelatologio.py') > cmd = '/home/nikos/public_html/cgi-bin/pelatologio.py' > output = b'\n\n' > returncode = 1 > with_traceback = Again the traceback is telling you where to look for the problem. And the problem is in your own script, pelatologio.py, though the line number that's causing the problem is obscured. Basically you want pelatologio.py to run and then you process the output through a template, correct? Well the traceback is telling you that pelatologio.py is erroring out. In fact, subprocess is telling you that this process (your script) quit with an error. Though the line number in pelatologio.py is unknown (the traceback is obscured by your html processing code in your template processor), the error message itself is not. Inside pelatologio.py there is some object that you are trying to reference the "id" attribute on it does not contain id. So the problem is in pelatologio.py. Double check your code there. Try to make a standalone test file you can run locally. From nagia.retsina at gmail.com Tue May 28 14:08:51 2013 From: nagia.retsina at gmail.com (nagia.retsina at gmail.com) Date: Tue, 28 May 2013 11:08:51 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> Message-ID: <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> ?? ?????, 28 ????? 2013 8:17:05 ?.?. UTC+3, ? ??????? Michael Torrie ??????: > Basically you want pelatologio.py to run and then you process the output > through a template, correct? That is correct. > Inside pelatologio.py > there is some object that you are trying to reference the "id" attribute > on it does not contain id. > So the problem is in pelatologio.py. Double check your code there. Try > to make a standalone test file you can run locally. I have, i see no error, the error appears only when i append the charset directive into the connector, if i remove it the error dissapears. From joel.goldstick at gmail.com Tue May 28 14:14:29 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 28 May 2013 14:14:29 -0400 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> Message-ID: On Tue, May 28, 2013 at 2:08 PM, wrote: > ?? ?????, 28 ????? 2013 8:17:05 ?.?. UTC+3, ? ??????? Michael Torrie > ??????: > > > Basically you want pelatologio.py to run and then you process the output > > through a template, correct? > > That is correct. > > > Inside pelatologio.py > > there is some object that you are trying to reference the "id" attribute > > on it does not contain id. > > So the problem is in pelatologio.py. Double check your code there. Try > > to make a standalone test file you can run locally. > > I have, i see no error, the error appears only when i append the charset > directive into the connector, if i remove it the error dissapears. > -- > http://mail.python.org/mailman/listinfo/python-list > http://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect Could this be the problem? ;) -- Joel Goldstick http://joelgoldstick.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From torriem at gmail.com Tue May 28 14:19:21 2013 From: torriem at gmail.com (Michael Torrie) Date: Tue, 28 May 2013 12:19:21 -0600 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> Message-ID: <51A4F529.6090801@gmail.com> On 05/28/2013 12:08 PM, nagia.retsina at gmail.com wrote: > ?? ?????, 28 ????? 2013 8:17:05 ?.?. UTC+3, ? ??????? Michael Torrie > ??????: > >> Basically you want pelatologio.py to run and then you process the >> output through a template, correct? > > That is correct. > >> Inside pelatologio.py there is some object that you are trying to >> reference the "id" attribute on it does not contain id. So the >> problem is in pelatologio.py. Double check your code there. Try >> to make a standalone test file you can run locally. > > I have, i see no error, the error appears only when i append the > charset directive into the connector, if i remove it the error > dissapears. No, when you remove the charset directive your database call fails and thus things fail even earlier. From nagia.retsina at gmail.com Tue May 28 15:03:48 2013 From: nagia.retsina at gmail.com (nagia.retsina at gmail.com) Date: Tue, 28 May 2013 12:03:48 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> Message-ID: <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> ?? ?????, 28 ????? 2013 9:19:21 ?.?. UTC+3, ? ??????? Michael Torrie ??????: > On 05/28/2013 12:08 PM, nagia.retsina at gmail.com wrote: > > > ?? ?????, 28 ????? 2013 8:17:05 ?.?. UTC+3, ? ??????? Michael Torrie > > > ??????: > > > > > >> Basically you want pelatologio.py to run and then you process the > > >> output through a template, correct? > > > > > > That is correct. > > > > > >> Inside pelatologio.py there is some object that you are trying to > > >> reference the "id" attribute on it does not contain id. So the > > >> problem is in pelatologio.py. Double check your code there. Try > > >> to make a standalone test file you can run locally. > > > > > > I have, i see no error, the error appears only when i append the > > > charset directive into the connector, if i remove it the error > > > dissapears. > > > > No, when you remove the charset directive your database call fails and > > thus things fail even earlier. Yes, it does create issues. i just tried to add it to metrites.py for testing and it breaks too. https://github.com/petehunt/PyMySQL/issues/38 Switching back to MySQLdb. From nagia.retsina at gmail.com Tue May 28 15:48:38 2013 From: nagia.retsina at gmail.com (nagia.retsina at gmail.com) Date: Tue, 28 May 2013 12:48:38 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> Message-ID: <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> But i think iam gonna fight it some more because mysqldb in python 3 has issues too :( From nagia.retsina at gmail.com Wed May 29 06:30:15 2013 From: nagia.retsina at gmail.com (nagia.retsina at gmail.com) Date: Wed, 29 May 2013 03:30:15 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> Message-ID: <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> What makes us o sure it is a pymysql issue and not python's encoding issue? From nagia.retsina at gmail.com Thu May 30 04:16:46 2013 From: nagia.retsina at gmail.com (nagia.retsina at gmail.com) Date: Thu, 30 May 2013 01:16:46 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <84fc3a0c-16d3-493e-bf65-217e9995ab26@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <84fc3a0c-16d3-493e-bf65-217e9995ab26@googlegroups.com> Message-ID: <6e432384-1ae4-418a-82c3-6e3f4664666b@googlegroups.com> Can ypou tell me how to install MySQLdb in python 3 using pip? pip install MySQLdb doesnt find the module. From torriem at gmail.com Thu May 30 05:29:56 2013 From: torriem at gmail.com (Michael Torrie) Date: Thu, 30 May 2013 03:29:56 -0600 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> Message-ID: <51A71C14.5010009@gmail.com> On 05/29/2013 04:30 AM, nagia.retsina at gmail.com wrote: > What makes us o sure it is a pymysql issue and not python's encoding > issue? The original traceback, which showed that the encoding error was happening in "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108. As was said, you solve that by passing a charset="utf-8" to the connection string. So doing that solved the encoding problem (a query is now being successfully built and sent to mysql) and went on to expose another problem (bug) in your code, but I cannot tell what that is, since the error happened in a subprocess and the traceback got sent to /dev/null. I suspect is has something to do with how the query results are being returned, or it could have something to do with the query itself. Python DB API does not specify exactly which style of prepared statements should be used by a given third-party module. So differences in syntax between how pymysql and MysqlDB define the variables could be the problem. In any case your course is clear. Run pelatologio.py outside of your templating system and see what the traceback says exactly now that the charset issue is fixed. From nagia.retsina at gmail.com Thu May 30 06:53:33 2013 From: nagia.retsina at gmail.com (nagia.retsina at gmail.com) Date: Thu, 30 May 2013 03:53:33 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> Message-ID: <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> ?? ??????, 30 ????? 2013 12:29:56 ?.?. UTC+3, ? ??????? Michael Torrie ??????: > On 05/29/2013 04:30 AM, nagia.retsina at gmail.com wrote: > > > What makes us o sure it is a pymysql issue and not python's encoding > > > issue? > > > > The original traceback, which showed that the encoding error was > > happening in > > "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108. > > As was said, you solve that by passing a charset="utf-8" to the > > connection string. > > > > So doing that solved the encoding problem (a query is now being > > successfully built and sent to mysql) and went on to expose another > > problem (bug) in your code, but I cannot tell what that is, since the > > error happened in a subprocess and the traceback got sent to /dev/null. > > I suspect is has something to do with how the query results are being > > returned, or it could have something to do with the query itself. > > Python DB API does not specify exactly which style of prepared > > statements should be used by a given third-party module. So differences > > in syntax between how pymysql and MysqlDB define the variables could be > > the problem. > > > > In any case your course is clear. Run pelatologio.py outside of your > > templating system and see what the traceback says exactly now that the > > charset issue is fixed. Good morning Michael, I'am afraid as much as you dont want to admin it that the moment i append the charset directive into the connections tring i receive a huge error which it can be displayed in: http://superhost.gr/cgi-bin/pelatologio.py This is run directly isolated form the templating system. From nagia.retsina at gmail.com Thu May 30 06:56:03 2013 From: nagia.retsina at gmail.com (nagia.retsina at gmail.com) Date: Thu, 30 May 2013 03:56:03 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> Message-ID: <34e69a6d-83a8-420a-a2a2-2a9ef64601ba@googlegroups.com> ?? ??????, 30 ????? 2013 1:53:33 ?.?. UTC+3, ? ??????? nagia.... at gmail.com ??????: > ?? ??????, 30 ????? 2013 12:29:56 ?.?. UTC+3, ? ??????? Michael Torrie ??????: > > > On 05/29/2013 04:30 AM, nagia.retsina at gmail.com wrote: > > > > > > > What makes us o sure it is a pymysql issue and not python's encoding > > > > > > > issue? > > > > > > > > > > > > The original traceback, which showed that the encoding error was > > > > > > happening in > > > > > > "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108. > > > > > > As was said, you solve that by passing a charset="utf-8" to the > > > > > > connection string. > > > > > > > > > > > > So doing that solved the encoding problem (a query is now being > > > > > > successfully built and sent to mysql) and went on to expose another > > > > > > problem (bug) in your code, but I cannot tell what that is, since the > > > > > > error happened in a subprocess and the traceback got sent to /dev/null. > > > > > > I suspect is has something to do with how the query results are being > > > > > > returned, or it could have something to do with the query itself. > > > > > > Python DB API does not specify exactly which style of prepared > > > > > > statements should be used by a given third-party module. So differences > > > > > > in syntax between how pymysql and MysqlDB define the variables could be > > > > > > the problem. > > > > > > > > > > > > In any case your course is clear. Run pelatologio.py outside of your > > > > > > templating system and see what the traceback says exactly now that the > > > > > > charset issue is fixed. > > > > Good morning Michael, > > > > I'am afraid as much as you dont want to admin it that the moment i append the charset directive into the connections tring i receive a huge error which it can be displayed in: > > > > http://superhost.gr/cgi-bin/pelatologio.py > > > > This is run directly isolated form the templating system. AttributeError: 'NoneType' object has no attribute 'id' args = ("'NoneType' object has no attribute 'id'",) with_traceback = is the error while i have no id varibale just an 'ID' inside: cur.execute('''SELECT ID FROM clients WHERE name = %s''', (name,) ) From rosuav at gmail.com Thu May 30 07:33:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 21:33:56 +1000 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> Message-ID: On Thu, May 30, 2013 at 8:53 PM, wrote: > Good morning Michael, > > I'am afraid as much as you dont want to admin it that the moment i append the charset directive into the connections tring i receive a huge error which it can be displayed in: > > http://superhost.gr/cgi-bin/pelatologio.py > > This is run directly isolated form the templating system. Your valid character set names can be found here: https://github.com/petehunt/PyMySQL/blob/master/pymysql/charset.py I found this simply by googling 'pymysql' and working through the call tree. Find what you want. Use it. You can probably get this information from the docs, too. Read them. ChrisA From support at superhost.gr Thu May 30 07:47:35 2013 From: support at superhost.gr (=?ISO-8859-7?B?zd/q7/Igw+rxMzPq?=) Date: Thu, 30 May 2013 04:47:35 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <69f5q8hkra42rvpfmbng4f0airgikqf5js@4ax.com> <9560d43f-8100-4914-9a71-c30ab479ec35@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> Message-ID: <56a24a19-e3ca-4188-a617-63da252917f3@googlegroups.com> ?? ??????, 30 ????? 2013 2:33:56 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > On Thu, May 30, 2013 at 8:53 PM, wrote: > > > Good morning Michael, > > > > > > I'am afraid as much as you dont want to admin it that the moment i append the charset directive into the connections tring i receive a huge error which it can be displayed in: > > > > > > http://superhost.gr/cgi-bin/pelatologio.py > > > > > > This is run directly isolated form the templating system. > > > > Your valid character set names can be found here: > > > > https://github.com/petehunt/PyMySQL/blob/master/pymysql/charset.py > > > > I found this simply by googling 'pymysql' and working through the call tree. > > > > Find what you want. Use it. > > > > You can probably get this information from the docs, too. Read them. > > > > ChrisA I cant "fucking believe it". The moen i switched "charset = 'utf-8'" => "charset = 'utf8'" all started to work properly! Thank you very much Chris, i cnat belive i was lookign 3 days for that error and it was a matter of a dash removal.....My God! From torriem at gmail.com Thu May 30 10:35:09 2013 From: torriem at gmail.com (Michael Torrie) Date: Thu, 30 May 2013 08:35:09 -0600 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <56a24a19-e3ca-4188-a617-63da252917f3@googlegroups.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> <56a24a19-e3ca-4188-a617-63da252917f3@googlegroups.com> Message-ID: <51A7639D.4000104@gmail.com> On 05/30/2013 05:47 AM, ????? ???33? wrote: > The moen i switched "charset = 'utf-8'" => "charset = 'utf8'" all > started to work properly! Glad you have it working. Perhaps this should be a lesson to you, Nick. Chris was able to spot your problem by READING THE DOCUMENTATION, which he probably found through some quick searching on Google. These are things you can and should do too. As well as looking at the source code of the modules you may have downloaded and are using, and understanding how the code works (or fits together) and how to debug. You seem to have a tendency to try to attack your problems with rather blunt instruments, and then come to the list where one of the list members invariable does the footwork for you. From rosuav at gmail.com Thu May 30 10:40:01 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 00:40:01 +1000 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <51A7639D.4000104@gmail.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> <56a24a19-e3ca-4188-a617-63da252917f3@googlegroups.com> <51A7639D.4000104@gmail.com> Message-ID: On Fri, May 31, 2013 at 12:35 AM, Michael Torrie wrote: > On 05/30/2013 05:47 AM, ????? ???33? wrote: >> The moen i switched "charset = 'utf-8'" => "charset = 'utf8'" all >> started to work properly! > > Glad you have it working. > > Perhaps this should be a lesson to you, Nick. Chris was able to spot > your problem by READING THE DOCUMENTATION, which he probably found > through some quick searching on Google. These are things you can and > should do too. As well as looking at the source code of the modules you > may have downloaded and are using, and understanding how the code works > (or fits together) and how to debug. Well, actually I didn't find the documentation when I did those quick web searches, which is why I ended up looking at the source. That probably doubled the time requirement, and roughly doubled the minimum skill points to cast this spell (programming is an RPG, right?), but if he's actively using the module, he probably knows where to find its docs. ChrisA From torriem at gmail.com Thu May 30 13:01:25 2013 From: torriem at gmail.com (Michael Torrie) Date: Thu, 30 May 2013 11:01:25 -0600 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> <56a24a19-e3ca-4188-a617-63da252917f3@googlegroups.com> <51A7639D.4000104@gmail.com> Message-ID: <51A785E5.7060509@gmail.com> On 05/30/2013 08:40 AM, Chris Angelico wrote: > but if he's actively using the module, he probably knows where to > find its docs. One would hope, but alas one probably hopes in vain. I'm not sure he wants to spend the time to read the code he's using and understand. He's in too much of a hurry to simply get a result. From rosuav at gmail.com Thu May 30 13:28:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 03:28:56 +1000 Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: <51A785E5.7060509@gmail.com> References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> <56a24a19-e3ca-4188-a617-63da252917f3@googlegroups.com> <51A7639D.4000104@gmail.com> <51A785E5.7060509@gmail.com> Message-ID: On Fri, May 31, 2013 at 3:01 AM, Michael Torrie wrote: > On 05/30/2013 08:40 AM, Chris Angelico wrote: >> but if he's actively using the module, he probably knows where to >> find its docs. > > One would hope, but alas one probably hopes in vain. I'm not sure he > wants to spend the time to read the code he's using and understand. > He's in too much of a hurry to simply get a result. Wonder how much less exciting this mailing list would be if he switched to decaf... *ducks for cover* ChrisA From nikos.gr33k at gmail.com Fri May 31 11:13:09 2013 From: nikos.gr33k at gmail.com (=?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?=) Date: Fri, 31 May 2013 08:13:09 -0700 (PDT) Subject: Encodign issue in Python 3.3.1 (once again) In-Reply-To: References: <7823093c-2c07-4fa0-ae97-960c62f8ff9d@googlegroups.com> <0e3ef4e1-800f-40fc-8cdb-f9d1467266b0@googlegroups.com> <2217da95-178b-4af1-b816-af3ac5cb5f07@googlegroups.com> <332e8acf-7484-49ba-b2e9-5861a001c2e6@googlegroups.com> <65dff60d-5b85-47ab-8f9b-f5578998e807@googlegroups.com> <626eb9fc-3d4c-4818-8ce0-67562c9ed5b7@googlegroups.com> <56a24a19-e3ca-4188-a617-63da252917f3@googlegroups.com> <51A7639D.4000104@gmail.com> <51A785E5.7060509@gmail.com> Message-ID: <8572c6d7-0e3a-4147-be2a-9c890e1e6671@googlegroups.com> ?? ??????, 30 ????? 2013 8:28:56 ?.?. UTC+3, ? ??????? Chris Angelico ??????: > Wonder how much less exciting this mailing list would be if he > switched to decaf... decaf is like tasting coffee without coffee! Caffeine gives the coffee a nice taste and make me sweaty and panik too if when i struggle to find something :) > *ducks for cover* You don't have to hide :) I'am not after you yet! From rvince99 at gmail.com Sun May 26 13:52:14 2013 From: rvince99 at gmail.com (RVic) Date: Sun, 26 May 2013 10:52:14 -0700 (PDT) Subject: Cutting a deck of cards Message-ID: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Suppose I have a deck of cards, and I shuffle them import random cards = [] decks = 6 cards = list(range(13 * 4 * decks)) random.shuffle(cards) So now I have an array of cards. I would like to cut these cards at some random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that to the top half of the cards array. For some reason, I can't see how this can be done (I know that it must be a simple line or two in Python, but I am really stuck here). Anyone have any direction they can give me on this? Thanks, RVic, python newbie From python at mrabarnett.plus.com Sun May 26 14:04:52 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 26 May 2013 19:04:52 +0100 Subject: Cutting a deck of cards In-Reply-To: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: <51A24EC4.7030904@mrabarnett.plus.com> On 26/05/2013 18:52, RVic wrote: > Suppose I have a deck of cards, and I shuffle them > > import random > cards = [] > decks = 6 > cards = list(range(13 * 4 * decks)) > random.shuffle(cards) > > So now I have an array of cards. I would like to cut these cards at some random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that to the top half of the cards array. > > For some reason, I can't see how this can be done (I know that it must be a simple line or two in Python, but I am really stuck here). Anyone have any direction they can give me on this? Thanks, RVic, python newbie > The list from its start up to, but excluding, index 'i' is cards[ : i], and the list from index 'i' to its end is cards[i : ]. Now concatenate them those slices. From mkamlesh at gmail.com Sun May 26 14:05:51 2013 From: mkamlesh at gmail.com (Kamlesh Mutha) Date: Sun, 26 May 2013 23:35:51 +0530 Subject: Cutting a deck of cards In-Reply-To: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: I guess, you will have to use list slicing mechanism to achieve the desired result. Hope this helps, Cheers, Kamlesh On Sun, May 26, 2013 at 11:22 PM, RVic wrote: > Suppose I have a deck of cards, and I shuffle them > > import random > cards = [] > decks = 6 > cards = list(range(13 * 4 * decks)) > random.shuffle(cards) > > So now I have an array of cards. I would like to cut these cards at some > random point (between 1 and 13 * 4 * decks - 1, moving the lower half of > that to the top half of the cards array. > > For some reason, I can't see how this can be done (I know that it must be > a simple line or two in Python, but I am really stuck here). Anyone have > any direction they can give me on this? Thanks, RVic, python newbie > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Faith waiting in the heart of a seed promises a miracle of life which it can not prove! -Ravindranath Tagore -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Sun May 26 14:16:22 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 21:16:22 +0300 Subject: Cutting a deck of cards In-Reply-To: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: ---------------------------------------- > Date: Sun, 26 May 2013 10:52:14 -0700 > Subject: Cutting a deck of cards > From: rvince99 at gmail.com > To: python-list at python.org > > Suppose I have a deck of cards, and I shuffle them > > import random > cards = [] > decks = 6 > cards = list(range(13 * 4 * decks)) > random.shuffle(cards) > > So now I have an array of cards. I would like to cut these cards at some random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that to the top half of the cards array. > > For some reason, I can't see how this can be done (I know that it must be a simple line or two in Python, but I am really stuck here). Anyone have any direction they can give me on this? Thanks, RVic, python newbie > > -- > http://mail.python.org/mailman/listinfo/python-list list(range(13 * 4 * decks)) == range(13 * 4 * decks) ;) From ian.g.kelly at gmail.com Sun May 26 14:47:02 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 26 May 2013 18:47:02 -0000 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 12:16 PM, Carlos Nepomuceno wrote: > list(range(13 * 4 * decks)) == range(13 * 4 * decks) Not in Python 3. From rosuav at gmail.com Sun May 26 15:04:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 26 May 2013 19:04:25 -0000 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: On Mon, May 27, 2013 at 4:16 AM, Carlos Nepomuceno wrote: > list(range(13 * 4 * decks)) == range(13 * 4 * decks) > > ;) Not in Python 3. ChrisA From breamoreboy at yahoo.co.uk Sun May 26 18:17:56 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 23:17:56 +0100 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: On 26/05/2013 19:16, Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Sun, 26 May 2013 10:52:14 -0700 >> Subject: Cutting a deck of cards >> From: rvince99 at gmail.com >> To: python-list at python.org >> >> Suppose I have a deck of cards, and I shuffle them >> >> import random >> cards = [] >> decks = 6 >> cards = list(range(13 * 4 * decks)) >> random.shuffle(cards) >> >> So now I have an array of cards. I would like to cut these cards at some random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that to the top half of the cards array. >> >> For some reason, I can't see how this can be done (I know that it must be a simple line or two in Python, but I am really stuck here). Anyone have any direction they can give me on this? Thanks, RVic, python newbie >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > list(range(13 * 4 * decks)) == range(13 * 4 * decks) > > ;) > Wrong if you're using Python 3 :( -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From carlosnepomuceno at outlook.com Sun May 26 18:30:53 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 27 May 2013 01:30:53 +0300 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com>, , Message-ID: ---------------------------------------- > To: python-list at python.org > From: breamoreboy at yahoo.co.uk [...] > Wrong if you're using Python 3 :( > > -- > If you're using GoogleCrap? please read this > http://wiki.python.org/moin/GoogleGroupsPython. > > Mark Lawrence Thanks guys! I've been delaying my dive into Python 3 (because I don't need it for now) but I'd like to run some code just to learn how different it is from Python 2 and even other Python flavors. So, I'd like to know if it's possible to have multiple Python installations on the same machine (Windows 7 in my particular case) without messing one with each other. What care must I take not to mess up with them? I've just found this[1] awesome service, but ridiculously it's PHP powered!!! lol Come on!!! Why there aren't more Python powered websites available? What's the catch? [1] http://www.compileonline.com/execute_python3_online.php From rosuav at gmail.com Sun May 26 18:42:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 08:42:56 +1000 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: On Mon, May 27, 2013 at 8:30 AM, Carlos Nepomuceno wrote: > Thanks guys! I've been delaying my dive into Python 3 (because I don't need it for now) but I'd like to run some code just to learn how different it is from Python 2 and even other Python flavors. > > So, I'd like to know if it's possible to have multiple Python installations on the same machine (Windows 7 in my particular case) without messing one with each other. What care must I take not to mess up with them? Easy. Just grab the standard installer and hit it. You'll get two separate directories (or more; I have \Python26, \Python27, \Python32, \Python33 on this box), and you can run whichever you want. The one thing to take care of is .py associations; I haven't actually done it (on here, all I actually do is IDLE, pretty much), but as of 3.3, you should be able to use a Unix-style shebang to indicate which Python you want to invoke. ChrisA From carlosnepomuceno at outlook.com Sun May 26 18:51:25 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 27 May 2013 01:51:25 +0300 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com>, , , , Message-ID: ---------------------------------------- > Date: Mon, 27 May 2013 08:42:56 +1000 > Subject: Re: Cutting a deck of cards > From: rosuav at gmail.com [...] > Easy. Just grab the standard installer and hit it. You'll get two > separate directories (or more; I have \Python26, \Python27, \Python32, > \Python33 on this box), and you can run whichever you want. The one > thing to take care of is .py associations; I haven't actually done it > (on here, all I actually do is IDLE, pretty much), but as of 3.3, you > should be able to use a Unix-style shebang to indicate which Python > you want to invoke. > > ChrisA I'm not even using shebangs in Windows because I thought it wouldn't make any difference. I'm used to run scripts like "python filename.py" from the command line. No problem! So, Python 3.3 will honor if I insert "#!C:\Python27\python.exe"? if I install it after Python 2.7? Cool!!! From breamoreboy at yahoo.co.uk Sun May 26 18:56:37 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 23:56:37 +0100 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: On 26/05/2013 23:42, Chris Angelico wrote: > On Mon, May 27, 2013 at 8:30 AM, Carlos Nepomuceno > wrote: >> Thanks guys! I've been delaying my dive into Python 3 (because I don't need it for now) but I'd like to run some code just to learn how different it is from Python 2 and even other Python flavors. >> >> So, I'd like to know if it's possible to have multiple Python installations on the same machine (Windows 7 in my particular case) without messing one with each other. What care must I take not to mess up with them? > > Easy. Just grab the standard installer and hit it. You'll get two > separate directories (or more; I have \Python26, \Python27, \Python32, > \Python33 on this box), and you can run whichever you want. The one > thing to take care of is .py associations; I haven't actually done it > (on here, all I actually do is IDLE, pretty much), but as of 3.3, you > should be able to use a Unix-style shebang to indicate which Python > you want to invoke. > > ChrisA > See this http://docs.python.org/3/whatsnew/3.3.html#pep-397-python-launcher-for-windows -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From carlosnepomuceno at outlook.com Sun May 26 19:07:13 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 23:07:13 -0000 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com>, , , , , Message-ID: ---------------------------------------- > To: python-list at python.org > From: breamoreboy at yahoo.co.uk [...] > See this > http://docs.python.org/3/whatsnew/3.3.html#pep-397-python-launcher-for-windows > > -- > If you're using GoogleCrap? please read this > http://wiki.python.org/moin/GoogleGroupsPython. > > Mark Lawrence Piece of cake! So, is there any risk of breaking anything if I install a new package after having both Puthon 2 and 3 installed? How do I choose which one will run the package? Will the package handle the settings accordingly or will I need to setup manually? From rvince99 at gmail.com Sun May 26 14:17:36 2013 From: rvince99 at gmail.com (RVic) Date: Sun, 26 May 2013 11:17:36 -0700 (PDT) Subject: Cutting a deck of cards In-Reply-To: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: Ah, brilliant -- yes, this is so much more elegant in Python: #now cut the cards x = random.randrange(2,range(13 * 4 * decks)) cards = cards[x:]+cards[:x] From ian.g.kelly at gmail.com Sun May 26 14:28:59 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 26 May 2013 18:28:59 -0000 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: On Sun, May 26, 2013 at 12:17 PM, RVic wrote: > Ah, brilliant -- yes, this is so much more elegant in Python: > > #now cut the cards > x = random.randrange(2,range(13 * 4 * decks)) > cards = cards[x:]+cards[:x] Or if for some reason you want to do it in place: cards[x:], cards[:x] = cards[:x], cards[x:] But note that the order of assignments is subtly important there, so unless you have a good reason for doing that, it's probably better just to create a new list for clarity. From roy at panix.com Sun May 26 14:21:29 2013 From: roy at panix.com (Roy Smith) Date: Sun, 26 May 2013 14:21:29 -0400 Subject: Cutting a deck of cards References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: In article <4d02f46f-8264-41bf-a254-d1c20469626e at googlegroups.com>, RVic wrote: > Suppose I have a deck of cards, and I shuffle them > > import random > cards = [] > decks = 6 > cards = list(range(13 * 4 * decks)) > random.shuffle(cards) > > So now I have an array of cards. I would like to cut these cards at some > random point (between 1 and 13 * 4 * decks - 1, moving the lower half of that > to the top half of the cards array. > > For some reason, I can't see how this can be done (I know that it must be a > simple line or two in Python, but I am really stuck here). Anyone have any > direction they can give me on this? Thanks, RVic, python newbie import random i = random.randrange(len(cards)) cut1 = cards[:i] cut2 = cards[i:] I haven't thought too much about the boundary conditions, but that's the general idea. From usenetmail at solar-empire.de Sun May 26 15:36:42 2013 From: usenetmail at solar-empire.de (Marc Christiansen) Date: Sun, 26 May 2013 21:36:42 +0200 Subject: Cutting a deck of cards References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: Carlos Nepomuceno wrote: > ---------------------------------------- >> Date: Sun, 26 May 2013 10:52:14 -0700 >> Subject: Cutting a deck of cards >> From: rvince99 at gmail.com >> To: python-list at python.org >> >> Suppose I have a deck of cards, and I shuffle them >> >> import random >> cards = [] >> decks = 6 >> cards = list(range(13 * 4 * decks)) >> random.shuffle(cards) >> >> So now I have an array of cards. I would like to cut these cards at >> some random point (between 1 and 13 * 4 * decks - 1, moving the lower >> half of that to the top half of the cards array. >> >> For some reason, I can't see how this can be done (I know that it >> must be a simple line or two in Python, but I am really stuck here). >> Anyone have any direction they can give me on this? Thanks, RVic, >> python newbie >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > list(range(13 * 4 * decks)) == range(13 * 4 * decks) > > ;) Not in Python3.x >>> decks = 6 >>> list(range(13 * 4 * decks)) == range(13 * 4 * decks) False Adia? Marc From carlosnepomuceno at outlook.com Sun May 26 15:54:44 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 22:54:44 +0300 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com>, , Message-ID: ---------------------------------------- > From: usenetmail at solar-empire.de [...] > Not in Python3.x >>>> decks = 6 >>>> list(range(13 * 4 * decks)) == range(13 * 4 * decks) > False > > Adia? > Marc What does "list(range(13 * 4 * decks))" returns in Python 3? From tjreedy at udel.edu Sun May 26 17:03:39 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Sun, 26 May 2013 17:03:39 -0400 Subject: Cutting a deck of cards In-Reply-To: References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com>, , Message-ID: On 5/26/2013 3:54 PM, Carlos Nepomuceno wrote: > ---------------------------------------- >> From: usenetmail at solar-empire.de > [...] >> Not in Python3.x >>>>> decks = 6 >>>>> list(range(13 * 4 * decks)) == range(13 * 4 * decks) >> False >> >> Adia? >> Marc > > > What does "list(range(13 * 4 * decks))" returns in Python 3? A list, obviously. What you should ask is what range returns in Python 3, and you should install python 3 and try it, and list its attributes. From usenetmail at solar-empire.de Sun May 26 16:13:55 2013 From: usenetmail at solar-empire.de (Marc Christiansen) Date: Sun, 26 May 2013 22:13:55 +0200 Subject: Cutting a deck of cards References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: <317a7a-jot.ln1@pluto.solar-empire.de> Carlos Nepomuceno wrote: > ---------------------------------------- >> From: usenetmail at solar-empire.de > [...] >> Not in Python3.x >>>>> decks = 6 >>>>> list(range(13 * 4 * decks)) == range(13 * 4 * decks) >> False > > What does "list(range(13 * 4 * decks))" returns in Python 3? A list of course. But Py3 range is very similar to Py2 xrange, it returns a range object. Adia? Marc From usenetmail at solar-empire.de Sun May 26 16:13:55 2013 From: usenetmail at solar-empire.de (Marc Christiansen) Date: Sun, 26 May 2013 22:13:55 +0200 Subject: Cutting a deck of cards References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: <317a7a-jot.ln1@pluto.solar-empire.de> Carlos Nepomuceno wrote: > ---------------------------------------- >> From: usenetmail at solar-empire.de > [...] >> Not in Python3.x >>>>> decks = 6 >>>>> list(range(13 * 4 * decks)) == range(13 * 4 * decks) >> False > > What does "list(range(13 * 4 * decks))" returns in Python 3? A list of course. But Py3 range is very similar to Py2 xrange, it returns a range object. Adia? Marc From carlosnepomuceno at outlook.com Sun May 26 16:35:51 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Sun, 26 May 2013 20:35:51 -0000 Subject: Cutting a deck of cards In-Reply-To: <317a7a-jot.ln1@pluto.solar-empire.de> References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com>, , , , <317a7a-jot.ln1@pluto.solar-empire.de> Message-ID: ---------------------------------------- > From: usenetmail at solar-empire.de > Subject: Re: Cutting a deck of cards > Date: Sun, 26 May 2013 22:13:55 +0200 > To: python-list at python.org > > Carlos Nepomuceno wrote: >> ---------------------------------------- >>> From: usenetmail at solar-empire.de >> [...] >>> Not in Python3.x >>>>>> decks = 6 >>>>>> list(range(13 * 4 * decks)) == range(13 * 4 * decks) >>> False >> >> What does "list(range(13 * 4 * decks))" returns in Python 3? > > A list of course. But Py3 range is very similar to Py2 xrange, it > returns a range object. > > Adia? > Marc What list? '[[0,1,2,...]]' or '[0,1,2,...]'? If it's the later then it's no different than what range() returns in Python 2.7.5! From leedanielcrocker at gmail.com Fri May 31 07:56:50 2013 From: leedanielcrocker at gmail.com (Lee Crocker) Date: Fri, 31 May 2013 04:56:50 -0700 (PDT) Subject: Cutting a deck of cards In-Reply-To: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> Message-ID: <4286e1af-f97c-4111-ab3d-cdf2e2fa5fa8@googlegroups.com> Why on Earth would you want to? "Cutting" a deck makes no sense in software. Randomize the deck properly (Google "Fisher-Yates") and start dealing. Cutting the deck will not make it any more random, and in fact will probably make it worse depending on how you choose the cutpoint. The purpose of "cutting" cards is to make it more difficult for human dealers to stack a deck. Simulating it in software makes no more sense than simulating the cigars you smoke while playing. From modulok at gmail.com Fri May 31 14:54:22 2013 From: modulok at gmail.com (Modulok) Date: Fri, 31 May 2013 12:54:22 -0600 Subject: Cutting a deck of cards In-Reply-To: <4286e1af-f97c-4111-ab3d-cdf2e2fa5fa8@googlegroups.com> References: <4d02f46f-8264-41bf-a254-d1c20469626e@googlegroups.com> <4286e1af-f97c-4111-ab3d-cdf2e2fa5fa8@googlegroups.com> Message-ID: > Why on Earth would you want to? "Cutting" a deck makes no sense in > software. Randomize the deck properly (Google "Fisher-Yates") and start > dealing. Cutting the deck will not make it any more random, and in fact > will probably make it worse depending on how you choose the cutpoint. > > The purpose of "cutting" cards is to make it more difficult for human > dealers to stack a deck. Simulating it in software makes no more sense than > simulating the cigars you smoke while playing. > > Perhaps the OP wanted to study the efficiency and affect of a real-world shuffling algorithm :-p Maybe he was designing a probabilistic magic trick and needed to evaluate how a cut would modify the outcome of a particular stack. Maybe it was a school assignment. Who knows? (But yeah if the purpose was for pure randomization then there's no real point.) There could be a lot of legitimate reasons though. -Modulok- -------------- next part -------------- An HTML attachment was scrubbed... URL: From danielgagliardiramos at gmail.com Sun May 26 15:10:28 2013 From: danielgagliardiramos at gmail.com (Daniel Gagliardi) Date: Sun, 26 May 2013 16:10:28 -0300 Subject: I want to know how to implement concurrent threads in Python Message-ID: I want to know how to implement concurrent threads in Python -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Sun May 26 17:10:51 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sun, 26 May 2013 16:10:51 -0500 Subject: I want to know how to implement concurrent threads in Python In-Reply-To: References: Message-ID: <51A27A5B.1030304@gmail.com> On 2013.05.26 14:10, Daniel Gagliardi wrote: > I want to know how to implement concurrent threads in Python With the threading module in the standard library. http://docs.python.org/3.3/library/threading.html There are plenty of tutorials on this out there; we'll be happy to help if you're stuck on something specific. -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From bahamutzero8825 at gmail.com Sun May 26 17:27:16 2013 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sun, 26 May 2013 16:27:16 -0500 Subject: I want to know how to implement concurrent threads in Python In-Reply-To: References: <51A27A5B.1030304@gmail.com> Message-ID: <51A27E34.2020406@gmail.com> On 2013.05.26 16:21, Daniel Gagliardi wrote: > shutup bitch! i do know python cannot concurrent threads. want a workaround You're a charming fellow. I'm sure everyone will flock to help you. From breamoreboy at yahoo.co.uk Sun May 26 18:24:45 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 23:24:45 +0100 Subject: I want to know how to implement concurrent threads in Python In-Reply-To: <51A27E34.2020406@gmail.com> References: <51A27A5B.1030304@gmail.com> <51A27E34.2020406@gmail.com> Message-ID: On 26/05/2013 22:27, Andrew Berg wrote: > On 2013.05.26 16:21, Daniel Gagliardi wrote: >> shutup bitch! i do know python cannot concurrent threads. want a workaround > You're a charming fellow. I'm sure everyone will flock to help you. > So "How to win friends and influence people" had two authors. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From breamoreboy at yahoo.co.uk Sun May 26 18:25:42 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 May 2013 23:25:42 +0100 Subject: I want to know how to implement concurrent threads in Python In-Reply-To: References: Message-ID: On 26/05/2013 20:10, Daniel Gagliardi wrote: > I want to know how to implement concurrent threads in Python > google, bing, duckduckgo, yahoo... -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From danielgagliardiramos at gmail.com Mon May 27 23:58:58 2013 From: danielgagliardiramos at gmail.com (Daniel Gagliardi) Date: Tue, 28 May 2013 00:58:58 -0300 Subject: I want to know how to implement concurrent threads in Python In-Reply-To: References: Message-ID: fuck! fuck! i'm gonna be fired if i didnt get this shit! i told my boss id do it. fuck! im gonna pipe some crakc. fuck... 2013/5/26 Mark Lawrence > On 26/05/2013 20:10, Daniel Gagliardi wrote: > >> I want to know how to implement concurrent threads in Python >> >> > google, bing, duckduckgo, yahoo... > > > -- > If you're using GoogleCrap? please read this http://wiki.python.org/moin/* > *GoogleGroupsPython . > > Mark Lawrence > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiosantosart at gmail.com Tue May 28 02:02:23 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 07:02:23 +0100 Subject: I want to know how to implement concurrent threads in Python In-Reply-To: References: Message-ID: On 28 May 2013 05:06, "Daniel Gagliardi" wrote: > > fuck! fuck! i'm gonna be fired if i didnt get this shit! i told my boss id do it. fuck! im gonna pipe some crakc. fuck... So do it. You've already been told how to. It's true that python does not do real concurrent execution, but if most of your threads spend most time sleeping, waiting on a socket or doing IO, those are opportunities for other threads to cut in and execute concurrently and the threading module is your best choice. If you need to do cpu-intensive tasks concurrently, use the multiprocessing module. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ulrich.eckhardt at dominolaser.com Tue May 28 03:49:50 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Tue, 28 May 2013 09:49:50 +0200 Subject: I want to know how to implement concurrent threads in Python In-Reply-To: References: Message-ID: Am 26.05.2013 21:10, schrieb Daniel Gagliardi: > I want to know how to implement concurrent threads in Python Have you tried searching the web or maybe looked on docs.python.org? Seriously, show at least some effort before asking here. Uli From carlosnepomuceno at outlook.com Sun May 26 19:13:54 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 27 May 2013 02:13:54 +0300 Subject: Python error codes and messages location Message-ID: Where can I find all error codes and messages that Python throws (actual codes and messages from exceptions raised by stdlib)? I've already found the module 'errno' and got a dictionary (errno.errorcode) and some system error messages (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find. From steve+comp.lang.python at pearwood.info Sun May 26 20:53:41 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 May 2013 00:53:41 GMT Subject: Python error codes and messages location References: Message-ID: <51a2ae95$0$30002$c3e8da3$5496439d@news.astraweb.com> On Mon, 27 May 2013 02:13:54 +0300, Carlos Nepomuceno wrote: > Where can I find all error codes and messages that Python throws (actual > codes and messages from exceptions raised by stdlib)? There is no list. It is subject to change from version to version, including point releases. Many functions and methods document which exceptions they can be expected to raise, or at least the *usual* exceptions, but many more do not. And the error messages themselves are implementation details and are subject to change without notice, even if the exception type is a documented part of the API. You can see a full list of built-in exception types in the docs: http://docs.python.org/3/library/exceptions.html but of course since they are classes, they can be subclassed, and in principle a function that raises (say) ValueError might sometimes raise a subclass of ValueError without documenting it. So the actual answer to your question is: "Read the source code." Sorry :-( > I've already found the module 'errno' and got a dictionary > (errno.errorcode) and some system error messages > (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find. These are the standard C operating system and file system error codes, not Python exceptions. -- Steven From carlosnepomuceno at outlook.com Sun May 26 21:49:28 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 27 May 2013 04:49:28 +0300 Subject: Python error codes and messages location In-Reply-To: <51a2ae95$0$30002$c3e8da3$5496439d@news.astraweb.com> References: , <51a2ae95$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: Python error codes and messages location > Date: Mon, 27 May 2013 00:53:41 +0000 > To: python-list at python.org > > On Mon, 27 May 2013 02:13:54 +0300, Carlos Nepomuceno wrote: > >> Where can I find all error codes and messages that Python throws (actual >> codes and messages from exceptions raised by stdlib)? > > There is no list. It is subject to change from version to version, > including point releases. > > Many functions and methods document which exceptions they can be expected > to raise, or at least the *usual* exceptions, but many more do not. And > the error messages themselves are implementation details and are subject > to change without notice, even if the exception type is a documented part > of the API. > > You can see a full list of built-in exception types in the docs: > > http://docs.python.org/3/library/exceptions.html > > but of course since they are classes, they can be subclassed, and in > principle a function that raises (say) ValueError might sometimes raise a > subclass of ValueError without documenting it. > > So the actual answer to your question is: > > "Read the source code." > > > Sorry :-( That's bad! I'd like to check all the IOError codes that may be raised by a function/method but the information isn't there. Take open() for example[1]. It only says it raises an IOError exception. I've had to try "open('','r')" to discover that Errno 22 is the one "IOError: [Errno 22] invalid mode ('r') or filename: ''" Will I only be able to get all error codes reading the source code of open()? Is there a way to simulate the errors instead of actually causing them? I mean not raising an instance of IOError that I create (like "raise IOError(666,'Hell happens')"), but one with the actual contents 'args = (errno, strerr)' that open() would raise? Something like: f=open('filename','r') for x in range(25): ??? try: ??????? f.raise_errno(x) ??? except IOError, e: ??????? if?? e.errno == 1: ??????????? treat_err1() ??????????? continue ??????? elif e.errno == 2: ??????????? treat_err2() ??????????? continue ... [1] http://docs.python.org/2/library/functions.html#open >> I've already found the module 'errno' and got a dictionary >> (errno.errorcode) and some system error messages >> (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find. > > These are the standard C operating system and file system error codes, > not Python exceptions. Yes, the docs say it's from "linux/include/errno.h". From cs at zip.com.au Mon May 27 02:11:54 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 27 May 2013 16:11:54 +1000 Subject: Python error codes and messages location In-Reply-To: References: Message-ID: <20130527061154.GA3301@cskk.homeip.net> On 27May2013 04:49, Carlos Nepomuceno wrote: | > From: steve+comp.lang.python at pearwood.info | > On Mon, 27 May 2013 02:13:54 +0300, Carlos Nepomuceno wrote: | >> Where can I find all error codes and messages that Python throws (actual | >> codes and messages from exceptions raised by stdlib)? | > | > There is no list. It is subject to change from version to version, | > including point releases. [...] | > "Read the source code." [...] | | That's bad! I'd like to check all the IOError codes that may be | raised by a function/method but the information isn't there. No, you really don't. There are particular, expected, errors you know what to do with and can proceed in the face of. And there will often be errors you _don't_ know what to do with. With the latter, what is your program going to do? You often can't proceed, so all you can do is log the error and cancel, cleaning up as you back out. In that circumstance, how will knowing the name of the error help? You've logged it, and hopefully the exception message is sufficient. | Take open() for example[1]. It only says it raises an IOError exception. | | I've had to try "open('','r')" to discover that Errno 22 is the one "IOError: [Errno 22] invalid mode ('r') or filename: ''" It is vague because that is an OS level error, and errno 22 is EINVAL. For example, MacOSX says (in "man 2 intro"): 22 EINVAL Invalid argument. Some invalid argument was supplied. (For example, specifying an undefined signal to a signal or kill func? tion). It happens that above, the filename '' is invalid. But so what? That call is _never_ going to work because the arguments are invalid. So you response will be the same, programmatically, as with many other errors: log and abort. | Will I only be able to get all error codes reading the source code of open()? Nope. Well, not directly. Python open call's the OS' open. If that succeeds, Python does some more housekeeping and hands you a "file" python object. But if the OS open fails, Python reports the OS error. From the Python source you will see that the OS open is called. So you must then go to the OS docuemntation for further information. | Is there a way to simulate the errors instead of actually causing | them? I mean not raising an instance of IOError that I create (like | "raise IOError(666,'Hell happens')"), but one with the actual | contents 'args = (errno, strerr)' that open() would raise? | | Something like: | | f=open('filename','r') | for x in range(25): | ??? try: | ??????? f.raise_errno(x) | ??? except IOError, e: | ??????? if?? e.errno == 1: | ??????????? treat_err1() | ??????????? continue | ??????? elif e.errno == 2: | ??????????? treat_err2() | ??????????? continue Well, maybe this (untested): for x in range(25): ??? try: ??????? exc = OSError(os.strerror(x)) exc.errno = x raise exc ??? except IOError, e: ??????? if?? e.errno == 1: ??????????? treat_err1() ??????????? continue ??????? elif e.errno == 2: ??????????? treat_err2() ??????????? continue BTW, I recommend importing "errno" and using symbolic names. It makes things much more readable, and accomodates the situation where the symbols map to different numbers on different platforms. And have a catch-all. For example: import errno ... try: f = open(filename, .....) except OSError, e: if e.errno == errno.EPERM: ... Operation not permitted ... elif e.errno == errno.ENOENT: ... No such file or directory ... else: # surprise! # log message, let the exception get out # callers can catch things in turn logging.error("open(%s) fails: %s" % (filename, e)) raise | >> I've already found the module 'errno' and got a dictionary | >> (errno.errorcode) and some system error messages | >> (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find. | > | > These are the standard C operating system and file system error codes, | > not Python exceptions. | | Yes, the docs say it's from "linux/include/errno.h". On Linux, yes. In general, on any UNIX system the command "man 2 intro" lists the errors that system calls like open may return. And "man 2 open" should list the particular errors that open can return. _On that platform_. The platform variation is a major reason why you should not hope to enumerate all the errors; the other reason is that you can't treat every error individually in a fine grained fashion without making your code hard to read and maintain: the core logic of the program is hidden behind a forest of tiny little special cases. Your code will be clearer if you handle the errors that are expected and that permit the program to proceed sensible. Other errors should elicit a log message of course, but if you can't proceed with the task at hand you can almost certainly only abort and try to tidy up while doing so. Cheers, -- Processes are like potatoes. - NCR device driver manual From rosuav at gmail.com Mon May 27 03:47:45 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 27 May 2013 17:47:45 +1000 Subject: Python error codes and messages location In-Reply-To: <20130527061154.GA3301@cskk.homeip.net> References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On Mon, May 27, 2013 at 4:11 PM, Cameron Simpson wrote: > On 27May2013 04:49, Carlos Nepomuceno wrote: > | That's bad! I'd like to check all the IOError codes that may be > | raised by a function/method but the information isn't there. > > No, you really don't. Heh. I concur. Opening a file can generate roughly forty-two million different errors. Here's a smattering that you might not be expecting: * The path points somewhere that's not currently mounted * The path represents a device that is not properly working * It's over a network and the server at the far end is down * It's over a network and the server sends back maliciously crafted data * It's over a network and the server sends back an incorrect error code * You tried to open something that doesn't exist, and your disk quota is used up * The system has run out of handles * You're trying to create something in the root directory of a FAT volume that already has too many root directory entries And plenty more besides. Those are just a few that I could come up with off the top of my head. Just catch whatever makes sense (IOError maybe), and don't sweat the details. ChrisA From breamoreboy at yahoo.co.uk Mon May 27 07:37:12 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 27 May 2013 12:37:12 +0100 Subject: Python error codes and messages location In-Reply-To: <20130527061154.GA3301@cskk.homeip.net> References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On 27/05/2013 07:11, Cameron Simpson wrote: > > BTW, I recommend importing "errno" and using symbolic names. It makes things much more readable, and accomodates the situation where the symbols map to different numbers on different platforms. And have a catch-all. For example: > > Cheers, > This should make life easier for us http://docs.python.org/3/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From fabiosantosart at gmail.com Mon May 27 08:46:50 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 27 May 2013 13:46:50 +0100 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On 27 May 2013 12:41, "Mark Lawrence" wrote: > This should make life easier for us http://docs.python.org/3/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy Speaking of PEPs and exceptions. When do we get localized exceptions? -------------- next part -------------- An HTML attachment was scrubbed... URL: From vito.detullio at gmail.com Mon May 27 14:19:33 2013 From: vito.detullio at gmail.com (Vito De Tullio) Date: Mon, 27 May 2013 20:19:33 +0200 Subject: Python error codes and messages location References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: F?bio Santos wrote: >> This should make life easier for us > http://docs.python.org/3/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy > > Speaking of PEPs and exceptions. When do we get localized exceptions? What do you mean by "localized exceptions"? Please, tell me it's *NOT* a proposal to send the exception message in the locale language! -- By ZeD From fabiosantosart at gmail.com Mon May 27 14:36:26 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 27 May 2013 19:36:26 +0100 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On 27 May 2013 19:23, "Vito De Tullio" wrote: > > F?bio Santos wrote: > > >> This should make life easier for us > > http://docs.python.org/3/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy > > > > Speaking of PEPs and exceptions. When do we get localized exceptions? > > What do you mean by "localized exceptions"? > > Please, tell me it's *NOT* a proposal to send the exception message in the > locale language! > > -- > By ZeD > > -- > http://mail.python.org/mailman/listinfo/python-list It is. I think I read it mentioned in python-dev or this list. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiosantosart at gmail.com Mon May 27 19:44:22 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 00:44:22 +0100 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On 27 May 2013 19:36, "F?bio Santos" wrote: > > > On 27 May 2013 19:23, "Vito De Tullio" wrote: > > > > F?bio Santos wrote: > > > > >> This should make life easier for us > > > http://docs.python.org/3/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy > > > > > > Speaking of PEPs and exceptions. When do we get localized exceptions? > > > > What do you mean by "localized exceptions"? > > > > Please, tell me it's *NOT* a proposal to send the exception message in the > > locale language! > > > > -- > > By ZeD > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > It is. I think I read it mentioned in python-dev or this list. Here is what I read. http://mail.python.org/pipermail/python-dev/2013-April/125364.html It wasn't only about exceptions after all. And it seems like something that will only happen far into the future. -------------- next part -------------- An HTML attachment was scrubbed... URL: From vito.detullio at gmail.com Tue May 28 00:13:18 2013 From: vito.detullio at gmail.com (Vito De Tullio) Date: Tue, 28 May 2013 06:13:18 +0200 Subject: Python error codes and messages location References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: F?bio Santos wrote: >> > > Speaking of PEPs and exceptions. When do we get localized exceptions? >> > >> > What do you mean by "localized exceptions"? >> > >> > Please, tell me it's *NOT* a proposal to send the exception message in >> > the >> > locale language! >> It is. I think I read it mentioned in python-dev or this list. > > Here is what I read. > > http://mail.python.org/pipermail/python-dev/2013-April/125364.html > > It wasn't only about exceptions after all. And it seems like something > that will only happen far into the future. I really hope really far... have you never tried to google a localized error message? :\ -- By ZeD From fabiosantosart at gmail.com Tue May 28 02:57:14 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 07:57:14 +0100 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On 28 May 2013 05:17, "Vito De Tullio" wrote: > > F?bio Santos wrote: > > >> > > Speaking of PEPs and exceptions. When do we get localized exceptions? > >> > > >> > What do you mean by "localized exceptions"? > >> > > >> > Please, tell me it's *NOT* a proposal to send the exception message in > >> > the > >> > locale language! > >> It is. I think I read it mentioned in python-dev or this list. > > > > Here is what I read. > > > > http://mail.python.org/pipermail/python-dev/2013-April/125364.html > > > > It wasn't only about exceptions after all. And it seems like something > > that will only happen far into the future. > > I really hope really far... have you never tried to google a localized error > message? :\ > > -- > By ZeD > Never. I don't even try. I still think this is a good idea. Not for debugging though. For UI (and error logs if your requirements include writing them in the user language), it could simplify all the user-facing-unexpected-error-reporting into an except hook, or a try..except high up in your call stack. It would be a pretty big step towards RAD. But it had to be made optional somehow. Maybe using errno and existing exception messages together with gettext? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue May 28 03:15:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 28 May 2013 17:15:51 +1000 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On Tue, May 28, 2013 at 4:57 PM, F?bio Santos wrote: > > On 28 May 2013 05:17, "Vito De Tullio" wrote: >> I really hope really far... have you never tried to google a localized >> error >> message? :\ > > Never. I don't even try. Same happens when someone pastes an error onto a mailing list like this. Unless there's some easily-identifiable token (maybe the exception type doesn't get localized, though that has its own consequences) by which everyone world-over can recognize the exception, this would be a major nuisance. Also, once the interpreter core and a few parts of the stdlib get localized, this would add a barrier to entry for new modules... not sure this is a good thing. Can we internationalize English instead of localizing Python? Not-entirely-joking-ly yours, ChrisA From fabiosantosart at gmail.com Tue May 28 04:10:51 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 09:10:51 +0100 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On 28 May 2013 08:19, "Chris Angelico" wrote: > > On Tue, May 28, 2013 at 4:57 PM, F?bio Santos wrote: > > > > On 28 May 2013 05:17, "Vito De Tullio" wrote: > >> I really hope really far... have you never tried to google a localized > >> error > >> message? :\ > > > > Never. I don't even try. > > Same happens when someone pastes an error onto a mailing list like > this. Unless there's some easily-identifiable token (maybe the > exception type doesn't get localized, though that has its own > consequences) by which everyone world-over can recognize the > exception, this would be a major nuisance. Just to clarify, I am suggesting to have the unchanged messages in tracebacks, but having some methods in the exception to get the exception and message localised. Just like repr() and str() are directed at different audiences (the programmer vs. The user), so is a traceback vs a message box. In English, we could "translate" the exception name (which is limited as a python name). EG. RuntimeError -> Runtime error. Of course, this is not a big deal, but it would let us show the error message to the user, together with the exception. It would be much harder to search for an error or ask for help if it was a message written by some random developer. OpenERP for example shows stack traces to the user. It could show a shorter message in the user's language. > Also, once the interpreter > core and a few parts of the stdlib get localized, this would add a > barrier to entry for new modules... not sure this is a good thing. I think that would be a job for a per-language team as opposed to someone implementing a bug fix or pushing a PEP? Even that would only be necessary for the exceptions a module raises. > Can we internationalize English instead of localizing Python? > That is worse than saying, "why don't we all just code in python?". Or c. Or java. Or JavaScript. Let's not get into that. I prefer len() to new LengthCalculatorFactoryBuilder().createLengthCalculatorFactory().createLengthCalculator().calculateLengthOf(). And a good old Python list or JavaScript array to a collection of segfaults. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue May 28 04:17:54 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 28 May 2013 18:17:54 +1000 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On Tue, May 28, 2013 at 6:10 PM, F?bio Santos wrote: > Just to clarify, I am suggesting to have the unchanged messages in > tracebacks, but having some methods in the exception to get the exception > and message localised. Just like repr() and str() are directed at different > audiences (the programmer vs. The user), so is a traceback vs a message box. There's no particular reason for that to be a method on the exception, then. You're just asking to localize a text string, and I think that functionality already exists someplace :) > OpenERP for example shows stack traces to the user. It could show a shorter > message in the user's language. Sounds like an OpenERP feature request, then. > On 28 May 2013 08:19, "Chris Angelico" wrote: >> Can we internationalize English instead of localizing Python? >> > > That is worse than saying, "why don't we all just code in python?". Or c. Or > java. Or JavaScript. > > Let's not get into that. I prefer len() to new > LengthCalculatorFactoryBuilder().createLengthCalculatorFactory().createLengthCalculator().calculateLengthOf(). > And a good old Python list or JavaScript array to a collection of segfaults. Yeah, like I said, that wasn't a serious suggestion :) ChrisA From storchaka at gmail.com Tue May 28 09:11:54 2013 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 28 May 2013 16:11:54 +0300 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: 28.05.13 11:17, Chris Angelico ???????(??): > On Tue, May 28, 2013 at 6:10 PM, F?bio Santos wrote: >> Just to clarify, I am suggesting to have the unchanged messages in >> tracebacks, but having some methods in the exception to get the exception >> and message localised. Just like repr() and str() are directed at different >> audiences (the programmer vs. The user), so is a traceback vs a message box. > > There's no particular reason for that to be a method on the exception, > then. You're just asking to localize a text string, and I think that > functionality already exists someplace :) It will be very helpful in case of KeyError for example. From fabiosantosart at gmail.com Tue May 28 11:26:01 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 16:26:01 +0100 Subject: Python error codes and messages location In-Reply-To: References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: On 28 May 2013 09:22, "Chris Angelico" wrote: > > On Tue, May 28, 2013 at 6:10 PM, F?bio Santos wrote: > > Just to clarify, I am suggesting to have the unchanged messages in > > tracebacks, but having some methods in the exception to get the exception > > and message localised. Just like repr() and str() are directed at different > > audiences (the programmer vs. The user), so is a traceback vs a message box. > > There's no particular reason for that to be a method on the exception, > then. You're just asking to localize a text string, and I think that > functionality already exists someplace :) Yes :) what we don't have is a translation string for every exception that Python could raise. Or at least some of them. > > OpenERP for example shows stack traces to the user. It could show a shorter > > message in the user's language. > > Sounds like an OpenERP feature request, then. No, it's just something that OpenERP would obviously do if it had a library to, or language support :) this could be the One Obvious Way To Do It for displaying error messages in localised applications. That said, I'm going to think about this and maybe write a library for localising exceptions and a portuguese translation pack. PS. FWIW I'm rather stubborn sometimes. Sorry if I bothered you in any way. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiosantosart at gmail.com Wed May 29 02:45:42 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 29 May 2013 07:45:42 +0100 Subject: Python error codes and messages location In-Reply-To: <5vfaq8tfcoigfij2og8hinr7inlvahssdm@invalid.netcom.com> References: <20130527061154.GA3301@cskk.homeip.net> <5vfaq8tfcoigfij2og8hinr7inlvahssdm@invalid.netcom.com> Message-ID: On 29 May 2013 00:44, "Dennis Lee Bieber" wrote: > > On Tue, 28 May 2013 17:15:51 +1000, Chris Angelico > declaimed the following in gmane.comp.python.general: > > > > Can we internationalize English instead of localizing Python? > > > > Not-entirely-joking-ly yours, > > > All that is required is to get Python declared a valid air traffic > control communication (all international air traffic control uses > English) > > Totally-joking-I-suspect > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ > with import mangoes as cargo: allow() -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Tue May 28 04:38:35 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 May 2013 08:38:35 GMT Subject: Python error codes and messages location References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: <51a46d0b$0$11118$c3e8da3@news.astraweb.com> On Tue, 28 May 2013 17:15:51 +1000, Chris Angelico wrote: > Can we internationalize English instead of localizing Python? We have. English is the primary international language for programmers. (For which I am profoundly grateful.) Japanese is also a pretty important language, but mostly in Japan. And China would like Chinese to be, in fact there is even a version of Python localised to Chinese: http://www.chinesepython.org/ I have no objection to people creating their own, localised, implementation or fork of Python, in which case good luck to them but they're on their own. But I don't think that exceptions should otherwise be localised. -- Steven From steve+comp.lang.python at pearwood.info Mon May 27 09:11:46 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 May 2013 13:11:46 GMT Subject: Python error codes and messages location References: <20130527061154.GA3301@cskk.homeip.net> Message-ID: <51a35b92$0$30002$c3e8da3$5496439d@news.astraweb.com> On Mon, 27 May 2013 13:46:50 +0100, F?bio Santos wrote: > Speaking of PEPs and exceptions. When do we get localized exceptions? We're waiting for you to volunteer. When can you start? -- Steven From fabiosantosart at gmail.com Mon May 27 09:26:01 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 27 May 2013 14:26:01 +0100 Subject: Python error codes and messages location In-Reply-To: <51a35b92$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <20130527061154.GA3301@cskk.homeip.net> <51a35b92$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, May 27, 2013 at 2:11 PM, Steven D'Aprano wrote: > On Mon, 27 May 2013 13:46:50 +0100, F?bio Santos wrote: > >> Speaking of PEPs and exceptions. When do we get localized exceptions? > > > We're waiting for you to volunteer. When can you start? I'd love to work on that but my C is too shabby to say the least. -- F?bio Santos From carlosnepomuceno at outlook.com Mon May 27 12:54:59 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Mon, 27 May 2013 19:54:59 +0300 Subject: Python error codes and messages location In-Reply-To: References: , <20130527061154.GA3301@cskk.homeip.net>, , , <51a35b92$0$30002$c3e8da3$5496439d@news.astraweb.com>, Message-ID: Thanks so much guys! I'm not planning to prepare for every possible situation, but I certainly am responsible to handle most common errors. So it's really important to know what a function/method returns when called. Exception handling may take lots of code, but I'm used to it. It's much better to have a graceful shutdown than a data/time loss. ;) I think PEP 3151 is a step ahead! That's almost exactly what I was looking for. Why did it take so long to have that implemented? From breamoreboy at yahoo.co.uk Mon May 27 13:14:21 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 27 May 2013 18:14:21 +0100 Subject: Python error codes and messages location In-Reply-To: References: , <20130527061154.GA3301@cskk.homeip.net>, , , <51a35b92$0$30002$c3e8da3$5496439d@news.astraweb.com>, Message-ID: On 27/05/2013 17:54, Carlos Nepomuceno wrote: > > I think PEP 3151 is a step ahead! That's almost exactly what I was looking for. Why did it take so long to have that implemented? > Lack of volunteers. -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From tjreedy at udel.edu Mon May 27 13:52:42 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Mon, 27 May 2013 13:52:42 -0400 Subject: Python error codes and messages location In-Reply-To: References: , <20130527061154.GA3301@cskk.homeip.net>, , , <51a35b92$0$30002$c3e8da3$5496439d@news.astraweb.com>, Message-ID: On 5/27/2013 12:54 PM, Carlos Nepomuceno wrote: > I think PEP 3151 is a step ahead! That's almost exactly what I was looking for. Why did it take so long to have that implemented? Since this PEP involved changing existing features, rather than adding something mew, it probably took moe time and discussion to get consensus on details of the change. From cs at zip.com.au Sun May 26 22:02:41 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 27 May 2013 12:02:41 +1000 Subject: Python error codes and messages location In-Reply-To: <51a2ae95$0$30002$c3e8da3$5496439d@news.astraweb.com> References: <51a2ae95$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20130527020241.GA85366@cskk.homeip.net> On 27May2013 00:53, Steven D'Aprano wrote: | On Mon, 27 May 2013 02:13:54 +0300, Carlos Nepomuceno wrote: | > Where can I find all error codes and messages that Python throws (actual | > codes and messages from exceptions raised by stdlib)? | | There is no list. It is subject to change from version to version, | including point releases. And better still, it is platform specific too. [...] | > I've already found the module 'errno' and got a dictionary | > (errno.errorcode) and some system error messages | > (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find. | | These are the standard C operating system and file system error codes, | not Python exceptions. And the poster boy example for platform dependence. Besides, knowing the exact errors that may occur is not the Python Way, it is the Java Way. Run it. if it goes bang, handle the errors you expect and understand. If you get something else, go bang for real because you _don't_ know what should happen, and proceeding is probably insane. Cheers, -- Cameron Simpson Motorcycles are like peanuts... who can stop at just one? - Zebee Johnstone aus.motorcycles Poser Permit #1 From avnesh.nitk at gmail.com Mon May 27 00:32:40 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Sun, 26 May 2013 21:32:40 -0700 (PDT) Subject: how to compare two json file line by line using python? Message-ID: <355f934e-bda0-4316-96bb-583c498ecb1a@googlegroups.com> hi, how to compare two json file line by line using python? Actually I am doing it in this way.. import simplejson as json def compare(): newJsonFile= open('newData.json') lastJsonFile= open('version1.json') newLines = newJsonFile.readlines() print newLines sortedNew = sorted([repr(x) for x in newJsonFile]) sortedLast = sorted([repr(x) for x in lastJsonFile]) print(sortedNew == sortedLast) compare() But I want to compare line by line and value by value. but i found that json data is unordered data, so how can i compare them without sorting it. please give me some idea about it. I am new for it. I want to check every value line by line. Thanks From rustompmody at gmail.com Mon May 27 00:51:59 2013 From: rustompmody at gmail.com (rusi) Date: Sun, 26 May 2013 21:51:59 -0700 (PDT) Subject: how to compare two json file line by line using python? References: <355f934e-bda0-4316-96bb-583c498ecb1a@googlegroups.com> Message-ID: On May 27, 9:32?am, Avnesh Shakya wrote: > hi, > ? ?how to compare two json file line by line using python? Actually I am doing it in this way.. > > import simplejson as json > def compare(): > ? ? newJsonFile= open('newData.json') > ? ? lastJsonFile= open('version1.json') > ? ? newLines = newJsonFile.readlines() > ? ? print newLines > ? ? sortedNew = sorted([repr(x) for x in newJsonFile]) > ? ? sortedLast = sorted([repr(x) for x in lastJsonFile]) > ? ? print(sortedNew == sortedLast) > > compare() > > But I want to compare line by line and value by value. but i found that json data is unordered data, so how can i compare them without sorting it. please give me some idea about it. I am new for it. > I want to check every value line by line. > > Thanks It really depends on what is your notion that the two files are same or not. For example does extra/deleted non-significant white-space matter? By and large there are two approaches: 1. Treat json as serialized python data-structures, (and so) read in the data-structures into python and compare there 2. Ignore the fact that the json file is a json file; just treat it as text and use string compare operations Naturally there could be other considerations: the files could be huge and so you might want some hybrid of json and text approaches etc etc From avnesh.nitk at gmail.com Mon May 27 01:05:39 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Mon, 27 May 2013 10:35:39 +0530 Subject: how to compare two json file line by line using python? In-Reply-To: References: <355f934e-bda0-4316-96bb-583c498ecb1a@googlegroups.com> Message-ID: Actually, I am extracting data from other site in json format and I want to put it in my database and when I extract data again then I want to compare last json file, if these are same then no issue otherwise i will add new data in database, so here may be every time data can be changed or may be not so I think sorting is required, but if i compare line by line that will be good, I am thinking in this way... On Mon, May 27, 2013 at 10:21 AM, rusi wrote: > On May 27, 9:32 am, Avnesh Shakya wrote: > > hi, > > how to compare two json file line by line using python? Actually I am > doing it in this way.. > > > > import simplejson as json > > def compare(): > > newJsonFile= open('newData.json') > > lastJsonFile= open('version1.json') > > newLines = newJsonFile.readlines() > > print newLines > > sortedNew = sorted([repr(x) for x in newJsonFile]) > > sortedLast = sorted([repr(x) for x in lastJsonFile]) > > print(sortedNew == sortedLast) > > > > compare() > > > > But I want to compare line by line and value by value. but i found that > json data is unordered data, so how can i compare them without sorting it. > please give me some idea about it. I am new for it. > > I want to check every value line by line. > > > > Thanks > > It really depends on what is your notion that the two files are same > or not. > > For example does extra/deleted non-significant white-space matter? > > By and large there are two approaches: > 1. Treat json as serialized python data-structures, (and so) read in > the data-structures into python and compare there > > 2. Ignore the fact that the json file is a json file; just treat it as > text and use string compare operations > > Naturally there could be other considerations: the files could be huge > and so you might want some hybrid of json and text approaches > etc etc > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Mon May 27 01:33:11 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 May 2013 05:33:11 GMT Subject: how to compare two json file line by line using python? References: <355f934e-bda0-4316-96bb-583c498ecb1a@googlegroups.com> Message-ID: <51a2f016$0$1744$c3e8da3$76491128@news.astraweb.com> On Sun, 26 May 2013 21:32:40 -0700, Avnesh Shakya wrote: > But I want to compare line by line and value by value. but i found that > json data is unordered data, so how can i compare them without sorting > it. please give me some idea about it. I am new for it. I want to check > every value line by line. Why do you care about checking every value line by line? As you say yourself, JSON data is unordered, so "line by line" is the wrong way to compare it. The right way is to decode the JSON data, and then compare whether it gives you the result you expect: a = json.load("file-a") b = json.load("file-b") if a == b: print("file-a and file-b contain the same JSON data") If what you care about is the *data* stored in the JSON file, this is the correct way to check it. On the other hand, if you don't care about the data, but you want to detect changes to whitespace, blank lines, or other changes that make no difference to the JSON data, then there is no need to care that this is JSON data. Just treat it as text, and use the difflib library. http://docs.python.org/2/library/difflib.html -- Steven From avnesh.nitk at gmail.com Mon May 27 02:28:42 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Mon, 27 May 2013 11:58:42 +0530 Subject: how to compare two json file line by line using python? In-Reply-To: <51a2f016$0$1744$c3e8da3$76491128@news.astraweb.com> References: <355f934e-bda0-4316-96bb-583c498ecb1a@googlegroups.com> <51a2f016$0$1744$c3e8da3$76491128@news.astraweb.com> Message-ID: Thanks a lot, I got it. On Mon, May 27, 2013 at 11:03 AM, Steven D'Aprano < steve+comp.lang.python at pearwood.info> wrote: > On Sun, 26 May 2013 21:32:40 -0700, Avnesh Shakya wrote: > > > But I want to compare line by line and value by value. but i found that > > json data is unordered data, so how can i compare them without sorting > > it. please give me some idea about it. I am new for it. I want to check > > every value line by line. > > Why do you care about checking every value line by line? As you say > yourself, JSON data is unordered, so "line by line" is the wrong way to > compare it. > > > The right way is to decode the JSON data, and then compare whether it > gives you the result you expect: > > a = json.load("file-a") > b = json.load("file-b") > if a == b: > print("file-a and file-b contain the same JSON data") > > If what you care about is the *data* stored in the JSON file, this is the > correct way to check it. > > On the other hand, if you don't care about the data, but you want to > detect changes to whitespace, blank lines, or other changes that make no > difference to the JSON data, then there is no need to care that this is > JSON data. Just treat it as text, and use the difflib library. > > http://docs.python.org/2/library/difflib.html > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Tue May 28 12:00:14 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 16:00:14 +0000 (UTC) Subject: how to compare two json file line by line using python? References: <355f934e-bda0-4316-96bb-583c498ecb1a@googlegroups.com> <51a2f016$0$1744$c3e8da3$76491128@news.astraweb.com> Message-ID: On 2013-05-27, Steven D'Aprano wrote: > On Sun, 26 May 2013 21:32:40 -0700, Avnesh Shakya wrote: > >> But I want to compare line by line and value by value. but i found that >> json data is unordered data, so how can i compare them without sorting >> it. please give me some idea about it. I am new for it. I want to check >> every value line by line. > > Why do you care about checking every value line by line? As you say > yourself, JSON data is unordered, so "line by line" is the wrong way to > compare it. There's no such thing as "lines" in JSON anyway. Outside of string literals, all whitespace is equivalent, so replacing all newlines with space characters results in equivalent blobs of JSON -- but one is a single line, and the other is multiple lines. > The right way is to decode the JSON data, and then compare whether it > gives you the result you expect: > > a = json.load("file-a") > b = json.load("file-b") > if a == b: > print("file-a and file-b contain the same JSON data") > > If what you care about is the *data* stored in the JSON file, this is > the correct way to check it. Yup. -- Grant Edwards grant.b.edwards Yow! Are we laid back yet? at gmail.com From denismfmcmahon at gmail.com Mon May 27 06:50:16 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Mon, 27 May 2013 10:50:16 +0000 (UTC) Subject: how to compare two json file line by line using python? References: <355f934e-bda0-4316-96bb-583c498ecb1a@googlegroups.com> Message-ID: On Sun, 26 May 2013 21:32:40 -0700, Avnesh Shakya wrote: > how to compare two json file line by line using python? Actually I am > doing it in this way.. Oh what a lot of homework you have today. Did you ever stop to think what the easiest way to compare two json datasets is? -- Denis McMahon, denismfmcmahon at gmail.com From lokeshkoppaka at gmail.com Mon May 27 00:48:34 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Sun, 26 May 2013 21:48:34 -0700 (PDT) Subject: help?? on functions Message-ID: def shuffle(input, i, j): pass input = input[i:j+1] +input[0:i] + input[j+1:] def test_shuffle(): input = [1, 2, 3, 4, 5, 6] shuffle(input, 1, 2) assert [2, 3, 1, 4, 5, 6] == input i had done the above code but the problem is i had manipulated the "input" in function shuffle(input, i, j) but once i get back to the test_shuffle() function again the variable "input" does not reflect the changes made in shuffle(input, i, j) why ,please can any one describe why . and help how to reflect that change to the variable "input". From steve+comp.lang.python at pearwood.info Mon May 27 01:48:34 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 May 2013 05:48:34 GMT Subject: help?? on functions References: Message-ID: <51a2f3b2$0$1744$c3e8da3$76491128@news.astraweb.com> On Sun, 26 May 2013 21:48:34 -0700, lokeshkoppaka wrote: > def shuffle(input, i, j): > pass > input = input[i:j+1] +input[0:i] + input[j+1:] "pass" does nothing. Take it out. > def test_shuffle(): > input = [1, 2, 3, 4, 5, 6] > shuffle(input, 1, 2) > assert [2, 3, 1, 4, 5, 6] == input > > > i had done the above code but the problem is i had manipulated the > "input" in function shuffle(input, i, j) but once i get back to the > test_shuffle() function again the variable "input" does not reflect the > changes made in shuffle(input, i, j) why ,please can any one describe > why . and help how to reflect that change to the variable "input". The line of code: input = input[i:j+1] +input[0:i] + input[j+1:] takes the input list, makes three slices from that list, creates a new list, and then reassigns the LOCAL variable "input". This does not touch the variable on the outside of the function. This will be more clear if you use different names: # Outside the function. mylist = [1, 2, 3, 4, 5, 6] shuffle(mylist, 1, 2) Inside the function "shuffle", "input" is a local variable, and when you reassign to it, the variable "mylist" on the outside is not changed. Try this small function to see what I mean: def demo(input): print('local variable, before:', input) input = 100 print('local variable, after:', input) print('non-local variable', mylist) mylist = [1, 2, 3, 4, 5, 6] demo(mylist) So, what can you do to fix this? You have two choices: 1) You can return the shuffled list. Add this line to the end of your shuffle function: return input and then inside the test function, do this: def test_shuffle(): input = [1, 2, 3, 4, 5, 6] input = shuffle(input, 1, 2) assert [2, 3, 1, 4, 5, 6] == input 2) You can modify the input list in place. In this case, instead of reassigning the local variable "input" with the new list, you simply tell Python to stuff the new list inside the original list. You do that with a slice: input[:] = input[i:j+1] + input[0:i] + input[j+1:] That's a small difference from what you wrote, just three characters [:], but it makes a big difference in the effect. Instead of reassigning the local variable to the new list, it takes the existing list, and replaces each value inside it with the values taken from the new list. For example: py> mylist = [100, 200, 300, 400, 500, 600] py> mylist[3:5] = ['A', 'B', 'C'] py> mylist [100, 200, 300, 'A', 'B', 'C', 600] py> mylist[1:] = [99, 98, 97] py> mylist [100, 99, 98, 97] Any questions? -- Steven From lokeshkoppaka at gmail.com Mon May 27 03:20:47 2013 From: lokeshkoppaka at gmail.com (lokeshkoppaka at gmail.com) Date: Mon, 27 May 2013 00:20:47 -0700 (PDT) Subject: help?? on functions In-Reply-To: <51a2f3b2$0$1744$c3e8da3$76491128@news.astraweb.com> References: <51a2f3b2$0$1744$c3e8da3$76491128@news.astraweb.com> Message-ID: On Monday, May 27, 2013 11:18:34 AM UTC+5:30, Steven D'Aprano wrote: > On Sun, 26 May 2013 21:48:34 -0700, lokeshkoppaka wrote: > > > > > def shuffle(input, i, j): > > > pass > > > input = input[i:j+1] +input[0:i] + input[j+1:] > > > > "pass" does nothing. Take it out. > > > > > > > > > def test_shuffle(): > > > input = [1, 2, 3, 4, 5, 6] > > > shuffle(input, 1, 2) > > > assert [2, 3, 1, 4, 5, 6] == input > > > > > > > > > i had done the above code but the problem is i had manipulated the > > > "input" in function shuffle(input, i, j) but once i get back to the > > > test_shuffle() function again the variable "input" does not reflect the > > > changes made in shuffle(input, i, j) why ,please can any one describe > > > why . and help how to reflect that change to the variable "input". > > > > The line of code: > > > > input = input[i:j+1] +input[0:i] + input[j+1:] > > > > > > takes the input list, makes three slices from that list, creates a new > > list, and then reassigns the LOCAL variable "input". This does not touch > > the variable on the outside of the function. > > > > This will be more clear if you use different names: > > > > # Outside the function. > > mylist = [1, 2, 3, 4, 5, 6] > > shuffle(mylist, 1, 2) > > > > > > Inside the function "shuffle", "input" is a local variable, and when you > > reassign to it, the variable "mylist" on the outside is not changed. Try > > this small function to see what I mean: > > > > def demo(input): > > print('local variable, before:', input) > > input = 100 > > print('local variable, after:', input) > > print('non-local variable', mylist) > > > > > > mylist = [1, 2, 3, 4, 5, 6] > > demo(mylist) > > > > > > > > So, what can you do to fix this? You have two choices: > > > > > > 1) You can return the shuffled list. Add this line to the end of your > > shuffle function: > > > > return input > > > > > > and then inside the test function, do this: > > > > def test_shuffle(): > > input = [1, 2, 3, 4, 5, 6] > > input = shuffle(input, 1, 2) > > assert [2, 3, 1, 4, 5, 6] == input > > > > > > 2) You can modify the input list in place. > > > > In this case, instead of reassigning the local variable "input" with the > > new list, you simply tell Python to stuff the new list inside the > > original list. You do that with a slice: > > > > > > input[:] = input[i:j+1] + input[0:i] + input[j+1:] > > > > > > That's a small difference from what you wrote, just three characters [:], > > but it makes a big difference in the effect. Instead of reassigning the > > local variable to the new list, it takes the existing list, and replaces > > each value inside it with the values taken from the new list. For example: > > > > > > py> mylist = [100, 200, 300, 400, 500, 600] > > py> mylist[3:5] = ['A', 'B', 'C'] > > py> mylist > > [100, 200, 300, 'A', 'B', 'C', 600] > > > > py> mylist[1:] = [99, 98, 97] > > py> mylist > > [100, 99, 98, 97] > > > > > > Any questions? > > > > > > -- > > Steven Steven wow, wonderful explanation ,i got it thanks a lot From john_ladasky at sbcglobal.net Mon May 27 04:03:46 2013 From: john_ladasky at sbcglobal.net (John Ladasky) Date: Mon, 27 May 2013 01:03:46 -0700 (PDT) Subject: help?? on functions In-Reply-To: References: Message-ID: Steven gave you a lot of good advice. Let me add just one remark. Python already has a builtin function called "input." If you define a variable with the same name as a builtin and then you try to use that builtin, you will be in for a (usually unpleasant) surprise. From silusilusilu at gmail.com Mon May 27 05:26:37 2013 From: silusilusilu at gmail.com (silusilusilu at gmail.com) Date: Mon, 27 May 2013 02:26:37 -0700 (PDT) Subject: Problems with python and pyQT Message-ID: <8d8fd7fc-2509-457e-a4c9-0a1ac1f65c02@googlegroups.com> Hi, i'm new with python: so excuse me for my questions.... i have this code: def updateLog(self, text): self.ui.logTextEdit.moveCursor(QTextCursor.End) self.ui.logTextEdit.insertHtml(""+text) self.ui.logTextEdit.moveCursor(QTextCursor.End) logTextEdit is a QTextEdit object.With this code,i can display only ascii characters: how can i diplay text as hex and binary values? Thanks From kwpolska at gmail.com Mon May 27 09:10:24 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Mon, 27 May 2013 15:10:24 +0200 Subject: Problems with python and pyQT In-Reply-To: <8d8fd7fc-2509-457e-a4c9-0a1ac1f65c02@googlegroups.com> References: <8d8fd7fc-2509-457e-a4c9-0a1ac1f65c02@googlegroups.com> Message-ID: On Mon, May 27, 2013 at 11:26 AM, wrote: > Hi, > i'm new with python: so excuse me for my questions.... > i have this code: > > def updateLog(self, text): > self.ui.logTextEdit.moveCursor(QTextCursor.End) > self.ui.logTextEdit.insertHtml(""+text) > self.ui.logTextEdit.moveCursor(QTextCursor.End) > > logTextEdit is a QTextEdit object.With this code,i can display only ascii characters: how can i diplay text as hex and binary values? > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list You would need to convert them to strings first. You may want bin() and hex() for that. And if you want to convert 'q' to 0x71, hex(ord("q")). And if you want to turn 'hello' into 0x68656c6c6f, you would need to iterate over 'hello' and run the above function over every letter. Also, you are able to display Unicode characters, too. -- Kwpolska | GPG KEY: 5EAAEA16 stop html mail | always bottom-post http://asciiribbon.org | http://caliburn.nl/topposting.html From silusilusilu at gmail.com Tue May 28 05:41:56 2013 From: silusilusilu at gmail.com (silusilusilu at gmail.com) Date: Tue, 28 May 2013 02:41:56 -0700 (PDT) Subject: Problems with python and pyQT In-Reply-To: References: <8d8fd7fc-2509-457e-a4c9-0a1ac1f65c02@googlegroups.com> Message-ID: <646e6bae-b0fc-46fe-bf60-23ec9b4958a3@googlegroups.com> Thanks for your reply: very useful!! I have another question: with hex command i display (for example) 0x1 is it possible to display 0x01? Thanks Il giorno luned? 27 maggio 2013 15:10:24 UTC+2, Chris ?Kwpolska? Warrick ha scritto: > On Mon, May 27, 2013 at 11:26 AM, wrote: > > > Hi, > > > i'm new with python: so excuse me for my questions.... > > > i have this code: > > > > > > def updateLog(self, text): > > > self.ui.logTextEdit.moveCursor(QTextCursor.End) > > > self.ui.logTextEdit.insertHtml(""+text) > > > self.ui.logTextEdit.moveCursor(QTextCursor.End) > > > > > > logTextEdit is a QTextEdit object.With this code,i can display only ascii characters: how can i diplay text as hex and binary values? > > > Thanks > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > You would need to convert them to strings first. You may want bin() > > and hex() for that. And if you want to convert 'q' to 0x71, > > hex(ord("q")). And if you want to turn 'hello' into 0x68656c6c6f, you > > would need to iterate over 'hello' and run the above function over > > every letter. > > > > Also, you are able to display Unicode characters, too. From davea at davea.name Tue May 28 06:59:27 2013 From: davea at davea.name (Dave Angel) Date: Tue, 28 May 2013 06:59:27 -0400 Subject: Problems with python and pyQT In-Reply-To: <646e6bae-b0fc-46fe-bf60-23ec9b4958a3@googlegroups.com> References: <8d8fd7fc-2509-457e-a4c9-0a1ac1f65c02@googlegroups.com> <646e6bae-b0fc-46fe-bf60-23ec9b4958a3@googlegroups.com> Message-ID: <51A48E0F.4070803@davea.name> On 05/28/2013 05:41 AM, silusilusilu at gmail.com wrote: > Thanks for your reply: very useful!! > I have another question: with hex command i display (for example) > > 0x1 > > is it possible to display 0x01? hex() is a function, not a command. And it only takes the one parameter, the int to be converted. For more generality, try the str.format() method. The x type converts to hex without any prefix, and then you can add the width and fill field. http://docs.python.org/2/library/stdtypes.html#str.format http://docs.python.org/2/library/string.html#formatstrings http://docs.python.org/2/library/string.html#format-specification-mini-language For starters, try: print "0x{0:0>2x}".format(12) or print "0x{0:0>2X}".format(12) if you like uppercase hex characters -- DaveA From avnesh.nitk at gmail.com Mon May 27 05:27:59 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Mon, 27 May 2013 02:27:59 -0700 (PDT) Subject: How to create new python file with increament number, if doesn't exist? Message-ID: <3c99daa0-6f30-4cbf-b107-56021a324f7c@googlegroups.com> hi, I want to create a new python file like 'data0.0.5', but if it is already exist then it should create 'data0.0.6', if it's also exist then next like 'data0.0.7'. I have done, but with range, please give me suggestion so that I can do it with specifying range. I was trying this way and it's working also.. i = 0 for i in range(100): try: with open('Data%d.%d.%d.json'%(0,0,i,)): pass continue except IOError: edxCorrectDataFile = file('Data%d.%d.%d.json'%(0,0,i,), 'a+') break But here I have defined range 100, Is it possible without range it create many required files? Thanks From denismfmcmahon at gmail.com Mon May 27 06:49:00 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Mon, 27 May 2013 10:49:00 +0000 (UTC) Subject: How to create new python file with increament number, if doesn't exist? References: <3c99daa0-6f30-4cbf-b107-56021a324f7c@googlegroups.com> Message-ID: On Mon, 27 May 2013 02:27:59 -0700, Avnesh Shakya wrote: > I want to create a new python file like 'data0.0.5', but if it is > already exist then it should create 'data0.0.6', if it's also exist > then next like 'data0.0.7'. I have done, but with range, please give > me suggestion so that I can do it with specifying range. Try and put your description into the sequence of instructions you want the computer follow. For this problem, my sequence of instructions would be: 1) Find the highest numbered existing file that matches the filename data0.0.[number] 2) Create a new file that is one number higher. Now the solution is easy. Find the list of filenames in the directory that match a suitable regular expression, take the numeric value of a substring of the filename for each file and find the highest, add one to it, then create the new file name. Something like the following (untested) with the relevant imports etc: nfn="data0.0."+str(max([int(f[8:])for f in os.listdir(p)if re.match ('^data0.0.[0-9]+$',f)])+1) -- Denis McMahon, denismfmcmahon at gmail.com From avnesh.nitk at gmail.com Mon May 27 07:00:26 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Mon, 27 May 2013 16:30:26 +0530 Subject: How to create new python file with increament number, if doesn't exist? In-Reply-To: References: <3c99daa0-6f30-4cbf-b107-56021a324f7c@googlegroups.com> Message-ID: Thanks On Mon, May 27, 2013 at 4:19 PM, Denis McMahon wrote: > On Mon, 27 May 2013 02:27:59 -0700, Avnesh Shakya wrote: > > > I want to create a new python file like 'data0.0.5', but if it is > > already exist then it should create 'data0.0.6', if it's also exist > > then next like 'data0.0.7'. I have done, but with range, please give > > me suggestion so that I can do it with specifying range. > > Try and put your description into the sequence of instructions you want > the computer follow. > > For this problem, my sequence of instructions would be: > > 1) Find the highest numbered existing file that matches the filename > data0.0.[number] > > 2) Create a new file that is one number higher. > > Now the solution is easy. Find the list of filenames in the directory > that match a suitable regular expression, take the numeric value of a > substring of the filename for each file and find the highest, add one to > it, then create the new file name. > > Something like the following (untested) with the relevant imports etc: > > nfn="data0.0."+str(max([int(f[8:])for f in os.listdir(p)if re.match > ('^data0.0.[0-9]+$',f)])+1) > > -- > Denis McMahon, denismfmcmahon at gmail.com > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jt at toerring.de Mon May 27 07:13:19 2013 From: jt at toerring.de (Jens Thoms Toerring) Date: 27 May 2013 11:13:19 GMT Subject: How to create new python file with increament number, if doesn't exist? References: <3c99daa0-6f30-4cbf-b107-56021a324f7c@googlegroups.com> Message-ID: Avnesh Shakya wrote: > I want to create a new python file like 'data0.0.5', but if it is already > exist then it should create 'data0.0.6', if it's also exist then next like > 'data0.0.7'. I have done, but with range, please give me suggestion so that > I can do it with specifying range. > I was trying this way and it's working also.. > i = 0 > for i in range(100): > try: > with open('Data%d.%d.%d.json'%(0,0,i,)): pass > continue > except IOError: > edxCorrectDataFile = file('Data%d.%d.%d.json'%(0,0,i,), 'a+') > break > But here I have defined range 100, Is it possible without range it create > many required files? What about something as simple as this? i = 0 while os.path.exists( 'Data{0}.{1}.{2}.json'.format( 0, 0, i ) ) : i += 1 f = open( 'Data{0}.{1}.{2}.json'.format( 0, 0, i ), 'w' ) For your code you'ld make it e.g. i = 0 try: while True : with open('Data%d.%d.%d.json'%(0,0,i)): i += 1 except IOError: edxCorrectDataFile = file('Data%d.%d.%d.json'%(0,0,i), 'a+') Note that I don't see how all this trying to open a file and catching an exeception if it doesn't exist is any better than simply using os.path.exists(). You may have read some- where that it avoids a race condition and would thus be more secure. That's not the case here, an attacker still could create e.g. a symbolic link with the name of the file you're going to open between the time the exception is thrown and your program getting around to open the file you expect not to exist, so nothing is gained by using this somewhat convoluted method. What would be needed for avoiding a race condition is an addi- tional flag to be passed to open() like the for example the O_EXCL flag that can be passed to Unix' open() system function. But that isn't supported by Pythons open() function which rather likeky is based on C's fopen() function). The best advice to avoid such problems is probably not to open files with a predictable name (or better to use one of the methods to create files with names guaranteed to be unique by the system) in directories to which other users than you also have write permission. Regards, Jens -- \ Jens Thoms Toerring ___ jt at toerring.de \__________________________ http://toerring.de From livewebcamsexs at gmail.com Mon May 27 07:43:59 2013 From: livewebcamsexs at gmail.com (MoneyMaker) Date: Mon, 27 May 2013 04:43:59 -0700 (PDT) Subject: Scary Pranks Message-ID: http://horrorhorrorhorror.webs.com/scary-pranks From mok-kong.shen at t-online.de Mon May 27 10:45:05 2013 From: mok-kong.shen at t-online.de (Mok-Kong Shen) Date: Mon, 27 May 2013 16:45:05 +0200 Subject: How to get an integer from a sequence of bytes Message-ID: From an int one can use to_bytes to get its individual bytes, but how can one reconstruct the int from the sequence of bytes? Thanks in advance. M. K. Shen From steve+comp.lang.python at pearwood.info Mon May 27 11:00:39 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 May 2013 15:00:39 GMT Subject: How to get an integer from a sequence of bytes References: Message-ID: <51a37516$0$30002$c3e8da3$5496439d@news.astraweb.com> On Mon, 27 May 2013 16:45:05 +0200, Mok-Kong Shen wrote: > From an int one can use to_bytes to get its individual bytes, but how > can one reconstruct the int from the sequence of bytes? Here's one way: py> n = 11999102937234 py> m = 0 py> for b in n.to_bytes(6, 'big'): ... m = 256*m + b ... py> m == n True -- Steven From carlosnepomuceno at outlook.com Mon May 27 20:37:37 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 03:37:37 +0300 Subject: How to get an integer from a sequence of bytes In-Reply-To: <51a37516$0$30002$c3e8da3$5496439d@news.astraweb.com> References: , <51a37516$0$30002$c3e8da3$5496439d@news.astraweb.com> Message-ID: ---------------------------------------- > From: steve+comp.lang.python at pearwood.info > Subject: Re: How to get an integer from a sequence of bytes > Date: Mon, 27 May 2013 15:00:39 +0000 > To: python-list at python.org > > On Mon, 27 May 2013 16:45:05 +0200, Mok-Kong Shen wrote: > >> From an int one can use to_bytes to get its individual bytes, but how >> can one reconstruct the int from the sequence of bytes? > > Here's one way: > > py> n = 11999102937234 > py> m = 0 > py> for b in n.to_bytes(6, 'big'): > ... m = 256*m + b > ... > py> m == n > True > > > -- > Steven > > -- > http://mail.python.org/mailman/listinfo/python-list Python 2 doesn't have to_bytes()! :( # Python 2, LSB 1st def to_lil_bytes(x): ??? r = [] ??? while x != 0: ??????? r.append(int(x & 0b11111111)) ??????? x>>= 8 ??? return r # Python 2, LSB 1st def from_lil_bytes(l): ??? x = 0 ??? for i in range(len(l)-1, -1, -1): ??????? x <<= 8 ??????? x |= l[i] ??? return x # Python 2, MSB 1st def to_big_bytes(x): ??? r = [] ??? while x != 0: ??????? r.insert(0, int(x & 0b11111111)) ??????? x>>= 8 ??? return r # Python 2, MSB 1st def from_big_bytes(l): ??? x = 0 ??? for i in range(len(l)): ??????? x <<= 8 ??????? x |= l[i] ??? return x Can it be faster? From ned at nedbatchelder.com Mon May 27 11:30:18 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Mon, 27 May 2013 11:30:18 -0400 Subject: How to get an integer from a sequence of bytes In-Reply-To: References: Message-ID: <51A37C0A.1030608@nedbatchelder.com> On 5/27/2013 10:45 AM, Mok-Kong Shen wrote: > From an int one can use to_bytes to get its individual bytes, > but how can one reconstruct the int from the sequence of bytes? > The next thing in the docs after int.to_bytes is int.from_bytes: http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes --Ned. > Thanks in advance. > > M. K. Shen From steve+comp.lang.python at pearwood.info Mon May 27 20:31:07 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 May 2013 00:31:07 GMT Subject: How to get an integer from a sequence of bytes References: Message-ID: <51a3faca$0$29966$c3e8da3$5496439d@news.astraweb.com> On Mon, 27 May 2013 11:30:18 -0400, Ned Batchelder wrote: > On 5/27/2013 10:45 AM, Mok-Kong Shen wrote: >> From an int one can use to_bytes to get its individual bytes, but how >> can one reconstruct the int from the sequence of bytes? >> >> > The next thing in the docs after int.to_bytes is int.from_bytes: And I can't believe I missed that too :-( -- Steven From davea at davea.name Mon May 27 20:41:59 2013 From: davea at davea.name (Dave Angel) Date: Mon, 27 May 2013 20:41:59 -0400 Subject: How to get an integer from a sequence of bytes In-Reply-To: <51a3faca$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <51a3faca$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51A3FD57.1030905@davea.name> On 05/27/2013 08:31 PM, Steven D'Aprano wrote: > On Mon, 27 May 2013 11:30:18 -0400, Ned Batchelder wrote: > >> On 5/27/2013 10:45 AM, Mok-Kong Shen wrote: >>> From an int one can use to_bytes to get its individual bytes, but how >>> can one reconstruct the int from the sequence of bytes? >>> >>> >> The next thing in the docs after int.to_bytes is int.from_bytes: > > And I can't believe I missed that too :-( > > And that approach probably works for negative ints too. -- DaveA From mok-kong.shen at t-online.de Thu May 30 14:26:10 2013 From: mok-kong.shen at t-online.de (Mok-Kong Shen) Date: Thu, 30 May 2013 20:26:10 +0200 Subject: How to get an integer from a sequence of bytes In-Reply-To: References: Message-ID: Am 27.05.2013 17:30, schrieb Ned Batchelder: > On 5/27/2013 10:45 AM, Mok-Kong Shen wrote: >> From an int one can use to_bytes to get its individual bytes, >> but how can one reconstruct the int from the sequence of bytes? >> > The next thing in the docs after int.to_bytes is int.from_bytes: > http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes I am sorry to have overlooked that. But one thing I yet wonder is why there is no direct possibilty of converting a byte to an int in [0,255], i.e. with a constrct int(b), where b is a byte. M. K. Shen From ian.g.kelly at gmail.com Thu May 30 14:42:24 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 30 May 2013 12:42:24 -0600 Subject: How to get an integer from a sequence of bytes In-Reply-To: References: Message-ID: On Thu, May 30, 2013 at 12:26 PM, Mok-Kong Shen wrote: > Am 27.05.2013 17:30, schrieb Ned Batchelder: >> >> On 5/27/2013 10:45 AM, Mok-Kong Shen wrote: >>> >>> From an int one can use to_bytes to get its individual bytes, >>> but how can one reconstruct the int from the sequence of bytes? >>> >> The next thing in the docs after int.to_bytes is int.from_bytes: >> http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes > > > I am sorry to have overlooked that. But one thing I yet wonder is why > there is no direct possibilty of converting a byte to an int in [0,255], > i.e. with a constrct int(b), where b is a byte. The bytes object can be viewed as a sequence of ints. So if b is a bytes object of non-zero length, then b[0] is an int in range(0, 256). From wxjmfauth at gmail.com Thu May 30 14:53:15 2013 From: wxjmfauth at gmail.com (jmfauth) Date: Thu, 30 May 2013 11:53:15 -0700 (PDT) Subject: How to get an integer from a sequence of bytes References: Message-ID: On 30 mai, 20:42, Ian Kelly wrote: > On Thu, May 30, 2013 at 12:26 PM, Mok-Kong Shen > > wrote: > > Am 27.05.2013 17:30, schrieb Ned Batchelder: > > >> On 5/27/2013 10:45 AM, Mok-Kong Shen wrote: > > >>> From an int one can use to_bytes to get its individual bytes, > >>> but how can one reconstruct the int from the sequence of bytes? > > >> The next thing in the docs after int.to_bytes is int.from_bytes: > >>http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes > > > I am sorry to have overlooked that. But one thing I yet wonder is why > > there is no direct possibilty of converting a byte to an int in [0,255], > > i.e. with a constrct int(b), where b is a byte. > > The bytes object can be viewed as a sequence of ints. ?So if b is a > bytes object of non-zero length, then b[0] is an int in range(0, 256). ---- Well, Python now "speaks" only "integer", the rest is commodity and there is a good coherency. >>> bin(255) '0b11111111' >>> oct(255) '0o377' >>> 255 255 >>> hex(255) '0xff' >>> >>> int('0b11111111', 2) 255 >>> int('0o377', 8) 255 >>> int('255') 255 >>> int('0xff', 16) 255 >>> >>> 0b11111111 255 >>> 0o377 255 >>> 255 255 >>> 0xff 255 >>> >>> type(0b11111111) >>> type(0o377) >>> type(255) >>> type(0xff) jmf From ned at nedbatchelder.com Thu May 30 15:22:26 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Thu, 30 May 2013 15:22:26 -0400 Subject: How to get an integer from a sequence of bytes In-Reply-To: References: Message-ID: <51A7A6F2.3070007@nedbatchelder.com> On 5/30/2013 2:26 PM, Mok-Kong Shen wrote: > Am 27.05.2013 17:30, schrieb Ned Batchelder: >> On 5/27/2013 10:45 AM, Mok-Kong Shen wrote: >>> From an int one can use to_bytes to get its individual bytes, >>> but how can one reconstruct the int from the sequence of bytes? >>> >> The next thing in the docs after int.to_bytes is int.from_bytes: >> http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes > > I am sorry to have overlooked that. But one thing I yet wonder is why > there is no direct possibilty of converting a byte to an int in [0,255], > i.e. with a constrct int(b), where b is a byte. > Presumably you want this to work: >>> int(b'\x03') 3 But you also want this to work: >>> int(b'7') 7 These two interpretations are incompatible. If b'\x03' becomes 3, then shouldn't b'\x37' become 55? But b'\x37' is b'7', and you want that to be 7. --Ned. > M. K. Shen > From invalid at invalid.invalid Tue May 28 12:04:01 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 16:04:01 +0000 (UTC) Subject: How to get an integer from a sequence of bytes References: Message-ID: On 2013-05-27, Mok-Kong Shen wrote: > From an int one can use to_bytes to get its individual bytes, > but how can one reconstruct the int from the sequence of bytes? One way is using the struct module. -- Grant Edwards grant.b.edwards Yow! Uh-oh!! I forgot at to submit to COMPULSORY gmail.com URINALYSIS! From phd at phdru.name Sun May 26 13:37:03 2013 From: phd at phdru.name (Oleg Broytman) Date: Sun, 26 May 2013 21:37:03 +0400 Subject: SQLObject 1.3.3 and 1.4.1 Message-ID: <20130526173703.GC14598@iskra.aviel.ru> Hello! I'm pleased to announce bugfix releases 1.3.3 and 1.4.1. What's new in SQLObject ======================= * Fixed bugs in pickling and unpickling (remove/restore a weak proxy to self, fixed cache handling). * Added an example of using SQLObject with web.py to the links page. Contributors for this release are Andrew Trusty and Rhubarb Sin. For a more complete list, please see the news: http://sqlobject.org/News.html What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with. SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite, Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB). Where is SQLObject ================== Site: http://sqlobject.org Development: http://sqlobject.org/devel/ Mailing list: https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss Archives: http://news.gmane.org/gmane.comp.python.sqlobject Download: https://pypi.python.org/pypi/SQLObject/1.3.3 https://pypi.python.org/pypi/SQLObject/1.4.1 News and changes: http://sqlobject.org/News.html Oleg. -- Oleg Broytman http://phdru.name/ phd at phdru.name Programmers don't die, they just GOSUB without RETURN. From britten.bryan at gmail.com Mon May 27 16:47:40 2013 From: britten.bryan at gmail.com (Bryan Britten) Date: Mon, 27 May 2013 13:47:40 -0700 (PDT) Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() Message-ID: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> Hey, everyone! I'm very new to Python and have only been using it for a couple of days, but have some experience in programming (albeit mostly statistical programming in SAS or R) so I'm hoping someone can answer this question in a technical way, but without using an abundant amount of jargon. The issue I'm having is that I'm trying to pull information from a website to practice Python with, but I'm having trouble getting the data in a timely fashion. If I use the following code: import json import urllib urlStr = "https://stream.twitter.com/1/statuses/sample.json" twtrDict = [json.loads(line) for line in urllib.urlopen(urlStr)] I get a memory issue. I'm running 32-bit Python 2.7 with 4 gigs of RAM if that helps at all. If I use the following code: import urllib urlStr = "https://stream.twitter.com/1/statuses/sample.json" fileHandle = urllib.urlopen(urlStr) twtrText = fileHandle.readlines() It takes hours (upwards of 6 or 7, if not more) to finish computing the last command. With that being said, my question is whether there is a more efficient manner to do this. I'm worried that if it's taking this long to process the .readlines() command, trying to work with the data is going to be a computational nightmare. Thanks in advance for any insights or advice! From roy at panix.com Mon May 27 16:56:31 2013 From: roy at panix.com (Roy Smith) Date: Mon, 27 May 2013 16:56:31 -0400 Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> Message-ID: In article <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e at googlegroups.com>, Bryan Britten wrote: > If I use the following code: > > > import urllib > > urlStr = "https://stream.twitter.com/1/statuses/sample.json" > > fileHandle = urllib.urlopen(urlStr) > > twtrText = fileHandle.readlines() > > > It takes hours (upwards of 6 or 7, if not more) to finish computing the last > command. I'm not surprised! readlines() reads in the ENTIRE file in one gulp. That a lot of tweets! > With that being said, my question is whether there is a more efficient manner > to do this. In general, when reading a large file, you want to iterate over lines of the file and process each one. Something like: for line in urllib.urlopen(urlStr): twtrDict = json.loads(line) You still need to download and process all the data, but at least you don't need to store it in memory all at once. There is an assumption here that there's exactly one json object per line. If that's not the case, things might get a little more complicated. From britten.bryan at gmail.com Mon May 27 17:29:38 2013 From: britten.bryan at gmail.com (Bryan Britten) Date: Mon, 27 May 2013 14:29:38 -0700 (PDT) Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() In-Reply-To: References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> Message-ID: Try to not sigh audibly as I ask what I'm sure are two asinine questions. 1) How is this approach different from twtrDict = [json.loads(line) for line in urllib.urlopen(urlStr)]? 2) How do I tell how many JSON objects are on each line? From denismfmcmahon at gmail.com Mon May 27 17:35:22 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Mon, 27 May 2013 21:35:22 +0000 (UTC) Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> Message-ID: On Mon, 27 May 2013 14:29:38 -0700, Bryan Britten wrote: > Try to not sigh audibly as I ask what I'm sure are two asinine > questions. > > 1) How is this approach different from twtrDict = [json.loads(line) for > line in urllib.urlopen(urlStr)]? > > 2) How do I tell how many JSON objects are on each line? Your code at (1) creates a single list of all the json objects The code you replied to loaded each object, assumed you did something with it, and then over-wrote it with the next one. As for (2) - either inspection, or errors from the json parser. -- Denis McMahon, denismfmcmahon at gmail.com From fabiosantosart at gmail.com Mon May 27 19:36:54 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 00:36:54 +0100 Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() In-Reply-To: References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> Message-ID: On 27 May 2013 22:36, "Bryan Britten" wrote: > > Try to not sigh audibly as I ask what I'm sure are two asinine questions. > > 1) How is this approach different from twtrDict = [json.loads(line) for line in urllib.urlopen(urlStr)]? > The suggested approach made use of generators. Just because you can iterate over something, that doesn't mean it is all in memory ;) Check out the difference between range() and xrange() in python 2 -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Mon May 27 19:58:05 2013 From: davea at davea.name (Dave Angel) Date: Mon, 27 May 2013 19:58:05 -0400 Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() In-Reply-To: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> Message-ID: <51A3F30D.5070800@davea.name> On 05/27/2013 04:47 PM, Bryan Britten wrote: > Hey, everyone! > > I'm very new to Python and have only been using it for a couple of days, but have some experience in programming (albeit mostly statistical programming in SAS or R) so I'm hoping someone can answer this question in a technical way, but without using an abundant amount of jargon. > > The issue I'm having is that I'm trying to pull information from a website to practice Python with, but I'm having trouble getting the data in a timely fashion. If I use the following code: > > > import json > import urllib > > urlStr = "https://stream.twitter.com/1/statuses/sample.json" > > twtrDict = [json.loads(line) for line in urllib.urlopen(urlStr)] > > > I get a memory issue. I'm running 32-bit Python 2.7 with 4 gigs of RAM if that helps at all. Which OS? The first question I'd ask is how big this file is. I can't tell, since it needs a user name & password to actually get the file. But it's not unusual to need at least double that space in memory, and in Windoze you're limited to two gig max, regardless of how big your hardware might be. If you separately fetch the file, then you can experiment with it, including cutting it down to a dozen lines, and see if you can deal with that much. How could you fetch it? With wget, with a browser (and saveAs), with a simple loop which uses read(4096) repeatedly and writes each block to a local file. Don't forget to use 'wb', as you don't know yet what line endings it might use. Once you have an idea what the data looks like, you can answer such questions as whether it's json at all, whether the lines each contain a single json record, or what. For all we know, the file might be a few terabytes in size. -- DaveA From britten.bryan at gmail.com Mon May 27 23:11:34 2013 From: britten.bryan at gmail.com (Bryan Britten) Date: Mon, 27 May 2013 20:11:34 -0700 (PDT) Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() In-Reply-To: References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> Message-ID: <4db5a3be-d9dc-455c-8e3b-5adebad2dcdd@googlegroups.com> On Monday, May 27, 2013 7:58:05 PM UTC-4, Dave Angel wrote: > On 05/27/2013 04:47 PM, Bryan Britten wrote: > > > Hey, everyone! > > > > > > I'm very new to Python and have only been using it for a couple of days, but have some experience in programming (albeit mostly statistical programming in SAS or R) so I'm hoping someone can answer this question in a technical way, but without using an abundant amount of jargon. > > > > > > The issue I'm having is that I'm trying to pull information from a website to practice Python with, but I'm having trouble getting the data in a timely fashion. If I use the following code: > > > > > > > > > import json > > > import urllib > > > > > > urlStr = "https://stream.twitter.com/1/statuses/sample.json" > > > > > > twtrDict = [json.loads(line) for line in urllib.urlopen(urlStr)] > > > > > > > > > I get a memory issue. I'm running 32-bit Python 2.7 with 4 gigs of RAM if that helps at all. > > > > Which OS? I'm operating on Windows 7. > > The first question I'd ask is how big this file is. I can't tell, since > > it needs a user name & password to actually get the file. If you have Twitter, you can just use your log-in information to access the file. > But it's not unusual to need at least double that space in memory, and in Windoze > > you're limited to two gig max, regardless of how big your hardware might be. > > > > If you separately fetch the file, then you can experiment with it, > > including cutting it down to a dozen lines, and see if you can deal with > > that much. > > > > How could you fetch it? With wget, with a browser (and saveAs), with a > > simple loop which uses read(4096) repeatedly and writes each block to a > > local file. Don't forget to use 'wb', as you don't know yet what line > > endings it might use. > I'm not familiar with using read(4096), I'll have to look into that. When I tried to just save the file, my computer just sat in limbo for some time and didn't seem to want to process the command. > > Once you have an idea what the data looks like, you can answer such > > questions as whether it's json at all, whether the lines each contain a > > single json record, or what. > Based on my *extremely* limited knowledge of JSON, that's definitely the type of file this is. Here is a snippet of what is seen when you log in: {"created_at":"Tue May 28 03:09:23 +0000 2013","id":339216806461972481,"id_str":"339216806461972481","text":"RT @aleon_11: Sigo creyendo que las noches lluviosas me acercan mucho m\u00e1s a ti!","source":"\u003ca href=\"http:\/\/blackberry.com\/twitter\" rel=\"nofollow\"\u003eTwitter for BlackBerry\u00ae\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":310910123,"id_str":"310910123","name":"\u2661","screen_name":"LaMarielita_","location":"","url":null,"description":"MERCADOLOGA & PUBLICISTA EN PROCESO, AMO A MI DIOS & MI FAMILIA\u2665 ME ENCANTA REIRME , MOLESTAR & HABLAR :D BFF, pancho, ale & china :) LY\u2661","protected":false,"followers_count":506,"friends_count":606,"listed_count":1,"created_at":"Sat Jun 04 15:24:19 +0000 2011","favourites_count":207,"utc_offset":-25200,"time_zone":"Mountain Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":17241,"lang":"es","contributors_enabled":false,"is_translator":false,"profile_background_color":"FF6699","profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme11\/bg.gif","profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme11\/bg.gif","profile_background_tile":true,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3720425493\/13a48910e56ca34edeea07ff04075c77_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3720425493\/13a48910e56ca34edeea07ff04075c77_normal.jpeg","profile_link_color":"B40B43","profile_sidebar_border_color":"CC3366","profile_sidebar_fill_color":"E5507E","profile_text_color":"362720","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Tue May 28 02:57:40 +0000 2013","id":339213856922537984,"id_str":"339213856922537984","text":"Sigo creyendo que las noches lluviosas me acercan mucho m\u00e1s a ti!","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":105252134,"id_str":"105252134","name":"Alejandra Le\u00f3n","screen_name":"aleon_11","location":"Guatemala","url":null,"description":"La vida se disfruta m\u00e1s, cuando no se le pone tanta importancia.","protected":false,"followers_count":143,"friends_count":251,"listed_count":0,"created_at":"Fri Jan 15 20:49:38 +0000 2010","favourites_count":83,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":1863,"lang":"es","contributors_enabled":false,"is_translator":false,"profile_background_color":"F8F2FC","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/811443451\/81abf2f37ee3e37deda396befa7fb557.jpeg","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/811443451\/81abf2f37ee3e37deda396befa7fb557.jpeg","profile_background_tile":true,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3578979563\/e973196904e25af5d960f2971616eb61_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3578979563\/e973196904e25af5d960f2971616eb61_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/105252134\/1364957374","profile_link_color":"F01A1A","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"7AC3EE","profile_text_color":"3D1957","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":2,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"es"},"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"aleon_11","name":"Alejandra Le\u00f3n","id":105252134,"id_str":"105252134","indices":[3,12]}]},"favorited":false,"retweeted":false,"filter_level":"low"} > > For all we know, the file might be a few terabytes in size. > > > > > > -- > > DaveA From fabiosantosart at gmail.com Tue May 28 03:31:35 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 08:31:35 +0100 Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() In-Reply-To: <4db5a3be-d9dc-455c-8e3b-5adebad2dcdd@googlegroups.com> References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> <4db5a3be-d9dc-455c-8e3b-5adebad2dcdd@googlegroups.com> Message-ID: On 28 May 2013 04:19, "Bryan Britten" wrote: > I'm not familiar with using read(4096), I'll have to look into that. When I tried to just save the file, my computer just sat in limbo for some time and didn't seem to want to process the command. That's just file.read with an integer argument. You can read a file by chunks by repeatedly calling that function until you get the empty string. > Based on my *extremely* limited knowledge of JSON, that's definitely the type of file this is. Here is a snippet of what is seen when you log in: ... That's json. It's pretty big, but not big enough to stall a slow computer more than half a second. - I've looked for documentation on that method on twitter. It seems that it's part of the twitter streaming api. https://dev.twitter.com/docs/streaming-apis What this means is that the requests aren't supposed to end. They are supposed to be read gradually, using the lines to split the response into meaningful chunks. That's why you can't read the data and why your browser never gets around to download it. Both urlopen and your browser block while waiting for the request to end. Here's more info on streaming requests on their docs: https://dev.twitter.com/docs/streaming-apis/processing For streaming requests in python, I would point you to the requests library, but I am not sure it handles streaming requests. -------------- next part -------------- An HTML attachment was scrubbed... URL: From britten.bryan at gmail.com Tue May 28 10:32:20 2013 From: britten.bryan at gmail.com (Bryan Britten) Date: Tue, 28 May 2013 07:32:20 -0700 (PDT) Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() In-Reply-To: References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> <4db5a3be-d9dc-455c-8e3b-5adebad2dcdd@googlegroups.com> Message-ID: <31d86773-88d4-43e2-8699-39021a5f27b8@googlegroups.com> Thanks to everyone for the help and insight. I think for now I'll just back away from this file and go back to something much easier to practice with. From alister.ware at ntlworld.com Tue May 28 13:52:28 2013 From: alister.ware at ntlworld.com (Alister) Date: Tue, 28 May 2013 17:52:28 GMT Subject: Reading *.json from URL - json.loads() versus urllib.urlopen.readlines() References: <10be5c62-4c58-4b4f-b00a-82d85ee4ef8e@googlegroups.com> <4db5a3be-d9dc-455c-8e3b-5adebad2dcdd@googlegroups.com> Message-ID: On Tue, 28 May 2013 08:31:35 +0100, F?bio Santos wrote: > On 28 May 2013 04:19, "Bryan Britten" wrote: >> I'm not familiar with using read(4096), I'll have to look into that. >> When > I tried to just save the file, my computer just sat in limbo for some > time and didn't seem to want to process the command. > > That's just file.read with an integer argument. You can read a file by > chunks by repeatedly calling that function until you get the empty > string. > >> Based on my *extremely* limited knowledge of JSON, that's definitely >> the > type of file this is. Here is a snippet of what is seen when you log in: > ... > That's json. It's pretty big, but not big enough to stall a slow > computer more than half a second. > > - > > I've looked for documentation on that method on twitter. > > It seems that it's part of the twitter streaming api. > > https://dev.twitter.com/docs/streaming-apis > > What this means is that the requests aren't supposed to end. They are > supposed to be read gradually, using the lines to split the response > into meaningful chunks. That's why you can't read the data and why your > browser never gets around to download it. Both urlopen and your browser > block while waiting for the request to end. Are we overlooking the obvious why not use one of the Python twitter modules to isolate your app from the nitty-gritty details of the twitter stream https://dev.twitter.com/docs/twitter-libraries -- Given sufficient time, what you put off doing today will get done by itself. From romila.anamaria at yahoo.com Mon May 27 16:50:24 2013 From: romila.anamaria at yahoo.com (Romila Anamaria) Date: Mon, 27 May 2013 20:50:24 -0000 Subject: .mat files processing in Python Message-ID: <1369687416.80604.YahooMailNeo@web161705.mail.bf1.yahoo.com> Hello, I am beginner in Python programming and I want to make an application (GUI) in Python 2.6 + wxPython for process data stored in? .mat files . ? (I make such data? processing in MatLAB.) ? My application consists in: ??????????????? -selecting the path of the files process? and? the file i want to process ? The .mat files contain several variables: ??????????????? -each variable corresponds to a key ? -when I select one key (or multiple keys) I want process only values ?? of variable(variables) that i have?? selected ?? - I want to check if each value is within the limits specified by me (minimum and maximum? given in interactively mode from the interface- for example a textbox) and show me status of comparison:? passed (if all values ??are in range) or failed (if one or more values are ??out of range) + the ?values ??that are out of limits ?? - after comparison, the results should be saved in a template( excel file) which I want to complete in the interface with the name of the person who makes the"test", time, file name, name of the variable / variables, and for variables? specify status:? passed / failed, and for ? failed?? speify the values ??that came out of limits ? ?-I want to do graphics (plots) of variation of values ??in time, and there should be a different colored line on both sides of the graph to be one line with different color-the lines mark the boundaries? of values - I want to make a comparison between two measurements of values that belong to the same variable, within the same graphic -I want to save these graphics in. jpeg or . png format, and integrated these graphics in the excel template? Can someone help me to make this application? Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: PROJECT.rar Type: application/octet-stream Size: 2038929 bytes Desc: not available URL: From dwight.guth at gmail.com Mon May 27 17:22:17 2013 From: dwight.guth at gmail.com (dwight.guth at gmail.com) Date: Mon, 27 May 2013 14:22:17 -0700 (PDT) Subject: Minor consistency question in io.IOBase Message-ID: Hi, so, I don't necessarily know if this is the right place to ask this question since it's kindof a rather technical one which gets into details of the python interpreter itself, but I thought I'd start here and if nobody knew the answer, they could let me know if it makes sense to ask on python-dev. I am wondering why it is that the default implementations of certain of the "mixin" methods on IOBase seem to behave somewhat inconsistently. Specifically, the behavior of precisely when in the pipeline a method checks to see whether the object is already closed seems to be inconsistent. In the following methods: IOBase.__next__ IOBase.readline IOBase.tell In each case, these methods never check to see whether the file is closed. __next__ immediately calls readline(), readline(limit) calls read(1) at most limit times, and tell calls seek(0, 1). It is relying on the underlying method to raise a ValueError if the file is already closed. Otherwise, __next__ and readline will raise AttributeErrors on unreadable files, and tell will raise an UnsupportedOperation on unseekable files. A side effect of this is that readline(0) on a closed file will succeed. In the following methods, however: IOBase.writelines IOBase.readlines In each case, the methods will check to determine whether the file has been closed before ever calling their underlying method. Thus, calling writelines([]) will raise a ValueError on a closed file, though it never calls write. Similarly, readlines() will raise a ValueError on a closed file before ever calling readline() even once. Can someone explain to me if there's some kind of consistent logic that explains the differing behaviors of these functions? Or is it simply an oversight on the part of the people who wrote Modules/_io/iobase.c? From carlosnepomuceno at outlook.com Mon May 27 21:59:33 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 04:59:33 +0300 Subject: Minor consistency question in io.IOBase In-Reply-To: References: Message-ID: ---------------------------------------- > Date: Mon, 27 May 2013 14:22:17 -0700 > Subject: Minor consistency question in io.IOBase > From: dwight.guth at gmail.com > To: python-list at python.org > > Hi, so, I don't necessarily know if this is the right place to ask this question since it's kindof a rather technical one which gets into details of the python interpreter itself, but I thought I'd start here and if nobody knew the answer, they could let me know if it makes sense to ask on python-dev. > > I am wondering why it is that the default implementations of certain of the "mixin" methods on IOBase seem to behave somewhat inconsistently. Specifically, the behavior of precisely when in the pipeline a method checks to see whether the object is already closed seems to be inconsistent. > > In the following methods: > > IOBase.__next__ > IOBase.readline > IOBase.tell > > In each case, these methods never check to see whether the file is closed. __next__ immediately calls readline(), readline(limit) calls read(1) at most limit times, and tell calls seek(0, 1). It is relying on the underlying method to raise a ValueError if the file is already closed. Otherwise, __next__ and readline will raise AttributeErrors on unreadable files, and tell will raise an UnsupportedOperation on unseekable files. A side effect of this is that readline(0) on a closed file will succeed. According to [1] & [2] it's a bug! Report the issue: http://bugs.python.org/ "close() Flush and close this stream. This method has no effect if the file is already closed. Once the file is closed, any operation on the file (e.g. reading or writing) will raise a ValueError." [1] http://docs.python.org/2/library/io.html#i-o-base-classes [2] http://docs.python.org/3.3/library/io.html#i-o-base-classes From ray at aarden.us Mon May 27 21:32:43 2013 From: ray at aarden.us (ray) Date: Mon, 27 May 2013 18:32:43 -0700 (PDT) Subject: How to: Setuptools Message-ID: I would like to use easy_install, but can't figure out how to install it. I have 64-bit Python 2.7.5 on Windows 7. Following the instructions on https://pypi.python.org/pypi/setuptools, it says: Download ez_setup.py and run it; it will download the appropriate .egg file and install it for you. (Currently, the provided .exe installer does not support 64-bit versions of Python for Windows, due to a distutils installer compatibility issue Being new to Python, I don't know what it means to "run it". I am not sure what I am looking at when I open it as the first line is "#!python". Looking down into the content of ez_setup.py, I find: 'setuptools-0.6c10-py2.6.egg': but there is no entry 'setuptools-0.6c10-py2.7.egg': Searching for it, I found a version at: https://pypi.python.org/packages/2.7/s/setuptools/ This appeared to be a linux version, the first line is: #!/bin/sh and the content seems to be encoded. There is an exe at https://pypi.python.org/pypi/setuptools, but the instructions on the page state the .exe won't work for 64-bit installs. Ray From carlosnepomuceno at outlook.com Mon May 27 21:45:03 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 04:45:03 +0300 Subject: How to: Setuptools In-Reply-To: References: Message-ID: curl -O http://peak.telecommunity.com/dist/ez_setup.py python ez_setup.py ---------------------------------------- > Date: Mon, 27 May 2013 18:32:43 -0700 > Subject: How to: Setuptools > From: ray at aarden.us > To: python-list at python.org > > I would like to use easy_install, but can't figure out how to install it. > > I have 64-bit Python 2.7.5 on Windows 7. > > Following the instructions on https://pypi.python.org/pypi/setuptools, it says: > Download ez_setup.py and run it; it will download the appropriate .egg file and install it for you. (Currently, the provided .exe installer does not support 64-bit versions of Python for Windows, due to a distutils installer compatibility issue > > Being new to Python, I don't know what it means to "run it". I am not sure what I am looking at when I open it as the first line is "#!python". > > Looking down into the content of ez_setup.py, I find: > 'setuptools-0.6c10-py2.6.egg': > but there is no entry > 'setuptools-0.6c10-py2.7.egg': > > Searching for it, I found a version at: > https://pypi.python.org/packages/2.7/s/setuptools/ > > This appeared to be a linux version, the first line is: > #!/bin/sh > and the content seems to be encoded. > > There is an exe at https://pypi.python.org/pypi/setuptools, but the > instructions on the page state the .exe won't work for 64-bit installs. > > Ray > > -- > http://mail.python.org/mailman/listinfo/python-list From rustompmody at gmail.com Mon May 27 22:57:47 2013 From: rustompmody at gmail.com (rusi) Date: Mon, 27 May 2013 19:57:47 -0700 (PDT) Subject: How to: Setuptools References: Message-ID: <88d4a817-f991-4599-927f-5b1f9804aa49@ve4g2000pbb.googlegroups.com> On May 28, 6:45?am, Carlos Nepomuceno wrote: > curl -Ohttp://peak.telecommunity.com/dist/ez_setup.py > python ez_setup.py Curl comes built into windows?? Does not seem so... http://serverfault.com/questions/483754/is-there-a-built-in-command-line-tool-under-windows-like-wget-curl Also given that the question clearly says 'I dont know what it means to "run it" ' I think your two lines could be too cryptic :-) To the OP: Sorry I am not being very helpful to you -- I am not on windows now and dont know these commands offhand I am sure someone else will chime in.. From carlosnepomuceno at outlook.com Mon May 27 23:06:23 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 06:06:23 +0300 Subject: How to: Setuptools In-Reply-To: <88d4a817-f991-4599-927f-5b1f9804aa49@ve4g2000pbb.googlegroups.com> References: , , <88d4a817-f991-4599-927f-5b1f9804aa49@ve4g2000pbb.googlegroups.com> Message-ID: ---------------------------------------- > Date: Mon, 27 May 2013 19:57:47 -0700 > Subject: Re: How to: Setuptools > From: rustompmody at gmail.com > To: python-list at python.org > > On May 28, 6:45 am, Carlos Nepomuceno > wrote: >> curl -Ohttp://peak.telecommunity.com/dist/ez_setup.py >> python ez_setup.py > > Curl comes built into windows?? No, but you can get it from http://curl.haxx.se/download.html > Does not seem so... > http://serverfault.com/questions/483754/is-there-a-built-in-command-line-tool-under-windows-like-wget-curl > > Also given that the question clearly says 'I dont know what it means > to "run it" ' > I think your two lines could be too cryptic :-) Perhaps, my grandma used to think English is very cryptic. ;) > To the OP: Sorry I am not being very helpful to you -- I am not on > windows now and dont know these commands offhand > > I am sure someone else will chime in.. > -- > http://mail.python.org/mailman/listinfo/python-list From rustompmody at gmail.com Mon May 27 23:54:53 2013 From: rustompmody at gmail.com (rusi) Date: Mon, 27 May 2013 20:54:53 -0700 (PDT) Subject: How to: Setuptools References: , , <88d4a817-f991-4599-927f-5b1f9804aa49@ve4g2000pbb.googlegroups.com> Message-ID: <452a2e20-6c64-4761-9da0-41ccf9a4fa6c@z10g2000pbn.googlegroups.com> On May 28, 8:06?am, Carlos Nepomuceno wrote: > ---------------------------------------- > > > Date: Mon, 27 May 2013 19:57:47 -0700 > > Subject: Re: How to: Setuptools > > From: rustompm... at gmail.com > > To: python-l... at python.org > > > On May 28, 6:45 am, Carlos Nepomuceno > > wrote: > >> curl -Ohttp://peak.telecommunity.com/dist/ez_setup.py > >> python ez_setup.py > > > Curl comes built into windows?? > > No, but you can get it fromhttp://curl.haxx.se/download.html Oooff! Talk of using sledgehammers to crack nuts... All that is needed is to visit http://peak.telecommunity.com/dist/ez_setup.py with the browser and to save the file!! From carlosnepomuceno at outlook.com Tue May 28 00:09:39 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 07:09:39 +0300 Subject: How to: Setuptools In-Reply-To: <452a2e20-6c64-4761-9da0-41ccf9a4fa6c@z10g2000pbn.googlegroups.com> References: , , , , <88d4a817-f991-4599-927f-5b1f9804aa49@ve4g2000pbb.googlegroups.com>, , <452a2e20-6c64-4761-9da0-41ccf9a4fa6c@z10g2000pbn.googlegroups.com> Message-ID: ---------------------------------------- > Date: Mon, 27 May 2013 20:54:53 -0700 > Subject: Re: How to: Setuptools > From: rustompmody at gmail.com [...] > > Oooff! Talk of using sledgehammers to crack nuts... > > All that is needed is to visit http://peak.telecommunity.com/dist/ez_setup.py > with the browser and to save the file!! Can you show me how to do that from a batch file? Please... From rustompmody at gmail.com Tue May 28 00:26:21 2013 From: rustompmody at gmail.com (rusi) Date: Mon, 27 May 2013 21:26:21 -0700 (PDT) Subject: How to: Setuptools References: , , , , <88d4a817-f991-4599-927f-5b1f9804aa49@ve4g2000pbb.googlegroups.com>, , <452a2e20-6c64-4761-9da0-41ccf9a4fa6c@z10g2000pbn.googlegroups.com> Message-ID: <5d03e793-4df0-47b8-ac7f-85ab57fc752d@z10g2000pbn.googlegroups.com> On May 28, 9:09?am, Carlos Nepomuceno wrote: > ---------------------------------------- > > > Date: Mon, 27 May 2013 20:54:53 -0700 > > Subject: Re: How to: Setuptools > > From: rustompm... at gmail.com > [...] > > > Oooff! Talk of using sledgehammers to crack nuts... > > > All that is needed is to visithttp://peak.telecommunity.com/dist/ez_setup.py > > with the browser and to save the file!! > > Can you show me how to do that from a batch file? Please... What in the OP's question translates into a need for a batch-file? And you are proposing that he downloads curl just to write that batch- file?!?! How come that proposal is not subject to the same requirement, viz. I dont see in your recipe any: "Here-is-a-batchfile-to-download-curl-without-curl?" From carlosnepomuceno at outlook.com Tue May 28 00:59:25 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 04:59:25 -0000 Subject: How to: Setuptools In-Reply-To: <5d03e793-4df0-47b8-ac7f-85ab57fc752d@z10g2000pbn.googlegroups.com> References: , ,,, , , <88d4a817-f991-4599-927f-5b1f9804aa49@ve4g2000pbb.googlegroups.com>, , , , <452a2e20-6c64-4761-9da0-41ccf9a4fa6c@z10g2000pbn.googlegroups.com>, , <5d03e793-4df0-47b8-ac7f-85ab57fc752d@z10g2000pbn.googlegroups.com> Message-ID: ---------------------------------------- > Date: Mon, 27 May 2013 21:26:21 -0700 > Subject: Re: How to: Setuptools > From: rustompmody at gmail.com > To: python-list at python.org > > On May 28, 9:09 am, Carlos Nepomuceno > wrote: >> ---------------------------------------- >> >>> Date: Mon, 27 May 2013 20:54:53 -0700 >>> Subject: Re: How to: Setuptools >>> From: rustompm... at gmail.com >> [...] >> >>> Oooff! Talk of using sledgehammers to crack nuts... >> >>> All that is needed is to visithttp://peak.telecommunity.com/dist/ez_setup.py >>> with the browser and to save the file!! >> >> Can you show me how to do that from a batch file? Please... > > What in the OP's question translates into a need for a batch-file? > > And you are proposing that he downloads curl just to write that batch- > file?!?! > > How come that proposal is not subject to the same requirement, viz. I > dont see in your recipe any: > "Here-is-a-batchfile-to-download-curl-without-curl?" > -- > http://mail.python.org/mailman/listinfo/python-list So, you don't know how to download from a batch file? From rustompmody at gmail.com Tue May 28 08:03:53 2013 From: rustompmody at gmail.com (rusi) Date: Tue, 28 May 2013 05:03:53 -0700 (PDT) Subject: How to: Setuptools References: Message-ID: <22b6ddfe-100f-4ff5-853b-9ec4e2232e7a@h9g2000pbr.googlegroups.com> On May 28, 6:32?am, ray wrote: > I would like to use easy_install, but can't figure out how to install it. > > I have 64-bit Python 2.7.5 on Windows 7. > > Following the instructions onhttps://pypi.python.org/pypi/setuptools, it says: > Download ez_setup.py and run it; it will download the appropriate .egg file and install it for you. (Currently, the provided .exe installer does not support 64-bit versions of Python for Windows, due to a distutils installer compatibility issue > > Being new to Python, I don't know what it means to "run it". ?I am not sure what I am looking at when I open it as the first line is "#!python". > > Looking down into the content of ez_setup.py, I find: > 'setuptools-0.6c10-py2.6.egg': > but there is no entry > 'setuptools-0.6c10-py2.7.egg': > > Searching for it, I found a version at:https://pypi.python.org/packages/2.7/s/setuptools/ > > This appeared to be a linux version, the first line is: > #!/bin/sh > and the content seems to be encoded. > > There is an exe athttps://pypi.python.org/pypi/setuptools, but the > instructions on the page state the .exe won't work for 64-bit installs. > > Ray Since there seems to be no response to this, let me try (even though I am not on windows) Just point your browser at the link http://peak.telecommunity.com/dist/ez_setup.py And save the file ez_setup.py The more windows-y way of running this is to right-click this and edit the run-with in some way that I dont have at my finger-tips. The more old-fashioned way is to start a dos-box (run cmd.exe) navigate to the directory where the ez_setup.py is saved from there run the command: python ez_setup.py Do that and report back on what happened From breamoreboy at yahoo.co.uk Tue May 28 08:26:22 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 28 May 2013 13:26:22 +0100 Subject: How to: Setuptools In-Reply-To: <22b6ddfe-100f-4ff5-853b-9ec4e2232e7a@h9g2000pbr.googlegroups.com> References: <22b6ddfe-100f-4ff5-853b-9ec4e2232e7a@h9g2000pbr.googlegroups.com> Message-ID: On 28/05/2013 13:03, rusi wrote: > On May 28, 6:32 am, ray wrote: >> I would like to use easy_install, but can't figure out how to install it. >> >> I have 64-bit Python 2.7.5 on Windows 7. >> >> Following the instructions onhttps://pypi.python.org/pypi/setuptools, it says: >> Download ez_setup.py and run it; it will download the appropriate .egg file and install it for you. (Currently, the provided .exe installer does not support 64-bit versions of Python for Windows, due to a distutils installer compatibility issue >> >> Being new to Python, I don't know what it means to "run it". I am not sure what I am looking at when I open it as the first line is "#!python". >> >> Looking down into the content of ez_setup.py, I find: >> 'setuptools-0.6c10-py2.6.egg': >> but there is no entry >> 'setuptools-0.6c10-py2.7.egg': >> >> Searching for it, I found a version at:https://pypi.python.org/packages/2.7/s/setuptools/ >> >> This appeared to be a linux version, the first line is: >> #!/bin/sh >> and the content seems to be encoded. >> >> There is an exe athttps://pypi.python.org/pypi/setuptools, but the >> instructions on the page state the .exe won't work for 64-bit installs. >> >> Ray > > Since there seems to be no response to this, let me try (even though I > am not on windows) > Just point your browser at the link > http://peak.telecommunity.com/dist/ez_setup.py > And save the file ez_setup.py > > The more windows-y way of running this is to right-click this and edit > the run-with in some way that I dont have at my finger-tips. Do this, something goes wrong, no output to see. > > The more old-fashioned way is to start a dos-box (run cmd.exe) > navigate to the directory where the ez_setup.py is saved > from there run the command: > python ez_setup.py Do this, something goes wrong, output in front of you. > > Do that and report back on what happened > -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From ray at aarden.us Tue May 28 09:05:51 2013 From: ray at aarden.us (ray) Date: Tue, 28 May 2013 06:05:51 -0700 (PDT) Subject: How to: Setuptools References: <22b6ddfe-100f-4ff5-853b-9ec4e2232e7a@h9g2000pbr.googlegroups.com> Message-ID: <1495a8dd-bbf5-4dda-8733-89d6db0240f1@g8g2000yqa.googlegroups.com> On May 28, 7:26?am, Mark Lawrence wrote: > On 28/05/2013 13:03, rusi wrote: > > > > > > > On May 28, 6:32 am, ray wrote: > >> I would like to use easy_install, but can't figure out how to install it. > > >> I have 64-bit Python 2.7.5 on Windows 7. > > >> Following the instructions onhttps://pypi.python.org/pypi/setuptools, it says: > >> Download ez_setup.py and run it; it will download the appropriate .egg file and install it for you. (Currently, the provided .exe installer does not support 64-bit versions of Python for Windows, due to a distutils installer compatibility issue > > >> Being new to Python, I don't know what it means to "run it". ?I am not sure what I am looking at when I open it as the first line is "#!python". > > >> Looking down into the content of ez_setup.py, I find: > >> 'setuptools-0.6c10-py2.6.egg': > >> but there is no entry > >> 'setuptools-0.6c10-py2.7.egg': > > >> Searching for it, I found a version at:https://pypi.python.org/packages/2.7/s/setuptools/ > > >> This appeared to be a linux version, the first line is: > >> #!/bin/sh > >> and the content seems to be encoded. > > >> There is an exe athttps://pypi.python.org/pypi/setuptools, but the > >> instructions on the page state the .exe won't work for 64-bit installs. > > >> Ray > > > Since there seems to be no response to this, let me try (even though I > > am not on windows) > > Just point your browser at the link > >http://peak.telecommunity.com/dist/ez_setup.py > > And save the file ez_setup.py > > > The more windows-y way of running this is to right-click this and edit > > the run-with in some way that I dont have at my finger-tips. > > Do this, something goes wrong, no output to see. > > > > > The more old-fashioned way is to start a dos-box (run cmd.exe) > > navigate to the directory where the ez_setup.py is saved > > from there run the command: > > python ez_setup.py > > Do this, something goes wrong, output in front of you. > > > > > Do that and report back on what happened > > -- > If you're using GoogleCrap??? please read thishttp://wiki.python.org/moin/GoogleGroupsPython. > > Mark Lawrence > > The installation fails. The report stated it could not find the file. Per: Looking down into the content of ez_setup.py, I find: 'setuptools-0.6c10-py2.6.egg': but there is no entry 'setuptools-0.6c10-py2.7.egg': Since I am trying to install this for Python 2.7, it fails. Any suggestions? Ray From rustompmody at gmail.com Tue May 28 09:35:23 2013 From: rustompmody at gmail.com (rusi) Date: Tue, 28 May 2013 06:35:23 -0700 (PDT) Subject: How to: Setuptools References: <22b6ddfe-100f-4ff5-853b-9ec4e2232e7a@h9g2000pbr.googlegroups.com> <1495a8dd-bbf5-4dda-8733-89d6db0240f1@g8g2000yqa.googlegroups.com> Message-ID: On May 28, 6:05?pm, ray wrote: > On May 28, 7:26?am, Mark Lawrence wrote: > > > > > > > > > > > On 28/05/2013 13:03, rusi wrote: > > > > On May 28, 6:32 am, ray wrote: > > >> I would like to use easy_install, but can't figure out how to install it. > > > >> I have 64-bit Python 2.7.5 on Windows 7. > > > >> Following the instructions onhttps://pypi.python.org/pypi/setuptools, it says: > > >> Download ez_setup.py and run it; it will download the appropriate .egg file and install it for you. (Currently, the provided .exe installer does not support 64-bit versions of Python for Windows, due to a distutils installer compatibility issue > > > >> Being new to Python, I don't know what it means to "run it". ?I am not sure what I am looking at when I open it as the first line is "#!python". > > > >> Looking down into the content of ez_setup.py, I find: > > >> 'setuptools-0.6c10-py2.6.egg': > > >> but there is no entry > > >> 'setuptools-0.6c10-py2.7.egg': > > > >> Searching for it, I found a version at:https://pypi.python.org/packages/2.7/s/setuptools/ > > > >> This appeared to be a linux version, the first line is: > > >> #!/bin/sh > > >> and the content seems to be encoded. > > > >> There is an exe athttps://pypi.python.org/pypi/setuptools, but the > > >> instructions on the page state the .exe won't work for 64-bit installs. > > > >> Ray > > > > Since there seems to be no response to this, let me try (even though I > > > am not on windows) > > > Just point your browser at the link > > >http://peak.telecommunity.com/dist/ez_setup.py > > > And save the file ez_setup.py > > > > The more windows-y way of running this is to right-click this and edit > > > the run-with in some way that I dont have at my finger-tips. > > > Do this, something goes wrong, no output to see. > > > > The more old-fashioned way is to start a dos-box (run cmd.exe) > > > navigate to the directory where the ez_setup.py is saved > > > from there run the command: > > > python ez_setup.py > > > Do this, something goes wrong, output in front of you. > > > > Do that and report back on what happened > > > -- > > If you're using GoogleCrap??? please read thishttp://wiki.python.org/moin/GoogleGroupsPython. > > > Mark Lawrence > > The installation fails. ?The report stated it could not find the file. > Per: > Looking down into the content of ez_setup.py, I find: > 'setuptools-0.6c10-py2.6.egg': > but there is no entry > 'setuptools-0.6c10-py2.7.egg': > > Since I am trying to install this for Python 2.7, it fails. > > Any suggestions? > > Ray Mysterious... If you are in hack mode you could try patching that file with the 2.7 egg name and md5 Both of them here https://pypi.python.org/pypi/setuptools ie after this line 'setuptools-0.6c11-py2.6.egg': 'bfa92100bd772d5a213eedd356d64086', add this line 'setuptools-0.6c11-py2.7.egg': 'fe1f997bc722265116870bc7919059ea', However it maybe a better idea to ask on the distutils mailing list http://mail.python.org/pipermail/distutils-sig/ [And let us know how/what happened!] From breamoreboy at yahoo.co.uk Tue May 28 09:40:05 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 28 May 2013 14:40:05 +0100 Subject: How to: Setuptools In-Reply-To: <1495a8dd-bbf5-4dda-8733-89d6db0240f1@g8g2000yqa.googlegroups.com> References: <22b6ddfe-100f-4ff5-853b-9ec4e2232e7a@h9g2000pbr.googlegroups.com> <1495a8dd-bbf5-4dda-8733-89d6db0240f1@g8g2000yqa.googlegroups.com> Message-ID: On 28/05/2013 14:05, ray wrote: > > The installation fails. The report stated it could not find the file. > Per: > Looking down into the content of ez_setup.py, I find: > 'setuptools-0.6c10-py2.6.egg': > but there is no entry > 'setuptools-0.6c10-py2.7.egg': > > Since I am trying to install this for Python 2.7, it fails. > > Any suggestions? > > Ray > Try reading this http://stackoverflow.com/questions/3652625/installing-setuptools-on-64-bit-windows which refers to this http://www.lfd.uci.edu/~gohlke/pythonlibs/ where you can get pip which is an easy_install replacement. You pays your money, you takes your choice :) -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From rustompmody at gmail.com Tue May 28 09:54:28 2013 From: rustompmody at gmail.com (rusi) Date: Tue, 28 May 2013 06:54:28 -0700 (PDT) Subject: How to: Setuptools References: <22b6ddfe-100f-4ff5-853b-9ec4e2232e7a@h9g2000pbr.googlegroups.com> <1495a8dd-bbf5-4dda-8733-89d6db0240f1@g8g2000yqa.googlegroups.com> Message-ID: <739c5344-65d8-4587-a002-83cb66fd797e@pd6g2000pbc.googlegroups.com> On May 28, 6:40?pm, Mark Lawrence wrote: > On 28/05/2013 14:05, ray wrote: > > > > > The installation fails. ?The report stated it could not find the file. > > Per: > > Looking down into the content of ez_setup.py, I find: > > 'setuptools-0.6c10-py2.6.egg': > > but there is no entry > > 'setuptools-0.6c10-py2.7.egg': > > > Since I am trying to install this for Python 2.7, it fails. > > > Any suggestions? > > > Ray > > Try reading thishttp://stackoverflow.com/questions/3652625/installing-setuptools-on-6... > which refers to thishttp://www.lfd.uci.edu/~gohlke/pythonlibs/where > you can get pip which is an easy_install replacement. ?You pays your > money, you takes your choice :) Hmm... Something new (for me) Last I knew, pip was the supposed replacement for setuptools and you had to install setuptools to install pip!!! Maybe this has changed?? Or its different on windows?? From hermanmu at gmail.com Tue May 28 16:59:56 2013 From: hermanmu at gmail.com (Michael Herman) Date: Tue, 28 May 2013 13:59:56 -0700 Subject: How to: Setuptools In-Reply-To: References: Message-ID: I have a great video on how to setup Easy_Install via setuptools as well as pip - http://www.youtube.com/watch?v=MIHYflJwyLk On Mon, May 27, 2013 at 6:32 PM, ray wrote: > > I would like to use easy_install, but can't figure out how to install it. > > I have 64-bit Python 2.7.5 on Windows 7. > > Following the instructions on https://pypi.python.org/pypi/setuptools, it > says: > Download ez_setup.py and run it; it will download the appropriate .egg > file and install it for you. (Currently, the provided .exe installer does > not support 64-bit versions of Python for Windows, due to a distutils > installer compatibility issue > > Being new to Python, I don't know what it means to "run it". I am not > sure what I am looking at when I open it as the first line is "#!python". > > Looking down into the content of ez_setup.py, I find: > 'setuptools-0.6c10-py2.6.egg': > but there is no entry > 'setuptools-0.6c10-py2.7.egg': > > Searching for it, I found a version at: > https://pypi.python.org/packages/2.7/s/setuptools/ > > This appeared to be a linux version, the first line is: > #!/bin/sh > and the content seems to be encoded. > > There is an exe at https://pypi.python.org/pypi/setuptools, but the > instructions on the page state the .exe won't work for 64-bit installs. > > Ray > > -- > http://mail.python.org/mailman/listinfo/python-list From ray at aarden.us Tue May 28 22:11:10 2013 From: ray at aarden.us (ray) Date: Tue, 28 May 2013 19:11:10 -0700 (PDT) Subject: How to: Setuptools In-Reply-To: References: Message-ID: <9e1a70de-85d8-4f73-b0ad-8f0babcdca84@googlegroups.com> On Monday, May 27, 2013 8:32:43 PM UTC-5, ray wrote: > I would like to use easy_install, but can't figure out how to install it. > > > > I have 64-bit Python 2.7.5 on Windows 7. > > > > Following the instructions on https://pypi.python.org/pypi/setuptools, it says: > > Download ez_setup.py and run it; it will download the appropriate .egg file and install it for you. (Currently, the provided .exe installer does not support 64-bit versions of Python for Windows, due to a distutils installer compatibility issue > > > > Being new to Python, I don't know what it means to "run it". I am not sure what I am looking at when I open it as the first line is "#!python". > > > > Looking down into the content of ez_setup.py, I find: > > 'setuptools-0.6c10-py2.6.egg': > > but there is no entry > > 'setuptools-0.6c10-py2.7.egg': > > > > Searching for it, I found a version at: > > https://pypi.python.org/packages/2.7/s/setuptools/ > > > > This appeared to be a linux version, the first line is: > > #!/bin/sh > > and the content seems to be encoded. > > > > There is an exe at https://pypi.python.org/pypi/setuptools, but the > > instructions on the page state the .exe won't work for 64-bit installs. > > > > Ray I looked through distutils-sig/ . There were many finding the problem. There is a solution using Setuptools 2.7 bdist_wininst - http://groups.google.com/group/distutils-sig/browse_thread/thread/6107d5c9044c3f37/4d1a53f9e8fb7d20?hl=en&lnk=gst&q=Setuptools+2.7+bdist_wininst> It wasn't clear on where to run things from nor did it address the missing 2.7 reference. The reference - http://www.lfd.uci.edu/~gohlke/pythonlibs/ has a 64 bit installer - distribute-0.6.43.win-amd64-py2.7.exe. It ran without error reports. This all was to install netwokx. That installed without error. Ray From dia.sussex at gmail.com Tue May 28 06:00:42 2013 From: dia.sussex at gmail.com (Debbie) Date: Tue, 28 May 2013 03:00:42 -0700 (PDT) Subject: Python for IPSA (Power flow analysis) Message-ID: <6288a409-db46-4987-91a7-2f3dd65b42dc@googlegroups.com> Hi there, I am new to Python, and wondering if you could help me with python based coding for the IPSA (Power system analysis software). I have a electrical distribution network with generators, buses and loads, on which I am performing the load flow analysis every 1 hour for a period of 1 year. The code to perform instantaneous load/power flow analysis is given below. I need to modify it such that I can perform this load flow analysis every 1 hour for a period of 1 year. Please help. from ipsa import * ipsasys = IscInterface() net = ipsasys.ReadFile("refinery.iif") bok = net.DoLoadFlow(); if bok: busbars = net.GetBusbars() print "Load Flow results:" print "" print "BusName Vmag(kV)" print "====================" for bus in busbars.itervalues(): name = bus.GetName() vm = bus.GetVoltageMagnitudekV() res = "%-8s %10.5f" % (name, vm) print res else: print "Load Flow failed!" Regards, Debbie From davea at davea.name Tue May 28 07:14:37 2013 From: davea at davea.name (Dave Angel) Date: Tue, 28 May 2013 07:14:37 -0400 Subject: Python for IPSA (Power flow analysis) In-Reply-To: <6288a409-db46-4987-91a7-2f3dd65b42dc@googlegroups.com> References: <6288a409-db46-4987-91a7-2f3dd65b42dc@googlegroups.com> Message-ID: <51A4919D.7090200@davea.name> On 05/28/2013 06:00 AM, Debbie wrote: > Hi there, > I am new to Python, Welcome. Could you tell us a little about yourself, such as whether you've experience in a few other languages, or if Python is your first programming experience? Also, what version of Python (presumably 2.7 or 2.6) and what OS ? > and wondering if you could help me with python based coding for the IPSA (Power system analysis software). I have a electrical distribution network with generators, buses and loads, on which I am performing the load flow analysis every 1 hour for a period of 1 year. > > The code to perform instantaneous load/power flow analysis is given below. I need to modify it such that I can perform this load flow analysis every 1 hour for a period of 1 year. Please help. > > from ipsa import * I have no idea what functionality is in ipsa, so this whole message is a guess. First question is whether the ReadFile() below and the DoLoadFlow() read the whole year's data, or data for a particular hour. And if the latter, how do you get the next set of data? > > ipsasys = IscInterface() > net = ipsasys.ReadFile("refinery.iif") > bok = net.DoLoadFlow(); > if bok: > busbars = net.GetBusbars() > print "Load Flow results:" > print "" > print "BusName Vmag(kV)" > print "====================" > for bus in busbars.itervalues(): > name = bus.GetName() > vm = bus.GetVoltageMagnitudekV() > res = "%-8s %10.5f" % (name, vm) > print res > else: > print "Load Flow failed!" > > Regards, > Debbie > First you want to make a function to print out a particular hour's data. That might turn out to be something like: def one_hour(net): busbars = net.GetBusbars() print "Load Flow results:" print "" print "BusName Vmag(kV)" print "====================" for bus in busbars.itervalues(): name = bus.GetName() vm = bus.GetVoltageMagnitudekV() res = "%-8s %10.5f" % (name, vm) print res (Just pasted from your code, I added in the probable parameter to the definition) Now your main function might be something like: def main(filename): ipsasys = IscInterface() net = ipsasys.ReadFile(filename) #if net gets you data for one hour, figure out how #to get the data for the whole year, in the form #of a list or an iterator called nets bok = net.DoLoadFlow(); if bok: for net in nets: one_hour(net) else: print "Load Flow failed!" #and your top-level code is: main("refinery.iif") As to ordering in the source file, put the import first: from ipsa import * then your function definitions one_hour() and main(), then your top-level code. -- DaveA From spam at capuchin.co.uk Tue May 28 08:08:32 2013 From: spam at capuchin.co.uk (Robert Marshall) Date: Tue, 28 May 2013 13:08:32 +0100 Subject: Python for IPSA (Power flow analysis) References: <6288a409-db46-4987-91a7-2f3dd65b42dc@googlegroups.com> Message-ID: <87zjvfnw73.fsf@capuchin.co.uk> On Tue, May 28 2013, Debbie wrote: > Hi there, I am new to Python, and wondering if you could help me with > python based coding for the IPSA (Power system analysis software). I > have a electrical distribution network with generators, buses and > loads, on which I am performing the load flow analysis every 1 hour > for a period of 1 year. > > The code to perform instantaneous load/power flow analysis is given > below. I need to modify it such that I can perform this load flow > analysis every 1 hour for a period of 1 year. Please help. > > from ipsa import * > > ipsasys = IscInterface() > net = ipsasys.ReadFile("refinery.iif") > bok = net.DoLoadFlow(); > if bok: > busbars = net.GetBusbars() ... > for bus in busbars.itervalues(): > name = bus.GetName() .. > else: > print "Load Flow failed!" > I think Dave's suggestions are useful, a few years ago I was one of the developers for the IPSA python API, I'm not sure how things have moved on since then but you need somewhere to incorporate some time modelling - at the moment I'm not sure how and if that was done. In any case I doubt if the simulation would give rigourous results over a simulation period of that length Is what you really have a set of iif files with current voltages over a set of periods within that year, if so you need to iterate though those, loading each one and doing individual load flows. IPSA has a linkedin discussion group and current IPSA developers will I think respond to you there (if you have an account) Robert -- La grenouille songe..dans son ch?teau d'eau Links and things http://rmstar.blogspot.com/ From steve.ingram at tnei.co.uk Wed May 29 04:51:36 2013 From: steve.ingram at tnei.co.uk (steve.ingram at tnei.co.uk) Date: Wed, 29 May 2013 01:51:36 -0700 (PDT) Subject: Python for IPSA (Power flow analysis) In-Reply-To: <6288a409-db46-4987-91a7-2f3dd65b42dc@googlegroups.com> References: <6288a409-db46-4987-91a7-2f3dd65b42dc@googlegroups.com> Message-ID: <09f09e22-626c-4b49-a80f-8259bdf2adac@googlegroups.com> On Tuesday, May 28, 2013 11:00:42 AM UTC+1, Debbie wrote: > Hi there, > > I am new to Python, and wondering if you could help me with python based coding for the IPSA (Power system analysis software). I have a electrical distribution network with generators, buses and loads, on which I am performing the load flow analysis every 1 hour for a period of 1 year. > > > > The code to perform instantaneous load/power flow analysis is given below. I need to modify it such that I can perform this load flow analysis every 1 hour for a period of 1 year. Please help. > > > > from ipsa import * > > > > ipsasys = IscInterface() > > net = ipsasys.ReadFile("refinery.iif") > > bok = net.DoLoadFlow(); > > if bok: > > busbars = net.GetBusbars() > > print "Load Flow results:" > > print "" > > print "BusName Vmag(kV)" > > print "====================" > > for bus in busbars.itervalues(): > > name = bus.GetName() > > vm = bus.GetVoltageMagnitudekV() > > res = "%-8s %10.5f" % (name, vm) > > print res > > else: > > print "Load Flow failed!" > > > > Regards, > > Debbie Hi Debbie, Just found your question. I work for Ipsa and, among other things, provide support for the scripting side. The quickest way to achieve what you want is to adjust the loads on the network and perform a load flow. The sequence would be roughly as follows; for hours in year(): # set load values loads = net.GetLoads() for load in loads().itervalues: # set new value for each load to represent the next hour of data load.SetDValue(IscLoad.ReactiveMVAr, next_MVAr_value) load.SetDValue(IscLoad.RealMW, next_MW_value) net.DoLoadFlow() # do something with these results The above code obviously won't work and will need tweaking, but should give you the idea. The next version of Ipsa (2.3.2) will have load profiles in so will give a different way of achieving the same results, as well as speed improvements. As Robert mentioned, the best place for Ipsa support is to go onto the website (www.ipsa-power.com) and check the Support of Education pages or check the LinkedIn group Ipsa Power Software Group at http://www.linkedin.com/groups/IPSA-Power-Software-4860907 Hope this helps, Steve From notreal at earthlink.net Tue May 28 11:32:06 2013 From: notreal at earthlink.net (JackM) Date: Tue, 28 May 2013 11:32:06 -0400 Subject: IndentationError: expected an indented block but it's there Message-ID: Having a problem getting a py script to execute. Got this error: File "/scripts/blockIPv4.py", line 19 ip = line.split(';')[0] ^ IndentationError: expected an indented block I'm perplexed because the code that the error refers to *is* indented: with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile: for line in inFile.readlines(): ip = line.split(';')[0] output = os.popen( '/etc/sysconfig/iptables -A INPUT -s ' + ip + ' -j REJECT' ) logFile.write(ip+' - Has been blocked\n') What am I missing here? -- My email address on the header is a non-monitored spam catching account. I can be reached via http://www.wvnh.net/contact.htm From torriem at gmail.com Tue May 28 11:55:52 2013 From: torriem at gmail.com (Michael Torrie) Date: Tue, 28 May 2013 09:55:52 -0600 Subject: IndentationError: expected an indented block but it's there In-Reply-To: References: Message-ID: <51A4D388.8090109@gmail.com> On 05/28/2013 09:32 AM, JackM wrote: > Having a problem getting a py script to execute. Got this error: > > File "/scripts/blockIPv4.py", line 19 > ip = line.split(';')[0] > ^ > IndentationError: expected an indented block > I'm perplexed because the code that the error refers to *is* indented: > with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile: > for line in inFile.readlines(): > ip = line.split(';')[0] > output = os.popen( '/etc/sysconfig/iptables -A INPUT -s ' + ip > + ' -j REJECT' ) > logFile.write(ip+' - Has been blocked\n') > > > What am I missing here? Indentation has to be consistent. likely the for line is indented with spaces and the next line with a tab. You can use tabs and spaces, but you have to be consistent with how you use them. IE if level 1 is indented with spaces, then level 2 has to be indented with spaces up to level 1 as well. Hope that makes sense. From steve+comp.lang.python at pearwood.info Tue May 28 12:01:14 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 May 2013 16:01:14 GMT Subject: IndentationError: expected an indented block but it's there References: Message-ID: <51a4d4ca$0$29966$c3e8da3$5496439d@news.astraweb.com> On Tue, 28 May 2013 11:32:06 -0400, JackM wrote: > Having a problem getting a py script to execute. Got this error: > > File "/scripts/blockIPv4.py", line 19 > ip = line.split(';')[0] > ^ > IndentationError: expected an indented block > > > I'm perplexed because the code that the error refers to *is* indented: Whenever you get perplexing indentation errors, suspect an inconsistency due to mixed tabs and spaces. Tabs good. Spaces good. Tabs and spaces together, bad. You can run the tabnanny module over your source code: python -m tabnanny to convert indentation to all spaces. -- Steven From __peter__ at web.de Tue May 28 12:19:47 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 May 2013 18:19:47 +0200 Subject: IndentationError: expected an indented block but it's there References: Message-ID: JackM wrote: > Having a problem getting a py script to execute. Got this error: > > File "/scripts/blockIPv4.py", line 19 > ip = line.split(';')[0] > ^ > IndentationError: expected an indented block > > > I'm perplexed because the code that the error refers to *is* indented: > > > > with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile: > for line in inFile.readlines(): > ip = line.split(';')[0] > output = os.popen( '/etc/sysconfig/iptables -A INPUT -s ' + ip > + ' -j REJECT' ) > logFile.write(ip+' - Has been blocked\n') > > > What am I missing here? If you are mixing tabs and spaces to indent your code and have your editor configured with a tab width other than eight your code may look correct when it isn't. A simulation in the interactive interpreter: The actual file contents: >>> s = "if 1:\n\tif 2:\n \tprint 'hi'" What you see: >>> print s.expandtabs(4) if 1: if 2: print 'hi' >>> exec s.expandtabs(4) hi What Python "sees": >>> print s.expandtabs(8) if 1: if 2: print 'hi' >>> exec s Traceback (most recent call last): File "", line 1, in File "", line 3 print 'hi' ^ IndentationError: expected an indented block Solution: configure your editor to use four spaces for indentation. From rosuav at gmail.com Tue May 28 12:31:47 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 29 May 2013 02:31:47 +1000 Subject: IndentationError: expected an indented block but it's there In-Reply-To: References: Message-ID: On Wed, May 29, 2013 at 2:19 AM, Peter Otten <__peter__ at web.de> wrote: > Solution: configure your editor to use four spaces for indentation. ITYM eight spaces. But the real solution is to not mix tabs and spaces. Stick to one or the other and you're safe. ChrisA From __peter__ at web.de Tue May 28 12:53:17 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 May 2013 18:53:17 +0200 Subject: IndentationError: expected an indented block but it's there References: Message-ID: Chris Angelico wrote: > On Wed, May 29, 2013 at 2:19 AM, Peter Otten <__peter__ at web.de> wrote: >> Solution: configure your editor to use four spaces for indentation. > > ITYM eight spaces. I meant: one hit of the Tab key should add spaces up to the next multiple of four. Which implies > But the real solution is to not mix tabs and > spaces. Stick to one or the other and you're safe. From rosuav at gmail.com Wed May 29 03:59:30 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 29 May 2013 17:59:30 +1000 Subject: IndentationError: expected an indented block but it's there In-Reply-To: References: Message-ID: On Wed, May 29, 2013 at 2:53 AM, Peter Otten <__peter__ at web.de> wrote: > Chris Angelico wrote: > >> On Wed, May 29, 2013 at 2:19 AM, Peter Otten <__peter__ at web.de> wrote: >>> Solution: configure your editor to use four spaces for indentation. >> >> ITYM eight spaces. > > I meant: one hit of the Tab key should add spaces up to the next multiple of > four. Which implies > >> But the real solution is to not mix tabs and >> spaces. Stick to one or the other and you're safe. Sure. If you configure your tab *key* to not insert a tab *character*, then you're fine. Or alternatively, if you always use \t for indentation, you can tweak the displayed width of it. (Or, as I do, just let it be eight wide. On today's screens that's not much of a problem.) ChrisA From wolfgang.maier at biologie.uni-freiburg.de Tue May 28 13:27:04 2013 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Tue, 28 May 2013 17:27:04 +0000 (UTC) Subject: usage of =?utf-8?b?b3MucG9zaXhfZmFkdmlzZQ==?= Message-ID: Dear all, I was just experimenting for the first time with os.posix_fadvise(), which is new in Python3.3 . I'm reading from a really huge file (several GB) and I want to use the data only once, so I don't want OS-level page caching. I tried os.posix_fadvise with the os.POSIX_FADV_NOREUSE and with the os.POSIX_FADV_DONTNEED flags, but neither seemed to have any effect on the caching behaviour of Ubuntu (still uses all available memory to page cache my I/O). Specifically, I was trying this: import os fd = os.open('myfile', os.O_RDONLY) # wasn't sure about the len parameter in fadvise, # so thought I just use it on the first 4GB os.posix_fadvise(fd, 0, 4000000000, os.POSIX_FADV_NOREUSE) # or DONTNEED then reading from the file with os.read() . By its very nature, I suppose posix_fadvise's effects will be OS-dependent, so my question is whether the lack of effect I'm observing is something Ubuntu-specific, or if I'm just using it in a wrong way. Any help is greatly appreciated! Best, Wolfgang From solipsis at pitrou.net Wed May 29 06:51:58 2013 From: solipsis at pitrou.net (Antoine Pitrou) Date: Wed, 29 May 2013 10:51:58 +0000 (UTC) Subject: usage of =?utf-8?b?b3MucG9zaXhfZmFkdmlzZQ==?= References: Message-ID: Hi, Wolfgang Maier biologie.uni-freiburg.de> writes: > > Dear all, > I was just experimenting for the first time with os.posix_fadvise(), which > is new in Python3.3 . I'm reading from a really huge file (several GB) and I > want to use the data only once, so I don't want OS-level page caching. I > tried os.posix_fadvise with the os.POSIX_FADV_NOREUSE and with the > os.POSIX_FADV_DONTNEED flags, but neither seemed to have any effect on the > caching behaviour of Ubuntu (still uses all available memory to page cache > my I/O). > Specifically, I was trying this: > > import os > fd = os.open('myfile', os.O_RDONLY) > # wasn't sure about the len parameter in fadvise, > # so thought I just use it on the first 4GB > os.posix_fadvise(fd, 0, 4000000000, os.POSIX_FADV_NOREUSE) # or DONTNEED The Linux version of "man posix_fadvise" probably holds the answer: "In kernels before 2.6.18, POSIX_FADV_NOREUSE had the same semantics as POSIX_FADV_WILLNEED. This was probably a bug; since kernel 2.6.18, this flag is a no-op." "POSIX_FADV_DONTNEED attempts to free cached pages associated with the specified region. This is useful, for example, while streaming large files. A program may periodically request the kernel to free cached data that has already been used, so that more useful cached pages are not discarded instead." So, in summary: - POSIX_FADV_NOREUSE doesn't do anything on (modern) Linux kernels - POSIX_FADV_DONTNEED must be called *after* you are done with a range of data, not before you read it (note that I haven't tested to confirm it :-)) Regards Antoine. From michael at stroeder.com Mon May 27 04:10:58 2013 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Mon, 27 May 2013 10:10:58 +0200 Subject: ANN: python-ldap 2.4.11 Message-ID: Find a new release of python-ldap: http://pypi.python.org/pypi/python-ldap/2.4.11 python-ldap provides an object-oriented API to access LDAP directory servers from Python programs. It mainly wraps the OpenLDAP 2.x libs for that purpose. Additionally it contains modules for other LDAP-related stuff (e.g. processing LDIF, LDAP URLs and LDAPv3 schema). Project's web site: http://www.python-ldap.org/ Ciao, Michael. ---------------------------------------------------------------- Released 2.4.11 2013-05-27 Changes since 2.4.10: Lib/ * ldap.controls.DecodeControlTuples() now simply ignores PyAsn1Error exception raised during decoding malformed response control values in case of non-critical controls. * ldif.LDIFWriter.unparse() does not simply skip empty records anymore. From tunacubes at gmail.com Tue May 28 14:25:30 2013 From: tunacubes at gmail.com (Matt Graves) Date: Tue, 28 May 2013 11:25:30 -0700 (PDT) Subject: String object has no attribute "append" Message-ID: <8d8143e8-74f3-43a0-9a52-06b60e857984@googlegroups.com> I receive this error while toying around with Functions... def pulldata(speclist,speccolumn): with open('profiles.csv', 'r') as f: reader = csv.reader(f) for column in reader: (speclist).append(column[('speccolumn')]) pulldata(speclist = 'numbers', speccolumn = "0") >Traceback (most recent call last): > File "C:\Desktop\Python\CFI\Devices V2\users.py", line 17, in > pulldata(speclist = 'numbers', speccolumn = "0") > File "C:\Desktop\Python\CFI\Devices V2\users.py", line 16, in pulldata > (speclist).append(column[('speccolumn')]) >AttributeError: 'str' object has no attribute 'append' I'm getting the error because it should say "numbers.append", but it is reading it as "(speclist).append". This is my first time playing with functions, so be gentle. From marduk at python.net Tue May 28 14:40:05 2013 From: marduk at python.net (marduk at python.net) Date: Tue, 28 May 2013 14:40:05 -0400 Subject: String object has no attribute "append" In-Reply-To: <8d8143e8-74f3-43a0-9a52-06b60e857984@googlegroups.com> References: <8d8143e8-74f3-43a0-9a52-06b60e857984@googlegroups.com> Message-ID: <1369766405.4997.140661236763125.530A41E5@webmail.messagingengine.com> On Tue, May 28, 2013, at 02:25 PM, Matt Graves wrote: > I receive this error while toying around with Functions... > > def pulldata(speclist,speccolumn): > with open('profiles.csv', 'r') as f: > reader = csv.reader(f) > for column in reader: > (speclist).append(column[('speccolumn')]) > > pulldata(speclist = 'numbers', speccolumn = "0") > > > >Traceback (most recent call last): > > File "C:\Desktop\Python\CFI\Devices V2\users.py", line 17, in > > pulldata(speclist = 'numbers', speccolumn = "0") > > File "C:\Desktop\Python\CFI\Devices V2\users.py", line 16, in pulldata > > (speclist).append(column[('speccolumn')]) > >AttributeError: 'str' object has no attribute 'append' > > I'm getting the error because it should say "numbers.append", but it is > reading it as "(speclist).append". Because it indeed says "(speclist).append"... am I missing something? From ian.g.kelly at gmail.com Tue May 28 14:41:37 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 28 May 2013 12:41:37 -0600 Subject: String object has no attribute "append" In-Reply-To: <8d8143e8-74f3-43a0-9a52-06b60e857984@googlegroups.com> References: <8d8143e8-74f3-43a0-9a52-06b60e857984@googlegroups.com> Message-ID: On Tue, May 28, 2013 at 12:25 PM, Matt Graves wrote: > I receive this error while toying around with Functions... > > def pulldata(speclist,speccolumn): > with open('profiles.csv', 'r') as f: > reader = csv.reader(f) > for column in reader: > (speclist).append(column[('speccolumn')]) > > pulldata(speclist = 'numbers', speccolumn = "0") > > >>Traceback (most recent call last): >> File "C:\Desktop\Python\CFI\Devices V2\users.py", line 17, in >> pulldata(speclist = 'numbers', speccolumn = "0") >> File "C:\Desktop\Python\CFI\Devices V2\users.py", line 16, in pulldata >> (speclist).append(column[('speccolumn')]) >>AttributeError: 'str' object has no attribute 'append' > > I'm getting the error because it should say "numbers.append", but it is reading it as "(speclist).append". > > This is my first time playing with functions, so be gentle. It looks like you're trying to pass in a list called 'numbers' into the pulldata function, but that is not what's happening. Because of the single quotes you have around numbers, you are passing in the /string/ 'numbers' and calling it 'speclist' instead. The same goes for speccolumn as well; I think you mean to pass in the integer 0, but you're passing in the string "0" instead. Try it without the quotes: pulldata(speclist = numbers, speccolumn = 0) From ian.g.kelly at gmail.com Tue May 28 14:43:10 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 28 May 2013 12:43:10 -0600 Subject: String object has no attribute "append" In-Reply-To: References: <8d8143e8-74f3-43a0-9a52-06b60e857984@googlegroups.com> Message-ID: On Tue, May 28, 2013 at 12:41 PM, Ian Kelly wrote: > On Tue, May 28, 2013 at 12:25 PM, Matt Graves wrote: >> I receive this error while toying around with Functions... >> >> def pulldata(speclist,speccolumn): >> with open('profiles.csv', 'r') as f: >> reader = csv.reader(f) >> for column in reader: >> (speclist).append(column[('speccolumn')]) >> >> pulldata(speclist = 'numbers', speccolumn = "0") >> >> >>>Traceback (most recent call last): >>> File "C:\Desktop\Python\CFI\Devices V2\users.py", line 17, in >>> pulldata(speclist = 'numbers', speccolumn = "0") >>> File "C:\Desktop\Python\CFI\Devices V2\users.py", line 16, in pulldata >>> (speclist).append(column[('speccolumn')]) >>>AttributeError: 'str' object has no attribute 'append' >> >> I'm getting the error because it should say "numbers.append", but it is reading it as "(speclist).append". >> >> This is my first time playing with functions, so be gentle. > > It looks like you're trying to pass in a list called 'numbers' into > the pulldata function, but that is not what's happening. Because of > the single quotes you have around numbers, you are passing in the > /string/ 'numbers' and calling it 'speclist' instead. The same goes > for speccolumn as well; I think you mean to pass in the integer 0, but > you're passing in the string "0" instead. Try it without the quotes: > > pulldata(speclist = numbers, speccolumn = 0) And along the same lines, the expression column[('speccolumn')] should just be column[speccolumn]. From rosuav at gmail.com Wed May 29 03:54:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 29 May 2013 17:54:38 +1000 Subject: String object has no attribute "append" In-Reply-To: <8d8143e8-74f3-43a0-9a52-06b60e857984@googlegroups.com> References: <8d8143e8-74f3-43a0-9a52-06b60e857984@googlegroups.com> Message-ID: On Wed, May 29, 2013 at 4:25 AM, Matt Graves wrote: > I receive this error while toying around with Functions... > > def pulldata(speclist,speccolumn): > (speclist).append(column[('speccolumn')]) > > pulldata(speclist = 'numbers', speccolumn = "0") > > I'm getting the error because it should say "numbers.append", but it is reading it as "(speclist).append". Others have answered the immediate problem, but you may find this a worthwhile read: http://mail.python.org/pipermail/tutor/2010-December/080505.html It's an excellent explanation of the way Python passes arguments to functions. ChrisA From carlosnepomuceno at outlook.com Tue May 28 15:46:37 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 22:46:37 +0300 Subject: Python #ifdef Message-ID: Are there Python 'preprocessor directives'? I'd like to have something like '#ifdef' to mix code from Python 2 and 3 in a single file. Is that possible? How? From joel.goldstick at gmail.com Tue May 28 15:49:03 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 28 May 2013 15:49:03 -0400 Subject: Python #ifdef In-Reply-To: References: Message-ID: No On May 28, 2013 3:48 PM, "Carlos Nepomuceno" wrote: > Are there Python 'preprocessor directives'? > > I'd like to have something like '#ifdef' to mix code from Python 2 and 3 > in a single file. > > Is that possible? How? > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Tue May 28 15:57:49 2013 From: davea at davea.name (Dave Angel) Date: Tue, 28 May 2013 15:57:49 -0400 Subject: Python #ifdef In-Reply-To: References: Message-ID: <51A50C3D.1090309@davea.name> On 05/28/2013 03:46 PM, Carlos Nepomuceno wrote: > Are there Python 'preprocessor directives'? Python doesn't define a preprocessor, and CPYthon doesn't implement one. Nothing to stop you from doing so, however. > > I'd like to have something like '#ifdef' to mix code from Python 2 and 3 in a single file. > > Is that possible? How? > It's quite possible that you don't need a preprocessor do what you want, since Python does much less compile-time checking than C. Also, it's possible to store function objects and module objects in variables, and thus to hide many things from the body of the code. One biggie is print, since that's a reserved word (and a statement) in Python 2.x. But if you're using 2.7 you can use from __future__ import print, or something like that, and just use 3.x function semantics. -- DaveA From neilc at norwich.edu Tue May 28 16:00:04 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 28 May 2013 20:00:04 GMT Subject: Python #ifdef References: Message-ID: On 2013-05-28, Joel Goldstick wrote: > > No Yes. More below. > On May 28, 2013 3:48 PM, "Carlos Nepomuceno" > wrote: >> Are there Python 'preprocessor directives'? >> >> I'd like to have something like '#ifdef' to mix code from Python 2 and 3 >> in a single file. >> >> Is that possible? How? You need sys.version_info. For more, see http://python3porting.com/noconv.html -- Neil Cerutti From carlosnepomuceno at outlook.com Tue May 28 16:07:39 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 23:07:39 +0300 Subject: Python #ifdef In-Reply-To: References: , , Message-ID: Thank you! I made it run like the following. What do you think about that? IS there a better way? #The following runs on Python 2.7 sc3=''' # Python 3 def original(n): ??? m = 0 ??? for b in n.to_bytes(6, 'big'): ??????? m = 256*m + b ??? return m ''' if sys.version_info[0] == 3: ??? exec(sc3) From benjamin.kaplan at case.edu Tue May 28 16:14:57 2013 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Tue, 28 May 2013 13:14:57 -0700 Subject: Python #ifdef In-Reply-To: References: Message-ID: On May 28, 2013 1:10 PM, "Carlos Nepomuceno" wrote: > > Thank you! I made it run like the following. What do you think about that? IS there a better way? > > > > #The following runs on Python 2.7 > sc3=''' > # Python 3 > def original(n): > m = 0 > for b in n.to_bytes(6, 'big'): > m = 256*m + b > return m > ''' > if sys.version_info[0] == 3: > exec(sc3) > -- No need for exec. if sys.version_info[0] >= 3: def original(n) : ... -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Tue May 28 16:12:59 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 20:12:59 +0000 (UTC) Subject: Python #ifdef References: Message-ID: On 2013-05-28, Carlos Nepomuceno wrote: > Thank you! I made it run like the following. What do you think about > that? IS there a better way? > > #The following runs on Python 2.7 > sc3=''' > # Python 3 > def original(n): > ??? m = 0 > ??? for b in n.to_bytes(6, 'big'): > ??????? m = 256*m + b > ??? return m > ''' > if sys.version_info[0] == 3: > ??? exec(sc3) You're trying to make this a lot harder than it really is: if sys.version_info[0] == 3: def original(n): m = 0 for b in n.to_bytes(6, 'big'): m = 256*m + b return m else: def original(n): -- Grant Edwards grant.b.edwards Yow! Am I having fun yet? at gmail.com From carlosnepomuceno at outlook.com Tue May 28 16:22:40 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 23:22:40 +0300 Subject: Python #ifdef In-Reply-To: References: , , , , Message-ID: ---------------------------------------- > From: invalid at invalid.invalid > Subject: Re: Python #ifdef [...] > You're trying to make this a lot harder than it really is: > > if sys.version_info[0] == 3: > def original(n): > m = 0 > for b in n.to_bytes(6, 'big'): > m = 256*m + b > return m > else: > def original(n): > > > > -- > Grant Edwards grant.b.edwards Yow! Am I having fun yet? > at > gmail.com Haha! That's it!!! Just realized how funny this can be: ;) ### never to be opened ### def pandoras_box(v): ??? return v/0.0 if customer_didnt_pay(): ??? pandoras_box() #lol From fabiosantosart at gmail.com Tue May 28 16:34:36 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 21:34:36 +0100 Subject: Python #ifdef In-Reply-To: References: Message-ID: On 28 May 2013 21:26, "Carlos Nepomuceno" wrote: > Haha! That's it!!! > > Just realized how funny this can be: ;) > > ### never to be opened ### > def pandoras_box(v): > return v/0.0 > > if customer_didnt_pay(): > pandoras_box() > > #lol 1/0 is, after print, my most common debug statement. -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Tue May 28 16:48:32 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 23:48:32 +0300 Subject: Python #ifdef In-Reply-To: References: , , , , , , Message-ID: ________________________________ > Date: Tue, 28 May 2013 21:34:36 +0100 > Subject: RE: Python #ifdef > From: fabiosantosart at gmail.com > To: carlosnepomuceno at outlook.com > CC: python-list at python.org > > > On 28 May 2013 21:26, "Carlos Nepomuceno" > > > wrote: > > Haha! That's it!!! > > > > Just realized how funny this can be: ;) > > > > ### never to be opened ### > > def pandoras_box(v): > > return v/0.0 > > > > if customer_didnt_pay(): > > pandoras_box() > > > > #lol > > 1/0 is, after print, my most common debug statement. What's the best debugger for Python? Have you tried HAP[1]? [1] http://hapdebugger.sourceforge.net/ From fabiosantosart at gmail.com Tue May 28 18:51:08 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 28 May 2013 23:51:08 +0100 Subject: Python #ifdef In-Reply-To: References: Message-ID: On 28 May 2013 21:53, "Carlos Nepomuceno" wrote: > > ________________________________ > > Date: Tue, 28 May 2013 21:34:36 +0100 > > Subject: RE: Python #ifdef > > From: fabiosantosart at gmail.com > > To: carlosnepomuceno at outlook.com > > CC: python-list at python.org > > > > > > On 28 May 2013 21:26, "Carlos Nepomuceno" > > > > > wrote: > > > Haha! That's it!!! > > > > > > Just realized how funny this can be: ;) > > > > > > ### never to be opened ### > > > def pandoras_box(v): > > > return v/0.0 > > > > > > if customer_didnt_pay(): > > > pandoras_box() > > > > > > #lol > > > > 1/0 is, after print, my most common debug statement. > > What's the best debugger for Python? Have you tried HAP[1]? > > [1] http://hapdebugger.sourceforge.net/ Never saw that, but the remote debugging looks like it adds some flexibility. That said, I don't often use a debugger. When I do, it's pdb. Pdb is not bad at all, and it comes in the stdlib, which makes it readily available in a virtualenv. It's impractical to set more than a breakpoint, but then again I only use a breakpoint at a time. -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Tue May 28 16:42:34 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 28 May 2013 20:42:34 +0000 (UTC) Subject: Python #ifdef References: Message-ID: On 2013-05-28, Carlos Nepomuceno wrote: > [...] >> You're trying to make this a lot harder than it really is: >> >> if sys.version_info[0] == 3: >> def original(n): >> m = 0 >> for b in n.to_bytes(6, 'big'): >> m = 256*m + b >> return m >> else: >> def original(n): >> > > Haha! That's it!!! > > Just realized how funny this can be: ;) Here's the important lesson from this thread: Instead of asking "how do I write X in Python" where yoy've assumed X is the solution to your problem, you're usually better off asking how to solve the underlying problem in a Pythonic way. IOW, instead of asking about a Python preprocessor (which you have assumed is the solution to the problem because that's how you would do it in C), ask about the actual problem (how to define a function differently depending on Python version). People on this list are very inventive and will expend a surprising amount of time to figure out often too-clever ways to do X because you asked how to do X -- even if doing X is a lousy way to solve your actual problem... When asking how do I solve a problem, it's OK to illustrate that question with an example X of how you would solve it in C or Java or Ruby or Perl or whatever, but remember 1) Not everybody here knows C or Java or Ruby or Perl or whatever, and the person who _does_ know everyting there is to know about solving some particular underlying problem isn't going to go learn a new language so that they can understand your example and figure out what you're trying to accomplish. 2) Programming languages differ. X may be the best way to solve the problem in one language, but it might be an awful way to do it in another language. -- Grant Edwards grant.b.edwards Yow! I'm ANN LANDERS!! at I can SHOPLIFT!! gmail.com From carlosnepomuceno at outlook.com Tue May 28 16:54:46 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Tue, 28 May 2013 23:54:46 +0300 Subject: Python #ifdef In-Reply-To: References: , , , , , , Message-ID: ---------------------------------------- > From: invalid at invalid.invalid > Subject: Re: Python #ifdef > Date: Tue, 28 May 2013 20:42:34 +0000 > To: python-list at python.org [...] > Here's the important lesson from this thread: > > Instead of asking "how do I write X in Python" where yoy've assumed X > is the solution to your problem, you're usually better off asking how > to solve the underlying problem in a Pythonic way. > > IOW, instead of asking about a Python preprocessor (which you have > assumed is the solution to the problem because that's how you would do > it in C), ask about the actual problem (how to define a function > differently depending on Python version). > > People on this list are very inventive and will expend a surprising > amount of time to figure out often too-clever ways to do X because you > asked how to do X -- even if doing X is a lousy way to solve your > actual problem... > > When asking how do I solve a problem, it's OK to illustrate that > question with an example X of how you would solve it in C or Java or > Ruby or Perl or whatever, but remember > > 1) Not everybody here knows C or Java or Ruby or Perl or whatever, > and the person who _does_ know everyting there is to know about > solving some particular underlying problem isn't going to go > learn a new language so that they can understand your example and > figure out what you're trying to accomplish. > > 2) Programming languages differ. X may be the best way to solve the > problem in one language, but it might be an awful way to do it in > another language. > > -- > Grant Edwards grant.b.edwards Yow! I'm ANN LANDERS!! > at I can SHOPLIFT!! > gmail.com You're right! Sometimes I hate myself for doing exactly the opposite of what I would like to do! Unfortunately I can't change the thread subject. How do you have "invalid at invalid.invalid" instead of your email address? From invalid at invalid.invalid Wed May 29 10:45:37 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 29 May 2013 14:45:37 +0000 (UTC) Subject: Python #ifdef References: Message-ID: On 2013-05-28, Carlos Nepomuceno wrote: > How do you have "invalid at invalid.invalid" instead of your email address? I have this in my .slrnrc: set hostname "invalid.invalid" set username "grant" set realname "Grant Edwards" I'm not sure why it doesn't show up as grant at invalid.invalid -- I think it used to. Since I'm this as a Usenet news group using the slrn newsreader program, there's no need to make my email address visible in the message headers in a usable form. I originally did this long ago (probably 20 years) to avoid spam. I'm pretty sure that it's all in vain these days, and the spam filters at Google are what's keeping me from drowning. -- Grant Edwards grant.b.edwards Yow! Gee, I feel kind of at LIGHT in the head now, gmail.com knowing I can't make my satellite dish PAYMENTS! From breamoreboy at yahoo.co.uk Tue May 28 18:18:34 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 28 May 2013 23:18:34 +0100 Subject: Python #ifdef In-Reply-To: References: Message-ID: On 28/05/2013 20:46, Carlos Nepomuceno wrote: > Are there Python 'preprocessor directives'? > > I'd like to have something like '#ifdef' to mix code from Python 2 and 3 in a single file. > > Is that possible? How? > https://pypi.python.org/pypi/six/1.3.0 -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From joel.goldstick at gmail.com Tue May 28 18:25:59 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 28 May 2013 18:25:59 -0400 Subject: Python #ifdef In-Reply-To: References: Message-ID: On Tue, May 28, 2013 at 6:18 PM, Mark Lawrence wrote: > On 28/05/2013 20:46, Carlos Nepomuceno wrote: > >> Are there Python 'preprocessor directives'? >> >> I'd like to have something like '#ifdef' to mix code from Python 2 and 3 >> in a single file. >> >> Is that possible? How? >> >> > https://pypi.python.org/pypi/**six/1.3.0 > > -- > If you're using GoogleCrap? please read this http://wiki.python.org/moin/* > *GoogleGroupsPython . > > Mark Lawrence > > -- > http://mail.python.org/**mailman/listinfo/python-list > my original response was from cell phone. I just answered that you can't do ifdefs, implying that there is no preprocessor in python. I learned a lot of things I didn't know reading the thread, but I wonder if it is a good idea in general to try to write code like this. -- combined 2.x/3.x codebase can be a bear to maintain. I wouldn't do it unless there was some imposing reason that I must. Its not just print() -- that isn't bad, but changes in module names (urllib), arithmetic, and unicode especially make this idea in general, very tricky. Pity the next developer who needs to try to maintain it. So, maybe you CAN do it, but SHOULD you want to do it? -- Joel Goldstick http://joelgoldstick.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From carlosnepomuceno at outlook.com Tue May 28 18:50:35 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Wed, 29 May 2013 01:50:35 +0300 Subject: Python #ifdef In-Reply-To: References: , , Message-ID: ________________________________ > Date: Tue, 28 May 2013 18:25:59 -0400 > Subject: Re: Python #ifdef > From: joel.goldstick at gmail.com > To: breamoreboy at yahoo.co.uk > CC: python-list at python.org [...] > > my original response was from cell phone. I just answered that you > can't do ifdefs, implying that there is no preprocessor in python. I > learned a lot of things I didn't know reading the thread, but I wonder > if it is a good idea in general to try to write code like this. -- > combined 2.x/3.x codebase can be a bear to maintain. I wouldn't do it > unless there was some imposing reason that I must. Its not just > print() -- that isn't bad, but changes in module names (urllib), > arithmetic, and unicode especially make this idea in general, very > tricky. Pity the next developer who needs to try to maintain it. > > So, maybe you CAN do it, but SHOULD you want to do it? > > -- > Joel Goldstick > http://joelgoldstick.com Thanks Joel! In this case I think it does because I would like to have the same short benchmarking script to be runnable by Python 2 and Python 3. The only piece of code that doesn't run on Python 2 is a to_bytes() single call. So it's not a huge maintenance load. ;) I didn't try to write 'portable' code to Python 3 yet. What's the catch? From tjreedy at udel.edu Tue May 28 20:31:39 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Tue, 28 May 2013 20:31:39 -0400 Subject: Python #ifdef In-Reply-To: References: Message-ID: On 5/28/2013 6:25 PM, Joel Goldstick wrote: > On Tue, May 28, 2013 at 6:18 PM, Mark Lawrence > wrote: > On 28/05/2013 20:46, Carlos Nepomuceno wrote: > I'd like to have something like '#ifdef' to mix code from Python > 2 and 3 in a single file. > https://pypi.python.org/pypi/__six/1.3.0 > > my original response was from cell phone. I just answered that you > can't do ifdefs, implying that there is no preprocessor in python. I > learned a lot of things I didn't know reading the thread, but I wonder > if it is a good idea in general to try to write code like this. -- > combined 2.x/3.x codebase can be a bear to maintain. Many people have come to prefer a) a single 2&3 codebase over b) separate 2 and 3 codebases or c) a single 2 codebase repeatedly converted to a 3 codebase with 2to3. They use 2to3 once (well, a few times) to discover differences that need to be considered. For 2.7 and 3.x, the future imports are enough for some code. The six module handles harder cases. > I wouldn't do it > unless there was some imposing reason that I must. Its not just print() > -- that isn't bad, but changes in module names (urllib), I believe six handles that > arithmetic, and from __future__ import integer_division # spelling? handles the only change > unicode Use unicode consistently and from __future__ import unicode_literals # spelling? or the re-addition u'' prefix do quite well. Otherwise, do not use things that evaporated, like apply() and classic classes. (Inherit from object if nothing else.) This is all hearsay coming from me ;-). Terry From steve+comp.lang.python at pearwood.info Tue May 28 20:57:10 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 29 May 2013 00:57:10 GMT Subject: Supporting both 2.x and 3.x in one code base [was Re: Python #ifdef] References: Message-ID: <51a55265$0$29966$c3e8da3$5496439d@news.astraweb.com> On Tue, 28 May 2013 18:25:59 -0400, Joel Goldstick wrote: > I wonder > if it is a good idea in general to try to write code like this. -- > combined 2.x/3.x codebase can be a bear to maintain. Not so much a bear as a tiny little kitten. > I wouldn't do it > unless there was some imposing reason that I must. Its not just print() > -- that isn't bad, but changes in module names (urllib), arithmetic, and > unicode especially make this idea in general, very tricky. Pity the > next developer who needs to try to maintain it. It's not that hard really. Well, like all things, it depends on the circumstances. If you're reliant on external modules, you *may* have a bad time if those modules are 2.x only or 3.x only, but the standard library and built-ins don't provide too much of a challenge. There's no doubt that supporting 2.x and 3.x in one code base is more difficult that just supporting one or the other, but the difficulty is much less than often supposed. Python 2.7, and to a lesser extent, 2.6, are designed to be as easy to port to 3.x as possible, which has the happy side-effect that they are also relatively easy to write code for them that will also run under 3.x. Many of the differences can be eliminated with a few __future__ imports: from __future__ import division, print_function Differences in behaviour of the built-ins can be eliminated: from future_builtins import * Built-ins such as reduce and cmp that have been moved, or eliminated, can easily be restored: if sys.version >= '3': from functools import reduce Or if you prefer a "Better To Ask Forgiveness Than Permission" approach: try: reduce except NameError: from functools import reduce Name changes of modules are easy to deal with: try: import configparser except ImportError: import ConfigParser as configparser The most difficult difference is the difference between strings in 2.x and 3.x, but if you drop support for Python 3.1 and 3.2, you can write code that works in both 2.7 and 3.3 by using the u"" syntax. Or just use ASCII literals, which work perfectly in both. There are really only a very few things that cannot be shared between 2.x and 3.x: syntactical features that are only supported by 3.x. So if you're planning on writing code that runs in both 2.x and 3.x, you need to eschew the 3-only features like keyword-only function arguments and function annotations. But that's no different than writing code to support *any* two versions that don't have identical syntax. E.g. 2.4 and 2.5: 2.5 supports ternary if, `a if condition else b`, while 2.4 does not, so if you need to support both, you can't use ternary if. Nearly all the code I write is for 2.4 or better, and I can assure you that the hardest part is supporting 2.4. Adding 3.x doesn't make it much harder. (My hat goes off to those supporting 2.3 through 3.3 in one code base. That is, frankly, astonishing.) Nobody *likes* to have to support really old versions missing the cool syntax that you want to use, but nobody says that you should even try. 3.x doesn't change that. -- Steven From dihedral88888 at gmail.com Tue May 28 19:04:25 2013 From: dihedral88888 at gmail.com (88888 Dihedral) Date: Tue, 28 May 2013 16:04:25 -0700 (PDT) Subject: Python #ifdef In-Reply-To: References: Message-ID: Carlos Nepomuceno? 2013?5?29????UTC+8??3?46?37???? > Are there Python 'preprocessor directives'? > > I'd like to have something like '#ifdef' to mix code from Python 2 and 3 in a single file. > > Is that possible? How? Use execfile(filename) at the beginning to get what you want. The .pyc version is preferred. From drsalists at gmail.com Tue May 28 21:23:33 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Tue, 28 May 2013 18:23:33 -0700 Subject: Python #ifdef In-Reply-To: References: Message-ID: Here're slides from a presentation about writing code that runs on 2.x and 3.x: http://stromberg.dnsalias.org/~dstromberg/Intro-to-Python/ And in case you still want a preprocessor for Python (you likely don't need one this time), here's an example of doing this using the venerable m4: https://pypi.python.org/pypi/red-black-tree-mod . Note the many comments added to keep line numbers consistent. Sent from my android phone. On May 28, 2013 12:47 PM, "Carlos Nepomuceno" wrote: > Are there Python 'preprocessor directives'? > > I'd like to have something like '#ifdef' to mix code from Python 2 and 3 > in a single file. > > Is that possible? How? > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Wed May 29 10:55:18 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 29 May 2013 14:55:18 +0000 (UTC) Subject: Python #ifdef References: Message-ID: On 2013-05-29, Dan Stromberg wrote: > And in case you still want a preprocessor for Python (you likely don't need > one this time), here's an example of doing this using the venerable m4: > https://pypi.python.org/pypi/red-black-tree-mod . Note the many comments > added to keep line numbers consistent. I was wondering whether or not to mention m4. Since m4 is (in my mind) inextricably linked to RATFOR and sendmail config files I try to avoid thinking about it lest the flashbacks start again... -- Grant Edwards grant.b.edwards Yow! Hmmm ... a CRIPPLED at ACCOUNTANT with a FALAFEL gmail.com sandwich is HIT by a TROLLEY-CAR ... From rosuav at gmail.com Wed May 29 11:05:35 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 01:05:35 +1000 Subject: Python #ifdef In-Reply-To: References: Message-ID: On Thu, May 30, 2013 at 12:55 AM, Grant Edwards wrote: > On 2013-05-29, Dan Stromberg wrote: > >> And in case you still want a preprocessor for Python (you likely don't need >> one this time), here's an example of doing this using the venerable m4: >> https://pypi.python.org/pypi/red-black-tree-mod . Note the many comments >> added to keep line numbers consistent. > > I was wondering whether or not to mention m4. Since m4 is (in my > mind) inextricably linked to RATFOR and sendmail config files I try > to avoid thinking about it lest the flashbacks start again... It's not a bad tool. I used it as a sort of PHP preprocessor, because requirements at work had me wanting to have a source file defining a PHP class and having an autogenerated section in the middle of that class. PHP's 'include' directive doesn't work for that. Of course, had we been using a better language, that wouldn't have been an issue (and it stopped being an issue when we improved the design and stopped using that class system, too, though I retained the makefile directives about building .php.m4 -> .php files). But still, GNU M4 is a decent piece of technology. ChrisA From invalid at invalid.invalid Wed May 29 11:25:56 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 29 May 2013 15:25:56 +0000 (UTC) Subject: Python #ifdef References: Message-ID: On 2013-05-29, Chris Angelico wrote: > On Thu, May 30, 2013 at 12:55 AM, Grant Edwards wrote: >> On 2013-05-29, Dan Stromberg wrote: >> >>> And in case you still want a preprocessor for Python (you likely don't need >>> one this time), here's an example of doing this using the venerable m4: >>> https://pypi.python.org/pypi/red-black-tree-mod . Note the many comments >>> added to keep line numbers consistent. >> >> I was wondering whether or not to mention m4. Since m4 is (in my >> mind) inextricably linked to RATFOR and sendmail config files I try >> to avoid thinking about it lest the flashbacks start again... > > It's not a bad tool. I used it as a sort of PHP preprocessor, > [...] > But still, GNU M4 is a decent piece of technology. I didn't mean to disparage m4 -- it always seemed well thought out and useful. It just happens to be associated with unpleasant things for me. [And it didn't really seem like a good option for the OP's problem.] Still, if one really did want a preprocessor for Python programs, m4 would be the first thing I'd look at. -- Grant Edwards grant.b.edwards Yow! I wonder if I ought at to tell them about my gmail.com PREVIOUS LIFE as a COMPLETE STRANGER? From fabiofz at gmail.com Tue May 28 16:28:31 2013 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Tue, 28 May 2013 17:28:31 -0300 Subject: PyDev 2.7.5 Released Message-ID: Hi All, PyDev 2.7.5 has been released Details on PyDev: http://pydev.org Details on its development: http://pydev.blogspot.com Release Highlights: ------------------------------- * Icons in the outline are now correct. * Fixed deadlock found on code analysis. * Project-related error markers no longer created in the main thread. * Showing a dialog to select template when a new module is created. * PyUnit view output font uses the same font as the console * New option in auto-formatting to auto-format only workspace files. * Auto-formatting with only deleted lines no longer changes everything. * PyUnit view orientation menu is now properly shown. * Fixed interaction with external files on pydev package explorer. What is PyDev? --------------------------- PyDev is a plugin that enables users to use Eclipse for Python, Jython and IronPython development -- making Eclipse a first class Python IDE -- It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others. Cheers, -- Fabio Zadrozny ------------------------------------------------------ Software Developer PyDev - Python Development Environment for Eclipse http://pydev.org http://pydev.blogspot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From ralf at systemexit.de Tue May 28 19:32:41 2013 From: ralf at systemexit.de (Ralf Schmitt) Date: Wed, 29 May 2013 01:32:41 +0200 Subject: [ANN] pypiserver 1.1.1 - minimal private pypi server Message-ID: <87ip227k9y.fsf@myhost.lan> Hi, I've just uploaded pypiserver 1.1.1 to the python package index. pypiserver is a minimal PyPI compatible server. It can be used to serve a set of packages and eggs to easy_install or pip. pypiserver is easy to install (i.e. just 'pip install pypiserver'). It doesn't have any external dependencies. https://pypi.python.org/pypi/pypiserver/ should contain enough information to easily get you started running your own PyPI server in a few minutes. The code is available on github: https://github.com/schmir/pypiserver Changes in this version ----------------------- - add 'overwrite' option to allow overwriting existing package files (default: false) - show names with hyphens instead of underscores on the "/simple" listing - make the standalone version work with jython 2.5.3 - upgrade waitress to 0.8.5 in the standalone version - workaround broken xmlrpc api on pypi.python.org by using HTTPS -- Cheers Ralf From omrahulrajcse at gmail.com Wed May 29 05:09:05 2013 From: omrahulrajcse at gmail.com (RAHUL RAJ) Date: Wed, 29 May 2013 02:09:05 -0700 (PDT) Subject: MySQL dymanic query in Python Message-ID: <2fd0cae6-6cfe-4163-964e-2aa863e65f5e@googlegroups.com> Can anyone tell me the proper way in which I can execute dynamic MySQL queries in Python? I want to do dynamic queries for both CREATE and INSERT statement. Here is my attempted code: sql="create table %s (%%s, %%s, %%s ... )" % (tablename,''.join(fields)+' '.join(types)) cur.execute(sql) where 'field' is the fieldnames stored in a list and 'types' are the fieldtypes stored in a list. From fabiosantosart at gmail.com Wed May 29 06:02:51 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 29 May 2013 11:02:51 +0100 Subject: MySQL dymanic query in Python In-Reply-To: <2fd0cae6-6cfe-4163-964e-2aa863e65f5e@googlegroups.com> References: <2fd0cae6-6cfe-4163-964e-2aa863e65f5e@googlegroups.com> Message-ID: On 29 May 2013 10:13, "RAHUL RAJ" wrote: > > Can anyone tell me the proper way in which I can execute dynamic MySQL queries in Python? > > I want to do dynamic queries for both CREATE and INSERT statement. > > Here is my attempted code: > > > sql="create table %s (%%s, %%s, %%s ... )" % (tablename,''.join(fields)+' '.join(types)) > cur.execute(sql) > > > where 'field' is the fieldnames stored in a list and 'types' are the fieldtypes stored in a list. You need to join the fields and the field types. Use zip(). Then join with commas. fields_and_types = ['%s %s' % (field, type) for field, type in zip(fields, types)] what_goes_between_the_parens = ', '.join(fields_and_types) sql = 'create table %s (%s)' % (tablename, what_goes_between_the_parens) See where that gets you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From omrahulrajcse at gmail.com Thu May 30 08:22:52 2013 From: omrahulrajcse at gmail.com (RAHUL RAJ) Date: Thu, 30 May 2013 05:22:52 -0700 (PDT) Subject: MySQL dymanic query in Python In-Reply-To: References: <2fd0cae6-6cfe-4163-964e-2aa863e65f5e@googlegroups.com> Message-ID: <9f07fb7a-4d37-47cb-be22-67245dd73d1f@googlegroups.com> On Wednesday, May 29, 2013 3:32:51 PM UTC+5:30, F?bio Santos wrote: > On 29 May 2013 10:13, "RAHUL RAJ" wrote: > > > > > > Can anyone tell me the proper way in which I can execute dynamic MySQL queries in Python? > > > > > > I want to do dynamic queries for both CREATE and INSERT statement. > > > > > > Here is my attempted code: > > > > > > > > > sql="create table %s (%%s, %%s, %%s ... )" % (tablename,''.join(fields)+' '.join(types)) > > > cur.execute(sql) > > > > > > > > > where 'field' is the fieldnames stored in a list and 'types' are the fieldtypes stored in a list. > > You need to join the fields and the field types. Use zip(). > > Then join with commas. > > fields_and_types = ['%s %s' % (field, type) for field, type in zip(fields, types)] > > what_goes_between_the_parens = ', '.join(fields_and_types) > > sql = 'create table %s (%s)' % (tablename, what_goes_between_the_parens) > > See where that gets you. Fantastic! It worked, Thanks :) From jabba.laci at gmail.com Wed May 29 07:16:59 2013 From: jabba.laci at gmail.com (Jabba Laci) Date: Wed, 29 May 2013 13:16:59 +0200 Subject: detect key conflict in a JSON file Message-ID: Hi, How can you detect if a key is duplicated in a JSON file? Example: { "something": [...], ... "something": [...] } I have a growing JSON file that I edit manually and it might happen that I repeat a key. If this happens, I would like to get notified. Currently the value of the second key silently overwrites the value of the first. Do you know about a command line JSON validator? Thanks, Laszlo From roy at panix.com Wed May 29 09:25:39 2013 From: roy at panix.com (Roy Smith) Date: Wed, 29 May 2013 09:25:39 -0400 Subject: detect key conflict in a JSON file References: Message-ID: In article , Jabba Laci wrote: > I have a growing JSON file that I edit manually and it might happen > that I repeat a key. If this happens, I would like to get notified. The real answer here is that JSON is probably not the best choice for large files that get hand-edited. For data that you intend to hand-edit a lot, YAML might be a better choice. > Currently the value of the second key silently overwrites the value of > the first. Yeah, that's what I would expect. http://www.json.org/ is mute on the question of what to do with duplicate keys, but I would be quite surprised to discover any implementation which behaved differently. > Do you know about a command line JSON validator? All JSON implementations validate their input. You are assuming that having duplicate keys is "invalid". I would think it falls more into the category of "undefined behavior". From jabba.laci at gmail.com Wed May 29 09:41:50 2013 From: jabba.laci at gmail.com (Jabba Laci) Date: Wed, 29 May 2013 15:41:50 +0200 Subject: detect key conflict in a JSON file In-Reply-To: References: Message-ID: > The real answer here is that JSON is probably not the best choice for > large files that get hand-edited. For data that you intend to hand-edit > a lot, YAML might be a better choice. > >> Currently the value of the second key silently overwrites the value of >> the first. Thanks but how would it be different with YAML? Can YAML check duplicate keys? How to process (read) YAML files in Python? Can you give me some hints how to get started? All I need is read it in and create a dictionary of it. Thanks, Laszlo From airween at gmail.com Wed May 29 10:04:00 2013 From: airween at gmail.com (Ervin =?utf-8?Q?Heged=C3=BCs?=) Date: Wed, 29 May 2013 16:04:00 +0200 Subject: detect key conflict in a JSON file In-Reply-To: References: Message-ID: <20130529140359.GA6033@arxnet.hu> Hello, On Wed, May 29, 2013 at 03:41:50PM +0200, Jabba Laci wrote: > > The real answer here is that JSON is probably not the best choice for > > large files that get hand-edited. For data that you intend to hand-edit > > a lot, YAML might be a better choice. > > > >> Currently the value of the second key silently overwrites the value of > >> the first. > > Thanks but how would it be different with YAML? Can YAML check duplicate keys? no, I think it can't, I'm using yaml a few months ago, as I noticed, the last key prevail. > How to process (read) YAML files in Python? Can you give me some hints > how to get started? All I need is read it in and create a dictionary > of it. import yaml struct = yaml.load(file("yamlfile.yml" 'r')) and struct will be a Python dictionary, As I know, yaml modul is not part of standard Python library, but most Linux systems has package, eg. Debian/Ubuntu has a "python-yaml". Cheers: a. From roy at panix.com Wed May 29 11:20:59 2013 From: roy at panix.com (Roy Smith) Date: Wed, 29 May 2013 11:20:59 -0400 Subject: detect key conflict in a JSON file In-Reply-To: References: Message-ID: On May 29, 2013, at 9:41 AM, Jabba Laci wrote: >> The real answer here is that JSON is probably not the best choice for >> large files that get hand-edited. For data that you intend to hand-edit >> a lot, YAML might be a better choice. >> >>> Currently the value of the second key silently overwrites the value of >>> the first. > > Thanks but how would it be different with YAML? Can YAML check duplicate keys? Oh, I didn't mean to imply that. I was just pointing out that in general, YAML is more friendly for hand-editing than JSON. They both have the same GIGO issue. > How to process (read) YAML files in Python? Take a look at http://pyyaml.org/ --- Roy Smith roy at panix.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed May 29 23:10:03 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 03:10:03 GMT Subject: detect key conflict in a JSON file References: Message-ID: <51a6c30b$0$29966$c3e8da3$5496439d@news.astraweb.com> On Wed, 29 May 2013 11:20:59 -0400, Roy Smith wrote: >> How to process (read) YAML files in Python? > > Take a look at http://pyyaml.org/ Beware that pyaml suffers from the same issue as pickle, namely that it can execute arbitrary code when reading untrusted data. -- Steven From rustompmody at gmail.com Wed May 29 10:36:02 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 29 May 2013 07:36:02 -0700 (PDT) Subject: detect key conflict in a JSON file References: Message-ID: <3ec122c1-2968-4fe4-859c-2f7a5a089958@vy4g2000pbc.googlegroups.com> On May 29, 6:41?pm, Jabba Laci wrote: > > The real answer here is that JSON is probably not the best choice for > > large files that get hand-edited. ?For data that you intend to hand-edit > > a lot, YAML might be a better choice. > > >> Currently the value of the second key silently overwrites the value of > >> the first. > > Thanks but how would it be different with YAML? Can YAML check duplicate keys? > > How to process (read) YAML files in Python? Can you give me some hints > how to get started? All I need is read it in and create a dictionary > of it. > > Thanks, > > Laszlo There seems to be a suggested patch for duplicate-detection here http://pyyaml.org/ticket/128 From clp2 at rebertia.com Wed May 29 13:10:26 2013 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 May 2013 10:10:26 -0700 Subject: detect key conflict in a JSON file In-Reply-To: References: Message-ID: On Wed, May 29, 2013 at 4:16 AM, Jabba Laci wrote: > Hi, > > How can you detect if a key is duplicated in a JSON file? Example: > > { > "something": [...], > ... > "something": [...] > } > > I have a growing JSON file that I edit manually and it might happen > that I repeat a key. If this happens, I would like to get notified. > Currently the value of the second key silently overwrites the value of > the first. You can pass an appropriate object_pairs_hook function to json.load(): http://docs.python.org/2/library/json.html#repeated-names-within-an-object http://docs.python.org/2/library/json.html#json.load Cheers, Chris From malaclypse2 at gmail.com Wed May 29 23:43:18 2013 From: malaclypse2 at gmail.com (Jerry Hill) Date: Wed, 29 May 2013 23:43:18 -0400 Subject: detect key conflict in a JSON file In-Reply-To: References: Message-ID: On Wed, May 29, 2013 at 1:10 PM, Chris Rebert wrote: > On Wed, May 29, 2013 at 4:16 AM, Jabba Laci wrote: > > I have a growing JSON file that I edit manually and it might happen > > that I repeat a key. If this happens, I would like to get notified. > > Currently the value of the second key silently overwrites the value of > > the first. > > You can pass an appropriate object_pairs_hook function to json.load(): > http://docs.python.org/2/library/json.html#repeated-names-within-an-object > http://docs.python.org/2/library/json.html#json.load > ?That makes it pretty easy to provide any validation you might like to your JSON data. Here's a quick example that raises ValueError on duplicate keys, since the docs didn't have any examples. Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 ?>>> s = '{"x": 1, "x": 2, "x": 3}' >>> def json_checker(seq): d = {} for key, value in seq: if key in d: raise ValueError("Duplicate key %r in json document" % key) else: d[key]=value return d >>> json.loads(s, object_pairs_hook=json_checker) Traceback (most recent call last): File "", line 1, in json.loads(s, object_pairs_hook=checker) File "C:\Python32\lib\json\__init__.py", line 320, in loads return cls(**kw).decode(s) File "C:\Python32\lib\json\decoder.py", line 351, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python32\lib\json\decoder.py", line 367, in raw_decode obj, end = self.scan_once(s, idx) File "", line 5, in json_checker raise ValueError("Duplicate key %r in json document" % key) ValueError: Duplicate key 'x' in json document -- ? Jerry? -------------- next part -------------- An HTML attachment was scrubbed... URL: From avnesh.nitk at gmail.com Wed May 29 07:14:57 2013 From: avnesh.nitk at gmail.com (Avnesh Shakya) Date: Wed, 29 May 2013 04:14:57 -0700 (PDT) Subject: Output is not coming with defined color Message-ID: <0965b949-8bf4-4105-aa8a-17cfd0d22aa4@googlegroups.com> hi, I am trying to display my output with different colour on terminal, but it's coming with that colour code. Please help me how is it possible? my code is - from fabric.colors import green, red, blue def colorr(): a = red('This is red') b = green('This is green') c = blue('This is blue') d = {a, b, c} print d colorr() output - set(['\x1b[32mThis is green\x1b[0m', '\x1b[34mThis is blue\x1b[0m', '\x1b[31mThis is red\x1b[0m']) Thanks From fabiosantosart at gmail.com Wed May 29 08:11:17 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 29 May 2013 13:11:17 +0100 Subject: Output is not coming with defined color In-Reply-To: <0965b949-8bf4-4105-aa8a-17cfd0d22aa4@googlegroups.com> References: <0965b949-8bf4-4105-aa8a-17cfd0d22aa4@googlegroups.com> Message-ID: On 29 May 2013 12:25, "Avnesh Shakya" wrote: > > hi, > I am trying to display my output with different colour on terminal, but it's > coming with that colour code. > Please help me how is it possible? > > my code is - > from fabric.colors import green, red, blue > def colorr(): > a = red('This is red') > b = green('This is green') > c = blue('This is blue') > d = {a, b, c} > print d > colorr() > > output - > set(['\x1b[32mThis is green\x1b[0m', '\x1b[34mThis is blue\x1b[0m', '\x1b[31mThis is red\x1b[0m']) > > Thanks You are printing the {a, b, c} set. That ends up printing the repr of all of its contents. The repr breaks the desired output. Try to just print a, b, c -------------- next part -------------- An HTML attachment was scrubbed... URL: From rustompmody at gmail.com Wed May 29 10:09:44 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 29 May 2013 07:09:44 -0700 (PDT) Subject: Output is not coming with defined color References: <0965b949-8bf4-4105-aa8a-17cfd0d22aa4@googlegroups.com> Message-ID: On May 29, 5:11?pm, F?bio Santos wrote: > On 29 May 2013 12:25, "Avnesh Shakya" wrote: > > > > > > > > > > > hi, > > ? ?I am trying to display my output with different colour on terminal, > but it's > > coming with that colour code. > > Please help me how is it possible? > > > my code is - > > from fabric.colors import green, red, blue > > def colorr(): > > ? ? a = red('This is red') > > ? ? b = green('This is green') > > ? ? c = blue('This is blue') > > ? ? d = {a, b, c} > > ? ? print d > > colorr() > > > output - > > set(['\x1b[32mThis is green\x1b[0m', '\x1b[34mThis is blue\x1b[0m', > > '\x1b[31mThis is red\x1b[0m']) > > > > > Thanks > > You are printing the {a, b, c} set. That ends up printing the repr of all > of its contents. The repr breaks the desired output. Try to just > > print a, b, c Or for x in d: print x should work From joshua.landau.ws at gmail.com Wed May 29 07:48:39 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 29 May 2013 12:48:39 +0100 Subject: Fatal Python error Message-ID: Hello all, again. Instead of revising like I'm meant to be, I've been delving into a bit of Python and I've come up with this code: class ClassWithProperty: @property def property(self): pass thingwithproperty = ClassWithProperty() def loop(): try: thingwithproperty.property except: pass loop() try: loop() except RuntimeError: pass As you will expect, this does nothing... on Python2.7 and PyPy. Python3.3 prefers to spit out a "Fatal Python error: Cannot recover from stack overflow.", which seems a bit unexpected. Wuzzup with that? -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Wed May 29 08:25:13 2013 From: davea at davea.name (Dave Angel) Date: Wed, 29 May 2013 08:25:13 -0400 Subject: Fatal Python error In-Reply-To: References: Message-ID: <51A5F3A9.3050709@davea.name> On 05/29/2013 07:48 AM, Joshua Landau wrote: > Hello all, again. Instead of revising like I'm meant to be, I've been > delving into a bit of Python and I've come up with this code: > To start with, please post in text mode. By using html, you've completely messed up any indentation you presumably had. > class ClassWithProperty: > @property > def property(self): > pass Did you really mean to hide the built-in property? I don't know if this does that, but it's certainly confusing. And perhaps that's a difference between 2.x and 3.x > > thingwithproperty = ClassWithProperty() > > def loop(): > try: > thingwithproperty.property > except: > pass > > loop() > > try: > loop() > except RuntimeError: > pass > > As you will expect, this does nothing... on Python2.7 and PyPy. Python3.3 > prefers to spit out a "Fatal Python error: Cannot recover from stack > overflow.", which seems a bit unexpected. > A stack overflow means you have infinite recursion. Try fixing the property name above, and see if that makes a difference. -- DaveA From joshua.landau.ws at gmail.com Wed May 29 08:43:48 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 29 May 2013 13:43:48 +0100 Subject: Fatal Python error In-Reply-To: <51A5F3A9.3050709@davea.name> References: <51A5F3A9.3050709@davea.name> Message-ID: On 29 May 2013 13:25, Dave Angel wrote: > On 05/29/2013 07:48 AM, Joshua Landau wrote: > >> Hello all, again. Instead of revising like I'm meant to be, I've been >> delving into a bit of Python and I've come up with this code: >> >> > To start with, please post in text mode. By using html, you've completely > messed up any indentation you presumably had. Appologies, I use GMail and I don't know how to force text-only > class ClassWithProperty: >> @property >> def property(self): >> pass >> > > Did you really mean to hide the built-in property? I don't know if this > does that, but it's certainly confusing. And perhaps that's a difference > between 2.x and 3.x I'm not. That goes to self.property, whilst the normal function isn't. I guess it does locally hide it, but who really cares? :P You can rename it if you want. Anything will do. Obviously this is a minimal example code, and not the real thing. As you will expect, this does nothing... on Python2.7 and PyPy. Python3.3 >> prefers to spit out a "Fatal Python error: Cannot recover from stack >> overflow.", which seems a bit unexpected. >> >> > A stack overflow means you have infinite recursion. I realise, but I was hoping to catch that with the "except RuntimeError". > Try fixing the property name above, and see if that makes a difference. It does not make a difference. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rustompmody at gmail.com Wed May 29 10:47:15 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 29 May 2013 07:47:15 -0700 (PDT) Subject: Fatal Python error References: <51A5F3A9.3050709@davea.name> Message-ID: On May 29, 5:43?pm, Joshua Landau wrote: > On 29 May 2013 13:25, Dave Angel wrote: > > > On 05/29/2013 07:48 AM, Joshua Landau wrote: > > >> Hello all, again. Instead of revising like I'm meant to be, I've been > >> delving into a bit of Python and I've come up with this code: > > > To start with, please post in text mode. ?By using html, you've completely > > messed up any indentation you presumably had. > > Appologies, I use GMail and I don't know how to force text-only In gmail New interface: There should be a down arrow next to the trash-can right bottom of posting-screen Click the down-arrow. YOu should see a 'choose text-only' option Old interface: When posting a new message, right at the top next to all the formatting icons is a text-only link From marcelgmr at gmail.com Wed May 29 08:30:58 2013 From: marcelgmr at gmail.com (Marcel Rodrigues) Date: Wed, 29 May 2013 09:30:58 -0300 Subject: Fatal Python error In-Reply-To: References: Message-ID: I just tried your code with similar results: it does nothing on PyPy 2.0.0-beta2 and Python 2.7.4. But on Python 3.3.1 it caused core dump. It's a little weird but so is the code. You have defined a function that calls itself unconditionally. This will cause a stack overflow, which is a RuntimeError. Since you are handling this very exception with a pass statement, we would expect that no error occurs. But the fatal error message seems pretty informative at this point: "Cannot recover from stack overflow.". One thing to note is that while it's reasonable to handle exceptions that happens at the level of your Python code, like a ValueError, it's not so reasonable to try to handle something that may disturb the interpreter itself in a lower level, like a stack overflow (I think that the stack used by your code is the same stack used by the interpreter code, but I'm not sure). 2013/5/29 Joshua Landau > Hello all, again. Instead of revising like I'm meant to be, I've been > delving into a bit of Python and I've come up with this code: > > class ClassWithProperty: > @property > def property(self): > pass > > thingwithproperty = ClassWithProperty() > > def loop(): > try: > thingwithproperty.property > except: > pass > > loop() > > try: > loop() > except RuntimeError: > pass > > As you will expect, this does nothing... on Python2.7 and PyPy. Python3.3 > prefers to spit out a "Fatal Python error: Cannot recover from stack > overflow.", which seems a bit unexpected. > > Wuzzup with that? > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Wed May 29 09:02:54 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 29 May 2013 14:02:54 +0100 Subject: Fatal Python error In-Reply-To: References: Message-ID: On 29 May 2013 13:30, Marcel Rodrigues wrote: > > I just tried your code with similar results: it does nothing on PyPy 2.0.0-beta2 and Python 2.7.4. But on Python 3.3.1 it caused core dump. > It's a little weird but so is the code. You have defined a function that calls itself unconditionally. This will cause a stack overflow, which is a RuntimeError. The weirdness of the code is simply as I've taken all the logic and conditionality away, since it was irrelevant. Why, though, does removing any one element make it fail properly? That's what's confusing, largely. > > Since you are handling this very exception with a pass statement, we would expect that no error occurs. But the fatal error message seems pretty informative at this point: "Cannot recover from stack overflow.". > > One thing to note is that while it's reasonable to handle exceptions that happens at the level of your Python code, like a ValueError, it's not so reasonable to try to handle something that may disturb the interpreter itself in a lower level, like a stack overflow (I think that the stack used by your code is the same stack used by the interpreter code, but I'm not sure). What is the expected response here then? Should I ever feel justified in catching a Stack Overflow error? This code was extracted from a file manager after much difficulty, but it should have been "caught" by a global try...except and not crashed the whole program immediately. I'd imagine that's a good enough reason to bring this up. Also; This works for the code: def loop(): thingwithproperty.prop loop() This does not: def loop(): try: thingwithproperty.prop except: pass loop() thingwithproperty.prop NEVER creates an error. (.prop is the new .property) -------------- next part -------------- An HTML attachment was scrubbed... URL: From marcelgmr at gmail.com Wed May 29 09:22:23 2013 From: marcelgmr at gmail.com (Marcel Rodrigues) Date: Wed, 29 May 2013 10:22:23 -0300 Subject: Fatal Python error In-Reply-To: References: Message-ID: I think the issue here has little to do with classes/objects/properties. See, for example, the code posted by Oscar Benjamin. What that code is trying to do is similar to responding to an "Out Of Memory" error with something that might require more memory allocation. Even if we consider the Py3 behavior here a bug, that code is unreliable by design. It's an infinite loop at the best. 2013/5/29 Joshua Landau > On 29 May 2013 13:30, Marcel Rodrigues wrote: > > > > I just tried your code with similar results: it does nothing on PyPy > 2.0.0-beta2 and Python 2.7.4. But on Python 3.3.1 it caused core dump. > > It's a little weird but so is the code. You have defined a function that > calls itself unconditionally. This will cause a stack overflow, which is a > RuntimeError. > > > The weirdness of the code is simply as I've taken all the logic and > conditionality away, since it was irrelevant. Why, though, does removing > any one element make it fail properly? That's what's confusing, largely. > > > > > > Since you are handling this very exception with a pass statement, we > would expect that no error occurs. But the fatal error message seems pretty > informative at this point: "Cannot recover from stack overflow.". > > > > One thing to note is that while it's reasonable to handle exceptions > that happens at the level of your Python code, like a ValueError, it's not > so reasonable to try to handle something that may disturb the interpreter > itself in a lower level, like a stack overflow (I think that the stack used > by your code is the same stack used by the interpreter code, but I'm not > sure). > > > What is the expected response here then? Should I ever feel justified in > catching a Stack Overflow error? This code was extracted from a file > manager after much difficulty, but it should have been "caught" by a global > try...except and not crashed the whole program immediately. I'd imagine > that's a good enough reason to bring this up. > > > > Also; > This works for the code: > > def loop(): > thingwithproperty.prop > loop() > > This does not: > > def loop(): > try: > thingwithproperty.prop > except: > pass > loop() > > thingwithproperty.prop NEVER creates an error. > (.prop is the new .property) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Wed May 29 08:45:14 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 29 May 2013 13:45:14 +0100 Subject: Fatal Python error In-Reply-To: References: Message-ID: On 29 May 2013 12:48, Joshua Landau wrote: > Hello all, again. Instead of revising like I'm meant to be, I've been > delving into a bit of Python and I've come up with this code: Here's a simpler example that gives similar results: $ py -3.3 Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> def broken(): ... try: ... broken() ... except RuntimeError: ... broken() ... >>> broken() Fatal Python error: Cannot recover from stack overflow. Current thread 0x0000058c: File "", line 3 in broken File "", line 3 in broken ... Under Python 2.7.5 it just goes into an infinite loop. Under Python 3.2.5 and 3.3.2 it crashes the interpreter as shown above. What the broken() function is doing is totally stupid: responding to a recursion error with more recursion. However this may indicate or be considered a bug in the 3.x interpreter. Oscar From davea at davea.name Wed May 29 09:02:53 2013 From: davea at davea.name (Dave Angel) Date: Wed, 29 May 2013 09:02:53 -0400 Subject: Fatal Python error In-Reply-To: References: Message-ID: <51A5FC7D.8080202@davea.name> On 05/29/2013 08:45 AM, Oscar Benjamin wrote: > On 29 May 2013 12:48, Joshua Landau wrote: >> Hello all, again. Instead of revising like I'm meant to be, I've been >> delving into a bit of Python and I've come up with this code: > > Here's a simpler example that gives similar results: > > $ py -3.3 > Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 > 32 bit (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> def broken(): > ... try: > ... broken() > ... except RuntimeError: > ... broken() > ... >>>> broken() > Fatal Python error: Cannot recover from stack overflow. > > Current thread 0x0000058c: > File "", line 3 in broken > File "", line 3 in broken > ... > > Under Python 2.7.5 it just goes into an infinite loop. Under Python > 3.2.5 and 3.3.2 it crashes the interpreter as shown above. > > What the broken() function is doing is totally stupid: responding to a > recursion error with more recursion. However this may indicate or be > considered a bug in the 3.x interpreter. > > > Oscar > More likely a bug in the 2.x interpreter. Once inside an exception handler, that frame must be held somehow. If not on the stack, then in some separate list. So the logic will presumably fill memory, it just may take longer on 2.x . Joshua: Avoid doing anything complex inside an exception handler. If nothing else, the exception frame is huge. I probably would have spotted it except for the indentation problem triggered by html. The top level code following your function didn't have any loops, so it wasn't a problem. Can anyone help Joshua put his gmail into text mode? -- DaveA From joshua.landau.ws at gmail.com Wed May 29 10:05:52 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 29 May 2013 15:05:52 +0100 Subject: Fatal Python error In-Reply-To: <51A5FC7D.8080202@davea.name> References: <51A5FC7D.8080202@davea.name> Message-ID: On 29 May 2013 14:02, Dave Angel wrote: > On 05/29/2013 08:45 AM, Oscar Benjamin wrote: > Joshua: Avoid doing anything complex inside an exception handler. Unfortunately, Ranger (the file manager in question) wraps a lot of stuff in one big exception handler. Hence there isn't much choice. The original wasn't actually in an infinite recursion, too, but just a recursion over a large directory. Is there a reason that Python 3 can't be made to work like Python 2 and PyPy, and -if not- should it? The catchable fail would be much nicer than just bombing the program. In the meantime the algorithm should just be reworked, but it seems like a larger step than should be needed. If nothing else, the exception frame is huge. I probably would have > spotted it except for the indentation problem triggered by html. The top > level code following your function didn't have any loops, so it wasn't a > problem. > > Can anyone help Joshua put his gmail into text mode? I've found a new option. As a test, here's a simplified version without the property: def loop(): try: (lambda: None)() except: pass loop() try: loop() except RuntimeError: pass which is pretty much Oscar Benjamin's, but less stupid. -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Wed May 29 11:17:58 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 29 May 2013 16:17:58 +0100 Subject: Fatal Python error In-Reply-To: <51A5FC7D.8080202@davea.name> References: <51A5FC7D.8080202@davea.name> Message-ID: On 29 May 2013 14:02, Dave Angel wrote: > On 05/29/2013 08:45 AM, Oscar Benjamin wrote: > > More likely a bug in the 2.x interpreter. Once inside an exception handler, > that frame must be held somehow. If not on the stack, then in some separate > list. So the logic will presumably fill memory, it just may take longer on > 2.x . I'm not so sure. The following gives the same behaviour in 2.7, 3.2 and 3.3: $ cat tmp.py def loop(): loop() loop() $ py -3.2 tmp.py Traceback (most recent call last): File "tmp.py", line 4, in loop() File "tmp.py", line 2, in loop loop() File "tmp.py", line 2, in loop loop() File "tmp.py", line 2, in loop loop() File "tmp.py", line 2, in loop ... However the following leads to a RuntimeError in 2.7 but different stack overflow errors in 3.2 and 3.3: $ cat tmp.py def loop(): try: (lambda: None)() except RuntimeError: pass loop() loop() $ py -2.7 tmp.py Traceback (most recent call last): File "tmp.py", line 8, in loop() File "tmp.py", line 6, in loop loop() File "tmp.py", line 6, in loop loop() File "tmp.py", line 6, in loop loop() File "tmp.py", line 6, in loop ... RuntimeError: maximum recursion depth exceeded $ py -3.2 tmp.py Fatal Python error: Cannot recover from stack overflow. This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. $ py -3.3 tmp.py Fatal Python error: Cannot recover from stack overflow. Current thread 0x000005c4: File "tmp.py", line 3 in loop File "tmp.py", line 6 in loop File "tmp.py", line 6 in loop File "tmp.py", line 6 in loop File "tmp.py", line 6 in loop File "tmp.py", line 6 in loop File "tmp.py", line 6 in loop ... I would expect this to give "RuntimeError: maximum recursion depth exceeded" in all three cases. Oscar From pruebauno at latinmail.com Fri May 31 10:31:44 2013 From: pruebauno at latinmail.com (nn) Date: Fri, 31 May 2013 07:31:44 -0700 (PDT) Subject: Fatal Python error References: <51A5FC7D.8080202@davea.name> Message-ID: <25cc650c-9047-4533-bf8d-cf5b8dcf6a05@v2g2000yqa.googlegroups.com> On May 29, 10:05?am, Joshua Landau wrote: > On 29 May 2013 14:02, Dave Angel wrote: > > > On 05/29/2013 08:45 AM, Oscar Benjamin wrote: > > Joshua: ?Avoid doing anything complex inside an exception handler. > > Unfortunately, Ranger (the file manager in question) wraps a lot of stuff > in one big exception handler. Hence there isn't much choice. The original > wasn't actually in an infinite recursion, too, but just a recursion over a > large directory. > > Is there a reason that Python 3 can't be made to work like Python 2 and > PyPy, and -if not- should it? The catchable fail would be much nicer than > just bombing the program. > > In the meantime the algorithm should just be reworked, but it seems like a > larger step than should be needed. > > If nothing else, the exception frame is huge. ?I probably would have > > > spotted it except for the indentation problem triggered by html. ?The top > > level code following your function didn't have any loops, so it wasn't a > > problem. > > > Can anyone help Joshua put his gmail into text mode? > > I've found a new option. As a test, here's a simplified version without the > property: > > def loop(): > ? ? try: > ? ? ? ? (lambda: None)() > ? ? except: > ? ? ? ? pass > > ? ? loop() > > try: > ? ? loop() > except RuntimeError: > ? ? pass > > which is pretty much Oscar Benjamin's, but less stupid. If nobody else has, I would recommend you submit a bug at bugs.python.org. From croepha at gmail.com Wed May 29 13:46:19 2013 From: croepha at gmail.com (Croepha) Date: Wed, 29 May 2013 12:46:19 -0500 Subject: Getting a callable for any value? Message-ID: Is there anything like this in the standard library? class AnyFactory(object): def __init__(self, anything): self.product = anything def __call__(self): return self.product def __repr__(self): return "%s.%s(%r)" % (self.__class__.__module__, self.__class__.__name__, self.product) my use case is: collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None)))) And I think lambda expressions are not preferable... I found itertools.repeat(anything).next and functools.partial(copy.copy, anything) but both of those don't repr well... and are confusing... I think AnyFactory is the most readable, but is confusing if the reader doesn't know what it is, am I missing a standard implementation of this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrea.crotti.0 at gmail.com Wed May 29 13:55:51 2013 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Wed, 29 May 2013 18:55:51 +0100 Subject: Getting a callable for any value? In-Reply-To: References: Message-ID: <51A64127.2020600@gmail.com> On 05/29/2013 06:46 PM, Croepha wrote: > Is there anything like this in the standard library? > > class AnyFactory(object): > def __init__(self, anything): > self.product = anything > def __call__(self): > return self.product > def __repr__(self): > return "%s.%s(%r)" % (self.__class__.__module__, > self.__class__.__name__, self.product) > > my use case is: > collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None)))) > I think I would scratch my head for a good half an hour if I see a string like this, so I hope there isn't anything in the standard library to do that :D -------------- next part -------------- An HTML attachment was scrubbed... URL: From ned at nedbatchelder.com Wed May 29 14:10:58 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Wed, 29 May 2013 14:10:58 -0400 Subject: Getting a callable for any value? In-Reply-To: References: Message-ID: <51A644B2.6090800@nedbatchelder.com> On 5/29/2013 1:46 PM, Croepha wrote: > Is there anything like this in the standard library? > > class AnyFactory(object): > def __init__(self, anything): > self.product = anything > def __call__(self): > return self.product > def __repr__(self): > return "%s.%s(%r)" % (self.__class__.__module__, > self.__class__.__name__, self.product) > > my use case is: > collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None)))) > > And I think lambda expressions are not preferable... > It's not clear to me that this does what you want. Won't your defaultdict use the same defaultdict for all of its default values? This is hard to describe in English but: d = collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None)))) d[1][1] = 1 d[2][2] = 2 print d[1] #-> defaultdict(__main__.AnyFactory(None), {1: 1, 2: 2}) print d[1] is d[2] #-> True It might not be possible to get this right without lambdas: d = collections.defaultdict(lambda: collections.defaultdict(lambda: None)) d[1][1] = 1 d[2][2] = 2 print d[1] #-> defaultdict( at 0x02091D70>, {1: 1}) print d[1] is d[2] #-> False --Ned. > I found itertools.repeat(anything).next and > functools.partial(copy.copy, anything) > > but both of those don't repr well... and are confusing... > > I think AnyFactory is the most readable, but is confusing if the > reader doesn't know what it is, am I missing a standard implementation > of this? > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiosantosart at gmail.com Wed May 29 14:19:28 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 29 May 2013 19:19:28 +0100 Subject: Getting a callable for any value? Message-ID: On 29 May 2013 18:51, "Croepha" wrote: > > Is there anything like this in the standard library? > > class AnyFactory(object): > def __init__(self, anything): > self.product = anything > def __call__(self): > return self.product > def __repr__(self): > return "%s.%s(%r)" % (self.__class__.__module__, self.__class__.__name__, self.product) > > my use case is: collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None)))) > > And I think lambda expressions are not preferable... > > I found itertools.repeat(anything).next and functools.partial(copy.copy, anything) > > but both of those don't repr well... and are confusing... > > I think AnyFactory is the most readable, but is confusing if the reader doesn't know what it is, am I missing a standard implementation of this? > > Are you sure you don't want to use a lambda expression? They are pretty pythonic. none_factory = lambda: None defaultdict_none_factory = lambda: defaultdict(none_factory) collections.defaultdict(defaultdict_none_factory) Just what are you trying to do? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Wed May 29 14:24:31 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 29 May 2013 12:24:31 -0600 Subject: Getting a callable for any value? In-Reply-To: References: Message-ID: On Wed, May 29, 2013 at 12:19 PM, F?bio Santos wrote: > Are you sure you don't want to use a lambda expression? They are pretty > pythonic. > > none_factory = lambda: None > defaultdict_none_factory = lambda: defaultdict(none_factory) > > collections.defaultdict(defaultdict_none_factory) Gah. If you're going to go to the trouble of assigning a name to the lambda, then just use a def statement: def none_factory: return None def defaultdict_none_factory: return defaultdict(none_factory) collections.defaultdict(defaultdict_none_factory) From ian.g.kelly at gmail.com Wed May 29 14:20:02 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 29 May 2013 12:20:02 -0600 Subject: Getting a callable for any value? In-Reply-To: References: Message-ID: On Wed, May 29, 2013 at 11:46 AM, Croepha wrote: > Is there anything like this in the standard library? > > class AnyFactory(object): > def __init__(self, anything): > self.product = anything > def __call__(self): > return self.product > def __repr__(self): > return "%s.%s(%r)" % (self.__class__.__module__, self.__class__.__name__, > self.product) In other words, the function (lambda: x) with a repr that tells you what x is? Not that I'm aware of, but you could just do something like: def return_x(): return x And then the repr will include the name "return_x", which will give you a hint as to what it does. Also, "AnyFactory" is a misleading name because the class above is not a factory. > my use case is: > collections.defaultdict(AnyFactory(collections.defaultdict(AnyFactory(None)))) > > And I think lambda expressions are not preferable... What you have above is actually buggy. Your "AnyFactory" will always return the *same instance* of the passed in defaultdict, which means that no matter what key you give to the outer defaultdict, you always get the same inner defaultdict. Anyway, I think it's clearer with lambdas: defaultdict(lambda: defaultdict(lambda: None)) But I can see your point about the repr. You could do something like this: class NoneDefaultDict(dict): def __missing__(self, key): return None def __repr__(self): return "NoneDefaultDict(%s)" % super(NoneDefaultDict, self).__repr__() some_dict = defaultdict(NoneDefaultDict) From steve+comp.lang.python at pearwood.info Thu May 30 02:41:01 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 06:41:01 GMT Subject: Getting a callable for any value? References: Message-ID: <51a6f47d$0$11118$c3e8da3@news.astraweb.com> On Wed, 29 May 2013 12:46:19 -0500, Croepha wrote: > Is there anything like this in the standard library? > > class AnyFactory(object): > def __init__(self, anything): > self.product = anything > def __call__(self): > return self.product > def __repr__(self): > return "%s.%s(%r)" % (self.__class__.__module__, > self.__class__.__name__, self.product) > > my use case is: > collections.defaultdict(AnyFactory(collections.defaultdict( > AnyFactory(None)))) That's not a use-case. That's a code snippet. What does it mean? Why would you write such an ugly thing? What does it do? I get a headache just looking at it. I *think* it's a defaultdict that returns a defaultdict on KeyError, where the *second* defaultdict returns None. from collections import defaultdict defaultdict(lambda: defaultdict(lambda: None)) looks more reasonable to me. I don't know why you need to wrap such a simple pair of functions in a class. This is not Java. http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of-nouns.html (Twice in one day I have linked to this.) I'm not sure why you care about the repr of the "AnythingFactory" object. You stuff it directly into the defaultdict, where you are very unlikely to need to inspect it. You only ever see the defaultdicts they return, and they already have a nice repr. -- Steven From damage3025 at gmail.com Wed May 29 14:23:28 2013 From: damage3025 at gmail.com (Ma Xiaojun) Date: Thu, 30 May 2013 02:23:28 +0800 Subject: The state of pySerial Message-ID: Hi, all. pySerial is probably "the solution" for serial port programming. Physical serial port is dead on PC but USB-to-Serial give it a second life. Serial port stuff won't interest end users at all. But it is still used in the EE world and so on. Arduino uses it to upload programs. Sensors may use serial port to communicate with PC. GSM Modem also uses serial port to communicate with PC. Unforunately, pySerial project doesn't seem to have a good state. I find pySerial + Python 3.3 broken on my machine (Python 2.7 is OK) . There are unanswered outstanding bugs, PyPI page has 2.6 while SF homepage still gives 2.5. Any idea? From invalid at invalid.invalid Wed May 29 15:47:30 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 29 May 2013 19:47:30 +0000 (UTC) Subject: The state of pySerial References: Message-ID: On 2013-05-29, Ma Xiaojun wrote: > pySerial is probably "the solution" for serial port programming. > Physical serial port is dead on PC but USB-to-Serial give it a second > life. Serial port stuff won't interest end users at all. But it is > still used in the EE world and so on. Arduino uses it to upload > programs. Sensors may use serial port to communicate with PC. GSM > Modem also uses serial port to communicate with PC. > > Unforunately, pySerial project doesn't seem to have a good state. I > find pySerial + Python 3.3 broken on my machine (Python 2.7 is OK) . > There are unanswered outstanding bugs, PyPI page has 2.6 while SF > homepage still gives 2.5. > > Any idea? Volunteer as a maintainer and start fixing bugs? I use pyserial regularly, and the current version works fine for me, but I'm using Python 2.7. There are still too many libraries that don't support 3.x for me to consider using 3.x for real work. -- Grant Edwards grant.b.edwards Yow! They collapsed at ... like nuns in the gmail.com street ... they had no teen appeal! From tjreedy at udel.edu Wed May 29 17:46:25 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Wed, 29 May 2013 17:46:25 -0400 Subject: The state of pySerial In-Reply-To: References: Message-ID: On 5/29/2013 3:47 PM, Grant Edwards wrote: > On 2013-05-29, Ma Xiaojun wrote: > >> pySerial is probably "the solution" for serial port programming. >> Physical serial port is dead on PC but USB-to-Serial give it a second >> life. Serial port stuff won't interest end users at all. But it is >> still used in the EE world and so on. Arduino uses it to upload >> programs. Sensors may use serial port to communicate with PC. GSM >> Modem also uses serial port to communicate with PC. >> >> Unforunately, pySerial project doesn't seem to have a good state. I >> find pySerial + Python 3.3 broken on my machine (Python 2.7 is OK) . >> There are unanswered outstanding bugs, PyPI page has 2.6 while SF >> homepage still gives 2.5. >> >> Any idea? > > Volunteer as a maintainer and start fixing bugs? It seems to be getting around 200 downloands a day. Quite worth someone supporting it. > I use pyserial regularly, and the current version works fine for me, > but I'm using Python 2.7. There are still too many libraries that > don't support 3.x for me to consider using 3.x for real work. The only download is a .exe. It it just the executable binary or is that a zip unpacker with source? From invalid at invalid.invalid Wed May 29 23:59:16 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 30 May 2013 03:59:16 +0000 (UTC) Subject: The state of pySerial References: Message-ID: On 2013-05-29, Terry Jan Reedy wrote: > On 5/29/2013 3:47 PM, Grant Edwards wrote: >> On 2013-05-29, Ma Xiaojun wrote: [...] >>> Unforunately, pySerial project doesn't seem to have a good state. I >>> find pySerial + Python 3.3 broken on my machine (Python 2.7 is OK) . >>> There are unanswered outstanding bugs, PyPI page has 2.6 while SF >>> homepage still gives 2.5. >>> >>> Any idea? >> >> Volunteer as a maintainer and start fixing bugs? > > It seems to be getting around 200 downloands a day. Quite worth > someone supporting it. Chris has a day job, just like the rest of us. He might even have a family and hobbies other than supporting pyserial. ;) Everybody should feel free to submit patches for open bugs and to test any patches waiting to be accepted. >> I use pyserial regularly, and the current version works fine for me, >> but I'm using Python 2.7. There are still too many libraries that >> don't support 3.x for me to consider using 3.x for real work. > > The only download is a .exe. It it just the executable binary or is that > a zip unpacker with source? http://pyserial.sourceforge.net/pyserial.html#from-source-tar-gz-or-checkout https://pypi.python.org/packages/source/p/pyserial/pyserial-2.6.tar.gz#md5=cde799970b7c1ce1f7d6e9ceebe64c98 -- Grant From wrw at mac.com Wed May 29 16:00:58 2013 From: wrw at mac.com (William Ray Wing) Date: Wed, 29 May 2013 16:00:58 -0400 Subject: The state of pySerial In-Reply-To: References: Message-ID: On May 29, 2013, at 2:23 PM, Ma Xiaojun wrote: > Hi, all. > > pySerial is probably "the solution" for serial port programming. > Physical serial port is dead on PC but USB-to-Serial give it a second > life. Serial port stuff won't interest end users at all. But it is > still used in the EE world and so on. Arduino uses it to upload > programs. Sensors may use serial port to communicate with PC. GSM > Modem also uses serial port to communicate with PC. > > Unforunately, pySerial project doesn't seem to have a good state. I > find pySerial + Python 3.3 broken on my machine (Python 2.7 is OK) . > There are unanswered outstanding bugs, PyPI page has 2.6 while SF > homepage still gives 2.5. > > Any idea? > -- > http://mail.python.org/mailman/listinfo/python-list Let me add another vote/request for pySerial support. I've been using it with python 2.7 on OS-X, unaware that there wasn't a path forward to python 3.x. If an external sensor absolutely positively has to be readable, then RS-232 is the only way to go. USB interfaces can and do lock up if recovery from a power failure puts power on the external side before the computer has finished initializing the CPU side. RS-232, bless its primitive heart, could care less. Thanks, Bill From tjreedy at udel.edu Wed May 29 17:38:33 2013 From: tjreedy at udel.edu (Terry Jan Reedy) Date: Wed, 29 May 2013 17:38:33 -0400 Subject: The state of pySerial In-Reply-To: References: Message-ID: On 5/29/2013 4:00 PM, William Ray Wing wrote: > On May 29, 2013, at 2:23 PM, Ma Xiaojun wrote: > >> Hi, all. >> >> pySerial is probably "the solution" for serial port programming. >> Physical serial port is dead on PC but USB-to-Serial give it a second >> life. Serial port stuff won't interest end users at all. But it is >> still used in the EE world and so on. Arduino uses it to upload >> programs. Sensors may use serial port to communicate with PC. GSM >> Modem also uses serial port to communicate with PC. >> >> Unforunately, pySerial project doesn't seem to have a good state. I >> find pySerial + Python 3.3 broken on my machine (Python 2.7 is OK) . >> There are unanswered outstanding bugs, PyPI page has 2.6 while SF >> homepage still gives 2.5. >> >> Any idea? >> -- >> http://mail.python.org/mailman/listinfo/python-list > > Let me add another vote/request for pySerial support. I've been using it with python 2.7 on OS-X, unaware that there wasn't a path forward to python 3.x. If an external sensor absolutely positively has to be readable, then RS-232 is the only way to go. USB interfaces can and do lock up if recovery from a power failure puts power on the external side before the computer has finished initializing the CPU side. RS-232, bless its primitive heart, could care less. Then 'someone' should ask the author his intentions and offer to help or take over. I did some RS-232 interfacing in the 1980s, and once past the fiddly start/stop/parity bit, baud rate, and wiring issues, I had a program run connected to multiple machines for years with no more interface problems. Terry From python at mrabarnett.plus.com Wed May 29 18:34:47 2013 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 May 2013 23:34:47 +0100 Subject: The state of pySerial In-Reply-To: References: Message-ID: <51A68287.1010505@mrabarnett.plus.com> On 29/05/2013 22:38, Terry Jan Reedy wrote: > On 5/29/2013 4:00 PM, William Ray Wing wrote: >> On May 29, 2013, at 2:23 PM, Ma Xiaojun wrote: >> >>> Hi, all. >>> >>> pySerial is probably "the solution" for serial port programming. >>> Physical serial port is dead on PC but USB-to-Serial give it a second >>> life. Serial port stuff won't interest end users at all. But it is >>> still used in the EE world and so on. Arduino uses it to upload >>> programs. Sensors may use serial port to communicate with PC. GSM >>> Modem also uses serial port to communicate with PC. >>> >>> Unforunately, pySerial project doesn't seem to have a good state. I >>> find pySerial + Python 3.3 broken on my machine (Python 2.7 is OK) . >>> There are unanswered outstanding bugs, PyPI page has 2.6 while SF >>> homepage still gives 2.5. >>> >>> Any idea? >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> Let me add another vote/request for pySerial support. I've been using it with python 2.7 on OS-X, unaware that there wasn't a path forward to python 3.x. If an external sensor absolutely positively has to be readable, then RS-232 is the only way to go. USB interfaces can and do lock up if recovery from a power failure puts power on the external side before the computer has finished initializing the CPU side. RS-232, bless its primitive heart, could care less. > > Then 'someone' should ask the author his intentions and offer to help or > take over. > This page: http://pyserial.sourceforge.net/pyserial.html#requirements says: "Python 2.3 or newer, including Python 3.x" > I did some RS-232 interfacing in the 1980s, and once past the fiddly > start/stop/parity bit, baud rate, and wiring issues, I had a program run > connected to multiple machines for years with no more interface problems. > From damage3025 at gmail.com Wed May 29 21:32:48 2013 From: damage3025 at gmail.com (Ma Xiaojun) Date: Thu, 30 May 2013 09:32:48 +0800 Subject: The state of pySerial In-Reply-To: <51A68287.1010505@mrabarnett.plus.com> References: <51A68287.1010505@mrabarnett.plus.com> Message-ID: I've already mailed the author, waiting for reply. For Windows people, downloading a exe get you pySerial 2.5, which list_ports and miniterm feature seems not included. To use 2.6, download the tar.gz and use standard "setup.py install" to install it (assume you have .py associated) . There is no C compiling involved in the installation process. For whether Python 3.3 is supported or not. I observed something like: http://paste.ubuntu.com/5715275/ . miniterm works for Python 3.3 at this time. From python at mrabarnett.plus.com Thu May 30 11:45:16 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 May 2013 16:45:16 +0100 Subject: The state of pySerial In-Reply-To: References: <51A68287.1010505@mrabarnett.plus.com> Message-ID: <51A7740C.9080402@mrabarnett.plus.com> On 30/05/2013 02:32, Ma Xiaojun wrote: > I've already mailed the author, waiting for reply. > > For Windows people, downloading a exe get you pySerial 2.5, which > list_ports and miniterm feature seems not included. To use 2.6, > download the tar.gz and use standard "setup.py install" to install it > (assume you have .py associated) . There is no C compiling involved in > the installation process. > > For whether Python 3.3 is supported or not. I observed something like: > http://paste.ubuntu.com/5715275/ . > > miniterm works for Python 3.3 at this time. > The problem there is that 'desc' is a bytestring, but the regex pattern can match only a Unicode string (Python 3 doesn't let you mix bytestrings and Unicode string like a Python 2). The simplest fix would probably be to decode 'desc' to Unicode. From steve at terrafirma.us Wed May 29 14:33:07 2013 From: steve at terrafirma.us (alcyon) Date: Wed, 29 May 2013 11:33:07 -0700 (PDT) Subject: python b'...' notation Message-ID: <17dc2765-bc59-407a-9227-d67572411ad1@googlegroups.com> This notation displays hex values except when they are 'printable', in which case it displays that printable character. How do I get it to force hex for all bytes? Thanks, Steve From ian.g.kelly at gmail.com Wed May 29 15:14:03 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 29 May 2013 13:14:03 -0600 Subject: python b'...' notation In-Reply-To: <17dc2765-bc59-407a-9227-d67572411ad1@googlegroups.com> References: <17dc2765-bc59-407a-9227-d67572411ad1@googlegroups.com> Message-ID: On Wed, May 29, 2013 at 12:33 PM, alcyon wrote: > This notation displays hex values except when they are 'printable', in which case it displays that printable character. How do I get it to force hex for all bytes? Thanks, Steve Is this what you want? >>> ''.join('%02x' % x for x in b'hello world') '68656c6c6f20776f726c64' From cs at zip.com.au Wed May 29 18:19:42 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 30 May 2013 08:19:42 +1000 Subject: python b'...' notation In-Reply-To: References: Message-ID: <20130529221942.GA55776@cskk.homeip.net> On 29May2013 13:14, Ian Kelly wrote: | On Wed, May 29, 2013 at 12:33 PM, alcyon wrote: | > This notation displays hex values except when they are 'printable', in which case it displays that printable character. How do I get it to force hex for all bytes? Thanks, Steve | | Is this what you want? | | >>> ''.join('%02x' % x for x in b'hello world') | '68656c6c6f20776f726c64' Not to forget binascii.hexlify. -- Cameron Simpson Every particle continues in its state of rest or uniform motion in a straight line except insofar as it doesn't. - Sir Arther Eddington From steve at terrafirma.us Thu May 30 18:19:38 2013 From: steve at terrafirma.us (alcyon) Date: Thu, 30 May 2013 15:19:38 -0700 (PDT) Subject: python b'...' notation In-Reply-To: References: Message-ID: <94eb93eb-3f95-448c-979a-5bfc4da9ef06@googlegroups.com> On Wednesday, May 29, 2013 3:19:42 PM UTC-7, Cameron Simpson wrote: > On 29May2013 13:14, Ian Kelly wrote: > > | On Wed, May 29, 2013 at 12:33 PM, alcyon wrote: > > | > This notation displays hex values except when they are 'printable', in which case it displays that printable character. How do I get it to force hex for all bytes? Thanks, Steve > > | > > | Is this what you want? > > | > > | >>> ''.join('%02x' % x for x in b'hello world') > > | '68656c6c6f20776f726c64' > > > > Not to forget binascii.hexlify. > > -- > > Cameron Simpson > > > > Every particle continues in its state of rest or uniform motion in a straight > > line except insofar as it doesn't. - Sir Arther Eddington Thanks for the binascii.hexlify tip. I was able to make it work but I did have to write a function to get it exactly the string I wanted. I wanted, for example, to display as <0x0A 0x00> or to display as <0x21 0xFF 0x28 0xC0>. From wxjmfauth at gmail.com Fri May 31 03:51:55 2013 From: wxjmfauth at gmail.com (jmfauth) Date: Fri, 31 May 2013 00:51:55 -0700 (PDT) Subject: python b'...' notation References: <94eb93eb-3f95-448c-979a-5bfc4da9ef06@googlegroups.com> Message-ID: On 31 mai, 00:19, alcyon wrote: > On Wednesday, May 29, 2013 3:19:42 PM UTC-7, Cameron Simpson wrote: > > On 29May2013 13:14, Ian Kelly wrote: > > > | On Wed, May 29, 2013 at 12:33 PM, alcyon wrote: > > > | > This notation displays hex values except when they are 'printable', in which case it displays that printable character. ?How do I get it to force hex for all bytes? ?Thanks, Steve > > > | > > > | Is this what you want? > > > | > > > | >>> ''.join('%02x' % x for x in b'hello world') > > > | '68656c6c6f20776f726c64' > > > Not to forget binascii.hexlify. > > > -- > > > Cameron Simpson > > > Every particle continues in its state of rest or uniform motion in a straight > > > line except insofar as it doesn't. ? ? ?- Sir Arther Eddington > > Thanks for the binascii.hexlify tip. I was able to make it work but I did have to write a function to get it exactly the string I wanted. ?I wanted, for example, to display as <0x0A 0x00> or to display as <0x21 0xFF 0x28 0xC0>. -------- >>> a = b'!\xff(\xc0\n\x00' >>> z = ['0x{:02X}'.format(c) for c in b] >>> z ['0x21', '0xFF', '0x28', '0xC0', '0x0A', '0x00'] >>> s = ' '.join(z) >>> s '0x21 0xFF 0x28 0xC0 0x0A 0x00' >>> jmf From damage3025 at gmail.com Wed May 29 14:13:23 2013 From: damage3025 at gmail.com (Ma Xiaojun) Date: Thu, 30 May 2013 02:13:23 +0800 Subject: How clean/elegant is Python's syntax? Message-ID: Hi, list. I hope this is not a duplicate of older question. If so, drop me a link is enough. I've used Python here and there, just for the sweet libraries though. For the core language, I have mixed feeling. On one hand, I find that Python has some sweet feature that is quite useful. On the other hand, I often find Pyhton snippets around hard to understand. I admit that I never learned Python very formally; I've programmed in many other languages already. Code snippets in BASIC or Pascal seems quite obvious to understand (Unless I don't understand the algorithm) . Can someone inspire me? From damage3025 at gmail.com Wed May 29 14:37:35 2013 From: damage3025 at gmail.com (Ma Xiaojun) Date: Thu, 30 May 2013 02:37:35 +0800 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: A bit more context. If visiting z.cn (Amazon China), one can see that there are plenty of new (published in 2010 or later) books on QBASIC, Visual Basic, Visual Foxpro. This is weird, if one want to do development legally these tools won't be a option for new programmers. However, I also like to know whether there are any technical arguments that Python is superior in the context of education. For pure procedural paradigm, I haven't seen much advantages of Python. Yes, Python has true OOP but I don't like this argument since I don't like Java-ism true OOP. Yes, Python has much more libraries. But it seems that Python is more useful and suitable in CLI and Web applications. People are still discussing whether to replace tkinter with wxPython or not. VB and VFP people are never bothered with such issue. From rosuav at gmail.com Wed May 29 14:54:44 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 04:54:44 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: On Thu, May 30, 2013 at 4:37 AM, Ma Xiaojun wrote: > Yes, Python has much more libraries. But it seems that Python is more > useful and suitable in CLI and Web applications. People are still > discussing whether to replace tkinter with wxPython or not. VB and VFP > people are never bothered with such issue. Let me put this debate in context by giving an analogous example. A while back, it was loudly proclaimed that it was far easier to write iPhone apps than Android ones, because Android apps had to worry about a variety of different screen resolutions/sizes, while iPhone ones could be certain of what they were going to get. But what this really meant was that writing for iPhone was equivalent to writing for "Android on phone model XYZ", and that you could, by targeting Android, also then make your app available on a bunch of other phone models by simply dealing with the differences. Python gives you a lot more choice than VB does. With Visual BASIC, if you don't like the windowing toolkit, tough. You don't have any alternative. With Python, you can pretend it's like VB by simply picking one toolkit and ignoring all the others; it'll be exactly the same. But the benefit is that, if you decide you want one of the others, it's relatively cheap to switch. That said, though, GUIs and databasing are two of the areas where I think Python's standard library could stand to be improved a bit. There are definitely some rough edges there. ChrisA From walterhurry at lavabit.com Wed May 29 17:01:34 2013 From: walterhurry at lavabit.com (Walter Hurry) Date: Wed, 29 May 2013 21:01:34 +0000 (UTC) Subject: How clean/elegant is Python's syntax? References: Message-ID: On Thu, 30 May 2013 04:54:44 +1000, Chris Angelico wrote: > GUIs and databasing are two of the areas where I > think Python's standard library could stand to be improved a bit. > There are definitely some rough edges there. Dunno what you mean about "standard library", but I'm very happy with wxPython and psycopg2 for GUIs and databasing respectively. From rosuav at gmail.com Wed May 29 23:57:49 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 13:57:49 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: On Thu, May 30, 2013 at 7:01 AM, Walter Hurry wrote: > On Thu, 30 May 2013 04:54:44 +1000, Chris Angelico wrote: > > >> GUIs and databasing are two of the areas where I >> think Python's standard library could stand to be improved a bit. >> There are definitely some rough edges there. > > Dunno what you mean about "standard library", but I'm very happy with > wxPython and psycopg2 for GUIs and databasing respectively. They are not part of the standard library. Yes, Python is strongly enhanced by additional packages off PyPI, but that's not the same thing; if I publish a program that requires psycopg2, I can't simply say "go get Python from your OS's repository or python.org", I have to also instruct people to install another package. On Debian, I can simply apt-get python-psycopg2, which I would trust to be (a) a stable build, (b) compatible with the apt-gettable python (which is 2.7.3; ditto python3-psycopg2 and python3, for 3.2.3), and (c) from an authoritative source. There's probably a way to do this for other Linuxes too, but I don't know the exact package names everywhere. And on Windows, I have no idea what the best way would be. These days, networking is considered essential. Python's standard library includes basic sockets, HTTP (client and server), etc. AFAIK Python doesn't have obscurities like DNS (obviously you can connect a socket by hostname, but you can't look up an SPF record, nor can you write a DNS server), but networking generally is considered important enough to be inbuilt. Why is databasing second-class? ChrisA From dreamingforward at gmail.com Wed May 29 17:13:12 2013 From: dreamingforward at gmail.com (Mark Janssen) Date: Wed, 29 May 2013 14:13:12 -0700 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: You might try http://wiki.python.org/moin/BeginnersGuide -- MarkJ Tacoma, Washington From cs at zip.com.au Wed May 29 18:12:07 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 30 May 2013 08:12:07 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: <20130529221207.GA44756@cskk.homeip.net> On 30May2013 02:13, Ma Xiaojun wrote: | For the core language, I have mixed feeling. On one hand, I find that | Python has some sweet feature that is quite useful. On the other hand, | I often find Pyhton snippets around hard to understand. I think you will find that is lack of practice. I find Python far far easier to read, even in snippet form. BUT, for years I found it obtuse because I hadn't learnt it. Now that I have, I rarely want to use other things (if Python is apt; often but not always). | I admit that I | never learned Python very formally; I've programmed in many other | languages already. Me too. Use it some more. | Code snippets in BASIC or Pascal seems quite obvious to understand | (Unless I don't understand the algorithm) . Code in BASIC is generally lower level than python; it is less expressive. Because of this, it will be more obvious in terms of being more direct. But it will be less expressive because you will often need more BASIC to do something than you would with Python. So python is usually more succinct, and therefore more more expressive: a shorter but very readable way to do the same thing. Comparison: your English is excellent. Presuming from context that you're in China, many of your compatriots do not speak fluent English (and why should they?) For those speaking English as a second language there are difficulties; English grammar I gather is very different, and it has a fine suite of irregular forms. (Let me say up front that I do not speak Chinese at all.) Anyway, would you rather converse with someone fluent, or not? I would expect you would far rather deal with a fluent English speaker or a fluent Chinese speaker than a speaker using English badly. Python code is a lot like written English. BASIC is like badly written English. Cheers, -- Cameron Simpson Helicopters are considerably more expensive [than fixed wing aircraft], which is only right because they don't actually fly, but just beat the air into submission. - Paul Tomblin From drsalists at gmail.com Wed May 29 20:24:48 2013 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 29 May 2013 17:24:48 -0700 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: On Wed, May 29, 2013 at 11:13 AM, Ma Xiaojun wrote: > Hi, list. > > For the core language, I have mixed feeling. On one hand, I find that > Python has some sweet feature that is quite useful. On the other hand, > I often find Pyhton snippets around hard to understand. I admit that I > never learned Python very formally; I've programmed in many other > languages already. > > Code snippets in BASIC or Pascal seems quite obvious to understand > (Unless I don't understand the algorithm) . > I'm finding it kind of hard to imagine not finding Python's syntax and semantics pretty graceful. About the only thing I don't like is: var = 1, That binds var to a tuple (singleton) value, instead of 1. Oh, and method decorators seem much more complex than they should've been. But on the whole, python is a pretty beautiful language. It's not just another rehash of Pascal though; if that's what you want you might be better off looking elsewhere. -------------- next part -------------- An HTML attachment was scrubbed... URL: From damage3025 at gmail.com Wed May 29 21:14:06 2013 From: damage3025 at gmail.com (Ma Xiaojun) Date: Thu, 30 May 2013 09:14:06 +0800 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: On Thu, May 30, 2013 at 8:24 AM, Dan Stromberg wrote: > I'm finding it kind of hard to imagine not finding Python's syntax and > semantics pretty graceful. > > About the only thing I don't like is: > > var = 1, > > That binds var to a tuple (singleton) value, instead of 1. > > Oh, and method decorators seem much more complex than they should've been. Yes, you touched something. IMHO, Python has far more built-in features so it looks at least complicated from time to time. For example, some people use "generating 9x9 multiplication table" as an programming exercise. What interest me is a one liner: print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in range(1,10)]) for j in range(1,10)]) I don't like code like this. But Python at least allow such practise. > But on the whole, python is a pretty beautiful language. It's not just > another rehash of Pascal though; if that's what you want you might be better > off looking elsewhere. That's a fair point. From rustompmody at gmail.com Wed May 29 22:49:19 2013 From: rustompmody at gmail.com (rusi) Date: Wed, 29 May 2013 19:49:19 -0700 (PDT) Subject: How clean/elegant is Python's syntax? References: Message-ID: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> On May 30, 6:14?am, Ma Xiaojun wrote: > What interest me is a one liner: > print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in > range(1,10)]) for j in range(1,10)]) Ha,Ha! The join method is one of the (for me) ugly features of python. You can sweep it under the carpet with a one-line join function and then write clean and pretty code: #joinwith def joinw(l,sep): return sep.join(l) def mktable(m,n): return [[(j,i,i*j) for i in range(1,m+1)] for j in range(1,n+1)] def prettyrow(r): return joinw(['%d*%d=%d' % ele for ele in r],'\t') def prettytable(t): return joinw([prettyrow(r) for r in t],'\n') > I don't like code like this. But Python at least allow such practise. Are you saying VB etc disallow dirty code?? Dirty code is always possible in all languages. Of course the shape and size and smell of the dirt will differ From ian.g.kelly at gmail.com Thu May 30 14:36:54 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 30 May 2013 12:36:54 -0600 Subject: How clean/elegant is Python's syntax? In-Reply-To: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> Message-ID: On Wed, May 29, 2013 at 8:49 PM, rusi wrote: > On May 30, 6:14 am, Ma Xiaojun wrote: >> What interest me is a one liner: >> print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in >> range(1,10)]) for j in range(1,10)]) > > Ha,Ha! The join method is one of the (for me) ugly features of python. > You can sweep it under the carpet with a one-line join function and > then write clean and pretty code: > > #joinwith > def joinw(l,sep): return sep.join(l) I don't object to changing the join method (one of the more shoe-horned string methods) back into a function, but to my eyes you've got the arguments backward. It should be: def join(sep, iterable): return sep.join(iterable) Putting the separator first feels more natural to me because I expect the separator to usually be short as compared to the iterable, which is often a longer expression (as is the case in both of your subsequent usages). Placing the separator first also preserves consistency of interface with the str.join and bytes.join functions, as well as the older string.join function. From rosuav at gmail.com Thu May 30 14:44:42 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 04:44:42 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> Message-ID: On Fri, May 31, 2013 at 4:36 AM, Ian Kelly wrote: > On Wed, May 29, 2013 at 8:49 PM, rusi wrote: >> On May 30, 6:14 am, Ma Xiaojun wrote: >>> What interest me is a one liner: >>> print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in >>> range(1,10)]) for j in range(1,10)]) >> >> Ha,Ha! The join method is one of the (for me) ugly features of python. >> You can sweep it under the carpet with a one-line join function and >> then write clean and pretty code: >> >> #joinwith >> def joinw(l,sep): return sep.join(l) > > I don't object to changing the join method (one of the more > shoe-horned string methods) back into a function, but to my eyes > you've got the arguments backward. It should be: > > def join(sep, iterable): return sep.join(iterable) Trouble is, it makes some sense either way. I often put the larger argument first - for instance, I would write 123412341324*5 rather than the other way around - and in this instance, it hardly seems as clear-cut as you imply. But the function can't be written to take them in either order, because strings are iterable too. (And functions that take args either way around aren't better than those that make a decision.) ChrisA From ian.g.kelly at gmail.com Thu May 30 14:51:14 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 30 May 2013 12:51:14 -0600 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> Message-ID: On Thu, May 30, 2013 at 12:44 PM, Chris Angelico wrote: > On Fri, May 31, 2013 at 4:36 AM, Ian Kelly wrote: >> I don't object to changing the join method (one of the more >> shoe-horned string methods) back into a function, but to my eyes >> you've got the arguments backward. It should be: >> >> def join(sep, iterable): return sep.join(iterable) > > Trouble is, it makes some sense either way. I often put the larger > argument first - for instance, I would write 123412341324*5 rather > than the other way around - and in this instance, it hardly seems as > clear-cut as you imply. But the function can't be written to take them > in either order, because strings are iterable too. (And functions that > take args either way around aren't better than those that make a > decision.) The reason I like having the shorter argument first (at least for function calls) is for when I'm reading the code. If I'm interested in the second argument, then to find it I have to scan over the first argument. I would rather scan over something short like '\n' than something longer like a list comprehension. It sounds like a trivial thing, but it really does make it easier to find where an expression starts and ends when the expression is short. From rosuav at gmail.com Thu May 30 17:28:17 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 07:28:17 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> Message-ID: On Fri, May 31, 2013 at 4:51 AM, Ian Kelly wrote: > On Thu, May 30, 2013 at 12:44 PM, Chris Angelico wrote: >> On Fri, May 31, 2013 at 4:36 AM, Ian Kelly wrote: >>> I don't object to changing the join method (one of the more >>> shoe-horned string methods) back into a function, but to my eyes >>> you've got the arguments backward. It should be: >>> >>> def join(sep, iterable): return sep.join(iterable) >> >> Trouble is, it makes some sense either way. I often put the larger >> argument first - for instance, I would write 123412341324*5 rather >> than the other way around - and in this instance, it hardly seems as >> clear-cut as you imply. But the function can't be written to take them >> in either order, because strings are iterable too. (And functions that >> take args either way around aren't better than those that make a >> decision.) > > The reason I like having the shorter argument first (at least for > function calls) is for when I'm reading the code. If I'm interested > in the second argument, then to find it I have to scan over the first > argument. I would rather scan over something short like '\n' than > something longer like a list comprehension. It sounds like a trivial > thing, but it really does make it easier to find where an expression > starts and ends when the expression is short. Yes, I do agree with that argument. But there's more to picking argument order than simply "sort by predicted length" :) I'm not saying it's *wrong* to put sep first, just that it's not as clearly-and-obviously-the-one-right-way as you suggested. ChrisA From python at mrabarnett.plus.com Thu May 30 15:38:40 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 May 2013 20:38:40 +0100 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> Message-ID: <51A7AAC0.6080509@mrabarnett.plus.com> On 30/05/2013 19:44, Chris Angelico wrote: > On Fri, May 31, 2013 at 4:36 AM, Ian Kelly wrote: >> On Wed, May 29, 2013 at 8:49 PM, rusi wrote: >>> On May 30, 6:14 am, Ma Xiaojun wrote: >>>> What interest me is a one liner: >>>> print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in >>>> range(1,10)]) for j in range(1,10)]) >>> >>> Ha,Ha! The join method is one of the (for me) ugly features of python. >>> You can sweep it under the carpet with a one-line join function and >>> then write clean and pretty code: >>> >>> #joinwith >>> def joinw(l,sep): return sep.join(l) >> >> I don't object to changing the join method (one of the more >> shoe-horned string methods) back into a function, but to my eyes >> you've got the arguments backward. It should be: >> >> def join(sep, iterable): return sep.join(iterable) > > Trouble is, it makes some sense either way. I often put the larger > argument first - for instance, I would write 123412341324*5 rather > than the other way around - and in this instance, it hardly seems as > clear-cut as you imply. But the function can't be written to take them > in either order, because strings are iterable too. (And functions that > take args either way around aren't better than those that make a > decision.) > And additional argument (pun not intended) for putting sep second is that you can give it a default value: def join(iterable, sep=""): return sep.join(iterable) From ian.g.kelly at gmail.com Fri May 31 11:43:54 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 31 May 2013 09:43:54 -0600 Subject: How clean/elegant is Python's syntax? In-Reply-To: <51A7AAC0.6080509@mrabarnett.plus.com> References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> <51A7AAC0.6080509@mrabarnett.plus.com> Message-ID: On Thu, May 30, 2013 at 1:38 PM, MRAB wrote: > And additional argument (pun not intended) for putting sep second is > that you can give it a default value: > > def join(iterable, sep=""): return sep.join(iterable) One argument against the default is that it is specific to the str type. If you then tried to use join with an iterable of bytes objects and the default sep argument, you would get a TypeError. At least not having the default forces you to be explicit about which string type you're joining. From rosuav at gmail.com Fri May 31 14:52:53 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 1 Jun 2013 04:52:53 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> <51A7AAC0.6080509@mrabarnett.plus.com> Message-ID: On Sat, Jun 1, 2013 at 1:43 AM, Ian Kelly wrote: > On Thu, May 30, 2013 at 1:38 PM, MRAB wrote: >> And additional argument (pun not intended) for putting sep second is >> that you can give it a default value: >> >> def join(iterable, sep=""): return sep.join(iterable) > > One argument against the default is that it is specific to the str > type. If you then tried to use join with an iterable of bytes objects > and the default sep argument, you would get a TypeError. At least not > having the default forces you to be explicit about which string type > you're joining. What about: def join(iterable, sep=None): if sep is not None: return sep.join(iterable) iterable=iter(iterable) first = next(iterable) return first + type(first)().join(iterable) Granted, it has some odd error messages if you pass it stuff that isn't strings: >>> join([[1,2,3],[4,5,6]]) Traceback (most recent call last): File "", line 1, in join([[1,2,3],[4,5,6]]) File "", line 5, in join return first + type(first)().join(iterable) AttributeError: 'list' object has no attribute 'join' but you'd get that sort of thing anyway. (NOTE: I am *not* advocating this. I just see it as a solution to one particular objection.) ChrisA From rustompmody at gmail.com Thu May 30 14:47:00 2013 From: rustompmody at gmail.com (rusi) Date: Thu, 30 May 2013 11:47:00 -0700 (PDT) Subject: How clean/elegant is Python's syntax? References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> Message-ID: <0debce7e-f05c-40c4-b688-737e7ad49a40@z10g2000pbn.googlegroups.com> On May 30, 11:36?pm, Ian Kelly wrote: > On Wed, May 29, 2013 at 8:49 PM, rusi wrote: > > On May 30, 6:14 am, Ma Xiaojun wrote: > >> What interest me is a one liner: > >> print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in > >> range(1,10)]) for j in range(1,10)]) > > > Ha,Ha! The join method is one of the (for me) ugly features of python. > > You can sweep it under the carpet with a one-line join function and > > then write clean and pretty code: > > > #joinwith > > def joinw(l,sep): return sep.join(l) > > I don't object to changing the join method (one of the more > shoe-horned string methods) back into a function, but to my eyes > you've got the arguments backward. ?It should be: > > def join(sep, iterable): return sep.join(iterable) > > Putting the separator first feels more natural to me because I expect > the separator to usually be short as compared to the iterable, which > is often a longer expression (as is the case in both of your > subsequent usages). ?Placing the separator first also preserves > consistency of interface with the str.join and bytes.join functions, > as well as the older string.join function. This is a subjective view of course... My problem is not method vs function but the order. Ive seen/used join dozens of times. Yet I find "".join(["apple","bear","cat"]) backkwards as compared to ["apple","bear","cat"].join("") The consistency is a separate question -- not arguing about that. Just that I dont like the look From john_ladasky at sbcglobal.net Thu May 30 18:01:20 2013 From: john_ladasky at sbcglobal.net (John Ladasky) Date: Thu, 30 May 2013 15:01:20 -0700 (PDT) Subject: How clean/elegant is Python's syntax? In-Reply-To: References: <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> Message-ID: <32d47a30-8b74-41f4-a774-c878c60c7f73@googlegroups.com> On Thursday, May 30, 2013 11:36:54 AM UTC-7, Ian wrote: > I don't object to changing the join method (one of the more > shoe-horned string methods) back into a function, but to my eyes > you've got the arguments backward. It should be: > > def join(sep, iterable): return sep.join(iterable) > > Putting the separator first feels more natural to me because I expect > the separator to usually be short as compared to the iterable, which > is often a longer expression (as is the case in both of your > subsequent usages). Placing the separator first also preserves > consistency of interface with the str.join and bytes.join functions, > as well as the older string.join function. That may all be true, but the join() function shown will return a sequence starting with iterable[0] (followed by sep, and then iterable[1], then sep, etc.). I find it more natural to see iterable as the first argument passed to join() for that reason. Season to taste, I guess. From rosuav at gmail.com Wed May 29 23:59:08 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 13:59:08 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: On Thu, May 30, 2013 at 10:24 AM, Dan Stromberg wrote: > I'm finding it kind of hard to imagine not finding Python's syntax and > semantics pretty graceful. > > About the only thing I don't like is: > > var = 1, > > That binds var to a tuple (singleton) value, instead of 1. I agree, and would write it as: var = (1,) for clarity. ChrisA From steve+comp.lang.python at pearwood.info Thu May 30 02:18:43 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 06:18:43 GMT Subject: How clean/elegant is Python's syntax? References: Message-ID: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> On Thu, 30 May 2013 02:37:35 +0800, Ma Xiaojun wrote: > For pure procedural paradigm, I haven't seen much advantages of Python. Nice syntax with a minimum of boiler plate -- "executable pseudo-code", as they say. Extensive library support -- "batteries included". These are both good advantages. > Yes, Python has true OOP but I don't like this argument since I don't > like Java-ism true OOP. Java is not the best example of OOP. In some ways, it is a terrible example of OOP: some values are not objects, classes are not first-class values, and the language is horribly verbose. There are good reasons for some of these things, but good reasons or bad, Java is *not* the exemplar of OOP that some Java coders believe. In some ways, Python is a more pure OOP language than Java: everything in Python is an object, including classes themselves. In other ways, Python is a less pure and more practical language. You don't have to wrap every piece of functionality in a class. Python encourages you to write mixed procedural, functional and object oriented code, whatever is best for the problem you are trying to solve, which is very much in contrast to Java: http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of- nouns.html > Yes, Python has much more libraries. But it seems that Python is more > useful and suitable in CLI and Web applications. That is fair. All languages have their strengths and weaknesses. I wouldn't use Python to program low-level device driver code, and I wouldn't write a web-app in C. > People are still > discussing whether to replace tkinter with wxPython or not. VB and VFP > people are never bothered with such issue. Which people? "People" can discuss any rubbish they like. For many reasons, tkinter will not be replaced. For the standard library, it is a good, stable, powerful but not cutting-edge GUI library. If you don't like it, you can install a third-party framework like wxPython. Using tkinter is not compulsory. In the case of VB and VFP, they aren't bothered by such issues because they're used to closed-source, proprietary programming where you use what you are given and like it. In the open-source world, if you don't like what you are given, you find something else, and if you can't find it, you make it yourself. -- Steven From damage3025 at gmail.com Thu May 30 04:54:14 2013 From: damage3025 at gmail.com (Ma Xiaojun) Date: Thu, 30 May 2013 16:54:14 +0800 Subject: How clean/elegant is Python's syntax? In-Reply-To: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> Message-ID: On Thu, May 30, 2013 at 2:18 PM, Steven D'Aprano wrote: > Which people? "People" can discuss any rubbish they like. For many > reasons, tkinter will not be replaced. For the standard library, it is a > good, stable, powerful but not cutting-edge GUI library. If you don't > like it, you can install a third-party framework like wxPython. Using > tkinter is not compulsory. I'm new to tkinter and find tkdocs.com seems quite good. But tkdocs.com's Python code sample is in Python 3. And wxPython doesn't support Python 3 yet. ( May not be big issue but it's kind of bad. ) I observation about tkinter is that it seems lack of sophisticated features. For example, there is nothing like DataWindow in PowerBuilder? Python's IDLE is written in tkinter. But anyone willing to use IDLE is a successful example of tkinter? I actually use Gedit more than PyDev, etc. But the non-fancy state of IDLE does reflect something, I guess. > In the case of VB and VFP, they aren't bothered by such issues because > they're used to closed-source, proprietary programming where you use what > you are given and like it. In the open-source world, if you don't like > what you are given, you find something else, and if you can't find it, > you make it yourself. I doesn't mean that choices are bad. I just kind of doubt that whether Python with a open source GUI toolkit can cover the features provided by VB standard controls and some external Windows built-in controls. I'm almost sure that tkinter lacks the features provided by sophisticated controls. I have limited knowledge to VFP. From rustompmody at gmail.com Thu May 30 13:12:22 2013 From: rustompmody at gmail.com (rusi) Date: Thu, 30 May 2013 10:12:22 -0700 (PDT) Subject: How clean/elegant is Python's syntax? References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> Message-ID: <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> On Thu, May 30, 2013 at 9:34 AM, Ma Xiaojun wrote: > > On Thu, May 30, 2013 at 10:49 AM, rusi wrote: > > Ha,Ha! The join method is one of the (for me) ugly features of python. > > You can sweep it under the carpet with a one-line join function and > > then write clean and pretty code: > > > > #joinwith > > def joinw(l,sep): return sep.join(l) > > > > def mktable(m,n): > > return [[(j,i,i*j) for i in range(1,m+1)] for j in range(1,n+1)] > > > > def prettyrow(r): > > return joinw(['%d*%d=%d' % ele for ele in r],'\t') > > > > def prettytable(t): > > return joinw([prettyrow(r) for r in t],'\n') > > Wait a minute! Isn't the most nature way of doing/thinking "generating > 9x9 multiplication table" two nested loop? Thats like saying that the most natur(al) way of using a car is to attach a horse to it. Sure if all you've seen are horse-carriages then a horseless carriage will seem nonsensical. Once you get used to horseless carriages the horsed ones would seem quite a nuisance [global warming aside!!] The only problem with this analogy is that you have to imagine the world of horse/horseless 'cars' in 1900 and not 2013. Likewise in the world of programming, 90% of programmers think imperative/OO programming is natural while functional programming is strange. Just wait 10 years and see if things are not drastically different! > > I understand that using "join", we don't need to worry about "one > element doesn't need white space" issue. And that's it. More evidence of the above! join like list-comprehensions are tools for functional programming and not merely ways of writing loops in short. The key difference between the two is seen not in the code you write but in the natural language (English) you use to describe it: > Wait a minute! Isn't the most nature way of doing/thinking "generating > 9x9 multiplication table" two nested loop? You associate the primal (f)act of thinking about programming with *doing* the generating. By contrast the functional programmer thinks about what *is* the result. Please also note the difference in emphasis in your code and mine. Rewriting to make it more obvious: # The main multiplication table building function def multtable(m,n): return [[(j,i,i*j) for i in range(1,m+1)] for j in range(1,n+1)] # The 'icing' to get it into the shape you want def joinw(l,sep): return sep.join(l) def prettytable(t): return joinw([joinw(['%d*%d=%d' % ele for ele in r],'\t') for r in t],'\n') The call that puts them together: >>> print prettytable(multtable(6,7)) The nice thing about the last is that it separates three things: 1. Computing the actual table 2. The string form of that table that looks the way we want 3. Printing that string And I can see each of these separately: In [2]: multtable(2,3) Out[2]: [[(1, 1, 1), (1, 2, 2)], [(2, 1, 2), (2, 2, 4)], [(3, 1, 3), (3, 2, 6)]] In [3]: prettytable(_) Out[3]: '1*1=1\t1*2=2\n2*1=2\t2*2=4\n3*1=3\t3*2=6' In [4]: print _ 1*1=1 1*2=2 2*1=2 2*2=4 3*1=3 3*2=6 ------------------- I would be pleasantly surprised if the double nested loop you think natural, allowed for such a modular separation! From rosuav at gmail.com Thu May 30 13:28:10 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 03:28:10 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> Message-ID: On Fri, May 31, 2013 at 3:12 AM, rusi wrote: > You associate the primal (f)act of thinking about programming with > *doing* the generating. > By contrast the functional programmer thinks about what *is* the > result. I wish you'd explain that to my boss :) He often has trouble understanding why sometimes I put two syntactic statements on one line, such as: for (int i=0;i References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> Message-ID: On Fri, May 31, 2013 at 1:28 AM, Chris Angelico wrote: > for (int i=0;i { > //do something with foo[i] > } This is interesting! From rosuav at gmail.com Thu May 30 13:50:06 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 03:50:06 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> Message-ID: On Fri, May 31, 2013 at 3:46 AM, Ma Xiaojun wrote: > On Fri, May 31, 2013 at 1:28 AM, Chris Angelico wrote: >> for (int i=0;i> { >> //do something with foo[i] >> } > > This is interesting! Yeah, but that's C++. It won't work in Python without this directive: from __future__ import braces ChrisA From damage3025 at gmail.com Thu May 30 13:50:58 2013 From: damage3025 at gmail.com (Ma Xiaojun) Date: Fri, 31 May 2013 01:50:58 +0800 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> Message-ID: functional VS imperative? mechanical thinking VS mathematical thinking? Sounds interesting. From rustompmody at gmail.com Thu May 30 13:59:13 2013 From: rustompmody at gmail.com (rusi) Date: Thu, 30 May 2013 10:59:13 -0700 (PDT) Subject: How clean/elegant is Python's syntax? References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> Message-ID: <034f2ffa-6bb3-453a-a0f2-b90ecafcda1e@qn4g2000pbc.googlegroups.com> On May 30, 10:28?pm, Chris Angelico wrote: > On Fri, May 31, 2013 at 3:12 AM, rusi wrote: > > You associate the primal (f)act of thinking about programming with > > *doing* the generating. > > By contrast the functional programmer thinks about what *is* the > > result. > > I wish you'd explain that to my boss :) He often has trouble > understanding why sometimes I put two syntactic statements on one > line, such as: > > for (int i=0;i { > ? ? //do something with foo[i] > > } > > In Python, that would probably be done with a list comprehension or > some other form of filtered iteration, and is to my mind a single > operation - "iterate over all the marked foo" is just as much a valid > loop header as "iterate over all the foo". This is a simple example, > and what you say about thinking about what *is* the result doesn't > really translate well into a C++ example, but the broader concept > applies: there's a difference between code as the compiler/interpreter > sees it and code as the programmer sees it, and there is not always a > 1:1 correspondence of statements. > > ChrisA I had a blog post about line-length in programs http://blog.languager.org/2012/10/layout-imperative-in-functional.html followed by an interesting discussion on the haskell mailing list http://groups.google.com/group/haskell-cafe/browse_thread/thread/f146ec7753c5db56/09eb73b1efe79fec The comment by Alexander Solla was insightful and is probably what you are saying. [Probably!! I am not sure what you are saying!] From rosuav at gmail.com Thu May 30 14:22:50 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 04:22:50 +1000 Subject: How clean/elegant is Python's syntax? In-Reply-To: <034f2ffa-6bb3-453a-a0f2-b90ecafcda1e@qn4g2000pbc.googlegroups.com> References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> <034f2ffa-6bb3-453a-a0f2-b90ecafcda1e@qn4g2000pbc.googlegroups.com> Message-ID: On Fri, May 31, 2013 at 3:59 AM, rusi wrote: > On May 30, 10:28 pm, Chris Angelico wrote: >> On Fri, May 31, 2013 at 3:12 AM, rusi wrote: >> > You associate the primal (f)act of thinking about programming with >> > *doing* the generating. >> > By contrast the functional programmer thinks about what *is* the >> > result. >> >> I wish you'd explain that to my boss :) He often has trouble >> understanding why sometimes I put two syntactic statements on one >> line, such as: >> >> for (int i=0;i> { >> //do something with foo[i] >> >> } >> >> In Python, that would probably be done with a list comprehension or >> some other form of filtered iteration, and is to my mind a single >> operation - "iterate over all the marked foo" is just as much a valid >> loop header as "iterate over all the foo". This is a simple example, >> and what you say about thinking about what *is* the result doesn't >> really translate well into a C++ example, but the broader concept >> applies: there's a difference between code as the compiler/interpreter >> sees it and code as the programmer sees it, and there is not always a >> 1:1 correspondence of statements. >> >> ChrisA > > I had a blog post about line-length in programs > http://blog.languager.org/2012/10/layout-imperative-in-functional.html > > followed by an interesting discussion on the haskell mailing list > http://groups.google.com/group/haskell-cafe/browse_thread/thread/f146ec7753c5db56/09eb73b1efe79fec > > The comment by Alexander Solla was insightful and is probably what you > are saying. > > [Probably!! I am not sure what you are saying!] Unfortunately a lot of your code specifics don't mean much to me because I don't speak Haskell, but you are making several similar points. A line of code should not be defined by the language's syntax, but by the programmer's intention. A Python example might be: for x in filter(lambda x: x%5 and x%6,range(40)): # do something with the numbers that don't count by 5 or 6 Stupid example, but it still puts the conditional inside the loop header. I'm sure you can come up with a more useful case! ChrisA From steve+comp.lang.python at pearwood.info Thu May 30 15:36:29 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 May 2013 19:36:29 GMT Subject: How clean/elegant is Python's syntax? References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> Message-ID: <51a7aa3c$0$29966$c3e8da3$5496439d@news.astraweb.com> On Thu, 30 May 2013 10:12:22 -0700, rusi wrote: > On Thu, May 30, 2013 at 9:34 AM, Ma Xiaojun > wrote: >> Wait a minute! Isn't the most nature way of doing/thinking "generating >> 9x9 multiplication table" two nested loop? > > Thats like saying that the most natur(al) way of using a car is to > attach a horse to it. >[...] > Likewise in the world of programming, 90% of programmers think > imperative/OO programming is natural while functional programming is > strange. Just wait 10 years and see if things are not drastically > different! It won't be. Functional programming goes back to Lisp, which is nearly as old as Fortran and older than Cobol. There have been many decades for functional languages to become mainstream, but they've never quite done it. There's no reason to think that the next decade will see a change to this. That's not to say that functional programming isn't one of the big three programming paradigms. But it's third out of the three, and quite a bit behind the other two: Procedural OOP Functional with Logic programming a distant fourth. Well, perhaps even third, ahead of Functional, taking into account that SQL is a form of Logic programming. Some modern Functional languages are really neat, like Haskell, but I think the sad truth is that to really master them (and not just make do with a small percentage of their functionality) is beyond 90% of programmers. I'm not ashamed to admit that I struggle with advanced FP concepts. But even if only a minority of programmers can master languages like Lisp, Haskell, or Scheme, doesn't mean that *all* programmers can't learn something from them. Functional programming is at least 50% a philosophy: * pass arguments to functions, and return results, rather than getting and setting state from a variable. This is a good strategy: it makes it easier to reason about the code, easier to document, easier to test, and makes it practical to use it in threaded code. -- Steven From rustompmody at gmail.com Thu May 30 22:59:21 2013 From: rustompmody at gmail.com (rusi) Date: Thu, 30 May 2013 19:59:21 -0700 (PDT) Subject: How clean/elegant is Python's syntax? References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> <503a1a1e-9c5c-4c01-929b-673421907ab8@ys5g2000pbc.googlegroups.com> <51a7aa3c$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On May 31, 12:36?am, Steven D'Aprano wrote: > > But even if only a minority of programmers can master languages like > Lisp, Haskell, or Scheme, doesn't mean that *all* programmers can't learn > something from them. Functional programming is at least 50% a philosophy: > > * pass arguments to functions, and return results, rather than getting > and setting state from a variable. Yes that's the sense in which I am using the term. More such ideas of FP that can be used by all sorts of programmers across the board (not just FPers) is here: http://blog.languager.org/2012/10/functional-programming-lost-booty.html > On Thu, 30 May 2013 10:12:22 -0700, rusi wrote: > > On Thu, May 30, 2013 at 9:34 AM, Ma Xiaojun > > wrote: > >> Wait a minute! Isn't the most nature way of doing/thinking "generating > >> 9x9 multiplication table" two nested loop? > > > Thats like saying that the most natur(al) way of using a car is to > > attach a horse to it. > >[...] > > Likewise in the world of programming, 90% of programmers think > > imperative/OO programming is natural while functional programming is > > strange. ?Just wait 10 years and see if things are not drastically > > different! > > It won't be. Functional programming goes back to Lisp, which is nearly as > old as Fortran and older than Cobol. There have been many decades for > functional languages to become mainstream, but they've never quite done > it. There's no reason to think that the next decade will see a change to > this. Depends on how you define your terms. Comprehensions and lambdas have come into python. From where? [Lambdas have even got into C++ !!] Also LINQ in C# is inspired by comprehensions Generics were not there in C# and Java early editions. Now they've been retrofitted -- Origin SML. Almost every modern language supports garbage collection. Origin Lisp Numpy is a rip-off of APL. [Ripping off is a tribute. Non- acknowledgement is sad...] TAOCP -Vol 1 is a gigantic exercise on how to do lisp without lisp. Also called Greenspun's 10th law: http://c2.com/cgi/wiki?GreenspunsTenthRuleOfProgramming > Some modern Functional languages are really neat, like Haskell, but I > think the sad truth is that to really master them (and not just make do > with a small percentage of their functionality) is beyond 90% of > programmers. I'm not ashamed to admit that I struggle with advanced FP > concepts. Yes Haskell is HARD. Gets harder with each new major addition. Which is why I suggest using Haskell core (ie minus most fancy features) as an *ideology* rather than as a *technology*. Which is what I am suggesting in my course proposal https://moocfellowship.org/submissions/the-dance-of-functional-programming-languaging-with-haskell-and-python "Think with Haskell --- Code into Python" Want to contribute ? Well actually the last applies to anyone who is interested -- suggestions welcome!! From torriem at gmail.com Thu May 30 19:30:34 2013 From: torriem at gmail.com (Michael Torrie) Date: Thu, 30 May 2013 17:30:34 -0600 Subject: How clean/elegant is Python's syntax? In-Reply-To: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> References: <51a6ef42$0$11118$c3e8da3@news.astraweb.com> Message-ID: <51A7E11A.6080700@gmail.com> On 05/30/2013 12:18 AM, Steven D'Aprano wrote: > In some ways, Python is a more pure OOP language than Java: everything in > Python is an object, including classes themselves. > > In other ways, Python is a less pure and more practical language. You > don't have to wrap every piece of functionality in a class. Python > encourages you to write mixed procedural, functional and object oriented > code, whatever is best for the problem you are trying to solve, which is > very much in contrast to Java. Depending on your understanding of what object-oriented means, procedural and functional code is still object-oriented. In fact modules (the "file") are in essence singleton objects that define attributes, but in practice there can only be one instance of this object (module). Seems like in Java a lot of code is needed to implement singletons (factory, etc). module-level code (procedural code) could simply be thought of as singleton initialization/constructor code that's automatically run when the singleton is created, using import or __import__(). At the function level, a simple function is still an object that implements callable. Python's implementation of OO isn't quite smalltalk pure, but it is much more consistent than in Java or C++. But yes, Python does not force you to shoe-horn your programming into one particular pattern. From rantingrickjohnson at gmail.com Thu May 30 19:15:50 2013 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Thu, 30 May 2013 16:15:50 -0700 (PDT) Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: <259c8152-395f-45ce-9912-45ea80d81f65@googlegroups.com> On Wednesday, May 29, 2013 7:24:48 PM UTC-5, Dan Stromberg wrote: > About the only thing I don't like is: > > ?? var = 1, > > That binds var to a tuple (singleton) value, instead of 1. I don't understand why Python needs tuples anyway; at least not tuple literals!. I mean, i like the idea of a sequence type that is immutable as much as the next fella, however, i just hate the fact that we had to pay for this type with syntactical multiplicity only to be forever a slave to it's resulting quirkiness! Psst: Guido, i think you got screwed! (And they thought Jack was a fool for acquiring those beans!) With the demand for type literals growing all the larger as time goes on and the ASCII char set remaining static, there must be a better way! (And no folks, I'm not suggesting space cadet keyboards!) From alister.ware at ntlworld.com Fri May 31 05:08:43 2013 From: alister.ware at ntlworld.com (Alister) Date: Fri, 31 May 2013 09:08:43 GMT Subject: How clean/elegant is Python's syntax? Message-ID: On Thu, 30 May 2013 20:38:40 +0100, MRAB wrote: > On 30/05/2013 19:44, Chris Angelico wrote: >> On Fri, May 31, 2013 at 4:36 AM, Ian Kelly >> wrote: >>> On Wed, May 29, 2013 at 8:49 PM, rusi wrote: >>>> On May 30, 6:14 am, Ma Xiaojun wrote: >>>>> What interest me is a one liner: >>>>> print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in >>>>> range(1,10)]) for j in range(1,10)]) >>>> >>>> Ha,Ha! The join method is one of the (for me) ugly features of >>>> python. >>>> You can sweep it under the carpet with a one-line join function and >>>> then write clean and pretty code: >>>> >>>> #joinwith def joinw(l,sep): return sep.join(l) >>> >>> I don't object to changing the join method (one of the more >>> shoe-horned string methods) back into a function, but to my eyes >>> you've got the arguments backward. It should be: >>> >>> def join(sep, iterable): return sep.join(iterable) >> >> Trouble is, it makes some sense either way. I often put the larger >> argument first - for instance, I would write 123412341324*5 rather than >> the other way around - and in this instance, it hardly seems as >> clear-cut as you imply. But the function can't be written to take them >> in either order, because strings are iterable too. (And functions that >> take args either way around aren't better than those that make a >> decision.) >> > And additional argument (pun not intended) for putting sep second is > that you can give it a default value: > > def join(iterable, sep=""): return sep.join(iterable) I think that is the winning argument. Next question is what should be the default ("", " " or',')? -- Nasrudin walked into a teahouse and declaimed, "The moon is more useful than the sun." "Why?", he was asked. "Because at night we need the light more." From fabiosantosart at gmail.com Fri May 31 06:16:14 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Fri, 31 May 2013 11:16:14 +0100 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: On Fri, May 31, 2013 at 10:08 AM, Alister wrote: > I think that is the winning argument. > Next question is what should be the default ("", " " or',')? join, comma_join, whitejoin, linejoin variants, with different defaults? -- F?bio Santos From ian.g.kelly at gmail.com Fri May 31 11:39:58 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 31 May 2013 09:39:58 -0600 Subject: How clean/elegant is Python's syntax? In-Reply-To: References: Message-ID: On Fri, May 31, 2013 at 4:16 AM, F?bio Santos wrote: > On Fri, May 31, 2013 at 10:08 AM, Alister wrote: >> I think that is the winning argument. >> Next question is what should be the default ("", " " or',')? > > join, comma_join, whitejoin, linejoin variants, with different defaults? The more specific versions should not even have the parameter as an argument that can be supplied. Otherwise you could do: comma_join(words, sep=';') which is just unclear, and there is no reason to allow it. From rustompmody at gmail.com Fri May 31 06:27:52 2013 From: rustompmody at gmail.com (rusi) Date: Fri, 31 May 2013 03:27:52 -0700 (PDT) Subject: How clean/elegant is Python's syntax? References: Message-ID: <51ff2721-d27e-466e-83e4-9ecc877d5583@fq2g2000pbb.googlegroups.com> On May 31, 2:08?pm, Alister wrote: > On Thu, 30 May 2013 20:38:40 +0100, MRAB wrote: > > And additional argument (pun not intended) for putting sep second is > > that you can give it a default value: > > > ? ? def join(iterable, sep=""): return sep.join(iterable) > > I think that is the winning argument. Yes > Next question is what should be the default ("", " " or',')? Hmm... Never thought there was any choice here except "". Yes can see the case for each. From alister.ware at ntlworld.com Fri May 31 07:52:11 2013 From: alister.ware at ntlworld.com (Alister) Date: Fri, 31 May 2013 11:52:11 GMT Subject: How clean/elegant is Python's syntax? References: <51ff2721-d27e-466e-83e4-9ecc877d5583@fq2g2000pbb.googlegroups.com> Message-ID: On Fri, 31 May 2013 03:27:52 -0700, rusi wrote: > On May 31, 2:08?pm, Alister wrote: >> On Thu, 30 May 2013 20:38:40 +0100, MRAB wrote: >> > And additional argument (pun not intended) for putting sep second is >> > that you can give it a default value: >> >> > ? ? def join(iterable, sep=""): return sep.join(iterable) >> >> I think that is the winning argument. > > Yes > >> Next question is what should be the default ("", " " or',')? > > Hmm... Never thought there was any choice here except "". Yes can see > the case for each. to be fair "" is probably the most sensible although in my programs most joins are using ',' -- We are governed not by armies and police but by ideas. -- Mona Caird, 1892 From mattjamesfrancis at gmail.com Thu May 30 00:27:54 2013 From: mattjamesfrancis at gmail.com (Matthew Francis) Date: Wed, 29 May 2013 21:27:54 -0700 (PDT) Subject: Building a HPC data assimilation system using Python? Message-ID: <676e9ba9-c54a-404a-8602-fa078cf19c32@googlegroups.com> I have a prototype data assimilation code ( an ionospheric nowcast/forecast model driven by GPS data ) that is written in IDL (interactive data language) which is a horrible language choice for scaling the application up to large datasets as IDL is serial and slow (interpreted). I am embarking on a project to convert this prototype into an operational parallel HPC code. In the past I've used C++ for this kind of project and am comfortable using MPI. On the other hand, I've recently started using python and appreciate the flexibility and speed of development using python compared with C++. I have read that there is a trend to use python as the high level 'glue' for these kind of large number crunching projects, so it would seem appropriate to go down that path. There are a number of C++ and FORTRAN(!) libraries I'd need to incorporate that handle things such as the processing of raw GPS data and computing ionospheric models, so I'd need to be able to make the appropriate interface for these into python. If anyone uses python is this way, I'd appreciate any tips, hints, things to be careful about and in general any war stories you can relate that you wish you'd heard before making some mistake. Here are the things I have investigated that it looks like I'd probably need to use: * scipy/numpy/matplotlib * Cython (or pyrex?) for speeding up any bottlenecks that occur in python code (as opposed to C++/FORTRAN libraries) * MPI for Python (mpi4py). Does this play nice with Cython? * Something to interface python with other language libraries. ctypes, swig, boost? Which would be best for this application? * Profiling. profile/cprofile are straightforward to use, but how do they cope with a parallel (mpi4py) code? * If a C++ library call has its own MPI calls, does that work smoothly with mpi4py operating in the python part of the code? Sorry if some of this is a little basic, I'm trying to get up to speed on this a quick as I can. Thanks in advance! From carlosnepomuceno at outlook.com Thu May 30 18:34:26 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 31 May 2013 01:34:26 +0300 Subject: Building a HPC data assimilation system using Python? In-Reply-To: <676e9ba9-c54a-404a-8602-fa078cf19c32@googlegroups.com> References: <676e9ba9-c54a-404a-8602-fa078cf19c32@googlegroups.com> Message-ID: Hi Matthew! I'm on a similar quest! I'm still learning the basics of Python so I may not be a good source of information. I'm reading a lot of stuff about how to use Python for the parallelization of code and data and found BSP[1] to be very interesting and perhaps worth the time to learn it! ;) [1] http://www.multicorebsp.com/ ---------------------------------------- > Date: Wed, 29 May 2013 21:27:54 -0700 > Subject: Building a HPC data assimilation system using Python? > From: mattjamesfrancis at gmail.com > To: python-list at python.org > > I have a prototype data assimilation code ( an ionospheric nowcast/forecast model driven by GPS data ) that is written in IDL (interactive data language) which is a horrible language choice for scaling the application up to large datasets as IDL is serial and slow (interpreted). > > I am embarking on a project to convert this prototype into an operational parallel HPC code. In the past I've used C++ for this kind of project and am comfortable using MPI. On the other hand, I've recently started using python and appreciate the flexibility and speed of development using python compared with C++. I have read that there is a trend to use python as the high level 'glue' for these kind of large number crunching projects, so it would seem appropriate to go down that path. There are a number of C++ and FORTRAN(!) libraries I'd need to incorporate that handle things such as the processing of raw GPS data and computing ionospheric models, so I'd need to be able to make the appropriate interface for these into python. > > If anyone uses python is this way, I'd appreciate any tips, hints, things to be careful about and in general any war stories you can relate that you wish you'd heard before making some mistake. > > Here are the things I have investigated that it looks like I'd probably need to use: > > * scipy/numpy/matplotlib > * Cython (or pyrex?) for speeding up any bottlenecks that occur in python code (as opposed to C++/FORTRAN libraries) > * MPI for Python (mpi4py). Does this play nice with Cython? > * Something to interface python with other language libraries. ctypes, swig, boost? Which would be best for this application? > * Profiling. profile/cprofile are straightforward to use, but how do they cope with a parallel (mpi4py) code? > * If a C++ library call has its own MPI calls, does that work smoothly with mpi4py operating in the python part of the code? > > Sorry if some of this is a little basic, I'm trying to get up to speed on this a quick as I can. > > Thanks in advance! > -- > http://mail.python.org/mailman/listinfo/python-list From bhk755 at gmail.com Thu May 30 05:48:54 2013 From: bhk755 at gmail.com (bhk755 at gmail.com) Date: Thu, 30 May 2013 02:48:54 -0700 (PDT) Subject: Can anyone please help me in understanding the following python code Message-ID: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Code : ----- def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] mergeSort(lefthalf) mergeSort(righthalf) i=0 j=0 k=0 while i References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: On Thu, May 30, 2013 at 7:48 PM, wrote: > Function mergeSort is called only once, but it is getting recursively executed after the printing the last statement "print("Merging ",alist)". But don't recursion taking place except at these places "mergeSort(lefthalf), mergeSort(righthalf)" > > Sometimes the function execution directly starts from i=0,j=0,k=0 . Not sure how. When it says "Splitting" with a single-element list, it then immediately prints "Merging" and returns (because all the rest of the code is guarded by the 'if'). Execution then continues where it left off, in the parent. One good way to get an idea of what's going on is to add a recursion depth parameter. Keep all the rest of the code the same, but change the print and recursion lines to be like this: def mergeSort(alist,depth): print("%*cSplitting %r"%(depth*2,' ',alist)) # ... mergeSort(lefthalf,depth+1) mergeSort(righthalf,depth+1) # ... print("%*cMerging %r"%(depth*2,' ',alist)) mergeSort(alist,0) That'll indent everything according to the depth of the call stack. (Also, I changed your 'print's to be compatible with Python 2 and Python 3; you seemed to have Python 3 style function calls and a Python 2 interpreter.) Hopefully that'll make clearer what's going on! ChrisA From wolfgang.maier at biologie.uni-freiburg.de Thu May 30 06:17:11 2013 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Thu, 30 May 2013 10:17:11 +0000 (UTC) Subject: Can anyone please help me in understanding the following python code References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: gmail.com> writes: > > > Function mergeSort is called only once, but it is getting recursively executed after the printing the last > statement "print("Merging ",alist)". But don't recursion taking place except at these places > "mergeSort(lefthalf), mergeSort(righthalf)" > > Sometimes the function execution directly starts from i=0,j=0,k=0 . Not sure how. Maybe you should tell us first what output you would have expected. It's not really clear from your question what confuses you. Doesn't the algorithm do what it's supposed to do? When does the function start from i=0, j=0, k=0 ? What exactly is the output then ? Maybe you are confused by the fact that the algorithm is recursive twice. It first goes through the lefthand branch, then only on its way out it works on the righthand branch. So the recursion through mergeSort is not started only once as you say, but twice (after finishing the recursion through mergeSort(lefthand), another recursion is kicked off by calling mergeSort(righthand). Best, Wolfgang From bhk755 at gmail.com Thu May 30 06:19:59 2013 From: bhk755 at gmail.com (bhk755 at gmail.com) Date: Thu, 30 May 2013 03:19:59 -0700 (PDT) Subject: Can anyone please help me in understanding the following python code In-Reply-To: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: Thanks for the reply Chris. I am newbie to python, so please excuse me if I am asking chilly questions. Can you please explain more about the following sentence. "When it says "Splitting" with a single-element list, it then immediately prints "Merging" and returns (because all the rest of the code is guarded by the 'if'). Execution then continues where it left off, in the parent." Because I am not sure how the control can go back to top of the function unless there is no loops there. Also, Can you please let me know how did you found out that I am using Python 2 Interpreter. Bharath From wolfgang.maier at biologie.uni-freiburg.de Thu May 30 06:43:52 2013 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Thu, 30 May 2013 10:43:52 +0000 (UTC) Subject: Can anyone please help me in understanding the following =?utf-8?b?cHl0aG9uCWNvZGU=?= References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: gmail.com> writes: > > Thanks for the reply Chris. > > I am newbie to python, so please excuse me if I am asking chilly questions. > > Can you please explain more about the following sentence. > "When it says "Splitting" with a single-element list, it then > immediately prints "Merging" and returns (because all the rest of the > code is guarded by the 'if'). Execution then continues where it left > off, in the parent." > > Because I am not sure how the control can go back to top of the function unless there is no loops there. > It doesn't. The function simply returns and execution resumes in the parent, i.e. in the calling function at depth-1, where it left off. In Python, a return None is implied when a function falls off its end. > Also, Can you please let me know how did you found out that I am using Python 2 Interpreter. print as a statement (without parentheses) only works in Python 2, in Python 3 print is a function. > > Bharath > > From rosuav at gmail.com Thu May 30 06:47:22 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 20:47:22 +1000 Subject: Can anyone please help me in understanding the following python code In-Reply-To: References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: On Thu, May 30, 2013 at 8:19 PM, wrote: > Thanks for the reply Chris. > > I am newbie to python, so please excuse me if I am asking chilly questions. All questions are welcome! > Can you please explain more about the following sentence. > "When it says "Splitting" with a single-element list, it then > immediately prints "Merging" and returns (because all the rest of the > code is guarded by the 'if'). Execution then continues where it left > off, in the parent." The code goes like this: 1) Print "Splitting" 2) If there's more than one element in the list: 2a) Divide the list in half 2b) Call self on each half 2c) Merge 3) Print "Merging In step 2b, all the steps from 1 through 3 are executed again (twice). Soon, those calls will just output "Splitting" followed by "Merging"; and then we go back to 2c. That's why it *seems* that the code goes from 3 to 2c. You'll notice that the call depth decreases when this happens. > Because I am not sure how the control can go back to top of the function unless there is no loops there. > > Also, Can you please let me know how did you found out that I am using Python 2 Interpreter. That one is actually based on my crystal ball. Here on python-list, we issue them to all our top respondents; they're extremely useful. I'll let you in on part of the secret of how this works, though. In Python 2, 'print' is (by default) a statement. When you give it something in parentheses, it sees a tuple: print("Splitting ",alist) ('Splitting ', [54, 26, 93, 17, 77, 31, 44, 55, 20]) In Python 3, 'print' is a function. It takes arguments, and prints out each argument, separated by spaces. print("Splitting ",alist) Splitting [17, 20, 26, 31, 44, 54, 55, 77, 93] You can make Python 2 behave the same way by putting this at the top of your program: from __future__ import print_function ChrisA From joshua.landau.ws at gmail.com Thu May 30 07:11:38 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 30 May 2013 12:11:38 +0100 Subject: Can anyone please help me in understanding the following python code In-Reply-To: References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: On 30 May 2013 11:19, wrote: > Also, Can you please let me know how did you found out that I am using Python 2 Interpreter. Do you have access to a Python3 interpreter? If so, try running it and your output will look like: Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] Splitting [54, 26, 93, 17] Splitting [54, 26] Splitting [54] Merging [54] Splitting [26] Merging [26] ... BLAH BLAH BLAH Which is obviously much nicer. This is how Chris knew the code was written for Python3. Therefore I would suggest you use Python3 - not all code runs on both! The "proper" way to write the prints for Python2 is without the brackets: print "Merging ",alist But the methods Chris and I have used work the same on both so are probably better in this case. They're more complicated, though. From joshua.landau.ws at gmail.com Thu May 30 07:03:22 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 30 May 2013 12:03:22 +0100 Subject: Can anyone please help me in understanding the following python code In-Reply-To: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: On 30 May 2013 10:48, wrote: > Question: > --------- > Function mergeSort is called only once, but it is getting recursively executed after the printing the last statement "print("Merging ",alist)". But don't recursion taking place except at these places "mergeSort(lefthalf), mergeSort(righthalf)" > > Sometimes the function execution directly starts from i=0,j=0,k=0 . Not sure how. Here's some different code; it does the same thing but in a less weird way: ########################## def mergeSort(alist, depth=1): # If the piece we have to sort is [i] or [], then we have no sorting to do if len(alist) <= 1: # Returning what we were given # Make a new list from it, though, because we are nice print("{padding}return {list}\n".format(padding=" "*depth*8, list=alist)) return alist[:] # Split along the middle mid = len(alist)//2 # We have two halves lefthalf = alist[:mid] righthalf = alist[mid:] # Which we sort, so we have two sorted halves print("{padding}lefthalf = mergesort({list})\n".format(padding=" "*depth*8, list=lefthalf)) lefthalf = mergeSort(lefthalf, depth+1) print("{padding}righthalf = mergesort({list})\n".format(padding=" "*depth*8, list=righthalf)) righthalf = mergeSort(righthalf, depth+1) # We want to return a sorted "alist" from our two lists # We'll add the items to here new_list = [] # We'll go through adding the smallest to new_list while lefthalf and righthalf: # Lefthalf has the smaller item, so we "pop" it off into new_list if lefthalf[0] < righthalf[0]: new_list.append(lefthalf.pop(0)) # Righthalf has the smaller item, so we "pop" it off into new_list else: new_list.append(righthalf.pop(0)) # One of our lists isn't empty, so just add them on new_list += lefthalf + righthalf print("{padding}return {list}\n".format(padding=" "*depth*8, list=new_list)) return new_list print("Start mergesort({list}):\n".format(list=[2, 4, 0, 1])) sorted_list = mergeSort([2, 4, 0, 1]) print("Our final result: {list}".format(list=sorted_list)) ########################## And it gives ########################## Start mergesort([2, 4, 0, 1]): lefthalf = mergesort([2, 4]) lefthalf = mergesort([2]) return [2] righthalf = mergesort([4]) return [4] return [2, 4] righthalf = mergesort([0, 1]) lefthalf = mergesort([0]) return [0] righthalf = mergesort([1]) return [1] return [0, 1] return [0, 1, 2, 4] Our final result: [0, 1, 2, 4] ########################## Hopefully this code is a little easier to understand - the original changes the list while it is running the algorithm and mine makes new lists. The algorithm is very similar, and what you learn applies to both. You can see in the output (thanks Chris Angelico) that the main function is always running. It runs *two* other instances: lefthalf and righthalf. So the "mergesort([2, 4, 0, 1])" runs "lefthalf = mergesort([2, 4])" "mergesort([2, 4])" runs "lefthalf = mergesort([2])" "mergesort([2])" gives back [2] to "mergesort([2, 4])" "mergesort([2, 4])" goes "OH! I can continue now" and runs "righthalf = mergesort([4])" "mergesort([4])" gives back [4] to "mergesort([2, 4])" "mergesort([2, 4])" goes "OH! I can continue now" and gives back [2, 4] to "mergesort([2, 4, 0, 1])" "mergesort([2, 4, 0, 1])" goes "OH! I can continue now" and runs "righthalf = mergesort([0, 1])" "mergesort([0, 1])" runs "lefthalf = mergesort([0])" "mergesort([0])" gives back [0] to "mergesort([0, 1])" "mergesort([0, 1])" goes "OH! I can continue now" and runs "righthalf = mergesort([1])" "mergesort([1])" gives back [1] to "mergesort([0, 1])" "mergesort([0, 1])" goes "OH! I can continue now" and gives back [0, 1] to "mergesort([2, 4, 0, 1])" "mergesort([2, 4, 0, 1])" goes "OH! I can continue now" and gives back [0, 1, 2, 4] DONE. Does that help you see the flow? Exactly the same flow happens for your code. The difference is that instead of returning a list, mergesort *sorts* a list, and then that is used to replace items in the original list. Personally, their way is a little silly (and mine is inefficient, so use neither). Question: Why did you rewrite the code? Answer: Revising is boring. Question: Sometimes the function execution directly starts from i=0,j=0,k=0 . Not sure how. Answer: That's not a question. Anyhow: i, j and k are LOCAL to a function. "mergesort([2, 4, 0, 1])" has a different i, j and k than "mergesort([0, 1])", They never use each other's i, j and k. Hence for each recursion they run "i=0, j=0, k=0" and they are set to 0 within the function. Does this help? From bhk755 at gmail.com Thu May 30 08:39:20 2013 From: bhk755 at gmail.com (bhk755 at gmail.com) Date: Thu, 30 May 2013 05:39:20 -0700 (PDT) Subject: Can anyone please help me in understanding the following python code In-Reply-To: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> Thanks Chris, Wolfgang and Joshua for your replies. --- In step 2b, all the steps from 1 through 3 are executed again (twice). Soon, those calls will just output "Splitting" followed by "Merging"; and then we go back to 2c. That's why it *seems* that the code goes from 3 to 2c. You'll notice that the call depth decreases when this happens Chris, Can you please let me know what makes the control of the program code go to 2c after the output "Merging". Also, please look into the following output, before calling main mergesort Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] left half before split [54, 26, 93, 17] right half before split [77, 31, 44, 55, 20] Splitting [54, 26, 93, 17] left half before split [54, 26] right half before split [93, 17] Splitting [54, 26] left half before split [54] right half before split [26] Splitting [54] Merging [54] Splitting [26] Merging [26] HERE AFTER SPLIT left half after split [54] right half after split [26] Merging [26, 54] Splitting [93, 17] left half before split [93] right half before split [17] Splitting [93] Merging [93] Splitting [17] Merging [17] HERE AFTER SPLIT left half after split [93] right half after split [17] Merging [17, 93] HERE AFTER SPLIT left half after split [26, 54] right half after split [17, 93] Merging [17, 26, 54, 93] Splitting [77, 31, 44, 55, 20] left half before split [77, 31] right half before split [44, 55, 20] Splitting [77, 31] left half before split [77] right half before split [31] Splitting [77] Merging [77] Splitting [31] Merging [31] HERE AFTER SPLIT left half after split [77] right half after split [31] Merging [31, 77] Splitting [44, 55, 20] left half before split [44] right half before split [55, 20] Splitting [44] Merging [44] Splitting [55, 20] left half before split [55] right half before split [20] Splitting [55] Merging [55] Splitting [20] Merging [20] HERE AFTER SPLIT left half after split [55] right half after split [20] Merging [20, 55] HERE AFTER SPLIT left half after split [44] right half after split [20, 55] Merging [20, 44, 55] HERE AFTER SPLIT left half after split [31, 77] right half after split [20, 44, 55] Merging [20, 31, 44, 55, 77] HERE AFTER SPLIT left half after split [17, 26, 54, 93] right half after split [20, 31, 44, 55, 77] Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] after calling main mergesort [17, 20, 26, 31, 44, 54, 55, 77, 93] ----------------------------- In the above output, the control goes to "HERE AFTER SPLIT" after the "Merging" statement which is of-course the last statement in the function.On what condition this is happening. Ideally as per my understanding, after the last statement of a function the control should come out of the function. Please correct me if I am wrong here. There is something with respect-to functions in python that I am not able to understand. From bhk755 at gmail.com Thu May 30 08:42:33 2013 From: bhk755 at gmail.com (bhk755 at gmail.com) Date: Thu, 30 May 2013 05:42:33 -0700 (PDT) Subject: Can anyone please help me in understanding the following python code In-Reply-To: <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> Message-ID: On Thursday, May 30, 2013 6:09:20 PM UTC+5:30, bhk... at gmail.com wrote: > Thanks Chris, Wolfgang and Joshua for your replies. > > > > --- > > In step 2b, all the steps from 1 through 3 are executed again (twice). > > Soon, those calls will just output "Splitting" followed by "Merging"; > > and then we go back to 2c. That's why it *seems* that the code goes > > from 3 to 2c. You'll notice that the call depth decreases when this > > happens > > > > Chris, Can you please let me know what makes the control of the program code go to 2c after the output "Merging". > > Also, please look into the following output for the same program with more print statements, > ---------------------------------------------- > before calling main mergesort > > Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] > > left half before split [54, 26, 93, 17] > > right half before split [77, 31, 44, 55, 20] > > Splitting [54, 26, 93, 17] > > left half before split [54, 26] > > right half before split [93, 17] > > Splitting [54, 26] > > left half before split [54] > > right half before split [26] > > Splitting [54] > > Merging [54] > > Splitting [26] > > Merging [26] > > > > HERE AFTER SPLIT > > > > left half after split [54] > > right half after split [26] > > Merging [26, 54] > > Splitting [93, 17] > > left half before split [93] > > right half before split [17] > > Splitting [93] > > Merging [93] > > Splitting [17] > > Merging [17] > > > > HERE AFTER SPLIT > > > > left half after split [93] > > right half after split [17] > > Merging [17, 93] > > > > HERE AFTER SPLIT > > > > left half after split [26, 54] > > right half after split [17, 93] > > Merging [17, 26, 54, 93] > > Splitting [77, 31, 44, 55, 20] > > left half before split [77, 31] > > right half before split [44, 55, 20] > > Splitting [77, 31] > > left half before split [77] > > right half before split [31] > > Splitting [77] > > Merging [77] > > Splitting [31] > > Merging [31] > > > > HERE AFTER SPLIT > > > > left half after split [77] > > right half after split [31] > > Merging [31, 77] > > Splitting [44, 55, 20] > > left half before split [44] > > right half before split [55, 20] > > Splitting [44] > > Merging [44] > > Splitting [55, 20] > > left half before split [55] > > right half before split [20] > > Splitting [55] > > Merging [55] > > Splitting [20] > > Merging [20] > > > > HERE AFTER SPLIT > > > > left half after split [55] > > right half after split [20] > > Merging [20, 55] > > > > HERE AFTER SPLIT > > > > left half after split [44] > > right half after split [20, 55] > > Merging [20, 44, 55] > > > > HERE AFTER SPLIT > > > > left half after split [31, 77] > > right half after split [20, 44, 55] > > Merging [20, 31, 44, 55, 77] > > > > HERE AFTER SPLIT > > > > left half after split [17, 26, 54, 93] > > right half after split [20, 31, 44, 55, 77] > > Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] > > > > after calling main mergesort > > > > [17, 20, 26, 31, 44, 54, 55, 77, 93] > > ------------------------------------------------------- > > In the above output, the control goes to "HERE AFTER SPLIT" after the "Merging" statement which is of-course the last statement in the function.On what condition this is happening. > > Ideally as per my understanding, after the last statement of a function the control should come out of the function. > > Please correct me if I am wrong here. > > There is something with respect-to functions in python that I am not able to understand. From davea at davea.name Thu May 30 09:29:52 2013 From: davea at davea.name (Dave Angel) Date: Thu, 30 May 2013 09:29:52 -0400 Subject: Can anyone please help me in understanding the following python code In-Reply-To: References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> Message-ID: <51A75450.6090903@davea.name> On 05/30/2013 08:42 AM, bhk755 at gmail.com wrote: >> >> In the above output, the control goes to "HERE AFTER SPLIT" after the "Merging" statement which is of-course the last statement in the function.On what condition this is happening. >> >> Ideally as per my understanding, after the last statement of a function the control should come out of the function. >> >> Please correct me if I am wrong here. >> >> There is something with respect-to functions in python that I am not able to understand. > I think you misunderstand recursion. And you should study it in a simpler example before trying to tackle that sort function. After the last statement of a function, or after an explicit return statement, the function does indeed return control to whoever called it. And if the call was in the middle of some other function, that's the place where execution will continue. Functions may nest this way to pretty large depths of complexity. If you're not sure you understand that, we should stop here. So in your reply, tell me if you can readily grasp function dfunc() calling cfunc(), which calls bfunc(), which calls afunc(). When afunc() returns, you'll be right in the middle of bfunc(), right after the call was made. Now to recursion. One can write a function which solves a particular problem by doing some of the work, but by calling on other functions which solve simpler subproblems. Recursion comes in when those other functions are simply instances of the same one. So instead of dfunc() calling cfunc(), we have rfunc() calling rfunc() calling rfunc() calling rfunc(). Let's take a classic problem for explaining recursion: factorial. We can write a simple function that solves the problem for 0: def afunc(n): if n == 0: return 1 else: throw-some-exception Silly function, I know. Now let's write one that solves it for one. def bfunc(n): if n == 1: return 1 * afunc(n-1) else: throw-some-exception Now for two: def bfunc(n): if n == 2: return 2 * bfunc(n-1) else: throw-some-exception Notice that each function calls the one above it to solve the "simpler" problem. Now if we wanted this to work for n=100, it would get really tedious. So let's see if we can write one function that handles all of these cases. def rfunc(n): if n == 0: return 1 else: return n * rfunc(n-1) Now if we call this with n==3, it'll make the zero check, then it'll call another function with the value 2. that one will in turn call another function with the value 1. And so on. So this function calls itself, as we say recursively. Now, for this simple case, we could have used a simple loop, or just called the library function. But it's a simple enough example to follow in its entirety (if I've been clear in my writing). Your mergeSort() function calls itself recursively, and each time it does, it passes a *smaller* list to be sorted. Eventually the calls recurse down to a list of size 1, which is already sorted by definition. The check for that is the line if len(alist)>1: near the beginning of the function. That's analogous to my if n==0 line, although to match it most directly, I could have reversed the if/else and written the test as if n!=0 Chris showed you how to change your output so you could see the nesting levels. I have done that sort of thing in the past, and found it very useful. But first you have to understand nested functions, and simple recursion. -- DaveA From bhk755 at gmail.com Thu May 30 08:42:46 2013 From: bhk755 at gmail.com (bhk755 at gmail.com) Date: Thu, 30 May 2013 05:42:46 -0700 (PDT) Subject: Can anyone please help me in understanding the following python code In-Reply-To: <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> Message-ID: <371ae93a-bc1e-4e79-910d-c7225c35cd9e@googlegroups.com> On Thursday, May 30, 2013 6:09:20 PM UTC+5:30, bhk... at gmail.com wrote: > Thanks Chris, Wolfgang and Joshua for your replies. > > > > --- > > In step 2b, all the steps from 1 through 3 are executed again (twice). > > Soon, those calls will just output "Splitting" followed by "Merging"; > > and then we go back to 2c. That's why it *seems* that the code goes > > from 3 to 2c. You'll notice that the call depth decreases when this > > happens > > > > Chris, Can you please let me know what makes the control of the program code go to 2c after the output "Merging". > > Also, please look into the following output for the same program with more print statements, > ---------------------------------------------- > before calling main mergesort > > Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] > > left half before split [54, 26, 93, 17] > > right half before split [77, 31, 44, 55, 20] > > Splitting [54, 26, 93, 17] > > left half before split [54, 26] > > right half before split [93, 17] > > Splitting [54, 26] > > left half before split [54] > > right half before split [26] > > Splitting [54] > > Merging [54] > > Splitting [26] > > Merging [26] > > > > HERE AFTER SPLIT > > > > left half after split [54] > > right half after split [26] > > Merging [26, 54] > > Splitting [93, 17] > > left half before split [93] > > right half before split [17] > > Splitting [93] > > Merging [93] > > Splitting [17] > > Merging [17] > > > > HERE AFTER SPLIT > > > > left half after split [93] > > right half after split [17] > > Merging [17, 93] > > > > HERE AFTER SPLIT > > > > left half after split [26, 54] > > right half after split [17, 93] > > Merging [17, 26, 54, 93] > > Splitting [77, 31, 44, 55, 20] > > left half before split [77, 31] > > right half before split [44, 55, 20] > > Splitting [77, 31] > > left half before split [77] > > right half before split [31] > > Splitting [77] > > Merging [77] > > Splitting [31] > > Merging [31] > > > > HERE AFTER SPLIT > > > > left half after split [77] > > right half after split [31] > > Merging [31, 77] > > Splitting [44, 55, 20] > > left half before split [44] > > right half before split [55, 20] > > Splitting [44] > > Merging [44] > > Splitting [55, 20] > > left half before split [55] > > right half before split [20] > > Splitting [55] > > Merging [55] > > Splitting [20] > > Merging [20] > > > > HERE AFTER SPLIT > > > > left half after split [55] > > right half after split [20] > > Merging [20, 55] > > > > HERE AFTER SPLIT > > > > left half after split [44] > > right half after split [20, 55] > > Merging [20, 44, 55] > > > > HERE AFTER SPLIT > > > > left half after split [31, 77] > > right half after split [20, 44, 55] > > Merging [20, 31, 44, 55, 77] > > > > HERE AFTER SPLIT > > > > left half after split [17, 26, 54, 93] > > right half after split [20, 31, 44, 55, 77] > > Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] > > > > after calling main mergesort > > > > [17, 20, 26, 31, 44, 54, 55, 77, 93] > > ------------------------------------------------------- > > In the above output, the control goes to "HERE AFTER SPLIT" after the "Merging" statement which is of-course the last statement in the function.On what condition this is happening. > > Ideally as per my understanding, after the last statement of a function the control should come out of the function. > > Please correct me if I am wrong here. > > There is something with respect-to functions in python that I am not able to understand. From rosuav at gmail.com Thu May 30 08:55:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 22:55:38 +1000 Subject: Can anyone please help me in understanding the following python code In-Reply-To: <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> Message-ID: On Thu, May 30, 2013 at 10:39 PM, wrote: > Chris, Can you please let me know what makes the control of the program code go to 2c after the output "Merging". It goes like this: 1. [eight element list] 2a. [eight element list] 2b. 1. [four element list] 2b. 2a. [four element list] 2b. 2b. 1. [two element list] 2b. 2b. 2a. [two element list] 2b. 2b. 2b. [two element list] 2b. 2b. 2b. 1. [one element list] 2b. 2b. 2b. 2. [one element list] 2b. 2b. 2b. 3. [one element list] 2b. 2b. 2c. [two element list] 2b. 2b. 3. [two element list] ... etc etc etc ... Notice how control actually flows from 1, to 2a, to 2b, to 2c, but in between, it goes back to 1? That's recursion. There are four separate function calls here, which you can see going down the page. Each time it gets to step 2b, a whole new "thread" starts, and the previous one doesn't do anything till the inner one reaches step 3 and finishes. That's why the indented output can help; eyeball them going down and you'll see that they don't do anything until the inner one finishes. ChrisA From bhk755 at gmail.com Thu May 30 09:01:02 2013 From: bhk755 at gmail.com (bhk755 at gmail.com) Date: Thu, 30 May 2013 06:01:02 -0700 (PDT) Subject: Can anyone please help me in understanding the following python code In-Reply-To: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: <4cfe27df-b35c-4c3f-a22c-93b29ee26462@googlegroups.com> Thanks Chris, Wolfgang and Joshua for your replies. ------------------------ In step 2b, all the steps from 1 through 3 are executed again (twice). Soon, those calls will just output "Splitting" followed by "Merging"; and then we go back to 2c. That's why it *seems* that the code goes from 3 to 2c. You'll notice that the call depth decreases when this happens Chris, Can you please let me know what makes the control of the program code go to 2c after the output "Merging". Also, please look into the following output for the same program with more print statements, ---------------------------------------------- before calling main mergesort Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] left half before split [54, 26, 93, 17] right half before split [77, 31, 44, 55, 20] Splitting [54, 26, 93, 17] left half before split [54, 26] right half before split [93, 17] Splitting [54, 26] left half before split [54] right half before split [26] Splitting [54] Merging [54] Splitting [26] Merging [26] HERE AFTER SPLIT left half after split [54] right half after split [26] Merging [26, 54] Splitting [93, 17] left half before split [93] right half before split [17] Splitting [93] Merging [93] Splitting [17] Merging [17] HERE AFTER SPLIT left half after split [93] right half after split [17] Merging [17, 93] HERE AFTER SPLIT left half after split [26, 54] right half after split [17, 93] Merging [17, 26, 54, 93] Splitting [77, 31, 44, 55, 20] left half before split [77, 31] right half before split [44, 55, 20] Splitting [77, 31] left half before split [77] right half before split [31] Splitting [77] Merging [77] Splitting [31] Merging [31] HERE AFTER SPLIT left half after split [77] right half after split [31] Merging [31, 77] Splitting [44, 55, 20] ft half before split [44] ght half before split [55, 20] Splitting [44] Merging [44] Splitting [55, 20] eft half before split [55] ight half before split [20] Splitting [55] Merging [55] Splitting [20] Merging [20] HERE AFTER SPLIT left half after split [55] right half after split [20] Merging [20, 55] HERE AFTER SPLIT left half after split [44] right half after split [20, 55] Merging [20, 44, 55] HERE AFTER SPLIT left half after split [31, 77] right half after split [20, 44, 55] Merging [20, 31, 44, 55, 77] HERE AFTER SPLIT left half after split [17, 26, 54, 93] right half after split [20, 31, 44, 55, 77] Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] after calling main mergesort [17, 20, 26, 31, 44, 54, 55, 77, 93] ------------------------------------------------------- In the above output, the control goes to "HERE AFTER SPLIT" after the "Merging" statement which is of-course the last statement in the function.On what condition this is happening. Ideally as per my understanding, after the last statement of a function the control should come out of the function. Please correct me if I am wrong here. There is something with respect-to functions in python that I am not able to understand. From bhk755 at gmail.com Fri May 31 00:53:10 2013 From: bhk755 at gmail.com (bhk755 at gmail.com) Date: Thu, 30 May 2013 21:53:10 -0700 (PDT) Subject: Can anyone please help me in understanding the following python code In-Reply-To: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: Got It!!!, Finally. Thanks Dave So, the control goes back to the place after the recursive function is called once the no. of element is equal to one and starts merging after which it will again start to split the remaining items. Thank you Chris for your multiple explanations. It was also very useful. One final question, Is there a way to edit the message once it has been posted? From bhk755 at gmail.com Fri May 31 00:54:35 2013 From: bhk755 at gmail.com (bhk755 at gmail.com) Date: Thu, 30 May 2013 21:54:35 -0700 (PDT) Subject: Can anyone please help me in understanding the following python code In-Reply-To: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Message-ID: Got It!!!, Finally. Thanks Dave So, the control goes back to the place after the recursive function is called once the no. of element is equal to one and starts merging after which it will again start to split the remaining items. Thank you Chris for your multiple explanations. One final question, Is there a way to edit the message once it has been posted? From cs at zip.com.au Fri May 31 01:43:17 2013 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 31 May 2013 15:43:17 +1000 Subject: Can anyone please help me in understanding the following python code In-Reply-To: References: Message-ID: <20130531054317.GA48344@cskk.homeip.net> On 30May2013 21:54, bhk755 at gmail.com wrote: | One final question, Is there a way to edit the message once it has been posted? Essentially, no. If there's some error in a post, reply to it yourself with a correction. Transparency is a good thing. Revisionist history pretty much is not. -- Cameron Simpson Well, it's one louder, isn't it? It's not ten. You see, most blokes are gonna be playing at ten, you're on ten here, all the way up, all the way up, all the way up, you're on ten on your guitar, where can you go from there? Where? Nowhere, exactly. What we do is, if we need that extra push over the cliff, you know what we do? Eleven. Exactly. One louder. - Nigel Tufnel, _This Is Spinal Tap_ From rosuav at gmail.com Fri May 31 03:13:17 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 31 May 2013 17:13:17 +1000 Subject: Can anyone please help me in understanding the following python code In-Reply-To: <20130531054317.GA48344@cskk.homeip.net> References: <20130531054317.GA48344@cskk.homeip.net> Message-ID: On Fri, May 31, 2013 at 3:43 PM, Cameron Simpson wrote: > On 30May2013 21:54, bhk755 at gmail.com wrote: > | One final question, Is there a way to edit the message once it has been posted? > > Essentially, no. If there's some error in a post, reply to it > yourself with a correction. Transparency is a good thing. Revisionist > history pretty much is not. Once your email or newsgroup post is sent, assume that it's also been received; at very least, you won't be far wrong. You'll sometimes see a response within minutes from someone who saw your post within seconds. ChrisA From eternaltheft at gmail.com Thu May 30 07:33:41 2013 From: eternaltheft at gmail.com (Eternaltheft) Date: Thu, 30 May 2013 04:33:41 -0700 (PDT) Subject: User Input Message-ID: Hi, I'm having trouble oh how prompt the user to enter a file name and how to set up conditions. For example, if there's no file name input by the user, a default is returned From fabiosantosart at gmail.com Thu May 30 07:45:43 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 30 May 2013 12:45:43 +0100 Subject: User Input In-Reply-To: References: Message-ID: On 30 May 2013 12:42, "Eternaltheft" wrote: > > Hi, I'm having trouble oh how prompt the user to enter a file name and how to set up conditions. For example, if there's no file name input by the user, a default is returned Are you using raw_input? It returns an empty string if the user enters nothing, so you can just use an if. filename = raw_input('file name: ') if not filename: filename = 'your default' -------------- next part -------------- An HTML attachment was scrubbed... URL: From eternaltheft at gmail.com Thu May 30 07:48:56 2013 From: eternaltheft at gmail.com (Eternaltheft) Date: Thu, 30 May 2013 04:48:56 -0700 (PDT) Subject: User Input In-Reply-To: References: Message-ID: <7f9b7a2e-3142-468b-a2ce-9434c1ad7989@googlegroups.com> On Thursday, May 30, 2013 7:33:41 PM UTC+8, Eternaltheft wrote: > Hi, I'm having trouble oh how prompt the user to enter a file name and how to set up conditions. For example, if there's no file name input by the user, a default is returned Thanks for such a fast reply! and no im not using raw input, im just using input. does raw_input work on python 3? From rosuav at gmail.com Thu May 30 08:06:41 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 22:06:41 +1000 Subject: User Input In-Reply-To: <7f9b7a2e-3142-468b-a2ce-9434c1ad7989@googlegroups.com> References: <7f9b7a2e-3142-468b-a2ce-9434c1ad7989@googlegroups.com> Message-ID: On Thu, May 30, 2013 at 9:48 PM, Eternaltheft wrote: > On Thursday, May 30, 2013 7:33:41 PM UTC+8, Eternaltheft wrote: >> Hi, I'm having trouble oh how prompt the user to enter a file name and how to set up conditions. For example, if there's no file name input by the user, a default is returned > > Thanks for such a fast reply! and no im not using raw input, im just using input. does raw_input work on python 3? No, on Python 3 just use input. In Python 2, input() is a dangerous function that evaluates the entered text and raw_input() is the safe one; in Python 3, raw_input got renamed to input(). Go ahead and use input the way Fabio used raw_input. By the way, there's a handy compact notation for what Fabio wrote: filename = raw_input('file name: ') or 'your default' Can be very handy! ChrisA From python at mrabarnett.plus.com Thu May 30 08:08:25 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 May 2013 13:08:25 +0100 Subject: User Input In-Reply-To: <7f9b7a2e-3142-468b-a2ce-9434c1ad7989@googlegroups.com> References: <7f9b7a2e-3142-468b-a2ce-9434c1ad7989@googlegroups.com> Message-ID: <51A74139.1020508@mrabarnett.plus.com> On 30/05/2013 12:48, Eternaltheft wrote: > On Thursday, May 30, 2013 7:33:41 PM UTC+8, Eternaltheft wrote: >> Hi, I'm having trouble oh how prompt the user to enter a file name >> and how to set up conditions. For example, if there's no file name >> input by the user, a default is returned > > Thanks for such a fast reply! and no im not using raw input, im just > using input. does raw_input work on python 3? > In Python 2 it's called "raw_input" and in Python 3 it's called "input". Python 2 does have a function called "input", but it's not recommended (it's dangerous because it's equivalent to "eval(raw_input())", which will evaluate _whatever_ is entered). From fabiosantosart at gmail.com Thu May 30 08:10:53 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 30 May 2013 13:10:53 +0100 Subject: User Input In-Reply-To: <7f9b7a2e-3142-468b-a2ce-9434c1ad7989@googlegroups.com> References: <7f9b7a2e-3142-468b-a2ce-9434c1ad7989@googlegroups.com> Message-ID: On 30 May 2013 12:58, "Eternaltheft" wrote: > > On Thursday, May 30, 2013 7:33:41 PM UTC+8, Eternaltheft wrote: > > Hi, I'm having trouble oh how prompt the user to enter a file name and how to set up conditions. For example, if there's no file name input by the user, a default is returned > > Thanks for such a fast reply! and no im not using raw input, im just using input. does raw_input work on python 3? On python 2, the function to prompt the user for input and return a string is raw_input. On python 3 that function has been renamed to input. However on python 2 input is something else. It also evaluates the input as a python expression. That makes it unsafe to use in most circumstances. -------------- next part -------------- An HTML attachment was scrubbed... URL: From eternaltheft at gmail.com Thu May 30 08:19:08 2013 From: eternaltheft at gmail.com (Eternaltheft) Date: Thu, 30 May 2013 05:19:08 -0700 (PDT) Subject: User Input In-Reply-To: References: Message-ID: Ok thanks guys. but when i use filename = input('file name: ') if not filename: #i get filename is not defined return(drawBoard) #possible to return function when no file input from user? From fabiosantosart at gmail.com Thu May 30 08:27:29 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 30 May 2013 13:27:29 +0100 Subject: User Input In-Reply-To: References: Message-ID: On 30 May 2013 13:24, "Eternaltheft" wrote: > > Ok thanks guys. but when i use > > filename = input('file name: ') > if not filename: #i get filename is not defined > return(drawBoard) #possible to return function when no file input from user? I don't really understand what you mean. Do you mean that you're getting a stack trace? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu May 30 08:28:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 22:28:39 +1000 Subject: User Input In-Reply-To: References: Message-ID: On Thu, May 30, 2013 at 10:19 PM, Eternaltheft wrote: > Ok thanks guys. but when i use > > filename = input('file name: ') > if not filename: #i get filename is not defined > return(drawBoard) #possible to return function when no file input from user? Do you really want to return there? What function is this defined in? I think you probably want what Fabio originally said: that "if not filename" (which, in this context, means "if the user hit Enter without typing anything"), assign something to filename. ChrisA From eternaltheft at gmail.com Thu May 30 08:37:31 2013 From: eternaltheft at gmail.com (Eternaltheft) Date: Thu, 30 May 2013 05:37:31 -0700 (PDT) Subject: User Input In-Reply-To: References: Message-ID: <864c9d49-9929-43dd-b484-d6be92564cc3@googlegroups.com> sorry about that, i got confused xD. yeah it works good now. what i meant to say was can i return a function that i made, if the user inputs nothing? From davea at davea.name Thu May 30 08:58:32 2013 From: davea at davea.name (Dave Angel) Date: Thu, 30 May 2013 08:58:32 -0400 Subject: User Input In-Reply-To: <864c9d49-9929-43dd-b484-d6be92564cc3@googlegroups.com> References: <864c9d49-9929-43dd-b484-d6be92564cc3@googlegroups.com> Message-ID: <51A74CF8.2030708@davea.name> On 05/30/2013 08:37 AM, Eternaltheft wrote: > sorry about that, i got confused xD. yeah it works good now. > what i meant to say was can i return a function that i made, if the user inputs nothing? > There wouldn't be anything to stop you. However, if you have multiple returns from the same function, it's usually wise to return the same type of information from each of them. That's why Chris suggested simply assigning to filename in the if clause. But without the whole function, and maybe even a description of what the function is expected to do, we can only guess. Your comments still make no sense to me, > filename = input('file name: ') > if not filename: #i get filename is not defined But filename IS defined, immediately above. If it were undefined, you'd not be able to test it here. Big difference between "not defined" and "is empty string". > return(drawBoard) #possible to return function when no file input from user? If drawBoard is a function object, it's certainly possible to return it. But again, without seeing the rest of the function, and maybe how it's intended to be used, I can't confirm whether it makes sense. -- DaveA From rosuav at gmail.com Thu May 30 08:59:42 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 30 May 2013 22:59:42 +1000 Subject: User Input In-Reply-To: <864c9d49-9929-43dd-b484-d6be92564cc3@googlegroups.com> References: <864c9d49-9929-43dd-b484-d6be92564cc3@googlegroups.com> Message-ID: On Thu, May 30, 2013 at 10:37 PM, Eternaltheft wrote: > sorry about that, i got confused xD. yeah it works good now. > what i meant to say was can i return a function that i made, if the user inputs nothing? Sure! Anything you want to do, you can do :) ChrisA From eternaltheft at gmail.com Thu May 30 09:10:22 2013 From: eternaltheft at gmail.com (Eternaltheft) Date: Thu, 30 May 2013 06:10:22 -0700 (PDT) Subject: User Input In-Reply-To: References: Message-ID: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> yeah i found out why it wasn't defined before because i tried to put it into a function. this is my drawBoard function: import turtle as Turtle Turtle.title("Checkers") b = 75 def drawBoard(b): Turtle.speed(0) Turtle.up() Turtle.goto(-4 * b, 4 * b) Turtle.down() for i in range (8): Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) for i in range (1): Turtle.right(90) Turtle.forward(b*2) Turtle.right(90) for i in range(8): Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) for i in range(1): Turtle.left(90) Turtle.forward(b) Turtle.left(90) for i in range (8): Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) for i in range (1): Turtle.right(90) Turtle.forward(b) Turtle.right(90) for i in range(8): Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) for i in range(1): Turtle.left(90) Turtle.forward(b) Turtle.left(90) for i in range (8): Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) for i in range (1): Turtle.right(90) Turtle.forward(b) Turtle.right(90) for i in range(8): Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) for i in range(1): Turtle.left(90) Turtle.forward(b) Turtle.left(90) for i in range (8): Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) Turtle.left(90) Turtle.forward(b) for i in range (1): Turtle.right(90) Turtle.forward(b) Turtle.right(90) for i in range(8): Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) Turtle.right(90) Turtle.forward(b) drawBoard(b) Turtle.done() it draws an 8x8 board table. what i initially wanted to do was to return this function if nothing was inputted from the user. i hope that makes more sense :S From davea at davea.name Thu May 30 09:37:56 2013 From: davea at davea.name (Dave Angel) Date: Thu, 30 May 2013 09:37:56 -0400 Subject: User Input In-Reply-To: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> References: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> Message-ID: <51A75634.3080505@davea.name> On 05/30/2013 09:10 AM, Eternaltheft wrote: > yeah i found out why it wasn't defined before because i tried to put it into a function. That's not a sentence, and it doesn't make sense in any permutation I can do on it. > > this is my drawBoard function: > > import turtle as Turtle > Turtle.title("Checkers") > b = 75 > > def drawBoard(b): > > Turtle.speed(0) > > Turtle.up() > > Turtle.goto(-4 * b, 4 * b) > Turtle.down() > > for i in range (8): > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > > for i in range (1): > Turtle.right(90) > Turtle.forward(b*2) > Turtle.right(90) > > for i in range(8): > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > > for i in range(1): > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > > for i in range (8): > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > > for i in range (1): > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > > for i in range(8): > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > > for i in range(1): > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > > for i in range (8): > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > > for i in range (1): > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > > for i in range(8): > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > > for i in range(1): > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > > for i in range (8): > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > Turtle.left(90) > Turtle.forward(b) > > for i in range (1): > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > > for i in range(8): > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > Turtle.right(90) > Turtle.forward(b) > > > drawBoard(b) > > Turtle.done() > > it draws an 8x8 board table. > > what i initially wanted to do was to return this function if nothing was inputted from the user. i hope that makes more sense :S > It makes sense if you're also returning a function object when the user does have something to say. But I can't see how you might be doing that, unless you're using a lambda to make up a custom function object. And you still don't show us the function that contains this input statement. Nor how it gets used. Is the user supposed to supply a value for b, and you put it in a variable called filename ? And perhaps you meant for your function to CALL drawBoard(), rather than returning the function object drawBoard. -- DaveA From eternaltheft at gmail.com Thu May 30 10:03:31 2013 From: eternaltheft at gmail.com (Eternaltheft) Date: Thu, 30 May 2013 07:03:31 -0700 (PDT) Subject: User Input In-Reply-To: References: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> Message-ID: do you think ti would be better if i call drawBoard? From davea at davea.name Thu May 30 10:14:01 2013 From: davea at davea.name (Dave Angel) Date: Thu, 30 May 2013 10:14:01 -0400 Subject: User Input In-Reply-To: References: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> Message-ID: <51A75EA9.2080204@davea.name> On 05/30/2013 10:03 AM, Eternaltheft wrote: > do you think ti would be better if i call drawBoard? > Better is meaningless without context. Are you being charged per keystroke? -- DaveA From breamoreboy at yahoo.co.uk Thu May 30 10:30:44 2013 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 30 May 2013 15:30:44 +0100 Subject: User Input In-Reply-To: References: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> Message-ID: On 30/05/2013 15:03, Eternaltheft wrote: > do you think ti would be better if i call drawBoard? > How would I know if you don't quote any context? -- If you're using GoogleCrap? please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence From eternaltheft at gmail.com Thu May 30 10:49:14 2013 From: eternaltheft at gmail.com (Eternaltheft) Date: Thu, 30 May 2013 07:49:14 -0700 (PDT) Subject: User Input In-Reply-To: References: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> Message-ID: > And perhaps you meant for your function to CALL drawBoard(), rather than > returning the function object drawBoard. > DaveA do you think it would be better if i call drawBoard? From eternaltheft at gmail.com Thu May 30 10:47:32 2013 From: eternaltheft at gmail.com (Eternaltheft) Date: Thu, 30 May 2013 07:47:32 -0700 (PDT) Subject: User Input In-Reply-To: References: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> Message-ID: <7064d3fc-7641-4ecd-9fe0-9631e949fe72@googlegroups.com> > And perhaps you meant for your function to CALL drawBoard(), rather than > > returning the function object drawBoard. > > DaveA do you think it would be better if i call drawBoard? From joshua.landau.ws at gmail.com Thu May 30 11:09:12 2013 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 30 May 2013 16:09:12 +0100 Subject: User Input In-Reply-To: <7064d3fc-7641-4ecd-9fe0-9631e949fe72@googlegroups.com> References: <2eaa39b8-9e87-402a-b0c0-8c8c5d42fed7@googlegroups.com> <7064d3fc-7641-4ecd-9fe0-9631e949fe72@googlegroups.com> Message-ID: On 30 May 2013 15:47, Eternaltheft wrote: >> And perhaps you meant for your function to CALL drawBoard(), rather than >> returning the function object drawBoard. >> >> DaveA > > do you think it would be better if i call drawBoard? Please read http://www.catb.org/esr/faqs/smart-questions.html, or anything similar you can find. Start from the beginning. 1) What are you doing? Not "what are you doing now" but, from the top, what is the goal you are trying to achieve? 2) How have you tried to do it? Code would be nice here too, but don't just send really large blocks of irrelevant code. For example, your "drawBoard" function would be better surmised as: def drawBoard(b): Turtle.speed(0) Turtle.up() Turtle.goto(-4 * b, 4 * b) Turtle.down() for i in range (8): Turtle.forward(b) Turtle.right(90) ... # etc, drawing a board 3) What are you stuck on? In this case, you are stuck on what to do after you call input("stuff"), if I understand. What do you want to do - not "how do you want to do it" but what is it that you are doing? 4) Finally, we should understand what calling drawBoard is for. Ask us again and we'll be much more likely to give good answers. From nobody at nowhere.org Thu May 30 08:20:18 2013 From: nobody at nowhere.org (Franck Ditter) Date: Thu, 30 May 2013 14:20:18 +0200 Subject: Python toplevel in a Web page Message-ID: Hello, I wonder if I can find some source code example of a Python 3 toplevel box in a Web page. Something simple, no mySQL, no Django hammer, etc. Just the basics of the technology to get the content of a small text editor in which the user writes some Python script, to be analyzed (eval'ed) then whose result is to be written in another text box. Simple, pythonistic. Thanks for the pointer, franck From carlosnepomuceno at outlook.com Thu May 30 17:40:58 2013 From: carlosnepomuceno at outlook.com (Carlos Nepomuceno) Date: Fri, 31 May 2013 00:40:58 +0300 Subject: Python toplevel in a Web page In-Reply-To: References: Message-ID: ---------------------------------------- > From: nobody at nowhere.org > Subject: Python toplevel in a Web page > Date: Thu, 30 May 2013 14:20:18 +0200 > To: python-list at python.org > > Hello, > I wonder if I can find some source code example > of a Python 3 toplevel box in a Web page. > Something simple, no mySQL, no Django hammer, etc. > Just the basics of the technology to get the > content of a small text editor in which the user Do you mean HTML